use new Zip Helper (and various fix)

This commit is contained in:
Jean-Christian Paul Denis 2023-03-21 21:05:57 +01:00
parent e9886ee444
commit 4046c9e2e7
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
2 changed files with 41 additions and 41 deletions

View File

@ -1,11 +1,10 @@
<?php <?php
// Language: Français /**
// Module: tinyPacker - * @package Dotclear
// Date: 2023-01-30 18:50:34 *
// Author: Pierre Van Glabeke * @copyright Olivier Meunier & Association Dotclear
// Translated with dcTranslater - 2022.12.26 * @copyright GPL-2.0-only
*/
# #
# DOT NOT MODIFY THIS FILE ! # DOT NOT MODIFY THIS FILE !
# #

View File

@ -15,13 +15,14 @@ declare(strict_types=1);
namespace Dotclear\Plugin\tinyPacker; namespace Dotclear\Plugin\tinyPacker;
/* dotclear */ /* dotclear */
use adminModulesList;
use dcCore; use dcCore;
use dcNsProcess; use dcNsProcess;
use dcPage; use dcPage;
use Dotclear\Helper\File\Zip\Zip;
/* clearbricks */ /* clearbricks */
use files; use files;
use fileZip;
use html; use html;
use http; use http;
use path; use path;
@ -36,61 +37,11 @@ use Exception;
*/ */
class Backend extends dcNsProcess class Backend extends dcNsProcess
{ {
public static function init(): bool /** @var string Public packages folder */
{ public const TINYPACKER_DIR = 'packages';
static::$init = defined('DC_CONTEXT_ADMIN') && dcCOre::app()->auth->isSuperAdmin();
return static::$init; /** @var array Excluded files and dirs */
} public const TINYPACKER_EXCLUDE = [
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminModulesListGetActions' => function ($list, $id, $_) {
return in_array($list->getList(), [
'plugin-activate',
'theme-activate',
]) ? sprintf(
'<input type="submit" name="%s[%s]" value="Pack" />',
self::id(),
html::escapeHTML($id)
) : null;
},
'adminModulesListDoActions' => function ($list, $modules, $type) {
# Pack action
if (empty($_POST[self::id()])
|| !is_array($_POST[self::id()])) {
return null;
}
# 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[self::id()]);
$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', '__MACOSX',
@ -104,6 +55,58 @@ class Backend extends dcNsProcess
'_disabled', '_disabled',
]; ];
public static function init(): bool
{
static::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->auth->isSuperAdmin();
return static::$init;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'adminModulesListGetActions' => function (adminModulesList $list, string $id, array $_): string {
return in_array($list->getList(), [
'plugin-activate',
'theme-activate',
]) ? sprintf(
'<input type="submit" name="%s[%s]" value="Pack" />',
self::id(),
html::escapeHTML($id)
) : '';
},
'adminModulesListDoActions' => function (adminModulesList $list, array $modules, string $type): void {
# Pack action
if (empty($_POST[self::id()])
|| !is_array($_POST[self::id()])) {
return;
}
# Repository directory
$dir = (string) path::real(
dcCore::app()->blog->public_path . DIRECTORY_SEPARATOR . self::TINYPACKER_DIR,
false
);
if (!empty($dir) && !is_dir($dir)) {
files::makeDir($dir, true);
}
if (empty($dir) || !is_writable($dir)) {
throw new Exception(__('Destination directory is not writable.'));
}
# Module to pack
$modules = array_keys($_POST[self::id()]);
$id = $modules[0];
if (!$list->modules->moduleExists($id)) {
throw new Exception(__('No such module.'));
}
$module = $list->modules->getModules($id);
# Packages names # Packages names
$files = [ $files = [
$type . '-' . $id . '.zip', $type . '-' . $id . '.zip',
@ -111,21 +114,19 @@ class Backend extends dcNsProcess
]; ];
# Create zip # Create zip
foreach ($files as $f) { foreach ($files as $file) {
@set_time_limit(300); @set_time_limit(300);
$fp = fopen($dir . '/' . $f, 'wb');
$zip = new fileZip($fp); $zip = new Zip($dir . '/' . $file);
foreach ($exclude as $e) { foreach (self::TINYPACKER_EXCLUDE as $e) {
$zip->addExclusion(sprintf( $zip->addExclusion(sprintf(
'#(^|/)(%s)(/|$)#', '#(^|/)(%s)(/|$)#',
$e $e
)); ));
} }
$zip->addDirectory($module['root'], $id, true); $zip->addDirectory((string) path::real($module['root']), $id, true);
$zip->write();
$zip->close(); $zip->close();
unset($zip); unset($zip);
} }
@ -140,7 +141,7 @@ class Backend extends dcNsProcess
return true; return true;
} }
private function id() private static function id(): string
{ {
return basename(dirname(__DIR__)); return basename(dirname(__DIR__));
} }