32c26f6502fc2d81b77c878494536e8bc71b7b53
[moodle.git] / user / edit.php
1 <?PHP // $Id$
3     require_once("../config.php");
4     require_once("$CFG->libdir/gdlib.php");
6     optional_variable($id);       // user id
7     optional_variable($course);   // course id
9     if (empty($id)) {         // See your own profile by default
10         require_login();
11         $id = $USER->id;
12     }
14     if (empty($course)) {     // See it at site level by default
15         $course = SITEID;
16     }
18     if (! $user = get_record("user", "id", $id)) {
19         error("User ID was incorrect");
20     }
22     if (! $course = get_record("course", "id", $course)) {
23         error("Course ID was incorrect");
24     }
26     if ($user->confirmed and user_not_fully_set_up($user)) {
27         // Special case which can only occur when a new account
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;
33         if (empty($USER)) {
34             error("Sessions don't seem to be working on this server!");
35         }
37     } else {
38         $newaccount  = false;
39         require_login($course->id);
40     }
42     if ($USER->id <> $user->id and !isadmin()) {
43         error("You can only edit your own information");
44     }
46     if (isguest()) {
47         error("The guest user cannot edit their profile.");
48     }
50     if (isguest($user->id)) {
51         error("Sorry, the guest user cannot be edited.");
52     }
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     }
63     
64 /// If data submitted, then process and store.
66     if ($usernew = data_submitted()) {
68         if (isset($USER->username)) {
69             check_for_restricted_user($USER->username, "$CFG->wwwroot/course/view.php?id=$course->id");
70         }
72         foreach ($usernew as $key => $data) {
73             $usernew->$key = addslashes(clean_text(stripslashes($usernew->$key), FORMAT_MOODLE));
74         }
76         $usernew->firstname = trim(strip_tags($usernew->firstname));
77         $usernew->lastname  = trim(strip_tags($usernew->lastname));
79         if (isset($usernew->username)) {
80             $usernew->username = trim(moodle_strtolower($usernew->username));
81         }
84         require_once($CFG->dirroot.'/lib/uploadlib.php');
85         $um = new upload_manager('imagefile',false,false,null,false,0,true,true);
87         if (find_form_errors($user, $usernew, $err, $um)) {
88             if (empty($err['imagefile']) && $usernew->picture = save_profile_image($user->id, $um,'users')) {
89                 set_field('user', 'picture', $usernew->picture, 'id', $user->id);  /// Note picture in DB
90             } else {
91                 if (!empty($usernew->deletepicture)) {
92                     set_field('user', 'picture', 0, 'id', $user->id);  /// Delete picture
93                     $usernew->picture = 0;
94                 }
95             }
97             $user = $usernew;
99         } else {
100             $timenow = time();
102             if (!$usernew->picture = save_profile_image($user->id,$um,'users')) {
103                 if (!empty($usernew->deletepicture)) {
104                     set_field('user', 'picture', 0, 'id', $user->id);  /// Delete picture
105                     $usernew->picture = 0;
106                 } else {
107                     $usernew->picture = $user->picture;
108                 }
109             }
111             $usernew->timemodified = time();
113             if (isadmin()) {
114                 if (!empty($usernew->newpassword)) {
115                     $usernew->password = md5($usernew->newpassword);
116                     // update external passwords
117                     if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
118                         if (function_exists('auth_user_update_password')){
119                             if (!auth_user_update_password($user->username, $usernew->newpassword)){
120                                 error('Failed to update password on external auth: ' . $user->auth .
121                                         '. See the server logs for more details.');
122                             }
123                         } else {
124                             error('Your external authentication module is misconfigued!'); 
125                         }
126                     }
127                 }
128                 // store forcepasswordchange in user's preferences
129                 if (!empty($usernew->forcepasswordchange)){
130                     set_user_preference('auth_forcepasswordchange', 1, $user->id);
131                 } else {
132                     unset_user_preference('auth_forcepasswordchange', $user->id);
133                 }
134             } else {
135                 if (isset($usernew->newpassword)) {
136                     error("You can not change the password like that");
137                 }
138             }
139             if ($usernew->url and !(substr($usernew->url, 0, 4) == "http")) {
140                 $usernew->url = "http://".$usernew->url;
141             }
143             if (update_record("user", $usernew)) {
144                 if (function_exists("auth_user_update")){ 
145                     auth_user_update($user, $usernew);
146                 }
147                 add_to_log($course->id, "user", "update", "view.php?id=$user->id&course=$course->id", "");
149                 if ($user->id == $USER->id) {
150                     // Copy data into $USER session variable
151                     $usernew = (array)$usernew;
152                     foreach ($usernew as $variable => $value) {
153                         $USER->$variable = stripslashes($value);
154                     }
155                     if (isset($USER->newadminuser)) {
156                         unset($USER->newadminuser);
157                         redirect("$CFG->wwwroot/", get_string("changessaved"));
158                     }
159                     redirect("$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id", get_string("changessaved"));
160                 } else {
161                     redirect("$CFG->wwwroot/$CFG->admin/user.php", get_string("changessaved"));
162                 }
163             } else {
164                 error("Could not update the user record ($user->id)");
165             }
166         }
167     }
169 /// Otherwise fill and print the form.
171     $streditmyprofile = get_string("editmyprofile");
172     $strparticipants = get_string("participants");
173     $strnewuser = get_string("newuser");
175     if (($user->firstname and $user->lastname) or $newaccount) {
176         if ($newaccount) {
177             $userfullname = $strnewuser;
178         } else {
179             $userfullname = fullname($user, isteacher($course->id));
180         }
181         if ($course->category) {
182             print_header("$course->shortname: $streditmyprofile", "$course->fullname: $streditmyprofile",
183                         "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
184                         -> <a href=\"index.php?id=$course->id\">$strparticipants</a>
185                         -> <a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
186                         -> $streditmyprofile", "");
187         } else {
188             if (isset($USER->newadminuser)) {
189                 print_header();
190             } else {
191                 print_header("$course->shortname: $streditmyprofile", "$course->fullname",
192                              "<a href=\"view.php?id=$user->id&amp;course=$course->id\">$userfullname</a>
193                               -> $streditmyprofile", "");
194             }
195         }
196     } else {
197         $userfullname = $strnewuser;
198         $straddnewuser = get_string("addnewuser");
200         $stradministration = get_string("administration");
201         print_header("$course->shortname: $streditmyprofile", "$course->fullname",
202                      "<a href=\"$CFG->wwwroot/$CFG->admin/\">$stradministration</a> -> ".
203                      "<a href=\"$CFG->wwwroot/$CFG->admin/users.php\">$strusers</a> -> $straddnewuser", "");
204     }
206     $teacher = strtolower($course->teacher);
207     if (!isadmin()) {
208         $teacheronly = "(".get_string("teacheronly", "", $teacher).")";
209     } else {
210         $teacheronly = "";
211     }
213     print_heading( get_string("userprofilefor", "", "$userfullname") );
215     if (isset($USER->newadminuser)) {
216         print_simple_box(get_string("configintroadmin"), "center", "50%");
217         echo "<br />";
218     }
220     print_simple_box_start("center", "", "$THEME->cellheading");
222     if (!empty($err)) {
223         echo "<center>";
224         notify(get_string("someerrorswerefound"));
225         echo "</center>";
226     }
228     include("edit.html");
230     if (!isadmin()) {      /// Lock all the locked fields using Javascript
231         $fields = get_user_fieldnames();
233         echo '<script type="text/javascript">'."\n";
234         echo '<!--'."\n";
236         foreach ($fields as $field) {
237             $configvariable = 'auth_user_'.$field.'_editlock';
238             if (!empty($CFG->$configvariable)) {
239                 echo "eval('document.form.$field.disabled=true');\n";
240             }
241         }
243         echo '-->'."\n";
244         echo '</script>'."\n";
245     }
247     print_simple_box_end();
249     if (!isset($USER->newadminuser)) {
250         print_footer($course);
251     }
253     exit;
257 /// FUNCTIONS ////////////////////
259 function find_form_errors(&$user, &$usernew, &$err, &$um) {
260     global $CFG;
262     if (isadmin()) {
263         if (empty($usernew->username)) {
264             $err["username"] = get_string("missingusername");
266         } else if (record_exists("user", "username", $usernew->username) and $user->username == "changeme") {
267             $err["username"] = get_string("usernameexists");
269         } else {
270             if (empty($CFG->extendedusernamechars)) {
271                 $string = eregi_replace("[^(-\.[:alnum:])]", "", $usernew->username);
272                 if (strcmp($usernew->username, $string)) {
273                     $err["username"] = get_string("alphanumerical");
274                 }
275             }
276         }
278         if (empty($usernew->newpassword) and empty($user->password) and is_internal_auth() )
279             $err["newpassword"] = get_string("missingpassword");
281         if (($usernew->newpassword == "admin") or ($user->password == md5("admin") and empty($usernew->newpassword)) ) {
282             $err["newpassword"] = get_string("unsafepassword");
283         }
284     }
286     if (empty($usernew->email))
287         $err["email"] = get_string("missingemail");
289     if (empty($usernew->description) and !isadmin())
290         $err["description"] = get_string("missingdescription");
292     if (empty($usernew->city))
293         $err["city"] = get_string("missingcity");
295     if (empty($usernew->firstname))
296         $err["firstname"] = get_string("missingfirstname");
298     if (empty($usernew->lastname))
299         $err["lastname"] = get_string("missinglastname");
301     if (empty($usernew->country))
302         $err["country"] = get_string("missingcountry");
304     if (! validate_email($usernew->email)) {
305         $err["email"] = get_string("invalidemail");
307     } else if ($otheruser = get_record("user", "email", $usernew->email)) {
308         if ($otheruser->id <> $user->id) {
309             $err["email"] = get_string("emailexists");
310         }
311     }
313     if (empty($err["email"]) and !isadmin()) {
314         if ($error = email_is_not_allowed($usernew->email)) {
315             $err["email"] = $error;
316         }
317     }
319     if (!$um->preprocess_files()) {
320         $err['imagefile'] = $um->notify;
321     }
323     if (!isadmin()) {      /// Make sure that locked fields are not being edited
324         $fields = get_user_fieldnames();
326         foreach ($fields as $field) {
327             $configvariable = 'auth_user_'.$field.'_editlock';
328             if (!empty($CFG->$configvariable)) {
329                 if ($user->$field !== $usernew->$field) {
330                     $err[$field] = get_string("editlock");
331                 }
332             }
333         }
334     }
336     $user->email = $usernew->email;
338     return count($err);
342 ?>