use namespace

This commit is contained in:
Jean-Christian Paul Denis 2023-04-10 10:14:10 +02:00
parent dde8f66b54
commit 2c10ad8e02
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
6 changed files with 590 additions and 437 deletions

View File

@ -10,12 +10,49 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
declare(strict_types=1);
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function (dcSettings $blog_settings) {
$s = $blog_settings->addNamespace(basename(__DIR__));
namespace Dotclear\Plugin\disclaimer;
use dcCore;
use dcPage;
use dcNsProcess;
use dcSettings;
use Dotclear\Helper\Html\Form\{
Checkbox,
Div,
Label,
Input,
Note,
Para,
Text,
Textarea
};
use Dotclear\Helper\Html\Html;
use Exception;
class Backend extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
]), dcCore::app()->blog->id);
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminBeforeBlogSettingsUpdate' => function (dcSettings $blog_settings): void {
$s = $blog_settings->get(My::id());
try {
$s->put('disclaimer_active', isset($_POST['disclaimer_active']));
@ -29,96 +66,66 @@ dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function (dcSettings
$s->drop('disclaimer_active');
$s->put('disclaimer_active', 0);
}
});
},
dcCore::app()->addBehavior('adminBlogPreferencesHeaders', function () {
'adminBlogPreferencesHeaders' => function (): string {
$editor = dcCore::app()->auth->getOption('editor');
return
dcCore::app()->callBehavior('adminPostEditor', $editor['xhtml'], 'disclaimer', ['#disclaimer_text'], 'xhtml') .
dcPage::jsModuleLoad(basename(__DIR__) . '/js/admin.js');
});
dcPage::jsModuleLoad(My::id() . '/js/backend.js');
},
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function (dcSettings $blog_settings) {
$s = $blog_settings->addNamespace(basename(__DIR__));
'adminBlogPreferencesFormV2' => function (dcSettings $blog_settings): void {
$s = $blog_settings->get(My::id());
$disclaimer_bots_agents = $s->get('disclaimer_bots_agents');
if (empty($disclaimer_bots_agents)) {
$disclaimer_bots_agents = 'bot;Scooter;Slurp;Voila;WiseNut;Fast;Index;Teoma;' .
'Mirago;search;find;loader;archive;Spider;Crawler';
$disclaimer_bots_agents = implode(';', My::DEFAULT_BOTS_AGENTS);
}
echo
'<div class="fieldset">' .
'<h4 id="disclaimerParam">' . __('Disclaimer') . '</h4>' .
'<div class="two-boxes">' .
(new Div())->class('fieldset')->items([
(new Text('h4', My::name()))->id('disclaimerParam'),
(new Div())->class('two-boxes even')->items([
(new Para())->items([
(new Checkbox('disclaimer_active', (bool) $s->get('disclaimer_active')))->value(1),
(new Label(__('Enable disclaimer'), Label::OUTSIDE_LABEL_AFTER))->for('disclaimer_active')->class('classic'),
]),
(new Para())->items([
(new Label(__('Title:')))->for('disclaimer_title'),
(new Input('disclaimer_title'))->size(30)->maxlenght(255)->value(Html::escapeHTML((string) $s->get('disclaimer_title'))),
]),
]),
(new Div())->class('two-boxes odd')->items([
(new Para())->items([
(new Checkbox('disclaimer_remember', (bool) $s->get('disclaimer_remember')))->value(1),
(new Label(__('Remember the visitor'), Label::OUTSIDE_LABEL_AFTER))->for('disclaimer_remember')->class('classic'),
]),
(new Para())->items([
(new Label(__('Link output:')))->for('disclaimer_redir'),
(new Input('disclaimer_redir'))->size(30)->maxlenght(255)->value(Html::escapeHTML((string) $s->get('disclaimer_redir'))),
]),
(new Note())->class('form-note')->text(__('Leave blank to redirect to the site Dotclear')),
]),
(new Div())->class('clear')->items([
(new Para())->items([
(new Label(__('Disclaimer:'), Label::OUTSIDE_LABEL_BEFORE))->for('disclaimer_text'),
(new Textarea('disclaimer_text', Html::escapeHTML((string) $s->get('disclaimer_text'))))->cols(60)->rows(5)->lang(dcCore::app()->blog->settings->get('system')->get('lang'))->spellcheck(true),
]),
(new Para())->items([
(new Label(__('List of robots allowed to index the site pages (separated by semicolons):')))->for('disclaimer_bots_agents'),
(new Input('disclaimer_bots_agents'))->size(120)->maxlenght(255)->value(Html::escapeHTML($disclaimer_bots_agents)),
]),
(new Para())->items([
(new Checkbox('disclaimer_bots_unactive', (bool) $s->get('disclaimer_bots_unactive')))->value(1),
(new Label(__('Disable the authorization of indexing by search engines'), Label::OUTSIDE_LABEL_AFTER))->for('disclaimer_bots_unactive')->class('classic'),
]),
]),
])->render();
},
]);
'<p><label class="classic" for="disclaimer_active">' .
form::checkbox(
'disclaimer_active',
'1',
(bool) $s->get('disclaimer_active')
) .
__('Enable disclaimer') . '</label></p>' .
'<p><label for="disclaimer_title">' . __('Title:') . '</label>' .
form::field(
'disclaimer_title',
30,
255,
html::escapeHTML((string) $s->get('disclaimer_title'))
) .
'</p>' .
'</div><div class="two-boxes">' .
'<p><label class="classic">' .
form::checkbox(
'disclaimer_remember',
'1',
(bool) $s->get('disclaimer_remember')
) .
__('Remember the visitor') . '</label></p>' .
'<p><label for="disclaimer_redir">' . __('Link output:') . '</label>' .
form::field(
'disclaimer_redir',
30,
255,
html::escapeHTML((string) $s->get('disclaimer_redir'))
) . '</p>' .
'<p class="form-note info">' . __('Leave blank to redirect to the site Dotclear') . '</p>' .
'</div><div class="clear">' .
'<p class="area"><label for="disclaimer_text">' . __('Disclaimer:') . '</label>' .
form::textarea(
'disclaimer_text',
60,
5,
[
'default' => html::escapeHTML((string) $s->get('disclaimer_text')),
'extra_html' => 'lang="' . dcCore::app()->blog->settings->get('system')->get('lang') . '" spellcheck="true"',
]
) . '</p>' .
'<p><label for="disclaimer_bots_agents">' . __('List of robots allowed to index the site pages (separated by semicolons):') . '</label>' .
form::field(
'disclaimer_bots_agents',
120,
255,
html::escapeHTML($disclaimer_bots_agents)
) . '</p>' .
'<p><label for="disclaimer_bots_unactive">' .
form::checkbox(
'disclaimer_bots_unactive',
'1',
(bool) $s->get('disclaimer_bots_unactive')
) .
__('Disable the authorization of indexing by search engines') .
'</label></p>' .
'</div>' .
'</div>';
});
return true;
}
}

View File

@ -10,188 +10,65 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
return null;
}
declare(strict_types=1);
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
namespace Dotclear\Plugin\disclaimer;
# Is active
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('disclaimer_active')) {
return null;
}
use ArrayObject;
use dcCore;
use dcNsProcess;
use dcUtils;
# Localized l10n
__('Disclaimer');
__('Accept terms of uses');
__('I agree');
__('I disagree');
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = My::phpCompliant();
# Templates
dcCore::app()->tpl->addValue('DisclaimerTitle', function ($attr) {
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
# Is active
if (!dcCore::app()->blog->settings->get(My::id())->get('disclaimer_active')) {
return false;
}
# Localized l10n
__('Disclaimer');
__('Accept terms of uses');
__('I agree');
__('I disagree');
# Templates
dcCore::app()->tpl->addValue('DisclaimerTitle', function (ArrayObject $attr): string {
return '<?php echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr),
'dcCore::app()->blog->settings->get("disclaimer")->get("disclaimer_title")'
) . '; ?>';
});
});
dcCore::app()->tpl->addValue('DisclaimerText', function ($attr) {
dcCore::app()->tpl->addValue('DisclaimerText', function (ArrayObject $attr): string {
return '<?php echo dcCore::app()->blog->settings->get("disclaimer")->get("disclaimer_text"); ?>';
});
});
dcCore::app()->tpl->addValue('DisclaimerFormURL', function ($attr) {
dcCore::app()->tpl->addValue('DisclaimerFormURL', function (ArrayObject $attr): string {
return '<?php dcCore::app()->blog->url; ?>';
});
});
# Behaviors
dcCore::app()->addBehavior('publicHeadContent', function () {
echo dcUtils::cssModuleLoad(basename(__DIR__) . '/css/disclaimer.css');
});
dcCore::app()->addBehavior(
'publicBeforeDocumentV2',
['urlDisclaimer', 'publicBeforeDocumentV2']
);
/**
* @ingroup DC_PLUGIN_DISCLAIMER
* @brief Public disclaimer - URL handler.
* @since 2.6
*/
class urlDisclaimer extends dcUrlHandlers
{
private const COOKIE_PREFIX = 'dc_disclaimer_cookie_';
private const SESSION_PREFIX = 'dc_disclaimer_sess_';
public static $default_bots_agents = [
'bot','Scooter','Slurp','Voila','WiseNut','Fast','Index','Teoma',
'Mirago','search','find','loader','archive','Spider','Crawler',
];
/**
* Remove public callbacks (and serve disclaimer css)
*
* @param array $args Arguments
*/
public static function overwriteCallbacks($args)
{
if ($args == 'disclaimer.css') {
self::serveDocument('disclaimer.css', 'text/css', false);
exit;
}
return null;
}
/**
* Check disclaimer
*/
public static function publicBeforeDocumentV2()
{
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
# Test user-agent to see if it is a bot
if (!$s->get('disclaimer_bots_unactive')) {
$bots_agents = $s->get('diclaimer_bots_agents');
$bots_agents = !$bots_agents ? self::$default_bots_agents : explode(';', $bots_agents);
$is_bot = false;
foreach ($bots_agents as $bot) {
if (stristr($_SERVER['HTTP_USER_AGENT'], $bot)) {
$is_bot = true;
}
}
if ($is_bot) {
return null;
}
}
# Set default-templates path for disclaimer files
$tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset');
if (!empty($tplset) && is_dir(__DIR__ . '/default-templates/' . $tplset)) {
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates/' . $tplset);
} else {
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), __DIR__ . '/default-templates/' . DC_DEFAULT_TPLSET);
}
# New URL handler
$urlHandler = new urlHandler();
$urlHandler->mode = dcCore::app()->url->mode;
$urlHandler->registerDefault([
'urlDisclaimer',
'overwriteCallbacks',
# Behaviors
dcCore::app()->addBehaviors([
'publicHeadContent' => function (): void {
echo dcUtils::cssModuleLoad(My::id() . '/css/disclaimer.css');
},
'publicBeforeDocumentV2' => [UrlHandler::class, 'publicBeforeDocumentV2'],
]);
# Create session
$session = new sessionDB(
dcCore::app()->con,
dcCore::app()->prefix . 'session',
self::SESSION_PREFIX . dcCore::app()->blog->id,
'/'
);
$session->start();
# Remove all URLs representations
foreach (dcCore::app()->url->getTypes() as $k => $v) {
$urlHandler->register(
$k,
$v['url'],
$v['representation'],
['urlDisclaimer', 'overwriteCallbacks']
);
}
# Get type
$urlHandler->getDocument();
$type = $urlHandler->type;
unset($urlHandler);
# Test cookie
$cookie_name = self::COOKIE_PREFIX . dcCore::app()->blog->id;
$cookie_value = empty($_COOKIE[$cookie_name]) || !$s->get('disclaimer_remember') ?
false : ($_COOKIE[$cookie_name]) == 1;
# User say "disagree" so go away
if (isset($_POST['disclaimerdisagree'])) {
$session->destroy();
if ($s->get('disclaimer_remember')) {
setcookie($cookie_name, '0', time() - 86400, '/');
}
$redir = $s->get('disclaimer_redir');
if (!$redir) {
$redir = 'http://www.dotclear.org';
}
http::redirect($redir);
exit;
}
# Check if user say yes before
elseif (!isset($_SESSION['sess_blog_disclaimer'])
|| $_SESSION['sess_blog_disclaimer'] != 1
) {
if ($s->get('disclaimer_remember')
&& $cookie_value != false
) {
$_SESSION['sess_blog_disclaimer'] = 1;
return null;
}
if (!empty($_POST['disclaimeragree'])) {
$_SESSION['sess_blog_disclaimer'] = 1;
if ($s->get('disclaimer_remember')) {
setcookie($cookie_name, '1', time() + 31536000, '/');
}
return null;
}
$session->destroy();
self::serveDocument('disclaimer.html', 'text/html', false);
exit;
}
return null;
return true;
}
}

View File

@ -10,12 +10,33 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
declare(strict_types=1);
// Module specs
$mod_conf = [
namespace Dotclear\Plugin\disclaimer;
use dcCore;
use dcNsProcess;
use Exception;
class Install extends dcNsProcess
{
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
// Module specs
$mod_conf = [
[
'disclaimer_active',
'Enable disclaimer plugin',
@ -75,22 +96,12 @@ $mod_conf = [
]),
'string',
],
];
// Nothing to change below
try {
// Version
if (!dcCore::app()->newVersion(
basename(__DIR__),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
)) {
return null;
}
];
try {
// Settings
dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
foreach ($mod_conf as $v) {
dcCore::app()->blog->settings->get(basename(__DIR__))->put(
dcCore::app()->blog->settings->get(My::id())->put(
$v[0],
$v[2],
$v[3],
@ -101,8 +112,10 @@ try {
}
return true;
} catch (Exception $e) {
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
return false;
return true;
}
}

83
src/My.php Normal file
View File

@ -0,0 +1,83 @@
<?php
/**
* @brief disclaimer, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis, Pierre Van Glabeke
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\disclaimer;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '7.4';
/** @var array Default list of bots agents */
public const DEFAULT_BOTS_AGENTS = [
'bot',
'Scooter',
'Slurp',
'Voila',
'WiseNut',
'Fast',
'Index',
'Teoma',
'Mirago',
'search',
'find',
'loader',
'archive',
'Spider',
'Crawler',
];
/** @var string disclaimer specific cookie prefix */
public const COOKIE_PREFIX = 'dc_disclaimer_cookie_';
/** @var string disclaimer specific session prefix */
public const SESSION_PREFIX = 'dc_disclaimer_sess_';
/**
* 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'));
}
/**
* This mdoule directory
*/
public static function root(): string
{
return dirname(__DIR__);
}
/**
* Check php version
*/
public static function phpCompliant(): bool
{
return version_compare(phpversion(), self::PHP_MIN, '>=');
}
}

View File

@ -10,72 +10,93 @@
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
declare(strict_types=1);
$this->addUserAction(
namespace Dotclear\Plugin\disclaimer;
class Uninstall
{
protected static bool $init = false;
public static function init(): bool
{
self::$init = defined('DC_RC_PATH');
return self::$init;
}
public static function process($uninstaller): ?bool
{
if (!self::$init) {
return false;
}
$uninstaller->addUserAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
basename(__DIR__),
My::id(),
/* desc */
__('delete all settings')
);
);
$this->addUserAction(
$uninstaller->addUserAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
basename(__DIR__),
My::id(),
/* desc */
__('delete plugin files')
);
);
$this->addUserAction(
$uninstaller->addUserAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
basename(__DIR__),
My::id(),
/* desc */
__('delete the version number')
);
);
$this->addDirectAction(
$uninstaller->addDirectAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
basename(__DIR__),
My::id(),
/* desc */
sprintf(__('delete all %s settings'), basename(__DIR__))
);
sprintf(__('delete all %s settings'), My::id())
);
$this->addDirectAction(
$uninstaller->addDirectAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
basename(__DIR__),
My::id(),
/* desc */
sprintf(__('delete %s plugin files'), basename(__DIR__))
);
sprintf(__('delete %s plugin files'), My::id())
);
$this->addDirectAction(
$uninstaller->addDirectAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
basename(__DIR__),
My::id(),
/* desc */
sprintf(__('delete %s version number'), basename(__DIR__))
);
sprintf(__('delete %s version number'), My::id())
);
return true;
}
}

152
src/UrlHandler.php Normal file
View File

@ -0,0 +1,152 @@
<?php
/**
* @brief disclaimer, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis, Pierre Van Glabeke
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\disclaimer;
use dcCore;
use dcUrlHandlers;
use Dotclear\Helper\Network\Http;
use sessionDB;
use urlHandler as HelperHandler;
/**
* @ingroup DC_PLUGIN_DISCLAIMER
* @brief Public disclaimer - URL handler.
* @since 2.6
*/
class UrlHandler extends dcUrlHandlers
{
/**
* Remove public callbacks (and serve disclaimer css)
*
* @param null|string $args URL argument
*/
public static function overwriteCallbacks(?string $args): void
{
if ($args == 'disclaimer.css') {
self::serveDocument('disclaimer.css', 'text/css', false);
exit;
}
}
/**
* Check disclaimer
*/
public static function publicBeforeDocumentV2(): void
{
$s = dcCore::app()->blog->settings->get(My::id());
# Test user-agent to see if it is a bot
if (!$s->get('disclaimer_bots_unactive')) {
$bots_agents = $s->get('diclaimer_bots_agents');
$bots_agents = !$bots_agents ? My::DEFAULT_BOTS_AGENTS : explode(';', $bots_agents);
$is_bot = false;
foreach ($bots_agents as $bot) {
if (stristr($_SERVER['HTTP_USER_AGENT'], $bot)) {
$is_bot = true;
}
}
if ($is_bot) {
return;
}
}
# Set default-templates path for disclaimer files
$tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset');
if (!empty($tplset) && is_dir(My::root() . '/default-templates/' . $tplset)) {
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), My::root() . '/default-templates/' . $tplset);
} else {
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), My::root() . '/default-templates/' . DC_DEFAULT_TPLSET);
}
# New URL handler
$urlHandler = new HelperHandler();
$urlHandler->mode = dcCore::app()->url->mode;
$urlHandler->registerDefault([
self::class,
'overwriteCallbacks',
]);
# Create session
$session = new sessionDB(
dcCore::app()->con,
dcCore::app()->prefix . 'session',
My::SESSION_PREFIX . dcCore::app()->blog->id,
'/'
);
$session->start();
# Remove all URLs representations
foreach (dcCore::app()->url->getTypes() as $k => $v) {
$urlHandler->register(
$k,
$v['url'],
$v['representation'],
[self::class, 'overwriteCallbacks']
);
}
# Get type
$urlHandler->getDocument();
$type = $urlHandler->type;
unset($urlHandler);
# Test cookie
$cookie_name = My::COOKIE_PREFIX . dcCore::app()->blog->id;
$cookie_value = empty($_COOKIE[$cookie_name]) || !$s->get('disclaimer_remember') ?
false : ($_COOKIE[$cookie_name]) == 1;
# User say "disagree" so go away
if (isset($_POST['disclaimerdisagree'])) {
$session->destroy();
if ($s->get('disclaimer_remember')) {
setcookie($cookie_name, '0', time() - 86400, '/');
}
$redir = $s->get('disclaimer_redir');
if (!$redir) {
$redir = 'http://www.dotclear.org';
}
Http::redirect($redir);
exit;
}
# Check if user say yes before
elseif (!isset($_SESSION['sess_blog_disclaimer'])
|| $_SESSION['sess_blog_disclaimer'] != 1
) {
if ($s->get('disclaimer_remember')
&& $cookie_value != false
) {
$_SESSION['sess_blog_disclaimer'] = 1;
return;
}
if (!empty($_POST['disclaimeragree'])) {
$_SESSION['sess_blog_disclaimer'] = 1;
if ($s->get('disclaimer_remember')) {
setcookie($cookie_name, '1', time() + 31536000, '/');
}
return;
}
$session->destroy();
self::serveDocument('disclaimer.html', 'text/html', false);
exit;
}
}
}