From 3a82baec9d793edf81ac2b151b0f4d4159641375 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Mon, 1 Apr 2024 11:09:25 -0400 Subject: login and register, liking on homepage --- src/web/_model/apps/auth.php | 13 ++++++ src/web/_model/apps/error.php | 31 ++++++++++++++ src/web/_model/apps/home.php | 22 ++++++++++ src/web/_model/cache.php | 37 +++++++++++++++++ src/web/_model/format.php | 45 ++++++++++++++++++++ src/web/_model/main.php | 96 +++++++++++++++++++++++++++++++++++++++++++ src/web/_model/request.php | 40 ++++++++++++++++++ 7 files changed, 284 insertions(+) create mode 100644 src/web/_model/apps/auth.php create mode 100644 src/web/_model/apps/error.php create mode 100644 src/web/_model/apps/home.php create mode 100644 src/web/_model/cache.php create mode 100644 src/web/_model/format.php create mode 100644 src/web/_model/main.php create mode 100644 src/web/_model/request.php (limited to 'src/web/_model') diff --git a/src/web/_model/apps/auth.php b/src/web/_model/apps/auth.php new file mode 100644 index 0000000..a1802de --- /dev/null +++ b/src/web/_model/apps/auth.php @@ -0,0 +1,13 @@ +get_msg($data); + return $data; + } +} +?> diff --git a/src/web/_model/apps/home.php b/src/web/_model/apps/home.php new file mode 100644 index 0000000..82fbf26 --- /dev/null +++ b/src/web/_model/apps/home.php @@ -0,0 +1,22 @@ +db + ->select('*') + ->from('admin.post') + ->limit(20) + ->rows(); + } + + public function get_data(): array { + $data = parent::get_data(); + $data['title'] = lang('title'); + $data['posts'] = $this->get_posts(); + return $data; + } +} diff --git a/src/web/_model/cache.php b/src/web/_model/cache.php new file mode 100644 index 0000000..6cf9924 --- /dev/null +++ b/src/web/_model/cache.php @@ -0,0 +1,37 @@ +users = array(); + } + + /** + * Gets a array of users + */ + public function get_users($objs) { + $ids = array(); + foreach ($objs as $obj) { + $id = $obj['user_id']; + if (!array_key_exists($id, $this->users)) { + array_push($ids, intval($id)); + } + } + if (!empty($ids)) { + $result = $this->main->db + ->select('*') + ->from('api.user') + ->where_in('id', $ids) + ->rows(); + foreach ($result as $user) { + $id = $user['id']; + $this->users[$id] = $user; + } + } + return $this->users; + } + +} diff --git a/src/web/_model/format.php b/src/web/_model/format.php new file mode 100644 index 0000000..52b51be --- /dev/null +++ b/src/web/_model/format.php @@ -0,0 +1,45 @@ +db = new DatabaseHelper(); + /// load the current session + if (array_key_exists('jwt', $_SESSION)) { + $this->get_session($_SESSION['jwt']); + } else { + $this->session = NULL; + }; + /// init other vars + $this->users = array(); + } + + /** + * Loads current session + * @param string $jwt - the user provided JWT + */ + private function get_session($jwt) { + $query = $this->db + ->select("_api.verify_jwt('" . $jwt . "') AS user_id;"); + $result = $query->row(); + $user_id = $result['user_id']; + if ($user_id) { + $this->session = array( + 'id' => $user_id, + 'jwt' => $jwt + ); + } + } + + /** + * Gets the stamp for a asset path + * @param string $path + */ + private function asset_stamp($path): int { + $root = $GLOBALS['webroot']; + $path = $root . '/../public/' . $path; + return filemtime($path); + } + + /** + * Loads a css html link + * @param string $path - the path to the css file + */ + public function link_css($path) { + $stamp = $this->asset_stamp($path); + return ''; + } + + /** + * Loads a js html link + * @param string $path - the path to the js file + */ + public function link_js($path) { + $stamp = $this->asset_stamp($path); + return ''; + } + + /** + * Gets the current user + */ + public function user() { + if ($this->session) { + return $this->db + ->select('*') + ->from('api.user') + ->where('id') + ->eq($this->session['id']) + ->row(); + } else { + return NULL; + } + } + +} + +?> diff --git a/src/web/_model/request.php b/src/web/_model/request.php new file mode 100644 index 0000000..4cce07a --- /dev/null +++ b/src/web/_model/request.php @@ -0,0 +1,40 @@ +