PageRenderTime 27ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/login/signup_form.php

https://github.com/200896596/moodle
PHP | 198 lines | 127 code | 41 blank | 30 comment | 33 complexity | b72659d46679c844acc68362a677d6dc MD5 | raw file
  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. class login_signup_form extends moodleform {
  28. function definition() {
  29. global $USER, $CFG;
  30. $mform = $this->_form;
  31. $mform->addElement('header', '', get_string('createuserandpass'), '');
  32. $mform->addElement('text', 'username', get_string('username').'(必须以.local结尾)', 'maxlength="100" size="12"');
  33. $mform->setType('username', PARAM_NOTAGS);
  34. $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
  35. if (!empty($CFG->passwordpolicy)){
  36. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  37. }
  38. $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
  39. $mform->setType('password', PARAM_RAW);
  40. $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
  41. $mform->addElement('header', '', get_string('supplyinfo'),'');
  42. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
  43. $mform->setType('email', PARAM_NOTAGS);
  44. $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
  45. $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
  46. $mform->setType('email2', PARAM_NOTAGS);
  47. $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
  48. if (!firstname_first()) { // See MDL-4325
  49. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  50. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  51. } else {
  52. $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
  53. $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
  54. }
  55. $mform->setType('firstname', PARAM_TEXT);
  56. $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
  57. $mform->setType('lastname', PARAM_TEXT);
  58. $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
  59. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
  60. $mform->setType('city', PARAM_TEXT);
  61. $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
  62. if (!empty($CFG->defaultcity)) {
  63. $mform->setDefault('city', $CFG->defaultcity);
  64. }
  65. $country = get_string_manager()->get_list_of_countries();
  66. $default_country[''] = get_string('selectacountry');
  67. $country = array_merge($default_country, $country);
  68. $mform->addElement('select', 'country', get_string('country'), $country);
  69. $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
  70. if( !empty($CFG->country) ){
  71. $mform->setDefault('country', $CFG->country);
  72. }else{
  73. $mform->setDefault('country', '');
  74. }
  75. if ($this->signup_captcha_enabled()) {
  76. $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
  77. $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
  78. }
  79. profile_signup_fields($mform);
  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. // buttons
  87. $this->add_action_buttons(true, get_string('createaccount'));
  88. }
  89. function definition_after_data(){
  90. $mform = $this->_form;
  91. $mform->applyFilter('username', 'trim');
  92. }
  93. function validation($data, $files) {
  94. global $CFG, $DB;
  95. $errors = parent::validation($data, $files);
  96. $authplugin = get_auth_plugin($CFG->registerauth);
  97. if (substr($data['username'], -6) != '.local') {
  98. $errors['username'] = '用户名必须以“.local”结尾';
  99. } else if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
  100. $errors['username'] = get_string('usernameexists');
  101. } else {
  102. //check allowed characters
  103. if ($data['username'] !== moodle_strtolower($data['username'])) {
  104. $errors['username'] = get_string('usernamelowercase');
  105. } else {
  106. if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
  107. $errors['username'] = get_string('invalidusername');
  108. }
  109. }
  110. }
  111. //check if user exists in external db
  112. //TODO: maybe we should check all enabled plugins instead
  113. if ($authplugin->user_exists($data['username'])) {
  114. $errors['username'] = get_string('usernameexists');
  115. }
  116. if (! validate_email($data['email'])) {
  117. $errors['email'] = get_string('invalidemail');
  118. } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
  119. $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
  120. }
  121. if (empty($data['email2'])) {
  122. $errors['email2'] = get_string('missingemail');
  123. } else if ($data['email2'] != $data['email']) {
  124. $errors['email2'] = get_string('invalidemail');
  125. }
  126. if (!isset($errors['email'])) {
  127. if ($err = email_is_not_allowed($data['email'])) {
  128. $errors['email'] = $err;
  129. }
  130. }
  131. $errmsg = '';
  132. if (!check_password_policy($data['password'], $errmsg)) {
  133. $errors['password'] = $errmsg;
  134. }
  135. if ($this->signup_captcha_enabled()) {
  136. $recaptcha_element = $this->_form->getElement('recaptcha_element');
  137. if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
  138. $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
  139. $response_field = $this->_form->_submitValues['recaptcha_response_field'];
  140. if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
  141. $errors['recaptcha'] = $result;
  142. }
  143. } else {
  144. $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
  145. }
  146. }
  147. return $errors;
  148. }
  149. /**
  150. * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
  151. * @return bool
  152. */
  153. function signup_captcha_enabled() {
  154. global $CFG;
  155. return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');
  156. }
  157. }