| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
if ( ! class_exists( 'ReduxLegacy_Filesystem' ) ) { |
| 9 |
class ReduxLegacy_Filesystem { |
| 10 |
|
| 11 |
/** |
| 12 |
* Instance of this class. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
* @var object |
| 16 |
*/ |
| 17 |
protected static $instance = null; |
| 18 |
|
| 19 |
protected static $direct = null; |
| 20 |
|
| 21 |
private $creds = array(); |
| 22 |
|
| 23 |
public $fs_object = null; |
| 24 |
|
| 25 |
public $parent = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Return an instance of this class. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @return object A single instance of this class. |
| 32 |
*/ |
| 33 |
public static function get_instance( $parent = null ) { |
| 34 |
|
| 35 |
// If the single instance hasn't been set, set it now. |
| 36 |
if ( null == self::$instance ) { |
| 37 |
self::$instance = new self(); |
| 38 |
} |
| 39 |
|
| 40 |
if ( $parent !== null ) { |
| 41 |
self::$instance->parent = $parent; |
| 42 |
} |
| 43 |
|
| 44 |
return self::$instance; |
| 45 |
} |
| 46 |
|
| 47 |
public function ftp_form() { |
| 48 |
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { |
| 49 |
echo '<div class="wrap"><div class="error"><p>'; |
| 50 |
echo '<strong>' . __( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1$1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2$2s" target="_blank">outlined here</a>.', 'redux-framework' ), ReduxLegacy_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ); |
| 51 |
echo '</p></div><h2></h2>' . '</div>'; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
function filesystem_init( $form_url, $method = '', $context = false, $fields = null ) { |
| 56 |
global $wp_filesystem; |
| 57 |
|
| 58 |
if ( ! empty( $this->creds ) ) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
ob_start(); |
| 63 |
|
| 64 |
/* first attempt to get credentials */ |
| 65 |
if ( false === ( $this->creds = request_filesystem_credentials( $form_url, $method, false, $context ) ) ) { |
| 66 |
$this->creds = array(); |
| 67 |
$this->parent->ftp_form = ob_get_contents(); |
| 68 |
ob_end_clean(); |
| 69 |
|
| 70 |
/** |
| 71 |
* if we comes here - we don't have credentials |
| 72 |
* so the request for them is displaying |
| 73 |
* no need for further processing |
| 74 |
*/ |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
/* now we got some credentials - try to use them*/ |
| 80 |
if ( ! WP_Filesystem( $this->creds ) ) { |
| 81 |
$this->creds = array(); |
| 82 |
/* incorrect connection data - ask for credentials again, now with error message */ |
| 83 |
request_filesystem_credentials( $form_url, '', true, $context ); |
| 84 |
$this->parent->ftp_form = ob_get_contents(); |
| 85 |
ob_end_clean(); |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
public static function load_direct() { |
| 94 |
if ( self::$direct === null ) { |
| 95 |
require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php'; |
| 96 |
require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php'; |
| 97 |
self::$direct = new WP_Filesystem_Direct( array() ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public function execute( $action, $file = '', $params = '' ) { |
| 102 |
|
| 103 |
if ( empty( $this->parent->args ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! empty( $params ) ) { |
| 108 |
extract( $params ); |
| 109 |
} |
| 110 |
|
| 111 |
// Setup the filesystem with creds |
| 112 |
require_once ABSPATH . '/wp-admin/includes/template.php'; |
| 113 |
require_once ABSPATH . '/wp-includes/pluggable.php'; |
| 114 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 115 |
|
| 116 |
if ( $this->parent->args['menu_type'] == 'submenu' ) { |
| 117 |
$page_parent = $this->parent->args['page_parent']; |
| 118 |
$base = $page_parent . '?page=' . $this->parent->args['page_slug']; |
| 119 |
} else { |
| 120 |
$base = 'admin.php?page=' . $this->parent->args['page_slug']; |
| 121 |
} |
| 122 |
|
| 123 |
$url = wp_nonce_url( $base, 'redux-options' ); |
| 124 |
|
| 125 |
$this->filesystem_init( $url, 'direct', dirname( $file ) ); |
| 126 |
|
| 127 |
if ( ! file_exists( ReduxFrameworkLegacy::$_upload_dir ) ) { |
| 128 |
$this->do_action( 'mkdir', ReduxFrameworkLegacy::$_upload_dir ); |
| 129 |
} |
| 130 |
|
| 131 |
$hash_path = trailingslashit( ReduxFrameworkLegacy::$_upload_dir ) . 'hash'; |
| 132 |
if ( ! file_exists( $hash_path ) ) { |
| 133 |
$this->do_action( |
| 134 |
'put_contents', |
| 135 |
$hash_path, |
| 136 |
array( |
| 137 |
'content' => md5( network_site_url() . '-' . $_SERVER['REMOTE_ADDR'] ), |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
$version_path = trailingslashit( ReduxFrameworkLegacy::$_upload_dir ) . 'version'; |
| 142 |
if ( ! file_exists( $version_path ) ) { |
| 143 |
$this->do_action( |
| 144 |
'put_contents', |
| 145 |
$version_path, |
| 146 |
array( |
| 147 |
'content' => ReduxFrameworkLegacy::$_version, |
| 148 |
) |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
$index_path = trailingslashit( ReduxFrameworkLegacy::$_upload_dir ) . 'index.php'; |
| 153 |
if ( ! file_exists( $index_path ) ) { |
| 154 |
$this->do_action( |
| 155 |
'put_contents', |
| 156 |
$index_path, |
| 157 |
array( |
| 158 |
'content' => '<?php' . PHP_EOL . '// Silence is golden.', |
| 159 |
) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
return $this->do_action( $action, $file, $params ); |
| 164 |
} |
| 165 |
|
| 166 |
public function do_action( $action, $file = '', $params = '' ) { |
| 167 |
|
| 168 |
if ( ! empty( $params ) ) { |
| 169 |
extract( $params ); |
| 170 |
} |
| 171 |
|
| 172 |
global $wp_filesystem; |
| 173 |
|
| 174 |
if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) { |
| 175 |
if ( defined( 'FS_CHMOD_FILE' ) ) { |
| 176 |
$chmod = FS_CHMOD_FILE; |
| 177 |
} else { |
| 178 |
$chmod = 0644; |
| 179 |
} |
| 180 |
} |
| 181 |
$res = false; |
| 182 |
if ( ! isset( $recursive ) ) { |
| 183 |
$recursive = false; |
| 184 |
} |
| 185 |
|
| 186 |
// $target_dir = $wp_filesystem->find_folder( dirname( $file ) ); |
| 187 |
|
| 188 |
// Do unique stuff |
| 189 |
if ( $action == 'mkdir' ) { |
| 190 |
|
| 191 |
if ( defined( 'FS_CHMOD_DIR' ) ) { |
| 192 |
$chmod = FS_CHMOD_DIR; |
| 193 |
} else { |
| 194 |
$chmod = 0755; |
| 195 |
} |
| 196 |
$res = $wp_filesystem->mkdir( $file ); |
| 197 |
if ( ! $res ) { |
| 198 |
wp_mkdir_p( $file ); |
| 199 |
|
| 200 |
$res = file_exists( $file ); |
| 201 |
if ( ! $res ) { |
| 202 |
mkdir( $file, $chmod, true ); |
| 203 |
$res = file_exists( $file ); |
| 204 |
} |
| 205 |
} |
| 206 |
$index_path = trailingslashit( $file ) . 'index.php'; |
| 207 |
if ( ! file_exists( $index_path ) ) { |
| 208 |
$wp_filesystem->put_contents( |
| 209 |
$index_path, |
| 210 |
'<?php' . PHP_EOL . '// Silence is golden.', |
| 211 |
FS_CHMOD_FILE // predefined mode settings for WP files |
| 212 |
); |
| 213 |
} |
| 214 |
} elseif ( $action == 'rmdir' ) { |
| 215 |
$res = $wp_filesystem->rmdir( $file, $recursive ); |
| 216 |
} elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) { |
| 217 |
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { |
| 218 |
$res = copy( $file, $destination ); |
| 219 |
if ( $res ) { |
| 220 |
chmod( $destination, $chmod ); |
| 221 |
} |
| 222 |
} else { |
| 223 |
$res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod ); |
| 224 |
} |
| 225 |
} elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) { |
| 226 |
$res = $wp_filesystem->copy( $file, $destination, $overwrite ); |
| 227 |
} elseif ( $action == 'delete' ) { |
| 228 |
$res = $wp_filesystem->delete( $file, $recursive ); |
| 229 |
} elseif ( $action == 'rmdir' ) { |
| 230 |
$res = $wp_filesystem->rmdir( $file, $recursive ); |
| 231 |
} elseif ( $action == 'dirlist' ) { |
| 232 |
if ( ! isset( $include_hidden ) ) { |
| 233 |
$include_hidden = true; |
| 234 |
} |
| 235 |
$res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive ); |
| 236 |
} elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) { |
| 237 |
// Write a string to a file |
| 238 |
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { |
| 239 |
self::load_direct(); |
| 240 |
$res = self::$direct->put_contents( $file, $content, $chmod ); |
| 241 |
} else { |
| 242 |
$res = $wp_filesystem->put_contents( $file, $content, $chmod ); |
| 243 |
} |
| 244 |
} elseif ( $action == 'chown' ) { |
| 245 |
// Changes file owner |
| 246 |
if ( isset( $owner ) && ! empty( $owner ) ) { |
| 247 |
$res = $wp_filesystem->chmod( $file, $chmod, $recursive ); |
| 248 |
} |
| 249 |
} elseif ( $action == 'owner' ) { |
| 250 |
// Gets file owner |
| 251 |
$res = $wp_filesystem->owner( $file ); |
| 252 |
} elseif ( $action == 'chmod' ) { |
| 253 |
|
| 254 |
if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) { |
| 255 |
$chmod = false; |
| 256 |
} |
| 257 |
|
| 258 |
$res = $wp_filesystem->chmod( $file, $chmod, $recursive ); |
| 259 |
|
| 260 |
} elseif ( $action == 'get_contents' ) { |
| 261 |
// Reads entire file into a string |
| 262 |
if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) { |
| 263 |
self::load_direct(); |
| 264 |
$res = self::$direct->get_contents( $file ); |
| 265 |
} else { |
| 266 |
$res = $wp_filesystem->get_contents( $file ); |
| 267 |
} |
| 268 |
} elseif ( $action == 'get_contents_array' ) { |
| 269 |
// Reads entire file into an array |
| 270 |
$res = $wp_filesystem->get_contents_array( $file ); |
| 271 |
} elseif ( $action == 'object' ) { |
| 272 |
$res = $wp_filesystem; |
| 273 |
} elseif ( $action == 'unzip' ) { |
| 274 |
$unzipfile = unzip_file( $file, $destination ); |
| 275 |
if ( $unzipfile ) { |
| 276 |
$res = true; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
if ( ! $res ) { |
| 281 |
if ( $action == 'dirlist' ) { |
| 282 |
if ( empty( $res ) || $res == false || $res == '' ) { |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
if ( is_array( $res ) && empty( $res ) ) { |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
if ( ! is_array( $res ) ) { |
| 291 |
if ( count( glob( "$file*" ) ) == 0 ) { |
| 292 |
return; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
$this->killswitch = true; |
| 298 |
|
| 299 |
$msg = '<strong>' . __( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1$1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2$2s" target="_blank">outlined here</a>.', 'redux-framework' ), ReduxLegacy_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' ); |
| 300 |
|
| 301 |
$data = array( |
| 302 |
'parent' => self::$instance->parent, |
| 303 |
'type' => 'error', |
| 304 |
'msg' => $msg, |
| 305 |
'id' => 'redux-wp-login', |
| 306 |
'dismiss' => false, |
| 307 |
); |
| 308 |
|
| 309 |
ReduxLegacy_Admin_Notices::set_notice( $data ); |
| 310 |
} |
| 311 |
|
| 312 |
return $res; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
ReduxLegacy_Filesystem::get_instance(); |
| 317 |
} |
| 318 |
|