pacKman/inc/Manage.php

369 lines
13 KiB
PHP
Raw Normal View History

2021-08-16 20:48:26 +00:00
<?php
2021-09-02 20:14:32 +00:00
/**
* @brief pacKman, a plugin for Dotclear 2
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @author Jean-Christian Denis
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2022-12-17 21:12:10 +00:00
declare(strict_types=1);
2022-12-18 23:21:11 +00:00
namespace Dotclear\Plugin\pacKman;
2021-08-16 20:48:26 +00:00
2022-12-17 21:12:10 +00:00
/* dotclear ns */
use dcCore;
use dcPage;
use dcThemes;
/* clearbricks ns */
use files;
use http;
use path;
/* php ns */
use Exception;
2022-12-18 23:21:11 +00:00
class Manage
2022-12-17 21:12:10 +00:00
{
private static $plugins_path = '';
private static $themes_path = '';
2022-12-18 23:21:11 +00:00
private static $init = false;
2022-12-17 21:12:10 +00:00
2022-12-18 23:21:11 +00:00
public static function init(): bool
2022-12-17 21:12:10 +00:00
{
2022-12-18 23:21:11 +00:00
if (defined('DC_CONTEXT_ADMIN')) {
dcPage::checkSuper();
dcCore::app()->blog->settings->addNamespace(Core::id());
# 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;
self::$init = true;
}
return self::$init;
2022-12-17 21:12:10 +00:00
}
2021-08-17 21:12:33 +00:00
2022-12-18 23:21:11 +00:00
public static function process(): void
2022-12-17 21:12:10 +00:00
{
2022-12-18 23:21:11 +00:00
if (!self::$init) {
return;
}
2022-12-17 21:12:10 +00:00
# Queries
$action = $_POST['action'] ?? '';
$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : '';
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
# Settings
2022-12-18 23:21:11 +00:00
$s = dcCore::app()->blog->settings->get(Core::id());
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
# Modules
if (!(dcCore::app()->themes instanceof dcThemes)) {
dcCore::app()->themes = new dcThemes();
dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null);
2021-08-17 21:12:33 +00:00
}
2022-12-17 21:12:10 +00:00
$themes = dcCore::app()->themes;
$plugins = dcCore::app()->plugins;
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
# Rights
$is_writable = Utils::is_writable(
2022-12-19 11:04:34 +00:00
$s->get('pack_repository'),
$s->get('pack_filename')
2021-08-17 21:12:33 +00:00
);
2022-12-17 21:12:10 +00:00
$is_editable = !empty($type)
&& !empty($_POST['modules'])
&& is_array($_POST['modules']);
# 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(
2022-12-19 11:04:34 +00:00
Core::getPackages(dirname($s->get('pack_repository') . '/' . $s->get('pack_filename'))),
Core::getPackages(dirname($s->get('pack_repository') . '/' . $s->get('secondpack_filename')))
2022-12-17 21:12:10 +00:00
);
}
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) {
2022-12-20 00:31:07 +00:00
dcPage::addErrorNotice(
__('No modules selected.')
);
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), [], '#packman-' . $type);
}
2022-12-17 21:12:10 +00:00
# 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';
2022-12-19 11:04:34 +00:00
$root = (string) $s->get('pack_repository');
2022-12-17 21:12:10 +00:00
$files = [
2022-12-19 11:04:34 +00:00
(string) $s->get('pack_filename'),
(string) $s->get('secondpack_filename'),
2022-12-17 21:12:10 +00:00
];
2022-12-19 11:04:34 +00:00
$nocomment = (bool) $s->get('pack_nocomment');
$fixnewline = (bool) $s->get('pack_fixnewline');
$overwrite = (bool) $s->get('pack_overwrite');
$exclude = explode(',', (string) $s->get('pack_excludefiles'));
2022-12-17 21:12:10 +00:00
# --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 {
2022-12-18 23:21:11 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), [], '#packman-' . $type);
2022-12-17 21:12:10 +00:00
}
# Delete
} elseif ($action == 'delete') {
2022-12-20 00:31:07 +00:00
$del_success= false;
2022-12-17 21:12:10 +00:00
foreach ($_POST['modules'] as $root => $id) {
if (!file_exists($root) || !files::isDeletable($root)) {
2022-12-20 00:31:07 +00:00
dcPage::addWarningNotice(sprintf(__('Undeletable file "%s"', $root)));
} else {
$del_success = true;
2022-12-17 21:12:10 +00:00
}
unlink($root);
}
2022-12-20 00:31:07 +00:00
if ($del_success) {
dcPage::addSuccessNotice(
__('Package successfully deleted.')
);
}
2022-12-17 21:12:10 +00:00
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-12-18 23:21:11 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), [], '#packman-repository-' . $type);
2022-12-17 21:12:10 +00:00
}
# 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 {
2022-12-18 23:21:11 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), [], '#packman-repository-' . $type);
2022-12-17 21:12:10 +00:00
}
# Copy
} elseif (strpos($action, 'copy_to_') !== false) {
2022-12-19 11:04:34 +00:00
$dest = (string) $s->get('pack_repository');
2022-12-17 21:12:10 +00:00
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 {
2022-12-18 23:21:11 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), [], '#packman-repository-' . $type);
2022-12-17 21:12:10 +00:00
}
# Move
} elseif (strpos($action, 'move_to_') !== false) {
2022-12-19 11:04:34 +00:00
$dest = (string) $s->get('pack_repository');
2022-12-17 21:12:10 +00:00
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 {
2022-12-18 23:21:11 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), [], '#packman-repository-' . $type);
2022-12-17 21:12:10 +00:00
}
2021-08-17 21:12:33 +00:00
}
2022-12-17 21:12:10 +00:00
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
2021-10-23 14:46:03 +00:00
}
2022-12-17 21:12:10 +00:00
}
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
public static function render()
{
2022-12-18 23:21:11 +00:00
if (!self::$init) {
return false;
}
2022-12-17 21:12:10 +00:00
# Settings
2022-12-18 23:21:11 +00:00
$s = dcCore::app()->blog->settings->get(Core::id());
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
$is_configured = Utils::is_configured(
2022-12-19 11:04:34 +00:00
$s->get('pack_repository'),
$s->get('pack_filename'),
$s->get('secondpack_filename')
2021-08-17 21:12:33 +00:00
);
2021-10-23 14:46:03 +00:00
2022-12-17 21:12:10 +00:00
# Display
echo
'<html><head><title>' . __('pacKman') . '</title>' .
dcPage::jsPageTabs() .
2022-12-18 23:21:11 +00:00
dcPage::jsLoad(dcPage::getPF(Core::id() . '/js/packman.js'));
2022-12-17 21:12:10 +00:00
# --BEHAVIOR-- packmanAdminHeader
dcCore::app()->callBehavior('packmanAdminHeader');
echo
'</head><body>' .
dcPage::breadcrumb([
__('Plugins') => '',
__('pacKman') => '',
]) .
dcPage::notices();
if (dcCore::app()->error->flag() || !$is_configured) {
echo
'<div class="warning">' . __('pacKman is not well configured.') . ' ' .
2022-12-18 23:21:11 +00:00
'<a href="' . dcCore::app()->adminurl->get('admin.plugins', ['module' => Core::id(), 'conf' => '1', 'redir' => dcCore::app()->adminurl->get('admin.plugin.' . Core::id())]) . '">' . __('Configuration') . '</a>' .
2022-12-17 21:12:10 +00:00
'</div>';
2021-10-23 14:46:03 +00:00
} else {
2022-12-17 21:12:10 +00:00
$repo_path_modules = array_merge(
2022-12-19 11:04:34 +00:00
Core::getPackages(dirname($s->get('pack_repository') . '/' . $s->get('pack_filename'))),
Core::getPackages(dirname($s->get('pack_repository') . '/' . $s->get('secondpack_filename')))
2022-12-17 21:12:10 +00:00
);
$plugins_path_modules = Core::getPackages(self::$plugins_path);
$themes_path_modules = Core::getPackages(self::$themes_path);
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
Utils::modules(
Utils::getModules('plugins'),
'plugins',
__('Installed plugins')
2021-08-17 21:12:33 +00:00
);
2022-12-17 21:12:10 +00:00
Utils::modules(
Utils::getModules('themes'),
'themes',
__('Installed themes')
);
2021-10-23 14:46:03 +00:00
2022-12-17 21:12:10 +00:00
Utils::repository(
$plugins_path_modules,
'plugins',
__('Plugins root')
);
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
Utils::repository(
$themes_path_modules,
'themes',
__('Themes root')
);
2021-08-17 21:12:33 +00:00
2022-12-17 21:12:10 +00:00
Utils::repository(
$repo_path_modules,
'repository',
__('Packages repository')
2021-08-17 21:12:33 +00:00
);
}
2022-12-17 21:12:10 +00:00
# --BEHAVIOR-- packmanAdminTabs
dcCore::app()->callBehavior('packmanAdminTabs');
2021-10-23 14:46:03 +00:00
2022-12-17 21:12:10 +00:00
dcPage::helpBlock('pacKman');
2021-08-16 20:48:26 +00:00
2022-12-17 21:12:10 +00:00
echo
'</body></html>';
}
2021-08-16 20:48:26 +00:00
}