use namespace

main
Jean-Christian Paul Denis 2023-04-10 19:06:11 +02:00
parent dd8f7280d4
commit 589adf75ce
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
5 changed files with 399 additions and 226 deletions

View File

@ -10,32 +10,61 @@
* @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;
}
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( namespace Dotclear\Plugin\construction;
__('Construction'),
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
urldecode(dcPage::getPF(basename(__DIR__) . '/icon.png')),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->id),
dcCore::app()->blog->settings->get(basename(__DIR__))->get('flag') ? 'construction-blog' : ''
);
dcCore::app()->addBehaviors([ use dcAdmin;
'adminPageHTMLHead' => function () { use dcCore;
if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('flag')) { use dcPage;
echo dcPage::cssModuleLoad(basename(__DIR__) . '/css/admin.css'); use dcFavorites;
use dcNsProcess;
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;
} }
},
'adminDashboardFavoritesV2' => function (dcFavorites $favs) { dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
$favs->register(basename(__DIR__), [ My::name(),
'title' => __('Construction'), dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), dcPage::getPF(My::id() . '/icon.png'),
'small-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon.png')), preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
'large-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon-big.png')), dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcCore::app()->auth::PERMISSION_ADMIN]), dcCore::app()->blog->id),
'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), dcCore::app()->blog->settings->get(My::id())->get('flag') ? 'construction-blog' : ''
);
dcCore::app()->addBehaviors([
'adminPageHTMLHead' => function (): void {
if (dcCore::app()->blog->settings->get(My::id())->get('flag')) {
echo dcPage::cssModuleLoad(My::id() . '/css/backend.css');
}
},
'adminDashboardFavoritesV2' => function (dcFavorites $favs): void {
$favs->register(My::id(), [
'title' => My::name(),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'small-icon' => dcPage::getPF(My::id() . '/icon.png'),
'large-icon' => dcPage::getPF(My::id() . '/icon-big.png'),
'permissions' => dcCore::app()->auth->makePermissions([dcCore::app()->auth::PERMISSION_ADMIN]),
]);
},
]); ]);
},
]); return true;
}
}

View File

@ -10,66 +10,91 @@
* @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()->addBehavior('publicBeforeDocument', function () { namespace Dotclear\Plugin\construction;
if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('flag')) {
return; use ArrayObject;
use dcCore;
use dcNsProcess;
use Dotclear\Helper\Network\Http;
use Exception;
class Frontend extends dcNsProcess
{
public static function init(): bool
{
static::$init = My::phpCompliant();
return static::$init;
} }
$tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset'); public static function process(): bool
if (!empty($tplset) && is_dir(implode(DIRECTORY_SEPARATOR, [__DIR__, 'default-templates', $tplset]))) { {
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [__DIR__, 'default-templates', $tplset])); if (!static::$init) {
} else { return false;
dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [__DIR__, 'default-templates', DC_DEFAULT_TPLSET])); }
}
$all_allowed_ip = json_decode(dcCore::app()->blog->settings->get(basename(__DIR__))->get('allowed_ip'), true); dcCore::app()->addBehavior('publicBeforeDocument', function (): void {
if (!is_array($all_allowed_ip)) { if (!dcCore::app()->blog->settings->get(My::id())->get('flag')) {
$all_allowed_ip = []; return;
} }
$extra_urls = json_decode(dcCore::app()->blog->settings->get(basename(__DIR__))->get('extra_urls'), true);
if (!in_array(http::realIP(), $all_allowed_ip)) {
dcCore::app()->url->registerDefault(function ($args) {
dcCore::app()->url->type = 'default';
throw new Exception('Blog under construction', 503); $tplset = dcCore::app()->themes->moduleInfo(dcCore::app()->blog->settings->get('system')->get('theme'), 'tplset');
}); if (!empty($tplset) && is_dir(implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', $tplset]))) {
dcCore::app()->url->registerError(function ($args, $type, $e) { dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', $tplset]));
header('Content-Type: text/html; charset=UTF-8'); } else {
http::head(503, 'Service Unavailable'); dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', DC_DEFAULT_TPLSET]));
dcCore::app()->url->type = '503'; }
dcCore::app()->ctx->__set('current_tpl', '503.html');
dcCore::app()->ctx->__set('content_type', 'text/html');
echo dcCore::app()->tpl->getData(dcCore::app()->ctx->__get('current_tpl')); $all_allowed_ip = json_decode(dcCore::app()->blog->settings->get(My::id())->get('allowed_ip'), true);
if (!is_array($all_allowed_ip)) {
$all_allowed_ip = [];
}
$extra_urls = json_decode(dcCore::app()->blog->settings->get(My::id())->get('extra_urls'), true);
if (!in_array(Http::realIP(), $all_allowed_ip)) {
dcCore::app()->url->registerDefault(function ($args) {
dcCore::app()->url->type = 'default';
# --BEHAVIOR-- publicAfterDocument
dcCore::app()->callBehavior('publicAfterDocumentV2');
exit;
});
foreach (dcCore::app()->url->getTypes() as $k => $v) {
if (($k != 'contactme') && !in_array($k, $extra_urls)) {
dcCore::app()->url->register($k, $v['url'], $v['representation'], function () {
throw new Exception('Blog under construction', 503); throw new Exception('Blog under construction', 503);
}); });
} dcCore::app()->url->registerError(function ($args, $type, $e) {
} header('Content-Type: text/html; charset=UTF-8');
} Http::head(503, 'Service Unavailable');
}); dcCore::app()->url->type = '503';
dcCore::app()->ctx->__set('current_tpl', '503.html');
dcCore::app()->ctx->__set('content_type', 'text/html');
dcCore::app()->tpl->addValue('ConstructionMessage', function ($attr) { echo dcCore::app()->tpl->getData(dcCore::app()->ctx->__get('current_tpl'));
return '<?php echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr), # --BEHAVIOR-- publicAfterDocument
'dcCore::app()->blog->settings->get("' . basename(__DIR__) . '")->get("message")' dcCore::app()->callBehavior('publicAfterDocumentV2');
) . '; ?>'; exit;
}); });
dcCore::app()->tpl->addValue('ConstructionTitle', function ($attr) {
return '<?php echo ' . sprintf( foreach (dcCore::app()->url->getTypes() as $k => $v) {
dcCore::app()->tpl->getFilters($attr), if (($k != 'contactme') && !in_array($k, $extra_urls)) {
'dcCore::app()->blog->settings->get("' . basename(__DIR__) . '")->get("title")' dcCore::app()->url->register($k, $v['url'], $v['representation'], function () {
) . '; ?>'; throw new Exception('Blog under construction', 503);
}); });
}
}
}
});
dcCore::app()->tpl->addValue('ConstructionMessage', function (ArrayObject $attr): string {
return '<?php echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr),
'dcCore::app()->blog->settings->get("' . My::id() . '")->get("message")'
) . '; ?>';
});
dcCore::app()->tpl->addValue('ConstructionTitle', function (ArrayObject $attr): string {
return '<?php echo ' . sprintf(
dcCore::app()->tpl->getFilters($attr),
'dcCore::app()->blog->settings->get("' . My::id() . '")->get("title")'
) . '; ?>';
});
return true;
}
}

View File

@ -10,69 +10,84 @@
* @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;
}
try { namespace Dotclear\Plugin\construction;
// Version
if (!dcCore::app()->newVersion( use dcCore;
basename(__DIR__), use dcNsProcess;
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') use Exception;
)) {
return null; 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;
} }
$s = dcCore::app()->blog->settings->get(basename(__DIR__)); public static function process(): bool
{
if (!static::$init) {
return false;
}
$s->put( try {
'flag', $s = dcCore::app()->blog->settings->get(My::id());
false,
'boolean',
'Construction blog flag',
false,
true
);
$s->put( $s->put(
'allowed_ip', 'flag',
json_encode(['127.0.0.1']), false,
'string', 'boolean',
'Construction blog allowed ip', 'Construction blog flag',
false, false,
true true
); );
$s->put( $s->put(
'title', 'allowed_ip',
__('Work in progress'), json_encode(['127.0.0.1']),
'string', 'string',
'Construction blog title', 'Construction blog allowed ip',
false, false,
true true
); );
$s->put( $s->put(
'message', 'title',
__('<p>The blog is currently under construction.</p>'), __('Work in progress'),
'string', 'string',
'Construction blog message', 'Construction blog title',
false, false,
true true
); );
$s->put( $s->put(
'extra_urls', 'message',
json_encode([]), __('<p>The blog is currently under construction.</p>'),
'string', 'string',
'Construction blog message', 'Construction blog message',
false, false,
true true
); );
return true; $s->put(
} catch (Exception $e) { 'extra_urls',
dcCore::app()->error->add($e->getMessage()); json_encode([]),
'string',
'Construction blog message',
false,
true
);
return true;
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
return true;
}
} }
return false;

View File

@ -10,106 +10,152 @@
* @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;
}
dcPage::check(dcCore::app()->auth->makePermissions([ namespace Dotclear\Plugin\construction;
dcAuth::PERMISSION_ADMIN,
]));
$s = dcCore::app()->blog->settings->get(basename(__DIR__)); use dcAuth;
use dcCore;
use dcNsProcess;
use dcPage;
use Dotclear\Helper\Html\Form\{
Checkbox,
Div,
Form,
Hidden,
Input,
Label,
Note,
Para,
Submit,
Text,
Textarea
};
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Network\Http;
use Exception;
if (!empty($_POST['saveconfig'])) { class Manage extends dcNsProcess
try { {
$allowed_ip = []; public static function init(): bool
foreach (explode("\n", $_POST['construction_allowed_ip']) as $ip) { {
$allowed_ip[] = trim($ip); static::$init = defined('DC_CONTEXT_ADMIN')
} && My::phpCompliant()
$extra_urls = []; && dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([
foreach (explode(',', $_POST['construction_extra_urls']) as $url) { dcAuth::PERMISSION_CONTENT_ADMIN,
$extra_urls[] = trim($url); ]), dcCore::app()->blog->id);
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
} }
$s->put('flag', empty($_POST['construction_flag']) ? false : true); $s = dcCore::app()->blog->settings->get(My::id());
$s->put('allowed_ip', json_encode($allowed_ip));
$s->put('title', $_POST['construction_title']);
$s->put('message', $_POST['construction_message']);
$s->put('extra_urls', json_encode($extra_urls));
dcCore::app()->blog->triggerBlog(); if (!empty($_POST['saveconfig'])) {
try {
$allowed_ip = [];
foreach (explode("\n", $_POST['construction_allowed_ip']) as $ip) {
$allowed_ip[] = trim($ip);
}
$extra_urls = [];
foreach (explode(',', $_POST['construction_extra_urls']) as $url) {
$extra_urls[] = trim($url);
}
dcAdminNotices::addSuccessNotice( $s->put('flag', !empty($_POST['construction_flag']));
__('Settings successfully updated.') $s->put('allowed_ip', json_encode($allowed_ip));
$s->put('title', $_POST['construction_title']);
$s->put('message', $_POST['construction_message']);
$s->put('extra_urls', json_encode($extra_urls));
dcCore::app()->blog->triggerBlog();
dcPage::addSuccessNotice(
__('Settings successfully updated.')
);
dcCore::app()->adminurl->redirect(
'admin.plugin.' . My::id()
);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
return true;
}
public static function render(): void
{
if (!static::$init) {
return;
}
$s = dcCore::app()->blog->settings->get(My::id());
$editor = dcCore::app()->auth->getOption('editor');
$nb_rows = count(json_decode($s->get('allowed_ip'), true));
if ($nb_rows < 2) {
$nb_rows = 2;
} elseif ($nb_rows > 10) {
$nb_rows = 10;
}
dcPage::openModule(
My::name(),
dcPage::jsConfirmClose('opts-forms') .
dcCore::app()->callBehavior('adminPostEditor', $editor['xhtml'], 'construction', ['#construction_message'], 'xhtml') .
dcPage::jsModuleLoad(My::id() . '/js/backend.js')
); );
dcCore::app()->adminurl->redirect( echo
'admin.plugin.' . basename(__DIR__) dcPage::breadcrumb([
); __('Plugins') => '',
} catch (Exception $e) { My::name() => '',
dcCore::app()->error->add($e->getMessage()); ]) .
dcPage::notices() .
(new Div())->id('construction_options')->items([
(new Form(My::id() . 'form'))->method('post')->action(dcCore::app()->adminurl->get('admin.plugin.' . My::id()))->fields([
(new Div())->class('fieldset')->items([
(new Text('h4', __('Configuration'))),
(new Para())->class('filed')->items([
(new Checkbox('construction_flag', (bool) $s->get('flag')))->value(1),
(new Label(__('Plugin activation'), Label::OUTSIDE_LABEL_AFTER))->for('construction_flag')->class('classic'),
]),
(new Para())->items([
(new Label(__('Allowed IP:'), Label::OUTSIDE_LABEL_BEFORE))->for('construction_allowed_ip'),
(new Textarea('construction_allowed_ip', Html::escapeHTML(implode("\n", json_decode($s->get('allowed_ip'), true)))))->cols(20)->rows($nb_rows),
]),
(new Note())->class('form-note')->text(sprintf(__('Your IP is <strong>%s</strong> - the allowed IP can view the blog normally.'), (string) Http::realIP())),
(new Para())->class('area')->items([
(new Label(__('Extra allowed URL types:'), Label::OUTSIDE_LABEL_BEFORE))->for('construction_extra_urls'),
(new Input('construction_extra_urls'))->size(20)->maxlenght(255)->class('maximal')->value(Html::escapeHTML(implode(',', json_decode($s->get('extra_urls'), true)))),
]),
]),
(new Div())->class('fieldset')->items([
(new Text('h4', __('Presentation'))),
(new Para())->class('area')->items([
(new Label(__('Title:'), Label::OUTSIDE_LABEL_BEFORE))->for('construction_title'),
(new Input('construction_title'))->size(20)->maxlenght(255)->class('maximal')->value(Html::escapeHTML($s->get('title'))),
]),
(new Para())->class('area')->items([
(new Label(__('Message:'), Label::OUTSIDE_LABEL_BEFORE))->for('construction_message'),
(new Textarea('construction_message', Html::escapeHTML($s->get('message'))))->cols(40)->rows(10),
]),
]),
(new Para())->items([
dcCore::app()->formNonce(false),
(new Hidden(['p'], 'construction')),
(new Submit(['saveconfig']))->value(__('Save')),
]),
]),
])->render();
dcPage::closeModule();
} }
} }
$nb_rows = count(json_decode($s->get('allowed_ip'), true));
if ($nb_rows < 2) {
$nb_rows = 2;
} elseif ($nb_rows > 10) {
$nb_rows = 10;
}
$editor = dcCore::app()->auth->getOption('editor');
echo '
<html><head><title>' . __('Construction') . '</title>' .
dcPage::jsConfirmClose('opts-forms') .
dcCore::app()->callBehavior('adminPostEditor', $editor['xhtml'], 'construction', ['#construction_message'], 'xhtml') .
dcPage::jsModuleLoad(basename(__DIR__) . '/js/index.js') . '
</head><body>' .
dcPage::breadcrumb([
__('Plugins') => '',
__('Construction') => '',
]) .
dcPage::notices() . '
<div id="construction_options">
<form method="post" action="' . dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '">
<div class="fieldset">
<h4>' . __('Configuration') . '</h4>
<p class="field">' .
form::checkbox('construction_flag', 1, $s->get('flag')) . '
<label class="classic" for="construction_flag">' . __('Plugin activation') . '</label>
</p>
<p><label for="construction_allowed_ip">' . __('Allowed IP:') . '</label> ' .
form::textarea('construction_allowed_ip', 20, $nb_rows, html::escapeHTML(implode("\n", json_decode($s->get('allowed_ip'), true)))) .
'</p>
<p class="form-note">' . sprintf(__('Your IP is <strong>%s</strong> - the allowed IP can view the blog normally.'), (string) http::realIP()) . '</p>
<p class="area"><label for="construction_extra_urls">' . __('Extra allowed URL types:') . '</label>' .
form::field('construction_extra_urls', 20, 255, html::escapeHTML(implode(',', json_decode($s->get('extra_urls'), true))), 'maximal') .
'</p>
</div>
<div class="fieldset">
<h4>' . __('Presentation') . '</h4>
<p class="area"><label for="construction_title">' . __('Title:') . '</label>' .
form::field('construction_title', 20, 255, html::escapeHTML($s->get('title')), 'maximal') .
'</p>
<p class="area"><label for="construction_message">' . __('Message:') . '</label>' .
form::textarea('construction_message', 40, 10, html::escapeHTML($s->get('message'))) .
'</p>
</div>
<p>' .
form::hidden(['p'], 'construction') .
dcCore::app()->formNonce() . '
<input type="submit" name="saveconfig" value="' . __('Save') . '" />
</p>
</form>
</div>
</body>
</html>';

58
src/My.php 100644
View File

@ -0,0 +1,58 @@
<?php
/**
* @brief construction, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Osku 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\construction;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/** @var string Required php version */
public const PHP_MIN = '7.4';
/**
* 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, '>=');
}
}