pacKman/index.php

315 lines
9.3 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
*/
2021-08-16 20:48:26 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
2021-08-17 21:12:33 +00:00
return null;
2021-08-16 20:48:26 +00:00
}
dcPage::checkSuper();
# Queries
2021-11-01 10:39:02 +00:00
$action = $_POST['action'] ?? '';
$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : '';
2021-08-16 20:48:26 +00:00
# Settings
2022-11-12 20:42:05 +00:00
dcCore::app()->blog->settings->addNamespace('pacKman');
$s = dcCore::app()->blog->settings->pacKman;
2021-08-16 20:48:26 +00:00
# Modules
2022-11-12 20:42:05 +00:00
if (!isset(dcCore::app()->themes)) {
dcCore::app()->themes = new dcThemes(dcCore::app());
dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null);
2021-08-16 20:48:26 +00:00
}
2022-11-12 20:42:05 +00:00
$themes = dcCore::app()->themes;
$plugins = dcCore::app()->plugins;
2021-08-16 20:48:26 +00:00
# Paths
2021-11-01 10:39:02 +00:00
$ppexp = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
$pppop = array_pop($ppexp);
2021-11-08 21:19:07 +00:00
$plugins_path = (string) path::real($pppop);
2022-11-12 20:42:05 +00:00
$themes_path = dcCore::app()->blog->themes_path;
2021-11-01 10:39:02 +00:00
$repo_path = $s->packman_pack_repository;
2021-08-16 20:48:26 +00:00
# Rights
$is_writable = libPackman::is_writable(
2021-08-17 21:12:33 +00:00
$s->packman_pack_repository,
$s->packman_pack_filename
2021-08-16 20:48:26 +00:00
);
2021-11-01 10:39:02 +00:00
$is_editable = !empty($type)
&& !empty($_POST['modules'])
2021-08-17 21:12:33 +00:00
&& is_array($_POST['modules']);
2021-08-16 20:48:26 +00:00
$is_configured = libPackman::is_configured(
2021-08-17 21:12:33 +00:00
$s->packman_pack_repository,
$s->packman_pack_filename,
$s->packman_secondpack_filename
2021-08-16 20:48:26 +00:00
);
# Actions
2021-10-23 14:46:03 +00:00
try {
2021-08-17 21:12:33 +00:00
# Download
if (isset($_REQUEST['package']) && empty($type)) {
2021-08-23 13:07:29 +00:00
$modules = [];
2021-08-17 21:12:33 +00:00
if ($type == 'plugins') {
2022-11-12 20:42:05 +00:00
$modules = dcPackman::getPackages($plugins_path);
2021-08-23 13:07:29 +00:00
} elseif ($type == 'themes') {
2022-11-12 20:42:05 +00:00
$modules = dcPackman::getPackages($themes_path);
2021-08-23 13:07:29 +00:00
} else {
2021-08-17 21:12:33 +00:00
$modules = array_merge(
2022-11-12 20:42:05 +00:00
dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_pack_filename)),
dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_secondpack_filename))
2021-08-17 21:12:33 +00:00
);
}
2021-11-01 10:39:02 +00:00
foreach ($modules as $f) {
2021-08-17 21:12:33 +00:00
if (preg_match('/' . preg_quote($_REQUEST['package']) . '$/', $f['root'])
2021-10-23 14:46:03 +00:00
&& is_file($f['root']) && is_readable($f['root'])
) {
2021-08-17 21:12:33 +00:00
# --BEHAVIOR-- packmanBeforeDownloadPackage
2022-11-12 20:42:05 +00:00
dcCore::app()->callBehavior('packmanBeforeDownloadPackage', $f, $type);
2021-08-17 21:12:33 +00:00
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($f['root']));
header('Content-Disposition: attachment; filename="' . basename($f['root']) . '"');
readfile($f['root']);
# --BEHAVIOR-- packmanAfterDownloadPackage
2022-11-12 20:42:05 +00:00
dcCore::app()->callBehavior('packmanAfterDownloadPackage', $f, $type);
2021-08-17 21:12:33 +00:00
exit;
}
}
# Not found
header('Content-Type: text/plain');
http::head(404, 'Not Found');
exit;
2021-08-23 13:07:29 +00:00
} elseif (!empty($action) && !$is_editable) {
2021-08-17 21:12:33 +00:00
throw new Exception('No selected modules');
# Pack
2021-08-23 13:07:29 +00:00
} elseif ($action == 'packup') {
2021-08-17 21:12:33 +00:00
foreach ($_POST['modules'] as $root => $id) {
if (!${$type}->moduleExists($id)) {
throw new Exception('No such module');
}
2021-11-01 10:39:02 +00:00
$module = ${$type}->getModules($id);
$module['id'] = $id;
2021-08-17 21:12:33 +00:00
$module['type'] = $type == 'themes' ? 'theme' : 'plugin';
2021-11-01 10:39:02 +00:00
$root = $s->packman_pack_repository;
2021-08-23 13:07:29 +00:00
$files = [
2021-08-17 21:12:33 +00:00
$s->packman_pack_filename,
$s->packman_secondpack_filename
2021-08-23 13:07:29 +00:00
];
2021-11-01 10:39:02 +00:00
$nocomment = $s->packman_pack_nocomment;
$fixnewline = $s->packman_pack_fixnewline;
2021-11-01 10:39:02 +00:00
$overwrite = $s->packman_pack_overwrite;
$exclude = explode(',', $s->packman_pack_excludefiles);
2021-08-17 21:12:33 +00:00
# --BEHAVIOR-- packmanBeforeCreatePackage
2022-11-26 21:01:17 +00:00
dcCore::app()->callBehavior('packmanBeforeCreatePackage', $module);
2021-08-17 21:12:33 +00:00
dcPackman::pack($module, $root, $files, $overwrite, $exclude, $nocomment, $fixnewline);
2021-08-17 21:12:33 +00:00
# --BEHAVIOR-- packmanAfterCreatePackage
2022-11-26 21:01:17 +00:00
dcCore::app()->callBehavior('packmanAfterCreatePackage', $module);
2021-08-17 21:12:33 +00:00
}
dcPage::addSuccessNotice(
__('Package successfully created.')
);
2021-10-23 14:46:03 +00:00
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-12 20:42:05 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.pacKman', [], '#packman-' . $type);
2021-10-23 14:46:03 +00:00
}
2021-08-17 21:12:33 +00:00
2021-11-01 10:39:02 +00:00
# Delete
2021-08-23 13:07:29 +00:00
} elseif ($action == 'delete') {
2021-08-17 21:12:33 +00:00
foreach ($_POST['modules'] as $root => $id) {
2021-11-01 10:39:02 +00:00
if (!file_exists($root) || !files::isDeletable($root)) {
2021-08-17 21:12:33 +00:00
throw new Exception('Undeletable file: ' . $root);
}
unlink($root);
}
dcPage::addSuccessNotice(
__('Package successfully deleted.')
);
2021-10-23 14:46:03 +00:00
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-12 20:42:05 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.pacKman', [], '#packman-repository-' . $type);
2021-10-23 14:46:03 +00:00
}
2021-08-17 21:12:33 +00:00
2021-11-01 10:39:02 +00:00
# Install
2021-08-23 13:07:29 +00:00
} elseif ($action == 'install') {
2021-08-17 21:12:33 +00:00
foreach ($_POST['modules'] as $root => $id) {
# --BEHAVIOR-- packmanBeforeInstallPackage
2022-11-12 20:42:05 +00:00
dcCore::app()->callBehavior('packmanBeforeInstallPackage', $type, $id, $root);
2021-08-17 21:12:33 +00:00
if ($type == 'plugins') {
$plugins->installPackage($root, $plugins);
}
if ($type == 'themes') {
$themes->installPackage($root, $themes);
}
# --BEHAVIOR-- packmanAfterInstallPackage
2022-11-12 20:42:05 +00:00
dcCore::app()->callBehavior('packmanAfterInstallPackage', $type, $id, $root);
2021-08-17 21:12:33 +00:00
}
dcPage::addSuccessNotice(
__('Package successfully installed.')
);
2021-10-23 14:46:03 +00:00
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-12 20:42:05 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.pacKman', [], '#packman-repository-' . $type);
2021-10-23 14:46:03 +00:00
}
2021-08-17 21:12:33 +00:00
2021-11-01 10:39:02 +00:00
# Copy
2021-08-23 13:07:29 +00:00
} elseif (strpos($action, 'copy_to_') !== false) {
2021-11-08 21:19:07 +00:00
$dest = $repo_path;
2021-08-17 21:12:33 +00:00
if ($action == 'copy_to_plugins') {
$dest = $plugins_path;
2021-08-23 13:07:29 +00:00
} elseif ($action == 'copy_to_themes') {
2021-08-17 21:12:33 +00:00
$dest = $themes_path;
}
foreach ($_POST['modules'] as $root => $id) {
file_put_contents(
$dest . '/' . basename($root),
file_get_contents($root)
);
}
dcPage::addSuccessNotice(
__('Package successfully copied.')
);
2021-10-23 14:46:03 +00:00
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-12 20:42:05 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.pacKman', [], '#packman-repository-' . $type);
2021-10-23 14:46:03 +00:00
}
2021-08-17 21:12:33 +00:00
2021-11-01 10:39:02 +00:00
# Move
2021-08-23 13:07:29 +00:00
} elseif (strpos($action, 'move_to_') !== false) {
2021-11-08 21:19:07 +00:00
$dest = $repo_path;
2021-08-17 21:12:33 +00:00
if ($action == 'move_to_plugins') {
$dest = $plugins_path;
2021-08-23 13:07:29 +00:00
} elseif ($action == 'move_to_themes') {
2021-08-17 21:12:33 +00:00
$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.')
);
2021-10-23 14:46:03 +00:00
if (!empty($_POST['redir'])) {
http::redirect($_POST['redir']);
} else {
2022-11-12 20:42:05 +00:00
dcCore::app()->adminurl->redirect('admin.plugin.pacKman', [], '#packman-repository-' . $type);
2021-10-23 14:46:03 +00:00
}
2021-08-17 21:12:33 +00:00
}
2021-11-01 10:39:02 +00:00
} catch (Exception $e) {
2022-11-12 20:42:05 +00:00
dcCore::app()->error->add($e->getMessage());
2021-08-16 20:48:26 +00:00
}
# Display
2021-11-01 10:39:02 +00:00
echo
2021-08-17 21:12:33 +00:00
'<html><head><title>' . __('pacKman') . '</title>' .
dcPage::jsPageTabs() .
2021-10-23 14:46:03 +00:00
dcPage::jsLoad(dcPage::getPF('pacKman/js/packman.js'));
2021-08-16 20:48:26 +00:00
# --BEHAVIOR-- packmanAdminHeader
2022-11-26 21:01:17 +00:00
dcCore::app()->callBehavior('packmanAdminHeader');
2021-08-16 20:48:26 +00:00
2021-11-01 10:39:02 +00:00
echo
2021-08-17 21:12:33 +00:00
'</head><body>' .
2021-08-16 20:48:26 +00:00
2021-08-23 13:07:29 +00:00
dcPage::breadcrumb([
2021-11-01 10:39:02 +00:00
__('Plugins') => '',
__('pacKman') => ''
]) .
2021-08-16 20:48:26 +00:00
dcPage::notices();
if (dcCore::app()->error->flag() || !$is_configured) {
2021-08-17 21:12:33 +00:00
echo
'<div class="warning">' . __('pacKman is not well configured.') . ' ' .
2021-08-17 21:12:33 +00:00
'<a href="plugins.php?module=pacKman&amp;conf=1&amp;redir=' .
urlencode('plugin.php?p=pacKman') . '">' . __('Configuration') . '</a>' .
'</div>';
2021-08-23 13:07:29 +00:00
} else {
2021-08-17 21:12:33 +00:00
$repo_path_modules = array_merge(
2022-11-12 20:42:05 +00:00
dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_pack_filename)),
dcPackman::getPackages(dirname($repo_path . '/' . $s->packman_secondpack_filename))
2021-08-17 21:12:33 +00:00
);
2022-11-12 20:42:05 +00:00
$plugins_path_modules = dcPackman::getPackages($plugins_path);
$themes_path_modules = dcPackman::getPackages($themes_path);
2021-08-17 21:12:33 +00:00
libPackman::modules(
$plugins->getModules(),
'plugins',
__('Installed plugins')
);
libPackman::modules(
$themes->getModules(),
'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')
);
2021-08-16 20:48:26 +00:00
}
# --BEHAVIOR-- packmanAdminTabs
2022-11-12 20:42:05 +00:00
dcCore::app()->callBehavior('packmanAdminTabs');
2021-08-16 20:48:26 +00:00
dcPage::helpBlock('pacKman');
2021-11-01 10:39:02 +00:00
echo
2021-11-06 13:56:29 +00:00
'</body></html>';