| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.6 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 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 bool True when the copy ran, false when the source or destination is unusable. |
| 28 |
*/ |
| 29 |
public static function copy($src, $dest) |
| 30 |
{ |
| 31 |
// Checked up front so a missing source does not raise an opendir() warning. |
| 32 |
if (!is_dir($src)) |
| 33 |
{ |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
$dir = opendir($src); |
| 38 |
|
| 39 |
// opendir() still returns false when $src exists but is unreadable. Bail out rather |
| 40 |
// than feeding false into readdir(), which is a TypeError since PHP 8.0. |
| 41 |
if ($dir === false) |
| 42 |
{ |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 47 |
if (!is_dir($dest) && !mkdir($dest) && !is_dir($dest)) |
| 48 |
{ |
| 49 |
closedir($dir); |
| 50 |
|
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
while(false !== ($file = readdir($dir))) |
| 55 |
{ |
| 56 |
if (in_array($file, ['.', '..'])) |
| 57 |
{ |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
if (is_dir($src . DIRECTORY_SEPARATOR . $file)) |
| 62 |
{ |
| 63 |
self::copy($src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file); |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
copy($src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
closedir($dir); |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates a directory |
| 78 |
* |
| 79 |
* @param string $dir |
| 80 |
* |
| 81 |
* @return mixed |
| 82 |
*/ |
| 83 |
public static function create($dir) |
| 84 |
{ |
| 85 |
if (!$dir) |
| 86 |
{ |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (file_exists($dir)) |
| 91 |
{ |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 96 |
mkdir($dir); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Empties a directory |
| 101 |
* |
| 102 |
* @param string $dir |
| 103 |
* |
| 104 |
* @return mixed |
| 105 |
*/ |
| 106 |
public static function empty($dir) |
| 107 |
{ |
| 108 |
if (!file_exists($dir)) |
| 109 |
{ |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); |
| 114 |
$files = new \RecursiveIteratorIterator($it, |
| 115 |
\RecursiveIteratorIterator::CHILD_FIRST); |
| 116 |
|
| 117 |
foreach($files as $file) |
| 118 |
{ |
| 119 |
if ($file->isDir()) |
| 120 |
{ |
| 121 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir |
| 122 |
rmdir($file->getRealPath()); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink |
| 127 |
unlink($file->getRealPath()); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Deletes a directory |
| 134 |
* |
| 135 |
* @param string $dir |
| 136 |
* |
| 137 |
* @return mixed |
| 138 |
*/ |
| 139 |
public static function delete($dir) |
| 140 |
{ |
| 141 |
if (!$dir) |
| 142 |
{ |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
if (!file_exists($dir)) |
| 147 |
{ |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
self::empty($dir); |
| 152 |
|
| 153 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir |
| 154 |
rmdir($dir); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Checks if the path exists. If not creates the folders as well as subfolders. |
| 159 |
* |
| 160 |
* @param string $path The folder path |
| 161 |
* @param string $protect If set to true, each folder will be protected by disabling PHP engine and preventing folder browsing |
| 162 |
* |
| 163 |
* @return bool |
| 164 |
*/ |
| 165 |
public static function createDirs($path, $protect = true) |
| 166 |
{ |
| 167 |
if (!is_dir($path)) |
| 168 |
{ |
| 169 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir |
| 170 |
mkdir($path, 0755, true); |
| 171 |
|
| 172 |
// New folder created. Let's protect it. |
| 173 |
if ($protect) |
| 174 |
{ |
| 175 |
self::writeHtaccessFile($path); |
| 176 |
self::writeIndexHtmlFile($path); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// Make sure the folder is writable |
| 181 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
| 182 |
return @is_writable($path); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Add an .htaccess file to the folder in order to disable PHP engine entirely |
| 187 |
* |
| 188 |
* @param string $path The path where to write the file |
| 189 |
* |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public static function writeHtaccessFile($path) |
| 193 |
{ |
| 194 |
$content = ' |
| 195 |
# Block direct PHP access |
| 196 |
<Files *.php> |
| 197 |
deny from all |
| 198 |
</Files> |
| 199 |
'; |
| 200 |
|
| 201 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 202 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 203 |
file_put_contents($path . '/.htaccess', $content); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Creates an empty index.html file to prevent directory listing |
| 208 |
* |
| 209 |
* @param string $path The path where to write the file |
| 210 |
* |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
public static function writeIndexHtmlFile($path) |
| 214 |
{ |
| 215 |
$content = '<!DOCTYPE html><title></title>'; |
| 216 |
|
| 217 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents |
| 218 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 219 |
file_put_contents($path . '/index.html', $content); |
| 220 |
} |
| 221 |
} |