diff --git a/CHANGELOG.md b/CHANGELOG.md index 52a1e53..7a80579 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +myUrlHandlers 2023.10.22 +=========================================================== +* Require Dotclear 2.28 +* Require PHP 8.1 +* Use default permissions and update to admin only +* Code review (phpstan) + myUrlHandlers 2023.10.11 =========================================================== * Require Dotclear 2.28 diff --git a/README.md b/README.md index 273f514..27ce798 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # README -[![Release](https://img.shields.io/badge/release-2023.10.11-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/myUrlHandlers/releases) -![Date](https://img.shields.io/badge/date-2023.10.11-c44d58.svg) +[![Release](https://img.shields.io/badge/release-2023.10.22-a2cbe9.svg)](https://git.dotclear.watch/JcDenis/myUrlHandlers/releases) +![Date](https://img.shields.io/badge/date-2023.10.22-c44d58.svg) [![Dotclear](https://img.shields.io/badge/dotclear-v2.28-137bbb.svg)](https://fr.dotclear.org/download) [![Dotaddict](https://img.shields.io/badge/dotaddict-official-9ac123.svg)](https://plugins.dotaddict.org/dc2/details/myUrlHandlers) [![License](https://img.shields.io/badge/license-GPL--2.0-ececec.svg)](https://git.dotclear.watch/JcDenis/myUrlHandlers/src/branch/master/LICENSE) @@ -10,13 +10,13 @@ _myUrlHandlers_ is a plugin for the open-source web publishing software called [Dotclear](https://www.dotclear.org). -> You can change public URL of each Dotclear parts, like post, category, etc and some plugins having public parts. +> Change public URL of each Dotclear parts, like post, category, etc and some plugins having public parts. ## REQUIREMENTS -* contentadmin permissions for management * Dotclear 2.28 * PHP 8.1+ +* Dotclear admin permissions for management ## USAGE @@ -38,6 +38,6 @@ or you can add dashboard icon. * Alex pirine (Author) * te2dy -* Jean-Christian Denis +* Jean-Christian Denis (latest) You are welcome to contribute to this code. diff --git a/dcstore.xml b/dcstore.xml index f5ceeda..2563769 100644 --- a/dcstore.xml +++ b/dcstore.xml @@ -2,10 +2,10 @@ URL handlers - 2023.10.11 + 2023.10.22 Alex Pirine and contributors Change Dotclear URL handlers - https://git.dotclear.watch/JcDenis/myUrlHandlers/releases/download/v2023.10.11/plugin-myUrlHandlers.zip + https://git.dotclear.watch/JcDenis/myUrlHandlers/releases/download/v2023.10.22/plugin-myUrlHandlers.zip 2.28 https://git.dotclear.watch/JcDenis/myUrlHandlers/src/branch/master/README.md https://git.dotclear.watch/JcDenis/myUrlHandlers/issues diff --git a/src/Backend.php b/src/Backend.php index 1600950..25c5f88 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -1,15 +1,5 @@ My::manageUrl(), 'small-icon' => My::icons(), 'large-icon' => My::icons(), - 'permissions' => App::auth()->makePermissions([App::auth()::PERMISSION_CONTENT_ADMIN]), + 'permissions' => App::auth()->makePermissions([App::auth()::PERMISSION_ADMIN]), ]); }); diff --git a/src/Install.php b/src/Install.php index 899eeb2..b8fc421 100644 --- a/src/Install.php +++ b/src/Install.php @@ -8,12 +8,11 @@ use Dotclear\App; use Dotclear\Core\Process; /** - * @brief myUrlHandlers installation class. - * @ingroup myUrlHandlers + * @brief myUrlHandlers installation class. + * @ingroup myUrlHandlers * - * @author Alex Pirine and contributors - * @author Jean-Christian Denis - * @copyright Alex Pirine + * @author Alex Pirine (author) + * @author Jean-Christian Denis (latest) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class Install extends Process diff --git a/src/Manage.php b/src/Manage.php index ff0f9d7..2f403ee 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -16,12 +16,11 @@ use Dotclear\Helper\Text; use Exception; /** - * @brief myUrlHandlers manage class. - * @ingroup myUrlHandlers + * @brief myUrlHandlers manage class. + * @ingroup myUrlHandlers * - * @author Alex Pirine and contributors - * @author Jean-Christian Denis - * @copyright Alex Pirine + * @author Alex Pirine (author) + * @author Jean-Christian Denis (latest) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class Manage extends Process @@ -47,14 +46,14 @@ class Manage extends Process if (empty($handlers[$name])) { throw new Exception(sprintf( __('Uknown handler "%s".'), - Html::escapeHTML($name) + Html::escapeHTML((string) $name) )); } if (empty($url)) { throw new Exception(sprintf( __('Invalid URL for handler "%s".'), - Html::escapeHTML($name) + Html::escapeHTML((string) $name) )); } diff --git a/src/My.php b/src/My.php index 3811508..55ca8e1 100644 --- a/src/My.php +++ b/src/My.php @@ -8,12 +8,11 @@ use Dotclear\App; use Dotclear\Module\MyPlugin; /** - * @brief myUrlHandlers My helper. - * @ingroup myUrlHandlers + * @brief myUrlHandlers My helper. + * @ingroup myUrlHandlers * - * @author Alex Pirine and contributors - * @author Jean-Christian Denis - * @copyright Alex Pirine + * @author Alex Pirine (author) + * @author Jean-Christian Denis (latest) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class My extends MyPlugin @@ -25,19 +24,5 @@ class My extends MyPlugin */ public const NS_SETTING_ID = 'handlers'; - public static function checkCustomContext(int $context): ?bool - { - return match ($context) { - // Whole module: Limit backend to registered user and pages user - self::MODULE => !App::task()->checkContext('BACKEND') - || ( - App::blog()->isDefined() - && App::auth()->check(App::auth()->makePermissions([ - App::auth()::PERMISSION_CONTENT_ADMIN, - ]), App::blog()->id()) - ), - - default => null, - }; - } + // Use default permissions } diff --git a/src/MyUrlHandlers.php b/src/MyUrlHandlers.php index cb5985d..7497643 100644 --- a/src/MyUrlHandlers.php +++ b/src/MyUrlHandlers.php @@ -8,60 +8,65 @@ use Dotclear\App; use Dotclear\Core\PostType; /** - * @brief myUrlHandlers main class. - * @ingroup myUrlHandlers + * @brief myUrlHandlers main class. + * @ingroup myUrlHandlers * - * @author Alex Pirine and contributors - * @author Jean-Christian Denis - * @copyright Alex Pirine + * @author Alex Pirine (author) + * @author Jean-Christian Denis (latest) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class MyUrlHandlers { /** - * The default URLs handlers. + * The URLs stack. * - * @var array> $defaults + * @var UrlStack $stack */ - private static array $defaults = []; + private static UrlStack $stack; /** * The posts types URLs. * - * @var array $url2post + * @var array $pt_public2type */ - private static array $url2post = []; + private static array $pt_public2type = []; /** * The posts types admin URLs. * - * @var array $post_adm_url + * @var array $pt_type2admin */ - private static array $post_adm_url = []; + private static array $pt_type2admin = []; /** * Initialize handlers list. */ public static function init(): void { + self::$stack = new UrlStack(); + # Set defaults foreach (App::url()->getTypes() as $k => $v) { if (empty($v['url'])) { continue; } - $p = '/' . preg_quote($v['url'], '/') . '/'; $v['representation'] = str_replace('%', '%%', $v['representation']); - $v['representation'] = preg_replace($p, '%s', $v['representation'], 1, $c); + $v['representation'] = preg_replace('/' . preg_quote($v['url'], '/') . '/', '%s', $v['representation'], 1, $c); - if ($c) { - self::$defaults[$k] = $v; + if ($c && is_string($v['representation'])) { + self::$stack->set(new UrlDescriptor( + $k, + $v['url'], + $v['representation'], + $v['handler'] ?? null + )); } } foreach (App::postTypes()->dump() as $pt) { - self::$url2post[$pt->public_url] = $pt->type; - self::$post_adm_url[$pt->type] = $pt->admin_url; + self::$pt_public2type[$pt->public_url] = $pt->type; + self::$pt_type2admin[$pt->type] = $pt->admin_url; } # Read user settings @@ -78,38 +83,40 @@ class MyUrlHandlers */ public static function overrideHandler(string $name, string $url): void { - if (!isset(self::$defaults[$name])) { + $desc = self::$stack->get($name); + if (is_null($desc->handler())) { return; } App::url()->register( $name, $url, - sprintf(self::$defaults[$name]['representation'], $url), - self::$defaults[$name]['handler'] + sprintf($desc->representation, $url), + $desc->handler() ); - $k = self::$url2post[self::$defaults[$name]['url'] . '/%s'] ?? ''; - - if ($k) { - App::postTypes()->set(new PostType( - $k, - self::$post_adm_url[$k], - App::url()->getBase($name) . '/%s' - )); + $type = self::$pt_public2type[$desc->url . '/%s'] ?? ''; + if (!$type) { + return; } + + App::postTypes()->set(new PostType( + $type, + self::$pt_type2admin[$type], + App::url()->getBase($name) . '/%s' + )); } /** * Get default URLs handlers. * - * @return array The default URLs handlers + * @return array The default URLs handlers */ public static function getDefaults(): array { $res = []; - foreach (self::$defaults as $k => $v) { - $res[$k] = $v['url']; + foreach (self::$stack->dump() as $v) { + $res[$v->id] = $v->url; } return $res; @@ -118,7 +125,7 @@ class MyUrlHandlers /** * Get custom blog URLs handlers. * - * @return array The blog URLs handlers + * @return array The blog URLs handlers */ public static function getBlogHandlers(): array { @@ -130,7 +137,7 @@ class MyUrlHandlers /** * Save custom URLs handlers. * - * @param array $handlers The custom URLs handlers + * @param array $handlers The custom URLs handlers */ public static function saveBlogHandlers(array $handlers): void { diff --git a/src/Prepend.php b/src/Prepend.php index 4c10119..8282572 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -7,12 +7,11 @@ namespace Dotclear\Plugin\myUrlHandlers; use Dotclear\Core\Process; /** - * @brief myUrlHandlers prepend class. - * @ingroup myUrlHandlers + * @brief myUrlHandlers prepend class. + * @ingroup myUrlHandlers * - * @author Alex Pirine and contributors - * @author Jean-Christian Denis - * @copyright Alex Pirine + * @author Alex Pirine (author) + * @author Jean-Christian Denis (latest) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class Prepend extends Process diff --git a/src/Uninstall.php b/src/Uninstall.php index dc125ab..8572224 100644 --- a/src/Uninstall.php +++ b/src/Uninstall.php @@ -8,12 +8,11 @@ use Dotclear\Core\Process; use Dotclear\Plugin\Uninstaller\Uninstaller; /** - * @brief myUrlHandlers uninstall class. - * @ingroup myUrlHandlers + * @brief myUrlHandlers uninstall class. + * @ingroup myUrlHandlers * - * @author Alex Pirine and contributors - * @author Jean-Christian Denis - * @copyright Alex Pirine + * @author Alex Pirine (author) + * @author Jean-Christian Denis (latest) * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ class Uninstall extends Process diff --git a/src/UrlDescriptor.php b/src/UrlDescriptor.php new file mode 100644 index 0000000..4472117 --- /dev/null +++ b/src/UrlDescriptor.php @@ -0,0 +1,51 @@ +handler = $handler; + } + + /** + * Get handler. + * + * @return ?callable The handler + */ + public function handler(): ?callable + { + return $this->handler; + } +} \ No newline at end of file diff --git a/src/UrlStack.php b/src/UrlStack.php new file mode 100644 index 0000000..57b94b0 --- /dev/null +++ b/src/UrlStack.php @@ -0,0 +1,69 @@ + + */ + private array $stack = []; + + /** + * Check if an handler is set. + * + * @param string $id The handler ID + * + * @return bool True if it exists + */ + public function has(string $id): bool + { + return array_key_exists($id, $this->stack); + } + + /** + * Set an URL handler. + * + * @param UrlDescriptor $descriptor The URL descriptor + */ + public function set(UrlDescriptor $descriptor): void + { + $this->stack[$descriptor->id] = $descriptor; + } + + /** + * Get an URL handler. + * + * If it does not exist, return an empty handler. + * + * @param string $id The handler ID + * + * @return UrlDescriptor The URL descriptor + */ + public function get(string $id): UrlDescriptor + { + return $this->stack[$id] ?? new UrlDescriptor($id); + } + + /** + * Get URLs stack. + * + * @return array + */ + public function dump() + { + return $this->stack; + } +} \ No newline at end of file