Skip to content

Commit

Permalink
fix:CommentPush支持php8 typecho-fans#115
Browse files Browse the repository at this point in the history
  • Loading branch information
chengs2035 committed Feb 1, 2021
1 parent 136fae2 commit bd29a6b
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CommentPush/Plugin.php
Expand Up @@ -353,6 +353,9 @@ public static function pushServiceReady($comment, $active)
return $comment;
}

/**
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
*/
public static function pushServiceGo($comment)
{
$options = Helper::options();
Expand All @@ -366,6 +369,9 @@ public static function pushServiceGo($comment)
self::$comment['coid'] = $comment->coid;

/** @var QQService | WeChatService | AliYunEmailService | SmtpService | DingTalkBotService | EnterpriseWeChatService $service */
foreach ($services as $service) call_user_func([$service, '__handler'], self::$active, self::$comment, $plugin);
foreach ($services as $service){
$inst=call_user_func([$service,'create']);
call_user_func([$inst, '__handler'], self::$active, self::$comment, $plugin);
}
}
}
10 changes: 9 additions & 1 deletion CommentPush/lib/AliYunEmailService.php
@@ -1,6 +1,7 @@
<?php
/**
* @author gaobinzhan <gaobinzhan@gmail.com>
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
*/

require_once 'Service.php';
Expand Down Expand Up @@ -29,7 +30,14 @@ class AliYunEmailService extends Service
'version' => '2017-06-22'
]
];

public static function create(){

static $instance ;
if (!$instance){
$instance = new AliYunEmailService();
}
return $instance;
}
public function __handler($active, $comment, $plugin)
{
try {
Expand Down
9 changes: 9 additions & 0 deletions CommentPush/lib/DingTalkBotService.php
@@ -1,12 +1,21 @@
<?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 {
Expand Down
9 changes: 9 additions & 0 deletions CommentPush/lib/EnterpriseWeChatService.php
@@ -1,12 +1,21 @@
<?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 {
Expand Down
9 changes: 9 additions & 0 deletions CommentPush/lib/OfficialAccountService.php
@@ -1,12 +1,21 @@
<?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 {
Expand Down
9 changes: 9 additions & 0 deletions CommentPush/lib/QQService.php
@@ -1,12 +1,21 @@
<?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 {
Expand Down
2 changes: 2 additions & 0 deletions CommentPush/lib/Service.php
@@ -1,13 +1,15 @@
<?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 = '')
{
Expand Down
9 changes: 9 additions & 0 deletions CommentPush/lib/SmtpService.php
@@ -1,6 +1,7 @@
<?php
/**
* @author gaobinzhan <gaobinzhan@gmail.com>
* @modify 小码农 <chengshongguo@qq.com> 增加实例化方法
*/

require_once 'Service.php';
Expand All @@ -9,6 +10,14 @@

class SmtpService extends Service
{
public static function create(){

static $instance ;
if (!$instance){
$instance = new SmtpService();
}
return $instance;
}
public function __handler($active, $comment, $plugin)
{
try {
Expand Down
167 changes: 167 additions & 0 deletions CommentPush/lib/SmtpService.sync-conflict-20210201-052039-BKQBY4R.php
@@ -0,0 +1,167 @@
<?php
/**
* @author gaobinzhan <gaobinzhan@gmail.com>
*/

require_once 'Service.php';
require_once 'Extend/SMTP.php';
require_once 'Extend/PHPMailer.php';

class SmtpService extends Service
{
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;
}
}
9 changes: 9 additions & 0 deletions CommentPush/lib/WeChatService.php
@@ -1,12 +1,21 @@
<?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 {
Expand Down

0 comments on commit bd29a6b

Please sign in to comment.