tinyPacker/_admin.php

109 lines
2.6 KiB
PHP
Raw Normal View History

2021-08-19 19:42:44 +00:00
<?php
2021-09-02 21:12:57 +00:00
/**
* @brief tinyPacker, a plugin for Dotclear 2
2022-11-20 21:06:36 +00:00
*
2021-09-02 21:12:57 +00:00
* @package Dotclear
* @subpackage Plugin
2022-11-20 21:06:36 +00:00
*
2021-09-02 21:12:57 +00:00
* @author Jean-Christian Denis
2022-11-20 21:06:36 +00:00
*
2021-09-02 21:12:57 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-19 19:42:44 +00:00
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
2022-11-13 22:43:30 +00:00
dcCore::app()->addBehavior(
2021-08-19 19:42:44 +00:00
'adminModulesListGetActions',
2022-12-10 20:03:00 +00:00
function ($list, $id, $_) {
2022-12-10 20:28:19 +00:00
return in_array($list->getList(), [
2022-12-10 20:03:00 +00:00
'plugin-activate',
'theme-activate',
2022-12-10 20:28:19 +00:00
]) ? sprintf(
'<input type="submit" name="%s[%s]" value="Pack" />',
basename(__DIR__),
html::escapeHTML($id)
) : null;
2021-08-19 19:42:44 +00:00
}
2022-12-10 20:03:00 +00:00
);
2021-08-19 19:42:44 +00:00
2022-12-10 20:03:00 +00:00
dcCore::app()->addBehavior(
'adminModulesListDoActions',
function ($list, $modules, $type) {
2021-08-19 19:42:44 +00:00
# Pack action
2022-12-10 20:28:19 +00:00
if (empty($_POST[basename(__DIR__)])
|| !is_array($_POST[basename(__DIR__)])) {
2021-08-19 19:42:44 +00:00
return null;
}
# Repository directory
2022-12-10 20:03:00 +00:00
$dir = path::real(
2022-12-10 20:28:19 +00:00
dcCore::app()->blog->public_path . '/packages',
2022-12-10 20:03:00 +00:00
false
);
if (!is_dir($dir)) {
files::makeDir($dir, true);
}
if (!is_writable($dir)) {
throw new Exception(__('Destination directory is not writable.'));
2021-08-19 19:42:44 +00:00
}
# Module to pack
2022-12-10 20:28:19 +00:00
$modules = array_keys($_POST[basename(__DIR__)]);
$id = $modules[0];
2021-08-19 19:42:44 +00:00
if (!$list->modules->moduleExists($id)) {
throw new Exception(__('No such module.'));
}
$module = $list->modules->getModules($id);
# Excluded files and dirs
2022-11-20 21:06:36 +00:00
$exclude = [
2021-08-19 19:42:44 +00:00
'\.',
'\.\.',
'__MACOSX',
'\.svn',
'\.hg.*?',
'\.git.*?',
'CVS',
'\.directory',
'\.DS_Store',
2022-11-20 21:06:36 +00:00
'Thumbs\.db',
2022-12-10 20:03:00 +00:00
'_disabled',
2022-11-20 21:06:36 +00:00
];
2021-08-19 19:42:44 +00:00
# Packages names
2022-11-20 21:06:36 +00:00
$files = [
2021-08-19 19:42:44 +00:00
$type . '-' . $id . '.zip',
2022-11-20 21:06:36 +00:00
$type . '-' . $id . '-' . $module['version'] . '.zip',
];
2021-08-19 19:42:44 +00:00
# Create zip
2022-11-20 21:06:36 +00:00
foreach ($files as $f) {
2021-08-19 19:42:44 +00:00
@set_time_limit(300);
2022-12-10 20:03:00 +00:00
$fp = fopen($dir . '/' . $f, 'wb');
2021-08-19 19:42:44 +00:00
$zip = new fileZip($fp);
2022-11-20 21:06:36 +00:00
foreach ($exclude as $e) {
2021-08-19 19:42:44 +00:00
$zip->addExclusion(sprintf(
2022-11-20 21:06:36 +00:00
'#(^|/)(%s)(/|$)#',
2021-08-19 19:42:44 +00:00
$e
));
}
$zip->addDirectory($module['root'], $id, true);
$zip->write();
$zip->close();
unset($zip);
}
dcPage::addSuccessNotice(
__('Task successfully executed.')
);
http::redirect($list->getURL());
}
2022-12-10 20:03:00 +00:00
);