gradebook MDL-19070 Added a guard to protect against a potential error
[moodle.git] / login / signup_form.php
CommitLineData
6c3ef410 1<?php
296c2a79 2
bfebaf64
MD
3if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
5}
6
296c2a79 7require_once($CFG->libdir.'/formslib.php');
831d450e 8require_once($CFG->dirroot.'/user/profile/lib.php');
296c2a79 9
dd0feda5 10class login_signup_form extends moodleform {
6bc1e5d5 11 function definition() {
12 global $USER, $CFG;
e8a34ad4 13
6bc1e5d5 14 $mform =& $this->_form;
e8a34ad4 15
6bc1e5d5 16 $mform->addElement('header', '', get_string('createuserandpass'), '');
e8a34ad4 17
18
d4787515 19 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
6bc1e5d5 20 $mform->setType('username', PARAM_NOTAGS);
18d8136e 21 $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
e8a34ad4 22
3bf11ca4
DM
23 if (!empty($CFG->passwordpolicy)){
24 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 25 }
54bb33eb 26 $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
6bc1e5d5 27 $mform->setType('password', PARAM_RAW);
18d8136e 28 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
e8a34ad4 29
6bc1e5d5 30 $mform->addElement('header', '', get_string('supplyinfo'),'');
e8a34ad4 31
d4787515 32 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
6bc1e5d5 33 $mform->setType('email', PARAM_NOTAGS);
18d8136e 34 $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 35
d4787515 36 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
6bc1e5d5 37 $mform->setType('email2', PARAM_NOTAGS);
18d8136e 38 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 39
06d7c6d6 40 $nameordercheck = new object();
baacc970 41 $nameordercheck->firstname = 'a';
42 $nameordercheck->lastname = 'b';
43 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
44 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
45 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
46 } else {
47 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
48 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
49 }
50
6bc1e5d5 51 $mform->setType('firstname', PARAM_TEXT);
18d8136e 52 $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
e8a34ad4 53
6bc1e5d5 54 $mform->setType('lastname', PARAM_TEXT);
18d8136e 55 $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
e8a34ad4 56
c5c0d2ff 57 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
6bc1e5d5 58 $mform->setType('city', PARAM_TEXT);
18d8136e 59 $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
e8a34ad4 60
0aa759b0 61 $country = get_string_manager()->get_list_of_countries();
6bc1e5d5 62 $default_country[''] = get_string('selectacountry');
63 $country = array_merge($default_country, $country);
64 $mform->addElement('select', 'country', get_string('country'), $country);
18d8136e 65 $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
cbc54f2c 66
67 if( !empty($CFG->country) ){
68 $mform->setDefault('country', $CFG->country);
69 }else{
70 $mform->setDefault('country', '');
71 }
f85509b5 72
5d6308d4 73 if (signup_captcha_enabled()) {
83947a36 74 $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
8db551df 75 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5d6308d4 76 }
e8a34ad4 77
831d450e 78 profile_signup_fields($mform);
79
18d8136e 80 if (!empty($CFG->sitepolicy)) {
81 $mform->addElement('header', '', get_string('policyagreement'), '');
82 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
83 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
84 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
85 }
86
5d2b6c1c 87 // buttons
b6d15dde 88 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 89
6bc1e5d5 90 }
296c2a79 91
6bc1e5d5 92 function definition_after_data(){
93 $mform =& $this->_form;
6bc1e5d5 94 $mform->applyFilter('username', 'trim');
95 }
e8a34ad4 96
a78890d5 97 function validation($data, $files) {
32648682 98 global $CFG, $DB;
a78890d5 99 $errors = parent::validation($data, $files);
e8a34ad4 100
3fe6b721 101 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 102
32648682 103 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
6bc1e5d5 104 $errors['username'] = get_string('usernameexists');
105 } else {
07ed083e
RW
106 //check allowed characters
107 if ($data['username'] !== moodle_strtolower($data['username'])) {
108 $errors['username'] = get_string('usernamelowercase');
6b8ad965
PS
109 } else {
110 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
79604225 111 $errors['username'] = get_string('invalidusername');
6bc1e5d5 112 }
6b8ad965 113
6bc1e5d5 114 }
115 }
116
117 //check if user exists in external db
118 //TODO: maybe we should check all enabled plugins instead
152bc5a2 119 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 120 $errors['username'] = get_string('usernameexists');
121 }
122
123
124 if (! validate_email($data['email'])) {
125 $errors['email'] = get_string('invalidemail');
126
32648682 127 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
6bc1e5d5 128 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
129 }
e8a34ad4 130 if (empty($data['email2'])) {
296c2a79 131 $errors['email2'] = get_string('missingemail');
e8a34ad4 132
133 } else if ($data['email2'] != $data['email']) {
296c2a79 134 $errors['email2'] = get_string('invalidemail');
e8a34ad4 135 }
6bc1e5d5 136 if (!isset($errors['email'])) {
137 if ($err = email_is_not_allowed($data['email'])) {
138 $errors['email'] = $err;
139 }
e8a34ad4 140
6bc1e5d5 141 }
e8a34ad4 142
a78890d5 143 $errmsg = '';
83022298 144 if (!check_password_policy($data['password'], $errmsg)) {
145 $errors['password'] = $errmsg;
146 }
f85509b5 147
5d6308d4 148 if (signup_captcha_enabled()) {
f85509b5 149 $recaptcha_element = $this->_form->getElement('recaptcha_element');
150 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
151 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
152 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
153 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
154 $errors['recaptcha'] = $result;
155 }
156 } else {
157 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 158 }
159 }
e8a34ad4 160
f85509b5 161 return $errors;
162
163
6bc1e5d5 164 }
e8a34ad4 165}