From 3e2e840dd96e9408ef5a921edf3456f68a2c3deb Mon Sep 17 00:00:00 2001 From: JcDenis Date: Sat, 21 Aug 2021 00:39:20 +0200 Subject: [PATCH] update license and adopt PSR2 coding style --- CHANGELOG.md | 10 +- README.md | 6 + _define.php | 39 ++- _prepend.php | 20 +- inc/lib.whitelistcom.php | 670 +++++++++++++++++++-------------------- 5 files changed, 370 insertions(+), 375 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8083c96..a4306e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,28 +1,26 @@ +whiteListCom 0.7 - 2021-08-19 + * fix PSR2 coding style + * update license + whiteListCom 0.6 - 2013-11-13 -=========================================================== * Clean up code whiteListCom 0.5 - 2011-01-19 -=========================================================== * Fixed calls to blog object * Fixed (hope so) postgreSQL compatibility * Added messages on admin * New year copyright whiteListCom 0.4 - 2010-06-05 -=========================================================== * Switched to DC 2.2 whiteListCom 0.3 - 2009-11-08 -=========================================================== * Fixed whitout comment_trackback in admin comments list * Fixed typo * Added LICENSE whiteListCom 0.2 - 2009-09-27 -=========================================================== * Changed to antispam system whiteListCom 0.1 - 2009-09-16 -=========================================================== * First lab release \ No newline at end of file diff --git a/README.md b/README.md index 3b38ebf..c6df0b5 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,9 @@ Dotaddict repository. (See Dotclear's documentation to know how do this) Enable and configure "Unmoderated authors" of "Reserved names" from antispam manager. Note: User must write a comment before able to be added to the list. + +## MORE + + * License : GNU GPL v2 + * Source & contribution : [GitHub Page](https://github.com/JcDenis/whiteListCom) + * Packages & details: [Dotaddict Page](https://plugins.dotaddict.org/dc2/details/whiteListCom) diff --git a/_define.php b/_define.php index 33fa590..18e91ac 100644 --- a/_define.php +++ b/_define.php @@ -3,8 +3,7 @@ # # This file is part of whiteListCom, a plugin for Dotclear 2. # -# Copyright (c) 2009-2013 Jean-Christian Denis and contributors -# contact@jcdenis.fr http://jcd.lv +# Copyright (c) 2009-2021 Jean-Christian Denis and contributors # # Licensed under the GPL version 2.0 license. # A copy of this license is available in LICENSE file or at @@ -12,23 +11,21 @@ # # -- END LICENSE BLOCK ------------------------------------ -if (!defined('DC_RC_PATH')){return;} - +if (!defined('DC_RC_PATH')) { + return null; +} + $this->registerModule( - /* Name */ - "Whitelist comments", - /* Description*/ - "Whitelists for comments moderation", - /* Author */ - "Jean-Christian Denis", - /* Version */ - '0.6', - array( - 'permissions' => 'admin', - 'priority' => 200, - 'type' => 'plugin', - 'dc_min' => '2.6', - 'support' => 'http://jcd.lv/q=whiteListCom', - 'details' => 'http://plugins.dotaddict.org/dc2/details/whiteListCom' - ) -); + 'Whitelist comments', + 'Whitelists for comments moderation', + 'Jean-Christian Denis and Contributors', + '0.7', + [ + 'permissions' => 'admin', + 'priority' => 200, + 'type' => 'plugin', + 'dc_min' => '2.18', + 'support' => 'https://github.com/JcDenis/whiteListCom', + 'details' => 'https://plugins.dotaddict.org/dc2/details/whiteListCom' + ] +); \ No newline at end of file diff --git a/_prepend.php b/_prepend.php index 40d963b..6e33310 100644 --- a/_prepend.php +++ b/_prepend.php @@ -3,8 +3,7 @@ # # This file is part of whiteListCom, a plugin for Dotclear 2. # -# Copyright (c) 2009-2013 Jean-Christian Denis and contributors -# contact@jcdenis.fr http://jcd.lv +# Copyright (c) 2009-2021 Jean-Christian Denis and contributors # # Licensed under the GPL version 2.0 license. # A copy of this license is available in LICENSE file or at @@ -13,22 +12,21 @@ # -- END LICENSE BLOCK ------------------------------------ if (!defined('DC_RC_PATH')) { - - return null; + return null; } $d = dirname(__FILE__).'/inc/lib.whitelistcom.php'; -$__autoload['whiteListCom'] = $d; -$__autoload['whiteListComBehaviors'] = $d; -$__autoload['whiteListComReservedFilter'] = $d; -$__autoload['whiteListComModeratedFilter'] = $d; +$__autoload['whiteListCom'] = $d; +$__autoload['whiteListComBehaviors'] = $d; +$__autoload['whiteListComReservedFilter'] = $d; +$__autoload['whiteListComModeratedFilter'] = $d; $core->spamfilters[] = 'whiteListComModeratedFilter'; $core->addBehavior( - 'publicAfterCommentCreate', - array('whiteListComBehaviors', 'switchStatus') + 'publicAfterCommentCreate', + ['whiteListComBehaviors', 'switchStatus'] ); -$core->spamfilters[] = 'whiteListComReservedFilter'; +$core->spamfilters[] = 'whiteListComReservedFilter'; \ No newline at end of file diff --git a/inc/lib.whitelistcom.php b/inc/lib.whitelistcom.php index 5258dd4..36b2824 100644 --- a/inc/lib.whitelistcom.php +++ b/inc/lib.whitelistcom.php @@ -3,8 +3,7 @@ # # This file is part of whiteListCom, a plugin for Dotclear 2. # -# Copyright (c) 2009-2013 Jean-Christian Denis and contributors -# contact@jcdenis.fr http://jcd.lv +# Copyright (c) 2009-2021 Jean-Christian Denis and contributors # # Licensed under the GPL version 2.0 license. # A copy of this license is available in LICENSE file or at @@ -13,8 +12,7 @@ # -- END LICENSE BLOCK ------------------------------------ if (!defined('DC_RC_PATH')) { - - return null; + return null; } /** @@ -26,121 +24,125 @@ if (!defined('DC_RC_PATH')) { */ class whiteListComModeratedFilter extends dcSpamFilter { - public $name = 'Unmoderated authors'; - public $has_gui = true; + public $name = 'Unmoderated authors'; + public $has_gui = true; - protected function setInfo() - { - $this->name = __('Unmoderated authors'); - $this->description = __('Whitelist of unmoderated authors'); - } + 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' - || $this->core->blog === null - || $this->core->blog->settings->system->comments_pub - ) { - return null; - } + public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) + { + if ($type != 'comment' + || $this->core->blog === null + || $this->core->blog->settings->system->comments_pub) { + return null; + } - try { - $wlc = new whiteListCom($this->core); - if ($wlc->isUnmoderated($email)) { - $status = 'unmoderated'; + try { + $wlc = new whiteListCom($this->core); + if ($wlc->isUnmoderated($email)) { + $status = 'unmoderated'; - # return true in order to change comment_status after - return true; - } - else { + # return true in order to change comment_status after + return true; + } else { - return null; - } - } - catch (Exception $e) {} - } + return null; + } + } catch (Exception $e) { - public function gui($url) - { - try { - $wlc = new whiteListCom($this->core); + } + } - 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) { - $this->core->error->add($e->getMessage()); - } + public function gui($url) + { + try { + $wlc = new whiteListCom($this->core); - $res = ''; + 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) { + $this->core->error->add($e->getMessage()); + } - if ($this->core->blog->settings->system->comments_pub) { - $res .= - '

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

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

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

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

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

'. - ''. - ''. - ''; + if ($this->core->blog->settings->system->comments_pub) { + $res .= + '

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

'; + } - foreach($posts as $user) { - $res .= - ''. - ''. - ''. - ''; - } + $res .= + '' . + '

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

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

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

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

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

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

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

' . + '' . + '' . + ''; - $res .= - ''. - '
' . __('Author') . '' . __('Email') . '
'. - '
'. - '
'. - '

'. - $this->core->formNonce().'

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

' . + $this->core->formNonce() . '

' . + ''; + + return $res; + } } /** @@ -150,95 +152,95 @@ class whiteListComModeratedFilter extends dcSpamFilter */ class whiteListComReservedFilter extends dcSpamFilter { - public $name = 'Reserved names'; - public $has_gui = true; + public $name = 'Reserved names'; + public $has_gui = true; - protected function setInfo() - { - $this->name = __('Reserved names'); - $this->description = __('Whitelist of reserved names of users'); - } + 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') { + public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) + { + if ($type != 'comment') { + return null; + } - return null; - } + $throw = false; + try { + $wlc = new whiteListCom($this->core); - $throw = false; - try { - $wlc = new whiteListCom($this->core); + if (true === $wlc->isReserved($author, $email)) { + $status = 'reserved name'; + //return true; + $throw = true; + } else { + return null; + } + } catch (Exception $e) { - 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.')); + } + } - # 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 getStatusMessage($status,$comment_id) - { - return __('This name is reserved to an other user.'); - } + public function gui($url) + { + try { + $wlc = new whiteListCom($this->core); - public function gui($url) - { - try { - $wlc = new whiteListCom($this->core); + 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) { + $this->core->error->add($e->getMessage()); + } - 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) { - $this->core->error->add($e->getMessage()); - } + $res = + '
' . + '

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

' . + '

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

' . + '' . + '' . + ''; - $res = - ''. - '

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

'. - '

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

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

' . + $this->core->formNonce() . '

' . + '
'; - $res .= - ''. - ''. - '

'. - $this->core->formNonce().'

'. - ''; - - return $res; - } + return $res; + } } /** @@ -248,159 +250,155 @@ class whiteListComReservedFilter extends dcSpamFilter */ class whiteListCom { - public $core; - public $con; - public $blog; - public $settings; + public $core; + public $con; + public $blog; + public $settings; - private $unmoderated = array(); - private $reserved = array(); + private $unmoderated = array(); + private $reserved = array(); - public function __construct($core) - { - $this->core = $core; - $this->con = $core->con; - $this->blog = $core->con->escape($core->blog->id); - - $core->blog->settings->addNamespace('whiteListCom'); - $this->settings = $core->blog->settings->whiteListCom; - - $unmoderated = $this->settings->whiteListCom_unmoderated; - $this->unmoderated = self::decode($unmoderated); - - $reserved = $this->settings->whiteListCom_reserved; - $this->reserved = self::decode($reserved); - } + public function __construct($core) + { + $this->core = $core; + $this->con = $core->con; + $this->blog = $core->con->escape($core->blog->id); + + $core->blog->settings->addNamespace('whiteListCom'); + $this->settings = $core->blog->settings->whiteListCom; + + $unmoderated = $this->settings->whiteListCom_unmoderated; + $this->unmoderated = self::decode($unmoderated); + + $reserved = $this->settings->whiteListCom_reserved; + $this->reserved = self::decode($reserved); + } - public function commit() - { - $this->settings->put( - 'whiteListCom_unmoderated', - self::encode($this->unmoderated), - 'string', - 'Whitelist of unmoderated users on comments', - true,false - ); - - $this->settings->put( - 'whiteListCom_reserved', - self::encode($this->reserved), - 'string', - 'Whitelist of reserved names on comments', - true,false - ); - } + public function commit() + { + $this->settings->put( + 'whiteListCom_unmoderated', + self::encode($this->unmoderated), + 'string', + 'Whitelist of unmoderated users on comments', + true,false + ); + + $this->settings->put( + 'whiteListCom_reserved', + self::encode($this->reserved), + 'string', + 'Whitelist of reserved names on comments', + true,false + ); + } - # Return - # true if it is a reserved name with wrong email - # false if it is not a reserved name - # null if it is a reserved name with right email - public function isReserved($author, $email) - { - if (!isset($this->reserved[$author])) { - return false; - } - elseif ($this->reserved[$author] != $email) { + # Return + # true if it is a reserved name with wrong email + # false if it is not a reserved name + # null if it is a reserved name with right email + public function isReserved($author, $email) + { + if (!isset($this->reserved[$author])) { + return false; + } elseif ($this->reserved[$author] != $email) { + return true; + } else { + return null; + } + } - return true; - } - else { + # You must do a commit to save this change + public function addReserved($author, $email) + { + $this->reserved[$author] = $email; + return true; + } - return null; - } - } + # You must do a commit to save this change + public function emptyReserved() + { + $this->reserved = array(); + } - # You must do a commit to save this change - public function addReserved($author, $email) - { - $this->reserved[$author] = $email; - return true; - } + # Return + # true if it is known as an unmoderated email else false + public function isUnmoderated($email) + { + return in_array($email, $this->unmoderated); + } - # You must do a commit to save this change - public function emptyReserved() - { - $this->reserved = array(); - } + # You must do a commit to save this change + public function addUnmoderated($email) + { + if (!in_array($email,$this->unmoderated)) { + $this->unmoderated[] = $email; - # Return - # true if it is known as an unmoderated email else false - public function isUnmoderated($email) - { - return in_array($email, $this->unmoderated); - } + return true; + } - # You must do a commit to save this change - public function addUnmoderated($email) - { - if (!in_array($email,$this->unmoderated)) { - $this->unmoderated[] = $email; + return null; + } - return true; - } + # You must do a commit to save this change + public function emptyUnmoderated() + { + $this->unmoderated = array(); + } - return null; - } + public function getPostsUsers() + { + $users = array(); + $rs = $this->core->blog->getPostsUsers(); + while($rs->fetch()) { + $name = dcUtils::getUserCN( + $rs->user_id, + $rs->user_name, + $rs->user_firstname, + $rs->user_displayname + ); + $users[] = array( + 'name' => $name, + 'email' => $rs->user_email + ); + } - # You must do a commit to save this change - public function emptyUnmoderated() - { - $this->unmoderated = array(); - } + return $users; + } - public function getPostsUsers() - { - $users = array(); - $rs = $this->core->blog->getPostsUsers(); - while($rs->fetch()) { - $name = dcUtils::getUserCN( - $rs->user_id, - $rs->user_name, - $rs->user_firstname, - $rs->user_displayname - ); - $users[] = array( - 'name' => $name, - 'email' => $rs->user_email - ); - } + public function getCommentsUsers() + { + $users = array(); + $rs = $this->con->select( + 'SELECT comment_author, comment_email ' . + 'FROM ' . $this->core->prefix . 'comment C ' . + 'LEFT JOIN ' . $this->core->prefix . 'post P ON C.post_id=P.post_id ' . + "WHERE blog_id='" . $this->blog . "' AND comment_trackback=0 " . + 'GROUP BY comment_email, comment_author ' // Added author to fix postgreSql + ); + while($rs->fetch()) { + $users[] = array( + 'name' => $rs->comment_author, + 'email' => $rs->comment_email + ); + } - return $users; - } + return $users; + } - public function getCommentsUsers() - { - $users = array(); - $rs = $this->con->select( - 'SELECT comment_author, comment_email '. - 'FROM '.$this->core->prefix.'comment C '. - 'LEFT JOIN '.$this->core->prefix.'post P ON C.post_id=P.post_id '. - "WHERE blog_id='".$this->blog."' AND comment_trackback=0 ". - 'GROUP BY comment_email, comment_author ' // Added author to fix postgreSql - ); - while($rs->fetch()) { - $users[] = array( - 'name' => $rs->comment_author, - 'email' => $rs->comment_email - ); - } + public static function encode($x) + { + $y = is_array($x) ? $x : array(); - return $users; - } + return base64_encode(serialize($y)); + } - public static function encode($x) - { - $y = is_array($x) ? $x : array(); + public static function decode($x) + { + $y = @unserialize(@base64_decode($x)); - return base64_encode(serialize($y)); - } - - public static function decode($x) - { - $y = @unserialize(@base64_decode($x)); - - return is_array($y) ? $y : array(); - } + return is_array($y) ? $y : array(); + } } /** @@ -410,31 +408,29 @@ class whiteListCom */ class whiteListComBehaviors { - # from behavior publicAfterCommentCreate - public static function switchStatus($cur,$id) - { - global $core; + # from behavior publicAfterCommentCreate + public static function switchStatus($cur, $id) + { + global $core; - if ($core->blog === null - || $core->blog->settings->system->comments_pub - ) { - return null; - } + if ($core->blog === null + || $core->blog->settings->system->comments_pub) { + return null; + } - if ($cur->comment_spam_filter == 'whiteListComModeratedFilter' - && $cur->comment_spam_status == 'unmoderated' - ) { - $core->con->writeLock($core->prefix.'comment'); + if ($cur->comment_spam_filter == 'whiteListComModeratedFilter' + && $cur->comment_spam_status == 'unmoderated') { + $core->con->writeLock($core->prefix.'comment'); - $cur->comment_status = 1; - $cur->comment_spam_status = 0; - $cur->comment_spam_filter = 0; - $cur->update('WHERE comment_id = '.$id.' '); + $cur->comment_status = 1; + $cur->comment_spam_status = 0; + $cur->comment_spam_filter = 0; + $cur->update('WHERE comment_id = ' . $id . ' '); - $core->con->unlock(); + $core->con->unlock(); - $core->blog->triggerComment($id); - $core->blog->triggerBlog(); - } - } -} + $core->blog->triggerComment($id); + $core->blog->triggerBlog(); + } + } +} \ No newline at end of file