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

86 lines
2.8 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
function hasProblemPermission($user, $problem) {
if ($user == null) {
return false;
}
2022-03-17 13:52:10 +00:00
if (isSuperUser($user) || isProblemManager($user)) {
return true;
}
if ($problem['uploader'] == $user['username']) {
2016-07-18 16:39:37 +00:00
return true;
}
return DB::selectFirst("select * from problems_permissions where username = '{$user['username']}' and problem_id = {$problem['id']}") != null;
}
2022-09-28 11:48:49 +00:00
2016-07-18 16:39:37 +00:00
function hasContestPermission($user, $contest) {
if ($user == null) {
return false;
}
if (isSuperUser($user)) {
return true;
}
return DB::selectFirst("select * from contests_permissions where username = '{$user['username']}' and contest_id = {$contest['id']}") != null;
}
function hasRegistered($user, $contest) {
return DB::selectFirst("select * from contests_registrants where username = '${user['username']}' and contest_id = ${contest['id']}") != null;
2020-06-25 12:41:16 +00:00
}
2016-07-18 16:39:37 +00:00
function queryUser($username) {
if (!validateUsername($username)) {
return null;
}
return DB::selectFirst("select * from user_info where username='$username'", MYSQLI_ASSOC);
2016-07-18 16:39:37 +00:00
}
function queryProblemBrief($id) {
return DB::selectFirst("select * from problems where id = $id", MYSQLI_ASSOC);
2016-07-18 16:39:37 +00:00
}
2022-09-28 11:48:49 +00:00
function querySolution($problem_id, $blog_id) {
return DB::selectFirst("select * from problems_solutions where blog_id='$blog_id' and problem_id='$problem_id'", MYSQLI_ASSOC);
}
2016-07-18 16:39:37 +00:00
function queryContestProblemRank($contest, $problem) {
if (!DB::selectFirst("select * from contests_problems where contest_id = {$contest['id']} and problem_id = {$problem['id']}")) {
return null;
}
2022-11-06 02:26:21 +00:00
$contest_problems = DB::selectAll("select problem_id from contests_problems where contest_id = {$contest['id']} order by level, problem_id");
2022-09-29 02:16:38 +00:00
return array_search(array('problem_id' => $problem['id']), $contest_problems) + 1;
2016-07-18 16:39:37 +00:00
}
function queryContest($id) {
return DB::selectFirst("select * from contests where id = $id", MYSQLI_ASSOC);
2016-07-18 16:39:37 +00:00
}
function queryBlog($id) {
return DB::selectFirst("select * from blogs where id='$id'", MYSQLI_ASSOC);
2016-07-18 16:39:37 +00:00
}
function queryBlogComment($id) {
return DB::selectFirst("select * from blogs_comments where id='$id'", MYSQLI_ASSOC);
2016-07-18 16:39:37 +00:00
}
function isProblemVisibleToUser($problem, $user) {
return !$problem['is_hidden'] || hasProblemPermission($user, $problem);
}
2022-10-22 11:24:48 +00:00
function isListVisibleToUser($list, $user) {
return !$list['is_hidden'] || isSuperUser($user);
}
2016-07-18 16:39:37 +00:00
function isRegisteredRunningContestProblem($user, $problem) {
$result = DB::query("select contest_id from contests_problems where problem_id = {$problem['id']}");
while (list($contest_id) = DB::fetch($result, MYSQLI_NUM)) {
$contest = queryContest($contest_id);
genMoreContestInfo($contest);
2022-11-06 02:26:21 +00:00
if (
$contest['cur_progress'] == CONTEST_IN_PROGRESS
&& hasRegistered($user, $contest)
&& !hasContestPermission($user, $contest)
2022-11-06 02:26:21 +00:00
&& queryContestProblemRank($contest, $problem)
) {
return true;
}
}
return false;
}