diff options
author | Freya Murphy <freya@freyacat.org> | 2024-03-30 12:14:42 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-03-30 12:14:42 -0400 |
commit | 1f04b83be337cc91a3fabcf4e574e2306f3d2eaa (patch) | |
tree | 74d7d65a7047e60d1877384e3c7b0d70c7b0e49a /web/_model/main.php | |
parent | start database (user and post), and initial barebones home page (diff) | |
download | xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.gz xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.bz2 xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.zip |
refactor
Diffstat (limited to 'web/_model/main.php')
-rw-r--r-- | web/_model/main.php | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/web/_model/main.php b/web/_model/main.php new file mode 100644 index 0000000..f72a2f3 --- /dev/null +++ b/web/_model/main.php @@ -0,0 +1,84 @@ +<?php /* Copyright (c) 2024 Freya Murphy */ +class Main_model { + + // the website database + public $db; + + // the current user session (can be NULL) + public $session; + + // current loaded users + private $users; + + // stores the current request info + public $info; + + /** + * Loads the main model + * @param Loader $load - the main loader object + */ + function __construct($load) { + /// load the database helper + $this->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 + ); + } + } + + /** + * Loads a css html link + * @param string $path - the path to the css file + */ + public function link_css($path) { + return '<link rel="stylesheet" href="/public/' . $path . '">'; + } + + /** + * Loads a js html link + * @param string $path - the path to the js file + */ + public function link_js($path) { + return '<script src="/public/'. $path . '"></script>'; + } + + /** + * 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; + } + } + +} + +?> |