uoj-form-lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. class UOJForm {
  3. public $form_name;
  4. public $succ_href;
  5. public $no_submit = false;
  6. public $ctrl_enter_submit = false;
  7. public $extra_validator = null;
  8. public $is_big = false;
  9. public $has_file = false;
  10. public $ajax_submit_js = null;
  11. public $run_at_server_handler = array();
  12. private $data = array();
  13. private $vdata = array();
  14. private $main_html = '';
  15. public $max_post_size = 15728640; // 15M
  16. public $handle;
  17. public $submit_button_config = array();
  18. public function __construct($form_name) {
  19. $this->form_name = $form_name;
  20. $this->succ_href = $_SERVER['REQUEST_URI'];
  21. $this->handle = function(&$vdata) {
  22. };
  23. $this->run_at_server_handler["check-{$this->form_name}"] = function() {
  24. die(json_encode($this->validateAtServer()));
  25. };
  26. $this->run_at_server_handler["submit-{$this->form_name}"] = function() {
  27. if ($this->no_submit) {
  28. become404Page();
  29. }
  30. foreach ($this->data as $field) {
  31. if (!isset($field['no_val']) && !isset($_POST[$field['name']])) {
  32. becomeMsgPage('The form is incomplete.');
  33. }
  34. }
  35. if (UOJContext::requestMethod() == 'POST') {
  36. $len = UOJContext::contentLength();
  37. if ($len === null) {
  38. become403Page();
  39. } elseif ($len > $this->max_post_size) {
  40. becomeMsgPage('The form is too large.');
  41. }
  42. }
  43. crsf_defend();
  44. $errors = $this->validateAtServer();
  45. if ($errors) {
  46. $err_str = '';
  47. foreach ($errors as $name => $err) {
  48. $esc_err = htmlspecialchars($err);
  49. $err_str .= "$name: $esc_err<br />";
  50. }
  51. becomeMsgPage($err_str);
  52. }
  53. $fun = $this->handle;
  54. $fun($this->vdata);
  55. if ($this->succ_href !== 'none') {
  56. header("Location: {$this->succ_href}");
  57. }
  58. die();
  59. };
  60. }
  61. public function setAjaxSubmit($js) {
  62. $GLOBALS['REQUIRE_LIB']['jquery.form'] = '';
  63. $this->ajax_submit_js = $js;
  64. }
  65. public function add($name, $html, $validator_php, $validator_js) {
  66. $this->main_html .= $html;
  67. $this->data[] = array(
  68. 'name' => $name,
  69. 'validator_php' => $validator_php,
  70. 'validator_js' => $validator_js);
  71. }
  72. public function appendHTML($html) {
  73. $this->main_html .= $html;
  74. }
  75. public function addNoVal($name, $html) {
  76. $this->main_html .= $html;
  77. $this->data[] = array(
  78. 'name' => $name,
  79. 'validator_js' => 'always_ok',
  80. 'no_val' => '');
  81. }
  82. public function addHidden($name, $default_value, $validator_php, $validator_js) {
  83. $default_value = htmlspecialchars($default_value);
  84. $html = <<<EOD
  85. <input type="hidden" name="$name" id="input-$name" value="$default_value" />
  86. EOD;
  87. $this->add($name, $html, $validator_php, $validator_js);
  88. }
  89. public function addInput($name, $type, $label_text, $default_value, $validator_php, $validator_js) {
  90. $default_value = htmlspecialchars($default_value);
  91. $html = <<<EOD
  92. <div id="div-$name" class="form-group">
  93. <label for="input-$name" class="col-sm-2 control-label">$label_text</label>
  94. <div class="col-sm-3">
  95. <input type="$type" class="form-control" name="$name" id="input-$name" value="$default_value" />
  96. <span class="help-block" id="help-$name"></span>
  97. </div>
  98. </div>
  99. EOD;
  100. $this->add($name, $html, $validator_php, $validator_js);
  101. }
  102. public function addSelect($name, $options, $label_text, $default_value) {
  103. $default_value = htmlspecialchars($default_value);
  104. $html = <<<EOD
  105. <div id="div-$name" class="form-group">
  106. <label for="input-$name" class="col-sm-2 control-label">$label_text</label>
  107. <div class="col-sm-3">
  108. <select class="form-control" id="input-content" name="$name">
  109. EOD;
  110. foreach ($options as $opt_name => $opt_label) {
  111. if ($opt_name != $default_value) {
  112. $html .= <<<EOD
  113. <option value="$opt_name">$opt_label</option>
  114. EOD;
  115. } else {
  116. $html .= <<<EOD
  117. <option value="$opt_name" selected="selected">$opt_label</option>
  118. EOD;
  119. }
  120. }
  121. $html .= <<<EOD
  122. </select>
  123. </div>
  124. </div>
  125. EOD;
  126. $this->add($name, $html,
  127. function($opt) use ($options) {
  128. return isset($options[$opt]) ? '' : "无效选项";
  129. },
  130. null
  131. );
  132. }
  133. public function addVInput($name, $type, $label_text, $default_value, $validator_php, $validator_js) {
  134. $html = HTML::div_vinput($name, $type, $label_text, $default_value);
  135. $this->add($name, $html, $validator_php, $validator_js);
  136. }
  137. public function addVSelect($name, $options, $label_text, $default_value) {
  138. $default_value = htmlspecialchars($default_value);
  139. $html = <<<EOD
  140. <div id="div-$name">
  141. <label for="input-$name" class="control-label">$label_text</label>
  142. <select class="form-control" id="input-{$name}" name="$name">
  143. EOD;
  144. foreach ($options as $opt_name => $opt_label) {
  145. if ($opt_name != $default_value) {
  146. $html .= <<<EOD
  147. <option value="$opt_name">$opt_label</option>
  148. EOD;
  149. } else {
  150. $html .= <<<EOD
  151. <option value="$opt_name" selected="selected">$opt_label</option>
  152. EOD;
  153. }
  154. }
  155. $html .= <<<EOD
  156. </select>
  157. </div>
  158. EOD;
  159. $this->add($name, $html,
  160. function($opt) use ($options) {
  161. return isset($options[$opt]) ? '' : "无效选项";
  162. },
  163. null
  164. );
  165. }
  166. public function addTextArea($name, $label_text, $default_value, $validator_php, $validator_js) {
  167. $default_value = htmlspecialchars($default_value);
  168. $this->is_big = true;
  169. $html = <<<EOD
  170. <div id="div-$name" class="form-group">
  171. <label for="input-$name" class="col-sm-2 control-label">$label_text</label>
  172. <div class="col-sm-10">
  173. <textarea class="form-control" name="$name" id="input-$name">$default_value</textarea>
  174. <span class="help-block" id="help-$name"></span>
  175. </div>
  176. </div>
  177. EOD;
  178. $this->add($name, $html, $validator_php, $validator_js);
  179. }
  180. public function addVTextArea($name, $label_text, $default_value, $validator_php, $validator_js) {
  181. $default_value = htmlspecialchars($default_value);
  182. $this->is_big = true;
  183. $html = <<<EOD
  184. <div id="div-$name" class="form-group">
  185. <label for="input-$name" class="control-label">$label_text</label>
  186. <textarea class="form-control" name="$name" id="input-$name">$default_value</textarea>
  187. <span class="help-block" id="help-$name"></span>
  188. </div>
  189. EOD;
  190. $this->add($name, $html, $validator_php, $validator_js);
  191. }
  192. public function addCheckBox($name, $label_text, $default_value) {
  193. $default_value = htmlspecialchars($default_value);
  194. $status = $default_value ? 'checked="checked" ' : '';
  195. $html = <<<EOD
  196. <div class="checkbox">
  197. <label for="input-$name"><input type="checkbox" id="input-$name" name="$name" $status/> $label_text</label>
  198. </div>
  199. EOD;
  200. $this->addNoVal($name, $html);
  201. }
  202. public function addCKEditor($name, $label_text, $default_value, $validator_php, $validator_js) {
  203. $default_value = htmlspecialchars($default_value);
  204. global $REQUIRE_LIB;
  205. $REQUIRE_LIB['ckeditor'] = '';
  206. $this->is_big = true;
  207. $html = <<<EOD
  208. <div id="div-$name">
  209. <label for="input-$name" class="control-label">$label_text</label>
  210. <textarea class="ckeditor" name="$name" id="input-$name">$default_value</textarea>
  211. <span class="help-block" id="help-$name"></span>
  212. </div>
  213. EOD;
  214. $this->add($name, $html, $validator_php, $validator_js);
  215. }
  216. public function addBlogEditor(UOJBlogEditor $editor) {
  217. global $REQUIRE_LIB;
  218. $REQUIRE_LIB['blog-editor'] = '';
  219. $this->is_big = true;
  220. $name = $editor->name;
  221. $this->addVInput("{$name}_title", 'text', '标题', $editor->cur_data['title'],
  222. function ($title) use ($editor) {
  223. return $editor->validateTitle();
  224. },
  225. null
  226. );
  227. $content_md_html = HTML::div_vtextarea("{$name}_content_md", '内容', $editor->cur_data['content_md']);
  228. $this->add("{$name}_content_md", $content_md_html,
  229. function ($content_md) use ($editor) {
  230. return $editor->validateContentMd();
  231. },
  232. 'always_ok'
  233. );
  234. $this->appendHTML(<<<EOD
  235. <script type="text/javascript">blog_editor_init("$name");</script>
  236. EOD
  237. );
  238. $this->run_at_server_handler["save-{$name}"] = function() use ($name, $editor) {
  239. if ($this->no_submit) {
  240. become404Page();
  241. }
  242. foreach (array("{$name}_title", "{$name}_content_md") as $field_name) {
  243. if (!isset($_POST[$field_name])) {
  244. becomeMsgPage('The form is incomplete.');
  245. }
  246. }
  247. crsf_defend();
  248. $errors = $this->validateAtServer();
  249. if (!$errors) {
  250. $editor->handleSave();
  251. }
  252. die(json_encode($errors));
  253. };
  254. }
  255. public function addSlideEditor($name, $label_text, $default_value, $validator_php, $validator_js) {
  256. $default_value = htmlspecialchars($default_value);
  257. global $REQUIRE_LIB;
  258. $REQUIRE_LIB['slide-editor'] = '';
  259. $this->is_big = true;
  260. $html = <<<EOD
  261. <div id="div-$name">
  262. <label for="input-$name" class="control-label">$label_text</label>
  263. <textarea name="$name" id="input-$name">$default_value</textarea>
  264. <span class="help-block" id="help-$name"></span>
  265. </div>
  266. <script type="text/javascript">
  267. $('#input-$name').slide_editor();
  268. </script>
  269. EOD;
  270. $this->add($name, $html, $validator_php, $validator_js);
  271. }
  272. static public function uploadedFileTmpName($name) {
  273. if (isset($_FILES[$name]) && is_uploaded_file($_FILES[$name]['tmp_name'])) {
  274. return $_FILES[$name]['tmp_name'];
  275. } else {
  276. return null;
  277. }
  278. }
  279. public function addSourceCodeInput($name, $text, $languages) {
  280. $this->add("{$name}_upload_type", '',
  281. function($type, &$vdata) use ($name) {
  282. if ($type == 'editor') {
  283. if (!isset($_POST["{$name}_editor"])) {
  284. return '你在干啥……怎么什么都没交过来……?';
  285. }
  286. } elseif ($type == 'file') {
  287. } else {
  288. return '……居然既不是用编辑器上传也不是用文件上传的……= =……';
  289. }
  290. },
  291. 'always_ok'
  292. );
  293. $this->addNoVal("{$name}_editor", '');
  294. $this->addNoVal("{$name}_file", '');
  295. $this->add("{$name}_language", '',
  296. function($lang) use ($languages) {
  297. if (!in_array($lang, $languages)) {
  298. return '该语言不被支持';
  299. }
  300. return '';
  301. },
  302. 'always_ok'
  303. );
  304. $preferred_language = Cookie::get('uoj_preferred_language');
  305. if ($preferred_language == null || !in_array($preferred_language, $languages)) {
  306. $preferred_language = $languages[0];
  307. }
  308. $langs_options_str = '';
  309. foreach ($languages as $lang) {
  310. $langs_options_str .= "<option";
  311. if ($lang == $preferred_language) {
  312. $langs_options_str .= ' selected="selected"';
  313. }
  314. $langs_options_str .= ">$lang</option>";
  315. }
  316. $langs_options_json = json_encode($langs_options_str);
  317. $this->appendHTML(<<<EOD
  318. <div class="form-group" id="form-group-$name"></div>
  319. <script type="text/javascript">
  320. $('#form-group-$name').source_code_form_group('$name', '$text', $langs_options_json);
  321. </script>
  322. EOD
  323. );
  324. $this->is_big = true;
  325. $this->has_file = true;
  326. }
  327. public function addTextFileInput($name, $text) {
  328. $this->add("{$name}_upload_type", '',
  329. function($type, &$vdata) use ($name) {
  330. if ($type == 'editor') {
  331. if (!isset($_POST["{$name}_editor"])) {
  332. return '你在干啥……怎么什么都没交过来……?';
  333. }
  334. } elseif ($type == 'file') {
  335. } else {
  336. return '……居然既不是用编辑器上传也不是用文件上传的……= =……';
  337. }
  338. },
  339. 'always_ok'
  340. );
  341. $this->addNoVal("{$name}_editor", '');
  342. $this->addNoVal("{$name}_file", '');
  343. $this->appendHTML(<<<EOD
  344. <div class="form-group" id="form-group-$name"></div>
  345. <script type="text/javascript">
  346. $('#form-group-$name').text_file_form_group('$name', '$text');
  347. </script>
  348. EOD
  349. );
  350. $this->is_big = true;
  351. $this->has_file = true;
  352. }
  353. public function printHTML() {
  354. $form_entype_str = $this->is_big ? ' enctype="multipart/form-data"' : '';
  355. echo '<form action="', $_SERVER['REQUEST_URI'], '" method="post" class="form-horizontal" id="form-', $this->form_name, '"', $form_entype_str, '>';
  356. echo HTML::hiddenToken();
  357. echo $this->main_html;
  358. if (!$this->no_submit) {
  359. if (!isset($this->submit_button_config['align'])) {
  360. $this->submit_button_config['align'] = 'center';
  361. }
  362. if (!isset($this->submit_button_config['text'])) {
  363. $this->submit_button_config['text'] = UOJLocale::get('submit');
  364. }
  365. if (!isset($this->submit_button_config['class_str'])) {
  366. $this->submit_button_config['class_str'] = 'btn btn-secondary';
  367. }
  368. if ($this->submit_button_config['align'] == 'offset') {
  369. echo '<div class="form-group">';
  370. echo '<div class="col-sm-offset-2 col-sm-3">';
  371. } else {
  372. echo '<div class="text-', $this->submit_button_config['align'], '">';
  373. }
  374. echo '<button type="submit" id="button-submit-', $this->form_name, '" name="submit-', $this->form_name, '" value="', $this->form_name, '" class="mt-2 ', $this->submit_button_config['class_str'], '">', $this->submit_button_config['text'], '</button>';
  375. if ($this->submit_button_config['align'] == 'offset') {
  376. echo '</div>';
  377. }
  378. echo '</div>';
  379. }
  380. echo '</form>';
  381. if ($this->no_submit) {
  382. return;
  383. }
  384. echo <<<EOD
  385. <script type="text/javascript">
  386. $(document).ready(function() {
  387. EOD;
  388. if ($this->ctrl_enter_submit) {
  389. echo <<<EOD
  390. $('#form-{$this->form_name}').keydown(function(e) {
  391. if (e.keyCode == 13 && e.ctrlKey) {
  392. $('#button-submit-{$this->form_name}').click();
  393. }
  394. });
  395. EOD;
  396. }
  397. echo <<<EOD
  398. $('#form-{$this->form_name}').submit(function(e) {
  399. var ok = true;
  400. EOD;
  401. $need_ajax = false;
  402. if ($this->extra_validator) {
  403. $need_ajax = true;
  404. }
  405. foreach ($this->data as $field) {
  406. if ($field['validator_js'] != null) {
  407. if ($field['validator_js'] != 'always_ok') {
  408. echo <<<EOD
  409. var ${field['name']}_err = (${field['validator_js']})($('#input-${field['name']}').val());
  410. EOD;
  411. }
  412. } else {
  413. $need_ajax = true;
  414. }
  415. }
  416. if ($need_ajax) {
  417. echo <<<EOD
  418. var post_data = {};
  419. EOD;
  420. foreach ($this->data as $field) {
  421. if ($field['validator_js'] == null) {
  422. echo <<<EOD
  423. var ${field['name']}_err = 'Unknown error';
  424. post_data.${field['name']} = $('#input-${field['name']}').val();
  425. EOD;
  426. }
  427. }
  428. echo <<<EOD
  429. if (post_data != {}) {
  430. post_data['check-{$this->form_name}'] = "";
  431. $.ajax({
  432. url : '${_SERVER['REQUEST_URI']}',
  433. type : 'POST',
  434. dataType : 'json',
  435. async : false,
  436. data : post_data,
  437. success : function(data) {
  438. EOD;
  439. foreach ($this->data as $field) {
  440. if ($field['validator_js'] == null) {
  441. echo <<<EOD
  442. ${field['name']}_err = data.${field['name']};
  443. EOD;
  444. }
  445. }
  446. echo <<<EOD
  447. if (data.extra != undefined) {
  448. alert(data.extra);
  449. ok = false;
  450. }
  451. EOD;
  452. echo <<<EOD
  453. }
  454. });
  455. }
  456. EOD;
  457. }
  458. foreach ($this->data as $field) {
  459. if ($field['validator_js'] != 'always_ok') {
  460. echo <<<EOD
  461. if (${field['name']}_err) {
  462. $('#div-${field['name']}').addClass('has-error');
  463. $('#help-${field['name']}').text(${field['name']}_err);
  464. ok = false;
  465. } else {
  466. $('#div-${field['name']}').removeClass('has-error');
  467. $('#help-${field['name']}').text('');
  468. }
  469. EOD;
  470. }
  471. }
  472. if (isset($this->submit_button_config['smart_confirm'])) {
  473. $this->submit_button_config['confirm_text'] = '你真的要' . $this->submit_button_config['text'] . '吗?';
  474. }
  475. if (isset($this->submit_button_config['confirm_text'])) {
  476. echo <<<EOD
  477. if (!confirm('{$this->submit_button_config['confirm_text']}')) {
  478. ok = false;
  479. }
  480. EOD;
  481. }
  482. if ($this->has_file) {
  483. echo <<<EOD
  484. $(this).find("input[type='file']").each(function() {
  485. for (var i = 0; i < this.files.length; i++) {
  486. if (this.files[i].size > 10 * 1024 * 1024) {
  487. $('#div-' + $(this).attr('name')).addClass('has-error');
  488. $('#help-' + $(this).attr('name')).text('文件大小不能超过10M');
  489. ok = false;
  490. } else {
  491. $('#div-' + $(this).attr('name')).removeClass('has-error');
  492. $('#help-' + $(this).attr('name')).text('');
  493. }
  494. }
  495. });
  496. EOD;
  497. }
  498. if ($this->ajax_submit_js !== null) {
  499. echo <<<EOD
  500. e.preventDefault();
  501. if (ok) {
  502. $(this).ajaxSubmit({
  503. beforeSubmit: function(formData) {
  504. formData.push({name: 'submit-{$this->form_name}', value: '{$this->form_name}', type: 'submit'});
  505. },
  506. success : {$this->ajax_submit_js}
  507. });
  508. }
  509. EOD;
  510. } else {
  511. echo <<<EOD
  512. return ok;
  513. EOD;
  514. }
  515. echo <<<EOD
  516. });
  517. });
  518. </script>
  519. EOD;
  520. }
  521. private function validateAtServer() {
  522. $errors = array();
  523. if ($this->extra_validator) {
  524. $fun = $this->extra_validator;
  525. $err = $fun();
  526. if ($err) {
  527. $errors['extra'] = $err;
  528. }
  529. }
  530. foreach ($this->data as $field) {
  531. if (!isset($field['no_val']) && isset($_POST[$field['name']])) {
  532. $fun = $field['validator_php'];
  533. $err = $fun($_POST[$field['name']], $this->vdata);
  534. if ($err) {
  535. $errors[$field['name']] = $err;
  536. }
  537. }
  538. }
  539. return $errors;
  540. }
  541. public function runAtServer() {
  542. foreach ($this->run_at_server_handler as $type => $handler) {
  543. if (isset($_POST[$type])) {
  544. $handler();
  545. }
  546. }
  547. }
  548. }
  549. function newAddDelCmdForm($form_name, $validate, $handle, $final = null) {
  550. $form = new UOJForm($form_name);
  551. $form->addTextArea(
  552. $form_name . '_cmds', '命令', '',
  553. function($str, &$vdata) use ($validate) {
  554. $cmds = array();
  555. foreach (explode("\n", $str) as $line_id => $raw_line) {
  556. $line = trim($raw_line);
  557. if ($line == '') {
  558. continue;
  559. }
  560. if ($line[0] != '+' && $line[0] != '-') {
  561. return '第' . ($line_id + 1) . '行:格式错误';
  562. }
  563. $obj = trim(substr($line, 1));
  564. if ($err = $validate($obj)) {
  565. return '第' . ($line_id + 1) . '行:' . $err;
  566. }
  567. $cmds[] = array('type' => $line[0], 'obj' => $obj);
  568. }
  569. $vdata['cmds'] = $cmds;
  570. return '';
  571. },
  572. null
  573. );
  574. if (!isset($final)) {
  575. $form->handle = function(&$vdata) use ($handle) {
  576. foreach ($vdata['cmds'] as $cmd) {
  577. $handle($cmd['type'], $cmd['obj']);
  578. }
  579. };
  580. } else {
  581. $form->handle = function(&$vdata) use ($handle, $final) {
  582. foreach ($vdata['cmds'] as $cmd) {
  583. $handle($cmd['type'], $cmd['obj']);
  584. }
  585. $final();
  586. };
  587. }
  588. return $form;
  589. }
  590. function newSubmissionForm($form_name, $requirement, $zip_file_name_gen, $handle) {
  591. $form = new UOJForm($form_name);
  592. foreach ($requirement as $req) {
  593. if ($req['type'] == "source code") {
  594. $languages = isset($req['languages']) ? $req['languages'] : $GLOBALS['uojSupportedLanguages'];
  595. $form->addSourceCodeInput("{$form_name}_{$req['name']}", UOJLocale::get('problems::source code').':'.$req['name'], $languages);
  596. } elseif ($req['type'] == "text") {
  597. $form->addTextFileInput("{$form_name}_{$req['name']}", UOJLocale::get('problems::text file').':'.$req['file_name']);
  598. }
  599. }
  600. $form->handle = function(&$vdata) use ($form_name, $requirement, $zip_file_name_gen, $handle) {
  601. global $myUser;
  602. if ($myUser == null) {
  603. redirectToLogin();
  604. }
  605. $tot_size = 0;
  606. $zip_file_name = $zip_file_name_gen();
  607. $zip_file = new ZipArchive();
  608. if ($zip_file->open(UOJContext::storagePath().$zip_file_name, ZipArchive::CREATE) !== true) {
  609. becomeMsgPage('提交失败');
  610. }
  611. $content = array();
  612. $content['file_name'] = $zip_file_name;
  613. $content['config'] = array();
  614. foreach ($requirement as $req) {
  615. if ($req['type'] == "source code") {
  616. $content['config'][] = array("{$req['name']}_language", $_POST["{$form_name}_{$req['name']}_language"]);
  617. }
  618. }
  619. foreach ($requirement as $req) {
  620. if ($_POST["{$form_name}_{$req['name']}_upload_type"] == 'editor') {
  621. $zip_file->addFromString($req['file_name'], $_POST["{$form_name}_{$req['name']}_editor"]);
  622. } else {
  623. $tmp_name = UOJForm::uploadedFileTmpName("{$form_name}_{$req['name']}_file");
  624. if ($tmp_name == null) {
  625. $zip_file->addFromString($req['file_name'], '');
  626. } else {
  627. $zip_file->addFile($tmp_name, $req['file_name']);
  628. }
  629. }
  630. $stat = $zip_file->statName($req['file_name']);
  631. if ($req['type'] == 'source code') {
  632. $max_size = isset($req['size']) ? (int)$req['size'] : 50;
  633. if ($stat['size'] > $max_size * 1024) {
  634. $zip_file->close();
  635. unlink(UOJContext::storagePath().$zip_file_name);
  636. becomeMsgPage("源代码长度不能超过 {$max_size}KB。");
  637. }
  638. }
  639. $tot_size += $stat['size'];
  640. }
  641. $zip_file->close();
  642. $handle($zip_file_name, $content, $tot_size);
  643. };
  644. return $form;
  645. }
  646. function newZipSubmissionForm($form_name, $requirement, $zip_file_name_gen, $handle) {
  647. $form = new UOJForm($form_name);
  648. $name = "zip_ans_{$form_name}";
  649. $text = UOJLocale::get('problems::zip file upload introduction', join(array_map(function($req) {
  650. return $req['file_name'];
  651. }, $requirement), ', '));
  652. $browse_text = UOJLocale::get('browse');
  653. $html = <<<EOD
  654. <div id="div-{$name}">
  655. <label for="input-{$name}">$text</label>
  656. <input type="file" id="input-{$name}" name="{$name}" style="display:none;" onchange="$('#input-{$name}_path').val($('#input-{$name}').val());" />
  657. <div class="input-group bot-buffer-md">
  658. <input id="input-{$name}_path" class="form-control" type="text" readonly="readonly" />
  659. <span class="input-group-append">
  660. <button type="button" class="btn btn-primary" style="width:100px; !important" onclick="$('#input-{$name}').click();"><span class="glyphicon glyphicon-folder-open"></span> $browse_text</button>
  661. </span>
  662. </div>
  663. <span class="help-block" id="help-{$name}"></span>
  664. </div>
  665. EOD;
  666. $form->addNoVal($name, $html);
  667. $form->is_big = true;
  668. $form->has_file = true;
  669. $form->handle = function() use ($name, $requirement, $zip_file_name_gen, $handle) {
  670. global $myUser;
  671. if ($myUser == null) {
  672. redirectToLogin();
  673. }
  674. if (!isset($_FILES[$name])) {
  675. becomeMsgPage('你在干啥……怎么什么都没交过来……?');
  676. } elseif (!is_uploaded_file($_FILES[$name]['tmp_name'])) {
  677. becomeMsgPage('上传出错,貌似你什么都没交过来……?');
  678. }
  679. $up_zip_file = new ZipArchive();
  680. if ($up_zip_file->open($_FILES[$name]['tmp_name']) !== true) {
  681. becomeMsgPage('不是合法的zip压缩文件');
  682. }
  683. $tot_size = 0;
  684. $zip_content = array();
  685. foreach ($requirement as $req) {
  686. $stat = $up_zip_file->statName($req['file_name']);
  687. if ($stat === false) {
  688. $zip_content[$req['name']] = '';
  689. } else {
  690. $tot_size += $stat['size'];
  691. if ($stat['size'] > 20 * 1024 * 1024) {
  692. becomeMsgPage("文件 {$req['file_name']} 实际大小过大。");
  693. }
  694. $ret = $up_zip_file->getFromName($req['file_name']);
  695. if ($ret === false) {
  696. $zip_content[$req['name']] = '';
  697. } else {
  698. $zip_content[$req['name']] = $ret;
  699. }
  700. }
  701. }
  702. $up_zip_file->close();
  703. $zip_file_name = $zip_file_name_gen();
  704. $zip_file = new ZipArchive();
  705. if ($zip_file->open(UOJContext::storagePath().$zip_file_name, ZipArchive::CREATE) !== true) {
  706. becomeMsgPage('提交失败');
  707. }
  708. foreach ($requirement as $req) {
  709. $zip_file->addFromString($req['file_name'], $zip_content[$req['name']]);
  710. }
  711. $zip_file->close();
  712. $content = array();
  713. $content['file_name'] = $zip_file_name;
  714. $content['config'] = array();
  715. $handle($zip_file_name, $content, $tot_size);
  716. };
  717. return $form;
  718. }
  719. ?>