| @@ -1,157 +1,114 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | namespace WPIDE\App\Services\Storage; |
| 4 | 3 | |
| 5 | -use WPIDE\App\Classes\Freemius; | |
| 4 | +use const WPIDE\Constants\CONTENT_DIR; | |
| 6 | 5 | use const WPIDE\Constants\DIR; |
| 7 | -use const WPIDE\Constants\CONTENT_DIR; | |
| 8 | -use const WPIDE\Constants\FATAL_ERROR_DROPIN; | |
| 9 | -use const WPIDE\Constants\IMAGE_DATA_DIR; | |
| 10 | -use const WPIDE\Constants\TMP_DIR; | |
| 11 | -use const WPIDE\Constants\WP_PATH; | |
| 6 | +use const WPIDE\Constants\UPLOADS_DIR; | |
| 7 | + | |
| 12 | 8 | use WPIDE\App\AppConfig; |
| 13 | 9 | use WPIDE\App\Services\Storage\Adapters\WPFileSystem; |
| 14 | -use WPIDE\App\Services\Storage\Adapters\DefaultFileSystem; | |
| 10 | +use League\Flysystem\Adapter\Local; | |
| 11 | + | |
| 15 | 12 | class LocalFileSystem { |
| 16 | - public static function load( $root = null ) : Filesystem { | |
| 13 | + | |
| 14 | + public static function load($root = null): Filesystem | |
| 15 | + { | |
| 16 | + | |
| 17 | 17 | $fs = new Filesystem(); |
| 18 | - $fs->init( self::getConfig( $root ) ); | |
| 18 | + $fs->init(self::getConfig($root)); | |
| 19 | + | |
| 19 | 20 | return $fs; |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | - public static function getConfig( $root = null ) : array { | |
| 23 | - if ( empty( $root ) ) { | |
| 24 | - $root = self::getRootDir(); | |
| 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')); | |
| 25 | 27 | } |
| 26 | - if ( !str_contains( $root, WP_PATH ) ) { | |
| 27 | - $root = WP_PATH . $root; | |
| 28 | + | |
| 29 | + if(!str_contains($root, CONTENT_DIR)) { | |
| 30 | + $root = CONTENT_DIR.$root; | |
| 28 | 31 | } |
| 29 | - $root .= '/'; | |
| 30 | - $root = wp_normalize_path( $root ); | |
| 31 | - $excluded = [ | |
| 32 | - 'dirs' => LocalFileSystem::excludedDirs(), | |
| 33 | - 'files' => LocalFileSystem::excludedFiles(), | |
| 34 | - ]; | |
| 32 | + | |
| 35 | 33 | return [ |
| 36 | - 'root' => $root, | |
| 37 | - 'separator' => '/', | |
| 38 | - 'excluded_dirs' => ( !empty( $excluded['dirs'] ) ? $excluded['dirs'] : [] ), | |
| 39 | - 'excluded_files' => ( !empty( $excluded['files'] ) ? $excluded['files'] : [] ), | |
| 40 | - 'config' => [], | |
| 41 | - 'adapter' => function () use($root) { | |
| 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 | + | |
| 42 | 41 | $permissions = [ |
| 43 | 42 | 'file' => [ |
| 44 | - 'public' => ( defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : (( fileperms( WP_PATH . '/index.php' ) ? 0644 : 0777 )) ), | |
| 43 | + 'public' => defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : (fileperms( ABSPATH . 'index.php' ) ? 0644 : 0777), | |
| 45 | 44 | 'private' => 0600, |
| 46 | 45 | ], |
| 47 | - 'dir' => [ | |
| 48 | - 'public' => ( defined( 'FS_CHMOD_DIR' ) ? FS_CHMOD_DIR : (( fileperms( WP_PATH ) ? 0755 : 0777 )) ), | |
| 46 | + 'dir' => [ | |
| 47 | + 'public' => defined( 'FS_CHMOD_DIR' ) ? FS_CHMOD_DIR : (fileperms( ABSPATH ) ? 0755 : 0777), | |
| 49 | 48 | 'private' => 0700, |
| 50 | 49 | ], |
| 51 | 50 | ]; |
| 52 | - global $wp_filesystem; | |
| 53 | - $use_wp_filesystem = false; | |
| 51 | + | |
| 52 | + $use_wp_filesystem = false; | |
| 53 | + $credentials = null; | |
| 54 | + | |
| 54 | 55 | // Try and use WP filesystem |
| 55 | - if ( function_exists( 'request_filesystem_credentials' ) ) { | |
| 56 | - if ( defined( 'FS_METHOD' ) ) { | |
| 57 | - if ( !defined( 'WPIDE_FS_METHOD_FORCED_ELSEWHERE' ) ) { | |
| 58 | - define( 'WPIDE_FS_METHOD_FORCED_ELSEWHERE', FS_METHOD ); | |
| 59 | - } | |
| 60 | - } else { | |
| 61 | - define( 'FS_METHOD', 'direct' ); | |
| 62 | - } | |
| 56 | + if(function_exists('request_filesystem_credentials')) { | |
| 63 | 57 | ob_start(); |
| 64 | - $credentials = request_filesystem_credentials( '', FS_METHOD ); | |
| 58 | + $credentials = request_filesystem_credentials('', '', false, false); | |
| 65 | 59 | ob_end_clean(); |
| 66 | - $use_wp_filesystem = !empty( $wp_filesystem ) && wp_filesystem( $credentials ) === true; | |
| 60 | + $use_wp_filesystem = true; | |
| 67 | 61 | } |
| 68 | - if ( $use_wp_filesystem ) { | |
| 69 | - return new WPFileSystem( | |
| 70 | - $root, | |
| 71 | - $wp_filesystem, | |
| 72 | - 1, | |
| 73 | - $permissions | |
| 74 | - ); | |
| 75 | - } else { | |
| 76 | - return new DefaultFileSystem( | |
| 77 | - $root, | |
| 78 | - LOCK_EX, | |
| 79 | - 1, | |
| 80 | - $permissions | |
| 81 | - ); | |
| 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 ); | |
| 82 | 68 | } |
| 83 | - }, | |
| 69 | + | |
| 70 | + } | |
| 84 | 71 | ]; |
| 85 | 72 | } |
| 86 | 73 | |
| 87 | - public static function getRootDir() { | |
| 88 | - return AppConfig::get( 'file.root', '/' ); | |
| 89 | - } | |
| 90 | 74 | |
| 91 | - public static function advancedModeEnabled() : bool { | |
| 92 | - $advanced_mode = (bool) AppConfig::get( 'file.advanced_mode', false ); | |
| 93 | - if ( $advanced_mode ) { | |
| 94 | - AppConfig::update( 'file.advanced_mode', false ); | |
| 95 | - } | |
| 96 | - $rootDefault = AppConfig::getField( 'file.root.default' ); | |
| 97 | - if ( self::isRootExcluded() ) { | |
| 98 | - AppConfig::update( 'file.root', $rootDefault ); | |
| 99 | - } | |
| 100 | - return false; | |
| 101 | - } | |
| 75 | + public static function excludedDirs(): array | |
| 76 | + { | |
| 77 | + $user_excluded_dirs = array_filter(AppConfig::get('file.filter_entries', []), function($entry) { | |
| 102 | 78 | |
| 103 | - public static function excludedEntries( $type ) : array { | |
| 104 | - $entries = AppConfig::get( 'file.filter_entries', [] ); | |
| 105 | - $entries = ( is_array( $entries ) ? $entries : [] ); | |
| 106 | - return array_filter( $entries, function ( $entry ) use($type) { | |
| 107 | - if ( $type === 'dir' ) { | |
| 108 | - return substr( $entry, -1 ) === '/'; | |
| 109 | - } else { | |
| 110 | - return substr( $entry, -1 ) !== '/'; | |
| 111 | - } | |
| 112 | - } ); | |
| 113 | - } | |
| 79 | + return substr($entry , -1) === '/'; | |
| 80 | + }); | |
| 114 | 81 | |
| 115 | - public static function excludedDirs( $forceSafeMode = false ) : array { | |
| 116 | - $user_excluded_dirs = self::excludedEntries( 'dir' ); | |
| 117 | - $user_excluded_dirs = array_merge( [IMAGE_DATA_DIR, TMP_DIR], $user_excluded_dirs ); | |
| 118 | - if ( $forceSafeMode || !self::advancedModeEnabled() ) { | |
| 119 | - $user_excluded_dirs = array_merge( [ | |
| 82 | + return array_merge( | |
| 83 | + [ | |
| 120 | 84 | DIR, |
| 121 | - rtrim( DIR, '/' ) . '-pro/', | |
| 122 | - wp_normalize_path( WP_PLUGIN_DIR . '/wpide/' ), | |
| 123 | - wp_normalize_path( WP_PLUGIN_DIR . '/wpide-pro/' ), | |
| 124 | - wp_normalize_path( WP_PATH . '/wp-admin/' ), | |
| 125 | - wp_normalize_path( WP_PATH . '/wp-includes/' ) | |
| 126 | - ], $user_excluded_dirs ); | |
| 127 | - } | |
| 128 | - return $user_excluded_dirs; | |
| 85 | + rtrim(DIR, '/').'-pro/', | |
| 86 | + UPLOADS_DIR, | |
| 87 | + ABSPATH . 'wp-admin/', | |
| 88 | + ABSPATH . 'wp-includes/' | |
| 89 | + ], | |
| 90 | + $user_excluded_dirs | |
| 91 | + ); | |
| 129 | 92 | } |
| 130 | 93 | |
| 131 | - public static function excludedFiles( $forceSafeMode = false ) : array { | |
| 132 | - $user_excluded_files = self::excludedEntries( 'file' ); | |
| 133 | - $user_excluded_files = array_merge( [CONTENT_DIR . '/' . FATAL_ERROR_DROPIN], $user_excluded_files ); | |
| 134 | - if ( $forceSafeMode || !self::advancedModeEnabled() ) { | |
| 135 | - $user_excluded_files = array_merge( [wp_normalize_path( WP_PATH . '/wp-*.php' ), wp_normalize_path( WP_PATH . '/index.php' ), wp_normalize_path( WP_PATH . '/xmlrpc.php' )], $user_excluded_files ); | |
| 136 | - } | |
| 137 | - return $user_excluded_files; | |
| 138 | - } | |
| 94 | + public static function excludedFiles(): array | |
| 95 | + { | |
| 139 | 96 | |
| 140 | - public static function isDirExcluded( $path ) : bool { | |
| 141 | - if ( $path !== '/' ) { | |
| 142 | - $path = wp_normalize_path( WP_PATH . '/' . $path . '/' ); | |
| 143 | - foreach ( self::excludedDirs( true ) as $excluded ) { | |
| 144 | - if ( str_contains( $path, $excluded ) ) { | |
| 145 | - return true; | |
| 146 | - } | |
| 147 | - } | |
| 148 | - } | |
| 149 | - return false; | |
| 150 | - } | |
| 97 | + $user_excluded_files = array_filter(AppConfig::get('file.filter_entries', []), function($entry) { | |
| 151 | 98 | |
| 152 | - public static function isRootExcluded() : bool { | |
| 153 | - $root = self::getRootDir(); | |
| 154 | - return self::isDirExcluded( $root ); | |
| 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 | + ); | |
| 155 | 112 | } |
| 156 | 113 | |
| 157 | -} | |
| 114 | +} | |