practice.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. Core::addScript("public/js/app-practice-test.js");
  3. $template = Template::get($id);
  4. if(!$template) throw new Exception(__("Practice form not found."));
  5. if(!$template->practice) throw new Exception(__("This practice form is no longer available."));
  6. $page_title = __("Practice");
  7. Core::addBreadcrumb("/account", __("Account"));
  8. Core::addBreadcrumb("/account/practice", $page_title);
  9. Core::addBreadcrumb("/account/practice/$id", __("Form"));
  10. $json = json_decode($template->getJSON());
  11. // Reset
  12. $reset = http::value("reset");
  13. $tests = $_SESSION["practice_tests"] ?? [];
  14. $test = $tests[$id] ?? false;
  15. if(!$test || $reset) {
  16. $test = new PracticeTest;
  17. $test->tester_id = Core::$user->id;
  18. $test->status = Test::STATUS_CONFIRMED;
  19. $test->setTemplateId($template->id);
  20. $tests[$id] = $test;
  21. $_SESSION["practice_tests"] = $tests;
  22. if($reset) {
  23. // Force redirect if resetting
  24. http::redirect("/account/practice/$id");
  25. }
  26. }
  27. $is_confirmed = $test->status == Test::STATUS_CONFIRMED;
  28. $is_started = $test->status == Test::STATUS_STARTED;
  29. $is_unverified = $test->status == Test::STATUS_PENDING_VERIFICATION;
  30. $submit = http::value("submit");
  31. if($submit) {
  32. $previous_timezone = date_default_timezone_get();
  33. try {
  34. Core::validateCSRFToken($submit);
  35. $form_exception = new FormException;
  36. if($is_confirmed) {
  37. // Parse before the test form
  38. $fields = $json->beforeTest;
  39. }
  40. if($is_started || $is_unverified) {
  41. // Parse after the test form
  42. $fields = $json->duringTest;
  43. // Start/End times
  44. if($is_started) {
  45. $start_override = http::getDateTimeFromFields("start_override");
  46. $end_override = http::getDateTimeFromFields("end_override");
  47. $start_unix = null;
  48. $end_unix = null;
  49. if($start_override && $end_override) {
  50. $start_unix = strtotime($start_override);
  51. $end_unix = strtotime($end_override);
  52. if($start_unix > $end_unix) $form_exception->addError(__("Entry Time must not be after Exit Time."));
  53. if(!$start_unix && $end_unix && $test->dts_start) {
  54. $recorded_start_unix = Util::getUnixTimeFromUTC($test->dts_start);
  55. if($end_unix < $recorded_start_unix) $form_exception->addError(__("Exit Time cannot be later than the recorded Entry Time."));
  56. }
  57. }
  58. else {
  59. $form_exception->addError(__("Entry Time and Exit Time must be filled in."));
  60. }
  61. }
  62. }
  63. foreach($fields as $field) {
  64. $name = $field->name ?? false;
  65. if(empty($name)) continue; // Not a field
  66. $required_key = $field->requiredKey ?? null;
  67. if($required_key) continue;
  68. $label = $field->label ?? "";
  69. $type = $field->type;
  70. $required = $field->required ?? false;
  71. $values = $field->values ?? [];
  72. if($type == "file") {
  73. $file = (object) $_FILES[$name];
  74. try {
  75. if(!$required && $file->error == UPLOAD_ERR_NO_FILE) continue;
  76. if($required && $file->error == UPLOAD_ERR_NO_FILE) {
  77. throw new Exception(sprintf(__("%s is required."), $label));
  78. }
  79. // Detect upload error
  80. if($file->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($file->error));
  81. // Get image details
  82. $size = @getimagesize($file->tmp_name);
  83. if(!$size) throw new Exception(sprintf(__("The uploaded file for %s is not an image.")), $label);
  84. list($width, $height, $type) = $size;
  85. if($type != IMAGETYPE_JPEG) throw new Exception(sprintf(__("The uploaded file for %s is not a JPEG."), $label));
  86. // Resize image
  87. $output_image = Image::normalise($file->tmp_name);
  88. // Write image to disk
  89. $output_path = $test->getDirectory() . "/" . $name . ".jpg";
  90. imagejpeg($output_image, $output_path, 60);
  91. imagedestroy($output_image);
  92. }
  93. catch(Exception $e) {
  94. $form_exception->addError($e->getMessage());
  95. }
  96. }
  97. else {
  98. $value = http::value($name, "trim");
  99. if($required && empty($value)) $form_exception->addError(sprintf(__("%s is required."), $label));
  100. if($values && !in_array($value, $values)) $form_exception->addError(sprintf(__("Value for %s is invalid."), $label));
  101. if(in_array($name, Test::TEST_FIELDS)) {
  102. $test->$name = $value;
  103. }
  104. else {
  105. $test->setValue($name, $value);
  106. }
  107. }
  108. }
  109. if($form_exception->hasErrors()) throw $form_exception;
  110. date_default_timezone_set("UTC");
  111. // Before the Test
  112. if($is_confirmed) {
  113. $test->status = Test::STATUS_STARTED;
  114. $test->dts_start = Util::getDateTime();
  115. $test->dts_updated = Util::getDateTime();
  116. $tests[$id] = $test;
  117. $_SESSION["practice_tests"] = $tests;
  118. HTTP::redirect("/account/practice/$id");
  119. }
  120. // During the test
  121. if($is_started || $is_unverified) {
  122. $test->status = Test::STATUS_PENDING_VERIFICATION;
  123. if($is_started) {
  124. if($start_unix && $end_unix) {
  125. $test->dts_start = date("Y-m-d H:i:s", $start_unix);
  126. $test->dts_end = date("Y-m-d H:i:s", $end_unix);
  127. }
  128. }
  129. $test->pass = ($test->challenge_stage <= 2) ? 1 : 0; // Pass = Challenged during browsing or before
  130. $test->dts_updated = Util::getDateTime();
  131. $tests[$id] = $test;
  132. $_SESSION["practice_tests"] = $tests;
  133. JSON::header();
  134. JSON::success();
  135. }
  136. }
  137. catch(FormException $e) {
  138. if(HTTP::isAJAX()) {
  139. JSON::header();
  140. $e->generateJSONError();
  141. }
  142. else {
  143. $e->generateAlerts();
  144. }
  145. }
  146. catch(Exception $e) {
  147. Core::addAlert("danger", $e->getMessage());
  148. }
  149. date_default_timezone_set($previous_timezone);
  150. }
  151. if($is_unverified) {
  152. $fields = array_merge($json->beforeTest, $json->duringTest);
  153. }
  154. View::includeOnce("header", compact("page_title"));
  155. View::include("practice_form", compact("test", "template", "fields"));
  156. View::includeOnce("footer");