3 require_once("../config.php");
4 require_once("../auth/$CFG->auth/lib.php");
6 //HTTPS is potentially required in this page
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.");
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);
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!");
36 error("User already exists on authentication database.");
40 if (! ($user->id
= insert_record("user", $user)) ) {
41 error("Could not add your record to the database!");
44 if (! send_confirmation_email($user)) {
45 error("Tried to send you an email but failed!");
48 $emailconfirm = get_string("emailconfirm");
49 print_header($emailconfirm, $emailconfirm, $emailconfirm);
50 notice(get_string("emailconfirmsent", "", $user->email
), "$CFG->wwwroot/index.php");
56 $focus = "form.".array_shift($temparr = array_flip(get_object_vars($err)));
61 if (empty($user->country
) and !empty($CFG->country
)) {
62 $user->country
= $CFG->country
;
65 $newaccount = get_string("newaccount");
66 $login = get_string("login");
68 if (empty($CFG->langmenu
)) {
71 $currlang = current_language();
72 $langs = get_list_of_languages();
73 $langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
76 print_header($newaccount, $newaccount, "<a href=\"index.php\">$login</a> -> $newaccount", $focus, "", true, "<div align=\"right\">$langmenu</div>");
77 include("signup_form.html");
82 /******************************************************************************
84 *****************************************************************************/
86 function validate_form($user, &$err) {
89 if (empty($user->username
)){
90 $err->username
= get_string("missingusername");
92 $user->username
= trim(moodle_strtolower($user->username
));
93 if (record_exists("user", "username", $user->username
)){
94 $err->username
= get_string("usernameexists");
96 if (empty($CFG->extendedusernamechars
)) {
97 $string = eregi_replace("[^(-\.[:alnum:])]", "", $user->username
);
98 if (strcmp($user->username
, $string)) {
99 $err->username
= get_string("alphanumerical");
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");
112 if (empty($user->password
)) {
113 $err->password
= get_string("missingpassword");
116 if (empty($user->firstname
)) {
117 $err->firstname
= get_string("missingfirstname");
120 if (empty($user->lastname
)) {
121 $err->lastname
= get_string("missinglastname");
125 if (empty($user->email
)) {
126 $err->email
= get_string("missingemail");
128 } else if (! validate_email($user->email
)) {
129 $err->email
= get_string("invalidemail");
131 } else if (record_exists("user", "email", $user->email
)) {
132 $err->email
= get_string("emailexists")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
136 if (empty($user->email2
)) {
137 $err->email2
= get_string("missingemail");
139 } else if ($user->email2
!= $user->email
) {
140 $err->email2
= get_string("invalidemail");
144 if (empty($user->city
)) {
145 $err->city
= get_string("missingcity");
148 if (empty($user->country
)) {
149 $err->country
= get_string("missingcountry");
152 if (empty($err->email
)) {
153 if ($error = email_is_not_allowed($user->email
)) {
154 $err->email
= $error;