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