| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- function hasProblemPermissionGroup($group, $problem) {
- if ($group == null)
- return false;
- return in_array($problem['id'], explode(',', $group['privs']));
- }
- function hasProblemViewPermission($user, $problem) {
- if ($user == null)
- return false;
- if (isSuperUser($user))
- return true;
- if (DB::selectFirst("select * from problems_permissions where username = '{$user['username']}' and problem_id = {$problem['id']}") != null)
- return true;
- $groups = explode(',', $user['extgroups']);
- foreach ($groups as $key => $grp)
- if (hasProblemPermissionGroup(queryUser($grp), $problem))
- return true;
- return false;
- }
- function hasProblemCtrlPermission($user, $problem) {
- if (!hasProblemViewPermission($user, $problem))
- return false;
- if (isSuperUser($user))
- return true;
- $grp = $problem['ctrl'];
- if ($grp == '') return false;
- $grp = queryUser($grp);
- if (!$grp) return false;
- if (in_array($user['username'], explode(',', $grp['ctrls'])))
- return true;
- return false;
- }
- function giveGroupPermission($user, $group) {
- if ($user == null || $group == null) return;
- $q = explode(',', $group['ctrls']);
- if (in_array($user['username'], $q)) return;
- $q[] = $user['username'];
- $s = join(',', $q);
- $groupname = $group['username'];
- DB::update("update user_info set ctrls = '$s' where username = '$groupname'");
- }
- function removeGroupPermission($user, $group) {
- if ($user == null || $group == null) return;
- $q = explode(',', $group['ctrls']);
- $p = array();
- foreach ($q as $i => $k) if ($k != $user['username']) $p[] = $k;
- $s = join(',', $p);
- $groupname = $group['username'];
- DB::update("update user_info set ctrls = '$s' where username = '$groupname'");
- }
- function attachProblemToGroup($problem, $group) {
- if ($problem == null || $group == null)
- return;
- if (in_array($problem['id'], explode(',', $group['privs'])))
- return;
- if ($group['privs'] == '') $privs = $problem['id'];
- else $privs = $group['privs'] . ',' . $problem['id'];
- $name = $group['username'];
- DB::update("update user_info set privs = '$privs' where username = '$name'");
- }
- function attachUserToGroup($user, $group) {
- if ($user == null || $group == null)
- return;
- if (in_array($group['username'], explode(',', $user['extgroups'])))
- return;
- if ($user['extgroups'] == '') $extgrps = $group['username'];
- else $extgrps = $user['extgroups'] . ',' . $group['username'];
- $name = $user['username'];
- DB::update("update user_info set extgroups = '$extgrps' where username = '$name'");
- }
- function deattachUserFromGroup($user, $group) {
- if ($user == null || $group == null) return;
- removeGroupPermission($user, $group);
- $q = explode(',', $user['extgroups']);
- if (!in_array($group['username'], $q)) return;
- $p = array();
- foreach ($q as $i => $k) if ($k != $group['username']) $p[] = $k;
- $s = join(',', $p);
- $name = $user['username'];
- DB::update("update user_info set extgroups = '$s' where username = '$name'");
- }
- function queryUserExtgroups($user) {
- if ($user == null) return;
- return explode(',', $user['extgroups']);
- }
- function queryGroupControllers($grp) {
- if ($grp == null) return;
- return explode(',', $grp['ctrls']);
- }
- function queryGroupMembers($grp) {
- $grpname = $grp['username'];
- return DB::selectAll("select username from user_info where find_in_set('$grpname', extgroups)");
- }
- function queryGroupProblems($grp) {
- $p = $grp['privs'];
- if ($p == '') return array ();
- return explode(',', $grp['privs']);
- }
- function queryProblemSize($pid) {
- return filesize("/var/uoj_data/$pid.zip");
- }
- function queryGroupTotalSize($grp) {
- $size = 0;
- foreach (queryGroupProblems($grp) as $i => $pid)
- if (queryProblemBrief($pid)['ctrl'] == $grp['username'])
- $size += queryProblemSize($pid);
- return $size;
- }
- function queryGroupCtrlProblems($grp) {
- $q = array();
- foreach (queryGroupProblems($grp) as $i => $pid)
- if (queryProblemBrief($pid)['ctrl'] == $grp['username']) $q[] = $pid;
- return $q;
- }
- function queryGroupQuota($grp) {
- return $grp['ac_num'] * 1048576;
- }
- function queryGroupProblemQuota($grp) {
- return 20;
- }
- function queryUserCtrlgroups($user) {
- if ($user == null) return;
- if (isSuperUser($user)) {
- $q = array();
- foreach (DB::selectAll("select username from user_info where username like 'group_%'") as $i => $k)
- $q[] = $k['username'];
- return $q;
- }
- $res = array();
- foreach (queryUserExtgroups($user) as $i => $grp) {
- if (in_array($user['username'], queryGroupControllers(queryUser($grp))))
- $res[] = $grp;
- }
- return $res;
- }
- function hasGroupCtrlPermission($user, $grp) {
- if ($user == null) return false;
- if (isSuperUser($user)) return true;
- if (in_array($user['username'], explode(',', $grp['ctrls'])))
- return true;
- else return false;
- }
- function hasViewPermission($str,$user,$problem,$submission) {
- if ($str=='ALL') {
- return true;
- }
- if ($str=='ALL_AFTER_AC') {
- return hasAC($user,$problem);
- }
- if ($str=='SELF') {
- return $submission['submitter']==$user['username'];
- }
- return false;
- }
- 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;
- }
- function hasAC($user, $problem) {
- return DB::selectFirst("select * from best_ac_submissions where submitter = '${user['username']}' and problem_id = ${problem['id']}") != null;
- }
- function queryUser($username) {
- if (!validateUsername($username)) {
- return null;
- }
- return DB::selectFirst("select * from user_info where username='$username'", MYSQLI_ASSOC);
- }
- function queryProblemContent($id) {
- return DB::selectFirst("select * from problems_contents where id = $id", MYSQLI_ASSOC);
- }
- function queryProblemBrief($id) {
- return DB::selectFirst("select * from problems where id = $id", MYSQLI_ASSOC);
- }
- function queryRemoteProblemBrief($pid) {
- return DB::selectFirst("select pid, title from rp where pid = '$pid'", MYSQLI_ASSOC);
- }
- function queryRemoteProblemFull($pid) {
- return DB::selectFirst("select pid, title, statement from rp where pid = '$pid'", MYSQLI_ASSOC);
- }
- function queryProblemTags($id) {
- $tags = array();
- $result = DB::query("select tag from problems_tags where problem_id = $id order by id");
- while ($row = DB::fetch($result, MYSQLI_NUM)) {
- $tags[] = $row[0];
- }
- return $tags;
- }
- function queryContestProblemRank($contest, $problem) {
- if (!DB::selectFirst("select * from contests_problems where contest_id = {$contest['id']} and problem_id = {$problem['id']}")) {
- return null;
- }
- return DB::selectCount("select count(*) from contests_problems where contest_id = {$contest['id']} and problem_id <= {$problem['id']}");
- }
- function queryDatumStream($id) {
- return DB::selectFirst("select * from datum_streams where id = $id", MYSQLI_ASSOC);
- }
- function querySubmission($id) {
- return DB::selectFirst("select * from submissions where id = $id", MYSQLI_ASSOC);
- }
- function queryHack($id) {
- return DB::selectFirst("select * from hacks where id = $id", MYSQLI_ASSOC);
- }
- function queryContest($id) {
- return DB::selectFirst("select * from contests where id = $id", MYSQLI_ASSOC);
- }
- function queryContestProblem($id) {
- return DB::selectFirst("select * from contest_problems where contest_id = $id", MYSQLI_ASSOC);
- }
- function queryDatumBrief($id) {
- return DB::selectFirst("select id, pid, status from datum_requests where id = $id", MYSQLI_ASSOC);
- }
- function queryDatum($id) {
- return DB::selectFirst("select * from datum_requests where id = $id", MYSQLI_ASSOC);
- }
- function queryZanVal($id, $type, $user) {
- if ($user == null) {
- return 0;
- }
- $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'");
- if ($row == null) {
- return 0;
- }
- return $row['val'];
- }
- function queryBlog($id) {
- return DB::selectFirst("select * from blogs where id='$id'", MYSQLI_ASSOC);
- }
- function queryBlogTags($id) {
- $tags = array();
- $result = DB::select("select tag from blogs_tags where blog_id = $id order by id");
- while ($row = DB::fetch($result, MYSQLI_NUM)) {
- $tags[] = $row[0];
- }
- return $tags;
- }
- function queryBlogComment($id) {
- return DB::selectFirst("select * from blogs_comments where id='$id'", MYSQLI_ASSOC);
- }
- function isProblemVisibleToUser($problem, $user) {
- return !$problem['is_hidden'] || hasProblemViewPermission($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) {
- if (isSuperUser($user)) {
- return true;
- } elseif (!$submission['is_hidden']) {
- return true;
- } else {
- return hasProblemViewPermission($user, $problem);
- }
- }
- function isHackVisibleToUser($hack, $problem, $user) {
- if (isSuperUser($user)) {
- return true;
- } elseif (!$hack['is_hidden']) {
- return true;
- } else {
- return hasProblemViewPermission($user, $problem);
- }
- }
- function isSubmissionFullVisibleToUser($submission, $contest, $problem, $user) {
- if (isSuperUser($user)) {
- return true;
- } elseif (!$contest) {
- return true;
- } elseif ($contest['cur_progress'] > CONTEST_IN_PROGRESS) {
- return true;
- } elseif ($submission['submitter'] == $user['username']) {
- return true;
- } else {
- return hasProblemCtrlPermission($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 hasProblemCtrlPermission($user, $problem);
- }
- }
- 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");
- }
|