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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
<?php
namespace Wcms;
use DateTimeImmutable;
use DateTimeZone;
use RuntimeException;
class User extends Item
{
protected $id = '';
protected $level = 0;
protected $signature = '';
protected $password;
protected $passwordhashed = false;
protected $cookie = 0;
protected $columns = ['title', 'datemodif', 'datecreation', 'secure', 'visitcount'];
protected $connectcount = 0;
protected $expiredate = false;
/** @var Bookmark[] Associative array as `id => Bookmark`*/
protected $bookmark = [];
/** @var array sessions */
protected $sessions = [];
protected $display = ['bookmark' => false];
public function __construct($datas = [])
{
if (!empty($datas)) {
$this->hydrate($datas);
}
}
// _________________________ G E T _______________________
public function id()
{
return $this->id;
}
public function level()
{
return $this->level;
}
public function password()
{
return $this->password;
}
public function signature()
{
return $this->signature;
}
public function passwordhashed()
{
return $this->passwordhashed;
}
public function cookie()
{
return $this->cookie;
}
public function columns()
{
return $this->columns;
}
public function connectcount()
{
return $this->connectcount;
}
public function expiredate(string $type = 'string')
{
if ($type == 'string') {
if (!empty($this->expiredate)) {
return $this->expiredate->format('Y-m-d');
} else {
return false;
}
} elseif ($type == 'date') {
if (!empty($this->expiredate)) {
return $this->expiredate;
} else {
return false;
}
} elseif ($type == 'hrdi') {
if (empty($this->expiredate)) {
return 'never';
} else {
$now = new DateTimeImmutable("now", timezone_open("Europe/Paris"));
if ($this->expiredate < $now) {
return 'expired';
} else {
return hrdi($this->expiredate->diff($now));
}
}
}
}
public function bookmark()
{
return $this->bookmark;
}
public function sessions()
{
return $this->sessions;
}
public function display()
{
return $this->display;
}
// _______________________ S E T _______________________
public function setid($id): bool
{
if (is_string($id)) {
$id = idclean($id);
if (!empty($id)) {
$this->id = $id;
return true;
}
}
return false;
}
public function setlevel($level)
{
$level = intval($level);
if ($level >= 0 && $level <= 10) {
$this->level = $level;
return true;
} else {
return false;
}
}
/**
* @return bool if password is compatible and set, otherwise flase
*/
public function setpassword($password): bool
{
if (!empty($password) && is_string($password)) {
if (strlen($password) >= Model::PASSWORD_MIN_LENGTH && strlen($password) <= Model::PASSWORD_MAX_LENGTH) {
$this->password = $password;
return true;
}
}
return false;
}
public function setsignature(string $signature)
{
if (strlen($signature) <= 128) {
$this->signature = $signature;
}
}
public function setpasswordhashed($passwordhashed)
{
$this->passwordhashed = boolval($passwordhashed);
}
public function setcookie($cookie)
{
$cookie = intval($cookie);
if ($cookie <= Model::MAX_COOKIE_CONSERVATION && $cookie >= 0) {
$this->cookie = $cookie;
return true;
} else {
return false;
}
}
public function setcolumns($columns)
{
if (is_array($columns)) {
$columns = array_filter(array_intersect(array_unique($columns), Model::COLUMNS));
$this->columns = $columns;
}
}
public function setconnectcount($connectcount)
{
if (is_int($connectcount) && $connectcount >= 0) {
$this->connectcount = $connectcount;
}
}
public function setexpiredate($expiredate)
{
if ($expiredate instanceof DateTimeImmutable) {
$this->expiredate = $expiredate;
} else {
$this->expiredate = DateTimeImmutable::createFromFormat(
'Y-m-d',
$expiredate,
new DateTimeZone('Europe/Paris')
);
}
}
public function setbookmark($bookmark)
{
if (is_array($bookmark)) {
$bookmark = array_map(
function ($datas) {
if (is_array($datas)) {
try {
return new Bookmark($datas);
} catch (RuntimeException $e) {
return false;
}
} else {
return false;
}
},
$bookmark
);
$this->bookmark = array_filter($bookmark);
}
}
public function setsessions($sessions)
{
if (is_array($sessions)) {
$this->sessions = $sessions;
}
}
public function setdisplay($display)
{
if (is_array($display)) {
$this->display = $display;
}
}
//____________________________________________________ F U N ____________________________________________________
/**
* Hash the password and set `$passwordhashed` to true.
*
* @return bool true in cas of success, otherwise false.
*/
public function hashpassword(): bool
{
$hashedpassword = password_hash($this->password, PASSWORD_DEFAULT);
if (!empty($hashedpassword)) {
$this->password = $hashedpassword;
$this->passwordhashed = true;
return true;
} else {
return false;
}
}
public function validpassword()
{
if (is_string($this->password)) {
if (
strlen($this->password) >= Model::PASSWORD_MIN_LENGTH
&& strlen($this->password) <= Model::PASSWORD_MAX_LENGTH
) {
return true;
}
}
return false;
}
/**
* Generate new unique session ID
* @param string $info session info to store
* @return string session key
*/
public function newsession(string $info = "no_info"): string
{
$exist = true;
while ($exist === true) {
$session = bin2hex(random_bytes(10));
$exist = key_exists($session, $this->sessions());
}
$this->sessions[$session] = $info;
return $session;
}
/**
* Remove Session from user
* @param string $session session ID to remove
* @return bool true if session exist and was destroyed, false if key does not exist
*/
public function destroysession(string $session): bool
{
if (key_exists($session, $this->sessions)) {
unset($this->sessions[$session]);
return true;
} else {
return false;
}
}
public function checksession(string $session): bool
{
return key_exists($session, $this->sessions);
}
public function isvisitor()
{
return $this->level === Modeluser::FREE;
}
public function iseditor()
{
return $this->level >= Modeluser::EDITOR;
}
public function issupereditor()
{
return $this->level >= Modeluser::SUPEREDITOR;
}
public function isinvite()
{
return $this->level >= Modeluser::INVITE;
}
public function isadmin()
{
return $this->level === Modeluser::ADMIN;
}
public function connectcounter()
{
$this->connectcount ++;
}
public function addbookmark(Bookmark $bookmark)
{
if (!empty($bookmark->id()) && !empty($bookmark->query()) && !empty($bookmark->route())) {
$this->bookmark[$bookmark->id()] = $bookmark;
}
}
public function deletebookmark(string $id)
{
if (key_exists($id, $this->bookmark)) {
unset($this->bookmark[$id]);
}
}
}
|