From 7506fdb918fd8e92e672287773b9f8a9ebd1941c Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Fri, 23 Dec 2022 11:27:35 +0100 Subject: [PATCH] use namespace --- _admin.php | 236 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 142 insertions(+), 94 deletions(-) diff --git a/_admin.php b/_admin.php index aaa044d..6ac9601 100644 --- a/_admin.php +++ b/_admin.php @@ -10,99 +10,147 @@ * @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); + +namespace Dotclear\Plugin\tinyPacker; + +/* dotclear */ +use dcCore; +use dcPage; + +/* clearbricks */ +use files; +use fileZip; +use html; +use http; +use path; + +/* php */ +use Exception; + +/** + * tinyPacker admin class + * + * Add action and button to modules lists. + */ +class Admin +{ + private static $init = false; + + public static function init(): bool + { + if (defined('DC_CONTEXT_ADMIN')) { + dcPage::checkSuper(); + self::$init = true; + } + + return self::$init; + } + + public static function process(): bool + { + if (!self::$init) { + return false; + } + + dcCore::app()->addBehavior( + 'adminModulesListGetActions', + function ($list, $id, $_) { + return in_array($list->getList(), [ + 'plugin-activate', + 'theme-activate', + ]) ? sprintf( + '', + basename(__DIR__), + html::escapeHTML($id) + ) : null; + } + ); + + dcCore::app()->addBehavior( + 'adminModulesListDoActions', + function ($list, $modules, $type) { + # Pack action + if (empty($_POST[basename(__DIR__)]) + || !is_array($_POST[basename(__DIR__)])) { + return null; + } + + # Repository directory + $dir = path::real( + dcCore::app()->blog->public_path . '/packages', + false + ); + + if (!is_dir($dir)) { + files::makeDir($dir, true); + } + if (!is_writable($dir)) { + throw new Exception(__('Destination directory is not writable.')); + } + + # Module to pack + $modules = array_keys($_POST[basename(__DIR__)]); + $id = $modules[0]; + + if (!$list->modules->moduleExists($id)) { + throw new Exception(__('No such module.')); + } + $module = $list->modules->getModules($id); + + # Excluded files and dirs + $exclude = [ + '\.', + '\.\.', + '__MACOSX', + '\.svn', + '\.hg.*?', + '\.git.*?', + 'CVS', + '\.directory', + '\.DS_Store', + 'Thumbs\.db', + '_disabled', + ]; + + # Packages names + $files = [ + $type . '-' . $id . '.zip', + $type . '-' . $id . '-' . $module['version'] . '.zip', + ]; + + # Create zip + foreach ($files as $f) { + @set_time_limit(300); + $fp = fopen($dir . '/' . $f, 'wb'); + + $zip = new fileZip($fp); + + foreach ($exclude as $e) { + $zip->addExclusion(sprintf( + '#(^|/)(%s)(/|$)#', + $e + )); + } + + $zip->addDirectory($module['root'], $id, true); + $zip->write(); + $zip->close(); + unset($zip); + } + + dcPage::addSuccessNotice( + __('Task successfully executed.') + ); + http::redirect($list->getURL()); + } + ); + + return true; + } } -dcCore::app()->addBehavior( - 'adminModulesListGetActions', - function ($list, $id, $_) { - return in_array($list->getList(), [ - 'plugin-activate', - 'theme-activate', - ]) ? sprintf( - '', - basename(__DIR__), - html::escapeHTML($id) - ) : null; - } -); - -dcCore::app()->addBehavior( - 'adminModulesListDoActions', - function ($list, $modules, $type) { - # Pack action - if (empty($_POST[basename(__DIR__)]) - || !is_array($_POST[basename(__DIR__)])) { - return null; - } - - # Repository directory - $dir = path::real( - dcCore::app()->blog->public_path . '/packages', - false - ); - - if (!is_dir($dir)) { - files::makeDir($dir, true); - } - if (!is_writable($dir)) { - throw new Exception(__('Destination directory is not writable.')); - } - - # Module to pack - $modules = array_keys($_POST[basename(__DIR__)]); - $id = $modules[0]; - - if (!$list->modules->moduleExists($id)) { - throw new Exception(__('No such module.')); - } - $module = $list->modules->getModules($id); - - # Excluded files and dirs - $exclude = [ - '\.', - '\.\.', - '__MACOSX', - '\.svn', - '\.hg.*?', - '\.git.*?', - 'CVS', - '\.directory', - '\.DS_Store', - 'Thumbs\.db', - '_disabled', - ]; - - # Packages names - $files = [ - $type . '-' . $id . '.zip', - $type . '-' . $id . '-' . $module['version'] . '.zip', - ]; - - # Create zip - foreach ($files as $f) { - @set_time_limit(300); - $fp = fopen($dir . '/' . $f, 'wb'); - - $zip = new fileZip($fp); - - foreach ($exclude as $e) { - $zip->addExclusion(sprintf( - '#(^|/)(%s)(/|$)#', - $e - )); - } - - $zip->addDirectory($module['root'], $id, true); - $zip->write(); - $zip->close(); - unset($zip); - } - - dcPage::addSuccessNotice( - __('Task successfully executed.') - ); - http::redirect($list->getURL()); - } -); +/* process */ +if (Admin::init()) { + Admin::process(); +}