Compare commits

..

No commits in common. "33275055005d9570c3ad76c2aae8b086521073ec" and "7ff7b24f366b86a5bf3a22593321d6f8ce7c7bf7" have entirely different histories.

13 changed files with 285 additions and 489 deletions

View File

@ -1,24 +1,3 @@
1.2 - 2023.08.03
- require Dotclear 2.27
- require PHP 7.4+
- upgrade to Dotclear 2.27
- move repository to gitea (testing)
1.2.1 - 2023.05.13
- require dotclear 2.26
- cleanup for dotclear 2.26 stable
1.2 - 2023.04.24
- require dotclear 2.26
- use latest dotclear namespace
- use sql statement
- use static methods for filters
- fix comments without email
- fix list display
- fix nullsafe warnings
- add dependencies to plugin antispam
- add plugin Uninstaller features
1.1 - 2023.03.11 1.1 - 2023.03.11
- code review for Dotclear 2.25 - code review for Dotclear 2.25

View File

@ -3,7 +3,7 @@
[![Release](https://img.shields.io/github/v/release/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/releases) [![Release](https://img.shields.io/github/v/release/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/releases)
[![Date](https://img.shields.io/github/release-date/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/releases) [![Date](https://img.shields.io/github/release-date/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/releases)
[![Issues](https://img.shields.io/github/issues/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/issues) [![Issues](https://img.shields.io/github/issues/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/issues)
[![Dotclear](https://img.shields.io/badge/dotclear-v2.27-blue.svg)](https://fr.dotclear.org/download) [![Dotclear](https://img.shields.io/badge/dotclear-v2.25-blue.svg)](https://fr.dotclear.org/download)
[![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://plugins.dotaddict.org/dc2/details/whiteListCom) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://plugins.dotaddict.org/dc2/details/whiteListCom)
[![License](https://img.shields.io/github/license/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/blob/master/LICENSE) [![License](https://img.shields.io/github/license/JcDenis/whiteListCom)](https://github.com/JcDenis/whiteListCom/blob/master/LICENSE)
@ -20,7 +20,7 @@ and a list of reserved names (pair of nickname / email ).
_whiteListCom_ requires: _whiteListCom_ requires:
* permissions to manage antispam * permissions to manage antispam
* Dotclear 2.27 * Dotclear 2.25
## USAGE ## USAGE
@ -34,8 +34,8 @@ Note: User must write a comment before able to be added to the list.
## LINKS ## LINKS
* License : [GNU GPL v2](https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html) * License : [GNU GPL v2](https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html)
* Source & contribution : [Gitea Page](http://gitea.jcdenis.fr/Dotclear/whiteListCom) or [GitHub Page](https://github.com/JcDenis/whiteListCom) * Source & contribution : [GitHub Page](https://github.com/JcDenis/whiteListCom)
* Packages & details : [Gitea Page](http://gitea.jcdenis.fr/Dotclear/whiteListCom/releases) or [Dotaddict Page](https://plugins.dotaddict.org/dc2/details/whiteListCom) * Packages & details : [Dotaddict Page](https://plugins.dotaddict.org/dc2/details/whiteListCom)
## CONTRIBUTORS ## CONTRIBUTORS

View File

@ -18,20 +18,17 @@ $this->registerModule(
'Whitelist comments', 'Whitelist comments',
'Whitelists for comments moderation', 'Whitelists for comments moderation',
'Jean-Christian Denis and Contributors', 'Jean-Christian Denis and Contributors',
'1.3', '1.1',
[ [
'requires' => [ 'requires' => [['core', '2.25']],
['core', '2.27'],
['antispam', '2.0'],
],
'permissions' => dcCore::app()->auth->makePermissions([ 'permissions' => dcCore::app()->auth->makePermissions([
dcCore::app()->auth::PERMISSION_USAGE, dcAuth::PERMISSION_USAGE,
dcCore::app()->auth::PERMISSION_CONTENT_ADMIN, dcAuth::PERMISSION_CONTENT_ADMIN,
]), ]),
'priority' => 200, 'priority' => 200,
'type' => 'plugin', 'type' => 'plugin',
'support' => 'http://gitea.jcdenis.fr/Dotclear/whiteListCom', 'support' => 'https://github.com/JcDenis/' . basename(__DIR__),
'details' => 'http://gitea.jcdenis.fr/Dotclear/whiteListCom/src/branch/master/README.md', 'details' => 'https://plugins.dotaddict.org/dc2/details/' . basename(__DIR__),
'repository' => 'http://gitea.jcdenis.fr/Dotclear/whiteListCom/raw/branch/master/dcstore.xml', 'repository' => 'https://raw.githubusercontent.com/JcDenis/' . basename(__DIR__) . '/master/dcstore.xml',
] ]
); );

View File

@ -2,12 +2,12 @@
<modules xmlns:da="http://dotaddict.org/da/"> <modules xmlns:da="http://dotaddict.org/da/">
<module id="whiteListCom"> <module id="whiteListCom">
<name>Whitelist comments</name> <name>Whitelist comments</name>
<version>1.3</version> <version>1.1</version>
<author>Jean-Christian Denis and Contributors</author> <author>Jean-Christian Denis and Contributors</author>
<desc>Whitelists for comments moderation</desc> <desc>Whitelists for comments moderation</desc>
<file>https://gitea.jcdenis.fr/Dotclear/whiteListCom/releases/download/v1.3/plugin-whiteListCom.zip</file> <file>https://github.com/JcDenis/whiteListCom/releases/download/v1.1/plugin-whiteListCom.zip</file>
<da:dcmin>2.27</da:dcmin> <da:dcmin>2.25</da:dcmin>
<da:details>http://gitea.jcdenis.fr/Dotclear/whiteListCom/src/branch/master/README.md</da:details> <da:details>https://plugins.dotaddict.org/dc2/details/whiteListCom</da:details>
<da:support>http://gitea.jcdenis.fr/Dotclear/whiteListCom</da:support> <da:support>https://github.com/JcDenis/whiteListCom</da:support>
</module> </module>
</modules> </modules>

View File

@ -9,18 +9,16 @@
# DOT NOT MODIFY THIS FILE ! # DOT NOT MODIFY THIS FILE !
# #
use Dotclear\Helper\L10n; l10n::$locales['Reserved names'] = 'Noms réservés';
l10n::$locales['Whitelist of reserved names of users'] = 'Liste blanche des noms d\'utilisateurs réservés';
L10n::$locales['Reserved names'] = 'Noms réservés'; l10n::$locales['This name is reserved to an other user.'] = 'Ce nom est réservé à un autre utilisateur.';
L10n::$locales['Whitelist of reserved names of users'] = 'Liste blanche des noms d\'utilisateurs réservés'; l10n::$locales['Reserved names have been successfully updated.'] = 'Le noms réservés ont été mis à jour.';
L10n::$locales['This name is reserved to an other user.'] = 'Ce nom est réservé à un autre utilisateur.'; l10n::$locales['Check the users who can make comments without being moderated.'] = 'Cocher les utilisateurs qui peuvent faire des commentaires sans être modérés.';
L10n::$locales['Reserved names have been successfully updated.'] = 'Le noms réservés ont été mis à jour.'; l10n::$locales['Comments authors list'] = 'Liste des auteurs de commentaires';
L10n::$locales['Check the users who can make comments without being moderated.'] = 'Cocher les utilisateurs qui peuvent faire des commentaires sans être modérés.'; l10n::$locales['Unmoderated authors'] = 'Auteurs non modérés';
L10n::$locales['Comments authors list'] = 'Liste des auteurs de commentaires'; l10n::$locales['Whitelist of unmoderated authors'] = 'Liste blanche des auteurs non modérés';
L10n::$locales['Unmoderated authors'] = 'Auteurs non modérés'; l10n::$locales['Unmoderated names have been successfully updated.'] = 'Les noms non modérés ont été mis à jour.';
L10n::$locales['Whitelist of unmoderated authors'] = 'Liste blanche des auteurs non modérés'; l10n::$locales['This filter is used only if comments are moderates'] = 'Ce filtre est utilisé seulement si les commentaires sont modérés.';
L10n::$locales['Unmoderated names have been successfully updated.'] = 'Les noms non modérés ont été mis à jour.'; l10n::$locales['Posts authors list'] = 'Liste des auteurs de billets';
L10n::$locales['This filter is used only if comments are moderates'] = 'Ce filtre est utilisé seulement si les commentaires sont modérés.'; l10n::$locales['Whitelists for comments moderation'] = 'Listes blanches pour la modération de commentaires';
L10n::$locales['Posts authors list'] = 'Liste des auteurs de billets'; l10n::$locales['Whitelist comments'] = 'Liste blanche des commentaires';
L10n::$locales['Whitelists for comments moderation'] = 'Listes blanches pour la modération de commentaires';
L10n::$locales['Whitelist comments'] = 'Liste blanche des commentaires';

173
src/Core.php 100644
View File

@ -0,0 +1,173 @@
<?php
/**
* @brief whiteListCom, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom;
/* dotclear ns */
use dcBlog;
use dcCore;
use dcUtils;
/**
* @ingroup DC_PLUGIN_WHITELISTCOM
* @brief White list filters methods
* @since 2.6
*/
class Core
{
public $con;
public $blog;
public $settings;
private $unmoderated = [];
private $reserved = [];
public function __construct()
{
$this->con = dcCore::app()->con;
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
$this->settings = dcCore::app()->blog->settings->get(My::id());
$this->unmoderated = self::decode($this->settings->get('unmoderated'));
$this->reserved = self::decode($this->settings->get('reserved'));
}
public function commit(): void
{
$this->settings->put(
'unmoderated',
self::encode($this->unmoderated),
'string',
'Whitelist of unmoderated users on comments',
true,
false
);
$this->settings->put(
'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): ?bool
{
if (!isset($this->reserved[$author])) {
return false;
} elseif ($this->reserved[$author] != $email) {
return true;
}
return null;
}
# You must do a commit to save this change
public function addReserved($author, $email): bool
{
$this->reserved[$author] = $email;
return true;
}
# You must do a commit to save this change
public function emptyReserved(): void
{
$this->reserved = [];
}
# Return
# true if it is known as an unmoderated email else false
public function isUnmoderated($email): bool
{
return in_array($email, $this->unmoderated);
}
# You must do a commit to save this change
public function addUnmoderated($email): ?bool
{
if (!in_array($email, $this->unmoderated)) {
$this->unmoderated[] = $email;
return true;
}
return null;
}
# You must do a commit to save this change
public function emptyUnmoderated(): void
{
$this->unmoderated = [];
}
public function getPostsUsers(): array
{
$users = [];
$rs = dcCore::app()->blog->getPostsUsers();
while ($rs->fetch()) {
$name = dcUtils::getUserCN(
$rs->f('user_id'),
$rs->f('user_name'),
$rs->f('user_firstname'),
$rs->f('user_displayname')
);
$users[] = [
'name' => $name,
'email' => $rs->f('user_email'),
];
}
return $users;
}
public function getCommentsUsers(): array
{
$users = [];
$rs = $this->con->select(
'SELECT comment_author, comment_email ' .
'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[] = [
'name' => $rs->f('comment_author'),
'email' => $rs->f('comment_email'),
];
}
return $users;
}
public static function encode($x): string
{
$y = is_array($x) ? $x : [];
return json_encode($y);
}
public static function decode($x): array
{
$y = json_decode($x, true);
return is_array($y) ? $y : [];
}
}

View File

@ -14,15 +14,18 @@ declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom; namespace Dotclear\Plugin\whiteListCom;
/* dotclear ns */
use dcCore; use dcCore;
use dcNamespace; use dcNamespace;
use Dotclear\Core\Process; use dcNsProcess;
/* php ns */
use Exception; use Exception;
class Install extends Process class Install extends dcNsProcess
{ {
// Module specs // Module specs
private static array $mod_conf = [ private static $mod_conf = [
[ [
'unmoderated', 'unmoderated',
'[]', '[]',
@ -39,12 +42,14 @@ class Install extends Process
public static function init(): bool public static function init(): bool
{ {
return self::status(My::checkContext(My::INSTALL)); static::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return static::$init;
} }
public static function process(): bool public static function process(): bool
{ {
if (!self::status()) { if (!sestaticlf::$init) {
return false; return false;
} }
@ -54,7 +59,7 @@ class Install extends Process
// Set module settings // Set module settings
foreach (self::$mod_conf as $v) { foreach (self::$mod_conf as $v) {
My::settings()->put( dcCore::app()->blog->settings->get(My::id())->put(
$v[0], $v[0],
$v[1], $v[1],
$v[2], $v[2],
@ -84,15 +89,15 @@ class Install extends Process
); );
while ($record->fetch()) { while ($record->fetch()) {
if (preg_match('/^whiteListCom(.*?)$/', $record->f('setting_id'), $match)) { if (preg_match('/^whiteListCom(.*?)$/', $record->setting_id, $match)) {
$value = @unserialize(@base64_decode($record->f('setting_value'))); $value = @unserialize(@base64_decode($record->setting_value));
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME);
$cur->setField('setting_id', $match[1]); $cur->setting_id = $match[1];
$cur->setField('setting_ns', My::id()); $cur->setting_ns = My::id();
$cur->setField('setting_value', is_array($value) ? json_encode($value) : '[]'); $cur->setting_value = is_array($value) ? json_encode($value) : '[]';
$cur->update( $cur->update(
"WHERE setting_id = '" . $record->f('setting_id') . "' and setting_ns = 'whiteListCom' " . "WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'whiteListCom' " .
'AND blog_id ' . (null === $record->f('blog_id') ? 'IS NULL ' : ("= '" . dcCore::app()->con->escapeStr((string) $record->f('blog_id')) . "' ")) 'AND blog_id ' . (null === $record->blog_id ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->blog_id) . "' "))
); );
} }
} }

View File

@ -14,11 +14,26 @@ declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom; namespace Dotclear\Plugin\whiteListCom;
use Dotclear\Module\MyPlugin; use dcCore;
/** /**
* This module definitions. * Plugin definitions
*/ */
class My extends MyPlugin class My
{ {
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
} }

View File

@ -16,18 +16,20 @@ namespace Dotclear\Plugin\whiteListCom;
use dcBlog; use dcBlog;
use dcCore; use dcCore;
use Dotclear\Core\Process; use dcNsProcess;
class Prepend extends Process class Prepend extends dcNsProcess
{ {
public static function init(): bool public static function init(): bool
{ {
return self::status(My::checkContext(My::PREPEND)); static::$init = true;
return static::$init;
} }
public static function process(): bool public static function process(): bool
{ {
if (!self::status()) { if (!static::$init) {
return false; return false;
} }

View File

@ -15,14 +15,14 @@ declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom; namespace Dotclear\Plugin\whiteListCom;
use dcCore; use dcCore;
use Dotclear\Core\Backend\Notices; use dcPage;
use dcSpamFilter;
use Dotclear\Helper\Html\Form\{ use Dotclear\Helper\Html\Form\{
Checkbox, Checkbox,
Hidden Hidden
}; };
use Dotclear\Helper\Html\Html; use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Network\Http; use Dotclear\Helper\Network\Http;
use Dotclear\Plugin\antispam\SpamFilter;
use Exception; use Exception;
/** /**
@ -30,23 +30,17 @@ use Exception;
* @brief Filter for reserved names. * @brief Filter for reserved names.
* @since 2.6 * @since 2.6
*/ */
class ReservedWhiteList extends SpamFilter class ReservedWhiteList extends dcSpamFilter
{ {
public $name = 'Reserved names'; public $name = 'Reserved names';
public $has_gui = true; public $has_gui = true;
/**
* @return void
*/
protected function setInfo() protected function setInfo()
{ {
$this->name = __('Reserved names'); $this->name = __('Reserved names');
$this->description = __('Whitelist of reserved names of users'); $this->description = __('Whitelist of reserved names of users');
} }
/**
* @return void|null|bool
*/
public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status)
{ {
if ($type != 'comment') { if ($type != 'comment') {
@ -56,7 +50,9 @@ class ReservedWhiteList extends SpamFilter
$throw = false; $throw = false;
try { try {
if (true === Utils::isReserved($author, $email)) { $wlc = new Core();
if (true === $wlc->isReserved($author, $email)) {
$status = 'reserved name'; $status = 'reserved name';
//return true; //return true;
$throw = true; $throw = true;
@ -72,9 +68,6 @@ class ReservedWhiteList extends SpamFilter
} }
} }
/**
* @return string
*/
public function getStatusMessage($status, $comment_id) public function getStatusMessage($status, $comment_id)
{ {
return __('This name is reserved to an other user.'); return __('This name is reserved to an other user.');
@ -82,41 +75,40 @@ class ReservedWhiteList extends SpamFilter
public function gui(string $url): string public function gui(string $url): string
{ {
$wlc = new Core();
$comments = []; $comments = [];
try { try {
if (!empty($_POST['update_reserved'])) { if (!empty($_POST['update_reserved'])) {
Utils::emptyReserved(); $wlc->emptyReserved();
foreach ($_POST['reserved'] as $i => $name) { foreach ($_POST['reserved'] as $i => $name) {
Utils::addReserved($name, $_POST['reserved_email'][$i]); $wlc->addReserved($name, $_POST['reserved_email'][$i]);
} }
Utils::commit(); $wlc->commit();
Notices::addSuccessNotice(__('Reserved names have been successfully updated.')); dcPage::addSuccessNotice(__('Reserved names have been successfully updated.'));
Http::redirect($url); Http::redirect($url);
} }
$comments = Utils::getCommentsUsers(); $comments = $wlc->getCommentsUsers();
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); dcCore::app()->error->add($e->getMessage());
} }
$res = '<form action="' . Html::escapeURL($url) . '" method="post">' . $res = '<form action="' . Html::escapeURL($url) . '" method="post">' .
'<p>' . __('Check the users who can make comments without being moderated.') . '</p>' . '<p>' . __('Check the users who can make comments without being moderated.') . '</p>' .
'<div class="table-outer">' . '<p>' . __('Comments authors list') . '</p>' .
'<table class="clear">' . '<table class="clear">' .
'<caption>' . __('Comments authors list') . '</caption>' .
'<thead><tr><th>' . __('Author') . '</th><th>' . __('Email') . '</th></tr></thead>' . '<thead><tr><th>' . __('Author') . '</th><th>' . __('Email') . '</th></tr></thead>' .
'<tbody>'; '<tbody>';
$i = 0; $i = 0;
foreach ($comments as $user) { foreach ($comments as $user) {
$checked = null === Utils::isReserved($user['name'], $user['email']); $res .= '<tr class="line">' .
$res .= '<tr class="line' . ($checked ? '' : ' offline') . '">' .
'<td class="nowrap">' . '<td class="nowrap">' .
(new Checkbox(['reserved[' . $i . ']'], $checked))->value($user['name'])->render() . (new Checkbox(['reserved[' . $i . ']'], (null === $wlc->isReserved($user['name'], $user['email']))))->value($user['name'])->render() .
(new Hidden(['reserved_email[' . $i . ']'], $user['email']))->render() . (new Hidden(['reserved_email[' . $i . ']'], $user['email']))->render() .
' ' . $user['name'] . '</td>' . ' ' . $user['name'] . '</td>' .
'<td class="nowrap maximal">' . $user['email'] . '</td>' . '<td class="nowrap">' . $user['email'] . '</td>' .
'</tr>'; '</tr>';
$i++; $i++;
} }

View File

@ -1,70 +0,0 @@
<?php
/**
* @brief whiteListCom, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom;
use dcCore;
use Dotclear\Core\Process;
use Dotclear\Plugin\Uninstaller\Uninstaller;
class Uninstall extends Process
{
public static function init(): bool
{
return self::status(My::checkContext(My::UNINSTALL));
}
public static function process(): bool
{
if (!self::status() || !dcCore::app()->plugins->moduleExists('Uninstaller')) {
return false;
}
Uninstaller::instance()
->addUserAction(
'settings',
'delete_all',
My::id()
)
->addUserAction(
'plugins',
'delete',
My::id()
)
->addUserAction(
'versions',
'delete',
My::id()
)
->addDirectAction(
'settings',
'delete_all',
My::id()
)
->addDirectAction(
'plugins',
'delete',
My::id()
)
->addDirectAction(
'versions',
'delete',
My::id()
)
;
// no custom action
return false;
}
}

View File

@ -15,11 +15,11 @@ declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom; namespace Dotclear\Plugin\whiteListCom;
use dcCore; use dcCore;
use Dotclear\COre\Backend\Notices; use dcPage;
use dcSpamFilter;
use Dotclear\Helper\Html\Form\Checkbox; use Dotclear\Helper\Html\Form\Checkbox;
use Dotclear\Helper\Html\Html; use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Network\Http; use Dotclear\Helper\Network\Http;
use Dotclear\Plugin\antispam\SpamFilter;
use Exception; use Exception;
/** /**
@ -29,23 +29,17 @@ use Exception;
* *
* This filter is used only if comments are moderates * This filter is used only if comments are moderates
*/ */
class UnmoderatedWhiteList extends SpamFilter class UnmoderatedWhiteList extends dcSpamFilter
{ {
public $name = 'Unmoderated authors'; public $name = 'Unmoderated authors';
public $has_gui = true; public $has_gui = true;
/**
* @return void
*/
protected function setInfo() protected function setInfo()
{ {
$this->name = __('Unmoderated authors'); $this->name = __('Unmoderated authors');
$this->description = __('Whitelist of unmoderated authors'); $this->description = __('Whitelist of unmoderated authors');
} }
/**
* @return void|null|bool
*/
public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status)
{ {
if ($type != 'comment' if ($type != 'comment'
@ -55,7 +49,8 @@ class UnmoderatedWhiteList extends SpamFilter
} }
try { try {
if (Utils::isUnmoderated($email)) { $wlc = new Core();
if ($wlc->isUnmoderated($email)) {
$status = 'unmoderated'; $status = 'unmoderated';
# return true in order to change comment_status after # return true in order to change comment_status after
@ -69,28 +64,28 @@ class UnmoderatedWhiteList extends SpamFilter
public function gui(string $url): string public function gui(string $url): string
{ {
$wlc = new Utils(); $wlc = new Core();
$posts = $comments = []; $posts = $comments = [];
try { try {
if (!empty($_POST['update_unmoderated'])) { if (!empty($_POST['update_unmoderated'])) {
Utils::emptyUnmoderated(); $wlc->emptyUnmoderated();
foreach ($_POST['unmoderated'] as $email) { foreach ($_POST['unmoderated'] as $email) {
Utils::addUnmoderated($email); $wlc->addUnmoderated($email);
} }
Utils::commit(); $wlc->commit();
Notices::addSuccessNotice(__('Unmoderated names have been successfully updated.')); dcPage::addSuccessNotice(__('Unmoderated names have been successfully updated.'));
Http::redirect($url); Http::redirect($url);
} }
$posts = Utils::getPostsUsers(); $posts = $wlc->getPostsUsers();
$comments = Utils::getCommentsUsers(); $comments = $wlc->getCommentsUsers();
} catch (Exception $e) { } catch (Exception $e) {
dcCore::app()->error->add($e->getMessage()); dcCore::app()->error->add($e->getMessage());
} }
$res = ''; $res = '';
if (!is_null(dcCore::app()->blog) && dcCore::app()->blog->settings->get('system')->get('comments_pub')) { if (dcCore::app()->blog->settings->get('system')->get('comments_pub')) {
$res .= '<p class="message">' . $res .= '<p class="message">' .
__('This filter is used only if comments are moderates') . __('This filter is used only if comments are moderates') .
'</p>'; '</p>';
@ -98,46 +93,42 @@ class UnmoderatedWhiteList extends SpamFilter
$res .= '<form action="' . Html::escapeURL($url) . '" method="post">' . $res .= '<form action="' . Html::escapeURL($url) . '" method="post">' .
'<p>' . __('Check the users who can make comments without being moderated.') . '</p>' . '<p>' . __('Check the users who can make comments without being moderated.') . '</p>' .
'<div class="two-boxes">' . '<div class="two-cols">' .
'<div class="box odd">' . '<div class="col">' .
'<div class="table-outer">' . '<p>' . __('Posts authors list') . '</p>' .
'<table class="clear">' . '<table class="clear">' .
'<caption>' . __('Posts authors list') . '</caption>' .
'<thead><tr><th>' . __('Name') . '</th><th>' . __('Email') . '</th></tr></thead>' . '<thead><tr><th>' . __('Name') . '</th><th>' . __('Email') . '</th></tr></thead>' .
'<tbody>'; '<tbody>';
foreach ($posts as $user) { foreach ($posts as $user) {
$checked = Utils::isUnmoderated($user['email']); $res .= '<tr class="line">' .
$res .= '<tr class="line' . ($checked ? '' : ' offline') . '">' .
'<td class="nowrap">' . '<td class="nowrap">' .
(new Checkbox(['unmoderated[]'], $checked))->value($user['email'])->render() . (new Checkbox(['unmoderated[]'], $wlc->isUnmoderated($user['email'])))->value($user['email'])->render() .
' ' . $user['name'] . '</td>' . ' ' . $user['name'] . '</td>' .
'<td class="nowrap">' . $user['email'] . '</td>' . '<td class="nowrap">' . $user['email'] . '</td>' .
'</tr>'; '</tr>';
} }
$res .= '</tbody>' . $res .= '</tbody>' .
'</table></div>' . '</table>' .
'</div>' . '</div>' .
'<div class="box even">' . '<div class="col">' .
'<div class="table-outer">' . '<p>' . __('Comments authors list') . '</p>' .
'<table class="clear">' . '<table class="clear">' .
'<caption>' . __('Comments authors list') . '</caption>' .
'<thead><tr><th>' . __('Author') . '</th><th>' . __('Email') . '</th></tr></thead>' . '<thead><tr><th>' . __('Author') . '</th><th>' . __('Email') . '</th></tr></thead>' .
'<tbody>'; '<tbody>';
foreach ($comments as $user) { foreach ($comments as $user) {
$checked = Utils::isUnmoderated($user['email']); $res .= '<tr class="line">' .
$res .= '<tr class="line' . ($checked ? '' : ' offline') . '">' .
'<td class="nowrap">' . '<td class="nowrap">' .
(new Checkbox(['unmoderated[]'], $checked))->value($user['email'])->render() . (new Checkbox(['unmoderated[]'], $wlc->isUnmoderated($user['email'])))->value($user['email'])->render() .
' ' . $user['name'] . '</td>' . ' ' . $user['name'] . '</td>' .
'<td class="nowrap">' . $user['email'] . '</td>' . '<td class="nowrap">' . $user['email'] . '</td>' .
'</tr>'; '</tr>';
} }
$res .= '</tbody>' . $res .= '</tbody>' .
'</table></div>' . '</table>' .
'</div>' . '</div>' .
'</div>' . '</div>' .
'<p><input type="submit" id="update_unmoderated" name="update_unmoderated" value="' . __('Save') . '" />' . '<p><input type="submit" id="update_unmoderated" name="update_unmoderated" value="' . __('Save') . '" />' .

View File

@ -1,286 +0,0 @@
<?php
/**
* @brief whiteListCom, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom;
use dcBlog;
use dcCore;
use dcUtils;
use Dotclear\Database\Statement\{
JoinStatement,
SelectStatement,
};
/**
* @ingroup DC_PLUGIN_WHITELISTCOM
* @brief White list filters methods
* @since 2.6
*/
class Utils
{
/** @var bool $init preload check */
private static bool $init = false;
/** @var array $unmoderated List of unmoderated users */
private static array $unmoderated = [];
/** @var array $unmoderated List of reserved name */
private static array $reserved = [];
/**
* Initialize properties.
*/
private static function init(): void
{
if (self::$init) {
return;
}
self::$unmoderated = self::decode(My::settings()->get('unmoderated'));
self::$reserved = self::decode(My::settings()->get('reserved'));
self::$init = true;
}
/**
* Save changes.
*/
public static function commit(): void
{
self::init();
My::settings()->put(
'unmoderated',
self::encode(self::$unmoderated),
'string',
'Whitelist of unmoderated users on comments',
true,
false
);
My::settings()->put(
'reserved',
self::encode(self::$reserved),
'string',
'Whitelist of reserved names on comments',
true,
false
);
}
/**
* Check if a name is reserved.
*
* 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
*
* @param string $author The author
* @param string $email The email
*
* @return null|bool The reserved state
*/
public static function isReserved(string $author, string $email): ?bool
{
self::init();
if (!isset(self::$reserved[$author])) {
return false;
} elseif (self::$reserved[$author] != $email) {
return true;
}
return null;
}
/**
* Add a reserved user.
*
* You must do a Utils::commit() to save this change
*
* @param string $author The author
* @param string $email The email
*/
public static function addReserved(string $author, string $email): void
{
self::init();
self::$reserved[$author] = $email;
}
/**
* Clean reserved names list.
*
* You must do a Utils::commit() to save this change
*/
public static function emptyReserved(): void
{
self::init();
self::$reserved = [];
}
/**
* Check if an email is unmoderated.
*
* Return:
* - true if it is known as an unmoderated email
* - false else
*
* @param string $email The email
*
* @return bool The reserved state
*/
public static function isUnmoderated(string $email): bool
{
self::init();
return in_array($email, self::$unmoderated);
}
/**
* Add a unmoderated user.
*
* You must do a Utils::commit() to save this change
*
* @param string $email The email
*/
public static function addUnmoderated(string $email): void
{
self::init();
if (!in_array($email, self::$unmoderated)) {
self::$unmoderated[] = $email;
}
}
/**
* Clean unmoderated users list.
*
* You must do a Utils::commit() to save this change
*/
public static function emptyUnmoderated(): void
{
self::init();
self::$unmoderated = [];
}
/**
* Get posts users.
*
* @return array The users name/email pairs
*/
public static function getPostsUsers(): array
{
if (is_null(dcCore::app()->blog)) {
return [];
}
$rs = dcCore::app()->blog->getPostsUsers();
if ($rs->isEmpty()) {
return [];
}
$users = [];
while ($rs->fetch()) {
$name = dcUtils::getUserCN(
$rs->f('user_id'),
$rs->f('user_name'),
$rs->f('user_firstname'),
$rs->f('user_displayname')
);
$users[] = [
'name' => $name,
'email' => $rs->f('user_email'),
];
}
return $users;
}
/**
* Get comments users.
*
* @return array The users name/email pairs
*/
public static function getCommentsUsers(): array
{
if (is_null(dcCore::app()->blog)) {
return [];
}
$sql = new SelectStatement();
$rs = $sql->from($sql->as(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME, 'C'))
->columns([
'comment_author',
'comment_email',
])
->join(
(new JoinStatement())
->left()
->from($sql->as(dcCore::app()->prefix . dcBlog::POST_TABLE_NAME, 'P'))
->on('C.post_id = P.post_id')
->statement()
)
->where('blog_id = ' . $sql->quote(dcCore::app()->blog->id))
->and('comment_trackback = 0')
->and("comment_email != ''")
->group('comment_email, comment_author') // Added author to fix postgreSql
->select();
if (is_null($rs) || $rs->isEmpty()) {
return [];
}
$users = [];
while ($rs->fetch()) {
$users[] = [
'name' => $rs->f('comment_author'),
'email' => $rs->f('comment_email'),
];
}
return $users;
}
/**
* Encode settings.
*
* @param array|string $x The value to encode
*
* @return string The encoded value
*/
public static function encode($x): string
{
$y = is_array($x) ? $x : [];
return (string) json_encode($y);
}
/**
* Decode settings.
*
* @param string $x The value to decode
*
* @return array The decoded value
*/
public static function decode($x): array
{
$y = json_decode($x, true);
return is_array($y) ? $y : [];
}
}