Skip to content

Commit

Permalink
UPDATE TO 1.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
byends committed Apr 21, 2014
1 parent 64e5271 commit 37fdf3d
Show file tree
Hide file tree
Showing 9 changed files with 5,073 additions and 3,375 deletions.
339 changes: 207 additions & 132 deletions Action.php

Large diffs are not rendered by default.

210 changes: 117 additions & 93 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,35 @@
* 评论回复邮件提醒插件
*
* @package CommentToMail
* @author Update By Byends
* @version 1.2.6
* @author Byends Upgrade
* @version 1.3.2
* @link http://www.byends.com
* @oriAuthor DEFE (http://defe.me)
*
* 原作者是 DEFE (http://defe.me),请尊重版权
*
* update at 2012-07-11
* 1、修复接收邮箱为空时 无法获取 博主邮箱 导致无法发送邮件的BUG
* 2、修复已经设置邮件标题 却无效的BUG
* 3、修复评论者对博主的回复进行回复时,发送邮件的格式依然选择博主格式时的逻辑BUG
* 4、修复由于没有设置字符类型 和 编码类型 可能导致邮件乱码的BUG,字符类型设置为 UTF-8,编码类型为 base64
* 5、修复评论时间 时错误的BUG
* 5、邮件标题增加不能为空的校验
* 6、整理代码格式,提高可读性
*/
class CommentToMail_Plugin implements Typecho_Plugin_Interface {
class CommentToMail_Plugin implements Typecho_Plugin_Interface
{
private static $_actionName = 'comment-to-mail';
private static $_isMailLog = false;

/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate() {
if (!ini_get('allow_url_fopen')) {
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机没有打开 allow_url_fopen 功能, 无法正常使用此插件'));
public static function activate()
{
if (false == Typecho_Http_Client::get()) {
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'));
}

Helper::addAction('comment-to-mail', 'CommentToMail_Action');
Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('CommentToMail_Plugin', 'parseComment');
Helper::addAction(self::$_actionName, 'CommentToMail_Action');

Typecho_Plugin::factory('Widget_Feedback')->finishComment = array('CommentToMail_Plugin', 'toMail');
return _t('请对插件进行正确设置,以使插件顺利工作!');
}

Expand All @@ -46,8 +43,9 @@ public static function activate() {
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate() {
Helper::removeAction('comment-to-mail');
public static function deactivate()
{
Helper::removeAction(self::$_actionName);
}

/**
Expand All @@ -57,7 +55,8 @@ public static function deactivate() {
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form) {
public static function config(Typecho_Widget_Helper_Form $form)
{
$mode= new Typecho_Widget_Helper_Form_Element_Radio('mode',
array( 'smtp' => 'smtp',
'mail' => 'mail()',
Expand Down Expand Up @@ -90,8 +89,12 @@ public static function config(Typecho_Widget_Helper_Form $form) {
$form->addInput($validate);

$mail = new Typecho_Widget_Helper_Form_Element_Text('mail', NULL, NULL,
_t('接收邮箱'),_t('接收邮件用的信箱,如为空则使用文章作者个人设置中的邮箱!'));
$form->addInput($mail->addRule('email', _t('请填写正确的邮箱!')));
_t('接收邮件的地址'),_t('接收邮件的地址,如为空则使用文章作者个人设置中的邮件地址!'));
$form->addInput($mail->addRule('email', _t('请填写正确的邮件地址!')));

$contactme = new Typecho_Widget_Helper_Form_Element_Text('contactme', NULL, NULL,
_t('联系我的邮件地址'),_t('联系我用的邮件地址,如为空则使用文章作者个人设置中的邮件地址!'));
$form->addInput($contactme->addRule('email', _t('请填写正确的邮件地址!')));

$status = new Typecho_Widget_Helper_Form_Element_Checkbox('status',
array('approved' => '提醒已通过评论',
Expand All @@ -105,9 +108,7 @@ public static function config(Typecho_Widget_Helper_Form $form) {
'to_guest' => '评论被回复时,发邮件通知评论者。',
'to_me'=>'自己回复自己的评论时,发邮件通知。(同时针对博主和访客)',
'to_log' => '记录邮件发送日志。'),
array('to_owner','to_guest','to_log'), '其他设置',_t('如果选上"记录邮件发送日志"选项,则会在./CommentToMail/log/mail_log.txt 文件中记录发送信息。<br />
关键性错误日志将自动记录到./CommentToMail/log/error_log.txt文件中。<br />
'));
array('to_owner','to_guest'), '其他设置',_t('如果选上"记录邮件发送日志"选项,则会在./CommentToMail/log/mailer_log.txt 文件中记录发送信息。'));
$form->addInput($other->multiMode());

$titleForOwner = new Typecho_Widget_Helper_Form_Element_Text('titleForOwner',null,"[{title}] 一文有新的评论",
Expand All @@ -126,92 +127,115 @@ public static function config(Typecho_Widget_Helper_Form $form) {
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form) {

}
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{}

/**
* 组合邮件内容
* 获取邮件内容
*
* @access public
* @param $comment 调用参数
* @return void
*/
public static function toMail($comment) {
$smtp = array();
$options = Typecho_Widget::widget('Widget_Options');
$smtp['site'] = $options->title;
$smtp['timezone'] = $options->timezone;
$smtp['cid'] = $comment->cid;
$smtp['coid'] = $comment->coid;
$smtp['created'] = $comment->created;
$smtp['author'] = $comment->author;
$smtp['authorId'] = $comment->authorId;
$smtp['ownerId'] = $comment->ownerId;
$smtp['mail'] = $comment->mail;
$smtp['ip'] = $comment->ip;
$smtp['title'] = $comment->title;
$smtp['text'] = $comment->text;
$smtp['permalink'] = $comment->permalink;
$smtp['status'] = $comment->status;
$smtp['parent'] = $comment->parent;
$smtp['manage'] = $options->siteUrl."admin/manage-comments.php";

//获取是否接收邮件的选项
if(isset ($_POST['banmail']) && 'stop' == $_POST['banmail']){
$smtp['banMail'] = 1;
} else{
$smtp['banMail'] = 0;
public static function parseComment($comment)
{
$options = Typecho_Widget::widget('Widget_Options');
$cfg = array(
'site' => $options->title,
'timezone' => $options->timezone,
'cid' => $comment->cid,
'coid' => $comment->coid,
'created' => $comment->created,
'author' => $comment->author,
'authorId' => $comment->authorId,
'ownerId' => $comment->ownerId,
'mail' => $comment->mail,
'ip' => $comment->ip,
'title' => $comment->title,
'text' => $comment->text,
'permalink' => $comment->permalink,
'status' => $comment->status,
'parent' => $comment->parent,
'manage' => $options->siteUrl . "admin/manage-comments.php"
);

self::$_isMailLog = in_array('to_log', Helper::options()->plugin('CommentToMail')->other) ? true : false;

//是否接收邮件
if (isset($_POST['banmail']) && 'stop' == $_POST['banmail']) {
$cfg['banMail'] = 1;
} else {
$cfg['banMail'] = 0;
}

$fileName = Typecho_Common::randString(7);
$cfg = (object)$cfg;
file_put_contents(dirname(__FILE__) . '/cache/' . $fileName, serialize($cfg));
$url = ($options->rewrite) ? $options->siteUrl : $options->siteUrl . 'index.php';
$url = rtrim($url, '/') . '/action/' . self::$_actionName . '?send=' . $fileName;


$client = Typecho_Http_Client::get('Socket', 'Curl');
if (false == $client) {
self::saveLog("'主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'\n");
return false;
}

// $client->send($url);

$filename = Typecho_Common::randString(7);
$smtp = (object)$smtp;
file_put_contents('.'.__TYPECHO_PLUGIN_DIR__.'/CommentToMail/cache/'.$filename, serialize($smtp));
$url = ($options->rewrite) ? $options->siteUrl : $options->siteUrl.'index.php';
self::asyncRequest($filename, $url);
}
/**
self::saveLog("开始发送请求:{$url}\n");
self::asyncRequest($url);
}

/**
* 发送异步请求
*
* @access public
* @param string $filename 存放邮件的临时文件名
* @param string $siteUrl 网站连接
* @return void
* @param $url
* @return bool
*/
public static function asyncRequest($filename, $siteUrl) {
public static function asyncRequest($url)
{
$params = parse_url($url);
$path = $params['path'] . '?' . $params['query'];
$host = $params['host'];
$port = 80;
$http = '';

if ('https' == $params['scheme']) {
$port = 443;
$http = 'ssl://';
}

$dmpt = parse_url($siteUrl);
$fp = @fsockopen($http . $host, $port, $errno, $errstr, 30);

$host = $dmpt['host'];
$port = $dmpt['port'] ? $dmpt['port'] : 80;
if ($fp === false) {
self::saveLog("SOCKET错误," . $errno . ':' . $errstr);
return false;
}

if(substr($dmpt['path'], -1) != '/') $dmpt['path'] .= '/';
$url = $dmpt['path'].'action/comment-to-mail';
$out = "GET " . $path . " HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";

$get = 'send='.$filename;
self::saveLog(var_export($params, true)."\n");
self::saveLog($out."\n");

$head = "GET ". $url . "?" . $get . " HTTP/1.0\r\n";
$head .= "Host: " . $host . "\r\n";
$head .= "\r\n";

if(function_exists('fsockopen')){
$fp = @fsockopen ($host, $port, $errno, $errstr, 30);
}
elseif(function_exists('pfsockopen')){
$fp = @pfsockopen ($host, $port, $errno, $errstr, 30);
} else {
$fp = stream_socket_client($host.":$port", $errno, $errstr, 30);
}

if($fp){
fputs ($fp, $head);
fclose($fp);
}else{
file_put_contents('.'.__TYPECHO_PLUGIN_DIR__.'/CommentToMail/log/error_log.txt', "SOCKET错误,".$errno.':'.$errstr);
}

fwrite($fp, $out);
sleep(1);
fclose($fp);
self::saveLog("请求结束\n");
}

/**
* 写入记录
* @param $content
* @return bool
*/
public static function saveLog($content)
{
if (!self::$_isMailLog) {
return false;
}


file_put_contents(dirname(__FILE__) . '/log/mailer_log.txt', $content, FILE_APPEND);
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ Typecho 评论邮件提醒插件
5. 设置smtp服务器地址、邮箱地址、密码等信息

### 升级日志

##### 1.3.2 Upgrade at 2014-04-17
- 更新PHPMailer邮件类至最新版
- 修复0.9版不能发送邮件的BUG
- 提高安全性,缓存文件读取一次后直接删除

##### 1.3.1 Upgrade at 2014-02-18
- 修复回复评论时不发送邮件的BUG
- 优化邮件发送效率

##### 1.3.0 Upgrade at 2014-01-21
- 添加联系我邮箱配置

##### 1.2.6 Upgrade at 2012-07-11
- 修复接收邮箱为空时 无法获取 博主邮箱 导致无法发送邮件的BUG
Expand Down
Loading

0 comments on commit 37fdf3d

Please sign in to comment.