This commit is contained in:
225
CommentPush/lib/AliYunEmailService.php
Normal file
225
CommentPush/lib/AliYunEmailService.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
|
||||
|
||||
class AliYunEmailService extends Service
|
||||
{
|
||||
const HANGZHOU = 'hangzhou';
|
||||
const SINGAPORE = 'singapore';
|
||||
const SYDNEY = 'sydney';
|
||||
|
||||
private static $regions = [
|
||||
self::HANGZHOU => [
|
||||
'regionId' => 'cn-hangzhou',
|
||||
'host' => 'https://dm.aliyuncs.com/',
|
||||
'version' => '2015-11-23'
|
||||
],
|
||||
self::SINGAPORE => [
|
||||
'regionId' => 'ap-southeast-1',
|
||||
'host' => 'https://dm.ap-southeast-1.aliyuncs.com/',
|
||||
'version' => '2017-06-22'
|
||||
],
|
||||
self::SYDNEY => [
|
||||
'regionId' => 'ap-southeast-2',
|
||||
'host' => 'https://dm.ap-southeast-2.aliyuncs.com/',
|
||||
'version' => '2017-06-22'
|
||||
]
|
||||
];
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new AliYunEmailService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$isPushBlogger = $plugin->isPushBlogger;
|
||||
if ($comment['authorId'] == 1 && $isPushBlogger == 1 && !$comment['parent']) return false;
|
||||
|
||||
$isPushCommentReply = $plugin->isPushCommentReply;
|
||||
|
||||
$options = Helper::options();
|
||||
$accountName = $plugin->accountName;
|
||||
$regionId = $plugin->regionId;
|
||||
$accessKeyId = $plugin->accessKeyId;
|
||||
$accessKeySecret = $plugin->accessKeySecret;
|
||||
$fromAlias = empty($plugin->fromAlias) ? $options->title : $plugin->fromAlias;
|
||||
$toAddress = $comment['mail'];
|
||||
|
||||
$regionInfo = self::$regions[$regionId];
|
||||
|
||||
|
||||
if (empty($accountName) || empty($accessKeyId) || empty($accessKeySecret) || empty($regionInfo)) throw new \Exception('缺少阿里云邮件推送配置');
|
||||
|
||||
|
||||
$parentComment = NULL;
|
||||
|
||||
if ($comment['authorId'] != $comment['ownerId']) {
|
||||
$author = self::getWidget('Users', 'uid', $comment['ownerId']);
|
||||
$toAddress = $author->mail;
|
||||
$parentComment = NULL;
|
||||
}
|
||||
|
||||
|
||||
if ($comment['parent'] && $comment['parent'] > 0) {
|
||||
$parentComment = self::getWidget('Comments', 'coid', $comment['parent']);
|
||||
if (isset($parentComment->coid) && $comment['mail'] != $parentComment->mail) {
|
||||
$toAddress = $parentComment->mail;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_null($parentComment) && $isPushCommentReply != 1) return false;
|
||||
|
||||
list($subject, $body) = self::getSubjectAndBody($parentComment, $options, $comment, $active);
|
||||
|
||||
|
||||
$data = [
|
||||
'Action' => 'SingleSendMail',
|
||||
'AccountName' => $accountName,
|
||||
'ReplyToAddress' => "true",
|
||||
'AddressType' => 1,
|
||||
'ToAddress' => $toAddress,
|
||||
'FromAlias' => $fromAlias,
|
||||
'Subject' => $subject,
|
||||
'HtmlBody' => $body,
|
||||
'Format' => 'JSON',
|
||||
'Version' => $regionInfo['version'],
|
||||
'AccessKeyId' => $accessKeyId,
|
||||
'SignatureMethod' => 'HMAC-SHA1',
|
||||
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
|
||||
'SignatureVersion' => '1.0',
|
||||
'SignatureNonce' => md5(time()),
|
||||
'RegionId' => $regionInfo['regionId']
|
||||
];
|
||||
$data['Signature'] = self::sign($data, $accessKeySecret);
|
||||
|
||||
|
||||
$content = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-type: application/x-www-form-urlencoded',
|
||||
'content' => self::getPostHttpBody($data)
|
||||
]
|
||||
]);
|
||||
$result = file_get_contents($regionInfo['host'], null, $content);
|
||||
self::logger(__CLASS__, $toAddress, $data, $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function getSubjectAndBody($parentComment, $options, $comment, $active)
|
||||
{
|
||||
$html = !is_null($parentComment) ?
|
||||
file_get_contents(dirname(__DIR__) . '/theme/reply.html') :
|
||||
file_get_contents(dirname(__DIR__) . '/theme/author.html');
|
||||
|
||||
|
||||
$subject = !is_null($parentComment) ?
|
||||
_t('您在 [' . trim($options->title) . '] 的评论有了新的回复!') :
|
||||
_t('您在 [' . trim($options->title) . '] 发表的文章有新评论!');
|
||||
|
||||
|
||||
$body = !is_null($parentComment) ? str_replace(
|
||||
[
|
||||
'{blogUrl}',
|
||||
'{blogName}',
|
||||
'{author}',
|
||||
'{permalink}',
|
||||
'{title}',
|
||||
'{text}',
|
||||
'{replyAuthor}',
|
||||
'{replyText}',
|
||||
'{commentUrl}'
|
||||
],
|
||||
[
|
||||
trim($options->siteUrl),
|
||||
trim($options->title),
|
||||
trim($parentComment->author),
|
||||
trim($active->permalink . '#comment-' . $comment['coid']),
|
||||
trim($active->title),
|
||||
trim($parentComment->text),
|
||||
trim($comment['author']),
|
||||
trim($comment['text']),
|
||||
trim($active->permalink . '#comment-' . $comment['coid'])
|
||||
], $html) : str_replace(
|
||||
[
|
||||
'{blogUrl}',
|
||||
'{blogName}',
|
||||
'{author}',
|
||||
'{permalink}',
|
||||
'{title}',
|
||||
'{text}',
|
||||
'{ip}'
|
||||
],
|
||||
[
|
||||
trim($options->siteUrl),
|
||||
trim($options->title),
|
||||
trim($comment['author']),
|
||||
trim($active->permalink . '#comment-' . $comment['coid']),
|
||||
trim($active->title),
|
||||
trim($comment['text']),
|
||||
trim($comment['ip'])
|
||||
], $html
|
||||
);
|
||||
|
||||
return [$subject, $body];
|
||||
}
|
||||
|
||||
private function getWidget($table, $key, $val)
|
||||
{
|
||||
$className = 'Widget_Abstract_' . $table;
|
||||
$db = Typecho_Db::get();
|
||||
$widget = new $className(Typecho_Request::getInstance(), Typecho_Widget_Helper_Empty::getInstance());
|
||||
$db->fetchRow($widget->select()->where($key . ' = ?', $val)->limit(1), array($widget, 'push'));
|
||||
|
||||
return $widget;
|
||||
}
|
||||
|
||||
private function sign($params, $accessKeySecret)
|
||||
{
|
||||
ksort($params);
|
||||
|
||||
$stringToSign = 'POST&' . self::percentEncode('/') . '&';
|
||||
|
||||
$tmp = '';
|
||||
|
||||
foreach ($params as $k => $param) $tmp .= '&' . self::percentEncode($k) . '=' . self::percentEncode($param);
|
||||
|
||||
$tmp = trim($tmp, '&');
|
||||
|
||||
$stringToSign = $stringToSign . self::percentEncode($tmp);
|
||||
|
||||
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', TRUE));
|
||||
|
||||
return $signature;
|
||||
}
|
||||
|
||||
|
||||
private function percentEncode($val)
|
||||
{
|
||||
$res = urlencode($val);
|
||||
$res = preg_replace('/\+/', '%20', $res);
|
||||
$res = preg_replace('/\*/', '%2A', $res);
|
||||
$res = preg_replace('/%7E/', '~', $res);
|
||||
return $res;
|
||||
}
|
||||
|
||||
private function getPostHttpBody($param)
|
||||
{
|
||||
$str = "";
|
||||
foreach ($param as $k => $v) $str .= $k . '=' . urlencode($v) . '&';
|
||||
return substr($str, 0, -1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
12
CommentPush/lib/Contract/ServiceInterface.php
Normal file
12
CommentPush/lib/Contract/ServiceInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
*/
|
||||
|
||||
|
||||
interface ServiceInterface
|
||||
{
|
||||
public function __handler($active, $comment, $plugin);
|
||||
|
||||
public function logger($object, $context, $result, $error);
|
||||
}
|
||||
67
CommentPush/lib/DingTalkBotService.php
Normal file
67
CommentPush/lib/DingTalkBotService.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* @author stars_kim <stars_kim@163.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
|
||||
class DingTalkBotService extends Service
|
||||
{
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new DingTalkBotService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$isPushBlogger = $plugin->isPushBlogger;
|
||||
if ($comment['authorId'] == 1 && $isPushBlogger == 1) return false;
|
||||
|
||||
$DingTalkWebhook = $plugin->DingTalkWebhook;
|
||||
$DingTalkSecret = $plugin->DingTalkSecret;
|
||||
|
||||
if (empty($DingTalkWebhook) || empty($DingTalkSecret)) throw new \Exception('缺少钉钉机器人配置参数');
|
||||
|
||||
$title = $active->title;
|
||||
$author = $comment['author'];
|
||||
$link = $active->permalink;
|
||||
$context = $comment['text'];
|
||||
|
||||
$template = '标题:' . $title . PHP_EOL
|
||||
. '评论人:' . $author . " [{$comment['ip']}]" . PHP_EOL
|
||||
. '评论内容:' . $context . PHP_EOL
|
||||
. '链接:' . $link . '#comment-' . $comment['coid'];
|
||||
|
||||
$params = [
|
||||
'msgtype' => 'text',
|
||||
'text' => [
|
||||
'content' => $template
|
||||
]
|
||||
];
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-Type: application/json;charset=utf-8',
|
||||
'content' => json_encode($params)
|
||||
]
|
||||
]);
|
||||
|
||||
list($s1, $s2) = explode(' ', microtime());
|
||||
$timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
|
||||
$stringToSign = $timestamp . "\n" . $DingTalkSecret;
|
||||
$signature = base64_encode(hash_hmac('sha256', $stringToSign, $DingTalkSecret, true));
|
||||
$signature = utf8_encode(urlencode($signature));
|
||||
$DingTalkWebhook .= "×tamp=$timestamp&sign=$signature";
|
||||
$result = file_get_contents($DingTalkWebhook, null, $context);
|
||||
self::logger(__CLASS__, '', $params, $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
61
CommentPush/lib/EnterpriseWeChatService.php
Normal file
61
CommentPush/lib/EnterpriseWeChatService.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* @author stars_kim <stars_kim@163.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
|
||||
class EnterpriseWeChatService extends Service
|
||||
{
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new EnterpriseWeChatService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$isPushBlogger = $plugin->isPushBlogger;
|
||||
if ($comment['authorId'] == 1 && $isPushBlogger == 1) return false;
|
||||
|
||||
|
||||
$EnterpriseWeChatWebhook = $plugin->EnterpriseWeChatWebhook;
|
||||
if (empty($EnterpriseWeChatWebhook)) throw new \Exception('缺少企业微信机器人配置参数');
|
||||
|
||||
$title = $active->title;
|
||||
$author = $comment['author'];
|
||||
$link = $active->permalink;
|
||||
$context = $comment['text'];
|
||||
|
||||
$template = '标题:' . $title . PHP_EOL
|
||||
. '评论人:' . $author . " [{$comment['ip']}]" . PHP_EOL
|
||||
. '评论内容:' . $context . PHP_EOL
|
||||
. '链接:' . $link . '#comment-' . $comment['coid'];
|
||||
|
||||
$params = [
|
||||
'msgtype' => 'text',
|
||||
'text' => [
|
||||
'content' => $template
|
||||
]
|
||||
];
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-Type: application/json;charset=utf-8',
|
||||
'content' => json_encode($params)
|
||||
]
|
||||
]);
|
||||
|
||||
$result = file_get_contents($EnterpriseWeChatWebhook, false, $context);
|
||||
self::logger(__CLASS__, '', $params, $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
4824
CommentPush/lib/Extend/PHPMailer.php
Normal file
4824
CommentPush/lib/Extend/PHPMailer.php
Normal file
File diff suppressed because it is too large
Load Diff
1371
CommentPush/lib/Extend/SMTP.php
Normal file
1371
CommentPush/lib/Extend/SMTP.php
Normal file
File diff suppressed because it is too large
Load Diff
82
CommentPush/lib/OfficialAccountService.php
Normal file
82
CommentPush/lib/OfficialAccountService.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
|
||||
class OfficialAccountService extends Service
|
||||
{
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new OfficialAccountService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$token = $plugin->officialAccountToken;
|
||||
$appId = $plugin->officialAccountAppId;
|
||||
$appSecret = $plugin->officialAccountAppSecret;
|
||||
$openid = $plugin->officialAccountOpenid;
|
||||
$templateId = $plugin->officialAccountTemplateId;
|
||||
if (empty($token) || empty($appId) || empty($appSecret) || empty($openid) || empty($templateId)) throw new \Exception('缺少微信公众号配置参数');
|
||||
|
||||
$accessTokenResult = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}");
|
||||
$accessTokenResult = json_decode($accessTokenResult, true);
|
||||
if ($accessTokenResult == false || isset($r['errcode']) || isset($r['errmsg'])) {
|
||||
self::logger(__CLASS__, '', '', $accessTokenResult);
|
||||
return false;
|
||||
}
|
||||
|
||||
$accessToken = $accessTokenResult['access_token'];
|
||||
|
||||
|
||||
$title = $active->title;
|
||||
$author = $comment['author'];
|
||||
$link = $active->permalink;
|
||||
$context = $comment['text'];
|
||||
|
||||
$params = json_encode([
|
||||
'touser' => $openid,
|
||||
'template_id' => $templateId,
|
||||
'url' => $link . '#comment-' . $comment['coid'],
|
||||
'appid' => $appId,
|
||||
'data' => [
|
||||
'title' => [
|
||||
'value' => $title,
|
||||
'color' => "#173177"
|
||||
],
|
||||
'user' => [
|
||||
'value' => $author,
|
||||
'color' => "#F00"
|
||||
],
|
||||
'ip' => [
|
||||
'value' => $comment['ip'],
|
||||
'color' => "#173177"
|
||||
],
|
||||
'content' => [
|
||||
'value' => $context,
|
||||
'color' => "#3D3D3D"
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-type: application/x-www-form-urlencoded',
|
||||
'content' => urldecode($params)
|
||||
]
|
||||
]);
|
||||
$result = file_get_contents('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $accessToken, false, $context);
|
||||
self::logger(__CLASS__, $openid, $params, $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
61
CommentPush/lib/QQService.php
Normal file
61
CommentPush/lib/QQService.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
|
||||
class QQService extends Service
|
||||
{
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new QQService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$isPushBlogger = $plugin->isPushBlogger;
|
||||
if ($comment['authorId'] == 1 && $isPushBlogger == 1) return false;
|
||||
|
||||
$qqApiUrl = $plugin->qqApiUrl;
|
||||
$receiveQq = $plugin->receiveQq;
|
||||
|
||||
if (empty($qqApiUrl) || empty($receiveQq)) throw new \Exception('缺少Qmsg酱配置参数');
|
||||
|
||||
$title = $active->title;
|
||||
$author = $comment['author'];
|
||||
$link = $active->permalink;
|
||||
$context = $comment['text'];
|
||||
|
||||
$template = '标题:' . $title . PHP_EOL
|
||||
. '评论人:' . $author . " [{$comment['ip']}]" . PHP_EOL
|
||||
. '评论内容:' . $context . PHP_EOL
|
||||
. '链接:' . $link . '#comment-' . $comment['coid'];
|
||||
|
||||
$params = [
|
||||
'qq' => $receiveQq,
|
||||
'msg' => $template
|
||||
];
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-type: application/x-www-form-urlencoded',
|
||||
'content' => http_build_query($params)
|
||||
]
|
||||
]);
|
||||
|
||||
$result = file_get_contents($qqApiUrl, false, $context);
|
||||
self::logger(__CLASS__, $receiveQq, $params, $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
27
CommentPush/lib/Service.php
Normal file
27
CommentPush/lib/Service.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Contract/ServiceInterface.php';
|
||||
|
||||
abstract class Service implements ServiceInterface
|
||||
{
|
||||
abstract public function __handler($active, $comment, $plugin);
|
||||
abstract public static function create();
|
||||
|
||||
public function logger($service, $object, $context, $result, $error = '')
|
||||
{
|
||||
$db = Typecho_Db::get();
|
||||
$db->query($db->insert('table.comment_push')
|
||||
->rows([
|
||||
'service' => $service,
|
||||
'object' => is_array($object) ? json_encode($object, JSON_UNESCAPED_UNICODE) : $object,
|
||||
'context' => is_array($context) ? json_encode($context, JSON_UNESCAPED_UNICODE) : $context,
|
||||
'result' => is_array($result) ? json_encode($result, JSON_UNESCAPED_UNICODE) : $result,
|
||||
'error' => is_array($error) ? json_encode($error, JSON_UNESCAPED_UNICODE) : $error,
|
||||
'time' => time()
|
||||
]));
|
||||
}
|
||||
}
|
||||
176
CommentPush/lib/SmtpService.php
Normal file
176
CommentPush/lib/SmtpService.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
require_once 'Extend/SMTP.php';
|
||||
require_once 'Extend/PHPMailer.php';
|
||||
|
||||
class SmtpService extends Service
|
||||
{
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new SmtpService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$isPushBlogger = $plugin->isPushBlogger;
|
||||
if ($comment['authorId'] == 1 && $isPushBlogger == 1 && !$comment['parent']) return false;
|
||||
|
||||
$isPushCommentReply = $plugin->isPushCommentReply;
|
||||
|
||||
$options = Helper::options();
|
||||
|
||||
$smtpHost = $plugin->smtpHost;
|
||||
$smtpPort = $plugin->smtpPort;
|
||||
$smtpUser = $plugin->smtpUser;
|
||||
$smtpPass = $plugin->smtpPass;
|
||||
$smtpAuth = $plugin->smtpAuth;
|
||||
$smtpSecure = $plugin->smtpSecure;
|
||||
|
||||
$authorTemplate = $plugin->authorTemplate;
|
||||
$replyTemplate = $plugin->replyTemplate;
|
||||
|
||||
$smtpFromAlias = empty($plugin->smtpFromAlias) ? $options->title : $plugin->smtpFromAlias;
|
||||
$toAddress = $comment['mail'];
|
||||
|
||||
|
||||
if (empty($smtpHost) || empty($smtpPort) || empty($smtpUser) || empty($smtpPass) || empty($smtpSecure)) throw new \Exception('缺少SMTP邮件推送配置');
|
||||
|
||||
|
||||
$parentComment = NULL;
|
||||
|
||||
if ($comment['authorId'] != $comment['ownerId']) {
|
||||
$author = self::getWidget('Users', 'uid', $comment['ownerId']);
|
||||
$toAddress = $author->mail;
|
||||
$parentComment = NULL;
|
||||
}
|
||||
|
||||
|
||||
if ($comment['parent'] && $comment['parent'] > 0) {
|
||||
$parentComment = self::getWidget('Comments', 'coid', $comment['parent']);
|
||||
if (isset($parentComment->coid) && $comment['mail'] != $parentComment->mail) {
|
||||
$toAddress = $parentComment->mail;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_null($parentComment) && $isPushCommentReply != 1) return false;
|
||||
|
||||
list($subject, $body) = self::getSubjectAndBody($parentComment, $options, $comment, $active, $authorTemplate, $replyTemplate);
|
||||
|
||||
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->CharSet = "UTF-8";
|
||||
$mail->SMTPDebug = 0;
|
||||
$mail->isSMTP();
|
||||
$mail->Host = $smtpHost;
|
||||
$mail->SMTPAuth = empty($smtpAuth) ? false : $smtpAuth;
|
||||
$mail->Username = $smtpUser;
|
||||
$mail->Password = $smtpPass;
|
||||
$mail->SMTPSecure = $smtpSecure;
|
||||
$mail->Port = $smtpPort;
|
||||
$mail->setFrom($smtpUser, $smtpFromAlias);
|
||||
$mail->addAddress($toAddress);
|
||||
$mail->addReplyTo($smtpUser, $smtpFromAlias);
|
||||
$mail->isHTML(true);
|
||||
$mail->Subject = $subject;
|
||||
$mail->Body = $body;
|
||||
$mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
|
||||
$result = $mail->send();
|
||||
self::logger(__CLASS__, $toAddress, json_encode($mail), $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function getSubjectAndBody($parentComment, $options, $comment, $active, $authorTemplate, $replyTemplate)
|
||||
{
|
||||
|
||||
if (!is_null($parentComment)) {
|
||||
if (is_null($replyTemplate) || empty($replyTemplate)) {
|
||||
$html = file_get_contents(dirname(__DIR__) . '/theme/reply.html');
|
||||
} else {
|
||||
$html = $replyTemplate;
|
||||
}
|
||||
} else {
|
||||
if (is_null($authorTemplate) || empty($authorTemplate)) {
|
||||
$html = file_get_contents(dirname(__DIR__) . '/theme/author.html');
|
||||
} else {
|
||||
$html = $authorTemplate;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*$html = !is_null($parentComment) ?
|
||||
file_get_contents(dirname(__DIR__) . '/theme/reply.html') :
|
||||
file_get_contents(dirname(__DIR__) . '/theme/author.html');*/
|
||||
|
||||
|
||||
$subject = !is_null($parentComment) ?
|
||||
_t('您在 [' . trim($options->title) . '] 的评论有了新的回复!') :
|
||||
_t('您在 [' . trim($options->title) . '] 发表的文章有新评论!');
|
||||
|
||||
|
||||
$body = !is_null($parentComment) ? str_replace(
|
||||
[
|
||||
'{blogUrl}',
|
||||
'{blogName}',
|
||||
'{author}',
|
||||
'{permalink}',
|
||||
'{title}',
|
||||
'{text}',
|
||||
'{replyAuthor}',
|
||||
'{replyText}',
|
||||
'{commentUrl}'
|
||||
],
|
||||
[
|
||||
trim($options->siteUrl),
|
||||
trim($options->title),
|
||||
trim($parentComment->author),
|
||||
trim($active->permalink . '#comment-' . $comment['coid']),
|
||||
trim($active->title),
|
||||
trim($parentComment->text),
|
||||
trim($comment['author']),
|
||||
trim($comment['text']),
|
||||
trim($active->permalink . '#comment-' . $comment['coid'])
|
||||
], $html) : str_replace(
|
||||
[
|
||||
'{blogUrl}',
|
||||
'{blogName}',
|
||||
'{author}',
|
||||
'{permalink}',
|
||||
'{title}',
|
||||
'{text}',
|
||||
'{ip}'
|
||||
],
|
||||
[
|
||||
trim($options->siteUrl),
|
||||
trim($options->title),
|
||||
trim($comment['author']),
|
||||
trim($active->permalink . '#comment-' . $comment['coid']),
|
||||
trim($active->title),
|
||||
trim($comment['text']),
|
||||
trim($comment['ip'])
|
||||
], $html
|
||||
);
|
||||
|
||||
return [$subject, $body];
|
||||
}
|
||||
|
||||
private function getWidget($table, $key, $val)
|
||||
{
|
||||
$className = 'Widget_Abstract_' . $table;
|
||||
$db = Typecho_Db::get();
|
||||
$widget = new $className(Typecho_Request::getInstance(), Typecho_Widget_Helper_Empty::getInstance());
|
||||
$db->fetchRow($widget->select()->where($key . ' = ?', $val)->limit(1), array($widget, 'push'));
|
||||
|
||||
return $widget;
|
||||
}
|
||||
}
|
||||
59
CommentPush/lib/WeChatService.php
Normal file
59
CommentPush/lib/WeChatService.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* @author gaobinzhan <gaobinzhan@gmail.com>
|
||||
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
|
||||
*/
|
||||
|
||||
require_once 'Service.php';
|
||||
|
||||
class WeChatService extends Service
|
||||
{
|
||||
public static function create(){
|
||||
|
||||
static $instance ;
|
||||
if (!$instance){
|
||||
$instance = new WeChatService();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
public function __handler($active, $comment, $plugin)
|
||||
{
|
||||
try {
|
||||
$isPushBlogger = $plugin->isPushBlogger;
|
||||
if ($comment['authorId'] == 1 && $isPushBlogger == 1) return false;
|
||||
|
||||
|
||||
$weChatScKey = $plugin->weChatScKey;
|
||||
if (empty($weChatScKey)) throw new \Exception('缺少Server酱配置参数');
|
||||
|
||||
$title = $active->title;
|
||||
$author = $comment['author'];
|
||||
$link = $active->permalink;
|
||||
$context = $comment['text'];
|
||||
|
||||
$template = '标题:' . $title . PHP_EOL
|
||||
. '评论人:' . $author . " [{$comment['ip']}]" . PHP_EOL
|
||||
. '评论内容:' . $context . PHP_EOL
|
||||
. '链接:' . $link . '#comment-' . $comment['coid'];
|
||||
|
||||
$params = [
|
||||
'text' => '有人给你评论啦!!',
|
||||
'desp' => $template
|
||||
];
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => 'Content-type: application/x-www-form-urlencoded',
|
||||
'content' => http_build_query($params)
|
||||
]
|
||||
]);
|
||||
|
||||
$result = file_get_contents('https://sc.ftqq.com/' . $weChatScKey . '.send', false, $context);
|
||||
self::logger(__CLASS__, '', $params, $result);
|
||||
} catch (\Exception $exception) {
|
||||
self::logger(__CLASS__, '', '', '', $exception->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user