From 81d49adf53043ce2b32dfa22ef8394a74d2fff99 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 17 Dec 2022 22:12:10 +0100 Subject: [PATCH] first attempt at namespace --- _admin.php | 56 ++-- _config.php | 227 +++++++++------- _define.php | 5 + _install.php | 160 +++++++----- _prepend.php | 25 +- inc/class.core.php | 26 +- inc/class.filezip.php | 13 +- inc/class.utils.php | 18 +- index.php | 592 ++++++++++++++++++++++-------------------- 9 files changed, 646 insertions(+), 476 deletions(-) diff --git a/_admin.php b/_admin.php index 15c7d22..6e76871 100644 --- a/_admin.php +++ b/_admin.php @@ -10,26 +10,48 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_CONTEXT_ADMIN')) { return null; } -dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); +/* dotclear ns */ +use dcAdmin; +use dcCore; +use dcFavorites; +use dcPage; -dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void { - $favs->register(basename(__DIR__), [ - 'title' => __('Packages repository'), - 'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '#packman-repository-repository', - 'small-icon' => [dcPage::getPF(basename(__DIR__) . '/icon.svg'), dcPage::getPF(basename(__DIR__) . '/icon-dark.svg')], - 'large-icon' => [dcPage::getPF(basename(__DIR__) . '/icon.svg'), dcPage::getPF(basename(__DIR__) . '/icon-dark.svg')], - //'permissions' => dcCore::app()->auth->isSuperAdmin(), - ]); -}); +class admin +{ + public static function init() + { + dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); + } -dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( - __('Packages repository'), - dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '#packman-repository-repository', - [dcPage::getPF(basename(__DIR__) . '/icon.svg'), dcPage::getPF(basename(__DIR__) . '/icon-dark.svg')], - preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']), - dcCore::app()->auth->isSuperAdmin() -); + public static function process() + { + dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void { + $favs->register(basename(__DIR__), [ + 'title' => __('Packages repository'), + 'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__), [], '#packman-repository-repository'), + 'small-icon' => [dcPage::getPF(basename(__DIR__) . '/icon.svg'), dcPage::getPF(basename(__DIR__) . '/icon-dark.svg')], + 'large-icon' => [dcPage::getPF(basename(__DIR__) . '/icon.svg'), dcPage::getPF(basename(__DIR__) . '/icon-dark.svg')], + //'permissions' => dcCore::app()->auth->isSuperAdmin(), + ]); + }); + + dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( + __('Packages repository'), + dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)) . '#packman-repository-repository', + [dcPage::getPF(basename(__DIR__) . '/icon.svg'), dcPage::getPF(basename(__DIR__) . '/icon-dark.svg')], + preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->auth->isSuperAdmin() + ); + } +} + +admin::init(); +admin::process(); diff --git a/_config.php b/_config.php index 1b5b16d..41f0b69 100644 --- a/_config.php +++ b/_config.php @@ -10,112 +10,145 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_CONTEXT_MODULE')) { return null; } -$redir = empty($_REQUEST['redir']) ? - dcCore::app()->admin->__get('list')->getURL() . '#plugins' : $_REQUEST['redir']; +/* dotclear ns */ +use dcCore; +use dcPage; -# -- Get settings -- -dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); -$s = dcCore::app()->blog->settings->__get(basename(__DIR__)); +/* clearbricks ns */ +use form; +use http; +use path; -$packman_pack_nocomment = $s->packman_pack_nocomment; -$packman_pack_fixnewline = $s->packman_pack_fixnewline; -$packman_pack_overwrite = $s->packman_pack_overwrite; -$packman_pack_filename = $s->packman_pack_filename; -$packman_secondpack_filename = $s->packman_secondpack_filename; -$packman_pack_repository = $s->packman_pack_repository; -$packman_pack_excludefiles = $s->packman_pack_excludefiles; +/* php ns */ +use Exception; -# -- Set settings -- -if (!empty($_POST['save'])) { - try { - $packman_pack_nocomment = !empty($_POST['packman_pack_nocomment']); - $packman_pack_fixnewline = !empty($_POST['packman_pack_fixnewline']); - $packman_pack_overwrite = !empty($_POST['packman_pack_overwrite']); - $packman_pack_filename = (string) $_POST['packman_pack_filename']; - $packman_secondpack_filename = (string) $_POST['packman_secondpack_filename']; - $packman_pack_repository = (string) path::real($_POST['packman_pack_repository'], false); - $packman_pack_excludefiles = (string) $_POST['packman_pack_excludefiles']; +class config +{ + public static function init(): void + { + dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); + } - $check = libPackman::is_configured( - $packman_pack_repository, - $packman_pack_filename, - $packman_secondpack_filename - ); - - if ($check) { - $s->put('packman_pack_nocomment', $packman_pack_nocomment); - $s->put('packman_pack_fixnewline', $packman_pack_fixnewline); - $s->put('packman_pack_overwrite', $packman_pack_overwrite); - $s->put('packman_pack_filename', $packman_pack_filename); - $s->put('packman_secondpack_filename', $packman_secondpack_filename); - $s->put('packman_pack_repository', $packman_pack_repository); - $s->put('packman_pack_excludefiles', $packman_pack_excludefiles); - - dcPage::addSuccessNotice( - __('Configuration has been successfully updated.') - ); - http::redirect( - dcCore::app()->admin->__get('list')->getURL('module=' . basename(__DIR__) . '&conf=1&redir=' . - dcCore::app()->admin->__get('list')->getRedir()) - ); + public static function process(): void + { + if (empty($_POST['save'])) { + return; } - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + + # -- Set settings -- + try { + $packman_pack_nocomment = !empty($_POST['packman_pack_nocomment']); + $packman_pack_fixnewline = !empty($_POST['packman_pack_fixnewline']); + $packman_pack_overwrite = !empty($_POST['packman_pack_overwrite']); + $packman_pack_filename = (string) $_POST['packman_pack_filename']; + $packman_secondpack_filename = (string) $_POST['packman_secondpack_filename']; + $packman_pack_repository = (string) path::real($_POST['packman_pack_repository'], false); + $packman_pack_excludefiles = (string) $_POST['packman_pack_excludefiles']; + + $check = Utils::is_configured( + $packman_pack_repository, + $packman_pack_filename, + $packman_secondpack_filename + ); + + if ($check) { + $s = dcCore::app()->blog->settings->__get(basename(__DIR__)); + $s->put('packman_pack_nocomment', $packman_pack_nocomment); + $s->put('packman_pack_fixnewline', $packman_pack_fixnewline); + $s->put('packman_pack_overwrite', $packman_pack_overwrite); + $s->put('packman_pack_filename', $packman_pack_filename); + $s->put('packman_secondpack_filename', $packman_secondpack_filename); + $s->put('packman_pack_repository', $packman_pack_repository); + $s->put('packman_pack_excludefiles', $packman_pack_excludefiles); + + dcPage::addSuccessNotice( + __('Configuration has been successfully updated.') + ); + http::redirect( + dcCore::app()->admin->__get('list')->getURL('module=' . basename(__DIR__) . '&conf=1&redir=' . + dcCore::app()->admin->__get('list')->getRedir()) + ); + } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + } + + public static function render(): void + { + # -- Get settings -- + $s = dcCore::app()->blog->settings->__get(basename(__DIR__)); + + $packman_pack_nocomment = $s->packman_pack_nocomment; + $packman_pack_fixnewline = $s->packman_pack_fixnewline; + $packman_pack_overwrite = $s->packman_pack_overwrite; + $packman_pack_filename = $s->packman_pack_filename; + $packman_secondpack_filename = $s->packman_secondpack_filename; + $packman_pack_repository = $s->packman_pack_repository; + $packman_pack_excludefiles = $s->packman_pack_excludefiles; + + # -- Display form -- + echo ' +
+

' . __('Root') . '

+ +

' . + '

' . sprintf( + __('Preconization: %s'), + dcCore::app()->blog->public_path ? + dcCore::app()->blog->public_path : __("Blog's public directory") + ) . '

+
+ +
+

' . __('Files') . '

+ +

+

' . sprintf(__('Preconization: %s'), '%type%-%id%') . '

+ +

+

' . sprintf(__('Preconization: %s'), '%type%-%id%-%version%') . '

+ +

+ +
+ +
+

' . __('Content') . '

+ +

+

' . sprintf(__('Preconization: %s'), '*.zip,*.tar,*.tar.gz') . '

+ +

+ +

+ +
'; } } -# -- Display form -- -echo ' -
-

' . __('Root') . '

- -

' . -'

' . sprintf( - __('Preconization: %s'), - dcCore::app()->blog->public_path ? - dcCore::app()->blog->public_path : __("Blog's public directory") -) . '

-
- -
-

' . __('Files') . '

- -

-

' . sprintf(__('Preconization: %s'), '%type%-%id%') . '

- -

-

' . sprintf(__('Preconization: %s'), '%type%-%id%-%version%') . '

- -

- -
- -
-

' . __('Content') . '

- -

-

' . sprintf(__('Preconization: %s'), '*.zip,*.tar,*.tar.gz') . '

- -

- -

- -
'; +config::init(); +config::process(); +config::render(); diff --git a/_define.php b/_define.php index bc9833d..16dedd2 100644 --- a/_define.php +++ b/_define.php @@ -10,6 +10,11 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +// can not use namespace as it is included inside a class method, +// and also can be included in third party plugins class methods. + if (!defined('DC_RC_PATH')) { return null; } diff --git a/_install.php b/_install.php index 90bec2f..3444674 100644 --- a/_install.php +++ b/_install.php @@ -10,84 +10,100 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_CONTEXT_ADMIN')) { return null; } -# -- Module specs -- +/* dotclear ns */ +use dcCore; -$mod_conf = [ - [ - 'packman_menu_plugins', - 'Add link to pacKman in plugins page', - false, - 'boolean', - ], - [ - 'packman_pack_nocomment', - 'Remove comments from files', - false, - 'boolean', - ], - [ - 'packman_pack_overwrite', - 'Overwrite existing package', - false, - 'boolean', - ], - [ - 'packman_pack_filename', - 'Name of package', - '%type%-%id%', - 'string', - ], - [ - 'packman_secondpack_filename', - 'Name of second package', - '%type%-%id%-%version%', - 'string', - ], - [ - 'packman_pack_repository', - 'Path to package repository', - '', - 'string', - ], - [ - 'packman_pack_excludefiles', - 'Extra files to exclude from package', - '*.zip,*.tar,*.tar.gz,.directory,.hg', - 'string', - ], -]; +/* php ns */ +use Exception; -# -- Nothing to change below -- - -try { - # Check module version - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; - } - - # Set module settings - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - foreach ($mod_conf as $v) { - dcCore::app()->blog->settings->__get(basename(__DIR__))->put( - $v[0], - $v[2], - $v[3], - $v[1], +class install +{ + # -- Module specs -- + private static $mod_conf = [ + [ + 'packman_menu_plugins', + 'Add link to pacKman in plugins page', false, - true - ); + 'boolean', + ], + [ + 'packman_pack_nocomment', + 'Remove comments from files', + false, + 'boolean', + ], + [ + 'packman_pack_overwrite', + 'Overwrite existing package', + false, + 'boolean', + ], + [ + 'packman_pack_filename', + 'Name of package', + '%type%-%id%', + 'string', + ], + [ + 'packman_secondpack_filename', + 'Name of second package', + '%type%-%id%-%version%', + 'string', + ], + [ + 'packman_pack_repository', + 'Path to package repository', + '', + 'string', + ], + [ + 'packman_pack_excludefiles', + 'Extra files to exclude from package', + '*.zip,*.tar,*.tar.gz,.directory,.hg', + 'string', + ], + ]; + + # -- Nothing to change below -- + public static function process() + { + try { + # Check module version + if (!dcCore::app()->newVersion( + basename(__DIR__), + dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') + )) { + return null; + } + + # Set module settings + dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); + foreach (self::$mod_conf as $v) { + dcCore::app()->blog->settings->__get(basename(__DIR__))->put( + $v[0], + $v[2], + $v[3], + $v[1], + false, + true + ); + } + + return true; + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + + return false; + } } - - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); - - return false; } + +return install::process(); diff --git a/_prepend.php b/_prepend.php index 6f05384..6e14812 100644 --- a/_prepend.php +++ b/_prepend.php @@ -10,12 +10,27 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_RC_PATH')) { return null; } -Clearbricks::lib()->autoload([ - 'dcPackman' => __DIR__ . '/inc/class.dc.packman.php', - 'libPackman' => __DIR__ . '/inc/lib.packman.php', - 'packmanFileZip' => __DIR__ . '/inc/lib.packman.filezip.php', -]); +/* clearbricks ns */ +use Clearbricks; + +class Prepend +{ + public static function init() + { + Clearbricks::lib()->autoload([ + 'plugins\\pacKman\\Core' => __DIR__ . '/inc/class.core.php', + 'plugins\\pacKman\\Utils' => __DIR__ . '/inc/class.utils.php', + 'plugins\\pacKman\\FileZip' => __DIR__ . '/inc/class.filezip.php', + ]); + } +} + +Prepend::init(); diff --git a/inc/class.core.php b/inc/class.core.php index 7f88807..03c025f 100644 --- a/inc/class.core.php +++ b/inc/class.core.php @@ -10,11 +10,29 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_CONTEXT_ADMIN')) { return null; } -class dcPackman +/* dotclear ns */ +use dcModules; +use dcThemes; + +/* clearbricks ns */ +use files; +use fileUnzip; +use path; + +/* packman ns */ + +/* php ns */ +use Exception; + +class Core { /** @var array Excluded files */ public static $exclude = [ @@ -129,12 +147,12 @@ class dcPackman $fp = fopen($dest, 'wb'); if ($nocomment) { - packmanFileZip::$remove_comment = true; + FileZip::$remove_comment = true; } if ($fixnewline) { - packmanFileZip::$fix_newline = true; + FileZip::$fix_newline = true; } - $zip = new packmanFileZip($fp); + $zip = new FileZip($fp); foreach ($exclude as $e) { $zip->addExclusion($e); diff --git a/inc/class.filezip.php b/inc/class.filezip.php index 214f0fc..b30aaba 100644 --- a/inc/class.filezip.php +++ b/inc/class.filezip.php @@ -10,7 +10,18 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -class packmanFileZip extends fileZip +declare(strict_types=1); + +namespace plugins\pacKman; + +if (!defined('DC_CONTEXT_ADMIN')) { + return null; +} + +/* clearbricks ns */ +use fileZip; + +class FileZip extends fileZip { /** @var boolean Remove comments from files content */ public static $remove_comment = false; diff --git a/inc/class.utils.php b/inc/class.utils.php index c5cfef3..965aa5b 100644 --- a/inc/class.utils.php +++ b/inc/class.utils.php @@ -10,11 +10,23 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_CONTEXT_ADMIN')) { return null; } -class libPackman +/* dotclear ns */ +use dcCore; + +/* clearbricks ns */ +use form; +use html; +use path; + +class Utils { public static function is_configured(string $repo, string $file_a, string $file_b): bool { @@ -203,10 +215,10 @@ class libPackman __(html::escapeHTML($module['name'])) . '' . '' . - '' . html::escapeHTML(basename($module['root'])) . '' . '' . diff --git a/index.php b/index.php index 482bffe..50ff1d4 100644 --- a/index.php +++ b/index.php @@ -10,302 +10,340 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ +declare(strict_types=1); + +namespace plugins\pacKman; + if (!defined('DC_CONTEXT_ADMIN')) { return null; } -dcPage::checkSuper(); +/* dotclear ns */ +use dcCore; +use dcPage; +use dcThemes; -# Queries -$action = $_POST['action'] ?? ''; -$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : ''; +/* clearbricks ns */ +use files; +use http; +use path; -# Settings -dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); -$s = dcCore::app()->blog->settings->__get(basename(__DIR__)); +/* php ns */ +use Exception; -# Modules -if (!(dcCore::app()->themes instanceof dcThemes)) { - dcCore::app()->themes = new dcThemes(); - dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null); -} -$themes = dcCore::app()->themes; -$plugins = dcCore::app()->plugins; +class index +{ + private static $plugins_path = ''; + private static $themes_path = ''; -# Paths -$ppexp = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT); -$pppop = array_pop($ppexp); -$plugins_path = (string) path::real($pppop); -$themes_path = dcCore::app()->blog->themes_path; -$repo_path = $s->packman_pack_repository; + public static function init() + { + dcPage::checkSuper(); + dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); -# Rights -$is_writable = libPackman::is_writable( - $s->packman_pack_repository, - $s->packman_pack_filename -); -$is_editable = !empty($type) - && !empty($_POST['modules']) - && is_array($_POST['modules']); + # Paths + $e = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT); + $p = array_pop($e); + self::$plugins_path = (string) path::real($p); + self::$themes_path = dcCore::app()->blog->themes_path; + } -$is_configured = libPackman::is_configured( - $s->packman_pack_repository, - $s->packman_pack_filename, - $s->packman_secondpack_filename -); + public static function process() + { + # Queries + $action = $_POST['action'] ?? ''; + $type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : ''; -# Actions -try { - # Download - if (isset($_REQUEST['package']) && empty($type)) { - $modules = []; - if ($type == 'plugins') { - $modules = dcPackman::getPackages($plugins_path); - } elseif ($type == 'themes') { - $modules = dcPackman::getPackages($themes_path); - } else { - $modules = array_merge( - dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_pack_filename)), - dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_secondpack_filename)) - ); + # Settings + $s = dcCore::app()->blog->settings->__get(basename(__DIR__)); + + # Modules + if (!(dcCore::app()->themes instanceof dcThemes)) { + dcCore::app()->themes = new dcThemes(); + dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null); } + $themes = dcCore::app()->themes; + $plugins = dcCore::app()->plugins; - foreach ($modules as $f) { - if (preg_match('/' . preg_quote($_REQUEST['package']) . '$/', $f['root']) - && is_file($f['root']) && is_readable($f['root']) - ) { - # --BEHAVIOR-- packmanBeforeDownloadPackage - dcCore::app()->callBehavior('packmanBeforeDownloadPackage', $f, $type); + # Rights + $is_writable = Utils::is_writable( + $s->packman_pack_repository, + $s->packman_pack_filename + ); + $is_editable = !empty($type) + && !empty($_POST['modules']) + && is_array($_POST['modules']); - header('Content-Type: application/zip'); - header('Content-Length: ' . filesize($f['root'])); - header('Content-Disposition: attachment; filename="' . basename($f['root']) . '"'); - readfile($f['root']); + # Actions + try { + # Download + if (isset($_REQUEST['package']) && empty($type)) { + $modules = []; + if ($type == 'plugins') { + $modules = Core::getPackages(self::$plugins_path); + } elseif ($type == 'themes') { + $modules = Core::getPackages(self::$themes_path); + } else { + $modules = array_merge( + Core::getPackages(dirname($s->packman_pack_repository . '/' . $s->packman_pack_filename)), + Core::getPackages(dirname($s->packman_pack_repository . '/' . $s->packman_secondpack_filename)) + ); + } - # --BEHAVIOR-- packmanAfterDownloadPackage - dcCore::app()->callBehavior('packmanAfterDownloadPackage', $f, $type); + foreach ($modules as $f) { + if (preg_match('/' . preg_quote($_REQUEST['package']) . '$/', $f['root']) + && is_file($f['root']) && is_readable($f['root']) + ) { + # --BEHAVIOR-- packmanBeforeDownloadPackage + dcCore::app()->callBehavior('packmanBeforeDownloadPackage', $f, $type); + header('Content-Type: application/zip'); + header('Content-Length: ' . filesize($f['root'])); + header('Content-Disposition: attachment; filename="' . basename($f['root']) . '"'); + readfile($f['root']); + + # --BEHAVIOR-- packmanAfterDownloadPackage + dcCore::app()->callBehavior('packmanAfterDownloadPackage', $f, $type); + + exit; + } + } + + # Not found + header('Content-Type: text/plain'); + http::head(404, 'Not Found'); exit; + } elseif (!empty($action) && !$is_editable) { + throw new Exception('No selected modules'); + + # Pack + } elseif ($action == 'packup') { + foreach ($_POST['modules'] as $root => $id) { + if (!Utils::moduleExists($type, $id)) { + throw new Exception('No such module'); + } + + $module = Utils::getModules($type, $id); + $module['id'] = $id; + $module['type'] = $type == 'themes' ? 'theme' : 'plugin'; + + $root = (string) $s->packman_pack_repository; + $files = [ + (string) $s->packman_pack_filename, + (string) $s->packman_secondpack_filename, + ]; + $nocomment = (bool) $s->packman_pack_nocomment; + $fixnewline = (bool) $s->packman_pack_fixnewline; + $overwrite = (bool) $s->packman_pack_overwrite; + $exclude = explode(',', (string) $s->packman_pack_excludefiles); + + # --BEHAVIOR-- packmanBeforeCreatePackage + dcCore::app()->callBehavior('packmanBeforeCreatePackage', $module); + + Core::pack($module, $root, $files, $overwrite, $exclude, $nocomment, $fixnewline); + + # --BEHAVIOR-- packmanAfterCreatePackage + dcCore::app()->callBehavior('packmanAfterCreatePackage', $module); + } + + dcPage::addSuccessNotice( + __('Package successfully created.') + ); + + if (!empty($_POST['redir'])) { + http::redirect($_POST['redir']); + } else { + dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-' . $type); + } + + # Delete + } elseif ($action == 'delete') { + foreach ($_POST['modules'] as $root => $id) { + if (!file_exists($root) || !files::isDeletable($root)) { + throw new Exception('Undeletable file: ' . $root); + } + + unlink($root); + } + + dcPage::addSuccessNotice( + __('Package successfully deleted.') + ); + + if (!empty($_POST['redir'])) { + http::redirect($_POST['redir']); + } else { + dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); + } + + # Install + } elseif ($action == 'install') { + foreach ($_POST['modules'] as $root => $id) { + # --BEHAVIOR-- packmanBeforeInstallPackage + dcCore::app()->callBehavior('packmanBeforeInstallPackage', $type, $id, $root); + + if ($type == 'plugins') { + $plugins->installPackage($root, $plugins); + } + if ($type == 'themes') { + $themes->installPackage($root, $themes); + } + + # --BEHAVIOR-- packmanAfterInstallPackage + dcCore::app()->callBehavior('packmanAfterInstallPackage', $type, $id, $root); + } + + dcPage::addSuccessNotice( + __('Package successfully installed.') + ); + + if (!empty($_POST['redir'])) { + http::redirect($_POST['redir']); + } else { + dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); + } + + # Copy + } elseif (strpos($action, 'copy_to_') !== false) { + $dest = $s->packman_pack_repository; + if ($action == 'copy_to_plugins') { + $dest = self::$plugins_path; + } elseif ($action == 'copy_to_themes') { + $dest = self::$themes_path; + } + + foreach ($_POST['modules'] as $root => $id) { + file_put_contents( + $dest . '/' . basename($root), + file_get_contents($root) + ); + } + + dcPage::addSuccessNotice( + __('Package successfully copied.') + ); + + if (!empty($_POST['redir'])) { + http::redirect($_POST['redir']); + } else { + dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); + } + + # Move + } elseif (strpos($action, 'move_to_') !== false) { + $dest = $s->packman_pack_repository; + if ($action == 'move_to_plugins') { + $dest = self::$plugins_path; + } elseif ($action == 'move_to_themes') { + $dest = self::$themes_path; + } + + foreach ($_POST['modules'] as $root => $id) { + file_put_contents( + $dest . '/' . basename($root), + file_get_contents($root) + ); + unlink($root); + } + + dcPage::addSuccessNotice( + __('Package successfully moved.') + ); + + if (!empty($_POST['redir'])) { + http::redirect($_POST['redir']); + } else { + dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); + } } - } - - # Not found - header('Content-Type: text/plain'); - http::head(404, 'Not Found'); - exit; - } elseif (!empty($action) && !$is_editable) { - throw new Exception('No selected modules'); - - # Pack - } elseif ($action == 'packup') { - foreach ($_POST['modules'] as $root => $id) { - if (!libPackman::moduleExists($type, $id)) { - throw new Exception('No such module'); - } - - $module = libPackman::getModules($type, $id); - $module['id'] = $id; - $module['type'] = $type == 'themes' ? 'theme' : 'plugin'; - - $root = $s->packman_pack_repository; - $files = [ - $s->packman_pack_filename, - $s->packman_secondpack_filename, - ]; - $nocomment = $s->packman_pack_nocomment; - $fixnewline = $s->packman_pack_fixnewline; - $overwrite = $s->packman_pack_overwrite; - $exclude = explode(',', $s->packman_pack_excludefiles); - - # --BEHAVIOR-- packmanBeforeCreatePackage - dcCore::app()->callBehavior('packmanBeforeCreatePackage', $module); - - dcPackman::pack($module, $root, $files, $overwrite, $exclude, $nocomment, $fixnewline); - - # --BEHAVIOR-- packmanAfterCreatePackage - dcCore::app()->callBehavior('packmanAfterCreatePackage', $module); - } - - dcPage::addSuccessNotice( - __('Package successfully created.') - ); - - if (!empty($_POST['redir'])) { - http::redirect($_POST['redir']); - } else { - dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-' . $type); - } - - # Delete - } elseif ($action == 'delete') { - foreach ($_POST['modules'] as $root => $id) { - if (!file_exists($root) || !files::isDeletable($root)) { - throw new Exception('Undeletable file: ' . $root); - } - - unlink($root); - } - - dcPage::addSuccessNotice( - __('Package successfully deleted.') - ); - - if (!empty($_POST['redir'])) { - http::redirect($_POST['redir']); - } else { - dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); - } - - # Install - } elseif ($action == 'install') { - foreach ($_POST['modules'] as $root => $id) { - # --BEHAVIOR-- packmanBeforeInstallPackage - dcCore::app()->callBehavior('packmanBeforeInstallPackage', $type, $id, $root); - - if ($type == 'plugins') { - $plugins->installPackage($root, $plugins); - } - if ($type == 'themes') { - $themes->installPackage($root, $themes); - } - - # --BEHAVIOR-- packmanAfterInstallPackage - dcCore::app()->callBehavior('packmanAfterInstallPackage', $type, $id, $root); - } - - dcPage::addSuccessNotice( - __('Package successfully installed.') - ); - - if (!empty($_POST['redir'])) { - http::redirect($_POST['redir']); - } else { - dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); - } - - # Copy - } elseif (strpos($action, 'copy_to_') !== false) { - $dest = $repo_path; - if ($action == 'copy_to_plugins') { - $dest = $plugins_path; - } elseif ($action == 'copy_to_themes') { - $dest = $themes_path; - } - - foreach ($_POST['modules'] as $root => $id) { - file_put_contents( - $dest . '/' . basename($root), - file_get_contents($root) - ); - } - - dcPage::addSuccessNotice( - __('Package successfully copied.') - ); - - if (!empty($_POST['redir'])) { - http::redirect($_POST['redir']); - } else { - dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); - } - - # Move - } elseif (strpos($action, 'move_to_') !== false) { - $dest = $repo_path; - if ($action == 'move_to_plugins') { - $dest = $plugins_path; - } elseif ($action == 'move_to_themes') { - $dest = $themes_path; - } - - foreach ($_POST['modules'] as $root => $id) { - file_put_contents( - $dest . '/' . basename($root), - file_get_contents($root) - ); - unlink($root); - } - - dcPage::addSuccessNotice( - __('Package successfully moved.') - ); - - if (!empty($_POST['redir'])) { - http::redirect($_POST['redir']); - } else { - dcCore::app()->adminurl->redirect('admin.plugin.' . basename(__DIR__), [], '#packman-repository-' . $type); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); } } -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + + public static function render() + { + # Settings + $s = dcCore::app()->blog->settings->__get(basename(__DIR__)); + + $is_configured = Utils::is_configured( + $s->packman_pack_repository, + $s->packman_pack_filename, + $s->packman_secondpack_filename + ); + + # Display + echo + '' . __('pacKman') . '' . + dcPage::jsPageTabs() . + dcPage::jsLoad(dcPage::getPF(basename(__DIR__) . '/js/packman.js')); + + # --BEHAVIOR-- packmanAdminHeader + dcCore::app()->callBehavior('packmanAdminHeader'); + + echo + '' . + + dcPage::breadcrumb([ + __('Plugins') => '', + __('pacKman') => '', + ]) . + dcPage::notices(); + + if (dcCore::app()->error->flag() || !$is_configured) { + echo + '
' . __('pacKman is not well configured.') . ' ' . + '' . __('Configuration') . '' . + '
'; + } else { + $repo_path_modules = array_merge( + Core::getPackages(dirname($s->packman_pack_repository . '/' . $s->packman_pack_filename)), + Core::getPackages(dirname($s->packman_pack_repository . '/' . $s->packman_secondpack_filename)) + ); + $plugins_path_modules = Core::getPackages(self::$plugins_path); + $themes_path_modules = Core::getPackages(self::$themes_path); + + Utils::modules( + Utils::getModules('plugins'), + 'plugins', + __('Installed plugins') + ); + + Utils::modules( + Utils::getModules('themes'), + 'themes', + __('Installed themes') + ); + + Utils::repository( + $plugins_path_modules, + 'plugins', + __('Plugins root') + ); + + Utils::repository( + $themes_path_modules, + 'themes', + __('Themes root') + ); + + Utils::repository( + $repo_path_modules, + 'repository', + __('Packages repository') + ); + } + + # --BEHAVIOR-- packmanAdminTabs + dcCore::app()->callBehavior('packmanAdminTabs'); + + dcPage::helpBlock('pacKman'); + + echo + ''; + } } -# Display -echo -'' . __('pacKman') . '' . -dcPage::jsPageTabs() . -dcPage::jsLoad(dcPage::getPF(basename(__DIR__) . '/js/packman.js')); - -# --BEHAVIOR-- packmanAdminHeader -dcCore::app()->callBehavior('packmanAdminHeader'); - -echo -'' . - -dcPage::breadcrumb([ - __('Plugins') => '', - __('pacKman') => '', -]) . -dcPage::notices(); - -if (dcCore::app()->error->flag() || !$is_configured) { - echo - '
' . __('pacKman is not well configured.') . ' ' . - '' . __('Configuration') . '' . - '
'; -} else { - $repo_path_modules = array_merge( - dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_pack_filename)), - dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_secondpack_filename)) - ); - $plugins_path_modules = dcPackman::getPackages($plugins_path); - $themes_path_modules = dcPackman::getPackages($themes_path); - - libPackman::modules( - libPackman::getModules('plugins'), - 'plugins', - __('Installed plugins') - ); - - libPackman::modules( - libPackman::getModules('themes'), - 'themes', - __('Installed themes') - ); - - libPackman::repository( - $plugins_path_modules, - 'plugins', - __('Plugins root') - ); - - libPackman::repository( - $themes_path_modules, - 'themes', - __('Themes root') - ); - - libPackman::repository( - $repo_path_modules, - 'repository', - __('Packages repository') - ); -} - -# --BEHAVIOR-- packmanAdminTabs -dcCore::app()->callBehavior('packmanAdminTabs'); - -dcPage::helpBlock('pacKman'); - -echo -''; +index::init(); +index::process(); +index::render();