3 $wp_file_descriptions = array ('index.php' => __( 'Main Index Template' ), 'style.css' => __( 'Stylesheet' ), 'rtl.css' => __( 'RTL Stylesheet' ), 'comments.php' => __( 'Comments' ), 'comments-popup.php' => __( 'Popup Comments' ), 'footer.php' => __( 'Footer' ), 'header.php' => __( 'Header' ), 'sidebar.php' => __( 'Sidebar' ), 'archive.php' => __( 'Archives' ), 'category.php' => __( 'Category Template' ), 'page.php' => __( 'Page Template' ), 'search.php' => __( 'Search Results' ), 'searchform.php' => __( 'Search Form' ), 'single.php' => __( 'Single Post' ), '404.php' => __( '404 Template' ), 'link.php' => __( 'Links Template' ), 'functions.php' => __( 'Theme Functions' ), 'attachment.php' => __( 'Attachment Template' ), 'my-hacks.php' => __( 'my-hacks.php (legacy hacks support)' ), '.htaccess' => __( '.htaccess (for rewrite rules )' ),
5 'wp-layout.css' => __( 'Stylesheet' ), 'wp-comments.php' => __( 'Comments Template' ), 'wp-comments-popup.php' => __( 'Popup Comments Template' ));
6 function get_file_description( $file ) {
7 global $wp_file_descriptions;
9 if ( isset( $wp_file_descriptions[basename( $file )] ) ) {
10 return $wp_file_descriptions[basename( $file )];
12 elseif ( file_exists( ABSPATH . $file ) && is_file( ABSPATH . $file ) ) {
13 $template_data = implode( '', file( ABSPATH . $file ) );
14 if ( preg_match( "|Template Name:(.*)|i", $template_data, $name ))
18 return basename( $file );
21 function get_home_path() {
22 $home = get_option( 'home' );
23 if ( $home != '' && $home != get_option( 'siteurl' ) ) {
24 $home_path = parse_url( $home );
25 $home_path = $home_path['path'];
26 $root = str_replace( $_SERVER["PHP_SELF"], '', $_SERVER["SCRIPT_FILENAME"] );
27 $home_path = trailingslashit( $root.$home_path );
35 function get_real_file_to_edit( $file ) {
36 if ('index.php' == $file || '.htaccess' == $file ) {
37 $real_file = get_home_path().$file;
39 $real_file = ABSPATH.$file;
45 function get_temp_dir() {
46 if ( defined('WP_TEMP_DIR') )
47 return trailingslashit(WP_TEMP_DIR);
49 $temp = ABSPATH . 'wp-content/';
50 if ( is_dir($temp) && is_writable($temp) )
53 if ( function_exists('sys_get_temp_dir') )
54 return trailingslashit(sys_get_temp_dir());
59 function validate_file( $file, $allowed_files = '' ) {
60 if ( false !== strpos( $file, '..' ))
63 if ( false !== strpos( $file, './' ))
66 if (':' == substr( $file, 1, 1 ))
69 if (!empty ( $allowed_files ) && (!in_array( $file, $allowed_files ) ) )
75 function validate_file_to_edit( $file, $allowed_files = '' ) {
76 $file = stripslashes( $file );
78 $code = validate_file( $file, $allowed_files );
85 wp_die( __('Sorry, can’t edit files with ".." in the name. If you are trying to edit a file in your WordPress home directory, you can just type the name of the file in.' ));
88 wp_die( __('Sorry, can’t call files with their real path.' ));
91 wp_die( __('Sorry, that file cannot be edited.' ));
95 // array wp_handle_upload ( array &file [, array overrides] )
96 // file: reference to a single element of $_FILES. Call the function once for each uploaded file.
97 // overrides: an associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
98 // On success, returns an associative array of file attributes.
99 // On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
100 function wp_handle_upload( &$file, $overrides = false ) {
101 // The default error handler.
102 if (! function_exists( 'wp_handle_upload_error' ) ) {
103 function wp_handle_upload_error( &$file, $message ) {
104 return array( 'error'=>$message );
108 // You may define your own function and pass the name in $overrides['upload_error_handler']
109 $upload_error_handler = 'wp_handle_upload_error';
111 // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
112 $action = 'wp_handle_upload';
114 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
115 $upload_error_strings = array( false,
116 __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
117 __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
118 __( "The uploaded file was only partially uploaded." ),
119 __( "No file was uploaded." ),
120 __( "Missing a temporary folder." ),
121 __( "Failed to write file to disk." ));
123 // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
127 // If you override this, you must provide $ext and $type!!!!
131 // Install user overrides. Did we mention that this voids your warranty?
132 if ( is_array( $overrides ) )
133 extract( $overrides, EXTR_OVERWRITE );
135 // A correct form post will pass this test.
136 if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
137 return $upload_error_handler( $file, __( 'Invalid form submission.' ));
139 // A successful upload will pass this test. It makes no sense to override this one.
140 if ( $file['error'] > 0 )
141 return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
143 // A non-empty file will pass this test.
144 if ( $test_size && !($file['size'] > 0 ) )
145 return $upload_error_handler( $file, __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini.' ));
147 // A properly uploaded file will pass this test. There should be no reason to override this one.
148 if (! @ is_uploaded_file( $file['tmp_name'] ) )
149 return $upload_error_handler( $file, __( 'Specified file failed upload test.' ));
151 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
153 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
155 extract( $wp_filetype );
157 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
158 return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
161 $ext = ltrim(strrchr($file['name'], '.'), '.');
164 $type = $file['type'];
167 // A writable uploads dir will pass this test. Again, there's no point overriding this one.
168 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
169 return $upload_error_handler( $file, $uploads['error'] );
171 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
173 // Move the file to the uploads dir
174 $new_file = $uploads['path'] . "/$filename";
175 if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) {
176 return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
179 // Set correct file permissions
180 $stat = stat( dirname( $new_file ));
181 $perms = $stat['mode'] & 0000666;
182 @ chmod( $new_file, $perms );
185 $url = $uploads['url'] . "/$filename";
187 $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
193 * Downloads a url to a local file using the Snoopy HTTP Class
195 * @param string $url the URL of the file to download
196 * @return mixed WP_Error on failure, string Filename on success.
198 function download_url( $url ) {
199 //WARNING: The file is not automatically deleted, The script must unlink() the file.
201 return new WP_Error('http_no_url', __('Invalid URL Provided'));
203 $tmpfname = tempnam(get_temp_dir(), 'wpupdate');
205 return new WP_Error('http_no_file', __('Could not create Temporary file'));
207 $handle = @fopen($tmpfname, 'w');
209 return new WP_Error('http_no_file', __('Could not create Temporary file'));
211 require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
212 $snoopy = new Snoopy();
213 $snoopy->fetch($url);
215 if( $snoopy->status != '200' ){
218 return new WP_Error('http_404', trim($snoopy->response_code));
220 fwrite($handle, $snoopy->results);
226 function unzip_file($file, $to) {
227 global $wp_filesystem;
229 if ( ! $wp_filesystem || !is_object($wp_filesystem) )
230 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
232 $fs =& $wp_filesystem;
234 require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
236 $archive = new PclZip($file);
238 // Is the archive valid?
239 if ( false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING)) )
240 return new WP_Error('incompatible_archive', __('Incompatible archive'), $archive->errorInfo(true));
242 if ( 0 == count($archive_files) )
243 return new WP_Error('empty_archive', __('Empty archive'));
245 $to = trailingslashit($to);
246 $path = explode('/', $to);
248 for ( $j = 0; $j < count($path) - 1; $j++ ) {
249 $tmppath .= $path[$j] . '/';
250 if ( ! $fs->is_dir($tmppath) )
251 $fs->mkdir($tmppath, 0755);
254 foreach ($archive_files as $file) {
255 $path = explode('/', $file['filename']);
258 // Loop through each of the items and check that the folder exists.
259 for ( $j = 0; $j < count($path) - 1; $j++ ) {
260 $tmppath .= $path[$j] . '/';
261 if ( ! $fs->is_dir($to . $tmppath) )
262 if ( !$fs->mkdir($to . $tmppath, 0755) )
263 return new WP_Error('mkdir_failed', __('Could not create directory'));
266 // We've made sure the folders are there, so let's extract the file now:
267 if ( ! $file['folder'] )
268 if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
269 return new WP_Error('copy_failed', __('Could not copy file'));
270 $fs->chmod($to . $file['filename'], 0644);
276 function copy_dir($from, $to) {
277 global $wp_filesystem;
279 $dirlist = $wp_filesystem->dirlist($from);
281 $from = trailingslashit($from);
282 $to = trailingslashit($to);
284 foreach ( (array) $dirlist as $filename => $fileinfo ) {
285 if ( 'f' == $fileinfo['type'] ) {
286 if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
288 $wp_filesystem->chmod($to . $filename, 0644);
289 } elseif ( 'd' == $fileinfo['type'] ) {
290 if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
292 if ( !copy_dir($from . $filename, $to . $filename) )
300 function WP_Filesystem( $args = false, $preference = false ) {
301 global $wp_filesystem;
303 $method = get_filesystem_method($preference);
307 require_once('class-wp-filesystem-'.$method.'.php');
308 $method = "WP_Filesystem_$method";
310 $wp_filesystem = new $method($args);
312 if ( $wp_filesystem->errors->get_error_code() )
315 if ( !$wp_filesystem->connect() )
316 return false; //There was an erorr connecting to the server.
321 function get_filesystem_method() {
322 $tempFile = tempnam(get_temp_dir(), 'WPU');
324 if ( getmyuid() == fileowner($tempFile) ) {
331 if ( extension_loaded('ftp') ) return 'ftpext';
332 if ( extension_loaded('sockets') || function_exists('fsockopen') ) return 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread