tinyPacker/_admin.php

152 lines
3.9 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
*/
2022-12-23 10:27:35 +00:00
declare(strict_types=1);
2021-08-19 19:42:44 +00:00
2022-12-23 10:27:35 +00:00
namespace Dotclear\Plugin\tinyPacker;
2021-08-19 19:42:44 +00:00
2022-12-23 10:27:35 +00:00
/* dotclear */
use dcCore;
use dcPage;
2022-12-10 20:03:00 +00:00
2022-12-23 10:27:35 +00:00
/* clearbricks */
use files;
use fileZip;
use html;
use http;
use path;
/* php */
use Exception;
/**
* tinyPacker admin class
*
* Add action and button to modules lists.
*/
class Admin
{
private static $init = false;
public static function init(): bool
{
if (defined('DC_CONTEXT_ADMIN')) {
dcPage::checkSuper();
self::$init = true;
2021-08-19 19:42:44 +00:00
}
2022-12-23 10:27:35 +00:00
return self::$init;
}
2022-12-10 20:28:19 +00:00
2022-12-23 10:27:35 +00:00
public static function process(): bool
{
if (!self::$init) {
return false;
2021-08-19 19:42:44 +00:00
}
2022-12-23 10:27:35 +00:00
2023-01-30 20:19:21 +00:00
dcCore::app()->addBehaviors([
'adminModulesListGetActions' => function ($list, $id, $_) {
2022-12-23 10:27:35 +00:00
return in_array($list->getList(), [
'plugin-activate',
'theme-activate',
]) ? sprintf(
'<input type="submit" name="%s[%s]" value="Pack" />',
basename(__DIR__),
html::escapeHTML($id)
) : null;
2023-01-30 20:19:21 +00:00
},
'adminModulesListDoActions' => function ($list, $modules, $type) {
2022-12-23 10:27:35 +00:00
# Pack action
if (empty($_POST[basename(__DIR__)])
|| !is_array($_POST[basename(__DIR__)])) {
return null;
}
2021-08-19 19:42:44 +00:00
2022-12-23 10:27:35 +00:00
# Repository directory
$dir = path::real(
dcCore::app()->blog->public_path . '/packages',
false
);
if (!is_dir($dir)) {
files::makeDir($dir, true);
}
if (!is_writable($dir)) {
throw new Exception(__('Destination directory is not writable.'));
}
# Module to pack
$modules = array_keys($_POST[basename(__DIR__)]);
$id = $modules[0];
if (!$list->modules->moduleExists($id)) {
throw new Exception(__('No such module.'));
}
$module = $list->modules->getModules($id);
# Excluded files and dirs
$exclude = [
'\.',
'\.\.',
'__MACOSX',
'\.svn',
'\.hg.*?',
'\.git.*?',
'CVS',
'\.directory',
'\.DS_Store',
'Thumbs\.db',
'_disabled',
];
# Packages names
$files = [
$type . '-' . $id . '.zip',
$type . '-' . $id . '-' . $module['version'] . '.zip',
];
# Create zip
foreach ($files as $f) {
@set_time_limit(300);
$fp = fopen($dir . '/' . $f, 'wb');
$zip = new fileZip($fp);
foreach ($exclude as $e) {
$zip->addExclusion(sprintf(
'#(^|/)(%s)(/|$)#',
$e
));
}
$zip->addDirectory($module['root'], $id, true);
$zip->write();
$zip->close();
unset($zip);
}
dcPage::addSuccessNotice(
__('Task successfully executed.')
);
http::redirect($list->getURL());
2023-01-30 20:19:21 +00:00
},
]);
2022-12-23 10:27:35 +00:00
return true;
2021-08-19 19:42:44 +00:00
}
2022-12-23 10:27:35 +00:00
}
/* process */
if (Admin::init()) {
Admin::process();
}