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