whiteListCom/inc/Core.php

171 lines
4.3 KiB
PHP
Raw Normal View History

2021-08-17 19:06:58 +00:00
<?php
2021-09-02 21:54:55 +00:00
/**
* @brief whiteListCom, a plugin for Dotclear 2
2022-11-16 21:22:48 +00:00
*
2021-09-02 21:54:55 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-16 21:22:48 +00:00
*
2021-09-02 21:54:55 +00:00
* @author Jean-Christian Denis and Contributors
2022-11-16 21:22:48 +00:00
*
2021-09-02 21:54:55 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-17 19:06:58 +00:00
if (!defined('DC_RC_PATH')) {
return null;
2021-08-17 19:06:58 +00:00
}
/**
* @ingroup DC_PLUGIN_WHITELISTCOM
* @brief White list filters methods
* @since 2.6
*/
class whiteListCom
{
public $con;
public $blog;
public $settings;
2022-11-16 21:22:48 +00:00
private $unmoderated = [];
private $reserved = [];
2022-11-16 21:22:48 +00:00
public function __construct()
{
2023-01-07 22:28:40 +00:00
$this->con = dcCore::app()->con;
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
$this->settings = dcCore::app()->blog->settings->whiteListCom;
2022-11-16 21:22:48 +00:00
$unmoderated = $this->settings->whiteListCom_unmoderated;
$this->unmoderated = self::decode($unmoderated);
2023-01-07 22:28:40 +00:00
$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',
2022-11-16 21:22:48 +00:00
true,
false
);
2021-09-02 21:54:55 +00:00
$this->settings->put(
'whiteListCom_reserved',
self::encode($this->reserved),
'string',
'Whitelist of reserved names on comments',
2022-11-16 21:22:48 +00:00
true,
false
);
}
2022-11-16 21:22:48 +00:00
# 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;
}
2022-11-16 21:22:48 +00:00
return null;
}
# You must do a commit to save this change
public function addReserved($author, $email)
{
$this->reserved[$author] = $email;
2022-11-16 21:22:48 +00:00
return true;
}
# You must do a commit to save this change
public function emptyReserved()
{
2022-11-16 21:22:48 +00:00
$this->reserved = [];
}
2022-11-16 21:22:48 +00:00
# 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 addUnmoderated($email)
{
2022-11-16 21:22:48 +00:00
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()
{
2022-11-16 21:22:48 +00:00
$this->unmoderated = [];
}
public function getPostsUsers()
{
2022-11-16 21:22:48 +00:00
$users = [];
$rs = dcCore::app()->blog->getPostsUsers();
while ($rs->fetch()) {
$name = dcUtils::getUserCN(
2023-01-07 22:28:40 +00:00
$rs->__get('user_id'),
$rs->__get('user_name'),
$rs->__get('user_firstname'),
$rs->__get('user_displayname')
);
2022-11-16 21:22:48 +00:00
$users[] = [
'name' => $name,
2023-01-07 22:28:40 +00:00
'email' => $rs->__get('user_email'),
2022-11-16 21:22:48 +00:00
];
}
return $users;
}
public function getCommentsUsers()
{
2022-11-16 21:22:48 +00:00
$users = [];
$rs = $this->con->select(
'SELECT comment_author, comment_email ' .
2022-11-16 21:22:48 +00:00
'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
);
2022-11-16 21:22:48 +00:00
while ($rs->fetch()) {
$users[] = [
2023-01-07 22:28:40 +00:00
'name' => $rs->__get('comment_author'),
'email' => $rs->__get('comment_email'),
2022-11-16 21:22:48 +00:00
];
}
return $users;
}
public static function encode($x)
{
2022-11-16 21:22:48 +00:00
$y = is_array($x) ? $x : [];
return base64_encode(serialize($y));
}
public static function decode($x)
{
$y = @unserialize(@base64_decode($x));
2022-11-16 21:22:48 +00:00
return is_array($y) ? $y : [];
}
2021-08-17 19:06:58 +00:00
}