web/wp-admin/includes/file.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
     7  */
     7  */
     8 
     8 
     9 /** The descriptions for theme files. */
     9 /** The descriptions for theme files. */
    10 $wp_file_descriptions = array (
    10 $wp_file_descriptions = array(
    11 	'index.php' => __( 'Main Index Template' ),
    11 	'index.php' => __( 'Main Index Template' ),
    12 	'style.css' => __( 'Stylesheet' ),
    12 	'style.css' => __( 'Stylesheet' ),
       
    13 	'editor-style.css' => __( 'Visual Editor Stylesheet' ),
       
    14 	'editor-style-rtl.css' => __( 'Visual Editor RTL Stylesheet' ),
    13 	'rtl.css' => __( 'RTL Stylesheet' ),
    15 	'rtl.css' => __( 'RTL Stylesheet' ),
    14 	'comments.php' => __( 'Comments' ),
    16 	'comments.php' => __( 'Comments' ),
    15 	'comments-popup.php' => __( 'Popup Comments' ),
    17 	'comments-popup.php' => __( 'Popup Comments' ),
    16 	'footer.php' => __( 'Footer' ),
    18 	'footer.php' => __( 'Footer' ),
    17 	'header.php' => __( 'Header' ),
    19 	'header.php' => __( 'Header' ),
    18 	'sidebar.php' => __( 'Sidebar' ),
    20 	'sidebar.php' => __( 'Sidebar' ),
    19 	'archive.php' => __( 'Archives' ),
    21 	'archive.php' => __( 'Archives' ),
       
    22 	'author.php' => __( 'Author Template' ),
       
    23 	'tag.php' => __( 'Tag Template' ),
    20 	'category.php' => __( 'Category Template' ),
    24 	'category.php' => __( 'Category Template' ),
    21 	'page.php' => __( 'Page Template' ),
    25 	'page.php' => __( 'Page Template' ),
    22 	'search.php' => __( 'Search Results' ),
    26 	'search.php' => __( 'Search Results' ),
    23 	'searchform.php' => __( 'Search Form' ),
    27 	'searchform.php' => __( 'Search Form' ),
    24 	'single.php' => __( 'Single Post' ),
    28 	'single.php' => __( 'Single Post' ),
    31 	'audio.php' => __('Audio Attachment Template'),
    35 	'audio.php' => __('Audio Attachment Template'),
    32 	'application.php' => __('Application Attachment Template'),
    36 	'application.php' => __('Application Attachment Template'),
    33 	'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ),
    37 	'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ),
    34 	'.htaccess' => __( '.htaccess (for rewrite rules )' ),
    38 	'.htaccess' => __( '.htaccess (for rewrite rules )' ),
    35 	// Deprecated files
    39 	// Deprecated files
    36 	'wp-layout.css' => __( 'Stylesheet' ), 'wp-comments.php' => __( 'Comments Template' ), 'wp-comments-popup.php' => __( 'Popup Comments Template' ));
    40 	'wp-layout.css' => __( 'Stylesheet' ),
    37 
    41 	'wp-comments.php' => __( 'Comments Template' ),
    38 /**
    42 	'wp-comments-popup.php' => __( 'Popup Comments Template' ),
    39  * {@internal Missing Short Description}}
    43 );
    40  *
    44 
    41  * @since unknown
    45 /**
    42  *
    46  * Get the description for standard WordPress theme files and other various standard
    43  * @param unknown_type $file
    47  * WordPress files
    44  * @return unknown
    48  *
       
    49  * @since 1.5.0
       
    50  *
       
    51  * @uses _cleanup_header_comment
       
    52  * @uses $wp_file_descriptions
       
    53  * @param string $file Filesystem path or filename
       
    54  * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist
    45  */
    55  */
    46 function get_file_description( $file ) {
    56 function get_file_description( $file ) {
    47 	global $wp_file_descriptions;
    57 	global $wp_file_descriptions;
    48 
    58 
    49 	if ( isset( $wp_file_descriptions[basename( $file )] ) ) {
    59 	if ( isset( $wp_file_descriptions[basename( $file )] ) ) {
    50 		return $wp_file_descriptions[basename( $file )];
    60 		return $wp_file_descriptions[basename( $file )];
    51 	}
    61 	}
    52 	elseif ( file_exists( WP_CONTENT_DIR . $file ) && is_file( WP_CONTENT_DIR . $file ) ) {
    62 	elseif ( file_exists( $file ) && is_file( $file ) ) {
    53 		$template_data = implode( '', file( WP_CONTENT_DIR . $file ) );
    63 		$template_data = implode( '', file( $file ) );
    54 		if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ))
    64 		if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ))
    55 			return _cleanup_header_comment($name[1]) . ' Page Template';
    65 			return sprintf( __( '%s Page Template' ), _cleanup_header_comment($name[1]) );
    56 	}
    66 	}
    57 
    67 
    58 	return basename( $file );
    68 	return trim( basename( $file ) );
    59 }
    69 }
    60 
    70 
    61 /**
    71 /**
    62  * {@internal Missing Short Description}}
    72  * Get the absolute filesystem path to the root of the WordPress installation
    63  *
    73  *
    64  * @since unknown
    74  * @since 1.5.0
    65  *
    75  *
    66  * @return unknown
    76  * @uses get_option
       
    77  * @return string Full filesystem path to the root of the WordPress installation
    67  */
    78  */
    68 function get_home_path() {
    79 function get_home_path() {
    69 	$home = get_option( 'home' );
    80 	$home = get_option( 'home' );
    70 	$siteurl = get_option( 'siteurl' );
    81 	$siteurl = get_option( 'siteurl' );
    71 	if ( $home != '' && $home != $siteurl ) {
    82 	if ( $home != '' && $home != $siteurl ) {
    72 	        $wp_path_rel_to_home = str_replace($home, '', $siteurl); /* $siteurl - $home */
    83 		$wp_path_rel_to_home = str_replace($home, '', $siteurl); /* $siteurl - $home */
    73 	        $pos = strpos($_SERVER["SCRIPT_FILENAME"], $wp_path_rel_to_home);
    84 		$pos = strrpos($_SERVER["SCRIPT_FILENAME"], $wp_path_rel_to_home);
    74 	        $home_path = substr($_SERVER["SCRIPT_FILENAME"], 0, $pos);
    85 		$home_path = substr($_SERVER["SCRIPT_FILENAME"], 0, $pos);
    75 		$home_path = trailingslashit( $home_path );
    86 		$home_path = trailingslashit( $home_path );
    76 	} else {
    87 	} else {
    77 		$home_path = ABSPATH;
    88 		$home_path = ABSPATH;
    78 	}
    89 	}
    79 
    90 
    80 	return $home_path;
    91 	return $home_path;
    81 }
    92 }
    82 
    93 
    83 /**
    94 /**
    84  * {@internal Missing Short Description}}
    95  * Get the real file system path to a file to edit within the admin
    85  *
    96  *
    86  * @since unknown
    97  * If the $file is index.php or .htaccess this function will assume it is relative
    87  *
    98  * to the install root, otherwise it is assumed the file is relative to the wp-content
    88  * @param unknown_type $file
    99  * directory
    89  * @return unknown
   100  *
       
   101  * @since 1.5.0
       
   102  *
       
   103  * @uses get_home_path
       
   104  * @uses WP_CONTENT_DIR full filesystem path to the wp-content directory
       
   105  * @param string $file filesystem path relative to the WordPress install directory or to the wp-content directory
       
   106  * @return string full file system path to edit
    90  */
   107  */
    91 function get_real_file_to_edit( $file ) {
   108 function get_real_file_to_edit( $file ) {
    92 	if ('index.php' == $file || '.htaccess' == $file ) {
   109 	if ('index.php' == $file || '.htaccess' == $file ) {
    93 		$real_file = get_home_path() . $file;
   110 		$real_file = get_home_path() . $file;
    94 	} else {
   111 	} else {
   107  * @param string $folder Full path to folder
   124  * @param string $folder Full path to folder
   108  * @param int $levels (optional) Levels of folders to follow, Default: 100 (PHP Loop limit).
   125  * @param int $levels (optional) Levels of folders to follow, Default: 100 (PHP Loop limit).
   109  * @return bool|array False on failure, Else array of files
   126  * @return bool|array False on failure, Else array of files
   110  */
   127  */
   111 function list_files( $folder = '', $levels = 100 ) {
   128 function list_files( $folder = '', $levels = 100 ) {
   112 	if( empty($folder) )
   129 	if ( empty($folder) )
   113 		return false;
   130 		return false;
   114 
   131 
   115 	if( ! $levels )
   132 	if ( ! $levels )
   116 		return false;
   133 		return false;
   117 
   134 
   118 	$files = array();
   135 	$files = array();
   119 	if ( $dir = @opendir( $folder ) ) {
   136 	if ( $dir = @opendir( $folder ) ) {
   120 		while (($file = readdir( $dir ) ) !== false ) {
   137 		while (($file = readdir( $dir ) ) !== false ) {
   121 			if ( in_array($file, array('.', '..') ) )
   138 			if ( in_array($file, array('.', '..') ) )
   122 				continue;
   139 				continue;
   123 			if ( is_dir( $folder . '/' . $file ) ) {
   140 			if ( is_dir( $folder . '/' . $file ) ) {
   124 				$files2 = list_files( $folder . '/' . $file, $levels - 1);
   141 				$files2 = list_files( $folder . '/' . $file, $levels - 1);
   125 				if( $files2 )
   142 				if ( $files2 )
   126 					$files = array_merge($files, $files2 );
   143 					$files = array_merge($files, $files2 );
   127 				else
   144 				else
   128 					$files[] = $folder . '/' . $file . '/';
   145 					$files[] = $folder . '/' . $file . '/';
   129 			} else {
   146 			} else {
   130 				$files[] = $folder . '/' . $file;
   147 				$files[] = $folder . '/' . $file;
   134 	@closedir( $dir );
   151 	@closedir( $dir );
   135 	return $files;
   152 	return $files;
   136 }
   153 }
   137 
   154 
   138 /**
   155 /**
   139  * Determines a writable directory for temporary files.
       
   140  * Function's preference is to WP_CONTENT_DIR followed by the return value of <code>sys_get_temp_dir()</code>, before finally defaulting to /tmp/
       
   141  *
       
   142  * In the event that this function does not find a writable location, It may be overridden by the <code>WP_TEMP_DIR</code> constant in your <code>wp-config.php</code> file.
       
   143  *
       
   144  * @since 2.5.0
       
   145  *
       
   146  * @return string Writable temporary directory
       
   147  */
       
   148 function get_temp_dir() {
       
   149 	if ( defined('WP_TEMP_DIR') )
       
   150 		return trailingslashit(WP_TEMP_DIR);
       
   151 
       
   152 	$temp = WP_CONTENT_DIR . '/';
       
   153 	if ( is_dir($temp) && is_writable($temp) )
       
   154 		return $temp;
       
   155 
       
   156 	if  ( function_exists('sys_get_temp_dir') )
       
   157 		return trailingslashit(sys_get_temp_dir());
       
   158 
       
   159 	return '/tmp/';
       
   160 }
       
   161 
       
   162 /**
       
   163  * Returns a filename of a Temporary unique file.
   156  * Returns a filename of a Temporary unique file.
   164  * Please note that the calling function must unlink() this itself.
   157  * Please note that the calling function must unlink() this itself.
   165  *
   158  *
   166  * The filename is based off the passed parameter or defaults to the current unix timestamp,
   159  * The filename is based off the passed parameter or defaults to the current unix timestamp,
   167  * while the directory can either be passed as well, or by leaving  it blank, default to a writable temporary directory.
   160  * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
   168  *
   161  *
   169  * @since 2.6.0
   162  * @since 2.6.0
   170  *
   163  *
   171  * @param string $filename (optional) Filename to base the Unique file off
   164  * @param string $filename (optional) Filename to base the Unique file off
   172  * @param string $dir (optional) Directory to store the file in
   165  * @param string $dir (optional) Directory to store the file in
   173  * @return string a writable filename
   166  * @return string a writable filename
   174  */
   167  */
   175 function wp_tempnam($filename = '', $dir = ''){
   168 function wp_tempnam($filename = '', $dir = '') {
   176 	if ( empty($dir) )
   169 	if ( empty($dir) )
   177 		$dir = get_temp_dir();
   170 		$dir = get_temp_dir();
   178 	$filename = basename($filename);
   171 	$filename = basename($filename);
   179 	if ( empty($filename) )
   172 	if ( empty($filename) )
   180 		$filename = time();
   173 		$filename = time();
   184 	touch($filename);
   177 	touch($filename);
   185 	return $filename;
   178 	return $filename;
   186 }
   179 }
   187 
   180 
   188 /**
   181 /**
   189  * {@internal Missing Short Description}}
   182  * Make sure that the file that was requested to edit, is allowed to be edited
   190  *
   183  *
   191  * @since unknown
   184  * Function will die if if you are not allowed to edit the file
   192  *
   185  *
   193  * @param unknown_type $file
   186  * @since 1.5.0
   194  * @param unknown_type $allowed_files
   187  *
   195  * @return unknown
   188  * @uses wp_die
       
   189  * @uses validate_file
       
   190  * @param string $file file the users is attempting to edit
       
   191  * @param array $allowed_files Array of allowed files to edit, $file must match an entry exactly
       
   192  * @return null
   196  */
   193  */
   197 function validate_file_to_edit( $file, $allowed_files = '' ) {
   194 function validate_file_to_edit( $file, $allowed_files = '' ) {
   198 	$code = validate_file( $file, $allowed_files );
   195 	$code = validate_file( $file, $allowed_files );
   199 
   196 
   200 	if (!$code )
   197 	if (!$code )
   211 			wp_die( __('Sorry, that file cannot be edited.' ));
   208 			wp_die( __('Sorry, that file cannot be edited.' ));
   212 	}
   209 	}
   213 }
   210 }
   214 
   211 
   215 /**
   212 /**
   216  * {@internal Missing Short Description}}
   213  * Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type,
   217  *
   214  * and moving the file to the appropriate directory within the uploads directory.
   218  * @since unknown
   215  *
   219  *
   216  * @since 2.0
       
   217  *
       
   218  * @uses wp_handle_upload_error
       
   219  * @uses apply_filters
       
   220  * @uses is_multisite
       
   221  * @uses wp_check_filetype_and_ext
       
   222  * @uses current_user_can
       
   223  * @uses wp_upload_dir
       
   224  * @uses wp_unique_filename
       
   225  * @uses delete_transient
   220  * @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file.
   226  * @param array $file Reference to a single element of $_FILES. Call the function once for each uploaded file.
   221  * @param array $overrides Optional. An associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
   227  * @param array $overrides Optional. An associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
   222  * @return array On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
   228  * @return array On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
   223  */
   229  */
   224 function wp_handle_upload( &$file, $overrides = false, $time = null ) {
   230 function wp_handle_upload( &$file, $overrides = false, $time = null ) {
       
   231 	// The default error handler.
       
   232 	if ( ! function_exists( 'wp_handle_upload_error' ) ) {
       
   233 		function wp_handle_upload_error( &$file, $message ) {
       
   234 			return array( 'error'=>$message );
       
   235 		}
       
   236 	}
       
   237 
       
   238 	$file = apply_filters( 'wp_handle_upload_prefilter', $file );
       
   239 
       
   240 	// You may define your own function and pass the name in $overrides['upload_error_handler']
       
   241 	$upload_error_handler = 'wp_handle_upload_error';
       
   242 
       
   243 	// You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
       
   244 	if ( isset( $file['error'] ) && !is_numeric( $file['error'] ) && $file['error'] )
       
   245 		return $upload_error_handler( $file, $file['error'] );
       
   246 
       
   247 	// You may define your own function and pass the name in $overrides['unique_filename_callback']
       
   248 	$unique_filename_callback = null;
       
   249 
       
   250 	// $_POST['action'] must be set and its value must equal $overrides['action'] or this:
       
   251 	$action = 'wp_handle_upload';
       
   252 
       
   253 	// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
       
   254 	$upload_error_strings = array( false,
       
   255 		__( "The uploaded file exceeds the upload_max_filesize directive in php.ini." ),
       
   256 		__( "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form." ),
       
   257 		__( "The uploaded file was only partially uploaded." ),
       
   258 		__( "No file was uploaded." ),
       
   259 		'',
       
   260 		__( "Missing a temporary folder." ),
       
   261 		__( "Failed to write file to disk." ),
       
   262 		__( "File upload stopped by extension." ));
       
   263 
       
   264 	// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
       
   265 	$test_form = true;
       
   266 	$test_size = true;
       
   267 	$test_upload = true;
       
   268 
       
   269 	// If you override this, you must provide $ext and $type!!!!
       
   270 	$test_type = true;
       
   271 	$mimes = false;
       
   272 
       
   273 	// Install user overrides. Did we mention that this voids your warranty?
       
   274 	if ( is_array( $overrides ) )
       
   275 		extract( $overrides, EXTR_OVERWRITE );
       
   276 
       
   277 	// A correct form post will pass this test.
       
   278 	if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
       
   279 		return call_user_func($upload_error_handler, $file, __( 'Invalid form submission.' ));
       
   280 
       
   281 	// A successful upload will pass this test. It makes no sense to override this one.
       
   282 	if ( $file['error'] > 0 )
       
   283 		return call_user_func($upload_error_handler, $file, $upload_error_strings[$file['error']] );
       
   284 
       
   285 	// A non-empty file will pass this test.
       
   286 	if ( $test_size && !($file['size'] > 0 ) ) {
       
   287 		if ( is_multisite() )
       
   288 			$error_msg = __( 'File is empty. Please upload something more substantial.' );
       
   289 		else
       
   290 			$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.' );
       
   291 		return call_user_func($upload_error_handler, $file, $error_msg);
       
   292 	}
       
   293 
       
   294 	// A properly uploaded file will pass this test. There should be no reason to override this one.
       
   295 	if ( $test_upload && ! @ is_uploaded_file( $file['tmp_name'] ) )
       
   296 		return call_user_func($upload_error_handler, $file, __( 'Specified file failed upload test.' ));
       
   297 
       
   298 	// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
       
   299 	if ( $test_type ) {
       
   300 		$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
       
   301 
       
   302 		extract( $wp_filetype );
       
   303 
       
   304 		// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
       
   305 		if ( $proper_filename )
       
   306 			$file['name'] = $proper_filename;
       
   307 
       
   308 		if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
       
   309 			return call_user_func($upload_error_handler, $file, __( 'Sorry, this file type is not permitted for security reasons.' ));
       
   310 
       
   311 		if ( !$ext )
       
   312 			$ext = ltrim(strrchr($file['name'], '.'), '.');
       
   313 
       
   314 		if ( !$type )
       
   315 			$type = $file['type'];
       
   316 	} else {
       
   317 		$type = '';
       
   318 	}
       
   319 
       
   320 	// A writable uploads dir will pass this test. Again, there's no point overriding this one.
       
   321 	if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
       
   322 		return call_user_func($upload_error_handler, $file, $uploads['error'] );
       
   323 
       
   324 	$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
       
   325 
       
   326 	// Move the file to the uploads dir
       
   327 	$new_file = $uploads['path'] . "/$filename";
       
   328 	if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
       
   329 		return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
       
   330 
       
   331 	// Set correct file permissions
       
   332 	$stat = stat( dirname( $new_file ));
       
   333 	$perms = $stat['mode'] & 0000666;
       
   334 	@ chmod( $new_file, $perms );
       
   335 
       
   336 	// Compute the URL
       
   337 	$url = $uploads['url'] . "/$filename";
       
   338 
       
   339 	if ( is_multisite() )
       
   340 		delete_transient( 'dirsize_cache' );
       
   341 
       
   342 	return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'upload' );
       
   343 }
       
   344 
       
   345 /**
       
   346  * Handle sideloads, which is the process of retrieving a media item from another server instead of
       
   347  * a traditional media upload. This process involves sanitizing the filename, checking extensions
       
   348  * for mime type, and moving the file to the appropriate directory within the uploads directory.
       
   349  *
       
   350  * @since 2.6.0
       
   351  *
       
   352  * @uses wp_handle_upload_error
       
   353  * @uses apply_filters
       
   354  * @uses wp_check_filetype_and_ext
       
   355  * @uses current_user_can
       
   356  * @uses wp_upload_dir
       
   357  * @uses wp_unique_filename
       
   358  * @param array $file an array similar to that of a PHP $_FILES POST array
       
   359  * @param array $overrides Optional. An associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
       
   360  * @return array On success, returns an associative array of file attributes. On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
       
   361  */
       
   362 function wp_handle_sideload( &$file, $overrides = false ) {
   225 	// The default error handler.
   363 	// The default error handler.
   226 	if (! function_exists( 'wp_handle_upload_error' ) ) {
   364 	if (! function_exists( 'wp_handle_upload_error' ) ) {
   227 		function wp_handle_upload_error( &$file, $message ) {
   365 		function wp_handle_upload_error( &$file, $message ) {
   228 			return array( 'error'=>$message );
   366 			return array( 'error'=>$message );
   229 		}
   367 		}
   230 	}
   368 	}
   231 
   369 
   232 	$file = apply_filters( 'wp_handle_upload_prefilter', $file );
       
   233 
       
   234 	// You may define your own function and pass the name in $overrides['upload_error_handler']
   370 	// You may define your own function and pass the name in $overrides['upload_error_handler']
   235 	$upload_error_handler = 'wp_handle_upload_error';
   371 	$upload_error_handler = 'wp_handle_upload_error';
   236 
   372 
   237 	// You may have had one or more 'wp_handle_upload_prefilter' functions error out the file.  Handle that gracefully.
       
   238 	if ( isset( $file['error'] ) && !is_numeric( $file['error'] ) && $file['error'] )
       
   239 		return $upload_error_handler( $file, $file['error'] );
       
   240 
       
   241 	// You may define your own function and pass the name in $overrides['unique_filename_callback']
   373 	// You may define your own function and pass the name in $overrides['unique_filename_callback']
   242 	$unique_filename_callback = null;
   374 	$unique_filename_callback = null;
   243 
   375 
   244 	// $_POST['action'] must be set and its value must equal $overrides['action'] or this:
   376 	// $_POST['action'] must be set and its value must equal $overrides['action'] or this:
   245 	$action = 'wp_handle_upload';
   377 	$action = 'wp_handle_sideload';
   246 
   378 
   247 	// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
   379 	// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
   248 	$upload_error_strings = array( false,
   380 	$upload_error_strings = array( false,
   249 		__( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
   381 		__( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
   250 		__( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
   382 		__( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
   253 		'',
   385 		'',
   254 		__( "Missing a temporary folder." ),
   386 		__( "Missing a temporary folder." ),
   255 		__( "Failed to write file to disk." ),
   387 		__( "Failed to write file to disk." ),
   256 		__( "File upload stopped by extension." ));
   388 		__( "File upload stopped by extension." ));
   257 
   389 
   258 	// All tests are on by default. Most can be turned off by $override[{test_name}] = false;
   390 	// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
   259 	$test_form = true;
   391 	$test_form = true;
   260 	$test_size = true;
   392 	$test_size = true;
   261 
   393 
   262 	// If you override this, you must provide $ext and $type!!!!
   394 	// If you override this, you must provide $ext and $type!!!!
   263 	$test_type = true;
   395 	$test_type = true;
   270 	// A correct form post will pass this test.
   402 	// A correct form post will pass this test.
   271 	if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
   403 	if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
   272 		return $upload_error_handler( $file, __( 'Invalid form submission.' ));
   404 		return $upload_error_handler( $file, __( 'Invalid form submission.' ));
   273 
   405 
   274 	// A successful upload will pass this test. It makes no sense to override this one.
   406 	// A successful upload will pass this test. It makes no sense to override this one.
   275 	if ( $file['error'] > 0 )
   407 	if ( ! empty( $file['error'] ) )
   276 		return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
   408 		return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
   277 
   409 
   278 	// A non-empty file will pass this test.
   410 	// A non-empty file will pass this test.
   279 	if ( $test_size && !($file['size'] > 0 ) )
   411 	if ( $test_size && !(filesize($file['tmp_name']) > 0 ) )
   280 		return $upload_error_handler( $file, __( '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.' ));
   412 		return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
   281 
   413 
   282 	// A properly uploaded file will pass this test. There should be no reason to override this one.
   414 	// A properly uploaded file will pass this test. There should be no reason to override this one.
   283 	if (! @ is_uploaded_file( $file['tmp_name'] ) )
   415 	if (! @ is_file( $file['tmp_name'] ) )
   284 		return $upload_error_handler( $file, __( 'Specified file failed upload test.' ));
   416 		return $upload_error_handler( $file, __( 'Specified file does not exist.' ));
   285 
   417 
   286 	// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
   418 	// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
   287 	if ( $test_type ) {
   419 	if ( $test_type ) {
   288 		$wp_filetype = wp_check_filetype( $file['name'], $mimes );
   420 		$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
   289 
   421 
   290 		extract( $wp_filetype );
   422 		extract( $wp_filetype );
   291 
   423 
       
   424 		// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
       
   425 		if ( $proper_filename )
       
   426 			$file['name'] = $proper_filename;
       
   427 
   292 		if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
   428 		if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
   293 			return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
   429 			return $upload_error_handler( $file, __( 'Sorry, this file type is not permitted for security reasons.' ));
   294 
   430 
   295 		if ( !$ext )
   431 		if ( !$ext )
   296 			$ext = ltrim(strrchr($file['name'], '.'), '.');
   432 			$ext = ltrim(strrchr($file['name'], '.'), '.');
   297 
   433 
   298 		if ( !$type )
   434 		if ( !$type )
   299 			$type = $file['type'];
   435 			$type = $file['type'];
   300 	} else {
       
   301 		$type = '';
       
   302 	}
   436 	}
   303 
   437 
   304 	// A writable uploads dir will pass this test. Again, there's no point overriding this one.
   438 	// A writable uploads dir will pass this test. Again, there's no point overriding this one.
   305 	if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
   439 	if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
   306 		return $upload_error_handler( $file, $uploads['error'] );
   440 		return $upload_error_handler( $file, $uploads['error'] );
   307 
   441 
   308 	$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
   442 	$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
       
   443 
       
   444 	// Strip the query strings.
       
   445 	$filename = str_replace('?','-', $filename);
       
   446 	$filename = str_replace('&','-', $filename);
   309 
   447 
   310 	// Move the file to the uploads dir
   448 	// Move the file to the uploads dir
   311 	$new_file = $uploads['path'] . "/$filename";
   449 	$new_file = $uploads['path'] . "/$filename";
   312 	if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) {
   450 	if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
   313 		return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
   451 		return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
   314 	}
   452 	}
   315 
   453 
   316 	// Set correct file permissions
   454 	// Set correct file permissions
   317 	$stat = stat( dirname( $new_file ));
   455 	$stat = stat( dirname( $new_file ));
   319 	@ chmod( $new_file, $perms );
   457 	@ chmod( $new_file, $perms );
   320 
   458 
   321 	// Compute the URL
   459 	// Compute the URL
   322 	$url = $uploads['url'] . "/$filename";
   460 	$url = $uploads['url'] . "/$filename";
   323 
   461 
   324 	return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
   462 	$return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'sideload' );
   325 }
       
   326 
       
   327 /**
       
   328  * {@internal Missing Short Description}}
       
   329  *
       
   330  * Pass this function an array similar to that of a $_FILES POST array.
       
   331  *
       
   332  * @since unknown
       
   333  *
       
   334  * @param unknown_type $file
       
   335  * @param unknown_type $overrides
       
   336  * @return unknown
       
   337  */
       
   338 function wp_handle_sideload( &$file, $overrides = false ) {
       
   339 	// The default error handler.
       
   340 	if (! function_exists( 'wp_handle_upload_error' ) ) {
       
   341 		function wp_handle_upload_error( &$file, $message ) {
       
   342 			return array( 'error'=>$message );
       
   343 		}
       
   344 	}
       
   345 
       
   346 	// You may define your own function and pass the name in $overrides['upload_error_handler']
       
   347 	$upload_error_handler = 'wp_handle_upload_error';
       
   348 
       
   349 	// You may define your own function and pass the name in $overrides['unique_filename_callback']
       
   350 	$unique_filename_callback = null;
       
   351 
       
   352 	// $_POST['action'] must be set and its value must equal $overrides['action'] or this:
       
   353 	$action = 'wp_handle_sideload';
       
   354 
       
   355 	// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
       
   356 	$upload_error_strings = array( false,
       
   357 		__( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
       
   358 		__( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
       
   359 		__( "The uploaded file was only partially uploaded." ),
       
   360 		__( "No file was uploaded." ),
       
   361 		'',
       
   362 		__( "Missing a temporary folder." ),
       
   363 		__( "Failed to write file to disk." ),
       
   364 		__( "File upload stopped by extension." ));
       
   365 
       
   366 	// All tests are on by default. Most can be turned off by $override[{test_name}] = false;
       
   367 	$test_form = true;
       
   368 	$test_size = true;
       
   369 
       
   370 	// If you override this, you must provide $ext and $type!!!!
       
   371 	$test_type = true;
       
   372 	$mimes = false;
       
   373 
       
   374 	// Install user overrides. Did we mention that this voids your warranty?
       
   375 	if ( is_array( $overrides ) )
       
   376 		extract( $overrides, EXTR_OVERWRITE );
       
   377 
       
   378 	// A correct form post will pass this test.
       
   379 	if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
       
   380 		return $upload_error_handler( $file, __( 'Invalid form submission.' ));
       
   381 
       
   382 	// A successful upload will pass this test. It makes no sense to override this one.
       
   383 	if ( $file['error'] > 0 )
       
   384 		return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
       
   385 
       
   386 	// A non-empty file will pass this test.
       
   387 	if ( $test_size && !(filesize($file['tmp_name']) > 0 ) )
       
   388 		return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
       
   389 
       
   390 	// A properly uploaded file will pass this test. There should be no reason to override this one.
       
   391 	if (! @ is_file( $file['tmp_name'] ) )
       
   392 		return $upload_error_handler( $file, __( 'Specified file does not exist.' ));
       
   393 
       
   394 	// A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
       
   395 	if ( $test_type ) {
       
   396 		$wp_filetype = wp_check_filetype( $file['name'], $mimes );
       
   397 
       
   398 		extract( $wp_filetype );
       
   399 
       
   400 		if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
       
   401 			return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
       
   402 
       
   403 		if ( !$ext )
       
   404 			$ext = ltrim(strrchr($file['name'], '.'), '.');
       
   405 
       
   406 		if ( !$type )
       
   407 			$type = $file['type'];
       
   408 	}
       
   409 
       
   410 	// A writable uploads dir will pass this test. Again, there's no point overriding this one.
       
   411 	if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
       
   412 		return $upload_error_handler( $file, $uploads['error'] );
       
   413 
       
   414 	$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
       
   415 
       
   416 	// Strip the query strings.
       
   417 	$filename = str_replace('?','-', $filename);
       
   418 	$filename = str_replace('&','-', $filename);
       
   419 
       
   420 	// Move the file to the uploads dir
       
   421 	$new_file = $uploads['path'] . "/$filename";
       
   422 	if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
       
   423 		return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
       
   424 	}
       
   425 
       
   426 	// Set correct file permissions
       
   427 	$stat = stat( dirname( $new_file ));
       
   428 	$perms = $stat['mode'] & 0000666;
       
   429 	@ chmod( $new_file, $perms );
       
   430 
       
   431 	// Compute the URL
       
   432 	$url = $uploads['url'] . "/$filename";
       
   433 
       
   434 	$return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
       
   435 
   463 
   436 	return $return;
   464 	return $return;
   437 }
   465 }
   438 
   466 
   439 /**
   467 /**
   440  * Downloads a url to a local temporary file using the WordPress HTTP Class.
   468  * Downloads a url to a local temporary file using the WordPress HTTP Class.
   441  * Please note, That the calling function must unlink() the  file.
   469  * Please note, That the calling function must unlink() the file.
   442  *
   470  *
   443  * @since 2.5.0
   471  * @since 2.5.0
   444  *
   472  *
   445  * @param string $url the URL of the file to download
   473  * @param string $url the URL of the file to download
       
   474  * @param int $timeout The timeout for the request to download the file default 300 seconds
   446  * @return mixed WP_Error on failure, string Filename on success.
   475  * @return mixed WP_Error on failure, string Filename on success.
   447  */
   476  */
   448 function download_url( $url ) {
   477 function download_url( $url, $timeout = 300 ) {
   449 	//WARNING: The file is not automatically deleted, The script must unlink() the file.
   478 	//WARNING: The file is not automatically deleted, The script must unlink() the file.
   450 	if ( ! $url )
   479 	if ( ! $url )
   451 		return new WP_Error('http_no_url', __('Invalid URL Provided'));
   480 		return new WP_Error('http_no_url', __('Invalid URL Provided.'));
   452 
   481 
   453 	$tmpfname = wp_tempnam($url);
   482 	$tmpfname = wp_tempnam($url);
   454 	if ( ! $tmpfname )
   483 	if ( ! $tmpfname )
   455 		return new WP_Error('http_no_file', __('Could not create Temporary file'));
   484 		return new WP_Error('http_no_file', __('Could not create Temporary file.'));
   456 
   485 
   457 	$handle = @fopen($tmpfname, 'wb');
   486 	$response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
   458 	if ( ! $handle )
   487 
   459 		return new WP_Error('http_no_file', __('Could not create Temporary file'));
   488 	if ( is_wp_error( $response ) ) {
   460 
   489 		unlink( $tmpfname );
   461 	$response = wp_remote_get($url, array('timeout' => 300));
       
   462 
       
   463 	if ( is_wp_error($response) ) {
       
   464 		fclose($handle);
       
   465 		unlink($tmpfname);
       
   466 		return $response;
   490 		return $response;
   467 	}
   491 	}
   468 
   492 
   469 	if ( $response['response']['code'] != '200' ){
   493 	if ( 200 != wp_remote_retrieve_response_code( $response ) ){
   470 		fclose($handle);
   494 		unlink( $tmpfname );
   471 		unlink($tmpfname);
   495 		return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
   472 		return new WP_Error('http_404', trim($response['response']['message']));
   496 	}
   473 	}
       
   474 
       
   475 	fwrite($handle, $response['body']);
       
   476 	fclose($handle);
       
   477 
   497 
   478 	return $tmpfname;
   498 	return $tmpfname;
   479 }
   499 }
   480 
   500 
   481 /**
   501 /**
   482  * Unzip's a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
   502  * Unzips a specified ZIP file to a location on the Filesystem via the WordPress Filesystem Abstraction.
   483  * Assumes that WP_Filesystem() has already been called and set up.
   503  * Assumes that WP_Filesystem() has already been called and set up. Does not extract a root-level __MACOSX directory, if present.
   484  *
   504  *
   485  * Attempts to increase the PHP Memory limit to 256M before uncompressing,
   505  * Attempts to increase the PHP Memory limit to 256M before uncompressing,
   486  * However, The most memory required shouldn't be much larger than the Archive itself.
   506  * However, The most memory required shouldn't be much larger than the Archive itself.
   487  *
   507  *
   488  * @since 2.5.0
   508  * @since 2.5.0
   495 	global $wp_filesystem;
   515 	global $wp_filesystem;
   496 
   516 
   497 	if ( ! $wp_filesystem || !is_object($wp_filesystem) )
   517 	if ( ! $wp_filesystem || !is_object($wp_filesystem) )
   498 		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
   518 		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
   499 
   519 
   500 	// Unzip uses a lot of memory, but not this much hopefully
   520 	// Unzip can use a lot of memory, but not this much hopefully
   501 	@ini_set('memory_limit', '256M');
   521 	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
   502 
   522 
   503 	$fs =& $wp_filesystem;
   523 	$needed_dirs = array();
   504 
       
   505 	require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
       
   506 
       
   507 	$archive = new PclZip($file);
       
   508 
       
   509 	// Is the archive valid?
       
   510 	if ( false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING)) )
       
   511 		return new WP_Error('incompatible_archive', __('Incompatible archive'), $archive->errorInfo(true));
       
   512 
       
   513 	if ( 0 == count($archive_files) )
       
   514 		return new WP_Error('empty_archive', __('Empty archive'));
       
   515 
       
   516 	$path = explode('/', untrailingslashit($to));
       
   517 	for ( $i = count($path); $i > 0; $i-- ) { //>0 = first element is empty allways for paths starting with '/'
       
   518 		$tmppath = implode('/', array_slice($path, 0, $i) );
       
   519 		if ( $fs->is_dir($tmppath) ) { //Found the highest folder that exists, Create from here(ie +1)
       
   520 			for ( $i = $i + 1; $i <= count($path); $i++ ) {
       
   521 				$tmppath = implode('/', array_slice($path, 0, $i) );
       
   522 				if ( ! $fs->mkdir($tmppath, FS_CHMOD_DIR) )
       
   523 					return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath);
       
   524 			}
       
   525 			break; //Exit main for loop
       
   526 		}
       
   527 	}
       
   528 
       
   529 	$to = trailingslashit($to);
   524 	$to = trailingslashit($to);
   530 	foreach ($archive_files as $file) {
   525 
   531 		$path = $file['folder'] ? $file['filename'] : dirname($file['filename']);
   526 	// Determine any parent dir's needed (of the upgrade directory)
   532 		$path = explode('/', $path);
   527 	if ( ! $wp_filesystem->is_dir($to) ) { //Only do parents if no children exist
   533 		for ( $i = count($path); $i >= 0; $i-- ) { //>=0 as the first element contains data
   528 		$path = preg_split('![/\\\]!', untrailingslashit($to));
       
   529 		for ( $i = count($path); $i >= 0; $i-- ) {
   534 			if ( empty($path[$i]) )
   530 			if ( empty($path[$i]) )
   535 				continue;
   531 				continue;
   536 			$tmppath = $to . implode('/', array_slice($path, 0, $i) );
   532 
   537 			if ( $fs->is_dir($tmppath) ) {//Found the highest folder that exists, Create from here
   533 			$dir = implode('/', array_slice($path, 0, $i+1) );
   538 				for ( $i = $i + 1; $i <= count($path); $i++ ) { //< count() no file component please.
   534 			if ( preg_match('!^[a-z]:$!i', $dir) ) // Skip it if it looks like a Windows Drive letter.
   539 					$tmppath = $to . implode('/', array_slice($path, 0, $i) );
   535 				continue;
   540 					if ( ! $fs->is_dir($tmppath) && ! $fs->mkdir($tmppath, FS_CHMOD_DIR) )
   536 
   541 						return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath);
   537 			if ( ! $wp_filesystem->is_dir($dir) )
   542 				}
   538 				$needed_dirs[] = $dir;
   543 				break; //Exit main for loop
   539 			else
   544 			}
   540 				break; // A folder exists, therefor, we dont need the check the levels below this
   545 		}
   541 		}
   546 
   542 	}
   547 		// We've made sure the folders are there, so let's extract the file now:
   543 
   548 		if ( ! $file['folder'] ) {
   544 	if ( class_exists('ZipArchive') && apply_filters('unzip_file_use_ziparchive', true ) ) {
   549 			if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
   545 		$result = _unzip_file_ziparchive($file, $to, $needed_dirs);
   550 				return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']);
   546 		if ( true === $result ) {
   551 			$fs->chmod($to . $file['filename'], FS_CHMOD_FILE);
   547 			return $result;
       
   548 		} elseif ( is_wp_error($result) ) {
       
   549 			if ( 'incompatible_archive' != $result->get_error_code() )
       
   550 				return $result;
   552 		}
   551 		}
       
   552 	}
       
   553 	// Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
       
   554 	return _unzip_file_pclzip($file, $to, $needed_dirs);
       
   555 }
       
   556 
       
   557 /**
       
   558  * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the ZipArchive class.
       
   559  * Assumes that WP_Filesystem() has already been called and set up.
       
   560  *
       
   561  * @since 3.0.0
       
   562  * @see unzip_file
       
   563  * @access private
       
   564  *
       
   565  * @param string $file Full path and filename of zip archive
       
   566  * @param string $to Full path on the filesystem to extract archive to
       
   567  * @param array $needed_dirs A partial list of required folders needed to be created.
       
   568  * @return mixed WP_Error on failure, True on success
       
   569  */
       
   570 function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) {
       
   571 	global $wp_filesystem;
       
   572 
       
   573 	$z = new ZipArchive();
       
   574 
       
   575 	// PHP4-compat - php4 classes can't contain constants
       
   576 	$zopen = $z->open($file, /* ZIPARCHIVE::CHECKCONS */ 4);
       
   577 	if ( true !== $zopen )
       
   578 		return new WP_Error('incompatible_archive', __('Incompatible Archive.'));
       
   579 
       
   580 	for ( $i = 0; $i < $z->numFiles; $i++ ) {
       
   581 		if ( ! $info = $z->statIndex($i) )
       
   582 			return new WP_Error('stat_failed', __('Could not retrieve file from archive.'));
       
   583 
       
   584 		if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Skip the OS X-created __MACOSX directory
       
   585 			continue;
       
   586 
       
   587 		if ( '/' == substr($info['name'], -1) ) // directory
       
   588 			$needed_dirs[] = $to . untrailingslashit($info['name']);
       
   589 		else
       
   590 			$needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
       
   591 	}
       
   592 
       
   593 	$needed_dirs = array_unique($needed_dirs);
       
   594 	foreach ( $needed_dirs as $dir ) {
       
   595 		// Check the parent folders of the folders all exist within the creation array.
       
   596 		if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist)
       
   597 			continue;
       
   598 		if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it
       
   599 			continue;
       
   600 
       
   601 		$parent_folder = dirname($dir);
       
   602 		while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) {
       
   603 			$needed_dirs[] = $parent_folder;
       
   604 			$parent_folder = dirname($parent_folder);
       
   605 		}
       
   606 	}
       
   607 	asort($needed_dirs);
       
   608 
       
   609 	// Create those directories if need be:
       
   610 	foreach ( $needed_dirs as $_dir ) {
       
   611 		if ( ! $wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && ! $wp_filesystem->is_dir($_dir) ) // Only check to see if the Dir exists upon creation failure. Less I/O this way.
       
   612 			return new WP_Error('mkdir_failed', __('Could not create directory.'), $_dir);
       
   613 	}
       
   614 	unset($needed_dirs);
       
   615 
       
   616 	for ( $i = 0; $i < $z->numFiles; $i++ ) {
       
   617 		if ( ! $info = $z->statIndex($i) )
       
   618 			return new WP_Error('stat_failed', __('Could not retrieve file from archive.'));
       
   619 
       
   620 		if ( '/' == substr($info['name'], -1) ) // directory
       
   621 			continue;
       
   622 
       
   623 		if ( '__MACOSX/' === substr($info['name'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files
       
   624 			continue;
       
   625 
       
   626 		$contents = $z->getFromIndex($i);
       
   627 		if ( false === $contents )
       
   628 			return new WP_Error('extract_failed', __('Could not extract file from archive.'), $info['name']);
       
   629 
       
   630 		if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE) )
       
   631 			return new WP_Error('copy_failed', __('Could not copy file.'), $to . $info['name']);
       
   632 	}
       
   633 
       
   634 	$z->close();
       
   635 
       
   636 	return true;
       
   637 }
       
   638 
       
   639 /**
       
   640  * This function should not be called directly, use unzip_file instead. Attempts to unzip an archive using the PclZip library.
       
   641  * Assumes that WP_Filesystem() has already been called and set up.
       
   642  *
       
   643  * @since 3.0.0
       
   644  * @see unzip_file
       
   645  * @access private
       
   646  *
       
   647  * @param string $file Full path and filename of zip archive
       
   648  * @param string $to Full path on the filesystem to extract archive to
       
   649  * @param array $needed_dirs A partial list of required folders needed to be created.
       
   650  * @return mixed WP_Error on failure, True on success
       
   651  */
       
   652 function _unzip_file_pclzip($file, $to, $needed_dirs = array()) {
       
   653 	global $wp_filesystem;
       
   654 
       
   655 	// See #15789 - PclZip uses string functions on binary data, If it's overloaded with Multibyte safe functions the results are incorrect.
       
   656 	if ( ini_get('mbstring.func_overload') && function_exists('mb_internal_encoding') ) {
       
   657 		$previous_encoding = mb_internal_encoding();
       
   658 		mb_internal_encoding('ISO-8859-1');
       
   659 	}
       
   660 
       
   661 	require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
       
   662 
       
   663 	$archive = new PclZip($file);
       
   664 
       
   665 	$archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING);
       
   666 
       
   667 	if ( isset($previous_encoding) )
       
   668 		mb_internal_encoding($previous_encoding);
       
   669 
       
   670 	// Is the archive valid?
       
   671 	if ( !is_array($archive_files) )
       
   672 		return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true));
       
   673 
       
   674 	if ( 0 == count($archive_files) )
       
   675 		return new WP_Error('empty_archive', __('Empty archive.'));
       
   676 
       
   677 	// Determine any children directories needed (From within the archive)
       
   678 	foreach ( $archive_files as $file ) {
       
   679 		if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Skip the OS X-created __MACOSX directory
       
   680 			continue;
       
   681 
       
   682 		$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) );
       
   683 	}
       
   684 
       
   685 	$needed_dirs = array_unique($needed_dirs);
       
   686 	foreach ( $needed_dirs as $dir ) {
       
   687 		// Check the parent folders of the folders all exist within the creation array.
       
   688 		if ( untrailingslashit($to) == $dir ) // Skip over the working directory, We know this exists (or will exist)
       
   689 			continue;
       
   690 		if ( strpos($dir, $to) === false ) // If the directory is not within the working directory, Skip it
       
   691 			continue;
       
   692 
       
   693 		$parent_folder = dirname($dir);
       
   694 		while ( !empty($parent_folder) && untrailingslashit($to) != $parent_folder && !in_array($parent_folder, $needed_dirs) ) {
       
   695 			$needed_dirs[] = $parent_folder;
       
   696 			$parent_folder = dirname($parent_folder);
       
   697 		}
       
   698 	}
       
   699 	asort($needed_dirs);
       
   700 
       
   701 	// Create those directories if need be:
       
   702 	foreach ( $needed_dirs as $_dir ) {
       
   703 		if ( ! $wp_filesystem->mkdir($_dir, FS_CHMOD_DIR) && ! $wp_filesystem->is_dir($_dir) ) // Only check to see if the dir exists upon creation failure. Less I/O this way.
       
   704 			return new WP_Error('mkdir_failed', __('Could not create directory.'), $_dir);
       
   705 	}
       
   706 	unset($needed_dirs);
       
   707 
       
   708 	// Extract the files from the zip
       
   709 	foreach ( $archive_files as $file ) {
       
   710 		if ( $file['folder'] )
       
   711 			continue;
       
   712 
       
   713 		if ( '__MACOSX/' === substr($file['filename'], 0, 9) ) // Don't extract the OS X-created __MACOSX directory files
       
   714 			continue;
       
   715 
       
   716 		if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE) )
       
   717 			return new WP_Error('copy_failed', __('Could not copy file.'), $to . $file['filename']);
   553 	}
   718 	}
   554 	return true;
   719 	return true;
   555 }
   720 }
   556 
   721 
   557 /**
   722 /**
   560  *
   725  *
   561  * @since 2.5.0
   726  * @since 2.5.0
   562  *
   727  *
   563  * @param string $from source directory
   728  * @param string $from source directory
   564  * @param string $to destination directory
   729  * @param string $to destination directory
       
   730  * @param array $skip_list a list of files/folders to skip copying
   565  * @return mixed WP_Error on failure, True on success.
   731  * @return mixed WP_Error on failure, True on success.
   566  */
   732  */
   567 function copy_dir($from, $to) {
   733 function copy_dir($from, $to, $skip_list = array() ) {
   568 	global $wp_filesystem;
   734 	global $wp_filesystem;
   569 
   735 
   570 	$dirlist = $wp_filesystem->dirlist($from);
   736 	$dirlist = $wp_filesystem->dirlist($from);
   571 
   737 
   572 	$from = trailingslashit($from);
   738 	$from = trailingslashit($from);
   573 	$to = trailingslashit($to);
   739 	$to = trailingslashit($to);
   574 
   740 
       
   741 	$skip_regex = '';
       
   742 	foreach ( (array)$skip_list as $key => $skip_file )
       
   743 		$skip_regex .= preg_quote($skip_file, '!') . '|';
       
   744 
       
   745 	if ( !empty($skip_regex) )
       
   746 		$skip_regex = '!(' . rtrim($skip_regex, '|') . ')$!i';
       
   747 
   575 	foreach ( (array) $dirlist as $filename => $fileinfo ) {
   748 	foreach ( (array) $dirlist as $filename => $fileinfo ) {
       
   749 		if ( !empty($skip_regex) )
       
   750 			if ( preg_match($skip_regex, $from . $filename) )
       
   751 				continue;
       
   752 
   576 		if ( 'f' == $fileinfo['type'] ) {
   753 		if ( 'f' == $fileinfo['type'] ) {
   577 			if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) ) {
   754 			if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
   578 				// If copy failed, chmod file to 0644 and try again.
   755 				// If copy failed, chmod file to 0644 and try again.
   579 				$wp_filesystem->chmod($to . $filename, 0644);
   756 				$wp_filesystem->chmod($to . $filename, 0644);
   580 				if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
   757 				if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) )
   581 					return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
   758 					return new WP_Error('copy_failed', __('Could not copy file.'), $to . $filename);
   582 			}
   759 			}
   583 			$wp_filesystem->chmod($to . $filename, FS_CHMOD_FILE);
       
   584 		} elseif ( 'd' == $fileinfo['type'] ) {
   760 		} elseif ( 'd' == $fileinfo['type'] ) {
   585 			if ( !$wp_filesystem->is_dir($to . $filename) ) {
   761 			if ( !$wp_filesystem->is_dir($to . $filename) ) {
   586 				if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
   762 				if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
   587 					return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
   763 					return new WP_Error('mkdir_failed', __('Could not create directory.'), $to . $filename);
   588 			}
   764 			}
   589 			$result = copy_dir($from . $filename, $to . $filename);
   765 			$result = copy_dir($from . $filename, $to . $filename, $skip_list);
   590 			if ( is_wp_error($result) )
   766 			if ( is_wp_error($result) )
   591 				return $result;
   767 				return $result;
   592 		}
   768 		}
   593 	}
   769 	}
   594 	return true;
   770 	return true;
   616 	if ( ! $method )
   792 	if ( ! $method )
   617 		return false;
   793 		return false;
   618 
   794 
   619 	if ( ! class_exists("WP_Filesystem_$method") ) {
   795 	if ( ! class_exists("WP_Filesystem_$method") ) {
   620 		$abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method);
   796 		$abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method);
   621 		if( ! file_exists($abstraction_file) )
   797 		if ( ! file_exists($abstraction_file) )
   622 			return;
   798 			return;
   623 
   799 
   624 		require_once($abstraction_file);
   800 		require_once($abstraction_file);
   625 	}
   801 	}
   626 	$method = "WP_Filesystem_$method";
   802 	$method = "WP_Filesystem_$method";
   635 
   811 
   636 	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
   812 	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
   637 		return false;
   813 		return false;
   638 
   814 
   639 	if ( !$wp_filesystem->connect() )
   815 	if ( !$wp_filesystem->connect() )
   640 		return false; //There was an erorr connecting to the server.
   816 		return false; //There was an error connecting to the server.
   641 
   817 
   642 	// Set the permission constants if not already set.
   818 	// Set the permission constants if not already set.
   643 	if ( ! defined('FS_CHMOD_DIR') )
   819 	if ( ! defined('FS_CHMOD_DIR') )
   644 		define('FS_CHMOD_DIR', 0755 );
   820 		define('FS_CHMOD_DIR', 0755 );
   645 	if ( ! defined('FS_CHMOD_FILE') )
   821 	if ( ! defined('FS_CHMOD_FILE') )
   648 	return true;
   824 	return true;
   649 }
   825 }
   650 
   826 
   651 /**
   827 /**
   652  * Determines which Filesystem Method to use.
   828  * Determines which Filesystem Method to use.
   653  * The priority of the Transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets (Via Sockets class, or fsoxkopen())
   829  * The priority of the Transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets (Via Sockets class, or fsockopen())
   654  *
   830  *
   655  * Note that the return value of this function can be overridden in 2 ways
   831  * Note that the return value of this function can be overridden in 2 ways
   656  *  - By defining FS_METHOD in your <code>wp-config.php</code> file
   832  *  - By defining FS_METHOD in your <code>wp-config.php</code> file
   657  *  - By using the filesystem_method filter
   833  *  - By using the filesystem_method filter
   658  * Valid values for these are: 'direct', 'ssh', 'ftpext' or 'ftpsockets'
   834  * Valid values for these are: 'direct', 'ssh', 'ftpext' or 'ftpsockets'
   665  * @return string The transport to use, see description for valid return values.
   841  * @return string The transport to use, see description for valid return values.
   666  */
   842  */
   667 function get_filesystem_method($args = array(), $context = false) {
   843 function get_filesystem_method($args = array(), $context = false) {
   668 	$method = defined('FS_METHOD') ? FS_METHOD : false; //Please ensure that this is either 'direct', 'ssh', 'ftpext' or 'ftpsockets'
   844 	$method = defined('FS_METHOD') ? FS_METHOD : false; //Please ensure that this is either 'direct', 'ssh', 'ftpext' or 'ftpsockets'
   669 
   845 
   670 	if( ! $method && function_exists('getmyuid') && function_exists('fileowner') ){
   846 	if ( ! $method && function_exists('getmyuid') && function_exists('fileowner') ){
   671 		if ( !$context )
   847 		if ( !$context )
   672 			$context = WP_CONTENT_DIR;
   848 			$context = WP_CONTENT_DIR;
   673 		$context = trailingslashit($context);
   849 		$context = trailingslashit($context);
   674 		$temp_file_name = $context . 'temp-write-test-' . time();
   850 		$temp_file_name = $context . 'temp-write-test-' . time();
   675 		$temp_handle = @fopen($temp_file_name, 'w');
   851 		$temp_handle = @fopen($temp_file_name, 'w');
   686 	if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
   862 	if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
   687 	return apply_filters('filesystem_method', $method, $args);
   863 	return apply_filters('filesystem_method', $method, $args);
   688 }
   864 }
   689 
   865 
   690 /**
   866 /**
   691  * Displays a form to the user to request for their FTP/SSH details in order to  connect to the filesystem.
   867  * Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.
   692  * All chosen/entered details are saved, Excluding the Password.
   868  * All chosen/entered details are saved, Excluding the Password.
   693  *
   869  *
   694  * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) to specify an alternate FTP/SSH port.
   870  * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) to specify an alternate FTP/SSH port.
   695  *
   871  *
   696  * Plugins may override this form by returning true|false via the <code>request_filesystem_credentials</code> filter.
   872  * Plugins may override this form by returning true|false via the <code>request_filesystem_credentials</code> filter.
   698  * @since 2.5.0
   874  * @since 2.5.0
   699  *
   875  *
   700  * @param string $form_post the URL to post the form to
   876  * @param string $form_post the URL to post the form to
   701  * @param string $type the chosen Filesystem method in use
   877  * @param string $type the chosen Filesystem method in use
   702  * @param boolean $error if the current request has failed to connect
   878  * @param boolean $error if the current request has failed to connect
   703  * @param string $context The directory which is needed access to, The write-test will be performed on  this directory by get_filesystem_method()
   879  * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method()
       
   880  * @param string $extra_fields Extra POST fields which should be checked for to be included in the post.
   704  * @return boolean False on failure. True on success.
   881  * @return boolean False on failure. True on success.
   705  */
   882  */
   706 function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false) {
   883 function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false, $extra_fields = null) {
   707 	$req_cred = apply_filters('request_filesystem_credentials', '', $form_post, $type, $error, $context);
   884 	$req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields );
   708 	if ( '' !== $req_cred )
   885 	if ( '' !== $req_cred )
   709 		return $req_cred;
   886 		return $req_cred;
   710 
   887 
   711 	if ( empty($type) )
   888 	if ( empty($type) )
   712 		$type = get_filesystem_method(array(), $context);
   889 		$type = get_filesystem_method(array(), $context);
   713 
   890 
   714 	if ( 'direct' == $type )
   891 	if ( 'direct' == $type )
   715 		return true;
   892 		return true;
       
   893 
       
   894 	if ( is_null( $extra_fields ) )
       
   895 		$extra_fields = array( 'version', 'locale' );
   716 
   896 
   717 	$credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => ''));
   897 	$credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => ''));
   718 
   898 
   719 	// 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)
   899 	// 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)
   720 	$credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? stripslashes($_POST['hostname']) : $credentials['hostname']);
   900 	$credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? stripslashes($_POST['hostname']) : $credentials['hostname']);
   763 	$password = '';
   943 	$password = '';
   764 	$connection_type = '';
   944 	$connection_type = '';
   765 	if ( !empty($credentials) )
   945 	if ( !empty($credentials) )
   766 		extract($credentials, EXTR_OVERWRITE);
   946 		extract($credentials, EXTR_OVERWRITE);
   767 	if ( $error ) {
   947 	if ( $error ) {
   768 		$error_string = __('<strong>Error:</strong> There was an error connecting to the server, Please verify the settings are correct.');
   948 		$error_string = __('<strong>ERROR:</strong> There was an error connecting to the server, Please verify the settings are correct.');
   769 		if ( is_wp_error($error) )
   949 		if ( is_wp_error($error) )
   770 			$error_string = $error->get_error_message();
   950 			$error_string = esc_html( $error->get_error_message() );
   771 		echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
   951 		echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
   772 	}
   952 	}
   773 
   953 
   774 	$types = array();
   954 	$types = array();
   775 	if ( extension_loaded('ftp') || extension_loaded('sockets') || function_exists('fsockopen') )
   955 	if ( extension_loaded('ftp') || extension_loaded('sockets') || function_exists('fsockopen') )
   797 </script>
   977 </script>
   798 <form action="<?php echo $form_post ?>" method="post">
   978 <form action="<?php echo $form_post ?>" method="post">
   799 <div class="wrap">
   979 <div class="wrap">
   800 <?php screen_icon(); ?>
   980 <?php screen_icon(); ?>
   801 <h2><?php _e('Connection Information') ?></h2>
   981 <h2><?php _e('Connection Information') ?></h2>
   802 <p><?php _e('To perform the requested action, connection information is required.') ?></p>
   982 <p><?php
   803 
   983 	$label_user = __('Username');
       
   984 	$label_pass = __('Password');
       
   985 	_e('To perform the requested action, WordPress needs to access your web server.');
       
   986 	echo ' ';
       
   987 	if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) {
       
   988 		if ( isset( $types['ssh'] ) ) {
       
   989 			_e('Please enter your FTP or SSH credentials to proceed.');
       
   990 			$label_user = __('FTP/SSH Username');
       
   991 			$label_pass = __('FTP/SSH Password');
       
   992 		} else {
       
   993 			_e('Please enter your FTP credentials to proceed.');
       
   994 			$label_user = __('FTP Username');
       
   995 			$label_pass = __('FTP Password');
       
   996 		}
       
   997 		echo ' ';
       
   998 	}
       
   999 	_e('If you do not remember your credentials, you should contact your web host.');
       
  1000 ?></p>
   804 <table class="form-table">
  1001 <table class="form-table">
   805 <tr valign="top">
  1002 <tr valign="top">
   806 <th scope="row"><label for="hostname"><?php _e('Hostname') ?></label></th>
  1003 <th scope="row"><label for="hostname"><?php _e('Hostname') ?></label></th>
   807 <td><input name="hostname" type="text" id="hostname" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php if( defined('FTP_HOST') ) echo ' disabled="disabled"' ?> size="40" /></td>
  1004 <td><input name="hostname" type="text" id="hostname" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php disabled( defined('FTP_HOST') ); ?> size="40" /></td>
   808 </tr>
  1005 </tr>
   809 
  1006 
   810 <tr valign="top">
  1007 <tr valign="top">
   811 <th scope="row"><label for="username"><?php _e('Username') ?></label></th>
  1008 <th scope="row"><label for="username"><?php echo $label_user; ?></label></th>
   812 <td><input name="username" type="text" id="username" value="<?php echo esc_attr($username) ?>"<?php if( defined('FTP_USER') ) echo ' disabled="disabled"' ?> size="40" /></td>
  1009 <td><input name="username" type="text" id="username" value="<?php echo esc_attr($username) ?>"<?php disabled( defined('FTP_USER') ); ?> size="40" /></td>
   813 </tr>
  1010 </tr>
   814 
  1011 
   815 <tr valign="top">
  1012 <tr valign="top">
   816 <th scope="row"><label for="password"><?php _e('Password') ?></label></th>
  1013 <th scope="row"><label for="password"><?php echo $label_pass; ?></label></th>
   817 <td><input name="password" type="password" id="password" value="<?php if ( defined('FTP_PASS') ) echo '*****'; ?>"<?php if ( defined('FTP_PASS') ) echo ' disabled="disabled"' ?> size="40" /></td>
  1014 <td><input name="password" type="password" id="password" value="<?php if ( defined('FTP_PASS') ) echo '*****'; ?>"<?php disabled( defined('FTP_PASS') ); ?> size="40" /></td>
   818 </tr>
  1015 </tr>
   819 
  1016 
   820 <?php if ( isset($types['ssh']) ) : ?>
  1017 <?php if ( isset($types['ssh']) ) : ?>
   821 <tr id="ssh_keys" valign="top" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>">
  1018 <tr id="ssh_keys" valign="top" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>">
   822 <th scope="row"><?php _e('Authentication Keys') ?>
  1019 <th scope="row"><?php _e('Authentication Keys') ?>
   823 <div class="key-labels textright">
  1020 <div class="key-labels textright">
   824 <label for="public_key"><?php _e('Public Key:') ?></label ><br />
  1021 <label for="public_key"><?php _e('Public Key:') ?></label ><br />
   825 <label for="private_key"><?php _e('Private Key:') ?></label>
  1022 <label for="private_key"><?php _e('Private Key:') ?></label>
   826 </div></th>
  1023 </div></th>
   827 <td><br /><input name="public_key" type="text" id="public_key" value="<?php echo esc_attr($public_key) ?>"<?php if( defined('FTP_PUBKEY') ) echo ' disabled="disabled"' ?> size="40" /><br /><input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php if( defined('FTP_PRIKEY') ) echo ' disabled="disabled"' ?> size="40" />
  1024 <td><br /><input name="public_key" type="text" id="public_key" value="<?php echo esc_attr($public_key) ?>"<?php disabled( defined('FTP_PUBKEY') ); ?> size="40" /><br /><input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php disabled( defined('FTP_PRIKEY') ); ?> size="40" />
   828 <div><?php _e('Enter the location on the server where the keys are located. If a passphrase is needed, enter that in the password field above.') ?></div></td>
  1025 <div><?php _e('Enter the location on the server where the keys are located. If a passphrase is needed, enter that in the password field above.') ?></div></td>
   829 </tr>
  1026 </tr>
   830 <?php endif; ?>
  1027 <?php endif; ?>
   831 
  1028 
   832 <tr valign="top">
  1029 <tr valign="top">
   833 <th scope="row"><?php _e('Connection Type') ?></th>
  1030 <th scope="row"><?php _e('Connection Type') ?></th>
   834 <td>
  1031 <td>
   835 <fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend>
  1032 <fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend>
   836 <?php
  1033 <?php
   837 
  1034 	$disabled = disabled( (defined('FTP_SSL') && FTP_SSL) || (defined('FTP_SSH') && FTP_SSH), true, false );
   838 	$disabled = (defined('FTP_SSL') && FTP_SSL) || (defined('FTP_SSH') && FTP_SSH) ? ' disabled="disabled"' : '';
       
   839 
       
   840 	foreach ( $types as $name => $text ) : ?>
  1035 	foreach ( $types as $name => $text ) : ?>
   841 	<label for="<?php echo esc_attr($name) ?>">
  1036 	<label for="<?php echo esc_attr($name) ?>">
   842 		<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; ?>/>
  1037 		<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; ?> />
   843 		<?php echo $text ?>
  1038 		<?php echo $text ?>
   844 	</label>
  1039 	</label>
   845 	<?php endforeach; ?>
  1040 	<?php endforeach; ?>
   846 </fieldset>
  1041 </fieldset>
   847 </td>
  1042 </td>
   848 </tr>
  1043 </tr>
   849 </table>
  1044 </table>
   850 
  1045 
   851 <?php if ( isset( $_POST['version'] ) ) : ?>
  1046 <?php
   852 <input type="hidden" name="version" value="<?php echo esc_attr(stripslashes($_POST['version'])) ?>" />
  1047 foreach ( (array) $extra_fields as $field ) {
   853 <?php endif; ?>
  1048 	if ( isset( $_POST[ $field ] ) )
   854 <?php if ( isset( $_POST['locale'] ) ) : ?>
  1049 		echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( stripslashes( $_POST[ $field ] ) ) . '" />';
   855 <input type="hidden" name="locale" value="<?php echo esc_attr(stripslashes($_POST['locale'])) ?>" />
  1050 }
   856 <?php endif; ?>
  1051 submit_button( __( 'Proceed' ), 'button', 'upgrade' );
   857 <p class="submit">
  1052 ?>
   858 <input id="upgrade" name="upgrade" type="submit" class="button" value="<?php esc_attr_e('Proceed'); ?>" />
       
   859 </p>
       
   860 </div>
  1053 </div>
   861 </form>
  1054 </form>
   862 <?php
  1055 <?php
   863 	return false;
  1056 	return false;
   864 }
  1057 }
   865 
       
   866 ?>