Passed
Push — master ( bb15a1...9668fd )
by Justin
46:19 queued 42:20
created
system/packages/com.jukusoft.cms.user/classes/user.php 1 patch
Indentation   +600 added lines, -600 removed lines patch added patch discarded remove patch
@@ -27,614 +27,614 @@
 block discarded – undo
27 27
 
28 28
 class User {
29 29
 
30
-	//instance of current (logged-in / guest) user
31
-	protected static $instance = null;
32
-
33
-	//current userID
34
-	protected $userID = -1;
35
-
36
-	//current username
37
-	protected $username = "Guest";
38
-
39
-	//flag, if user is logged in
40
-	protected $isLoggedIn = false;
41
-
42
-	//current database row
43
-	protected $row = null;
44
-
45
-	protected static $default_authentificator = null;
46
-
47
-	public function __construct() {
48
-		//
49
-	}
50
-
51
-	public function load (int $userID = -1) {
52
-		//check, if user is logged in
53
-		if ($userID === -1) {
54
-			if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) {
55
-				if (!isset($_SESSION['userID']) || empty($_SESSION['userID'])) {
56
-					throw new IllegalStateException("userID is not set in session.");
57
-				}
58
-
59
-				if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
60
-					throw new IllegalStateException("username is not set in session.");
61
-				}
62
-
63
-				$this->userID = (int) $_SESSION['userID'];
64
-				$this->username = $_SESSION['username'];
65
-				$this->isLoggedIn = true;
66
-
67
-				//TODO: update online state in database
68
-			} else {
69
-				$this->setGuest();
70
-			}
71
-		} else {
72
-			$this->userID = (int) $userID;
73
-		}
74
-
75
-		Events::throwEvent("before_load_user", array(
76
-			'userID' => &$this->userID,
77
-			'isLoggedIn' => &$this->isLoggedIn,
78
-			'user' => &$this
79
-		));
80
-
81
-		//try to load from cache
82
-		if (Cache::contains("user", "user-" . $this->userID)) {
83
-			$this->row = Cache::get("user", "user-" . $this->userID);
84
-		} else {
85
-			$row = false;
86
-
87
-			//check, if guest user, because guest user doesnt exists in database
88
-			if ($this->userID !== -1) {
89
-				$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
90
-					'userID' => array(
91
-						'type' => PDO::PARAM_INT,
92
-						'value' => $this->userID
93
-					)
94
-				));
95
-			}
96
-
97
-			if (!$row) {
98
-				$logout_user = true;
99
-
100
-				//user not found, throw an event, so plugins can handle this (optional)
101
-				Events::throwEvent("user_not_found", array(
102
-					'userID' => &$this->userID,
103
-					'username' => &$this->username,
104
-					'isLoggedIn' => &$this->isLoggedIn,
105
-					'row' => &$row,
106
-					'logout_user' => &$logout_user,
107
-					'user' => &$this
108
-				));
109
-
110
-				if ($logout_user) {
111
-					//logout user
112
-					$this->logout();
113
-				}
114
-			} else {
115
-				//remove password hash from row
116
-				unset($row['password']);
117
-
118
-				Events::throwEvent("before_cache_user", array(
119
-					'userID' => &$this->userID,
120
-					'username' => &$this->username,
121
-					'isLoggedIn' => &$this->isLoggedIn,
122
-					'row' => &$row,
123
-					'user' => &$this
124
-				));
125
-
126
-				//cache entry
127
-				Cache::put("user", "user-" . $this->userID, $row);
128
-
129
-				$this->row = $row;
130
-			}
131
-		}
132
-
133
-		if ($this->row !== null) {
134
-			$this->userID = (int) $this->row['userID'];
135
-			$this->username = $this->row['username'];
136
-		}
137
-
138
-		Events::throwEvent("after_load_user", array(
139
-			'userID' => &$this->userID,
140
-			'username' => &$this->username,
141
-			'isLoggedIn' => &$this->isLoggedIn,
142
-			'row' => &$row,
143
-			'user' => &$this
144
-		));
145
-
146
-		//TODO: update online state and IP
147
-		if ($userID === -1 && $this->isLoggedIn()) {
148
-			$this->setOnline();
149
-		}
150
-	}
151
-
152
-	public function loginByUsername (string $username, string $password) : array {
153
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
154
-			'username' => &$username
155
-		));
156
-
157
-		if (!$row) {
158
-			//get default authentificator
159
-			$authentificator = self::getDefaultAuthentificator();
160
-
161
-			$userID = $authentificator->checkPasswordAndImport($username, $password);
162
-
163
-			if ($userID == -1) {
164
-				//user not found
165
-			} else {
166
-				//user was imported now, get user row
167
-				$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
168
-					'userID' => &$userID
169
-				));
170
-			}
171
-		}
172
-
173
-		return $this->loginRow($row, $password);
174
-	}
175
-
176
-	public function loginByMail (string $mail, string $password) : array {
177
-		//check, if mail is valide
178
-		$validator = new Validator_Mail();
179
-
180
-		if (!$validator->isValide($mail)) {
181
-			return array(
182
-				'success' => false,
183
-				'error' => "mail_not_valide"
184
-			);
185
-		}
186
-
187
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `mail` = :mail AND `activated` = '1'; ", array(
188
-			'mail' => &$mail
189
-		));
190
-
191
-		return $this->loginRow($row, $password);
192
-	}
193
-
194
-	public function loginByID (int $userID) : array {
195
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
196
-			'userID' => &$userID
197
-		));
198
-
199
-		$res = array();
200
-
201
-		if ($row !== false) {
202
-			//set online state
203
-			$this->setOnline();
204
-
205
-			//set logged in
206
-			$this->setLoggedIn($row['userID'], $row['username'], $row);
207
-
208
-			//login successful
209
-			$res['success'] = true;
210
-			$res['error'] = "none";
211
-			return $res;
212
-		} else {
213
-			//user doesnt exists
214
-			$res['success'] = false;
215
-			$res['error'] = "user_not_exists";
216
-			return $res;
217
-		}
218
-	}
219
-
220
-	/**
221
-	 * check password of current user
222
-	 *
223
-	 * @param $password string password
224
-	 *
225
-	 * @throws IllegalStateException if user wasnt loaded before
226
-	 *
227
-	 * @return true, if password is correct
228
-	 */
229
-	public function checkPassword (string $password) : bool {
230
-		if ($this->row == null || empty($this->row)) {
231
-			throw new IllegalStateException("user wasnt loaded.");
232
-		}
233
-
234
-		//because password is not cached, we have to load it directly from database
235
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
236
-			'userID' => $this->getID()
237
-		));
238
-
239
-		//get salt
240
-		$salt = $row['salt'];
241
-
242
-		//add salt to password
243
-		$password .= $salt;
244
-
245
-		return password_verify($password, $row['password']);
246
-	}
247
-
248
-	public function setPassword (string $password) {
249
-		if ($this->row == null || empty($this->row)) {
250
-			throw new IllegalStateException("user wasnt loaded.");
251
-		}
252
-
253
-		//validate password
254
-		$password = Validator_Password::get($password);
255
-
256
-		//create new salt
257
-		$salt = md5(PHPUtils::randomString(50));
258
-
259
-		//generate password hash
260
-		$hashed_password = self::hashPassword($password, $salt);
261
-
262
-		//update database
263
-		Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password, `salt` = :salt WHERE `userID` = :userID; ", array(
264
-			'password' => $hashed_password,
265
-			'salt' => $salt,
266
-			'userID' => $this->getID()
267
-		));
268
-
269
-		//clear cache
270
-		Cache::clear("user", "user-" . $this->getID());
271
-	}
272
-
273
-	protected function loginRow ($row, string $password) : array {
274
-		$res = array();
275
-
276
-		if (!$row) {
277
-			//user doesnt exists
278
-			$res['success'] = false;
279
-			$res['error'] = "user_not_exists";
280
-
281
-			return $res;
282
-		}
283
-
284
-		//get authentificator
285
-		$authentificator = self::getAuthentificatorByID($row['userID']);
286
-
287
-		$auth_res = $authentificator->checkPasswordAndImport($row['username'], $password);
288
-
289
-		//check password
290
-		if ($auth_res !== -1) {
291
-			//password is correct
292
-
293
-			//set online state
294
-			$this->setOnline();
295
-
296
-			//set logged in
297
-			$this->setLoggedIn($row['userID'], $row['username'], $row);
298
-
299
-			//login successful
300
-			$res['success'] = true;
301
-			$res['error'] = "none";
302
-			return $res;
303
-		} else {
304
-			//wrong password
305
-
306
-			//user doesnt exists
307
-			$res['success'] = false;
308
-			$res['error'] = "wrong_password";
309
-
310
-			return $res;
311
-		}
312
-	}
313
-
314
-	protected function setLoggedIn (int $userID, string $username, array $row) {
315
-		$_SESSION['logged-in'] = true;
316
-		$_SESSION['userID'] = (int) $userID;
317
-		$_SESSION['username'] = $username;
318
-
319
-		//remove password hash from row (so password isnt cached)
320
-		unset($row['password']);
321
-
322
-		$this->userID = $userID;
323
-		$this->username = $username;
324
-		$this->row = $row;
325
-	}
326
-
327
-	public function logout () {
328
-		//check, if session was started
329
-		PHPUtils::checkSessionStarted();
330
-
331
-		unset($_SESSION['userID']);
332
-		unset($_SESSION['username']);
333
-
334
-		$_SESSION['logged-in'] = false;
335
-
336
-		$this->setGuest();
337
-	}
338
-
339
-	protected function setGuest () {
340
-		$this->userID = (int) Settings::get("guest_userid", "-1");
341
-		$this->username = Settings::get("guest_username", "Guest");
342
-		$this->isLoggedIn = false;
343
-	}
344
-
345
-	protected static function hashPassword ($password, $salt) {
346
-		//http://php.net/manual/de/function.password-hash.php
347
-
348
-		//add salt to password
349
-		$password .= $salt;
350
-
351
-		$options = array(
352
-			'cost' => (int) Settings::get("password_hash_cost", "10")
353
-		);
354
-		$algo = PASSWORD_DEFAULT;
355
-
356
-		Events::throwEvent("hashing_password", array(
357
-			'options' => &$options,
358
-			'algo' => &$algo
359
-		));
360
-
361
-		return password_hash($password, $algo, $options);
362
-	}
363
-
364
-	/**
365
-	 * get user ID of user
366
-	 *
367
-	 * @return integer userID
368
-	 */
369
-	public function getID () : int {
370
-		return $this->userID;
371
-	}
372
-
373
-	/**
374
-	 * get username of user
375
-	 *
376
-	 * @return string username
377
-	 */
378
-	public function getUsername () : string {
379
-		return $this->username;
380
-	}
381
-
382
-	public function getMail () : string {
383
-		return $this->row['mail'];
384
-	}
385
-
386
-	public function isLoggedIn () : bool {
387
-		return $this->isLoggedIn;
388
-	}
389
-
390
-	public function getRow () : array {
391
-		return $this->row;
392
-	}
393
-
394
-	public function setOnline (bool $updateIP = true) {
395
-		//get client ip
396
-		$ip = PHPUtils::getClientIP();
397
-
398
-		if ($updateIP) {
399
-			Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array(
400
-				'userid' => array(
401
-					'type' => PDO::PARAM_INT,
402
-					'value' => (int) $this->userID
403
-				),
404
-				'ip' => $ip
405
-			));
406
-		} else {
407
-			Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array(
408
-				'userid' => array(
409
-					'type' => PDO::PARAM_INT,
410
-					'value' => (int) $this->userID
411
-				)
412
-			));
413
-		}
414
-	}
415
-
416
-	public function updateOnlineList () {
417
-		$interval_minutes = (int) Settings::get("online_interval", "5");
418
-
419
-		Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; ");
420
-	}
421
-
422
-	/**
423
-	 * creates user if userID is absent
424
-	 *
425
-	 * Only use this method for installation & upgrade!
426
-	 */
427
-	public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
428
-		if (self::existsUserID($userID)) {
429
-			//dont create user, if user already exists
430
-			return;
431
-		}
432
-
433
-		//create salt
434
-		$salt = md5(PHPUtils::randomString(50));
435
-
436
-		//generate password hash
437
-		$hashed_password = self::hashPassword($password, $salt);
438
-
439
-		Database::getInstance()->execute("INSERT INTO `{praefix}user` (
30
+    //instance of current (logged-in / guest) user
31
+    protected static $instance = null;
32
+
33
+    //current userID
34
+    protected $userID = -1;
35
+
36
+    //current username
37
+    protected $username = "Guest";
38
+
39
+    //flag, if user is logged in
40
+    protected $isLoggedIn = false;
41
+
42
+    //current database row
43
+    protected $row = null;
44
+
45
+    protected static $default_authentificator = null;
46
+
47
+    public function __construct() {
48
+        //
49
+    }
50
+
51
+    public function load (int $userID = -1) {
52
+        //check, if user is logged in
53
+        if ($userID === -1) {
54
+            if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) {
55
+                if (!isset($_SESSION['userID']) || empty($_SESSION['userID'])) {
56
+                    throw new IllegalStateException("userID is not set in session.");
57
+                }
58
+
59
+                if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
60
+                    throw new IllegalStateException("username is not set in session.");
61
+                }
62
+
63
+                $this->userID = (int) $_SESSION['userID'];
64
+                $this->username = $_SESSION['username'];
65
+                $this->isLoggedIn = true;
66
+
67
+                //TODO: update online state in database
68
+            } else {
69
+                $this->setGuest();
70
+            }
71
+        } else {
72
+            $this->userID = (int) $userID;
73
+        }
74
+
75
+        Events::throwEvent("before_load_user", array(
76
+            'userID' => &$this->userID,
77
+            'isLoggedIn' => &$this->isLoggedIn,
78
+            'user' => &$this
79
+        ));
80
+
81
+        //try to load from cache
82
+        if (Cache::contains("user", "user-" . $this->userID)) {
83
+            $this->row = Cache::get("user", "user-" . $this->userID);
84
+        } else {
85
+            $row = false;
86
+
87
+            //check, if guest user, because guest user doesnt exists in database
88
+            if ($this->userID !== -1) {
89
+                $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
90
+                    'userID' => array(
91
+                        'type' => PDO::PARAM_INT,
92
+                        'value' => $this->userID
93
+                    )
94
+                ));
95
+            }
96
+
97
+            if (!$row) {
98
+                $logout_user = true;
99
+
100
+                //user not found, throw an event, so plugins can handle this (optional)
101
+                Events::throwEvent("user_not_found", array(
102
+                    'userID' => &$this->userID,
103
+                    'username' => &$this->username,
104
+                    'isLoggedIn' => &$this->isLoggedIn,
105
+                    'row' => &$row,
106
+                    'logout_user' => &$logout_user,
107
+                    'user' => &$this
108
+                ));
109
+
110
+                if ($logout_user) {
111
+                    //logout user
112
+                    $this->logout();
113
+                }
114
+            } else {
115
+                //remove password hash from row
116
+                unset($row['password']);
117
+
118
+                Events::throwEvent("before_cache_user", array(
119
+                    'userID' => &$this->userID,
120
+                    'username' => &$this->username,
121
+                    'isLoggedIn' => &$this->isLoggedIn,
122
+                    'row' => &$row,
123
+                    'user' => &$this
124
+                ));
125
+
126
+                //cache entry
127
+                Cache::put("user", "user-" . $this->userID, $row);
128
+
129
+                $this->row = $row;
130
+            }
131
+        }
132
+
133
+        if ($this->row !== null) {
134
+            $this->userID = (int) $this->row['userID'];
135
+            $this->username = $this->row['username'];
136
+        }
137
+
138
+        Events::throwEvent("after_load_user", array(
139
+            'userID' => &$this->userID,
140
+            'username' => &$this->username,
141
+            'isLoggedIn' => &$this->isLoggedIn,
142
+            'row' => &$row,
143
+            'user' => &$this
144
+        ));
145
+
146
+        //TODO: update online state and IP
147
+        if ($userID === -1 && $this->isLoggedIn()) {
148
+            $this->setOnline();
149
+        }
150
+    }
151
+
152
+    public function loginByUsername (string $username, string $password) : array {
153
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
154
+            'username' => &$username
155
+        ));
156
+
157
+        if (!$row) {
158
+            //get default authentificator
159
+            $authentificator = self::getDefaultAuthentificator();
160
+
161
+            $userID = $authentificator->checkPasswordAndImport($username, $password);
162
+
163
+            if ($userID == -1) {
164
+                //user not found
165
+            } else {
166
+                //user was imported now, get user row
167
+                $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
168
+                    'userID' => &$userID
169
+                ));
170
+            }
171
+        }
172
+
173
+        return $this->loginRow($row, $password);
174
+    }
175
+
176
+    public function loginByMail (string $mail, string $password) : array {
177
+        //check, if mail is valide
178
+        $validator = new Validator_Mail();
179
+
180
+        if (!$validator->isValide($mail)) {
181
+            return array(
182
+                'success' => false,
183
+                'error' => "mail_not_valide"
184
+            );
185
+        }
186
+
187
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `mail` = :mail AND `activated` = '1'; ", array(
188
+            'mail' => &$mail
189
+        ));
190
+
191
+        return $this->loginRow($row, $password);
192
+    }
193
+
194
+    public function loginByID (int $userID) : array {
195
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
196
+            'userID' => &$userID
197
+        ));
198
+
199
+        $res = array();
200
+
201
+        if ($row !== false) {
202
+            //set online state
203
+            $this->setOnline();
204
+
205
+            //set logged in
206
+            $this->setLoggedIn($row['userID'], $row['username'], $row);
207
+
208
+            //login successful
209
+            $res['success'] = true;
210
+            $res['error'] = "none";
211
+            return $res;
212
+        } else {
213
+            //user doesnt exists
214
+            $res['success'] = false;
215
+            $res['error'] = "user_not_exists";
216
+            return $res;
217
+        }
218
+    }
219
+
220
+    /**
221
+     * check password of current user
222
+     *
223
+     * @param $password string password
224
+     *
225
+     * @throws IllegalStateException if user wasnt loaded before
226
+     *
227
+     * @return true, if password is correct
228
+     */
229
+    public function checkPassword (string $password) : bool {
230
+        if ($this->row == null || empty($this->row)) {
231
+            throw new IllegalStateException("user wasnt loaded.");
232
+        }
233
+
234
+        //because password is not cached, we have to load it directly from database
235
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
236
+            'userID' => $this->getID()
237
+        ));
238
+
239
+        //get salt
240
+        $salt = $row['salt'];
241
+
242
+        //add salt to password
243
+        $password .= $salt;
244
+
245
+        return password_verify($password, $row['password']);
246
+    }
247
+
248
+    public function setPassword (string $password) {
249
+        if ($this->row == null || empty($this->row)) {
250
+            throw new IllegalStateException("user wasnt loaded.");
251
+        }
252
+
253
+        //validate password
254
+        $password = Validator_Password::get($password);
255
+
256
+        //create new salt
257
+        $salt = md5(PHPUtils::randomString(50));
258
+
259
+        //generate password hash
260
+        $hashed_password = self::hashPassword($password, $salt);
261
+
262
+        //update database
263
+        Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password, `salt` = :salt WHERE `userID` = :userID; ", array(
264
+            'password' => $hashed_password,
265
+            'salt' => $salt,
266
+            'userID' => $this->getID()
267
+        ));
268
+
269
+        //clear cache
270
+        Cache::clear("user", "user-" . $this->getID());
271
+    }
272
+
273
+    protected function loginRow ($row, string $password) : array {
274
+        $res = array();
275
+
276
+        if (!$row) {
277
+            //user doesnt exists
278
+            $res['success'] = false;
279
+            $res['error'] = "user_not_exists";
280
+
281
+            return $res;
282
+        }
283
+
284
+        //get authentificator
285
+        $authentificator = self::getAuthentificatorByID($row['userID']);
286
+
287
+        $auth_res = $authentificator->checkPasswordAndImport($row['username'], $password);
288
+
289
+        //check password
290
+        if ($auth_res !== -1) {
291
+            //password is correct
292
+
293
+            //set online state
294
+            $this->setOnline();
295
+
296
+            //set logged in
297
+            $this->setLoggedIn($row['userID'], $row['username'], $row);
298
+
299
+            //login successful
300
+            $res['success'] = true;
301
+            $res['error'] = "none";
302
+            return $res;
303
+        } else {
304
+            //wrong password
305
+
306
+            //user doesnt exists
307
+            $res['success'] = false;
308
+            $res['error'] = "wrong_password";
309
+
310
+            return $res;
311
+        }
312
+    }
313
+
314
+    protected function setLoggedIn (int $userID, string $username, array $row) {
315
+        $_SESSION['logged-in'] = true;
316
+        $_SESSION['userID'] = (int) $userID;
317
+        $_SESSION['username'] = $username;
318
+
319
+        //remove password hash from row (so password isnt cached)
320
+        unset($row['password']);
321
+
322
+        $this->userID = $userID;
323
+        $this->username = $username;
324
+        $this->row = $row;
325
+    }
326
+
327
+    public function logout () {
328
+        //check, if session was started
329
+        PHPUtils::checkSessionStarted();
330
+
331
+        unset($_SESSION['userID']);
332
+        unset($_SESSION['username']);
333
+
334
+        $_SESSION['logged-in'] = false;
335
+
336
+        $this->setGuest();
337
+    }
338
+
339
+    protected function setGuest () {
340
+        $this->userID = (int) Settings::get("guest_userid", "-1");
341
+        $this->username = Settings::get("guest_username", "Guest");
342
+        $this->isLoggedIn = false;
343
+    }
344
+
345
+    protected static function hashPassword ($password, $salt) {
346
+        //http://php.net/manual/de/function.password-hash.php
347
+
348
+        //add salt to password
349
+        $password .= $salt;
350
+
351
+        $options = array(
352
+            'cost' => (int) Settings::get("password_hash_cost", "10")
353
+        );
354
+        $algo = PASSWORD_DEFAULT;
355
+
356
+        Events::throwEvent("hashing_password", array(
357
+            'options' => &$options,
358
+            'algo' => &$algo
359
+        ));
360
+
361
+        return password_hash($password, $algo, $options);
362
+    }
363
+
364
+    /**
365
+     * get user ID of user
366
+     *
367
+     * @return integer userID
368
+     */
369
+    public function getID () : int {
370
+        return $this->userID;
371
+    }
372
+
373
+    /**
374
+     * get username of user
375
+     *
376
+     * @return string username
377
+     */
378
+    public function getUsername () : string {
379
+        return $this->username;
380
+    }
381
+
382
+    public function getMail () : string {
383
+        return $this->row['mail'];
384
+    }
385
+
386
+    public function isLoggedIn () : bool {
387
+        return $this->isLoggedIn;
388
+    }
389
+
390
+    public function getRow () : array {
391
+        return $this->row;
392
+    }
393
+
394
+    public function setOnline (bool $updateIP = true) {
395
+        //get client ip
396
+        $ip = PHPUtils::getClientIP();
397
+
398
+        if ($updateIP) {
399
+            Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array(
400
+                'userid' => array(
401
+                    'type' => PDO::PARAM_INT,
402
+                    'value' => (int) $this->userID
403
+                ),
404
+                'ip' => $ip
405
+            ));
406
+        } else {
407
+            Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array(
408
+                'userid' => array(
409
+                    'type' => PDO::PARAM_INT,
410
+                    'value' => (int) $this->userID
411
+                )
412
+            ));
413
+        }
414
+    }
415
+
416
+    public function updateOnlineList () {
417
+        $interval_minutes = (int) Settings::get("online_interval", "5");
418
+
419
+        Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; ");
420
+    }
421
+
422
+    /**
423
+     * creates user if userID is absent
424
+     *
425
+     * Only use this method for installation & upgrade!
426
+     */
427
+    public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
428
+        if (self::existsUserID($userID)) {
429
+            //dont create user, if user already exists
430
+            return;
431
+        }
432
+
433
+        //create salt
434
+        $salt = md5(PHPUtils::randomString(50));
435
+
436
+        //generate password hash
437
+        $hashed_password = self::hashPassword($password, $salt);
438
+
439
+        Database::getInstance()->execute("INSERT INTO `{praefix}user` (
440 440
 			`userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `registered`, `activated`
441 441
 		) VALUES (
442 442
 			:userID, :username, :password, :salt, :mail, '0.0.0.0', :main_group, :title, '0', '0000-00-00 00:00:00', CURRENT_TIMESTAMP , :activated
443 443
 		)", array(
444
-			'userID' => $userID,
445
-			'username' => $username,
446
-			'password' => $hashed_password,
447
-			'salt' => $salt,
448
-			'mail' => $mail,
449
-			'main_group' => $main_group,
450
-			'title' => $specific_title,
451
-			'activated' => $activated
452
-		));
453
-	}
454
-
455
-	public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1, string $authentificator = "LocalAuthentificator", string $owner = "system") {
456
-		if (self::existsUsername($username)) {
457
-			//dont create user, if username already exists
458
-			return false;
459
-		}
460
-
461
-		if (self::existsMail($mail)) {
462
-			//dont create user, if mail already exists
463
-			return false;
464
-		}
465
-
466
-		if (empty($specific_title)) {
467
-			$specific_title = "none";
468
-		}
469
-
470
-		//create salt
471
-		$salt = md5(PHPUtils::randomString(50));
472
-
473
-		//generate password hash
474
-		$hashed_password = self::hashPassword($password, $salt);
475
-
476
-		//create user in database
477
-		Database::getInstance()->execute("INSERT INTO `{praefix}user` (
444
+            'userID' => $userID,
445
+            'username' => $username,
446
+            'password' => $hashed_password,
447
+            'salt' => $salt,
448
+            'mail' => $mail,
449
+            'main_group' => $main_group,
450
+            'title' => $specific_title,
451
+            'activated' => $activated
452
+        ));
453
+    }
454
+
455
+    public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1, string $authentificator = "LocalAuthentificator", string $owner = "system") {
456
+        if (self::existsUsername($username)) {
457
+            //dont create user, if username already exists
458
+            return false;
459
+        }
460
+
461
+        if (self::existsMail($mail)) {
462
+            //dont create user, if mail already exists
463
+            return false;
464
+        }
465
+
466
+        if (empty($specific_title)) {
467
+            $specific_title = "none";
468
+        }
469
+
470
+        //create salt
471
+        $salt = md5(PHPUtils::randomString(50));
472
+
473
+        //generate password hash
474
+        $hashed_password = self::hashPassword($password, $salt);
475
+
476
+        //create user in database
477
+        Database::getInstance()->execute("INSERT INTO `{praefix}user` (
478 478
 			`userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `authentificator`, `owner`, `registered`, `activated`
479 479
 		) VALUES (
480 480
 			NULL, :username, :password, :salt, :mail, :ip, :main_group, :title, '0', '0000-00-00 00:00:00', :authentificator, :owner, CURRENT_TIMESTAMP , :activated
481 481
 		)", array(
482
-			'username' => $username,
483
-			'password' => $hashed_password,
484
-			'salt' => $salt,
485
-			'mail' => $mail,
486
-			'ip' => $ip,
487
-			'main_group' => $main_group,
488
-			'title' => $specific_title,
489
-			'authentificator' => $authentificator,
490
-			'owner' => $owner,
491
-			'activated' => $activated
492
-		));
493
-
494
-		//get userID
495
-		$userID = self::getIDByUsernameFromDB($username);
496
-
497
-		if ($userID == Settings::get("guest_userid", -1)) {
498
-			//something went wrong
499
-			return false;
500
-		}
501
-
502
-		//add user to group "registered users"
503
-		Groups::addGroupToUser(2, $userID, false);
504
-
505
-		Events::throwEvent("add_user", array(
506
-			'userID' => $userID,
507
-			'username' => &$username,
508
-			'mail' => $mail,
509
-			'main_group' => $main_group
510
-		));
511
-
512
-		return array(
513
-			'success' => true,
514
-			'userID' => $userID,
515
-			'username' => $username,
516
-			'mail' => $mail
517
-		);
518
-	}
519
-
520
-	public static function deleteUserID (int $userID) {
521
-		Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array(
522
-			'userID' => array(
523
-				'type' => PDO::PARAM_INT,
524
-				'value' => $userID
525
-			)
526
-		));
527
-
528
-		//remove user from cache
529
-		Cache::clear("user", "user-" . $userID);
530
-	}
531
-
532
-	public static function existsUserID (int $userID) : bool {
533
-		//search for userID in database
534
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array(
535
-			'userID' => array(
536
-				'type' => PDO::PARAM_INT,
537
-				'value' => $userID
538
-			)
539
-		));
540
-
541
-		return $row !== false;
542
-	}
543
-
544
-	public static function existsUsername (string $username) : bool {
545
-		//search for username in database, ignore case
546
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
547
-
548
-		return $row !== false;
549
-	}
550
-
551
-	public static function existsMail (string $mail) : bool {
552
-		//search for mail in database, ignore case
553
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail));
554
-
555
-		return $row !== false;
556
-	}
557
-
558
-	public static function getIDByUsernameFromDB (string $username) : int {
559
-		//search for username in database, ignore case
560
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
561
-
562
-		if ($row === false) {
563
-			//return guest userID
564
-			return Settings::get("guest_userid", -1);
565
-		}
566
-
567
-		return $row['userID'];
568
-	}
569
-
570
-	public static function &getAuthentificatorByID (int $userID = -1) {
571
-		if ($userID == -1) {
572
-			//get default authentificator
573
-			return self::getDefaultAuthentificator();
574
-		} else {
575
-			//get authentificator class
576
-
577
-			//check, if user exists
578
-			if (!self::existsUserID($userID)) {
579
-				throw new IllegalStateException("user with userID '" . $userID . "' doesnt exists.");
580
-			}
581
-
582
-			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
583
-				'userID' => &$userID
584
-			));
585
-
586
-			$class_name = $row['authentificator'];
587
-			$obj = new $class_name();
588
-
589
-			return $obj;
590
-		}
591
-	}
592
-
593
-	public static function &getAuthentificatorByUsername (string $username = "") {
594
-		if ($username == null || empty($username)) {
595
-			//get default authentificator
596
-			return self::getDefaultAuthentificator();
597
-		} else {
598
-			//get authentificator class
599
-
600
-			//check, if user exists
601
-			if (!self::existsUsername($username)) {
602
-				throw new IllegalStateException("user with username '" . $username . "' doesnt exists.");
603
-			}
604
-
605
-			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
606
-				'username' => &$username
607
-			));
608
-
609
-			$class_name = $row['authentificator'];
610
-			$obj = new $class_name();
611
-
612
-			return $obj;
613
-		}
614
-	}
615
-
616
-	public static function &getDefaultAuthentificator () : IAuthentificator {
617
-		if (self::$default_authentificator == null) {
618
-			$class_name = Settings::get("default_authentificator", "LocalAuthentificator");
619
-			$obj = new $class_name();
620
-
621
-			self::$default_authentificator = $obj;
622
-		}
623
-
624
-		return self::$default_authentificator;
625
-	}
626
-
627
-	/**
628
-	 * get instance of current (logged in / guest) user
629
-	 */
630
-	public static function &current () : User {
631
-		if (self::$instance == null) {
632
-			self::$instance = new User();
633
-			self::$instance->load();
634
-		}
635
-
636
-		return self::$instance;
637
-	}
482
+            'username' => $username,
483
+            'password' => $hashed_password,
484
+            'salt' => $salt,
485
+            'mail' => $mail,
486
+            'ip' => $ip,
487
+            'main_group' => $main_group,
488
+            'title' => $specific_title,
489
+            'authentificator' => $authentificator,
490
+            'owner' => $owner,
491
+            'activated' => $activated
492
+        ));
493
+
494
+        //get userID
495
+        $userID = self::getIDByUsernameFromDB($username);
496
+
497
+        if ($userID == Settings::get("guest_userid", -1)) {
498
+            //something went wrong
499
+            return false;
500
+        }
501
+
502
+        //add user to group "registered users"
503
+        Groups::addGroupToUser(2, $userID, false);
504
+
505
+        Events::throwEvent("add_user", array(
506
+            'userID' => $userID,
507
+            'username' => &$username,
508
+            'mail' => $mail,
509
+            'main_group' => $main_group
510
+        ));
511
+
512
+        return array(
513
+            'success' => true,
514
+            'userID' => $userID,
515
+            'username' => $username,
516
+            'mail' => $mail
517
+        );
518
+    }
519
+
520
+    public static function deleteUserID (int $userID) {
521
+        Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array(
522
+            'userID' => array(
523
+                'type' => PDO::PARAM_INT,
524
+                'value' => $userID
525
+            )
526
+        ));
527
+
528
+        //remove user from cache
529
+        Cache::clear("user", "user-" . $userID);
530
+    }
531
+
532
+    public static function existsUserID (int $userID) : bool {
533
+        //search for userID in database
534
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array(
535
+            'userID' => array(
536
+                'type' => PDO::PARAM_INT,
537
+                'value' => $userID
538
+            )
539
+        ));
540
+
541
+        return $row !== false;
542
+    }
543
+
544
+    public static function existsUsername (string $username) : bool {
545
+        //search for username in database, ignore case
546
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
547
+
548
+        return $row !== false;
549
+    }
550
+
551
+    public static function existsMail (string $mail) : bool {
552
+        //search for mail in database, ignore case
553
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail));
554
+
555
+        return $row !== false;
556
+    }
557
+
558
+    public static function getIDByUsernameFromDB (string $username) : int {
559
+        //search for username in database, ignore case
560
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
561
+
562
+        if ($row === false) {
563
+            //return guest userID
564
+            return Settings::get("guest_userid", -1);
565
+        }
566
+
567
+        return $row['userID'];
568
+    }
569
+
570
+    public static function &getAuthentificatorByID (int $userID = -1) {
571
+        if ($userID == -1) {
572
+            //get default authentificator
573
+            return self::getDefaultAuthentificator();
574
+        } else {
575
+            //get authentificator class
576
+
577
+            //check, if user exists
578
+            if (!self::existsUserID($userID)) {
579
+                throw new IllegalStateException("user with userID '" . $userID . "' doesnt exists.");
580
+            }
581
+
582
+            $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
583
+                'userID' => &$userID
584
+            ));
585
+
586
+            $class_name = $row['authentificator'];
587
+            $obj = new $class_name();
588
+
589
+            return $obj;
590
+        }
591
+    }
592
+
593
+    public static function &getAuthentificatorByUsername (string $username = "") {
594
+        if ($username == null || empty($username)) {
595
+            //get default authentificator
596
+            return self::getDefaultAuthentificator();
597
+        } else {
598
+            //get authentificator class
599
+
600
+            //check, if user exists
601
+            if (!self::existsUsername($username)) {
602
+                throw new IllegalStateException("user with username '" . $username . "' doesnt exists.");
603
+            }
604
+
605
+            $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
606
+                'username' => &$username
607
+            ));
608
+
609
+            $class_name = $row['authentificator'];
610
+            $obj = new $class_name();
611
+
612
+            return $obj;
613
+        }
614
+    }
615
+
616
+    public static function &getDefaultAuthentificator () : IAuthentificator {
617
+        if (self::$default_authentificator == null) {
618
+            $class_name = Settings::get("default_authentificator", "LocalAuthentificator");
619
+            $obj = new $class_name();
620
+
621
+            self::$default_authentificator = $obj;
622
+        }
623
+
624
+        return self::$default_authentificator;
625
+    }
626
+
627
+    /**
628
+     * get instance of current (logged in / guest) user
629
+     */
630
+    public static function &current () : User {
631
+        if (self::$instance == null) {
632
+            self::$instance = new User();
633
+            self::$instance->load();
634
+        }
635
+
636
+        return self::$instance;
637
+    }
638 638
 
639 639
 }
640 640
 
Please login to merge, or discard this patch.