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-03-17 13:52:10 +00:00
function hasViewPermission ( $str , $user , $problem , $submission ) {
if ( $str == 'ALL' ) {
2016-07-18 16:39:37 +00:00
return true ;
2020-06-25 12:41:16 +00:00
}
2022-03-17 13:52:10 +00:00
if ( $str == 'ALL_AFTER_AC' ) {
2016-07-18 16:39:37 +00:00
return hasAC ( $user , $problem );
2020-06-25 12:41:16 +00:00
}
2022-03-17 13:52:10 +00:00
if ( $str == 'SELF' ) {
return $submission [ 'submitter' ] == $user [ 'username' ];
2020-06-25 12:41:16 +00:00
}
2016-07-18 16:39:37 +00:00
return false ;
}
2022-09-28 11:48:49 +00:00
function hasViewSolutionPermission ( $str , $user , $problem ) {
2022-10-16 02:18:49 +00:00
if ( isSuperUser ( $user ) || isProblemManager ( $user )) {
2022-09-28 11:48:49 +00:00
return true ;
}
if ( $str == 'ALL' ) {
return true ;
}
if ( $str == 'ALL_AFTER_AC' ) {
return hasAC ( $user , $problem );
}
return false ;
}
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 ) {
2017-11-25 15:29:18 +00:00
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 hasAC ( $user , $problem ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from best_ac_submissions where submitter = ' ${ user['username'] } ' and problem_id = ${ problem['id'] } " ) != null ;
2016-07-18 16:39:37 +00:00
}
2022-09-19 23:49:45 +00:00
function hasParticipated ( $user , $contest ) {
$result = DB :: selectFirst ( " select * from contests_registrants where username = ' ${ user['username'] } ' and contest_id = ${ contest['id'] } " );
return $result != null && $result [ 'has_participated' ];
}
2016-07-18 16:39:37 +00:00
function queryUser ( $username ) {
if ( ! validateUsername ( $username )) {
return null ;
}
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from user_info where username=' $username ' " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryProblemContent ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from problems_contents where id = $id " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryProblemBrief ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from problems where id = $id " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryProblemTags ( $id ) {
$tags = array ();
2017-11-25 15:29:18 +00:00
$result = DB :: query ( " select tag from problems_tags where problem_id = $id order by id " );
while ( $row = DB :: fetch ( $result , MYSQLI_NUM )) {
2016-07-18 16:39:37 +00:00
$tags [] = $row [ 0 ];
}
return $tags ;
}
2022-09-18 11:34:17 +00:00
function queryProblemList ( $id ) {
return DB :: selectFirst ( " select * from lists where id = $id " , MYSQLI_ASSOC );
}
function queryProblemListTags ( $id ) {
$tags = array ();
$result = DB :: query ( " select tag from lists_tags where list_id = $id order by id " );
if ( ! $result ) {
return $tags ;
}
while ( $row = DB :: fetch ( $result , MYSQLI_NUM )) {
$tags [] = $row [ 0 ];
}
return $tags ;
}
function queryProblemInList ( $list_id , $problem_id ) {
return DB :: selectFirst ( " select * from lists_problems where list_id=' $blog_id ' and problem_id=' $problem_id ' " , MYSQLI_ASSOC );
}
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-09-29 02:16:38 +00:00
$contest_problems = DB :: selectAll ( " select problem_id from contests_problems where contest_id = { $contest [ 'id' ] } order by dfn, problem_id " );
return array_search ( array ( 'problem_id' => $problem [ 'id' ]), $contest_problems ) + 1 ;
2016-07-18 16:39:37 +00:00
}
function querySubmission ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from submissions where id = $id " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryHack ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from hacks where id = $id " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryContest ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from contests where id = $id " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryContestProblem ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from contest_problems where contest_id = $id " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
2022-09-20 01:50:37 +00:00
function queryContestProblems ( $id ) {
return DB :: selectAll ( " select * from contests_problems where contest_id = $id order by dfn, problem_id " , MYSQLI_ASSOC );
}
2016-07-18 16:39:37 +00:00
2022-03-20 00:07:46 +00:00
function queryGroup ( $id ) {
return DB :: selectFirst ( " select * from groups where id = $id " , MYSQLI_ASSOC );
}
2022-10-21 12:42:48 +00:00
function queryGroupUsers ( $id ) {
return DB :: selectAll ( " SELECT * FROM groups_users WHERE group_id = $id " );
}
2022-03-20 00:07:46 +00:00
function queryUserInGroup ( $group_id , $username ) {
return DB :: selectFirst ( " select * from groups_users where username=' $username ' and group_id=' $group_id ' " , MYSQLI_ASSOC );
}
2022-09-23 13:36:14 +00:00
function queryGroupsOfUser ( $username ) {
2022-03-20 00:07:46 +00:00
return DB :: selectAll ( " select b.title as title, b.id as id from groups_users a inner join groups b on a.group_id = b.id where a.username = ' $username ' and b.is_hidden = 0 order by id " , MYSQLI_ASSOC );
}
function queryGroupmateCurrentAC ( $username ) {
return DB :: selectAll ( " select a.problem_id as problem_id, a.submitter as submitter, a.submission_id as submission_id, b.submit_time as submit_time, c.group_id as group_id, c.group_name as group_name, d.title as problem_title, b.submit_time as submit_time, e.realname as realname from best_ac_submissions a inner join submissions b on (a.submission_id = b.id) inner join (select a.username as username, any_value(a.group_id) as group_id, any_value(c.title) as group_name from groups_users a inner join (select a.group_id as group_id from groups_users a inner join groups b on a.group_id = b.id where a.username = ' $username ' and b.is_hidden = 0) b on a.group_id = b.group_id inner join groups c on a.group_id = c.id group by a.username) c on a.submitter = c.username inner join problems d on (a.problem_id = d.id and d.is_hidden = 0) inner join user_info e on a.submitter = e.username where b.submit_time > addtime(now(), '-360:00:00') order by b.submit_time desc limit 10 " , MYSQLI_ASSOC );
}
function queryGroupCurrentAC ( $group_id ) {
return DB :: selectAll ( " select a.problem_id as problem_id, a.submitter as submitter, a.submission_id as submission_id, b.submit_time as submit_time, d.title as problem_title, b.submit_time as submit_time, e.realname as realname from best_ac_submissions a inner join submissions b on (a.submission_id = b.id) inner join groups_users c on (a.submitter = c.username and c.group_id = $group_id ) inner join problems d on (a.problem_id = d.id and d.is_hidden = 0) inner join user_info e on (a.submitter = e.username) where b.submit_time > addtime(now(), '-360:00:00') order by b.submit_time desc limit 10 " , MYSQLI_ASSOC );
}
2022-09-26 04:09:54 +00:00
function queryGroupAssignments ( $group_id ) {
2022-10-21 12:42:48 +00:00
return DB :: selectAll ( " select a.id as id, a.list_id as list_id, a.end_time as end_time, b.title from groups_assignments a left join lists b on a.list_id = b.id where a.group_id = $group_id order by a.end_time asc " , MYSQLI_ASSOC );
2022-09-26 04:09:54 +00:00
}
2022-09-20 00:28:38 +00:00
function queryGroupActiveAssignments ( $group_id ) {
2022-10-21 14:04:17 +00:00
return DB :: selectAll ( " select a.id as id, a.group_id as group_id, a.list_id as list_id, a.end_time as end_time, b.title from groups_assignments a left join lists b on a.list_id = b.id where a.group_id = $group_id and a.end_time >= addtime(now(), '-168:00:00') order by a.end_time asc " , MYSQLI_ASSOC );
2022-09-20 00:28:38 +00:00
}
function queryAssignmentByGroupListID ( $group_id , $list_id ) {
2022-10-21 12:42:48 +00:00
return DB :: selectFirst ( " select * from groups_assignments where list_id=' $list_id ' and group_id=' $group_id ' " , MYSQLI_ASSOC );
2022-09-20 00:28:38 +00:00
}
2022-03-20 00:07:46 +00:00
2016-07-18 16:39:37 +00:00
function queryZanVal ( $id , $type , $user ) {
if ( $user == null ) {
return 0 ;
}
2017-11-25 15:29:18 +00:00
$esc_type = DB :: escape ( $type );
$row = DB :: selectFirst ( " select val from click_zans where username=' { $user [ 'username' ] } ' and type=' $esc_type ' and target_id=' $id ' " );
2016-07-18 16:39:37 +00:00
if ( $row == null ) {
return 0 ;
}
return $row [ 'val' ];
}
function queryBlog ( $id ) {
2017-11-25 15:29:18 +00:00
return DB :: selectFirst ( " select * from blogs where id=' $id ' " , MYSQLI_ASSOC );
2016-07-18 16:39:37 +00:00
}
function queryBlogTags ( $id ) {
$tags = array ();
$result = DB :: select ( " select tag from blogs_tags where blog_id = $id order by id " );
2017-11-25 15:29:18 +00:00
while ( $row = DB :: fetch ( $result , MYSQLI_NUM )) {
2016-07-18 16:39:37 +00:00
$tags [] = $row [ 0 ];
}
return $tags ;
}
function queryBlogComment ( $id ) {
2017-11-25 15:29:18 +00:00
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 );
}
function isContestProblemVisibleToUser ( $problem , $contest , $user ) {
if ( isProblemVisibleToUser ( $problem , $user )) {
return true ;
}
if ( $contest [ 'cur_progress' ] >= CONTEST_PENDING_FINAL_TEST ) {
return true ;
}
if ( $contest [ 'cur_progress' ] == CONTEST_NOT_STARTED ) {
return false ;
}
return hasRegistered ( $user , $contest );
}
function isSubmissionVisibleToUser ( $submission , $problem , $user ) {
2022-10-10 09:03:57 +00:00
if ( isProblemManager ( $user )) {
2016-07-18 16:39:37 +00:00
return true ;
2020-06-25 12:41:16 +00:00
} elseif ( ! $submission [ 'is_hidden' ]) {
2016-07-18 16:39:37 +00:00
return true ;
} else {
return hasProblemPermission ( $user , $problem );
}
}
function isHackVisibleToUser ( $hack , $problem , $user ) {
if ( isSuperUser ( $user )) {
return true ;
} elseif ( ! $hack [ 'is_hidden' ]) {
return true ;
} else {
return hasProblemPermission ( $user , $problem );
}
}
function isSubmissionFullVisibleToUser ( $submission , $contest , $problem , $user ) {
if ( isSuperUser ( $user )) {
return true ;
2022-10-03 01:11:43 +00:00
} elseif ( $submission [ 'submitter' ] == $user [ 'username' ]) {
return true ;
} elseif ( isRegisteredRunningContestProblem ( $user , $problem )) {
return false ;
2016-07-18 16:39:37 +00:00
} elseif ( ! $contest ) {
return true ;
} elseif ( $contest [ 'cur_progress' ] > CONTEST_IN_PROGRESS ) {
return true ;
} else {
return hasProblemPermission ( $user , $problem );
}
}
function isHackFullVisibleToUser ( $hack , $contest , $problem , $user ) {
if ( isSuperUser ( $user )) {
return true ;
} elseif ( ! $contest ) {
return true ;
} elseif ( $contest [ 'cur_progress' ] > CONTEST_IN_PROGRESS ) {
return true ;
} elseif ( $hack [ 'hacker' ] == $user [ 'username' ]) {
return true ;
} else {
return hasProblemPermission ( $user , $problem );
}
}
2022-10-03 00:49:23 +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-10-03 01:11:43 +00:00
if ( $contest [ 'cur_progress' ] == CONTEST_IN_PROGRESS
2022-10-03 00:49:23 +00:00
&& hasRegistered ( $user , $contest )
&& ! hasContestPermission ( $user , $contest )
&& queryContestProblemRank ( $contest , $problem )) {
return true ;
}
}
return false ;
}
2016-07-18 16:39:37 +00:00
function deleteBlog ( $id ) {
if ( ! validateUInt ( $id )) {
return ;
}
DB :: delete ( " delete from click_zans where type = 'B' and target_id = $id " );
DB :: delete ( " delete from click_zans where type = 'BC' and target_id in (select id from blogs_comments where blog_id = $id ) " );
DB :: delete ( " delete from blogs where id = $id " );
DB :: delete ( " delete from blogs_comments where blog_id = $id " );
DB :: delete ( " delete from important_blogs where blog_id = $id " );
DB :: delete ( " delete from blogs_tags where blog_id = $id " );
2022-10-18 02:27:11 +00:00
DB :: delete ( " DELETE FROM problems_solutions WHERE blog_id = $id " );
2022-03-17 13:52:10 +00:00
}