0
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Functions for reading, writing, modifying, and deleting files on the file system. |
|
4 |
* Includes functionality for theme-specific files as well as operations for uploading, |
|
5 |
* archiving, and rendering output when necessary. |
|
6 |
* |
|
7 |
* @package WordPress |
|
8 |
* @subpackage Administration |
|
9 |
*/ |
|
10 |
|
|
11 |
/** The descriptions for theme files. */ |
|
12 |
$wp_file_descriptions = array( |
|
13 |
'index.php' => __( 'Main Index Template' ), |
|
14 |
'style.css' => __( 'Stylesheet' ), |
|
15 |
'editor-style.css' => __( 'Visual Editor Stylesheet' ), |
|
16 |
'editor-style-rtl.css' => __( 'Visual Editor RTL Stylesheet' ), |
|
17 |
'rtl.css' => __( 'RTL Stylesheet' ), |
|
18 |
'comments.php' => __( 'Comments' ), |
|
19 |
'comments-popup.php' => __( 'Popup Comments' ), |
|
20 |
'footer.php' => __( 'Footer' ), |
|
21 |
'header.php' => __( 'Header' ), |
|
22 |
'sidebar.php' => __( 'Sidebar' ), |
|
23 |
'archive.php' => __( 'Archives' ), |
|
24 |
'author.php' => __( 'Author Template' ), |
|
25 |
'tag.php' => __( 'Tag Template' ), |
|
26 |
'category.php' => __( 'Category Template' ), |
|
27 |
'page.php' => __( 'Page Template' ), |
|
28 |
'search.php' => __( 'Search Results' ), |
|
29 |
'searchform.php' => __( 'Search Form' ), |
|
30 |
'single.php' => __( 'Single Post' ), |
|
31 |
'404.php' => __( '404 Template' ), |
|
32 |
'link.php' => __( 'Links Template' ), |
|
33 |
'functions.php' => __( 'Theme Functions' ), |
|
34 |
'attachment.php' => __( 'Attachment Template' ), |
|
35 |
'image.php' => __('Image Attachment Template'), |
|
36 |
'video.php' => __('Video Attachment Template'), |
|
37 |
'audio.php' => __('Audio Attachment Template'), |
|
38 |
'application.php' => __('Application Attachment Template'), |
|
39 |
'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ), |
|
40 |
'.htaccess' => __( '.htaccess (for rewrite rules )' ), |
|
41 |
// Deprecated files |
|
42 |
'wp-layout.css' => __( 'Stylesheet' ), |
|
43 |
'wp-comments.php' => __( 'Comments Template' ), |
|
44 |
'wp-comments-popup.php' => __( 'Popup Comments Template' ), |
|
45 |
); |
|
46 |
|
|
47 |
/** |
|
48 |
* Get the description for standard WordPress theme files and other various standard |
|
49 |
* WordPress files |
|
50 |
* |
|
51 |
* @since 1.5.0 |
|
52 |
* |
|
53 |
* @uses $wp_file_descriptions |
|
54 |
* @param string $file Filesystem path or filename |
|
55 |
* @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist |
|
56 |
*/ |
|
57 |
function get_file_description( $file ) { |
|
58 |
global $wp_file_descriptions; |
|
59 |
|
|
60 |
if ( isset( $wp_file_descriptions[basename( $file )] ) ) { |
|
61 |
return $wp_file_descriptions[basename( $file )]; |
|
62 |
} |
|
63 |
elseif ( file_exists( $file ) && is_file( $file ) ) { |
|
64 |
$template_data = implode( '', file( $file ) ); |
|
65 |
if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name )) |
|
66 |
return sprintf( __( '%s Page Template' ), _cleanup_header_comment($name[1]) ); |
|
67 |
} |
|
68 |
|
|
69 |
return trim( basename( $file ) ); |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* Get the absolute filesystem path to the root of the WordPress installation |
|
74 |
* |
|
75 |
* @since 1.5.0 |
|
76 |
* |
|
77 |
* @return string Full filesystem path to the root of the WordPress installation |
|
78 |
*/ |
|
79 |
function get_home_path() { |
5
|
80 |
$home = set_url_scheme( get_option( 'home' ), 'http' ); |
|
81 |
$siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' ); |
0
|
82 |
if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) { |
|
83 |
$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */ |
|
84 |
$pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) ); |
|
85 |
$home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos ); |
|
86 |
$home_path = trailingslashit( $home_path ); |
|
87 |
} else { |
|
88 |
$home_path = ABSPATH; |
|
89 |
} |
|
90 |
|
|
91 |
return str_replace( '\\', '/', $home_path ); |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep. |
|
96 |
* The depth of the recursiveness can be controlled by the $levels param. |
|
97 |
* |
|
98 |
* @since 2.6.0 |
|
99 |
* |
5
|
100 |
* @param string $folder Optional. Full path to folder. Default empty. |
|
101 |
* @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit). |
0
|
102 |
* @return bool|array False on failure, Else array of files |
|
103 |
*/ |
|
104 |
function list_files( $folder = '', $levels = 100 ) { |
|
105 |
if ( empty($folder) ) |
|
106 |
return false; |
|
107 |
|
|
108 |
if ( ! $levels ) |
|
109 |
return false; |
|
110 |
|
|
111 |
$files = array(); |
|
112 |
if ( $dir = @opendir( $folder ) ) { |
|
113 |
while (($file = readdir( $dir ) ) !== false ) { |
|
114 |
if ( in_array($file, array('.', '..') ) ) |
|
115 |
continue; |
|
116 |
if ( is_dir( $folder . '/' . $file ) ) { |
|
117 |
$files2 = list_files( $folder . '/' . $file, $levels - 1); |
|
118 |
if ( $files2 ) |
|
119 |
$files = array_merge($files, $files2 ); |
|
120 |
else |
|
121 |
$files[] = $folder . '/' . $file . '/'; |
|
122 |
} else { |
|
123 |
$files[] = $folder . '/' . $file; |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
@closedir( $dir ); |
|
128 |
return $files; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Returns a filename of a Temporary unique file. |
|
133 |
* Please note that the calling function must unlink() this itself. |
|
134 |
* |
|
135 |
* The filename is based off the passed parameter or defaults to the current unix timestamp, |
|
136 |
* while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory. |
|
137 |
* |
|
138 |
* @since 2.6.0 |
|
139 |
* |
5
|
140 |
* @param string $filename Optional. Filename to base the Unique file off. Default empty. |
|
141 |
* @param string $dir Optional. Directory to store the file in. Default empty. |
0
|
142 |
* @return string a writable filename |
|
143 |
*/ |
5
|
144 |
function wp_tempnam( $filename = '', $dir = '' ) { |
|
145 |
if ( empty( $dir ) ) { |
0
|
146 |
$dir = get_temp_dir(); |
5
|
147 |
} |
|
148 |
|
|
149 |
if ( empty( $filename ) || '.' == $filename || '/' == $filename ) { |
0
|
150 |
$filename = time(); |
5
|
151 |
} |
0
|
152 |
|
5
|
153 |
// Use the basename of the given file without the extension as the name for the temporary directory |
|
154 |
$temp_filename = basename( $filename ); |
|
155 |
$temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename ); |
|
156 |
|
|
157 |
// If the folder is falsey, use it's parent directory name instead |
|
158 |
if ( ! $temp_filename ) { |
|
159 |
return wp_tempnam( dirname( $filename ), $dir ); |
|
160 |
} |
|
161 |
|
|
162 |
$temp_filename .= '.tmp'; |
|
163 |
$temp_filename = $dir . wp_unique_filename( $dir, $temp_filename ); |
|
164 |
touch( $temp_filename ); |
|
165 |
|
|
166 |
return $temp_filename; |
0
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* Make sure that the file that was requested to edit, is allowed to be edited |
|
171 |
* |
|
172 |
* Function will die if if you are not allowed to edit the file |
|
173 |
* |
|
174 |
* @since 1.5.0 |
|
175 |
* |
|
176 |
* @param string $file file the users is attempting to edit |
|
177 |
* @param array $allowed_files Array of allowed files to edit, $file must match an entry exactly |
5
|
178 |
* @return string|null |
0
|
179 |
*/ |
|
180 |
function validate_file_to_edit( $file, $allowed_files = '' ) { |
|
181 |
$code = validate_file( $file, $allowed_files ); |
|
182 |
|
|
183 |
if (!$code ) |
|
184 |
return $file; |
|
185 |
|
|
186 |
switch ( $code ) { |
|
187 |
case 1 : |
5
|
188 |
wp_die( __( 'Sorry, that file cannot be edited.' ) ); |
0
|
189 |
|
5
|
190 |
// case 2 : |
|
191 |
// wp_die( __('Sorry, can’t call files with their real path.' )); |
0
|
192 |
|
|
193 |
case 3 : |
5
|
194 |
wp_die( __( 'Sorry, that file cannot be edited.' ) ); |
0
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
199 |
* Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type, |
|
200 |
* and moving the file to the appropriate directory within the uploads directory. |
|
201 |
* |
5
|
202 |
* @since 4.0.0 |
|
203 |
* |
|
204 |
* @see wp_handle_upload_error |
0
|
205 |
* |
5
|
206 |
* @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file. |
|
207 |
* @param array|false $overrides An associative array of names => values to override default variables. Default false. |
|
208 |
* @param string $time Time formatted in 'yyyy/mm'. |
|
209 |
* @param string $action Expected value for $_POST['action']. |
|
210 |
* @return array On success, returns an associative array of file attributes. On failure, returns |
|
211 |
* $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ). |
|
212 |
*/ |
|
213 |
function _wp_handle_upload( &$file, $overrides, $time, $action ) { |
0
|
214 |
// The default error handler. |
|
215 |
if ( ! function_exists( 'wp_handle_upload_error' ) ) { |
|
216 |
function wp_handle_upload_error( &$file, $message ) { |
5
|
217 |
return array( 'error' => $message ); |
0
|
218 |
} |
|
219 |
} |
|
220 |
|
5
|
221 |
/** |
|
222 |
* Filter the data for a file before it is uploaded to WordPress. |
|
223 |
* |
|
224 |
* The dynamic portion of the hook name, `$action`, refers to the post action. |
|
225 |
* |
|
226 |
* @since 2.9.0 as 'wp_handle_upload_prefilter'. |
|
227 |
* @since 4.0.0 Converted to a dynamic hook with `$action`. |
|
228 |
* |
|
229 |
* @param array $file An array of data for a single file. |
|
230 |
*/ |
|
231 |
$file = apply_filters( "{$action}_prefilter", $file ); |
0
|
232 |
|
|
233 |
// You may define your own function and pass the name in $overrides['upload_error_handler'] |
|
234 |
$upload_error_handler = 'wp_handle_upload_error'; |
5
|
235 |
if ( isset( $overrides['upload_error_handler'] ) ) { |
|
236 |
$upload_error_handler = $overrides['upload_error_handler']; |
|
237 |
} |
0
|
238 |
|
|
239 |
// You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully. |
5
|
240 |
if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) { |
0
|
241 |
return $upload_error_handler( $file, $file['error'] ); |
5
|
242 |
} |
|
243 |
|
|
244 |
// Install user overrides. Did we mention that this voids your warranty? |
0
|
245 |
|
|
246 |
// You may define your own function and pass the name in $overrides['unique_filename_callback'] |
|
247 |
$unique_filename_callback = null; |
5
|
248 |
if ( isset( $overrides['unique_filename_callback'] ) ) { |
|
249 |
$unique_filename_callback = $overrides['unique_filename_callback']; |
|
250 |
} |
0
|
251 |
|
5
|
252 |
/* |
|
253 |
* This may not have orignially been intended to be overrideable, |
|
254 |
* but historically has been. |
|
255 |
*/ |
|
256 |
if ( isset( $overrides['upload_error_strings'] ) ) { |
|
257 |
$upload_error_strings = $overrides['upload_error_strings']; |
|
258 |
} else { |
|
259 |
// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error']. |
|
260 |
$upload_error_strings = array( |
|
261 |
false, |
|
262 |
__( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.' ), |
|
263 |
__( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.' ), |
|
264 |
__( 'The uploaded file was only partially uploaded.' ), |
|
265 |
__( 'No file was uploaded.' ), |
|
266 |
'', |
|
267 |
__( 'Missing a temporary folder.' ), |
|
268 |
__( 'Failed to write file to disk.' ), |
|
269 |
__( 'File upload stopped by extension.' ) |
|
270 |
); |
|
271 |
} |
0
|
272 |
|
|
273 |
// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false; |
5
|
274 |
$test_form = isset( $overrides['test_form'] ) ? $overrides['test_form'] : true; |
|
275 |
$test_size = isset( $overrides['test_size'] ) ? $overrides['test_size'] : true; |
0
|
276 |
|
5
|
277 |
// If you override this, you must provide $ext and $type!! |
|
278 |
$test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true; |
|
279 |
$mimes = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false; |
0
|
280 |
|
|
281 |
// A correct form post will pass this test. |
5
|
282 |
if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) { |
|
283 |
return call_user_func( $upload_error_handler, $file, __( 'Invalid form submission.' ) ); |
|
284 |
} |
0
|
285 |
// A successful upload will pass this test. It makes no sense to override this one. |
5
|
286 |
if ( isset( $file['error'] ) && $file['error'] > 0 ) { |
|
287 |
return call_user_func( $upload_error_handler, $file, $upload_error_strings[ $file['error'] ] ); |
|
288 |
} |
0
|
289 |
|
5
|
290 |
$test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] ); |
0
|
291 |
// A non-empty file will pass this test. |
5
|
292 |
if ( $test_size && ! ( $test_file_size > 0 ) ) { |
|
293 |
if ( is_multisite() ) { |
0
|
294 |
$error_msg = __( 'File is empty. Please upload something more substantial.' ); |
5
|
295 |
} else { |
0
|
296 |
$error_msg = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ); |
5
|
297 |
} |
|
298 |
return call_user_func( $upload_error_handler, $file, $error_msg ); |
0
|
299 |
} |
|
300 |
|
|
301 |
// A properly uploaded file will pass this test. There should be no reason to override this one. |
5
|
302 |
$test_uploaded_file = 'wp_handle_upload' === $action ? @ is_uploaded_file( $file['tmp_name'] ) : @ is_file( $file['tmp_name'] ); |
|
303 |
if ( ! $test_uploaded_file ) { |
|
304 |
return call_user_func( $upload_error_handler, $file, __( 'Specified file failed upload test.' ) ); |
|
305 |
} |
0
|
306 |
|
|
307 |
// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter. |
|
308 |
if ( $test_type ) { |
|
309 |
$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes ); |
5
|
310 |
$ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext']; |
|
311 |
$type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type']; |
|
312 |
$proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename']; |
0
|
313 |
|
|
314 |
// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect |
5
|
315 |
if ( $proper_filename ) { |
0
|
316 |
$file['name'] = $proper_filename; |
5
|
317 |
} |
|
318 |
if ( ( ! $type || !$ext ) && ! current_user_can( 'unfiltered_upload' ) ) { |
|
319 |
return call_user_func( $upload_error_handler, $file, __( 'Sorry, this file type is not permitted for security reasons.' ) ); |
|
320 |
} |
|
321 |
if ( ! $type ) { |
0
|
322 |
$type = $file['type']; |
5
|
323 |
} |
0
|
324 |
} else { |
|
325 |
$type = ''; |
|
326 |
} |
|
327 |
|
5
|
328 |
/* |
|
329 |
* A writable uploads dir will pass this test. Again, there's no point |
|
330 |
* overriding this one. |
|
331 |
*/ |
|
332 |
if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) ) { |
|
333 |
return call_user_func( $upload_error_handler, $file, $uploads['error'] ); |
|
334 |
} |
0
|
335 |
|
|
336 |
$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback ); |
|
337 |
|
5
|
338 |
// Move the file to the uploads dir. |
0
|
339 |
$new_file = $uploads['path'] . "/$filename"; |
5
|
340 |
if ( 'wp_handle_upload' === $action ) { |
|
341 |
$move_new_file = @ move_uploaded_file( $file['tmp_name'], $new_file ); |
|
342 |
} else { |
|
343 |
$move_new_file = @ rename( $file['tmp_name'], $new_file ); |
|
344 |
} |
|
345 |
|
|
346 |
if ( false === $move_new_file ) { |
|
347 |
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) { |
0
|
348 |
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; |
5
|
349 |
} else { |
0
|
350 |
$error_path = basename( $uploads['basedir'] ) . $uploads['subdir']; |
5
|
351 |
} |
0
|
352 |
return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) ); |
|
353 |
} |
|
354 |
|
5
|
355 |
// Set correct file permissions. |
0
|
356 |
$stat = stat( dirname( $new_file )); |
|
357 |
$perms = $stat['mode'] & 0000666; |
|
358 |
@ chmod( $new_file, $perms ); |
|
359 |
|
5
|
360 |
// Compute the URL. |
0
|
361 |
$url = $uploads['url'] . "/$filename"; |
|
362 |
|
5
|
363 |
if ( is_multisite() ) { |
0
|
364 |
delete_transient( 'dirsize_cache' ); |
5
|
365 |
} |
0
|
366 |
|
5
|
367 |
/** |
|
368 |
* Filter the data array for the uploaded file. |
|
369 |
* |
|
370 |
* @since 2.1.0 |
|
371 |
* |
|
372 |
* @param array $upload { |
|
373 |
* Array of upload data. |
|
374 |
* |
|
375 |
* @type string $file Filename of the newly-uploaded file. |
|
376 |
* @type string $url URL of the uploaded file. |
|
377 |
* @type string $type File type. |
|
378 |
* } |
|
379 |
* @param string $context The type of upload action. Values include 'upload' or 'sideload'. |
|
380 |
*/ |
|
381 |
return apply_filters( 'wp_handle_upload', array( |
|
382 |
'file' => $new_file, |
|
383 |
'url' => $url, |
|
384 |
'type' => $type |
|
385 |
), 'wp_handle_sideload' === $action ? 'sideload' : 'upload' ); } |
|
386 |
|
|
387 |
/** |
|
388 |
* Wrapper for _wp_handle_upload(), passes 'wp_handle_upload' action. |
|
389 |
* |
|
390 |
* @since 2.0.0 |
|
391 |
* |
|
392 |
* @see _wp_handle_upload() |
|
393 |
* |
|
394 |
* @param array $file Reference to a single element of $_FILES. Call the function once for |
|
395 |
* each uploaded file. |
|
396 |
* @param array|bool $overrides Optional. An associative array of names=>values to override default |
|
397 |
* variables. Default false. |
|
398 |
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. |
|
399 |
* @return array On success, returns an associative array of file attributes. On failure, returns |
|
400 |
* $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ). |
|
401 |
*/ |
|
402 |
function wp_handle_upload( &$file, $overrides = false, $time = null ) { |
|
403 |
/* |
|
404 |
* $_POST['action'] must be set and its value must equal $overrides['action'] |
|
405 |
* or this: |
|
406 |
*/ |
|
407 |
$action = 'wp_handle_upload'; |
|
408 |
if ( isset( $overrides['action'] ) ) { |
|
409 |
$action = $overrides['action']; |
|
410 |
} |
|
411 |
|
|
412 |
return _wp_handle_upload( $file, $overrides, $time, $action ); |
0
|
413 |
} |
|
414 |
|
|
415 |
/** |
5
|
416 |
* Wrapper for _wp_handle_upload(), passes 'wp_handle_sideload' action |
0
|
417 |
* |
|
418 |
* @since 2.6.0 |
|
419 |
* |
5
|
420 |
* @see _wp_handle_upload() |
|
421 |
* |
|
422 |
* @param array $file An array similar to that of a PHP $_FILES POST array |
|
423 |
* @param array|bool $overrides Optional. An associative array of names=>values to override default |
|
424 |
* variables. Default false. |
|
425 |
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. |
|
426 |
* @return array On success, returns an associative array of file attributes. On failure, returns |
|
427 |
* $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ). |
0
|
428 |
*/ |
|
429 |
function wp_handle_sideload( &$file, $overrides = false, $time = null ) { |
5
|
430 |
/* |
|
431 |
* $_POST['action'] must be set and its value must equal $overrides['action'] |
|
432 |
* or this: |
|
433 |
*/ |
0
|
434 |
$action = 'wp_handle_sideload'; |
5
|
435 |
if ( isset( $overrides['action'] ) ) { |
|
436 |
$action = $overrides['action']; |
|
437 |
} |
|
438 |
return _wp_handle_upload( $file, $overrides, $time, $action ); |
|
439 |
} |
0
|
440 |
|
|
441 |
|
|
442 |
/** |
|
443 |
* Downloads a url to a local temporary file using the WordPress HTTP Class. |
|
444 |
* Please note, That the calling function must unlink() the file. |
|
445 |
* |
|
446 |
* @since 2.5.0 |
|
447 |
* |
|
448 |
* @param string $url the URL of the file to download |
|
449 |
* @param int $timeout The timeout for the request to download the file default 300 seconds |
|
450 |
* @return mixed WP_Error on failure, string Filename on success. |
|
451 |
*/ |
|
452 |
function download_url( $url, $timeout = 300 ) { |
|
453 |
//WARNING: The file is not automatically deleted, The script must unlink() the file. |
|
454 |
if ( ! $url ) |
|
455 |
return new WP_Error('http_no_url', __('Invalid URL Provided.')); |
|
456 |
|
|
457 |
$tmpfname = wp_tempnam($url); |
|
458 |
if ( ! $tmpfname ) |
|
459 |
return new WP_Error('http_no_file', __('Could not create Temporary file.')); |
|
460 |
|
|
461 |
$response = wp_safe_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); |
|
462 |
|
|
463 |
if ( is_wp_error( $response ) ) { |
|
464 |
unlink( $tmpfname ); |
|
465 |
return $response; |
|
466 |
} |
|
467 |
|
|
468 |
if ( 200 != wp_remote_retrieve_response_code( $response ) ){ |
|
469 |
unlink( $tmpfname ); |
|
470 |
return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) ); |
|
471 |
} |
|
472 |
|
|
473 |
$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' ); |
|
474 |
if ( $content_md5 ) { |
|
475 |
$md5_check = verify_file_md5( $tmpfname, $content_md5 ); |
|
476 |
if ( is_wp_error( $md5_check ) ) { |
|
477 |
unlink( $tmpfname ); |
|
478 |
return $md5_check; |
|
479 |
} |
|
480 |
} |
|
481 |
|
|
482 |
return $tmpfname; |
|
483 |
} |
|
484 |
|
|
485 |
/** |
5
|
486 |
* Calculates and compares the MD5 of a file to its expected value. |
0
|
487 |
* |
|
488 |
* @since 3.7.0 |
|
489 |
* |
|
490 |
* @param string $filename The filename to check the MD5 of. |
|
491 |
* @param string $expected_md5 The expected MD5 of the file, either a base64 encoded raw md5, or a hex-encoded md5 |
|
492 |
* @return bool|object WP_Error on failure, true on success, false when the MD5 format is unknown/unexpected |
|
493 |
*/ |
|
494 |
function verify_file_md5( $filename, $expected_md5 ) { |
|
495 |
if ( 32 == strlen( $expected_md5 ) ) |
|
496 |
$expected_raw_md5 = pack( 'H*', $expected_md5 ); |
|
497 |
elseif ( 24 == strlen( $expected_md5 ) ) |
|
498 |
$expected_raw_md5 = base64_decode( $expected_md5 ); |
|
499 |
else |
|
500 |
return false; // unknown format |
|
501 |
|
|
502 |
$file_md5 = md5_file( $filename, true ); |
|
503 |
|
|
504 |
if ( $file_md5 === $expected_raw_md5 ) |
|
505 |
return true; |
|
506 |
|
|
507 |
return new WP_Error( 'md5_mismatch', sprintf( __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ), bin2hex( $file_md5 ), bin2hex( $expected_raw_md5 ) ) ); |
|
508 |
} |
|
509 |
|
|
510 |
/** |
|
511 |
* Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction. |
|
512 |
* Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present. |
|
513 |
* |
|
514 |
* Attempts to increase the PHP Memory limit to 256M before uncompressing, |
|
515 |
* However, The most memory required shouldn't be much larger than the Archive itself. |
|
516 |
* |
|
517 |
* @since 2.5.0 |
|
518 |
* |
|
519 |
* @param string $file Full path and filename of zip archive |
|
520 |
* @param string $to Full path on the filesystem to extract archive to |
|
521 |
* @return mixed WP_Error on failure, True on success |
|
522 |
*/ |
|
523 |
function unzip_file($file, $to) { |
|
524 |
global $wp_filesystem; |
|
525 |
|
|
526 |
if ( ! $wp_filesystem || !is_object($wp_filesystem) ) |
|
527 |
return new WP_Error('fs_unavailable', __('Could not access filesystem.')); |
|
528 |
|
|
529 |
// Unzip can use a lot of memory, but not this much hopefully |
5
|
530 |
/** This filter is documented in wp-admin/admin.php */ |
0
|
531 |
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
|
532 |
|
|
533 |
$needed_dirs = array(); |
|
534 |
$to = trailingslashit($to); |
|
535 |
|
|
536 |
// Determine any parent dir's needed (of the upgrade directory) |
|
537 |
if ( ! $wp_filesystem->is_dir($to) ) { //Only do parents if no children exist |
|
538 |
$path = preg_split('![/\\\]!', untrailingslashit($to)); |
|
539 |
for ( $i = count($path); $i >= 0; $i-- ) { |
|
540 |
if ( empty($path[$i]) ) |
|
541 |
continue; |
|
542 |
|
|
543 |
$dir = implode('/', array_slice($path, 0, $i+1) ); |
|
544 |
if ( preg_match('!^[a-z]:$!i', $dir) ) // Skip it if it looks like a Windows Drive letter. |
|
545 |
continue; |
|
546 |
|
|
547 |
if ( ! $wp_filesystem->is_dir($dir) ) |
|
548 |
$needed_dirs[] = $dir; |
|
549 |
else |
|
550 |
break; // A folder exists, therefor, we dont need the check the levels below this |
|
551 |
} |
|
552 |
} |
|
553 |
|
5
|
554 |
/** |
|
555 |
* Filter whether to use ZipArchive to unzip archives. |
|
556 |
* |
|
557 |
* @since 3.0.0 |
|
558 |
* |
|
559 |
* @param bool $ziparchive Whether to use ZipArchive. Default true. |
|
560 |
*/ |
|
561 |
if ( class_exists( 'ZipArchive' ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { |
0
|
562 |
$result = _unzip_file_ziparchive($file, $to, $needed_dirs); |
|
563 |
if ( true === $result ) { |
|
564 |
return $result; |
|
565 |
} elseif ( is_wp_error($result) ) { |
|
566 |
if ( 'incompatible_archive' != $result->get_error_code() ) |
|
567 |
return $result; |
|
568 |
} |
|
569 |
} |
|
570 |
// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. |
|
571 |
return _unzip_file_pclzip($file, $to, $needed_dirs); |
|
572 |
} |
|
573 |
|
|
574 |
/** |
|
575 |
* This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class. |
|
576 |
* Assumes that WP_Filesystem() has already been called and set up. |
|
577 |
* |
|
578 |
* @since 3.0.0 |
|
579 |
* @see unzip_file |
|
580 |
* @access private |
|
581 |
* |
|
582 |
* @param string $file Full path and filename of zip archive |
|
583 |
* @param string $to Full path on the filesystem to extract archive to |
|
584 |
* @param array $needed_dirs A partial list of required folders needed to be created. |
|
585 |
* @return mixed WP_Error on failure, True on success |
|
586 |
*/ |
|
587 |
function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) { |
|
588 |
global $wp_filesystem; |
|
589 |
|
|
590 |
$z = new ZipArchive(); |
|
591 |
|
|
592 |
$zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS ); |
|
593 |
if ( true !== $zopen ) |
|
594 |
return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) ); |
|
595 |
|
|
596 |
$uncompressed_size = 0; |
|
597 |
|
|
598 |
for ( $i = 0; $i < $z->numFiles; $i++ ) { |
|
599 |
if ( ! $info = $z->statIndex($i) ) |
|
600 |
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); |
|
601 |
|
|
602 |
if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Skip the OS X-created __MACOSX directory |
|
603 |
continue; |
|
604 |
|
|
605 |
$uncompressed_size += $info['size']; |
|
606 |
|
|
607 |
if ( '/' == substr($info['name'], -1) ) // directory |
|
608 |
$needed_dirs[] = $to . untrailingslashit($info['name']); |
|
609 |
else |
|
610 |
$needed_dirs[] = $to . untrailingslashit(dirname($info['name'])); |
|
611 |
} |
|
612 |
|
|
613 |
/* |
|
614 |
* disk_free_space() could return false. Assume that any falsey value is an error. |
|
615 |
* A disk that has zero free bytes has bigger problems. |
|
616 |
* Require we have enough space to unzip the file and copy its contents, with a 10% buffer. |
|
617 |
*/ |
|
618 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
|
619 |
$available_space = @disk_free_space( WP_CONTENT_DIR ); |
|
620 |
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) |
|
621 |
return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) ); |
|
622 |
} |
|
623 |
|
|
624 |
$needed_dirs = array_unique($needed_dirs); |
|
625 |
foreach ( $needed_dirs as $dir ) { |
|
626 |
// Check the parent folders of the folders all exist within the creation array. |
|
627 |
if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist) |
|
628 |
continue; |
|
629 |
if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it |
|
630 |
continue; |
|
631 |
|
|
632 |
$parent_folder = dirname($dir); |
|
633 |
while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) { |
|
634 |
$needed_dirs[] = $parent_folder; |
|
635 |
$parent_folder = dirname($parent_folder); |
|
636 |
} |
|
637 |
} |
|
638 |
asort($needed_dirs); |
|
639 |
|
|
640 |
// Create those directories if need be: |
|
641 |
foreach ( $needed_dirs as $_dir ) { |
5
|
642 |
// Only check to see if the Dir exists upon creation failure. Less I/O this way. |
|
643 |
if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) { |
0
|
644 |
return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) ); |
5
|
645 |
} |
0
|
646 |
} |
|
647 |
unset($needed_dirs); |
|
648 |
|
|
649 |
for ( $i = 0; $i < $z->numFiles; $i++ ) { |
|
650 |
if ( ! $info = $z->statIndex($i) ) |
|
651 |
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); |
|
652 |
|
|
653 |
if ( '/' == substr($info['name'], -1) ) // directory |
|
654 |
continue; |
|
655 |
|
|
656 |
if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files |
|
657 |
continue; |
|
658 |
|
|
659 |
$contents = $z->getFromIndex($i); |
|
660 |
if ( false === $contents ) |
|
661 |
return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] ); |
|
662 |
|
|
663 |
if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE) ) |
|
664 |
return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] ); |
|
665 |
} |
|
666 |
|
|
667 |
$z->close(); |
|
668 |
|
|
669 |
return true; |
|
670 |
} |
|
671 |
|
|
672 |
/** |
|
673 |
* This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the PclZip library. |
|
674 |
* Assumes that WP_Filesystem() has already been called and set up. |
|
675 |
* |
|
676 |
* @since 3.0.0 |
|
677 |
* @see unzip_file |
|
678 |
* @access private |
|
679 |
* |
|
680 |
* @param string $file Full path and filename of zip archive |
|
681 |
* @param string $to Full path on the filesystem to extract archive to |
|
682 |
* @param array $needed_dirs A partial list of required folders needed to be created. |
|
683 |
* @return mixed WP_Error on failure, True on success |
|
684 |
*/ |
|
685 |
function _unzip_file_pclzip($file, $to, $needed_dirs = array()) { |
|
686 |
global $wp_filesystem; |
|
687 |
|
|
688 |
mbstring_binary_safe_encoding(); |
|
689 |
|
|
690 |
require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); |
|
691 |
|
|
692 |
$archive = new PclZip($file); |
|
693 |
|
|
694 |
$archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING); |
|
695 |
|
|
696 |
reset_mbstring_encoding(); |
|
697 |
|
|
698 |
// Is the archive valid? |
|
699 |
if ( !is_array($archive_files) ) |
|
700 |
return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true)); |
|
701 |
|
|
702 |
if ( 0 == count($archive_files) ) |
|
703 |
return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) ); |
|
704 |
|
|
705 |
$uncompressed_size = 0; |
|
706 |
|
|
707 |
// Determine any children directories needed (From within the archive) |
|
708 |
foreach ( $archive_files as $file ) { |
|
709 |
if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Skip the OS X-created __MACOSX directory |
|
710 |
continue; |
|
711 |
|
|
712 |
$uncompressed_size += $file['size']; |
|
713 |
|
|
714 |
$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) ); |
|
715 |
} |
|
716 |
|
|
717 |
/* |
|
718 |
* disk_free_space() could return false. Assume that any falsey value is an error. |
|
719 |
* A disk that has zero free bytes has bigger problems. |
|
720 |
* Require we have enough space to unzip the file and copy its contents, with a 10% buffer. |
|
721 |
*/ |
|
722 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
|
723 |
$available_space = @disk_free_space( WP_CONTENT_DIR ); |
|
724 |
if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) |
|
725 |
return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) ); |
|
726 |
} |
|
727 |
|
|
728 |
$needed_dirs = array_unique($needed_dirs); |
|
729 |
foreach ( $needed_dirs as $dir ) { |
|
730 |
// Check the parent folders of the folders all exist within the creation array. |
|
731 |
if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist) |
|
732 |
continue; |
|
733 |
if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it |
|
734 |
continue; |
|
735 |
|
|
736 |
$parent_folder = dirname($dir); |
|
737 |
while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) { |
|
738 |
$needed_dirs[] = $parent_folder; |
|
739 |
$parent_folder = dirname($parent_folder); |
|
740 |
} |
|
741 |
} |
|
742 |
asort($needed_dirs); |
|
743 |
|
|
744 |
// Create those directories if need be: |
|
745 |
foreach ( $needed_dirs as $_dir ) { |
|
746 |
// Only check to see if the dir exists upon creation failure. Less I/O this way. |
|
747 |
if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) |
|
748 |
return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) ); |
|
749 |
} |
|
750 |
unset($needed_dirs); |
|
751 |
|
|
752 |
// Extract the files from the zip |
|
753 |
foreach ( $archive_files as $file ) { |
|
754 |
if ( $file['folder'] ) |
|
755 |
continue; |
|
756 |
|
|
757 |
if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files |
|
758 |
continue; |
|
759 |
|
|
760 |
if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE) ) |
|
761 |
return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] ); |
|
762 |
} |
|
763 |
return true; |
|
764 |
} |
|
765 |
|
|
766 |
/** |
|
767 |
* Copies a directory from one location to another via the WordPress Filesystem Abstraction. |
|
768 |
* Assumes that WP_Filesystem() has already been called and setup. |
|
769 |
* |
|
770 |
* @since 2.5.0 |
|
771 |
* |
|
772 |
* @param string $from source directory |
|
773 |
* @param string $to destination directory |
|
774 |
* @param array $skip_list a list of files/folders to skip copying |
|
775 |
* @return mixed WP_Error on failure, True on success. |
|
776 |
*/ |
|
777 |
function copy_dir($from, $to, $skip_list = array() ) { |
|
778 |
global $wp_filesystem; |
|
779 |
|
|
780 |
$dirlist = $wp_filesystem->dirlist($from); |
|
781 |
|
|
782 |
$from = trailingslashit($from); |
|
783 |
$to = trailingslashit($to); |
|
784 |
|
|
785 |
foreach ( (array) $dirlist as $filename => $fileinfo ) { |
|
786 |
if ( in_array( $filename, $skip_list ) ) |
|
787 |
continue; |
|
788 |
|
|
789 |
if ( 'f' == $fileinfo['type'] ) { |
|
790 |
if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) { |
|
791 |
// If copy failed, chmod file to 0644 and try again. |
|
792 |
$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE ); |
|
793 |
if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) |
|
794 |
return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename ); |
|
795 |
} |
|
796 |
} elseif ( 'd' == $fileinfo['type'] ) { |
|
797 |
if ( !$wp_filesystem->is_dir($to . $filename) ) { |
|
798 |
if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) ) |
|
799 |
return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename ); |
|
800 |
} |
|
801 |
|
|
802 |
// generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list |
|
803 |
$sub_skip_list = array(); |
|
804 |
foreach ( $skip_list as $skip_item ) { |
|
805 |
if ( 0 === strpos( $skip_item, $filename . '/' ) ) |
|
806 |
$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item ); |
|
807 |
} |
|
808 |
|
|
809 |
$result = copy_dir($from . $filename, $to . $filename, $sub_skip_list); |
|
810 |
if ( is_wp_error($result) ) |
|
811 |
return $result; |
|
812 |
} |
|
813 |
} |
|
814 |
return true; |
|
815 |
} |
|
816 |
|
|
817 |
/** |
|
818 |
* Initialises and connects the WordPress Filesystem Abstraction classes. |
|
819 |
* This function will include the chosen transport and attempt connecting. |
|
820 |
* |
5
|
821 |
* Plugins may add extra transports, And force WordPress to use them by returning |
|
822 |
* the filename via the {@see 'filesystem_method_file'} filter. |
0
|
823 |
* |
|
824 |
* @since 2.5.0 |
|
825 |
* |
5
|
826 |
* @param array $args Optional. Connection args, These are passed directly to |
|
827 |
* the `WP_Filesystem_*()` classes. Default false. |
|
828 |
* @param string $context Optional. Context for {@see get_filesystem_method()}. |
|
829 |
* Default false. |
|
830 |
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. |
|
831 |
* Default false. |
|
832 |
* @return null|boolean false on failure, true on success. |
0
|
833 |
*/ |
5
|
834 |
function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { |
0
|
835 |
global $wp_filesystem; |
|
836 |
|
|
837 |
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'); |
|
838 |
|
5
|
839 |
$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership ); |
0
|
840 |
|
|
841 |
if ( ! $method ) |
|
842 |
return false; |
|
843 |
|
|
844 |
if ( ! class_exists("WP_Filesystem_$method") ) { |
5
|
845 |
|
|
846 |
/** |
|
847 |
* Filter the path for a specific filesystem method class file. |
|
848 |
* |
|
849 |
* @since 2.6.0 |
|
850 |
* |
|
851 |
* @see get_filesystem_method() |
|
852 |
* |
|
853 |
* @param string $path Path to the specific filesystem method class file. |
|
854 |
* @param string $method The filesystem method to use. |
|
855 |
*/ |
|
856 |
$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method ); |
|
857 |
|
0
|
858 |
if ( ! file_exists($abstraction_file) ) |
|
859 |
return; |
|
860 |
|
|
861 |
require_once($abstraction_file); |
|
862 |
} |
|
863 |
$method = "WP_Filesystem_$method"; |
|
864 |
|
|
865 |
$wp_filesystem = new $method($args); |
|
866 |
|
|
867 |
//Define the timeouts for the connections. Only available after the construct is called to allow for per-transport overriding of the default. |
|
868 |
if ( ! defined('FS_CONNECT_TIMEOUT') ) |
|
869 |
define('FS_CONNECT_TIMEOUT', 30); |
|
870 |
if ( ! defined('FS_TIMEOUT') ) |
|
871 |
define('FS_TIMEOUT', 30); |
|
872 |
|
|
873 |
if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) |
|
874 |
return false; |
|
875 |
|
|
876 |
if ( !$wp_filesystem->connect() ) |
|
877 |
return false; //There was an error connecting to the server. |
|
878 |
|
|
879 |
// Set the permission constants if not already set. |
|
880 |
if ( ! defined('FS_CHMOD_DIR') ) |
|
881 |
define('FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) ); |
|
882 |
if ( ! defined('FS_CHMOD_FILE') ) |
|
883 |
define('FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) ); |
|
884 |
|
|
885 |
return true; |
|
886 |
} |
|
887 |
|
|
888 |
/** |
5
|
889 |
* Determines which method to use for reading, writing, modifying, or deleting |
|
890 |
* files on the filesystem. |
|
891 |
* |
|
892 |
* The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets |
|
893 |
* (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2', |
|
894 |
* 'ftpext' or 'ftpsockets'. |
0
|
895 |
* |
5
|
896 |
* The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`, |
|
897 |
* or filtering via {@see 'filesystem_method'}. |
|
898 |
* |
|
899 |
* @link https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants |
|
900 |
* |
|
901 |
* Plugins may define a custom transport handler, See WP_Filesystem(). |
0
|
902 |
* |
|
903 |
* @since 2.5.0 |
|
904 |
* |
5
|
905 |
* @param array $args Optional. Connection details. Default empty array. |
|
906 |
* @param string $context Optional. Full path to the directory that is tested |
|
907 |
* for being writable. Default false. |
|
908 |
* @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. |
|
909 |
* Default false. |
0
|
910 |
* @return string The transport to use, see description for valid return values. |
|
911 |
*/ |
5
|
912 |
function get_filesystem_method( $args = array(), $context = false, $allow_relaxed_file_ownership = false ) { |
|
913 |
$method = defined('FS_METHOD') ? FS_METHOD : false; // Please ensure that this is either 'direct', 'ssh2', 'ftpext' or 'ftpsockets' |
|
914 |
|
|
915 |
if ( ! $context ) { |
|
916 |
$context = WP_CONTENT_DIR; |
|
917 |
} |
0
|
918 |
|
5
|
919 |
// If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it. |
|
920 |
if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) { |
|
921 |
$context = dirname( $context ); |
|
922 |
} |
0
|
923 |
|
5
|
924 |
$context = trailingslashit( $context ); |
0
|
925 |
|
5
|
926 |
if ( ! $method ) { |
|
927 |
|
0
|
928 |
$temp_file_name = $context . 'temp-write-test-' . time(); |
|
929 |
$temp_handle = @fopen($temp_file_name, 'w'); |
|
930 |
if ( $temp_handle ) { |
5
|
931 |
|
|
932 |
// Attempt to determine the file owner of the WordPress files, and that of newly created files |
|
933 |
$wp_file_owner = $temp_file_owner = false; |
|
934 |
if ( function_exists('fileowner') ) { |
|
935 |
$wp_file_owner = @fileowner( __FILE__ ); |
|
936 |
$temp_file_owner = @fileowner( $temp_file_name ); |
|
937 |
} |
|
938 |
|
|
939 |
if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) { |
|
940 |
// WordPress is creating files as the same owner as the WordPress files, |
|
941 |
// this means it's safe to modify & create new files via PHP. |
0
|
942 |
$method = 'direct'; |
5
|
943 |
$GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; |
|
944 |
} elseif ( $allow_relaxed_file_ownership ) { |
|
945 |
// The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files |
|
946 |
// safely in this directory. This mode doesn't create new files, only alter existing ones. |
|
947 |
$method = 'direct'; |
|
948 |
$GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; |
|
949 |
} |
|
950 |
|
0
|
951 |
@fclose($temp_handle); |
|
952 |
@unlink($temp_file_name); |
|
953 |
} |
|
954 |
} |
|
955 |
|
|
956 |
if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2'; |
|
957 |
if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext'; |
|
958 |
if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread |
5
|
959 |
|
|
960 |
/** |
|
961 |
* Filter the filesystem method to use. |
|
962 |
* |
|
963 |
* @since 2.6.0 |
|
964 |
* |
|
965 |
* @param string $method Filesystem method to return. |
|
966 |
* @param array $args An array of connection details for the method. |
|
967 |
* @param string $context Full path to the directory that is tested for being writable. |
|
968 |
* @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
|
969 |
*/ |
|
970 |
return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership ); |
0
|
971 |
} |
|
972 |
|
|
973 |
/** |
5
|
974 |
* Displays a form to the user to request for their FTP/SSH details in order |
|
975 |
* to connect to the filesystem. |
|
976 |
* |
0
|
977 |
* All chosen/entered details are saved, Excluding the Password. |
|
978 |
* |
5
|
979 |
* Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) |
|
980 |
* to specify an alternate FTP/SSH port. |
0
|
981 |
* |
5
|
982 |
* Plugins may override this form by returning true|false via the |
|
983 |
* {@see 'request_filesystem_credentials'} filter. |
0
|
984 |
* |
5
|
985 |
* @since 2.5. |
|
986 |
* |
|
987 |
* @todo Properly mark optional arguments as such |
0
|
988 |
* |
|
989 |
* @param string $form_post the URL to post the form to |
|
990 |
* @param string $type the chosen Filesystem method in use |
|
991 |
* @param boolean $error if the current request has failed to connect |
|
992 |
* @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() |
5
|
993 |
* @param array $extra_fields Extra POST fields which should be checked for to be included in the post. |
|
994 |
* @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
0
|
995 |
* @return boolean False on failure. True on success. |
|
996 |
*/ |
5
|
997 |
function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false, $extra_fields = null, $allow_relaxed_file_ownership = false ) { |
|
998 |
|
|
999 |
/** |
|
1000 |
* Filter the filesystem credentials form output. |
|
1001 |
* |
|
1002 |
* Returning anything other than an empty string will effectively short-circuit |
|
1003 |
* output of the filesystem credentials form, returning that value instead. |
|
1004 |
* |
|
1005 |
* @since 2.5.0 |
|
1006 |
* |
|
1007 |
* @param mixed $output Form output to return instead. Default empty. |
|
1008 |
* @param string $form_post URL to POST the form to. |
|
1009 |
* @param string $type Chosen type of filesystem. |
|
1010 |
* @param bool $error Whether the current request has failed to connect. |
|
1011 |
* Default false. |
|
1012 |
* @param string $context Full path to the directory that is tested for |
|
1013 |
* being writable. |
|
1014 |
* @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
|
1015 |
* @param array $extra_fields Extra POST fields. |
|
1016 |
*/ |
|
1017 |
$req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ); |
0
|
1018 |
if ( '' !== $req_cred ) |
|
1019 |
return $req_cred; |
|
1020 |
|
5
|
1021 |
if ( empty($type) ) { |
|
1022 |
$type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
|
1023 |
} |
0
|
1024 |
|
|
1025 |
if ( 'direct' == $type ) |
|
1026 |
return true; |
|
1027 |
|
|
1028 |
if ( is_null( $extra_fields ) ) |
|
1029 |
$extra_fields = array( 'version', 'locale' ); |
|
1030 |
|
|
1031 |
$credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => '')); |
|
1032 |
|
|
1033 |
// If defined, set it to that, Else, If POST'd, set it to that, If not, Set it to whatever it previously was(saved details in option) |
|
1034 |
$credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? wp_unslash( $_POST['hostname'] ) : $credentials['hostname']); |
|
1035 |
$credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? wp_unslash( $_POST['username'] ) : $credentials['username']); |
|
1036 |
$credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? wp_unslash( $_POST['password'] ) : ''); |
|
1037 |
|
|
1038 |
// Check to see if we are setting the public/private keys for ssh |
|
1039 |
$credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? wp_unslash( $_POST['public_key'] ) : ''); |
|
1040 |
$credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? wp_unslash( $_POST['private_key'] ) : ''); |
|
1041 |
|
5
|
1042 |
// Sanitize the hostname, Some people might pass in odd-data: |
0
|
1043 |
$credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off |
|
1044 |
|
|
1045 |
if ( strpos($credentials['hostname'], ':') ) { |
|
1046 |
list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2); |
|
1047 |
if ( ! is_numeric($credentials['port']) ) |
|
1048 |
unset($credentials['port']); |
|
1049 |
} else { |
|
1050 |
unset($credentials['port']); |
|
1051 |
} |
|
1052 |
|
5
|
1053 |
if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { |
0
|
1054 |
$credentials['connection_type'] = 'ssh'; |
5
|
1055 |
} elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { //Only the FTP Extension understands SSL |
0
|
1056 |
$credentials['connection_type'] = 'ftps'; |
5
|
1057 |
} elseif ( ! empty( $_POST['connection_type'] ) ) { |
0
|
1058 |
$credentials['connection_type'] = wp_unslash( $_POST['connection_type'] ); |
5
|
1059 |
} elseif ( ! isset( $credentials['connection_type'] ) ) { //All else fails (And it's not defaulted to something else saved), Default to FTP |
0
|
1060 |
$credentials['connection_type'] = 'ftp'; |
5
|
1061 |
} |
0
|
1062 |
if ( ! $error && |
|
1063 |
( |
|
1064 |
( !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) || |
|
1065 |
( 'ssh' == $credentials['connection_type'] && !empty($credentials['public_key']) && !empty($credentials['private_key']) ) |
|
1066 |
) ) { |
|
1067 |
$stored_credentials = $credentials; |
|
1068 |
if ( !empty($stored_credentials['port']) ) //save port as part of hostname to simplify above code. |
|
1069 |
$stored_credentials['hostname'] .= ':' . $stored_credentials['port']; |
|
1070 |
|
|
1071 |
unset($stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key']); |
5
|
1072 |
if ( ! defined( 'WP_INSTALLING' ) ) { |
|
1073 |
update_option( 'ftp_credentials', $stored_credentials ); |
|
1074 |
} |
0
|
1075 |
return $credentials; |
|
1076 |
} |
5
|
1077 |
$hostname = isset( $credentials['hostname'] ) ? $credentials['hostname'] : ''; |
|
1078 |
$username = isset( $credentials['username'] ) ? $credentials['username'] : ''; |
|
1079 |
$public_key = isset( $credentials['public_key'] ) ? $credentials['public_key'] : ''; |
|
1080 |
$private_key = isset( $credentials['private_key'] ) ? $credentials['private_key'] : ''; |
|
1081 |
$port = isset( $credentials['port'] ) ? $credentials['port'] : ''; |
|
1082 |
$connection_type = isset( $credentials['connection_type'] ) ? $credentials['connection_type'] : ''; |
|
1083 |
|
0
|
1084 |
if ( $error ) { |
|
1085 |
$error_string = __('<strong>ERROR:</strong> There was an error connecting to the server, Please verify the settings are correct.'); |
|
1086 |
if ( is_wp_error($error) ) |
|
1087 |
$error_string = esc_html( $error->get_error_message() ); |
|
1088 |
echo '<div id="message" class="error"><p>' . $error_string . '</p></div>'; |
|
1089 |
} |
|
1090 |
|
|
1091 |
$types = array(); |
|
1092 |
if ( extension_loaded('ftp') || extension_loaded('sockets') || function_exists('fsockopen') ) |
|
1093 |
$types[ 'ftp' ] = __('FTP'); |
|
1094 |
if ( extension_loaded('ftp') ) //Only this supports FTPS |
|
1095 |
$types[ 'ftps' ] = __('FTPS (SSL)'); |
|
1096 |
if ( extension_loaded('ssh2') && function_exists('stream_get_contents') ) |
|
1097 |
$types[ 'ssh' ] = __('SSH2'); |
|
1098 |
|
5
|
1099 |
/** |
|
1100 |
* Filter the connection types to output to the filesystem credentials form. |
|
1101 |
* |
|
1102 |
* @since 2.9.0 |
|
1103 |
* |
|
1104 |
* @param array $types Types of connections. |
|
1105 |
* @param array $credentials Credentials to connect with. |
|
1106 |
* @param string $type Chosen filesystem method. |
|
1107 |
* @param object $error Error object. |
|
1108 |
* @param string $context Full path to the directory that is tested |
|
1109 |
* for being writable. |
|
1110 |
*/ |
|
1111 |
$types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context ); |
0
|
1112 |
|
|
1113 |
?> |
|
1114 |
<script type="text/javascript"> |
|
1115 |
<!-- |
|
1116 |
jQuery(function($){ |
|
1117 |
jQuery("#ssh").click(function () { |
|
1118 |
jQuery("#ssh_keys").show(); |
|
1119 |
}); |
|
1120 |
jQuery("#ftp, #ftps").click(function () { |
|
1121 |
jQuery("#ssh_keys").hide(); |
|
1122 |
}); |
5
|
1123 |
jQuery('#request-filesystem-credentials-form input[value=""]:first').focus(); |
0
|
1124 |
}); |
|
1125 |
--> |
|
1126 |
</script> |
|
1127 |
<form action="<?php echo esc_url( $form_post ) ?>" method="post"> |
5
|
1128 |
<div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form"> |
|
1129 |
<h3 id="request-filesystem-credentials-title"><?php _e( 'Connection Information' ) ?></h3> |
|
1130 |
<p id="request-filesystem-credentials-desc"><?php |
0
|
1131 |
$label_user = __('Username'); |
|
1132 |
$label_pass = __('Password'); |
|
1133 |
_e('To perform the requested action, WordPress needs to access your web server.'); |
|
1134 |
echo ' '; |
|
1135 |
if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) { |
|
1136 |
if ( isset( $types['ssh'] ) ) { |
|
1137 |
_e('Please enter your FTP or SSH credentials to proceed.'); |
|
1138 |
$label_user = __('FTP/SSH Username'); |
|
1139 |
$label_pass = __('FTP/SSH Password'); |
|
1140 |
} else { |
|
1141 |
_e('Please enter your FTP credentials to proceed.'); |
|
1142 |
$label_user = __('FTP Username'); |
|
1143 |
$label_pass = __('FTP Password'); |
|
1144 |
} |
|
1145 |
echo ' '; |
|
1146 |
} |
|
1147 |
_e('If you do not remember your credentials, you should contact your web host.'); |
|
1148 |
?></p> |
5
|
1149 |
<label for="hostname"> |
|
1150 |
<span class="field-title"><?php _e( 'Hostname' ) ?></span> |
|
1151 |
<input name="hostname" type="text" id="hostname" aria-describedby="request-filesystem-credentials-desc" class="code" placeholder="<?php esc_attr_e( 'example: www.wordpress.org' ) ?>" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php disabled( defined('FTP_HOST') ); ?> /> |
|
1152 |
</label> |
|
1153 |
<div class="ftp-username"> |
|
1154 |
<label for="username"> |
|
1155 |
<span class="field-title"><?php echo $label_user; ?></span> |
|
1156 |
<input name="username" type="text" id="username" value="<?php echo esc_attr($username) ?>"<?php disabled( defined('FTP_USER') ); ?> /> |
|
1157 |
</label> |
|
1158 |
</div> |
|
1159 |
<div class="ftp-password"> |
|
1160 |
<label for="password"> |
|
1161 |
<span class="field-title"><?php echo $label_pass; ?></span> |
|
1162 |
<input name="password" type="password" id="password" value="<?php if ( defined('FTP_PASS') ) echo '*****'; ?>"<?php disabled( defined('FTP_PASS') ); ?> /> |
|
1163 |
<em><?php if ( ! defined('FTP_PASS') ) _e( 'This password will not be stored on the server.' ); ?></em> |
|
1164 |
</label> |
|
1165 |
</div> |
0
|
1166 |
<?php if ( isset($types['ssh']) ) : ?> |
5
|
1167 |
<h4><?php _e('Authentication Keys') ?></h4> |
|
1168 |
<label for="public_key"> |
|
1169 |
<span class="field-title"><?php _e('Public Key:') ?></span> |
|
1170 |
<input name="public_key" type="text" id="public_key" aria-describedby="auth-keys-desc" value="<?php echo esc_attr($public_key) ?>"<?php disabled( defined('FTP_PUBKEY') ); ?> /> |
|
1171 |
</label> |
|
1172 |
<label for="private_key"> |
|
1173 |
<span class="field-title"><?php _e('Private Key:') ?></span> |
|
1174 |
<input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php disabled( defined('FTP_PRIKEY') ); ?> /> |
|
1175 |
</label> |
|
1176 |
<span id="auth-keys-desc"><?php _e('Enter the location on the server where the public and private keys are located. If a passphrase is needed, enter that in the password field above.') ?></span> |
0
|
1177 |
<?php endif; ?> |
5
|
1178 |
<h4><?php _e('Connection Type') ?></h4> |
0
|
1179 |
<fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend> |
|
1180 |
<?php |
|
1181 |
$disabled = disabled( (defined('FTP_SSL') && FTP_SSL) || (defined('FTP_SSH') && FTP_SSH), true, false ); |
|
1182 |
foreach ( $types as $name => $text ) : ?> |
|
1183 |
<label for="<?php echo esc_attr($name) ?>"> |
|
1184 |
<input type="radio" name="connection_type" id="<?php echo esc_attr($name) ?>" value="<?php echo esc_attr($name) ?>"<?php checked($name, $connection_type); echo $disabled; ?> /> |
|
1185 |
<?php echo $text ?> |
|
1186 |
</label> |
|
1187 |
<?php endforeach; ?> |
|
1188 |
</fieldset> |
|
1189 |
<?php |
|
1190 |
foreach ( (array) $extra_fields as $field ) { |
|
1191 |
if ( isset( $_POST[ $field ] ) ) |
|
1192 |
echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( wp_unslash( $_POST[ $field ] ) ) . '" />'; |
|
1193 |
} |
|
1194 |
?> |
5
|
1195 |
<p class="request-filesystem-credentials-action-buttons"> |
|
1196 |
<button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button> |
|
1197 |
<?php submit_button( __( 'Proceed' ), 'button', 'upgrade', false ); ?> |
|
1198 |
</p> |
0
|
1199 |
</div> |
|
1200 |
</form> |
|
1201 |
<?php |
|
1202 |
return false; |
|
1203 |
} |
5
|
1204 |
|
|
1205 |
/** |
|
1206 |
* Print the filesystem credentials modal when needed. |
|
1207 |
* |
|
1208 |
* @since 4.2.0 |
|
1209 |
*/ |
|
1210 |
function wp_print_request_filesystem_credentials_modal() { |
|
1211 |
$filesystem_method = get_filesystem_method(); |
|
1212 |
ob_start(); |
|
1213 |
$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
|
1214 |
ob_end_clean(); |
|
1215 |
$request_filesystem_credentials = ( $filesystem_method != 'direct' && ! $filesystem_credentials_are_stored ); |
|
1216 |
if ( ! $request_filesystem_credentials ) { |
|
1217 |
return; |
|
1218 |
} |
|
1219 |
?> |
|
1220 |
<div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog"> |
|
1221 |
<div class="notification-dialog-background"></div> |
|
1222 |
<div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0"> |
|
1223 |
<div class="request-filesystem-credentials-dialog-content"> |
|
1224 |
<?php request_filesystem_credentials( site_url() ); ?> |
|
1225 |
<div> |
|
1226 |
</div> |
|
1227 |
</div> |
|
1228 |
<?php |
|
1229 |
} |