@@ -24,666 +24,666 @@ |
||
24 | 24 | class EEH_File extends EEH_Base implements EEHI_File |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @var string $_credentials_form |
|
29 | - */ |
|
30 | - private static $_credentials_form; |
|
31 | - |
|
32 | - protected static $_wp_filesystem_direct; |
|
33 | - |
|
34 | - /** |
|
35 | - * @param string|null $filepath the filepath we want to work in. If its in the |
|
36 | - * wp uploads directory, we'll want to just use the filesystem directly. |
|
37 | - * If not provided, we have to assume its not in the uploads directory |
|
38 | - * @throws EE_Error if filesystem credentials are required |
|
39 | - * @return WP_Filesystem_Base |
|
40 | - */ |
|
41 | - private static function _get_wp_filesystem($filepath = null) |
|
42 | - { |
|
43 | - if (apply_filters( |
|
44 | - 'FHEE__EEH_File___get_wp_filesystem__allow_using_filesystem_direct', |
|
45 | - $filepath && EEH_File::is_in_uploads_folder($filepath), |
|
46 | - $filepath |
|
47 | - ) ) { |
|
48 | - if (! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
49 | - require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
50 | - $method = 'direct'; |
|
51 | - $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); |
|
52 | - // check constants defined, just like in wp-admin/includes/file.php's WP_Filesystem() |
|
53 | - if (! defined('FS_CHMOD_DIR')) { |
|
54 | - define('FS_CHMOD_DIR', ( fileperms(ABSPATH) & 0777 | 0755 )); |
|
55 | - } |
|
56 | - if (! defined('FS_CHMOD_FILE')) { |
|
57 | - define('FS_CHMOD_FILE', ( fileperms(ABSPATH . 'index.php') & 0777 | 0644 )); |
|
58 | - } |
|
59 | - require_once($wp_filesystem_direct_file); |
|
60 | - EEH_File::$_wp_filesystem_direct = new WP_Filesystem_Direct(array()); |
|
61 | - } |
|
62 | - return EEH_File::$_wp_filesystem_direct; |
|
63 | - } |
|
64 | - global $wp_filesystem; |
|
65 | - // no filesystem setup ??? |
|
66 | - if (! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
67 | - // if some eager beaver's just trying to get in there too early... |
|
68 | - // let them do it, because we are one of those eager beavers! :P |
|
69 | - /** |
|
70 | - * more explanations are probably merited. http://codex.wordpress.org/Filesystem_API#Initializing_WP_Filesystem_Base |
|
71 | - * says WP_Filesystem should be used after 'wp_loaded', but currently EE's activation process |
|
72 | - * is setup to mostly happen on 'init', and refactoring to have it happen on |
|
73 | - * 'wp_loaded' is too much work on a BETA milestone. |
|
74 | - * So this fix is expected to work if the WP files are owned by the server user, |
|
75 | - * but probably not if the user needs to enter their FTP credentials to modify files |
|
76 | - * and there may be troubles if the WP files are owned by a different user |
|
77 | - * than the server user. But both of these issues should exist in 4.4 and earlier too |
|
78 | - */ |
|
79 | - if (false && ! did_action('wp_loaded')) { |
|
80 | - $msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'); |
|
81 | - if (WP_DEBUG) { |
|
82 | - $msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
83 | - } |
|
84 | - throw new EE_Error($msg); |
|
85 | - } else { |
|
86 | - // should be loaded if we are past the wp_loaded hook... |
|
87 | - if (! function_exists('WP_Filesystem')) { |
|
88 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
89 | - require_once(ABSPATH . 'wp-admin/includes/template.php'); |
|
90 | - } |
|
91 | - // turn on output buffering so that we can capture the credentials form |
|
92 | - ob_start(); |
|
93 | - $credentials = request_filesystem_credentials(''); |
|
94 | - // store credentials form for the time being |
|
95 | - EEH_File::$_credentials_form = ob_get_clean(); |
|
96 | - // basically check for direct or previously configured access |
|
97 | - if (! WP_Filesystem($credentials)) { |
|
98 | - // if credentials do NOT exist |
|
99 | - if ($credentials === false) { |
|
100 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
101 | - throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso')); |
|
102 | - } elseif (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
|
103 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
104 | - throw new EE_Error( |
|
105 | - sprintf( |
|
106 | - __('WP Filesystem Error: $1%s', 'event_espresso'), |
|
107 | - $wp_filesystem->errors->get_error_message() |
|
108 | - ) |
|
109 | - ); |
|
110 | - } |
|
111 | - } |
|
112 | - } |
|
113 | - } |
|
114 | - return $wp_filesystem; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * display_request_filesystem_credentials_form |
|
119 | - */ |
|
120 | - public static function display_request_filesystem_credentials_form() |
|
121 | - { |
|
122 | - if (! empty(EEH_File::$_credentials_form)) { |
|
123 | - echo '<div class="updated espresso-notices-attention"><p>' . EEH_File::$_credentials_form . '</p></div>'; |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * verify_filepath_and_permissions |
|
131 | - * checks that a file is readable and has sufficient file permissions set to access |
|
132 | - * |
|
133 | - * @access public |
|
134 | - * @param string $full_file_path - full server path to the folder or file |
|
135 | - * @param string $file_name - name of file if checking a file |
|
136 | - * @param string $file_ext - file extension (ie: "php") if checking a file |
|
137 | - * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
138 | - * @throws EE_Error if filesystem credentials are required |
|
139 | - * @return bool |
|
140 | - */ |
|
141 | - public static function verify_filepath_and_permissions($full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '') |
|
142 | - { |
|
143 | - // load WP_Filesystem and set file permissions |
|
144 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
145 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
146 | - if (! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
147 | - $file_name = ! empty($type_of_file) ? $file_name . ' ' . $type_of_file : $file_name; |
|
148 | - $file_name .= ! empty($file_ext) ? ' file' : ' folder'; |
|
149 | - $msg = sprintf( |
|
150 | - __('The requested %1$s could not be found or is not readable, possibly due to an incorrect filepath, or incorrect file permissions.%2$s', 'event_espresso'), |
|
151 | - $file_name, |
|
152 | - '<br />' |
|
153 | - ); |
|
154 | - if (EEH_File::exists($full_file_path)) { |
|
155 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, $type_of_file); |
|
156 | - } else { |
|
157 | - // no file permissions means the file was not found |
|
158 | - $msg .= sprintf( |
|
159 | - __('Please ensure the following path is correct: "%s".', 'event_espresso'), |
|
160 | - $full_file_path |
|
161 | - ); |
|
162 | - } |
|
163 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
164 | - throw new EE_Error($msg . '||' . $msg); |
|
165 | - } |
|
166 | - return false; |
|
167 | - } |
|
168 | - return true; |
|
169 | - } |
|
170 | - |
|
171 | - |
|
172 | - |
|
173 | - /** |
|
174 | - * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
|
175 | - * |
|
176 | - * @access private |
|
177 | - * @param string $full_file_path - full server path to the folder or file |
|
178 | - * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
179 | - * @throws EE_Error if filesystem credentials are required |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - private static function _permissions_error_for_unreadable_filepath($full_file_path = '', $type_of_file = '') |
|
183 | - { |
|
184 | - // load WP_Filesystem and set file permissions |
|
185 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
186 | - // check file permissions |
|
187 | - $perms = $wp_filesystem->getchmod(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
188 | - if ($perms) { |
|
189 | - // file permissions exist, but way be set incorrectly |
|
190 | - $type_of_file = ! empty($type_of_file) ? $type_of_file . ' ' : ''; |
|
191 | - $type_of_file .= ! empty($type_of_file) ? 'file' : 'folder'; |
|
192 | - return sprintf( |
|
193 | - __('File permissions for the requested %1$s are currently set at "%2$s". The recommended permissions are 644 for files and 755 for folders.', 'event_espresso'), |
|
194 | - $type_of_file, |
|
195 | - $perms |
|
196 | - ); |
|
197 | - } else { |
|
198 | - // file exists but file permissions could not be read ?!?! |
|
199 | - return sprintf( |
|
200 | - __('Please ensure that the server and/or PHP configuration allows the current process to access the following file: "%s".', 'event_espresso'), |
|
201 | - $full_file_path |
|
202 | - ); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * ensure_folder_exists_and_is_writable |
|
210 | - * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
|
211 | - * Also ensures all the parent folders exist, and if not tries to create them. |
|
212 | - * Also, if this function creates the folder, adds a .htaccess file and index.html file |
|
213 | - * @param string $folder |
|
214 | - * @throws EE_Error if the folder exists and is writeable, but for some reason we |
|
215 | - * can't write to it |
|
216 | - * @return bool false if folder isn't writable; true if it exists and is writeable, |
|
217 | - */ |
|
218 | - public static function ensure_folder_exists_and_is_writable($folder = '') |
|
219 | - { |
|
220 | - if (empty($folder)) { |
|
221 | - return false; |
|
222 | - } |
|
223 | - // remove ending / |
|
224 | - $folder = EEH_File::standardise_directory_separators(rtrim($folder, '/\\')); |
|
225 | - $parent_folder = EEH_File::get_parent_folder($folder); |
|
226 | - // add / to folder |
|
227 | - $folder = EEH_File::end_with_directory_separator($folder); |
|
228 | - $wp_filesystem = EEH_File::_get_wp_filesystem($folder); |
|
229 | - if (! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
230 | - // ok so it doesn't exist. Does its parent? Can we write to it? |
|
231 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
232 | - return false; |
|
233 | - } |
|
234 | - if (! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
235 | - return false; |
|
236 | - } else { |
|
237 | - if (! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
238 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
239 | - $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); |
|
240 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); |
|
241 | - throw new EE_Error($msg); |
|
242 | - } |
|
243 | - return false; |
|
244 | - } |
|
245 | - EEH_File::add_index_file($folder); |
|
246 | - } |
|
247 | - } elseif (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
248 | - return false; |
|
249 | - } |
|
250 | - return true; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - |
|
255 | - /** |
|
256 | - * verify_is_writable - checks if a file or folder is writable |
|
257 | - * @param string $full_path - full server path to file or folder |
|
258 | - * @param string $file_or_folder - whether checking a file or folder |
|
259 | - * @throws EE_Error if filesystem credentials are required |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public static function verify_is_writable($full_path = '', $file_or_folder = 'folder') |
|
263 | - { |
|
264 | - // load WP_Filesystem and set file permissions |
|
265 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_path); |
|
266 | - $full_path = EEH_File::standardise_directory_separators($full_path); |
|
267 | - if (! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
268 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
269 | - $msg = sprintf(__('The "%1$s" %2$s is not writable.', 'event_espresso'), $full_path, $file_or_folder); |
|
270 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_path); |
|
271 | - throw new EE_Error($msg); |
|
272 | - } |
|
273 | - return false; |
|
274 | - } |
|
275 | - return true; |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - |
|
280 | - /** |
|
281 | - * ensure_file_exists_and_is_writable |
|
282 | - * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
|
283 | - * Also ensures all the parent folders exist, and if not tries to create them. |
|
284 | - * @param string $full_file_path |
|
285 | - * @throws EE_Error if filesystem credentials are required |
|
286 | - * @return bool |
|
287 | - */ |
|
288 | - public static function ensure_file_exists_and_is_writable($full_file_path = '') |
|
289 | - { |
|
290 | - // load WP_Filesystem and set file permissions |
|
291 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
292 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
293 | - $parent_folder = EEH_File::get_parent_folder($full_file_path); |
|
294 | - if (! EEH_File::exists($full_file_path)) { |
|
295 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
296 | - return false; |
|
297 | - } |
|
298 | - if (! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
299 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
300 | - $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); |
|
301 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
302 | - throw new EE_Error($msg); |
|
303 | - } |
|
304 | - return false; |
|
305 | - } |
|
306 | - } |
|
307 | - if (! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
308 | - return false; |
|
309 | - } |
|
310 | - return true; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Gets the parent folder. If provided with file, gets the folder that contains it. |
|
315 | - * If provided a folder, gets its parent folder. |
|
316 | - * @param string $file_or_folder_path |
|
317 | - * @return string parent folder, ENDING with a directory separator |
|
318 | - */ |
|
319 | - public static function get_parent_folder($file_or_folder_path) |
|
320 | - { |
|
321 | - // find the last /, ignoring a / on the very end |
|
322 | - // eg if given "/var/something/somewhere/", we want to get "somewhere"'s |
|
323 | - // parent folder, "/var/something/" |
|
324 | - $ds = strlen($file_or_folder_path) > 1 |
|
325 | - ? strrpos($file_or_folder_path, '/', -2) |
|
326 | - : strlen($file_or_folder_path); |
|
327 | - return substr($file_or_folder_path, 0, $ds + 1); |
|
328 | - } |
|
329 | - |
|
330 | - // public static function ensure_folder_exists_recursively( $folder ) { |
|
331 | - // |
|
332 | - // } |
|
333 | - |
|
334 | - |
|
335 | - |
|
336 | - /** |
|
337 | - * get_file_contents |
|
338 | - * @param string $full_file_path |
|
339 | - * @throws EE_Error if filesystem credentials are required |
|
340 | - * @return string |
|
341 | - */ |
|
342 | - public static function get_file_contents($full_file_path = '') |
|
343 | - { |
|
344 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
345 | - if (EEH_File::verify_filepath_and_permissions($full_file_path, EEH_File::get_filename_from_filepath($full_file_path), EEH_File::get_file_extension($full_file_path))) { |
|
346 | - // load WP_Filesystem and set file permissions |
|
347 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
348 | - return $wp_filesystem->get_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
349 | - } |
|
350 | - return ''; |
|
351 | - } |
|
352 | - |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * write_file |
|
357 | - * @param string $full_file_path |
|
358 | - * @param string $file_contents - the content to be written to the file |
|
359 | - * @param string $file_type |
|
360 | - * @throws EE_Error if filesystem credentials are required |
|
361 | - * @return bool |
|
362 | - */ |
|
363 | - public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
|
364 | - { |
|
365 | - $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
366 | - $file_type = ! empty($file_type) ? rtrim($file_type, ' ') . ' ' : ''; |
|
367 | - $folder = EEH_File::remove_filename_from_filepath($full_file_path); |
|
368 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
369 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
370 | - $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path); |
|
371 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
372 | - throw new EE_Error($msg); |
|
373 | - } |
|
374 | - return false; |
|
375 | - } |
|
376 | - // load WP_Filesystem and set file permissions |
|
377 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
378 | - // write the file |
|
379 | - if (! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
380 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
381 | - $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path); |
|
382 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f'); |
|
383 | - throw new EE_Error($msg); |
|
384 | - } |
|
385 | - return false; |
|
386 | - } |
|
387 | - return true; |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Wrapper for WP_Filesystem_Base::delete |
|
392 | - * |
|
393 | - * @param string $filepath |
|
394 | - * @param boolean $recursive |
|
395 | - * @param boolean|string $type 'd' for directory, 'f' for file |
|
396 | - * @throws EE_Error if filesystem credentials are required |
|
397 | - * @return boolean |
|
398 | - */ |
|
399 | - public static function delete($filepath, $recursive = false, $type = false) |
|
400 | - { |
|
401 | - $wp_filesystem = EEH_File::_get_wp_filesystem(); |
|
402 | - return $wp_filesystem->delete($filepath, $recursive, $type) ? true : false; |
|
403 | - } |
|
404 | - |
|
405 | - |
|
406 | - |
|
407 | - /** |
|
408 | - * exists |
|
409 | - * checks if a file exists using the WP filesystem |
|
410 | - * @param string $full_file_path |
|
411 | - * @throws EE_Error if filesystem credentials are required |
|
412 | - * @return bool |
|
413 | - */ |
|
414 | - public static function exists($full_file_path = '') |
|
415 | - { |
|
416 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
417 | - return $wp_filesystem->exists(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)) ? true : false; |
|
418 | - } |
|
419 | - |
|
420 | - |
|
421 | - |
|
422 | - /** |
|
423 | - * is_readable |
|
424 | - * checks if a file is_readable using the WP filesystem |
|
425 | - * |
|
426 | - * @param string $full_file_path |
|
427 | - * @throws EE_Error if filesystem credentials are required |
|
428 | - * @return bool |
|
429 | - */ |
|
430 | - public static function is_readable($full_file_path = '') |
|
431 | - { |
|
432 | - $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
433 | - if ($wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
434 | - return true; |
|
435 | - } else { |
|
436 | - return false; |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - |
|
441 | - |
|
442 | - /** |
|
443 | - * remove_filename_from_filepath |
|
444 | - * given a full path to a file including the filename itself, this removes the filename and returns the path, up to, but NOT including the filename OR slash |
|
445 | - * |
|
446 | - * @param string $full_file_path |
|
447 | - * @return string |
|
448 | - */ |
|
449 | - public static function remove_filename_from_filepath($full_file_path = '') |
|
450 | - { |
|
451 | - return pathinfo($full_file_path, PATHINFO_DIRNAME); |
|
452 | - } |
|
453 | - |
|
454 | - |
|
455 | - /** |
|
456 | - * get_filename_from_filepath. Arguably the same as basename() |
|
457 | - * |
|
458 | - * @param string $full_file_path |
|
459 | - * @return string |
|
460 | - */ |
|
461 | - public static function get_filename_from_filepath($full_file_path = '') |
|
462 | - { |
|
463 | - return pathinfo($full_file_path, PATHINFO_BASENAME); |
|
464 | - } |
|
465 | - |
|
466 | - |
|
467 | - /** |
|
468 | - * get_file_extension |
|
469 | - * |
|
470 | - * @param string $full_file_path |
|
471 | - * @return string |
|
472 | - */ |
|
473 | - public static function get_file_extension($full_file_path = '') |
|
474 | - { |
|
475 | - return pathinfo($full_file_path, PATHINFO_EXTENSION); |
|
476 | - } |
|
477 | - |
|
478 | - |
|
479 | - |
|
480 | - /** |
|
481 | - * add_htaccess_deny_from_all so the webserver cannot access this folder |
|
482 | - * @param string $folder |
|
483 | - * @throws EE_Error if filesystem credentials are required |
|
484 | - * @return bool |
|
485 | - */ |
|
486 | - public static function add_htaccess_deny_from_all($folder = '') |
|
487 | - { |
|
488 | - $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
489 | - if (! EEH_File::exists($folder . '.htaccess')) { |
|
490 | - if (! EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { |
|
491 | - return false; |
|
492 | - } |
|
493 | - } |
|
494 | - |
|
495 | - return true; |
|
496 | - } |
|
497 | - |
|
498 | - /** |
|
499 | - * Adds an index file to this folder, so folks can't list all the file's contents |
|
500 | - * @param string $folder |
|
501 | - * @throws EE_Error if filesystem credentials are required |
|
502 | - * @return boolean |
|
503 | - */ |
|
504 | - public static function add_index_file($folder) |
|
505 | - { |
|
506 | - $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
507 | - if (! EEH_File::exists($folder . 'index.php')) { |
|
508 | - if (! EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
509 | - return false; |
|
510 | - } |
|
511 | - } |
|
512 | - return true; |
|
513 | - } |
|
514 | - |
|
515 | - |
|
516 | - |
|
517 | - /** |
|
518 | - * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
|
519 | - * extract that classname. |
|
520 | - * @param string $file_path |
|
521 | - * @return string |
|
522 | - */ |
|
523 | - public static function get_classname_from_filepath_with_standard_filename($file_path) |
|
524 | - { |
|
525 | - // extract file from path |
|
526 | - $filename = basename($file_path); |
|
527 | - // now remove the first period and everything after |
|
528 | - $pos_of_first_period = strpos($filename, '.'); |
|
529 | - return substr($filename, 0, $pos_of_first_period); |
|
530 | - } |
|
531 | - |
|
532 | - |
|
533 | - |
|
534 | - /** |
|
535 | - * standardise_directory_separators |
|
536 | - * convert all directory separators in a file path. |
|
537 | - * @param string $file_path |
|
538 | - * @return string |
|
539 | - */ |
|
540 | - public static function standardise_directory_separators($file_path) |
|
541 | - { |
|
542 | - return str_replace(array( '\\', '/' ), '/', $file_path); |
|
543 | - } |
|
544 | - |
|
545 | - |
|
546 | - |
|
547 | - /** |
|
548 | - * end_with_directory_separator |
|
549 | - * ensures that file path ends with '/' |
|
550 | - * @param string $file_path |
|
551 | - * @return string |
|
552 | - */ |
|
553 | - public static function end_with_directory_separator($file_path) |
|
554 | - { |
|
555 | - return rtrim($file_path, '/\\') . '/'; |
|
556 | - } |
|
557 | - |
|
558 | - |
|
559 | - |
|
560 | - /** |
|
561 | - * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
|
562 | - * @param $file_path |
|
563 | - * @return string |
|
564 | - */ |
|
565 | - public static function standardise_and_end_with_directory_separator($file_path) |
|
566 | - { |
|
567 | - return self::end_with_directory_separator(self::standardise_directory_separators($file_path)); |
|
568 | - } |
|
569 | - |
|
570 | - |
|
571 | - |
|
572 | - /** |
|
573 | - * takes the folder name (with or without trailing slash) and finds the files it in, |
|
574 | - * and what the class's name inside of each should be. |
|
575 | - * @param array $folder_paths |
|
576 | - * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
|
577 | - * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
|
578 | - * Set it TRUE if you know there are files in the directory with the same name but different extensions |
|
579 | - * @throws EE_Error if filesystem credentials are required |
|
580 | - * @return array if $index_numerically == TRUE keys are numeric , |
|
581 | - * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
|
582 | - * and values are their filepaths |
|
583 | - */ |
|
584 | - public static function get_contents_of_folders($folder_paths = array(), $index_numerically = false) |
|
585 | - { |
|
586 | - $class_to_folder_path = array(); |
|
587 | - foreach ($folder_paths as $folder_path) { |
|
588 | - $folder_path = self::standardise_and_end_with_directory_separator($folder_path); |
|
589 | - // load WP_Filesystem and set file permissions |
|
590 | - $files_in_folder = glob($folder_path . '*.php'); |
|
591 | - $class_to_folder_path = array(); |
|
592 | - if ($files_in_folder) { |
|
593 | - foreach ($files_in_folder as $file_path) { |
|
594 | - // only add files, not folders |
|
595 | - if (! is_dir($file_path)) { |
|
596 | - if ($index_numerically) { |
|
597 | - $class_to_folder_path[] = $file_path; |
|
598 | - } else { |
|
599 | - $classname = self::get_classname_from_filepath_with_standard_filename($file_path); |
|
600 | - $class_to_folder_path[ $classname ] = $file_path; |
|
601 | - } |
|
602 | - } |
|
603 | - } |
|
604 | - } |
|
605 | - } |
|
606 | - return $class_to_folder_path; |
|
607 | - } |
|
608 | - |
|
609 | - |
|
610 | - |
|
611 | - /** |
|
612 | - * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
|
613 | - * @param string $source_file |
|
614 | - * @param string $destination_file |
|
615 | - * @param boolean $overwrite |
|
616 | - * @throws EE_Error if filesystem credentials are required |
|
617 | - * @return boolean success |
|
618 | - */ |
|
619 | - public static function copy($source_file, $destination_file, $overwrite = false) |
|
620 | - { |
|
621 | - $full_source_path = EEH_File::standardise_directory_separators($source_file); |
|
622 | - if (! EEH_File::exists($full_source_path)) { |
|
623 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
624 | - $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); |
|
625 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); |
|
626 | - throw new EE_Error($msg); |
|
627 | - } |
|
628 | - return false; |
|
629 | - } |
|
630 | - |
|
631 | - $full_dest_path = EEH_File::standardise_directory_separators($destination_file); |
|
632 | - $folder = EEH_File::remove_filename_from_filepath($full_dest_path); |
|
633 | - EEH_File::ensure_folder_exists_and_is_writable($folder); |
|
634 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
635 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
636 | - $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); |
|
637 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); |
|
638 | - throw new EE_Error($msg); |
|
639 | - } |
|
640 | - return false; |
|
641 | - } |
|
642 | - |
|
643 | - // load WP_Filesystem and set file permissions |
|
644 | - $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file); |
|
645 | - // write the file |
|
646 | - if (! $wp_filesystem->copy( |
|
647 | - EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), |
|
648 | - EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), |
|
649 | - $overwrite |
|
650 | - )) { |
|
651 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
652 | - $msg = sprintf(__('Attempted writing to file %1$s, but could not, probably because of permissions issues', 'event_espresso'), $full_source_path); |
|
653 | - $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path, 'f'); |
|
654 | - throw new EE_Error($msg); |
|
655 | - } |
|
656 | - return false; |
|
657 | - } |
|
658 | - return true; |
|
659 | - } |
|
660 | - |
|
661 | - /** |
|
662 | - * Reports whether or not the filepath is in the EE uploads folder or not |
|
663 | - * @param string $filepath |
|
664 | - * @return boolean |
|
665 | - */ |
|
666 | - public static function is_in_uploads_folder($filepath) |
|
667 | - { |
|
668 | - $uploads = wp_upload_dir(); |
|
669 | - return strpos($filepath, $uploads['basedir']) === 0 ? true : false; |
|
670 | - } |
|
671 | - |
|
672 | - /** |
|
673 | - * Given a "local" filepath (what you probably thought was the only filepath), |
|
674 | - * converts it into a "remote" filepath (the filepath the currently-in-use |
|
675 | - * $wp_filesystem needs to use access the folder or file). |
|
676 | - * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
|
677 | - * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
|
678 | - * is in use, so you need to provide it |
|
679 | - * @param string $local_filepath the filepath to the folder/file locally |
|
680 | - * @throws EE_Error if filesystem credentials are required |
|
681 | - * @return string the remote filepath (eg the filepath the filesystem method, eg |
|
682 | - * ftp or ssh, will use to access the folder |
|
683 | - */ |
|
684 | - public static function convert_local_filepath_to_remote_filepath($local_filepath) |
|
685 | - { |
|
686 | - $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); |
|
687 | - return str_replace(WP_CONTENT_DIR . '/', $wp_filesystem->wp_content_dir(), $local_filepath); |
|
688 | - } |
|
27 | + /** |
|
28 | + * @var string $_credentials_form |
|
29 | + */ |
|
30 | + private static $_credentials_form; |
|
31 | + |
|
32 | + protected static $_wp_filesystem_direct; |
|
33 | + |
|
34 | + /** |
|
35 | + * @param string|null $filepath the filepath we want to work in. If its in the |
|
36 | + * wp uploads directory, we'll want to just use the filesystem directly. |
|
37 | + * If not provided, we have to assume its not in the uploads directory |
|
38 | + * @throws EE_Error if filesystem credentials are required |
|
39 | + * @return WP_Filesystem_Base |
|
40 | + */ |
|
41 | + private static function _get_wp_filesystem($filepath = null) |
|
42 | + { |
|
43 | + if (apply_filters( |
|
44 | + 'FHEE__EEH_File___get_wp_filesystem__allow_using_filesystem_direct', |
|
45 | + $filepath && EEH_File::is_in_uploads_folder($filepath), |
|
46 | + $filepath |
|
47 | + ) ) { |
|
48 | + if (! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
49 | + require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
50 | + $method = 'direct'; |
|
51 | + $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); |
|
52 | + // check constants defined, just like in wp-admin/includes/file.php's WP_Filesystem() |
|
53 | + if (! defined('FS_CHMOD_DIR')) { |
|
54 | + define('FS_CHMOD_DIR', ( fileperms(ABSPATH) & 0777 | 0755 )); |
|
55 | + } |
|
56 | + if (! defined('FS_CHMOD_FILE')) { |
|
57 | + define('FS_CHMOD_FILE', ( fileperms(ABSPATH . 'index.php') & 0777 | 0644 )); |
|
58 | + } |
|
59 | + require_once($wp_filesystem_direct_file); |
|
60 | + EEH_File::$_wp_filesystem_direct = new WP_Filesystem_Direct(array()); |
|
61 | + } |
|
62 | + return EEH_File::$_wp_filesystem_direct; |
|
63 | + } |
|
64 | + global $wp_filesystem; |
|
65 | + // no filesystem setup ??? |
|
66 | + if (! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
67 | + // if some eager beaver's just trying to get in there too early... |
|
68 | + // let them do it, because we are one of those eager beavers! :P |
|
69 | + /** |
|
70 | + * more explanations are probably merited. http://codex.wordpress.org/Filesystem_API#Initializing_WP_Filesystem_Base |
|
71 | + * says WP_Filesystem should be used after 'wp_loaded', but currently EE's activation process |
|
72 | + * is setup to mostly happen on 'init', and refactoring to have it happen on |
|
73 | + * 'wp_loaded' is too much work on a BETA milestone. |
|
74 | + * So this fix is expected to work if the WP files are owned by the server user, |
|
75 | + * but probably not if the user needs to enter their FTP credentials to modify files |
|
76 | + * and there may be troubles if the WP files are owned by a different user |
|
77 | + * than the server user. But both of these issues should exist in 4.4 and earlier too |
|
78 | + */ |
|
79 | + if (false && ! did_action('wp_loaded')) { |
|
80 | + $msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'); |
|
81 | + if (WP_DEBUG) { |
|
82 | + $msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
83 | + } |
|
84 | + throw new EE_Error($msg); |
|
85 | + } else { |
|
86 | + // should be loaded if we are past the wp_loaded hook... |
|
87 | + if (! function_exists('WP_Filesystem')) { |
|
88 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
89 | + require_once(ABSPATH . 'wp-admin/includes/template.php'); |
|
90 | + } |
|
91 | + // turn on output buffering so that we can capture the credentials form |
|
92 | + ob_start(); |
|
93 | + $credentials = request_filesystem_credentials(''); |
|
94 | + // store credentials form for the time being |
|
95 | + EEH_File::$_credentials_form = ob_get_clean(); |
|
96 | + // basically check for direct or previously configured access |
|
97 | + if (! WP_Filesystem($credentials)) { |
|
98 | + // if credentials do NOT exist |
|
99 | + if ($credentials === false) { |
|
100 | + add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
101 | + throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso')); |
|
102 | + } elseif (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
|
103 | + add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
104 | + throw new EE_Error( |
|
105 | + sprintf( |
|
106 | + __('WP Filesystem Error: $1%s', 'event_espresso'), |
|
107 | + $wp_filesystem->errors->get_error_message() |
|
108 | + ) |
|
109 | + ); |
|
110 | + } |
|
111 | + } |
|
112 | + } |
|
113 | + } |
|
114 | + return $wp_filesystem; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * display_request_filesystem_credentials_form |
|
119 | + */ |
|
120 | + public static function display_request_filesystem_credentials_form() |
|
121 | + { |
|
122 | + if (! empty(EEH_File::$_credentials_form)) { |
|
123 | + echo '<div class="updated espresso-notices-attention"><p>' . EEH_File::$_credentials_form . '</p></div>'; |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * verify_filepath_and_permissions |
|
131 | + * checks that a file is readable and has sufficient file permissions set to access |
|
132 | + * |
|
133 | + * @access public |
|
134 | + * @param string $full_file_path - full server path to the folder or file |
|
135 | + * @param string $file_name - name of file if checking a file |
|
136 | + * @param string $file_ext - file extension (ie: "php") if checking a file |
|
137 | + * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
138 | + * @throws EE_Error if filesystem credentials are required |
|
139 | + * @return bool |
|
140 | + */ |
|
141 | + public static function verify_filepath_and_permissions($full_file_path = '', $file_name = '', $file_ext = '', $type_of_file = '') |
|
142 | + { |
|
143 | + // load WP_Filesystem and set file permissions |
|
144 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
145 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
146 | + if (! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
147 | + $file_name = ! empty($type_of_file) ? $file_name . ' ' . $type_of_file : $file_name; |
|
148 | + $file_name .= ! empty($file_ext) ? ' file' : ' folder'; |
|
149 | + $msg = sprintf( |
|
150 | + __('The requested %1$s could not be found or is not readable, possibly due to an incorrect filepath, or incorrect file permissions.%2$s', 'event_espresso'), |
|
151 | + $file_name, |
|
152 | + '<br />' |
|
153 | + ); |
|
154 | + if (EEH_File::exists($full_file_path)) { |
|
155 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, $type_of_file); |
|
156 | + } else { |
|
157 | + // no file permissions means the file was not found |
|
158 | + $msg .= sprintf( |
|
159 | + __('Please ensure the following path is correct: "%s".', 'event_espresso'), |
|
160 | + $full_file_path |
|
161 | + ); |
|
162 | + } |
|
163 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
164 | + throw new EE_Error($msg . '||' . $msg); |
|
165 | + } |
|
166 | + return false; |
|
167 | + } |
|
168 | + return true; |
|
169 | + } |
|
170 | + |
|
171 | + |
|
172 | + |
|
173 | + /** |
|
174 | + * _permissions_error_for_unreadable_filepath - attempts to determine why permissions are set incorrectly for a file or folder |
|
175 | + * |
|
176 | + * @access private |
|
177 | + * @param string $full_file_path - full server path to the folder or file |
|
178 | + * @param string $type_of_file - general type of file (ie: "module"), this is only used to improve error messages |
|
179 | + * @throws EE_Error if filesystem credentials are required |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + private static function _permissions_error_for_unreadable_filepath($full_file_path = '', $type_of_file = '') |
|
183 | + { |
|
184 | + // load WP_Filesystem and set file permissions |
|
185 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
186 | + // check file permissions |
|
187 | + $perms = $wp_filesystem->getchmod(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
188 | + if ($perms) { |
|
189 | + // file permissions exist, but way be set incorrectly |
|
190 | + $type_of_file = ! empty($type_of_file) ? $type_of_file . ' ' : ''; |
|
191 | + $type_of_file .= ! empty($type_of_file) ? 'file' : 'folder'; |
|
192 | + return sprintf( |
|
193 | + __('File permissions for the requested %1$s are currently set at "%2$s". The recommended permissions are 644 for files and 755 for folders.', 'event_espresso'), |
|
194 | + $type_of_file, |
|
195 | + $perms |
|
196 | + ); |
|
197 | + } else { |
|
198 | + // file exists but file permissions could not be read ?!?! |
|
199 | + return sprintf( |
|
200 | + __('Please ensure that the server and/or PHP configuration allows the current process to access the following file: "%s".', 'event_espresso'), |
|
201 | + $full_file_path |
|
202 | + ); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * ensure_folder_exists_and_is_writable |
|
210 | + * ensures that a folder exists and is writable, will attempt to create folder if it does not exist |
|
211 | + * Also ensures all the parent folders exist, and if not tries to create them. |
|
212 | + * Also, if this function creates the folder, adds a .htaccess file and index.html file |
|
213 | + * @param string $folder |
|
214 | + * @throws EE_Error if the folder exists and is writeable, but for some reason we |
|
215 | + * can't write to it |
|
216 | + * @return bool false if folder isn't writable; true if it exists and is writeable, |
|
217 | + */ |
|
218 | + public static function ensure_folder_exists_and_is_writable($folder = '') |
|
219 | + { |
|
220 | + if (empty($folder)) { |
|
221 | + return false; |
|
222 | + } |
|
223 | + // remove ending / |
|
224 | + $folder = EEH_File::standardise_directory_separators(rtrim($folder, '/\\')); |
|
225 | + $parent_folder = EEH_File::get_parent_folder($folder); |
|
226 | + // add / to folder |
|
227 | + $folder = EEH_File::end_with_directory_separator($folder); |
|
228 | + $wp_filesystem = EEH_File::_get_wp_filesystem($folder); |
|
229 | + if (! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
230 | + // ok so it doesn't exist. Does its parent? Can we write to it? |
|
231 | + if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
232 | + return false; |
|
233 | + } |
|
234 | + if (! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
235 | + return false; |
|
236 | + } else { |
|
237 | + if (! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
238 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
239 | + $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); |
|
240 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); |
|
241 | + throw new EE_Error($msg); |
|
242 | + } |
|
243 | + return false; |
|
244 | + } |
|
245 | + EEH_File::add_index_file($folder); |
|
246 | + } |
|
247 | + } elseif (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
248 | + return false; |
|
249 | + } |
|
250 | + return true; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + |
|
255 | + /** |
|
256 | + * verify_is_writable - checks if a file or folder is writable |
|
257 | + * @param string $full_path - full server path to file or folder |
|
258 | + * @param string $file_or_folder - whether checking a file or folder |
|
259 | + * @throws EE_Error if filesystem credentials are required |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public static function verify_is_writable($full_path = '', $file_or_folder = 'folder') |
|
263 | + { |
|
264 | + // load WP_Filesystem and set file permissions |
|
265 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_path); |
|
266 | + $full_path = EEH_File::standardise_directory_separators($full_path); |
|
267 | + if (! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
268 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
269 | + $msg = sprintf(__('The "%1$s" %2$s is not writable.', 'event_espresso'), $full_path, $file_or_folder); |
|
270 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_path); |
|
271 | + throw new EE_Error($msg); |
|
272 | + } |
|
273 | + return false; |
|
274 | + } |
|
275 | + return true; |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + |
|
280 | + /** |
|
281 | + * ensure_file_exists_and_is_writable |
|
282 | + * ensures that a file exists and is writable, will attempt to create file if it does not exist. |
|
283 | + * Also ensures all the parent folders exist, and if not tries to create them. |
|
284 | + * @param string $full_file_path |
|
285 | + * @throws EE_Error if filesystem credentials are required |
|
286 | + * @return bool |
|
287 | + */ |
|
288 | + public static function ensure_file_exists_and_is_writable($full_file_path = '') |
|
289 | + { |
|
290 | + // load WP_Filesystem and set file permissions |
|
291 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
292 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
293 | + $parent_folder = EEH_File::get_parent_folder($full_file_path); |
|
294 | + if (! EEH_File::exists($full_file_path)) { |
|
295 | + if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
296 | + return false; |
|
297 | + } |
|
298 | + if (! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
299 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
300 | + $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); |
|
301 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
302 | + throw new EE_Error($msg); |
|
303 | + } |
|
304 | + return false; |
|
305 | + } |
|
306 | + } |
|
307 | + if (! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
308 | + return false; |
|
309 | + } |
|
310 | + return true; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Gets the parent folder. If provided with file, gets the folder that contains it. |
|
315 | + * If provided a folder, gets its parent folder. |
|
316 | + * @param string $file_or_folder_path |
|
317 | + * @return string parent folder, ENDING with a directory separator |
|
318 | + */ |
|
319 | + public static function get_parent_folder($file_or_folder_path) |
|
320 | + { |
|
321 | + // find the last /, ignoring a / on the very end |
|
322 | + // eg if given "/var/something/somewhere/", we want to get "somewhere"'s |
|
323 | + // parent folder, "/var/something/" |
|
324 | + $ds = strlen($file_or_folder_path) > 1 |
|
325 | + ? strrpos($file_or_folder_path, '/', -2) |
|
326 | + : strlen($file_or_folder_path); |
|
327 | + return substr($file_or_folder_path, 0, $ds + 1); |
|
328 | + } |
|
329 | + |
|
330 | + // public static function ensure_folder_exists_recursively( $folder ) { |
|
331 | + // |
|
332 | + // } |
|
333 | + |
|
334 | + |
|
335 | + |
|
336 | + /** |
|
337 | + * get_file_contents |
|
338 | + * @param string $full_file_path |
|
339 | + * @throws EE_Error if filesystem credentials are required |
|
340 | + * @return string |
|
341 | + */ |
|
342 | + public static function get_file_contents($full_file_path = '') |
|
343 | + { |
|
344 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
345 | + if (EEH_File::verify_filepath_and_permissions($full_file_path, EEH_File::get_filename_from_filepath($full_file_path), EEH_File::get_file_extension($full_file_path))) { |
|
346 | + // load WP_Filesystem and set file permissions |
|
347 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
348 | + return $wp_filesystem->get_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
|
349 | + } |
|
350 | + return ''; |
|
351 | + } |
|
352 | + |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * write_file |
|
357 | + * @param string $full_file_path |
|
358 | + * @param string $file_contents - the content to be written to the file |
|
359 | + * @param string $file_type |
|
360 | + * @throws EE_Error if filesystem credentials are required |
|
361 | + * @return bool |
|
362 | + */ |
|
363 | + public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
|
364 | + { |
|
365 | + $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
|
366 | + $file_type = ! empty($file_type) ? rtrim($file_type, ' ') . ' ' : ''; |
|
367 | + $folder = EEH_File::remove_filename_from_filepath($full_file_path); |
|
368 | + if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
369 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
370 | + $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path); |
|
371 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
|
372 | + throw new EE_Error($msg); |
|
373 | + } |
|
374 | + return false; |
|
375 | + } |
|
376 | + // load WP_Filesystem and set file permissions |
|
377 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
378 | + // write the file |
|
379 | + if (! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
380 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
381 | + $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path); |
|
382 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f'); |
|
383 | + throw new EE_Error($msg); |
|
384 | + } |
|
385 | + return false; |
|
386 | + } |
|
387 | + return true; |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Wrapper for WP_Filesystem_Base::delete |
|
392 | + * |
|
393 | + * @param string $filepath |
|
394 | + * @param boolean $recursive |
|
395 | + * @param boolean|string $type 'd' for directory, 'f' for file |
|
396 | + * @throws EE_Error if filesystem credentials are required |
|
397 | + * @return boolean |
|
398 | + */ |
|
399 | + public static function delete($filepath, $recursive = false, $type = false) |
|
400 | + { |
|
401 | + $wp_filesystem = EEH_File::_get_wp_filesystem(); |
|
402 | + return $wp_filesystem->delete($filepath, $recursive, $type) ? true : false; |
|
403 | + } |
|
404 | + |
|
405 | + |
|
406 | + |
|
407 | + /** |
|
408 | + * exists |
|
409 | + * checks if a file exists using the WP filesystem |
|
410 | + * @param string $full_file_path |
|
411 | + * @throws EE_Error if filesystem credentials are required |
|
412 | + * @return bool |
|
413 | + */ |
|
414 | + public static function exists($full_file_path = '') |
|
415 | + { |
|
416 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
417 | + return $wp_filesystem->exists(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)) ? true : false; |
|
418 | + } |
|
419 | + |
|
420 | + |
|
421 | + |
|
422 | + /** |
|
423 | + * is_readable |
|
424 | + * checks if a file is_readable using the WP filesystem |
|
425 | + * |
|
426 | + * @param string $full_file_path |
|
427 | + * @throws EE_Error if filesystem credentials are required |
|
428 | + * @return bool |
|
429 | + */ |
|
430 | + public static function is_readable($full_file_path = '') |
|
431 | + { |
|
432 | + $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
|
433 | + if ($wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
434 | + return true; |
|
435 | + } else { |
|
436 | + return false; |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + |
|
441 | + |
|
442 | + /** |
|
443 | + * remove_filename_from_filepath |
|
444 | + * given a full path to a file including the filename itself, this removes the filename and returns the path, up to, but NOT including the filename OR slash |
|
445 | + * |
|
446 | + * @param string $full_file_path |
|
447 | + * @return string |
|
448 | + */ |
|
449 | + public static function remove_filename_from_filepath($full_file_path = '') |
|
450 | + { |
|
451 | + return pathinfo($full_file_path, PATHINFO_DIRNAME); |
|
452 | + } |
|
453 | + |
|
454 | + |
|
455 | + /** |
|
456 | + * get_filename_from_filepath. Arguably the same as basename() |
|
457 | + * |
|
458 | + * @param string $full_file_path |
|
459 | + * @return string |
|
460 | + */ |
|
461 | + public static function get_filename_from_filepath($full_file_path = '') |
|
462 | + { |
|
463 | + return pathinfo($full_file_path, PATHINFO_BASENAME); |
|
464 | + } |
|
465 | + |
|
466 | + |
|
467 | + /** |
|
468 | + * get_file_extension |
|
469 | + * |
|
470 | + * @param string $full_file_path |
|
471 | + * @return string |
|
472 | + */ |
|
473 | + public static function get_file_extension($full_file_path = '') |
|
474 | + { |
|
475 | + return pathinfo($full_file_path, PATHINFO_EXTENSION); |
|
476 | + } |
|
477 | + |
|
478 | + |
|
479 | + |
|
480 | + /** |
|
481 | + * add_htaccess_deny_from_all so the webserver cannot access this folder |
|
482 | + * @param string $folder |
|
483 | + * @throws EE_Error if filesystem credentials are required |
|
484 | + * @return bool |
|
485 | + */ |
|
486 | + public static function add_htaccess_deny_from_all($folder = '') |
|
487 | + { |
|
488 | + $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
489 | + if (! EEH_File::exists($folder . '.htaccess')) { |
|
490 | + if (! EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { |
|
491 | + return false; |
|
492 | + } |
|
493 | + } |
|
494 | + |
|
495 | + return true; |
|
496 | + } |
|
497 | + |
|
498 | + /** |
|
499 | + * Adds an index file to this folder, so folks can't list all the file's contents |
|
500 | + * @param string $folder |
|
501 | + * @throws EE_Error if filesystem credentials are required |
|
502 | + * @return boolean |
|
503 | + */ |
|
504 | + public static function add_index_file($folder) |
|
505 | + { |
|
506 | + $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
|
507 | + if (! EEH_File::exists($folder . 'index.php')) { |
|
508 | + if (! EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
509 | + return false; |
|
510 | + } |
|
511 | + } |
|
512 | + return true; |
|
513 | + } |
|
514 | + |
|
515 | + |
|
516 | + |
|
517 | + /** |
|
518 | + * Given that the file in $file_path has the normal name, (ie, CLASSNAME.whatever.php), |
|
519 | + * extract that classname. |
|
520 | + * @param string $file_path |
|
521 | + * @return string |
|
522 | + */ |
|
523 | + public static function get_classname_from_filepath_with_standard_filename($file_path) |
|
524 | + { |
|
525 | + // extract file from path |
|
526 | + $filename = basename($file_path); |
|
527 | + // now remove the first period and everything after |
|
528 | + $pos_of_first_period = strpos($filename, '.'); |
|
529 | + return substr($filename, 0, $pos_of_first_period); |
|
530 | + } |
|
531 | + |
|
532 | + |
|
533 | + |
|
534 | + /** |
|
535 | + * standardise_directory_separators |
|
536 | + * convert all directory separators in a file path. |
|
537 | + * @param string $file_path |
|
538 | + * @return string |
|
539 | + */ |
|
540 | + public static function standardise_directory_separators($file_path) |
|
541 | + { |
|
542 | + return str_replace(array( '\\', '/' ), '/', $file_path); |
|
543 | + } |
|
544 | + |
|
545 | + |
|
546 | + |
|
547 | + /** |
|
548 | + * end_with_directory_separator |
|
549 | + * ensures that file path ends with '/' |
|
550 | + * @param string $file_path |
|
551 | + * @return string |
|
552 | + */ |
|
553 | + public static function end_with_directory_separator($file_path) |
|
554 | + { |
|
555 | + return rtrim($file_path, '/\\') . '/'; |
|
556 | + } |
|
557 | + |
|
558 | + |
|
559 | + |
|
560 | + /** |
|
561 | + * shorthand for both EEH_FIle::end_with_directory_separator AND EEH_File::standardise_directory_separators |
|
562 | + * @param $file_path |
|
563 | + * @return string |
|
564 | + */ |
|
565 | + public static function standardise_and_end_with_directory_separator($file_path) |
|
566 | + { |
|
567 | + return self::end_with_directory_separator(self::standardise_directory_separators($file_path)); |
|
568 | + } |
|
569 | + |
|
570 | + |
|
571 | + |
|
572 | + /** |
|
573 | + * takes the folder name (with or without trailing slash) and finds the files it in, |
|
574 | + * and what the class's name inside of each should be. |
|
575 | + * @param array $folder_paths |
|
576 | + * @param boolean $index_numerically if TRUE, the returned array will be indexed numerically; |
|
577 | + * if FALSE (Default), returned array will be indexed by the filenames minus extensions. |
|
578 | + * Set it TRUE if you know there are files in the directory with the same name but different extensions |
|
579 | + * @throws EE_Error if filesystem credentials are required |
|
580 | + * @return array if $index_numerically == TRUE keys are numeric , |
|
581 | + * if $index_numerically == FALSE (Default) keys are what the class names SHOULD be; |
|
582 | + * and values are their filepaths |
|
583 | + */ |
|
584 | + public static function get_contents_of_folders($folder_paths = array(), $index_numerically = false) |
|
585 | + { |
|
586 | + $class_to_folder_path = array(); |
|
587 | + foreach ($folder_paths as $folder_path) { |
|
588 | + $folder_path = self::standardise_and_end_with_directory_separator($folder_path); |
|
589 | + // load WP_Filesystem and set file permissions |
|
590 | + $files_in_folder = glob($folder_path . '*.php'); |
|
591 | + $class_to_folder_path = array(); |
|
592 | + if ($files_in_folder) { |
|
593 | + foreach ($files_in_folder as $file_path) { |
|
594 | + // only add files, not folders |
|
595 | + if (! is_dir($file_path)) { |
|
596 | + if ($index_numerically) { |
|
597 | + $class_to_folder_path[] = $file_path; |
|
598 | + } else { |
|
599 | + $classname = self::get_classname_from_filepath_with_standard_filename($file_path); |
|
600 | + $class_to_folder_path[ $classname ] = $file_path; |
|
601 | + } |
|
602 | + } |
|
603 | + } |
|
604 | + } |
|
605 | + } |
|
606 | + return $class_to_folder_path; |
|
607 | + } |
|
608 | + |
|
609 | + |
|
610 | + |
|
611 | + /** |
|
612 | + * Copies a file. Mostly a wrapper of WP_Filesystem::copy |
|
613 | + * @param string $source_file |
|
614 | + * @param string $destination_file |
|
615 | + * @param boolean $overwrite |
|
616 | + * @throws EE_Error if filesystem credentials are required |
|
617 | + * @return boolean success |
|
618 | + */ |
|
619 | + public static function copy($source_file, $destination_file, $overwrite = false) |
|
620 | + { |
|
621 | + $full_source_path = EEH_File::standardise_directory_separators($source_file); |
|
622 | + if (! EEH_File::exists($full_source_path)) { |
|
623 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
624 | + $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); |
|
625 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); |
|
626 | + throw new EE_Error($msg); |
|
627 | + } |
|
628 | + return false; |
|
629 | + } |
|
630 | + |
|
631 | + $full_dest_path = EEH_File::standardise_directory_separators($destination_file); |
|
632 | + $folder = EEH_File::remove_filename_from_filepath($full_dest_path); |
|
633 | + EEH_File::ensure_folder_exists_and_is_writable($folder); |
|
634 | + if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
635 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
636 | + $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); |
|
637 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); |
|
638 | + throw new EE_Error($msg); |
|
639 | + } |
|
640 | + return false; |
|
641 | + } |
|
642 | + |
|
643 | + // load WP_Filesystem and set file permissions |
|
644 | + $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file); |
|
645 | + // write the file |
|
646 | + if (! $wp_filesystem->copy( |
|
647 | + EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), |
|
648 | + EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), |
|
649 | + $overwrite |
|
650 | + )) { |
|
651 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
652 | + $msg = sprintf(__('Attempted writing to file %1$s, but could not, probably because of permissions issues', 'event_espresso'), $full_source_path); |
|
653 | + $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path, 'f'); |
|
654 | + throw new EE_Error($msg); |
|
655 | + } |
|
656 | + return false; |
|
657 | + } |
|
658 | + return true; |
|
659 | + } |
|
660 | + |
|
661 | + /** |
|
662 | + * Reports whether or not the filepath is in the EE uploads folder or not |
|
663 | + * @param string $filepath |
|
664 | + * @return boolean |
|
665 | + */ |
|
666 | + public static function is_in_uploads_folder($filepath) |
|
667 | + { |
|
668 | + $uploads = wp_upload_dir(); |
|
669 | + return strpos($filepath, $uploads['basedir']) === 0 ? true : false; |
|
670 | + } |
|
671 | + |
|
672 | + /** |
|
673 | + * Given a "local" filepath (what you probably thought was the only filepath), |
|
674 | + * converts it into a "remote" filepath (the filepath the currently-in-use |
|
675 | + * $wp_filesystem needs to use access the folder or file). |
|
676 | + * See http://wordpress.stackexchange.com/questions/124900/using-wp-filesystem-in-plugins |
|
677 | + * @param WP_Filesystem_Base $wp_filesystem we aren't initially sure which one |
|
678 | + * is in use, so you need to provide it |
|
679 | + * @param string $local_filepath the filepath to the folder/file locally |
|
680 | + * @throws EE_Error if filesystem credentials are required |
|
681 | + * @return string the remote filepath (eg the filepath the filesystem method, eg |
|
682 | + * ftp or ssh, will use to access the folder |
|
683 | + */ |
|
684 | + public static function convert_local_filepath_to_remote_filepath($local_filepath) |
|
685 | + { |
|
686 | + $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); |
|
687 | + return str_replace(WP_CONTENT_DIR . '/', $wp_filesystem->wp_content_dir(), $local_filepath); |
|
688 | + } |
|
689 | 689 | } |
@@ -44,17 +44,17 @@ discard block |
||
44 | 44 | 'FHEE__EEH_File___get_wp_filesystem__allow_using_filesystem_direct', |
45 | 45 | $filepath && EEH_File::is_in_uploads_folder($filepath), |
46 | 46 | $filepath |
47 | - ) ) { |
|
48 | - if (! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
49 | - require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
47 | + )) { |
|
48 | + if ( ! EEH_File::$_wp_filesystem_direct instanceof WP_Filesystem_Direct) { |
|
49 | + require_once(ABSPATH.'wp-admin/includes/class-wp-filesystem-base.php'); |
|
50 | 50 | $method = 'direct'; |
51 | - $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); |
|
51 | + $wp_filesystem_direct_file = apply_filters('filesystem_method_file', ABSPATH.'wp-admin/includes/class-wp-filesystem-'.$method.'.php', $method); |
|
52 | 52 | // check constants defined, just like in wp-admin/includes/file.php's WP_Filesystem() |
53 | - if (! defined('FS_CHMOD_DIR')) { |
|
54 | - define('FS_CHMOD_DIR', ( fileperms(ABSPATH) & 0777 | 0755 )); |
|
53 | + if ( ! defined('FS_CHMOD_DIR')) { |
|
54 | + define('FS_CHMOD_DIR', (fileperms(ABSPATH) & 0777 | 0755)); |
|
55 | 55 | } |
56 | - if (! defined('FS_CHMOD_FILE')) { |
|
57 | - define('FS_CHMOD_FILE', ( fileperms(ABSPATH . 'index.php') & 0777 | 0644 )); |
|
56 | + if ( ! defined('FS_CHMOD_FILE')) { |
|
57 | + define('FS_CHMOD_FILE', (fileperms(ABSPATH.'index.php') & 0777 | 0644)); |
|
58 | 58 | } |
59 | 59 | require_once($wp_filesystem_direct_file); |
60 | 60 | EEH_File::$_wp_filesystem_direct = new WP_Filesystem_Direct(array()); |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | } |
64 | 64 | global $wp_filesystem; |
65 | 65 | // no filesystem setup ??? |
66 | - if (! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
66 | + if ( ! $wp_filesystem instanceof WP_Filesystem_Base) { |
|
67 | 67 | // if some eager beaver's just trying to get in there too early... |
68 | 68 | // let them do it, because we are one of those eager beavers! :P |
69 | 69 | /** |
@@ -79,14 +79,14 @@ discard block |
||
79 | 79 | if (false && ! did_action('wp_loaded')) { |
80 | 80 | $msg = __('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso'); |
81 | 81 | if (WP_DEBUG) { |
82 | - $msg .= '<br />' . __('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
82 | + $msg .= '<br />'.__('The WP Filesystem can not be accessed until after the "wp_loaded" hook has run, so it\'s best not to attempt access until the "admin_init" hookpoint.', 'event_espresso'); |
|
83 | 83 | } |
84 | 84 | throw new EE_Error($msg); |
85 | 85 | } else { |
86 | 86 | // should be loaded if we are past the wp_loaded hook... |
87 | - if (! function_exists('WP_Filesystem')) { |
|
88 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
89 | - require_once(ABSPATH . 'wp-admin/includes/template.php'); |
|
87 | + if ( ! function_exists('WP_Filesystem')) { |
|
88 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
89 | + require_once(ABSPATH.'wp-admin/includes/template.php'); |
|
90 | 90 | } |
91 | 91 | // turn on output buffering so that we can capture the credentials form |
92 | 92 | ob_start(); |
@@ -94,13 +94,13 @@ discard block |
||
94 | 94 | // store credentials form for the time being |
95 | 95 | EEH_File::$_credentials_form = ob_get_clean(); |
96 | 96 | // basically check for direct or previously configured access |
97 | - if (! WP_Filesystem($credentials)) { |
|
97 | + if ( ! WP_Filesystem($credentials)) { |
|
98 | 98 | // if credentials do NOT exist |
99 | 99 | if ($credentials === false) { |
100 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
100 | + add_action('admin_notices', array('EEH_File', 'display_request_filesystem_credentials_form'), 999); |
|
101 | 101 | throw new EE_Error(__('An attempt to access and/or write to a file on the server could not be completed due to a lack of sufficient credentials.', 'event_espresso')); |
102 | 102 | } elseif (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
103 | - add_action('admin_notices', array( 'EEH_File', 'display_request_filesystem_credentials_form' ), 999); |
|
103 | + add_action('admin_notices', array('EEH_File', 'display_request_filesystem_credentials_form'), 999); |
|
104 | 104 | throw new EE_Error( |
105 | 105 | sprintf( |
106 | 106 | __('WP Filesystem Error: $1%s', 'event_espresso'), |
@@ -119,8 +119,8 @@ discard block |
||
119 | 119 | */ |
120 | 120 | public static function display_request_filesystem_credentials_form() |
121 | 121 | { |
122 | - if (! empty(EEH_File::$_credentials_form)) { |
|
123 | - echo '<div class="updated espresso-notices-attention"><p>' . EEH_File::$_credentials_form . '</p></div>'; |
|
122 | + if ( ! empty(EEH_File::$_credentials_form)) { |
|
123 | + echo '<div class="updated espresso-notices-attention"><p>'.EEH_File::$_credentials_form.'</p></div>'; |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
@@ -143,8 +143,8 @@ discard block |
||
143 | 143 | // load WP_Filesystem and set file permissions |
144 | 144 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
145 | 145 | $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
146 | - if (! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
147 | - $file_name = ! empty($type_of_file) ? $file_name . ' ' . $type_of_file : $file_name; |
|
146 | + if ( ! $wp_filesystem->is_readable(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
147 | + $file_name = ! empty($type_of_file) ? $file_name.' '.$type_of_file : $file_name; |
|
148 | 148 | $file_name .= ! empty($file_ext) ? ' file' : ' folder'; |
149 | 149 | $msg = sprintf( |
150 | 150 | __('The requested %1$s could not be found or is not readable, possibly due to an incorrect filepath, or incorrect file permissions.%2$s', 'event_espresso'), |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | ); |
162 | 162 | } |
163 | 163 | if (defined('WP_DEBUG') && WP_DEBUG) { |
164 | - throw new EE_Error($msg . '||' . $msg); |
|
164 | + throw new EE_Error($msg.'||'.$msg); |
|
165 | 165 | } |
166 | 166 | return false; |
167 | 167 | } |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | $perms = $wp_filesystem->getchmod(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path)); |
188 | 188 | if ($perms) { |
189 | 189 | // file permissions exist, but way be set incorrectly |
190 | - $type_of_file = ! empty($type_of_file) ? $type_of_file . ' ' : ''; |
|
190 | + $type_of_file = ! empty($type_of_file) ? $type_of_file.' ' : ''; |
|
191 | 191 | $type_of_file .= ! empty($type_of_file) ? 'file' : 'folder'; |
192 | 192 | return sprintf( |
193 | 193 | __('File permissions for the requested %1$s are currently set at "%2$s". The recommended permissions are 644 for files and 755 for folders.', 'event_espresso'), |
@@ -226,15 +226,15 @@ discard block |
||
226 | 226 | // add / to folder |
227 | 227 | $folder = EEH_File::end_with_directory_separator($folder); |
228 | 228 | $wp_filesystem = EEH_File::_get_wp_filesystem($folder); |
229 | - if (! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
229 | + if ( ! $wp_filesystem->is_dir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
230 | 230 | // ok so it doesn't exist. Does its parent? Can we write to it? |
231 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
231 | + if ( ! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
232 | 232 | return false; |
233 | 233 | } |
234 | - if (! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
234 | + if ( ! EEH_File::verify_is_writable($parent_folder, 'folder')) { |
|
235 | 235 | return false; |
236 | 236 | } else { |
237 | - if (! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
237 | + if ( ! $wp_filesystem->mkdir(EEH_File::convert_local_filepath_to_remote_filepath($folder))) { |
|
238 | 238 | if (defined('WP_DEBUG') && WP_DEBUG) { |
239 | 239 | $msg = sprintf(__('"%s" could not be created.', 'event_espresso'), $folder); |
240 | 240 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($folder); |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | } |
245 | 245 | EEH_File::add_index_file($folder); |
246 | 246 | } |
247 | - } elseif (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
247 | + } elseif ( ! EEH_File::verify_is_writable($folder, 'folder')) { |
|
248 | 248 | return false; |
249 | 249 | } |
250 | 250 | return true; |
@@ -264,7 +264,7 @@ discard block |
||
264 | 264 | // load WP_Filesystem and set file permissions |
265 | 265 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_path); |
266 | 266 | $full_path = EEH_File::standardise_directory_separators($full_path); |
267 | - if (! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
267 | + if ( ! $wp_filesystem->is_writable(EEH_File::convert_local_filepath_to_remote_filepath($full_path))) { |
|
268 | 268 | if (defined('WP_DEBUG') && WP_DEBUG) { |
269 | 269 | $msg = sprintf(__('The "%1$s" %2$s is not writable.', 'event_espresso'), $full_path, $file_or_folder); |
270 | 270 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_path); |
@@ -291,11 +291,11 @@ discard block |
||
291 | 291 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
292 | 292 | $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
293 | 293 | $parent_folder = EEH_File::get_parent_folder($full_file_path); |
294 | - if (! EEH_File::exists($full_file_path)) { |
|
295 | - if (! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
294 | + if ( ! EEH_File::exists($full_file_path)) { |
|
295 | + if ( ! EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { |
|
296 | 296 | return false; |
297 | 297 | } |
298 | - if (! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
298 | + if ( ! $wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { |
|
299 | 299 | if (defined('WP_DEBUG') && WP_DEBUG) { |
300 | 300 | $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); |
301 | 301 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | return false; |
305 | 305 | } |
306 | 306 | } |
307 | - if (! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
307 | + if ( ! EEH_File::verify_is_writable($full_file_path, 'file')) { |
|
308 | 308 | return false; |
309 | 309 | } |
310 | 310 | return true; |
@@ -363,9 +363,9 @@ discard block |
||
363 | 363 | public static function write_to_file($full_file_path = '', $file_contents = '', $file_type = '') |
364 | 364 | { |
365 | 365 | $full_file_path = EEH_File::standardise_directory_separators($full_file_path); |
366 | - $file_type = ! empty($file_type) ? rtrim($file_type, ' ') . ' ' : ''; |
|
366 | + $file_type = ! empty($file_type) ? rtrim($file_type, ' ').' ' : ''; |
|
367 | 367 | $folder = EEH_File::remove_filename_from_filepath($full_file_path); |
368 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
368 | + if ( ! EEH_File::verify_is_writable($folder, 'folder')) { |
|
369 | 369 | if (defined('WP_DEBUG') && WP_DEBUG) { |
370 | 370 | $msg = sprintf(__('The %1$sfile located at "%2$s" is not writable.', 'event_espresso'), $file_type, $full_file_path); |
371 | 371 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | // load WP_Filesystem and set file permissions |
377 | 377 | $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); |
378 | 378 | // write the file |
379 | - if (! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
379 | + if ( ! $wp_filesystem->put_contents(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path), $file_contents)) { |
|
380 | 380 | if (defined('WP_DEBUG') && WP_DEBUG) { |
381 | 381 | $msg = sprintf(__('The %1$sfile located at "%2$s" could not be written to.', 'event_espresso'), $file_type, $full_file_path); |
382 | 382 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path, 'f'); |
@@ -486,8 +486,8 @@ discard block |
||
486 | 486 | public static function add_htaccess_deny_from_all($folder = '') |
487 | 487 | { |
488 | 488 | $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
489 | - if (! EEH_File::exists($folder . '.htaccess')) { |
|
490 | - if (! EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { |
|
489 | + if ( ! EEH_File::exists($folder.'.htaccess')) { |
|
490 | + if ( ! EEH_File::write_to_file($folder.'.htaccess', 'deny from all', '.htaccess')) { |
|
491 | 491 | return false; |
492 | 492 | } |
493 | 493 | } |
@@ -504,8 +504,8 @@ discard block |
||
504 | 504 | public static function add_index_file($folder) |
505 | 505 | { |
506 | 506 | $folder = EEH_File::standardise_and_end_with_directory_separator($folder); |
507 | - if (! EEH_File::exists($folder . 'index.php')) { |
|
508 | - if (! EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
507 | + if ( ! EEH_File::exists($folder.'index.php')) { |
|
508 | + if ( ! EEH_File::write_to_file($folder.'index.php', 'You are not permitted to read from this folder', '.php')) { |
|
509 | 509 | return false; |
510 | 510 | } |
511 | 511 | } |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | */ |
540 | 540 | public static function standardise_directory_separators($file_path) |
541 | 541 | { |
542 | - return str_replace(array( '\\', '/' ), '/', $file_path); |
|
542 | + return str_replace(array('\\', '/'), '/', $file_path); |
|
543 | 543 | } |
544 | 544 | |
545 | 545 | |
@@ -552,7 +552,7 @@ discard block |
||
552 | 552 | */ |
553 | 553 | public static function end_with_directory_separator($file_path) |
554 | 554 | { |
555 | - return rtrim($file_path, '/\\') . '/'; |
|
555 | + return rtrim($file_path, '/\\').'/'; |
|
556 | 556 | } |
557 | 557 | |
558 | 558 | |
@@ -587,17 +587,17 @@ discard block |
||
587 | 587 | foreach ($folder_paths as $folder_path) { |
588 | 588 | $folder_path = self::standardise_and_end_with_directory_separator($folder_path); |
589 | 589 | // load WP_Filesystem and set file permissions |
590 | - $files_in_folder = glob($folder_path . '*.php'); |
|
590 | + $files_in_folder = glob($folder_path.'*.php'); |
|
591 | 591 | $class_to_folder_path = array(); |
592 | 592 | if ($files_in_folder) { |
593 | 593 | foreach ($files_in_folder as $file_path) { |
594 | 594 | // only add files, not folders |
595 | - if (! is_dir($file_path)) { |
|
595 | + if ( ! is_dir($file_path)) { |
|
596 | 596 | if ($index_numerically) { |
597 | 597 | $class_to_folder_path[] = $file_path; |
598 | 598 | } else { |
599 | 599 | $classname = self::get_classname_from_filepath_with_standard_filename($file_path); |
600 | - $class_to_folder_path[ $classname ] = $file_path; |
|
600 | + $class_to_folder_path[$classname] = $file_path; |
|
601 | 601 | } |
602 | 602 | } |
603 | 603 | } |
@@ -619,7 +619,7 @@ discard block |
||
619 | 619 | public static function copy($source_file, $destination_file, $overwrite = false) |
620 | 620 | { |
621 | 621 | $full_source_path = EEH_File::standardise_directory_separators($source_file); |
622 | - if (! EEH_File::exists($full_source_path)) { |
|
622 | + if ( ! EEH_File::exists($full_source_path)) { |
|
623 | 623 | if (defined('WP_DEBUG') && WP_DEBUG) { |
624 | 624 | $msg = sprintf(__('The file located at "%2$s" is not readable or doesn\'t exist.', 'event_espresso'), $full_source_path); |
625 | 625 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_source_path); |
@@ -631,7 +631,7 @@ discard block |
||
631 | 631 | $full_dest_path = EEH_File::standardise_directory_separators($destination_file); |
632 | 632 | $folder = EEH_File::remove_filename_from_filepath($full_dest_path); |
633 | 633 | EEH_File::ensure_folder_exists_and_is_writable($folder); |
634 | - if (! EEH_File::verify_is_writable($folder, 'folder')) { |
|
634 | + if ( ! EEH_File::verify_is_writable($folder, 'folder')) { |
|
635 | 635 | if (defined('WP_DEBUG') && WP_DEBUG) { |
636 | 636 | $msg = sprintf(__('The file located at "%2$s" is not writable.', 'event_espresso'), $full_dest_path); |
637 | 637 | $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_dest_path); |
@@ -643,7 +643,7 @@ discard block |
||
643 | 643 | // load WP_Filesystem and set file permissions |
644 | 644 | $wp_filesystem = EEH_File::_get_wp_filesystem($destination_file); |
645 | 645 | // write the file |
646 | - if (! $wp_filesystem->copy( |
|
646 | + if ( ! $wp_filesystem->copy( |
|
647 | 647 | EEH_File::convert_local_filepath_to_remote_filepath($full_source_path), |
648 | 648 | EEH_File::convert_local_filepath_to_remote_filepath($full_dest_path), |
649 | 649 | $overwrite |
@@ -684,6 +684,6 @@ discard block |
||
684 | 684 | public static function convert_local_filepath_to_remote_filepath($local_filepath) |
685 | 685 | { |
686 | 686 | $wp_filesystem = EEH_File::_get_wp_filesystem($local_filepath); |
687 | - return str_replace(WP_CONTENT_DIR . '/', $wp_filesystem->wp_content_dir(), $local_filepath); |
|
687 | + return str_replace(WP_CONTENT_DIR.'/', $wp_filesystem->wp_content_dir(), $local_filepath); |
|
688 | 688 | } |
689 | 689 | } |