PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/file.php

https://bitbucket.org/devbctph/futura_wp
PHP | 1799 lines | 1077 code | 213 blank | 509 comment | 272 complexity | 50753da05cc993c482ce832784bc7430 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause
  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. // Attachments
  39. 'attachment.php' => __( 'Attachment Template' ),
  40. 'image.php' => __( 'Image Attachment Template' ),
  41. 'video.php' => __( 'Video Attachment Template' ),
  42. 'audio.php' => __( 'Audio Attachment Template' ),
  43. 'application.php' => __( 'Application Attachment Template' ),
  44. // Embeds
  45. 'embed.php' => __( 'Embed Template' ),
  46. 'embed-404.php' => __( 'Embed 404 Template' ),
  47. 'embed-content.php' => __( 'Embed Content Template' ),
  48. 'header-embed.php' => __( 'Embed Header Template' ),
  49. 'footer-embed.php' => __( 'Embed Footer Template' ),
  50. // Stylesheets
  51. 'style.css' => __( 'Stylesheet' ),
  52. 'editor-style.css' => __( 'Visual Editor Stylesheet' ),
  53. 'editor-style-rtl.css' => __( 'Visual Editor RTL Stylesheet' ),
  54. 'rtl.css' => __( 'RTL Stylesheet' ),
  55. // Other
  56. 'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ),
  57. '.htaccess' => __( '.htaccess (for rewrite rules )' ),
  58. // Deprecated files
  59. 'wp-layout.css' => __( 'Stylesheet' ),
  60. 'wp-comments.php' => __( 'Comments Template' ),
  61. 'wp-comments-popup.php' => __( 'Popup Comments Template' ),
  62. 'comments-popup.php' => __( 'Popup Comments' ),
  63. );
  64. /**
  65. * Get the description for standard WordPress theme files and other various standard
  66. * WordPress files
  67. *
  68. * @since 1.5.0
  69. *
  70. * @global array $wp_file_descriptions Theme file descriptions.
  71. * @global array $allowed_files List of allowed files.
  72. * @param string $file Filesystem path or filename
  73. * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist.
  74. * Appends 'Page Template' to basename of $file if the file is a page template
  75. */
  76. function get_file_description( $file ) {
  77. global $wp_file_descriptions, $allowed_files;
  78. $dirname = pathinfo( $file, PATHINFO_DIRNAME );
  79. $file_path = $allowed_files[ $file ];
  80. if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) {
  81. return $wp_file_descriptions[ basename( $file ) ];
  82. } elseif ( file_exists( $file_path ) && is_file( $file_path ) ) {
  83. $template_data = implode( '', file( $file_path ) );
  84. if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) ) {
  85. return sprintf( __( '%s Page Template' ), _cleanup_header_comment( $name[1] ) );
  86. }
  87. }
  88. return trim( basename( $file ) );
  89. }
  90. /**
  91. * Get the absolute filesystem path to the root of the WordPress installation
  92. *
  93. * @since 1.5.0
  94. *
  95. * @return string Full filesystem path to the root of the WordPress installation
  96. */
  97. function get_home_path() {
  98. $home = set_url_scheme( get_option( 'home' ), 'http' );
  99. $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );
  100. if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
  101. $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
  102. $pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
  103. $home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
  104. $home_path = trailingslashit( $home_path );
  105. } else {
  106. $home_path = ABSPATH;
  107. }
  108. return str_replace( '\\', '/', $home_path );
  109. }
  110. /**
  111. * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
  112. * The depth of the recursiveness can be controlled by the $levels param.
  113. *
  114. * @since 2.6.0
  115. * @since 4.9.0 Added the `$exclusions` parameter.
  116. *
  117. * @param string $folder Optional. Full path to folder. Default empty.
  118. * @param int $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
  119. * @param array $exclusions Optional. List of folders and files to skip.
  120. * @return bool|array False on failure, Else array of files
  121. */
  122. function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
  123. if ( empty( $folder ) ) {
  124. return false;
  125. }
  126. $folder = trailingslashit( $folder );
  127. if ( ! $levels ) {
  128. return false;
  129. }
  130. $files = array();
  131. $dir = @opendir( $folder );
  132. if ( $dir ) {
  133. while ( ( $file = readdir( $dir ) ) !== false ) {
  134. // Skip current and parent folder links.
  135. if ( in_array( $file, array( '.', '..' ), true ) ) {
  136. continue;
  137. }
  138. // Skip hidden and excluded files.
  139. if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
  140. continue;
  141. }
  142. if ( is_dir( $folder . $file ) ) {
  143. $files2 = list_files( $folder . $file, $levels - 1 );
  144. if ( $files2 ) {
  145. $files = array_merge($files, $files2 );
  146. } else {
  147. $files[] = $folder . $file . '/';
  148. }
  149. } else {
  150. $files[] = $folder . $file;
  151. }
  152. }
  153. }
  154. @closedir( $dir );
  155. return $files;
  156. }
  157. /**
  158. * Get list of file extensions that are editable in plugins.
  159. *
  160. * @since 4.9.0
  161. *
  162. * @param string $plugin Plugin.
  163. * @return array File extensions.
  164. */
  165. function wp_get_plugin_file_editable_extensions( $plugin ) {
  166. $editable_extensions = array(
  167. 'bash',
  168. 'conf',
  169. 'css',
  170. 'diff',
  171. 'htm',
  172. 'html',
  173. 'http',
  174. 'inc',
  175. 'include',
  176. 'js',
  177. 'json',
  178. 'jsx',
  179. 'less',
  180. 'md',
  181. 'patch',
  182. 'php',
  183. 'php3',
  184. 'php4',
  185. 'php5',
  186. 'php7',
  187. 'phps',
  188. 'phtml',
  189. 'sass',
  190. 'scss',
  191. 'sh',
  192. 'sql',
  193. 'svg',
  194. 'text',
  195. 'txt',
  196. 'xml',
  197. 'yaml',
  198. 'yml',
  199. );
  200. /**
  201. * Filters file type extensions editable in the plugin editor.
  202. *
  203. * @since 2.8.0
  204. * @since 4.9.0 Adds $plugin param.
  205. *
  206. * @param string $plugin Plugin file.
  207. * @param array $editable_extensions An array of editable plugin file extensions.
  208. */
  209. $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions, $plugin );
  210. return $editable_extensions;
  211. }
  212. /**
  213. * Get list of file extensions that are editable for a given theme.
  214. *
  215. * @param WP_Theme $theme Theme.
  216. * @return array File extensions.
  217. */
  218. function wp_get_theme_file_editable_extensions( $theme ) {
  219. $default_types = array(
  220. 'bash',
  221. 'conf',
  222. 'css',
  223. 'diff',
  224. 'htm',
  225. 'html',
  226. 'http',
  227. 'inc',
  228. 'include',
  229. 'js',
  230. 'json',
  231. 'jsx',
  232. 'less',
  233. 'md',
  234. 'patch',
  235. 'php',
  236. 'php3',
  237. 'php4',
  238. 'php5',
  239. 'php7',
  240. 'phps',
  241. 'phtml',
  242. 'sass',
  243. 'scss',
  244. 'sh',
  245. 'sql',
  246. 'svg',
  247. 'text',
  248. 'txt',
  249. 'xml',
  250. 'yaml',
  251. 'yml',
  252. );
  253. /**
  254. * Filters the list of file types allowed for editing in the Theme editor.
  255. *
  256. * @since 4.4.0
  257. *
  258. * @param array $default_types List of file types. Default types include 'php' and 'css'.
  259. * @param WP_Theme $theme The current Theme object.
  260. */
  261. $file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
  262. // Ensure that default types are still there.
  263. return array_unique( array_merge( $file_types, $default_types ) );
  264. }
  265. /**
  266. * Print file editor templates (for plugins and themes).
  267. *
  268. * @since 4.9.0
  269. */
  270. function wp_print_file_editor_templates() {
  271. ?>
  272. <script type="text/html" id="tmpl-wp-file-editor-notice">
  273. <div class="notice inline notice-{{ data.type || 'info' }} {{ data.alt ? 'notice-alt' : '' }} {{ data.dismissible ? 'is-dismissible' : '' }} {{ data.classes || '' }}">
  274. <# if ( 'php_error' === data.code ) { #>
  275. <p>
  276. <?php
  277. printf(
  278. /* translators: %$1s is line number and %1$s is file path. */
  279. __( '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.' ),
  280. '{{ data.line }}',
  281. '{{ data.file }}'
  282. );
  283. ?>
  284. </p>
  285. <pre>{{ data.message }}</pre>
  286. <# } else if ( 'file_not_writable' === data.code ) { #>
  287. <p><?php _e( 'You need to make this file writable before you can save your changes. See <a href="https://codex.wordpress.org/Changing_File_Permissions">the Codex</a> for more information.' ); ?></p>
  288. <# } else { #>
  289. <p>{{ data.message || data.code }}</p>
  290. <# if ( 'lint_errors' === data.code ) { #>
  291. <p>
  292. <# var elementId = 'el-' + String( Math.random() ); #>
  293. <input id="{{ elementId }}" type="checkbox">
  294. <label for="{{ elementId }}"><?php _e( 'Update anyway, even though it might break your site?' ); ?></label>
  295. </p>
  296. <# } #>
  297. <# } #>
  298. <# if ( data.dismissible ) { #>
  299. <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e( 'Dismiss' ); ?></span></button>
  300. <# } #>
  301. </div>
  302. </script>
  303. <?php
  304. }
  305. /**
  306. * Attempt to edit a file for a theme or plugin.
  307. *
  308. * When editing a PHP file, loopback requests will be made to the admin and the homepage
  309. * to attempt to see if there is a fatal error introduced. If so, the PHP change will be
  310. * reverted.
  311. *
  312. * @since 4.9.0
  313. *
  314. * @param array $args {
  315. * Args. Note that all of the arg values are already unslashed. They are, however,
  316. * coming straight from $_POST and are not validated or sanitized in any way.
  317. *
  318. * @type string $file Relative path to file.
  319. * @type string $plugin Plugin being edited.
  320. * @type string $theme Theme being edited.
  321. * @type string $newcontent New content for the file.
  322. * @type string $nonce Nonce.
  323. * }
  324. * @return true|WP_Error True on success or `WP_Error` on failure.
  325. */
  326. function wp_edit_theme_plugin_file( $args ) {
  327. if ( empty( $args['file'] ) ) {
  328. return new WP_Error( 'missing_file' );
  329. }
  330. $file = $args['file'];
  331. if ( 0 !== validate_file( $file ) ) {
  332. return new WP_Error( 'bad_file' );
  333. }
  334. if ( ! isset( $args['newcontent'] ) ) {
  335. return new WP_Error( 'missing_content' );
  336. }
  337. $content = $args['newcontent'];
  338. if ( ! isset( $args['nonce'] ) ) {
  339. return new WP_Error( 'missing_nonce' );
  340. }
  341. $plugin = null;
  342. $theme = null;
  343. $real_file = null;
  344. if ( ! empty( $args['plugin'] ) ) {
  345. $plugin = $args['plugin'];
  346. if ( ! current_user_can( 'edit_plugins' ) ) {
  347. return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
  348. }
  349. if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
  350. return new WP_Error( 'nonce_failure' );
  351. }
  352. if ( ! array_key_exists( $plugin, get_plugins() ) ) {
  353. return new WP_Error( 'invalid_plugin' );
  354. }
  355. if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
  356. return new WP_Error( 'bad_plugin_file_path', __( 'Sorry, that file cannot be edited.' ) );
  357. }
  358. $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
  359. $real_file = WP_PLUGIN_DIR . '/' . $file;
  360. $is_active = in_array(
  361. $plugin,
  362. (array) get_option( 'active_plugins', array() ),
  363. true
  364. );
  365. } elseif ( ! empty( $args['theme'] ) ) {
  366. $stylesheet = $args['theme'];
  367. if ( 0 !== validate_file( $stylesheet ) ) {
  368. return new WP_Error( 'bad_theme_path' );
  369. }
  370. if ( ! current_user_can( 'edit_themes' ) ) {
  371. return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit templates for this site.' ) );
  372. }
  373. $theme = wp_get_theme( $stylesheet );
  374. if ( ! $theme->exists() ) {
  375. return new WP_Error( 'non_existent_theme', __( 'The requested theme does not exist.' ) );
  376. }
  377. $real_file = $theme->get_stylesheet_directory() . '/' . $file;
  378. if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $real_file . $stylesheet ) ) {
  379. return new WP_Error( 'nonce_failure' );
  380. }
  381. if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
  382. return new WP_Error(
  383. 'theme_no_stylesheet',
  384. __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message()
  385. );
  386. }
  387. $editable_extensions = wp_get_theme_file_editable_extensions( $theme );
  388. $allowed_files = array();
  389. foreach ( $editable_extensions as $type ) {
  390. switch ( $type ) {
  391. case 'php':
  392. $allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) );
  393. break;
  394. case 'css':
  395. $style_files = $theme->get_files( 'css', -1 );
  396. $allowed_files['style.css'] = $style_files['style.css'];
  397. $allowed_files = array_merge( $allowed_files, $style_files );
  398. break;
  399. default:
  400. $allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) );
  401. break;
  402. }
  403. }
  404. // Compare based on relative paths
  405. if ( 0 !== validate_file( $file, array_keys( $allowed_files ) ) ) {
  406. return new WP_Error( 'disallowed_theme_file', __( 'Sorry, that file cannot be edited.' ) );
  407. }
  408. $is_active = ( get_stylesheet() === $stylesheet || get_template() === $stylesheet );
  409. } else {
  410. return new WP_Error( 'missing_theme_or_plugin' );
  411. }
  412. // Ensure file is real.
  413. if ( ! is_file( $real_file ) ) {
  414. return new WP_Error( 'file_does_not_exist', __( 'No such file exists! Double check the name and try again.' ) );
  415. }
  416. // Ensure file extension is allowed.
  417. $extension = null;
  418. if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
  419. $extension = strtolower( $matches[1] );
  420. if ( ! in_array( $extension, $editable_extensions, true ) ) {
  421. return new WP_Error( 'illegal_file_type', __( 'Files of this type are not editable.' ) );
  422. }
  423. }
  424. $previous_content = file_get_contents( $real_file );
  425. if ( ! is_writeable( $real_file ) ) {
  426. return new WP_Error( 'file_not_writable' );
  427. }
  428. $f = fopen( $real_file, 'w+' );
  429. if ( false === $f ) {
  430. return new WP_Error( 'file_not_writable' );
  431. }
  432. $written = fwrite( $f, $content );
  433. fclose( $f );
  434. if ( false === $written ) {
  435. return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
  436. }
  437. if ( 'php' === $extension && function_exists( 'opcache_invalidate' ) ) {
  438. opcache_invalidate( $real_file, true );
  439. }
  440. if ( $is_active && 'php' === $extension ) {
  441. $scrape_key = md5( rand() );
  442. $transient = 'scrape_key_' . $scrape_key;
  443. $scrape_nonce = strval( rand() );
  444. set_transient( $transient, $scrape_nonce, 60 ); // It shouldn't take more than 60 seconds to make the two loopback requests.
  445. $cookies = wp_unslash( $_COOKIE );
  446. $scrape_params = array(
  447. 'wp_scrape_key' => $scrape_key,
  448. 'wp_scrape_nonce' => $scrape_nonce,
  449. );
  450. $headers = array(
  451. 'Cache-Control' => 'no-cache',
  452. );
  453. // Include Basic auth in loopback requests.
  454. if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
  455. $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
  456. }
  457. // Make sure PHP process doesn't die before loopback requests complete.
  458. @set_time_limit( 300 );
  459. // Time to wait for loopback requests to finish.
  460. $timeout = 100;
  461. $needle_start = "###### wp_scraping_result_start:$scrape_key ######";
  462. $needle_end = "###### wp_scraping_result_end:$scrape_key ######";
  463. // Attempt loopback request to editor to see if user just whitescreened themselves.
  464. if ( $plugin ) {
  465. $url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
  466. } elseif ( isset( $stylesheet ) ) {
  467. $url = add_query_arg(
  468. array(
  469. 'theme' => $stylesheet,
  470. 'file' => $file,
  471. ),
  472. admin_url( 'theme-editor.php' )
  473. );
  474. } else {
  475. $url = admin_url();
  476. }
  477. $url = add_query_arg( $scrape_params, $url );
  478. $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout' ) );
  479. $body = wp_remote_retrieve_body( $r );
  480. $scrape_result_position = strpos( $body, $needle_start );
  481. $loopback_request_failure = array(
  482. 'code' => 'loopback_request_failed',
  483. '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.' ),
  484. );
  485. $json_parse_failure = array(
  486. 'code' => 'json_parse_error',
  487. );
  488. $result = null;
  489. if ( false === $scrape_result_position ) {
  490. $result = $loopback_request_failure;
  491. } else {
  492. $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
  493. $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
  494. $result = json_decode( trim( $error_output ), true );
  495. if ( empty( $result ) ) {
  496. $result = $json_parse_failure;
  497. }
  498. }
  499. // Try making request to homepage as well to see if visitors have been whitescreened.
  500. if ( true === $result ) {
  501. $url = home_url( '/' );
  502. $url = add_query_arg( $scrape_params, $url );
  503. $r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout' ) );
  504. $body = wp_remote_retrieve_body( $r );
  505. $scrape_result_position = strpos( $body, $needle_start );
  506. if ( false === $scrape_result_position ) {
  507. $result = $loopback_request_failure;
  508. } else {
  509. $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
  510. $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
  511. $result = json_decode( trim( $error_output ), true );
  512. if ( empty( $result ) ) {
  513. $result = $json_parse_failure;
  514. }
  515. }
  516. }
  517. delete_transient( $transient );
  518. if ( true !== $result ) {
  519. // Roll-back file change.
  520. file_put_contents( $real_file, $previous_content );
  521. if ( function_exists( 'opcache_invalidate' ) ) {
  522. opcache_invalidate( $real_file, true );
  523. }
  524. if ( ! isset( $result['message'] ) ) {
  525. $message = __( 'An unidentified error has occurred.' );
  526. } else {
  527. $message = $result['message'];
  528. unset( $result['message'] );
  529. }
  530. return new WP_Error( 'php_error', $message, $result );
  531. }
  532. }
  533. if ( $theme instanceof WP_Theme ) {
  534. $theme->cache_delete();
  535. }
  536. return true;
  537. }
  538. /**
  539. * Returns a filename of a Temporary unique file.
  540. * Please note that the calling function must unlink() this itself.
  541. *
  542. * The filename is based off the passed parameter or defaults to the current unix timestamp,
  543. * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
  544. *
  545. * @since 2.6.0
  546. *
  547. * @param string $filename Optional. Filename to base the Unique file off. Default empty.
  548. * @param string $dir Optional. Directory to store the file in. Default empty.
  549. * @return string a writable filename
  550. */
  551. function wp_tempnam( $filename = '', $dir = '' ) {
  552. if ( empty( $dir ) ) {
  553. $dir = get_temp_dir();
  554. }
  555. if ( empty( $filename ) || '.' == $filename || '/' == $filename || '\\' == $filename ) {
  556. $filename = time();
  557. }
  558. // Use the basename of the given file without the extension as the name for the temporary directory
  559. $temp_filename = basename( $filename );
  560. $temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );
  561. // If the folder is falsey, use its parent directory name instead.
  562. if ( ! $temp_filename ) {
  563. return wp_tempnam( dirname( $filename ), $dir );
  564. }
  565. // Suffix some random data to avoid filename conflicts
  566. $temp_filename .= '-' . wp_generate_password( 6, false );
  567. $temp_filename .= '.tmp';
  568. $temp_filename = $dir . wp_unique_filename( $dir, $temp_filename );
  569. $fp = @fopen( $temp_filename, 'x' );
  570. if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
  571. return wp_tempnam( $filename, $dir );
  572. }
  573. if ( $fp ) {
  574. fclose( $fp );
  575. }
  576. return $temp_filename;
  577. }
  578. /**
  579. * Makes sure that the file that was requested to be edited is allowed to be edited.
  580. *
  581. * Function will die if you are not allowed to edit the file.
  582. *
  583. * @since 1.5.0
  584. *
  585. * @param string $file File the user is attempting to edit.
  586. * @param array $allowed_files Optional. Array of allowed files to edit, $file must match an entry exactly.
  587. * @return string|null
  588. */
  589. function validate_file_to_edit( $file, $allowed_files = array() ) {
  590. $code = validate_file( $file, $allowed_files );
  591. if (!$code )
  592. return $file;
  593. switch ( $code ) {
  594. case 1 :
  595. wp_die( __( 'Sorry, that file cannot be edited.' ) );
  596. // case 2 :
  597. // wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
  598. case 3 :
  599. wp_die( __( 'Sorry, that file cannot be edited.' ) );
  600. }
  601. }
  602. /**
  603. * Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type,
  604. * and moving the file to the appropriate directory within the uploads directory.
  605. *
  606. * @access private
  607. * @since 4.0.0
  608. *
  609. * @see wp_handle_upload_error
  610. *
  611. * @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file.
  612. * @param array|false $overrides An associative array of names => values to override default variables. Default false.
  613. * @param string $time Time formatted in 'yyyy/mm'.
  614. * @param string $action Expected value for $_POST['action'].
  615. * @return array On success, returns an associative array of file attributes. On failure, returns
  616. * $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
  617. */
  618. function _wp_handle_upload( &$file, $overrides, $time, $action ) {
  619. // The default error handler.
  620. if ( ! function_exists( 'wp_handle_upload_error' ) ) {
  621. function wp_handle_upload_error( &$file, $message ) {
  622. return array( 'error' => $message );
  623. }
  624. }
  625. /**
  626. * Filters the data for a file before it is uploaded to WordPress.
  627. *
  628. * The dynamic portion of the hook name, `$action`, refers to the post action.
  629. *
  630. * @since 2.9.0 as 'wp_handle_upload_prefilter'.
  631. * @since 4.0.0 Converted to a dynamic hook with `$action`.
  632. *
  633. * @param array $file An array of data for a single file.
  634. */
  635. $file = apply_filters( "{$action}_prefilter", $file );
  636. // You may define your own function and pass the name in $overrides['upload_error_handler']
  637. $upload_error_handler = 'wp_handle_upload_error';
  638. if ( isset( $overrides['upload_error_handler'] ) ) {
  639. $upload_error_handler = $overrides['upload_error_handler'];
  640. }
  641. // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
  642. if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) {
  643. return call_user_func_array( $upload_error_handler, array( &$file, $file['error'] ) );
  644. }
  645. // Install user overrides. Did we mention that this voids your warranty?
  646. // You may define your own function and pass the name in $overrides['unique_filename_callback']
  647. $unique_filename_callback = null;
  648. if ( isset( $overrides['unique_filename_callback'] ) ) {
  649. $unique_filename_callback = $overrides['unique_filename_callback'];
  650. }
  651. /*
  652. * This may not have orignially been intended to be overrideable,
  653. * but historically has been.
  654. */
  655. if ( isset( $overrides['upload_error_strings'] ) ) {
  656. $upload_error_strings = $overrides['upload_error_strings'];
  657. } else {
  658. // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
  659. $upload_error_strings = array(
  660. false,
  661. __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.' ),
  662. __( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.' ),
  663. __( 'The uploaded file was only partially uploaded.' ),
  664. __( 'No file was uploaded.' ),
  665. '',
  666. __( 'Missing a temporary folder.' ),
  667. __( 'Failed to write file to disk.' ),
  668. __( 'File upload stopped by extension.' )
  669. );
  670. }
  671. // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
  672. $test_form = isset( $overrides['test_form'] ) ? $overrides['test_form'] : true;
  673. $test_size = isset( $overrides['test_size'] ) ? $overrides['test_size'] : true;
  674. // If you override this, you must provide $ext and $type!!
  675. $test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true;
  676. $mimes = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false;
  677. // A correct form post will pass this test.
  678. if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) {
  679. return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) );
  680. }
  681. // A successful upload will pass this test. It makes no sense to override this one.
  682. if ( isset( $file['error'] ) && $file['error'] > 0 ) {
  683. return call_user_func_array( $upload_error_handler, array( &$file, $upload_error_strings[ $file['error'] ] ) );
  684. }
  685. $test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] );
  686. // A non-empty file will pass this test.
  687. if ( $test_size && ! ( $test_file_size > 0 ) ) {
  688. if ( is_multisite() ) {
  689. $error_msg = __( 'File is empty. Please upload something more substantial.' );
  690. } else {
  691. $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.' );
  692. }
  693. return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) );
  694. }
  695. // A properly uploaded file will pass this test. There should be no reason to override this one.
  696. $test_uploaded_file = 'wp_handle_upload' === $action ? @ is_uploaded_file( $file['tmp_name'] ) : @ is_file( $file['tmp_name'] );
  697. if ( ! $test_uploaded_file ) {
  698. return call_user_func_array( $upload_error_handler, array( &$file, __( 'Specified file failed upload test.' ) ) );
  699. }
  700. // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
  701. if ( $test_type ) {
  702. $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
  703. $ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
  704. $type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
  705. $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
  706. // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
  707. if ( $proper_filename ) {
  708. $file['name'] = $proper_filename;
  709. }
  710. if ( ( ! $type || !$ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
  711. return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, this file type is not permitted for security reasons.' ) ) );
  712. }
  713. if ( ! $type ) {
  714. $type = $file['type'];
  715. }
  716. } else {
  717. $type = '';
  718. }
  719. /*
  720. * A writable uploads dir will pass this test. Again, there's no point
  721. * overriding this one.
  722. */
  723. if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) ) {
  724. return call_user_func_array( $upload_error_handler, array( &$file, $uploads['error'] ) );
  725. }
  726. $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
  727. // Move the file to the uploads dir.
  728. $new_file = $uploads['path'] . "/$filename";
  729. /**
  730. * Filters whether to short-circuit moving the uploaded file after passing all checks.
  731. *
  732. * If a non-null value is passed to the filter, moving the file and any related error
  733. * reporting will be completely skipped.
  734. *
  735. * @since 4.9.0
  736. *
  737. * @param string $move_new_file If null (default) move the file after the upload.
  738. * @param string $file An array of data for a single file.
  739. * @param string $new_file Filename of the newly-uploaded file.
  740. * @param string $type File type.
  741. */
  742. $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type );
  743. if ( null === $move_new_file ) {
  744. if ( 'wp_handle_upload' === $action ) {
  745. $move_new_file = @ move_uploaded_file( $file['tmp_name'], $new_file );
  746. } else {
  747. // use copy and unlink because rename breaks streams.
  748. $move_new_file = @ copy( $file['tmp_name'], $new_file );
  749. unlink( $file['tmp_name'] );
  750. }
  751. if ( false === $move_new_file ) {
  752. if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
  753. $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
  754. } else {
  755. $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
  756. }
  757. return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $error_path ) );
  758. }
  759. }
  760. // Set correct file permissions.
  761. $stat = stat( dirname( $new_file ));
  762. $perms = $stat['mode'] & 0000666;
  763. @ chmod( $new_file, $perms );
  764. // Compute the URL.
  765. $url = $uploads['url'] . "/$filename";
  766. if ( is_multisite() ) {
  767. delete_transient( 'dirsize_cache' );
  768. }
  769. /**
  770. * Filters the data array for the uploaded file.
  771. *
  772. * @since 2.1.0
  773. *
  774. * @param array $upload {
  775. * Array of upload data.
  776. *
  777. * @type string $file Filename of the newly-uploaded file.
  778. * @type string $url URL of the uploaded file.
  779. * @type string $type File type.
  780. * }
  781. * @param string $context The type of upload action. Values include 'upload' or 'sideload'.
  782. */
  783. return apply_filters( 'wp_handle_upload', array(
  784. 'file' => $new_file,
  785. 'url' => $url,
  786. 'type' => $type
  787. ), 'wp_handle_sideload' === $action ? 'sideload' : 'upload' );
  788. }
  789. /**
  790. * Wrapper for _wp_handle_upload().
  791. *
  792. * Passes the {@see 'wp_handle_upload'} action.
  793. *
  794. * @since 2.0.0
  795. *
  796. * @see _wp_handle_upload()
  797. *
  798. * @param array $file Reference to a single element of `$_FILES`. Call the function once for
  799. * each uploaded file.
  800. * @param array|bool $overrides Optional. An associative array of names=>values to override default
  801. * variables. Default false.
  802. * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
  803. * @return array On success, returns an associative array of file attributes. On failure, returns
  804. * $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
  805. */
  806. function wp_handle_upload( &$file, $overrides = false, $time = null ) {
  807. /*
  808. * $_POST['action'] must be set and its value must equal $overrides['action']
  809. * or this:
  810. */
  811. $action = 'wp_handle_upload';
  812. if ( isset( $overrides['action'] ) ) {
  813. $action = $overrides['action'];
  814. }
  815. return _wp_handle_upload( $file, $overrides, $time, $action );
  816. }
  817. /**
  818. * Wrapper for _wp_handle_upload().
  819. *
  820. * Passes the {@see 'wp_handle_sideload'} action.
  821. *
  822. * @since 2.6.0
  823. *
  824. * @see _wp_handle_upload()
  825. *
  826. * @param array $file An array similar to that of a PHP `$_FILES` POST array
  827. * @param array|bool $overrides Optional. An associative array of names=>values to override default
  828. * variables. Default false.
  829. * @param string $time Optional. Time formatted in 'yyyy/mm'. Default null.
  830. * @return array On success, returns an associative array of file attributes. On failure, returns
  831. * $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
  832. */
  833. function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
  834. /*
  835. * $_POST['action'] must be set and its value must equal $overrides['action']
  836. * or this:
  837. */
  838. $action = 'wp_handle_sideload';
  839. if ( isset( $overrides['action'] ) ) {
  840. $action = $overrides['action'];
  841. }
  842. return _wp_handle_upload( $file, $overrides, $time, $action );
  843. }
  844. /**
  845. * Downloads a URL to a local temporary file using the WordPress HTTP Class.
  846. * Please note, That the calling function must unlink() the file.
  847. *
  848. * @since 2.5.0
  849. *
  850. * @param string $url the URL of the file to download
  851. * @param int $timeout The timeout for the request to download the file default 300 seconds
  852. * @return mixed WP_Error on failure, string Filename on success.
  853. */
  854. function download_url( $url, $timeout = 300 ) {
  855. //WARNING: The file is not automatically deleted, The script must unlink() the file.
  856. if ( ! $url )
  857. return new WP_Error('http_no_url', __('Invalid URL Provided.'));
  858. $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
  859. $tmpfname = wp_tempnam( $url_filename );
  860. if ( ! $tmpfname )
  861. return new WP_Error('http_no_file', __('Could not create Temporary file.'));
  862. $response = wp_safe_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
  863. if ( is_wp_error( $response ) ) {
  864. unlink( $tmpfname );
  865. return $response;
  866. }
  867. if ( 200 != wp_remote_retrieve_response_code( $response ) ){
  868. unlink( $tmpfname );
  869. return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
  870. }
  871. $content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
  872. if ( $content_md5 ) {
  873. $md5_check = verify_file_md5( $tmpfname, $content_md5 );
  874. if ( is_wp_error( $md5_check ) ) {
  875. unlink( $tmpfname );
  876. return $md5_check;
  877. }
  878. }
  879. return $tmpfname;
  880. }
  881. /**
  882. * Calculates and compares the MD5 of a file to its expected value.
  883. *
  884. * @since 3.7.0
  885. *
  886. * @param string $filename The filename to check the MD5 of.
  887. * @param string $expected_md5 The expected MD5 of the file, either a base64 encoded raw md5, or a hex-encoded md5
  888. * @return bool|object WP_Error on failure, true on success, false when the MD5 format is unknown/unexpected
  889. */
  890. function verify_file_md5( $filename, $expected_md5 ) {
  891. if ( 32 == strlen( $expected_md5 ) )
  892. $expected_raw_md5 = pack( 'H*', $expected_md5 );
  893. elseif ( 24 == strlen( $expected_md5 ) )
  894. $expected_raw_md5 = base64_decode( $expected_md5 );
  895. else
  896. return false; // unknown format
  897. $file_md5 = md5_file( $filename, true );
  898. if ( $file_md5 === $expected_raw_md5 )
  899. return true;
  900. return new WP_Error( 'md5_mismatch', sprintf( __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ), bin2hex( $file_md5 ), bin2hex( $expected_raw_md5 ) ) );
  901. }
  902. /**
  903. * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
  904. * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
  905. *
  906. * Attempts to increase the PHP Memory limit to 256M before uncompressing,
  907. * However, The most memory required shouldn't be much larger than the Archive itself.
  908. *
  909. * @since 2.5.0
  910. *
  911. * @global WP_Filesystem_Base $wp_filesystem Subclass
  912. *
  913. * @param string $file Full path and filename of zip archive
  914. * @param string $to Full path on the filesystem to extract archive to
  915. * @return mixed WP_Error on failure, True on success
  916. */
  917. function unzip_file($file, $to) {
  918. global $wp_filesystem;
  919. if ( ! $wp_filesystem || !is_object($wp_filesystem) )
  920. return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  921. // Unzip can use a lot of memory, but not this much hopefully.
  922. wp_raise_memory_limit( 'admin' );
  923. $needed_dirs = array();
  924. $to = trailingslashit($to);
  925. // Determine any parent dir's needed (of the upgrade directory)
  926. if ( ! $wp_filesystem->is_dir($to) ) { //Only do parents if no children exist
  927. $path = preg_split('![/\\\]!', untrailingslashit($to));
  928. for ( $i = count($path); $i >= 0; $i-- ) {
  929. if ( empty($path[$i]) )
  930. continue;
  931. $dir = implode('/', array_slice($path, 0, $i+1) );
  932. if ( preg_match('!^[a-z]:$!i', $dir) ) // Skip it if it looks like a Windows Drive letter.
  933. continue;
  934. if ( ! $wp_filesystem->is_dir($dir) )
  935. $needed_dirs[] = $dir;
  936. else
  937. break; // A folder exists, therefor, we dont need the check the levels below this
  938. }
  939. }
  940. /**
  941. * Filters whether to use ZipArchive to unzip archives.
  942. *
  943. * @since 3.0.0
  944. *
  945. * @param bool $ziparchive Whether to use ZipArchive. Default true.
  946. */
  947. if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
  948. $result = _unzip_file_ziparchive($file, $to, $needed_dirs);
  949. if ( true === $result ) {
  950. return $result;
  951. } elseif ( is_wp_error($result) ) {
  952. if ( 'incompatible_archive' != $result->get_error_code() )
  953. return $result;
  954. }
  955. }
  956. // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
  957. return _unzip_file_pclzip($file, $to, $needed_dirs);
  958. }
  959. /**
  960. * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
  961. * Assumes that WP_Filesystem() has already been called and set up.
  962. *
  963. * @since 3.0.0
  964. * @see unzip_file
  965. * @access private
  966. *
  967. * @global WP_Filesystem_Base $wp_filesystem Subclass
  968. *
  969. * @param string $file Full path and filename of zip archive
  970. * @param string $to Full path on the filesystem to extract archive to
  971. * @param array $needed_dirs A partial list of required folders needed to be created.
  972. * @return mixed WP_Error on failure, True on success
  973. */
  974. function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) {
  975. global $wp_filesystem;
  976. $z = new ZipArchive();
  977. $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
  978. if ( true !== $zopen )
  979. return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
  980. $uncompressed_size = 0;
  981. for ( $i = 0; $i < $z->numFiles; $i++ ) {
  982. if ( ! $info = $z->statIndex($i) )
  983. return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
  984. if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Skip the OS X-created __MACOSX directory
  985. continue;
  986. // Don't extract invalid files:
  987. if ( 0 !== validate_file( $info['name'] ) ) {
  988. continue;
  989. }
  990. $uncompressed_size += $info['size'];
  991. if ( '/' === substr( $info['name'], -1 ) ) {
  992. // Directory.
  993. $needed_dirs[] = $to . untrailingslashit( $info['name'] );
  994. } elseif ( '.' !== $dirname = dirname( $info['name'] ) ) {
  995. // Path to a file.
  996. $needed_dirs[] = $to . untrailingslashit( $dirname );
  997. }
  998. }
  999. /*
  1000. * disk_free_space() could return false. Assume that any falsey value is an error.
  1001. * A disk that has zero free bytes has bigger problems.
  1002. * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
  1003. */
  1004. if ( wp_doing_cron() ) {
  1005. $available_space = @disk_free_space( WP_CONTENT_DIR );
  1006. if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space )
  1007. 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' ) );
  1008. }
  1009. $needed_dirs = array_unique($needed_dirs);
  1010. foreach ( $needed_dirs as $dir ) {
  1011. // Check the parent folders of the folders all exist within the creation array.
  1012. if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist)
  1013. continue;
  1014. if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it
  1015. continue;
  1016. $parent_folder = dirname($dir);
  1017. while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) {
  1018. $needed_dirs[] = $parent_folder;
  1019. $parent_folder = dirname($parent_folder);
  1020. }
  1021. }
  1022. asort($needed_dirs);
  1023. // Create those directories if need be:
  1024. foreach ( $needed_dirs as $_dir ) {
  1025. // Only check to see if the Dir exists upon creation failure. Less I/O this way.
  1026. if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
  1027. return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
  1028. }
  1029. }
  1030. unset($needed_dirs);
  1031. for ( $i = 0; $i < $z->numFiles; $i++ ) {
  1032. if ( ! $info = $z->statIndex($i) )
  1033. return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
  1034. if ( '/' == substr($info['name'], -1) ) // directory
  1035. continue;
  1036. if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files
  1037. continue;
  1038. // Don't extract invalid files:
  1039. if ( 0 !== validate_file( $info['name'] ) ) {
  1040. continue;
  1041. }
  1042. $contents = $z->getFromIndex($i);
  1043. if ( false === $contents )
  1044. return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] );
  1045. if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE) )
  1046. return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] );
  1047. }
  1048. $z->close();
  1049. return true;
  1050. }
  1051. /**
  1052. * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the PclZip library.
  1053. * Assumes that WP_Filesystem() has already been called and set up.
  1054. *
  1055. * @since 3.0.0
  1056. * @see unzip_file
  1057. * @access private
  1058. *
  1059. * @global WP_Filesystem_Base $wp_filesystem Subclass
  1060. *
  1061. * @param string $file Full path and filename of zip archive
  1062. * @param string $to Full path on the filesystem to extract archive to
  1063. * @param array $needed_dirs A partial list of required folders needed to be created.
  1064. * @return mixed WP_Error on failure, True on success
  1065. */
  1066. function _unzip_file_pclzip($file, $to, $needed_dirs = array()) {
  1067. global $wp_filesystem;
  1068. mbstring_binary_safe_encoding();
  1069. require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
  1070. $archive = new PclZip($file);
  1071. $archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING);
  1072. reset_mbstring_encoding();
  1073. // Is the archive valid?
  1074. if ( !is_array($archive_files) )
  1075. return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true));
  1076. if ( 0 == count($archive_files) )
  1077. return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
  1078. $uncompressed_size = 0;
  1079. // Determine any children directories needed (From within the archive)
  1080. foreach ( $archive_files as $file ) {
  1081. if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Skip the OS X-created __MACOSX directory
  1082. continue;
  1083. $uncompressed_size += $file['size'];
  1084. $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) );
  1085. }
  1086. /*
  1087. * disk_free_space() could return false. Assume that any falsey value is an error.
  1088. * A disk that has zero free bytes has bigger problems.
  1089. * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
  1090. */
  1091. if ( wp_doing_cron() ) {
  1092. $available_space = @disk_free_space( WP_CONTENT_DIR );
  1093. if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space )
  1094. 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' ) );
  1095. }
  1096. $needed_dirs = array_unique($needed_dirs);
  1097. foreach ( $needed_dirs as $dir ) {
  1098. // Check the parent folders of the folders all exist within the creation array.
  1099. if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist)
  1100. continue;
  1101. if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it
  1102. continue;
  1103. $parent_folder = dirname($dir);
  1104. while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) {
  1105. $needed_dirs[] = $parent_folder;
  1106. $parent_folder = dirname($parent_folder);
  1107. }
  1108. }
  1109. asort($needed_dirs);
  1110. // Create those directories if need be:
  1111. foreach ( $needed_dirs as $_dir ) {
  1112. // Only check to see if the dir exists upon creation failure. Less I/O this way.
  1113. if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) )
  1114. return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
  1115. }
  1116. unset($needed_dirs);
  1117. // Extract the files from the zip
  1118. foreach ( $archive_files as $file ) {
  1119. if ( $file['folder'] )
  1120. continue;
  1121. if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files
  1122. continue;
  1123. // Don't extract invalid files:
  1124. if ( 0 !== validate_file( $file['filename'] ) ) {
  1125. continue;
  1126. }
  1127. if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE) )
  1128. return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
  1129. }
  1130. return true;
  1131. }
  1132. /**
  1133. * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
  1134. * Assumes that WP_Filesystem() has already been called and setup.
  1135. *
  1136. * @since 2.5.0
  1137. *
  1138. * @global WP_Filesystem_Base $wp_filesystem Subclass
  1139. *
  1140. * @param string $from source directory
  1141. * @param string $to destination directory
  1142. * @param array $skip_list a list of files/folders to skip copying
  1143. * @return mixed WP_Error on failure, True on success.
  1144. */
  1145. function copy_dir($from, $to, $skip_list = array() ) {
  1146. global $wp_filesystem;
  1147. $dirlist = $wp_filesystem->dirlist($from);
  1148. $from = trailingslashit($from);
  1149. $to = trailingslashit($to);
  1150. foreach ( (array) $dirlist as $filename => $fileinfo ) {
  1151. if ( in_array( $filename, $skip_list ) )
  1152. continue;
  1153. if ( 'f' == $fileinfo['type'] ) {
  1154. if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
  1155. // If copy failed, chmod file to 0644 and try again.
  1156. $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
  1157. if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) )
  1158. return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
  1159. }
  1160. } elseif ( 'd' == $fileinfo['type'] ) {
  1161. if ( !$wp_filesystem->is_dir($to . $filename) ) {
  1162. if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
  1163. return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename );
  1164. }
  1165. // generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list
  1166. $sub_skip_list = array();
  1167. foreach ( $skip_list as $skip_item ) {
  1168. if ( 0 === strpos( $skip_item, $filename . '/' ) )
  1169. $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
  1170. }
  1171. $result = copy_dir($from . $filename, $to . $filename, $sub_skip_list);
  1172. if ( is_wp_error($result) )
  1173. return $result;
  1174. }
  1175. }
  1176. return true;
  1177. }
  1178. /**
  1179. * Initialises and connects the WordPress Filesystem Abstraction classes.
  1180. * This function will include the chosen transport and attempt connecting.
  1181. *
  1182. * Plugins may add extra transports, And force WordPress to use them by returning
  1183. * the filename via the {@see 'filesystem_method_file'} filter.
  1184. *
  1185. * @since 2.5.0
  1186. *
  1187. * @global WP_Filesystem_Base $wp_filesystem Subclass
  1188. *
  1189. * @param array|false $args Optional. Connection args, These are passed directly to
  1190. * the `WP_Filesystem_*()` classes. Default false.
  1191. * @param string|false $context Optional. Context for get_filesystem_method(). Default false.
  1192. * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
  1193. * @return null|bool false on failure, true on success.
  1194. */
  1195. function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) {
  1196. global $wp_filesystem;
  1197. require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
  1198. $method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
  1199. if ( ! $method )
  1200. return false;
  1201. if ( ! class_exists( "WP_Filesystem_$method" ) ) {
  1202. /**
  1203. * Filters the path for a specific filesystem method class file.
  1204. *
  1205. * @since 2.6.0
  1206. *
  1207. * @see get_filesystem_method()
  1208. *
  1209. * @param string $path Path to the specific filesystem method class file.
  1210. * @param string $method The filesystem method to use.
  1211. */
  1212. $abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
  1213. if ( ! file_exists($abstraction_file) )
  1214. return;
  1215. require_once($abstraction_file);
  1216. }
  1217. $method = "WP_Filesystem_$method";
  1218. $wp_filesystem = new $method($args);
  1219. //Define the timeouts for the connections. Only available after the construct is called to allow for per-transport overriding of the default.
  1220. if ( ! defined('FS_CONNECT_TIMEOUT') )
  1221. define('FS_CONNECT_TIMEOUT', 30);
  1222. if ( ! defined('FS_TIMEOUT') )
  1223. define('FS_TIMEOUT', 30);
  1224. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
  1225. return false;
  1226. if ( !$wp_filesystem->connect() )
  1227. return false; //There was an error connecting to the server.
  1228. // Set the permission constants if not already set.
  1229. if ( ! defined('FS_CHMOD_DIR') )
  1230. define('FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
  1231. if ( ! defined('FS_CHMOD_FILE') )
  1232. define('FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
  1233. return true;
  1234. }
  1235. /**
  1236. * Determines which method to use for reading, writing, modifying, or deleting
  1237. * files on the filesystem.
  1238. *
  1239. * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
  1240. * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
  1241. * 'ftpext' or 'ftpsockets'.
  1242. *
  1243. * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
  1244. * or filtering via {@see 'filesystem_method'}.
  1245. *
  1246. * @link https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants
  1247. *
  1248. * Plugins may define a custom transport handler, See WP_Filesystem().
  1249. *
  1250. * @since 2.5.0
  1251. *
  1252. * @global callable $_wp_filesystem_direct_method
  1253. *
  1254. * @param array $args Optional. Connection details. Default empty array.
  1255. * @param string $context Optional. Full path to the directory that is tested
  1256. * for being writable. Default empty.
  1257. * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
  1258. * Default false.
  1259. * @return string The transport to use, see description for valid return values.
  1260. */
  1261. function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
  1262. $method = defined('FS_METHOD') ? FS_METHOD : false; // Please ensure that this is either 'direct', 'ssh2', 'ftpext' or 'ftpsockets'
  1263. if ( ! $context ) {
  1264. $context = WP_CONTENT_DIR;
  1265. }
  1266. // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
  1267. if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) {
  1268. $context = dirname( $context );
  1269. }
  1270. $context = trailingslashit( $context );
  1271. if ( ! $method ) {
  1272. $temp_file_name = $context . 'temp-write-test-' . time();
  1273. $temp_handle = @fopen($temp_file_name, 'w');
  1274. if ( $temp_handle ) {
  1275. // Attempt to determine the file owner of the WordPress files, and that of newly created files
  1276. $wp_file_owner = $temp_file_owner = false;
  1277. if ( function_exists('fileowner') ) {
  1278. $wp_file_owner = @fileowner( __FILE__ );
  1279. $temp_file_owner = @fileowner( $temp_file_name );
  1280. }
  1281. if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) {
  1282. // WordPress is creating files as the same owner as the WordPress files,
  1283. // this means it's safe to modify & create new files via PHP.
  1284. $method = 'direct';
  1285. $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
  1286. } elseif ( $allow_relaxed_file_ownership ) {
  1287. // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files
  1288. // safely in this directory. This mode doesn't create new files, only alter existing ones.
  1289. $method = 'direct';
  1290. $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
  1291. }
  1292. @fclose($temp_handle);
  1293. @unlink($temp_file_name);
  1294. }
  1295. }
  1296. if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2';
  1297. if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
  1298. if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
  1299. /**
  1300. * Filters the filesystem method to use.
  1301. *
  1302. * @since 2.6.0
  1303. *
  1304. * @param string $method Filesystem method to return.
  1305. * @param array $args An array of connection details for the method.
  1306. * @param string $context Full path to the directory that is tested for being writable.
  1307. * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
  1308. */
  1309. return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
  1310. }
  1311. /**
  1312. * Displays a form to the user to request for their FTP/SSH details in order
  1313. * to connect to the filesystem.
  1314. *
  1315. * All chosen/entered details are saved, excluding the password.
  1316. *
  1317. * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467)
  1318. * to specify an alternate FTP/SSH port.
  1319. *
  1320. * Plugins may override this form by returning true|false via the {@see 'request_filesystem_credentials'} filter.
  1321. *
  1322. * @since 2.5.0
  1323. * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
  1324. *
  1325. * @global string $pagenow
  1326. *
  1327. * @param string $form_post The URL to post the form to.
  1328. * @param string $type Optional. Chosen type of filesystem. Default empty.
  1329. * @param bool $error Optional. Whether the current request has failed to connect.
  1330. * Default false.
  1331. * @param string $context Optional. Full path to the directory that is tested for being
  1332. * writable. Default empty.
  1333. * @param array $extra_fields Optional. Extra `POST` fields to be checked for inclusion in
  1334. * the post. Default null.
  1335. * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
  1336. *
  1337. * @return bool False on failure, true on success.
  1338. */
  1339. function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) {
  1340. global $pagenow;
  1341. /**
  1342. * Filters the filesystem credentials form output.
  1343. *
  1344. * Returning anything other than an empty string will effectively short-circuit
  1345. * output of the filesystem credentials form, returning that value instead.
  1346. *
  1347. * @since 2.5.0
  1348. * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
  1349. *
  1350. * @param mixed $output Form output to return instead. Default empty.
  1351. * @param string $form_post The URL to post the form to.
  1352. * @param string $type Chosen type of filesystem.
  1353. * @param bool $error Whether the current request has failed to connect.
  1354. * Default false.
  1355. * @param string $context Full path to the directory that is tested for
  1356. * being writable.
  1357. * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
  1358. * Default false.
  1359. * @param array $extra_fields Extra POST fields.
  1360. */
  1361. $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
  1362. if ( '' !== $req_cred )
  1363. return $req_cred;
  1364. if ( empty($type) ) {
  1365. $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
  1366. }
  1367. if ( 'direct' == $type )
  1368. return true;
  1369. if ( is_null( $extra_fields ) )
  1370. $extra_fields = array( 'version', 'locale' );
  1371. $credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => ''));
  1372. $submitted_form = wp_unslash( $_POST );
  1373. // Verify nonce, or unset submitted form field values on failure
  1374. if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) {
  1375. unset(
  1376. $submitted_form['hostname'],
  1377. $submitted_form['username'],
  1378. $submitted_form['password'],
  1379. $submitted_form['public_key'],
  1380. $submitted_form['private_key'],
  1381. $submitted_form['connection_type']
  1382. );
  1383. }
  1384. // 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)
  1385. $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($submitted_form['hostname']) ? $submitted_form['hostname'] : $credentials['hostname']);
  1386. $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($submitted_form['username']) ? $submitted_form['username'] : $credentials['username']);
  1387. $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($submitted_form['password']) ? $submitted_form['password'] : '');
  1388. // Check to see if we are setting the public/private keys for ssh
  1389. $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($submitted_form['public_key']) ? $submitted_form['public_key'] : '');
  1390. $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($submitted_form['private_key']) ? $submitted_form['private_key'] : '');
  1391. // Sanitize the hostname, Some people might pass in odd-data:
  1392. $credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off
  1393. if ( strpos($credentials['hostname'], ':') ) {
  1394. list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2);
  1395. if ( ! is_numeric($credentials['port']) )
  1396. unset($credentials['port']);
  1397. } else {
  1398. unset($credentials['port']);
  1399. }
  1400. if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) {
  1401. $credentials['connection_type'] = 'ssh';
  1402. } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { //Only the FTP Extension understands SSL
  1403. $credentials['connection_type'] = 'ftps';
  1404. } elseif ( ! empty( $submitted_form['connection_type'] ) ) {
  1405. $credentials['connection_type'] = $submitted_form['connection_type'];
  1406. } elseif ( ! isset( $credentials['connection_type'] ) ) { //All else fails (And it's not defaulted to something else saved), Default to FTP
  1407. $credentials['connection_type'] = 'ftp';
  1408. }
  1409. if ( ! $error &&
  1410. (
  1411. ( !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) ||
  1412. ( 'ssh' == $credentials['connection_type'] && !empty($credentials['public_key']) && !empty($credentials['private_key']) )
  1413. ) ) {
  1414. $stored_credentials = $credentials;
  1415. if ( !empty($stored_credentials['port']) ) //save port as part of hostname to simplify above code.
  1416. $stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
  1417. unset($stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key']);
  1418. if ( ! wp_installing() ) {
  1419. update_option( 'ftp_credentials', $stored_credentials );
  1420. }
  1421. return $credentials;
  1422. }
  1423. $hostname = isset( $credentials['hostname'] ) ? $credentials['hostname'] : '';
  1424. $username = isset( $credentials['username'] ) ? $credentials['username'] : '';
  1425. $public_key = isset( $credentials['public_key'] ) ? $credentials['public_key'] : '';
  1426. $private_key = isset( $credentials['private_key'] ) ? $credentials['private_key'] : '';
  1427. $port = isset( $credentials['port'] ) ? $credentials['port'] : '';
  1428. $connection_type = isset( $credentials['connection_type'] ) ? $credentials['connection_type'] : '';
  1429. if ( $error ) {
  1430. $error_string = __('<strong>ERROR:</strong> There was an error connecting to the server, Please verify the settings are correct.');
  1431. if ( is_wp_error($error) )
  1432. $error_string = esc_html( $error->get_error_message() );
  1433. echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
  1434. }
  1435. $types = array();
  1436. if ( extension_loaded('ftp') || extension_loaded('sockets') || function_exists('fsockopen') )
  1437. $types[ 'ftp' ] = __('FTP');
  1438. if ( extension_loaded('ftp') ) //Only this supports FTPS
  1439. $types[ 'ftps' ] = __('FTPS (SSL)');
  1440. if ( extension_loaded('ssh2') && function_exists('stream_get_contents') )
  1441. $types[ 'ssh' ] = __('SSH2');
  1442. /**
  1443. * Filters the connection types to output to the filesystem credentials form.
  1444. *
  1445. * @since 2.9.0
  1446. * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
  1447. *
  1448. * @param array $types Types of connections.
  1449. * @param array $credentials Credentials to connect with.
  1450. * @param string $type Chosen filesystem method.
  1451. * @param object $error Error object.
  1452. * @param string $context Full path to the directory that is tested
  1453. * for being writable.
  1454. */
  1455. $types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context );
  1456. ?>
  1457. <form action="<?php echo esc_url( $form_post ) ?>" method="post">
  1458. <div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form">
  1459. <?php
  1460. // Print a H1 heading in the FTP credentials modal dialog, default is a H2.
  1461. $heading_tag = 'h2';
  1462. if ( 'plugins.php' === $pagenow || 'plugin-install.php' === $pagenow ) {
  1463. $heading_tag = 'h1';
  1464. }
  1465. echo "<$heading_tag id='request-filesystem-credentials-title'>" . __( 'Connection Information' ) . "</$heading_tag>";
  1466. ?>
  1467. <p id="request-filesystem-credentials-desc"><?php
  1468. $label_user = __('Username');
  1469. $label_pass = __('Password');
  1470. _e('To perform the requested action, WordPress needs to access your web server.');
  1471. echo ' ';
  1472. if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) {
  1473. if ( isset( $types['ssh'] ) ) {
  1474. _e('Please enter your FTP or SSH credentials to proceed.');
  1475. $label_user = __('FTP/SSH Username');
  1476. $label_pass = __('FTP/SSH Password');
  1477. } else {
  1478. _e('Please enter your FTP credentials to proceed.');
  1479. $label_user = __('FTP Username');
  1480. $label_pass = __('FTP Password');
  1481. }
  1482. echo ' ';
  1483. }
  1484. _e('If you do not remember your credentials, you should contact your web host.');
  1485. ?></p>
  1486. <label for="hostname">
  1487. <span class="field-title"><?php _e( 'Hostname' ) ?></span>
  1488. <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 esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php disabled( defined('FTP_HOST') ); ?> />
  1489. </label>
  1490. <div class="ftp-username">
  1491. <label for="username">
  1492. <span class="field-title"><?php echo $label_user; ?></span>
  1493. <input name="username" type="text" id="username" value="<?php echo esc_attr($username) ?>"<?php disabled( defined('FTP_USER') ); ?> />
  1494. </label>
  1495. </div>
  1496. <div class="ftp-password">
  1497. <label for="password">
  1498. <span class="field-title"><?php echo $label_pass; ?></span>
  1499. <input name="password" type="password" id="password" value="<?php if ( defined('FTP_PASS') ) echo '*****'; ?>"<?php disabled( defined('FTP_PASS') ); ?> />
  1500. <em><?php if ( ! defined('FTP_PASS') ) _e( 'This password will not be stored on the server.' ); ?></em>
  1501. </label>
  1502. </div>
  1503. <fieldset>
  1504. <legend><?php _e( 'Connection Type' ); ?></legend>
  1505. <?php
  1506. $disabled = disabled( ( defined( 'FTP_SSL' ) && FTP_SSL ) || ( defined( 'FTP_SSH' ) && FTP_SSH ), true, false );
  1507. foreach ( $types as $name => $text ) : ?>
  1508. <label for="<?php echo esc_attr( $name ) ?>">
  1509. <input type="radio" name="connection_type" id="<?php echo esc_attr( $name ) ?>" value="<?php echo esc_attr( $name ) ?>"<?php checked( $name, $connection_type ); echo $disabled; ?> />
  1510. <?php echo $text; ?>
  1511. </label>
  1512. <?php
  1513. endforeach;
  1514. ?>
  1515. </fieldset>
  1516. <?php
  1517. if ( isset( $types['ssh'] ) ) {
  1518. $hidden_class = '';
  1519. if ( 'ssh' != $connection_type || empty( $connection_type ) ) {
  1520. $hidden_class = ' class="hidden"';
  1521. }
  1522. ?>
  1523. <fieldset id="ssh-keys"<?php echo $hidden_class; ?>>
  1524. <legend><?php _e( 'Authentication Keys' ); ?></legend>
  1525. <label for="public_key">
  1526. <span class="field-title"><?php _e('Public Key:') ?></span>
  1527. <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') ); ?> />
  1528. </label>
  1529. <label for="private_key">
  1530. <span class="field-title"><?php _e('Private Key:') ?></span>
  1531. <input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php disabled( defined('FTP_PRIKEY') ); ?> />
  1532. </label>
  1533. <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>
  1534. </fieldset>
  1535. <?php
  1536. }
  1537. foreach ( (array) $extra_fields as $field ) {
  1538. if ( isset( $submitted_form[ $field ] ) )
  1539. echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( $submitted_form[ $field ] ) . '" />';
  1540. }
  1541. ?>
  1542. <p class="request-filesystem-credentials-action-buttons">
  1543. <?php wp_nonce_field( 'filesystem-credentials', '_fs_nonce', false, true ); ?>
  1544. <button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button>
  1545. <?php submit_button( __( 'Proceed' ), '', 'upgrade', false ); ?>
  1546. </p>
  1547. </div>
  1548. </form>
  1549. <?php
  1550. return false;
  1551. }
  1552. /**
  1553. * Print the filesystem credentials modal when needed.
  1554. *
  1555. * @since 4.2.0
  1556. */
  1557. function wp_print_request_filesystem_credentials_modal() {
  1558. $filesystem_method = get_filesystem_method();
  1559. ob_start();
  1560. $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
  1561. ob_end_clean();
  1562. $request_filesystem_credentials = ( $filesystem_method != 'direct' && ! $filesystem_credentials_are_stored );
  1563. if ( ! $request_filesystem_credentials ) {
  1564. return;
  1565. }
  1566. ?>
  1567. <div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog">
  1568. <div class="notification-dialog-background"></div>
  1569. <div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0">
  1570. <div class="request-filesystem-credentials-dialog-content">
  1571. <?php request_filesystem_credentials( site_url() ); ?>
  1572. </div>
  1573. </div>
  1574. </div>
  1575. <?php
  1576. }