super_manage.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. requirePHPLib('form');
  3. requirePHPLib('judger');
  4. if ($myUser == null || !isSuperUser($myUser)) {
  5. become403Page();
  6. }
  7. $user_form = new UOJForm('user');
  8. $user_form->addInput('username', 'text', '用户名', '',
  9. function ($username) {
  10. if (!validateUsername($username)) {
  11. return '用户名不合法';
  12. }
  13. if (!queryUser($username)) {
  14. return '用户不存在';
  15. }
  16. return '';
  17. },
  18. null
  19. );
  20. $options = array(
  21. 'banneduser' => '设为封禁用户',
  22. 'normaluser' => '设为普通用户',
  23. 'superuser' => '设为超级用户'
  24. );
  25. $user_form->addSelect('op-type', $options, '操作类型', '');
  26. $user_form->handle = function() {
  27. global $user_form;
  28. $username = $_POST['username'];
  29. switch ($_POST['op-type']) {
  30. case 'banneduser':
  31. DB::update("update user_info set usergroup = 'B' where username = '{$username}'");
  32. break;
  33. case 'normaluser':
  34. DB::update("update user_info set usergroup = 'U' where username = '{$username}'");
  35. break;
  36. case 'superuser':
  37. DB::update("update user_info set usergroup = 'S' where username = '{$username}'");
  38. break;
  39. }
  40. };
  41. $user_form->runAtServer();
  42. $assign_certs_form = new UOJForm('assign_certs_form');
  43. $color_options = array('red' => '红色', 'blue' => '蓝色', 'green' => '绿色', 'purple' => '紫色', 'pink' => '粉色', 'olive' => '屎黄色', 'deepblue' => '深蓝');
  44. $assign_certs_form->addSelect('color-type', $color_options, '勋章颜色', '');
  45. $assign_certs_form->addInput('certs_user', 'text', '用户名', '',
  46. function ($username) {
  47. if (!validateUsername($username))
  48. return '用户名不合法';
  49. if (!queryUser($username))
  50. return '用户不存在';
  51. return '';
  52. }, null
  53. );
  54. $assign_certs_form->addInput('certs_name', 'text', '称号', '',
  55. function ($text) {
  56. if (strpos($text, '#') != false || strpos($text, ',') != false)
  57. return '不能含有井号或逗号';
  58. if (mb_strlen($text) > 8) return '称号过长' . $text;
  59. return '';
  60. }, null
  61. );
  62. $assign_certs_form->handle = function() {
  63. $username = $_POST['certs_user'];
  64. $text = $_POST['certs_name'];
  65. $color = $_POST['color-type'];
  66. $user = queryUser($username);
  67. $newcerts = $user['certslist'];
  68. if ($user['certslist'] != '') $newcerts = $newcerts . ',' . $color . '#' . $text;
  69. else $newcerts = $color . '#' . $text;
  70. DB::update("update user_info set certslist = '{$newcerts}' where username = '{$username}'");
  71. if ($user['certs'] == '') DB::update("update user_info set certs = '{$newcerts}' where username = '{$username}'");
  72. };
  73. $assign_certs_form->runAtServer();
  74. $user_attach_form = new UOJForm('user_attach_form');
  75. $user_attach_form->addInput('username', 'text', '用户名', '',
  76. function ($username) {
  77. if (!validateUsername($username)) {
  78. return '用户名不合法';
  79. }
  80. if (!queryUser($username)) {
  81. return '用户不存在';
  82. }
  83. return '';
  84. },
  85. null
  86. );
  87. $user_attach_form->addInput('group', 'text', '组名', '',
  88. function ($username) {
  89. if (!validateUsername($username))
  90. return '组名不合法';
  91. if (!queryUser($username))
  92. return '组不存在';
  93. if (substr($username, 0, 6) != 'group_')
  94. return '不是合法组(前缀非 group_)';
  95. return '';
  96. },
  97. null
  98. );
  99. $user_attach_form->handle = function() {
  100. $username = $_POST['username'];
  101. $group = $_POST['group'];
  102. attachUserToGroup (queryUser($username), queryUser($group));
  103. };
  104. $user_attach_form->runAtServer();
  105. $problem_attach_form = new UOJForm('problem_attach_form');
  106. $problem_attach_form->addInput('pid', 'text', '题号', '',
  107. function ($pid) {
  108. if (!validateUInt($pid)) {
  109. return '题号不合法';
  110. }
  111. if (!queryProblemBrief($pid)) {
  112. return '题目不存在';
  113. }
  114. return '';
  115. },
  116. null
  117. );
  118. $problem_attach_form->addInput('problem_group', 'text', '组名', '',
  119. function ($username) {
  120. if (!validateUsername($username))
  121. return '组名不合法';
  122. if (!queryUser($username))
  123. return '组不存在';
  124. if (substr($username, 0, 6) != 'group_')
  125. return '不是合法组(前缀非 group_)';
  126. return '';
  127. },
  128. null
  129. );
  130. $problem_attach_form->handle = function() {
  131. $pid = $_POST['pid'];
  132. $group = $_POST['problem_group'];
  133. attachProblemToGroup (queryProblemBrief ($pid), queryUser($group));
  134. };
  135. $problem_attach_form->runAtServer();
  136. $blog_link_contests = new UOJForm('blog_link_contests');
  137. $blog_link_contests->addInput('blog_id', 'text', '博客ID', '',
  138. function ($x) {
  139. if (!validateUInt($x)) {
  140. return 'ID不合法';
  141. }
  142. if (!queryBlog($x)) {
  143. return '博客不存在';
  144. }
  145. return '';
  146. },
  147. null
  148. );
  149. $blog_link_contests->addInput('contest_id', 'text', '比赛ID', '',
  150. function ($x) {
  151. if (!validateUInt($x)) {
  152. return 'ID不合法';
  153. }
  154. if (!queryContest($x)) {
  155. return '比赛不存在';
  156. }
  157. return '';
  158. },
  159. null
  160. );
  161. $blog_link_contests->addInput('title', 'text', '标题', '',
  162. function ($x) {
  163. return '';
  164. },
  165. null
  166. );
  167. $options = array(
  168. 'add' => '添加',
  169. 'del' => '删除'
  170. );
  171. $blog_link_contests->addSelect('op-type', $options, '操作类型', '');
  172. $blog_link_contests->handle = function() {
  173. $blog_id = $_POST['blog_id'];
  174. $contest_id = $_POST['contest_id'];
  175. $str = DB::selectFirst(("select * from contests where id='${contest_id}'"));
  176. $all_config = json_decode($str['extra_config'], true);
  177. $config = $all_config['links'];
  178. $n = count($config);
  179. if ($_POST['op-type'] == 'add') {
  180. $row = array();
  181. $row[0] = $_POST['title'];
  182. $row[1] = $blog_id;
  183. $config[$n] = $row;
  184. }
  185. if ($_POST['op-type'] == 'del') {
  186. for ($i = 0; $i < $n; $i++) {
  187. if ($config[$i][1] == $blog_id) {
  188. $config[$i] = $config[$n - 1];
  189. unset($config[$n - 1]);
  190. break;
  191. }
  192. }
  193. }
  194. $all_config['links'] = $config;
  195. $str = json_encode($all_config);
  196. $str = DB::escape($str);
  197. DB::query("update contests set extra_config='${str}' where id='${contest_id}'");
  198. };
  199. $blog_link_contests->runAtServer();
  200. $blog_link_index = new UOJForm('blog_link_index');
  201. $blog_link_index->addInput('blog_id2', 'text', '博客ID', '',
  202. function ($x) {
  203. if (!validateUInt($x)) {
  204. return 'ID不合法';
  205. }
  206. if (!queryBlog($x)) {
  207. return '博客不存在';
  208. }
  209. return '';
  210. },
  211. null
  212. );
  213. $blog_link_index->addInput('blog_level', 'text', '置顶级别(删除不用填)', '0',
  214. function ($x) {
  215. if (!validateUInt($x)) {
  216. return '数字不合法';
  217. }
  218. if ($x > 3) {
  219. return '该级别不存在';
  220. }
  221. return '';
  222. },
  223. null
  224. );
  225. $options = array(
  226. 'add' => '添加',
  227. 'del' => '删除'
  228. );
  229. $blog_link_index->addSelect('op-type2', $options, '操作类型', '');
  230. $blog_link_index->handle = function() {
  231. $blog_id = $_POST['blog_id2'];
  232. $blog_level = $_POST['blog_level'];
  233. if ($_POST['op-type2'] == 'add') {
  234. if (DB::selectFirst("select * from important_blogs where blog_id = {$blog_id}")) {
  235. DB::update("update important_blogs set level = {$blog_level} where blog_id = {$blog_id}");
  236. } else {
  237. DB::insert("insert into important_blogs (blog_id, level) values ({$blog_id}, {$blog_level})");
  238. }
  239. }
  240. if ($_POST['op-type2'] == 'del') {
  241. DB::delete("delete from important_blogs where blog_id = {$blog_id}");
  242. }
  243. };
  244. $blog_link_index->runAtServer();
  245. $blog_deleter = new UOJForm('blog_deleter');
  246. $blog_deleter->addInput('blog_del_id', 'text', '博客ID', '',
  247. function ($x) {
  248. if (!validateUInt($x)) {
  249. return 'ID不合法';
  250. }
  251. if (!queryBlog($x)) {
  252. return '博客不存在';
  253. }
  254. return '';
  255. },
  256. null
  257. );
  258. $blog_deleter->handle = function() {
  259. deleteBlog($_POST['blog_del_id']);
  260. };
  261. $blog_deleter->runAtServer();
  262. $contest_submissions_deleter = new UOJForm('contest_submissions');
  263. $contest_submissions_deleter->addInput('contest_id', 'text', '比赛ID', '',
  264. function ($x) {
  265. if (!validateUInt($x)) {
  266. return 'ID不合法';
  267. }
  268. if (!queryContest($x)) {
  269. return '博客不存在';
  270. }
  271. return '';
  272. },
  273. null
  274. );
  275. $contest_submissions_deleter->handle = function() {
  276. $contest = queryContest($_POST['contest_id']);
  277. genMoreContestInfo($contest);
  278. $contest_problems = DB::selectAll("select problem_id from contests_problems where contest_id = {$contest['id']}");
  279. foreach ($contest_problems as $problem) {
  280. $submissions = DB::selectAll("select * from submissions where problem_id = {$problem['problem_id']} and submit_time < '{$contest['start_time_str']}'");
  281. foreach ($submissions as $submission) {
  282. $content = json_decode($submission['content'], true);
  283. unlink(UOJContext::storagePath().$content['file_name']);
  284. DB::delete("delete from submissions where id = {$submission['id']}");
  285. updateBestACSubmissions($submission['submitter'], $submission['problem_id']);
  286. }
  287. }
  288. };
  289. $contest_submissions_deleter->runAtServer();
  290. $custom_test_deleter = new UOJForm('custom_test_deleter');
  291. $custom_test_deleter->addInput('last', 'text', '删除末尾记录', '5',
  292. function ($x, &$vdata) {
  293. if (!validateUInt($x)) {
  294. return '不合法';
  295. }
  296. $vdata['last'] = $x;
  297. return '';
  298. },
  299. null
  300. );
  301. $custom_test_deleter->handle = function(&$vdata) {
  302. $all = DB::selectAll("select * from custom_test_submissions order by id asc limit {$vdata['last']}");
  303. foreach ($all as $submission) {
  304. $content = json_decode($submission['content'], true);
  305. unlink(UOJContext::storagePath().$content['file_name']);
  306. }
  307. DB::delete("delete from custom_test_submissions order by id asc limit {$vdata['last']}");
  308. };
  309. $custom_test_deleter->runAtServer();
  310. $judger_adder = new UOJForm('judger_adder');
  311. $judger_adder->addInput('judger_adder_name', 'text', '评测机名称', '',
  312. function ($x, &$vdata) {
  313. if (!validateUsername($x)) {
  314. return '不合法';
  315. }
  316. if (DB::selectCount("select count(*) from judger_info where judger_name='$x'")!=0) {
  317. return '不合法';
  318. }
  319. $vdata['name'] = $x;
  320. return '';
  321. },
  322. null
  323. );
  324. $judger_adder->handle = function(&$vdata) {
  325. $password=uojRandString(32);
  326. DB::insert("insert into judger_info (judger_name,password) values('{$vdata['name']}','{$password}')");
  327. };
  328. $judger_adder->runAtServer();
  329. $judger_deleter = new UOJForm('judger_deleter');
  330. $judger_deleter->addInput('judger_deleter_name', 'text', '评测机名称', '',
  331. function ($x, &$vdata) {
  332. if (!validateUsername($x)) {
  333. return '不合法';
  334. }
  335. if (DB::selectCount("select count(*) from judger_info where judger_name='$x'")!=1) {
  336. return '不合法';
  337. }
  338. $vdata['name'] = $x;
  339. return '';
  340. },
  341. null
  342. );
  343. $judger_deleter->handle = function(&$vdata) {
  344. DB::delete("delete from judger_info where judger_name='{$vdata['name']}'");
  345. };
  346. $judger_deleter->runAtServer();
  347. $paste_deleter = new UOJForm('paste_deleter');
  348. $paste_deleter->addInput('paste_deleter_name', 'text', 'Paste ID', '',
  349. function ($x, &$vdata) {
  350. if (DB::selectCount("select count(*) from pastes where `index`='$x'")==0) {
  351. return '不合法';
  352. }
  353. $vdata['name'] = $x;
  354. return '';
  355. },
  356. null
  357. );
  358. $paste_deleter->handle = function(&$vdata) {
  359. DB::delete("delete from pastes where `index` = '${vdata['name']}'");
  360. };
  361. $paste_deleter->runAtServer();
  362. $judgerlist_cols = array('judger_name', 'password');
  363. $judgerlist_config = array();
  364. $judgerlist_header_row = <<<EOD
  365. <tr>
  366. <th>评测机名称</th>
  367. <th>密码</th>
  368. </tr>
  369. EOD;
  370. $judgerlist_print_row = function($row) {
  371. echo <<<EOD
  372. <tr>
  373. <td>{$row['judger_name']}</td>
  374. <td>{$row['password']}</td>
  375. </tr>
  376. EOD;
  377. };
  378. $banlist_cols = array('username', 'usergroup');
  379. $banlist_config = array();
  380. $banlist_header_row = <<<EOD
  381. <tr>
  382. <th>用户名</th>
  383. </tr>
  384. EOD;
  385. $banlist_print_row = function($row) {
  386. $hislink = getUserLink($row['username']);
  387. echo <<<EOD
  388. <tr>
  389. <td>${hislink}</td>
  390. </tr>
  391. EOD;
  392. };
  393. $cur_tab = isset($_GET['tab']) ? $_GET['tab'] : 'users';
  394. $tabs_info = array(
  395. 'users' => array(
  396. 'name' => '用户操作',
  397. 'url' => "/super-manage/users"
  398. ),
  399. 'blogs' => array(
  400. 'name' => '博客管理',
  401. 'url' => "/super-manage/blogs"
  402. ),
  403. 'submissions' => array(
  404. 'name' => '提交记录',
  405. 'url' => "/super-manage/submissions"
  406. ),
  407. 'custom-test' => array(
  408. 'name' => '自定义测试',
  409. 'url' => '/super-manage/custom-test'
  410. ),
  411. 'click-zan' => array(
  412. 'name' => '点赞管理',
  413. 'url' => '/super-manage/click-zan'
  414. ),
  415. 'search' => array(
  416. 'name' => '搜索管理',
  417. 'url' => '/super-manage/search'
  418. ),
  419. 'judger' => array(
  420. 'name' => '评测机管理',
  421. 'url' => '/super-manage/judger'
  422. ),
  423. 'paste' => array(
  424. 'name' => 'Paste管理',
  425. 'url' => '/super-manage/paste'
  426. ),
  427. 'groups' => array(
  428. 'name' => '用户组管理',
  429. 'url' => '/super-manage/groups'
  430. ),
  431. 'certs' => array(
  432. 'name' => '勋章管理',
  433. 'url' => '/super-manage/certs'
  434. )
  435. );
  436. if (!isset($tabs_info[$cur_tab])) {
  437. become404Page();
  438. }
  439. ?>
  440. <?php
  441. requireLib('hljs');
  442. requireLib('morris');
  443. ?>
  444. <?php echoUOJPageHeader('系统管理') ?>
  445. <div class="row">
  446. <div class="col-sm-3">
  447. <?= HTML::tablist($tabs_info, $cur_tab, 'nav-pills flex-column') ?>
  448. </div>
  449. <div class="col-sm-9">
  450. <?php if ($cur_tab === 'users'): ?>
  451. <?php $user_form->printHTML(); ?>
  452. <h3>封禁名单</h3>
  453. <?php echoLongTable($banlist_cols, 'user_info', "usergroup='B'", '', $banlist_header_row, $banlist_print_row, $banlist_config) ?>
  454. <?php elseif ($cur_tab === 'blogs'): ?>
  455. <div>
  456. <h4>添加到比赛链接</h4>
  457. <?php $blog_link_contests->printHTML(); ?>
  458. </div>
  459. <div>
  460. <h4>添加到公告</h4>
  461. <?php $blog_link_index->printHTML(); ?>
  462. </div>
  463. <div>
  464. <h4>删除博客</h4>
  465. <?php $blog_deleter->printHTML(); ?>
  466. </div>
  467. <?php elseif ($cur_tab === 'submissions'): ?>
  468. <div>
  469. <h4>删除赛前提交记录</h4>
  470. <?php $contest_submissions_deleter->printHTML(); ?>
  471. </div>
  472. <div>
  473. <h4>测评失败的提交记录</h4>
  474. <?php echoSubmissionsList("result_error = 'Judgement Failed'", 'order by id desc', array('result_hidden' => ''), $myUser); ?>
  475. </div>
  476. <?php elseif ($cur_tab === 'custom-test'): ?>
  477. <?php $custom_test_deleter->printHTML() ?>
  478. <?php
  479. $submissions_pag = new Paginator(array(
  480. 'col_names' => array('*'),
  481. 'table_name' => 'custom_test_submissions',
  482. 'cond' => '1',
  483. 'tail' => 'order by id asc',
  484. 'page_len' => 5
  485. ));
  486. foreach ($submissions_pag->get() as $submission) {
  487. $problem = queryProblemBrief($submission['problem_id']);
  488. $submission_result = json_decode($submission['result'], true);
  489. echo '<dl class="dl-horizontal">';
  490. echo '<dt>id</dt>';
  491. echo '<dd>', "#{$submission['id']}", '</dd>';
  492. echo '<dt>problem_id</dt>';
  493. echo '<dd>', "#{$submission['problem_id']}", '</dd>';
  494. echo '<dt>submit time</dt>';
  495. echo '<dd>', $submission['submit_time'], '</dd>';
  496. echo '<dt>submitter</dt>';
  497. echo '<dd>', $submission['submitter'], '</dd>';
  498. echo '<dt>judge_time</dt>';
  499. echo '<dd>', $submission['judge_time'], '</dd>';
  500. echo '</dl>';
  501. echoSubmissionContent($submission, getProblemCustomTestRequirement($problem));
  502. echoCustomTestSubmissionDetails($submission_result['details'], "submission-{$submission['id']}-details");
  503. }
  504. ?>
  505. <?= $submissions_pag->pagination() ?>
  506. <?php elseif ($cur_tab === 'click-zan'): ?>
  507. 没写好QAQ
  508. <?php elseif ($cur_tab === 'search'): ?>
  509. <h2 class="text-center">一周搜索情况</h2>
  510. <div id="search-distribution-chart-week" style="height: 250px;"></div>
  511. <script type="text/javascript">
  512. new Morris.Line({
  513. element: 'search-distribution-chart-week',
  514. data: <?= json_encode(DB::selectAll("select DATE_FORMAT(created_at, '%Y-%m-%d %h:00'), count(*) from search_requests where created_at > now() - interval 1 week group by DATE_FORMAT(created_at, '%Y-%m-%d %h:00')")) ?>,
  515. xkey: "DATE_FORMAT(created_at, '%Y-%m-%d %h:00')",
  516. ykeys: ["count(*)"],
  517. labels: ['number'],
  518. resize: true
  519. });
  520. </script>
  521. <h2 class="text-center">一月搜索情况</h2>
  522. <div id="search-distribution-chart-month" style="height: 250px;"></div>
  523. <script type="text/javascript">
  524. new Morris.Line({
  525. element: 'search-distribution-chart-month',
  526. data: <?= json_encode(DB::selectAll("select DATE_FORMAT(created_at, '%Y-%m-%d'), count(*) from search_requests where created_at > now() - interval 1 week group by DATE_FORMAT(created_at, '%Y-%m-%d')")) ?>,
  527. xkey: "DATE_FORMAT(created_at, '%Y-%m-%d')",
  528. ykeys: ["count(*)"],
  529. labels: ['number'],
  530. resize: true
  531. });
  532. </script>
  533. <?php echoLongTable(array('*'), 'search_requests', "1", 'order by id desc',
  534. '<tr><th>id</th><th>created_at</th><th>remote_addr</th><th>type</th><th>q</th><tr>',
  535. function($row) {
  536. echo '<tr>';
  537. echo '<td>', $row['id'], '</td>';
  538. echo '<td>', $row['created_at'], '</td>';
  539. echo '<td>', $row['remote_addr'], '</td>';
  540. echo '<td>', $row['type'], '</td>';
  541. echo '<td>', HTML::escape($row['q']), '</td>';
  542. echo '</tr>';
  543. }, array(
  544. 'page_len' => 1000
  545. ))
  546. ?>
  547. <?php elseif ($cur_tab === 'judger'): ?>
  548. <div>
  549. <h4>添加评测机</h4>
  550. <?php $judger_adder->printHTML(); ?>
  551. </div>
  552. <div>
  553. <h4>删除评测机</h4>
  554. <?php $judger_deleter->printHTML(); ?>
  555. </div>
  556. <h3>评测机列表</h3>
  557. <?php echoLongTable($judgerlist_cols, 'judger_info', "1=1", '', $judgerlist_header_row, $judgerlist_print_row, $judgerlist_config) ?>
  558. <?php elseif ($cur_tab === 'paste'): ?>
  559. <div>
  560. <h4>Paste管理</h4>
  561. <?php echoPastesList() ?>
  562. </div>
  563. <?php elseif ($cur_tab == 'groups'): ?>
  564. <div>
  565. <h4>用户组管理</h4>
  566. <?php echoGroupsList() ?>
  567. </div>
  568. <div>
  569. <h4>添加用户到组</h4>
  570. <?php $user_attach_form->printHTML(); ?>
  571. </div>
  572. <div>
  573. <h4>添加题目到组</h4>
  574. <?php $problem_attach_form->printHTML(); ?>
  575. </div>
  576. <?php elseif ($cur_tab == 'certs'): ?>
  577. <div>
  578. <h4>将勋章授予某人</h4>
  579. <?php $assign_certs_form->printHTML(); ?>
  580. </div>
  581. <?php endif ?>
  582. </div>
  583. </div>
  584. <?php echoUOJPageFooter() ?>