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
|
|
|
|
*/
|
2021-08-16 20:48:26 +00:00
|
|
|
if (!defined('DC_CONTEXT_ADMIN')) {
|
2021-08-23 13:07:29 +00:00
|
|
|
return null;
|
2021-08-16 20:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class dcPackman
|
|
|
|
{
|
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',
|
|
|
|
'Thumbs.db'
|
|
|
|
];
|
|
|
|
|
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-11-12 20:42:05 +00:00
|
|
|
$modules = new dcModules(dcCore::app());
|
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-11-12 20:42:05 +00:00
|
|
|
$themes = new dcThemes(dcCore::app());
|
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,
|
|
|
|
'root' => $root . '/' . $zip_file
|
|
|
|
]);
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
2021-09-02 20:13:21 +00:00
|
|
|
if ($nocomment) {
|
|
|
|
packmanFileZip::$remove_comment = true;
|
|
|
|
}
|
|
|
|
if ($fixnewline) {
|
|
|
|
packmanFileZip::$fix_newline = true;
|
|
|
|
}
|
|
|
|
$zip = new packmanFileZip($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)) {
|
|
|
|
throw new Exception('Directory is not writable');
|
|
|
|
}
|
|
|
|
|
|
|
|
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'])
|
|
|
|
) {
|
2021-08-23 13:07:29 +00:00
|
|
|
throw new Exception('Failed to get module info');
|
|
|
|
}
|
|
|
|
|
|
|
|
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%',
|
|
|
|
'%time%'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
$info['type'],
|
|
|
|
$info['id'],
|
|
|
|
$info['version'],
|
|
|
|
$info['author'],
|
|
|
|
time()
|
|
|
|
],
|
|
|
|
$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)) {
|
|
|
|
@mkdir($c);
|
|
|
|
}
|
|
|
|
if (!is_writable($c)) {
|
|
|
|
throw new Exception('Failed to get temporary directory');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}
|
2021-11-06 13:56:29 +00:00
|
|
|
}
|