From 88e2d6c3b2291ef1f8094d8dfda70e10a32f0087 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 7 Jan 2023 23:28:40 +0100 Subject: [PATCH] split file --- _prepend.php | 37 ++++-- inc/Core.php | 277 ++------------------------------------- inc/ModeratedFilter.php | 138 +++++++++++++++++++ inc/ReservedFilter.php | 111 ++++++++++++++++ locales/fr/main.lang.php | 22 ++++ 5 files changed, 308 insertions(+), 277 deletions(-) create mode 100644 inc/ModeratedFilter.php create mode 100644 inc/ReservedFilter.php create mode 100644 locales/fr/main.lang.php diff --git a/_prepend.php b/_prepend.php index 728e206..2047c4b 100644 --- a/_prepend.php +++ b/_prepend.php @@ -14,20 +14,33 @@ if (!defined('DC_RC_PATH')) { return null; } -$d = __DIR__ . '/inc/lib.whitelistcom.php'; - Clearbricks::lib()->autoload([ - 'whiteListCom' => $d, - 'whiteListComBehaviors' => $d, - 'whiteListComReservedFilter' => $d, - 'whiteListComModeratedFilter' => $d, + 'whiteListCom' => __DIR__ . '/inc/Core.php', + 'whiteListComReservedFilter' => __DIR__ . '/inc/ReservedFilter.php', + 'whiteListComModeratedFilter' => __DIR__ . '/inc/ModeratedFilter.php', ]); dcCore::app()->spamfilters[] = 'whiteListComModeratedFilter'; - -dcCore::app()->addBehavior( - 'publicAfterCommentCreate', - ['whiteListComBehaviors', 'switchStatus'] -); - dcCore::app()->spamfilters[] = 'whiteListComReservedFilter'; + +dcCore::app()->addBehavior('publicAfterCommentCreate', function ($cur, $id) { + if (dcCore::app()->blog === null + || dcCore::app()->blog->settings->get('system')->get('comments_pub')) { + return null; + } + + if ($cur->__get('comment_spam_filter') == 'whiteListComModeratedFilter' + && $cur->__get('comment_spam_status') == 'unmoderated') { + dcCore::app()->con->writeLock(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME); + + $cur->__set('comment_status', 1); + $cur->__set('comment_spam_status', 0); + $cur->__set('comment_spam_filter', 0); + $cur->update('WHERE comment_id = ' . $id . ' '); + + dcCore::app()->con->unlock(); + + dcCore::app()->blog->triggerComment($id); + dcCore::app()->blog->triggerBlog(); + } +}); diff --git a/inc/Core.php b/inc/Core.php index 2a7fc0b..459f925 100644 --- a/inc/Core.php +++ b/inc/Core.php @@ -14,223 +14,6 @@ if (!defined('DC_RC_PATH')) { return null; } -/** - * @ingroup DC_PLUGIN_WHITELISTCOM - * @brief Filter for unmoderated authors. - * @since 2.6 - * - * This filter is used only if comments are moderates - */ -class whiteListComModeratedFilter extends dcSpamFilter -{ - public $name = 'Unmoderated authors'; - public $has_gui = true; - - protected function setInfo() - { - $this->name = __('Unmoderated authors'); - $this->description = __('Whitelist of unmoderated authors'); - } - - public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) - { - if ($type != 'comment' - || dcCore::app()->blog === null - || dcCore::app()->blog->settings->system->comments_pub) { - return null; - } - - try { - $wlc = new whiteListCom(); - if ($wlc->isUnmoderated($email)) { - $status = 'unmoderated'; - - # return true in order to change comment_status after - return true; - } - - return null; - } catch (Exception $e) { - } - } - - public function gui(string $url): string - { - try { - $wlc = new whiteListCom(); - - if (!empty($_POST['update_unmoderated'])) { - $wlc->emptyUnmoderated(); - foreach ($_POST['unmoderated'] as $email) { - $wlc->addUnmoderated($email); - } - $wlc->commit(); - } - $posts = $wlc->getPostsUsers(); - $comments = $wlc->getCommentsUsers(); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - } - - $res = ''; - - if (dcCore::app()->blog->settings->system->comments_pub) { - $res .= '

' . - __('This filter is used only if comments are moderates') . - '

'; - } - - $res .= '
' . - '

' . __('Check the users who can make comments without being moderated.') . '

' . - '
' . - '
' . - '

' . __('Posts authors list') . '

' . - '' . - '' . - ''; - - foreach ($posts as $user) { - $res .= '' . - '' . - '' . - ''; - } - - $res .= '' . - '
' . __('Name') . '' . __('Email') . '
' . - form::checkbox( - ['unmoderated[]'], - $user['email'], - $wlc->isUnmoderated($user['email']) - ) . - ' ' . $user['name'] . '' . $user['email'] . '
' . - '
' . - '
' . - '

' . __('Comments authors list') . '

' . - '' . - '' . - ''; - - foreach ($comments as $user) { - $res .= '' . - '' . - '' . - ''; - } - - $res .= '' . - '
' . __('Author') . '' . __('Email') . '
' . - form::checkbox( - ['unmoderated[]'], - $user['email'], - $wlc->isUnmoderated($user['email']) - ) . - ' ' . $user['name'] . '' . $user['email'] . '
' . - '
' . - '
' . - '

' . - dcCore::app()->formNonce() . '

' . - '
'; - - return $res; - } -} - -/** - * @ingroup DC_PLUGIN_WHITELISTCOM - * @brief Filter for reserved names. - * @since 2.6 - */ -class whiteListComReservedFilter extends dcSpamFilter -{ - public $name = 'Reserved names'; - public $has_gui = true; - - protected function setInfo() - { - $this->name = __('Reserved names'); - $this->description = __('Whitelist of reserved names of users'); - } - - public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) - { - if ($type != 'comment') { - return null; - } - - $throw = false; - - try { - $wlc = new whiteListCom(); - - if (true === $wlc->isReserved($author, $email)) { - $status = 'reserved name'; - //return true; - $throw = true; - } else { - return null; - } - } catch (Exception $e) { - } - - # This message is show to author even if comments are moderated, comment is not saved - if ($throw) { - throw new Exception(__('This name is reserved to an other user.')); - } - } - - public function getStatusMessage($status, $comment_id) - { - return __('This name is reserved to an other user.'); - } - - public function gui(string $url): string - { - try { - $wlc = new whiteListCom(); - - if (!empty($_POST['update_reserved'])) { - $wlc->emptyReserved(); - foreach ($_POST['reserved'] as $email => $name) { - $wlc->addReserved($name, $email); - } - $wlc->commit(); - } - $comments = $wlc->getCommentsUsers(); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - } - - $res = '
' . - '

' . __('Check the users who can make comments without being moderated.') . '

' . - '

' . __('Comments authors list') . '

' . - '' . - '' . - ''; - - foreach ($comments as $user) { - $res .= '' . - '' . - '' . - ''; - } - - $res .= '' . - '
' . __('Author') . '' . __('Email') . '
' . - form::checkbox( - ['reserved[' . $user['email'] . ']'], - $user['name'], - (null === $wlc->isReserved($user['name'], $user['email'])) - ) . - ' ' . $user['name'] . '' . $user['email'] . '
' . - '

' . - dcCore::app()->formNonce() . '

' . - '
'; - - return $res; - } -} - /** * @ingroup DC_PLUGIN_WHITELISTCOM * @brief White list filters methods @@ -247,17 +30,13 @@ class whiteListCom public function __construct() { - $this->con = dcCore::app()->con; - $this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id); - - dcCore::app()->blog->settings->addNamespace('whiteListCom'); - $this->settings = dcCore::app()->blog->settings->whiteListCom; - + $this->con = dcCore::app()->con; + $this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id); + $this->settings = dcCore::app()->blog->settings->whiteListCom; $unmoderated = $this->settings->whiteListCom_unmoderated; $this->unmoderated = self::decode($unmoderated); - - $reserved = $this->settings->whiteListCom_reserved; - $this->reserved = self::decode($reserved); + $reserved = $this->settings->whiteListCom_reserved; + $this->reserved = self::decode($reserved); } public function commit() @@ -341,14 +120,14 @@ class whiteListCom $rs = dcCore::app()->blog->getPostsUsers(); while ($rs->fetch()) { $name = dcUtils::getUserCN( - $rs->user_id, - $rs->user_name, - $rs->user_firstname, - $rs->user_displayname + $rs->__get('user_id'), + $rs->__get('user_name'), + $rs->__get('user_firstname'), + $rs->__get('user_displayname') ); $users[] = [ 'name' => $name, - 'email' => $rs->user_email, + 'email' => $rs->__get('user_email'), ]; } @@ -367,8 +146,8 @@ class whiteListCom ); while ($rs->fetch()) { $users[] = [ - 'name' => $rs->comment_author, - 'email' => $rs->comment_email, + 'name' => $rs->__get('comment_author'), + 'email' => $rs->__get('comment_email'), ]; } @@ -389,35 +168,3 @@ class whiteListCom return is_array($y) ? $y : []; } } - -/** - * @ingroup DC_PLUGIN_WHITELISTCOM - * @brief White list behaviors - * @since 2.6 - */ -class whiteListComBehaviors -{ - # from behavior publicAfterCommentCreate - public static function switchStatus($cur, $id) - { - if (dcCore::app()->blog === null - || dcCore::app()->blog->settings->system->comments_pub) { - return null; - } - - if ($cur->comment_spam_filter == 'whiteListComModeratedFilter' - && $cur->comment_spam_status == 'unmoderated') { - dcCore::app()->con->writeLock(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME); - - $cur->comment_status = 1; - $cur->comment_spam_status = 0; - $cur->comment_spam_filter = 0; - $cur->update('WHERE comment_id = ' . $id . ' '); - - dcCore::app()->con->unlock(); - - dcCore::app()->blog->triggerComment($id); - dcCore::app()->blog->triggerBlog(); - } - } -} diff --git a/inc/ModeratedFilter.php b/inc/ModeratedFilter.php new file mode 100644 index 0000000..4eea963 --- /dev/null +++ b/inc/ModeratedFilter.php @@ -0,0 +1,138 @@ +name = __('Unmoderated authors'); + $this->description = __('Whitelist of unmoderated authors'); + } + + public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) + { + if ($type != 'comment' + || dcCore::app()->blog === null + || dcCore::app()->blog->settings->system->comments_pub) { + return null; + } + + try { + $wlc = new whiteListCom(); + if ($wlc->isUnmoderated($email)) { + $status = 'unmoderated'; + + # return true in order to change comment_status after + return true; + } + + return null; + } catch (Exception $e) { + } + } + + public function gui(string $url): string + { + $wlc = new whiteListCom(); + $posts = $comments = []; + + try { + if (!empty($_POST['update_unmoderated'])) { + $wlc->emptyUnmoderated(); + foreach ($_POST['unmoderated'] as $email) { + $wlc->addUnmoderated($email); + } + $wlc->commit(); + } + $posts = $wlc->getPostsUsers(); + $comments = $wlc->getCommentsUsers(); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + $res = ''; + + if (dcCore::app()->blog->settings->system->comments_pub) { + $res .= '

' . + __('This filter is used only if comments are moderates') . + '

'; + } + + $res .= '
' . + '

' . __('Check the users who can make comments without being moderated.') . '

' . + '
' . + '
' . + '

' . __('Posts authors list') . '

' . + '' . + '' . + ''; + + foreach ($posts as $user) { + $res .= '' . + '' . + '' . + ''; + } + + $res .= '' . + '
' . __('Name') . '' . __('Email') . '
' . + form::checkbox( + ['unmoderated[]'], + $user['email'], + $wlc->isUnmoderated($user['email']) + ) . + ' ' . $user['name'] . '' . $user['email'] . '
' . + '
' . + '
' . + '

' . __('Comments authors list') . '

' . + '' . + '' . + ''; + + foreach ($comments as $user) { + $res .= '' . + '' . + '' . + ''; + } + + $res .= '' . + '
' . __('Author') . '' . __('Email') . '
' . + form::checkbox( + ['unmoderated[]'], + $user['email'], + $wlc->isUnmoderated($user['email']) + ) . + ' ' . $user['name'] . '' . $user['email'] . '
' . + '
' . + '
' . + '

' . + dcCore::app()->formNonce() . '

' . + '
'; + + return $res; + } +} diff --git a/inc/ReservedFilter.php b/inc/ReservedFilter.php new file mode 100644 index 0000000..84c87e5 --- /dev/null +++ b/inc/ReservedFilter.php @@ -0,0 +1,111 @@ +name = __('Reserved names'); + $this->description = __('Whitelist of reserved names of users'); + } + + public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) + { + if ($type != 'comment') { + return null; + } + + $throw = false; + + try { + $wlc = new whiteListCom(); + + if (true === $wlc->isReserved($author, $email)) { + $status = 'reserved name'; + //return true; + $throw = true; + } else { + return null; + } + } catch (Exception $e) { + } + + # This message is show to author even if comments are moderated, comment is not saved + if ($throw) { + throw new Exception(__('This name is reserved to an other user.')); + } + } + + public function getStatusMessage($status, $comment_id) + { + return __('This name is reserved to an other user.'); + } + + public function gui(string $url): string + { + $wlc = new whiteListCom(); + $comments = []; + + try { + if (!empty($_POST['update_reserved'])) { + $wlc->emptyReserved(); + foreach ($_POST['reserved'] as $email => $name) { + $wlc->addReserved($name, $email); + } + $wlc->commit(); + } + $comments = $wlc->getCommentsUsers(); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + $res = '
' . + '

' . __('Check the users who can make comments without being moderated.') . '

' . + '

' . __('Comments authors list') . '

' . + '' . + '' . + ''; + + foreach ($comments as $user) { + $res .= '' . + '' . + '' . + ''; + } + + $res .= '' . + '
' . __('Author') . '' . __('Email') . '
' . + form::checkbox( + ['reserved[' . $user['email'] . ']'], + $user['name'], + (null === $wlc->isReserved($user['name'], $user['email'])) + ) . + ' ' . $user['name'] . '' . $user['email'] . '
' . + '

' . + dcCore::app()->formNonce() . '

' . + '
'; + + return $res; + } +} diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php new file mode 100644 index 0000000..720847b --- /dev/null +++ b/locales/fr/main.lang.php @@ -0,0 +1,22 @@ +