-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.php
executable file
·147 lines (130 loc) · 5.5 KB
/
Action.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
<?php
class MailValidate_Action extends Typecho_Widget implements Widget_Interface_Do
{
/** @var 数据操作对象 */
private $_db;
/** @var 插件根目录 */
private $_dir;
/** @var 插件配置信息 */
private $_cfg;
/** @var 系统配置信息 */
private $_options;
/** @var bool 是否记录日志 */
private $_isMailLog = false;
/** @var 当前登录用户 */
private $_user;
/** @var 邮件内容信息 */
private $_email;
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
}
public function init()
{
$this->_dir = dirname(__FILE__);
$this->_db = Typecho_Db::get();
$this->_user = $this->widget('Widget_User');
$this->_options = $this->widget('Widget_Options');
$this->_cfg = Helper::options()->plugin('MailValidate');
}
public function execute() {
return;
}
/*
* 发送邮件
*/
public function sendMail()
{
/** 载入邮件组件 */
require_once $this->_dir . '/lib/class.phpmailer.php';
$mailer = new PHPMailer();
$mailer->CharSet = 'UTF-8';
$mailer->Encoding = 'base64';
//选择发信模式
switch ($this->_cfg->mode)
{
case 'mail':
break;
case 'sendmail':
$mailer->IsSendmail();
break;
case 'smtp':
$mailer->IsSMTP();
if (in_array('validate', $this->_cfg->validate)) {
$mailer->SMTPAuth = true;
}
if (in_array('ssl', $this->_cfg->validate)) {
$mailer->SMTPSecure = "ssl";
}
$mailer->Host = $this->_cfg->host;
$mailer->Port = $this->_cfg->port;
$mailer->Username = $this->_cfg->user;
$mailer->Password = $this->_cfg->pass;
break;
}
$mailer->SetFrom($this->_email->from, $this->_email->fromName);
$mailer->AddReplyTo($this->_email->to, $this->_email->toName);
$mailer->Subject = $this->_email->subject;
$mailer->AltBody = $this->_email->altBody;
$mailer->MsgHTML($this->_email->msgHtml);
$mailer->AddAddress($this->_email->to, $this->_email->toName);
if ($result = $mailer->Send()) {
$this->mailLog();
} else {
$this->mailLog(false, $mailer->ErrorInfo . "\r\n");
$result = $mailer->ErrorInfo;
}
$mailer->ClearAddresses();
$mailer->ClearReplyTos();
return $result;
}
public function action(){
$this->init();
$token=$this->request->token;
if($token){
try {
$row = $this->_db->fetchRow($this->_db->select('validate_state')->from('table.users')->where('validate_token = ?', $token));
if($row['validate_state']==="1"){
$this->_db->query($this->_db->update('table.users')->rows(array('validate_state' => 2))->where('validate_token = ?', $token));
$group = $this->_db->fetchRow($this->_db->select('group')->from('table.users')->where('validate_token = ?', $token));
if($group['group']==="subscriber"){
$this->_db->query($this->_db->update('table.users')->rows(array('group' => "contributor"))->where('validate_token = ?', $token));
}
echo(file_get_contents($this->_dir."/success.html"));
}else{
echo(file_get_contents($this->_dir."/fail.html"));
}
} catch (Exception $ex) {
echo $ex->getCode();
}
} else {
echo(file_get_contents($this->_dir."/fail.html"));
}
}
public function send(){
$this->init();
if(!$this->_user->mail){
$this->widget('Widget_Notice')->set("邮件发送失败",'notice');
$this->response->goBack();
}else{
$this->_email->from = $this->_cfg->user;
$this->_email->fromName = $this->_cfg->fromName ? $this->_cfg->fromName : $this->_options->title;
$this->_email->to = $this->_user->mail;
$this->_email->toName = $this->_user->screenName;
$this->_email->subject = $this->_cfg->titleForGuest;
//生成token:md5(mail+time+随机数)
$token=md5($this->_user->mail.time().$this->_user->mail.rand());
$this->_db->query($this->_db->update('table.users')->rows(array('validate_token' => $token))->where('uid = ?', $this->_user->uid));
$mailcontent=file_get_contents($this->_dir."/mail.html");
$keys=array('%sitename%'=>$this->_options->title,'%username%'=>$this->_user->screenName,'%verifyurl%'=>$this->_options->siteUrl."MailValidate/verify?token=".$token,'%useravatar%'=>md5($this->_user->mail));
$this->_email->altBody = $mailcontent;
$this->_email->msgHtml = $mailcontent;
$result = $this->sendMail();
$this->_db->query($this->_db->update('table.users')->rows(array('validate_state' => 1))->where('uid = ?', $this->_user->uid));
$this->widget('Widget_Notice')->set(true === $result ? _t('邮件发送成功') : _t('邮件发送失败:' . $result),
true === $result ? 'success' : 'notice');
$this->response->goBack();
}
}
}
?>