Register.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Widget;
  3. use Typecho\Common;
  4. use Typecho\Cookie;
  5. use Typecho\Db\Exception;
  6. use Typecho\Validate;
  7. use Utils\PasswordHash;
  8. use Widget\Base\Users;
  9. if (!defined('__TYPECHO_ROOT_DIR__')) {
  10. exit;
  11. }
  12. /**
  13. * 注册组件
  14. *
  15. * @author qining
  16. * @category typecho
  17. * @package Widget
  18. */
  19. class Register extends Users implements ActionInterface
  20. {
  21. /**
  22. * 初始化函数
  23. *
  24. * @throws Exception
  25. */
  26. public function action()
  27. {
  28. // protect
  29. $this->security->protect();
  30. /** 如果已经登录 */
  31. if ($this->user->hasLogin() || !$this->options->allowRegister) {
  32. /** 直接返回 */
  33. $this->response->redirect($this->options->index);
  34. }
  35. /** 初始化验证类 */
  36. $validator = new Validate();
  37. $validator->addRule('name', 'required', _t('必须填写用户名称'));
  38. $validator->addRule('name', 'minLength', _t('用户名至少包含2个字符'), 2);
  39. $validator->addRule('name', 'maxLength', _t('用户名最多包含32个字符'), 32);
  40. $validator->addRule('name', 'xssCheck', _t('请不要在用户名中使用特殊字符'));
  41. $validator->addRule('name', [$this, 'nameExists'], _t('用户名已经存在'));
  42. $validator->addRule('mail', 'required', _t('必须填写电子邮箱'));
  43. $validator->addRule('mail', [$this, 'mailExists'], _t('电子邮箱地址已经存在'));
  44. $validator->addRule('mail', 'email', _t('电子邮箱格式错误'));
  45. $validator->addRule('mail', 'maxLength', _t('电子邮箱最多包含64个字符'), 64);
  46. /** 如果请求中有password */
  47. if (array_key_exists('password', $_REQUEST)) {
  48. $validator->addRule('password', 'required', _t('必须填写密码'));
  49. $validator->addRule('password', 'minLength', _t('为了保证账户安全, 请输入至少六位的密码'), 6);
  50. $validator->addRule('password', 'maxLength', _t('为了便于记忆, 密码长度请不要超过十八位'), 18);
  51. $validator->addRule('confirm', 'confirm', _t('两次输入的密码不一致'), 'password');
  52. }
  53. /** 截获验证异常 */
  54. if ($error = $validator->run($this->request->from('name', 'password', 'mail', 'confirm'))) {
  55. Cookie::set('__typecho_remember_name', $this->request->name);
  56. Cookie::set('__typecho_remember_mail', $this->request->mail);
  57. /** 设置提示信息 */
  58. Notice::alloc()->set($error);
  59. $this->response->goBack();
  60. }
  61. $hasher = new PasswordHash(8, true);
  62. $generatedPassword = Common::randString(7);
  63. $dataStruct = [
  64. 'name' => $this->request->name,
  65. 'mail' => $this->request->mail,
  66. 'screenName' => $this->request->name,
  67. 'password' => $hasher->hashPassword($generatedPassword),
  68. 'created' => $this->options->time,
  69. 'group' => 'subscriber'
  70. ];
  71. $dataStruct = self::pluginHandle()->register($dataStruct);
  72. $insertId = $this->insert($dataStruct);
  73. $this->db->fetchRow($this->select()->where('uid = ?', $insertId)
  74. ->limit(1), [$this, 'push']);
  75. self::pluginHandle()->finishRegister($this);
  76. $this->user->login($this->request->name, $generatedPassword);
  77. Cookie::delete('__typecho_first_run');
  78. Cookie::delete('__typecho_remember_name');
  79. Cookie::delete('__typecho_remember_mail');
  80. Notice::alloc()->set(
  81. _t(
  82. '用户 <strong>%s</strong> 已经成功注册, 密码为 <strong>%s</strong>',
  83. $this->screenName,
  84. $generatedPassword
  85. ),
  86. 'success'
  87. );
  88. $this->response->redirect($this->options->adminUrl);
  89. }
  90. }