[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/includes/ -> file.php (source)

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


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7