emailupdate.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Change a users email address
  18. *
  19. * @copyright 1999 Martin Dougiamas http://dougiamas.com
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_user
  22. */
  23. require_once('../config.php');
  24. require_once($CFG->libdir.'/adminlib.php');
  25. require_once($CFG->dirroot.'/user/editlib.php');
  26. require_once($CFG->dirroot.'/user/lib.php');
  27. $key = required_param('key', PARAM_ALPHANUM);
  28. $id = required_param('id', PARAM_INT);
  29. $PAGE->set_url('/user/emailupdate.php', array('id' => $id, 'key' => $key));
  30. $PAGE->set_context(context_system::instance());
  31. if (!$user = $DB->get_record('user', array('id' => $id))) {
  32. print_error('invaliduserid');
  33. }
  34. $preferences = get_user_preferences(null, null, $user->id);
  35. $a = new stdClass();
  36. $a->fullname = fullname($user, true);
  37. $stremailupdate = get_string('emailupdate', 'auth', $a);
  38. $PAGE->set_title(format_string($SITE->fullname) . ": $stremailupdate");
  39. $PAGE->set_heading(format_string($SITE->fullname) . ": $stremailupdate");
  40. if (empty($preferences['newemailattemptsleft'])) {
  41. redirect("$CFG->wwwroot/user/view.php?id=$user->id");
  42. } else if ($preferences['newemailattemptsleft'] < 1) {
  43. cancel_email_update($user->id);
  44. echo $OUTPUT->header();
  45. echo $OUTPUT->box(get_string('auth_outofnewemailupdateattempts', 'auth'), 'center');
  46. echo $OUTPUT->footer();
  47. } else if ($key == $preferences['newemailkey']) {
  48. $olduser = clone($user);
  49. cancel_email_update($user->id);
  50. $user->email = $preferences['newemail'];
  51. // Detect duplicate before saving.
  52. if ($DB->get_record('user', array('email' => $user->email))) {
  53. redirect(new moodle_url('/user/view.php', ['id' => $user->id]), get_string('emailnowexists', 'auth'));
  54. } else {
  55. // Update user email.
  56. $authplugin = get_auth_plugin($user->auth);
  57. $authplugin->user_update($olduser, $user);
  58. user_update_user($user, false);
  59. $a->email = $user->email;
  60. redirect(
  61. new moodle_url('/user/view.php', ['id' => $user->id]),
  62. get_string('emailupdatesuccess', 'auth', $a),
  63. null,
  64. \core\output\notification::NOTIFY_SUCCESS
  65. );
  66. }
  67. } else {
  68. $preferences['newemailattemptsleft']--;
  69. set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
  70. echo $OUTPUT->header();
  71. echo $OUTPUT->box(get_string('auth_invalidnewemailkey', 'auth'), 'center');
  72. echo $OUTPUT->footer();
  73. }