pacKman/src/Core.php

223 lines
6.2 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
*/
2022-12-17 21:12:10 +00:00
declare(strict_types=1);
2022-12-18 23:21:11 +00:00
namespace Dotclear\Plugin\pacKman;
2021-08-16 20:48:26 +00:00
use dcCore;
use dcModuleDefine;
2022-12-17 21:12:10 +00:00
use dcModules;
use Dotclear\Helper\File\Files;
use Dotclear\Helper\File\Path;
2023-03-18 15:58:55 +00:00
use Dotclear\Helper\File\Zip\Unzip;
2023-04-08 12:18:12 +00:00
use Exception;
2022-12-17 21:12:10 +00:00
class Core
2021-08-16 20:48:26 +00:00
{
2021-11-08 21:19:07 +00:00
public static function quote_exclude(array $exclude): array
2021-08-23 13:07:29 +00:00
{
foreach ($exclude as $k => $v) {
2021-08-23 13:07:29 +00:00
$exclude[$k] = '#(^|/)(' . str_replace(
['.', '*'],
['\.', '.*?'],
trim($v)
) . ')(/|$)#';
}
return $exclude;
}
2022-11-12 20:42:05 +00:00
public static function getPackages(string $root): array
2021-08-23 13:07:29 +00:00
{
2021-10-23 14:46:03 +00:00
$res = [];
2021-08-23 13:07:29 +00:00
if (!is_dir($root) || !is_readable($root)) {
return $res;
}
$files = Files::scanDir($root);
2021-10-23 14:46:03 +00:00
$zip_files = [];
2021-11-01 10:39:02 +00:00
foreach ($files as $file) {
2021-08-23 13:07:29 +00:00
if (!preg_match('#(^|/)(.*?)\.zip(/|$)#', $file)) {
continue;
}
$zip_files[] = $file;
}
if (empty($zip_files)) {
return $res;
}
$sandboxes = [
'theme' => clone dcCore::app()->themes,
'plugin' => clone dcCore::app()->plugins,
];
2021-08-23 13:07:29 +00:00
$i = 0;
2021-11-01 10:39:02 +00:00
foreach ($zip_files as $zip_file) {
$zip_file = $root . DIRECTORY_SEPARATOR . $zip_file;
2023-03-18 15:58:55 +00:00
$zip = new Unzip($zip_file);
$zip->getList(false, '#(^|/)(__MACOSX|\.svn|\.hg.*|\.git.*|\.DS_Store|\.directory|Thumbs\.db)(/|$)#');
2021-08-23 13:07:29 +00:00
$zip_root_dir = $zip->getRootDir();
if ($zip_root_dir != false) {
2023-03-18 16:20:48 +00:00
$target = dirname($zip_file);
$path = $target . DIRECTORY_SEPARATOR . $zip_root_dir;
$define = $zip_root_dir . '/' . dcModules::MODULE_FILE_DEFINE;
$init = $zip_root_dir . '/' . dcModules::MODULE_FILE_INIT;
2021-08-23 13:07:29 +00:00
} else {
2023-03-18 16:20:48 +00:00
$target = dirname($zip_file) . DIRECTORY_SEPARATOR . preg_replace('/\.([^.]+)$/', '', basename($zip_file));
$path = $target;
$define = dcModules::MODULE_FILE_DEFINE;
$init = dcModules::MODULE_FILE_INIT;
2021-08-23 13:07:29 +00:00
}
if ($zip->isEmpty()) {
$zip->close();
2021-08-23 13:07:29 +00:00
continue;
}
2023-03-18 15:58:55 +00:00
if (!$zip->hasFile($define)) {
$zip->close();
2021-08-23 13:07:29 +00:00
continue;
2021-08-23 13:07:29 +00:00
}
foreach ($sandboxes as $type => $sandbox) {
try {
Files::makeDir($path, true);
// can't load twice _init.php file !
$unlink = false;
2023-03-18 15:58:55 +00:00
if ($zip->hasFile($init)) {
$unlink = true;
2023-03-18 15:58:55 +00:00
$zip->unzip($init, $path);
}
2023-03-18 15:58:55 +00:00
$zip->unzip($define, $path);
$sandbox->resetModulesList();
2023-03-18 15:58:55 +00:00
$sandbox->requireDefine($path, basename($path));
if ($unlink) {
2023-03-18 15:58:55 +00:00
unlink($target . DIRECTORY_SEPARATOR . $init);
}
2023-03-18 15:58:55 +00:00
unlink($target . DIRECTORY_SEPARATOR . $define);
2023-03-18 15:58:55 +00:00
if (!$sandbox->getErrors()) {
$module = $sandbox->getDefine(basename($path));
if ($module->isDefined() && $module->get('type') == $type) {
$res[$i] = $module;
$res[$i]->set('root', $zip_file);
2023-03-18 15:58:55 +00:00
$i++;
}
}
} catch (Exception $e) {
}
Files::deltree($path);
2022-02-01 20:39:38 +00:00
}
2023-03-18 15:58:55 +00:00
$zip->close();
2021-08-23 13:07:29 +00:00
}
return $res;
}
public static function pack(dcModuleDefine $define, string $root, array $files, bool $overwrite = false, array $exclude = [], bool $nocomment = false, bool $fixnewline = false): bool
2021-08-23 13:07:29 +00:00
{
// check define
if (!$define->isDefined()
|| empty($define->get('root'))
|| !is_dir($define->get('root'))
2021-10-23 14:46:03 +00:00
) {
throw new Exception(__('Failed to get module info'));
}
// check root
$root = (string) Path::real($root);
if (!is_dir($root) || !is_writable($root)) {
throw new Exception(__('Directory is not writable'));
2021-08-23 13:07:29 +00:00
}
//set excluded
$exclude = self::quote_exclude(array_merge(My::EXCLUDED_FILES, $exclude));
2021-08-23 13:07:29 +00:00
2021-11-01 10:39:02 +00:00
foreach ($files as $file) {
if (empty($file)) {
continue;
}
// check path
$path = $root . DIRECTORY_SEPARATOR . self::getFile($file, $define);
if (file_exists($path) && !$overwrite) {
// don't break loop
2021-08-23 13:07:29 +00:00
continue;
}
@set_time_limit(300);
if ($nocomment) {
2023-03-18 15:58:55 +00:00
Zip::$remove_comment = true;
}
if ($fixnewline) {
2023-03-18 15:58:55 +00:00
Zip::$fix_newline = true;
}
$zip = new Zip($path);
2021-08-23 13:07:29 +00:00
2021-11-01 10:39:02 +00:00
foreach ($exclude as $e) {
2021-08-23 13:07:29 +00:00
$zip->addExclusion($e);
}
$zip->addDirectory(
(string) Path::real($define->get('root'), false),
$define->getId(),
2021-08-23 13:07:29 +00:00
true
);
$zip->close();
unset($zip);
}
return true;
}
private static function getFile(string $file, dcModuleDefine $define): string
2021-08-23 13:07:29 +00:00
{
$file = str_replace(
[
2023-03-19 09:47:24 +00:00
'\\',
2021-08-23 13:07:29 +00:00
'%type%',
'%id%',
'%version%',
'%author%',
2022-11-26 21:22:42 +00:00
'%time%',
2021-08-23 13:07:29 +00:00
],
[
2023-03-19 09:47:24 +00:00
'/',
$define->get('type'),
$define->getId(),
$define->get('version'),
$define->get('author'),
2022-11-26 21:22:42 +00:00
time(),
2021-08-23 13:07:29 +00:00
],
$file
);
$parts = explode('/', $file);
2021-11-01 10:39:02 +00:00
foreach ($parts as $i => $part) {
$parts[$i] = Files::tidyFileName($part);
2021-08-23 13:07:29 +00:00
}
2021-11-01 10:39:02 +00:00
2023-03-19 09:47:24 +00:00
return implode(DIRECTORY_SEPARATOR, $parts) . '.zip';
2021-08-23 13:07:29 +00:00
}
2021-11-06 13:56:29 +00:00
}