pacKman/index.php

332 lines
9.0 KiB
PHP
Raw Normal View History

2021-08-16 20:48:26 +00:00
<?php
# -- BEGIN LICENSE BLOCK ----------------------------------
#
# This file is part of pacKman, a plugin for Dotclear 2.
#
2021-08-23 13:08:59 +00:00
# Copyright (c) 2009-2021 Jean-Christian Denis and contributors
2021-08-16 20:48:26 +00:00
#
# Licensed under the GPL version 2.0 license.
# A copy of this license is available in LICENSE file or at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK ------------------------------------
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
$p_url = 'plugin.php?p=pacKman';
$action = isset($_POST['action']) ? $_POST['action'] : '';
2021-08-23 13:07:29 +00:00
$type = isset($_POST['type']) && in_array($_POST['type'], ['plugins', 'themes', 'repository']) ? $_POST['type'] : '';
2021-08-16 20:48:26 +00:00
# Settings
$core->blog->settings->addNamespace('pacKman');
$s = $core->blog->settings->pacKman;
# Modules
if (!isset($core->themes)) {
2021-08-17 21:12:33 +00:00
$core->themes = new dcThemes($core);
$core->themes->loadModules($core->blog->themes_path, null);
2021-08-16 20:48:26 +00:00
}
$themes = $core->themes;
$plugins = $core->plugins;
# Paths
$ppexp = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
$pppop = array_pop($ppexp);
$plugins_path = path::real($pppop);
$themes_path = $core->blog->themes_path;
$repo_path = $s->packman_pack_repository;
# 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
);
$is_editable =
2021-08-17 21:12:33 +00:00
!empty($type)
&& !empty($_POST['modules'])
&& 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
$core,
$s->packman_pack_repository,
$s->packman_pack_filename,
$s->packman_secondpack_filename
2021-08-16 20:48:26 +00:00
);
# Actions
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') {
$modules = dcPackman::getPackages($core, $plugins_path);
2021-08-23 13:07:29 +00:00
} elseif ($type == 'themes') {
2021-08-17 21:12:33 +00:00
$modules = dcPackman::getPackages($core, $themes_path);
2021-08-23 13:07:29 +00:00
} else {
2021-08-17 21:12:33 +00:00
$modules = array_merge(
dcPackman::getPackages($core, dirname($repo_path . '/' . $s->packman_pack_filename)),
dcPackman::getPackages($core, dirname($repo_path . '/' . $s->packman_secondpack_filename))
);
}
foreach($modules as $f) {
if (preg_match('/' . preg_quote($_REQUEST['package']) . '$/', $f['root'])
2021-08-23 13:07:29 +00:00
&& is_file($f['root']) && is_readable($f['root'])) {
2021-08-17 21:12:33 +00:00
# --BEHAVIOR-- packmanBeforeDownloadPackage
$core->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
$core->callBehavior('packmanAfterDownloadPackage', $f, $type);
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');
}
$module = ${$type}->getModules($id);
$module['id'] = $id;
$module['type'] = $type == 'themes' ? 'theme' : 'plugin';
$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-08-17 21:12:33 +00:00
$nocomment = $s->packman_pack_nocomment;
$overwrite = $s->packman_pack_overwrite;
$exclude = explode(',', $s->packman_pack_excludefiles);
# --BEHAVIOR-- packmanBeforeCreatePackage
$core->callBehavior('packmanBeforeCreatePackage', $core, $module);
dcPackman::pack($module, $root, $files, $overwrite, $exclude, $nocomment);
# --BEHAVIOR-- packmanAfterCreatePackage
$core->callBehavior('packmanAfterCreatePackage', $core, $module);
}
dcPage::addSuccessNotice(
__('Package successfully created.')
);
http::redirect(empty($_POST['redir']) ?
$p_url . '#packman-' . $type : $_POST['redir']
);
# 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) {
if (!file_exists($root) || !files::isDeletable($root)) {
throw new Exception('Undeletable file: ' . $root);
}
unlink($root);
}
dcPage::addSuccessNotice(
__('Package successfully deleted.')
);
http::redirect(
$p_url . '#packman-repository-' . $type
);
# 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
$core->callBehavior('packmanBeforeInstallPackage', $type, $id, $root);
if ($type == 'plugins') {
$plugins->installPackage($root, $plugins);
}
if ($type == 'themes') {
$themes->installPackage($root, $themes);
}
# --BEHAVIOR-- packmanAfterInstallPackage
$core->callBehavior('packmanAfterInstallPackage', $type, $id, $root);
}
dcPage::addSuccessNotice(
__('Package successfully installed.')
);
http::redirect(
$p_url . '#packman-repository-' . $type
);
# Copy
2021-08-23 13:07:29 +00:00
} elseif (strpos($action, 'copy_to_') !== false) {
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;
2021-08-23 13:07:29 +00:00
} elseif ($action == 'copy_to_repository') {
2021-08-17 21:12:33 +00:00
$dest = $repo_path;
}
foreach ($_POST['modules'] as $root => $id) {
file_put_contents(
$dest . '/' . basename($root),
file_get_contents($root)
);
}
dcPage::addSuccessNotice(
__('Package successfully copied.')
);
http::redirect(
$p_url . '#packman-repository-' . $type
);
# Move
2021-08-23 13:07:29 +00:00
} elseif (strpos($action, 'move_to_') !== false) {
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;
2021-08-23 13:07:29 +00:00
} elseif ($action == 'move_to_repository') {
2021-08-17 21:12:33 +00:00
$dest = $repo_path;
}
foreach ($_POST['modules'] as $root => $id) {
file_put_contents(
$dest . '/' . basename($root),
file_get_contents($root)
);
unlink($root);
}
dcPage::addSuccessNotice(
__('Package successfully moved.')
);
http::redirect(
$p_url . '#packman-repository-' . $type
);
}
2021-08-23 13:07:29 +00:00
} catch(Exception $e) {
2021-08-17 21:12:33 +00:00
$core->error->add($e->getMessage());
2021-08-16 20:48:26 +00:00
}
# Display
echo
2021-08-17 21:12:33 +00:00
'<html><head><title>' . __('pacKman') . '</title>' .
dcPage::jsPageTabs() .
2021-08-16 20:48:26 +00:00
dcPage::jsLoad('index.php?pf=pacKman/js/packman.js');
# --BEHAVIOR-- packmanAdminHeader
$core->callBehavior('packmanAdminHeader', $core);
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-08-17 21:12:33 +00:00
__('Plugins') => '',
__('pacKman') => ''
2021-08-23 13:07:29 +00:00
]).
2021-08-16 20:48:26 +00:00
dcPage::notices();
if ($core->error->flag()) {
2021-08-17 21:12:33 +00:00
echo
'<p class="warning">' . __('pacKman is not well configured.') . ' ' .
'<a href="plugins.php?module=pacKman&amp;conf=1&amp;redir=' .
urlencode('plugin.php?p=pacKman') . '">' . __('Configuration') . '</a>' .
'</p>';
2021-08-16 20:48:26 +00:00
2021-08-23 13:07:29 +00:00
} else {
2021-08-17 21:12:33 +00:00
$repo_path_modules = array_merge(
dcPackman::getPackages(
$core,
dirname($repo_path . '/' . $s->packman_pack_filename)
),
dcPackman::getPackages(
$core,
dirname($repo_path . '/' . $s->packman_secondpack_filename)
)
);
$plugins_path_modules = dcPackman::getPackages(
$core,
$plugins_path
);
$themes_path_modules = dcPackman::getPackages(
$core,
$themes_path
);
libPackman::modules(
$core,
$plugins->getModules(),
'plugins',
__('Installed plugins')
);
libPackman::modules(
$core,
$themes->getModules(),
'themes',
__('Installed themes')
);
libPackman::repository(
$core,
$plugins_path_modules,
'plugins',
__('Plugins root')
);
libPackman::repository(
$core,
$themes_path_modules,
'themes',
__('Themes root')
);
libPackman::repository(
$core,
$repo_path_modules,
'repository',
__('Packages repository')
);
2021-08-16 20:48:26 +00:00
}
# --BEHAVIOR-- packmanAdminTabs
$core->callBehavior('packmanAdminTabs', $core);
dcPage::helpBlock('pacKman');
echo
2021-08-17 21:12:33 +00:00
'</body></html>';