| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Filesystem API: Top-level functionality 4 * 5 * Functions for reading, writing, modifying, and deleting files on the file system. 6 * Includes functionality for theme-specific files as well as operations for uploading, 7 * archiving, and rendering output when necessary. 8 * 9 * @package WordPress 10 * @subpackage Filesystem 11 * @since 2.3.0 12 */ 13 14 /** The descriptions for theme files. */ 15 $wp_file_descriptions = array( 16 'functions.php' => __( 'Theme Functions' ), 17 'header.php' => __( 'Theme Header' ), 18 'footer.php' => __( 'Theme Footer' ), 19 'sidebar.php' => __( 'Sidebar' ), 20 'comments.php' => __( 'Comments' ), 21 'searchform.php' => __( 'Search Form' ), 22 '404.php' => __( '404 Template' ), 23 'link.php' => __( 'Links Template' ), 24 'theme.json' => __( 'Theme Styles & Block Settings' ), 25 // Archives. 26 'index.php' => __( 'Main Index Template' ), 27 'archive.php' => __( 'Archives' ), 28 'author.php' => __( 'Author Template' ), 29 'taxonomy.php' => __( 'Taxonomy Template' ), 30 'category.php' => __( 'Category Template' ), 31 'tag.php' => __( 'Tag Template' ), 32 'home.php' => __( 'Posts Page' ), 33 'search.php' => __( 'Search Results' ), 34 'date.php' => __( 'Date Template' ), 35 // Content. 36 'singular.php' => __( 'Singular Template' ), 37 'single.php' => __( 'Single Post' ), 38 'page.php' => __( 'Single Page' ), 39 'front-page.php' => __( 'Homepage' ), 40 'privacy-policy.php' => __( 'Privacy Policy Page' ), 41 // Attachments. 42 'attachment.php' => __( 'Attachment Template' ), 43 'image.php' => __( 'Image Attachment Template' ), 44 'video.php' => __( 'Video Attachment Template' ), 45 'audio.php' => __( 'Audio Attachment Template' ), 46 'application.php' => __( 'Application Attachment Template' ), 47 // Embeds. 48 'embed.php' => __( 'Embed Template' ), 49 'embed-404.php' => __( 'Embed 404 Template' ), 50 'embed-content.php' => __( 'Embed Content Template' ), 51 'header-embed.php' => __( 'Embed Header Template' ), 52 'footer-embed.php' => __( 'Embed Footer Template' ), 53 // Stylesheets. 54 'style.css' => __( 'Stylesheet' ), 55 'editor-style.css' => __( 'Visual Editor Stylesheet' ), 56 'editor-style-rtl.css' => __( 'Visual Editor RTL Stylesheet' ), 57 'rtl.css' => __( 'RTL Stylesheet' ), 58 // Other. 59 'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ), 60 '.htaccess' => __( '.htaccess (for rewrite rules )' ), 61 // Deprecated files. 62 'wp-layout.css' => __( 'Stylesheet' ), 63 'wp-comments.php' => __( 'Comments Template' ), 64 'wp-comments-popup.php' => __( 'Popup Comments Template' ), 65 'comments-popup.php' => __( 'Popup Comments' ), 66 ); 67 68 /** 69 * Gets the description for standard WordPress theme files. 70 * 71 * @since 1.5.0 72 * 73 * @global array $wp_file_descriptions Theme file descriptions. 74 * @global array $allowed_files List of allowed files. 75 * 76 * @param string $file Filesystem path or filename. 77 * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist. 78 * Appends 'Page Template' to basename of $file if the file is a page template. 79 */ 80 function get_file_description( $file ) { 81 global $wp_file_descriptions, $allowed_files; 82 83 $dirname = pathinfo( $file, PATHINFO_DIRNAME ); 84 $file_path = $allowed_files[ $file ]; 85 86 if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) { 87 return $wp_file_descriptions[ basename( $file ) ]; 88 } elseif ( file_exists( $file_path ) && is_file( $file_path ) ) { 89 $template_data = implode( '', file( $file_path ) ); 90 91 if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) ) { 92 /* translators: %s: Template name. */ 93 return sprintf( __( '%s Page Template' ), _cleanup_header_comment( $name[1] ) ); 94 } 95 } 96 97 return trim( basename( $file ) ); 98 } 99 100 /** 101 * Gets the absolute filesystem path to the root of the WordPress installation. 102 * 103 * @since 1.5.0 104 * 105 * @return string Full filesystem path to the root of the WordPress installation. 106 */ 107 function get_home_path() { 108 $home = set_url_scheme( get_option( 'home' ), 'http' ); 109 $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' ); 110 111 if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) { 112 $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */ 113 $pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) ); 114 $home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos ); 115 $home_path = trailingslashit( $home_path ); 116 } else { 117 $home_path = ABSPATH; 118 } 119 120 return str_replace( '\\', '/', $home_path ); 121 } 122 123 /** 124 * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep. 125 * 126 * The depth of the recursiveness can be controlled by the $levels param. 127 * 128 * @since 2.6.0 129 * @since 4.9.0 Added the `$exclusions` parameter. 130 * @since 6.3.0 Added the `$include_hidden` parameter. 131 * 132 * @param string $folder Optional. Full path to folder. Default empty. 133 * @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit). 134 * @param string[] $exclusions Optional. List of folders and files to skip. 135 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. 136 * Default false. 137 * @return string[]|false Array of files on success, false on failure. 138 */ 139 function list_files( $folder = '', $levels = 100, $exclusions = array(), $include_hidden = false ) { 140 if ( empty( $folder ) ) { 141 return false; 142 } 143 144 $folder = trailingslashit( $folder ); 145 146 if ( ! $levels ) { 147 return false; 148 } 149 150 $files = array(); 151 152 $dir = @opendir( $folder ); 153 154 if ( $dir ) { 155 while ( ( $file = readdir( $dir ) ) !== false ) { 156 // Skip current and parent folder links. 157 if ( in_array( $file, array( '.', '..' ), true ) ) { 158 continue; 159 } 160 161 // Skip hidden and excluded files. 162 if ( ( ! $include_hidden && '.' === $file[0] ) || in_array( $file, $exclusions, true ) ) { 163 continue; 164 } 165 166 if ( is_dir( $folder . $file ) ) { 167 $files2 = list_files( $folder . $file, $levels - 1, array(), $include_hidden ); 168 if ( $files2 ) { 169 $files = array_merge( $files, $files2 ); 170 } else { 171 $files[] = $folder . $file . '/'; 172 } 173 } else { 174 $files[] = $folder . $file; 175 } 176 } 177 178 closedir( $dir ); 179 } 180 181 return $files; 182 } 183 184 /** 185 * Gets the list of file extensions that are editable in plugins. 186 * 187 * @since 4.9.0 188 * 189 * @param string $plugin Path to the plugin file relative to the plugins directory. 190 * @return string[] Array of editable file extensions. 191 */ 192 function wp_get_plugin_file_editable_extensions( $plugin ) { 193 194 $default_types = array( 195 'bash', 196 'conf', 197 'css', 198 'diff', 199 'htm', 200 'html', 201 'http', 202 'inc', 203 'include', 204 'js', 205 'mjs', 206 'json', 207 'jsx', 208 'less', 209 'md', 210 'patch', 211 'php', 212 'php3', 213 'php4', 214 'php5', 215 'php7', 216 'phps', 217 'phtml', 218 'sass', 219 'scss', 220 'sh', 221 'sql', 222 'svg', 223 'text', 224 'txt', 225 'xml', 226 'yaml', 227 'yml', 228 ); 229 230 /** 231 * Filters the list of file types allowed for editing in the plugin file editor. 232 * 233 * @since 2.8.0 234 * @since 4.9.0 Added the `$plugin` parameter. 235 * 236 * @param string[] $default_types An array of editable plugin file extensions. 237 * @param string $plugin Path to the plugin file relative to the plugins directory. 238 */ 239 $file_types = (array) apply_filters( 'editable_extensions', $default_types, $plugin ); 240 241 return $file_types; 242 } 243 244 /** 245 * Gets the list of file extensions that are editable for a given theme. 246 * 247 * @since 4.9.0 248 * 249 * @param WP_Theme $theme Theme object. 250 * @return string[] Array of editable file extensions. 251 */ 252 function wp_get_theme_file_editable_extensions( $theme ) { 253 254 $default_types = array( 255 'bash', 256 'conf', 257 'css', 258 'diff', 259 'htm', 260 'html', 261 'http', 262 'inc', 263 'include', 264 'js', 265 'mjs', 266 'json', 267 'jsx', 268 'less', 269 'md', 270 'patch', 271 'php', 272 'php3', 273 'php4', 274 'php5', 275 'php7', 276 'phps', 277 'phtml', 278 'sass', 279 'scss', 280 'sh', 281 'sql', 282 'svg', 283 'text', 284 'txt', 285 'xml', 286 'yaml', 287 'yml', 288 ); 289 290 /** 291 * Filters the list of file types allowed for editing in the theme file editor. 292 * 293 * @since 4.4.0 294 * 295 * @param string[] $default_types An array of editable theme file extensions. 296 * @param WP_Theme $theme The active theme object. 297 */ 298 $file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme ); 299 300 // Ensure that default types are still there. 301 return array_unique( array_merge( $file_types, $default_types ) ); 302 } 303 304 /** 305 * Prints file editor templates (for plugins and themes). 306 * 307 * @since 4.9.0 308 */ 309 function wp_print_file_editor_templates() { 310 ?> 311 <script type="text/html" id="tmpl-wp-file-editor-notice"> 312 <div class="notice inline notice-{{ data.type || 'info' }} {{ data.alt ? 'notice-alt' : '' }} {{ data.dismissible ? 'is-dismissible' : '' }} {{ data.classes || '' }}"> 313 <# if ( 'php_error' === data.code ) { #> 314 <p> 315 <?php 316 printf( 317 /* translators: 1: Line number, 2: File path. */ 318 __( 'Your PHP code changes were not applied due to an error on line %1$s of file %2$s. Please fix and try saving again.' ), 319 '{{ data.line }}', 320 '{{ data.file }}' 321 ); 322 ?> 323 </p> 324 <pre>{{ data.message }}</pre> 325 <# } else if ( 'file_not_writable' === data.code ) { #> 326 <p> 327 <?php 328 printf( 329 /* translators: %s: Documentation URL. */ 330 __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ), 331 __( 'https://developer.wordpress.org/advanced-administration/server/file-permissions/' ) 332 ); 333 ?> 334 </p> 335 <# } else { #> 336 <p>{{ data.message || data.code }}</p> 337 338 <# if ( 'lint_errors' === data.code ) { #> 339 <p> 340 <# var elementId = 'el-' + String( Math.random() ); #> 341 <input id="{{ elementId }}" type="checkbox"> 342 <label for="{{ elementId }}"><?php _e( 'Update anyway, even though it might break your site?' ); ?></label> 343 </p> 344 <# } #> 345 <# } #> 346 <# if ( data.dismissible ) { #> 347 <button type="button" class="notice-dismiss"><span class="screen-reader-text"> 348 <?php 349 /* translators: Hidden accessibility text. */ 350 _e( 'Dismiss' ); 351 ?> 352 </span></button> 353 <# } #> 354 </div> 355 </script> 356 <?php 357 } 358 359 /** 360 * Attempts to edit a file for a theme or plugin. 361 * 362 * When editing a PHP file, loopback requests will be made to the admin and the homepage 363 * to attempt to see if there is a fatal error introduced. If so, the PHP change will be 364 * reverted. 365 * 366 * @since 4.9.0 367 * 368 * @param string[] $args { 369 * Args. Note that all of the arg values are already unslashed. They are, however, 370 * coming straight from `$_POST` and are not validated or sanitized in any way. 371 * 372 * @type string $file Relative path to file. 373 * @type string $plugin Path to the plugin file relative to the plugins directory. 374 * @type string $theme Theme being edited. 375 * @type string $newcontent New content for the file. 376 * @type string $nonce Nonce. 377 * } 378 * @return true|WP_Error True on success or `WP_Error` on failure. 379 */ 380 function wp_edit_theme_plugin_file( $args ) { 381 if ( empty( $args['file'] ) ) { 382 return new WP_Error( 'missing_file' ); 383 } 384 385 if ( 0 !== validate_file( $args['file'] ) ) { 386 return new WP_Error( 'bad_file' ); 387 } 388 389 if ( ! isset( $args['newcontent'] ) ) { 390 return new WP_Error( 'missing_content' ); 391 } 392 393 if ( ! isset( $args['nonce'] ) ) { 394 return new WP_Error( 'missing_nonce' ); 395 } 396 397 $file = $args['file']; 398 $content = $args['newcontent']; 399 400 $plugin = null; 401 $theme = null; 402 $real_file = null; 403 404 if ( ! empty( $args['plugin'] ) ) { 405 $plugin = $args['plugin']; 406 407 if ( ! current_user_can( 'edit_plugins' ) ) { 408 return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit plugins for this site.' ) ); 409 } 410 411 if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) { 412 return new WP_Error( 'nonce_failure' ); 413 } 414 415 if ( ! array_key_exists( $plugin, get_plugins() ) ) { 416 return new WP_Error( 'invalid_plugin' ); 417 } 418 419 if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) { 420 return new WP_Error( 'bad_plugin_file_path', __( 'Sorry, that file cannot be edited.' ) ); 421 } 422 423 $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin ); 424 425 $real_file = WP_PLUGIN_DIR . '/' . $file; 426 427 $is_active = in_array( 428 $plugin, 429 (array) get_option( 'active_plugins', array() ), 430 true 431 ); 432 433 } elseif ( ! empty( $args['theme'] ) ) { 434 $stylesheet = $args['theme']; 435 436 if ( 0 !== validate_file( $stylesheet ) ) { 437 return new WP_Error( 'bad_theme_path' ); 438 } 439 440 if ( ! current_user_can( 'edit_themes' ) ) { 441 return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit templates for this site.' ) ); 442 } 443 444 $theme = wp_get_theme( $stylesheet ); 445 if ( ! $theme->exists() ) { 446 return new WP_Error( 'non_existent_theme', __( 'The requested theme does not exist.' ) ); 447 } 448 449 if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $stylesheet . '_' . $file ) ) { 450 return new WP_Error( 'nonce_failure' ); 451 } 452 453 if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) { 454 return new WP_Error( 455 'theme_no_stylesheet', 456 __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message() 457 ); 458 } 459 460 $editable_extensions = wp_get_theme_file_editable_extensions( $theme ); 461 462 $allowed_files = array(); 463 foreach ( $editable_extensions as $type ) { 464 switch ( $type ) { 465 case 'php': 466 $allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) ); 467 break; 468 case 'css': 469 $style_files = $theme->get_files( 'css', -1 ); 470 $allowed_files['style.css'] = $style_files['style.css']; 471 $allowed_files = array_merge( $allowed_files, $style_files ); 472 break; 473 default: 474 $allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) ); 475 break; 476 } 477 } 478 479 // Compare based on relative paths. 480 if ( 0 !== validate_file( $file, array_keys( $allowed_files ) ) ) { 481 return new WP_Error( 'disallowed_theme_file', __( 'Sorry, that file cannot be edited.' ) ); 482 } 483 484 $real_file = $theme->get_stylesheet_directory() . '/' . $file; 485 486 $is_active = ( get_stylesheet() === $stylesheet || get_template() === $stylesheet ); 487 488 } else { 489 return new WP_Error( 'missing_theme_or_plugin' ); 490 } 491 492 // Ensure file is real. 493 if ( ! is_file( $real_file ) ) { 494 return new WP_Error( 'file_does_not_exist', __( 'File does not exist! Please double check the name and try again.' ) ); 495 } 496 497 // Ensure file extension is allowed. 498 $extension = null; 499 if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) { 500 $extension = strtolower( $matches[1] ); 501 if ( ! in_array( $extension, $editable_extensions, true ) ) { 502 return new WP_Error( 'illegal_file_type', __( 'Files of this type are not editable.' ) ); 503 } 504 } 505 506 $previous_content = file_get_contents( $real_file ); 507 508 if ( ! is_writable( $real_file ) ) { 509 return new WP_Error( 'file_not_writable' ); 510 } 511 512 $f = fopen( $real_file, 'w+' ); 513 514 if ( false === $f ) { 515 return new WP_Error( 'file_not_writable' ); 516 } 517 518 $written = fwrite( $f, $content ); 519 fclose( $f ); 520 521 if ( false === $written ) { 522 return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) ); 523 } 524 525 wp_opcache_invalidate( $real_file, true ); 526 527 if ( $is_active && 'php' === $extension ) { 528 529 $scrape_key = md5( rand() ); 530 $transient = 'scrape_key_' . $scrape_key; 531 $scrape_nonce = (string) rand(); 532 // It shouldn't take more than 60 seconds to make the two loopback requests. 533 set_transient( $transient, $scrape_nonce, 60 ); 534 535 $cookies = wp_unslash( $_COOKIE ); 536 $scrape_params = array( 537 'wp_scrape_key' => $scrape_key, 538 'wp_scrape_nonce' => $scrape_nonce, 539 ); 540 $headers = array( 541 'Cache-Control' => 'no-cache', 542 ); 543 544 /** This filter is documented in wp-includes/class-wp-http-streams.php */ 545 $sslverify = apply_filters( 'https_local_ssl_verify', false ); 546 547 // Include Basic auth in loopback requests. 548 if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) { 549 $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ); 550 } 551 552 // Make sure PHP process doesn't die before loopback requests complete. 553 if ( function_exists( 'set_time_limit' ) ) { 554 set_time_limit( 5 * MINUTE_IN_SECONDS ); 555 } 556 557 // Time to wait for loopback requests to finish. 558 $timeout = 100; // 100 seconds. 559 560 $needle_start = "###### wp_scraping_result_start:$scrape_key ######"; 561 $needle_end = "###### wp_scraping_result_end:$scrape_key ######"; 562 563 // Attempt loopback request to editor to see if user just whitescreened themselves. 564 if ( $plugin ) { 565 $url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) ); 566 } elseif ( isset( $stylesheet ) ) { 567 $url = add_query_arg( 568 array( 569 'theme' => $stylesheet, 570 'file' => $file, 571 ), 572 admin_url( 'theme-editor.php' ) 573 ); 574 } else { 575 $url = admin_url(); 576 } 577 578 if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) { 579 /* 580 * Close any active session to prevent HTTP requests from timing out 581 * when attempting to connect back to the site. 582 */ 583 session_write_close(); 584 } 585 586 $url = add_query_arg( $scrape_params, $url ); 587 $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) ); 588 $body = wp_remote_retrieve_body( $r ); 589 $scrape_result_position = strpos( $body, $needle_start ); 590 591 $loopback_request_failure = array( 592 'code' => 'loopback_request_failed', 593 'message' => __( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ), 594 ); 595 $json_parse_failure = array( 596 'code' => 'json_parse_error', 597 ); 598 599 $result = null; 600 601 if ( false === $scrape_result_position ) { 602 $result = $loopback_request_failure; 603 } else { 604 $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) ); 605 $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) ); 606 $result = json_decode( trim( $error_output ), true ); 607 if ( empty( $result ) ) { 608 $result = $json_parse_failure; 609 } 610 } 611 612 // Try making request to homepage as well to see if visitors have been whitescreened. 613 if ( true === $result ) { 614 $url = home_url( '/' ); 615 $url = add_query_arg( $scrape_params, $url ); 616 $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) ); 617 $body = wp_remote_retrieve_body( $r ); 618 $scrape_result_position = strpos( $body, $needle_start ); 619 620 if ( false === $scrape_result_position ) { 621 $result = $loopback_request_failure; 622 } else { 623 $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) ); 624 $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) ); 625 $result = json_decode( trim( $error_output ), true ); 626 if ( empty( $result ) ) { 627 $result = $json_parse_failure; 628 } 629 } 630 } 631 632 delete_transient( $transient ); 633 634 if ( true !== $result ) { 635 // Roll-back file change. 636 file_put_contents( $real_file, $previous_content ); 637 wp_opcache_invalidate( $real_file, true ); 638 639 if ( ! isset( $result['message'] ) ) { 640 $message = __( 'An error occurred. Please try again later.' ); 641 } else { 642 $message = $result['message']; 643 unset( $result['message'] ); 644 } 645 646 return new WP_Error( 'php_error', $message, $result ); 647 } 648 } 649 650 if ( $theme instanceof WP_Theme ) { 651 $theme->cache_delete(); 652 } 653 654 return true; 655 } 656 657 658 /** 659 * Returns a filename of a temporary unique file. 660 * 661 * Please note that the calling function must delete or move the file. 662 * 663 * The filename is based off the passed parameter or defaults to the current unix timestamp, 664 * while the directory can either be passed as well, or by leaving it blank, default to a writable 665 * temporary directory. 666 * 667 * @since 2.6.0 668 * 669 * @param string $filename Optional. Filename to base the Unique file off. Default empty. 670 * @param string $dir Optional. Directory to store the file in. Default empty. 671 * @return string A writable filename. 672 */ 673 function wp_tempnam( $filename = '', $dir = '' ) { 674 if ( empty( $dir ) ) { 675 $dir = get_temp_dir(); 676 } 677 678 if ( empty( $filename ) || in_array( $filename, array( '.', '/', '\\' ), true ) ) { 679 $filename = uniqid(); 680 } 681 682 // Use the basename of the given file without the extension as the name for the temporary directory. 683 $temp_filename = basename( $filename ); 684 $temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename ); 685 686 // If the folder is falsey, use its parent directory name instead. 687 if ( ! $temp_filename ) { 688 return wp_tempnam( dirname( $filename ), $dir ); 689 } 690 691 // Suffix some random data to avoid filename conflicts. 692 $temp_filename .= '-' . wp_generate_password( 6, false ); 693 $temp_filename .= '.tmp'; 694 $temp_filename = wp_unique_filename( $dir, $temp_filename ); 695 696 /* 697 * Filesystems typically have a limit of 255 characters for a filename. 698 * 699 * If the generated unique filename exceeds this, truncate the initial 700 * filename and try again. 701 * 702 * As it's possible that the truncated filename may exist, producing a 703 * suffix of "-1" or "-10" which could exceed the limit again, truncate 704 * it to 252 instead. 705 */ 706 $characters_over_limit = strlen( $temp_filename ) - 252; 707 if ( $characters_over_limit > 0 ) { 708 $filename = substr( $filename, 0, -$characters_over_limit ); 709 return wp_tempnam( $filename, $dir ); 710 } 711 712 $temp_filename = $dir . $temp_filename; 713 714 $fp = @fopen( $temp_filename, 'x' ); 715 716 if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) { 717 return wp_tempnam( $filename, $dir ); 718 } 719 720 if ( $fp ) { 721 fclose( $fp ); 722 } 723 724 return $temp_filename; 725 } 726 727 /** 728 * Makes sure that the file that was requested to be edited is allowed to be edited. 729 * 730 * Function will die if you are not allowed to edit the file. 731 * 732 * @since 1.5.0 733 * 734 * @param string $file File the user is attempting to edit. 735 * @param string[] $allowed_files Optional. Array of allowed files to edit. 736 * `$file` must match an entry exactly. 737 * @return string|null Returns the file name on success, null in case of absolute Windows drive paths, and dies on failure. 738 */ 739 function validate_file_to_edit( $file, $allowed_files = array() ) { 740 $code = validate_file( $file, $allowed_files ); 741 742 if ( ! $code ) { 743 return $file; 744 } 745 746 switch ( $code ) { 747 case 1: 748 wp_die( __( 'Sorry, that file cannot be edited.' ) ); 749 750 // case 2 : 751 // wp_die( __('Sorry, cannot call files with their real path.' )); 752 753 case 3: 754 wp_die( __( 'Sorry, that file cannot be edited.' ) ); 755 } 756 return null; 757 } 758 759 /** 760 * Handles PHP uploads in WordPress. 761 * 762 * Sanitizes file names, checks extensions for mime type, and moves the file 763 * to the appropriate directory within the uploads directory. 764 * 765 * @access private 766 * @since 4.0.0 767 * 768 * @see wp_handle_upload_error 769 * 770 * @param array $file { 771 * Reference to a single element from `$_FILES`. Call the function once for each uploaded file. 772 * 773 * @type string $name The original name of the file on the client machine. 774 * @type string $type The mime type of the file, if the browser provided this information. 775 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 776 * @type int $size The size, in bytes, of the uploaded file. 777 * @type int $error The error code associated with this file upload. 778 * } 779 * @param array|false $overrides { 780 * An array of override parameters for this file, or boolean false if none are provided. 781 * 782 * @type callable $upload_error_handler Function to call when there is an error during the upload process. 783 * See {@see wp_handle_upload_error()}. 784 * @type callable $unique_filename_callback Function to call when determining a unique file name for the file. 785 * See {@see wp_unique_filename()}. 786 * @type string[] $upload_error_strings The strings that describe the error indicated in 787 * `$_FILES[{form field}]['error']`. 788 * @type bool $test_form Whether to test that the `$_POST['action']` parameter is as expected. 789 * @type bool $test_size Whether to test that the file size is greater than zero bytes. 790 * @type bool $test_type Whether to test that the mime type of the file is as expected. 791 * @type string[] $mimes Array of allowed mime types keyed by their file extension regex. 792 * } 793 * @param string $time Time formatted in 'yyyy/mm'. 794 * @param string $action Expected value for `$_POST['action']`. 795 * @return array { 796 * On success, returns an associative array of file attributes. 797 * On failure, returns `$overrides['upload_error_handler']( &$file, $message )` 798 * or `array( 'error' => $message )`. 799 * 800 * @type string $file Filename of the newly-uploaded file. 801 * @type string $url URL of the newly-uploaded file. 802 * @type string $type Mime type of the newly-uploaded file. 803 * } 804 */ 805 function _wp_handle_upload( &$file, $overrides, $time, $action ) { 806 // The default error handler. 807 if ( ! function_exists( 'wp_handle_upload_error' ) ) { 808 function wp_handle_upload_error( &$file, $message ) { 809 return array( 'error' => $message ); 810 } 811 } 812 813 /** 814 * Filters the data for a file before it is uploaded to WordPress. 815 * 816 * The dynamic portion of the hook name, `$action`, refers to the post action. 817 * 818 * Possible hook names include: 819 * 820 * - `wp_handle_sideload_prefilter` 821 * - `wp_handle_upload_prefilter` 822 * 823 * @since 2.9.0 as 'wp_handle_upload_prefilter'. 824 * @since 4.0.0 Converted to a dynamic hook with `$action`. 825 * 826 * @param array $file { 827 * Reference to a single element from `$_FILES`. 828 * 829 * @type string $name The original name of the file on the client machine. 830 * @type string $type The mime type of the file, if the browser provided this information. 831 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 832 * @type int $size The size, in bytes, of the uploaded file. 833 * @type int $error The error code associated with this file upload. 834 * } 835 */ 836 $file = apply_filters( "{$action}_prefilter", $file ); 837 838 /** 839 * Filters the override parameters for a file before it is uploaded to WordPress. 840 * 841 * The dynamic portion of the hook name, `$action`, refers to the post action. 842 * 843 * Possible hook names include: 844 * 845 * - `wp_handle_sideload_overrides` 846 * - `wp_handle_upload_overrides` 847 * 848 * @since 5.7.0 849 * 850 * @param array|false $overrides An array of override parameters for this file. Boolean false if none are 851 * provided. See {@see _wp_handle_upload()}. 852 * @param array $file { 853 * Reference to a single element from `$_FILES`. 854 * 855 * @type string $name The original name of the file on the client machine. 856 * @type string $type The mime type of the file, if the browser provided this information. 857 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 858 * @type int $size The size, in bytes, of the uploaded file. 859 * @type int $error The error code associated with this file upload. 860 * } 861 */ 862 $overrides = apply_filters( "{$action}_overrides", $overrides, $file ); 863 864 // You may define your own function and pass the name in $overrides['upload_error_handler']. 865 $upload_error_handler = 'wp_handle_upload_error'; 866 if ( isset( $overrides['upload_error_handler'] ) ) { 867 $upload_error_handler = $overrides['upload_error_handler']; 868 } 869 870 // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully. 871 if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) { 872 return call_user_func_array( $upload_error_handler, array( &$file, $file['error'] ) ); 873 } 874 875 // Install user overrides. Did we mention that this voids your warranty? 876 877 // You may define your own function and pass the name in $overrides['unique_filename_callback']. 878 $unique_filename_callback = null; 879 if ( isset( $overrides['unique_filename_callback'] ) ) { 880 $unique_filename_callback = $overrides['unique_filename_callback']; 881 } 882 883 /* 884 * This may not have originally been intended to be overridable, 885 * but historically has been. 886 */ 887 if ( isset( $overrides['upload_error_strings'] ) ) { 888 $upload_error_strings = $overrides['upload_error_strings']; 889 } else { 890 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error']. 891 $upload_error_strings = array( 892 false, 893 sprintf( 894 /* translators: 1: upload_max_filesize, 2: php.ini */ 895 __( 'The uploaded file exceeds the %1$s directive in %2$s.' ), 896 'upload_max_filesize', 897 'php.ini' 898 ), 899 sprintf( 900 /* translators: %s: MAX_FILE_SIZE */ 901 __( 'The uploaded file exceeds the %s directive that was specified in the HTML form.' ), 902 'MAX_FILE_SIZE' 903 ), 904 __( 'The uploaded file was only partially uploaded.' ), 905 __( 'No file was uploaded.' ), 906 '', 907 __( 'Missing a temporary folder.' ), 908 __( 'Failed to write file to disk.' ), 909 __( 'File upload stopped by extension.' ), 910 ); 911 } 912 913 // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false; 914 $test_form = $overrides['test_form'] ?? true; 915 $test_size = $overrides['test_size'] ?? true; 916 917 // If you override this, you must provide $ext and $type!! 918 $test_type = $overrides['test_type'] ?? true; 919 $mimes = $overrides['mimes'] ?? null; 920 921 // A correct form post will pass this test. 922 if ( $test_form && ( ! isset( $_POST['action'] ) || $_POST['action'] !== $action ) ) { 923 return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) ); 924 } 925 926 // A successful upload will pass this test. It makes no sense to override this one. 927 if ( isset( $file['error'] ) && $file['error'] > 0 ) { 928 return call_user_func_array( $upload_error_handler, array( &$file, $upload_error_strings[ $file['error'] ] ) ); 929 } 930 931 // A properly uploaded file will pass this test. There should be no reason to override this one. 932 $test_uploaded_file = 'wp_handle_upload' === $action ? is_uploaded_file( $file['tmp_name'] ) : @is_readable( $file['tmp_name'] ); 933 if ( ! $test_uploaded_file ) { 934 return call_user_func_array( $upload_error_handler, array( &$file, __( 'Specified file failed upload test.' ) ) ); 935 } 936 937 $test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] ); 938 // A non-empty file will pass this test. 939 if ( $test_size && ! ( $test_file_size > 0 ) ) { 940 if ( is_multisite() ) { 941 $error_msg = __( 'File is empty. Please upload something more substantial.' ); 942 } else { 943 $error_msg = sprintf( 944 /* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */ 945 __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ), 946 'php.ini', 947 'post_max_size', 948 'upload_max_filesize' 949 ); 950 } 951 952 return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) ); 953 } 954 955 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter. 956 if ( $test_type ) { 957 $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes ); 958 $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext']; 959 $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type']; 960 $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename']; 961 962 // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect. 963 if ( $proper_filename ) { 964 $file['name'] = $proper_filename; 965 } 966 967 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) { 968 return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, you are not allowed to upload this file type.' ) ) ); 969 } 970 971 if ( ! $type ) { 972 $type = $file['type']; 973 } 974 } else { 975 $type = ''; 976 } 977 978 /* 979 * A writable uploads dir will pass this test. Again, there's no point 980 * overriding this one. 981 */ 982 $uploads = wp_upload_dir( $time ); 983 if ( ! ( $uploads && false === $uploads['error'] ) ) { 984 return call_user_func_array( $upload_error_handler, array( &$file, $uploads['error'] ) ); 985 } 986 987 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback ); 988 989 // Move the file to the uploads dir. 990 $new_file = $uploads['path'] . "/$filename"; 991 992 /** 993 * Filters whether to short-circuit moving the uploaded file after passing all checks. 994 * 995 * If a non-null value is returned from the filter, moving the file and any related 996 * error reporting will be completely skipped. 997 * 998 * @since 4.9.0 999 * 1000 * @param mixed $move_new_file If null (default) move the file after the upload. 1001 * @param array $file { 1002 * Reference to a single element from `$_FILES`. 1003 * 1004 * @type string $name The original name of the file on the client machine. 1005 * @type string $type The mime type of the file, if the browser provided this information. 1006 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 1007 * @type int $size The size, in bytes, of the uploaded file. 1008 * @type int $error The error code associated with this file upload. 1009 * } 1010 * @param string $new_file Filename of the newly-uploaded file. 1011 * @param string $type Mime type of the newly-uploaded file. 1012 */ 1013 $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type ); 1014 1015 if ( null === $move_new_file ) { 1016 if ( 'wp_handle_upload' === $action ) { 1017 $move_new_file = @move_uploaded_file( $file['tmp_name'], $new_file ); 1018 } else { 1019 // Use copy and unlink because rename breaks streams. 1020 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 1021 $move_new_file = @copy( $file['tmp_name'], $new_file ); 1022 unlink( $file['tmp_name'] ); 1023 } 1024 1025 if ( false === $move_new_file ) { 1026 if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) { 1027 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; 1028 } else { 1029 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir']; 1030 } 1031 1032 return $upload_error_handler( 1033 $file, 1034 sprintf( 1035 /* translators: %s: Destination file path. */ 1036 __( 'The uploaded file could not be moved to %s.' ), 1037 $error_path 1038 ) 1039 ); 1040 } 1041 } 1042 1043 // Set correct file permissions. 1044 $stat = stat( dirname( $new_file ) ); 1045 $perms = $stat['mode'] & 0000666; 1046 chmod( $new_file, $perms ); 1047 1048 // Compute the URL. 1049 $url = $uploads['url'] . "/$filename"; 1050 1051 if ( is_multisite() ) { 1052 clean_dirsize_cache( $new_file ); 1053 } 1054 1055 /** 1056 * Filters the data array for the uploaded file. 1057 * 1058 * @since 2.1.0 1059 * 1060 * @param array $upload { 1061 * Array of upload data. 1062 * 1063 * @type string $file Filename of the newly-uploaded file. 1064 * @type string $url URL of the newly-uploaded file. 1065 * @type string $type Mime type of the newly-uploaded file. 1066 * } 1067 * @param string $context The type of upload action. Values include 'upload' or 'sideload'. 1068 */ 1069 return apply_filters( 1070 'wp_handle_upload', 1071 array( 1072 'file' => $new_file, 1073 'url' => $url, 1074 'type' => $type, 1075 ), 1076 'wp_handle_sideload' === $action ? 'sideload' : 'upload' 1077 ); 1078 } 1079 1080 /** 1081 * Wrapper for _wp_handle_upload(). 1082 * 1083 * Passes the {@see 'wp_handle_upload'} action. 1084 * 1085 * @since 2.0.0 1086 * 1087 * @see _wp_handle_upload() 1088 * 1089 * @param array $file Reference to a single element of `$_FILES`. 1090 * Call the function once for each uploaded file. 1091 * See _wp_handle_upload() for accepted values. 1092 * @param array|false $overrides Optional. An associative array of names => values 1093 * to override default variables. Default false. 1094 * See _wp_handle_upload() for accepted values. 1095 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 1096 * @return array See _wp_handle_upload() for return value. 1097 */ 1098 function wp_handle_upload( &$file, $overrides = false, $time = null ) { 1099 /* 1100 * $_POST['action'] must be set and its value must equal $overrides['action'] 1101 * or this: 1102 */ 1103 $action = $overrides['action'] ?? 'wp_handle_upload'; 1104 return _wp_handle_upload( $file, $overrides, $time, $action ); 1105 } 1106 1107 /** 1108 * Wrapper for _wp_handle_upload(). 1109 * 1110 * Passes the {@see 'wp_handle_sideload'} action. 1111 * 1112 * @since 2.6.0 1113 * 1114 * @see _wp_handle_upload() 1115 * 1116 * @param array $file Reference to a single element of `$_FILES`. 1117 * Call the function once for each uploaded file. 1118 * See _wp_handle_upload() for accepted values. 1119 * @param array|false $overrides Optional. An associative array of names => values 1120 * to override default variables. Default false. 1121 * See _wp_handle_upload() for accepted values. 1122 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 1123 * @return array See _wp_handle_upload() for return value. 1124 */ 1125 function wp_handle_sideload( &$file, $overrides = false, $time = null ) { 1126 /* 1127 * $_POST['action'] must be set and its value must equal $overrides['action'] 1128 * or this: 1129 */ 1130 $action = $overrides['action'] ?? 'wp_handle_sideload'; 1131 return _wp_handle_upload( $file, $overrides, $time, $action ); 1132 } 1133 1134 /** 1135 * Downloads a URL to a local temporary file using the WordPress HTTP API. 1136 * 1137 * Please note that the calling function must delete or move the file. 1138 * 1139 * @since 2.5.0 1140 * @since 5.2.0 Signature Verification with SoftFail was added. 1141 * @since 5.9.0 Support for Content-Disposition filename was added. 1142 * 1143 * @param string $url The URL of the file to download. 1144 * @param int $timeout The timeout for the request to download the file. 1145 * Default 300 seconds. 1146 * @param bool $signature_verification Whether to perform Signature Verification. 1147 * Default false. 1148 * @return string|WP_Error Filename on success, WP_Error on failure. 1149 */ 1150 function download_url( $url, $timeout = 300, $signature_verification = false ) { 1151 // WARNING: The file is not automatically deleted, the script must delete or move the file. 1152 if ( ! $url ) { 1153 return new WP_Error( 'http_no_url', __( 'No URL Provided.' ) ); 1154 } 1155 1156 $url_path = parse_url( $url, PHP_URL_PATH ); 1157 $url_filename = ''; 1158 if ( is_string( $url_path ) && '' !== $url_path ) { 1159 $url_filename = basename( $url_path ); 1160 } 1161 1162 $tmpfname = wp_tempnam( $url_filename ); 1163 if ( ! $tmpfname ) { 1164 return new WP_Error( 'http_no_file', __( 'Could not create temporary file.' ) ); 1165 } 1166 1167 $response = wp_safe_remote_get( 1168 $url, 1169 array( 1170 'timeout' => $timeout, 1171 'stream' => true, 1172 'filename' => $tmpfname, 1173 ) 1174 ); 1175 1176 if ( is_wp_error( $response ) ) { 1177 unlink( $tmpfname ); 1178 return $response; 1179 } 1180 1181 $response_code = wp_remote_retrieve_response_code( $response ); 1182 1183 if ( 200 !== $response_code ) { 1184 $data = array( 1185 'code' => $response_code, 1186 ); 1187 1188 // Retrieve a sample of the response body for debugging purposes. 1189 $tmpf = fopen( $tmpfname, 'rb' ); 1190 1191 if ( $tmpf ) { 1192 /** 1193 * Filters the maximum error response body size in `download_url()`. 1194 * 1195 * @since 5.1.0 1196 * 1197 * @see download_url() 1198 * 1199 * @param int $size The maximum error response body size. Default 1 KB. 1200 */ 1201 $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES ); 1202 1203 $data['body'] = fread( $tmpf, $response_size ); 1204 fclose( $tmpf ); 1205 } 1206 1207 unlink( $tmpfname ); 1208 1209 return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data ); 1210 } 1211 1212 $content_disposition = wp_remote_retrieve_header( $response, 'Content-Disposition' ); 1213 1214 if ( $content_disposition ) { 1215 $content_disposition = strtolower( $content_disposition ); 1216 1217 if ( str_starts_with( $content_disposition, 'attachment; filename=' ) ) { 1218 $tmpfname_disposition = sanitize_file_name( substr( $content_disposition, 21 ) ); 1219 } else { 1220 $tmpfname_disposition = ''; 1221 } 1222 1223 // Potential file name must be valid string. 1224 if ( $tmpfname_disposition && is_string( $tmpfname_disposition ) 1225 && ( 0 === validate_file( $tmpfname_disposition ) ) 1226 ) { 1227 $tmpfname_disposition = dirname( $tmpfname ) . '/' . $tmpfname_disposition; 1228 1229 if ( rename( $tmpfname, $tmpfname_disposition ) ) { 1230 $tmpfname = $tmpfname_disposition; 1231 } 1232 1233 if ( ( $tmpfname !== $tmpfname_disposition ) && file_exists( $tmpfname_disposition ) ) { 1234 unlink( $tmpfname_disposition ); 1235 } 1236 } 1237 } 1238 1239 $mime_type = wp_remote_retrieve_header( $response, 'content-type' ); 1240 if ( $mime_type && 'tmp' === pathinfo( $tmpfname, PATHINFO_EXTENSION ) ) { 1241 $valid_mime_types = array_flip( get_allowed_mime_types() ); 1242 if ( ! empty( $valid_mime_types[ $mime_type ] ) ) { 1243 $extensions = explode( '|', $valid_mime_types[ $mime_type ] ); 1244 $new_image_name = substr( $tmpfname, 0, -4 ) . ".{$extensions[0]}"; 1245 if ( 0 === validate_file( $new_image_name ) ) { 1246 if ( rename( $tmpfname, $new_image_name ) ) { 1247 $tmpfname = $new_image_name; 1248 } 1249 1250 if ( ( $tmpfname !== $new_image_name ) && file_exists( $new_image_name ) ) { 1251 unlink( $new_image_name ); 1252 } 1253 } 1254 } 1255 } 1256 1257 $content_md5 = wp_remote_retrieve_header( $response, 'Content-MD5' ); 1258 1259 if ( $content_md5 ) { 1260 $md5_check = verify_file_md5( $tmpfname, $content_md5 ); 1261 1262 if ( is_wp_error( $md5_check ) ) { 1263 unlink( $tmpfname ); 1264 return $md5_check; 1265 } 1266 } 1267 1268 // If the caller expects signature verification to occur, check to see if this URL supports it. 1269 if ( $signature_verification ) { 1270 /** 1271 * Filters the list of hosts which should have Signature Verification attempted on. 1272 * 1273 * @since 5.2.0 1274 * 1275 * @param string[] $hostnames List of hostnames. 1276 */ 1277 $signed_hostnames = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) ); 1278 1279 $signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true ); 1280 } 1281 1282 // Perform signature validation if supported. 1283 if ( $signature_verification ) { 1284 $signature = wp_remote_retrieve_header( $response, 'X-Content-Signature' ); 1285 1286 if ( ! $signature ) { 1287 /* 1288 * Retrieve signatures from a file if the header wasn't included. 1289 * WordPress.org stores signatures at $package_url.sig. 1290 */ 1291 1292 $signature_url = false; 1293 1294 if ( is_string( $url_path ) && ( str_ends_with( $url_path, '.zip' ) || str_ends_with( $url_path, '.tar.gz' ) ) ) { 1295 $signature_url = str_replace( $url_path, $url_path . '.sig', $url ); 1296 } 1297 1298 /** 1299 * Filters the URL where the signature for a file is located. 1300 * 1301 * @since 5.2.0 1302 * 1303 * @param false|string $signature_url The URL where signatures can be found for a file, or false if none are known. 1304 * @param string $url The URL being verified. 1305 */ 1306 $signature_url = apply_filters( 'wp_signature_url', $signature_url, $url ); 1307 1308 if ( $signature_url ) { 1309 $signature_request = wp_safe_remote_get( 1310 $signature_url, 1311 array( 1312 'limit_response_size' => 10 * KB_IN_BYTES, // 10KB should be large enough for quite a few signatures. 1313 ) 1314 ); 1315 1316 if ( ! is_wp_error( $signature_request ) && 200 === wp_remote_retrieve_response_code( $signature_request ) ) { 1317 $signature = explode( "\n", wp_remote_retrieve_body( $signature_request ) ); 1318 } 1319 } 1320 } 1321 1322 // Perform the checks. 1323 $signature_verification = verify_file_signature( $tmpfname, $signature, $url_filename ); 1324 } 1325 1326 if ( is_wp_error( $signature_verification ) ) { 1327 if ( 1328 /** 1329 * Filters whether Signature Verification failures should be allowed to soft fail. 1330 * 1331 * WARNING: This may be removed from a future release. 1332 * 1333 * @since 5.2.0 1334 * 1335 * @param bool $signature_softfail If a softfail is allowed. 1336 * @param string $url The url being accessed. 1337 */ 1338 apply_filters( 'wp_signature_softfail', true, $url ) 1339 ) { 1340 $signature_verification->add_data( $tmpfname, 'softfail-filename' ); 1341 } else { 1342 // Hard-fail. 1343 unlink( $tmpfname ); 1344 } 1345 1346 return $signature_verification; 1347 } 1348 1349 return $tmpfname; 1350 } 1351 1352 /** 1353 * Calculates and compares the MD5 of a file to its expected value. 1354 * 1355 * @since 3.7.0 1356 * 1357 * @param string $filename The filename to check the MD5 of. 1358 * @param string $expected_md5 The expected MD5 of the file, either a base64-encoded raw md5, 1359 * or a hex-encoded md5. 1360 * @return bool|WP_Error True on success, false when the MD5 format is unknown/unexpected, 1361 * WP_Error on failure. 1362 */ 1363 function verify_file_md5( $filename, $expected_md5 ) { 1364 if ( 32 === strlen( $expected_md5 ) ) { 1365 $expected_raw_md5 = pack( 'H*', $expected_md5 ); 1366 } elseif ( 24 === strlen( $expected_md5 ) ) { 1367 $expected_raw_md5 = base64_decode( $expected_md5 ); 1368 } else { 1369 return false; // Unknown format. 1370 } 1371 1372 $file_md5 = md5_file( $filename, true ); 1373 1374 if ( $file_md5 === $expected_raw_md5 ) { 1375 return true; 1376 } 1377 1378 return new WP_Error( 1379 'md5_mismatch', 1380 sprintf( 1381 /* translators: 1: File checksum, 2: Expected checksum value. */ 1382 __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ), 1383 bin2hex( $file_md5 ), 1384 bin2hex( $expected_raw_md5 ) 1385 ) 1386 ); 1387 } 1388 1389 /** 1390 * Verifies the contents of a file against its ED25519 signature. 1391 * 1392 * @since 5.2.0 1393 * 1394 * @param string $filename The file to validate. 1395 * @param string|array $signatures A Signature provided for the file. 1396 * @param string|false $filename_for_errors Optional. A friendly filename for errors. 1397 * @return bool|WP_Error True on success, false if verification not attempted, 1398 * or WP_Error describing an error condition. 1399 */ 1400 function verify_file_signature( $filename, $signatures, $filename_for_errors = false ) { 1401 if ( ! $filename_for_errors ) { 1402 $filename_for_errors = wp_basename( $filename ); 1403 } 1404 1405 // Check we can process signatures. 1406 if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) || ! in_array( 'sha384', array_map( 'strtolower', hash_algos() ), true ) ) { 1407 return new WP_Error( 1408 'signature_verification_unsupported', 1409 sprintf( 1410 /* translators: %s: The filename of the package. */ 1411 __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ), 1412 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1413 ), 1414 ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' ) 1415 ); 1416 } 1417 1418 // Verify runtime speed of Sodium_Compat is acceptable. 1419 if ( ! extension_loaded( 'sodium' ) && ! ParagonIE_Sodium_Compat::polyfill_is_fast() ) { 1420 $sodium_compat_is_fast = false; 1421 1422 // Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one. 1423 if ( method_exists( 'ParagonIE_Sodium_Compat', 'runtime_speed_test' ) ) { 1424 /* 1425 * Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode, 1426 * as that's what WordPress utilizes during signing verifications. 1427 */ 1428 // phpcs:disable WordPress.NamingConventions.ValidVariableName 1429 $old_fastMult = ParagonIE_Sodium_Compat::$fastMult; 1430 ParagonIE_Sodium_Compat::$fastMult = true; 1431 $sodium_compat_is_fast = ParagonIE_Sodium_Compat::runtime_speed_test( 100, 10 ); 1432 ParagonIE_Sodium_Compat::$fastMult = $old_fastMult; 1433 // phpcs:enable 1434 } 1435 1436 /* 1437 * This cannot be performed in a reasonable amount of time. 1438 * https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast 1439 */ 1440 if ( ! $sodium_compat_is_fast ) { 1441 return new WP_Error( 1442 'signature_verification_unsupported', 1443 sprintf( 1444 /* translators: %s: The filename of the package. */ 1445 __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ), 1446 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1447 ), 1448 array( 1449 'php' => PHP_VERSION, 1450 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ), 1451 'polyfill_is_fast' => false, 1452 'max_execution_time' => ini_get( 'max_execution_time' ), 1453 ) 1454 ); 1455 } 1456 } 1457 1458 if ( ! $signatures ) { 1459 return new WP_Error( 1460 'signature_verification_no_signature', 1461 sprintf( 1462 /* translators: %s: The filename of the package. */ 1463 __( 'The authenticity of %s could not be verified as no signature was found.' ), 1464 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1465 ), 1466 array( 1467 'filename' => $filename_for_errors, 1468 ) 1469 ); 1470 } 1471 1472 $trusted_keys = wp_trusted_keys(); 1473 $file_hash = hash_file( 'sha384', $filename, true ); 1474 1475 mbstring_binary_safe_encoding(); 1476 1477 $skipped_key = 0; 1478 $skipped_signature = 0; 1479 1480 foreach ( (array) $signatures as $signature ) { 1481 $signature_raw = base64_decode( $signature ); 1482 1483 // Ensure only valid-length signatures are considered. 1484 if ( SODIUM_CRYPTO_SIGN_BYTES !== strlen( $signature_raw ) ) { 1485 ++$skipped_signature; 1486 continue; 1487 } 1488 1489 foreach ( (array) $trusted_keys as $key ) { 1490 $key_raw = base64_decode( $key ); 1491 1492 // Only pass valid public keys through. 1493 if ( SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES !== strlen( $key_raw ) ) { 1494 ++$skipped_key; 1495 continue; 1496 } 1497 1498 if ( sodium_crypto_sign_verify_detached( $signature_raw, $file_hash, $key_raw ) ) { 1499 reset_mbstring_encoding(); 1500 return true; 1501 } 1502 } 1503 } 1504 1505 reset_mbstring_encoding(); 1506 1507 return new WP_Error( 1508 'signature_verification_failed', 1509 sprintf( 1510 /* translators: %s: The filename of the package. */ 1511 __( 'The authenticity of %s could not be verified.' ), 1512 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1513 ), 1514 // Error data helpful for debugging: 1515 array( 1516 'filename' => $filename_for_errors, 1517 'keys' => $trusted_keys, 1518 'signatures' => $signatures, 1519 'hash' => bin2hex( $file_hash ), 1520 'skipped_key' => $skipped_key, 1521 'skipped_sig' => $skipped_signature, 1522 'php' => PHP_VERSION, 1523 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ), 1524 ) 1525 ); 1526 } 1527 1528 /** 1529 * Retrieves the list of signing keys trusted by WordPress. 1530 * 1531 * @since 5.2.0 1532 * 1533 * @return string[] Array of base64-encoded signing keys. 1534 */ 1535 function wp_trusted_keys() { 1536 $trusted_keys = array(); 1537 1538 if ( time() < 1617235200 ) { 1539 // WordPress.org Key #1 - This key is only valid before April 1st, 2021. 1540 $trusted_keys[] = 'fRPyrxb/MvVLbdsYi+OOEv4xc+Eqpsj+kkAS6gNOkI0='; 1541 } 1542 1543 // TODO: Add key #2 with longer expiration. 1544 1545 /** 1546 * Filters the valid signing keys used to verify the contents of files. 1547 * 1548 * @since 5.2.0 1549 * 1550 * @param string[] $trusted_keys The trusted keys that may sign packages. 1551 */ 1552 return apply_filters( 'wp_trusted_keys', $trusted_keys ); 1553 } 1554 1555 /** 1556 * Determines whether the given file is a valid ZIP file. 1557 * 1558 * This function does not test to ensure that a file exists. Non-existent files 1559 * are not valid ZIPs, so those will also return false. 1560 * 1561 * @since 6.4.4 1562 * 1563 * @param string $file Full path to the ZIP file. 1564 * @return bool Whether the file is a valid ZIP file. 1565 */ 1566 function wp_zip_file_is_valid( $file ) { 1567 /** This filter is documented in wp-admin/includes/file.php */ 1568 if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { 1569 $archive = new ZipArchive(); 1570 $archive_is_valid = $archive->open( $file, ZipArchive::CHECKCONS ); 1571 if ( true === $archive_is_valid ) { 1572 $archive->close(); 1573 return true; 1574 } 1575 } 1576 1577 // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. 1578 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; 1579 1580 $archive = new PclZip( $file ); 1581 $archive_is_valid = is_array( $archive->properties() ); 1582 1583 return $archive_is_valid; 1584 } 1585 1586 /** 1587 * Unzips a specified ZIP file to a location on the filesystem via the WordPress 1588 * Filesystem Abstraction. 1589 * 1590 * Assumes that WP_Filesystem() has already been called and set up. Does not extract 1591 * a root-level __MACOSX directory, if present. 1592 * 1593 * Attempts to increase the PHP memory limit to 256M before uncompressing. However, 1594 * the most memory required shouldn't be much larger than the archive itself. 1595 * 1596 * @since 2.5.0 1597 * 1598 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1599 * 1600 * @param string $file Full path and filename of ZIP archive. 1601 * @param string $to Full path on the filesystem to extract archive to. 1602 * @return true|WP_Error True on success, WP_Error on failure. 1603 */ 1604 function unzip_file( $file, $to ) { 1605 global $wp_filesystem; 1606 1607 if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) { 1608 return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) ); 1609 } 1610 1611 // Unzip can use a lot of memory, but not this much hopefully. 1612 wp_raise_memory_limit( 'admin' ); 1613 1614 $needed_dirs = array(); 1615 $to = trailingslashit( $to ); 1616 1617 // Determine any parent directories needed (of the upgrade directory). 1618 if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parents if no children exist. 1619 $path = preg_split( '![/\\\]!', untrailingslashit( $to ) ); 1620 for ( $i = count( $path ); $i >= 0; $i-- ) { 1621 if ( empty( $path[ $i ] ) ) { 1622 continue; 1623 } 1624 1625 $dir = implode( '/', array_slice( $path, 0, $i + 1 ) ); 1626 if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Skip it if it looks like a Windows Drive letter. 1627 continue; 1628 } 1629 1630 if ( ! $wp_filesystem->is_dir( $dir ) ) { 1631 $needed_dirs[] = $dir; 1632 } else { 1633 break; // A folder exists, therefore we don't need to check the levels below this. 1634 } 1635 } 1636 } 1637 1638 /** 1639 * Filters whether to use ZipArchive to unzip archives. 1640 * 1641 * @since 3.0.0 1642 * 1643 * @param bool $ziparchive Whether to use ZipArchive. Default true. 1644 */ 1645 if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { 1646 $result = _unzip_file_ziparchive( $file, $to, $needed_dirs ); 1647 if ( true === $result ) { 1648 return $result; 1649 } elseif ( is_wp_error( $result ) ) { 1650 if ( 'incompatible_archive' !== $result->get_error_code() ) { 1651 return $result; 1652 } 1653 } 1654 } 1655 // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. 1656 return _unzip_file_pclzip( $file, $to, $needed_dirs ); 1657 } 1658 1659 /** 1660 * Attempts to unzip an archive using the ZipArchive class. 1661 * 1662 * This function should not be called directly, use `unzip_file()` instead. 1663 * 1664 * Assumes that WP_Filesystem() has already been called and set up. 1665 * 1666 * @since 3.0.0 1667 * @access private 1668 * 1669 * @see unzip_file() 1670 * 1671 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1672 * 1673 * @param string $file Full path and filename of ZIP archive. 1674 * @param string $to Full path on the filesystem to extract archive to. 1675 * @param string[] $needed_dirs A partial list of required folders needed to be created. 1676 * @return true|WP_Error True on success, WP_Error on failure. 1677 */ 1678 function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { 1679 global $wp_filesystem; 1680 1681 $z = new ZipArchive(); 1682 1683 $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS ); 1684 1685 if ( true !== $zopen ) { 1686 return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) ); 1687 } 1688 1689 $uncompressed_size = 0; 1690 1691 for ( $i = 0; $i < $z->numFiles; $i++ ) { 1692 $info = $z->statIndex( $i ); 1693 1694 if ( ! $info ) { 1695 $z->close(); 1696 return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); 1697 } 1698 1699 if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. 1700 continue; 1701 } 1702 1703 // Don't extract invalid files: 1704 if ( 0 !== validate_file( $info['name'] ) ) { 1705 continue; 1706 } 1707 1708 $uncompressed_size += $info['size']; 1709 1710 $dirname = dirname( $info['name'] ); 1711 1712 if ( str_ends_with( $info['name'], '/' ) ) { 1713 // Directory. 1714 $needed_dirs[] = $to . untrailingslashit( $info['name'] ); 1715 } elseif ( '.' !== $dirname ) { 1716 // Path to a file. 1717 $needed_dirs[] = $to . untrailingslashit( $dirname ); 1718 } 1719 } 1720 1721 // Enough space to unzip the file and copy its contents, with a 10% buffer. 1722 $required_space = $uncompressed_size * 2.1; 1723 1724 /* 1725 * disk_free_space() could return false. Assume that any falsey value is an error. 1726 * A disk that has zero free bytes has bigger problems. 1727 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer. 1728 */ 1729 if ( wp_doing_cron() ) { 1730 $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; 1731 1732 if ( $available_space && ( $required_space > $available_space ) ) { 1733 $z->close(); 1734 return new WP_Error( 1735 'disk_full_unzip_file', 1736 __( 'Could not copy files. You may have run out of disk space.' ), 1737 compact( 'uncompressed_size', 'available_space' ) 1738 ); 1739 } 1740 } 1741 1742 $needed_dirs = array_unique( $needed_dirs ); 1743 1744 foreach ( $needed_dirs as $dir ) { 1745 // Check the parent folders of the folders all exist within the creation array. 1746 if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist). 1747 continue; 1748 } 1749 1750 if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it. 1751 continue; 1752 } 1753 1754 $parent_folder = dirname( $dir ); 1755 1756 while ( ! empty( $parent_folder ) 1757 && untrailingslashit( $to ) !== $parent_folder 1758 && ! in_array( $parent_folder, $needed_dirs, true ) 1759 ) { 1760 $needed_dirs[] = $parent_folder; 1761 $parent_folder = dirname( $parent_folder ); 1762 } 1763 } 1764 1765 asort( $needed_dirs ); 1766 1767 // Create those directories if need be: 1768 foreach ( $needed_dirs as $_dir ) { 1769 // Only check to see if the Dir exists upon creation failure. Less I/O this way. 1770 if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) { 1771 $z->close(); 1772 return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), $_dir ); 1773 } 1774 } 1775 1776 /** 1777 * Filters archive unzipping to override with a custom process. 1778 * 1779 * @since 6.4.0 1780 * 1781 * @param null|true|WP_Error $result The result of the override. True on success, otherwise WP Error. Default null. 1782 * @param string $file Full path and filename of ZIP archive. 1783 * @param string $to Full path on the filesystem to extract archive to. 1784 * @param string[] $needed_dirs A full list of required folders that need to be created. 1785 * @param float $required_space The space required to unzip the file and copy its contents, with a 10% buffer. 1786 */ 1787 $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space ); 1788 1789 if ( null !== $pre ) { 1790 // Ensure the ZIP file archive has been closed. 1791 $z->close(); 1792 1793 return $pre; 1794 } 1795 1796 for ( $i = 0; $i < $z->numFiles; $i++ ) { 1797 $info = $z->statIndex( $i ); 1798 1799 if ( ! $info ) { 1800 $z->close(); 1801 return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); 1802 } 1803 1804 if ( str_ends_with( $info['name'], '/' ) ) { // Directory. 1805 continue; 1806 } 1807 1808 if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files. 1809 continue; 1810 } 1811 1812 // Don't extract invalid files: 1813 if ( 0 !== validate_file( $info['name'] ) ) { 1814 continue; 1815 } 1816 1817 $contents = $z->getFromIndex( $i ); 1818 1819 if ( false === $contents ) { 1820 $z->close(); 1821 return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] ); 1822 } 1823 1824 if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) { 1825 $z->close(); 1826 return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] ); 1827 } 1828 } 1829 1830 $z->close(); 1831 1832 /** 1833 * Filters the result of unzipping an archive. 1834 * 1835 * @since 6.4.0 1836 * 1837 * @param true|WP_Error $result The result of unzipping the archive. True on success, otherwise WP_Error. Default true. 1838 * @param string $file Full path and filename of ZIP archive. 1839 * @param string $to Full path on the filesystem the archive was extracted to. 1840 * @param string[] $needed_dirs A full list of required folders that were created. 1841 * @param float $required_space The space required to unzip the file and copy its contents, with a 10% buffer. 1842 */ 1843 $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space ); 1844 1845 unset( $needed_dirs ); 1846 1847 return $result; 1848 } 1849 1850 /** 1851 * Attempts to unzip an archive using the PclZip library. 1852 * 1853 * This function should not be called directly, use `unzip_file()` instead. 1854 * 1855 * Assumes that WP_Filesystem() has already been called and set up. 1856 * 1857 * @since 3.0.0 1858 * @access private 1859 * 1860 * @see unzip_file() 1861 * 1862 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1863 * 1864 * @param string $file Full path and filename of ZIP archive. 1865 * @param string $to Full path on the filesystem to extract archive to. 1866 * @param string[] $needed_dirs A partial list of required folders needed to be created. 1867 * @return true|WP_Error True on success, WP_Error on failure. 1868 */ 1869 function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) { 1870 global $wp_filesystem; 1871 1872 mbstring_binary_safe_encoding(); 1873 1874 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; 1875 1876 $archive = new PclZip( $file ); 1877 1878 $archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING ); 1879 1880 reset_mbstring_encoding(); 1881 1882 // Is the archive valid? 1883 if ( ! is_array( $archive_files ) ) { 1884 return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) ); 1885 } 1886 1887 if ( 0 === count( $archive_files ) ) { 1888 return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) ); 1889 } 1890 1891 $uncompressed_size = 0; 1892 1893 // Determine any children directories needed (From within the archive). 1894 foreach ( $archive_files as $archive_file ) { 1895 if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. 1896 continue; 1897 } 1898 1899 // Don't extract invalid files: 1900 if ( 0 !== validate_file( $archive_file['filename'] ) ) { 1901 continue; 1902 } 1903 1904 $uncompressed_size += $archive_file['size']; 1905 1906 $needed_dirs[] = $to . untrailingslashit( $archive_file['folder'] ? $archive_file['filename'] : dirname( $archive_file['filename'] ) ); 1907 } 1908 1909 // Enough space to unzip the file and copy its contents, with a 10% buffer. 1910 $required_space = $uncompressed_size * 2.1; 1911 1912 /* 1913 * disk_free_space() could return false. Assume that any falsey value is an error. 1914 * A disk that has zero free bytes has bigger problems. 1915 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer. 1916 */ 1917 if ( wp_doing_cron() ) { 1918 $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; 1919 1920 if ( $available_space && ( $required_space > $available_space ) ) { 1921 return new WP_Error( 1922 'disk_full_unzip_file', 1923 __( 'Could not copy files. You may have run out of disk space.' ), 1924 compact( 'uncompressed_size', 'available_space' ) 1925 ); 1926 } 1927 } 1928 1929 $needed_dirs = array_unique( $needed_dirs ); 1930 1931 foreach ( $needed_dirs as $dir ) { 1932 // Check the parent folders of the folders all exist within the creation array. 1933 if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist). 1934 continue; 1935 } 1936 1937 if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it. 1938 continue; 1939 } 1940 1941 $parent_folder = dirname( $dir ); 1942 1943 while ( ! empty( $parent_folder ) 1944 && untrailingslashit( $to ) !== $parent_folder 1945 && ! in_array( $parent_folder, $needed_dirs, true ) 1946 ) { 1947 $needed_dirs[] = $parent_folder; 1948 $parent_folder = dirname( $parent_folder ); 1949 } 1950 } 1951 1952 asort( $needed_dirs ); 1953 1954 // Create those directories if need be: 1955 foreach ( $needed_dirs as $_dir ) { 1956 // Only check to see if the dir exists upon creation failure. Less I/O this way. 1957 if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) { 1958 return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), $_dir ); 1959 } 1960 } 1961 1962 /** This filter is documented in wp-admin/includes/file.php */ 1963 $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space ); 1964 1965 if ( null !== $pre ) { 1966 return $pre; 1967 } 1968 1969 // Extract the files from the zip. 1970 foreach ( $archive_files as $archive_file ) { 1971 if ( $archive_file['folder'] ) { 1972 continue; 1973 } 1974 1975 if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files. 1976 continue; 1977 } 1978 1979 // Don't extract invalid files: 1980 if ( 0 !== validate_file( $archive_file['filename'] ) ) { 1981 continue; 1982 } 1983 1984 if ( ! $wp_filesystem->put_contents( $to . $archive_file['filename'], $archive_file['content'], FS_CHMOD_FILE ) ) { 1985 return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $archive_file['filename'] ); 1986 } 1987 } 1988 1989 /** This filter is documented in wp-admin/includes/file.php */ 1990 $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space ); 1991 1992 unset( $needed_dirs ); 1993 1994 return $result; 1995 } 1996 1997 /** 1998 * Copies a directory from one location to another via the WordPress Filesystem 1999 * Abstraction. 2000 * 2001 * Assumes that WP_Filesystem() has already been called and setup. 2002 * 2003 * @since 2.5.0 2004 * 2005 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2006 * 2007 * @param string $from Source directory. 2008 * @param string $to Destination directory. 2009 * @param string[] $skip_list An array of files/folders to skip copying. 2010 * @return true|WP_Error True on success, WP_Error on failure. 2011 */ 2012 function copy_dir( $from, $to, $skip_list = array() ) { 2013 global $wp_filesystem; 2014 2015 $dirlist = $wp_filesystem->dirlist( $from ); 2016 2017 if ( false === $dirlist ) { 2018 return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $from ) ); 2019 } 2020 2021 $from = trailingslashit( $from ); 2022 $to = trailingslashit( $to ); 2023 2024 if ( ! $wp_filesystem->exists( $to ) && ! $wp_filesystem->mkdir( $to ) ) { 2025 return new WP_Error( 2026 'mkdir_destination_failed_copy_dir', 2027 __( 'Could not create the destination directory.' ), 2028 basename( $to ) 2029 ); 2030 } 2031 2032 foreach ( (array) $dirlist as $filename => $fileinfo ) { 2033 if ( in_array( $filename, $skip_list, true ) ) { 2034 continue; 2035 } 2036 2037 if ( 'f' === $fileinfo['type'] ) { 2038 if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) { 2039 // If copy failed, chmod file to 0644 and try again. 2040 $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE ); 2041 2042 if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) { 2043 return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename ); 2044 } 2045 } 2046 2047 wp_opcache_invalidate( $to . $filename ); 2048 } elseif ( 'd' === $fileinfo['type'] ) { 2049 if ( ! $wp_filesystem->is_dir( $to . $filename ) ) { 2050 if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) { 2051 return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename ); 2052 } 2053 } 2054 2055 // Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list. 2056 $sub_skip_list = array(); 2057 2058 foreach ( $skip_list as $skip_item ) { 2059 if ( str_starts_with( $skip_item, $filename . '/' ) ) { 2060 $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item ); 2061 } 2062 } 2063 2064 $result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list ); 2065 2066 if ( is_wp_error( $result ) ) { 2067 return $result; 2068 } 2069 } 2070 } 2071 2072 return true; 2073 } 2074 2075 /** 2076 * Moves a directory from one location to another. 2077 * 2078 * Recursively invalidates OPcache on success. 2079 * 2080 * If the renaming failed, falls back to copy_dir(). 2081 * 2082 * Assumes that WP_Filesystem() has already been called and setup. 2083 * 2084 * This function is not designed to merge directories, copy_dir() should be used instead. 2085 * 2086 * @since 6.2.0 2087 * 2088 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2089 * 2090 * @param string $from Source directory. 2091 * @param string $to Destination directory. 2092 * @param bool $overwrite Optional. Whether to overwrite the destination directory if it exists. 2093 * Default false. 2094 * @return true|WP_Error True on success, WP_Error on failure. 2095 */ 2096 function move_dir( $from, $to, $overwrite = false ) { 2097 global $wp_filesystem; 2098 2099 if ( trailingslashit( strtolower( $from ) ) === trailingslashit( strtolower( $to ) ) ) { 2100 return new WP_Error( 'source_destination_same_move_dir', __( 'The source and destination are the same.' ) ); 2101 } 2102 2103 if ( $wp_filesystem->exists( $to ) ) { 2104 if ( ! $overwrite ) { 2105 return new WP_Error( 'destination_already_exists_move_dir', __( 'The destination folder already exists.' ), $to ); 2106 } elseif ( ! $wp_filesystem->delete( $to, true ) ) { 2107 // Can't overwrite if the destination couldn't be deleted. 2108 return new WP_Error( 'destination_not_deleted_move_dir', __( 'The destination directory already exists and could not be removed.' ) ); 2109 } 2110 } 2111 2112 if ( $wp_filesystem->move( $from, $to ) ) { 2113 /* 2114 * When using an environment with shared folders, 2115 * there is a delay in updating the filesystem's cache. 2116 * 2117 * This is a known issue in environments with a VirtualBox provider. 2118 * 2119 * A 200ms delay gives time for the filesystem to update its cache, 2120 * prevents "Operation not permitted", and "No such file or directory" warnings. 2121 * 2122 * This delay is used in other projects, including Composer. 2123 * @link https://github.com/composer/composer/blob/2.5.1/src/Composer/Util/Platform.php#L228-L233 2124 */ 2125 usleep( 200000 ); 2126 wp_opcache_invalidate_directory( $to ); 2127 2128 return true; 2129 } 2130 2131 // Fall back to a recursive copy. 2132 if ( ! $wp_filesystem->is_dir( $to ) ) { 2133 if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) { 2134 return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to ); 2135 } 2136 } 2137 2138 $result = copy_dir( $from, $to, array( basename( $to ) ) ); 2139 2140 // Clear the source directory. 2141 if ( true === $result ) { 2142 $wp_filesystem->delete( $from, true ); 2143 } 2144 2145 return $result; 2146 } 2147 2148 /** 2149 * Initializes and connects the WordPress Filesystem Abstraction classes. 2150 * 2151 * This function will include the chosen transport and attempt connecting. 2152 * 2153 * Plugins may add extra transports, And force WordPress to use them by returning 2154 * the filename via the {@see 'filesystem_method_file'} filter. 2155 * 2156 * @since 2.5.0 2157 * 2158 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2159 * 2160 * @param array|false $args Optional. Connection args, These are passed 2161 * directly to the `WP_Filesystem_*()` classes. 2162 * Default false. 2163 * @param string|false $context Optional. Context for get_filesystem_method(). 2164 * Default false. 2165 * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. 2166 * Default false. 2167 * @return bool|null True on success, false on failure, 2168 * null if the filesystem method class file does not exist. 2169 */ 2170 function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 2171 global $wp_filesystem; 2172 2173 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; 2174 2175 $method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership ); 2176 2177 if ( ! $method ) { 2178 return false; 2179 } 2180 2181 if ( ! class_exists( "WP_Filesystem_$method" ) ) { 2182 2183 /** 2184 * Filters the path for a specific filesystem method class file. 2185 * 2186 * @since 2.6.0 2187 * 2188 * @see get_filesystem_method() 2189 * 2190 * @param string $path Path to the specific filesystem method class file. 2191 * @param string $method The filesystem method to use. 2192 */ 2193 $abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method ); 2194 2195 if ( ! file_exists( $abstraction_file ) ) { 2196 return null; 2197 } 2198 2199 require_once $abstraction_file; 2200 } 2201 $method = "WP_Filesystem_$method"; 2202 2203 $wp_filesystem = new $method( $args ); 2204 2205 /* 2206 * Define the timeouts for the connections. Only available after the constructor is called 2207 * to allow for per-transport overriding of the default. 2208 */ 2209 if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) { 2210 define( 'FS_CONNECT_TIMEOUT', 30 ); // 30 seconds. 2211 } 2212 if ( ! defined( 'FS_TIMEOUT' ) ) { 2213 define( 'FS_TIMEOUT', 30 ); // 30 seconds. 2214 } 2215 2216 if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 2217 return false; 2218 } 2219 2220 if ( ! $wp_filesystem->connect() ) { 2221 return false; // There was an error connecting to the server. 2222 } 2223 2224 // Set the permission constants if not already set. 2225 if ( ! defined( 'FS_CHMOD_DIR' ) ) { 2226 define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) ); 2227 } 2228 if ( ! defined( 'FS_CHMOD_FILE' ) ) { 2229 define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) ); 2230 } 2231 2232 return true; 2233 } 2234 2235 /** 2236 * Determines which method to use for reading, writing, modifying, or deleting 2237 * files on the filesystem. 2238 * 2239 * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets 2240 * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2', 2241 * 'ftpext' or 'ftpsockets'. 2242 * 2243 * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`, 2244 * or filtering via {@see 'filesystem_method'}. 2245 * 2246 * @link https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#wordpress-upgrade-constants 2247 * 2248 * Plugins may define a custom transport handler, See WP_Filesystem(). 2249 * 2250 * @since 2.5.0 2251 * 2252 * @global callable $_wp_filesystem_direct_method 2253 * 2254 * @param array $args Optional. Connection details. Default empty array. 2255 * @param string $context Optional. Full path to the directory that is tested 2256 * for being writable. Default empty. 2257 * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. 2258 * Default false. 2259 * @return string The transport to use, see description for valid return values. 2260 */ 2261 function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) { 2262 // Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'. 2263 $method = defined( 'FS_METHOD' ) ? FS_METHOD : false; 2264 2265 if ( ! $context ) { 2266 $context = WP_CONTENT_DIR; 2267 } 2268 2269 // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it. 2270 if ( WP_LANG_DIR === $context && ! is_dir( $context ) ) { 2271 $context = dirname( $context ); 2272 } 2273 2274 $context = trailingslashit( $context ); 2275 2276 if ( ! $method ) { 2277 2278 $temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) ); 2279 $temp_handle = @fopen( $temp_file_name, 'w' ); 2280 if ( $temp_handle ) { 2281 2282 // Attempt to determine the file owner of the WordPress files, and that of newly created files. 2283 $wp_file_owner = false; 2284 $temp_file_owner = false; 2285 if ( function_exists( 'fileowner' ) ) { 2286 $wp_file_owner = @fileowner( __FILE__ ); 2287 $temp_file_owner = @fileowner( $temp_file_name ); 2288 } 2289 2290 if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) { 2291 /* 2292 * WordPress is creating files as the same owner as the WordPress files, 2293 * this means it's safe to modify & create new files via PHP. 2294 */ 2295 $method = 'direct'; 2296 $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; 2297 } elseif ( $allow_relaxed_file_ownership ) { 2298 /* 2299 * The $context directory is writable, and $allow_relaxed_file_ownership is set, 2300 * this means we can modify files safely in this directory. 2301 * This mode doesn't create new files, only alter existing ones. 2302 */ 2303 $method = 'direct'; 2304 $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; 2305 } 2306 2307 fclose( $temp_handle ); 2308 @unlink( $temp_file_name ); 2309 } 2310 } 2311 2312 if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) { 2313 $method = 'ssh2'; 2314 } 2315 if ( ! $method && extension_loaded( 'ftp' ) ) { 2316 $method = 'ftpext'; 2317 } 2318 if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) { 2319 $method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread. 2320 } 2321 2322 /** 2323 * Filters the filesystem method to use. 2324 * 2325 * @since 2.6.0 2326 * 2327 * @param string $method Filesystem method to return. 2328 * @param array $args An array of connection details for the method. 2329 * @param string $context Full path to the directory that is tested for being writable. 2330 * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. 2331 */ 2332 return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership ); 2333 } 2334 2335 /** 2336 * Displays a form to the user to request for their FTP/SSH details in order 2337 * to connect to the filesystem. 2338 * 2339 * All chosen/entered details are saved, excluding the password. 2340 * 2341 * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) 2342 * to specify an alternate FTP/SSH port. 2343 * 2344 * Plugins may override this form by returning true|false via the {@see 'request_filesystem_credentials'} filter. 2345 * 2346 * @since 2.5.0 2347 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string. 2348 * 2349 * @global string $pagenow The filename of the current screen. 2350 * 2351 * @param string $form_post The URL to post the form to. 2352 * @param string $type Optional. Chosen type of filesystem. Default empty. 2353 * @param bool|WP_Error $error Optional. Whether the current request has failed 2354 * to connect, or an error object. Default false. 2355 * @param string $context Optional. Full path to the directory that is tested 2356 * for being writable. Default empty. 2357 * @param array $extra_fields Optional. Extra `POST` fields to be checked 2358 * for inclusion in the post. Default null. 2359 * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. 2360 * Default false. 2361 * @return bool|array True if no filesystem credentials are required, 2362 * false if they are required but have not been provided, 2363 * array of credentials if they are required and have been provided. 2364 */ 2365 function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) { 2366 global $pagenow; 2367 2368 /** 2369 * Filters the filesystem credentials. 2370 * 2371 * Returning anything other than an empty string will effectively short-circuit 2372 * output of the filesystem credentials form, returning that value instead. 2373 * 2374 * A filter should return true if no filesystem credentials are required, false if they are required but have not been 2375 * provided, or an array of credentials if they are required and have been provided. 2376 * 2377 * @since 2.5.0 2378 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string. 2379 * 2380 * @param mixed $credentials Credentials to return instead. Default empty string. 2381 * @param string $form_post The URL to post the form to. 2382 * @param string $type Chosen type of filesystem. 2383 * @param bool|WP_Error $error Whether the current request has failed to connect, 2384 * or an error object. 2385 * @param string $context Full path to the directory that is tested for 2386 * being writable. 2387 * @param array $extra_fields Extra POST fields. 2388 * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. 2389 */ 2390 $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ); 2391 2392 if ( '' !== $req_cred ) { 2393 return $req_cred; 2394 } 2395 2396 if ( empty( $type ) ) { 2397 $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); 2398 } 2399 2400 if ( 'direct' === $type ) { 2401 return true; 2402 } 2403 2404 if ( is_null( $extra_fields ) ) { 2405 $extra_fields = array( 'version', 'locale' ); 2406 } 2407 2408 $credentials = get_option( 2409 'ftp_credentials', 2410 array( 2411 'hostname' => '', 2412 'username' => '', 2413 ) 2414 ); 2415 2416 $submitted_form = wp_unslash( $_POST ); 2417 2418 // Verify nonce, or unset submitted form field values on failure. 2419 if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) { 2420 unset( 2421 $submitted_form['hostname'], 2422 $submitted_form['username'], 2423 $submitted_form['password'], 2424 $submitted_form['public_key'], 2425 $submitted_form['private_key'], 2426 $submitted_form['connection_type'] 2427 ); 2428 } 2429 2430 $ftp_constants = array( 2431 'hostname' => 'FTP_HOST', 2432 'username' => 'FTP_USER', 2433 'password' => 'FTP_PASS', 2434 'public_key' => 'FTP_PUBKEY', 2435 'private_key' => 'FTP_PRIKEY', 2436 ); 2437 2438 /* 2439 * If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string. 2440 * Otherwise, keep it as it previously was (saved details in option). 2441 */ 2442 foreach ( $ftp_constants as $key => $constant ) { 2443 if ( defined( $constant ) ) { 2444 $credentials[ $key ] = constant( $constant ); 2445 } elseif ( ! empty( $submitted_form[ $key ] ) ) { 2446 $credentials[ $key ] = $submitted_form[ $key ]; 2447 } elseif ( ! isset( $credentials[ $key ] ) ) { 2448 $credentials[ $key ] = ''; 2449 } 2450 } 2451 2452 // Sanitize the hostname, some people might pass in odd data. 2453 $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); // Strip any schemes off. 2454 2455 if ( strpos( $credentials['hostname'], ':' ) ) { 2456 list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); 2457 if ( ! is_numeric( $credentials['port'] ) ) { 2458 unset( $credentials['port'] ); 2459 } 2460 } else { 2461 unset( $credentials['port'] ); 2462 } 2463 2464 if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) { 2465 $credentials['connection_type'] = 'ssh'; 2466 } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL. 2467 $credentials['connection_type'] = 'ftps'; 2468 } elseif ( ! empty( $submitted_form['connection_type'] ) ) { 2469 $credentials['connection_type'] = $submitted_form['connection_type']; 2470 } elseif ( ! isset( $credentials['connection_type'] ) ) { // All else fails (and it's not defaulted to something else saved), default to FTP. 2471 $credentials['connection_type'] = 'ftp'; 2472 } 2473 2474 if ( ! $error 2475 && ( ! empty( $credentials['hostname'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['password'] ) 2476 || 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) 2477 ) 2478 ) { 2479 $stored_credentials = $credentials; 2480 2481 if ( ! empty( $stored_credentials['port'] ) ) { // Save port as part of hostname to simplify above code. 2482 $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; 2483 } 2484 2485 unset( 2486 $stored_credentials['password'], 2487 $stored_credentials['port'], 2488 $stored_credentials['private_key'], 2489 $stored_credentials['public_key'] 2490 ); 2491 2492 if ( ! wp_installing() ) { 2493 update_option( 'ftp_credentials', $stored_credentials, false ); 2494 } 2495 2496 return $credentials; 2497 } 2498 2499 $hostname = $credentials['hostname'] ?? ''; 2500 $username = $credentials['username'] ?? ''; 2501 $public_key = $credentials['public_key'] ?? ''; 2502 $private_key = $credentials['private_key'] ?? ''; 2503 $port = $credentials['port'] ?? ''; 2504 $connection_type = $credentials['connection_type'] ?? ''; 2505 2506 if ( $error ) { 2507 $error_string = __( '<strong>Error:</strong> Could not connect to the server. Please verify the settings are correct.' ); 2508 if ( is_wp_error( $error ) ) { 2509 $error_string = esc_html( $error->get_error_message() ); 2510 } 2511 wp_admin_notice( 2512 $error_string, 2513 array( 2514 'id' => 'message', 2515 'additional_classes' => array( 'error' ), 2516 ) 2517 ); 2518 } 2519 2520 $types = array(); 2521 if ( extension_loaded( 'ftp' ) || extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) { 2522 $types['ftp'] = __( 'FTP' ); 2523 } 2524 if ( extension_loaded( 'ftp' ) ) { // Only this supports FTPS. 2525 $types['ftps'] = __( 'FTPS (SSL)' ); 2526 } 2527 if ( extension_loaded( 'ssh2' ) ) { 2528 $types['ssh'] = __( 'SSH2' ); 2529 } 2530 2531 /** 2532 * Filters the connection types to output to the filesystem credentials form. 2533 * 2534 * @since 2.9.0 2535 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string. 2536 * 2537 * @param string[] $types Types of connections. 2538 * @param array $credentials Credentials to connect with. 2539 * @param string $type Chosen filesystem method. 2540 * @param bool|WP_Error $error Whether the current request has failed to connect, 2541 * or an error object. 2542 * @param string $context Full path to the directory that is tested for being writable. 2543 */ 2544 $types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context ); 2545 ?> 2546 <form action="<?php echo esc_url( $form_post ); ?>" method="post"> 2547 <div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form"> 2548 <?php 2549 // Print a H1 heading in the FTP credentials modal dialog, default is a H2. 2550 $heading_tag = 'h2'; 2551 if ( 'plugins.php' === $pagenow || 'plugin-install.php' === $pagenow ) { 2552 $heading_tag = 'h1'; 2553 } 2554 echo "<$heading_tag id='request-filesystem-credentials-title'>" . __( 'Connection Information' ) . "</$heading_tag>"; 2555 ?> 2556 <p id="request-filesystem-credentials-desc"> 2557 <?php 2558 $label_user = __( 'Username' ); 2559 $label_pass = __( 'Password' ); 2560 _e( 'To perform the requested action, WordPress needs to access your web server.' ); 2561 echo ' '; 2562 if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) { 2563 if ( isset( $types['ssh'] ) ) { 2564 _e( 'Please enter your FTP or SSH credentials to proceed.' ); 2565 $label_user = __( 'FTP/SSH Username' ); 2566 $label_pass = __( 'FTP/SSH Password' ); 2567 } else { 2568 _e( 'Please enter your FTP credentials to proceed.' ); 2569 $label_user = __( 'FTP Username' ); 2570 $label_pass = __( 'FTP Password' ); 2571 } 2572 echo ' '; 2573 } 2574 _e( 'If you do not remember your credentials, you should contact your web host.' ); 2575 2576 $hostname_value = esc_attr( $hostname ); 2577 if ( ! empty( $port ) ) { 2578 $hostname_value .= ":$port"; 2579 } 2580 2581 $password_value = ''; 2582 if ( defined( 'FTP_PASS' ) ) { 2583 $password_value = '*****'; 2584 } 2585 ?> 2586 </p> 2587 <label for="hostname"> 2588 <span class="field-title"><?php _e( 'Hostname' ); ?></span> 2589 <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 $hostname_value; ?>"<?php disabled( defined( 'FTP_HOST' ) ); ?> /> 2590 </label> 2591 <div class="ftp-username"> 2592 <label for="username"> 2593 <span class="field-title"><?php echo $label_user; ?></span> 2594 <input name="username" type="text" id="username" value="<?php echo esc_attr( $username ); ?>"<?php disabled( defined( 'FTP_USER' ) ); ?> /> 2595 </label> 2596 </div> 2597 <div class="ftp-password"> 2598 <label for="password"> 2599 <span class="field-title"><?php echo $label_pass; ?></span> 2600 <input name="password" type="password" id="password" value="<?php echo $password_value; ?>"<?php disabled( defined( 'FTP_PASS' ) ); ?> spellcheck="false" /> 2601 <?php 2602 if ( ! defined( 'FTP_PASS' ) ) { 2603 _e( 'This password will not be stored on the server.' ); 2604 } 2605 ?> 2606 </label> 2607 </div> 2608 <fieldset> 2609 <legend><?php _e( 'Connection Type' ); ?></legend> 2610 <?php 2611 $disabled = disabled( ( defined( 'FTP_SSL' ) && FTP_SSL ) || ( defined( 'FTP_SSH' ) && FTP_SSH ), true, false ); 2612 foreach ( $types as $name => $text ) : 2613 ?> 2614 <label for="<?php echo esc_attr( $name ); ?>"> 2615 <input type="radio" name="connection_type" id="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $name ); ?>" <?php checked( $name, $connection_type ); ?> <?php echo $disabled; ?> /> 2616 <?php echo $text; ?> 2617 </label> 2618 <?php 2619 endforeach; 2620 ?> 2621 </fieldset> 2622 <?php 2623 if ( isset( $types['ssh'] ) ) { 2624 $hidden_class = ''; 2625 if ( 'ssh' !== $connection_type ) { 2626 $hidden_class = ' class="hidden"'; 2627 } 2628 ?> 2629 <fieldset id="ssh-keys"<?php echo $hidden_class; ?>> 2630 <legend><?php _e( 'Authentication Keys' ); ?></legend> 2631 <label for="public_key"> 2632 <span class="field-title"><?php _e( 'Public Key:' ); ?></span> 2633 <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' ) ); ?> /> 2634 </label> 2635 <label for="private_key"> 2636 <span class="field-title"><?php _e( 'Private Key:' ); ?></span> 2637 <input name="private_key" type="text" id="private_key" value="<?php echo esc_attr( $private_key ); ?>"<?php disabled( defined( 'FTP_PRIKEY' ) ); ?> /> 2638 </label> 2639 <p 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.' ); ?></p> 2640 </fieldset> 2641 <?php 2642 } 2643 2644 foreach ( (array) $extra_fields as $field ) { 2645 if ( isset( $submitted_form[ $field ] ) ) { 2646 echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( $submitted_form[ $field ] ) . '" />'; 2647 } 2648 } 2649 2650 /* 2651 * Make sure the `submit_button()` function is available during the REST API call 2652 * from WP_Site_Health_Auto_Updates::test_check_wp_filesystem_method(). 2653 */ 2654 if ( ! function_exists( 'submit_button' ) ) { 2655 require_once ABSPATH . 'wp-admin/includes/template.php'; 2656 } 2657 ?> 2658 <p class="request-filesystem-credentials-action-buttons"> 2659 <?php wp_nonce_field( 'filesystem-credentials', '_fs_nonce', false, true ); ?> 2660 <button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button> 2661 <?php submit_button( __( 'Proceed' ), 'primary', 'upgrade', false ); ?> 2662 </p> 2663 </div> 2664 </form> 2665 <?php 2666 return false; 2667 } 2668 2669 /** 2670 * Prints the filesystem credentials modal when needed. 2671 * 2672 * @since 4.2.0 2673 */ 2674 function wp_print_request_filesystem_credentials_modal() { 2675 $filesystem_method = get_filesystem_method(); 2676 2677 ob_start(); 2678 $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); 2679 ob_end_clean(); 2680 2681 $request_filesystem_credentials = ( 'direct' !== $filesystem_method && ! $filesystem_credentials_are_stored ); 2682 if ( ! $request_filesystem_credentials ) { 2683 return; 2684 } 2685 ?> 2686 <div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog"> 2687 <div class="notification-dialog-background"></div> 2688 <div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0"> 2689 <div class="request-filesystem-credentials-dialog-content"> 2690 <?php request_filesystem_credentials( site_url() ); ?> 2691 </div> 2692 </div> 2693 </div> 2694 <?php 2695 } 2696 2697 /** 2698 * Attempts to clear the opcode cache for an individual PHP file. 2699 * 2700 * This function can be called safely without having to check the file extension 2701 * or availability of the OPcache extension. 2702 * 2703 * Whether or not invalidation is possible is cached to improve performance. 2704 * 2705 * @since 5.5.0 2706 * 2707 * @link https://www.php.net/manual/en/function.opcache-invalidate.php 2708 * 2709 * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared. 2710 * @param bool $force Invalidate even if the modification time is not newer than the file in cache. 2711 * Default false. 2712 * @return bool True if opcache was invalidated for `$filepath`, or there was nothing to invalidate. 2713 * False if opcache invalidation is not available, or is disabled via filter. 2714 */ 2715 function wp_opcache_invalidate( $filepath, $force = false ) { 2716 static $can_invalidate = null; 2717 2718 /* 2719 * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value. 2720 * 2721 * First, check to see if the function is available to call, then if the host has restricted 2722 * the ability to run the function to avoid a PHP warning. 2723 * 2724 * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`. 2725 * 2726 * If the host has this set, check whether the path in `opcache.restrict_api` matches 2727 * the beginning of the path of the origin file. 2728 * 2729 * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()` 2730 * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI. 2731 * 2732 * For more details, see: 2733 * - https://www.php.net/manual/en/opcache.configuration.php 2734 * - https://www.php.net/manual/en/reserved.variables.server.php 2735 * - https://core.trac.wordpress.org/ticket/36455 2736 */ 2737 if ( null === $can_invalidate 2738 && function_exists( 'opcache_invalidate' ) 2739 && ( ! ini_get( 'opcache.restrict_api' ) 2740 || stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 ) 2741 ) { 2742 $can_invalidate = true; 2743 } 2744 2745 // If invalidation is not available, return early. 2746 if ( ! $can_invalidate ) { 2747 return false; 2748 } 2749 2750 // Verify that file to be invalidated has a PHP extension. 2751 if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) { 2752 return false; 2753 } 2754 2755 /** 2756 * Filters whether to invalidate a file from the opcode cache. 2757 * 2758 * @since 5.5.0 2759 * 2760 * @param bool $will_invalidate Whether WordPress will invalidate `$filepath`. Default true. 2761 * @param string $filepath The path to the PHP file to invalidate. 2762 */ 2763 if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) { 2764 return opcache_invalidate( $filepath, $force ); 2765 } 2766 2767 return false; 2768 } 2769 2770 /** 2771 * Attempts to clear the opcode cache for a directory of files. 2772 * 2773 * @since 6.2.0 2774 * 2775 * @see wp_opcache_invalidate() 2776 * @link https://www.php.net/manual/en/function.opcache-invalidate.php 2777 * 2778 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2779 * 2780 * @param string $dir The path to the directory for which the opcode cache is to be cleared. 2781 */ 2782 function wp_opcache_invalidate_directory( $dir ) { 2783 global $wp_filesystem; 2784 2785 if ( ! is_string( $dir ) || '' === trim( $dir ) ) { 2786 if ( WP_DEBUG ) { 2787 $error_message = sprintf( 2788 /* translators: %s: The function name. */ 2789 __( '%s expects a non-empty string.' ), 2790 '<code>wp_opcache_invalidate_directory()</code>' 2791 ); 2792 wp_trigger_error( '', $error_message ); 2793 } 2794 return; 2795 } 2796 2797 $dirlist = $wp_filesystem->dirlist( $dir, false, true ); 2798 2799 if ( empty( $dirlist ) ) { 2800 return; 2801 } 2802 2803 /* 2804 * Recursively invalidate opcache of files in a directory. 2805 * 2806 * WP_Filesystem_*::dirlist() returns an array of file and directory information. 2807 * 2808 * This does not include a path to the file or directory. 2809 * To invalidate files within sub-directories, recursion is needed 2810 * to prepend an absolute path containing the sub-directory's name. 2811 * 2812 * @param array $dirlist Array of file/directory information from WP_Filesystem_Base::dirlist(), 2813 * with sub-directories represented as nested arrays. 2814 * @param string $path Absolute path to the directory. 2815 */ 2816 $invalidate_directory = static function ( $dirlist, $path ) use ( &$invalidate_directory ) { 2817 $path = trailingslashit( $path ); 2818 2819 foreach ( $dirlist as $name => $details ) { 2820 if ( 'f' === $details['type'] ) { 2821 wp_opcache_invalidate( $path . $name, true ); 2822 } elseif ( is_array( $details['files'] ) && ! empty( $details['files'] ) ) { 2823 $invalidate_directory( $details['files'], $path . $name ); 2824 } 2825 } 2826 }; 2827 2828 $invalidate_directory( $dirlist, $dir ); 2829 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Apr 15 08:20:10 2026 | Cross-referenced by PHPXref |