1
|
|
|
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed'); |
2
|
|
|
|
3
|
|
|
class User_Model extends CI_Model { |
4
|
|
|
public $id; |
5
|
|
|
public $username; |
6
|
|
|
public $email; |
7
|
|
|
|
8
|
127 |
|
public function __construct() { |
9
|
127 |
|
parent::__construct(); |
10
|
|
|
|
11
|
|
|
//This needs to be set here as needs to be set before `logged_in` is called. |
12
|
127 |
|
if($remember = $this->input->cookie('remember_time')) { |
13
|
|
|
$this->set_user_expire_time($remember); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
//CHECK: Should this be placed elsewhere? |
17
|
127 |
|
if($this->logged_in()) { |
18
|
3 |
|
if(!$this->session->userdata('username')) { |
19
|
3 |
|
$this->session->set_userdata('username', $this->ion_auth->user()->row()->username); |
20
|
|
|
} |
21
|
3 |
|
if(!$this->session->userdata('email')) { |
22
|
|
|
//CHECK: This seems like a bad idea? |
23
|
3 |
|
$this->session->set_userdata('email', $this->ion_auth->user()->row()->email); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
127 |
|
$this->id = (int) $this->ion_auth->get_user_id(); |
28
|
127 |
|
$this->username = $this->session->userdata('username'); |
29
|
127 |
|
$this->email = $this->session->userdata('email'); |
30
|
127 |
|
} |
31
|
|
|
|
32
|
127 |
|
public function logged_in() : bool { |
33
|
127 |
|
return $this->ion_auth->logged_in(); |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
public function login_redirect() { |
37
|
8 |
|
if(!strpos(current_url(), '/import_list') && !strpos(current_url(), '/export_list')) { |
38
|
7 |
|
$this->session->set_flashdata('referred_from', current_url()); |
39
|
|
|
} |
40
|
|
|
//FIXME: We should handle the redirect here too, but it causes issues with tests |
41
|
|
|
//redirect('user/login'); |
42
|
8 |
|
} |
43
|
|
|
|
44
|
2 |
|
public function username_exists(string $username) : bool { |
45
|
2 |
|
$this->load->database(); |
46
|
|
|
|
47
|
2 |
|
$query = $this->db->select('*') |
48
|
2 |
|
->from('auth_users') |
49
|
2 |
|
->where('username', $username) |
50
|
2 |
|
->get(); |
51
|
|
|
|
52
|
2 |
|
return (bool) $query->num_rows(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $identity |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
4 |
|
public function find_email_from_identity(string $identity) { |
61
|
|
|
//login allows using email or username, but ion_auth doesn't support this |
62
|
|
|
//check if identity is email, and if not, try and find it |
63
|
|
|
//returns: email or FALSE |
64
|
|
|
//CHECK: How should we handle invalid emails being passed to this? |
65
|
4 |
|
$email = $identity; |
66
|
|
|
|
67
|
4 |
|
if(!strpos($identity, '@')) { |
68
|
|
|
//identity does not contain @, assume username |
69
|
2 |
|
$this->load->database(); |
70
|
|
|
|
71
|
2 |
|
$query = $this->db->select('email') |
72
|
2 |
|
->from('auth_users') |
73
|
2 |
|
->where('username', $identity) |
74
|
2 |
|
->get(); |
75
|
|
|
|
76
|
2 |
|
if($query->num_rows() > 0) { |
77
|
|
|
//username exists, grab email |
78
|
1 |
|
$email = $query->row('email'); |
79
|
|
|
}else{ |
80
|
|
|
//username doesn't exist, return FALSE |
81
|
1 |
|
$email = FALSE; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
return $email; |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
public function get_user_by_username(string $username) { |
89
|
5 |
|
$user = NULL; |
90
|
|
|
|
91
|
5 |
|
$query = $this->db->select('*') |
92
|
5 |
|
->from('auth_users') |
93
|
5 |
|
->where('username', $username) |
94
|
5 |
|
->get(); |
95
|
|
|
|
96
|
5 |
|
if($query->num_rows() > 0) { |
97
|
1 |
|
$user = $query->row(); |
98
|
|
|
} |
99
|
5 |
|
return $user; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function getGravatarURL($email = NULL, $size = NULL) : string { |
103
|
1 |
|
$email = $email ?? $this->email; |
104
|
|
|
//TODO: FIXME ON PROFILE PAGES |
105
|
1 |
|
return $this->gravatar->get($email, $size); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function get_new_api_key() : string { |
109
|
|
|
$api_key = NULL; |
110
|
|
|
if($this->logged_in()) { |
111
|
|
|
$api_key = substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", mt_rand(0, 51), 1) . substr(md5((string) time()), 1); |
112
|
|
|
|
113
|
|
|
$this->db->where('id', $this->id); |
114
|
|
|
$this->db->update('auth_users', ['api_key' => $api_key]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $api_key; |
118
|
|
|
} |
119
|
|
|
public function restore_api_key() : ?string { |
120
|
|
|
$api_key = NULL; |
121
|
|
|
if($this->logged_in()) { |
122
|
|
|
$this->db->select('api_key') |
123
|
|
|
->where('id', $this->User->id) |
124
|
|
|
->get('auth_users'); |
125
|
|
|
|
126
|
|
|
$query = $this->db->select('api_key') |
127
|
|
|
->where('id', $this->User->id) |
128
|
|
|
->get('auth_users'); |
129
|
|
|
|
130
|
|
|
if($query->num_rows() > 0) { |
131
|
|
|
$api_key = $query->row('api_key'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $api_key; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function get_id_from_api_key(string $api_key) { |
139
|
|
|
$query = $this->db->select('id') |
140
|
|
|
->from('auth_users') |
141
|
|
|
->where('api_key', $api_key) |
142
|
|
|
->get(); |
143
|
|
|
|
144
|
|
|
if($query->num_rows() > 0) { |
145
|
|
|
$userID = $query->row('id'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $userID ?? FALSE; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function set_user_expire_time($remember) : int { |
152
|
|
|
$expire_time = 0; |
153
|
|
|
switch($remember) { |
154
|
|
|
case '1day': |
|
|
|
|
155
|
|
|
$expire_time = 86400; |
156
|
|
|
break; |
157
|
|
|
case '3day': |
|
|
|
|
158
|
|
|
//This is default so do nothing. |
159
|
|
|
break; |
160
|
|
|
case '1week': |
161
|
|
|
$expire_time = 604800; |
162
|
|
|
break; |
163
|
|
|
case '1month': |
164
|
|
|
$expire_time = 2419200; |
165
|
|
|
break; |
166
|
|
|
case '3month': |
167
|
|
|
$expire_time = 7257600; |
168
|
|
|
break; |
169
|
|
|
default: |
170
|
|
|
//Somehow remember_time isn't set? |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
if($expire_time > 0) { |
174
|
|
|
$this->config->set_item_by_index('user_expire', $expire_time, 'ion_auth'); |
175
|
|
|
} |
176
|
|
|
return $expire_time; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** NOTICES **/ |
180
|
|
|
|
181
|
|
|
public function getLatestNotice() { |
182
|
|
|
$query = $this->db |
183
|
|
|
->select('tn.notice, DATE_FORMAT(tn.created_at, "%Y/%m/%d") AS date_formatted') |
184
|
|
|
->from('tracker_notices AS tn') |
185
|
|
|
->where("id > IFNULL((SELECT hidden_notice_id FROM tracker_user_notices WHERE user_id = {$this->User->id}), '0')", NULL, FALSE) |
186
|
|
|
->order_by('tn.id DESC') |
187
|
|
|
->limit(1) |
188
|
|
|
->get(); |
189
|
|
|
|
190
|
|
|
$noticeData = []; |
191
|
|
|
if($query->num_rows() > 0) { |
192
|
|
|
$row = $query->row(); |
193
|
|
|
|
194
|
|
|
$noticeData = [ |
195
|
|
|
'date' => $row->date_formatted, |
196
|
|
|
'text' => $this->Parsedown->text($row->notice) |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $noticeData; |
201
|
|
|
} |
202
|
|
|
public function hideLatestNotice() { |
203
|
|
|
$idQuery = $this->db->select('1') |
204
|
|
|
->where('user_id', $this->User->id) |
205
|
|
|
->get('tracker_user_notices'); |
206
|
|
|
if($idQuery->num_rows() > 0) { |
207
|
|
|
$success = (bool) $this->db->set('hidden_notice_id', '(SELECT id FROM tracker_notices ORDER BY id DESC LIMIT 1)', FALSE) |
208
|
|
|
->where('user_id', $this->User->id) |
209
|
|
|
->update('tracker_user_notices'); |
210
|
|
|
} else { |
211
|
|
|
$success = (bool) $this->db->insert('tracker_user_notices', [ |
212
|
|
|
'user_id' => $this->User->id, |
213
|
|
|
'hidden_notice_id' => '(SELECT id FROM tracker_notices ORDER BY id DESC LIMIT 1)' |
214
|
|
|
], FALSE); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $success; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.