PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/login/signup_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 197 lines | 127 code | 39 blank | 31 comment | 29 complexity | 327f1d1a7b2fc4a7b9fae28aaffda75d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * User sign-up form.
  18. *
  19. * @package core
  20. * @subpackage auth
  21. * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir.'/formslib.php');
  26. require_once($CFG->dirroot.'/user/profile/lib.php');
  27. require_once($CFG->dirroot . '/user/editlib.php');
  28. class login_signup_form extends moodleform {
  29. function definition() {
  30. global $USER, $CFG;
  31. $mform = $this->_form;
  32. $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
  33. $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
  34. $mform->setType('username', PARAM_NOTAGS);
  35. $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
  36. if (!empty($CFG->passwordpolicy)){
  37. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  38. }
  39. $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
  40. $mform->setType('password', PARAM_RAW);
  41. $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
  42. $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
  43. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
  44. $mform->setType('email', PARAM_NOTAGS);
  45. $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
  46. $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
  47. $mform->setType('email2', PARAM_NOTAGS);
  48. $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
  49. $namefields = useredit_get_required_name_fields();
  50. foreach ($namefields as $field) {
  51. $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
  52. $mform->setType($field, PARAM_TEXT);
  53. $stringid = 'missing' . $field;
  54. if (!get_string_manager()->string_exists($stringid, 'moodle')) {
  55. $stringid = 'required';
  56. }
  57. $mform->addRule($field, get_string($stringid), 'required', null, 'server');
  58. }
  59. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
  60. $mform->setType('city', PARAM_TEXT);
  61. if (!empty($CFG->defaultcity)) {
  62. $mform->setDefault('city', $CFG->defaultcity);
  63. }
  64. $country = get_string_manager()->get_list_of_countries();
  65. $default_country[''] = get_string('selectacountry');
  66. $country = array_merge($default_country, $country);
  67. $mform->addElement('select', 'country', get_string('country'), $country);
  68. if( !empty($CFG->country) ){
  69. $mform->setDefault('country', $CFG->country);
  70. }else{
  71. $mform->setDefault('country', '');
  72. }
  73. if ($this->signup_captcha_enabled()) {
  74. $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
  75. $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
  76. }
  77. profile_signup_fields($mform);
  78. if (!empty($CFG->sitepolicy)) {
  79. $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
  80. $mform->setExpanded('policyagreement');
  81. $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
  82. $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
  83. $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
  84. }
  85. // buttons
  86. $this->add_action_buttons(true, get_string('createaccount'));
  87. }
  88. function definition_after_data(){
  89. $mform = $this->_form;
  90. $mform->applyFilter('username', 'trim');
  91. }
  92. function validation($data, $files) {
  93. global $CFG, $DB;
  94. $errors = parent::validation($data, $files);
  95. $authplugin = get_auth_plugin($CFG->registerauth);
  96. if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
  97. $errors['username'] = get_string('usernameexists');
  98. } else {
  99. //check allowed characters
  100. if ($data['username'] !== core_text::strtolower($data['username'])) {
  101. $errors['username'] = get_string('usernamelowercase');
  102. } else {
  103. if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
  104. $errors['username'] = get_string('invalidusername');
  105. }
  106. }
  107. }
  108. //check if user exists in external db
  109. //TODO: maybe we should check all enabled plugins instead
  110. if ($authplugin->user_exists($data['username'])) {
  111. $errors['username'] = get_string('usernameexists');
  112. }
  113. if (! validate_email($data['email'])) {
  114. $errors['email'] = get_string('invalidemail');
  115. } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
  116. $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
  117. }
  118. if (empty($data['email2'])) {
  119. $errors['email2'] = get_string('missingemail');
  120. } else if ($data['email2'] != $data['email']) {
  121. $errors['email2'] = get_string('invalidemail');
  122. }
  123. if (!isset($errors['email'])) {
  124. if ($err = email_is_not_allowed($data['email'])) {
  125. $errors['email'] = $err;
  126. }
  127. }
  128. $errmsg = '';
  129. if (!check_password_policy($data['password'], $errmsg)) {
  130. $errors['password'] = $errmsg;
  131. }
  132. if ($this->signup_captcha_enabled()) {
  133. $recaptcha_element = $this->_form->getElement('recaptcha_element');
  134. if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
  135. $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
  136. $response_field = $this->_form->_submitValues['recaptcha_response_field'];
  137. if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
  138. $errors['recaptcha'] = $result;
  139. }
  140. } else {
  141. $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
  142. }
  143. }
  144. // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
  145. $dataobject = (object)$data;
  146. $dataobject->id = 0;
  147. $errors += profile_validation($dataobject, $files);
  148. return $errors;
  149. }
  150. /**
  151. * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
  152. * @return bool
  153. */
  154. function signup_captcha_enabled() {
  155. global $CFG;
  156. return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');
  157. }
  158. }