MDL-41176 do not set description and pw fields in $USER when editing own profile
[moodle.git] / user / edit.php
CommitLineData
ce221eb5 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Allows you to edit a users profile
20 *
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package user
24 */
25
26require_once('../config.php');
27require_once($CFG->libdir.'/gdlib.php');
28require_once($CFG->dirroot.'/user/edit_form.php');
29require_once($CFG->dirroot.'/user/editlib.php');
30require_once($CFG->dirroot.'/user/profile/lib.php');
bb78e249 31require_once($CFG->dirroot.'/user/lib.php');
ce221eb5 32
17c70aa0
PS
33//HTTPS is required in this page when $CFG->loginhttps enabled
34$PAGE->https_required();
ce221eb5 35
36$userid = optional_param('id', $USER->id, PARAM_INT); // user id
37$course = optional_param('course', SITEID, PARAM_INT); // course id (defaults to Site)
17c70aa0 38$cancelemailchange = optional_param('cancelemailchange', 0, PARAM_INT); // course id (defaults to Site)
ce221eb5 39
01ec550b 40$PAGE->set_url('/user/edit.php', array('course'=>$course, 'id'=>$userid));
ce221eb5 41
42if (!$course = $DB->get_record('course', array('id'=>$course))) {
43 print_error('invalidcourseid');
44}
45
46if ($course->id != SITEID) {
47 require_login($course);
48} else if (!isloggedin()) {
49 if (empty($SESSION->wantsurl)) {
50 $SESSION->wantsurl = $CFG->httpswwwroot.'/user/edit.php';
56f52742 51 }
ce221eb5 52 redirect(get_login_url());
53} else {
0601e0ee 54 $PAGE->set_context(context_system::instance());
01ec550b 55 $PAGE->set_pagelayout('standard');
ce221eb5 56}
57
58// Guest can not edit
59if (isguestuser()) {
60 print_error('guestnoeditprofile');
61}
62
63// The user profile we are editing
64if (!$user = $DB->get_record('user', array('id'=>$userid))) {
65 print_error('invaliduserid');
66}
67
68// Guest can not be edited
69if (isguestuser($user)) {
70 print_error('guestnoeditprofile');
71}
72
73// User interests separated by commas
74if (!empty($CFG->usetags)) {
75 require_once($CFG->dirroot.'/tag/lib.php');
76 $user->interests = tag_get_tags_array('user', $user->id);
77}
78
79// remote users cannot be edited
80if (is_mnet_remote_user($user)) {
5f08e124
PL
81 if (user_not_fully_set_up($user)) {
82 $hostwwwroot = $DB->get_field('mnet_host', 'wwwroot', array('id'=>$user->mnethostid));
83 print_error('usernotfullysetup', 'mnet', '', $hostwwwroot);
84 }
ce221eb5 85 redirect($CFG->wwwroot . "/user/view.php?course={$course->id}");
86}
87
99f9f85f
PS
88// load the appropriate auth plugin
89$userauth = get_auth_plugin($user->auth);
90
91if (!$userauth->can_edit_profile()) {
92 print_error('noprofileedit', 'auth');
93}
94
95if ($editurl = $userauth->edit_profile_url()) {
96 // this internal script not used
97 redirect($editurl);
98}
99
ce221eb5 100if ($course->id == SITEID) {
43731030 101 $coursecontext = context_system::instance(); // SYSTEM context
ce221eb5 102} else {
43731030 103 $coursecontext = context_course::instance($course->id); // Course context
ce221eb5 104}
43731030
FM
105$systemcontext = context_system::instance();
106$personalcontext = context_user::instance($user->id);
ce221eb5 107
108// check access control
109if ($user->id == $USER->id) {
110 //editing own profile - require_login() MUST NOT be used here, it would result in infinite loop!
111 if (!has_capability('moodle/user:editownprofile', $systemcontext)) {
112 print_error('cannotedityourprofile');
bb9a123a 113 }
1cb3da36 114
ce221eb5 115} else {
116 // teachers, parents, etc.
117 require_capability('moodle/user:editprofile', $personalcontext);
118 // no editing of guest user account
119 if (isguestuser($user->id)) {
120 print_error('guestnoeditprofileother');
b0c90e6e 121 }
ce221eb5 122 // no editing of primary admin!
4f622c38
PS
123 if (is_siteadmin($user) and !is_siteadmin($USER)) { // Only admins may edit other admins
124 print_error('useradmineditadmin');
f5fc83e8 125 }
ce221eb5 126}
f5fc83e8 127
ce221eb5 128if ($user->deleted) {
129 echo $OUTPUT->header();
130 echo $OUTPUT->heading(get_string('userdeleted'));
131 echo $OUTPUT->footer();
132 die;
133}
05c38e2b 134
ce221eb5 135// Process email change cancellation
136if ($cancelemailchange) {
137 cancel_email_update($user->id);
138}
14a6b7e1 139
ce221eb5 140//load user preferences
141useredit_load_preferences($user);
f9903ed0 142
ce221eb5 143//Load custom profile fields data
144profile_load_data($user);
0be6f678 145
2d836c0b 146
8bdc9cac 147// Prepare the editor and create form
e9de1cf4
RT
148$editoroptions = array(
149 'maxfiles' => EDITOR_UNLIMITED_FILES,
150 'maxbytes' => $CFG->maxbytes,
151 'trusttext' => false,
152 'forcehttps' => false,
153 'context' => $personalcontext
154);
155
64f93798 156$user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
4e782b32
RT
157// Prepare filemanager draft area.
158$draftitemid = 0;
159$filemanagercontext = $editoroptions['context'];
160$filemanageroptions = array('maxbytes' => $CFG->maxbytes,
161 'subdirs' => 0,
162 'maxfiles' => 1,
163 'accepted_types' => 'web_image');
164file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
165$user->imagefile = $draftitemid;
166//create form
167$userform = new user_edit_form(null, array(
168 'editoroptions' => $editoroptions,
fc3aa0fb
RT
169 'filemanageroptions' => $filemanageroptions,
170 'userid' => $user->id));
ce221eb5 171if (empty($user->country)) {
172 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
173 unset($user->country);
174}
175$userform->set_data($user);
d6ace123 176
ce221eb5 177$email_changed = false;
1e1c51a3 178
ce221eb5 179if ($usernew = $userform->get_data()) {
f9903ed0 180
ce221eb5 181 $email_changed_html = '';
d6ace123 182
ce221eb5 183 if ($CFG->emailchangeconfirmation) {
c968c1b7
AD
184 // Users with 'moodle/user:update' can change their email address immediately
185 // Other users require a confirmation email
ce221eb5 186 if (isset($usernew->email) and $user->email != $usernew->email && !has_capability('moodle/user:update', $systemcontext)) {
187 $a = new stdClass();
188 $a->newemail = $usernew->preference_newemail = $usernew->email;
189 $usernew->preference_newemailkey = random_string(20);
190 $usernew->preference_newemailattemptsleft = 3;
191 $a->oldemail = $usernew->email = $user->email;
192
c6a074f8 193 $email_changed_html = $OUTPUT->box(get_string('auth_changingemailaddress', 'auth', $a), 'generalbox', 'notice');
ce221eb5 194 $email_changed_html .= $OUTPUT->continue_button("$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id");
195 $email_changed = true;
d6ace123 196 }
ce221eb5 197 }
d6ace123 198
ce221eb5 199 $authplugin = get_auth_plugin($user->auth);
a3447e10 200
ce221eb5 201 $usernew->timemodified = time();
42086054
SH
202
203 // description editor element may not exist!
204 if (isset($usernew->description_editor)) {
64f93798 205 $usernew = file_postupdate_standard_editor($usernew, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
42086054 206 }
8f0cd6ef 207
bb78e249
RT
208 // Pass a true old $user here.
209 if (!$authplugin->user_update($user, $usernew)) {
210 // Auth update failed.
ce221eb5 211 print_error('cannotupdateprofile');
212 }
6bc1e5d5 213
bb78e249
RT
214 // Update user with new profile data.
215 user_update_user($usernew, false);
216
ce221eb5 217 //update preferences
218 useredit_update_user_preference($usernew);
0be6f678 219
ce221eb5 220 //update interests
221 if (!empty($CFG->usetags)) {
222 useredit_update_interests($usernew, $usernew->interests);
223 }
0be6f678 224
ce221eb5 225 //update user picture
689096bc 226 if (empty($CFG->disableuserimages)) {
4e782b32 227 useredit_update_picture($usernew, $userform, $filemanageroptions);
ce221eb5 228 }
d1c8eb14 229
ce221eb5 230 // update mail bounces
231 useredit_update_bounces($user, $usernew);
7cbb4c96 232
ce221eb5 233 /// update forum track preference
234 useredit_update_trackforums($user, $usernew);
f9903ed0 235
ce221eb5 236 // save custom profile fields data
237 profile_save_data($usernew);
f9a0ea69 238
e1132146 239 // If email was changed and confirmation is required, send confirmation email now to the new address.
ce221eb5 240 if ($email_changed && $CFG->emailchangeconfirmation) {
e1132146 241 $temp_user = $DB->get_record('user', array('id'=>$user->id), '*', MUST_EXIST);
ce221eb5 242 $temp_user->email = $usernew->preference_newemail;
d6ace123 243
ce221eb5 244 $a = new stdClass();
245 $a->url = $CFG->wwwroot . '/user/emailupdate.php?key=' . $usernew->preference_newemailkey . '&id=' . $user->id;
43731030 246 $a->site = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
e1132146 247 $a->fullname = fullname($temp_user, true);
d6ace123 248
c6a074f8
PS
249 $emailupdatemessage = get_string('emailupdatemessage', 'auth', $a);
250 $emailupdatetitle = get_string('emailupdatetitle', 'auth', $a);
d6ace123 251
d8f14128 252 //email confirmation directly rather than using messaging so they will definitely get an email
2b503e40 253 $supportuser = core_user::get_support_user();
c968c1b7 254 if (!$mail_results = email_to_user($temp_user, $supportuser, $emailupdatetitle, $emailupdatemessage)) {
ce221eb5 255 die("could not send email!");
d6ace123 256 }
ce221eb5 257 }
d6ace123 258
cf361a95
PS
259 // Reload from db, we need new full name on this page if we do not redirect.
260 $user = $DB->get_record('user', array('id'=>$user->id), '*', MUST_EXIST);
d6ace123 261
ce221eb5 262 if ($USER->id == $user->id) {
263 // Override old $USER session variable if needed
cf361a95
PS
264 foreach ((array)$user as $variable => $value) {
265 if ($variable === 'description' or $variable === 'password') {
266 // These are not set for security nad perf reasons.
267 continue;
268 }
ce221eb5 269 $USER->$variable = $value;
d6ace123 270 }
a1248ca4
PS
271 // preload custom fields
272 profile_load_custom_fields($USER);
1f33691c 273 }
274
79efabd4
PS
275 if (is_siteadmin() and empty($SITE->shortname)) {
276 // fresh cli install - we need to finish site settings
277 redirect(new moodle_url('/admin/index.php'));
278 }
279
ce221eb5 280 if (!$email_changed || !$CFG->emailchangeconfirmation) {
281 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id");
bb9a123a 282 }
ce221eb5 283}
0be6f678 284
17c70aa0
PS
285// make sure we really are on the https page when https login required
286$PAGE->verify_https_required();
287
0236ec73 288
ce221eb5 289/// Display page header
290$streditmyprofile = get_string('editmyprofile');
291$strparticipants = get_string('participants');
292$userfullname = fullname($user, true);
293
ce221eb5 294$PAGE->set_title("$course->shortname: $streditmyprofile");
295$PAGE->set_heading($course->fullname);
296
297echo $OUTPUT->header();
83f31bcf 298echo $OUTPUT->heading($userfullname);
ce221eb5 299
ce221eb5 300if ($email_changed) {
301 echo $email_changed_html;
302} else {
303/// Finally display THE form
304 $userform->display();
305}
a3f1f815 306
d8734783 307/// and proper footer
ce221eb5 308echo $OUTPUT->footer();
1f33691c 309