public function testRequestedUrl() { fSession::set('fAuthorization::requested_url', 'test_url.php?query_string=TRUE'); $this->assertEquals('test_url.php?query_string=TRUE', fAuthorization::getRequestedURL(FALSE)); $this->assertEquals('test_url.php?query_string=TRUE', fAuthorization::getRequestedURL(TRUE)); $this->assertEquals(NULL, fAuthorization::getRequestedURL(TRUE)); $this->assertEquals('test_url2.php?query_string=TRUE', fAuthorization::getRequestedURL(TRUE, 'test_url2.php?query_string=TRUE')); }
$action = fRequest::get('action'); // --------------------------------- // if ('log_out' == $action) { fAuthorization::destroyUserInfo(); fSession::destroy(); fMessaging::create('success', User::makeUrl('login'), 'You were successfully logged out'); fURL::redirect(User::makeUrl('login')); // --------------------------------- // } else { if (!fAuthorization::checkLoggedIn()) { if (fRequest::isPost()) { try { $user = new User(array('username' => fRequest::get('username'))); $valid_pass = fCryptography::checkPasswordHash(fRequest::get('password'), $user->getPassword()); if (!$valid_pass) { throw new fValidationException('The login or password entered is invalid'); } fAuthorization::setUserToken($user->getEmail()); fAuthorization::setUserAuthLevel($user->getRole()); fSession::set('user_id', $user->getUserId()); fSession::set('user_name', $user->getUsername()); fURL::redirect(fAuthorization::getRequestedURL(TRUE, 'index.php')); } catch (fExpectedException $e) { fMessaging::create('error', fURL::get(), $e->getMessage()); } } include VIEW_PATH . '/log_in.php'; } else { fURL::redirect('index.php'); } }
public function login() { $username = trim(fRequest::get('username', 'string')); $password = fRequest::get('password', 'string'); $password_hash = static::hashPassword($password); try { if (fRequest::get('action') == '登录') { $user = new User($username); if ($user->getPassword() == $password_hash) { fAuthorization::setUserToken($user->getUsername()); fMessaging::create('success', 'Logged in successfully.'); fURL::redirect(fAuthorization::getRequestedURL(TRUE, Util::getReferer())); } else { throw new fValidationException('Password mismatch.'); } } else { if (fRequest::get('action') == '注册') { if (strlen($username) < 4) { throw new fValidationException('Username is too short.'); } if (strlen($username) > 20) { throw new fValidationException('Username is too long.'); } if (strlen($password) < 6) { throw new fValidationException('Password is too short.'); } if (Util::contains('`~!@#$%^&*()-+=[]\\;\',/{}|:"<>?', $username) or preg_match('/\\s/', $username)) { throw new fValidationException('Username is illegal.'); } $realname = trim(fRequest::get('realname', 'string')); $gender = trim(fRequest::get('gender', 'string')); $school = trim(fRequest::get('school', 'string')); $major = trim(fRequest::get('major', 'string')); $grade = trim(fRequest::get('grade', 'integer', NULL)); $phone = trim(fRequest::get('phone', 'string')); $qq = trim(fRequest::get('qq', 'string')); if (strlen($realname) < 1) { throw new fValidationException('请填写真实姓名'); } if (strlen($gender) < 1) { throw new fValidationException('请选择性别'); } if (strlen($phone) < 1) { throw new fValidationException('请填写手机号码'); } try { $user = new User($username); throw new fValidationException('User already exists.'); } catch (fNotFoundException $e) { $user = new User(); $user->setUsername($username); $user->setPassword($password_hash); $user->store(); try { $profile = new Profile($username); } catch (fNotFoundException $e) { $profile = new Profile(); $profile->setUsername($username); } $profile->setRealname($realname); $profile->setGender($gender); $profile->setSchool($school); $profile->setMajor($major); $profile->setGrade($grade); $profile->setPhoneNumber($phone); $profile->setQq($qq); $profile->store(); fAuthorization::setUserToken($user->getUsername()); fMessaging::create('success', 'Registered successfully.'); Util::redirect('/email/verify'); } } } } catch (fException $e) { fMessaging::create('error', $e->getMessage()); fURL::redirect(fAuthorization::getRequestedURL(TRUE, Util::getReferer())); } }
<?php include_once __DIR__ . '/inc/init.php'; if (fAuthorization::checkLoggedIn()) { fURL::redirect(fAuthorization::getRequestedURL(false, SITE_BASE)); } else { $errmsg = ''; $username = ''; if (fRequest::isPost()) { $username = fRequest::get('username'); $password = fRequest::get('password'); if (empty($username)) { $errmsg = '请输入用户名'; } else { if (empty($password)) { $errmsg = '请输入密码'; } else { if (!login_authenticate($db, $username, $password)) { $errmsg = '登录失败'; } else { fURL::redirect(fAuthorization::getRequestedURL(false, SITE_BASE)); } } } } include __DIR__ . '/tpl/login.php'; }