@@ -27,606 +27,606 @@ |
||
| 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 | - //check password |
|
| 288 | - if ($authentificator->checkPasswordAndImport($row['username'], $password) !== -1) { |
|
| 289 | - //password is correct |
|
| 290 | - |
|
| 291 | - //set online state |
|
| 292 | - $this->setOnline(); |
|
| 293 | - |
|
| 294 | - //set logged in |
|
| 295 | - $this->setLoggedIn($row['userID'], $row['username'], $row); |
|
| 296 | - |
|
| 297 | - //login successful |
|
| 298 | - $res['success'] = true; |
|
| 299 | - $res['error'] = "none"; |
|
| 300 | - return $res; |
|
| 301 | - } else { |
|
| 302 | - //wrong password |
|
| 303 | - |
|
| 304 | - //user doesnt exists |
|
| 305 | - $res['success'] = false; |
|
| 306 | - $res['error'] = "wrong_password"; |
|
| 307 | - |
|
| 308 | - return $res; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - protected function setLoggedIn (int $userID, string $username, array $row) { |
|
| 313 | - $_SESSION['logged-in'] = true; |
|
| 314 | - $_SESSION['userID'] = (int) $userID; |
|
| 315 | - $_SESSION['username'] = $username; |
|
| 316 | - |
|
| 317 | - //remove password hash from row (so password isnt cached) |
|
| 318 | - unset($row['password']); |
|
| 319 | - |
|
| 320 | - $this->userID = $userID; |
|
| 321 | - $this->username = $username; |
|
| 322 | - $this->row = $row; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - public function logout () { |
|
| 326 | - //check, if session was started |
|
| 327 | - PHPUtils::checkSessionStarted(); |
|
| 328 | - |
|
| 329 | - unset($_SESSION['userID']); |
|
| 330 | - unset($_SESSION['username']); |
|
| 331 | - |
|
| 332 | - $_SESSION['logged-in'] = false; |
|
| 333 | - |
|
| 334 | - $this->setGuest(); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - protected function setGuest () { |
|
| 338 | - $this->userID = (int) Settings::get("guest_userid", "-1"); |
|
| 339 | - $this->username = Settings::get("guest_username", "Guest"); |
|
| 340 | - $this->isLoggedIn = false; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - protected static function hashPassword ($password, $salt) { |
|
| 344 | - //http://php.net/manual/de/function.password-hash.php |
|
| 345 | - |
|
| 346 | - //add salt to password |
|
| 347 | - $password .= $salt; |
|
| 348 | - |
|
| 349 | - $options = array( |
|
| 350 | - 'cost' => (int) Settings::get("password_hash_cost", "10") |
|
| 351 | - ); |
|
| 352 | - $algo = PASSWORD_DEFAULT; |
|
| 353 | - |
|
| 354 | - Events::throwEvent("hashing_password", array( |
|
| 355 | - 'options' => &$options, |
|
| 356 | - 'algo' => &$algo |
|
| 357 | - )); |
|
| 358 | - |
|
| 359 | - return password_hash($password, $algo, $options); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * get user ID of user |
|
| 364 | - * |
|
| 365 | - * @return integer userID |
|
| 366 | - */ |
|
| 367 | - public function getID () : int { |
|
| 368 | - return $this->userID; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * get username of user |
|
| 373 | - * |
|
| 374 | - * @return string username |
|
| 375 | - */ |
|
| 376 | - public function getUsername () : string { |
|
| 377 | - return $this->username; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - public function getMail () : string { |
|
| 381 | - return $this->row['mail']; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - public function isLoggedIn () : bool { |
|
| 385 | - return $this->isLoggedIn; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - public function getRow () : array { |
|
| 389 | - return $this->row; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - public function setOnline (bool $updateIP = true) { |
|
| 393 | - //get client ip |
|
| 394 | - $ip = PHPUtils::getClientIP(); |
|
| 395 | - |
|
| 396 | - if ($updateIP) { |
|
| 397 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array( |
|
| 398 | - 'userid' => array( |
|
| 399 | - 'type' => PDO::PARAM_INT, |
|
| 400 | - 'value' => (int) $this->userID |
|
| 401 | - ), |
|
| 402 | - 'ip' => $ip |
|
| 403 | - )); |
|
| 404 | - } else { |
|
| 405 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array( |
|
| 406 | - 'userid' => array( |
|
| 407 | - 'type' => PDO::PARAM_INT, |
|
| 408 | - 'value' => (int) $this->userID |
|
| 409 | - ) |
|
| 410 | - )); |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - public function updateOnlineList () { |
|
| 415 | - $interval_minutes = (int) Settings::get("online_interval", "5"); |
|
| 416 | - |
|
| 417 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; "); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * creates user if userID is absent |
|
| 422 | - * |
|
| 423 | - * Only use this method for installation & upgrade! |
|
| 424 | - */ |
|
| 425 | - public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
| 426 | - if (self::existsUserID($userID)) { |
|
| 427 | - //dont create user, if user already exists |
|
| 428 | - return; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - //create salt |
|
| 432 | - $salt = md5(PHPUtils::randomString(50)); |
|
| 433 | - |
|
| 434 | - //generate password hash |
|
| 435 | - $hashed_password = self::hashPassword($password, $salt); |
|
| 436 | - |
|
| 437 | - 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 | + //check password |
|
| 288 | + if ($authentificator->checkPasswordAndImport($row['username'], $password) !== -1) { |
|
| 289 | + //password is correct |
|
| 290 | + |
|
| 291 | + //set online state |
|
| 292 | + $this->setOnline(); |
|
| 293 | + |
|
| 294 | + //set logged in |
|
| 295 | + $this->setLoggedIn($row['userID'], $row['username'], $row); |
|
| 296 | + |
|
| 297 | + //login successful |
|
| 298 | + $res['success'] = true; |
|
| 299 | + $res['error'] = "none"; |
|
| 300 | + return $res; |
|
| 301 | + } else { |
|
| 302 | + //wrong password |
|
| 303 | + |
|
| 304 | + //user doesnt exists |
|
| 305 | + $res['success'] = false; |
|
| 306 | + $res['error'] = "wrong_password"; |
|
| 307 | + |
|
| 308 | + return $res; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + protected function setLoggedIn (int $userID, string $username, array $row) { |
|
| 313 | + $_SESSION['logged-in'] = true; |
|
| 314 | + $_SESSION['userID'] = (int) $userID; |
|
| 315 | + $_SESSION['username'] = $username; |
|
| 316 | + |
|
| 317 | + //remove password hash from row (so password isnt cached) |
|
| 318 | + unset($row['password']); |
|
| 319 | + |
|
| 320 | + $this->userID = $userID; |
|
| 321 | + $this->username = $username; |
|
| 322 | + $this->row = $row; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + public function logout () { |
|
| 326 | + //check, if session was started |
|
| 327 | + PHPUtils::checkSessionStarted(); |
|
| 328 | + |
|
| 329 | + unset($_SESSION['userID']); |
|
| 330 | + unset($_SESSION['username']); |
|
| 331 | + |
|
| 332 | + $_SESSION['logged-in'] = false; |
|
| 333 | + |
|
| 334 | + $this->setGuest(); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + protected function setGuest () { |
|
| 338 | + $this->userID = (int) Settings::get("guest_userid", "-1"); |
|
| 339 | + $this->username = Settings::get("guest_username", "Guest"); |
|
| 340 | + $this->isLoggedIn = false; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + protected static function hashPassword ($password, $salt) { |
|
| 344 | + //http://php.net/manual/de/function.password-hash.php |
|
| 345 | + |
|
| 346 | + //add salt to password |
|
| 347 | + $password .= $salt; |
|
| 348 | + |
|
| 349 | + $options = array( |
|
| 350 | + 'cost' => (int) Settings::get("password_hash_cost", "10") |
|
| 351 | + ); |
|
| 352 | + $algo = PASSWORD_DEFAULT; |
|
| 353 | + |
|
| 354 | + Events::throwEvent("hashing_password", array( |
|
| 355 | + 'options' => &$options, |
|
| 356 | + 'algo' => &$algo |
|
| 357 | + )); |
|
| 358 | + |
|
| 359 | + return password_hash($password, $algo, $options); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * get user ID of user |
|
| 364 | + * |
|
| 365 | + * @return integer userID |
|
| 366 | + */ |
|
| 367 | + public function getID () : int { |
|
| 368 | + return $this->userID; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * get username of user |
|
| 373 | + * |
|
| 374 | + * @return string username |
|
| 375 | + */ |
|
| 376 | + public function getUsername () : string { |
|
| 377 | + return $this->username; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + public function getMail () : string { |
|
| 381 | + return $this->row['mail']; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + public function isLoggedIn () : bool { |
|
| 385 | + return $this->isLoggedIn; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + public function getRow () : array { |
|
| 389 | + return $this->row; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + public function setOnline (bool $updateIP = true) { |
|
| 393 | + //get client ip |
|
| 394 | + $ip = PHPUtils::getClientIP(); |
|
| 395 | + |
|
| 396 | + if ($updateIP) { |
|
| 397 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, `ip` = :ip WHERE `userid` = :userid; ", array( |
|
| 398 | + 'userid' => array( |
|
| 399 | + 'type' => PDO::PARAM_INT, |
|
| 400 | + 'value' => (int) $this->userID |
|
| 401 | + ), |
|
| 402 | + 'ip' => $ip |
|
| 403 | + )); |
|
| 404 | + } else { |
|
| 405 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '1', `last_online` = CURRENT_TIMESTAMP, WHERE `userid` = :userid; ", array( |
|
| 406 | + 'userid' => array( |
|
| 407 | + 'type' => PDO::PARAM_INT, |
|
| 408 | + 'value' => (int) $this->userID |
|
| 409 | + ) |
|
| 410 | + )); |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + public function updateOnlineList () { |
|
| 415 | + $interval_minutes = (int) Settings::get("online_interval", "5"); |
|
| 416 | + |
|
| 417 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `online` = '0' WHERE DATE_SUB(NOW(), INTERVAL " . $interval_minutes . " MINUTE) > `last_online`; "); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * creates user if userID is absent |
|
| 422 | + * |
|
| 423 | + * Only use this method for installation & upgrade! |
|
| 424 | + */ |
|
| 425 | + public static function createIfIdAbsent (int $userID, string $username, string $password, string $mail, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
| 426 | + if (self::existsUserID($userID)) { |
|
| 427 | + //dont create user, if user already exists |
|
| 428 | + return; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + //create salt |
|
| 432 | + $salt = md5(PHPUtils::randomString(50)); |
|
| 433 | + |
|
| 434 | + //generate password hash |
|
| 435 | + $hashed_password = self::hashPassword($password, $salt); |
|
| 436 | + |
|
| 437 | + Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
| 438 | 438 | `userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `registered`, `activated` |
| 439 | 439 | ) VALUES ( |
| 440 | 440 | :userID, :username, :password, :salt, :mail, '0.0.0.0', :main_group, :title, '0', '0000-00-00 00:00:00', CURRENT_TIMESTAMP , :activated |
| 441 | 441 | )", array( |
| 442 | - 'userID' => $userID, |
|
| 443 | - 'username' => $username, |
|
| 444 | - 'password' => $hashed_password, |
|
| 445 | - 'salt' => $salt, |
|
| 446 | - 'mail' => $mail, |
|
| 447 | - 'main_group' => $main_group, |
|
| 448 | - 'title' => $specific_title, |
|
| 449 | - 'activated' => $activated |
|
| 450 | - )); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
| 454 | - if (self::existsUsername($username)) { |
|
| 455 | - //dont create user, if username already exists |
|
| 456 | - return false; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - if (self::existsMail($mail)) { |
|
| 460 | - //dont create user, if mail already exists |
|
| 461 | - return false; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - if (empty($specific_title)) { |
|
| 465 | - $specific_title = "none"; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - //create salt |
|
| 469 | - $salt = md5(PHPUtils::randomString(50)); |
|
| 470 | - |
|
| 471 | - //generate password hash |
|
| 472 | - $hashed_password = self::hashPassword($password, $salt); |
|
| 473 | - |
|
| 474 | - //create user in database |
|
| 475 | - Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
| 442 | + 'userID' => $userID, |
|
| 443 | + 'username' => $username, |
|
| 444 | + 'password' => $hashed_password, |
|
| 445 | + 'salt' => $salt, |
|
| 446 | + 'mail' => $mail, |
|
| 447 | + 'main_group' => $main_group, |
|
| 448 | + 'title' => $specific_title, |
|
| 449 | + 'activated' => $activated |
|
| 450 | + )); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + public static function create (string $username, string $password, string $mail, string $ip, int $main_group = 2, string $specific_title = "none", int $activated = 1) { |
|
| 454 | + if (self::existsUsername($username)) { |
|
| 455 | + //dont create user, if username already exists |
|
| 456 | + return false; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + if (self::existsMail($mail)) { |
|
| 460 | + //dont create user, if mail already exists |
|
| 461 | + return false; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + if (empty($specific_title)) { |
|
| 465 | + $specific_title = "none"; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + //create salt |
|
| 469 | + $salt = md5(PHPUtils::randomString(50)); |
|
| 470 | + |
|
| 471 | + //generate password hash |
|
| 472 | + $hashed_password = self::hashPassword($password, $salt); |
|
| 473 | + |
|
| 474 | + //create user in database |
|
| 475 | + Database::getInstance()->execute("INSERT INTO `{praefix}user` ( |
|
| 476 | 476 | `userID`, `username`, `password`, `salt`, `mail`, `ip`, `main_group`, `specific_title`, `online`, `last_online`, `registered`, `activated` |
| 477 | 477 | ) VALUES ( |
| 478 | 478 | NULL, :username, :password, :salt, :mail, :ip, :main_group, :title, '0', '0000-00-00 00:00:00', CURRENT_TIMESTAMP , :activated |
| 479 | 479 | )", array( |
| 480 | - 'username' => $username, |
|
| 481 | - 'password' => $hashed_password, |
|
| 482 | - 'salt' => $salt, |
|
| 483 | - 'mail' => $mail, |
|
| 484 | - 'ip' => $ip, |
|
| 485 | - 'main_group' => $main_group, |
|
| 486 | - 'title' => $specific_title, |
|
| 487 | - 'activated' => $activated |
|
| 488 | - )); |
|
| 489 | - |
|
| 490 | - //get userID |
|
| 491 | - $userID = self::getIDByUsernameFromDB($username); |
|
| 492 | - |
|
| 493 | - if ($userID == Settings::get("guest_userid", -1)) { |
|
| 494 | - //something went wrong |
|
| 495 | - return false; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - //add user to group "registered users" |
|
| 499 | - Groups::addGroupToUser(2, $userID, false); |
|
| 500 | - |
|
| 501 | - Events::throwEvent("add_user", array( |
|
| 502 | - 'userID' => $userID, |
|
| 503 | - 'username' => &$username, |
|
| 504 | - 'mail' => $mail, |
|
| 505 | - 'main_group' => $main_group |
|
| 506 | - )); |
|
| 507 | - |
|
| 508 | - return array( |
|
| 509 | - 'success' => true, |
|
| 510 | - 'userID' => $userID, |
|
| 511 | - 'username' => $username, |
|
| 512 | - 'mail' => $mail |
|
| 513 | - ); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - public static function deleteUserID (int $userID) { |
|
| 517 | - Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
| 518 | - 'userID' => array( |
|
| 519 | - 'type' => PDO::PARAM_INT, |
|
| 520 | - 'value' => $userID |
|
| 521 | - ) |
|
| 522 | - )); |
|
| 523 | - |
|
| 524 | - //remove user from cache |
|
| 525 | - Cache::clear("user", "user-" . $userID); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - public static function existsUserID (int $userID) : bool { |
|
| 529 | - //search for userID in database |
|
| 530 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
| 531 | - 'userID' => array( |
|
| 532 | - 'type' => PDO::PARAM_INT, |
|
| 533 | - 'value' => $userID |
|
| 534 | - ) |
|
| 535 | - )); |
|
| 536 | - |
|
| 537 | - return $row !== false; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - public static function existsUsername (string $username) : bool { |
|
| 541 | - //search for username in database, ignore case |
|
| 542 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
| 543 | - |
|
| 544 | - return $row !== false; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - public static function existsMail (string $mail) : bool { |
|
| 548 | - //search for mail in database, ignore case |
|
| 549 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail)); |
|
| 550 | - |
|
| 551 | - return $row !== false; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - public static function getIDByUsernameFromDB (string $username) : int { |
|
| 555 | - //search for username in database, ignore case |
|
| 556 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
| 557 | - |
|
| 558 | - if ($row === false) { |
|
| 559 | - //return guest userID |
|
| 560 | - return Settings::get("guest_userid", -1); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - return $row['userID']; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - public static function &getAuthentificatorByID (int $userID = -1) { |
|
| 567 | - if ($userID == -1) { |
|
| 568 | - //get default authentificator |
|
| 569 | - return self::getDefaultAuthentificator(); |
|
| 570 | - } else { |
|
| 571 | - //get authentificator class |
|
| 572 | - |
|
| 573 | - //check, if user exists |
|
| 574 | - if (!self::existsUserID($userID)) { |
|
| 575 | - throw new IllegalStateException("user with userID '" . $userID . "' doesnt exists."); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
| 579 | - 'userID' => &$userID |
|
| 580 | - )); |
|
| 581 | - |
|
| 582 | - $class_name = $row['authentificator']; |
|
| 583 | - return new $class_name(); |
|
| 584 | - } |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - public static function &getAuthentificatorByUsername (string $username = "") { |
|
| 588 | - if ($username == null || empty($username)) { |
|
| 589 | - //get default authentificator |
|
| 590 | - return self::getDefaultAuthentificator(); |
|
| 591 | - } else { |
|
| 592 | - //get authentificator class |
|
| 593 | - |
|
| 594 | - //check, if user exists |
|
| 595 | - if (!self::existsUsername($username)) { |
|
| 596 | - throw new IllegalStateException("user with username '" . $username . "' doesnt exists."); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
| 600 | - 'username' => &$username |
|
| 601 | - )); |
|
| 602 | - |
|
| 603 | - $class_name = $row['authentificator']; |
|
| 604 | - return new $class_name(); |
|
| 605 | - } |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - public static function &getDefaultAuthentificator () : IAuthentificator { |
|
| 609 | - if (self::$default_authentificator == null) { |
|
| 610 | - $class_name = Settings::get("default_authentificator", "LocalAuthentificator"); |
|
| 611 | - $obj = new $class_name(); |
|
| 612 | - |
|
| 613 | - self::$default_authentificator = $obj; |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - return self::$default_authentificator; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - /** |
|
| 620 | - * get instance of current (logged in / guest) user |
|
| 621 | - */ |
|
| 622 | - public static function ¤t () : User { |
|
| 623 | - if (self::$instance == null) { |
|
| 624 | - self::$instance = new User(); |
|
| 625 | - self::$instance->load(); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - return self::$instance; |
|
| 629 | - } |
|
| 480 | + 'username' => $username, |
|
| 481 | + 'password' => $hashed_password, |
|
| 482 | + 'salt' => $salt, |
|
| 483 | + 'mail' => $mail, |
|
| 484 | + 'ip' => $ip, |
|
| 485 | + 'main_group' => $main_group, |
|
| 486 | + 'title' => $specific_title, |
|
| 487 | + 'activated' => $activated |
|
| 488 | + )); |
|
| 489 | + |
|
| 490 | + //get userID |
|
| 491 | + $userID = self::getIDByUsernameFromDB($username); |
|
| 492 | + |
|
| 493 | + if ($userID == Settings::get("guest_userid", -1)) { |
|
| 494 | + //something went wrong |
|
| 495 | + return false; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + //add user to group "registered users" |
|
| 499 | + Groups::addGroupToUser(2, $userID, false); |
|
| 500 | + |
|
| 501 | + Events::throwEvent("add_user", array( |
|
| 502 | + 'userID' => $userID, |
|
| 503 | + 'username' => &$username, |
|
| 504 | + 'mail' => $mail, |
|
| 505 | + 'main_group' => $main_group |
|
| 506 | + )); |
|
| 507 | + |
|
| 508 | + return array( |
|
| 509 | + 'success' => true, |
|
| 510 | + 'userID' => $userID, |
|
| 511 | + 'username' => $username, |
|
| 512 | + 'mail' => $mail |
|
| 513 | + ); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + public static function deleteUserID (int $userID) { |
|
| 517 | + Database::getInstance()->execute("DELETE FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
| 518 | + 'userID' => array( |
|
| 519 | + 'type' => PDO::PARAM_INT, |
|
| 520 | + 'value' => $userID |
|
| 521 | + ) |
|
| 522 | + )); |
|
| 523 | + |
|
| 524 | + //remove user from cache |
|
| 525 | + Cache::clear("user", "user-" . $userID); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + public static function existsUserID (int $userID) : bool { |
|
| 529 | + //search for userID in database |
|
| 530 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID; ", array( |
|
| 531 | + 'userID' => array( |
|
| 532 | + 'type' => PDO::PARAM_INT, |
|
| 533 | + 'value' => $userID |
|
| 534 | + ) |
|
| 535 | + )); |
|
| 536 | + |
|
| 537 | + return $row !== false; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + public static function existsUsername (string $username) : bool { |
|
| 541 | + //search for username in database, ignore case |
|
| 542 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
| 543 | + |
|
| 544 | + return $row !== false; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + public static function existsMail (string $mail) : bool { |
|
| 548 | + //search for mail in database, ignore case |
|
| 549 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`mail`) LIKE UPPER(:mail); ", array('mail' => $mail)); |
|
| 550 | + |
|
| 551 | + return $row !== false; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + public static function getIDByUsernameFromDB (string $username) : int { |
|
| 555 | + //search for username in database, ignore case |
|
| 556 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE UPPER(`username`) LIKE UPPER(:username); ", array('username' => $username)); |
|
| 557 | + |
|
| 558 | + if ($row === false) { |
|
| 559 | + //return guest userID |
|
| 560 | + return Settings::get("guest_userid", -1); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + return $row['userID']; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + public static function &getAuthentificatorByID (int $userID = -1) { |
|
| 567 | + if ($userID == -1) { |
|
| 568 | + //get default authentificator |
|
| 569 | + return self::getDefaultAuthentificator(); |
|
| 570 | + } else { |
|
| 571 | + //get authentificator class |
|
| 572 | + |
|
| 573 | + //check, if user exists |
|
| 574 | + if (!self::existsUserID($userID)) { |
|
| 575 | + throw new IllegalStateException("user with userID '" . $userID . "' doesnt exists."); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `userID` = :userID AND `activated` = '1'; ", array( |
|
| 579 | + 'userID' => &$userID |
|
| 580 | + )); |
|
| 581 | + |
|
| 582 | + $class_name = $row['authentificator']; |
|
| 583 | + return new $class_name(); |
|
| 584 | + } |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + public static function &getAuthentificatorByUsername (string $username = "") { |
|
| 588 | + if ($username == null || empty($username)) { |
|
| 589 | + //get default authentificator |
|
| 590 | + return self::getDefaultAuthentificator(); |
|
| 591 | + } else { |
|
| 592 | + //get authentificator class |
|
| 593 | + |
|
| 594 | + //check, if user exists |
|
| 595 | + if (!self::existsUsername($username)) { |
|
| 596 | + throw new IllegalStateException("user with username '" . $username . "' doesnt exists."); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
| 600 | + 'username' => &$username |
|
| 601 | + )); |
|
| 602 | + |
|
| 603 | + $class_name = $row['authentificator']; |
|
| 604 | + return new $class_name(); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + public static function &getDefaultAuthentificator () : IAuthentificator { |
|
| 609 | + if (self::$default_authentificator == null) { |
|
| 610 | + $class_name = Settings::get("default_authentificator", "LocalAuthentificator"); |
|
| 611 | + $obj = new $class_name(); |
|
| 612 | + |
|
| 613 | + self::$default_authentificator = $obj; |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + return self::$default_authentificator; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + /** |
|
| 620 | + * get instance of current (logged in / guest) user |
|
| 621 | + */ |
|
| 622 | + public static function ¤t () : User { |
|
| 623 | + if (self::$instance == null) { |
|
| 624 | + self::$instance = new User(); |
|
| 625 | + self::$instance->load(); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + return self::$instance; |
|
| 629 | + } |
|
| 630 | 630 | |
| 631 | 631 | } |
| 632 | 632 | |