validator
4 days ago
class-adbc-common-utils.php
4 months ago
class-adbc-files.php
7 months ago
class-adbc-logging.php
3 months ago
class-adbc-notifications.php
4 days ago
class-adbc-rest.php
4 days ago
class-adbc-files.php
433 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC files class. |
| 9 | * |
| 10 | * This class provides files and folders methods. |
| 11 | */ |
| 12 | class ADBC_Files extends ADBC_Singleton { |
| 13 | |
| 14 | /** |
| 15 | * The WordPress file system. |
| 16 | * |
| 17 | * @var object|null |
| 18 | */ |
| 19 | private $wp_fs = null; |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | */ |
| 24 | protected function __construct() { |
| 25 | parent::__construct(); |
| 26 | $this->prepare_wp_fs(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Prepare the WordPress file system. |
| 31 | */ |
| 32 | private function prepare_wp_fs() { |
| 33 | |
| 34 | // Load the WP file API if needed. |
| 35 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 36 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 37 | } |
| 38 | |
| 39 | // Initialize the global filesystem instance if it is not already initialized. |
| 40 | if ( empty( $GLOBALS['wp_filesystem'] ) ) { |
| 41 | WP_Filesystem(); |
| 42 | } |
| 43 | |
| 44 | $fs = ! empty( $GLOBALS['wp_filesystem'] ) ? $GLOBALS['wp_filesystem'] : null; |
| 45 | |
| 46 | // If FTPext is selected in the FS_METHOD but fails in connection, fall back to direct Filesystem. |
| 47 | // This avoids Fatal error: ftp_pwd(): Argument #1 ($ftp) must be of type FTP\Connection, null given |
| 48 | if ( $fs instanceof WP_Filesystem_FTPext && empty( $fs->link ) ) { |
| 49 | |
| 50 | if ( ! class_exists( 'WP_Filesystem_Direct' ) ) { |
| 51 | require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 52 | require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; |
| 53 | } |
| 54 | |
| 55 | $fs = new WP_Filesystem_Direct( null ); |
| 56 | } |
| 57 | |
| 58 | $this->wp_fs = $fs; |
| 59 | |
| 60 | // guarantee FS_CHMOD_* constants exist (important for PHP 8+). |
| 61 | |
| 62 | if ( ! defined( 'FS_CHMOD_DIR' ) ) { |
| 63 | $dir_perms = @fileperms( ABSPATH ); |
| 64 | if ( $dir_perms ) { |
| 65 | define( 'FS_CHMOD_DIR', ( $dir_perms & 0777 ) | 0755 ); |
| 66 | } else { |
| 67 | // Safe fallback if fileperms() fails. |
| 68 | define( 'FS_CHMOD_DIR', 0755 ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if ( ! defined( 'FS_CHMOD_FILE' ) ) { |
| 73 | $file_perms = @fileperms( ABSPATH . 'index.php' ); |
| 74 | if ( $file_perms ) { |
| 75 | define( 'FS_CHMOD_FILE', ( $file_perms & 0777 ) | 0644 ); |
| 76 | } else { |
| 77 | // Safe fallback if fileperms() fails. |
| 78 | define( 'FS_CHMOD_FILE', 0644 ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if the WordPress file system is initialized. |
| 86 | * |
| 87 | * @return boolean True if the WordPress file system is initialized, false otherwise. |
| 88 | */ |
| 89 | public function is_wp_fs_initialized() { |
| 90 | |
| 91 | if ( $this->wp_fs === null ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the handle of a file. |
| 100 | * |
| 101 | * @param string $file_path The file path. |
| 102 | * @param string $mode The mode to open the file in. Default is 'r'. |
| 103 | * @return resource|bool The file handle or false if the file does not exist or is not readable. |
| 104 | */ |
| 105 | public function get_file_handle( $file_path, $mode = 'r' ) { |
| 106 | |
| 107 | if ( ! $this->is_wp_fs_initialized() ) |
| 108 | return false; |
| 109 | |
| 110 | if ( $mode == 'r' && ( ! $this->exists( $file_path ) || ! $this->is_readable( $file_path ) ) ) |
| 111 | return false; |
| 112 | |
| 113 | $handle = fopen( $file_path, $mode ); |
| 114 | |
| 115 | return $handle; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if a file exists. |
| 120 | * |
| 121 | * @return boolean True if the file exists, false otherwise. |
| 122 | */ |
| 123 | public function exists( $file_path ) { |
| 124 | |
| 125 | if ( ! $this->is_wp_fs_initialized() ) |
| 126 | return false; |
| 127 | |
| 128 | return $this->wp_fs->exists( $file_path ); |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Check if a file is readable. |
| 134 | * |
| 135 | * @return boolean True if the file is readable, false otherwise. |
| 136 | */ |
| 137 | public function is_readable( $file_path ) { |
| 138 | |
| 139 | if ( ! $this->is_wp_fs_initialized() ) |
| 140 | return false; |
| 141 | |
| 142 | return $this->wp_fs->is_readable( $file_path ); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Check if a file is writable. |
| 148 | * |
| 149 | * @return boolean True if the file is writable, false otherwise. |
| 150 | */ |
| 151 | public function is_writable( $file_path ) { |
| 152 | |
| 153 | if ( ! $this->is_wp_fs_initialized() ) |
| 154 | return false; |
| 155 | |
| 156 | return $this->wp_fs->is_writable( $file_path ); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check if a file is readable and writable. |
| 162 | * |
| 163 | * @return boolean True if the file is readable and writable, false otherwise. |
| 164 | */ |
| 165 | public function is_readable_and_writable( $file_path ) { |
| 166 | |
| 167 | if ( ! $this->is_wp_fs_initialized() ) |
| 168 | return false; |
| 169 | |
| 170 | return $this->is_readable( $file_path ) && $this->is_writable( $file_path ); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Check if a path is a directory. |
| 176 | * |
| 177 | * @return boolean True if the path is a directory, false otherwise. |
| 178 | */ |
| 179 | public function is_dir( $folder_path ) { |
| 180 | |
| 181 | if ( ! $this->is_wp_fs_initialized() ) |
| 182 | return false; |
| 183 | |
| 184 | return $this->wp_fs->is_dir( $folder_path ); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Check if a path is a file. |
| 190 | * |
| 191 | * @param string $file_path The file path. |
| 192 | * @return boolean True if the path is a file, false otherwise. |
| 193 | */ |
| 194 | public function is_file( $file_path ) { |
| 195 | |
| 196 | if ( ! $this->is_wp_fs_initialized() ) |
| 197 | return false; |
| 198 | |
| 199 | return $this->wp_fs->is_file( $file_path ); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get the file size. |
| 205 | * |
| 206 | * @return int|bool The file size or false in case of failure. |
| 207 | */ |
| 208 | public function size( $file_path ) { |
| 209 | |
| 210 | if ( ! $this->is_wp_fs_initialized() ) |
| 211 | return false; |
| 212 | |
| 213 | return $this->wp_fs->size( $file_path ); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Get the contents of a file. |
| 219 | * |
| 220 | * @param string $file_path The file path. |
| 221 | * @return string|bool The file content or false in case of failure. |
| 222 | */ |
| 223 | public function get_contents( $file_path ) { |
| 224 | |
| 225 | if ( ! $this->is_wp_fs_initialized() ) |
| 226 | return false; |
| 227 | |
| 228 | return $this->wp_fs->get_contents( $file_path ); |
| 229 | |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Create an new folder if it doesn't exist. |
| 234 | * |
| 235 | * @param string $folder_path Folder path to create. |
| 236 | * @return boolean True if successful, false otherwise. |
| 237 | */ |
| 238 | public function create_folder( $folder_path, $secure_folder = false ) { |
| 239 | |
| 240 | if ( ! $this->is_wp_fs_initialized() ) |
| 241 | return false; |
| 242 | |
| 243 | // Create the directory only if it doesn't exist. |
| 244 | if ( ! $this->is_dir( $folder_path ) ) { |
| 245 | if ( ! $this->wp_fs->mkdir( $folder_path ) ) |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | // Always try to secure the folder if needed. |
| 250 | if ( $secure_folder ) |
| 251 | $this->secure_folder( $folder_path ); |
| 252 | |
| 253 | return true; |
| 254 | |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Create an new empty file if it doesn't exist. |
| 259 | * |
| 260 | * @param string $file_path The file path. |
| 261 | * @return boolean True if successful, false otherwise. |
| 262 | */ |
| 263 | public function create_file( $file_path ) { |
| 264 | |
| 265 | if ( ! $this->is_wp_fs_initialized() ) |
| 266 | return false; |
| 267 | |
| 268 | // Check if the file already exists. |
| 269 | if ( $this->exists( $file_path ) ) |
| 270 | return true; |
| 271 | |
| 272 | // Create the file. |
| 273 | if ( ! $this->put_contents( $file_path, "" ) ) |
| 274 | return false; |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Put contents into a file or create a new file if it doesn't exist. |
| 281 | * |
| 282 | * @param string $file_path The file path. |
| 283 | * @param string $content The content to put into the file. |
| 284 | * @return boolean True if successful, false otherwise. |
| 285 | */ |
| 286 | public function put_contents( $file_path, $content ) { |
| 287 | |
| 288 | if ( ! $this->is_wp_fs_initialized() ) |
| 289 | return false; |
| 290 | |
| 291 | return $this->wp_fs->put_contents( $file_path, $content ); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Create a silence is golden file or .htaccess or all. |
| 297 | * |
| 298 | * @param string $folder_path Folder path to create the file in. |
| 299 | * @param string $file_type The file type to create ('index', 'htaccess', 'all'). |
| 300 | * @return boolean True if successful, false otherwise. |
| 301 | */ |
| 302 | public function secure_folder( $folder_path, $file_type = 'all' ) { |
| 303 | |
| 304 | if ( ! $this->is_wp_fs_initialized() ) |
| 305 | return false; |
| 306 | |
| 307 | // Check if the file type is valid. |
| 308 | if ( ! in_array( $file_type, [ 'index', 'htaccess', 'all' ] ) ) |
| 309 | return false; |
| 310 | |
| 311 | // Create the index.php file if it doesn't exist. |
| 312 | if ( in_array( $file_type, [ 'index', 'all' ] ) ) { |
| 313 | if ( ! $this->exists( $folder_path . '/index.php' ) ) { |
| 314 | $index_content = "<?php\n// Silence is golden."; |
| 315 | if ( ! $this->put_contents( $folder_path . '/index.php', $index_content ) ) |
| 316 | return false; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Create the .htaccess file if it doesn't exist. |
| 321 | if ( in_array( $file_type, [ 'htaccess', 'all' ] ) ) { |
| 322 | if ( ! $this->exists( $folder_path . '/.htaccess' ) ) { |
| 323 | $htaccess_content = "Order deny,allow\nDeny from all"; |
| 324 | if ( ! $this->put_contents( $folder_path . '/.htaccess', $htaccess_content ) ) |
| 325 | return false; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Delete folder with its content. |
| 334 | * |
| 335 | * @param string $folder_path Folder path to delete. |
| 336 | * @return boolean True if successful, false otherwise. |
| 337 | */ |
| 338 | public function delete_folder( $folder_path ) { |
| 339 | |
| 340 | if ( ! $this->is_wp_fs_initialized() ) |
| 341 | return false; |
| 342 | |
| 343 | if ( ! $this->exists( $folder_path ) ) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | if ( ! $this->is_dir( $folder_path ) ) { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | $deleted = $this->wp_fs->delete( $folder_path, true ); |
| 352 | |
| 353 | if ( ! $deleted ) { |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | return true; |
| 358 | |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /** |
| 363 | * Get the list of folders names inside a directory. |
| 364 | * |
| 365 | * @param string $dir_path The directory path. |
| 366 | * @param boolean $alph_sorted Whether to sort the folders alphabetically or not. |
| 367 | * @return string[] The list of folders names inside the directory. |
| 368 | */ |
| 369 | public function get_list_of_dirs_inside_dir( $dir_path, $alph_sorted = false ) { |
| 370 | |
| 371 | if ( ! $this->is_wp_fs_initialized() ) |
| 372 | return []; |
| 373 | |
| 374 | if ( ! $this->is_dir( $dir_path ) ) |
| 375 | return []; |
| 376 | |
| 377 | $dirs = $this->wp_fs->dirlist( $dir_path ); |
| 378 | |
| 379 | if ( ! is_array( $dirs ) ) |
| 380 | return []; |
| 381 | |
| 382 | $dirs_list = []; |
| 383 | |
| 384 | foreach ( $dirs as $dir ) { |
| 385 | if ( $dir['type'] === 'd' ) { |
| 386 | $dirs_list[] = $dir['name']; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if ( $alph_sorted ) |
| 391 | sort( $dirs_list ); |
| 392 | |
| 393 | return $dirs_list; |
| 394 | |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /** |
| 399 | * Get the list of files names inside a directory. |
| 400 | * |
| 401 | * @param string $dir_path The directory path. |
| 402 | * @param boolean $alph_sorted Whether to sort the files alphabetically or not. |
| 403 | * @return string[] The list of files names inside the directory. |
| 404 | */ |
| 405 | public function get_list_of_files_inside_dir( $dir_path, $alph_sorted = false ) { |
| 406 | |
| 407 | if ( ! $this->is_wp_fs_initialized() ) |
| 408 | return []; |
| 409 | |
| 410 | if ( ! $this->is_dir( $dir_path ) ) |
| 411 | return []; |
| 412 | |
| 413 | $files = $this->wp_fs->dirlist( $dir_path ); |
| 414 | |
| 415 | if ( ! is_array( $files ) ) |
| 416 | return []; |
| 417 | |
| 418 | $files_list = []; |
| 419 | |
| 420 | foreach ( $files as $file ) { |
| 421 | if ( $file['type'] === 'f' ) { |
| 422 | $files_list[] = $file['name']; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if ( $alph_sorted ) |
| 427 | sort( $files_list ); |
| 428 | |
| 429 | return $files_list; |
| 430 | |
| 431 | } |
| 432 | |
| 433 | } |