S2OJ/web/app/libs/uoj-security-lib.php

57 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2016-07-18 16:39:37 +00:00
<?php
function getPasswordToStore($password, $username) {
return md5($username . $password);
}
function checkPassword($user, $password) {
2020-06-25 12:41:16 +00:00
return $user['password'] == md5($user['username'] . $password);
2016-07-18 16:39:37 +00:00
}
function getPasswordClientSalt() {
return UOJConfig::$data['security']['user']['client_salt'];
}
function crsf_token() {
if (!isset($_SESSION['_token'])) {
$_SESSION['_token'] = uojRandString(60);
}
return $_SESSION['_token'];
}
function crsf_check() {
if (isset($_POST['_token'])) {
$_token = $_POST['_token'];
2020-06-25 12:41:16 +00:00
} elseif (isset($_GET['_token'])) {
2016-07-18 16:39:37 +00:00
$_token = $_GET['_token'];
} else {
return false;
}
return $_token === $_SESSION['_token'];
}
function crsf_defend() {
if (!crsf_check()) {
UOJResponse::page403(<<<EOD
<div>页面已过期(可能页面真的过期了,也可能是刚才你访问的网页没有完全加载,也可能是你的浏览器版本太老)</div>
<div><a href="">返回</a></div>
EOD);
2016-07-18 16:39:37 +00:00
}
}
2022-11-06 02:26:21 +00:00
function submission_frequency_check() {
$submission_frequency = UOJContext::getMeta('submission_frequency');
2022-11-06 02:26:21 +00:00
$recent = clone UOJTime::$time_now;
$recent->sub(new DateInterval($submission_frequency['interval']));
2022-11-06 02:26:21 +00:00
$num = DB::selectCount([
"select count(*) from submissions",
"where", [
"submitter" => Auth::id(),
["submit_time", ">=", $recent->format('Y-m-d H:i:s')]
]
]);
if ($num >= max(1, $submission_frequency['limit'])) {
2022-11-06 02:26:21 +00:00
return false;
}
2022-11-06 02:26:21 +00:00
return true;
}