MDL-55873 login: change back username fields to use PARAM_RAW
[moodle.git] / login / signup_form.php
CommitLineData
6c3ef410 1<?php
296c2a79 2
c30949a9
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * User sign-up form.
20 *
21 * @package core
22 * @subpackage auth
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
bfebaf64 28
296c2a79 29require_once($CFG->libdir.'/formslib.php');
831d450e 30require_once($CFG->dirroot.'/user/profile/lib.php');
0859a08d 31require_once($CFG->dirroot . '/user/editlib.php');
296c2a79 32
dd0feda5 33class login_signup_form extends moodleform {
6bc1e5d5 34 function definition() {
35 global $USER, $CFG;
e8a34ad4 36
c30949a9 37 $mform = $this->_form;
e8a34ad4 38
807cf3d0 39 $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
e8a34ad4 40
41
d4787515 42 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
90d8bf59 43 $mform->setType('username', PARAM_RAW);
bd7a0fd3 44 $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
e8a34ad4 45
3bf11ca4
DM
46 if (!empty($CFG->passwordpolicy)){
47 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 48 }
54bb33eb 49 $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
ac9768fc 50 $mform->setType('password', core_user::get_property_type('password'));
bd7a0fd3 51 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
e8a34ad4 52
807cf3d0 53 $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
e8a34ad4 54
d4787515 55 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
ac9768fc 56 $mform->setType('email', core_user::get_property_type('email'));
bd7a0fd3 57 $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
e8a34ad4 58
d4787515 59 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
ac9768fc 60 $mform->setType('email2', core_user::get_property_type('email'));
bd7a0fd3 61 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
e8a34ad4 62
0859a08d
AA
63 $namefields = useredit_get_required_name_fields();
64 foreach ($namefields as $field) {
65 $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
ac9768fc 66 $mform->setType($field, core_user::get_property_type('firstname'));
0859a08d
AA
67 $stringid = 'missing' . $field;
68 if (!get_string_manager()->string_exists($stringid, 'moodle')) {
69 $stringid = 'required';
70 }
bd7a0fd3 71 $mform->addRule($field, get_string($stringid), 'required', null, 'client');
baacc970 72 }
73
c5c0d2ff 74 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
ac9768fc 75 $mform->setType('city', core_user::get_property_type('city'));
fa7f750c
PS
76 if (!empty($CFG->defaultcity)) {
77 $mform->setDefault('city', $CFG->defaultcity);
78 }
e8a34ad4 79
0aa759b0 80 $country = get_string_manager()->get_list_of_countries();
6bc1e5d5 81 $default_country[''] = get_string('selectacountry');
82 $country = array_merge($default_country, $country);
83 $mform->addElement('select', 'country', get_string('country'), $country);
cbc54f2c 84
85 if( !empty($CFG->country) ){
86 $mform->setDefault('country', $CFG->country);
87 }else{
88 $mform->setDefault('country', '');
89 }
f85509b5 90
d83318f1
ME
91 profile_signup_fields($mform);
92
c30949a9 93 if ($this->signup_captcha_enabled()) {
3256b358 94 $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'), array('https' => $CFG->loginhttps));
8db551df 95 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5e91f96c 96 $mform->closeHeaderBefore('recaptcha_element');
5d6308d4 97 }
e8a34ad4 98
18d8136e 99 if (!empty($CFG->sitepolicy)) {
807cf3d0
FM
100 $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
101 $mform->setExpanded('policyagreement');
18d8136e 102 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
103 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
bd7a0fd3 104 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'client');
18d8136e 105 }
106
5d2b6c1c 107 // buttons
b6d15dde 108 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 109
6bc1e5d5 110 }
296c2a79 111
6bc1e5d5 112 function definition_after_data(){
c30949a9 113 $mform = $this->_form;
6bc1e5d5 114 $mform->applyFilter('username', 'trim');
e9c27d1e
DM
115
116 // Trim required name fields.
117 foreach (useredit_get_required_name_fields() as $field) {
118 $mform->applyFilter($field, 'trim');
119 }
6bc1e5d5 120 }
e8a34ad4 121
a78890d5 122 function validation($data, $files) {
32648682 123 global $CFG, $DB;
a78890d5 124 $errors = parent::validation($data, $files);
e8a34ad4 125
3fe6b721 126 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 127
32648682 128 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
6bc1e5d5 129 $errors['username'] = get_string('usernameexists');
130 } else {
07ed083e 131 //check allowed characters
2f1e464a 132 if ($data['username'] !== core_text::strtolower($data['username'])) {
07ed083e 133 $errors['username'] = get_string('usernamelowercase');
6b8ad965 134 } else {
ac9768fc 135 if ($data['username'] !== core_user::clean_field($data['username'], 'username')) {
79604225 136 $errors['username'] = get_string('invalidusername');
6bc1e5d5 137 }
6b8ad965 138
6bc1e5d5 139 }
140 }
141
142 //check if user exists in external db
143 //TODO: maybe we should check all enabled plugins instead
152bc5a2 144 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 145 $errors['username'] = get_string('usernameexists');
146 }
147
148
149 if (! validate_email($data['email'])) {
150 $errors['email'] = get_string('invalidemail');
151
32648682 152 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
6bc1e5d5 153 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
154 }
e8a34ad4 155 if (empty($data['email2'])) {
296c2a79 156 $errors['email2'] = get_string('missingemail');
e8a34ad4 157
158 } else if ($data['email2'] != $data['email']) {
296c2a79 159 $errors['email2'] = get_string('invalidemail');
e8a34ad4 160 }
6bc1e5d5 161 if (!isset($errors['email'])) {
162 if ($err = email_is_not_allowed($data['email'])) {
163 $errors['email'] = $err;
164 }
e8a34ad4 165
6bc1e5d5 166 }
e8a34ad4 167
a78890d5 168 $errmsg = '';
83022298 169 if (!check_password_policy($data['password'], $errmsg)) {
170 $errors['password'] = $errmsg;
171 }
f85509b5 172
c30949a9 173 if ($this->signup_captcha_enabled()) {
f85509b5 174 $recaptcha_element = $this->_form->getElement('recaptcha_element');
175 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
176 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
177 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
178 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
179 $errors['recaptcha'] = $result;
180 }
181 } else {
182 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 183 }
184 }
c862e2f4 185 // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
9247265b 186 $dataobject = (object)$data;
c862e2f4 187 $dataobject->id = 0;
9247265b 188 $errors += profile_validation($dataobject, $files);
e8a34ad4 189
f85509b5 190 return $errors;
191
c30949a9 192 }
f85509b5 193
c30949a9
PS
194 /**
195 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
196 * @return bool
197 */
198 function signup_captcha_enabled() {
199 global $CFG;
7f2033ea
RK
200 $authplugin = get_auth_plugin($CFG->registerauth);
201 return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && $authplugin->is_captcha_enabled();
6bc1e5d5 202 }
c30949a9 203
e8a34ad4 204}