Test Failed
Push — master ( 62e539...2c88a2 )
by Justin
41:32 queued 38:00
created
system/packages/com.jukusoft.cms.user/classes/user.php 2 patches
Indentation   +484 added lines, -484 removed lines patch added patch discarded remove patch
@@ -27,498 +27,498 @@
 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
-	public function __construct() {
46
-		//
47
-	}
48
-
49
-	public function load (int $userID = -1) {
50
-		//check, if user is logged in
51
-		if ($userID === -1) {
52
-			if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) {
53
-				if (!isset($_SESSION['userID']) || empty($_SESSION['userID'])) {
54
-					throw new IllegalStateException("userID is not set in session.");
55
-				}
56
-
57
-				if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
58
-					throw new IllegalStateException("username is not set in session.");
59
-				}
60
-
61
-				$this->userID = (int) $_SESSION['userID'];
62
-				$this->username = $_SESSION['username'];
63
-				$this->isLoggedIn = true;
64
-
65
-				//TODO: update online state in database
66
-			} else {
67
-				$this->setGuest();
68
-			}
69
-		} else {
70
-			$this->userID = (int) $userID;
71
-		}
72
-
73
-		Events::throwEvent("before_load_user", array(
74
-			'userID' => &$this->userID,
75
-			'isLoggedIn' => &$this->isLoggedIn,
76
-			'user' => &$this
77
-		));
78
-
79
-		//try to load from cache
80
-		if (Cache::contains("user", "user-" . $this->userID)) {
81
-			$this->row = Cache::get("user", "user-" . $this->userID);
82
-		} else {
83
-			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
84
-				'userID' => array(
85
-					'type' => PDO::PARAM_INT,
86
-					'value' => $this->userID
87
-				)
88
-			));
89
-
90
-			if (!$row) {
91
-				$logout_user = true;
92
-
93
-				//user not found, throw an event, so plugins can handle this (optional)
94
-				Events::throwEvent("user_not_found", array(
95
-					'userID' => &$this->userID,
96
-					'username' => &$this->username,
97
-					'isLoggedIn' => &$this->isLoggedIn,
98
-					'row' => &$row,
99
-					'logout_user' => &$logout_user,
100
-					'user' => &$this
101
-				));
102
-
103
-				if ($logout_user) {
104
-					//logout user
105
-					$this->logout();
106
-				}
107
-			} else {
108
-				//remove password hash from row
109
-				unset($row['password']);
110
-
111
-				Events::throwEvent("before_cache_user", array(
112
-					'userID' => &$this->userID,
113
-					'username' => &$this->username,
114
-					'isLoggedIn' => &$this->isLoggedIn,
115
-					'row' => &$row,
116
-					'user' => &$this
117
-				));
118
-
119
-				//cache entry
120
-				Cache::put("user", "user-" . $this->userID, $row);
121
-
122
-				$this->row = $row;
123
-			}
124
-		}
125
-
126
-		if ($this->row !== null) {
127
-			$this->userID = (int) $this->row['userID'];
128
-			$this->username = $this->row['username'];
129
-		}
130
-
131
-		Events::throwEvent("after_load_user", array(
132
-			'userID' => &$this->userID,
133
-			'username' => &$this->username,
134
-			'isLoggedIn' => &$this->isLoggedIn,
135
-			'row' => &$row,
136
-			'user' => &$this
137
-		));
138
-
139
-		//TODO: update online state and IP
140
-		if ($userID === -1 && $this->isLoggedIn()) {
141
-			$this->setOnline();
142
-		}
143
-	}
144
-
145
-	public function loginByUsername (string $username, string $password) : array {
146
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
147
-			'username' => &$username
148
-		));
149
-
150
-		return $this->loginRow($row, $password);
151
-	}
152
-
153
-	public function loginByMail (string $mail, string $password) : array {
154
-		//check, if mail is valide
155
-		$validator = new Validator_Mail();
156
-
157
-		if (!$validator->isValide($mail)) {
158
-			return array(
159
-				'success' => false,
160
-				'error' => "mail_not_valide"
161
-			);
162
-		}
163
-
164
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `mail` = :mail AND `activated` = '1'; ", array(
165
-			'mail' => &$mail
166
-		));
167
-
168
-		return $this->loginRow($row, $password);
169
-	}
170
-
171
-	public function loginByID (int $userID) : array {
172
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
173
-			'userID' => &$userID
174
-		));
175
-
176
-		$res = array();
177
-
178
-		if ($row !== false) {
179
-			//set online state
180
-			$this->setOnline();
181
-
182
-			//set logged in
183
-			$this->setLoggedIn($row['userID'], $row['username'], $row);
184
-
185
-			//login successful
186
-			$res['success'] = true;
187
-			$res['error'] = "none";
188
-			return $res;
189
-		} else {
190
-			//user doesnt exists
191
-			$res['success'] = false;
192
-			$res['error'] = "user_not_exists";
193
-			return $res;
194
-		}
195
-	}
196
-
197
-	protected function loginRow ($row, string $password) : array {
198
-		if (!$row) {
199
-			//user doesnt exists
200
-			$res['success'] = false;
201
-			$res['error'] = "user_not_exists";
202
-
203
-			return $res;
204
-		}
205
-
206
-		//user exists
207
-
208
-		//get salt
209
-		$salt = $row['salt'];
210
-
211
-		//add salt to password
212
-		$password .= $salt;
213
-
214
-		//verify password
215
-		if (password_verify($password, $row['password'])) {
216
-			//correct password
217
-
218
-			//check, if a newer password algorithmus is available --> rehash required
219
-			if (password_needs_rehash($row['password'], PASSWORD_DEFAULT)) {
220
-				//rehash password
221
-				$new_hash = self::hashPassword($password, $salt);
222
-
223
-				//update password in database
224
-				Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password WHERE `userID` = :userID; ", array(
225
-					'password' => $new_hash,
226
-					'userID' => array(
227
-						'type' => PDO::PARAM_INT,
228
-						'value' => $row['userID']
229
-					)
230
-				));
231
-			}
232
-
233
-			//set online state
234
-			$this->setOnline();
235
-
236
-			//set logged in
237
-			$this->setLoggedIn($row['userID'], $row['username'], $row);
238
-
239
-			//login successful
240
-			$res['success'] = true;
241
-			$res['error'] = "none";
242
-			return $res;
243
-		} else {
244
-			//wrong password
245
-
246
-			//user doesnt exists
247
-			$res['success'] = false;
248
-			$res['error'] = "wrong_password";
249
-
250
-			return $res;
251
-		}
252
-	}
253
-
254
-	protected function setLoggedIn (int $userID, string $username, array $row) {
255
-		$_SESSION['logged-in'] = true;
256
-		$_SESSION['userID'] = (int) $userID;
257
-		$_SESSION['username'] = $username;
258
-
259
-		//remove password hash from row (so password isnt cached)
260
-		unset($row['password']);
261
-
262
-		$this->userID = $userID;
263
-		$this->username = $username;
264
-		$this->row = $row;
265
-	}
266
-
267
-	public function logout () {
268
-		//check, if session was started
269
-		PHPUtils::checkSessionStarted();
270
-
271
-		unset($_SESSION['userID']);
272
-		unset($_SESSION['username']);
273
-
274
-		$_SESSION['logged-in'] = false;
275
-
276
-		echo "logout";
277
-		exit;
278
-
279
-		$this->setGuest();
280
-	}
281
-
282
-	protected function setGuest () {
283
-		$this->userID = (int) Settings::get("guest_userid", "-1");
284
-		$this->username = Settings::get("guest_username", "Guest");
285
-		$this->isLoggedIn = false;
286
-	}
287
-
288
-	protected static function hashPassword ($password, $salt) {
289
-		//http://php.net/manual/de/function.password-hash.php
290
-
291
-		//add salt to password
292
-		$password .= $salt;
293
-
294
-		$options = array(
295
-			'cost' => (int) Settings::get("password_hash_cost", "10")
296
-		);
297
-		$algo = PASSWORD_DEFAULT;
298
-
299
-		Events::throwEvent("hashing_password", array(
300
-			'options' => &$options,
301
-			'algo' => &$algo
302
-		));
303
-
304
-		return password_hash($password, $algo, $options);
305
-	}
306
-
307
-	/**
308
-	 * get user ID of user
309
-	 *
310
-	 * @return integer userID
311
-	 */
312
-	public function getID () : int {
313
-		return $this->userID;
314
-	}
315
-
316
-	/**
317
-	 * get username of user
318
-	 *
319
-	 * @return string username
320
-	 */
321
-	public function getUsername () : string {
322
-		return $this->username;
323
-	}
324
-
325
-	public function getMail () : string {
326
-		return $this->row['mail'];
327
-	}
328
-
329
-	public function isLoggedIn () : bool {
330
-		return $this->isLoggedIn;
331
-	}
332
-
333
-	public function getRow () : array {
334
-		return $this->row;
335
-	}
336
-
337
-	public function setOnline (bool $updateIP = true) {
338
-		//get client ip
339
-		$ip = PHPUtils::getClientIP();
340
-
341
-		if ($updateIP) {
342
-			Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array(
343
-				'userid' => array(
344
-					'type' => PDO::PARAM_INT,
345
-					'value' => (int) $this->userID
346
-				),
347
-				'ip' => $ip
348
-			));
349
-		} else {
350
-			Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array(
351
-				'userid' => array(
352
-					'type' => PDO::PARAM_INT,
353
-					'value' => (int) $this->userID
354
-				)
355
-			));
356
-		}
357
-	}
358
-
359
-	public function updateOnlineList () {
360
-		$interval_minutes = (int) Settings::get("online_interval", "5");
361
-
362
-		Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; ");
363
-	}
364
-
365
-	/**
366
-	 * creates user if userID is absent
367
-	 *
368
-	 * Only use this method for installation & upgrade!
369
-	 */
370
-	public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
371
-		if (self::existsUserID($userID)) {
372
-			//dont create user, if user already exists
373
-			return;
374
-		}
375
-
376
-		//create salt
377
-		$salt = md5(PHPUtils::randomString(50));
378
-
379
-		//generate password hash
380
-		$hashed_password = self::hashPassword($password, $salt);
381
-
382
-		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
+    public function __construct() {
46
+        //
47
+    }
48
+
49
+    public function load (int $userID = -1) {
50
+        //check, if user is logged in
51
+        if ($userID === -1) {
52
+            if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) {
53
+                if (!isset($_SESSION['userID']) || empty($_SESSION['userID'])) {
54
+                    throw new IllegalStateException("userID is not set in session.");
55
+                }
56
+
57
+                if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
58
+                    throw new IllegalStateException("username is not set in session.");
59
+                }
60
+
61
+                $this->userID = (int) $_SESSION['userID'];
62
+                $this->username = $_SESSION['username'];
63
+                $this->isLoggedIn = true;
64
+
65
+                //TODO: update online state in database
66
+            } else {
67
+                $this->setGuest();
68
+            }
69
+        } else {
70
+            $this->userID = (int) $userID;
71
+        }
72
+
73
+        Events::throwEvent("before_load_user", array(
74
+            'userID' => &$this->userID,
75
+            'isLoggedIn' => &$this->isLoggedIn,
76
+            'user' => &$this
77
+        ));
78
+
79
+        //try to load from cache
80
+        if (Cache::contains("user", "user-" . $this->userID)) {
81
+            $this->row = Cache::get("user", "user-" . $this->userID);
82
+        } else {
83
+            $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
84
+                'userID' => array(
85
+                    'type' => PDO::PARAM_INT,
86
+                    'value' => $this->userID
87
+                )
88
+            ));
89
+
90
+            if (!$row) {
91
+                $logout_user = true;
92
+
93
+                //user not found, throw an event, so plugins can handle this (optional)
94
+                Events::throwEvent("user_not_found", array(
95
+                    'userID' => &$this->userID,
96
+                    'username' => &$this->username,
97
+                    'isLoggedIn' => &$this->isLoggedIn,
98
+                    'row' => &$row,
99
+                    'logout_user' => &$logout_user,
100
+                    'user' => &$this
101
+                ));
102
+
103
+                if ($logout_user) {
104
+                    //logout user
105
+                    $this->logout();
106
+                }
107
+            } else {
108
+                //remove password hash from row
109
+                unset($row['password']);
110
+
111
+                Events::throwEvent("before_cache_user", array(
112
+                    'userID' => &$this->userID,
113
+                    'username' => &$this->username,
114
+                    'isLoggedIn' => &$this->isLoggedIn,
115
+                    'row' => &$row,
116
+                    'user' => &$this
117
+                ));
118
+
119
+                //cache entry
120
+                Cache::put("user", "user-" . $this->userID, $row);
121
+
122
+                $this->row = $row;
123
+            }
124
+        }
125
+
126
+        if ($this->row !== null) {
127
+            $this->userID = (int) $this->row['userID'];
128
+            $this->username = $this->row['username'];
129
+        }
130
+
131
+        Events::throwEvent("after_load_user", array(
132
+            'userID' => &$this->userID,
133
+            'username' => &$this->username,
134
+            'isLoggedIn' => &$this->isLoggedIn,
135
+            'row' => &$row,
136
+            'user' => &$this
137
+        ));
138
+
139
+        //TODO: update online state and IP
140
+        if ($userID === -1 && $this->isLoggedIn()) {
141
+            $this->setOnline();
142
+        }
143
+    }
144
+
145
+    public function loginByUsername (string $username, string $password) : array {
146
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
147
+            'username' => &$username
148
+        ));
149
+
150
+        return $this->loginRow($row, $password);
151
+    }
152
+
153
+    public function loginByMail (string $mail, string $password) : array {
154
+        //check, if mail is valide
155
+        $validator = new Validator_Mail();
156
+
157
+        if (!$validator->isValide($mail)) {
158
+            return array(
159
+                'success' => false,
160
+                'error' => "mail_not_valide"
161
+            );
162
+        }
163
+
164
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `mail` = :mail AND `activated` = '1'; ", array(
165
+            'mail' => &$mail
166
+        ));
167
+
168
+        return $this->loginRow($row, $password);
169
+    }
170
+
171
+    public function loginByID (int $userID) : array {
172
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
173
+            'userID' => &$userID
174
+        ));
175
+
176
+        $res = array();
177
+
178
+        if ($row !== false) {
179
+            //set online state
180
+            $this->setOnline();
181
+
182
+            //set logged in
183
+            $this->setLoggedIn($row['userID'], $row['username'], $row);
184
+
185
+            //login successful
186
+            $res['success'] = true;
187
+            $res['error'] = "none";
188
+            return $res;
189
+        } else {
190
+            //user doesnt exists
191
+            $res['success'] = false;
192
+            $res['error'] = "user_not_exists";
193
+            return $res;
194
+        }
195
+    }
196
+
197
+    protected function loginRow ($row, string $password) : array {
198
+        if (!$row) {
199
+            //user doesnt exists
200
+            $res['success'] = false;
201
+            $res['error'] = "user_not_exists";
202
+
203
+            return $res;
204
+        }
205
+
206
+        //user exists
207
+
208
+        //get salt
209
+        $salt = $row['salt'];
210
+
211
+        //add salt to password
212
+        $password .= $salt;
213
+
214
+        //verify password
215
+        if (password_verify($password, $row['password'])) {
216
+            //correct password
217
+
218
+            //check, if a newer password algorithmus is available --> rehash required
219
+            if (password_needs_rehash($row['password'], PASSWORD_DEFAULT)) {
220
+                //rehash password
221
+                $new_hash = self::hashPassword($password, $salt);
222
+
223
+                //update password in database
224
+                Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password WHERE `userID` = :userID; ", array(
225
+                    'password' => $new_hash,
226
+                    'userID' => array(
227
+                        'type' => PDO::PARAM_INT,
228
+                        'value' => $row['userID']
229
+                    )
230
+                ));
231
+            }
232
+
233
+            //set online state
234
+            $this->setOnline();
235
+
236
+            //set logged in
237
+            $this->setLoggedIn($row['userID'], $row['username'], $row);
238
+
239
+            //login successful
240
+            $res['success'] = true;
241
+            $res['error'] = "none";
242
+            return $res;
243
+        } else {
244
+            //wrong password
245
+
246
+            //user doesnt exists
247
+            $res['success'] = false;
248
+            $res['error'] = "wrong_password";
249
+
250
+            return $res;
251
+        }
252
+    }
253
+
254
+    protected function setLoggedIn (int $userID, string $username, array $row) {
255
+        $_SESSION['logged-in'] = true;
256
+        $_SESSION['userID'] = (int) $userID;
257
+        $_SESSION['username'] = $username;
258
+
259
+        //remove password hash from row (so password isnt cached)
260
+        unset($row['password']);
261
+
262
+        $this->userID = $userID;
263
+        $this->username = $username;
264
+        $this->row = $row;
265
+    }
266
+
267
+    public function logout () {
268
+        //check, if session was started
269
+        PHPUtils::checkSessionStarted();
270
+
271
+        unset($_SESSION['userID']);
272
+        unset($_SESSION['username']);
273
+
274
+        $_SESSION['logged-in'] = false;
275
+
276
+        echo "logout";
277
+        exit;
278
+
279
+        $this->setGuest();
280
+    }
281
+
282
+    protected function setGuest () {
283
+        $this->userID = (int) Settings::get("guest_userid", "-1");
284
+        $this->username = Settings::get("guest_username", "Guest");
285
+        $this->isLoggedIn = false;
286
+    }
287
+
288
+    protected static function hashPassword ($password, $salt) {
289
+        //http://php.net/manual/de/function.password-hash.php
290
+
291
+        //add salt to password
292
+        $password .= $salt;
293
+
294
+        $options = array(
295
+            'cost' => (int) Settings::get("password_hash_cost", "10")
296
+        );
297
+        $algo = PASSWORD_DEFAULT;
298
+
299
+        Events::throwEvent("hashing_password", array(
300
+            'options' => &$options,
301
+            'algo' => &$algo
302
+        ));
303
+
304
+        return password_hash($password, $algo, $options);
305
+    }
306
+
307
+    /**
308
+     * get user ID of user
309
+     *
310
+     * @return integer userID
311
+     */
312
+    public function getID () : int {
313
+        return $this->userID;
314
+    }
315
+
316
+    /**
317
+     * get username of user
318
+     *
319
+     * @return string username
320
+     */
321
+    public function getUsername () : string {
322
+        return $this->username;
323
+    }
324
+
325
+    public function getMail () : string {
326
+        return $this->row['mail'];
327
+    }
328
+
329
+    public function isLoggedIn () : bool {
330
+        return $this->isLoggedIn;
331
+    }
332
+
333
+    public function getRow () : array {
334
+        return $this->row;
335
+    }
336
+
337
+    public function setOnline (bool $updateIP = true) {
338
+        //get client ip
339
+        $ip = PHPUtils::getClientIP();
340
+
341
+        if ($updateIP) {
342
+            Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array(
343
+                'userid' => array(
344
+                    'type' => PDO::PARAM_INT,
345
+                    'value' => (int) $this->userID
346
+                ),
347
+                'ip' => $ip
348
+            ));
349
+        } else {
350
+            Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array(
351
+                'userid' => array(
352
+                    'type' => PDO::PARAM_INT,
353
+                    'value' => (int) $this->userID
354
+                )
355
+            ));
356
+        }
357
+    }
358
+
359
+    public function updateOnlineList () {
360
+        $interval_minutes = (int) Settings::get("online_interval", "5");
361
+
362
+        Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; ");
363
+    }
364
+
365
+    /**
366
+     * creates user if userID is absent
367
+     *
368
+     * Only use this method for installation & upgrade!
369
+     */
370
+    public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
371
+        if (self::existsUserID($userID)) {
372
+            //dont create user, if user already exists
373
+            return;
374
+        }
375
+
376
+        //create salt
377
+        $salt = md5(PHPUtils::randomString(50));
378
+
379
+        //generate password hash
380
+        $hashed_password = self::hashPassword($password, $salt);
381
+
382
+        Database::getInstance()->execute("INSERT INTO `{praefix}user` (
383 383
 			`userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `registered`, `activated`
384 384
 		) VALUES (
385 385
 			:userID, :username, :password, :salt, :mail, '0.0.0.0', :main_group, :title, '0', '0000-00-00 00:00:00', CURRENT_TIMESTAMP , :activated
386 386
 		)", array(
387
-			'userID' => $userID,
388
-			'username' => $username,
389
-			'password' => $hashed_password,
390
-			'salt' => $salt,
391
-			'mail' => $mail,
392
-			'main_group' => $main_group,
393
-			'title' => $specific_title,
394
-			'activated' => $activated
395
-		));
396
-	}
397
-
398
-	public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
399
-		if (self::existsUsername($username)) {
400
-			//dont create user, if username already exists
401
-			return false;
402
-		}
403
-
404
-		if (self::existsMail($mail)) {
405
-			//dont create user, if mail already exists
406
-			return false;
407
-		}
408
-
409
-		if (empty($specific_title)) {
410
-			$specific_title = "none";
411
-		}
412
-
413
-		//create salt
414
-		$salt = md5(PHPUtils::randomString(50));
415
-
416
-		//generate password hash
417
-		$hashed_password = self::hashPassword($password, $salt);
418
-
419
-		//create user in database
420
-		Database::getInstance()->execute("INSERT INTO `{praefix}user` (
387
+            'userID' => $userID,
388
+            'username' => $username,
389
+            'password' => $hashed_password,
390
+            'salt' => $salt,
391
+            'mail' => $mail,
392
+            'main_group' => $main_group,
393
+            'title' => $specific_title,
394
+            'activated' => $activated
395
+        ));
396
+    }
397
+
398
+    public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
399
+        if (self::existsUsername($username)) {
400
+            //dont create user, if username already exists
401
+            return false;
402
+        }
403
+
404
+        if (self::existsMail($mail)) {
405
+            //dont create user, if mail already exists
406
+            return false;
407
+        }
408
+
409
+        if (empty($specific_title)) {
410
+            $specific_title = "none";
411
+        }
412
+
413
+        //create salt
414
+        $salt = md5(PHPUtils::randomString(50));
415
+
416
+        //generate password hash
417
+        $hashed_password = self::hashPassword($password, $salt);
418
+
419
+        //create user in database
420
+        Database::getInstance()->execute("INSERT INTO `{praefix}user` (
421 421
 			`userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `registered`, `activated`
422 422
 		) VALUES (
423 423
 			NULL, :username, :password, :salt, :mail, :ip, :main_group, :title, '0', '0000-00-00 00:00:00', CURRENT_TIMESTAMP , :activated
424 424
 		)", array(
425
-			'username' => $username,
426
-			'password' => $hashed_password,
427
-			'salt' => $salt,
428
-			'mail' => $mail,
429
-			'ip' => $ip,
430
-			'main_group' => $main_group,
431
-			'title' => $specific_title,
432
-			'activated' => $activated
433
-		));
434
-
435
-		//get userID
436
-		$userID = self::getIDByUsernameFromDB($username);
437
-
438
-		if ($userID == Settings::get("guest_userid", -1)) {
439
-			//something went wrong
440
-			return false;
441
-		}
442
-
443
-		//add user to group "registered users"
444
-		Groups::addGroupToUser(2, $userID, false);
445
-
446
-		Events::throwEvent("add_user", array(
447
-			'userID' => $userID,
448
-			'username' => &$username,
449
-			'mail' => $mail,
450
-			'main_group' => $main_group
451
-		));
452
-
453
-		return array(
454
-			'success' => true,
455
-			'userID' => $userID,
456
-			'username' => $username,
457
-			'mail' => $mail
458
-		);
459
-	}
460
-
461
-	public static function deleteUserID (int $userID) {
462
-		Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array(
463
-			'userID' => array(
464
-				'type' => PDO::PARAM_INT,
465
-				'value' => $userID
466
-			)
467
-		));
468
-
469
-		//remove user from cache
470
-		Cache::clear("user", "user-" . $userID);
471
-	}
472
-
473
-	public static function existsUserID (int $userID) : bool {
474
-		//search for userID in database
475
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array(
476
-			'userID' => array(
477
-				'type' => PDO::PARAM_INT,
478
-				'value' => $userID
479
-			)
480
-		));
481
-
482
-		return $row !== false;
483
-	}
484
-
485
-	public static function existsUsername (string $username) : bool {
486
-		//search for username in database, ignore case
487
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
488
-
489
-		return $row !== false;
490
-	}
491
-
492
-	public static function existsMail (string $mail) : bool {
493
-		//search for mail in database, ignore case
494
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail));
495
-
496
-		return $row !== false;
497
-	}
498
-
499
-	public static function getIDByUsernameFromDB (string $username) : int {
500
-		//search for username in database, ignore case
501
-		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
502
-
503
-		if ($row === false) {
504
-			//return guest userID
505
-			return Settings::get("guest_userid", -1);
506
-		}
507
-
508
-		return $row['userID'];
509
-	}
510
-
511
-	/**
512
-	 * get instance of current (logged in / guest) user
513
-	 */
514
-	public static function &current () : User {
515
-		if (self::$instance == null) {
516
-			self::$instance = new User();
517
-			self::$instance->load();
518
-		}
519
-
520
-		return self::$instance;
521
-	}
425
+            'username' => $username,
426
+            'password' => $hashed_password,
427
+            'salt' => $salt,
428
+            'mail' => $mail,
429
+            'ip' => $ip,
430
+            'main_group' => $main_group,
431
+            'title' => $specific_title,
432
+            'activated' => $activated
433
+        ));
434
+
435
+        //get userID
436
+        $userID = self::getIDByUsernameFromDB($username);
437
+
438
+        if ($userID == Settings::get("guest_userid", -1)) {
439
+            //something went wrong
440
+            return false;
441
+        }
442
+
443
+        //add user to group "registered users"
444
+        Groups::addGroupToUser(2, $userID, false);
445
+
446
+        Events::throwEvent("add_user", array(
447
+            'userID' => $userID,
448
+            'username' => &$username,
449
+            'mail' => $mail,
450
+            'main_group' => $main_group
451
+        ));
452
+
453
+        return array(
454
+            'success' => true,
455
+            'userID' => $userID,
456
+            'username' => $username,
457
+            'mail' => $mail
458
+        );
459
+    }
460
+
461
+    public static function deleteUserID (int $userID) {
462
+        Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array(
463
+            'userID' => array(
464
+                'type' => PDO::PARAM_INT,
465
+                'value' => $userID
466
+            )
467
+        ));
468
+
469
+        //remove user from cache
470
+        Cache::clear("user", "user-" . $userID);
471
+    }
472
+
473
+    public static function existsUserID (int $userID) : bool {
474
+        //search for userID in database
475
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array(
476
+            'userID' => array(
477
+                'type' => PDO::PARAM_INT,
478
+                'value' => $userID
479
+            )
480
+        ));
481
+
482
+        return $row !== false;
483
+    }
484
+
485
+    public static function existsUsername (string $username) : bool {
486
+        //search for username in database, ignore case
487
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
488
+
489
+        return $row !== false;
490
+    }
491
+
492
+    public static function existsMail (string $mail) : bool {
493
+        //search for mail in database, ignore case
494
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail));
495
+
496
+        return $row !== false;
497
+    }
498
+
499
+    public static function getIDByUsernameFromDB (string $username) : int {
500
+        //search for username in database, ignore case
501
+        $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
502
+
503
+        if ($row === false) {
504
+            //return guest userID
505
+            return Settings::get("guest_userid", -1);
506
+        }
507
+
508
+        return $row['userID'];
509
+    }
510
+
511
+    /**
512
+     * get instance of current (logged in / guest) user
513
+     */
514
+    public static function &current () : User {
515
+        if (self::$instance == null) {
516
+            self::$instance = new User();
517
+            self::$instance->load();
518
+        }
519
+
520
+        return self::$instance;
521
+    }
522 522
 
523 523
 }
524 524
 
Please login to merge, or discard this patch.
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@  discard block
 block discarded – undo
46 46
 		//
47 47
 	}
48 48
 
49
-	public function load (int $userID = -1) {
49
+	public function load(int $userID = -1) {
50 50
 		//check, if user is logged in
51 51
 		if ($userID === -1) {
52 52
 			if (isset($_SESSION['logged-in']) && $_SESSION['logged-in'] === true) {
@@ -142,7 +142,7 @@  discard block
 block discarded – undo
142 142
 		}
143 143
 	}
144 144
 
145
-	public function loginByUsername (string $username, string $password) : array {
145
+	public function loginByUsername(string $username, string $password) : array {
146 146
 		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array(
147 147
 			'username' => &$username
148 148
 		));
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 		return $this->loginRow($row, $password);
151 151
 	}
152 152
 
153
-	public function loginByMail (string $mail, string $password) : array {
153
+	public function loginByMail(string $mail, string $password) : array {
154 154
 		//check, if mail is valide
155 155
 		$validator = new Validator_Mail();
156 156
 
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
 		return $this->loginRow($row, $password);
169 169
 	}
170 170
 
171
-	public function loginByID (int $userID) : array {
171
+	public function loginByID(int $userID) : array {
172 172
 		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array(
173 173
 			'userID' => &$userID
174 174
 		));
@@ -194,7 +194,7 @@  discard block
 block discarded – undo
194 194
 		}
195 195
 	}
196 196
 
197
-	protected function loginRow ($row, string $password) : array {
197
+	protected function loginRow($row, string $password) : array {
198 198
 		if (!$row) {
199 199
 			//user doesnt exists
200 200
 			$res['success'] = false;
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
 		}
252 252
 	}
253 253
 
254
-	protected function setLoggedIn (int $userID, string $username, array $row) {
254
+	protected function setLoggedIn(int $userID, string $username, array $row) {
255 255
 		$_SESSION['logged-in'] = true;
256 256
 		$_SESSION['userID'] = (int) $userID;
257 257
 		$_SESSION['username'] = $username;
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
 		$this->row = $row;
265 265
 	}
266 266
 
267
-	public function logout () {
267
+	public function logout() {
268 268
 		//check, if session was started
269 269
 		PHPUtils::checkSessionStarted();
270 270
 
@@ -279,13 +279,13 @@  discard block
 block discarded – undo
279 279
 		$this->setGuest();
280 280
 	}
281 281
 
282
-	protected function setGuest () {
282
+	protected function setGuest() {
283 283
 		$this->userID = (int) Settings::get("guest_userid", "-1");
284 284
 		$this->username = Settings::get("guest_username", "Guest");
285 285
 		$this->isLoggedIn = false;
286 286
 	}
287 287
 
288
-	protected static function hashPassword ($password, $salt) {
288
+	protected static function hashPassword($password, $salt) {
289 289
 		//http://php.net/manual/de/function.password-hash.php
290 290
 
291 291
 		//add salt to password
@@ -309,7 +309,7 @@  discard block
 block discarded – undo
309 309
 	 *
310 310
 	 * @return integer userID
311 311
 	 */
312
-	public function getID () : int {
312
+	public function getID() : int {
313 313
 		return $this->userID;
314 314
 	}
315 315
 
@@ -318,23 +318,23 @@  discard block
 block discarded – undo
318 318
 	 *
319 319
 	 * @return string username
320 320
 	 */
321
-	public function getUsername () : string {
321
+	public function getUsername() : string {
322 322
 		return $this->username;
323 323
 	}
324 324
 
325
-	public function getMail () : string {
325
+	public function getMail() : string {
326 326
 		return $this->row['mail'];
327 327
 	}
328 328
 
329
-	public function isLoggedIn () : bool {
329
+	public function isLoggedIn() : bool {
330 330
 		return $this->isLoggedIn;
331 331
 	}
332 332
 
333
-	public function getRow () : array {
333
+	public function getRow() : array {
334 334
 		return $this->row;
335 335
 	}
336 336
 
337
-	public function setOnline (bool $updateIP = true) {
337
+	public function setOnline(bool $updateIP = true) {
338 338
 		//get client ip
339 339
 		$ip = PHPUtils::getClientIP();
340 340
 
@@ -356,7 +356,7 @@  discard block
 block discarded – undo
356 356
 		}
357 357
 	}
358 358
 
359
-	public function updateOnlineList () {
359
+	public function updateOnlineList() {
360 360
 		$interval_minutes = (int) Settings::get("online_interval", "5");
361 361
 
362 362
 		Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; ");
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
 	 *
368 368
 	 * Only use this method for installation & upgrade!
369 369
 	 */
370
-	public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
370
+	public static function createIfIdAbsent(int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
371 371
 		if (self::existsUserID($userID)) {
372 372
 			//dont create user, if user already exists
373 373
 			return;
@@ -395,7 +395,7 @@  discard block
 block discarded – undo
395 395
 		));
396 396
 	}
397 397
 
398
-	public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
398
+	public static function create(string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1) {
399 399
 		if (self::existsUsername($username)) {
400 400
 			//dont create user, if username already exists
401 401
 			return false;
@@ -458,7 +458,7 @@  discard block
 block discarded – undo
458 458
 		);
459 459
 	}
460 460
 
461
-	public static function deleteUserID (int $userID) {
461
+	public static function deleteUserID(int $userID) {
462 462
 		Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array(
463 463
 			'userID' => array(
464 464
 				'type' => PDO::PARAM_INT,
@@ -470,7 +470,7 @@  discard block
 block discarded – undo
470 470
 		Cache::clear("user", "user-" . $userID);
471 471
 	}
472 472
 
473
-	public static function existsUserID (int $userID) : bool {
473
+	public static function existsUserID(int $userID) : bool {
474 474
 		//search for userID in database
475 475
 		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array(
476 476
 			'userID' => array(
@@ -482,21 +482,21 @@  discard block
 block discarded – undo
482 482
 		return $row !== false;
483 483
 	}
484 484
 
485
-	public static function existsUsername (string $username) : bool {
485
+	public static function existsUsername(string $username) : bool {
486 486
 		//search for username in database, ignore case
487 487
 		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
488 488
 
489 489
 		return $row !== false;
490 490
 	}
491 491
 
492
-	public static function existsMail (string $mail) : bool {
492
+	public static function existsMail(string $mail) : bool {
493 493
 		//search for mail in database, ignore case
494 494
 		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail));
495 495
 
496 496
 		return $row !== false;
497 497
 	}
498 498
 
499
-	public static function getIDByUsernameFromDB (string $username) : int {
499
+	public static function getIDByUsernameFromDB(string $username) : int {
500 500
 		//search for username in database, ignore case
501 501
 		$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username));
502 502
 
@@ -511,7 +511,7 @@  discard block
 block discarded – undo
511 511
 	/**
512 512
 	 * get instance of current (logged in / guest) user
513 513
 	 */
514
-	public static function &current () : User {
514
+	public static function &current() : User {
515 515
 		if (self::$instance == null) {
516 516
 			self::$instance = new User();
517 517
 			self::$instance->load();
Please login to merge, or discard this patch.