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 = WP_CONTENT_DIR . $file;
44 //$folder = Full path to folder
45 //$levels = Levels of folders to follow, Default: 100 (PHP Loop limit)
46 function list_files( $folder = '', $levels = 100 ) {
54 if ( $dir = @opendir( $folder ) ) {
55 while (($file = readdir( $dir ) ) !== false ) {
56 if ( in_array($file, array('.', '..') ) )
58 if ( is_dir( $folder . '/' . $file ) ) {
59 $files2 = list_files( $folder . '/' . $file, $levels - 1);
61 $files = array_merge($files, $files2 );
63 $files[] = $folder . '/' . $file . '/';
65 $files[] = $folder . '/' . $file;
73 function get_temp_dir() {
74 if ( defined('WP_TEMP_DIR') )
75 return trailingslashit(WP_TEMP_DIR);
77 $temp = WP_CONTENT_DIR . '/';
78 if ( is_dir($temp) && is_writable($temp) )
81 if ( function_exists('sys_get_temp_dir') )
82 return trailingslashit(sys_get_temp_dir());
87 function wp_tempnam($filename = '', $dir = ''){
89 $dir = get_temp_dir();
90 $filename = basename($filename);
91 if ( empty($filename) )
94 $filename = $dir . wp_unique_filename($dir, $filename);
99 function validate_file_to_edit( $file, $allowed_files = '' ) {
100 $file = stripslashes( $file );
102 $code = validate_file( $file, $allowed_files );
109 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.' ));
112 wp_die( __('Sorry, can’t call files with their real path.' ));
115 wp_die( __('Sorry, that file cannot be edited.' ));
119 // array wp_handle_upload ( array &file [, array overrides] )
120 // file: reference to a single element of $_FILES. Call the function once for each uploaded file.
121 // overrides: an associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
122 // On success, returns an associative array of file attributes.
123 // On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
124 function wp_handle_upload( &$file, $overrides = false ) {
125 // The default error handler.
126 if (! function_exists( 'wp_handle_upload_error' ) ) {
127 function wp_handle_upload_error( &$file, $message ) {
128 return array( 'error'=>$message );
132 // You may define your own function and pass the name in $overrides['upload_error_handler']
133 $upload_error_handler = 'wp_handle_upload_error';
135 // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
136 $action = 'wp_handle_upload';
138 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
139 $upload_error_strings = array( false,
140 __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
141 __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
142 __( "The uploaded file was only partially uploaded." ),
143 __( "No file was uploaded." ),
144 __( "Missing a temporary folder." ),
145 __( "Failed to write file to disk." ));
147 // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
151 // If you override this, you must provide $ext and $type!!!!
155 // Install user overrides. Did we mention that this voids your warranty?
156 if ( is_array( $overrides ) )
157 extract( $overrides, EXTR_OVERWRITE );
159 // A correct form post will pass this test.
160 if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
161 return $upload_error_handler( $file, __( 'Invalid form submission.' ));
163 // A successful upload will pass this test. It makes no sense to override this one.
164 if ( $file['error'] > 0 )
165 return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
167 // A non-empty file will pass this test.
168 if ( $test_size && !($file['size'] > 0 ) )
169 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.' ));
171 // A properly uploaded file will pass this test. There should be no reason to override this one.
172 if (! @ is_uploaded_file( $file['tmp_name'] ) )
173 return $upload_error_handler( $file, __( 'Specified file failed upload test.' ));
175 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
177 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
179 extract( $wp_filetype );
181 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
182 return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
185 $ext = ltrim(strrchr($file['name'], '.'), '.');
188 $type = $file['type'];
191 // A writable uploads dir will pass this test. Again, there's no point overriding this one.
192 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
193 return $upload_error_handler( $file, $uploads['error'] );
195 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
197 // Move the file to the uploads dir
198 $new_file = $uploads['path'] . "/$filename";
199 if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) ) {
200 return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
203 // Set correct file permissions
204 $stat = stat( dirname( $new_file ));
205 $perms = $stat['mode'] & 0000666;
206 @ chmod( $new_file, $perms );
209 $url = $uploads['url'] . "/$filename";
211 $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
215 // Pass this function an array similar to that of a $_FILES POST array.
216 function wp_handle_sideload( &$file, $overrides = false ) {
217 // The default error handler.
218 if (! function_exists( 'wp_handle_upload_error' ) ) {
219 function wp_handle_upload_error( &$file, $message ) {
220 return array( 'error'=>$message );
224 // You may define your own function and pass the name in $overrides['upload_error_handler']
225 $upload_error_handler = 'wp_handle_upload_error';
227 // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
228 $action = 'wp_handle_sideload';
230 // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
231 $upload_error_strings = array( false,
232 __( "The file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
233 __( "The file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
234 __( "The file was only partially uploaded." ),
235 __( "No file was sent." ),
236 __( "Missing a temporary folder." ),
237 __( "Failed to write file to disk." ));
239 // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
243 // If you override this, you must provide $ext and $type!!!!
247 // Install user overrides. Did we mention that this voids your warranty?
248 if ( is_array( $overrides ) )
249 extract( $overrides, EXTR_OVERWRITE );
251 // A correct form post will pass this test.
252 if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
253 return $upload_error_handler( $file, __( 'Invalid form submission.' ));
255 // A successful upload will pass this test. It makes no sense to override this one.
256 if ( $file['error'] > 0 )
257 return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
259 // A non-empty file will pass this test.
260 if ( $test_size && !(filesize($file['tmp_name']) > 0 ) )
261 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.' ));
263 // A properly uploaded file will pass this test. There should be no reason to override this one.
264 if (! @ is_file( $file['tmp_name'] ) )
265 return $upload_error_handler( $file, __( 'Specified file does not exist.' ));
267 // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
269 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
271 extract( $wp_filetype );
273 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
274 return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
277 $ext = ltrim(strrchr($file['name'], '.'), '.');
280 $type = $file['type'];
283 // A writable uploads dir will pass this test. Again, there's no point overriding this one.
284 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
285 return $upload_error_handler( $file, $uploads['error'] );
287 $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
289 // Strip the query strings.
290 $filename = str_replace('?','-', $filename);
291 $filename = str_replace('&','-', $filename);
293 // Move the file to the uploads dir
294 $new_file = $uploads['path'] . "/$filename";
295 if ( false === @ rename( $file['tmp_name'], $new_file ) ) {
296 return $upload_error_handler( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );
299 // Set correct file permissions
300 $stat = stat( dirname( $new_file ));
301 $perms = $stat['mode'] & 0000666;
302 @ chmod( $new_file, $perms );
305 $url = $uploads['url'] . "/$filename";
307 $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
313 * Downloads a url to a local file using the Snoopy HTTP Class
315 * @param string $url the URL of the file to download
316 * @return mixed WP_Error on failure, string Filename on success.
318 function download_url( $url ) {
319 //WARNING: The file is not automatically deleted, The script must unlink() the file.
321 return new WP_Error('http_no_url', __('Invalid URL Provided'));
323 $tmpfname = wp_tempnam($url);
325 return new WP_Error('http_no_file', __('Could not create Temporary file'));
327 $handle = @fopen($tmpfname, 'w');
329 return new WP_Error('http_no_file', __('Could not create Temporary file'));
331 require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
332 $snoopy = new Snoopy();
333 $snoopy->fetch($url);
335 if( $snoopy->status != '200' ){
338 return new WP_Error('http_404', trim($snoopy->response_code));
340 fwrite($handle, $snoopy->results);
346 function unzip_file($file, $to) {
347 global $wp_filesystem;
349 if ( ! $wp_filesystem || !is_object($wp_filesystem) )
350 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
352 $fs =& $wp_filesystem;
354 require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
356 $archive = new PclZip($file);
358 // Is the archive valid?
359 if ( false == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING)) )
360 return new WP_Error('incompatible_archive', __('Incompatible archive'), $archive->errorInfo(true));
362 if ( 0 == count($archive_files) )
363 return new WP_Error('empty_archive', __('Empty archive'));
365 $to = trailingslashit($to);
366 $path = explode('/', $to);
368 for ( $j = 0; $j < count($path) - 1; $j++ ) {
369 $tmppath .= $path[$j] . '/';
370 if ( ! $fs->is_dir($tmppath) )
371 $fs->mkdir($tmppath, 0755);
374 foreach ($archive_files as $file) {
375 $path = explode('/', $file['filename']);
378 // Loop through each of the items and check that the folder exists.
379 for ( $j = 0; $j < count($path) - 1; $j++ ) {
380 $tmppath .= $path[$j] . '/';
381 if ( ! $fs->is_dir($to . $tmppath) )
382 if ( !$fs->mkdir($to . $tmppath, 0755) )
383 return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $tmppath);
386 // We've made sure the folders are there, so let's extract the file now:
387 if ( ! $file['folder'] )
388 if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
389 return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']);
390 $fs->chmod($to . $file['filename'], 0644);
396 function copy_dir($from, $to) {
397 global $wp_filesystem;
399 $dirlist = $wp_filesystem->dirlist($from);
401 $from = trailingslashit($from);
402 $to = trailingslashit($to);
404 foreach ( (array) $dirlist as $filename => $fileinfo ) {
405 if ( 'f' == $fileinfo['type'] ) {
406 if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
407 return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
408 $wp_filesystem->chmod($to . $filename, 0644);
409 } elseif ( 'd' == $fileinfo['type'] ) {
410 if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
411 return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
412 $result = copy_dir($from . $filename, $to . $filename);
413 if ( is_wp_error($result) )
419 function WP_Filesystem( $args = false ) {
420 global $wp_filesystem;
422 require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
424 $method = get_filesystem_method();
429 $abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-'.$method.'.php', $method);
430 if( ! file_exists($abstraction_file) )
433 require_once($abstraction_file);
434 $method = "WP_Filesystem_$method";
436 $wp_filesystem = new $method($args);
438 if ( $wp_filesystem->errors->get_error_code() )
441 if ( !$wp_filesystem->connect() )
442 return false; //There was an erorr connecting to the server.
447 function get_filesystem_method() {
449 if( function_exists('getmyuid') && function_exists('fileowner') ){
450 $temp_file = wp_tempnam();
451 if ( getmyuid() == fileowner($temp_file) )
456 if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
457 if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
458 return apply_filters('filesystem_method', $method);