PluginProbe
QuadMenu – Mega Menu / 1.8.9
QuadMenu – Mega Menu v1.8.9
3.3.8 3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 88 releases
quadmenu / lib / ReduxCore / inc / class.redux_filesystem.php

class.redux_filesystem.php in QuadMenu – Mega Menu 1.8.9, at lib/ReduxCore/inc/class.redux_filesystem.php

308 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 if ( ! class_exists( 'Redux_Filesystem' ) ) {
9 class Redux_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>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'redux-framework' ), Redux_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( ReduxFramework::$_upload_dir ) ) {
128 $this->do_action( 'mkdir', ReduxFramework::$_upload_dir );
129 }
130
131 $hash_path = trailingslashit( ReduxFramework::$_upload_dir ) . 'hash';
132 if ( ! file_exists( $hash_path ) ) {
133 $this->do_action( 'put_contents', $hash_path, array(
134 'content' => md5( network_site_url() . '-' . $_SERVER['REMOTE_ADDR'] )
135 )
136 );
137 }
138 $version_path = trailingslashit( ReduxFramework::$_upload_dir ) . 'version';
139 if ( ! file_exists( $version_path ) ) {
140 $this->do_action( 'put_contents', $version_path, array(
141 'content' => ReduxFramework::$_version
142 )
143 );
144 }
145
146 $index_path = trailingslashit( ReduxFramework::$_upload_dir ) . 'index.php';
147 if ( ! file_exists( $index_path ) ) {
148 $this->do_action( 'put_contents', $index_path, array(
149 'content' => '<?php' . PHP_EOL . '// Silence is golden.'
150 ) );
151 }
152
153 return $this->do_action( $action, $file, $params );
154 }
155
156 public function do_action( $action, $file = '', $params = '' ) {
157
158 if ( ! empty ( $params ) ) {
159 extract( $params );
160 }
161
162 global $wp_filesystem;
163
164 if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
165 if ( defined( 'FS_CHMOD_FILE' ) ) {
166 $chmod = FS_CHMOD_FILE;
167 } else {
168 $chmod = 0644;
169 }
170 }
171 $res = false;
172 if ( ! isset( $recursive ) ) {
173 $recursive = false;
174 }
175
176 //$target_dir = $wp_filesystem->find_folder( dirname( $file ) );
177
178 // Do unique stuff
179 if ( $action == 'mkdir' ) {
180
181 if ( defined( 'FS_CHMOD_DIR' ) ) {
182 $chmod = FS_CHMOD_DIR;
183 } else {
184 $chmod = 0755;
185 }
186 $res = $wp_filesystem->mkdir( $file );
187 if ( ! $res ) {
188 wp_mkdir_p( $file );
189
190 $res = file_exists( $file );
191 if ( ! $res ) {
192 mkdir( $file, $chmod, true );
193 $res = file_exists( $file );
194 }
195 }
196 $index_path = trailingslashit( $file ) . 'index.php';
197 if ( ! file_exists( $index_path ) ) {
198 $wp_filesystem->put_contents(
199 $index_path,
200 '<?php' . PHP_EOL . '// Silence is golden.',
201 FS_CHMOD_FILE // predefined mode settings for WP files
202 );
203 }
204 } elseif ( $action == 'rmdir' ) {
205 $res = $wp_filesystem->rmdir( $file, $recursive );
206 } elseif ( $action == 'copy' && ! isset( $this->filesystem->killswitch ) ) {
207 if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
208 $res = copy( $file, $destination );
209 if ( $res ) {
210 chmod( $destination, $chmod );
211 }
212 } else {
213 $res = $wp_filesystem->copy( $file, $destination, $overwrite, $chmod );
214 }
215 } elseif ( $action == 'move' && ! isset( $this->filesystem->killswitch ) ) {
216 $res = $wp_filesystem->copy( $file, $destination, $overwrite );
217 } elseif ( $action == 'delete' ) {
218 $res = $wp_filesystem->delete( $file, $recursive );
219 } elseif ( $action == 'rmdir' ) {
220 $res = $wp_filesystem->rmdir( $file, $recursive );
221 } elseif ( $action == 'dirlist' ) {
222 if ( ! isset( $include_hidden ) ) {
223 $include_hidden = true;
224 }
225 $res = $wp_filesystem->dirlist( $file, $include_hidden, $recursive );
226 } elseif ( $action == 'put_contents' && ! isset( $this->filesystem->killswitch ) ) {
227 // Write a string to a file
228 if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
229 self::load_direct();
230 $res = self::$direct->put_contents( $file, $content, $chmod );
231 } else {
232 $res = $wp_filesystem->put_contents( $file, $content, $chmod );
233 }
234 } elseif ( $action == 'chown' ) {
235 // Changes file owner
236 if ( isset( $owner ) && ! empty( $owner ) ) {
237 $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
238 }
239 } elseif ( $action == 'owner' ) {
240 // Gets file owner
241 $res = $wp_filesystem->owner( $file );
242 } elseif ( $action == 'chmod' ) {
243
244 if ( ! isset( $params['chmod'] ) || ( isset( $params['chmod'] ) && empty( $params['chmod'] ) ) ) {
245 $chmod = false;
246 }
247
248 $res = $wp_filesystem->chmod( $file, $chmod, $recursive );
249
250 } elseif ( $action == 'get_contents' ) {
251 // Reads entire file into a string
252 if ( isset( $this->parent->ftp_form ) && ! empty( $this->parent->ftp_form ) ) {
253 self::load_direct();
254 $res = self::$direct->get_contents( $file );
255 } else {
256 $res = $wp_filesystem->get_contents( $file );
257 }
258 } elseif ( $action == 'get_contents_array' ) {
259 // Reads entire file into an array
260 $res = $wp_filesystem->get_contents_array( $file );
261 } elseif ( $action == 'object' ) {
262 $res = $wp_filesystem;
263 } elseif ( $action == 'unzip' ) {
264 $unzipfile = unzip_file( $file, $destination );
265 if ( $unzipfile ) {
266 $res = true;
267 }
268 }
269
270 if ( ! $res ) {
271 if ( $action == 'dirlist' ) {
272 if ( empty( $res ) || $res == false || $res == '' ) {
273 return;
274 }
275
276 if ( is_array( $res ) && empty( $res ) ) {
277 return;
278 }
279
280 if ( ! is_array( $res ) ) {
281 if ( count( glob( "$file*" ) ) == 0 ) {
282 return;
283 }
284 }
285 }
286
287 $this->killswitch = true;
288
289 $msg = '<strong>' . __( 'File Permission Issues', 'redux-framework' ) . '</strong><br/>' . sprintf( __( 'We were unable to modify required files. Please ensure that <code>%1s</code> has the proper read-write permissions, or modify your wp-config.php file to contain your FTP login credentials as <a href="%2s" target="_blank">outlined here</a>.', 'redux-framework' ), Redux_Helpers::cleanFilePath( trailingslashit( WP_CONTENT_DIR ) ) . '/uploads/', 'https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants' );
290
291 $data = array(
292 'parent' => self::$instance->parent,
293 'type' => 'error',
294 'msg' => $msg,
295 'id' => 'redux-wp-login',
296 'dismiss' => false
297 );
298
299 Redux_Admin_Notices::set_notice($data);
300 }
301
302 return $res;
303 }
304 }
305
306 Redux_Filesystem::get_instance();
307 }
308