weekly release 3.2dev
[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');
525ef9c8 58 $mform->setForceLtr('email');
e8a34ad4 59
d4787515 60 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
ac9768fc 61 $mform->setType('email2', core_user::get_property_type('email'));
bd7a0fd3 62 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
525ef9c8 63 $mform->setForceLtr('email2');
e8a34ad4 64
0859a08d
AA
65 $namefields = useredit_get_required_name_fields();
66 foreach ($namefields as $field) {
67 $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
ac9768fc 68 $mform->setType($field, core_user::get_property_type('firstname'));
0859a08d
AA
69 $stringid = 'missing' . $field;
70 if (!get_string_manager()->string_exists($stringid, 'moodle')) {
71 $stringid = 'required';
72 }
bd7a0fd3 73 $mform->addRule($field, get_string($stringid), 'required', null, 'client');
baacc970 74 }
75
c5c0d2ff 76 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
ac9768fc 77 $mform->setType('city', core_user::get_property_type('city'));
fa7f750c
PS
78 if (!empty($CFG->defaultcity)) {
79 $mform->setDefault('city', $CFG->defaultcity);
80 }
e8a34ad4 81
0aa759b0 82 $country = get_string_manager()->get_list_of_countries();
6bc1e5d5 83 $default_country[''] = get_string('selectacountry');
84 $country = array_merge($default_country, $country);
85 $mform->addElement('select', 'country', get_string('country'), $country);
cbc54f2c 86
87 if( !empty($CFG->country) ){
88 $mform->setDefault('country', $CFG->country);
89 }else{
90 $mform->setDefault('country', '');
91 }
f85509b5 92
d83318f1
ME
93 profile_signup_fields($mform);
94
c30949a9 95 if ($this->signup_captcha_enabled()) {
3256b358 96 $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'), array('https' => $CFG->loginhttps));
8db551df 97 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5e91f96c 98 $mform->closeHeaderBefore('recaptcha_element');
5d6308d4 99 }
e8a34ad4 100
18d8136e 101 if (!empty($CFG->sitepolicy)) {
807cf3d0
FM
102 $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
103 $mform->setExpanded('policyagreement');
18d8136e 104 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
105 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
bd7a0fd3 106 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'client');
18d8136e 107 }
108
5d2b6c1c 109 // buttons
b6d15dde 110 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 111
6bc1e5d5 112 }
296c2a79 113
6bc1e5d5 114 function definition_after_data(){
c30949a9 115 $mform = $this->_form;
6bc1e5d5 116 $mform->applyFilter('username', 'trim');
e9c27d1e
DM
117
118 // Trim required name fields.
119 foreach (useredit_get_required_name_fields() as $field) {
120 $mform->applyFilter($field, 'trim');
121 }
6bc1e5d5 122 }
e8a34ad4 123
a78890d5 124 function validation($data, $files) {
32648682 125 global $CFG, $DB;
a78890d5 126 $errors = parent::validation($data, $files);
e8a34ad4 127
3fe6b721 128 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 129
32648682 130 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
6bc1e5d5 131 $errors['username'] = get_string('usernameexists');
132 } else {
07ed083e 133 //check allowed characters
2f1e464a 134 if ($data['username'] !== core_text::strtolower($data['username'])) {
07ed083e 135 $errors['username'] = get_string('usernamelowercase');
6b8ad965 136 } else {
ac9768fc 137 if ($data['username'] !== core_user::clean_field($data['username'], 'username')) {
79604225 138 $errors['username'] = get_string('invalidusername');
6bc1e5d5 139 }
6b8ad965 140
6bc1e5d5 141 }
142 }
143
144 //check if user exists in external db
145 //TODO: maybe we should check all enabled plugins instead
152bc5a2 146 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 147 $errors['username'] = get_string('usernameexists');
148 }
149
150
151 if (! validate_email($data['email'])) {
152 $errors['email'] = get_string('invalidemail');
153
32648682 154 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
6bc1e5d5 155 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
156 }
e8a34ad4 157 if (empty($data['email2'])) {
296c2a79 158 $errors['email2'] = get_string('missingemail');
e8a34ad4 159
160 } else if ($data['email2'] != $data['email']) {
296c2a79 161 $errors['email2'] = get_string('invalidemail');
e8a34ad4 162 }
6bc1e5d5 163 if (!isset($errors['email'])) {
164 if ($err = email_is_not_allowed($data['email'])) {
165 $errors['email'] = $err;
166 }
e8a34ad4 167
6bc1e5d5 168 }
e8a34ad4 169
a78890d5 170 $errmsg = '';
83022298 171 if (!check_password_policy($data['password'], $errmsg)) {
172 $errors['password'] = $errmsg;
173 }
f85509b5 174
c30949a9 175 if ($this->signup_captcha_enabled()) {
f85509b5 176 $recaptcha_element = $this->_form->getElement('recaptcha_element');
177 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
178 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
179 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
180 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
181 $errors['recaptcha'] = $result;
182 }
183 } else {
184 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 185 }
186 }
c862e2f4 187 // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
9247265b 188 $dataobject = (object)$data;
c862e2f4 189 $dataobject->id = 0;
9247265b 190 $errors += profile_validation($dataobject, $files);
e8a34ad4 191
f85509b5 192 return $errors;
193
c30949a9 194 }
f85509b5 195
c30949a9
PS
196 /**
197 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
198 * @return bool
199 */
200 function signup_captcha_enabled() {
201 global $CFG;
7f2033ea
RK
202 $authplugin = get_auth_plugin($CFG->registerauth);
203 return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && $authplugin->is_captcha_enabled();
6bc1e5d5 204 }
c30949a9 205
e8a34ad4 206}