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