submit_id.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. list($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. list($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 = [$id_document, $photo];
  25. foreach($files as $index => $file) {
  26. if($index == 0) {
  27. $name = "ID document";
  28. $type = UserFile::TYPE_ID;
  29. }
  30. if($index == 1) {
  31. $name = "photo";
  32. $type = UserFile::TYPE_PHOTO;
  33. }
  34. $output_image = Image::normalise($file->tmp_name);
  35. $user_file = new UserFile;
  36. $user_file->user_id = $user->id;
  37. $user_file->type = $type;
  38. $user_file->dts_uploaded = Util::getDateTime();
  39. $success = $user_file->commit();
  40. if(!$success) throw new Exception(sprintf(__("Failed to save the %s. Please try again later."), $name));
  41. $output_path = $user->getDirectory() . "/" . $user_file->id . ".jpg";
  42. imagejpeg($output_image, $output_path, 60);
  43. imagedestroy($output_image);
  44. }
  45. $user->level = User::LEVEL_UNVERIFIED;
  46. $success = $user->commit();
  47. // TODO: Send out pending verification email
  48. Core::addAlert("success", __("ID submitted successfully."));
  49. HTTP::redirect("/account");
  50. }
  51. catch(FormException $e) {
  52. $e->generateAlerts();
  53. }
  54. catch(Exception $e) {
  55. Core::addAlert("danger", $e->getMessage());
  56. }
  57. }
  58. View::includeOnce("header", compact("page_title"));
  59. View::include("submit_id");
  60. View::includeOnce("footer");
  61. ?>