fdb73cca5f61a653b07121f40b3dac06f83a3ff5
[moodle.git] / login / signup.php
1 <?php // $Id$
3     require_once("../config.php");
4     require_once("../auth/$CFG->auth/lib.php");
6     //HTTPS is potentially required in this page
7     httpsrequired();
9     if ($CFG->auth != 'email' and (empty($CFG->auth_user_create) or !(function_exists('auth_user_create'))) ) {
10         error("Sorry, you may not use this page.");
11     }
13     if ($user = data_submitted()) {
15         $user->firstname = strip_tags($user->firstname);
16         $user->lastname = strip_tags($user->lastname);
17         $user->email = strip_tags($user->email);
19         validate_form($user, $err);
20         $user->username= trim(moodle_strtolower($user->username));
22         if (count((array)$err) == 0) {
23             $plainpass = $user->password;
24             $user->password = hash_internal_user_password($plainpass);
25             $user->confirmed = 0;
26             $user->lang = current_language();
27             $user->firstaccess = time();
28             $user->secret = random_string(15);
29             $user->auth = $CFG->auth;
30             if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){
31                 if (! auth_user_exists($user->username)) {
32                     if (! auth_user_create($user,$plainpass)) {
33                         error("Could not add user to authentication module!");
34                     }
35                 } else {
36                     error("User already exists on authentication database.");
37                 }
38             }
40             if (! ($user->id = insert_record("user", $user)) ) {
41                 error("Could not add your record to the database!");
42             }
44             if (! send_confirmation_email($user)) {
45                 error("Tried to send you an email but failed!");
46             }
47     
48             $emailconfirm = get_string("emailconfirm");
49             print_header($emailconfirm, $emailconfirm, $emailconfirm);
50             notice(get_string("emailconfirmsent", "", $user->email), "$CFG->wwwroot/index.php");
51             exit;
52         }
53     }
55     if (!empty($err)) {
56         $focus = "form.".array_shift($temparr = array_flip(get_object_vars($err)));
57     } else {
58         $focus = "";
59     }
61     if (empty($user->country) and !empty($CFG->country)) {
62         $user->country = $CFG->country;
63     }
65     $newaccount = get_string("newaccount");
66     $login = get_string("login");
68     if (empty($CFG->langmenu)) {
69         $langmenu = "";
70     } else {
71         $currlang = current_language();
72         $langs    = get_list_of_languages();
73         $langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
74     }
76     print_header($newaccount, $newaccount, "<a href=\"index.php\">$login</a> -> $newaccount", $focus, "", true, "<div align=\"right\">$langmenu</div>");
77     include("signup_form.html");
78     print_footer();
82 /******************************************************************************
83  * FUNCTIONS
84  *****************************************************************************/
86 function validate_form($user, &$err) {
87     global $CFG;
89     if (empty($user->username)){
90         $err->username = get_string("missingusername");
91     } else{
92         $user->username = trim(moodle_strtolower($user->username));
93         if (record_exists("user", "username", $user->username)){
94             $err->username = get_string("usernameexists");
95         } else {
96             if (empty($CFG->extendedusernamechars)) {
97                 $string = eregi_replace("[^(-\.[:alnum:])]", "", $user->username);
98                 if (strcmp($user->username, $string)) {
99                     $err->username = get_string("alphanumerical");
100                 }
101             }
102         }
103     }
105     if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){
106         if (auth_user_exists($user->username)) {
107             $err->username = get_string("usernameexists");
108         }
109     }         
112     if (empty($user->password)) {
113         $err->password = get_string("missingpassword");
114     }
116     if (empty($user->firstname)) {
117         $err->firstname = get_string("missingfirstname");
118     }
119         
120     if (empty($user->lastname)) {
121         $err->lastname = get_string("missinglastname");
122     }
123         
125     if (empty($user->email)) {
126         $err->email = get_string("missingemail");
127         
128     } else if (! validate_email($user->email)) {
129         $err->email = get_string("invalidemail");
130     
131     } else if (record_exists("user", "email", $user->email)) {
132         $err->email = get_string("emailexists")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
133     }
134     
136     if (empty($user->email2)) {
137         $err->email2 = get_string("missingemail");
139     } else if ($user->email2 != $user->email) {
140         $err->email2 = get_string("invalidemail");
141     }
144     if (empty($user->city)) {
145         $err->city = get_string("missingcity");
146     }
148     if (empty($user->country)) {
149         $err->country = get_string("missingcountry");
150     }
152     if (empty($err->email)) {
153         if ($error = email_is_not_allowed($user->email)) {
154             $err->email = $error;
155         }
156     }
158     return;
162 ?>