2024-09-11 17:38:46 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel\helpers;
|
|
|
|
|
|
|
|
use FilesystemIterator;
|
2024-10-08 17:06:14 +03:00
|
|
|
use ZipArchive;
|
2024-09-11 17:38:46 +03:00
|
|
|
|
|
|
|
class Files
|
|
|
|
{
|
2024-12-19 12:53:55 +03:00
|
|
|
public function copy_folder($d1, $d2, int $permissions = 0775, bool $recursive = true): void
|
2024-09-11 17:38:46 +03:00
|
|
|
{
|
|
|
|
if (is_dir($d1)) {
|
|
|
|
if (!file_exists($d2)){
|
2024-12-19 12:53:55 +03:00
|
|
|
$old_mask = umask(0);
|
|
|
|
$_d2 = mkdir($d2, permissions: $permissions, recursive: $recursive);
|
|
|
|
umask($old_mask);
|
2024-09-11 17:38:46 +03:00
|
|
|
if (!$_d2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$d = dir($d1);
|
|
|
|
while (false !== ($entry = $d->read())) {
|
2024-12-19 12:53:55 +03:00
|
|
|
if ($entry != '.' && $entry != '..') {
|
2024-09-11 17:38:46 +03:00
|
|
|
$this->copy_folder("$d1/$entry", "$d2/$entry");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$d->close();
|
|
|
|
} else {
|
|
|
|
copy($d1, $d2);
|
2024-12-19 12:53:55 +03:00
|
|
|
chmod($d2, permissions: $permissions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function copyKernelFolder($d1, $d2, int $permissions = 0775, bool $recursive = true): void
|
|
|
|
{
|
|
|
|
if (is_dir($d1)) {
|
|
|
|
if (!file_exists($d2)){
|
|
|
|
$old_mask = umask(0);
|
|
|
|
$_d2 = mkdir($d2, permissions: $permissions, recursive: $recursive);
|
|
|
|
umask($old_mask);
|
|
|
|
if (!$_d2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$d = dir($d1);
|
|
|
|
while (false !== ($entry = $d->read())) {
|
|
|
|
if ($entry != '.' && $entry != '..' && $entry != 'app_modules') {
|
|
|
|
$this->copyKernelFolder("$d1/$entry", "$d2/$entry");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$d->close();
|
|
|
|
} else {
|
|
|
|
copy($d1, $d2);
|
|
|
|
chmod($d2, permissions: $permissions);
|
2024-09-11 17:38:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function recursiveRemoveDir($dir): void
|
|
|
|
{
|
|
|
|
$includes = new FilesystemIterator($dir);
|
|
|
|
foreach ($includes as $include) {
|
|
|
|
if(is_dir($include) && !is_link($include)) {
|
|
|
|
$this->recursiveRemoveDir($include);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unlink($include);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir($dir);
|
|
|
|
}
|
2024-10-08 17:06:14 +03:00
|
|
|
|
2024-10-23 16:16:47 +03:00
|
|
|
public function recursiveRemoveKernelDir(): void
|
|
|
|
{
|
|
|
|
$includes = new FilesystemIterator(KERNEL_DIR);
|
|
|
|
foreach ($includes as $include) {
|
|
|
|
if ($include->getFilename() === 'app_modules') continue;
|
|
|
|
|
|
|
|
if(is_dir($include) && !is_link($include)) {
|
|
|
|
$this->recursiveRemoveDir($include);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unlink($include);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 15:12:30 +03:00
|
|
|
public function pack(string $source, string $destination/*, bool $include_source = true*/): void
|
2024-10-08 17:06:14 +03:00
|
|
|
{
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE);
|
|
|
|
|
|
|
|
if (is_file($source)) {
|
|
|
|
$zip->addFile(basename($source));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($source)) {
|
|
|
|
$this->recursiveAddFile($zip, $source);
|
|
|
|
}
|
|
|
|
|
|
|
|
$zip->close();
|
|
|
|
}
|
|
|
|
|
2024-11-18 16:04:57 +03:00
|
|
|
public static function uploadByUrl(string $url, string $uploadDir = RESOURCES_DIR . "/upload"): void
|
|
|
|
{
|
|
|
|
$file_name = basename($url);
|
|
|
|
file_put_contents($uploadDir . '/' . $file_name, file_get_contents($url));
|
|
|
|
}
|
|
|
|
|
2024-10-09 15:12:30 +03:00
|
|
|
private function recursiveAddFile(ZipArchive $zip, string $dir, string $folder = ''): void
|
2024-10-08 17:06:14 +03:00
|
|
|
{
|
|
|
|
$includes = new FilesystemIterator($dir);
|
2024-10-09 15:12:30 +03:00
|
|
|
|
2024-10-08 17:06:14 +03:00
|
|
|
foreach ($includes as $include) {
|
2024-10-09 15:12:30 +03:00
|
|
|
if(is_dir($include)) {
|
2024-10-14 15:52:52 +03:00
|
|
|
if ($include->getFilename() === 'app_modules') continue;
|
2024-10-09 15:12:30 +03:00
|
|
|
|
2024-11-05 13:24:04 +03:00
|
|
|
$tmpFolder = $folder . '/' . $include->getFilename();
|
2024-10-09 15:12:30 +03:00
|
|
|
$this->recursiveAddFile($zip, $include, $tmpFolder);
|
2024-10-08 17:06:14 +03:00
|
|
|
}
|
2024-10-09 15:12:30 +03:00
|
|
|
if (is_file($include)) {
|
2024-11-05 13:24:04 +03:00
|
|
|
$zip->addFile($include->getPathName(), $folder . "/" . $include->getFilename());
|
2024-10-08 17:06:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 15:12:30 +03:00
|
|
|
|
2024-09-11 17:38:46 +03:00
|
|
|
}
|