Added Files class to handle zip/unzip/rmrf and more

This commit is contained in:
fbsanchez 2021-06-11 10:48:58 +02:00
parent 75b889875e
commit 085e0ba300
6 changed files with 226 additions and 10 deletions

View File

@ -0,0 +1,183 @@
<?php
/**
* Class to manage some advanced operations over files.
*
* @category Class
* @package Pandora FMS
* @subpackage Tools
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
// Begin.
namespace PandoraFMS\Tools;
global $config;
/**
* Files class definition.
*/
class Files
{
/**
* Create Zip.
*
* @param string $name Name file.zip genarate.
* @param string $dir Directory to conver zip.
*
* @return void
*/
public static function zip(string $name, string $dir)
{
// Generate a collections zip for Metaconsole.
$zip = new \ZipArchive();
$zip->open(
$name,
(\ZipArchive::CREATE | \ZipArchive::OVERWRITE)
);
$rdi = new \RecursiveDirectoryIterator(
$dir
);
$files = new \RecursiveIteratorIterator(
$rdi,
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $file) {
// Skip directories
// (they would be added automatically).
if ($file->isDir() === false) {
// Get real and relative
// path for current file.
$filePath = $file->getRealPath();
$relativePath = substr(
$filePath,
(strlen($dir) + 1)
);
// Add current file to archive.
$zip->addFile($filePath, $relativePath);
// Keep file permissions.
$zip->setExternalAttributesName(
$relativePath,
\ZipArchive::OPSYS_UNIX,
(\fileperms($filePath) << 16)
);
}
}
$zip->close();
}
/**
* Uncompress a zip file keeping the file permissions.
*
* @param string $file File.
* @param string $target_path Target path.
*
* @return boolean
*/
public static function unzip(string $file, string $target_path):bool
{
$zip = new \ZipArchive;
if ($zip->open($file) === true) {
$idx = 0;
$s = $zip->statIndex($idx);
while ($s !== false && $s !== null) {
if ($zip->extractTo($target_path, $s['name']) === true) {
if ($zip->getExternalAttributesIndex($idx, $opsys, $attr) === true
&& $opsys === \ZipArchive::OPSYS_UNIX
) {
chmod(
$target_path.'/'.$s['name'],
(($attr >> 16) & 0777)
);
}
}
$s = $zip->statIndex(++$idx);
};
$zip->close();
return true;
}
return false;
}
/**
* Completely deletes a folder or only its content.
*
* @param string $folder Folder to delete.
* @param boolean $content_only Remove only folder content.
* @param array $exclusions Name of folders or files to avoid deletion
* [
* 'a',
* 'a/b.txt'
* ]
* Specify full paths when definining exclusions and don't forget to
* exclude containing folder also.
*
* @return void
*/
public static function rmrf(
string $folder,
bool $content_only=false,
array $exclusions=[]
):void {
if (is_dir($folder) !== true || is_readable($folder) !== true) {
return;
}
$pd = opendir($folder);
if ((bool) $pd === true) {
while (($pf = readdir($pd)) !== false) {
if ($pf !== '.' && $pf !== '..') {
$pf = $folder.$pf;
if (is_dir($pf) === true) {
// It's a directory.
self::rmrf($pf.'/');
} else {
// It's a file.
if (in_array($pf, $exclusions) === false) {
unlink($pf);
}
}
}
}
closedir($pd);
if ($content_only === false) {
if (in_array($pf, $exclusions) === false) {
rmdir($folder);
}
}
}
}
}

View File

@ -338,7 +338,7 @@ class ClassLoader
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return bool|null True if loaded, null otherwise
* @return true|null True if loaded, null otherwise
*/
public function loadClass($class)
{
@ -347,6 +347,8 @@ class ClassLoader
return true;
}
return null;
}
/**

View File

@ -1,3 +1,4 @@
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
@ -17,3 +18,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -93,6 +93,7 @@ return array(
'Egulias\\EmailValidator\\Warning\\QuotedString' => $vendorDir . '/egulias/email-validator/EmailValidator/Warning/QuotedString.php',
'Egulias\\EmailValidator\\Warning\\TLD' => $vendorDir . '/egulias/email-validator/EmailValidator/Warning/TLD.php',
'Egulias\\EmailValidator\\Warning\\Warning' => $vendorDir . '/egulias/email-validator/EmailValidator/Warning/Warning.php',
'Enterprise\\Models\\VisualConsole\\Items\\Service' => $baseDir . '/enterprise/include/rest-api/models/VisualConsole/Items/Service.php',
'FPDF_TPL' => $vendorDir . '/setasign/fpdi/fpdf_tpl.php',
'FPDI' => $vendorDir . '/setasign/fpdi/fpdi.php',
'FilterASCII85' => $vendorDir . '/setasign/fpdi/filters/FilterASCII85.php',
@ -315,6 +316,23 @@ return array(
'PandoraFMS\\Dashboard\\Cell' => $baseDir . '/include/lib/Dashboard/Cell.php',
'PandoraFMS\\Dashboard\\Manager' => $baseDir . '/include/lib/Dashboard/Manager.php',
'PandoraFMS\\Dashboard\\Widget' => $baseDir . '/include/lib/Dashboard/Widget.php',
'PandoraFMS\\Enterprise\\Agent' => $baseDir . '/enterprise/include/lib/Agent.php',
'PandoraFMS\\Enterprise\\Cluster' => $baseDir . '/enterprise/include/lib/Cluster.php',
'PandoraFMS\\Enterprise\\ClusterModule' => $baseDir . '/enterprise/include/lib/ClusterModule.php',
'PandoraFMS\\Enterprise\\ClusterViewer\\ClusterManager' => $baseDir . '/enterprise/include/lib/ClusterViewer/ClusterManager.php',
'PandoraFMS\\Enterprise\\ClusterViewer\\ClusterWizard' => $baseDir . '/enterprise/include/lib/ClusterViewer/ClusterWizard.php',
'PandoraFMS\\Enterprise\\Metaconsole\\MergeQueue' => $baseDir . '/enterprise/include/lib/Metaconsole/MergeQueue.php',
'PandoraFMS\\Enterprise\\Metaconsole\\Node' => $baseDir . '/enterprise/include/lib/Metaconsole/Node.php',
'PandoraFMS\\Enterprise\\Metaconsole\\PriorityPlan' => $baseDir . '/enterprise/include/lib/Metaconsole/PriorityPlan.php',
'PandoraFMS\\Enterprise\\Metaconsole\\SyncQueue' => $baseDir . '/enterprise/include/lib/Metaconsole/SyncQueue.php',
'PandoraFMS\\Enterprise\\Metaconsole\\SyncQueueItem' => $baseDir . '/enterprise/include/lib/Metaconsole/SyncQueueItem.php',
'PandoraFMS\\Enterprise\\Metaconsole\\Synchronizer' => $baseDir . '/enterprise/include/lib/Metaconsole/Synchronizer.php',
'PandoraFMS\\Enterprise\\Policy' => $baseDir . '/enterprise/include/lib/Policy.php',
'PandoraFMS\\Enterprise\\Policy\\Inventory' => $baseDir . '/enterprise/include/lib/Policy/Inventory.php',
'PandoraFMS\\Enterprise\\Policy\\Module' => $baseDir . '/enterprise/include/lib/Policy/Module.php',
'PandoraFMS\\Enterprise\\Policy\\Queue' => $baseDir . '/enterprise/include/lib/Policy/Queue.php',
'PandoraFMS\\Enterprise\\Service' => $baseDir . '/enterprise/include/lib/Service.php',
'PandoraFMS\\Enterprise\\ServiceElement' => $baseDir . '/enterprise/include/lib/ServiceElement.php',
'PandoraFMS\\Entity' => $baseDir . '/include/lib/Entity.php',
'PandoraFMS\\Event' => $baseDir . '/include/lib/Event.php',
'PandoraFMS\\Group' => $baseDir . '/include/lib/Group.php',
@ -322,6 +340,7 @@ return array(
'PandoraFMS\\ModuleStatus' => $baseDir . '/include/lib/ModuleStatus.php',
'PandoraFMS\\ModuleType' => $baseDir . '/include/lib/ModuleType.php',
'PandoraFMS\\PublicLogin' => $baseDir . '/include/lib/PublicLogin.php',
'PandoraFMS\\Tools\\Files' => $baseDir . '/include/lib/Tools/Files.php',
'PandoraFMS\\User' => $baseDir . '/include/lib/User.php',
'PandoraFMS\\View' => $baseDir . '/include/lib/View.php',
'PandoraFMS\\Websockets\\WSManager' => $baseDir . '/include/lib/Websockets/WSManager.php',

View File

@ -6,7 +6,6 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Tests\\' => array($baseDir . '/tests'),
'Psr\\Log\\' => array($vendorDir . '/psr/log/Psr/Log'),
'PandoraFMS\\Enterprise\\' => array($baseDir . '/enterprise/include/lib'),
'PandoraFMS\\' => array($baseDir . '/include/lib'),

View File

@ -12,10 +12,6 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa
);
public static $prefixLengthsPsr4 = array (
'T' =>
array (
'Tests\\' => 6,
),
'P' =>
array (
'Psr\\Log\\' => 8,
@ -39,10 +35,6 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa
);
public static $prefixDirsPsr4 = array (
'Tests\\' =>
array (
0 => __DIR__ . '/../..' . '/tests',
),
'Psr\\Log\\' =>
array (
0 => __DIR__ . '/..' . '/psr/log/Psr/Log',
@ -175,6 +167,7 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa
'Egulias\\EmailValidator\\Warning\\QuotedString' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Warning/QuotedString.php',
'Egulias\\EmailValidator\\Warning\\TLD' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Warning/TLD.php',
'Egulias\\EmailValidator\\Warning\\Warning' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Warning/Warning.php',
'Enterprise\\Models\\VisualConsole\\Items\\Service' => __DIR__ . '/../..' . '/enterprise/include/rest-api/models/VisualConsole/Items/Service.php',
'FPDF_TPL' => __DIR__ . '/..' . '/setasign/fpdi/fpdf_tpl.php',
'FPDI' => __DIR__ . '/..' . '/setasign/fpdi/fpdi.php',
'FilterASCII85' => __DIR__ . '/..' . '/setasign/fpdi/filters/FilterASCII85.php',
@ -397,6 +390,23 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa
'PandoraFMS\\Dashboard\\Cell' => __DIR__ . '/../..' . '/include/lib/Dashboard/Cell.php',
'PandoraFMS\\Dashboard\\Manager' => __DIR__ . '/../..' . '/include/lib/Dashboard/Manager.php',
'PandoraFMS\\Dashboard\\Widget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widget.php',
'PandoraFMS\\Enterprise\\Agent' => __DIR__ . '/../..' . '/enterprise/include/lib/Agent.php',
'PandoraFMS\\Enterprise\\Cluster' => __DIR__ . '/../..' . '/enterprise/include/lib/Cluster.php',
'PandoraFMS\\Enterprise\\ClusterModule' => __DIR__ . '/../..' . '/enterprise/include/lib/ClusterModule.php',
'PandoraFMS\\Enterprise\\ClusterViewer\\ClusterManager' => __DIR__ . '/../..' . '/enterprise/include/lib/ClusterViewer/ClusterManager.php',
'PandoraFMS\\Enterprise\\ClusterViewer\\ClusterWizard' => __DIR__ . '/../..' . '/enterprise/include/lib/ClusterViewer/ClusterWizard.php',
'PandoraFMS\\Enterprise\\Metaconsole\\MergeQueue' => __DIR__ . '/../..' . '/enterprise/include/lib/Metaconsole/MergeQueue.php',
'PandoraFMS\\Enterprise\\Metaconsole\\Node' => __DIR__ . '/../..' . '/enterprise/include/lib/Metaconsole/Node.php',
'PandoraFMS\\Enterprise\\Metaconsole\\PriorityPlan' => __DIR__ . '/../..' . '/enterprise/include/lib/Metaconsole/PriorityPlan.php',
'PandoraFMS\\Enterprise\\Metaconsole\\SyncQueue' => __DIR__ . '/../..' . '/enterprise/include/lib/Metaconsole/SyncQueue.php',
'PandoraFMS\\Enterprise\\Metaconsole\\SyncQueueItem' => __DIR__ . '/../..' . '/enterprise/include/lib/Metaconsole/SyncQueueItem.php',
'PandoraFMS\\Enterprise\\Metaconsole\\Synchronizer' => __DIR__ . '/../..' . '/enterprise/include/lib/Metaconsole/Synchronizer.php',
'PandoraFMS\\Enterprise\\Policy' => __DIR__ . '/../..' . '/enterprise/include/lib/Policy.php',
'PandoraFMS\\Enterprise\\Policy\\Inventory' => __DIR__ . '/../..' . '/enterprise/include/lib/Policy/Inventory.php',
'PandoraFMS\\Enterprise\\Policy\\Module' => __DIR__ . '/../..' . '/enterprise/include/lib/Policy/Module.php',
'PandoraFMS\\Enterprise\\Policy\\Queue' => __DIR__ . '/../..' . '/enterprise/include/lib/Policy/Queue.php',
'PandoraFMS\\Enterprise\\Service' => __DIR__ . '/../..' . '/enterprise/include/lib/Service.php',
'PandoraFMS\\Enterprise\\ServiceElement' => __DIR__ . '/../..' . '/enterprise/include/lib/ServiceElement.php',
'PandoraFMS\\Entity' => __DIR__ . '/../..' . '/include/lib/Entity.php',
'PandoraFMS\\Event' => __DIR__ . '/../..' . '/include/lib/Event.php',
'PandoraFMS\\Group' => __DIR__ . '/../..' . '/include/lib/Group.php',
@ -404,6 +414,7 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa
'PandoraFMS\\ModuleStatus' => __DIR__ . '/../..' . '/include/lib/ModuleStatus.php',
'PandoraFMS\\ModuleType' => __DIR__ . '/../..' . '/include/lib/ModuleType.php',
'PandoraFMS\\PublicLogin' => __DIR__ . '/../..' . '/include/lib/PublicLogin.php',
'PandoraFMS\\Tools\\Files' => __DIR__ . '/../..' . '/include/lib/Tools/Files.php',
'PandoraFMS\\User' => __DIR__ . '/../..' . '/include/lib/User.php',
'PandoraFMS\\View' => __DIR__ . '/../..' . '/include/lib/View.php',
'PandoraFMS\\Websockets\\WSManager' => __DIR__ . '/../..' . '/include/lib/Websockets/WSManager.php',