Closed hole when using userid information from form data (merged from STABLE)
[moodle.git] / user / edit.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
dc2590e5 3 require_once("../config.php");
951b22a8 4 require_once("$CFG->libdir/gdlib.php");
f9903ed0 5
e41ddc4b 6 optional_variable($id); // user id
7 optional_variable($course); // course id
f9903ed0 8
e41ddc4b 9 if (empty($id)) { // See your own profile by default
10 require_login();
11 $id = $USER->id;
12 }
8f0cd6ef 13
e41ddc4b 14 if (empty($course)) { // See it at site level by default
15 $course = SITEID;
16 }
f971d502 17
f9903ed0 18 if (! $user = get_record("user", "id", $id)) {
19 error("User ID was incorrect");
20 }
21
22 if (! $course = get_record("course", "id", $course)) {
0087d8a6 23 error("Course ID was incorrect");
f9903ed0 24 }
25
2c104c01 26 if ($user->confirmed and user_not_fully_set_up($user)) {
8f0cd6ef 27 // Special case which can only occur when a new account
faebaf0f 28 // has just been created by EXTERNAL authentication
29 // This is the only page in Moodle that has the exception
30 // so that users can set up their accounts
31 $newaccount = true;
32
deae201f 33 if (empty($USER)) {
ea229804 34 error("Sessions don't seem to be working on this server!");
35 }
36
faebaf0f 37 } else {
38 $newaccount = false;
dc2590e5 39 require_login($course->id);
faebaf0f 40 }
f9903ed0 41
1a86eccc 42 if (($USER->id <> $user->id) && !isadmin()) {
f9903ed0 43 error("You can only edit your own information");
44 }
45
603d4c72 46 if (isguest()) {
47 error("The guest user cannot edit their profile.");
48 }
49
a3447e10 50 if (isguest($user->id)) {
51 error("Sorry, the guest user cannot be edited.");
52 }
53
d35757eb 54 // load the relevant auth libraries
55 if ($user->auth) {
56 $auth = $user->auth;
57 if (!file_exists("$CFG->dirroot/auth/$auth/lib.php")) {
58 $auth = "manual"; // Can't find auth module, default to internal
59 }
60 require_once("$CFG->dirroot/auth/$auth/lib.php");
61 }
f9903ed0 62
d35757eb 63
f9903ed0 64/// If data submitted, then process and store.
65
dc2590e5 66 if ($usernew = data_submitted()) {
999beee0 67
1a86eccc 68 if (($USER->id <> $usernew->id) && !isadmin()) {
69 error("You can only edit your own information");
70 }
71
ab394456 72 if (isset($USER->username)) {
73 check_for_restricted_user($USER->username, "$CFG->wwwroot/course/view.php?id=$course->id");
74 }
75
999beee0 76 foreach ($usernew as $key => $data) {
6acfbb43 77 $usernew->$key = addslashes(clean_text(stripslashes($usernew->$key), FORMAT_MOODLE));
999beee0 78 }
79
9d05e261 80 $usernew->firstname = trim(strip_tags($usernew->firstname));
81 $usernew->lastname = trim(strip_tags($usernew->lastname));
999beee0 82
dc2590e5 83 if (isset($usernew->username)) {
84 $usernew->username = trim(moodle_strtolower($usernew->username));
85 }
86
de38e262 87
81d425b4 88 require_once($CFG->dirroot.'/lib/uploadlib.php');
96038147 89 $um = new upload_manager('imagefile',false,false,null,false,0,true,true);
81d425b4 90
d35757eb 91 if (find_form_errors($user, $usernew, $err, $um)) {
81d425b4 92 if (empty($err['imagefile']) && $usernew->picture = save_profile_image($user->id, $um,'users')) {
1aacb503 93 set_field('user', 'picture', $usernew->picture, 'id', $user->id); /// Note picture in DB
ec67cbf2 94 } else {
95 if (!empty($usernew->deletepicture)) {
96 set_field('user', 'picture', 0, 'id', $user->id); /// Delete picture
97 $usernew->picture = 0;
98 }
a406cdec 99 }
100
a3447e10 101 $user = $usernew;
102
103 } else {
dc2590e5 104 $timenow = time();
8f0cd6ef 105
81d425b4 106 if (!$usernew->picture = save_profile_image($user->id,$um,'users')) {
ec67cbf2 107 if (!empty($usernew->deletepicture)) {
108 set_field('user', 'picture', 0, 'id', $user->id); /// Delete picture
109 $usernew->picture = 0;
110 } else {
111 $usernew->picture = $user->picture;
112 }
f9903ed0 113 }
8f0cd6ef 114
f9903ed0 115 $usernew->timemodified = time();
8f0cd6ef 116
a3447e10 117 if (isadmin()) {
f0eec3b6 118 if (!empty($usernew->newpassword)) {
a3447e10 119 $usernew->password = md5($usernew->newpassword);
d35757eb 120 // update external passwords
121 if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
a3f1f815 122 if (function_exists('auth_user_update_password')){
d35757eb 123 if (!auth_user_update_password($user->username, $usernew->newpassword)){
124 error('Failed to update password on external auth: ' . $user->auth .
125 '. See the server logs for more details.');
126 }
127 } else {
128 error('Your external authentication module is misconfigued!');
129 }
130 }
a3f1f815 131 }
132 // store forcepasswordchange in user's preferences
6eb3e776 133 if (!empty($usernew->forcepasswordchange)){
a3f1f815 134 set_user_preference('auth_forcepasswordchange', 1, $user->id);
135 } else {
6eb3e776 136 unset_user_preference('auth_forcepasswordchange', $user->id);
a3447e10 137 }
138 } else {
139 if (isset($usernew->newpassword)) {
140 error("You can not change the password like that");
141 }
142 }
ef9955b0 143 if ($usernew->url and !(substr($usernew->url, 0, 4) == "http")) {
144 $usernew->url = "http://".$usernew->url;
145 }
873960de 146
f9903ed0 147 if (update_record("user", $usernew)) {
d35757eb 148 if (function_exists("auth_user_update")){
149 auth_user_update($user, $usernew);
150 }
151 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
873960de 152
a3447e10 153 if ($user->id == $USER->id) {
154 // Copy data into $USER session variable
155 $usernew = (array)$usernew;
156 foreach ($usernew as $variable => $value) {
521d04cf 157 $USER->$variable = stripslashes($value);
a3447e10 158 }
1f33691c 159 if (isset($USER->newadminuser)) {
160 unset($USER->newadminuser);
dd85cc81 161 redirect("$CFG->wwwroot/", get_string("changessaved"));
1f33691c 162 }
8f0cd6ef 163 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id", get_string("changessaved"));
a3447e10 164 } else {
003296c4 165 redirect("$CFG->wwwroot/$CFG->admin/user.php", get_string("changessaved"));
873960de 166 }
f9903ed0 167 } else {
168 error("Could not update the user record ($user->id)");
169 }
dc2590e5 170 }
f9903ed0 171 }
8f0cd6ef 172
f9903ed0 173/// Otherwise fill and print the form.
174
faebaf0f 175 $streditmyprofile = get_string("editmyprofile");
176 $strparticipants = get_string("participants");
177 $strnewuser = get_string("newuser");
8553b700 178
faebaf0f 179 if (($user->firstname and $user->lastname) or $newaccount) {
180 if ($newaccount) {
181 $userfullname = $strnewuser;
182 } else {
5fde0ca6 183 $userfullname = fullname($user, isteacher($course->id));
faebaf0f 184 }
7cbb4c96 185 if ($course->category) {
dc2590e5 186 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
8f0cd6ef 187 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
031c49fa 188 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
8f0cd6ef 189 -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
faebaf0f 190 -> $streditmyprofile", "");
7cbb4c96 191 } else {
1f33691c 192 if (isset($USER->newadminuser)) {
193 print_header();
194 } else {
195 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
8f0cd6ef 196 "<a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
1f33691c 197 -> $streditmyprofile", "");
198 }
7cbb4c96 199 }
f9903ed0 200 } else {
faebaf0f 201 $userfullname = $strnewuser;
7cbb4c96 202 $straddnewuser = get_string("addnewuser");
203
204 $stradministration = get_string("administration");
dc2590e5 205 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
55e4b5f9 206 "<a href=\"$CFG->wwwroot/$CFG->admin/\">$stradministration</a> -> ".
207 "<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">$strusers</a> -> $straddnewuser", "");
f9903ed0 208 }
209
bda8d43a 210 $teacher = strtolower($course->teacher);
a3447e10 211 if (!isadmin()) {
212 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
9c9f7d77 213 } else {
214 $teacheronly = "";
a3447e10 215 }
bda8d43a 216
7cbb4c96 217 print_heading( get_string("userprofilefor", "", "$userfullname") );
1f33691c 218
219 if (isset($USER->newadminuser)) {
d2b6ba70 220 print_simple_box(get_string("configintroadmin"), "center", "50%");
1f33691c 221 echo "<br />";
222 }
223
4d0dde91 224 print_simple_box_start("center", "", "$THEME->cellheading");
a3f1f815 225
9c9f7d77 226 if (!empty($err)) {
d35757eb 227 echo "<center>";
228 notify(get_string("someerrorswerefound"));
229 echo "</center>";
a406cdec 230 }
a3f1f815 231
dc2590e5 232 include("edit.html");
a3f1f815 233
234 if (!isadmin()) { /// Lock all the locked fields using Javascript
235 $fields = get_user_fieldnames();
236
237 echo '<script type="text/javascript">'."\n";
238 echo '<!--'."\n";
239
240 foreach ($fields as $field) {
241 $configvariable = 'auth_user_'.$field.'_editlock';
242 if (!empty($CFG->$configvariable)) {
243 echo "eval('document.form.$field.disabled=true');\n";
244 }
245 }
246
247 echo '-->'."\n";
248 echo '</script>'."\n";
249 }
250
f9903ed0 251 print_simple_box_end();
f9903ed0 252
1f33691c 253 if (!isset($USER->newadminuser)) {
254 print_footer($course);
255 }
256
257 exit;
f9903ed0 258
259
260
261/// FUNCTIONS ////////////////////
262
81d425b4 263function find_form_errors(&$user, &$usernew, &$err, &$um) {
c9ca1fa5 264 global $CFG;
f9903ed0 265
a3447e10 266 if (isadmin()) {
2b25f2a0 267 if (empty($usernew->username)) {
a3447e10 268 $err["username"] = get_string("missingusername");
269
2b25f2a0 270 } else if (record_exists("user", "username", $usernew->username) and $user->username == "changeme") {
d35757eb 271 $err["username"] = get_string("usernameexists");
2b25f2a0 272
273 } else {
c9ca1fa5 274 if (empty($CFG->extendedusernamechars)) {
275 $string = eregi_replace("[^(-\.[:alnum:])]", "", $usernew->username);
276 if (strcmp($usernew->username, $string)) {
277 $err["username"] = get_string("alphanumerical");
278 }
279 }
2b25f2a0 280 }
281
ecac660c 282 if (empty($usernew->newpassword) and empty($user->password) and is_internal_auth() )
a3447e10 283 $err["newpassword"] = get_string("missingpassword");
e98e0915 284
09ba0c8a 285 if (($usernew->newpassword == "admin") or ($user->password == md5("admin") and empty($usernew->newpassword)) ) {
e98e0915 286 $err["newpassword"] = get_string("unsafepassword");
09ba0c8a 287 }
a3447e10 288 }
289
f9903ed0 290 if (empty($usernew->email))
8553b700 291 $err["email"] = get_string("missingemail");
f9903ed0 292
87f3a895 293 if (empty($usernew->description) and !isadmin())
a3447e10 294 $err["description"] = get_string("missingdescription");
295
bda8d43a 296 if (empty($usernew->city))
8553b700 297 $err["city"] = get_string("missingcity");
bda8d43a 298
9c055aa5 299 if (empty($usernew->firstname))
300 $err["firstname"] = get_string("missingfirstname");
301
302 if (empty($usernew->lastname))
303 $err["lastname"] = get_string("missinglastname");
304
bda8d43a 305 if (empty($usernew->country))
8553b700 306 $err["country"] = get_string("missingcountry");
bda8d43a 307
21f01485 308 if (! validate_email($usernew->email)) {
8553b700 309 $err["email"] = get_string("invalidemail");
f9903ed0 310
21f01485 311 } else if ($otheruser = get_record("user", "email", $usernew->email)) {
f9903ed0 312 if ($otheruser->id <> $user->id) {
8553b700 313 $err["email"] = get_string("emailexists");
f9903ed0 314 }
315 }
8f0cd6ef 316
21f01485 317 if (empty($err["email"]) and !isadmin()) {
85a1d4c9 318 if ($error = email_is_not_allowed($usernew->email)) {
319 $err["email"] = $error;
21f01485 320 }
321 }
a3f1f815 322
81d425b4 323 if (!$um->preprocess_files()) {
324 $err['imagefile'] = $um->notify;
325 }
f9903ed0 326
a3f1f815 327 if (!isadmin()) { /// Make sure that locked fields are not being edited
328 $fields = get_user_fieldnames();
329
330 foreach ($fields as $field) {
331 $configvariable = 'auth_user_'.$field.'_editlock';
332 if (!empty($CFG->$configvariable)) {
333 if ($user->$field !== $usernew->$field) {
334 $err[$field] = get_string("editlock");
335 }
336 }
337 }
d35757eb 338 }
339
f9903ed0 340 $user->email = $usernew->email;
341
342 return count($err);
343}
344
345
346?>