MDL-25979 signup: Usability message for recaptcha label form
[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"');
6bc1e5d5 43 $mform->setType('username', PARAM_NOTAGS);
18d8136e 44 $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
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"');
6bc1e5d5 50 $mform->setType('password', PARAM_RAW);
18d8136e 51 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
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"');
6bc1e5d5 56 $mform->setType('email', PARAM_NOTAGS);
18d8136e 57 $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 58
d4787515 59 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
6bc1e5d5 60 $mform->setType('email2', PARAM_NOTAGS);
18d8136e 61 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
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"');
66 $mform->setType($field, PARAM_TEXT);
67 $stringid = 'missing' . $field;
68 if (!get_string_manager()->string_exists($stringid, 'moodle')) {
69 $stringid = 'required';
70 }
71 $mform->addRule($field, get_string($stringid), 'required', null, 'server');
baacc970 72 }
73
c5c0d2ff 74 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
6bc1e5d5 75 $mform->setType('city', PARAM_TEXT);
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
c30949a9 91 if ($this->signup_captcha_enabled()) {
3256b358 92 $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'), array('https' => $CFG->loginhttps));
8db551df 93 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5d6308d4 94 }
e8a34ad4 95
831d450e 96 profile_signup_fields($mform);
97
18d8136e 98 if (!empty($CFG->sitepolicy)) {
807cf3d0
FM
99 $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
100 $mform->setExpanded('policyagreement');
18d8136e 101 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
102 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
103 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
104 }
105
5d2b6c1c 106 // buttons
b6d15dde 107 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 108
6bc1e5d5 109 }
296c2a79 110
6bc1e5d5 111 function definition_after_data(){
c30949a9 112 $mform = $this->_form;
6bc1e5d5 113 $mform->applyFilter('username', 'trim');
114 }
e8a34ad4 115
a78890d5 116 function validation($data, $files) {
32648682 117 global $CFG, $DB;
a78890d5 118 $errors = parent::validation($data, $files);
e8a34ad4 119
3fe6b721 120 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 121
32648682 122 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
6bc1e5d5 123 $errors['username'] = get_string('usernameexists');
124 } else {
07ed083e 125 //check allowed characters
2f1e464a 126 if ($data['username'] !== core_text::strtolower($data['username'])) {
07ed083e 127 $errors['username'] = get_string('usernamelowercase');
6b8ad965
PS
128 } else {
129 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
79604225 130 $errors['username'] = get_string('invalidusername');
6bc1e5d5 131 }
6b8ad965 132
6bc1e5d5 133 }
134 }
135
136 //check if user exists in external db
137 //TODO: maybe we should check all enabled plugins instead
152bc5a2 138 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 139 $errors['username'] = get_string('usernameexists');
140 }
141
142
143 if (! validate_email($data['email'])) {
144 $errors['email'] = get_string('invalidemail');
145
32648682 146 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
6bc1e5d5 147 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
148 }
e8a34ad4 149 if (empty($data['email2'])) {
296c2a79 150 $errors['email2'] = get_string('missingemail');
e8a34ad4 151
152 } else if ($data['email2'] != $data['email']) {
296c2a79 153 $errors['email2'] = get_string('invalidemail');
e8a34ad4 154 }
6bc1e5d5 155 if (!isset($errors['email'])) {
156 if ($err = email_is_not_allowed($data['email'])) {
157 $errors['email'] = $err;
158 }
e8a34ad4 159
6bc1e5d5 160 }
e8a34ad4 161
a78890d5 162 $errmsg = '';
83022298 163 if (!check_password_policy($data['password'], $errmsg)) {
164 $errors['password'] = $errmsg;
165 }
f85509b5 166
c30949a9 167 if ($this->signup_captcha_enabled()) {
f85509b5 168 $recaptcha_element = $this->_form->getElement('recaptcha_element');
169 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
170 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
171 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
172 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
173 $errors['recaptcha'] = $result;
174 }
175 } else {
176 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 177 }
178 }
c862e2f4 179 // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
9247265b 180 $dataobject = (object)$data;
c862e2f4 181 $dataobject->id = 0;
9247265b 182 $errors += profile_validation($dataobject, $files);
e8a34ad4 183
f85509b5 184 return $errors;
185
c30949a9 186 }
f85509b5 187
c30949a9
PS
188 /**
189 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
190 * @return bool
191 */
192 function signup_captcha_enabled() {
193 global $CFG;
194 return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');
6bc1e5d5 195 }
c30949a9 196
e8a34ad4 197}