wp/wp-admin/includes/file.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
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 $wp_file_descriptions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
 * @param string $file Filesystem path or filename
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
 * @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
    56
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
function get_file_description( $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	global $wp_file_descriptions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	if ( isset( $wp_file_descriptions[basename( $file )] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		return $wp_file_descriptions[basename( $file )];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	elseif ( file_exists( $file ) && is_file( $file ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		$template_data = implode( '', file( $file ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ))
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			return sprintf( __( '%s Page Template' ), _cleanup_header_comment($name[1]) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	return trim( basename( $file ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
}
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
 * Get the absolute filesystem path to the root of the WordPress installation
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
 * @return string Full filesystem path to the root of the WordPress installation
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
function get_home_path() {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
	$home    = set_url_scheme( get_option( 'home' ), 'http' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
	$siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
		$wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		$pos = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		$home_path = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		$home_path = trailingslashit( $home_path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		$home_path = ABSPATH;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	return str_replace( '\\', '/', $home_path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
 * 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
    96
 * The depth of the recursiveness can be controlled by the $levels param.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
 * @param string $folder Optional. Full path to folder. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
 * @param int    $levels Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
 * @return bool|array False on failure, Else array of files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
function list_files( $folder = '', $levels = 100 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	if ( empty($folder) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	if ( ! $levels )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	$files = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
	if ( $dir = @opendir( $folder ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		while (($file = readdir( $dir ) ) !== false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
			if ( in_array($file, array('.', '..') ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
			if ( is_dir( $folder . '/' . $file ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
				$files2 = list_files( $folder . '/' . $file, $levels - 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
				if ( $files2 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
					$files = array_merge($files, $files2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
					$files[] = $folder . '/' . $file . '/';
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
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	@closedir( $dir );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	return $files;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 * Returns a filename of a Temporary unique file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 * Please note that the calling function must unlink() this itself.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * 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
   136
 * 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
   137
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
 * @param string $filename Optional. Filename to base the Unique file off. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
 * @param string $dir      Optional. Directory to store the file in. Default empty.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
 * @return string a writable filename
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
function wp_tempnam( $filename = '', $dir = '' ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
	if ( empty( $dir ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
		$dir = get_temp_dir();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
	if ( empty( $filename ) || '.' == $filename || '/' == $filename ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
		$filename = time();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
	// Use the basename of the given file without the extension as the name for the temporary directory
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   154
	$temp_filename = basename( $filename );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   155
	$temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   156
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   157
	// If the folder is falsey, use it's parent directory name instead
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   158
	if ( ! $temp_filename ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
		return wp_tempnam( dirname( $filename ), $dir );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   160
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   161
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   162
	$temp_filename .= '.tmp';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   163
	$temp_filename = $dir . wp_unique_filename( $dir, $temp_filename );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   164
	touch( $temp_filename );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   165
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
	return $temp_filename;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
 * 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
   171
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
 * Function will die if if you are not allowed to edit the file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
 * @since 1.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
 * @param string $file file the users is attempting to edit
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
 * @param array $allowed_files Array of allowed files to edit, $file must match an entry exactly
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
 * @return string|null
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
function validate_file_to_edit( $file, $allowed_files = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
	$code = validate_file( $file, $allowed_files );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
	if (!$code )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
		return $file;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	switch ( $code ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
		case 1 :
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
			wp_die( __( 'Sorry, that file cannot be edited.' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   190
		// case 2 :
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   191
		// wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
		case 3 :
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
			wp_die( __( 'Sorry, that file cannot be edited.' ) );
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
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
 * Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
 * and moving the file to the appropriate directory within the uploads directory.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   202
 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   204
 * @see wp_handle_upload_error
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   206
 * @param array       $file      Reference to a single element of $_FILES. Call the function once for each uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   207
 * @param array|false $overrides An associative array of names => values to override default variables. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   208
 * @param string      $time      Time formatted in 'yyyy/mm'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   209
 * @param string      $action    Expected value for $_POST['action'].
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
 * @return array On success, returns an associative array of file attributes. On failure, returns
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   211
 *               $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
*/
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
function _wp_handle_upload( &$file, $overrides, $time, $action ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
	// The default error handler.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
	if ( ! function_exists( 'wp_handle_upload_error' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		function wp_handle_upload_error( &$file, $message ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   217
			return array( 'error' => $message );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   222
	 * Filter the data for a file before it is uploaded to WordPress.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
	 * The dynamic portion of the hook name, `$action`, refers to the post action.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
	 * @since 2.9.0 as 'wp_handle_upload_prefilter'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
	 * @since 4.0.0 Converted to a dynamic hook with `$action`.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   228
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
	 * @param array $file An array of data for a single file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   230
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   231
	$file = apply_filters( "{$action}_prefilter", $file );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
	// 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
   234
	$upload_error_handler = 'wp_handle_upload_error';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   235
	if ( isset( $overrides['upload_error_handler'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   236
		$upload_error_handler = $overrides['upload_error_handler'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   237
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	// You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   240
	if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		return $upload_error_handler( $file, $file['error'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   244
	// Install user overrides. Did we mention that this voids your warranty?
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	// 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
   247
	$unique_filename_callback = null;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   248
	if ( isset( $overrides['unique_filename_callback'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   249
		$unique_filename_callback = $overrides['unique_filename_callback'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
	 * This may not have orignially been intended to be overrideable,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
	 * but historically has been.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   255
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   256
	if ( isset( $overrides['upload_error_strings'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   257
		$upload_error_strings = $overrides['upload_error_strings'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   258
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   259
		// Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
		$upload_error_strings = array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
			false,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
			__( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
			__( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
			__( 'The uploaded file was only partially uploaded.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
			__( 'No file was uploaded.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
			'',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   267
			__( 'Missing a temporary folder.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
			__( 'Failed to write file to disk.' ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   269
			__( 'File upload stopped by extension.' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
		);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	// All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   274
	$test_form = isset( $overrides['test_form'] ) ? $overrides['test_form'] : true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	$test_size = isset( $overrides['test_size'] ) ? $overrides['test_size'] : true;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
	// If you override this, you must provide $ext and $type!!
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
	$test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
	$mimes = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
	// A correct form post will pass this test.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
	if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
		return call_user_func( $upload_error_handler, $file, __( 'Invalid form submission.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
	// A successful upload will pass this test. It makes no sense to override this one.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
	if ( isset( $file['error'] ) && $file['error'] > 0 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   287
		return call_user_func( $upload_error_handler, $file, $upload_error_strings[ $file['error'] ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
	$test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	// A non-empty file will pass this test.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
	if ( $test_size && ! ( $test_file_size > 0 ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
		if ( is_multisite() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
			$error_msg = __( 'File is empty. Please upload something more substantial.' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
			$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.' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
		return call_user_func( $upload_error_handler, $file, $error_msg );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	// A properly uploaded file will pass this test. There should be no reason to override this one.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   302
	$test_uploaded_file = 'wp_handle_upload' === $action ? @ is_uploaded_file( $file['tmp_name'] ) : @ is_file( $file['tmp_name'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
	if ( ! $test_uploaded_file ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
		return call_user_func( $upload_error_handler, $file, __( 'Specified file failed upload test.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   305
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	// 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
   308
	if ( $test_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
		$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   310
		$ext = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   311
		$type = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   312
		$proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		// Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   315
		if ( $proper_filename ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
			$file['name'] = $proper_filename;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   317
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   318
		if ( ( ! $type || !$ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   319
			return call_user_func( $upload_error_handler, $file, __( 'Sorry, this file type is not permitted for security reasons.' ) );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   320
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   321
		if ( ! $type ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
			$type = $file['type'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   323
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
		$type = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   328
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   329
	 * A writable uploads dir will pass this test. Again, there's no point
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   330
	 * overriding this one.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   331
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   332
	if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   333
		return call_user_func( $upload_error_handler, $file, $uploads['error'] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   334
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
	$filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   338
	// Move the file to the uploads dir.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
	$new_file = $uploads['path'] . "/$filename";
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   340
	if ( 'wp_handle_upload' === $action ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
		$move_new_file = @ move_uploaded_file( $file['tmp_name'], $new_file );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   342
	} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
		$move_new_file = @ rename( $file['tmp_name'], $new_file );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   344
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   345
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   346
	if ( false === $move_new_file ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   347
		if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
			$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
			$error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		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
   353
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   355
	// Set correct file permissions.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
	$stat = stat( dirname( $new_file ));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	$perms = $stat['mode'] & 0000666;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
	@ chmod( $new_file, $perms );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
	// Compute the URL.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
	$url = $uploads['url'] . "/$filename";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
	if ( is_multisite() ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		delete_transient( 'dirsize_cache' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
	 * Filter the data array for the uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
	 * @since 2.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   371
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   372
	 * @param array  $upload {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   373
	 *     Array of upload data.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   374
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   375
	 *     @type string $file Filename of the newly-uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   376
	 *     @type string $url  URL of the uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   377
	 *     @type string $type File type.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   378
	 * }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   379
	 * @param string $context The type of upload action. Values include 'upload' or 'sideload'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   380
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   381
	return apply_filters( 'wp_handle_upload', array(
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   382
		'file' => $new_file,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   383
		'url'  => $url,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   384
		'type' => $type
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   385
	), 'wp_handle_sideload' === $action ? 'sideload' : 'upload' ); }
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   386
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   387
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   388
 * Wrapper for _wp_handle_upload(), passes 'wp_handle_upload' action.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   389
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   390
 * @since 2.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   391
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   392
 * @see _wp_handle_upload()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   393
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   394
 * @param array      $file      Reference to a single element of $_FILES. Call the function once for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   395
 *                              each uploaded file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
 * @param array|bool $overrides Optional. An associative array of names=>values to override default
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
 *                              variables. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
 * @param string     $time      Optional. Time formatted in 'yyyy/mm'. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
 * @return array On success, returns an associative array of file attributes. On failure, returns
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
 *               $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
function wp_handle_upload( &$file, $overrides = false, $time = null ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
	 *  $_POST['action'] must be set and its value must equal $overrides['action']
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   405
	 *  or this:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   406
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   407
	$action = 'wp_handle_upload';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   408
	if ( isset( $overrides['action'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   409
		$action = $overrides['action'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   410
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   411
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   412
	return _wp_handle_upload( $file, $overrides, $time, $action );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
 * Wrapper for _wp_handle_upload(), passes 'wp_handle_sideload' action
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   420
 * @see _wp_handle_upload()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   421
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
 * @param array      $file      An array similar to that of a PHP $_FILES POST array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
 * @param array|bool $overrides Optional. An associative array of names=>values to override default
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   424
 *                              variables. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   425
 * @param string     $time      Optional. Time formatted in 'yyyy/mm'. Default null.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   426
 * @return array On success, returns an associative array of file attributes. On failure, returns
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   427
 *               $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   430
	/*
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   431
	 *  $_POST['action'] must be set and its value must equal $overrides['action']
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   432
	 *  or this:
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   433
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
	$action = 'wp_handle_sideload';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
	if ( isset( $overrides['action'] ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
		$action = $overrides['action'];
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   437
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   438
	return _wp_handle_upload( $file, $overrides, $time, $action );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   439
}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
 * Downloads a url to a local temporary file using the WordPress HTTP Class.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
 * Please note, That the calling function must unlink() the file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
 * @param string $url the URL of the file to download
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
 * @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
   450
 * @return mixed WP_Error on failure, string Filename on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
function download_url( $url, $timeout = 300 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
	//WARNING: The file is not automatically deleted, The script must unlink() the file.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	if ( ! $url )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
		return new WP_Error('http_no_url', __('Invalid URL Provided.'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
	$tmpfname = wp_tempnam($url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
	if ( ! $tmpfname )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
		return new WP_Error('http_no_file', __('Could not create Temporary 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
	$response = wp_safe_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
	if ( is_wp_error( $response ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		unlink( $tmpfname );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		return $response;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	if ( 200 != wp_remote_retrieve_response_code( $response ) ){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
		unlink( $tmpfname );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
		return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	$content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	if ( $content_md5 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
		$md5_check = verify_file_md5( $tmpfname, $content_md5 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		if ( is_wp_error( $md5_check ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
			unlink( $tmpfname );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
			return $md5_check;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
	return $tmpfname;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   486
 * Calculates and compares the MD5 of a file to its expected value.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
 * @param string $filename The filename to check the MD5 of.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
 * @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
   492
 * @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
   493
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
function verify_file_md5( $filename, $expected_md5 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
	if ( 32 == strlen( $expected_md5 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
		$expected_raw_md5 = pack( 'H*', $expected_md5 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
	elseif ( 24 == strlen( $expected_md5 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
		$expected_raw_md5 = base64_decode( $expected_md5 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
		return false; // unknown format
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
	$file_md5 = md5_file( $filename, true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	if ( $file_md5 === $expected_raw_md5 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	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
   508
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
 * 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
   512
 * 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
   513
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
 * Attempts to increase the PHP Memory limit to 256M before uncompressing,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
 * 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
   516
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
 * @param string $file Full path and filename of zip archive
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
 * @param string $to Full path on the filesystem to extract archive to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
 * @return mixed WP_Error on failure, True on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
function unzip_file($file, $to) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
	if ( ! $wp_filesystem || !is_object($wp_filesystem) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
	// Unzip can use a lot of memory, but not this much hopefully
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   530
	/** This filter is documented in wp-admin/admin.php */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
	@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
	$needed_dirs = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	$to = trailingslashit($to);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
	// Determine any parent dir's needed (of the upgrade directory)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
	if ( ! $wp_filesystem->is_dir($to) ) { //Only do parents if no children exist
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
		$path = preg_split('![/\\\]!', untrailingslashit($to));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
		for ( $i = count($path); $i >= 0; $i-- ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
			if ( empty($path[$i]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
			$dir = implode('/', array_slice($path, 0, $i+1) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
			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
   545
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
			if ( ! $wp_filesystem->is_dir($dir) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
				$needed_dirs[] = $dir;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
			else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
				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
   551
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   554
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   555
	 * Filter whether to use ZipArchive to unzip archives.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   556
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   557
	 * @since 3.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   558
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
	 * @param bool $ziparchive Whether to use ZipArchive. Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   561
	if ( class_exists( 'ZipArchive' ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
		$result = _unzip_file_ziparchive($file, $to, $needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
		if ( true === $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
			return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
		} elseif ( is_wp_error($result) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
			if ( 'incompatible_archive' != $result->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
				return $result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
	// 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
   571
	return _unzip_file_pclzip($file, $to, $needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
 * 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
   576
 * Assumes that WP_Filesystem() has already been called and set up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
 * @see unzip_file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
 * @param string $file Full path and filename of zip archive
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
 * @param string $to Full path on the filesystem to extract archive to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
 * @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
   585
 * @return mixed WP_Error on failure, True on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
function _unzip_file_ziparchive($file, $to, $needed_dirs = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
	global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
	$z = new ZipArchive();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
	$zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
	if ( true !== $zopen )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
		return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
	$uncompressed_size = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
	for ( $i = 0; $i < $z->numFiles; $i++ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
		if ( ! $info = $z->statIndex($i) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
			return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
		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
   603
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
		$uncompressed_size += $info['size'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
		if ( '/' == substr($info['name'], -1) ) // directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
			$needed_dirs[] = $to . untrailingslashit($info['name']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
			$needed_dirs[] = $to . untrailingslashit(dirname($info['name']));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
	/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
	 * 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
   615
	 * A disk that has zero free bytes has bigger problems.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
	 * 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
   617
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
	if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
		$available_space = @disk_free_space( WP_CONTENT_DIR );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
		if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
			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
   622
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
	$needed_dirs = array_unique($needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
	foreach ( $needed_dirs as $dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		// Check the parent folders of the folders all exist within the creation array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
		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
   628
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
		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
   630
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
		$parent_folder = dirname($dir);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
		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
   634
			$needed_dirs[] = $parent_folder;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
			$parent_folder = dirname($parent_folder);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
	asort($needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
	// Create those directories if need be:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
	foreach ( $needed_dirs as $_dir ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   642
		// Only check to see if the Dir exists upon creation failure. Less I/O this way.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   643
		if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
			return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   645
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
	unset($needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
	for ( $i = 0; $i < $z->numFiles; $i++ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
		if ( ! $info = $z->statIndex($i) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
			return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
		if ( '/' == substr($info['name'], -1) ) // directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
		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
   657
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		$contents = $z->getFromIndex($i);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
		if ( false === $contents )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
			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
   662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
		if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
			return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
	$z->close();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	return true;
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
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
 * 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
   674
 * Assumes that WP_Filesystem() has already been called and set up.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
 * @see unzip_file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
 * @param string $file Full path and filename of zip archive
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
 * @param string $to Full path on the filesystem to extract archive to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
 * @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
   683
 * @return mixed WP_Error on failure, True on success
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
function _unzip_file_pclzip($file, $to, $needed_dirs = array()) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
	global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
	mbstring_binary_safe_encoding();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
	require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
	$archive = new PclZip($file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
	$archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
	reset_mbstring_encoding();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
	// Is the archive valid?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
	if ( !is_array($archive_files) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
		return new WP_Error('incompatible_archive', __('Incompatible Archive.'), $archive->errorInfo(true));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
	if ( 0 == count($archive_files) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
		return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
	$uncompressed_size = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
	// Determine any children directories needed (From within the archive)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	foreach ( $archive_files as $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
		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
   710
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
		$uncompressed_size += $file['size'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
		$needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname($file['filename']) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
	/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
	 * 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
   719
	 * A disk that has zero free bytes has bigger problems.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
	 * 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
   721
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
	if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
		$available_space = @disk_free_space( WP_CONTENT_DIR );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
		if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
			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
   726
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
	$needed_dirs = array_unique($needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
	foreach ( $needed_dirs as $dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
		// Check the parent folders of the folders all exist within the creation array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		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
   732
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		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
   734
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
		$parent_folder = dirname($dir);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
		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
   738
			$needed_dirs[] = $parent_folder;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
			$parent_folder = dirname($parent_folder);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
	asort($needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
	// Create those directories if need be:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
	foreach ( $needed_dirs as $_dir ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
		// 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
   747
		if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
			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
   749
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
	unset($needed_dirs);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	// Extract the files from the zip
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
	foreach ( $archive_files as $file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
		if ( $file['folder'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
		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
   758
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
		if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
			return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
 * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
 * Assumes that WP_Filesystem() has already been called and setup.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
 * @param string $from source directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
 * @param string $to destination directory
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
 * @param array $skip_list a list of files/folders to skip copying
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
 * @return mixed WP_Error on failure, True on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
function copy_dir($from, $to, $skip_list = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
	global $wp_filesystem;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
	$dirlist = $wp_filesystem->dirlist($from);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
	$from = trailingslashit($from);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
	$to = trailingslashit($to);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
	foreach ( (array) $dirlist as $filename => $fileinfo ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
		if ( in_array( $filename, $skip_list ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
		if ( 'f' == $fileinfo['type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
			if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
				// If copy failed, chmod file to 0644 and try again.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
				$wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
				if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
					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
   795
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
		} elseif ( 'd' == $fileinfo['type'] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
			if ( !$wp_filesystem->is_dir($to . $filename) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
				if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
					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
   800
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
			// 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
   803
			$sub_skip_list = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
			foreach ( $skip_list as $skip_item ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
				if ( 0 === strpos( $skip_item, $filename . '/' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
					$sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
			$result = copy_dir($from . $filename, $to . $filename, $sub_skip_list);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
			if ( is_wp_error($result) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
				return $result;
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
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
 * Initialises and connects the WordPress Filesystem Abstraction classes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
 * This function will include the chosen transport and attempt connecting.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   821
 * Plugins may add extra transports, And force WordPress to use them by returning
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   822
 * the filename via the {@see 'filesystem_method_file'} filter.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   826
 * @param array  $args                         Optional. Connection args, These are passed directly to
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   827
 *                                             the `WP_Filesystem_*()` classes. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   828
 * @param string $context                      Optional. Context for {@see get_filesystem_method()}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   829
 *                                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   830
 * @param bool   $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   831
 *                                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   832
 * @return null|boolean false on failure, true on success.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   834
function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) {
0
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
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   839
	$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
0
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") ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   845
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   846
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   847
		 * Filter the path for a specific filesystem method class file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   848
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   849
		 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   850
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   851
		 * @see get_filesystem_method()
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   852
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   853
		 * @param string $path   Path to the specific filesystem method class file.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   854
		 * @param string $method The filesystem method to use.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   855
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   856
		$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   857
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
		if ( ! file_exists($abstraction_file) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
		require_once($abstraction_file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
	$method = "WP_Filesystem_$method";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
	$wp_filesystem = new $method($args);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
	//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
   868
	if ( ! defined('FS_CONNECT_TIMEOUT') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
		define('FS_CONNECT_TIMEOUT', 30);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
	if ( ! defined('FS_TIMEOUT') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
		define('FS_TIMEOUT', 30);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
	if ( !$wp_filesystem->connect() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
		return false; //There was an error connecting to the server.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	// Set the permission constants if not already set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
	if ( ! defined('FS_CHMOD_DIR') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
		define('FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
	if ( ! defined('FS_CHMOD_FILE') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
		define('FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   889
 * Determines which method to use for reading, writing, modifying, or deleting
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   890
 * files on the filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   891
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   892
 * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   893
 * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   894
 * 'ftpext' or 'ftpsockets'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   896
 * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   897
 * or filtering via {@see 'filesystem_method'}.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   898
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   899
 * @link https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   900
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   901
 * Plugins may define a custom transport handler, See WP_Filesystem().
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   905
 * @param array  $args                         Optional. Connection details. Default empty array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   906
 * @param string $context                      Optional. Full path to the directory that is tested
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   907
 *                                             for being writable. Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   908
 * @param bool   $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   909
 *                                             Default false.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
 * @return string The transport to use, see description for valid return values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   912
function get_filesystem_method( $args = array(), $context = false, $allow_relaxed_file_ownership = false ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   913
	$method = defined('FS_METHOD') ? FS_METHOD : false; // Please ensure that this is either 'direct', 'ssh2', 'ftpext' or 'ftpsockets'
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   914
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   915
	if ( ! $context ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   916
		$context = WP_CONTENT_DIR;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   917
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   919
	// If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   920
	if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   921
		$context = dirname( $context );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   922
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   924
	$context = trailingslashit( $context );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   926
	if ( ! $method ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   927
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
		$temp_file_name = $context . 'temp-write-test-' . time();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
		$temp_handle = @fopen($temp_file_name, 'w');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
		if ( $temp_handle ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   932
			// Attempt to determine the file owner of the WordPress files, and that of newly created files
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   933
			$wp_file_owner = $temp_file_owner = false;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   934
			if ( function_exists('fileowner') ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   935
				$wp_file_owner = @fileowner( __FILE__ );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   936
				$temp_file_owner = @fileowner( $temp_file_name );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   937
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   938
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   939
			if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   940
				// WordPress is creating files as the same owner as the WordPress files,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   941
				// this means it's safe to modify & create new files via PHP.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
				$method = 'direct';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   943
				$GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   944
			} elseif ( $allow_relaxed_file_ownership ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   945
				// The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   946
				// safely in this directory. This mode doesn't create new files, only alter existing ones.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   947
				$method = 'direct';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   948
				$GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   949
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   950
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
			@fclose($temp_handle);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
			@unlink($temp_file_name);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
 	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
	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
   957
	if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
	if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   959
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   960
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
	 * Filter the filesystem method to use.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   962
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   963
	 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   964
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   965
	 * @param string $method  Filesystem method to return.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   966
	 * @param array  $args    An array of connection details for the method.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   967
	 * @param string $context Full path to the directory that is tested for being writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   968
	 * @param bool   $allow_relaxed_file_ownership Whether to allow Group/World writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   969
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   970
	return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
0
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
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   974
 * Displays a form to the user to request for their FTP/SSH details in order
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   975
 * to connect to the filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   976
 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
 * All chosen/entered details are saved, Excluding the Password.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   979
 * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   980
 * to specify an alternate FTP/SSH port.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   982
 * Plugins may override this form by returning true|false via the
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   983
 * {@see 'request_filesystem_credentials'} filter.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   985
 * @since 2.5.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   986
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   987
 * @todo Properly mark optional arguments as such
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
 * @param string $form_post the URL to post the form to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
 * @param string $type the chosen Filesystem method in use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
 * @param boolean $error if the current request has failed to connect
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
 * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method()
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   993
 * @param array $extra_fields Extra POST fields which should be checked for to be included in the post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   994
 * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
 * @return boolean False on failure. True on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   997
function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false, $extra_fields = null, $allow_relaxed_file_ownership = false ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   998
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   999
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1000
	 * Filter the filesystem credentials form output.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1001
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1002
	 * Returning anything other than an empty string will effectively short-circuit
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1003
	 * output of the filesystem credentials form, returning that value instead.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1004
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1005
	 * @since 2.5.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1006
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1007
	 * @param mixed  $output       Form output to return instead. Default empty.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1008
	 * @param string $form_post    URL to POST the form to.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1009
	 * @param string $type         Chosen type of filesystem.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1010
	 * @param bool   $error        Whether the current request has failed to connect.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1011
	 *                             Default false.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1012
	 * @param string $context      Full path to the directory that is tested for
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1013
	 *                             being writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1014
	 * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
	 * @param array  $extra_fields Extra POST fields.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1016
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
	$req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
	if ( '' !== $req_cred )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		return $req_cred;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1021
	if ( empty($type) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1022
		$type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
	if ( 'direct' == $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	if ( is_null( $extra_fields ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
		$extra_fields = array( 'version', 'locale' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	$credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => ''));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
	// 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
  1034
	$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
  1035
	$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
  1036
	$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
  1037
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
	// Check to see if we are setting the public/private keys for ssh
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
	$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
  1040
	$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
  1041
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1042
	// Sanitize the hostname, Some people might pass in odd-data:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	$credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	if ( strpos($credentials['hostname'], ':') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		if ( ! is_numeric($credentials['port']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
			unset($credentials['port']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
		unset($credentials['port']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1053
	if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
		$credentials['connection_type'] = 'ssh';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1055
	} elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { //Only the FTP Extension understands SSL
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
		$credentials['connection_type'] = 'ftps';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
	} elseif ( ! empty( $_POST['connection_type'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
		$credentials['connection_type'] = wp_unslash( $_POST['connection_type'] );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1059
	} elseif ( ! isset( $credentials['connection_type'] ) ) { //All else fails (And it's not defaulted to something else saved), Default to FTP
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
		$credentials['connection_type'] = 'ftp';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1061
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
	if ( ! $error &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
			(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
				( !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) ||
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
				( 'ssh' == $credentials['connection_type'] && !empty($credentials['public_key']) && !empty($credentials['private_key']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
			) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
		$stored_credentials = $credentials;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
		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
  1069
			$stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
		unset($stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key']);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1072
		if ( ! defined( 'WP_INSTALLING' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1073
			update_option( 'ftp_credentials', $stored_credentials );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1074
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
		return $credentials;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1077
	$hostname = isset( $credentials['hostname'] ) ? $credentials['hostname'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1078
	$username = isset( $credentials['username'] ) ? $credentials['username'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1079
	$public_key = isset( $credentials['public_key'] ) ? $credentials['public_key'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1080
	$private_key = isset( $credentials['private_key'] ) ? $credentials['private_key'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1081
	$port = isset( $credentials['port'] ) ? $credentials['port'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1082
	$connection_type = isset( $credentials['connection_type'] ) ? $credentials['connection_type'] : '';
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1083
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
	if ( $error ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
		$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
  1086
		if ( is_wp_error($error) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
			$error_string = esc_html( $error->get_error_message() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
		echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
	$types = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
	if ( extension_loaded('ftp') || extension_loaded('sockets') || function_exists('fsockopen') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		$types[ 'ftp' ] = __('FTP');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
	if ( extension_loaded('ftp') ) //Only this supports FTPS
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
		$types[ 'ftps' ] = __('FTPS (SSL)');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
	if ( extension_loaded('ssh2') && function_exists('stream_get_contents') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
		$types[ 'ssh' ] = __('SSH2');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1099
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1100
	 * Filter the connection types to output to the filesystem credentials form.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1101
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1102
	 * @since 2.9.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1103
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1104
	 * @param array  $types       Types of connections.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1105
	 * @param array  $credentials Credentials to connect with.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1106
	 * @param string $type        Chosen filesystem method.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1107
	 * @param object $error       Error object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1108
	 * @param string $context     Full path to the directory that is tested
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1109
	 *                            for being writable.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1110
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1111
	$types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
<script type="text/javascript">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
<!--
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
jQuery(function($){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
	jQuery("#ssh").click(function () {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
		jQuery("#ssh_keys").show();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
	jQuery("#ftp, #ftps").click(function () {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
		jQuery("#ssh_keys").hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
	});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1123
	jQuery('#request-filesystem-credentials-form input[value=""]:first').focus();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
-->
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
</script>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
<form action="<?php echo esc_url( $form_post ) ?>" method="post">
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1128
<div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1129
<h3 id="request-filesystem-credentials-title"><?php _e( 'Connection Information' ) ?></h3>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1130
<p id="request-filesystem-credentials-desc"><?php
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
	$label_user = __('Username');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
	$label_pass = __('Password');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
	_e('To perform the requested action, WordPress needs to access your web server.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
	echo ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
	if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
		if ( isset( $types['ssh'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
			_e('Please enter your FTP or SSH credentials to proceed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
			$label_user = __('FTP/SSH Username');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
			$label_pass = __('FTP/SSH Password');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
			_e('Please enter your FTP credentials to proceed.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
			$label_user = __('FTP Username');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
			$label_pass = __('FTP Password');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		echo ' ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
	_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
  1148
?></p>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1149
<label for="hostname">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1150
	<span class="field-title"><?php _e( 'Hostname' ) ?></span>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1151
	<input name="hostname" type="text" id="hostname" aria-describedby="request-filesystem-credentials-desc" class="code" placeholder="<?php esc_attr_e( 'example: www.wordpress.org' ) ?>" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php disabled( defined('FTP_HOST') ); ?> />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1152
</label>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1153
<div class="ftp-username">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1154
	<label for="username">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1155
		<span class="field-title"><?php echo $label_user; ?></span>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1156
		<input name="username" type="text" id="username" value="<?php echo esc_attr($username) ?>"<?php disabled( defined('FTP_USER') ); ?> />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1157
	</label>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1158
</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1159
<div class="ftp-password">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1160
	<label for="password">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1161
		<span class="field-title"><?php echo $label_pass; ?></span>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1162
		<input name="password" type="password" id="password" value="<?php if ( defined('FTP_PASS') ) echo '*****'; ?>"<?php disabled( defined('FTP_PASS') ); ?> />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1163
		<em><?php if ( ! defined('FTP_PASS') ) _e( 'This password will not be stored on the server.' ); ?></em>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1164
	</label>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1165
</div>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
<?php if ( isset($types['ssh']) ) : ?>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1167
<h4><?php _e('Authentication Keys') ?></h4>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1168
<label for="public_key">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1169
	<span class="field-title"><?php _e('Public Key:') ?></span>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1170
	<input name="public_key" type="text" id="public_key" aria-describedby="auth-keys-desc" value="<?php echo esc_attr($public_key) ?>"<?php disabled( defined('FTP_PUBKEY') ); ?> />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1171
</label>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1172
<label for="private_key">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1173
	<span class="field-title"><?php _e('Private Key:') ?></span>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1174
	<input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php disabled( defined('FTP_PRIKEY') ); ?> />
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1175
</label>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1176
<span id="auth-keys-desc"><?php _e('Enter the location on the server where the public and private keys are located. If a passphrase is needed, enter that in the password field above.') ?></span>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
<?php endif; ?>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1178
<h4><?php _e('Connection Type') ?></h4>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
<fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
	$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
  1182
	foreach ( $types as $name => $text ) : ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
	<label for="<?php echo esc_attr($name) ?>">
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
		<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
  1185
		<?php echo $text ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
	</label>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
	<?php endforeach; ?>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
</fieldset>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
foreach ( (array) $extra_fields as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
	if ( isset( $_POST[ $field ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
		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
  1193
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
?>
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1195
	<p class="request-filesystem-credentials-action-buttons">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1196
		<button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1197
		<?php submit_button( __( 'Proceed' ), 'button', 'upgrade', false ); ?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1198
	</p>
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
</div>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
</form>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1204
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1205
/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1206
 * Print the filesystem credentials modal when needed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1207
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1208
 * @since 4.2.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1209
 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1210
function wp_print_request_filesystem_credentials_modal() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1211
	$filesystem_method = get_filesystem_method();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1212
	ob_start();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1213
	$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1214
	ob_end_clean();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1215
	$request_filesystem_credentials = ( $filesystem_method != 'direct' && ! $filesystem_credentials_are_stored );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1216
	if ( ! $request_filesystem_credentials ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1217
		return;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1218
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1219
	?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1220
	<div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1221
		<div class="notification-dialog-background"></div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1222
		<div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1223
			<div class="request-filesystem-credentials-dialog-content">
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1224
				<?php request_filesystem_credentials( site_url() ); ?>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1225
			<div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1226
		</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1227
	</div>
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1228
	<?php
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1229
}