use namespace

master
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,115 +10,122 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
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']));
$s->put('disclaimer_remember', isset($_POST['disclaimer_remember']));
$s->put('disclaimer_redir', $_POST['disclaimer_redir'] ?? '');
$s->put('disclaimer_title', $_POST['disclaimer_title'] ?? '');
$s->put('disclaimer_text', $_POST['disclaimer_text'] ?? '');
$s->put('disclaimer_bots_unactive', isset($_POST['disclaimer_bots_unactive']));
$s->put('disclaimer_bots_agents', $_POST['disclaimer_bots_agents'] ?? '');
} catch (Exception $e) {
$s->drop('disclaimer_active');
$s->put('disclaimer_active', 0);
}
},
'adminBlogPreferencesHeaders' => function (): string {
$editor = dcCore::app()->auth->getOption('editor');
return
dcCore::app()->callBehavior('adminPostEditor', $editor['xhtml'], 'disclaimer', ['#disclaimer_text'], 'xhtml') .
dcPage::jsModuleLoad(My::id() . '/js/backend.js');
},
'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 = implode(';', My::DEFAULT_BOTS_AGENTS);
}
echo
(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();
},
]);
return true;
}
} }
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function (dcSettings $blog_settings) {
$s = $blog_settings->addNamespace(basename(__DIR__));
try {
$s->put('disclaimer_active', isset($_POST['disclaimer_active']));
$s->put('disclaimer_remember', isset($_POST['disclaimer_remember']));
$s->put('disclaimer_redir', $_POST['disclaimer_redir'] ?? '');
$s->put('disclaimer_title', $_POST['disclaimer_title'] ?? '');
$s->put('disclaimer_text', $_POST['disclaimer_text'] ?? '');
$s->put('disclaimer_bots_unactive', isset($_POST['disclaimer_bots_unactive']));
$s->put('disclaimer_bots_agents', $_POST['disclaimer_bots_agents'] ?? '');
} catch (Exception $e) {
$s->drop('disclaimer_active');
$s->put('disclaimer_active', 0);
}
});
dcCore::app()->addBehavior('adminBlogPreferencesHeaders', function () {
$editor = dcCore::app()->auth->getOption('editor');
return
dcCore::app()->callBehavior('adminPostEditor', $editor['xhtml'], 'disclaimer', ['#disclaimer_text'], 'xhtml') .
dcPage::jsModuleLoad(basename(__DIR__) . '/js/admin.js');
});
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function (dcSettings $blog_settings) {
$s = $blog_settings->addNamespace(basename(__DIR__));
$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';
}
echo
'<div class="fieldset">' .
'<h4 id="disclaimerParam">' . __('Disclaimer') . '</h4>' .
'<div class="two-boxes">' .
'<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>';
});

View File

@ -10,188 +10,65 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
}
dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); namespace Dotclear\Plugin\disclaimer;
# Is active use ArrayObject;
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('disclaimer_active')) { use dcCore;
return null; use dcNsProcess;
} use dcUtils;
# Localized l10n class Frontend extends dcNsProcess
__('Disclaimer');
__('Accept terms of uses');
__('I agree');
__('I disagree');
# Templates
dcCore::app()->tpl->addValue('DisclaimerTitle', function ($attr) {
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) {
return '<?php echo dcCore::app()->blog->settings->get("disclaimer")->get("disclaimer_text"); ?>';
});
dcCore::app()->tpl->addValue('DisclaimerFormURL', function ($attr) {
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_'; public static function init(): bool
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') { static::$init = My::phpCompliant();
self::serveDocument('disclaimer.css', 'text/css', false);
exit;
}
return null; return static::$init;
} }
/** public static function process(): bool
* Check disclaimer
*/
public static function publicBeforeDocumentV2()
{ {
$s = dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); if (!static::$init) {
return false;
# 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 # Is active
$tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset'); if (!dcCore::app()->blog->settings->get(My::id())->get('disclaimer_active')) {
if (!empty($tplset) && is_dir(__DIR__ . '/default-templates/' . $tplset)) { return false;
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 # Localized l10n
$urlHandler = new urlHandler(); __('Disclaimer');
$urlHandler->mode = dcCore::app()->url->mode; __('Accept terms of uses');
$urlHandler->registerDefault([ __('I agree');
'urlDisclaimer', __('I disagree');
'overwriteCallbacks',
# 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 (ArrayObject $attr): string {
return '<?php echo dcCore::app()->blog->settings->get("disclaimer")->get("disclaimer_text"); ?>';
});
dcCore::app()->tpl->addValue('DisclaimerFormURL', function (ArrayObject $attr): string {
return '<?php dcCore::app()->blog->url; ?>';
});
# Behaviors
dcCore::app()->addBehaviors([
'publicHeadContent' => function (): void {
echo dcUtils::cssModuleLoad(My::id() . '/css/disclaimer.css');
},
'publicBeforeDocumentV2' => [UrlHandler::class, 'publicBeforeDocumentV2'],
]); ]);
# Create session return true;
$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;
} }
} }

View File

@ -10,99 +10,112 @@
* @copyright Jean-Christian Denis * @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
}
// Module specs namespace Dotclear\Plugin\disclaimer;
$mod_conf = [
[
'disclaimer_active',
'Enable disclaimer plugin',
false,
'boolean',
],
[
'disclaimer_remember',
'Remember the visitor who has already accepted the terms',
false,
'boolean',
],
[
'disclaimer_redir',
'Redirection if disclaimer is refused',
'https://www.google.fr',
'string',
],
[
'disclaimer_title',
'Title for disclaimer',
'Disclaimer',
'string',
],
[
'disclaimer_text',
'Description for disclaimer',
__('<p>You must accept this term before entering</p>'),
//'You must accept this term before entering',
'string',
],
[
'disclaimer_bots_unactive',
'Bypass disclaimer for bots',
false,
'boolean',
],
[
'disclaimer_bots_agents',
'List of know bots',
implode(';', [
'bot',
'Scooter',
'Slurp',
'Voila',
'WiseNut',
'Fast',
'Index',
'Teoma',
'Mirago',
'search',
'find',
'loader',
'archive',
'Spider',
'Crawler',
]),
'string',
],
];
// Nothing to change below use dcCore;
try { use dcNsProcess;
// Version use Exception;
if (!dcCore::app()->newVersion(
basename(__DIR__), class Install extends dcNsProcess
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') {
)) { public static function init(): bool
return null; {
static::$init = defined('DC_CONTEXT_ADMIN')
&& My::phpCompliant()
&& dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version'));
return static::$init;
} }
// Settings public static function process(): bool
dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); {
foreach ($mod_conf as $v) { if (!static::$init) {
dcCore::app()->blog->settings->get(basename(__DIR__))->put( return false;
$v[0], }
$v[2],
$v[3], // Module specs
$v[1], $mod_conf = [
false, [
true 'disclaimer_active',
); 'Enable disclaimer plugin',
false,
'boolean',
],
[
'disclaimer_remember',
'Remember the visitor who has already accepted the terms',
false,
'boolean',
],
[
'disclaimer_redir',
'Redirection if disclaimer is refused',
'https://www.google.fr',
'string',
],
[
'disclaimer_title',
'Title for disclaimer',
'Disclaimer',
'string',
],
[
'disclaimer_text',
'Description for disclaimer',
__('<p>You must accept this term before entering</p>'),
//'You must accept this term before entering',
'string',
],
[
'disclaimer_bots_unactive',
'Bypass disclaimer for bots',
false,
'boolean',
],
[
'disclaimer_bots_agents',
'List of know bots',
implode(';', [
'bot',
'Scooter',
'Slurp',
'Voila',
'WiseNut',
'Fast',
'Index',
'Teoma',
'Mirago',
'search',
'find',
'loader',
'archive',
'Spider',
'Crawler',
]),
'string',
],
];
try {
// Settings
foreach ($mod_conf as $v) {
dcCore::app()->blog->settings->get(My::id())->put(
$v[0],
$v[2],
$v[3],
$v[1],
false,
true
);
}
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
return true;
} }
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
} }
return false;

83
src/My.php 100644
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 Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
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 */
My::id(),
/* desc */
__('delete all settings')
);
$uninstaller->addUserAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
My::id(),
/* desc */
__('delete plugin files')
);
$uninstaller->addUserAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
My::id(),
/* desc */
__('delete the version number')
);
$uninstaller->addDirectAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
My::id(),
/* desc */
sprintf(__('delete all %s settings'), My::id())
);
$uninstaller->addDirectAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
My::id(),
/* desc */
sprintf(__('delete %s plugin files'), My::id())
);
$uninstaller->addDirectAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
My::id(),
/* desc */
sprintf(__('delete %s version number'), My::id())
);
return true;
}
} }
$this->addUserAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
basename(__DIR__),
/* desc */
__('delete all settings')
);
$this->addUserAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
basename(__DIR__),
/* desc */
__('delete plugin files')
);
$this->addUserAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
basename(__DIR__),
/* desc */
__('delete the version number')
);
$this->addDirectAction(
/* type */
'settings',
/* action */
'delete_all',
/* ns */
basename(__DIR__),
/* desc */
sprintf(__('delete all %s settings'), basename(__DIR__))
);
$this->addDirectAction(
/* type */
'plugins',
/* action */
'delete',
/* ns */
basename(__DIR__),
/* desc */
sprintf(__('delete %s plugin files'), basename(__DIR__))
);
$this->addDirectAction(
/* type */
'versions',
/* action */
'delete',
/* ns */
basename(__DIR__),
/* desc */
sprintf(__('delete %s version number'), basename(__DIR__))
);

152
src/UrlHandler.php 100644
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;
}
}
}