pacKman/src/Core.php

255 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
2022-12-17 21:12:10 +00:00
/* dotclear ns */
use dcModules;
use dcThemes;
/* clearbricks ns */
use files;
use fileUnzip;
use path;
/* packman ns */
/* php ns */
use Exception;
class Core
2021-08-16 20:48:26 +00:00
{
2021-11-08 21:19:07 +00:00
/** @var array Excluded files */
2021-08-23 13:07:29 +00:00
public static $exclude = [
'.',
'..',
'__MACOSX',
'.svn',
'.hg*',
'.git*',
'CVS',
'.DS_Store',
2022-11-26 21:22:42 +00:00
'Thumbs.db',
2022-12-03 13:40:00 +00:00
'_disabled',
2021-08-23 13:07:29 +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
{
2021-11-01 10:39:02 +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
$cache = self::getCache() . '/';
if (!is_dir($root) || !is_readable($root)) {
return $res;
}
2021-11-01 10:39:02 +00:00
$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;
}
$i = 0;
2021-11-01 10:39:02 +00:00
foreach ($zip_files as $zip_file) {
2021-08-23 13:07:29 +00:00
$zip = new fileUnzip($root . '/' . $zip_file);
$zip_root_dir = $zip->getRootDir();
if ($zip_root_dir != false) {
2021-11-01 10:39:02 +00:00
$define = $zip_root_dir . '/_define.php';
2021-08-23 13:07:29 +00:00
$has_define = $zip->hasFile($define);
} else {
2021-11-01 10:39:02 +00:00
$define = '_define.php';
2021-08-23 13:07:29 +00:00
$has_define = $zip->hasFile($define);
}
if (!$has_define) {
continue;
}
$zip->unzip($define, $cache . '/_define.php');
2022-12-02 00:17:58 +00:00
$modules = new dcModules();
2021-08-23 13:07:29 +00:00
$modules->requireDefine($cache, $zip_root_dir);
if ($modules->moduleExists($zip_root_dir)) {
$res[$i] = $modules->getModules($zip_root_dir);
} else {
2022-12-02 00:17:58 +00:00
$themes = new dcThemes();
2021-08-23 13:07:29 +00:00
$themes->requireDefine($cache, $zip_root_dir);
$res[$i] = $themes->getModules($zip_root_dir);
}
2022-02-01 20:39:38 +00:00
if (is_array($res[$i])) {
$res[$i] = array_merge($res[$i], [
'id' => $zip_root_dir,
2022-11-26 21:22:42 +00:00
'root' => $root . '/' . $zip_file,
2022-02-01 20:39:38 +00:00
]);
unlink($cache . '_define.php');
$i++;
}
2021-08-23 13:07:29 +00:00
}
return $res;
}
2021-11-08 21:19:07 +00:00
public static function pack(array $info, string $root, array $files, bool $overwrite = false, array $exclude = [], bool $nocomment = false, bool $fixnewline = false): bool
2021-08-23 13:07:29 +00:00
{
if (!($info = self::getInfo($info))
2021-10-23 14:46:03 +00:00
|| !($root = self::getRoot($root))
) {
2021-08-23 13:07:29 +00:00
return false;
}
$exclude = self::getExclude($exclude);
2021-11-01 10:39:02 +00:00
foreach ($files as $file) {
2021-08-23 13:07:29 +00:00
if (!($file = self::getFile($file, $info))
2021-10-23 14:46:03 +00:00
|| !($dest = self::getOverwrite($overwrite, $root, $file))
) {
2021-08-23 13:07:29 +00:00
continue;
}
@set_time_limit(300);
$fp = fopen($dest, 'wb');
if ($nocomment) {
2022-12-18 23:21:11 +00:00
Filezip::$remove_comment = true;
}
if ($fixnewline) {
2022-12-18 23:21:11 +00:00
Filezip::$fix_newline = true;
}
2022-12-18 23:21:11 +00:00
$zip = new Filezip($fp);
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(
path::real($info['root']),
$info['id'],
true
);
$zip->write();
$zip->close();
unset($zip);
}
return true;
}
2021-11-08 21:19:07 +00:00
private static function getRoot(string $root): string
2021-08-23 13:07:29 +00:00
{
2021-11-08 21:19:07 +00:00
$root = (string) path::real($root);
2021-08-23 13:07:29 +00:00
if (!is_dir($root) || !is_writable($root)) {
2022-11-26 21:20:11 +00:00
throw new Exception(__('Directory is not writable'));
2021-08-23 13:07:29 +00:00
}
return $root;
}
2021-11-08 21:19:07 +00:00
private static function getInfo(array $info): array
2021-08-23 13:07:29 +00:00
{
2021-11-01 10:39:02 +00:00
if (!isset($info['root'])
|| !isset($info['id'])
2021-10-23 14:46:03 +00:00
|| !is_dir($info['root'])
) {
2022-11-26 21:20:11 +00:00
throw new Exception(__('Failed to get module info'));
2021-08-23 13:07:29 +00:00
}
return $info;
}
2021-11-08 21:19:07 +00:00
private static function getExclude(array $exclude): array
2021-08-23 13:07:29 +00:00
{
$exclude = array_merge(self::$exclude, $exclude);
return self::quote_exclude($exclude);
}
2021-11-08 21:19:07 +00:00
private static function getFile(string $file, array $info): ?string
2021-08-23 13:07:29 +00:00
{
if (empty($file) || empty($info)) {
return null;
}
$file = str_replace(
[
'%type%',
'%id%',
'%version%',
'%author%',
2022-11-26 21:22:42 +00:00
'%time%',
2021-08-23 13:07:29 +00:00
],
[
$info['type'],
$info['id'],
$info['version'],
$info['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) {
2021-08-23 13:07:29 +00:00
$parts[$i] = files::tidyFileName($part);
}
2021-11-01 10:39:02 +00:00
2021-08-23 13:07:29 +00:00
return implode('/', $parts) . '.zip';
}
2021-11-08 21:19:07 +00:00
private static function getOverwrite(bool $overwrite, string $root, string$file): ?string
2021-08-23 13:07:29 +00:00
{
$path = $root . '/' . $file;
if (file_exists($path) && !$overwrite) {
// don't break loop
//throw new Exception('File already exists');
return null;
}
return $path;
}
2021-11-08 21:19:07 +00:00
private static function getCache(): string
2021-08-23 13:07:29 +00:00
{
$c = DC_TPL_CACHE . '/packman';
if (!file_exists($c)) {
@files::makeDir($c);
2021-08-23 13:07:29 +00:00
}
if (!is_writable($c)) {
2022-11-26 21:20:11 +00:00
throw new Exception(__('Failed to get temporary directory'));
2021-08-23 13:07:29 +00:00
}
return $c;
}
2021-11-06 13:56:29 +00:00
}