file index.html was added on branch MOODLE_15_STABLE on 2005-07-15 19:46:14 +0000
[moodle.git] / login / signup.php
blob58a238d7bea7a3d52742f3844b0c349b4501d4eb
1 <?php // $Id$
3 require_once("../config.php");
4 require_once("../auth/$CFG->auth/lib.php");
6 if ($CFG->auth != 'email' and (empty($CFG->auth_user_create) or !(function_exists('auth_user_create'))) ) {
7 error("Sorry, you may not use this page.");
10 if ($user = data_submitted()) {
12 $user->firstname = strip_tags($user->firstname);
13 $user->lastname = strip_tags($user->lastname);
14 $user->email = strip_tags($user->email);
16 validate_form($user, $err);
17 $user->username= trim(moodle_strtolower($user->username));
19 if (count((array)$err) == 0) {
20 $plainpass = $user->password;
21 $user->password = md5($user->password);
22 $user->confirmed = 0;
23 $user->lang = current_language();
24 $user->firstaccess = time();
25 $user->secret = random_string(15);
26 $user->auth = $CFG->auth;
27 if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){
28 if (! auth_user_exists($user->username)) {
29 if (! auth_user_create($user,$plainpass)) {
30 error("Could not add user to authentication module!");
32 } else {
33 error("User already exists on authentication database.");
37 if (! ($user->id = insert_record("user", $user)) ) {
38 error("Could not add your record to the database!");
41 if (! send_confirmation_email($user)) {
42 error("Tried to send you an email but failed!");
45 $emailconfirm = get_string("emailconfirm");
46 print_header($emailconfirm, $emailconfirm, $emailconfirm);
47 notice(get_string("emailconfirmsent", "", $user->email), "$CFG->wwwroot/");
48 exit;
52 if (!empty($err)) {
53 $focus = "form.".array_shift(array_flip(get_object_vars($err)));
54 } else {
55 $focus = "";
58 if (empty($user->country) and !empty($CFG->country)) {
59 $user->country = $CFG->country;
62 $newaccount = get_string("newaccount");
63 $login = get_string("login");
65 if (empty($CFG->langmenu)) {
66 $langmenu = "";
67 } else {
68 $currlang = current_language();
69 $langs = get_list_of_languages();
70 $langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
73 print_header($newaccount, $newaccount, "<a href=\"index.php\">$login</a> -> $newaccount", $focus, "", true, "<div align=\"right\">$langmenu</div>");
74 include("signup_form.html");
75 print_footer();
79 /******************************************************************************
80 * FUNCTIONS
81 *****************************************************************************/
83 function validate_form($user, &$err) {
84 global $CFG;
86 if (empty($user->username)){
87 $err->username = get_string("missingusername");
88 } else{
89 $user->username = trim(moodle_strtolower($user->username));
90 if (record_exists("user", "username", $user->username)){
91 $err->username = get_string("usernameexists");
92 } else {
93 if (empty($CFG->extendedusernamechars)) {
94 $string = eregi_replace("[^(-\.[:alnum:])]", "", $user->username);
95 if (strcmp($user->username, $string)) {
96 $err->username = get_string("alphanumerical");
102 if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){
103 if (auth_user_exists($user->username)) {
104 $err->username = get_string("usernameexists");
109 if (empty($user->password)) {
110 $err->password = get_string("missingpassword");
113 if (empty($user->firstname)) {
114 $err->firstname = get_string("missingfirstname");
117 if (empty($user->lastname)) {
118 $err->lastname = get_string("missinglastname");
122 if (empty($user->email)) {
123 $err->email = get_string("missingemail");
125 } else if (! validate_email($user->email)) {
126 $err->email = get_string("invalidemail");
128 } else if (record_exists("user", "email", $user->email)) {
129 $err->email = get_string("emailexists")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
133 if (empty($user->email2)) {
134 $err->email2 = get_string("missingemail");
136 } else if ($user->email2 != $user->email) {
137 $err->email2 = get_string("invalidemail");
141 if (empty($user->city)) {
142 $err->city = get_string("missingcity");
145 if (empty($user->country)) {
146 $err->country = get_string("missingcountry");
149 if (empty($err->email)) {
150 if ($error = email_is_not_allowed($user->email)) {
151 $err->email = $error;
155 return;