From 71fde26ae64affe9d587541e2714b12783289404 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Wed, 16 Nov 2022 22:22:48 +0100 Subject: [PATCH] prepare to DC 2.24 --- _define.php | 25 +++-- _prepend.php | 23 +++-- inc/lib.whitelistcom.php | 193 ++++++++++++++++++--------------------- locales/fr/main.lang.php | 39 -------- 4 files changed, 113 insertions(+), 167 deletions(-) delete mode 100644 locales/fr/main.lang.php diff --git a/_define.php b/_define.php index 43a0482..7c2a64b 100644 --- a/_define.php +++ b/_define.php @@ -1,16 +1,15 @@ registerModule( 'Whitelist comments', 'Whitelists for comments moderation', 'Jean-Christian Denis and Contributors', - '0.8', + '0.9', [ - 'requires' => [['core', '2.19']], - 'permissions' => 'admin', - 'priority' => 200, - 'type'=> 'plugin', - 'support'=> 'https://github.com/JcDenis/whiteListCom', - 'details'=> 'https://plugins.dotaddict.org/dc2/details/whiteListCom', - 'repository' => 'https://raw.githubusercontent.com/JcDenis/whiteListCom/master/dcstore.xml' + 'requires' => [['core', '2.24']], + 'permissions' => dcAuth::PERMISSION_ADMIN, + 'priority' => 200, + 'type' => 'plugin', + 'support' => 'https://github.com/JcDenis/whiteListCom', + 'details' => 'https://plugins.dotaddict.org/dc2/details/whiteListCom', + 'repository' => 'https://raw.githubusercontent.com/JcDenis/whiteListCom/master/dcstore.xml', ] -); \ No newline at end of file +); diff --git a/_prepend.php b/_prepend.php index efd235f..f5a91a7 100644 --- a/_prepend.php +++ b/_prepend.php @@ -1,32 +1,31 @@ autoload(['whiteListCom' => $d]); +Clearbricks::lib()->autoload(['whiteListComBehaviors' => $d]); +Clearbricks::lib()->autoload(['whiteListComReservedFilter' => $d]); +Clearbricks::lib()->autoload(['whiteListComModeratedFilter' => $d]); -$core->spamfilters[] = 'whiteListComModeratedFilter'; +dcCore::app()->spamfilters[] = 'whiteListComModeratedFilter'; -$core->addBehavior( +dcCore::app()->addBehavior( 'publicAfterCommentCreate', ['whiteListComBehaviors', 'switchStatus'] ); -$core->spamfilters[] = 'whiteListComReservedFilter'; \ No newline at end of file +dcCore::app()->spamfilters[] = 'whiteListComReservedFilter'; diff --git a/inc/lib.whitelistcom.php b/inc/lib.whitelistcom.php index 43e9eae..2a7fc0b 100644 --- a/inc/lib.whitelistcom.php +++ b/inc/lib.whitelistcom.php @@ -1,16 +1,15 @@ name = __('Unmoderated authors'); + $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) { + || dcCore::app()->blog === null + || dcCore::app()->blog->settings->system->comments_pub) { return null; } try { - $wlc = new whiteListCom($this->core); + $wlc = new whiteListCom(); if ($wlc->isUnmoderated($email)) { $status = 'unmoderated'; # return true in order to change comment_status after - return true; - } else { - - return null; + return true; } - } catch (Exception $e) { + return null; + } catch (Exception $e) { } } - public function gui($url) + public function gui(string $url): string { try { - $wlc = new whiteListCom($this->core); + $wlc = new whiteListCom(); if (!empty($_POST['update_unmoderated'])) { $wlc->emptyUnmoderated(); - foreach($_POST['unmoderated'] as $email) { + foreach ($_POST['unmoderated'] as $email) { $wlc->addUnmoderated($email); } $wlc->commit(); } - $posts = $wlc->getPostsUsers(); + $posts = $wlc->getPostsUsers(); $comments = $wlc->getCommentsUsers(); } catch (Exception $e) { - $this->core->error->add($e->getMessage()); + dcCore::app()->error->add($e->getMessage()); } $res = ''; - if ($this->core->blog->settings->system->comments_pub) { - $res .= - '

' . + if (dcCore::app()->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.') . '

' . '
' . '
' . @@ -94,12 +89,11 @@ class whiteListComModeratedFilter extends dcSpamFilter '' . __('Name') . '' . __('Email') . '' . ''; - foreach($posts as $user) { - $res .= - '' . + foreach ($posts as $user) { + $res .= '' . '' . form::checkbox( - array('unmoderated[]'), + ['unmoderated[]'], $user['email'], $wlc->isUnmoderated($user['email']) ) . @@ -108,8 +102,7 @@ class whiteListComModeratedFilter extends dcSpamFilter ''; } - $res .= - '' . + $res .= '' . '' . '
' . '
' . @@ -118,12 +111,11 @@ class whiteListComModeratedFilter extends dcSpamFilter '' . __('Author') . '' . __('Email') . '' . ''; - foreach($comments as $user) { - $res .= - '' . + foreach ($comments as $user) { + $res .= '' . '' . form::checkbox( - array('unmoderated[]'), + ['unmoderated[]'], $user['email'], $wlc->isUnmoderated($user['email']) ) . @@ -132,13 +124,12 @@ class whiteListComModeratedFilter extends dcSpamFilter ''; } - $res .= - '' . + $res .= '' . '' . '
' . '
' . '

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

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

' . '
'; return $res; @@ -152,12 +143,12 @@ class whiteListComModeratedFilter extends dcSpamFilter */ class whiteListComReservedFilter extends dcSpamFilter { - public $name = 'Reserved names'; + public $name = 'Reserved names'; public $has_gui = true; protected function setInfo() { - $this->name = __('Reserved names'); + $this->name = __('Reserved names'); $this->description = __('Whitelist of reserved names of users'); } @@ -168,8 +159,9 @@ class whiteListComReservedFilter extends dcSpamFilter } $throw = false; + try { - $wlc = new whiteListCom($this->core); + $wlc = new whiteListCom(); if (true === $wlc->isReserved($author, $email)) { $status = 'reserved name'; @@ -179,11 +171,10 @@ class whiteListComReservedFilter extends dcSpamFilter return null; } } catch (Exception $e) { - } # This message is show to author even if comments are moderated, comment is not saved - if($throw) { + if ($throw) { throw new Exception(__('This name is reserved to an other user.')); } } @@ -193,37 +184,35 @@ class whiteListComReservedFilter extends dcSpamFilter return __('This name is reserved to an other user.'); } - public function gui($url) + public function gui(string $url): string { try { - $wlc = new whiteListCom($this->core); + $wlc = new whiteListCom(); if (!empty($_POST['update_reserved'])) { $wlc->emptyReserved(); - foreach($_POST['reserved'] as $email => $name) { + foreach ($_POST['reserved'] as $email => $name) { $wlc->addReserved($name, $email); } $wlc->commit(); } $comments = $wlc->getCommentsUsers(); } catch (Exception $e) { - $this->core->error->add($e->getMessage()); + dcCore::app()->error->add($e->getMessage()); } - $res = - '
' . + $res = '' . '

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

' . '

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

' . '' . '' . ''; - foreach($comments as $user) { - $res .= - '' . + foreach ($comments as $user) { + $res .= '' . ''; } - $res .= - '' . + $res .= '' . '
' . __('Author') . '' . __('Email') . '
' . form::checkbox( - array('reserved[' . $user['email'] . ']'), + ['reserved[' . $user['email'] . ']'], $user['name'], (null === $wlc->isReserved($user['name'], $user['email'])) ) . @@ -232,11 +221,10 @@ class whiteListComReservedFilter extends dcSpamFilter '
' . '

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

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

' . '
'; return $res; @@ -250,27 +238,25 @@ class whiteListComReservedFilter extends dcSpamFilter */ class whiteListCom { - public $core; public $con; public $blog; public $settings; - private $unmoderated = array(); - private $reserved = array(); + private $unmoderated = []; + private $reserved = []; - public function __construct($core) + public function __construct() { - $this->core = $core; - $this->con = $core->con; - $this->blog = $core->con->escape($core->blog->id); + $this->con = dcCore::app()->con; + $this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id); - $core->blog->settings->addNamespace('whiteListCom'); - $this->settings = $core->blog->settings->whiteListCom; + dcCore::app()->blog->settings->addNamespace('whiteListCom'); + $this->settings = dcCore::app()->blog->settings->whiteListCom; - $unmoderated = $this->settings->whiteListCom_unmoderated; + $unmoderated = $this->settings->whiteListCom_unmoderated; $this->unmoderated = self::decode($unmoderated); - $reserved = $this->settings->whiteListCom_reserved; + $reserved = $this->settings->whiteListCom_reserved; $this->reserved = self::decode($reserved); } @@ -281,7 +267,8 @@ class whiteListCom self::encode($this->unmoderated), 'string', 'Whitelist of unmoderated users on comments', - true,false + true, + false ); $this->settings->put( @@ -289,11 +276,12 @@ class whiteListCom self::encode($this->reserved), 'string', 'Whitelist of reserved names on comments', - true,false + true, + false ); } - # Return + # 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 @@ -303,25 +291,26 @@ class whiteListCom return false; } elseif ($this->reserved[$author] != $email) { return true; - } else { - return null; } + + return null; } # You must do a commit to save this change public function addReserved($author, $email) { $this->reserved[$author] = $email; + return true; } # You must do a commit to save this change public function emptyReserved() { - $this->reserved = array(); + $this->reserved = []; } - # Return + # Return # true if it is known as an unmoderated email else false public function isUnmoderated($email) { @@ -331,7 +320,7 @@ class whiteListCom # You must do a commit to save this change public function addUnmoderated($email) { - if (!in_array($email,$this->unmoderated)) { + if (!in_array($email, $this->unmoderated)) { $this->unmoderated[] = $email; return true; @@ -343,24 +332,24 @@ class whiteListCom # You must do a commit to save this change public function emptyUnmoderated() { - $this->unmoderated = array(); + $this->unmoderated = []; } public function getPostsUsers() { - $users = array(); - $rs = $this->core->blog->getPostsUsers(); - while($rs->fetch()) { + $users = []; + $rs = dcCore::app()->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 - ); + $users[] = [ + 'name' => $name, + 'email' => $rs->user_email, + ]; } return $users; @@ -368,19 +357,19 @@ class whiteListCom public function getCommentsUsers() { - $users = array(); - $rs = $this->con->select( + $users = []; + $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 ' . + 'FROM ' . dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME . ' C ' . + 'LEFT JOIN ' . dcCore::app()->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 - ); + while ($rs->fetch()) { + $users[] = [ + 'name' => $rs->comment_author, + 'email' => $rs->comment_email, + ]; } return $users; @@ -388,7 +377,7 @@ class whiteListCom public static function encode($x) { - $y = is_array($x) ? $x : array(); + $y = is_array($x) ? $x : []; return base64_encode(serialize($y)); } @@ -397,7 +386,7 @@ class whiteListCom { $y = @unserialize(@base64_decode($x)); - return is_array($y) ? $y : array(); + return is_array($y) ? $y : []; } } @@ -411,26 +400,24 @@ class whiteListComBehaviors # from behavior publicAfterCommentCreate public static function switchStatus($cur, $id) { - global $core; - - if ($core->blog === null - || $core->blog->settings->system->comments_pub) { + 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') { - $core->con->writeLock($core->prefix.'comment'); + dcCore::app()->con->writeLock(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME); - $cur->comment_status = 1; + $cur->comment_status = 1; $cur->comment_spam_status = 0; $cur->comment_spam_filter = 0; $cur->update('WHERE comment_id = ' . $id . ' '); - $core->con->unlock(); + dcCore::app()->con->unlock(); - $core->blog->triggerComment($id); - $core->blog->triggerBlog(); + dcCore::app()->blog->triggerComment($id); + dcCore::app()->blog->triggerBlog(); } } -} \ No newline at end of file +} diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php deleted file mode 100644 index 1e7996c..0000000 --- a/locales/fr/main.lang.php +++ /dev/null @@ -1,39 +0,0 @@ -