PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/file.php

http://github.com/markjaquith/WordPress
PHP | 2271 lines | 1375 code | 256 blank | 640 comment | 299 complexity | 2eea221f1211be526fd2ce008b2f289c MD5 | raw file
Possible License(s): 0BSD

Large files files are truncated, but you can click here to view the full file

  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. /** The descriptions for theme files. */
  14. $wp_file_descriptions = array(
  15. 'functions.php' => __( 'Theme Functions' ),
  16. 'header.php' => __( 'Theme Header' ),
  17. 'footer.php' => __( 'Theme Footer' ),
  18. 'sidebar.php' => __( 'Sidebar' ),
  19. 'comments.php' => __( 'Comments' ),
  20. 'searchform.php' => __( 'Search Form' ),
  21. '404.php' => __( '404 Template' ),
  22. 'link.php' => __( 'Links Template' ),
  23. // Archives.
  24. 'index.php' => __( 'Main Index Template' ),
  25. 'archive.php' => __( 'Archives' ),
  26. 'author.php' => __( 'Author Template' ),
  27. 'taxonomy.php' => __( 'Taxonomy Template' ),
  28. 'category.php' => __( 'Category Template' ),
  29. 'tag.php' => __( 'Tag Template' ),
  30. 'home.php' => __( 'Posts Page' ),
  31. 'search.php' => __( 'Search Results' ),
  32. 'date.php' => __( 'Date Template' ),
  33. // Content.
  34. 'singular.php' => __( 'Singular Template' ),
  35. 'single.php' => __( 'Single Post' ),
  36. 'page.php' => __( 'Single Page' ),
  37. 'front-page.php' => __( 'Homepage' ),
  38. 'privacy-policy.php' => __( 'Privacy Policy Page' ),
  39. // Attachments.
  40. 'attachment.php' => __( 'Attachment Template' ),
  41. 'image.php' => __( 'Image Attachment Template' ),
  42. 'video.php' => __( 'Video Attachment Template' ),
  43. 'audio.php' => __( 'Audio Attachment Template' ),
  44. 'application.php' => __( 'Application Attachment Template' ),
  45. // Embeds.
  46. 'embed.php' => __( 'Embed Template' ),
  47. 'embed-404.php' => __( 'Embed 404 Template' ),
  48. 'embed-content.php' => __( 'Embed Content Template' ),
  49. 'header-embed.php' => __( 'Embed Header Template' ),
  50. 'footer-embed.php' => __( 'Embed Footer Template' ),
  51. // Stylesheets.
  52. 'style.css' => __( 'Stylesheet' ),
  53. 'editor-style.css' => __( 'Visual Editor Stylesheet' ),
  54. 'editor-style-rtl.css' => __( 'Visual Editor RTL Stylesheet' ),
  55. 'rtl.css' => __( 'RTL Stylesheet' ),
  56. // Other.
  57. 'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ),
  58. '.htaccess' => __( '.htaccess (for rewrite rules )' ),
  59. // Deprecated files.
  60. 'wp-layout.css' => __( 'Stylesheet' ),
  61. 'wp-comments.php' => __( 'Comments Template' ),
  62. 'wp-comments-popup.php' => __( 'Popup Comments Template' ),
  63. 'comments-popup.php' => __( 'Popup Comments' ),
  64. );
  65. /**
  66. * Get the description for standard WordPress theme files and other various standard
  67. * WordPress files
  68. *
  69. * @since 1.5.0
  70. *
  71. * @global array $wp_file_descriptions Theme file descriptions.
  72. * @global array $allowed_files List of allowed files.
  73. * @param string $file Filesystem path or filename
  74. * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist.
  75. * Appends 'Page Template' to basename of $file if the file is a page template
  76. */
  77. function get_file_description( $file ) {
  78. global $wp_file_descriptions, $allowed_files;
  79. $dirname = pathinfo( $file, PATHINFO_DIRNAME );
  80. $file_path = $allowed_files[ $file ];
  81. if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) {
  82. return $wp_file_descriptions[ basename( $file ) ];
  83. } elseif ( file_exists( $file_path ) && is_file( $file_path ) ) {
  84. $template_data = implode( '', file( $file_path ) );
  85. if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) ) {
  86. /* translators: %s: Template name. */
  87. return sprintf( __( '%s Page Template' ), _cleanup_header_comment( $name[1] ) );
  88. }
  89. }
  90. return trim( basename( $file ) );
  91. }
  92. /**
  93. * Get the absolute filesystem path to the root of the WordPress installation
  94. *
  95. * @since 1.5.0
  96. *
  97. * @return string Full filesystem path to the root of the WordPress installation
  98. */
  99. function get_home_path() {
  100. $home = set_url_scheme( get_option( 'home' ), 'http' );
  101. $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );
  102. if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
  103. $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
  104. $pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
  105. $home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
  106. $home_path = trailingslashit( $home_path );
  107. } else {
  108. $home_path = ABSPATH;
  109. }
  110. return str_replace( '\\', '/', $home_path );
  111. }
  112. /**
  113. * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
  114. * The depth of the recursiveness can be controlled by the $levels param.
  115. *
  116. * @since 2.6.0
  117. * @since 4.9.0 Added the `$exclusions` parameter.
  118. *
  119. * @param string $folder Optional. Full path to folder. Default empty.
  120. * @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
  121. * @param string[] $exclusions Optional. List of folders and files to skip.
  122. * @return bool|string[] False on failure, else array of files.
  123. */
  124. function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
  125. if ( empty( $folder ) ) {
  126. return false;
  127. }
  128. $folder = trailingslashit( $folder );
  129. if ( ! $levels ) {
  130. return false;
  131. }
  132. $files = array();
  133. $dir = @opendir( $folder );
  134. if ( $dir ) {
  135. while ( ( $file = readdir( $dir ) ) !== false ) {
  136. // Skip current and parent folder links.
  137. if ( in_array( $file, array( '.', '..' ), true ) ) {
  138. continue;
  139. }
  140. // Skip hidden and excluded files.
  141. if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
  142. continue;
  143. }
  144. if ( is_dir( $folder . $file ) ) {
  145. $files2 = list_files( $folder . $file, $levels - 1 );
  146. if ( $files2 ) {
  147. $files = array_merge( $files, $files2 );
  148. } else {
  149. $files[] = $folder . $file . '/';
  150. }
  151. } else {
  152. $files[] = $folder . $file;
  153. }
  154. }
  155. closedir( $dir );
  156. }
  157. return $files;
  158. }
  159. /**
  160. * Get list of file extensions that are editable in plugins.
  161. *
  162. * @since 4.9.0
  163. *
  164. * @param string $plugin Path to the plugin file relative to the plugins directory.
  165. * @return string[] Array of editable file extensions.
  166. */
  167. function wp_get_plugin_file_editable_extensions( $plugin ) {
  168. $editable_extensions = array(
  169. 'bash',
  170. 'conf',
  171. 'css',
  172. 'diff',
  173. 'htm',
  174. 'html',
  175. 'http',
  176. 'inc',
  177. 'include',
  178. 'js',
  179. 'json',
  180. 'jsx',
  181. 'less',
  182. 'md',
  183. 'patch',
  184. 'php',
  185. 'php3',
  186. 'php4',
  187. 'php5',
  188. 'php7',
  189. 'phps',
  190. 'phtml',
  191. 'sass',
  192. 'scss',
  193. 'sh',
  194. 'sql',
  195. 'svg',
  196. 'text',
  197. 'txt',
  198. 'xml',
  199. 'yaml',
  200. 'yml',
  201. );
  202. /**
  203. * Filters file type extensions editable in the plugin editor.
  204. *
  205. * @since 2.8.0
  206. * @since 4.9.0 Added the `$plugin` parameter.
  207. *
  208. * @param string[] $editable_extensions An array of editable plugin file extensions.
  209. * @param string $plugin Path to the plugin file relative to the plugins directory.
  210. */
  211. $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions, $plugin );
  212. return $editable_extensions;
  213. }
  214. /**
  215. * Get list of file extensions that are editable for a given theme.
  216. *
  217. * @param WP_Theme $theme Theme object.
  218. * @return string[] Array of editable file extensions.
  219. */
  220. function wp_get_theme_file_editable_extensions( $theme ) {
  221. $default_types = array(
  222. 'bash',
  223. 'conf',
  224. 'css',
  225. 'diff',
  226. 'htm',
  227. 'html',
  228. 'http',
  229. 'inc',
  230. 'include',
  231. 'js',
  232. 'json',
  233. 'jsx',
  234. 'less',
  235. 'md',
  236. 'patch',
  237. 'php',
  238. 'php3',
  239. 'php4',
  240. 'php5',
  241. 'php7',
  242. 'phps',
  243. 'phtml',
  244. 'sass',
  245. 'scss',
  246. 'sh',
  247. 'sql',
  248. 'svg',
  249. 'text',
  250. 'txt',
  251. 'xml',
  252. 'yaml',
  253. 'yml',
  254. );
  255. /**
  256. * Filters the list of file types allowed for editing in the Theme editor.
  257. *
  258. * @since 4.4.0
  259. *
  260. * @param string[] $default_types List of allowed file types.
  261. * @param WP_Theme $theme The current Theme object.
  262. */
  263. $file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
  264. // Ensure that default types are still there.
  265. return array_unique( array_merge( $file_types, $default_types ) );
  266. }
  267. /**
  268. * Print file editor templates (for plugins and themes).
  269. *
  270. * @since 4.9.0
  271. */
  272. function wp_print_file_editor_templates() {
  273. ?>
  274. <script type="text/html" id="tmpl-wp-file-editor-notice">
  275. <div class="notice inline notice-{{ data.type || 'info' }} {{ data.alt ? 'notice-alt' : '' }} {{ data.dismissible ? 'is-dismissible' : '' }} {{ data.classes || '' }}">
  276. <# if ( 'php_error' === data.code ) { #>
  277. <p>
  278. <?php
  279. printf(
  280. /* translators: 1: Line number, 2: File path. */
  281. __( '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.' ),
  282. '{{ data.line }}',
  283. '{{ data.file }}'
  284. );
  285. ?>
  286. </p>
  287. <pre>{{ data.message }}</pre>
  288. <# } else if ( 'file_not_writable' === data.code ) { #>
  289. <p>
  290. <?php
  291. printf(
  292. /* translators: %s: Documentation URL. */
  293. __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
  294. __( 'https://wordpress.org/support/article/changing-file-permissions/' )
  295. );
  296. ?>
  297. </p>
  298. <# } else { #>
  299. <p>{{ data.message || data.code }}</p>
  300. <# if ( 'lint_errors' === data.code ) { #>
  301. <p>
  302. <# var elementId = 'el-' + String( Math.random() ); #>
  303. <input id="{{ elementId }}" type="checkbox">
  304. <label for="{{ elementId }}"><?php _e( 'Update anyway, even though it might break your site?' ); ?></label>
  305. </p>
  306. <# } #>
  307. <# } #>
  308. <# if ( data.dismissible ) { #>
  309. <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e( 'Dismiss' ); ?></span></button>
  310. <# } #>
  311. </div>
  312. </script>
  313. <?php
  314. }
  315. /**
  316. * Attempt to edit a file for a theme or plugin.
  317. *
  318. * When editing a PHP file, loopback requests will be made to the admin and the homepage
  319. * to attempt to see if there is a fatal error introduced. If so, the PHP change will be
  320. * reverted.
  321. *
  322. * @since 4.9.0
  323. *
  324. * @param string[] $args {
  325. * Args. Note that all of the arg values are already unslashed. They are, however,
  326. * coming straight from `$_POST` and are not validated or sanitized in any way.
  327. *
  328. * @type string $file Relative path to file.
  329. * @type string $plugin Path to the plugin file relative to the plugins directory.
  330. * @type string $theme Theme being edited.
  331. * @type string $newcontent New content for the file.
  332. * @type string $nonce Nonce.
  333. * }
  334. * @return true|WP_Error True on success or `WP_Error` on failure.
  335. */
  336. function wp_edit_theme_plugin_file( $args ) {
  337. if ( empty( $args['file'] ) ) {
  338. return new WP_Error( 'missing_file' );
  339. }
  340. $file = $args['file'];
  341. if ( 0 !== validate_file( $file ) ) {
  342. return new WP_Error( 'bad_file' );
  343. }
  344. if ( ! isset( $args['newcontent'] ) ) {
  345. return new WP_Error( 'missing_content' );
  346. }
  347. $content = $args['newcontent'];
  348. if ( ! isset( $args['nonce'] ) ) {
  349. return new WP_Error( 'missing_nonce' );
  350. }
  351. $plugin = null;
  352. $theme = null;
  353. $real_file = null;
  354. if ( ! empty( $args['plugin'] ) ) {
  355. $plugin = $args['plugin'];
  356. if ( ! current_user_can( 'edit_plugins' ) ) {
  357. return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
  358. }
  359. if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
  360. return new WP_Error( 'nonce_failure' );
  361. }
  362. if ( ! array_key_exists( $plugin, get_plugins() ) ) {
  363. return new WP_Error( 'invalid_plugin' );
  364. }
  365. if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
  366. return new WP_Error( 'bad_plugin_file_path', __( 'Sorry, that file cannot be edited.' ) );
  367. }
  368. $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
  369. $real_file = WP_PLUGIN_DIR . '/' . $file;
  370. $is_active = in_array(
  371. $plugin,
  372. (array) get_option( 'active_plugins', array() ),
  373. true
  374. );
  375. } elseif ( ! empty( $args['theme'] ) ) {
  376. $stylesheet = $args['theme'];
  377. if ( 0 !== validate_file( $stylesheet ) ) {
  378. return new WP_Error( 'bad_theme_path' );
  379. }
  380. if ( ! current_user_can( 'edit_themes' ) ) {
  381. return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit templates for this site.' ) );
  382. }
  383. $theme = wp_get_theme( $stylesheet );
  384. if ( ! $theme->exists() ) {
  385. return new WP_Error( 'non_existent_theme', __( 'The requested theme does not exist.' ) );
  386. }
  387. if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $stylesheet . '_' . $file ) ) {
  388. return new WP_Error( 'nonce_failure' );
  389. }
  390. if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
  391. return new WP_Error(
  392. 'theme_no_stylesheet',
  393. __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message()
  394. );
  395. }
  396. $editable_extensions = wp_get_theme_file_editable_extensions( $theme );
  397. $allowed_files = array();
  398. foreach ( $editable_extensions as $type ) {
  399. switch ( $type ) {
  400. case 'php':
  401. $allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) );
  402. break;
  403. case 'css':
  404. $style_files = $theme->get_files( 'css', -1 );
  405. $allowed_files['style.css'] = $style_files['style.css'];
  406. $allowed_files = array_merge( $allowed_files, $style_files );
  407. break;
  408. default:
  409. $allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) );
  410. break;
  411. }
  412. }
  413. // Compare based on relative paths.
  414. if ( 0 !== validate_file( $file, array_keys( $allowed_files ) ) ) {
  415. return new WP_Error( 'disallowed_theme_file', __( 'Sorry, that file cannot be edited.' ) );
  416. }
  417. $real_file = $theme->get_stylesheet_directory() . '/' . $file;
  418. $is_active = ( get_stylesheet() === $stylesheet || get_template() === $stylesheet );
  419. } else {
  420. return new WP_Error( 'missing_theme_or_plugin' );
  421. }
  422. // Ensure file is real.
  423. if ( ! is_file( $real_file ) ) {
  424. return new WP_Error( 'file_does_not_exist', __( 'File does not exist! Please double check the name and try again.' ) );
  425. }
  426. // Ensure file extension is allowed.
  427. $extension = null;
  428. if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
  429. $extension = strtolower( $matches[1] );
  430. if ( ! in_array( $extension, $editable_extensions, true ) ) {
  431. return new WP_Error( 'illegal_file_type', __( 'Files of this type are not editable.' ) );
  432. }
  433. }
  434. $previous_content = file_get_contents( $real_file );
  435. if ( ! is_writeable( $real_file ) ) {
  436. return new WP_Error( 'file_not_writable' );
  437. }
  438. $f = fopen( $real_file, 'w+' );
  439. if ( false === $f ) {
  440. return new WP_Error( 'file_not_writable' );
  441. }
  442. $written = fwrite( $f, $content );
  443. fclose( $f );
  444. if ( false === $written ) {
  445. return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
  446. }
  447. if ( 'php' === $extension && function_exists( 'opcache_invalidate' ) ) {
  448. opcache_invalidate( $real_file, true );
  449. }
  450. if ( $is_active && 'php' === $extension ) {
  451. $scrape_key = md5( rand() );
  452. $transient = 'scrape_key_' . $scrape_key;
  453. $scrape_nonce = strval( rand() );
  454. // It shouldn't take more than 60 seconds to make the two loopback requests.
  455. set_transient( $transient, $scrape_nonce, 60 );
  456. $cookies = wp_unslash( $_COOKIE );
  457. $scrape_params = array(
  458. 'wp_scrape_key' => $scrape_key,
  459. 'wp_scrape_nonce' => $scrape_nonce,
  460. );
  461. $headers = array(
  462. 'Cache-Control' => 'no-cache',
  463. );
  464. /** This filter is documented in wp-includes/class-wp-http-streams.php */
  465. $sslverify = apply_filters( 'https_local_ssl_verify', false );
  466. // Include Basic auth in loopback requests.
  467. if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
  468. $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
  469. }
  470. // Make sure PHP process doesn't die before loopback requests complete.
  471. set_time_limit( 300 );
  472. // Time to wait for loopback requests to finish.
  473. $timeout = 100;
  474. $needle_start = "###### wp_scraping_result_start:$scrape_key ######";
  475. $needle_end = "###### wp_scraping_result_end:$scrape_key ######";
  476. // Attempt loopback request to editor to see if user just whitescreened themselves.
  477. if ( $plugin ) {
  478. $url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
  479. } elseif ( isset( $stylesheet ) ) {
  480. $url = add_query_arg(
  481. array(
  482. 'theme' => $stylesheet,
  483. 'file' => $file,
  484. ),
  485. admin_url( 'theme-editor.php' )
  486. );
  487. } else {
  488. $url = admin_url();
  489. }
  490. if ( PHP_SESSION_ACTIVE === session_status() ) {
  491. // Close any active session to prevent HTTP requests from timing out
  492. // when attempting to connect back to the site.
  493. session_write_close();
  494. }
  495. $url = add_query_arg( $scrape_params, $url );
  496. $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
  497. $body = wp_remote_retrieve_body( $r );
  498. $scrape_result_position = strpos( $body, $needle_start );
  499. $loopback_request_failure = array(
  500. 'code' => 'loopback_request_failed',
  501. '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.' ),
  502. );
  503. $json_parse_failure = array(
  504. 'code' => 'json_parse_error',
  505. );
  506. $result = null;
  507. if ( false === $scrape_result_position ) {
  508. $result = $loopback_request_failure;
  509. } else {
  510. $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
  511. $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
  512. $result = json_decode( trim( $error_output ), true );
  513. if ( empty( $result ) ) {
  514. $result = $json_parse_failure;
  515. }
  516. }
  517. // Try making request to homepage as well to see if visitors have been whitescreened.
  518. if ( true === $result ) {
  519. $url = home_url( '/' );
  520. $url = add_query_arg( $scrape_params, $url );
  521. $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout' ) );
  522. $body = wp_remote_retrieve_body( $r );
  523. $scrape_result_position = strpos( $body, $needle_start );
  524. if ( false === $scrape_result_position ) {
  525. $result = $loopback_request_failure;
  526. } else {
  527. $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
  528. $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
  529. $result = json_decode( trim( $error_output ), true );
  530. if ( empty( $result ) ) {
  531. $result = $json_parse_failure;
  532. }
  533. }
  534. }
  535. delete_transient( $transient );
  536. if ( true !== $result ) {
  537. // Roll-back file change.
  538. file_put_contents( $real_file, $previous_content );
  539. if ( function_exists( 'opcache_invalidate' ) ) {
  540. opcache_invalidate( $real_file, true );
  541. }
  542. if ( ! isset( $result['message'] ) ) {
  543. $message = __( 'Something went wrong.' );
  544. } else {
  545. $message = $result['message'];
  546. unset( $result['message'] );
  547. }
  548. return new WP_Error( 'php_error', $message, $result );
  549. }
  550. }
  551. if ( $theme instanceof WP_Theme ) {
  552. $theme->cache_delete();
  553. }
  554. return true;
  555. }
  556. /**
  557. * Returns a filename of a Temporary unique file.
  558. * Please note that the calling function must unlink() this itself.
  559. *
  560. * The filename is based off the passed parameter or defaults to the current unix timestamp,
  561. * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
  562. *
  563. * @since 2.6.0
  564. *
  565. * @param string $filename Optional. Filename to base the Unique file off. Default empty.
  566. * @param string $dir Optional. Directory to store the file in. Default empty.
  567. * @return string a writable filename
  568. */
  569. function wp_tempnam( $filename = '', $dir = '' ) {
  570. if ( empty( $dir ) ) {
  571. $dir = get_temp_dir();
  572. }
  573. if ( empty( $filename ) || '.' == $filename || '/' == $filename || '\\' == $filename ) {
  574. $filename = uniqid();
  575. }
  576. // Use the basename of the given file without the extension as the name for the temporary directory.
  577. $temp_filename = basename( $filename );
  578. $temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );
  579. // If the folder is falsey, use its parent directory name instead.
  580. if ( ! $temp_filename ) {
  581. return wp_tempnam( dirname( $filename ), $dir );
  582. }
  583. // Suffix some random data to avoid filename conflicts.
  584. $temp_filename .= '-' . wp_generate_password( 6, false );
  585. $temp_filename .= '.tmp';
  586. $temp_filename = $dir . wp_unique_filename( $dir, $temp_filename );
  587. $fp = @fopen( $temp_filename, 'x' );
  588. if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
  589. return wp_tempnam( $filename, $dir );
  590. }
  591. if ( $fp ) {
  592. fclose( $fp );
  593. }
  594. return $temp_filename;
  595. }
  596. /**
  597. * Makes sure that the file that was requested to be edited is allowed to be edited.
  598. *
  599. * Function will die if you are not allowed to edit the file.
  600. *
  601. * @since 1.5.0
  602. *
  603. * @param string $file File the user is attempting to edit.
  604. * @param string[] $allowed_files Optional. Array of allowed files to edit. `$file` must match an entry exactly.
  605. * @return string|void Returns the file name on success, dies on failure.
  606. */
  607. function validate_file_to_edit( $file, $allowed_files = array() ) {
  608. $code = validate_file( $file, $allowed_files );
  609. if ( ! $code ) {
  610. return $file;
  611. }
  612. switch ( $code ) {
  613. case 1:
  614. wp_die( __( 'Sorry, that file cannot be edited.' ) );
  615. // case 2 :
  616. // wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
  617. case 3:
  618. wp_die( __( 'Sorry, that file cannot be edited.' ) );
  619. }
  620. }
  621. /**
  622. * Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type,
  623. * and moving the file to the appropriate directory within the uploads directory.
  624. *
  625. * @access private
  626. * @since 4.0.0
  627. *
  628. * @see wp_handle_upload_error
  629. *
  630. * @param string[] $file Reference to a single element of `$_FILES`. Call the function once for each uploaded file.
  631. * @param string[]|false $overrides An associative array of names => values to override default variables. Default false.
  632. * @param string $time Time formatted in 'yyyy/mm'.
  633. * @param string $action Expected value for `$_POST['action']`.
  634. * @return string[] On success, returns an associative array of file attributes. On failure, returns
  635. * `$overrides['upload_error_handler']( &$file, $message )` or `array( 'error' => $message )`.
  636. */
  637. function _wp_handle_upload( &$file, $overrides, $time, $action ) {
  638. // The default error handler.
  639. if ( ! function_exists( 'wp_handle_upload_error' ) ) {
  640. function wp_handle_upload_error( &$file, $message ) {
  641. return array( 'error' => $message );
  642. }
  643. }
  644. /**
  645. * Filters the data for a file before it is uploaded to WordPress.
  646. *
  647. * The dynamic portion of the hook name, `$action`, refers to the post action.
  648. *
  649. * @since 2.9.0 as 'wp_handle_upload_prefilter'.
  650. * @since 4.0.0 Converted to a dynamic hook with `$action`.
  651. *
  652. * @param string[] $file An array of data for a single file.
  653. */
  654. $file = apply_filters( "{$action}_prefilter", $file );
  655. // You may define your own function and pass the name in $overrides['upload_error_handler'].
  656. $upload_error_handler = 'wp_handle_upload_error';
  657. if ( isset( $overrides['upload_error_handler'] ) ) {
  658. $upload_error_handler = $overrides['upload_error_handler'];
  659. }
  660. // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
  661. if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) {
  662. return call_user_func_array( $upload_error_handler, array( &$file, $file['error'] ) );
  663. }
  664. // Install user overrides. Did we mention that this voids your warranty?
  665. // You may define your own function and pass the name in $overrides['unique_filename_callback'].
  666. $unique_filename_callback = null;
  667. if ( isset( $overrides['unique_filename_callback'] ) ) {
  668. $unique_filename_callback = $overrides['unique_filename_callback'];
  669. }
  670. /*
  671. * This may not have originally been intended to be overridable,
  672. * but historically has been.
  673. */
  674. if ( isset( $overrides['upload_error_strings'] ) ) {
  675. $upload_error_strings = $overrides['upload_error_strings'];
  676. } else {
  677. // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
  678. $upload_error_strings = array(
  679. false,
  680. sprintf(
  681. /* translators: 1: upload_max_filesize, 2: php.ini */
  682. __( 'The uploaded file exceeds the %1$s directive in %2$s.' ),
  683. 'upload_max_filesize',
  684. 'php.ini'
  685. ),
  686. sprintf(
  687. /* translators: %s: MAX_FILE_SIZE */
  688. __( 'The uploaded file exceeds the %s directive that was specified in the HTML form.' ),
  689. 'MAX_FILE_SIZE'
  690. ),
  691. __( 'The uploaded file was only partially uploaded.' ),
  692. __( 'No file was uploaded.' ),
  693. '',
  694. __( 'Missing a temporary folder.' ),
  695. __( 'Failed to write file to disk.' ),
  696. __( 'File upload stopped by extension.' ),
  697. );
  698. }
  699. // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
  700. $test_form = isset( $overrides['test_form'] ) ? $overrides['test_form'] : true;
  701. $test_size = isset( $overrides['test_size'] ) ? $overrides['test_size'] : true;
  702. // If you override this, you must provide $ext and $type!!
  703. $test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true;
  704. $mimes = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false;
  705. // A correct form post will pass this test.
  706. if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) {
  707. return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) );
  708. }
  709. // A successful upload will pass this test. It makes no sense to override this one.
  710. if ( isset( $file['error'] ) && $file['error'] > 0 ) {
  711. return call_user_func_array( $upload_error_handler, array( &$file, $upload_error_strings[ $file['error'] ] ) );
  712. }
  713. // A properly uploaded file will pass this test. There should be no reason to override this one.
  714. $test_uploaded_file = 'wp_handle_upload' === $action ? is_uploaded_file( $file['tmp_name'] ) : @is_readable( $file['tmp_name'] );
  715. if ( ! $test_uploaded_file ) {
  716. return call_user_func_array( $upload_error_handler, array( &$file, __( 'Specified file failed upload test.' ) ) );
  717. }
  718. $test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] );
  719. // A non-empty file will pass this test.
  720. if ( $test_size && ! ( $test_file_size > 0 ) ) {
  721. if ( is_multisite() ) {
  722. $error_msg = __( 'File is empty. Please upload something more substantial.' );
  723. } else {
  724. $error_msg = sprintf(
  725. /* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
  726. __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ),
  727. 'php.ini',
  728. 'post_max_size',
  729. 'upload_max_filesize'
  730. );
  731. }
  732. return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) );
  733. }
  734. // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
  735. if ( $test_type ) {
  736. $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
  737. $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
  738. $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
  739. $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
  740. // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect.
  741. if ( $proper_filename ) {
  742. $file['name'] = $proper_filename;
  743. }
  744. if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
  745. return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, this file type is not permitted for security reasons.' ) ) );
  746. }
  747. if ( ! $type ) {
  748. $type = $file['type'];
  749. }
  750. } else {
  751. $type = '';
  752. }
  753. /*
  754. * A writable uploads dir will pass this test. Again, there's no point
  755. * overriding this one.
  756. */
  757. $uploads = wp_upload_dir( $time );
  758. if ( ! ( $uploads && false === $uploads['error'] ) ) {
  759. return call_user_func_array( $upload_error_handler, array( &$file, $uploads['error'] ) );
  760. }
  761. $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
  762. // Move the file to the uploads dir.
  763. $new_file = $uploads['path'] . "/$filename";
  764. /**
  765. * Filters whether to short-circuit moving the uploaded file after passing all checks.
  766. *
  767. * If a non-null value is passed to the filter, moving the file and any related error
  768. * reporting will be completely skipped.
  769. *
  770. * @since 4.9.0
  771. *
  772. * @param mixed $move_new_file If null (default) move the file after the upload.
  773. * @param string[] $file An array of data for a single file.
  774. * @param string $new_file Filename of the newly-uploaded file.
  775. * @param string $type File type.
  776. */
  777. $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type );
  778. if ( null === $move_new_file ) {
  779. if ( 'wp_handle_upload' === $action ) {
  780. $move_new_file = @move_uploaded_file( $file['tmp_name'], $new_file );
  781. } else {
  782. // Use copy and unlink because rename breaks streams.
  783. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  784. $move_new_file = @copy( $file['tmp_name'], $new_file );
  785. unlink( $file['tmp_name'] );
  786. }
  787. if ( false === $move_new_file ) {
  788. if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
  789. $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
  790. } else {
  791. $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
  792. }
  793. return $upload_error_handler(
  794. $file,
  795. sprintf(
  796. /* translators: %s: Destination file path. */
  797. __( 'The uploaded file could not be moved to %s.' ),
  798. $error_path
  799. )
  800. );
  801. }
  802. }
  803. // Set correct file permissions.
  804. $stat = stat( dirname( $new_file ) );
  805. $perms = $stat['mode'] & 0000666;
  806. chmod( $new_file, $perms );
  807. // Compute the URL.
  808. $url = $uploads['url'] . "/$filename";
  809. if ( is_multisite() ) {
  810. delete_transient( 'dirsize_cache' );
  811. }
  812. /**
  813. * Filters the data array for the uploaded file.
  814. *
  815. * @since 2.1.0
  816. *
  817. * @param array $upload {
  818. * Array of upload data.
  819. *
  820. * @type string $file Filename of the newly-uploaded file.
  821. * @type string $url URL of the uploaded file.
  822. * @type string $type File type.
  823. * }
  824. * @param string $context The type of upload action. Values include 'upload' or 'sideload'.
  825. */
  826. return apply_filters(
  827. 'wp_handle_upload',
  828. array(
  829. 'file' => $new_file,
  830. 'url' => $url,
  831. 'type' => $type,
  832. ),
  833. 'wp_handle_sideload' === $action ? 'sideload' : 'upload'
  834. );
  835. }
  836. /**
  837. * Wrapper for _wp_handle_upload().
  838. *
  839. * Passes the {@see 'wp_handle_upload'} action.
  840. *
  841. * @since 2.0.0
  842. *
  843. * @see _wp_handle_upload()
  844. *
  845. * @param array $file Reference to a single element of `$_FILES`. Call the function once for
  846. * each uploaded file.
  847. * @param array|bool $overrides Optional. An associative array of names=>values to override default
  848. * variables. Default false.
  849. * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
  850. * @return array On success, returns an associative array of file attributes. On failure, returns
  851. * $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
  852. */
  853. function wp_handle_upload( &$file, $overrides = false, $time = null ) {
  854. /*
  855. * $_POST['action'] must be set and its value must equal $overrides['action']
  856. * or this:
  857. */
  858. $action = 'wp_handle_upload';
  859. if ( isset( $overrides['action'] ) ) {
  860. $action = $overrides['action'];
  861. }
  862. return _wp_handle_upload( $file, $overrides, $time, $action );
  863. }
  864. /**
  865. * Wrapper for _wp_handle_upload().
  866. *
  867. * Passes the {@see 'wp_handle_sideload'} action.
  868. *
  869. * @since 2.6.0
  870. *
  871. * @see _wp_handle_upload()
  872. *
  873. * @param array $file An array similar to that of a PHP `$_FILES` POST array
  874. * @param array|bool $overrides Optional. An associative array of names=>values to override default
  875. * variables. Default false.
  876. * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
  877. * @return array On success, returns an associative array of file attributes. On failure, returns
  878. * $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
  879. */
  880. function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
  881. /*
  882. * $_POST['action'] must be set and its value must equal $overrides['action']
  883. * or this:
  884. */
  885. $action = 'wp_handle_sideload';
  886. if ( isset( $overrides['action'] ) ) {
  887. $action = $overrides['action'];
  888. }
  889. return _wp_handle_upload( $file, $overrides, $time, $action );
  890. }
  891. /**
  892. * Downloads a URL to a local temporary file using the WordPress HTTP API.
  893. *
  894. * Please note that the calling function must unlink() the file.
  895. *
  896. * @since 2.5.0
  897. * @since 5.2.0 Signature Verification with SoftFail was added.
  898. *
  899. * @param string $url The URL of the file to download.
  900. * @param int $timeout The timeout for the request to download the file. Default 300 seconds.
  901. * @param bool $signature_verification Whether to perform Signature Verification. Default false.
  902. * @return string|WP_Error Filename on success, WP_Error on failure.
  903. */
  904. function download_url( $url, $timeout = 300, $signature_verification = false ) {
  905. // WARNING: The file is not automatically deleted, the script must unlink() the file.
  906. if ( ! $url ) {
  907. return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) );
  908. }
  909. $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
  910. $tmpfname = wp_tempnam( $url_filename );
  911. if ( ! $tmpfname ) {
  912. return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) );
  913. }
  914. $response = wp_safe_remote_get(
  915. $url,
  916. array(
  917. 'timeout' => $timeout,
  918. 'stream' => true,
  919. 'filename' => $tmpfname,
  920. )
  921. );
  922. if ( is_wp_error( $response ) ) {
  923. unlink( $tmpfname );
  924. return $response;
  925. }
  926. $response_code = wp_remote_retrieve_response_code( $response );
  927. if ( 200 != $response_code ) {
  928. $data = array(
  929. 'code' => $response_code,
  930. );
  931. // Retrieve a sample of the response body for debugging purposes.
  932. $tmpf = fopen( $tmpfname, 'rb' );
  933. if ( $tmpf ) {
  934. /**
  935. * Filters the maximum error response body size in `download_url()`.
  936. *
  937. * @since 5.1.0
  938. *
  939. * @see download_url()
  940. *
  941. * @param int $size The maximum error response body size. Default 1 KB.
  942. */
  943. $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
  944. $data['body'] = fread( $tmpf, $response_size );
  945. fclose( $tmpf );
  946. }
  947. unlink( $tmpfname );
  948. return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
  949. }
  950. $content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
  951. if ( $content_md5 ) {
  952. $md5_check = verify_file_md5( $tmpfname, $content_md5 );
  953. if ( is_wp_error( $md5_check ) ) {
  954. unlink( $tmpfname );
  955. return $md5_check;
  956. }
  957. }
  958. // If the caller expects signature verification to occur, check to see if this URL supports it.
  959. if ( $signature_verification ) {
  960. /**
  961. * Filters the list of hosts which should have Signature Verification attempted on.
  962. *
  963. * @since 5.2.0
  964. *
  965. * @param string[] $hostnames List of hostnames.
  966. */
  967. $signed_hostnames = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) );
  968. $signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true );
  969. }
  970. // Perform signature valiation if supported.
  971. if ( $signature_verification ) {
  972. $signature = wp_remote_retrieve_header( $response, 'x-content-signature' );
  973. if ( ! $signature ) {
  974. // Retrieve signatures from a file if the header wasn't included.
  975. // WordPress.org stores signatures at $package_url.sig.
  976. $signature_url = false;
  977. $url_path = parse_url( $url, PHP_URL_PATH );
  978. if ( substr( $url_path, -4 ) == '.zip' || substr( $url_path, -7 ) == '.tar.gz' ) {
  979. $signature_url = str_replace( $url_path, $url_path . '.sig', $url );
  980. }
  981. /**
  982. * Filter the URL where the signature for a file is located.
  983. *
  984. * @since 5.2.0
  985. *
  986. * @param false|string $signature_url The URL where signatures can be found for a file, or false if none are known.
  987. * @param string $url The URL being verified.
  988. */
  989. $signature_url = apply_filters( 'wp_signature_url', $signature_url, $url );
  990. if ( $signature_url ) {
  991. $signature_request = wp_safe_remote_get(
  992. $signature_url,
  993. array(
  994. 'limit_response_size' => 10 * KB_IN_BYTES, // 10KB should be large enough for quite a few signatures.
  995. )
  996. );
  997. if ( ! is_wp_error( $signature_request ) && 200 === wp_remote_retrieve_response_code( $signature_request ) ) {
  998. $signature = explode( "\n", wp_remote_retrieve_body( $signature_request ) );
  999. }
  1000. }
  1001. }
  1002. // Perform the checks.
  1003. $signature_verification = verify_file_signature( $tmpfname, $signature, basename( parse_url( $url, PHP_URL_PATH ) ) );
  1004. }
  1005. if ( is_wp_error( $signature_verification ) ) {
  1006. if (
  1007. /**
  1008. * Filters whether Signature Verification failures should be allowed to soft fail.
  1009. *
  1010. * WARNING: This may be removed from a future release.
  1011. *
  1012. * @since 5.2.0
  1013. *
  1014. * @param bool $signature_softfail If a softfail is allowed.
  1015. * @param string $url The url being accessed.
  1016. */
  1017. apply_filters( 'wp_signature_softfail', true, $url )
  1018. ) {
  1019. $signature_verification->add_data( $tmpfname, 'softfail-filename' );
  1020. } else {
  1021. // Hard-fail.
  1022. unlink( $tmpfname );
  1023. }
  1024. return $signature_verification;
  1025. }
  1026. return $tmpfname;
  1027. }
  1028. /**
  1029. * Calculates and compares the MD5 of a file to its expected value.
  1030. *
  1031. * @since 3.7.0
  1032. *
  1033. * @param string $filename The filename to check the MD5 of.
  1034. * @param string $expected_md5 The expected MD5 of the file, either a base64-encoded raw md5,
  1035. * or a hex-encoded md5.
  1036. * @return bool|WP_Error True on success, false when the MD5 format is unknown/unexpected,
  1037. * WP_Error on failure.
  1038. */
  1039. function verify_file_md5( $filename, $expected_md5 ) {
  1040. if ( 32 == strlen( $expected_md5 ) ) {
  1041. $expected_raw_md5 = pack( 'H*', $expected_md5 );
  1042. } elseif ( 24 == strlen( $expected_md5 ) ) {
  1043. $expected_raw_md5 = base64_decode( $expected_md5 );
  1044. } else {
  1045. return false; // Unknown format.
  1046. }
  1047. $file_md5 = md5_file( $filename, true );
  1048. if ( $file_md5 === $expected_raw_md5 ) {
  1049. return true;
  1050. }
  1051. return new WP_Error(
  1052. 'md5_mismatch',
  1053. sprintf(
  1054. /* translators: 1: File checksum, 2: Expected checksum value. */
  1055. __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ),
  1056. bin2hex( $file_md5 ),
  1057. bin2hex( $expected_raw_md5 )
  1058. )
  1059. );
  1060. }
  1061. /**
  1062. * Verifies the contents of a file against its ED25519 signature.
  1063. *
  1064. * @since 5.2.0
  1065. *
  1066. * @param string $filename The file to validate.
  1067. * @param string|array $signatures A Signature provided for the file.
  1068. * @param string $filename_for_errors A friendly filename for errors. Optional.
  1069. * @return bool|WP_Error True on success, false if verification not attempted,
  1070. * or WP_Error describing an error condition.
  1071. */
  1072. function verify_file_signature( $filename, $signatures, $filename_for_errors = false ) {
  1073. if ( ! $filename_for_errors ) {
  1074. $filename_for_errors = wp_basename( $filename );
  1075. }
  1076. // Check we can process signatures.
  1077. if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) || ! in_array( 'sha384', array_map( 'strtolower', hash_algos() ), true ) ) {
  1078. return new WP_Error(
  1079. 'signature_verification_unsupported',
  1080. sprintf(
  1081. /* translators: %s: The filename of the package. */
  1082. __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
  1083. '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
  1084. ),
  1085. ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' )
  1086. );
  1087. }
  1088. // Check for a edge-case affecting PHP Maths abilities.
  1089. if (
  1090. ! extension_loaded( 'sodium' ) &&
  1091. in_array( PHP_VERSION_ID, array( 70200, 70201, 70202 ), true ) &&
  1092. extension_loaded( 'opcache' )
  1093. ) {
  1094. // 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.
  1095. // https://bugs.php.net/bug.php?id=75938
  1096. return new WP_Error(
  1097. 'signature_verification_unsupported',
  1098. sprintf(
  1099. /* translators: %s: The filename of the package. */
  1100. __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
  1101. '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
  1102. ),
  1103. array(
  1104. 'php' => phpversion(),
  1105. // phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
  1106. 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
  1107. )
  1108. );
  1109. }
  1110. // Verify runtime speed of Sodium_Compat is acceptable.
  1111. if ( ! extension_loaded( 'sodium' ) && ! ParagonIE_Sodium_Compat::polyfill_is_fast() ) {
  1112. $sodium_compat_is_fast = false;
  1113. // Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one.
  1114. if ( method_exists( 'ParagonIE_Sodium_Compat', 'runtime_speed_test' ) ) {
  1115. // Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode, as that's what WordPress utilises during signing verifications.
  1116. // phpcs:disable WordPress.NamingConventions.ValidVariableName
  1117. $old_fastMult = ParagonIE_Sodium_Compat::$fastMult;
  1118. ParagonIE_Sodium_Compat::$fastMult = true;
  1119. $sodium_compat_is_fast = ParagonIE_Sodium_Compat::runtime_speed_test( 100, 10 );
  1120. ParagonIE_Sodium_Compat::$fastMult = $old_fastMult;
  1121. // phpcs:enable
  1122. }
  1123. // This cannot be performed in a reasonable amount of time.
  1124. // https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast
  1125. if ( ! $sodium_compat_is_fast ) {
  1126. return new WP_Error(
  1127. 'signature_verification_unsupported',
  1128. sprintf(
  1129. /* translators: %s: The filename of the package. */
  1130. __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
  1131. '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
  1132. ),
  1133. array(
  1134. 'php' => phpversion(),
  1135. // phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
  1136. 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
  1137. 'polyfill_is_fast' => false,
  1138. 'max_execution_time' => ini_get( 'max_execution_time' ),
  1139. )
  1140. );
  1141. }
  1142. }
  1143. if ( ! $signatures ) {
  1144. return new WP_Error(
  1145. 'signature_verification_no_signature',
  1146. sprintf(
  1147. /* translators: %s: The filename of the package. */
  1148. __( 'The authenticity of %s could not be verified as no signature was found.' ),
  1149. '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
  1150. ),
  1151. array(
  1152. 'filename' => $filename_for_errors,
  1153. )
  1154. );
  1155. }
  1156. $trusted_keys = wp_trusted_keys();
  1157. $file_hash = hash_file( 'sha384', $filename, true );
  1158. mbstring_binary_safe_encoding();
  1159. $skipped_key = 0;
  1160. $skipped_signature = 0;
  1161. foreach ( (array) $signatures as $signature ) {
  1162. $signature_raw = base64_decode( $signature );
  1163. // Ensure only valid-length signatures are considered.
  1164. if ( SODIUM_CRYPTO_SIGN_BYTES !== strlen( $signature_raw ) ) {
  1165. $skipped_signature++;
  1166. continue;
  1167. }
  1168. foreach ( (array) $trusted_keys as $key ) {
  1169. $key_raw = base64_decode( $key );
  1170. // Only pass valid public keys through.
  1171. if ( SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES !== strlen( $key_raw ) ) {
  1172. $skipped_key++;
  1173. continue;
  1174. }
  1175. if ( sodium_crypto_sign_verify_detached( $signature_raw, $file_hash, $key_raw ) ) {
  1176. reset_mbstring_encoding();
  1177. return true;
  1178. }
  1179. }
  1180. }
  1181. reset_mbstring_encoding();
  1182. return new WP_Error(
  1183. 'signature_verification_failed',
  1184. sprintf(
  1185. /* translators: %s: The filename of the package. */
  1186. __( 'The authenticity of %s could not be verified.' ),
  1187. '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
  1188. ),
  1189. // Error data helpful for debugging:
  1190. array(
  1191. 'filename' => $filename_for_errors,
  1192. 'keys' => $trusted_keys,
  1193. 'signatures' => $signatures,
  1194. 'hash' => bin2hex( $file_hash ),
  1195. 'skipped_key' => $skipped_key,
  1196. 'skipped_sig' => $skipped_signature,
  1197. 'php' => phpversion(),
  1198. // phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
  1199. 'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
  1200. )
  1201. );
  1202. }
  1203. /**
  1204. * Retrieve the list of signing keys trusted by WordPress.
  1205. *
  1206. * @since 5.2.0
  1207. *
  1208. * @return string[] Array of base64-encoded signing keys.
  1209. */
  1210. function wp_trusted_keys() {
  1211. $trusted_keys = array();
  1212. if ( time() < 1617235200 ) {
  1213. // WordPress.org Key #1 - This key is only valid before April 1st, 2021.
  1214. $trusted_keys[] = 'fRPyrxb/MvVLbdsYi+OOEv4xc+Eqpsj+kkAS6gNOkI0=';
  1215. }
  1216. // TODO: Add key #2 with longer expiration.
  1217. /**
  1218. * Filter the valid signing keys used to verify the contents of files.
  1219. *
  1220. * @since 5.2.0
  1221. *
  1222. * @param string[] $trusted_keys The trusted keys that may sign packages.
  1223. */
  1224. return apply_filters( 'wp_trusted_keys', $trusted_keys );
  1225. }
  1226. /**
  1227. * Unzips a specified ZIP file to a location on the filesystem via the WordPress
  1228. * Filesystem Abstraction.
  1229. *
  1230. * Assumes that WP_Filesystem() has already been called and set up. Does not extract
  1231. * a root-level __MACOSX directory, if present.
  1232. *
  1233. * Attempts to increase the PHP memory limit to 256M before uncompressing. However,
  1234. * the most memory required shouldn't be much larger than the archive itself.
  1235. *
  1236. * @since 2.5.0
  1237. *
  1238. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  1239. *
  1240. * @param string $file Full path and filename of ZIP archive.
  1241. * @param string $to Full path on the filesystem to extract archive to.
  1242. * @return true|WP_Error True on success, WP_Error on failure.
  1243. */
  1244. function unzip_file( $file, $to ) {
  1245. global $wp_filesystem;
  1246. if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
  1247. return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
  1248. }
  1249. // Unzip can use a lot of memory, but not this much hopefully.
  1250. wp_raise_memory_limit( 'admin' );
  1251. $needed_dirs = array();
  1252. $to = trailingslashit( $to );
  1253. // Determine any parent directories needed (of the upgrade directory).
  1254. if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parents if no children exist.
  1255. $path = preg_split( '![/\\\]!', untrailingslashit( $to ) );
  1256. for ( $i = count( $path ); $i >= 0; $i-- ) {
  1257. if ( empty( $path[ $i ] ) ) {
  1258. continue;
  1259. }
  1260. $dir = implode( '/', array_slice( $path, 0, $i + 1 ) );
  1261. if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Skip it if it looks like a Windows Drive letter.
  1262. continue;
  1263. }
  1264. if ( ! $wp_filesystem->is_dir( $dir ) ) {
  1265. $needed_dirs[] = $dir;
  1266. } else {
  1267. break; // A folder exists, therefore we don't need to check the levels below this.
  1268. }
  1269. }
  1270. }
  1271. /**
  1272. * Filters whether to use ZipArchive to unzip archives.
  1273. *
  1274. * @since 3.0.0
  1275. *
  1276. * @param bool $ziparchive Whether to use ZipArchive. Default true.
  1277. */
  1278. if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
  1279. $result = _unzip_file_ziparchive( $file, $to, $needed_dirs );
  1280. if ( true === $result ) {
  1281. return $result;
  1282. } elseif ( is_wp_error( $result ) ) {
  1283. if ( 'incompatible_archive' != $result->get_error_code() ) {
  1284. return $result;
  1285. }
  1286. }
  1287. }
  1288. // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
  1289. return _unzip_file_pclzip( $file, $to, $needed_dirs );
  1290. }
  1291. /**
  1292. * Attempts to unzip an archive using the ZipArchive class.
  1293. *
  1294. * This function should not be called directly, use `unzip_file()` instead.
  1295. *
  1296. * Assumes that WP_Filesystem() has already been called and set up.
  1297. *
  1298. * @since 3.0.0
  1299. * @see unzip_file()
  1300. * @access private
  1301. *
  1302. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  1303. *
  1304. * @param string $file Full path and filename of ZIP archive.
  1305. * @param string $to Full path on the filesystem to extract archive to.
  1306. * @param string[] $needed_dirs A partial list of required folders needed to be created.
  1307. * @return true|WP_Error True on success, WP_Error on failure.
  1308. */
  1309. function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
  1310. global $wp_filesystem;
  1311. $z = new ZipArchive();
  1312. $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
  1313. if ( true !== $zopen ) {
  1314. return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'zipa…

Large files files are truncated, but you can click here to view the full file