forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.php
259 lines (217 loc) · 8.19 KB
/
Widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 找回密码类
*
* @package Passport
* @copyright Copyright (c) 2016 ShingChi (https://github.com/shingchi)
* @license GNU General Public License 2.0
*/
class Passport_Widget extends Typecho_Widget
{
/**
* 配置表
*
* @access private
* @var Typecho_Options
*/
private $options;
/**
* 插件配置
*
* @access private
* @var Object
*/
private $config;
/**
* 提示框组件
*
* @access private
* @var Widget_Notice
*/
private $notice;
/**
* 构造函数
*
* @access public
* @param mixed $request request对象
* @param mixed $response response对象
* @param mixed $params 参数列表
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
$this->notice = parent::widget('Widget_Notice');
$this->options = parent::widget('Widget_Options');
$this->config = $this->options->plugin('Passport');
}
/**
* execute function.
*
* @access public
* @return void
*/
public function execute(){}
/**
* 找回密码
*
* @access public
* @return void
*/
public function doForgot()
{
require_once 'theme/forgot.php';
if ($this->request->isPost()) {
/* 验证表单 */
if ($error = $this->forgotForm()->validate()) {
$this->notice->set($error, 'error');
return false;
}
$db = Typecho_Db::get();
$user = $db->fetchRow($db->select()->from('table.users')->where('mail = ?', $this->request->mail));
if (empty($user)) {
// 返回没有该用户
$this->notice->set(_t('该邮箱还没有注册'), 'error');
return false;
}
/* 生成重置密码地址 */
$hashString = $user['name'] . $user['mail'] . $user['password'];
$hashValidate = Typecho_Common::hash($hashString);
$token = base64_encode($user['uid'] . '.' . $hashValidate . '.' . $this->options->gmtTime);
$url = Typecho_Common::url('/passport/reset?token=' . $token, $this->options->index);
/* 发送重置密码地址 */
require_once 'PHPMailer/PHPMailerAutoload.php';
$phpMailer = new PHPMailer();
/* SMTP设置 */
$phpMailer->CharSet = 'UTF-8';
$phpMailer->isSMTP();
$phpMailer->SMTPAuth = true;
$phpMailer->Host = $this->config->host;
$phpMailer->Port = $this->config->port;
$phpMailer->Username = $this->config->username;
$phpMailer->Password = $this->config->password;
$phpMailer->isHTML(true);
if ('none' != $this->config->secure) {
$phpMailer->SMTPSecure = $this->config->secure;
}
$phpMailer->setFrom($this->config->username, $this->options->title);
$phpMailer->addAddress($user['mail'], $user['name']);
$phpMailer->Subject = '密码重置';
$phpMailer->Body = '<p>' . $user['name'] . ' 您好,您申请了重置登录密码</p>'
. '<p>请在 1 小时内点击此链接以完成重置 <a href="' . $url . '">' . $url . '</a>';
if(!$phpMailer->send()) {
$this->notice->set(_t('邮件发送失败, 请重试或联系站长'), 'error');
} else {
$this->notice->set(_t('邮件已成功发送, 请注意查收'), 'success');
}
}
}
/**
* 重置密码
*
* @access public
* @return void
*/
public function doReset()
{
/* 验证token */
$token = $this->request->filter('strip_tags', 'trim', 'xss')->token;
list($uid, $hashValidate, $timeStamp) = explode('.', base64_decode($token));
$currentTimeStamp = $this->options->gmtTime;
/* 检查链接时效 */
if (($currentTimeStamp - $timeStamp) > 3600) {
// 链接失效, 返回登录页
$this->notice->set(_t('该链接已失效, 请重新获取'), 'notice');
$this->response->redirect($this->options->loginUrl);
}
$db = Typecho_Db::get();
$user = $db->fetchRow($db->select()->from('table.users')->where('uid = ?', $uid));
$hashString = $user['name'] . $user['mail'] . $user['password'];
$hashValidate = Typecho_Common::hashValidate($hashString, $hashValidate);
if (!$hashValidate) {
// token错误, 返回登录页
$this->notice->set(_t('该链接已失效, 请重新获取'), 'notice');
$this->response->redirect($this->options->loginUrl);
}
require_once 'theme/reset.php';
/* 重置密码 */
if ($this->request->isPost()) {
/* 验证表单 */
if ($error = $this->resetForm()->validate()) {
$this->notice->set($error, 'error');
return false;
}
$hasher = new PasswordHash(8, true);
$password = $hasher->HashPassword($this->request->password);
$update = $db->query($db->update('table.users')
->rows(array('password' => $password))
->where('uid = ?', $user['uid']));
if (!$update) {
$this->notice->set(_t('重置密码失败'), 'error');
}
$this->notice->set(_t('重置密码成功'), 'success');
$this->response->redirect($this->options->loginUrl);
}
}
/**
* 生成找回密码表单
*
* @access public
* @return Typecho_Widget_Helper_Form
*/
public function forgotForm() {
$form = new Typecho_Widget_Helper_Form(NULL, Typecho_Widget_Helper_Form::POST_METHOD);
$mail = new Typecho_Widget_Helper_Form_Element_Text('mail',
NULL,
NULL,
_t('邮箱'),
_t('账号对应的邮箱地址'));
$form->addInput($mail);
/** 用户动作 */
$do = new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'mail');
$form->addInput($do);
/** 提交按钮 */
$submit = new Typecho_Widget_Helper_Form_Element_Submit('submit', NULL, _t('提交'));
$submit->input->setAttribute('class', 'btn primary');
$form->addItem($submit);
$mail->addRule('required', _t('必须填写电子邮箱'));
$mail->addRule('email', _t('电子邮箱格式错误'));
return $form;
}
/**
* 生成重置密码表单
*
* @access public
* @return Typecho_Widget_Helper_Form
*/
public function resetForm() {
$form = new Typecho_Widget_Helper_Form(NULL, Typecho_Widget_Helper_Form::POST_METHOD);
/** 新密码 */
$password = new Typecho_Widget_Helper_Form_Element_Password('password',
NULL,
NULL,
_t('新密码'),
_t('建议使用特殊字符与字母、数字的混编样式,以增加系统安全性.'));
$password->input->setAttribute('class', 'w-100');
$form->addInput($password);
/** 新密码确认 */
$confirm = new Typecho_Widget_Helper_Form_Element_Password('confirm',
NULL,
NULL,
_t('密码确认'),
_t('请确认你的密码, 与上面输入的密码保持一致.'));
$confirm->input->setAttribute('class', 'w-100');
$form->addInput($confirm);
/** 用户动作 */
$do = new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'password');
$form->addInput($do);
/** 提交按钮 */
$submit = new Typecho_Widget_Helper_Form_Element_Submit('submit', NULL, _t('更新密码'));
$submit->input->setAttribute('class', 'btn primary');
$form->addItem($submit);
$password->addRule('required', _t('必须填写密码'));
$password->addRule('minLength', _t('为了保证账户安全, 请输入至少六位的密码'), 6);
$confirm->addRule('confirm', _t('两次输入的密码不一致'), 'password');
return $form;
}
}