| 1 |
<?php |
| 2 |
namespace WPIDE\App\Services\Storage; |
| 3 |
|
| 4 |
use const WPIDE\Constants\CONTENT_DIR; |
| 5 |
use const WPIDE\Constants\DIR; |
| 6 |
use const WPIDE\Constants\UPLOADS_DIR; |
| 7 |
|
| 8 |
use WPIDE\App\AppConfig; |
| 9 |
use WPIDE\App\Services\Storage\Adapters\WPFileSystem; |
| 10 |
use League\Flysystem\Adapter\Local; |
| 11 |
|
| 12 |
class LocalFileSystem { |
| 13 |
|
| 14 |
public static function load($root = null): Filesystem |
| 15 |
{ |
| 16 |
|
| 17 |
$fs = new Filesystem(); |
| 18 |
$fs->init(self::getConfig($root)); |
| 19 |
|
| 20 |
return $fs; |
| 21 |
} |
| 22 |
|
| 23 |
public static function getConfig($root = null, $excluded = []): array |
| 24 |
{ |
| 25 |
if(empty($root)) { |
| 26 |
$root = apply_filters('wpide_filesystem_root', AppConfig::get('file.root')); |
| 27 |
} |
| 28 |
|
| 29 |
if(!str_contains($root, CONTENT_DIR)) { |
| 30 |
$root = CONTENT_DIR.$root; |
| 31 |
} |
| 32 |
|
| 33 |
return [ |
| 34 |
'root' => $root, |
| 35 |
'separator' => '/', |
| 36 |
'excluded_dirs' => !empty($excluded['dirs']) ? $excluded['dirs'] : [], |
| 37 |
'excluded_files' => !empty($excluded['files']) ? $excluded['files'] : [], |
| 38 |
'config' => [], |
| 39 |
'adapter' => function() use($root){ |
| 40 |
|
| 41 |
$permissions = [ |
| 42 |
'file' => [ |
| 43 |
'public' => defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : (fileperms( ABSPATH . 'index.php' ) ? 0644 : 0777), |
| 44 |
'private' => 0600, |
| 45 |
], |
| 46 |
'dir' => [ |
| 47 |
'public' => defined( 'FS_CHMOD_DIR' ) ? FS_CHMOD_DIR : (fileperms( ABSPATH ) ? 0755 : 0777), |
| 48 |
'private' => 0700, |
| 49 |
], |
| 50 |
]; |
| 51 |
|
| 52 |
$use_wp_filesystem = false; |
| 53 |
$credentials = null; |
| 54 |
|
| 55 |
// Try and use WP filesystem |
| 56 |
if(function_exists('request_filesystem_credentials')) { |
| 57 |
ob_start(); |
| 58 |
$credentials = request_filesystem_credentials('', '', false, false); |
| 59 |
ob_end_clean(); |
| 60 |
$use_wp_filesystem = true; |
| 61 |
} |
| 62 |
|
| 63 |
if ( $use_wp_filesystem && wp_filesystem( $credentials ) ) { |
| 64 |
global $wp_filesystem; |
| 65 |
return new WPFileSystem($root, $wp_filesystem, 1, $permissions ); |
| 66 |
}else{ |
| 67 |
return new Local($root, LOCK_EX, 1, $permissions ); |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public static function excludedDirs(): array |
| 76 |
{ |
| 77 |
$user_excluded_dirs = array_filter(AppConfig::get('file.filter_entries', []), function($entry) { |
| 78 |
|
| 79 |
return substr($entry , -1) === '/'; |
| 80 |
}); |
| 81 |
|
| 82 |
return array_merge( |
| 83 |
[ |
| 84 |
DIR, |
| 85 |
rtrim(DIR, '/').'-pro/', |
| 86 |
UPLOADS_DIR, |
| 87 |
ABSPATH . 'wp-admin/', |
| 88 |
ABSPATH . 'wp-includes/' |
| 89 |
], |
| 90 |
$user_excluded_dirs |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
public static function excludedFiles(): array |
| 95 |
{ |
| 96 |
|
| 97 |
$user_excluded_files = array_filter(AppConfig::get('file.filter_entries', []), function($entry) { |
| 98 |
|
| 99 |
return substr($entry , -1) !== '/'; |
| 100 |
}); |
| 101 |
|
| 102 |
return array_merge( |
| 103 |
[ |
| 104 |
ABSPATH . '!wp-config.php', |
| 105 |
ABSPATH . 'wp-*.php', |
| 106 |
ABSPATH . 'index.php', |
| 107 |
ABSPATH . 'xmlrpc.php', |
| 108 |
CONTENT_DIR.'/fatal-error-handler.php', |
| 109 |
], |
| 110 |
$user_excluded_files |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
} |