From 589adf75ce483d406f6a0a6275da7a7939b8a35e Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Mon, 10 Apr 2023 19:06:11 +0200 Subject: [PATCH] use namespace --- src/Backend.php | 79 ++++++++++------ src/Frontend.php | 131 ++++++++++++++++----------- src/Install.php | 127 ++++++++++++++------------ src/Manage.php | 230 ++++++++++++++++++++++++++++------------------- src/My.php | 58 ++++++++++++ 5 files changed, 399 insertions(+), 226 deletions(-) create mode 100644 src/My.php diff --git a/src/Backend.php b/src/Backend.php index cecc92e..2ac1588 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,32 +10,61 @@ * @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()->menu[dcAdmin::MENU_PLUGINS]->addItem( - __('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' : '' -); +namespace Dotclear\Plugin\construction; -dcCore::app()->addBehaviors([ - 'adminPageHTMLHead' => function () { - if (dcCore::app()->blog->settings->get(basename(__DIR__))->get('flag')) { - echo dcPage::cssModuleLoad(basename(__DIR__) . '/css/admin.css'); +use dcAdmin; +use dcCore; +use dcPage; +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) { - $favs->register(basename(__DIR__), [ - 'title' => __('Construction'), - 'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), - 'small-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon.png')), - 'large-icon' => urldecode(dcPage::getPF(basename(__DIR__) . '/icon-big.png')), - 'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_ADMIN]), + + dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( + My::name(), + dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + dcPage::getPF(My::id() . '/icon.png'), + preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcCore::app()->auth::PERMISSION_ADMIN]), dcCore::app()->blog->id), + 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; + } +} diff --git a/src/Frontend.php b/src/Frontend.php index 470de69..dde2ba5 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -10,66 +10,91 @@ * @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()->addBehavior('publicBeforeDocument', function () { - if (!dcCore::app()->blog->settings->get(basename(__DIR__))->get('flag')) { - return; +namespace Dotclear\Plugin\construction; + +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'); - 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])); - } else { - dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [__DIR__, 'default-templates', DC_DEFAULT_TPLSET])); - } + public static function process(): bool + { + if (!static::$init) { + return false; + } - $all_allowed_ip = json_decode(dcCore::app()->blog->settings->get(basename(__DIR__))->get('allowed_ip'), true); - if (!is_array($all_allowed_ip)) { - $all_allowed_ip = []; - } - $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'; + dcCore::app()->addBehavior('publicBeforeDocument', function (): void { + if (!dcCore::app()->blog->settings->get(My::id())->get('flag')) { + return; + } - 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'); + $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()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', $tplset])); + } else { + dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), implode(DIRECTORY_SEPARATOR, [My::root(), 'default-templates', DC_DEFAULT_TPLSET])); + } - 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); }); - } - } - } -}); + 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) { - return 'tpl->getFilters($attr), - 'dcCore::app()->blog->settings->get("' . basename(__DIR__) . '")->get("message")' - ) . '; ?>'; -}); -dcCore::app()->tpl->addValue('ConstructionTitle', function ($attr) { - return 'tpl->getFilters($attr), - 'dcCore::app()->blog->settings->get("' . basename(__DIR__) . '")->get("title")' - ) . '; ?>'; -}); + echo dcCore::app()->tpl->getData(dcCore::app()->ctx->__get('current_tpl')); + + # --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); + }); + } + } + } + }); + + dcCore::app()->tpl->addValue('ConstructionMessage', function (ArrayObject $attr): string { + return 'tpl->getFilters($attr), + 'dcCore::app()->blog->settings->get("' . My::id() . '")->get("message")' + ) . '; ?>'; + }); + dcCore::app()->tpl->addValue('ConstructionTitle', function (ArrayObject $attr): string { + return 'tpl->getFilters($attr), + 'dcCore::app()->blog->settings->get("' . My::id() . '")->get("title")' + ) . '; ?>'; + }); + + return true; + } +} diff --git a/src/Install.php b/src/Install.php index 90c3e9b..9355a2b 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,69 +10,84 @@ * @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); -try { - // Version - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; +namespace Dotclear\Plugin\construction; + +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; } - $s = dcCore::app()->blog->settings->get(basename(__DIR__)); + public static function process(): bool + { + if (!static::$init) { + return false; + } - $s->put( - 'flag', - false, - 'boolean', - 'Construction blog flag', - false, - true - ); + try { + $s = dcCore::app()->blog->settings->get(My::id()); - $s->put( - 'allowed_ip', - json_encode(['127.0.0.1']), - 'string', - 'Construction blog allowed ip', - false, - true - ); + $s->put( + 'flag', + false, + 'boolean', + 'Construction blog flag', + false, + true + ); - $s->put( - 'title', - __('Work in progress'), - 'string', - 'Construction blog title', - false, - true - ); + $s->put( + 'allowed_ip', + json_encode(['127.0.0.1']), + 'string', + 'Construction blog allowed ip', + false, + true + ); - $s->put( - 'message', - __('

The blog is currently under construction.

'), - 'string', - 'Construction blog message', - false, - true - ); + $s->put( + 'title', + __('Work in progress'), + 'string', + 'Construction blog title', + false, + true + ); - $s->put( - 'extra_urls', - json_encode([]), - 'string', - 'Construction blog message', - false, - true - ); + $s->put( + 'message', + __('

The blog is currently under construction.

'), + 'string', + 'Construction blog message', + false, + true + ); - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + $s->put( + 'extra_urls', + json_encode([]), + 'string', + 'Construction blog message', + false, + true + ); + + return true; + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + return true; + } } - -return false; diff --git a/src/Manage.php b/src/Manage.php index 1b65f64..6487b97 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -10,106 +10,152 @@ * @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); -dcPage::check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_ADMIN, -])); +namespace Dotclear\Plugin\construction; -$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'])) { - 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); +class Manage extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && My::phpCompliant() + && dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + ]), 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->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)); + $s = dcCore::app()->blog->settings->get(My::id()); - 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( - __('Settings successfully updated.') + $s->put('flag', !empty($_POST['construction_flag'])); + $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( - 'admin.plugin.' . basename(__DIR__) - ); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + echo + dcPage::breadcrumb([ + __('Plugins') => '', + My::name() => '', + ]) . + 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 %s - 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 ' -' . __('Construction') . '' . -dcPage::jsConfirmClose('opts-forms') . -dcCore::app()->callBehavior('adminPostEditor', $editor['xhtml'], 'construction', ['#construction_message'], 'xhtml') . -dcPage::jsModuleLoad(basename(__DIR__) . '/js/index.js') . ' -' . - -dcPage::breadcrumb([ - __('Plugins') => '', - __('Construction') => '', -]) . -dcPage::notices() . ' - -
-
-
-

' . __('Configuration') . '

- -

' . -form::checkbox('construction_flag', 1, $s->get('flag')) . ' - -

- -

' . -form::textarea('construction_allowed_ip', 20, $nb_rows, html::escapeHTML(implode("\n", json_decode($s->get('allowed_ip'), true)))) . -'

-

' . sprintf(__('Your IP is %s - the allowed IP can view the blog normally.'), (string) http::realIP()) . '

- -

' . -form::field('construction_extra_urls', 20, 255, html::escapeHTML(implode(',', json_decode($s->get('extra_urls'), true))), 'maximal') . -'

- -
-
-

' . __('Presentation') . '

-

' . -form::field('construction_title', 20, 255, html::escapeHTML($s->get('title')), 'maximal') . -'

- -

' . -form::textarea('construction_message', 40, 10, html::escapeHTML($s->get('message'))) . -'

-
-

' . -form::hidden(['p'], 'construction') . -dcCore::app()->formNonce() . ' - -

-
-
- - -'; diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..42769f4 --- /dev/null +++ b/src/My.php @@ -0,0 +1,58 @@ +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, '>='); + } +}