S2OJ/web/app/models/Auth.php

159 lines
3.2 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
class Auth {
public static function check() {
global $myUser;
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
return $myUser !== null;
2016-07-18 16:39:37 +00:00
}
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
public static function id() {
global $myUser;
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
if ($myUser === null) {
return null;
}
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
return $myUser['username'];
}
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
public static function user() {
global $myUser;
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
return $myUser;
}
2023-02-13 07:45:06 +00:00
2023-01-30 03:53:02 +00:00
public static function property($name) {
global $myUser;
2023-02-13 07:45:06 +00:00
2023-01-30 03:53:02 +00:00
if (!$myUser) {
return false;
}
2023-02-13 07:45:06 +00:00
if (!isset($myUser[$name])) {
return false;
}
2023-01-30 03:53:02 +00:00
return $myUser[$name];
}
2023-02-13 07:45:06 +00:00
2023-02-13 14:01:17 +00:00
public static function login($username) {
2016-07-18 16:39:37 +00:00
if (!validateUsername($username)) {
return;
}
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
$_SESSION['username'] = $username;
2023-02-13 07:45:06 +00:00
2023-02-13 14:01:17 +00:00
$remember_token = DB::selectSingle([
"select remember_token from user_info",
"where", ["username" => $username]
]);
if ($remember_token == '') {
$remember_token = uojRandString(60);
DB::update([
"update user_info",
"set", ["remember_token" => $remember_token],
2023-01-30 03:53:02 +00:00
"where", ["username" => $username]
]);
2023-02-13 14:01:17 +00:00
}
2023-01-30 03:53:02 +00:00
2023-02-13 14:01:17 +00:00
$_SESSION['last_login'] = time();
$_SESSION['remember_token'] = $remember_token;
2023-02-13 07:45:06 +00:00
2023-02-13 14:01:17 +00:00
$expire = time() + 60 * 60 * 24 * 7;
Cookie::safeSet('uoj_username', $username, $expire, '/', array('httponly' => true));
Cookie::safeSet('uoj_remember_token', $remember_token, $expire, '/', array('httponly' => true));
2022-11-06 02:26:21 +00:00
DB::update([
"update user_info",
"set", ["last_login_time" => UOJTime::$time_now_str],
"where", ["username" => $username]
]);
2023-02-13 07:45:06 +00:00
session_regenerate_id(true);
2016-07-18 16:39:37 +00:00
}
2023-01-30 03:53:02 +00:00
2016-07-18 16:39:37 +00:00
public static function logout() {
2023-01-30 03:53:02 +00:00
session_unset();
2023-02-13 07:45:06 +00:00
2023-02-13 14:01:17 +00:00
Cookie::unsetVar(session_name(), '/');
2016-07-18 16:39:37 +00:00
Cookie::safeUnset('uoj_username', '/');
Cookie::safeUnset('uoj_remember_token', '/');
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
DB::update([
2023-01-30 03:53:02 +00:00
"update user_info",
"set", ["remember_token" => ''],
"where", ["username" => Auth::id()]
]);
2016-07-18 16:39:37 +00:00
}
private static function initMyUser() {
global $myUser;
$myUser = null;
2023-01-30 03:53:02 +00:00
2016-07-18 16:39:37 +00:00
Cookie::safeCheck('uoj_username', '/');
Cookie::safeCheck('uoj_remember_token', '/');
2023-01-30 03:53:02 +00:00
2016-07-18 16:39:37 +00:00
if (isset($_SESSION['username'])) {
if (!validateUsername($_SESSION['username'])) {
return;
}
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
$myUser = UOJUser::query($_SESSION['username']);
2023-02-13 07:45:06 +00:00
// 当 remember_token 不同时,注销登录
if ($_SESSION['remember_token'] !== $myUser['remember_token']) {
$myUser = null;
return;
}
2016-07-18 16:39:37 +00:00
return;
}
$remember_token = Cookie::safeGet('uoj_remember_token', '/');
if ($remember_token != null) {
$username = Cookie::safeGet('uoj_username', '/');
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
if (!validateUsername($username)) {
return;
}
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
$myUser = UOJUser::query($username);
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
if ($myUser['remember_token'] !== $remember_token) {
$myUser = null;
return;
2016-07-18 16:39:37 +00:00
}
2023-02-13 07:45:06 +00:00
$_SESSION['username'] = $myUser['username'];
$_SESSION['remember_token'] = $myUser['remember_token'];
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
return;
}
}
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
public static function init() {
global $myUser;
2023-01-30 03:53:02 +00:00
2016-07-18 16:39:37 +00:00
Auth::initMyUser();
2022-11-06 02:26:21 +00:00
if ($myUser && UOJUser::getAccountStatus($myUser) != 'ok') {
$myUser = null;
2016-07-18 16:39:37 +00:00
}
2023-02-13 07:45:06 +00:00
2016-07-18 16:39:37 +00:00
if ($myUser) {
2022-10-04 08:35:07 +00:00
if (!isset($_SESSION['last_login'])) {
2022-11-06 02:26:21 +00:00
$_SESSION['last_login'] = strtotime($myUser['last_login_time']);
2022-10-04 06:58:46 +00:00
}
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
$myUser = UOJUser::updateVisitHistory($myUser, [
'remote_addr' => UOJContext::remoteAddr(),
2023-01-30 03:53:02 +00:00
'http_x_forwarded_for' => UOJContext::httpXForwardedFor(),
'http_user_agent' => UOJContext::httpUserAgent()
]);
2023-02-13 07:45:06 +00:00
2022-11-06 02:26:21 +00:00
$_SESSION['last_visited'] = time();
2016-07-18 16:39:37 +00:00
}
}
}