email_change_confirmation.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * This procedure handles the confirmation url when requesting an email change
  4. *
  5. * Expected input is:
  6. * u: for the user_guid
  7. * c: for the validation code
  8. */
  9. $user_guid = (int) get_input("u");
  10. $validation_code = get_input("c");
  11. $forward_url = "";
  12. if (empty($user_guid) || empty($validation_code)) {
  13. register_error(elgg_echo("error:missing_data"));
  14. forward();
  15. }
  16. $user = elgg_get_logged_in_user_entity();
  17. if (($user_guid != $user->getGUID()) || !$user->canEdit()) {
  18. register_error(elgg_echo("security_tools:email_change_confirmation:error:user"));
  19. forward();
  20. }
  21. $new_email = $user->getAnnotations("email_change_confirmation");
  22. if (empty($new_email)) {
  23. register_error(elgg_echo("security_tools:email_change_confirmation:error:request"));
  24. forward();
  25. }
  26. $new_email = $new_email[0]->value;
  27. $valid_code = security_tools_generate_email_code($user, $new_email);
  28. if ($validation_code !== $valid_code) {
  29. register_error(elgg_echo("security_tools:email_change_confirmation:error:code"));
  30. forward();
  31. }
  32. $site = elgg_get_site_entity();
  33. // send confirmation to old email that change occured
  34. $subject = elgg_echo("security_tools:notify_user:email_change:subject", array($site->name));
  35. $message = elgg_echo("security_tools:notify_user:email_change:message", array(
  36. $user->name,
  37. $site->name,
  38. ));
  39. notify_user($user->getGUID(), $site->getGUID(), $subject, $message, null, "email");
  40. $user->email = $new_email;
  41. if ($user->save()) {
  42. $user->deleteAnnotations("email_change_confirmation");
  43. $forward_url = $user->getURL();
  44. system_message(elgg_echo("email:save:success"));
  45. } else {
  46. register_error(elgg_echo("email:save:fail"));
  47. }
  48. forward($forward_url);