uoj-query-lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. function hasProblemPermissionGroup($group, $problem) {
  3. if ($group == null)
  4. return false;
  5. return in_array($problem['id'], explode(',', $group['privs']));
  6. }
  7. function hasProblemViewPermission($user, $problem) {
  8. if ($user == null)
  9. return false;
  10. if (isSuperUser($user))
  11. return true;
  12. if (DB::selectFirst("select * from problems_permissions where username = '{$user['username']}' and problem_id = {$problem['id']}") != null)
  13. return true;
  14. $groups = explode(',', $user['extgroups']);
  15. foreach ($groups as $key => $grp)
  16. if (hasProblemPermissionGroup(queryUser($grp), $problem))
  17. return true;
  18. return false;
  19. }
  20. function hasProblemCtrlPermission($user, $problem) {
  21. if (!hasProblemViewPermission($user, $problem))
  22. return false;
  23. if (isSuperUser($user))
  24. return true;
  25. $grp = $problem['ctrl'];
  26. if ($grp == '') return false;
  27. $grp = queryUser($grp);
  28. if (!$grp) return false;
  29. if (in_array($user['username'], explode(',', $grp['ctrls'])))
  30. return true;
  31. return false;
  32. }
  33. function giveGroupPermission($user, $group) {
  34. if ($user == null || $group == null) return;
  35. $q = explode(',', $group['ctrls']);
  36. if (in_array($user['username'], $q)) return;
  37. $q[] = $user['username'];
  38. $s = join(',', $q);
  39. $groupname = $group['username'];
  40. DB::update("update user_info set ctrls = '$s' where username = '$groupname'");
  41. }
  42. function removeGroupPermission($user, $group) {
  43. if ($user == null || $group == null) return;
  44. $q = explode(',', $group['ctrls']);
  45. $p = array();
  46. foreach ($q as $i => $k) if ($k != $user['username']) $p[] = $k;
  47. $s = join(',', $p);
  48. $groupname = $group['username'];
  49. DB::update("update user_info set ctrls = '$s' where username = '$groupname'");
  50. }
  51. function attachProblemToGroup($problem, $group) {
  52. if ($problem == null || $group == null)
  53. return;
  54. if (in_array($problem['id'], explode(',', $group['privs'])))
  55. return;
  56. if ($group['privs'] == '') $privs = $problem['id'];
  57. else $privs = $group['privs'] . ',' . $problem['id'];
  58. $name = $group['username'];
  59. DB::update("update user_info set privs = '$privs' where username = '$name'");
  60. }
  61. function attachUserToGroup($user, $group) {
  62. if ($user == null || $group == null)
  63. return;
  64. if (in_array($group['username'], explode(',', $user['extgroups'])))
  65. return;
  66. if ($user['extgroups'] == '') $extgrps = $group['username'];
  67. else $extgrps = $user['extgroups'] . ',' . $group['username'];
  68. $name = $user['username'];
  69. DB::update("update user_info set extgroups = '$extgrps' where username = '$name'");
  70. }
  71. function deattachUserFromGroup($user, $group) {
  72. if ($user == null || $group == null) return;
  73. removeGroupPermission($user, $group);
  74. $q = explode(',', $user['extgroups']);
  75. if (!in_array($group['username'], $q)) return;
  76. $p = array();
  77. foreach ($q as $i => $k) if ($k != $group['username']) $p[] = $k;
  78. $s = join(',', $p);
  79. $name = $user['username'];
  80. DB::update("update user_info set extgroups = '$s' where username = '$name'");
  81. }
  82. function queryUserExtgroups($user) {
  83. if ($user == null) return;
  84. return explode(',', $user['extgroups']);
  85. }
  86. function queryGroupControllers($grp) {
  87. if ($grp == null) return;
  88. return explode(',', $grp['ctrls']);
  89. }
  90. function queryGroupMembers($grp) {
  91. $grpname = $grp['username'];
  92. return DB::selectAll("select username from user_info where find_in_set('$grpname', extgroups)");
  93. }
  94. function queryGroupProblems($grp) {
  95. $p = $grp['privs'];
  96. if ($p == '') return array ();
  97. return explode(',', $grp['privs']);
  98. }
  99. function queryProblemSize($pid) {
  100. return filesize("/var/uoj_data/$pid.zip");
  101. }
  102. function queryGroupTotalSize($grp) {
  103. $size = 0;
  104. foreach (queryGroupProblems($grp) as $i => $pid)
  105. if (queryProblemBrief($pid)['ctrl'] == $grp['username'])
  106. $size += queryProblemSize($pid);
  107. return $size;
  108. }
  109. function queryGroupCtrlProblems($grp) {
  110. $q = array();
  111. foreach (queryGroupProblems($grp) as $i => $pid)
  112. if (queryProblemBrief($pid)['ctrl'] == $grp['username']) $q[] = $pid;
  113. return $q;
  114. }
  115. function queryGroupQuota($grp) {
  116. return $grp['ac_num'] * 1048576;
  117. }
  118. function queryGroupProblemQuota($grp) {
  119. return 20;
  120. }
  121. function queryUserCtrlgroups($user) {
  122. if ($user == null) return;
  123. if (isSuperUser($user)) {
  124. $q = array();
  125. foreach (DB::selectAll("select username from user_info where username like 'group_%'") as $i => $k)
  126. $q[] = $k['username'];
  127. return $q;
  128. }
  129. $res = array();
  130. foreach (queryUserExtgroups($user) as $i => $grp) {
  131. if (in_array($user['username'], queryGroupControllers(queryUser($grp))))
  132. $res[] = $grp;
  133. }
  134. return $res;
  135. }
  136. function hasGroupCtrlPermission($user, $grp) {
  137. if ($user == null) return false;
  138. if (isSuperUser($user)) return true;
  139. if (in_array($user['username'], explode(',', $grp['ctrls'])))
  140. return true;
  141. else return false;
  142. }
  143. function hasViewPermission($str,$user,$problem,$submission) {
  144. if ($str=='ALL') {
  145. return true;
  146. }
  147. if ($str=='ALL_AFTER_AC') {
  148. return hasAC($user,$problem);
  149. }
  150. if ($str=='SELF') {
  151. return $submission['submitter']==$user['username'];
  152. }
  153. return false;
  154. }
  155. function hasContestPermission($user, $contest) {
  156. if ($user == null) {
  157. return false;
  158. }
  159. if (isSuperUser($user)) {
  160. return true;
  161. }
  162. return DB::selectFirst("select * from contests_permissions where username = '{$user['username']}' and contest_id = {$contest['id']}") != null;
  163. }
  164. function hasRegistered($user, $contest) {
  165. return DB::selectFirst("select * from contests_registrants where username = '${user['username']}' and contest_id = ${contest['id']}") != null;
  166. }
  167. function hasAC($user, $problem) {
  168. return DB::selectFirst("select * from best_ac_submissions where submitter = '${user['username']}' and problem_id = ${problem['id']}") != null;
  169. }
  170. function queryUser($username) {
  171. if (!validateUsername($username)) {
  172. return null;
  173. }
  174. return DB::selectFirst("select * from user_info where username='$username'", MYSQLI_ASSOC);
  175. }
  176. function queryProblemContent($id) {
  177. return DB::selectFirst("select * from problems_contents where id = $id", MYSQLI_ASSOC);
  178. }
  179. function queryProblemBrief($id) {
  180. return DB::selectFirst("select * from problems where id = $id", MYSQLI_ASSOC);
  181. }
  182. function queryRemoteProblemBrief($pid) {
  183. return DB::selectFirst("select pid, title from rp where pid = '$pid'", MYSQLI_ASSOC);
  184. }
  185. function queryRemoteProblemFull($pid) {
  186. return DB::selectFirst("select pid, title, statement from rp where pid = '$pid'", MYSQLI_ASSOC);
  187. }
  188. function queryProblemTags($id) {
  189. $tags = array();
  190. $result = DB::query("select tag from problems_tags where problem_id = $id order by id");
  191. while ($row = DB::fetch($result, MYSQLI_NUM)) {
  192. $tags[] = $row[0];
  193. }
  194. return $tags;
  195. }
  196. function queryContestProblemRank($contest, $problem) {
  197. if (!DB::selectFirst("select * from contests_problems where contest_id = {$contest['id']} and problem_id = {$problem['id']}")) {
  198. return null;
  199. }
  200. return DB::selectCount("select count(*) from contests_problems where contest_id = {$contest['id']} and problem_id <= {$problem['id']}");
  201. }
  202. function queryDatumStream($id) {
  203. return DB::selectFirst("select * from datum_streams where id = $id", MYSQLI_ASSOC);
  204. }
  205. function querySubmission($id) {
  206. return DB::selectFirst("select * from submissions where id = $id", MYSQLI_ASSOC);
  207. }
  208. function queryHack($id) {
  209. return DB::selectFirst("select * from hacks where id = $id", MYSQLI_ASSOC);
  210. }
  211. function queryContest($id) {
  212. return DB::selectFirst("select * from contests where id = $id", MYSQLI_ASSOC);
  213. }
  214. function queryContestProblem($id) {
  215. return DB::selectFirst("select * from contest_problems where contest_id = $id", MYSQLI_ASSOC);
  216. }
  217. function queryDatumBrief($id) {
  218. return DB::selectFirst("select id, pid, status from datum_requests where id = $id", MYSQLI_ASSOC);
  219. }
  220. function queryDatum($id) {
  221. return DB::selectFirst("select * from datum_requests where id = $id", MYSQLI_ASSOC);
  222. }
  223. function queryZanVal($id, $type, $user) {
  224. if ($user == null) {
  225. return 0;
  226. }
  227. $esc_type = DB::escape($type);
  228. $row = DB::selectFirst("select val from click_zans where username='{$user['username']}' and type='$esc_type' and target_id='$id'");
  229. if ($row == null) {
  230. return 0;
  231. }
  232. return $row['val'];
  233. }
  234. function queryBlog($id) {
  235. return DB::selectFirst("select * from blogs where id='$id'", MYSQLI_ASSOC);
  236. }
  237. function queryBlogTags($id) {
  238. $tags = array();
  239. $result = DB::select("select tag from blogs_tags where blog_id = $id order by id");
  240. while ($row = DB::fetch($result, MYSQLI_NUM)) {
  241. $tags[] = $row[0];
  242. }
  243. return $tags;
  244. }
  245. function queryBlogComment($id) {
  246. return DB::selectFirst("select * from blogs_comments where id='$id'", MYSQLI_ASSOC);
  247. }
  248. function isProblemVisibleToUser($problem, $user) {
  249. return !$problem['is_hidden'] || hasProblemViewPermission($user, $problem);
  250. }
  251. function isContestProblemVisibleToUser($problem, $contest, $user) {
  252. if (isProblemVisibleToUser($problem, $user)) {
  253. return true;
  254. }
  255. if ($contest['cur_progress'] >= CONTEST_PENDING_FINAL_TEST) {
  256. return true;
  257. }
  258. if ($contest['cur_progress'] == CONTEST_NOT_STARTED) {
  259. return false;
  260. }
  261. return hasRegistered($user, $contest);
  262. }
  263. function isSubmissionVisibleToUser($submission, $problem, $user) {
  264. if (isSuperUser($user)) {
  265. return true;
  266. } elseif (!$submission['is_hidden']) {
  267. return true;
  268. } else {
  269. return hasProblemViewPermission($user, $problem);
  270. }
  271. }
  272. function isHackVisibleToUser($hack, $problem, $user) {
  273. if (isSuperUser($user)) {
  274. return true;
  275. } elseif (!$hack['is_hidden']) {
  276. return true;
  277. } else {
  278. return hasProblemViewPermission($user, $problem);
  279. }
  280. }
  281. function isSubmissionFullVisibleToUser($submission, $contest, $problem, $user) {
  282. if (isSuperUser($user)) {
  283. return true;
  284. } elseif (!$contest) {
  285. return true;
  286. } elseif ($contest['cur_progress'] > CONTEST_IN_PROGRESS) {
  287. return true;
  288. } elseif ($submission['submitter'] == $user['username']) {
  289. return true;
  290. } else {
  291. return hasProblemCtrlPermission($user, $problem);
  292. }
  293. }
  294. function isHackFullVisibleToUser($hack, $contest, $problem, $user) {
  295. if (isSuperUser($user)) {
  296. return true;
  297. } elseif (!$contest) {
  298. return true;
  299. } elseif ($contest['cur_progress'] > CONTEST_IN_PROGRESS) {
  300. return true;
  301. } elseif ($hack['hacker'] == $user['username']) {
  302. return true;
  303. } else {
  304. return hasProblemCtrlPermission($user, $problem);
  305. }
  306. }
  307. function deleteBlog($id) {
  308. if (!validateUInt($id)) {
  309. return;
  310. }
  311. DB::delete("delete from click_zans where type = 'B' and target_id = $id");
  312. DB::delete("delete from click_zans where type = 'BC' and target_id in (select id from blogs_comments where blog_id = $id)");
  313. DB::delete("delete from blogs where id = $id");
  314. DB::delete("delete from blogs_comments where blog_id = $id");
  315. DB::delete("delete from important_blogs where blog_id = $id");
  316. DB::delete("delete from blogs_tags where blog_id = $id");
  317. }