blob: 58ae307c38dc811466574fdbc9fd67f7b7385afe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
<?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;
// tthe logged in user
private $user;
/**
* 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();
$this->user = NULL;
}
/**
* Loads current session
* @param string $jwt - the user provided JWT
*/
private function get_session($jwt) {
$query = $this->db
->select("_api.verify_jwt(?) AS user_id;");
$result = $query->row($jwt);
$user_id = $result['user_id'];
if ($user_id) {
$this->session = array(
'id' => $user_id,
'jwt' => $jwt
);
$user = $this->user();
if ($user === FALSE) {
/// valid jwt for invalid user!!!
$this->session = NULL;
$this->user = NULL;
}
}
}
/**
* 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 '<link rel="stylesheet" href="/public/' . $path . '?stamp=' . $stamp . '">';
}
/**
* 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 '<script src="/public/'. $path . '?stamp=' . $stamp . '"></script>';
}
/**
* Gets the current user
*/
public function user() {
if ($this->user) {
return $this->user;
}
if ($this->session) {
$this->user = $this->db
->select('*')
->from('api.user')
->where('id')
->eq($this->session['id'])
->row();
return $this->user;
}
return NULL;
}
}
?>
|