1
0

settings.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. $page_title = "Settings";
  3. Core::addBreadcrumb("/account", "Account");
  4. Core::addBreadcrumb("/account/settings", $page_title);
  5. $user = Core::$user;
  6. $submit = http::value("submit");
  7. if($submit) {
  8. try {
  9. Core::validateCSRFToken($submit);
  10. $form_exception = new FormException;
  11. // Password
  12. $password = http::value("password");
  13. $password_confirm = http::value("password_confirm");
  14. if($password != $password_confirm) $form_exception->addError("Passwords do not match.");
  15. if($password) {
  16. $user->password = password_hash($password, PASSWORD_DEFAULT, ["cost" => 14]);
  17. }
  18. $user->title = http::value("title", "trim");
  19. $user->first_name = http::value("first_name", "trim");
  20. $user->last_name = http::value("last_name", "trim");
  21. $user->phone = http::value("phone", "trim");
  22. $user->preferred_payment_method = http::value("preferred_payment_method", "int");
  23. $user->bank_name = http::value("bank_name", "trim");
  24. $user->bank_sort_code = http::value("bank_sort_code", "trim");
  25. $user->bank_account_number = http::value("bank_account_number", "trim");
  26. $user->paypal_email = http::value("paypal_email", "email");
  27. if($password && !User::validatePassword($password)) $form_exception->addError("Password must be at least 6 characters in length and contain at least one lowercase, uppercase, number and special character.");
  28. if(empty($user->email)) $form_exception->addError("Email address is invalid.");
  29. if(empty($user->title)) $form_exception->addError("Title is required.");
  30. if(empty($user->first_name)) $form_exception->addError("First name is required.");
  31. if(empty($user->last_name)) $form_exception->addError("Last name is required.");
  32. if(empty($user->date_of_birth)) $form_exception->addError("Date of birth is required.");
  33. if(empty($user->phone)) $form_exception->addError("Mobile number is required.");
  34. if(!isset(User::PAYMENT_METHODS[$user->preferred_payment_method])) $form_exception->addError("Invalid payment method selected.");
  35. // Activation
  36. $deactivate = http::value("deactivate");
  37. if($deactivate && $user->level == User::LEVEL_TESTER) {
  38. $user->level = User::LEVEL_DEACTIVATED;
  39. }
  40. $reactivate = http::value("reactivate");
  41. if($reactivate && $user->level == User::LEVEL_DEACTIVATED) {
  42. $user->level = User::LEVEL_TESTER;
  43. }
  44. if($form_exception->hasErrors()) throw $form_exception;
  45. $success = $user->commit();
  46. Core::addAlert("success", "Settings saved successfully.");
  47. HTTP::redirect("/account/settings");
  48. }
  49. catch(FormException $e) {
  50. $e->generateAlerts();
  51. }
  52. catch(Exception $e) {
  53. Core::addAlert("danger", $e->getMessage());
  54. }
  55. }
  56. View::includeOnce("header", compact("page_title"));
  57. View::include("settings", compact("user"));
  58. View::includeOnce("footer");
  59. ?>