| [ 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|void Returns the file name on success, 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 } 757 758 /** 759 * Handles PHP uploads in WordPress. 760 * 761 * Sanitizes file names, checks extensions for mime type, and moves the file 762 * to the appropriate directory within the uploads directory. 763 * 764 * @access private 765 * @since 4.0.0 766 * 767 * @see wp_handle_upload_error 768 * 769 * @param array $file { 770 * Reference to a single element from `$_FILES`. Call the function once for each uploaded file. 771 * 772 * @type string $name The original name of the file on the client machine. 773 * @type string $type The mime type of the file, if the browser provided this information. 774 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 775 * @type int $size The size, in bytes, of the uploaded file. 776 * @type int $error The error code associated with this file upload. 777 * } 778 * @param array|false $overrides { 779 * An array of override parameters for this file, or boolean false if none are provided. 780 * 781 * @type callable $upload_error_handler Function to call when there is an error during the upload process. 782 * See {@see wp_handle_upload_error()}. 783 * @type callable $unique_filename_callback Function to call when determining a unique file name for the file. 784 * See {@see wp_unique_filename()}. 785 * @type string[] $upload_error_strings The strings that describe the error indicated in 786 * `$_FILES[{form field}]['error']`. 787 * @type bool $test_form Whether to test that the `$_POST['action']` parameter is as expected. 788 * @type bool $test_size Whether to test that the file size is greater than zero bytes. 789 * @type bool $test_type Whether to test that the mime type of the file is as expected. 790 * @type string[] $mimes Array of allowed mime types keyed by their file extension regex. 791 * } 792 * @param string $time Time formatted in 'yyyy/mm'. 793 * @param string $action Expected value for `$_POST['action']`. 794 * @return array { 795 * On success, returns an associative array of file attributes. 796 * On failure, returns `$overrides['upload_error_handler']( &$file, $message )` 797 * or `array( 'error' => $message )`. 798 * 799 * @type string $file Filename of the newly-uploaded file. 800 * @type string $url URL of the newly-uploaded file. 801 * @type string $type Mime type of the newly-uploaded file. 802 * } 803 */ 804 function _wp_handle_upload( &$file, $overrides, $time, $action ) { 805 // The default error handler. 806 if ( ! function_exists( 'wp_handle_upload_error' ) ) { 807 function wp_handle_upload_error( &$file, $message ) { 808 return array( 'error' => $message ); 809 } 810 } 811 812 /** 813 * Filters the data for a file before it is uploaded to WordPress. 814 * 815 * The dynamic portion of the hook name, `$action`, refers to the post action. 816 * 817 * Possible hook names include: 818 * 819 * - `wp_handle_sideload_prefilter` 820 * - `wp_handle_upload_prefilter` 821 * 822 * @since 2.9.0 as 'wp_handle_upload_prefilter'. 823 * @since 4.0.0 Converted to a dynamic hook with `$action`. 824 * 825 * @param array $file { 826 * Reference to a single element from `$_FILES`. 827 * 828 * @type string $name The original name of the file on the client machine. 829 * @type string $type The mime type of the file, if the browser provided this information. 830 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 831 * @type int $size The size, in bytes, of the uploaded file. 832 * @type int $error The error code associated with this file upload. 833 * } 834 */ 835 $file = apply_filters( "{$action}_prefilter", $file ); 836 837 /** 838 * Filters the override parameters for a file before it is uploaded to WordPress. 839 * 840 * The dynamic portion of the hook name, `$action`, refers to the post action. 841 * 842 * Possible hook names include: 843 * 844 * - `wp_handle_sideload_overrides` 845 * - `wp_handle_upload_overrides` 846 * 847 * @since 5.7.0 848 * 849 * @param array|false $overrides An array of override parameters for this file. Boolean false if none are 850 * provided. See {@see _wp_handle_upload()}. 851 * @param array $file { 852 * Reference to a single element from `$_FILES`. 853 * 854 * @type string $name The original name of the file on the client machine. 855 * @type string $type The mime type of the file, if the browser provided this information. 856 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 857 * @type int $size The size, in bytes, of the uploaded file. 858 * @type int $error The error code associated with this file upload. 859 * } 860 */ 861 $overrides = apply_filters( "{$action}_overrides", $overrides, $file ); 862 863 // You may define your own function and pass the name in $overrides['upload_error_handler']. 864 $upload_error_handler = 'wp_handle_upload_error'; 865 if ( isset( $overrides['upload_error_handler'] ) ) { 866 $upload_error_handler = $overrides['upload_error_handler']; 867 } 868 869 // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully. 870 if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) { 871 return call_user_func_array( $upload_error_handler, array( &$file, $file['error'] ) ); 872 } 873 874 // Install user overrides. Did we mention that this voids your warranty? 875 876 // You may define your own function and pass the name in $overrides['unique_filename_callback']. 877 $unique_filename_callback = null; 878 if ( isset( $overrides['unique_filename_callback'] ) ) { 879 $unique_filename_callback = $overrides['unique_filename_callback']; 880 } 881 882 /* 883 * This may not have originally been intended to be overridable, 884 * but historically has been. 885 */ 886 if ( isset( $overrides['upload_error_strings'] ) ) { 887 $upload_error_strings = $overrides['upload_error_strings']; 888 } else { 889 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error']. 890 $upload_error_strings = array( 891 false, 892 sprintf( 893 /* translators: 1: upload_max_filesize, 2: php.ini */ 894 __( 'The uploaded file exceeds the %1$s directive in %2$s.' ), 895 'upload_max_filesize', 896 'php.ini' 897 ), 898 sprintf( 899 /* translators: %s: MAX_FILE_SIZE */ 900 __( 'The uploaded file exceeds the %s directive that was specified in the HTML form.' ), 901 'MAX_FILE_SIZE' 902 ), 903 __( 'The uploaded file was only partially uploaded.' ), 904 __( 'No file was uploaded.' ), 905 '', 906 __( 'Missing a temporary folder.' ), 907 __( 'Failed to write file to disk.' ), 908 __( 'File upload stopped by extension.' ), 909 ); 910 } 911 912 // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false; 913 $test_form = $overrides['test_form'] ?? true; 914 $test_size = $overrides['test_size'] ?? true; 915 916 // If you override this, you must provide $ext and $type!! 917 $test_type = $overrides['test_type'] ?? true; 918 $mimes = $overrides['mimes'] ?? null; 919 920 // A correct form post will pass this test. 921 if ( $test_form && ( ! isset( $_POST['action'] ) || $_POST['action'] !== $action ) ) { 922 return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) ); 923 } 924 925 // A successful upload will pass this test. It makes no sense to override this one. 926 if ( isset( $file['error'] ) && $file['error'] > 0 ) { 927 return call_user_func_array( $upload_error_handler, array( &$file, $upload_error_strings[ $file['error'] ] ) ); 928 } 929 930 // A properly uploaded file will pass this test. There should be no reason to override this one. 931 $test_uploaded_file = 'wp_handle_upload' === $action ? is_uploaded_file( $file['tmp_name'] ) : @is_readable( $file['tmp_name'] ); 932 if ( ! $test_uploaded_file ) { 933 return call_user_func_array( $upload_error_handler, array( &$file, __( 'Specified file failed upload test.' ) ) ); 934 } 935 936 $test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] ); 937 // A non-empty file will pass this test. 938 if ( $test_size && ! ( $test_file_size > 0 ) ) { 939 if ( is_multisite() ) { 940 $error_msg = __( 'File is empty. Please upload something more substantial.' ); 941 } else { 942 $error_msg = sprintf( 943 /* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */ 944 __( '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.' ), 945 'php.ini', 946 'post_max_size', 947 'upload_max_filesize' 948 ); 949 } 950 951 return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) ); 952 } 953 954 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter. 955 if ( $test_type ) { 956 $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes ); 957 $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext']; 958 $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type']; 959 $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename']; 960 961 // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect. 962 if ( $proper_filename ) { 963 $file['name'] = $proper_filename; 964 } 965 966 if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) { 967 return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, you are not allowed to upload this file type.' ) ) ); 968 } 969 970 if ( ! $type ) { 971 $type = $file['type']; 972 } 973 } else { 974 $type = ''; 975 } 976 977 /* 978 * A writable uploads dir will pass this test. Again, there's no point 979 * overriding this one. 980 */ 981 $uploads = wp_upload_dir( $time ); 982 if ( ! ( $uploads && false === $uploads['error'] ) ) { 983 return call_user_func_array( $upload_error_handler, array( &$file, $uploads['error'] ) ); 984 } 985 986 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback ); 987 988 // Move the file to the uploads dir. 989 $new_file = $uploads['path'] . "/$filename"; 990 991 /** 992 * Filters whether to short-circuit moving the uploaded file after passing all checks. 993 * 994 * If a non-null value is returned from the filter, moving the file and any related 995 * error reporting will be completely skipped. 996 * 997 * @since 4.9.0 998 * 999 * @param mixed $move_new_file If null (default) move the file after the upload. 1000 * @param array $file { 1001 * Reference to a single element from `$_FILES`. 1002 * 1003 * @type string $name The original name of the file on the client machine. 1004 * @type string $type The mime type of the file, if the browser provided this information. 1005 * @type string $tmp_name The temporary filename of the file in which the uploaded file was stored on the server. 1006 * @type int $size The size, in bytes, of the uploaded file. 1007 * @type int $error The error code associated with this file upload. 1008 * } 1009 * @param string $new_file Filename of the newly-uploaded file. 1010 * @param string $type Mime type of the newly-uploaded file. 1011 */ 1012 $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type ); 1013 1014 if ( null === $move_new_file ) { 1015 if ( 'wp_handle_upload' === $action ) { 1016 $move_new_file = @move_uploaded_file( $file['tmp_name'], $new_file ); 1017 } else { 1018 // Use copy and unlink because rename breaks streams. 1019 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged 1020 $move_new_file = @copy( $file['tmp_name'], $new_file ); 1021 unlink( $file['tmp_name'] ); 1022 } 1023 1024 if ( false === $move_new_file ) { 1025 if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) { 1026 $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; 1027 } else { 1028 $error_path = basename( $uploads['basedir'] ) . $uploads['subdir']; 1029 } 1030 1031 return $upload_error_handler( 1032 $file, 1033 sprintf( 1034 /* translators: %s: Destination file path. */ 1035 __( 'The uploaded file could not be moved to %s.' ), 1036 $error_path 1037 ) 1038 ); 1039 } 1040 } 1041 1042 // Set correct file permissions. 1043 $stat = stat( dirname( $new_file ) ); 1044 $perms = $stat['mode'] & 0000666; 1045 chmod( $new_file, $perms ); 1046 1047 // Compute the URL. 1048 $url = $uploads['url'] . "/$filename"; 1049 1050 if ( is_multisite() ) { 1051 clean_dirsize_cache( $new_file ); 1052 } 1053 1054 /** 1055 * Filters the data array for the uploaded file. 1056 * 1057 * @since 2.1.0 1058 * 1059 * @param array $upload { 1060 * Array of upload data. 1061 * 1062 * @type string $file Filename of the newly-uploaded file. 1063 * @type string $url URL of the newly-uploaded file. 1064 * @type string $type Mime type of the newly-uploaded file. 1065 * } 1066 * @param string $context The type of upload action. Values include 'upload' or 'sideload'. 1067 */ 1068 return apply_filters( 1069 'wp_handle_upload', 1070 array( 1071 'file' => $new_file, 1072 'url' => $url, 1073 'type' => $type, 1074 ), 1075 'wp_handle_sideload' === $action ? 'sideload' : 'upload' 1076 ); 1077 } 1078 1079 /** 1080 * Wrapper for _wp_handle_upload(). 1081 * 1082 * Passes the {@see 'wp_handle_upload'} action. 1083 * 1084 * @since 2.0.0 1085 * 1086 * @see _wp_handle_upload() 1087 * 1088 * @param array $file Reference to a single element of `$_FILES`. 1089 * Call the function once for each uploaded file. 1090 * See _wp_handle_upload() for accepted values. 1091 * @param array|false $overrides Optional. An associative array of names => values 1092 * to override default variables. Default false. 1093 * See _wp_handle_upload() for accepted values. 1094 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 1095 * @return array See _wp_handle_upload() for return value. 1096 */ 1097 function wp_handle_upload( &$file, $overrides = false, $time = null ) { 1098 /* 1099 * $_POST['action'] must be set and its value must equal $overrides['action'] 1100 * or this: 1101 */ 1102 $action = $overrides['action'] ?? 'wp_handle_upload'; 1103 return _wp_handle_upload( $file, $overrides, $time, $action ); 1104 } 1105 1106 /** 1107 * Wrapper for _wp_handle_upload(). 1108 * 1109 * Passes the {@see 'wp_handle_sideload'} action. 1110 * 1111 * @since 2.6.0 1112 * 1113 * @see _wp_handle_upload() 1114 * 1115 * @param array $file Reference to a single element of `$_FILES`. 1116 * Call the function once for each uploaded file. 1117 * See _wp_handle_upload() for accepted values. 1118 * @param array|false $overrides Optional. An associative array of names => values 1119 * to override default variables. Default false. 1120 * See _wp_handle_upload() for accepted values. 1121 * @param string|null $time Optional. Time formatted in 'yyyy/mm'. Default null. 1122 * @return array See _wp_handle_upload() for return value. 1123 */ 1124 function wp_handle_sideload( &$file, $overrides = false, $time = null ) { 1125 /* 1126 * $_POST['action'] must be set and its value must equal $overrides['action'] 1127 * or this: 1128 */ 1129 $action = $overrides['action'] ?? 'wp_handle_sideload'; 1130 return _wp_handle_upload( $file, $overrides, $time, $action ); 1131 } 1132 1133 /** 1134 * Downloads a URL to a local temporary file using the WordPress HTTP API. 1135 * 1136 * Please note that the calling function must delete or move the file. 1137 * 1138 * @since 2.5.0 1139 * @since 5.2.0 Signature Verification with SoftFail was added. 1140 * @since 5.9.0 Support for Content-Disposition filename was added. 1141 * 1142 * @param string $url The URL of the file to download. 1143 * @param int $timeout The timeout for the request to download the file. 1144 * Default 300 seconds. 1145 * @param bool $signature_verification Whether to perform Signature Verification. 1146 * Default false. 1147 * @return string|WP_Error Filename on success, WP_Error on failure. 1148 */ 1149 function download_url( $url, $timeout = 300, $signature_verification = false ) { 1150 // WARNING: The file is not automatically deleted, the script must delete or move the file. 1151 if ( ! $url ) { 1152 return new WP_Error( 'http_no_url', __( 'No URL Provided.' ) ); 1153 } 1154 1155 $url_path = parse_url( $url, PHP_URL_PATH ); 1156 $url_filename = ''; 1157 if ( is_string( $url_path ) && '' !== $url_path ) { 1158 $url_filename = basename( $url_path ); 1159 } 1160 1161 $tmpfname = wp_tempnam( $url_filename ); 1162 if ( ! $tmpfname ) { 1163 return new WP_Error( 'http_no_file', __( 'Could not create temporary file.' ) ); 1164 } 1165 1166 $response = wp_safe_remote_get( 1167 $url, 1168 array( 1169 'timeout' => $timeout, 1170 'stream' => true, 1171 'filename' => $tmpfname, 1172 ) 1173 ); 1174 1175 if ( is_wp_error( $response ) ) { 1176 unlink( $tmpfname ); 1177 return $response; 1178 } 1179 1180 $response_code = wp_remote_retrieve_response_code( $response ); 1181 1182 if ( 200 !== $response_code ) { 1183 $data = array( 1184 'code' => $response_code, 1185 ); 1186 1187 // Retrieve a sample of the response body for debugging purposes. 1188 $tmpf = fopen( $tmpfname, 'rb' ); 1189 1190 if ( $tmpf ) { 1191 /** 1192 * Filters the maximum error response body size in `download_url()`. 1193 * 1194 * @since 5.1.0 1195 * 1196 * @see download_url() 1197 * 1198 * @param int $size The maximum error response body size. Default 1 KB. 1199 */ 1200 $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES ); 1201 1202 $data['body'] = fread( $tmpf, $response_size ); 1203 fclose( $tmpf ); 1204 } 1205 1206 unlink( $tmpfname ); 1207 1208 return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data ); 1209 } 1210 1211 $content_disposition = wp_remote_retrieve_header( $response, 'Content-Disposition' ); 1212 1213 if ( $content_disposition ) { 1214 $content_disposition = strtolower( $content_disposition ); 1215 1216 if ( str_starts_with( $content_disposition, 'attachment; filename=' ) ) { 1217 $tmpfname_disposition = sanitize_file_name( substr( $content_disposition, 21 ) ); 1218 } else { 1219 $tmpfname_disposition = ''; 1220 } 1221 1222 // Potential file name must be valid string. 1223 if ( $tmpfname_disposition && is_string( $tmpfname_disposition ) 1224 && ( 0 === validate_file( $tmpfname_disposition ) ) 1225 ) { 1226 $tmpfname_disposition = dirname( $tmpfname ) . '/' . $tmpfname_disposition; 1227 1228 if ( rename( $tmpfname, $tmpfname_disposition ) ) { 1229 $tmpfname = $tmpfname_disposition; 1230 } 1231 1232 if ( ( $tmpfname !== $tmpfname_disposition ) && file_exists( $tmpfname_disposition ) ) { 1233 unlink( $tmpfname_disposition ); 1234 } 1235 } 1236 } 1237 1238 $mime_type = wp_remote_retrieve_header( $response, 'content-type' ); 1239 if ( $mime_type && 'tmp' === pathinfo( $tmpfname, PATHINFO_EXTENSION ) ) { 1240 $valid_mime_types = array_flip( get_allowed_mime_types() ); 1241 if ( ! empty( $valid_mime_types[ $mime_type ] ) ) { 1242 $extensions = explode( '|', $valid_mime_types[ $mime_type ] ); 1243 $new_image_name = substr( $tmpfname, 0, -4 ) . ".{$extensions[0]}"; 1244 if ( 0 === validate_file( $new_image_name ) ) { 1245 if ( rename( $tmpfname, $new_image_name ) ) { 1246 $tmpfname = $new_image_name; 1247 } 1248 1249 if ( ( $tmpfname !== $new_image_name ) && file_exists( $new_image_name ) ) { 1250 unlink( $new_image_name ); 1251 } 1252 } 1253 } 1254 } 1255 1256 $content_md5 = wp_remote_retrieve_header( $response, 'Content-MD5' ); 1257 1258 if ( $content_md5 ) { 1259 $md5_check = verify_file_md5( $tmpfname, $content_md5 ); 1260 1261 if ( is_wp_error( $md5_check ) ) { 1262 unlink( $tmpfname ); 1263 return $md5_check; 1264 } 1265 } 1266 1267 // If the caller expects signature verification to occur, check to see if this URL supports it. 1268 if ( $signature_verification ) { 1269 /** 1270 * Filters the list of hosts which should have Signature Verification attempted on. 1271 * 1272 * @since 5.2.0 1273 * 1274 * @param string[] $hostnames List of hostnames. 1275 */ 1276 $signed_hostnames = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) ); 1277 1278 $signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true ); 1279 } 1280 1281 // Perform signature validation if supported. 1282 if ( $signature_verification ) { 1283 $signature = wp_remote_retrieve_header( $response, 'X-Content-Signature' ); 1284 1285 if ( ! $signature ) { 1286 /* 1287 * Retrieve signatures from a file if the header wasn't included. 1288 * WordPress.org stores signatures at $package_url.sig. 1289 */ 1290 1291 $signature_url = false; 1292 1293 if ( is_string( $url_path ) && ( str_ends_with( $url_path, '.zip' ) || str_ends_with( $url_path, '.tar.gz' ) ) ) { 1294 $signature_url = str_replace( $url_path, $url_path . '.sig', $url ); 1295 } 1296 1297 /** 1298 * Filters the URL where the signature for a file is located. 1299 * 1300 * @since 5.2.0 1301 * 1302 * @param false|string $signature_url The URL where signatures can be found for a file, or false if none are known. 1303 * @param string $url The URL being verified. 1304 */ 1305 $signature_url = apply_filters( 'wp_signature_url', $signature_url, $url ); 1306 1307 if ( $signature_url ) { 1308 $signature_request = wp_safe_remote_get( 1309 $signature_url, 1310 array( 1311 'limit_response_size' => 10 * KB_IN_BYTES, // 10KB should be large enough for quite a few signatures. 1312 ) 1313 ); 1314 1315 if ( ! is_wp_error( $signature_request ) && 200 === wp_remote_retrieve_response_code( $signature_request ) ) { 1316 $signature = explode( "\n", wp_remote_retrieve_body( $signature_request ) ); 1317 } 1318 } 1319 } 1320 1321 // Perform the checks. 1322 $signature_verification = verify_file_signature( $tmpfname, $signature, $url_filename ); 1323 } 1324 1325 if ( is_wp_error( $signature_verification ) ) { 1326 if ( 1327 /** 1328 * Filters whether Signature Verification failures should be allowed to soft fail. 1329 * 1330 * WARNING: This may be removed from a future release. 1331 * 1332 * @since 5.2.0 1333 * 1334 * @param bool $signature_softfail If a softfail is allowed. 1335 * @param string $url The url being accessed. 1336 */ 1337 apply_filters( 'wp_signature_softfail', true, $url ) 1338 ) { 1339 $signature_verification->add_data( $tmpfname, 'softfail-filename' ); 1340 } else { 1341 // Hard-fail. 1342 unlink( $tmpfname ); 1343 } 1344 1345 return $signature_verification; 1346 } 1347 1348 return $tmpfname; 1349 } 1350 1351 /** 1352 * Calculates and compares the MD5 of a file to its expected value. 1353 * 1354 * @since 3.7.0 1355 * 1356 * @param string $filename The filename to check the MD5 of. 1357 * @param string $expected_md5 The expected MD5 of the file, either a base64-encoded raw md5, 1358 * or a hex-encoded md5. 1359 * @return bool|WP_Error True on success, false when the MD5 format is unknown/unexpected, 1360 * WP_Error on failure. 1361 */ 1362 function verify_file_md5( $filename, $expected_md5 ) { 1363 if ( 32 === strlen( $expected_md5 ) ) { 1364 $expected_raw_md5 = pack( 'H*', $expected_md5 ); 1365 } elseif ( 24 === strlen( $expected_md5 ) ) { 1366 $expected_raw_md5 = base64_decode( $expected_md5 ); 1367 } else { 1368 return false; // Unknown format. 1369 } 1370 1371 $file_md5 = md5_file( $filename, true ); 1372 1373 if ( $file_md5 === $expected_raw_md5 ) { 1374 return true; 1375 } 1376 1377 return new WP_Error( 1378 'md5_mismatch', 1379 sprintf( 1380 /* translators: 1: File checksum, 2: Expected checksum value. */ 1381 __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ), 1382 bin2hex( $file_md5 ), 1383 bin2hex( $expected_raw_md5 ) 1384 ) 1385 ); 1386 } 1387 1388 /** 1389 * Verifies the contents of a file against its ED25519 signature. 1390 * 1391 * @since 5.2.0 1392 * 1393 * @param string $filename The file to validate. 1394 * @param string|array $signatures A Signature provided for the file. 1395 * @param string|false $filename_for_errors Optional. A friendly filename for errors. 1396 * @return bool|WP_Error True on success, false if verification not attempted, 1397 * or WP_Error describing an error condition. 1398 */ 1399 function verify_file_signature( $filename, $signatures, $filename_for_errors = false ) { 1400 if ( ! $filename_for_errors ) { 1401 $filename_for_errors = wp_basename( $filename ); 1402 } 1403 1404 // Check we can process signatures. 1405 if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) || ! in_array( 'sha384', array_map( 'strtolower', hash_algos() ), true ) ) { 1406 return new WP_Error( 1407 'signature_verification_unsupported', 1408 sprintf( 1409 /* translators: %s: The filename of the package. */ 1410 __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ), 1411 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1412 ), 1413 ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' ) 1414 ); 1415 } 1416 1417 // Verify runtime speed of Sodium_Compat is acceptable. 1418 if ( ! extension_loaded( 'sodium' ) && ! ParagonIE_Sodium_Compat::polyfill_is_fast() ) { 1419 $sodium_compat_is_fast = false; 1420 1421 // Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one. 1422 if ( method_exists( 'ParagonIE_Sodium_Compat', 'runtime_speed_test' ) ) { 1423 /* 1424 * Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode, 1425 * as that's what WordPress utilizes during signing verifications. 1426 */ 1427 // phpcs:disable WordPress.NamingConventions.ValidVariableName 1428 $old_fastMult = ParagonIE_Sodium_Compat::$fastMult; 1429 ParagonIE_Sodium_Compat::$fastMult = true; 1430 $sodium_compat_is_fast = ParagonIE_Sodium_Compat::runtime_speed_test( 100, 10 ); 1431 ParagonIE_Sodium_Compat::$fastMult = $old_fastMult; 1432 // phpcs:enable 1433 } 1434 1435 /* 1436 * This cannot be performed in a reasonable amount of time. 1437 * https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast 1438 */ 1439 if ( ! $sodium_compat_is_fast ) { 1440 return new WP_Error( 1441 'signature_verification_unsupported', 1442 sprintf( 1443 /* translators: %s: The filename of the package. */ 1444 __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ), 1445 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1446 ), 1447 array( 1448 'php' => PHP_VERSION, 1449 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ), 1450 'polyfill_is_fast' => false, 1451 'max_execution_time' => ini_get( 'max_execution_time' ), 1452 ) 1453 ); 1454 } 1455 } 1456 1457 if ( ! $signatures ) { 1458 return new WP_Error( 1459 'signature_verification_no_signature', 1460 sprintf( 1461 /* translators: %s: The filename of the package. */ 1462 __( 'The authenticity of %s could not be verified as no signature was found.' ), 1463 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1464 ), 1465 array( 1466 'filename' => $filename_for_errors, 1467 ) 1468 ); 1469 } 1470 1471 $trusted_keys = wp_trusted_keys(); 1472 $file_hash = hash_file( 'sha384', $filename, true ); 1473 1474 mbstring_binary_safe_encoding(); 1475 1476 $skipped_key = 0; 1477 $skipped_signature = 0; 1478 1479 foreach ( (array) $signatures as $signature ) { 1480 $signature_raw = base64_decode( $signature ); 1481 1482 // Ensure only valid-length signatures are considered. 1483 if ( SODIUM_CRYPTO_SIGN_BYTES !== strlen( $signature_raw ) ) { 1484 ++$skipped_signature; 1485 continue; 1486 } 1487 1488 foreach ( (array) $trusted_keys as $key ) { 1489 $key_raw = base64_decode( $key ); 1490 1491 // Only pass valid public keys through. 1492 if ( SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES !== strlen( $key_raw ) ) { 1493 ++$skipped_key; 1494 continue; 1495 } 1496 1497 if ( sodium_crypto_sign_verify_detached( $signature_raw, $file_hash, $key_raw ) ) { 1498 reset_mbstring_encoding(); 1499 return true; 1500 } 1501 } 1502 } 1503 1504 reset_mbstring_encoding(); 1505 1506 return new WP_Error( 1507 'signature_verification_failed', 1508 sprintf( 1509 /* translators: %s: The filename of the package. */ 1510 __( 'The authenticity of %s could not be verified.' ), 1511 '<span class="code">' . esc_html( $filename_for_errors ) . '</span>' 1512 ), 1513 // Error data helpful for debugging: 1514 array( 1515 'filename' => $filename_for_errors, 1516 'keys' => $trusted_keys, 1517 'signatures' => $signatures, 1518 'hash' => bin2hex( $file_hash ), 1519 'skipped_key' => $skipped_key, 1520 'skipped_sig' => $skipped_signature, 1521 'php' => PHP_VERSION, 1522 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ), 1523 ) 1524 ); 1525 } 1526 1527 /** 1528 * Retrieves the list of signing keys trusted by WordPress. 1529 * 1530 * @since 5.2.0 1531 * 1532 * @return string[] Array of base64-encoded signing keys. 1533 */ 1534 function wp_trusted_keys() { 1535 $trusted_keys = array(); 1536 1537 if ( time() < 1617235200 ) { 1538 // WordPress.org Key #1 - This key is only valid before April 1st, 2021. 1539 $trusted_keys[] = 'fRPyrxb/MvVLbdsYi+OOEv4xc+Eqpsj+kkAS6gNOkI0='; 1540 } 1541 1542 // TODO: Add key #2 with longer expiration. 1543 1544 /** 1545 * Filters the valid signing keys used to verify the contents of files. 1546 * 1547 * @since 5.2.0 1548 * 1549 * @param string[] $trusted_keys The trusted keys that may sign packages. 1550 */ 1551 return apply_filters( 'wp_trusted_keys', $trusted_keys ); 1552 } 1553 1554 /** 1555 * Determines whether the given file is a valid ZIP file. 1556 * 1557 * This function does not test to ensure that a file exists. Non-existent files 1558 * are not valid ZIPs, so those will also return false. 1559 * 1560 * @since 6.4.4 1561 * 1562 * @param string $file Full path to the ZIP file. 1563 * @return bool Whether the file is a valid ZIP file. 1564 */ 1565 function wp_zip_file_is_valid( $file ) { 1566 /** This filter is documented in wp-admin/includes/file.php */ 1567 if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { 1568 $archive = new ZipArchive(); 1569 $archive_is_valid = $archive->open( $file, ZipArchive::CHECKCONS ); 1570 if ( true === $archive_is_valid ) { 1571 $archive->close(); 1572 return true; 1573 } 1574 } 1575 1576 // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. 1577 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; 1578 1579 $archive = new PclZip( $file ); 1580 $archive_is_valid = is_array( $archive->properties() ); 1581 1582 return $archive_is_valid; 1583 } 1584 1585 /** 1586 * Unzips a specified ZIP file to a location on the filesystem via the WordPress 1587 * Filesystem Abstraction. 1588 * 1589 * Assumes that WP_Filesystem() has already been called and set up. Does not extract 1590 * a root-level __MACOSX directory, if present. 1591 * 1592 * Attempts to increase the PHP memory limit to 256M before uncompressing. However, 1593 * the most memory required shouldn't be much larger than the archive itself. 1594 * 1595 * @since 2.5.0 1596 * 1597 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1598 * 1599 * @param string $file Full path and filename of ZIP archive. 1600 * @param string $to Full path on the filesystem to extract archive to. 1601 * @return true|WP_Error True on success, WP_Error on failure. 1602 */ 1603 function unzip_file( $file, $to ) { 1604 global $wp_filesystem; 1605 1606 if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) { 1607 return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) ); 1608 } 1609 1610 // Unzip can use a lot of memory, but not this much hopefully. 1611 wp_raise_memory_limit( 'admin' ); 1612 1613 $needed_dirs = array(); 1614 $to = trailingslashit( $to ); 1615 1616 // Determine any parent directories needed (of the upgrade directory). 1617 if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parents if no children exist. 1618 $path = preg_split( '![/\\\]!', untrailingslashit( $to ) ); 1619 for ( $i = count( $path ); $i >= 0; $i-- ) { 1620 if ( empty( $path[ $i ] ) ) { 1621 continue; 1622 } 1623 1624 $dir = implode( '/', array_slice( $path, 0, $i + 1 ) ); 1625 if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Skip it if it looks like a Windows Drive letter. 1626 continue; 1627 } 1628 1629 if ( ! $wp_filesystem->is_dir( $dir ) ) { 1630 $needed_dirs[] = $dir; 1631 } else { 1632 break; // A folder exists, therefore we don't need to check the levels below this. 1633 } 1634 } 1635 } 1636 1637 /** 1638 * Filters whether to use ZipArchive to unzip archives. 1639 * 1640 * @since 3.0.0 1641 * 1642 * @param bool $ziparchive Whether to use ZipArchive. Default true. 1643 */ 1644 if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) { 1645 $result = _unzip_file_ziparchive( $file, $to, $needed_dirs ); 1646 if ( true === $result ) { 1647 return $result; 1648 } elseif ( is_wp_error( $result ) ) { 1649 if ( 'incompatible_archive' !== $result->get_error_code() ) { 1650 return $result; 1651 } 1652 } 1653 } 1654 // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file. 1655 return _unzip_file_pclzip( $file, $to, $needed_dirs ); 1656 } 1657 1658 /** 1659 * Attempts to unzip an archive using the ZipArchive class. 1660 * 1661 * This function should not be called directly, use `unzip_file()` instead. 1662 * 1663 * Assumes that WP_Filesystem() has already been called and set up. 1664 * 1665 * @since 3.0.0 1666 * @access private 1667 * 1668 * @see unzip_file() 1669 * 1670 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1671 * 1672 * @param string $file Full path and filename of ZIP archive. 1673 * @param string $to Full path on the filesystem to extract archive to. 1674 * @param string[] $needed_dirs A partial list of required folders needed to be created. 1675 * @return true|WP_Error True on success, WP_Error on failure. 1676 */ 1677 function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { 1678 global $wp_filesystem; 1679 1680 $z = new ZipArchive(); 1681 1682 $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS ); 1683 1684 if ( true !== $zopen ) { 1685 return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) ); 1686 } 1687 1688 $uncompressed_size = 0; 1689 1690 for ( $i = 0; $i < $z->numFiles; $i++ ) { 1691 $info = $z->statIndex( $i ); 1692 1693 if ( ! $info ) { 1694 $z->close(); 1695 return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); 1696 } 1697 1698 if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. 1699 continue; 1700 } 1701 1702 // Don't extract invalid files: 1703 if ( 0 !== validate_file( $info['name'] ) ) { 1704 continue; 1705 } 1706 1707 $uncompressed_size += $info['size']; 1708 1709 $dirname = dirname( $info['name'] ); 1710 1711 if ( str_ends_with( $info['name'], '/' ) ) { 1712 // Directory. 1713 $needed_dirs[] = $to . untrailingslashit( $info['name'] ); 1714 } elseif ( '.' !== $dirname ) { 1715 // Path to a file. 1716 $needed_dirs[] = $to . untrailingslashit( $dirname ); 1717 } 1718 } 1719 1720 // Enough space to unzip the file and copy its contents, with a 10% buffer. 1721 $required_space = $uncompressed_size * 2.1; 1722 1723 /* 1724 * disk_free_space() could return false. Assume that any falsey value is an error. 1725 * A disk that has zero free bytes has bigger problems. 1726 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer. 1727 */ 1728 if ( wp_doing_cron() ) { 1729 $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; 1730 1731 if ( $available_space && ( $required_space > $available_space ) ) { 1732 $z->close(); 1733 return new WP_Error( 1734 'disk_full_unzip_file', 1735 __( 'Could not copy files. You may have run out of disk space.' ), 1736 compact( 'uncompressed_size', 'available_space' ) 1737 ); 1738 } 1739 } 1740 1741 $needed_dirs = array_unique( $needed_dirs ); 1742 1743 foreach ( $needed_dirs as $dir ) { 1744 // Check the parent folders of the folders all exist within the creation array. 1745 if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist). 1746 continue; 1747 } 1748 1749 if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it. 1750 continue; 1751 } 1752 1753 $parent_folder = dirname( $dir ); 1754 1755 while ( ! empty( $parent_folder ) 1756 && untrailingslashit( $to ) !== $parent_folder 1757 && ! in_array( $parent_folder, $needed_dirs, true ) 1758 ) { 1759 $needed_dirs[] = $parent_folder; 1760 $parent_folder = dirname( $parent_folder ); 1761 } 1762 } 1763 1764 asort( $needed_dirs ); 1765 1766 // Create those directories if need be: 1767 foreach ( $needed_dirs as $_dir ) { 1768 // Only check to see if the Dir exists upon creation failure. Less I/O this way. 1769 if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) { 1770 $z->close(); 1771 return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), $_dir ); 1772 } 1773 } 1774 1775 /** 1776 * Filters archive unzipping to override with a custom process. 1777 * 1778 * @since 6.4.0 1779 * 1780 * @param null|true|WP_Error $result The result of the override. True on success, otherwise WP Error. Default null. 1781 * @param string $file Full path and filename of ZIP archive. 1782 * @param string $to Full path on the filesystem to extract archive to. 1783 * @param string[] $needed_dirs A full list of required folders that need to be created. 1784 * @param float $required_space The space required to unzip the file and copy its contents, with a 10% buffer. 1785 */ 1786 $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space ); 1787 1788 if ( null !== $pre ) { 1789 // Ensure the ZIP file archive has been closed. 1790 $z->close(); 1791 1792 return $pre; 1793 } 1794 1795 for ( $i = 0; $i < $z->numFiles; $i++ ) { 1796 $info = $z->statIndex( $i ); 1797 1798 if ( ! $info ) { 1799 $z->close(); 1800 return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); 1801 } 1802 1803 if ( str_ends_with( $info['name'], '/' ) ) { // Directory. 1804 continue; 1805 } 1806 1807 if ( str_starts_with( $info['name'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files. 1808 continue; 1809 } 1810 1811 // Don't extract invalid files: 1812 if ( 0 !== validate_file( $info['name'] ) ) { 1813 continue; 1814 } 1815 1816 $contents = $z->getFromIndex( $i ); 1817 1818 if ( false === $contents ) { 1819 $z->close(); 1820 return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] ); 1821 } 1822 1823 if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) { 1824 $z->close(); 1825 return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] ); 1826 } 1827 } 1828 1829 $z->close(); 1830 1831 /** 1832 * Filters the result of unzipping an archive. 1833 * 1834 * @since 6.4.0 1835 * 1836 * @param true|WP_Error $result The result of unzipping the archive. True on success, otherwise WP_Error. Default true. 1837 * @param string $file Full path and filename of ZIP archive. 1838 * @param string $to Full path on the filesystem the archive was extracted to. 1839 * @param string[] $needed_dirs A full list of required folders that were created. 1840 * @param float $required_space The space required to unzip the file and copy its contents, with a 10% buffer. 1841 */ 1842 $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space ); 1843 1844 unset( $needed_dirs ); 1845 1846 return $result; 1847 } 1848 1849 /** 1850 * Attempts to unzip an archive using the PclZip library. 1851 * 1852 * This function should not be called directly, use `unzip_file()` instead. 1853 * 1854 * Assumes that WP_Filesystem() has already been called and set up. 1855 * 1856 * @since 3.0.0 1857 * @access private 1858 * 1859 * @see unzip_file() 1860 * 1861 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 1862 * 1863 * @param string $file Full path and filename of ZIP archive. 1864 * @param string $to Full path on the filesystem to extract archive to. 1865 * @param string[] $needed_dirs A partial list of required folders needed to be created. 1866 * @return true|WP_Error True on success, WP_Error on failure. 1867 */ 1868 function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) { 1869 global $wp_filesystem; 1870 1871 mbstring_binary_safe_encoding(); 1872 1873 require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; 1874 1875 $archive = new PclZip( $file ); 1876 1877 $archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING ); 1878 1879 reset_mbstring_encoding(); 1880 1881 // Is the archive valid? 1882 if ( ! is_array( $archive_files ) ) { 1883 return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) ); 1884 } 1885 1886 if ( 0 === count( $archive_files ) ) { 1887 return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) ); 1888 } 1889 1890 $uncompressed_size = 0; 1891 1892 // Determine any children directories needed (From within the archive). 1893 foreach ( $archive_files as $archive_file ) { 1894 if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Skip the OS X-created __MACOSX directory. 1895 continue; 1896 } 1897 1898 $uncompressed_size += $archive_file['size']; 1899 1900 $needed_dirs[] = $to . untrailingslashit( $archive_file['folder'] ? $archive_file['filename'] : dirname( $archive_file['filename'] ) ); 1901 } 1902 1903 // Enough space to unzip the file and copy its contents, with a 10% buffer. 1904 $required_space = $uncompressed_size * 2.1; 1905 1906 /* 1907 * disk_free_space() could return false. Assume that any falsey value is an error. 1908 * A disk that has zero free bytes has bigger problems. 1909 * Require we have enough space to unzip the file and copy its contents, with a 10% buffer. 1910 */ 1911 if ( wp_doing_cron() ) { 1912 $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; 1913 1914 if ( $available_space && ( $required_space > $available_space ) ) { 1915 return new WP_Error( 1916 'disk_full_unzip_file', 1917 __( 'Could not copy files. You may have run out of disk space.' ), 1918 compact( 'uncompressed_size', 'available_space' ) 1919 ); 1920 } 1921 } 1922 1923 $needed_dirs = array_unique( $needed_dirs ); 1924 1925 foreach ( $needed_dirs as $dir ) { 1926 // Check the parent folders of the folders all exist within the creation array. 1927 if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist). 1928 continue; 1929 } 1930 1931 if ( ! str_contains( $dir, $to ) ) { // If the directory is not within the working directory, skip it. 1932 continue; 1933 } 1934 1935 $parent_folder = dirname( $dir ); 1936 1937 while ( ! empty( $parent_folder ) 1938 && untrailingslashit( $to ) !== $parent_folder 1939 && ! in_array( $parent_folder, $needed_dirs, true ) 1940 ) { 1941 $needed_dirs[] = $parent_folder; 1942 $parent_folder = dirname( $parent_folder ); 1943 } 1944 } 1945 1946 asort( $needed_dirs ); 1947 1948 // Create those directories if need be: 1949 foreach ( $needed_dirs as $_dir ) { 1950 // Only check to see if the dir exists upon creation failure. Less I/O this way. 1951 if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) { 1952 return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), $_dir ); 1953 } 1954 } 1955 1956 /** This filter is documented in src/wp-admin/includes/file.php */ 1957 $pre = apply_filters( 'pre_unzip_file', null, $file, $to, $needed_dirs, $required_space ); 1958 1959 if ( null !== $pre ) { 1960 return $pre; 1961 } 1962 1963 // Extract the files from the zip. 1964 foreach ( $archive_files as $archive_file ) { 1965 if ( $archive_file['folder'] ) { 1966 continue; 1967 } 1968 1969 if ( str_starts_with( $archive_file['filename'], '__MACOSX/' ) ) { // Don't extract the OS X-created __MACOSX directory files. 1970 continue; 1971 } 1972 1973 // Don't extract invalid files: 1974 if ( 0 !== validate_file( $archive_file['filename'] ) ) { 1975 continue; 1976 } 1977 1978 if ( ! $wp_filesystem->put_contents( $to . $archive_file['filename'], $archive_file['content'], FS_CHMOD_FILE ) ) { 1979 return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $archive_file['filename'] ); 1980 } 1981 } 1982 1983 /** This action is documented in src/wp-admin/includes/file.php */ 1984 $result = apply_filters( 'unzip_file', true, $file, $to, $needed_dirs, $required_space ); 1985 1986 unset( $needed_dirs ); 1987 1988 return $result; 1989 } 1990 1991 /** 1992 * Copies a directory from one location to another via the WordPress Filesystem 1993 * Abstraction. 1994 * 1995 * Assumes that WP_Filesystem() has already been called and setup. 1996 * 1997 * @since 2.5.0 1998 * 1999 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2000 * 2001 * @param string $from Source directory. 2002 * @param string $to Destination directory. 2003 * @param string[] $skip_list An array of files/folders to skip copying. 2004 * @return true|WP_Error True on success, WP_Error on failure. 2005 */ 2006 function copy_dir( $from, $to, $skip_list = array() ) { 2007 global $wp_filesystem; 2008 2009 $dirlist = $wp_filesystem->dirlist( $from ); 2010 2011 if ( false === $dirlist ) { 2012 return new WP_Error( 'dirlist_failed_copy_dir', __( 'Directory listing failed.' ), basename( $from ) ); 2013 } 2014 2015 $from = trailingslashit( $from ); 2016 $to = trailingslashit( $to ); 2017 2018 if ( ! $wp_filesystem->exists( $to ) && ! $wp_filesystem->mkdir( $to ) ) { 2019 return new WP_Error( 2020 'mkdir_destination_failed_copy_dir', 2021 __( 'Could not create the destination directory.' ), 2022 basename( $to ) 2023 ); 2024 } 2025 2026 foreach ( (array) $dirlist as $filename => $fileinfo ) { 2027 if ( in_array( $filename, $skip_list, true ) ) { 2028 continue; 2029 } 2030 2031 if ( 'f' === $fileinfo['type'] ) { 2032 if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) { 2033 // If copy failed, chmod file to 0644 and try again. 2034 $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE ); 2035 2036 if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) { 2037 return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename ); 2038 } 2039 } 2040 2041 wp_opcache_invalidate( $to . $filename ); 2042 } elseif ( 'd' === $fileinfo['type'] ) { 2043 if ( ! $wp_filesystem->is_dir( $to . $filename ) ) { 2044 if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) { 2045 return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename ); 2046 } 2047 } 2048 2049 // Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list. 2050 $sub_skip_list = array(); 2051 2052 foreach ( $skip_list as $skip_item ) { 2053 if ( str_starts_with( $skip_item, $filename . '/' ) ) { 2054 $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item ); 2055 } 2056 } 2057 2058 $result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list ); 2059 2060 if ( is_wp_error( $result ) ) { 2061 return $result; 2062 } 2063 } 2064 } 2065 2066 return true; 2067 } 2068 2069 /** 2070 * Moves a directory from one location to another. 2071 * 2072 * Recursively invalidates OPcache on success. 2073 * 2074 * If the renaming failed, falls back to copy_dir(). 2075 * 2076 * Assumes that WP_Filesystem() has already been called and setup. 2077 * 2078 * This function is not designed to merge directories, copy_dir() should be used instead. 2079 * 2080 * @since 6.2.0 2081 * 2082 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2083 * 2084 * @param string $from Source directory. 2085 * @param string $to Destination directory. 2086 * @param bool $overwrite Optional. Whether to overwrite the destination directory if it exists. 2087 * Default false. 2088 * @return true|WP_Error True on success, WP_Error on failure. 2089 */ 2090 function move_dir( $from, $to, $overwrite = false ) { 2091 global $wp_filesystem; 2092 2093 if ( trailingslashit( strtolower( $from ) ) === trailingslashit( strtolower( $to ) ) ) { 2094 return new WP_Error( 'source_destination_same_move_dir', __( 'The source and destination are the same.' ) ); 2095 } 2096 2097 if ( $wp_filesystem->exists( $to ) ) { 2098 if ( ! $overwrite ) { 2099 return new WP_Error( 'destination_already_exists_move_dir', __( 'The destination folder already exists.' ), $to ); 2100 } elseif ( ! $wp_filesystem->delete( $to, true ) ) { 2101 // Can't overwrite if the destination couldn't be deleted. 2102 return new WP_Error( 'destination_not_deleted_move_dir', __( 'The destination directory already exists and could not be removed.' ) ); 2103 } 2104 } 2105 2106 if ( $wp_filesystem->move( $from, $to ) ) { 2107 /* 2108 * When using an environment with shared folders, 2109 * there is a delay in updating the filesystem's cache. 2110 * 2111 * This is a known issue in environments with a VirtualBox provider. 2112 * 2113 * A 200ms delay gives time for the filesystem to update its cache, 2114 * prevents "Operation not permitted", and "No such file or directory" warnings. 2115 * 2116 * This delay is used in other projects, including Composer. 2117 * @link https://github.com/composer/composer/blob/2.5.1/src/Composer/Util/Platform.php#L228-L233 2118 */ 2119 usleep( 200000 ); 2120 wp_opcache_invalidate_directory( $to ); 2121 2122 return true; 2123 } 2124 2125 // Fall back to a recursive copy. 2126 if ( ! $wp_filesystem->is_dir( $to ) ) { 2127 if ( ! $wp_filesystem->mkdir( $to, FS_CHMOD_DIR ) ) { 2128 return new WP_Error( 'mkdir_failed_move_dir', __( 'Could not create directory.' ), $to ); 2129 } 2130 } 2131 2132 $result = copy_dir( $from, $to, array( basename( $to ) ) ); 2133 2134 // Clear the source directory. 2135 if ( true === $result ) { 2136 $wp_filesystem->delete( $from, true ); 2137 } 2138 2139 return $result; 2140 } 2141 2142 /** 2143 * Initializes and connects the WordPress Filesystem Abstraction classes. 2144 * 2145 * This function will include the chosen transport and attempt connecting. 2146 * 2147 * Plugins may add extra transports, And force WordPress to use them by returning 2148 * the filename via the {@see 'filesystem_method_file'} filter. 2149 * 2150 * @since 2.5.0 2151 * 2152 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2153 * 2154 * @param array|false $args Optional. Connection args, These are passed 2155 * directly to the `WP_Filesystem_*()` classes. 2156 * Default false. 2157 * @param string|false $context Optional. Context for get_filesystem_method(). 2158 * Default false. 2159 * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. 2160 * Default false. 2161 * @return bool|null True on success, false on failure, 2162 * null if the filesystem method class file does not exist. 2163 */ 2164 function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid 2165 global $wp_filesystem; 2166 2167 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; 2168 2169 $method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership ); 2170 2171 if ( ! $method ) { 2172 return false; 2173 } 2174 2175 if ( ! class_exists( "WP_Filesystem_$method" ) ) { 2176 2177 /** 2178 * Filters the path for a specific filesystem method class file. 2179 * 2180 * @since 2.6.0 2181 * 2182 * @see get_filesystem_method() 2183 * 2184 * @param string $path Path to the specific filesystem method class file. 2185 * @param string $method The filesystem method to use. 2186 */ 2187 $abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method ); 2188 2189 if ( ! file_exists( $abstraction_file ) ) { 2190 return; 2191 } 2192 2193 require_once $abstraction_file; 2194 } 2195 $method = "WP_Filesystem_$method"; 2196 2197 $wp_filesystem = new $method( $args ); 2198 2199 /* 2200 * Define the timeouts for the connections. Only available after the constructor is called 2201 * to allow for per-transport overriding of the default. 2202 */ 2203 if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) { 2204 define( 'FS_CONNECT_TIMEOUT', 30 ); // 30 seconds. 2205 } 2206 if ( ! defined( 'FS_TIMEOUT' ) ) { 2207 define( 'FS_TIMEOUT', 30 ); // 30 seconds. 2208 } 2209 2210 if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 2211 return false; 2212 } 2213 2214 if ( ! $wp_filesystem->connect() ) { 2215 return false; // There was an error connecting to the server. 2216 } 2217 2218 // Set the permission constants if not already set. 2219 if ( ! defined( 'FS_CHMOD_DIR' ) ) { 2220 define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) ); 2221 } 2222 if ( ! defined( 'FS_CHMOD_FILE' ) ) { 2223 define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) ); 2224 } 2225 2226 return true; 2227 } 2228 2229 /** 2230 * Determines which method to use for reading, writing, modifying, or deleting 2231 * files on the filesystem. 2232 * 2233 * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets 2234 * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2', 2235 * 'ftpext' or 'ftpsockets'. 2236 * 2237 * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`, 2238 * or filtering via {@see 'filesystem_method'}. 2239 * 2240 * @link https://developer.wordpress.org/advanced-administration/wordpress/wp-config/#wordpress-upgrade-constants 2241 * 2242 * Plugins may define a custom transport handler, See WP_Filesystem(). 2243 * 2244 * @since 2.5.0 2245 * 2246 * @global callable $_wp_filesystem_direct_method 2247 * 2248 * @param array $args Optional. Connection details. Default empty array. 2249 * @param string $context Optional. Full path to the directory that is tested 2250 * for being writable. Default empty. 2251 * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. 2252 * Default false. 2253 * @return string The transport to use, see description for valid return values. 2254 */ 2255 function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) { 2256 // Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'. 2257 $method = defined( 'FS_METHOD' ) ? FS_METHOD : false; 2258 2259 if ( ! $context ) { 2260 $context = WP_CONTENT_DIR; 2261 } 2262 2263 // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it. 2264 if ( WP_LANG_DIR === $context && ! is_dir( $context ) ) { 2265 $context = dirname( $context ); 2266 } 2267 2268 $context = trailingslashit( $context ); 2269 2270 if ( ! $method ) { 2271 2272 $temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) ); 2273 $temp_handle = @fopen( $temp_file_name, 'w' ); 2274 if ( $temp_handle ) { 2275 2276 // Attempt to determine the file owner of the WordPress files, and that of newly created files. 2277 $wp_file_owner = false; 2278 $temp_file_owner = false; 2279 if ( function_exists( 'fileowner' ) ) { 2280 $wp_file_owner = @fileowner( __FILE__ ); 2281 $temp_file_owner = @fileowner( $temp_file_name ); 2282 } 2283 2284 if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) { 2285 /* 2286 * WordPress is creating files as the same owner as the WordPress files, 2287 * this means it's safe to modify & create new files via PHP. 2288 */ 2289 $method = 'direct'; 2290 $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; 2291 } elseif ( $allow_relaxed_file_ownership ) { 2292 /* 2293 * The $context directory is writable, and $allow_relaxed_file_ownership is set, 2294 * this means we can modify files safely in this directory. 2295 * This mode doesn't create new files, only alter existing ones. 2296 */ 2297 $method = 'direct'; 2298 $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; 2299 } 2300 2301 fclose( $temp_handle ); 2302 @unlink( $temp_file_name ); 2303 } 2304 } 2305 2306 if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) { 2307 $method = 'ssh2'; 2308 } 2309 if ( ! $method && extension_loaded( 'ftp' ) ) { 2310 $method = 'ftpext'; 2311 } 2312 if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) { 2313 $method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread. 2314 } 2315 2316 /** 2317 * Filters the filesystem method to use. 2318 * 2319 * @since 2.6.0 2320 * 2321 * @param string $method Filesystem method to return. 2322 * @param array $args An array of connection details for the method. 2323 * @param string $context Full path to the directory that is tested for being writable. 2324 * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. 2325 */ 2326 return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership ); 2327 } 2328 2329 /** 2330 * Displays a form to the user to request for their FTP/SSH details in order 2331 * to connect to the filesystem. 2332 * 2333 * All chosen/entered details are saved, excluding the password. 2334 * 2335 * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) 2336 * to specify an alternate FTP/SSH port. 2337 * 2338 * Plugins may override this form by returning true|false via the {@see 'request_filesystem_credentials'} filter. 2339 * 2340 * @since 2.5.0 2341 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string. 2342 * 2343 * @global string $pagenow The filename of the current screen. 2344 * 2345 * @param string $form_post The URL to post the form to. 2346 * @param string $type Optional. Chosen type of filesystem. Default empty. 2347 * @param bool|WP_Error $error Optional. Whether the current request has failed 2348 * to connect, or an error object. Default false. 2349 * @param string $context Optional. Full path to the directory that is tested 2350 * for being writable. Default empty. 2351 * @param array $extra_fields Optional. Extra `POST` fields to be checked 2352 * for inclusion in the post. Default null. 2353 * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. 2354 * Default false. 2355 * @return bool|array True if no filesystem credentials are required, 2356 * false if they are required but have not been provided, 2357 * array of credentials if they are required and have been provided. 2358 */ 2359 function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) { 2360 global $pagenow; 2361 2362 /** 2363 * Filters the filesystem credentials. 2364 * 2365 * Returning anything other than an empty string will effectively short-circuit 2366 * output of the filesystem credentials form, returning that value instead. 2367 * 2368 * A filter should return true if no filesystem credentials are required, false if they are required but have not been 2369 * provided, or an array of credentials if they are required and have been provided. 2370 * 2371 * @since 2.5.0 2372 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string. 2373 * 2374 * @param mixed $credentials Credentials to return instead. Default empty string. 2375 * @param string $form_post The URL to post the form to. 2376 * @param string $type Chosen type of filesystem. 2377 * @param bool|WP_Error $error Whether the current request has failed to connect, 2378 * or an error object. 2379 * @param string $context Full path to the directory that is tested for 2380 * being writable. 2381 * @param array $extra_fields Extra POST fields. 2382 * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. 2383 */ 2384 $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ); 2385 2386 if ( '' !== $req_cred ) { 2387 return $req_cred; 2388 } 2389 2390 if ( empty( $type ) ) { 2391 $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); 2392 } 2393 2394 if ( 'direct' === $type ) { 2395 return true; 2396 } 2397 2398 if ( is_null( $extra_fields ) ) { 2399 $extra_fields = array( 'version', 'locale' ); 2400 } 2401 2402 $credentials = get_option( 2403 'ftp_credentials', 2404 array( 2405 'hostname' => '', 2406 'username' => '', 2407 ) 2408 ); 2409 2410 $submitted_form = wp_unslash( $_POST ); 2411 2412 // Verify nonce, or unset submitted form field values on failure. 2413 if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) { 2414 unset( 2415 $submitted_form['hostname'], 2416 $submitted_form['username'], 2417 $submitted_form['password'], 2418 $submitted_form['public_key'], 2419 $submitted_form['private_key'], 2420 $submitted_form['connection_type'] 2421 ); 2422 } 2423 2424 $ftp_constants = array( 2425 'hostname' => 'FTP_HOST', 2426 'username' => 'FTP_USER', 2427 'password' => 'FTP_PASS', 2428 'public_key' => 'FTP_PUBKEY', 2429 'private_key' => 'FTP_PRIKEY', 2430 ); 2431 2432 /* 2433 * If defined, set it to that. Else, if POST'd, set it to that. If not, set it to an empty string. 2434 * Otherwise, keep it as it previously was (saved details in option). 2435 */ 2436 foreach ( $ftp_constants as $key => $constant ) { 2437 if ( defined( $constant ) ) { 2438 $credentials[ $key ] = constant( $constant ); 2439 } elseif ( ! empty( $submitted_form[ $key ] ) ) { 2440 $credentials[ $key ] = $submitted_form[ $key ]; 2441 } elseif ( ! isset( $credentials[ $key ] ) ) { 2442 $credentials[ $key ] = ''; 2443 } 2444 } 2445 2446 // Sanitize the hostname, some people might pass in odd data. 2447 $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); // Strip any schemes off. 2448 2449 if ( strpos( $credentials['hostname'], ':' ) ) { 2450 list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 ); 2451 if ( ! is_numeric( $credentials['port'] ) ) { 2452 unset( $credentials['port'] ); 2453 } 2454 } else { 2455 unset( $credentials['port'] ); 2456 } 2457 2458 if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) { 2459 $credentials['connection_type'] = 'ssh'; 2460 } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL. 2461 $credentials['connection_type'] = 'ftps'; 2462 } elseif ( ! empty( $submitted_form['connection_type'] ) ) { 2463 $credentials['connection_type'] = $submitted_form['connection_type']; 2464 } elseif ( ! isset( $credentials['connection_type'] ) ) { // All else fails (and it's not defaulted to something else saved), default to FTP. 2465 $credentials['connection_type'] = 'ftp'; 2466 } 2467 2468 if ( ! $error 2469 && ( ! empty( $credentials['hostname'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['password'] ) 2470 || 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) 2471 ) 2472 ) { 2473 $stored_credentials = $credentials; 2474 2475 if ( ! empty( $stored_credentials['port'] ) ) { // Save port as part of hostname to simplify above code. 2476 $stored_credentials['hostname'] .= ':' . $stored_credentials['port']; 2477 } 2478 2479 unset( 2480 $stored_credentials['password'], 2481 $stored_credentials['port'], 2482 $stored_credentials['private_key'], 2483 $stored_credentials['public_key'] 2484 ); 2485 2486 if ( ! wp_installing() ) { 2487 update_option( 'ftp_credentials', $stored_credentials, false ); 2488 } 2489 2490 return $credentials; 2491 } 2492 2493 $hostname = $credentials['hostname'] ?? ''; 2494 $username = $credentials['username'] ?? ''; 2495 $public_key = $credentials['public_key'] ?? ''; 2496 $private_key = $credentials['private_key'] ?? ''; 2497 $port = $credentials['port'] ?? ''; 2498 $connection_type = $credentials['connection_type'] ?? ''; 2499 2500 if ( $error ) { 2501 $error_string = __( '<strong>Error:</strong> Could not connect to the server. Please verify the settings are correct.' ); 2502 if ( is_wp_error( $error ) ) { 2503 $error_string = esc_html( $error->get_error_message() ); 2504 } 2505 wp_admin_notice( 2506 $error_string, 2507 array( 2508 'id' => 'message', 2509 'additional_classes' => array( 'error' ), 2510 ) 2511 ); 2512 } 2513 2514 $types = array(); 2515 if ( extension_loaded( 'ftp' ) || extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) { 2516 $types['ftp'] = __( 'FTP' ); 2517 } 2518 if ( extension_loaded( 'ftp' ) ) { // Only this supports FTPS. 2519 $types['ftps'] = __( 'FTPS (SSL)' ); 2520 } 2521 if ( extension_loaded( 'ssh2' ) ) { 2522 $types['ssh'] = __( 'SSH2' ); 2523 } 2524 2525 /** 2526 * Filters the connection types to output to the filesystem credentials form. 2527 * 2528 * @since 2.9.0 2529 * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string. 2530 * 2531 * @param string[] $types Types of connections. 2532 * @param array $credentials Credentials to connect with. 2533 * @param string $type Chosen filesystem method. 2534 * @param bool|WP_Error $error Whether the current request has failed to connect, 2535 * or an error object. 2536 * @param string $context Full path to the directory that is tested for being writable. 2537 */ 2538 $types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context ); 2539 ?> 2540 <form action="<?php echo esc_url( $form_post ); ?>" method="post"> 2541 <div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form"> 2542 <?php 2543 // Print a H1 heading in the FTP credentials modal dialog, default is a H2. 2544 $heading_tag = 'h2'; 2545 if ( 'plugins.php' === $pagenow || 'plugin-install.php' === $pagenow ) { 2546 $heading_tag = 'h1'; 2547 } 2548 echo "<$heading_tag id='request-filesystem-credentials-title'>" . __( 'Connection Information' ) . "</$heading_tag>"; 2549 ?> 2550 <p id="request-filesystem-credentials-desc"> 2551 <?php 2552 $label_user = __( 'Username' ); 2553 $label_pass = __( 'Password' ); 2554 _e( 'To perform the requested action, WordPress needs to access your web server.' ); 2555 echo ' '; 2556 if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) { 2557 if ( isset( $types['ssh'] ) ) { 2558 _e( 'Please enter your FTP or SSH credentials to proceed.' ); 2559 $label_user = __( 'FTP/SSH Username' ); 2560 $label_pass = __( 'FTP/SSH Password' ); 2561 } else { 2562 _e( 'Please enter your FTP credentials to proceed.' ); 2563 $label_user = __( 'FTP Username' ); 2564 $label_pass = __( 'FTP Password' ); 2565 } 2566 echo ' '; 2567 } 2568 _e( 'If you do not remember your credentials, you should contact your web host.' ); 2569 2570 $hostname_value = esc_attr( $hostname ); 2571 if ( ! empty( $port ) ) { 2572 $hostname_value .= ":$port"; 2573 } 2574 2575 $password_value = ''; 2576 if ( defined( 'FTP_PASS' ) ) { 2577 $password_value = '*****'; 2578 } 2579 ?> 2580 </p> 2581 <label for="hostname"> 2582 <span class="field-title"><?php _e( 'Hostname' ); ?></span> 2583 <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' ) ); ?> /> 2584 </label> 2585 <div class="ftp-username"> 2586 <label for="username"> 2587 <span class="field-title"><?php echo $label_user; ?></span> 2588 <input name="username" type="text" id="username" value="<?php echo esc_attr( $username ); ?>"<?php disabled( defined( 'FTP_USER' ) ); ?> /> 2589 </label> 2590 </div> 2591 <div class="ftp-password"> 2592 <label for="password"> 2593 <span class="field-title"><?php echo $label_pass; ?></span> 2594 <input name="password" type="password" id="password" value="<?php echo $password_value; ?>"<?php disabled( defined( 'FTP_PASS' ) ); ?> spellcheck="false" /> 2595 <?php 2596 if ( ! defined( 'FTP_PASS' ) ) { 2597 _e( 'This password will not be stored on the server.' ); 2598 } 2599 ?> 2600 </label> 2601 </div> 2602 <fieldset> 2603 <legend><?php _e( 'Connection Type' ); ?></legend> 2604 <?php 2605 $disabled = disabled( ( defined( 'FTP_SSL' ) && FTP_SSL ) || ( defined( 'FTP_SSH' ) && FTP_SSH ), true, false ); 2606 foreach ( $types as $name => $text ) : 2607 ?> 2608 <label for="<?php echo esc_attr( $name ); ?>"> 2609 <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; ?> /> 2610 <?php echo $text; ?> 2611 </label> 2612 <?php 2613 endforeach; 2614 ?> 2615 </fieldset> 2616 <?php 2617 if ( isset( $types['ssh'] ) ) { 2618 $hidden_class = ''; 2619 if ( 'ssh' !== $connection_type ) { 2620 $hidden_class = ' class="hidden"'; 2621 } 2622 ?> 2623 <fieldset id="ssh-keys"<?php echo $hidden_class; ?>> 2624 <legend><?php _e( 'Authentication Keys' ); ?></legend> 2625 <label for="public_key"> 2626 <span class="field-title"><?php _e( 'Public Key:' ); ?></span> 2627 <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' ) ); ?> /> 2628 </label> 2629 <label for="private_key"> 2630 <span class="field-title"><?php _e( 'Private Key:' ); ?></span> 2631 <input name="private_key" type="text" id="private_key" value="<?php echo esc_attr( $private_key ); ?>"<?php disabled( defined( 'FTP_PRIKEY' ) ); ?> /> 2632 </label> 2633 <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> 2634 </fieldset> 2635 <?php 2636 } 2637 2638 foreach ( (array) $extra_fields as $field ) { 2639 if ( isset( $submitted_form[ $field ] ) ) { 2640 echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( $submitted_form[ $field ] ) . '" />'; 2641 } 2642 } 2643 2644 /* 2645 * Make sure the `submit_button()` function is available during the REST API call 2646 * from WP_Site_Health_Auto_Updates::test_check_wp_filesystem_method(). 2647 */ 2648 if ( ! function_exists( 'submit_button' ) ) { 2649 require_once ABSPATH . 'wp-admin/includes/template.php'; 2650 } 2651 ?> 2652 <p class="request-filesystem-credentials-action-buttons"> 2653 <?php wp_nonce_field( 'filesystem-credentials', '_fs_nonce', false, true ); ?> 2654 <button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button> 2655 <?php submit_button( __( 'Proceed' ), 'primary', 'upgrade', false ); ?> 2656 </p> 2657 </div> 2658 </form> 2659 <?php 2660 return false; 2661 } 2662 2663 /** 2664 * Prints the filesystem credentials modal when needed. 2665 * 2666 * @since 4.2.0 2667 */ 2668 function wp_print_request_filesystem_credentials_modal() { 2669 $filesystem_method = get_filesystem_method(); 2670 2671 ob_start(); 2672 $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); 2673 ob_end_clean(); 2674 2675 $request_filesystem_credentials = ( 'direct' !== $filesystem_method && ! $filesystem_credentials_are_stored ); 2676 if ( ! $request_filesystem_credentials ) { 2677 return; 2678 } 2679 ?> 2680 <div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog"> 2681 <div class="notification-dialog-background"></div> 2682 <div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0"> 2683 <div class="request-filesystem-credentials-dialog-content"> 2684 <?php request_filesystem_credentials( site_url() ); ?> 2685 </div> 2686 </div> 2687 </div> 2688 <?php 2689 } 2690 2691 /** 2692 * Attempts to clear the opcode cache for an individual PHP file. 2693 * 2694 * This function can be called safely without having to check the file extension 2695 * or availability of the OPcache extension. 2696 * 2697 * Whether or not invalidation is possible is cached to improve performance. 2698 * 2699 * @since 5.5.0 2700 * 2701 * @link https://www.php.net/manual/en/function.opcache-invalidate.php 2702 * 2703 * @param string $filepath Path to the file, including extension, for which the opcode cache is to be cleared. 2704 * @param bool $force Invalidate even if the modification time is not newer than the file in cache. 2705 * Default false. 2706 * @return bool True if opcache was invalidated for `$filepath`, or there was nothing to invalidate. 2707 * False if opcache invalidation is not available, or is disabled via filter. 2708 */ 2709 function wp_opcache_invalidate( $filepath, $force = false ) { 2710 static $can_invalidate = null; 2711 2712 /* 2713 * Check to see if WordPress is able to run `opcache_invalidate()` or not, and cache the value. 2714 * 2715 * First, check to see if the function is available to call, then if the host has restricted 2716 * the ability to run the function to avoid a PHP warning. 2717 * 2718 * `opcache.restrict_api` can specify the path for files allowed to call `opcache_invalidate()`. 2719 * 2720 * If the host has this set, check whether the path in `opcache.restrict_api` matches 2721 * the beginning of the path of the origin file. 2722 * 2723 * `$_SERVER['SCRIPT_FILENAME']` approximates the origin file's path, but `realpath()` 2724 * is necessary because `SCRIPT_FILENAME` can be a relative path when run from CLI. 2725 * 2726 * For more details, see: 2727 * - https://www.php.net/manual/en/opcache.configuration.php 2728 * - https://www.php.net/manual/en/reserved.variables.server.php 2729 * - https://core.trac.wordpress.org/ticket/36455 2730 */ 2731 if ( null === $can_invalidate 2732 && function_exists( 'opcache_invalidate' ) 2733 && ( ! ini_get( 'opcache.restrict_api' ) 2734 || stripos( realpath( $_SERVER['SCRIPT_FILENAME'] ), ini_get( 'opcache.restrict_api' ) ) === 0 ) 2735 ) { 2736 $can_invalidate = true; 2737 } 2738 2739 // If invalidation is not available, return early. 2740 if ( ! $can_invalidate ) { 2741 return false; 2742 } 2743 2744 // Verify that file to be invalidated has a PHP extension. 2745 if ( '.php' !== strtolower( substr( $filepath, -4 ) ) ) { 2746 return false; 2747 } 2748 2749 /** 2750 * Filters whether to invalidate a file from the opcode cache. 2751 * 2752 * @since 5.5.0 2753 * 2754 * @param bool $will_invalidate Whether WordPress will invalidate `$filepath`. Default true. 2755 * @param string $filepath The path to the PHP file to invalidate. 2756 */ 2757 if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) { 2758 return opcache_invalidate( $filepath, $force ); 2759 } 2760 2761 return false; 2762 } 2763 2764 /** 2765 * Attempts to clear the opcode cache for a directory of files. 2766 * 2767 * @since 6.2.0 2768 * 2769 * @see wp_opcache_invalidate() 2770 * @link https://www.php.net/manual/en/function.opcache-invalidate.php 2771 * 2772 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 2773 * 2774 * @param string $dir The path to the directory for which the opcode cache is to be cleared. 2775 */ 2776 function wp_opcache_invalidate_directory( $dir ) { 2777 global $wp_filesystem; 2778 2779 if ( ! is_string( $dir ) || '' === trim( $dir ) ) { 2780 if ( WP_DEBUG ) { 2781 $error_message = sprintf( 2782 /* translators: %s: The function name. */ 2783 __( '%s expects a non-empty string.' ), 2784 '<code>wp_opcache_invalidate_directory()</code>' 2785 ); 2786 wp_trigger_error( '', $error_message ); 2787 } 2788 return; 2789 } 2790 2791 $dirlist = $wp_filesystem->dirlist( $dir, false, true ); 2792 2793 if ( empty( $dirlist ) ) { 2794 return; 2795 } 2796 2797 /* 2798 * Recursively invalidate opcache of files in a directory. 2799 * 2800 * WP_Filesystem_*::dirlist() returns an array of file and directory information. 2801 * 2802 * This does not include a path to the file or directory. 2803 * To invalidate files within sub-directories, recursion is needed 2804 * to prepend an absolute path containing the sub-directory's name. 2805 * 2806 * @param array $dirlist Array of file/directory information from WP_Filesystem_Base::dirlist(), 2807 * with sub-directories represented as nested arrays. 2808 * @param string $path Absolute path to the directory. 2809 */ 2810 $invalidate_directory = static function ( $dirlist, $path ) use ( &$invalidate_directory ) { 2811 $path = trailingslashit( $path ); 2812 2813 foreach ( $dirlist as $name => $details ) { 2814 if ( 'f' === $details['type'] ) { 2815 wp_opcache_invalidate( $path . $name, true ); 2816 } elseif ( is_array( $details['files'] ) && ! empty( $details['files'] ) ) { 2817 $invalidate_directory( $details['files'], $path . $name ); 2818 } 2819 } 2820 }; 2821 2822 $invalidate_directory( $dirlist, $dir ); 2823 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Feb 11 08:20:09 2026 | Cross-referenced by PHPXref |