release 1.1

master
Jean-Christian Paul Denis 2023-03-11 22:50:36 +01:00
parent 0d2b8e4aff
commit 7c7711a367
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
13 changed files with 94 additions and 118 deletions

View File

@ -1,3 +1,6 @@
1.1 - 2023.03.11
- code review for Dotclear 2.25
1.0 - 2023.01.08 1.0 - 2023.01.08
- use abstract plugin id - use abstract plugin id
- use anonymous functions - use anonymous functions

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.24-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.24 * Dotclear 2.25
## USAGE ## USAGE

View File

@ -18,9 +18,9 @@ $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.0', '1.1',
[ [
'requires' => [['core', '2.24']], 'requires' => [['core', '2.25']],
'permissions' => dcCore::app()->auth->makePermissions([ 'permissions' => dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE, dcAuth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN, dcAuth::PERMISSION_CONTENT_ADMIN,

View File

@ -1,20 +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);
$install = implode('\\', ['Dotclear', 'Plugin', basename(__DIR__), 'Install']);
if ($install::init()) {
return $install::process();
}
return null;

View File

@ -1,22 +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);
$prepend = implode('\\', ['Dotclear', 'Plugin', basename(__DIR__), 'Prepend']);
if (!class_exists($prepend)) {
require implode(DIRECTORY_SEPARATOR, [__DIR__, 'inc', 'Prepend.php']);
if ($prepend::init()) {
$prepend::process();
}
}

View File

@ -2,11 +2,11 @@
<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.0</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://github.com/JcDenis/whiteListCom/releases/download/v1.0/plugin-whiteListCom.zip</file> <file>https://github.com/JcDenis/whiteListCom/releases/download/v1.1/plugin-whiteListCom.zip</file>
<da:dcmin>2.24</da:dcmin> <da:dcmin>2.25</da:dcmin>
<da:details>https://plugins.dotaddict.org/dc2/details/whiteListCom</da:details> <da:details>https://plugins.dotaddict.org/dc2/details/whiteListCom</da:details>
<da:support>https://github.com/JcDenis/whiteListCom</da:support> <da:support>https://github.com/JcDenis/whiteListCom</da:support>
</module> </module>

View File

@ -1,9 +1,9 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Project-Id-Version: whiteListCom 1.0\n" "Project-Id-Version: whiteListCom 1.1\n"
"POT-Creation-Date: \n" "POT-Creation-Date: \n"
"PO-Revision-Date: 2023-01-07T23:39:35+00:00\n" "PO-Revision-Date: 2023-03-11T21:46:05+00:00\n"
"Last-Translator: Jean-Christian Denis\n" "Last-Translator: Jean-Christian Denis\n"
"Language-Team: \n" "Language-Team: \n"
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"

View File

@ -37,14 +37,12 @@ class Core
{ {
$this->con = dcCore::app()->con; $this->con = dcCore::app()->con;
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id); $this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
$this->settings = dcCore::app()->blog->settings->get(basename(dirname(__DIR__))); $this->settings = dcCore::app()->blog->settings->get(My::id());
$unmoderated = $this->settings->get('unmoderated'); $this->unmoderated = self::decode($this->settings->get('unmoderated'));
$this->unmoderated = self::decode($unmoderated); $this->reserved = self::decode($this->settings->get('reserved'));
$reserved = $this->settings->get('reserved');
$this->reserved = self::decode($reserved);
} }
public function commit() public function commit(): void
{ {
$this->settings->put( $this->settings->put(
'unmoderated', 'unmoderated',
@ -69,7 +67,7 @@ class Core
# true if it is a reserved name with wrong email # true if it is a reserved name with wrong email
# false if it is not a reserved name # false if it is not a reserved name
# null if it is a reserved name with right email # null if it is a reserved name with right email
public function isReserved($author, $email) public function isReserved($author, $email): ?bool
{ {
if (!isset($this->reserved[$author])) { if (!isset($this->reserved[$author])) {
return false; return false;
@ -81,7 +79,7 @@ class Core
} }
# You must do a commit to save this change # You must do a commit to save this change
public function addReserved($author, $email) public function addReserved($author, $email): bool
{ {
$this->reserved[$author] = $email; $this->reserved[$author] = $email;
@ -89,20 +87,20 @@ class Core
} }
# You must do a commit to save this change # You must do a commit to save this change
public function emptyReserved() public function emptyReserved(): void
{ {
$this->reserved = []; $this->reserved = [];
} }
# Return # Return
# true if it is known as an unmoderated email else false # true if it is known as an unmoderated email else false
public function isUnmoderated($email) public function isUnmoderated($email): bool
{ {
return in_array($email, $this->unmoderated); return in_array($email, $this->unmoderated);
} }
# You must do a commit to save this change # You must do a commit to save this change
public function addUnmoderated($email) public function addUnmoderated($email): ?bool
{ {
if (!in_array($email, $this->unmoderated)) { if (!in_array($email, $this->unmoderated)) {
$this->unmoderated[] = $email; $this->unmoderated[] = $email;
@ -114,32 +112,32 @@ class Core
} }
# You must do a commit to save this change # You must do a commit to save this change
public function emptyUnmoderated() public function emptyUnmoderated(): void
{ {
$this->unmoderated = []; $this->unmoderated = [];
} }
public function getPostsUsers() public function getPostsUsers(): array
{ {
$users = []; $users = [];
$rs = dcCore::app()->blog->getPostsUsers(); $rs = dcCore::app()->blog->getPostsUsers();
while ($rs->fetch()) { while ($rs->fetch()) {
$name = dcUtils::getUserCN( $name = dcUtils::getUserCN(
$rs->__get('user_id'), $rs->f('user_id'),
$rs->__get('user_name'), $rs->f('user_name'),
$rs->__get('user_firstname'), $rs->f('user_firstname'),
$rs->__get('user_displayname') $rs->f('user_displayname')
); );
$users[] = [ $users[] = [
'name' => $name, 'name' => $name,
'email' => $rs->__get('user_email'), 'email' => $rs->f('user_email'),
]; ];
} }
return $users; return $users;
} }
public function getCommentsUsers() public function getCommentsUsers(): array
{ {
$users = []; $users = [];
$rs = $this->con->select( $rs = $this->con->select(
@ -151,22 +149,22 @@ class Core
); );
while ($rs->fetch()) { while ($rs->fetch()) {
$users[] = [ $users[] = [
'name' => $rs->__get('comment_author'), 'name' => $rs->f('comment_author'),
'email' => $rs->__get('comment_email'), 'email' => $rs->f('comment_email'),
]; ];
} }
return $users; return $users;
} }
public static function encode($x) public static function encode($x): string
{ {
$y = is_array($x) ? $x : []; $y = is_array($x) ? $x : [];
return json_encode($y); return json_encode($y);
} }
public static function decode($x) public static function decode($x): array
{ {
$y = json_decode($x, true); $y = json_decode($x, true);

View File

@ -17,11 +17,12 @@ namespace Dotclear\Plugin\whiteListCom;
/* dotclear ns */ /* dotclear ns */
use dcCore; use dcCore;
use dcNamespace; use dcNamespace;
use dcNsProcess;
/* php ns */ /* php ns */
use Exception; use Exception;
class Install class Install extends dcNsProcess
{ {
// Module specs // Module specs
private static $mod_conf = [ private static $mod_conf = [
@ -39,19 +40,14 @@ class Install
], ],
]; ];
// Nothing to change below
private static $pid = '';
protected static $init = false;
public static function init(): bool public static function init(): bool
{ {
self::$pid = basename(dirname(__DIR__)); self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(self::$pid, dcCore::app()->plugins->moduleInfo(self::$pid, 'version'));
return self::$init; return self::$init;
} }
public static function process(): ?bool public static function process(): bool
{ {
if (!self::$init) { if (!self::$init) {
return false; return false;
@ -63,7 +59,7 @@ class Install
// Set module settings // Set module settings
foreach (self::$mod_conf as $v) { foreach (self::$mod_conf as $v) {
dcCore::app()->blog->settings->__get(self::$pid)->put( dcCore::app()->blog->settings->get(My::id())->put(
$v[0], $v[0],
$v[1], $v[1],
$v[2], $v[2],
@ -83,7 +79,7 @@ class Install
public static function growUp(): void public static function growUp(): void
{ {
$current = dcCore::app()->getVersion(self::$pid); $current = dcCore::app()->getVersion(My::id());
// Update settings id, ns // Update settings id, ns
if ($current && version_compare($current, '1.0', '<')) { if ($current && version_compare($current, '1.0', '<')) {
@ -97,7 +93,7 @@ class Install
$value = @unserialize(@base64_decode($record->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->setting_id = $match[1]; $cur->setting_id = $match[1];
$cur->setting_ns = self::$pid; $cur->setting_ns = My::id();
$cur->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->setting_id . "' and setting_ns = 'whiteListCom' " . "WHERE setting_id = '" . $record->setting_id . "' and setting_ns = 'whiteListCom' " .

39
src/My.php 100644
View File

@ -0,0 +1,39 @@
<?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;
/**
* Plugin definitions
*/
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

@ -14,50 +14,32 @@ declare(strict_types=1);
namespace Dotclear\Plugin\whiteListCom; namespace Dotclear\Plugin\whiteListCom;
/* dotclear ns */
use dcBlog; use dcBlog;
use dcCore; use dcCore;
use dcNsProcess;
/* clearbricks ns */ class Prepend extends dcNsProcess
use Clearbricks;
class Prepend
{ {
private const LIBS = [
'Core',
'UnmoderatedWhiteList',
'ReservedWhiteList',
'Install',
'Prepend',
];
protected static $init = false;
public static function init(): bool public static function init(): bool
{ {
self::$init = defined('DC_RC_PATH'); self::$init = true;
return self::$init; return self::$init;
} }
public static function process(): ?bool public static function process(): bool
{ {
if (!self::$init) { if (!self::$init) {
return false; return false;
} }
foreach (self::LIBS as $lib) { dcCore::app()->spamfilters[] = UnmoderatedWhiteList::class;
Clearbricks::lib()->autoload([ dcCore::app()->spamfilters[] = ReservedWhiteList::class;
__NAMESPACE__ . '\\' . $lib => __DIR__ . DIRECTORY_SEPARATOR . $lib . '.php',
]);
}
dcCore::app()->spamfilters[] = __NAMESPACE__ . '\\' . 'UnmoderatedWhiteList'; dcCore::app()->addBehavior('publicAfterCommentCreate', function ($cur, $id): void {
dcCore::app()->spamfilters[] = __NAMESPACE__ . '\\' . 'ReservedWhiteList';
dcCore::app()->addBehavior('publicAfterCommentCreate', function ($cur, $id) {
if (dcCore::app()->blog === null if (dcCore::app()->blog === null
|| dcCore::app()->blog->settings->get('system')->get('comments_pub')) { || dcCore::app()->blog->settings->get('system')->get('comments_pub')) {
return null; return;
} }
if ($cur->__get('comment_spam_filter') == 'UnmoderatedWhiteList' if ($cur->__get('comment_spam_filter') == 'UnmoderatedWhiteList'

View File

@ -49,7 +49,7 @@ class UnmoderatedWhiteList extends dcSpamFilter
{ {
if ($type != 'comment' if ($type != 'comment'
|| dcCore::app()->blog === null || dcCore::app()->blog === null
|| dcCore::app()->blog->settings->system->comments_pub) { || dcCore::app()->blog->settings->get('system')->get('comments_pub')) {
return null; return null;
} }
@ -90,7 +90,7 @@ class UnmoderatedWhiteList extends dcSpamFilter
$res = ''; $res = '';
if (dcCore::app()->blog->settings->system->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>';