From c9e3ea8f3fceb8f8e554401463029879ec2c72c4 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 11 Mar 2023 12:16:12 +0100 Subject: [PATCH] release 2023.03.11 for Dotclear 2.25 --- CHANGELOG.md | 9 ++ README.md | 2 +- _define.php | 14 +- dcstore.xml | 8 +- locales/fr/main.lang.php | 20 +++ locales/fr/main.po | 16 +-- src/Backend.php | 66 ++++++--- src/Install.php | 78 +++++++--- src/Manage.php | 301 ++++++++++++++++++++++----------------- src/My.php | 38 +++++ src/MyUrlHandlers.php | 30 +++- src/Prepend.php | 31 +++- 12 files changed, 408 insertions(+), 205 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 locales/fr/main.lang.php create mode 100644 src/My.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d01a601 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +2023.03.11 +* Update to Dotclear 2.25 +* use abstract pligin id +* use php namespace +* use json instead of serialize +* rename settings + +2022.11.26 +* update to Dotclear 2.24 \ No newline at end of file diff --git a/README.md b/README.md index 3adc720..08b563a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Release](https://img.shields.io/github/v/release/JcDenis/myUrlHandlers)](https://github.com/JcDenis/myUrlHandlers/releases) [![Date](https://img.shields.io/github/release-date/JcDenis/myUrlHandlers)](https://github.com/JcDenis/myUrlHandlers/releases) [![Issues](https://img.shields.io/github/issues/JcDenis/myUrlHandlers)](https://github.com/JcDenis/myUrlHandlers/issues) -[![Dotclear](https://img.shields.io/badge/dotclear-v2.24-blue.svg)](https://fr.dotclear.org/download) +[![Dotclear](https://img.shields.io/badge/dotclear-v2.25-blue.svg)](https://fr.dotclear.org/download) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://plugins.dotaddict.org/dc2/details/myUrlHandlers) [![License](https://img.shields.io/github/license/JcDenis/myUrlHandlers)](https://github.com/JcDenis/myUrlHandlers/blob/master/LICENSE) diff --git a/_define.php b/_define.php index d896fb9..e8baa8d 100644 --- a/_define.php +++ b/_define.php @@ -18,16 +18,16 @@ $this->registerModule( 'My URL handlers', 'Change Dotclear URL handlers', 'Alex Pirine and contributors', - '2022.11.26', + '2023.03.11', [ - 'requires' => [['core', '2.24']], + 'requires' => [['core', '2.25']], 'permissions' => dcCore::app()->auth->makePermissions([ dcAuth::PERMISSION_CONTENT_ADMIN, ]), - 'priority' => 150000, - 'type' => 'plugin', - 'support' => 'https://github.com/JcDenis/myUrlHandlers', - 'details' => 'http://plugins.dotaddict.org/dc2/details/myUrlHandlers', - 'repository' => 'https://raw.githubusercontent.com/JcDenis/myUrlHandlers/master/dcstore.xml', + 'priority' => 150000, + 'type' => 'plugin', + 'support' => 'https://github.com/JcDenis/myUrlHandlers', + 'details' => 'http://plugins.dotaddict.org/dc2/details/myUrlHandlers', + 'repository' => 'https://raw.githubusercontent.com/JcDenis/myUrlHandlers/master/dcstore.xml', ] ); diff --git a/dcstore.xml b/dcstore.xml index 22fa6d7..e8f9055 100644 --- a/dcstore.xml +++ b/dcstore.xml @@ -2,12 +2,12 @@ My URL handlers - 2022.11.26 + 2023.03.11 Alex Pirine and contributors Change Dotclear URL handlers - https://github.com/JcDenis/myUrlHandlers/releases/download/v2022.11.26/plugin-myUrlHandlers.zip - 2.24 + https://github.com/JcDenis/myUrlHandlers/releases/download/v2023.03.11/plugin-myUrlHandlers.zip + 2.25 http://plugins.dotaddict.org/dc2/details/myUrlHandlers - http://forum.dotclear.org/viewforum.php?id=16 + https://github.com/JcDenis/myUrlHandlers diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php new file mode 100644 index 0000000..ce5869c --- /dev/null +++ b/locales/fr/main.lang.php @@ -0,0 +1,20 @@ + 1);\n" -msgid "URL handlers" -msgstr "Gestionnaires d'URL" - msgid "Uknown handler \"%s\"." msgstr "Le gestionnaire \"%s\" n'existe pas." @@ -31,9 +28,12 @@ msgstr "Les gestionnaires d'URL ont été restaurés avec succès." msgid "No URL handler to configure." msgstr "Il n'y a aucun gestionnaire d'URL à définir." -msgid "You can write your own URL for each handler of this list." -msgstr "Vous pouvez écrire votre propre URL pour chaque gestionnaire de cette liste." - msgid "URL handlers list" msgstr "Liste des gestionnaires d'URL" +msgid "You can write your own URL for each handler of this list." +msgstr "Vous pouvez écrire votre propre URL pour chaque gestionnaire de cette liste." + +msgid "URL handlers" +msgstr "Gestionnaires d'URL" + diff --git a/src/Backend.php b/src/Backend.php index ec74d95..f42acd6 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,24 +10,50 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return; +declare(strict_types=1); + +namespace Dotclear\Plugin\myUrlHandlers; + +use dcAuth; +use dcAdmin; +use dcCore; +use dcFavorites; +use dcPage; +use dcNsProcess; + +class Backend extends dcNsProcess +{ + public static function init(): bool + { + self::$init = defined('DC_CONTEXT_ADMIN'); + + return self::$init; + } + + public static function process(): bool + { + if (!self::$init) { + return false; + } + + 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([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) + ); + + dcCore::app()->addBehavior('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([dcAuth::PERMISSION_CONTENT_ADMIN]), + ]); + }); + + return true; + } } - -dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( - __('URL handlers'), - dcCore::app()->adminurl->get('admin.plugin.myUrlHandlers'), - dcPage::getPF('myUrlHandlers/icon.png'), - preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.myUrlHandlers')) . '(&.*)?$/', $_SERVER['REQUEST_URI']), - dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) -); - -dcCore::app()->addBehavior('adminDashboardFavoritesV2', function ($favs) { - $favs->register('myUrlHandlers', [ - 'title' => __('URL handlers'), - 'url' => dcCore::app()->adminurl->get('admin.plugin.myUrlHandlers'), - 'small-icon' => dcPage::getPF('myUrlHandlers/icon.png'), - 'large-icon' => dcPage::getPF('myUrlHandlers/icon-big.png'), - 'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), - ]); -}); diff --git a/src/Install.php b/src/Install.php index c4b60ae..5271e97 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,30 +10,64 @@ * @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); -try { - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; +namespace Dotclear\Plugin\myUrlHandlers; + +use dcCore; +use dcNamespace; +use dcNsProcess; + +class Install extends dcNsProcess +{ + public static function init(): bool + { + self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version')); + + return self::$init; } - dcCore::app()->blog->settings->addNamespace('myurlhandlers'); - dcCore::app()->blog->settings->myurlhandlers->put( - 'url_handlers', - '', - 'string', - 'Personalized URL handlers', - false - ); + public static function process(): bool + { + if (!self::$init) { + return false; + } - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + self::growUp(); + + dcCore::app()->blog->settings->get(My::id())->put( + My::NS_SETTING_ID, + json_encode([]), + 'string', + 'Personalized URL handlers', + false + ); + + return true; + } + + private static function growUp(): void + { + $current = dcCore::app()->getVersion(My::id()); + + // Update settings id, ns, value + if ($current && version_compare($current, '2023.03.11', '<')) { + $record = dcCore::app()->con->select( + 'SELECT * FROM ' . dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME . ' ' . + "WHERE setting_ns = 'myurlhandlers' AND setting_id = 'url_handlers' " + ); + + while ($record->fetch()) { + $value = @unserialize($record->f('setting_value')); + $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcNamespace::NS_TABLE_NAME); + $cur->setting_id = My::NS_SETTING_ID; + $cur->setting_ns = My::id(); + $cur->setting_value = json_encode(is_array($value) ? $value : []); + $cur->update( + "WHERE setting_id = '" . $record->f('setting_id') . "' and setting_ns = '" . $record->f('setting_ns') . "' " . + 'AND blog_id ' . (null === $record->f('blog_id') ? 'IS NULL ' : ("= '" . dcCore::app()->con->escape($record->f('blog_id')) . "' ")) + ); + } + } + } } - -return false; diff --git a/src/Manage.php b/src/Manage.php index 577e1a7..32fa48b 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -10,143 +10,182 @@ * @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_CONTENT_ADMIN])); +namespace Dotclear\Plugin\myUrlHandlers; -try { - # Read default handlers - $handlers = myUrlHandlers::getDefaults(); +use dcAuth; +use dcCore; +use dcNsProcess; +use dcPage; +use Exception; +use form; +use html; +use text; - # Overwrite with user settings - $settings = @unserialize(dcCore::app()->blog->settings->myurlhandlers->url_handlers); - if (is_array($settings)) { - foreach ($settings as $name => $url) { +/** + * Manage contributions list + */ +class Manage extends dcNsProcess +{ + public static function init(): bool + { + if (defined('DC_CONTEXT_ADMIN')) { + dcPage::check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + ])); + + self::$init = true; + } + + return self::$init; + } + + public static function process(): bool + { + if (!self::$init) { + return false; + } + + try { + $handlers = self::getHandlers(); + + if (!empty($_POST['handlers']) && is_array($_POST['handlers'])) { + foreach ($_POST['handlers'] as $name => $url) { + $url = text::tidyURL($url); + + if (empty($handlers[$name])) { + throw new Exception(sprintf( + __('Uknown handler "%s".'), + html::escapeHTML($name) + )); + } + + if (empty($url)) { + throw new Exception(sprintf( + __('Invalid URL for handler "%s".'), + html::escapeHTML($name) + )); + } + + $handlers[$name] = $url; + } + + # Get duplicates + $w = array_unique(array_diff_key($handlers, array_unique($handlers))); + + /** + * Error on the line + * array_walk($w,create_function('&$v,$k,$h','$v = array_keys($h,$v);'),$handlers); + * + * Begin fix + */ + $v = function (&$v, $k, $h) { + return array_keys($h, $v); + }; + + array_walk($w, $v, $handlers); + + /** + * End fix + */ + $w = call_user_func_array('array_merge', $w); + + if (!empty($w)) { + throw new Exception(sprintf( + __('Duplicate URL in handlers "%s".'), + implode('", "', $w) + )); + } + } + + if (isset($_POST['act_save'])) { + MyUrlHandlers::saveBlogHandlers($handlers); + dcPage::addSuccessNotice(__('URL handlers have been successfully updated.')); + dcCore::app()->adminurl->redirect('admin.plugin.' . My::id()); + } elseif (isset($_POST['act_restore'])) { + MyUrlHandlers::saveBlogHandlers([]); + dcPage::addSuccessNotice(__('URL handlers have been successfully restored.')); + 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 (!self::$init) { + return; + } + + $handlers = self::getHandlers(); + + dcPage::openModule(My::name()); + + echo + dcPage::breadcrumb( + [ + html::escapeHTML(dcCore::app()->blog->name) => '', + My::name() => '', + ] + ) . + dcPage::notices(); + + if (empty($handlers)) { + echo + '

' . __('No URL handler to configure.') . '

'; + } else { + echo + '
' . + '
' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + '' . + ''; + + foreach ($handlers as $name => $url) { + echo + '' . + '' . + '' . + ''; + } + + echo + '
' . __('URL handlers list') . '
' . __('Type') . '' . __('URL') . '
' . html::escapeHTML($name) . '' . + form::field(['handlers[' . $name . ']'], 20, 255, html::escapeHTML($url)) . + '
' . + '

' . __('You can write your own URL for each handler of this list.') . '

' . + '

' . + ' ' . + '' . + dcCore::app()->formNonce() . '

' . + '
'; + } + + dcPage::closeModule(); + } + + private static function getHandlers(): array + { + # Read default handlers + $handlers = MyUrlHandlers::getDefaults(); + + # Overwrite with user settings + foreach (MyUrlHandlers::getBlogHandlers() as $name => $url) { if (isset($handlers[$name])) { $handlers[$name] = $url; } } + + return $handlers; } - unset($settings); - - if (!empty($_POST['handlers']) && is_array($_POST['handlers'])) { - foreach ($_POST['handlers'] as $name => $url) { - $url = text::tidyURL($url); - - if (empty($handlers[$name])) { - throw new Exception(sprintf( - __('Uknown handler "%s".'), - html::escapeHTML($name) - )); - } - - if (empty($url)) { - throw new Exception(sprintf( - __('Invalid URL for handler "%s".'), - html::escapeHTML($name) - )); - } - - $handlers[$name] = $url; - } - - # Get duplicates - $w = array_unique(array_diff_key($handlers, array_unique($handlers))); - - /** - * Error on the line - * array_walk($w,create_function('&$v,$k,$h','$v = array_keys($h,$v);'),$handlers); - * - * Begin fix - */ - $v = function (&$v, $k, $h) { - return array_keys($h, $v); - }; - - array_walk($w, $v, $handlers); - - /** - * End fix - */ - $w = call_user_func_array('array_merge', $w); - - if (!empty($w)) { - throw new Exception(sprintf( - __('Duplicate URL in handlers "%s".'), - implode('", "', $w) - )); - } - } - - if (isset($_POST['act_save'])) { - dcCore::app()->blog->settings->myurlhandlers->put('url_handlers', serialize($handlers)); - dcCore::app()->blog->triggerBlog(); - dcAdminNotices::addSuccessNotice(__('URL handlers have been successfully updated.')); - } elseif (isset($_POST['act_restore'])) { - dcCore::app()->blog->settings->myurlhandlers->put('url_handlers', serialize([])); - dcCore::app()->blog->triggerBlog(); - $handlers = myUrlHandlers::getDefaults(); - dcAdminNotices::addSuccessNotice(__('URL handlers have been successfully restored.')); - } -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); } - -/* DISPLAY ---------------------------------------------------- */ - -?> - -<?php echo __('URL handlers'); ?> - -blog->name) => '', - __('URL handlers') => '', - ] -) . -dcPage::notices(); - -if (empty($handlers)) { - echo - '

' . __('No URL handler to configure.') . '

'; -} else { - echo - '
' . - '
' . - '' . - '' . - '' . - '' . - '' . - '' . - '' . - '' . - ''; - - foreach ($handlers as $name => $url) { - echo - '' . - '' . - ''. - ''; - } - - echo - '
' . __('URL handlers list') . '
' . __('Type') . '' . __('URL') . '
' . html::escapeHTML($name) . '' . - form::field(['handlers[' . $name . ']'], 20, 255, html::escapeHTML($url)) . - '
' . - '

' . __('You can write your own URL for each handler of this list.') . '

' . - '

' . - ' ' . - '' . - dcCore::app()->formNonce() . '

' . - '
'; -} -?> - diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..98b2ad5 --- /dev/null +++ b/src/My.php @@ -0,0 +1,38 @@ +plugins->moduleInfo(self::id(), 'name')); + } +} diff --git a/src/MyUrlHandlers.php b/src/MyUrlHandlers.php index 6addc25..d786e07 100644 --- a/src/MyUrlHandlers.php +++ b/src/MyUrlHandlers.php @@ -10,13 +10,19 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class myUrlHandlers +declare(strict_types=1); + +namespace Dotclear\Plugin\myUrlHandlers; + +use dcCore; + +class MyUrlHandlers { private static $defaults = []; private static $url2post = []; private static $post_adm_url = []; - public static function init() + public static function init(): void { # Set defaults foreach (dcCore::app()->url->getTypes() as $k => $v) { @@ -39,13 +45,12 @@ class myUrlHandlers } # Read user settings - $handlers = (array) @unserialize(dcCore::app()->blog->settings->myurlhandlers->url_handlers); - foreach ($handlers as $name => $url) { + foreach (self::getBlogHandlers() as $name => $url) { self::overrideHandler($name, $url); } } - public static function overrideHandler($name, $url) + public static function overrideHandler(string $name, string $url): void { if (!isset(self::$defaults[$name])) { return; @@ -65,7 +70,7 @@ class myUrlHandlers } } - public static function getDefaults() + public static function getDefaults(): array { $res = []; foreach (self::$defaults as $k => $v) { @@ -74,4 +79,17 @@ class myUrlHandlers return $res; } + + public static function getBlogHandlers(): array + { + $handlers = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get(My::NS_SETTING_ID), true); + + return is_array($handlers) ? $handlers : []; + } + + public static function saveBlogHandlers(array $handlers): void + { + dcCore::app()->blog->settings->get(My::id())->put(My::NS_SETTING_ID, json_encode($handlers)); + dcCore::app()->blog->triggerBlog(); + } } diff --git a/src/Prepend.php b/src/Prepend.php index 9baf09d..b89be04 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -10,10 +10,29 @@ * @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); + +namespace Dotclear\Plugin\myUrlHandlers; + +use dcNsProcess; + +class Prepend extends dcNsProcess +{ + public static function init(): bool + { + self::$init = true; + + return self::$init; + } + + public static function process(): bool + { + if (!self::$init) { + return false; + } + + MyUrlHandlers::init(); + + return true; + } } - -Clearbricks::lib()->autoload(['myUrlHandlers' => __DIR__ . '/inc/class.myurlhandlers.php']); - -myUrlHandlers::init();