/** * Verifies registration data from user. * * @param String $username Input username. * @param String $password Input password. * @param String $passwordRepeat Input repeated password. * @param String $name Input name. Can be empty. * @param String $contact Input contact. Can be empty. * * @throws RUsernameAndPasswordLengthException When $username length is less than 3 * AND $password length is less than 6. * @throws RPasswordLengthException When $password length is less than 6. * @throws RUsernameLengthException When $username length is less than 3. * @throws RPasswordMismatchException When $password and $passwordRepeat are * NOT equal. * @throws RUserExistsException When $username already exist in the database. * @throws RInvalidCharactersException When $username contains invalid characters. * * @return boolean True if registration is successful. */ public function verifyRegisterCredentials($username, $password, $passwordRepeat, $name, $contact) { if (strlen($username) < 3 && strlen($password) < 6) { throw new RUsernameAndPasswordLengthException(); } else { if (strlen($password) < 6) { throw new RPasswordLengthException(); } else { if (strlen($username) < 3) { throw new RUsernameLengthException(); } else { if ($password !== $passwordRepeat) { throw new RPasswordMismatchException(); } else { if ($this->dal->findUserByUsername($username)) { throw new RUserExistsException(); } else { if (preg_match("/^[0-9A-Za-z_]+\$/", $username) == 0) { throw new RInvalidCharactersException(); } else { $user = new User($username, $password); $infoJSON = '{"name": "' . $name . '", "contact": "' . $contact . '"}'; $user->setProfile($infoJSON); $this->dal->add($user); return true; } } } } } } }
/** * Verifies login data stored in cookies. * * @param String $cookieName Username stored in cookie. * @param String $cookiePassword Password stored in cookie. * * @throws LWrongCookieInformationException When user does not exist in database, or if user exist * but password does not match. */ public function verifyPersistentLogin($cookieName, $cookiePassword) { if (!$this->dal->findUserByUsername($cookieName)) { throw new LWrongCookieInformationException(); } else { $user = $this->dal->findUserByUsername($cookieName); if (base64_encode($user->getPassword()) == $cookiePassword) { if (!isset($_SESSION[self::$loggedIn])) { $_SESSION[self::$loggedIn] = true; } } else { throw new LWrongCookieInformationException(); } } }