Some new errors
[moodle.git] / user / edit.php
CommitLineData
1066e0dc 1<?php // $Id$
f9903ed0 2
dc2590e5 3 require_once("../config.php");
951b22a8 4 require_once("$CFG->libdir/gdlib.php");
f9903ed0 5
b6c93894 6 $id = optional_param('id', PARAM_INT); // user id
7 $course = optional_param('course', PARAM_INT); // 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
1066e0dc 33 if (empty($USER->id)) {
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
b6c93894 76 // data cleanup
77 // username is validated in find_form_errors
78 $usernew->country = clean_param($usernew->country, PARAM_ALPHA);
79 $usernew->lang = clean_param($usernew->lang, PARAM_FILE);
80 $usernew->url = clean_param($usernew->url, PARAM_URL);
81 $usernew->icq = clean_param($usernew->icq, PARAM_INT);
766d2bf3 82 if (!$usernew->icq) {
83 $usernew->icq = '';
84 }
85 $usernew->skype = clean_param($usernew->skype, PARAM_CLEAN);
86 $usernew->yahoo = clean_param($usernew->yahoo, PARAM_CLEAN);
87 $usernew->aim = clean_param($usernew->aim, PARAM_CLEAN);
88 $usernew->msn = clean_param($usernew->msn, PARAM_CLEAN);
b6c93894 89
90 $usernew->maildisplay = clean_param($usernew->maildisplay, PARAM_INT);
91 $usernew->mailformat = clean_param($usernew->mailformat, PARAM_INT);
92 $usernew->maildigest = clean_param($usernew->maildigest, PARAM_INT);
93 $usernew->autosubscribe = clean_param($usernew->autosubscribe, PARAM_INT);
94 $usernew->htmleditor = clean_param($usernew->htmleditor, PARAM_INT);
95 $usernew->emailstop = clean_param($usernew->emailstop, PARAM_INT);
96
999beee0 97 foreach ($usernew as $key => $data) {
6acfbb43 98 $usernew->$key = addslashes(clean_text(stripslashes($usernew->$key), FORMAT_MOODLE));
999beee0 99 }
100
9d05e261 101 $usernew->firstname = trim(strip_tags($usernew->firstname));
102 $usernew->lastname = trim(strip_tags($usernew->lastname));
999beee0 103
dc2590e5 104 if (isset($usernew->username)) {
105 $usernew->username = trim(moodle_strtolower($usernew->username));
106 }
107
de38e262 108
81d425b4 109 require_once($CFG->dirroot.'/lib/uploadlib.php');
96038147 110 $um = new upload_manager('imagefile',false,false,null,false,0,true,true);
81d425b4 111
d35757eb 112 if (find_form_errors($user, $usernew, $err, $um)) {
81d425b4 113 if (empty($err['imagefile']) && $usernew->picture = save_profile_image($user->id, $um,'users')) {
1aacb503 114 set_field('user', 'picture', $usernew->picture, 'id', $user->id); /// Note picture in DB
ec67cbf2 115 } else {
116 if (!empty($usernew->deletepicture)) {
117 set_field('user', 'picture', 0, 'id', $user->id); /// Delete picture
118 $usernew->picture = 0;
119 }
a406cdec 120 }
121
b36a8fc4 122 $usernew->auth = $user->auth;
a3447e10 123 $user = $usernew;
124
125 } else {
dc2590e5 126 $timenow = time();
b36a8fc4 127
81d425b4 128 if (!$usernew->picture = save_profile_image($user->id,$um,'users')) {
ec67cbf2 129 if (!empty($usernew->deletepicture)) {
130 set_field('user', 'picture', 0, 'id', $user->id); /// Delete picture
131 $usernew->picture = 0;
132 } else {
133 $usernew->picture = $user->picture;
134 }
f9903ed0 135 }
8f0cd6ef 136
f9903ed0 137 $usernew->timemodified = time();
8f0cd6ef 138
a3447e10 139 if (isadmin()) {
f0eec3b6 140 if (!empty($usernew->newpassword)) {
a3447e10 141 $usernew->password = md5($usernew->newpassword);
d35757eb 142 // update external passwords
143 if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
a3f1f815 144 if (function_exists('auth_user_update_password')){
d35757eb 145 if (!auth_user_update_password($user->username, $usernew->newpassword)){
146 error('Failed to update password on external auth: ' . $user->auth .
147 '. See the server logs for more details.');
148 }
149 } else {
150 error('Your external authentication module is misconfigued!');
151 }
152 }
a3f1f815 153 }
154 // store forcepasswordchange in user's preferences
6eb3e776 155 if (!empty($usernew->forcepasswordchange)){
a3f1f815 156 set_user_preference('auth_forcepasswordchange', 1, $user->id);
157 } else {
6eb3e776 158 unset_user_preference('auth_forcepasswordchange', $user->id);
a3447e10 159 }
160 } else {
161 if (isset($usernew->newpassword)) {
162 error("You can not change the password like that");
163 }
164 }
ef9955b0 165 if ($usernew->url and !(substr($usernew->url, 0, 4) == "http")) {
166 $usernew->url = "http://".$usernew->url;
167 }
873960de 168
f9903ed0 169 if (update_record("user", $usernew)) {
b36a8fc4 170 if (function_exists("auth_user_update")){
171 // pass a true $userold here
172 auth_user_update($userold, $usernew);
173 };
174
bb64b51a 175 if ($userold->email != $usernew->email) {
176 set_bounce_count($usernew,true);
177 set_send_count($usernew,true);
178 }
179
d35757eb 180 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
873960de 181
a3447e10 182 if ($user->id == $USER->id) {
183 // Copy data into $USER session variable
184 $usernew = (array)$usernew;
185 foreach ($usernew as $variable => $value) {
521d04cf 186 $USER->$variable = stripslashes($value);
a3447e10 187 }
1f33691c 188 if (isset($USER->newadminuser)) {
189 unset($USER->newadminuser);
dd85cc81 190 redirect("$CFG->wwwroot/", get_string("changessaved"));
1f33691c 191 }
8f0cd6ef 192 redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id", get_string("changessaved"));
a3447e10 193 } else {
003296c4 194 redirect("$CFG->wwwroot/$CFG->admin/user.php", get_string("changessaved"));
873960de 195 }
f9903ed0 196 } else {
197 error("Could not update the user record ($user->id)");
198 }
dc2590e5 199 }
f9903ed0 200 }
8f0cd6ef 201
f9903ed0 202/// Otherwise fill and print the form.
203
faebaf0f 204 $streditmyprofile = get_string("editmyprofile");
205 $strparticipants = get_string("participants");
206 $strnewuser = get_string("newuser");
8553b700 207
bb64b51a 208 if (over_bounce_threshold($user) && empty($err['email'])) {
209 $err['email'] = get_string('toomanybounces');
210 }
211
faebaf0f 212 if (($user->firstname and $user->lastname) or $newaccount) {
213 if ($newaccount) {
214 $userfullname = $strnewuser;
215 } else {
5fde0ca6 216 $userfullname = fullname($user, isteacher($course->id));
faebaf0f 217 }
7cbb4c96 218 if ($course->category) {
dc2590e5 219 print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
8f0cd6ef 220 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
031c49fa 221 -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
8f0cd6ef 222 -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
faebaf0f 223 -> $streditmyprofile", "");
7cbb4c96 224 } else {
1f33691c 225 if (isset($USER->newadminuser)) {
226 print_header();
227 } else {
228 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
8f0cd6ef 229 "<a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
1f33691c 230 -> $streditmyprofile", "");
231 }
7cbb4c96 232 }
f9903ed0 233 } else {
faebaf0f 234 $userfullname = $strnewuser;
7cbb4c96 235 $straddnewuser = get_string("addnewuser");
236
237 $stradministration = get_string("administration");
dc2590e5 238 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
55e4b5f9 239 "<a href=\"$CFG->wwwroot/$CFG->admin/\">$stradministration</a> -> ".
240 "<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">$strusers</a> -> $straddnewuser", "");
f9903ed0 241 }
242
bda8d43a 243 $teacher = strtolower($course->teacher);
a3447e10 244 if (!isadmin()) {
245 $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
9c9f7d77 246 } else {
247 $teacheronly = "";
a3447e10 248 }
bda8d43a 249
7cbb4c96 250 print_heading( get_string("userprofilefor", "", "$userfullname") );
1f33691c 251
252 if (isset($USER->newadminuser)) {
6f07ab64 253 print_simple_box(get_string("configintroadmin", 'admin'), "center", "50%");
1f33691c 254 echo "<br />";
255 }
256
b77c41c1 257 print_simple_box_start("center");
a3f1f815 258
9c9f7d77 259 if (!empty($err)) {
d35757eb 260 echo "<center>";
261 notify(get_string("someerrorswerefound"));
262 echo "</center>";
a406cdec 263 }
a3f1f815 264
dc2590e5 265 include("edit.html");
a3f1f815 266
267 if (!isadmin()) { /// Lock all the locked fields using Javascript
268 $fields = get_user_fieldnames();
269
270 echo '<script type="text/javascript">'."\n";
271 echo '<!--'."\n";
272
273 foreach ($fields as $field) {
274 $configvariable = 'auth_user_'.$field.'_editlock';
275 if (!empty($CFG->$configvariable)) {
276 echo "eval('document.form.$field.disabled=true');\n";
277 }
278 }
279
280 echo '-->'."\n";
281 echo '</script>'."\n";
282 }
283
f9903ed0 284 print_simple_box_end();
f9903ed0 285
1f33691c 286 if (!isset($USER->newadminuser)) {
287 print_footer($course);
288 }
289
290 exit;
f9903ed0 291
292
293
294/// FUNCTIONS ////////////////////
295
81d425b4 296function find_form_errors(&$user, &$usernew, &$err, &$um) {
c9ca1fa5 297 global $CFG;
f9903ed0 298
a3447e10 299 if (isadmin()) {
2b25f2a0 300 if (empty($usernew->username)) {
a3447e10 301 $err["username"] = get_string("missingusername");
302
2b25f2a0 303 } else if (record_exists("user", "username", $usernew->username) and $user->username == "changeme") {
d35757eb 304 $err["username"] = get_string("usernameexists");
2b25f2a0 305
306 } else {
c9ca1fa5 307 if (empty($CFG->extendedusernamechars)) {
308 $string = eregi_replace("[^(-\.[:alnum:])]", "", $usernew->username);
309 if (strcmp($usernew->username, $string)) {
310 $err["username"] = get_string("alphanumerical");
311 }
312 }
2b25f2a0 313 }
314
ecac660c 315 if (empty($usernew->newpassword) and empty($user->password) and is_internal_auth() )
a3447e10 316 $err["newpassword"] = get_string("missingpassword");
e98e0915 317
09ba0c8a 318 if (($usernew->newpassword == "admin") or ($user->password == md5("admin") and empty($usernew->newpassword)) ) {
e98e0915 319 $err["newpassword"] = get_string("unsafepassword");
09ba0c8a 320 }
a3447e10 321 }
322
f9903ed0 323 if (empty($usernew->email))
8553b700 324 $err["email"] = get_string("missingemail");
f9903ed0 325
bb64b51a 326 if (over_bounce_threshold($user) && $user->email == $usernew->email)
327 $err['email'] = get_string('toomanybounces');
328
87f3a895 329 if (empty($usernew->description) and !isadmin())
a3447e10 330 $err["description"] = get_string("missingdescription");
331
bda8d43a 332 if (empty($usernew->city))
8553b700 333 $err["city"] = get_string("missingcity");
bda8d43a 334
9c055aa5 335 if (empty($usernew->firstname))
336 $err["firstname"] = get_string("missingfirstname");
337
338 if (empty($usernew->lastname))
339 $err["lastname"] = get_string("missinglastname");
340
bda8d43a 341 if (empty($usernew->country))
8553b700 342 $err["country"] = get_string("missingcountry");
bda8d43a 343
21f01485 344 if (! validate_email($usernew->email)) {
8553b700 345 $err["email"] = get_string("invalidemail");
f9903ed0 346
21f01485 347 } else if ($otheruser = get_record("user", "email", $usernew->email)) {
f9903ed0 348 if ($otheruser->id <> $user->id) {
8553b700 349 $err["email"] = get_string("emailexists");
f9903ed0 350 }
351 }
8f0cd6ef 352
21f01485 353 if (empty($err["email"]) and !isadmin()) {
85a1d4c9 354 if ($error = email_is_not_allowed($usernew->email)) {
355 $err["email"] = $error;
21f01485 356 }
357 }
a3f1f815 358
81d425b4 359 if (!$um->preprocess_files()) {
360 $err['imagefile'] = $um->notify;
361 }
f9903ed0 362
a3f1f815 363 if (!isadmin()) { /// Make sure that locked fields are not being edited
364 $fields = get_user_fieldnames();
365
366 foreach ($fields as $field) {
367 $configvariable = 'auth_user_'.$field.'_editlock';
368 if (!empty($CFG->$configvariable)) {
369 if ($user->$field !== $usernew->$field) {
370 $err[$field] = get_string("editlock");
371 }
372 }
373 }
d35757eb 374 }
375
f9903ed0 376 $user->email = $usernew->email;
377
378 return count($err);
379}
380
381
382?>