2024-03-30 16:14:42 +00:00
|
|
|
<?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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 01:28:46 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:14:42 +00:00
|
|
|
/**
|
|
|
|
* Loads a css html link
|
|
|
|
* @param string $path - the path to the css file
|
|
|
|
*/
|
|
|
|
public function link_css($path) {
|
2024-03-31 01:28:46 +00:00
|
|
|
$stamp = $this->asset_stamp($path);
|
|
|
|
return '<link rel="stylesheet" href="/public/' . $path . '?stamp=' . $stamp . '">';
|
2024-03-30 16:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a js html link
|
|
|
|
* @param string $path - the path to the js file
|
|
|
|
*/
|
|
|
|
public function link_js($path) {
|
2024-03-31 01:28:46 +00:00
|
|
|
$stamp = $this->asset_stamp($path);
|
|
|
|
return '<script src="/public/'. $path . '?stamp=' . $stamp . '"></script>';
|
2024-03-30 16:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|