forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
emailupdate.php
executable file
·57 lines (47 loc) · 2.13 KB
/
emailupdate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php // $Id$
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/user/editlib.php');
$key = required_param('key', PARAM_ALPHANUM);
$id = required_param('id', PARAM_INT);
if (!$user = $DB->get_record('user', array('id' => $id))) {
error("Unknown user ID");
}
$preferences = get_user_preferences(null, null, $user->id);
$a = new stdClass();
$a->fullname = fullname($user, true);
$stremailupdate = get_string('auth_emailupdate', 'auth', $a);
print_header(format_string($SITE->fullname) . ": $stremailupdate", format_string($SITE->fullname) . ": $stremailupdate");
if (empty($preferences['newemailattemptsleft'])) {
redirect("$CFG->wwwroot/user/view.php?id=$user->id");
} elseif ($preferences['newemailattemptsleft'] < 1) {
cancel_email_update($user->id);
$stroutofattempts = get_string('auth_outofnewemailupdateattempts', 'auth');
print_box($stroutofattempts, 'center');
} elseif ($key == $preferences['newemailkey']) {
cancel_email_update($user->id);
$user->email = $preferences['newemail'];
// Detect duplicate before saving
if ($DB->get_record('user', array('email' => $user->email))) {
$stremailnowexists = get_string('auth_emailnowexists', 'auth');
print_box($stremailnowexists, 'center');
print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
} else {
// update user email
if (!$DB->set_field('user', 'email', $user->email, array('id' => $user->id))) {
error('Error updating user record');
} else {
events_trigger('user_updated', $user);
$a->email = $user->email;
$stremailupdatesuccess = get_string('auth_emailupdatesuccess', 'auth', $a);
print_box($stremailupdatesuccess, 'center');
print_continue("$CFG->wwwroot/user/view.php?id=$user->id");
}
}
} else {
$preferences['newemailattemptsleft']--;
set_user_preference('newemailattemptsleft', $preferences['newemailattemptsleft'], $user->id);
$strinvalidkey = get_string('auth_invalidnewemailkey', 'auth');
print_box($strinvalidkey, 'center');
}
print_footer('none');