| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.1.102 |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Directory |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Copies files from source to directory |
| 23 |
* |
| 24 |
* @param string $src |
| 25 |
* @param string $dest |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public static function copy($src, $dest) |
| 30 |
{ |
| 31 |
$dir = opendir($src); |
| 32 |
|
| 33 |
mkdir($dest); |
| 34 |
|
| 35 |
while(false !== ($file = readdir($dir))) |
| 36 |
{ |
| 37 |
if (in_array($file, ['.', '..'])) |
| 38 |
{ |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
if (is_dir($src . DIRECTORY_SEPARATOR . $file)) |
| 43 |
{ |
| 44 |
self::copy($src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file); |
| 45 |
} |
| 46 |
else |
| 47 |
{ |
| 48 |
copy($src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
closedir($dir); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates a directory |
| 57 |
* |
| 58 |
* @param string $dir |
| 59 |
* |
| 60 |
* @return mixed |
| 61 |
*/ |
| 62 |
public static function create($dir) |
| 63 |
{ |
| 64 |
if (!$dir) |
| 65 |
{ |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if (file_exists($dir)) |
| 70 |
{ |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
mkdir($dir); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Empties a directory |
| 79 |
* |
| 80 |
* @param string $dir |
| 81 |
* |
| 82 |
* @return mixed |
| 83 |
*/ |
| 84 |
public static function empty($dir) |
| 85 |
{ |
| 86 |
if (!file_exists($dir)) |
| 87 |
{ |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); |
| 92 |
$files = new \RecursiveIteratorIterator($it, |
| 93 |
\RecursiveIteratorIterator::CHILD_FIRST); |
| 94 |
|
| 95 |
foreach($files as $file) |
| 96 |
{ |
| 97 |
if ($file->isDir()) |
| 98 |
{ |
| 99 |
rmdir($file->getRealPath()); |
| 100 |
} |
| 101 |
else |
| 102 |
{ |
| 103 |
unlink($file->getRealPath()); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Deletes a directory |
| 110 |
* |
| 111 |
* @param string $dir |
| 112 |
* |
| 113 |
* @return mixed |
| 114 |
*/ |
| 115 |
public static function delete($dir) |
| 116 |
{ |
| 117 |
if (!$dir) |
| 118 |
{ |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
if (!file_exists($dir)) |
| 123 |
{ |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
self::empty($dir); |
| 128 |
|
| 129 |
rmdir($dir); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Checks if the path exists. If not creates the folders as well as subfolders. |
| 134 |
* |
| 135 |
* @param string $path The folder path |
| 136 |
* @param string $protect If set to true, each folder will be protected by disabling PHP engine and preventing folder browsing |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public static function createDirs($path, $protect = true) |
| 141 |
{ |
| 142 |
if (!is_dir($path)) |
| 143 |
{ |
| 144 |
mkdir($path, 0755, true); |
| 145 |
|
| 146 |
// New folder created. Let's protect it. |
| 147 |
if ($protect) |
| 148 |
{ |
| 149 |
self::writeHtaccessFile($path); |
| 150 |
self::writeIndexHtmlFile($path); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Make sure the folder is writable |
| 155 |
return @is_writable($path); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Add an .htaccess file to the folder in order to disable PHP engine entirely |
| 160 |
* |
| 161 |
* @param string $path The path where to write the file |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public static function writeHtaccessFile($path) |
| 166 |
{ |
| 167 |
$content = ' |
| 168 |
# Block direct PHP access |
| 169 |
<Files *.php> |
| 170 |
deny from all |
| 171 |
</Files> |
| 172 |
'; |
| 173 |
|
| 174 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 175 |
file_put_contents($path . '/.htaccess', $content); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Creates an empty index.html file to prevent directory listing |
| 180 |
* |
| 181 |
* @param string $path The path where to write the file |
| 182 |
* |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
public static function writeIndexHtmlFile($path) |
| 186 |
{ |
| 187 |
$content = '<!DOCTYPE html><title></title>'; |
| 188 |
|
| 189 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 190 |
file_put_contents($path . '/index.html', $content); |
| 191 |
} |
| 192 |
} |