1
0

submit_id.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. $page_title = __("Submit ID");
  3. Core::addBreadcrumb("/account", __("Account"));
  4. Core::addBreadcrumb("/account/submit-id", $page_title);
  5. $submit = http::value("submit");
  6. if($submit) {
  7. $user = Core::$user;
  8. try {
  9. Core::validateCSRFToken($submit);
  10. $form_exception = new FormException;
  11. $id_document = (object) $_FILES["id_document"];
  12. $photo = (object) $_FILES["photo"];
  13. if($id_document->error !== UPLOAD_ERR_OK) $form_exception->addError(HTTP::getUploadErrorString($id_document->error));
  14. if($photo->error !== UPLOAD_ERR_OK) $form_exception->addError(HTTP::getUploadErrorString($photo->error));
  15. $size = @getimagesize($id_document->tmp_name);
  16. if(!$size) $form_exception->addError(__("The uploaded ID document is not an image."));
  17. [$width, $height, $type] = $size;
  18. if($type !== IMAGETYPE_JPEG) $form_exception->addError(__("The uploaded ID document is not a JPEG."));
  19. $size = @getimagesize($photo->tmp_name);
  20. if(!$size) $form_exception->addError(__("The uploaded photo is not an image."));
  21. [$width, $height, $type] = $size;
  22. if($type !== IMAGETYPE_JPEG) $form_exception->addError(__("The uploaded photo is not a JPEG."));
  23. if($form_exception->hasErrors()) throw $form_exception;
  24. $files = [
  25. ["file" => $id_document, "name" => "ID document", "type" => UserFile::TYPE_ID],
  26. ["file" => $photo, "name" => "photo", "type" => UserFile::TYPE_PHOTO],
  27. ];
  28. foreach($files as $file_info) {
  29. ["file" => $file, "name" => $name, "type" => $type] = $file_info;
  30. $output_image = Image::normalise($file->tmp_name);
  31. $user_file = new UserFile;
  32. $user_file->user_id = $user->id;
  33. $user_file->type = $type;
  34. $user_file->dts_uploaded = Util::getDateTime();
  35. $success = $user_file->commit();
  36. if(!$success) throw new Exception(sprintf(__("Failed to save the %s. Please try again later."), $name));
  37. $output_path = $user->getDirectory() . "/" . $user_file->id . ".jpg";
  38. $success = imagejpeg($output_image, $output_path, 60);
  39. imagedestroy($output_image);
  40. if(!$success) throw new Exception(__("Failed to save %s.", $name));
  41. }
  42. $user->level = User::LEVEL_UNVERIFIED;
  43. $success = $user->commit();
  44. // TODO: Send out pending verification email
  45. Core::addAlert("success", __("ID submitted successfully."));
  46. HTTP::redirect("/account");
  47. }
  48. catch(FormException $e) {
  49. $e->generateAlerts();
  50. }
  51. catch(Exception $e) {
  52. Core::addAlert("danger", $e->getMessage());
  53. }
  54. }
  55. View::includeOnce("header", compact("page_title"));
  56. View::include("submit_id");
  57. View::includeOnce("footer");