diff --git a/_prepend.php b/_prepend.php index 2047c4b..0631e66 100644 --- a/_prepend.php +++ b/_prepend.php @@ -1,46 +1,11 @@ autoload([ - 'whiteListCom' => __DIR__ . '/inc/Core.php', - 'whiteListComReservedFilter' => __DIR__ . '/inc/ReservedFilter.php', - 'whiteListComModeratedFilter' => __DIR__ . '/inc/ModeratedFilter.php', -]); +$prepend = implode('\\', ['Dotclear', 'Plugin', basename(__DIR__), 'Prepend']); +if (!class_exists($prepend)) { + require implode(DIRECTORY_SEPARATOR, [__DIR__, 'inc', 'Prepend.php']); -dcCore::app()->spamfilters[] = 'whiteListComModeratedFilter'; -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 ($prepend::init()) { + $prepend::process(); } - - 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(); - } -}); +} \ No newline at end of file diff --git a/inc/Core.php b/inc/Core.php index 459f925..ac39578 100644 --- a/inc/Core.php +++ b/inc/Core.php @@ -10,16 +10,21 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); + +namespace Dotclear\Plugin\whiteListCom; + +/* dotclear ns */ +use dcCore; +use dcUtils; + /** * @ingroup DC_PLUGIN_WHITELISTCOM * @brief White list filters methods * @since 2.6 */ -class whiteListCom +class Core { public $con; public $blog; @@ -32,17 +37,17 @@ class 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->settings = dcCore::app()->blog->settings->get(basename(dirname(__DIR__))); + $unmoderated = $this->settings->get('unmoderated'); $this->unmoderated = self::decode($unmoderated); - $reserved = $this->settings->whiteListCom_reserved; + $reserved = $this->settings->get('reserved'); $this->reserved = self::decode($reserved); } public function commit() { $this->settings->put( - 'whiteListCom_unmoderated', + 'unmoderated', self::encode($this->unmoderated), 'string', 'Whitelist of unmoderated users on comments', @@ -51,7 +56,7 @@ class whiteListCom ); $this->settings->put( - 'whiteListCom_reserved', + 'reserved', self::encode($this->reserved), 'string', 'Whitelist of reserved names on comments', @@ -158,12 +163,12 @@ class whiteListCom { $y = is_array($x) ? $x : []; - return base64_encode(serialize($y)); + return json_encode($y); } public static function decode($x) { - $y = @unserialize(@base64_decode($x)); + $y = json_decode($x); return is_array($y) ? $y : []; } diff --git a/inc/Prepend.php b/inc/Prepend.php new file mode 100644 index 0000000..399beda --- /dev/null +++ b/inc/Prepend.php @@ -0,0 +1,80 @@ +autoload([ + __NAMESPACE__ . '\\' . $lib => __DIR__ . DIRECTORY_SEPARATOR . $lib . '.php', + ]); + } + + dcCore::app()->spamfilters[] = 'UnmoderatedWhiteList'; + dcCore::app()->spamfilters[] = 'ReservedWhiteList'; + + 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') == 'UnmoderatedWhiteList' + && $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(); + } + }); + + return true; + } +} + diff --git a/inc/ReservedFilter.php b/inc/ReservedWhiteList.php similarity index 92% rename from inc/ReservedFilter.php rename to inc/ReservedWhiteList.php index 84c87e5..fd78c10 100644 --- a/inc/ReservedFilter.php +++ b/inc/ReservedWhiteList.php @@ -10,9 +10,21 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); + +namespace Dotclear\Plugin\whiteListCom; + +/* dotclear ns */ +use dcCore; +use dcSpamFilter; + +/* clearbricks ns */ +use form; +use html; + +/* php ns */ +use Exception; + /** * @ingroup DC_PLUGIN_WHITELISTCOM @@ -39,7 +51,7 @@ class whiteListComReservedFilter extends dcSpamFilter $throw = false; try { - $wlc = new whiteListCom(); + $wlc = new Core(); if (true === $wlc->isReserved($author, $email)) { $status = 'reserved name'; @@ -64,7 +76,7 @@ class whiteListComReservedFilter extends dcSpamFilter public function gui(string $url): string { - $wlc = new whiteListCom(); + $wlc = new Core(); $comments = []; try { diff --git a/inc/ModeratedFilter.php b/inc/UnmoderatedWhiteList.php similarity index 92% rename from inc/ModeratedFilter.php rename to inc/UnmoderatedWhiteList.php index 4eea963..500c894 100644 --- a/inc/ModeratedFilter.php +++ b/inc/UnmoderatedWhiteList.php @@ -10,9 +10,20 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); + +namespace Dotclear\Plugin\whiteListCom; + +/* dotclear ns */ +use dcCore; +use dcSpamFilter; + +/* clearbricks ns */ +use form; +use html; + +/* php ns */ +use Exception; /** * @ingroup DC_PLUGIN_WHITELISTCOM @@ -21,7 +32,7 @@ if (!defined('DC_RC_PATH')) { * * This filter is used only if comments are moderates */ -class whiteListComModeratedFilter extends dcSpamFilter +class UnmoderatedWhiteList extends dcSpamFilter { public $name = 'Unmoderated authors'; public $has_gui = true; @@ -41,7 +52,7 @@ class whiteListComModeratedFilter extends dcSpamFilter } try { - $wlc = new whiteListCom(); + $wlc = new Core(); if ($wlc->isUnmoderated($email)) { $status = 'unmoderated'; @@ -56,7 +67,7 @@ class whiteListComModeratedFilter extends dcSpamFilter public function gui(string $url): string { - $wlc = new whiteListCom(); + $wlc = new Core(); $posts = $comments = []; try {