Compare commits
No commits in common. "0d0e69ccc34f698530c789edd3c2a6bacdc0d562" and "e9a36181e804816d46f196f9ff74e803c1489e22" have entirely different histories.
0d0e69ccc3
...
e9a36181e8
14
CHANGELOG.md
14
CHANGELOG.md
|
@ -1,17 +1,3 @@
|
|||
1.5 - 2023.05.14
|
||||
- require dotclear 2.26
|
||||
- fix wrong repository version
|
||||
|
||||
1.4 - 2023.04.22
|
||||
- require dotclear 2.26
|
||||
- use latest dotclear namespace
|
||||
- use sql statement
|
||||
- fix nullsafe warnings
|
||||
|
||||
1.3 - 2023.03.26
|
||||
- require dotclear 2.26
|
||||
- use namespace
|
||||
|
||||
1.2 - 2022.12.07
|
||||
- update to dotclear 2.24
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
[![Release](https://img.shields.io/github/v/release/JcDenis/emailNotification)](https://github.com/JcDenis/emailNotification/releases)
|
||||
[![Date](https://img.shields.io/github/release-date/JcDenis/emailNotification)](https://github.com/JcDenis/emailNotification/releases)
|
||||
[![Issues](https://img.shields.io/github/issues/JcDenis/emailNotification)](https://github.com/JcDenis/emailNotification/issues)
|
||||
[![Dotclear](https://img.shields.io/badge/dotclear-v2.26-blue.svg)](https://fr.dotclear.org/download)
|
||||
[![Dotclear](https://img.shields.io/badge/dotclear-v2.24-blue.svg)](https://fr.dotclear.org/download)
|
||||
[![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://plugins.dotaddict.org/dc2/details/emailNotification)
|
||||
[![License](https://img.shields.io/github/license/JcDenis/emailNotification)](https://github.com/JcDenis/emailNotification/blob/master/LICENSE)
|
||||
|
||||
|
@ -18,7 +18,7 @@ It sends email when a new comment is done.
|
|||
|
||||
_Email notification_ requires:
|
||||
|
||||
* Dotclear 2.26
|
||||
* Dotclear 2.24
|
||||
* A working mail service
|
||||
|
||||
## USAGE
|
||||
|
|
10
_define.php
10
_define.php
|
@ -10,7 +10,7 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
if (!defined('DC_RC_PATH') || is_null(dcCore::app()->auth)) {
|
||||
if (!defined('DC_RC_PATH')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -18,12 +18,12 @@ $this->registerModule(
|
|||
'Email notification',
|
||||
'Email notification',
|
||||
'Olivier Meunier and contributors',
|
||||
'1.5',
|
||||
'1.2',
|
||||
[
|
||||
'requires' => [['core', '2.26']],
|
||||
'requires' => [['core', '2.24']],
|
||||
'permissions' => dcCore::app()->auth->makePermissions([
|
||||
dcCore::app()->auth::PERMISSION_USAGE,
|
||||
dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
|
||||
dcAuth::PERMISSION_USAGE,
|
||||
dcAuth::PERMISSION_CONTENT_ADMIN,
|
||||
]),
|
||||
'type' => 'plugin',
|
||||
'support' => 'https://github.com/JcDenis/emailNotification',
|
||||
|
|
|
@ -10,12 +10,8 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dotclear\Plugin\emailNotification;
|
||||
|
||||
use Dotclear\Module\MyPlugin;
|
||||
|
||||
class My extends MyPlugin
|
||||
{
|
||||
if (!defined('DC_RC_PATH')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Clearbricks::lib()->autoload(['emailNotificationBehaviors' => __DIR__ . '/inc/class.emailnotification.behaviors.php']);
|
|
@ -2,11 +2,11 @@
|
|||
<modules xmlns:da="http://dotaddict.org/da/">
|
||||
<module id="emailNotification">
|
||||
<name>Email notification</name>
|
||||
<version>1.5</version>
|
||||
<version>1.2</version>
|
||||
<author>Olivier Meunier and contributors</author>
|
||||
<desc>Email notification</desc>
|
||||
<file>https://github.com/JcDenis/emailNotification/releases/download/v1.5/plugin-emailNotification.zip</file>
|
||||
<da:dcmin>2.26</da:dcmin>
|
||||
<file>https://github.com/JcDenis/emailNotification/releases/download/v1.2/plugin-emailNotification.zip</file>
|
||||
<da:dcmin>2.24</da:dcmin>
|
||||
<da:details>https://plugins.dotaddict.org/dc2/details/emailNotification</da:details>
|
||||
<da:support>https://github.com/JcDenis/emailNotification</da:support>
|
||||
</module>
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @package Dotclear
|
||||
*
|
||||
* @copyright Olivier Meunier & Association Dotclear
|
||||
* @copyright GPL-2.0-only
|
||||
*/
|
||||
#
|
||||
# DOT NOT MODIFY THIS FILE !
|
||||
#
|
||||
|
||||
use Dotclear\Helper\L10n;
|
||||
|
||||
L10n::$locales['Never'] = 'Jamais';
|
||||
L10n::$locales['My entries'] = 'Mes billets';
|
||||
L10n::$locales['All entries'] = 'Tous les billets';
|
||||
L10n::$locales['Email notification'] = 'Notification par e-mail';
|
||||
L10n::$locales['Notify new comments by email:'] = 'Avertir des nouveaux commentaires par e-mail :';
|
||||
L10n::$locales['"%s" - New comment'] = '"%s" - Nouveau commentaire';
|
||||
L10n::$locales['Blog: %s'] = 'Blog : %s';
|
||||
L10n::$locales['Entry: %s <%s>'] = 'Billet : %s <%s>';
|
||||
L10n::$locales['Comment by: %s <%s>'] = 'Commentaire par : %s <%s>';
|
||||
L10n::$locales['Website: %s'] = 'Site web : %s';
|
||||
L10n::$locales['Comment status: %s'] = 'Statut du commentaire : %s';
|
||||
L10n::$locales['Edit this comment: <%s>'] = 'Modifier ce commentaire : %s';
|
||||
L10n::$locales['You must log in on the backend before clicking on this link to go directly to the comment.'] = 'Vous devez vous enregistrer sur l\'interface d\'administration avant de cliquer sur ce lien pour aller directement sur ce commentaire.';
|
||||
L10n::$locales['You received a new comment on your blog:'] = 'Vous avez reçu un nouveau commentaire sur votre blog :';
|
|
@ -10,33 +10,11 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dotclear\Plugin\emailNotification;
|
||||
|
||||
use dcCore;
|
||||
use Dotclear\Core\Process;
|
||||
|
||||
class Backend extends Process
|
||||
{
|
||||
public static function init(): bool
|
||||
{
|
||||
return self::status(My::checkContext(My::BACKEND));
|
||||
if (!defined('DC_CONTEXT_ADMIN')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function process(): bool
|
||||
{
|
||||
if (!self::status()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dcCore::app()->addBehaviors([
|
||||
'adminPreferencesFormV2' => [BackendBehaviors::class, 'adminUserForm'],
|
||||
'adminUserForm' => [BackendBehaviors::class, 'adminUserForm'],
|
||||
'adminBeforeUserUpdate' => [BackendBehaviors::class, 'adminBeforeUserUpdate'],
|
||||
'adminBeforeUserOptionsUpdate' => [BackendBehaviors::class, 'adminBeforeUserUpdate'],
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
dcCore::app()->addBehavior('adminPreferencesFormV2', ['emailNotificationBehaviors', 'adminUserForm']);
|
||||
dcCore::app()->addBehavior('adminUserForm', ['emailNotificationBehaviors', 'adminUserForm']);
|
||||
dcCore::app()->addBehavior('adminBeforeUserUpdate', ['emailNotificationBehaviors', 'adminBeforeUserUpdate']);
|
||||
dcCore::app()->addBehavior('adminBeforeUserOptionsUpdate', ['emailNotificationBehaviors', 'adminBeforeUserUpdate']);
|
||||
|
|
|
@ -10,42 +10,137 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
if (!defined('DC_RC_PATH')) {
|
||||
return;
|
||||
}
|
||||
|
||||
namespace Dotclear\Plugin\emailNotification;
|
||||
|
||||
use dcCore;
|
||||
use Dotclear\Database\Cursor;
|
||||
use Dotclear\Helper\Html\Form\{
|
||||
Label,
|
||||
Para,
|
||||
Select
|
||||
};
|
||||
|
||||
class BackendBehaviors
|
||||
class emailNotificationBehaviors
|
||||
{
|
||||
public static function adminUserForm(): void
|
||||
public static function adminUserForm()
|
||||
{
|
||||
$options = dcCore::app()->auth->getOptions();
|
||||
|
||||
echo
|
||||
'<div class="fieldset"><h5>' . __('Email notification') . '</h5>' .
|
||||
(new Para())->items([
|
||||
(new Label(__('Notify new comments by email:')))->for('notify_comments'),
|
||||
(new Select('notify_comments'))->default($options['notify_comments'] ?? '0')->items([
|
||||
'<p><label class="classic" for="notify_comments">' . __('Notify new comments by email:') . '</label> ' .
|
||||
form::combo(
|
||||
'notify_comments',
|
||||
[
|
||||
__('Never') => '0',
|
||||
__('My entries') => 'mine',
|
||||
__('All entries') => 'all',
|
||||
]),
|
||||
])->render() .
|
||||
],
|
||||
$options['notify_comments'] ?? '0'
|
||||
) .
|
||||
'</p>' .
|
||||
'</div>';
|
||||
}
|
||||
|
||||
public static function adminBeforeUserUpdate(Cursor $cur, string $user_id = ''): void
|
||||
public static function adminBeforeUserUpdate($cur, $user_id = '')
|
||||
{
|
||||
$opt = $cur->getField('user_options');
|
||||
$opt = is_null($opt) ? [] : $opt;
|
||||
$opt['notify_comments'] = $_POST['notify_comments'];
|
||||
$cur->setField('user_options', $opt);
|
||||
$cur->user_options['notify_comments'] = $_POST['notify_comments'];
|
||||
}
|
||||
|
||||
public static function publicAfterCommentCreate($cur, $comment_id)
|
||||
{
|
||||
# We don't want notification for spam
|
||||
if ($cur->comment_status == -2) {
|
||||
return;
|
||||
}
|
||||
|
||||
# Information on comment author and post author
|
||||
$rs = dcCore::app()->auth->sudo([dcCore::app()->blog, 'getComments'], ['comment_id' => $comment_id]);
|
||||
|
||||
if ($rs->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
# Information on blog users
|
||||
$strReq = 'SELECT U.user_id, user_email, user_options ' .
|
||||
'FROM ' . dcCore::app()->blog->prefix . dcAuth::USER_TABLE_NAME . ' U ' .
|
||||
'JOIN ' . dcCore::app()->blog->prefix . dcAuth::PERMISSIONS_TABLE_NAME . ' P ON U.user_id = P.user_id ' .
|
||||
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
|
||||
'UNION ' .
|
||||
'SELECT user_id, user_email, user_options ' .
|
||||
'FROM ' . dcCore::app()->blog->prefix . dcAuth::USER_TABLE_NAME . ' ' .
|
||||
'WHERE user_super = 1 ';
|
||||
|
||||
$users = dcCore::app()->con->select($strReq);
|
||||
|
||||
# Create notify list
|
||||
$ulist = [];
|
||||
while ($users->fetch()) {
|
||||
if (!$users->user_email) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$notification_pref = self::notificationPref(rsExtUser::options(new dcRecord($users)));
|
||||
|
||||
if ($notification_pref == 'all'
|
||||
|| ($notification_pref == 'mine' && $users->user_id == $rs->user_id)) {
|
||||
$ulist[$users->user_id] = $users->user_email;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($ulist) > 0) {
|
||||
# Author of the post wants to be notified by mail
|
||||
$headers = [
|
||||
'Reply-To: ' . $rs->comment_email,
|
||||
'Content-Type: text/plain; charset=UTF-8;',
|
||||
'X-Mailer: Dotclear',
|
||||
'X-Blog-Id: ' . mail::B64Header(dcCore::app()->blog->id),
|
||||
'X-Blog-Name: ' . mail::B64Header(dcCore::app()->blog->name),
|
||||
'X-Blog-Url: ' . mail::B64Header(dcCore::app()->blog->url),
|
||||
];
|
||||
|
||||
$subject = '[' . dcCore::app()->blog->name . '] ' . sprintf(__('"%s" - New comment'), $rs->post_title);
|
||||
$subject = mail::B64Header($subject);
|
||||
|
||||
$msg = preg_replace('%</p>\s*<p>%msu', "\n\n", $rs->comment_content);
|
||||
$msg = html::clean($msg);
|
||||
$msg = html_entity_decode($msg);
|
||||
|
||||
if ($cur->comment_status == 1) {
|
||||
$status = __('published');
|
||||
} elseif ($cur->comment_status == 0) {
|
||||
$status = __('unpublished');
|
||||
} elseif ($cur->comment_status == -1) {
|
||||
$status = __('pending');
|
||||
} else {
|
||||
# unknown status
|
||||
$status = $cur->comment_status;
|
||||
}
|
||||
|
||||
$msg .= "\n\n-- \n" .
|
||||
sprintf(__('Blog: %s'), dcCore::app()->blog->name) . "\n" .
|
||||
sprintf(__('Entry: %s <%s>'), $rs->post_title, $rs->getPostURL()) . "\n" .
|
||||
sprintf(__('Comment by: %s <%s>'), $rs->comment_author, $rs->comment_email) . "\n" .
|
||||
sprintf(__('Website: %s'), $rs->getAuthorURL()) . "\n" .
|
||||
sprintf(__('Comment status: %s'), $status) . "\n" .
|
||||
sprintf(__('Edit this comment: <%s>'), DC_ADMIN_URL .
|
||||
((substr(DC_ADMIN_URL, -1) != '/') ? '/' : '') .
|
||||
'comment.php?id=' . $cur->comment_id .
|
||||
'&switchblog=' . dcCore::app()->blog->id) . "\n" .
|
||||
__('You must log in on the backend before clicking on this link to go directly to the comment.');
|
||||
|
||||
$msg = __('You received a new comment on your blog:') . "\n\n" . $msg;
|
||||
|
||||
# --BEHAVIOR-- emailNotificationAppendToEmail
|
||||
$msg .= dcCore::app()->callBehavior('emailNotificationAppendToEmail', $cur);
|
||||
|
||||
foreach ($ulist as $email) {
|
||||
$h = array_merge(['From: ' . $email], $headers);
|
||||
mail::sendMail($email, $subject, $msg, $h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function notificationPref($o)
|
||||
{
|
||||
if (is_array($o) && isset($o['notify_comments'])) {
|
||||
return $o['notify_comments'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
155
src/Frontend.php
155
src/Frontend.php
|
@ -10,157 +10,8 @@
|
|||
* @copyright Jean-Christian Denis
|
||||
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Dotclear\Plugin\emailNotification;
|
||||
|
||||
use dcAuth;
|
||||
use dcBlog;
|
||||
use dcCore;
|
||||
use Dotclear\Core\Process;
|
||||
use Dotclear\Database\{
|
||||
Cursor,
|
||||
MetaRecord
|
||||
};
|
||||
use Dotclear\Database\Statement\{
|
||||
JoinStatement,
|
||||
SelectStatement
|
||||
};
|
||||
use Dotclear\Helper\Html\Html;
|
||||
use Dotclear\Helper\Network\Mail\Mail;
|
||||
use rsExtUser;
|
||||
|
||||
class Frontend extends Process
|
||||
{
|
||||
public static function init(): bool
|
||||
{
|
||||
return self::status(My::checkContext(My::FRONTEND));
|
||||
if (!defined('DC_RC_PATH')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function process(): bool
|
||||
{
|
||||
if (!self::status()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
dcCore::app()->addBehavior('publicAfterCommentCreate', function (Cursor $cur, ?int $comment_id): void {
|
||||
// nullsafe PHP < 8.0
|
||||
if (is_null(dcCore::app()->blog)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We don't want notification for spam
|
||||
if ((int) $cur->getField('comment_status') == dcBlog::COMMENT_JUNK) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Information on comment author and post author
|
||||
$rs = dcCore::app()->auth->sudo([dcCore::app()->blog, 'getComments'], ['comment_id' => $comment_id]);
|
||||
if (is_null($rs) || $rs->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = new SelectStatement();
|
||||
$users = $sql->from($sql->as(dcCore::app()->blog->prefix . dcAuth::USER_TABLE_NAME, 'U'))
|
||||
->columns([
|
||||
'U.user_id as user_id',
|
||||
'user_email',
|
||||
'user_options',
|
||||
])
|
||||
->join(
|
||||
(new JoinStatement())
|
||||
->from($sql->as(dcCore::app()->blog->prefix . dcAuth::PERMISSIONS_TABLE_NAME, 'P'))
|
||||
->on('U.user_id = P.user_id')
|
||||
->statement()
|
||||
)
|
||||
->where('blog_id = ' . $sql->quote(dcCore::app()->blog->id))
|
||||
->union(
|
||||
(new SelectStatement())
|
||||
->columns([
|
||||
'U.user_id as user_id',
|
||||
'user_email',
|
||||
'user_options',
|
||||
])
|
||||
->from($sql->as(dcCore::app()->blog->prefix . dcAuth::USER_TABLE_NAME, 'U'))
|
||||
->where('user_super = 1')
|
||||
->statement()
|
||||
)
|
||||
->select();
|
||||
|
||||
if (is_null($users) || $users->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create notify list
|
||||
$ulist = [];
|
||||
while ($users->fetch()) {
|
||||
if (!$users->f('user_email')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$o = rsExtUser::options($users);
|
||||
$notification_pref = is_array($o) && isset($o['notify_comments']) ? $o['notify_comments'] : null;
|
||||
unset($o);
|
||||
|
||||
if ($notification_pref == 'all'
|
||||
|| ($notification_pref == 'mine' && $users->f('user_id') == $rs->f('user_id'))) {
|
||||
$ulist[$users->f('user_id')] = $users->f('user_email');
|
||||
}
|
||||
}
|
||||
|
||||
if (count($ulist) > 0) {
|
||||
// Author of the post wants to be notified by mail
|
||||
$headers = [
|
||||
'Reply-To: ' . $rs->f('comment_email'),
|
||||
'Content-Type: text/plain; charset=UTF-8;',
|
||||
'X-Mailer: Dotclear',
|
||||
'X-Blog-Id: ' . Mail::B64Header(dcCore::app()->blog->id),
|
||||
'X-Blog-Name: ' . Mail::B64Header(dcCore::app()->blog->name),
|
||||
'X-Blog-Url: ' . Mail::B64Header(dcCore::app()->blog->url),
|
||||
];
|
||||
|
||||
$subject = '[' . dcCore::app()->blog->name . '] ' . sprintf(__('"%s" - New comment'), $rs->f('post_title'));
|
||||
$subject = Mail::B64Header($subject);
|
||||
|
||||
$msg = preg_replace('%</p>\s*<p>%msu', "\n\n", $rs->f('comment_content'));
|
||||
$msg = Html::clean($msg);
|
||||
$msg = html_entity_decode($msg);
|
||||
|
||||
if ((int) $cur->getField('comment_status') == dcBlog::COMMENT_PUBLISHED) {
|
||||
$status = __('published');
|
||||
} elseif ((int) $cur->getField('comment_status') == dcBlog::COMMENT_UNPUBLISHED) {
|
||||
$status = __('unpublished');
|
||||
} elseif ((int) $cur->getField('comment_status') == dcBlog::COMMENT_PENDING) {
|
||||
$status = __('pending');
|
||||
} else {
|
||||
// unknown status
|
||||
$status = $cur->getField('comment_status');
|
||||
}
|
||||
|
||||
$msg .= "\n\n-- \n" .
|
||||
sprintf(__('Blog: %s'), dcCore::app()->blog->name) . "\n" .
|
||||
sprintf(__('Entry: %s <%s>'), $rs->f('post_title'), $rs->getPostURL()) . "\n" .
|
||||
sprintf(__('Comment by: %s <%s>'), $rs->f('comment_author'), $rs->f('comment_email')) . "\n" .
|
||||
sprintf(__('Website: %s'), $rs->getAuthorURL()) . "\n" .
|
||||
sprintf(__('Comment status: %s'), $status) . "\n" .
|
||||
sprintf(__('Edit this comment: <%s>'), DC_ADMIN_URL .
|
||||
((substr(DC_ADMIN_URL, -1) != '/') ? '/' : '') .
|
||||
'comment.php?id=' . $cur->getField('comment_id') .
|
||||
'&switchblog=' . dcCore::app()->blog->id) . "\n" .
|
||||
__('You must log in on the backend before clicking on this link to go directly to the comment.');
|
||||
|
||||
$msg = __('You received a new comment on your blog:') . "\n\n" . $msg;
|
||||
|
||||
// --BEHAVIOR-- emailNotificationAppendToEmail -- Cursor
|
||||
$msg .= dcCore::app()->callBehavior('emailNotificationAppendToEmail', $cur);
|
||||
|
||||
foreach ($ulist as $email) {
|
||||
$h = array_merge(['From: ' . $email], $headers);
|
||||
Mail::sendMail($email, $subject, $msg, $h);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
dcCore::app()->addBehavior('publicAfterCommentCreate', ['emailNotificationBehaviors', 'publicAfterCommentCreate']);
|
||||
|
|
Loading…
Reference in New Issue