MDL-9053 - detect + fix broken $CFG->auth setting from the login page.
[moodle.git] / login / signup_form.php
CommitLineData
296c2a79 1<?php // $Id$
2
3require_once($CFG->libdir.'/formslib.php');
4
dd0feda5 5class login_signup_form extends moodleform {
6bc1e5d5 6 function definition() {
7 global $USER, $CFG;
e8a34ad4 8
6bc1e5d5 9 $mform =& $this->_form;
e8a34ad4 10
6bc1e5d5 11 $mform->addElement('header', '', get_string('createuserandpass'), '');
e8a34ad4 12
13
6bc1e5d5 14 $mform->addElement('text', 'username', get_string('username'), 'size="12"');
15 $mform->setType('username', PARAM_NOTAGS);
16 $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
e8a34ad4 17
6bc1e5d5 18 $mform->addElement('password', 'password', get_string('password'), 'size="12"');
19 $mform->setType('password', PARAM_RAW);
20 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
e8a34ad4 21
6bc1e5d5 22 $mform->addElement('header', '', get_string('supplyinfo'),'');
e8a34ad4 23
6bc1e5d5 24 $mform->addElement('text', 'email', get_string('email'), 'size="25"');
25 $mform->setType('email', PARAM_NOTAGS);
26 $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
e8a34ad4 27
6bc1e5d5 28 $mform->addElement('text', 'email2', get_string('emailagain'), 'size="25"');
29 $mform->setType('email2', PARAM_NOTAGS);
30 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
e8a34ad4 31
6bc1e5d5 32 $mform->addElement('text', 'firstname', get_string('firstname'), 'size="25"');
33 $mform->setType('firstname', PARAM_TEXT);
34 $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'client');
e8a34ad4 35
6bc1e5d5 36 $mform->addElement('text', 'lastname', get_string('lastname'), 'size="25"');
37 $mform->setType('lastname', PARAM_TEXT);
38 $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'client');
e8a34ad4 39
6bc1e5d5 40 $mform->addElement('text', 'city', get_string('city'), 'size="20"');
41 $mform->setType('city', PARAM_TEXT);
42 $mform->addRule('city', get_string('missingcity'), 'required', null, 'client');
e8a34ad4 43
6bc1e5d5 44 $country = get_list_of_countries();
45 $default_country[''] = get_string('selectacountry');
46 $country = array_merge($default_country, $country);
47 $mform->addElement('select', 'country', get_string('country'), $country);
48 $mform->addRule('country', get_string('missingcountry'), 'required', null, 'client');
296c2a79 49 $mform->setDefault('country', '');
e8a34ad4 50
5d2b6c1c 51 // buttons
b6d15dde 52 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 53
6bc1e5d5 54 }
296c2a79 55
6bc1e5d5 56 function definition_after_data(){
57 $mform =& $this->_form;
296c2a79 58
6bc1e5d5 59 $mform->applyFilter('username', 'moodle_strtolower');
60 $mform->applyFilter('username', 'trim');
61 }
e8a34ad4 62
6bc1e5d5 63 function validation($data) {
64 global $CFG;
65 $errors = array();
e8a34ad4 66
3fe6b721 67 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 68
6bc1e5d5 69 if (record_exists('user', 'username', $data['username'], 'mnethostid', $CFG->mnet_localhost_id)) {
70 $errors['username'] = get_string('usernameexists');
71 } else {
72 if (empty($CFG->extendedusernamechars)) {
73 $string = eregi_replace("[^(-\.[:alnum:])]", '', $data['username']);
74 if (strcmp($data['username'], $string)) {
75 $errors['username'] = get_string('alphanumerical');
76 }
77 }
78 }
79
80 //check if user exists in external db
81 //TODO: maybe we should check all enabled plugins instead
82 if ($authplugin->user_exists($user->username)) {
83 $errors['username'] = get_string('usernameexists');
84 }
85
86
87 if (! validate_email($data['email'])) {
88 $errors['email'] = get_string('invalidemail');
89
90 } else if (record_exists('user', 'email', $data['email'])) {
91 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
92 }
e8a34ad4 93 if (empty($data['email2'])) {
296c2a79 94 $errors['email2'] = get_string('missingemail');
e8a34ad4 95
96 } else if ($data['email2'] != $data['email']) {
296c2a79 97 $errors['email2'] = get_string('invalidemail');
e8a34ad4 98 }
6bc1e5d5 99 if (!isset($errors['email'])) {
100 if ($err = email_is_not_allowed($data['email'])) {
101 $errors['email'] = $err;
102 }
e8a34ad4 103
6bc1e5d5 104 }
e8a34ad4 105
106
6bc1e5d5 107 if (0 == count($errors)){
108 return true;
109 } else {
110 return $errors;
111 }
e8a34ad4 112
113
6bc1e5d5 114 }
e8a34ad4 115}
116
117?>