]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/file.php
Wordpress 2.6.2-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / file.php
1 <?php
2
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 )' ),
4         // Deprecated files
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;
8
9         if ( isset( $wp_file_descriptions[basename( $file )] ) ) {
10                 return $wp_file_descriptions[basename( $file )];
11         }
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 ))
15                         return $name[1];
16         }
17
18         return basename( $file );
19 }
20
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 );
28         } else {
29                 $home_path = ABSPATH;
30         }
31
32         return $home_path;
33 }
34
35 function get_real_file_to_edit( $file ) {
36         if ('index.php' == $file || '.htaccess' == $file ) {
37                 $real_file = get_home_path() . $file;
38         } else {
39                 $real_file = WP_CONTENT_DIR . $file;
40         }
41
42         return $real_file;
43 }
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 ) {
47         if( empty($folder) )
48                 return false;
49
50         if( ! $levels )
51                 return false;
52
53         $files = array();
54         if ( $dir = @opendir( $folder ) ) {
55                 while (($file = readdir( $dir ) ) !== false ) {
56                         if ( in_array($file, array('.', '..') ) )
57                                 continue;
58                         if ( is_dir( $folder . '/' . $file ) ) {
59                                 $files2 = list_files( $folder . '/' . $file, $levels - 1);
60                                 if( $files2 )
61                                         $files = array_merge($files, $files2 );
62                                 else
63                                         $files[] = $folder . '/' . $file . '/';
64                         } else {
65                                 $files[] = $folder . '/' . $file;
66                         }
67                 }
68         }
69         @closedir( $dir );
70         return $files;
71 }
72
73 function get_temp_dir() {
74         if ( defined('WP_TEMP_DIR') )
75                 return trailingslashit(WP_TEMP_DIR);
76
77         $temp = WP_CONTENT_DIR . '/';
78         if ( is_dir($temp) && is_writable($temp) )
79                 return $temp;
80
81         if  ( function_exists('sys_get_temp_dir') )
82                 return trailingslashit(sys_get_temp_dir());
83
84         return '/tmp/';
85 }
86
87 function wp_tempnam($filename = '', $dir = ''){
88         if ( empty($dir) )
89                 $dir = get_temp_dir();
90         $filename = basename($filename);
91         if ( empty($filename) )
92                 $filename = time();
93
94         $filename = $dir . wp_unique_filename($dir, $filename);
95         touch($filename);
96         return $filename;
97 }
98
99 function validate_file_to_edit( $file, $allowed_files = '' ) {
100         $file = stripslashes( $file );
101
102         $code = validate_file( $file, $allowed_files );
103
104         if (!$code )
105                 return $file;
106
107         switch ( $code ) {
108                 case 1 :
109                         wp_die( __('Sorry, can&#8217;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.' ));
110
111                 case 2 :
112                         wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
113
114                 case 3 :
115                         wp_die( __('Sorry, that file cannot be edited.' ));
116         }
117 }
118
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 );
129                 }
130         }
131
132         // You may define your own function and pass the name in $overrides['upload_error_handler']
133         $upload_error_handler = 'wp_handle_upload_error';
134
135         // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
136         $action = 'wp_handle_upload';
137
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." ));
146
147         // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
148         $test_form = true;
149         $test_size = true;
150
151         // If you override this, you must provide $ext and $type!!!!
152         $test_type = true;
153         $mimes = false;
154
155         // Install user overrides. Did we mention that this voids your warranty?
156         if ( is_array( $overrides ) )
157                 extract( $overrides, EXTR_OVERWRITE );
158
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.' ));
162
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']] );
166
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.' ));
170
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.' ));
174
175         // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
176         if ( $test_type ) {
177                 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
178
179                 extract( $wp_filetype );
180
181                 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
182                         return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
183
184                 if ( !$ext )
185                         $ext = ltrim(strrchr($file['name'], '.'), '.');
186
187                 if ( !$type )
188                         $type = $file['type'];
189         }
190
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'] );
194
195         $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
196
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'] ) );
201         }
202
203         // Set correct file permissions
204         $stat = stat( dirname( $new_file ));
205         $perms = $stat['mode'] & 0000666;
206         @ chmod( $new_file, $perms );
207
208         // Compute the URL
209         $url = $uploads['url'] . "/$filename";
210
211         $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
212
213         return $return;
214 }
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 );
221                 }
222         }
223
224         // You may define your own function and pass the name in $overrides['upload_error_handler']
225         $upload_error_handler = 'wp_handle_upload_error';
226
227         // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
228         $action = 'wp_handle_sideload';
229
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." ));
238
239         // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
240         $test_form = true;
241         $test_size = true;
242
243         // If you override this, you must provide $ext and $type!!!!
244         $test_type = true;
245         $mimes = false;
246
247         // Install user overrides. Did we mention that this voids your warranty?
248         if ( is_array( $overrides ) )
249                 extract( $overrides, EXTR_OVERWRITE );
250
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.' ));
254
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']] );
258
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.' ));
262
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.' ));
266
267         // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
268         if ( $test_type ) {
269                 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
270
271                 extract( $wp_filetype );
272
273                 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
274                         return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
275
276                 if ( !$ext )
277                         $ext = ltrim(strrchr($file['name'], '.'), '.');
278
279                 if ( !$type )
280                         $type = $file['type'];
281         }
282
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'] );
286
287         $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
288         
289         // Strip the query strings.
290         $filename = str_replace('?','-', $filename);
291         $filename = str_replace('&','-', $filename);
292         
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'] ) );
297         }
298
299         // Set correct file permissions
300         $stat = stat( dirname( $new_file ));
301         $perms = $stat['mode'] & 0000666;
302         @ chmod( $new_file, $perms );
303
304         // Compute the URL
305         $url = $uploads['url'] . "/$filename";
306
307         $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
308
309         return $return;
310 }
311
312 /**
313 * Downloads a url to a local file using the Snoopy HTTP Class
314 *
315 * @param string $url the URL of the file to download
316 * @return mixed WP_Error on failure, string Filename on success.
317 */
318 function download_url( $url ) {
319         //WARNING: The file is not automatically deleted, The script must unlink() the file.
320         if( ! $url )
321                 return new WP_Error('http_no_url', __('Invalid URL Provided'));
322
323         $tmpfname = wp_tempnam($url);
324         if( ! $tmpfname )
325                 return new WP_Error('http_no_file', __('Could not create Temporary file'));
326
327         $handle = @fopen($tmpfname, 'w');
328         if( ! $handle )
329                 return new WP_Error('http_no_file', __('Could not create Temporary file'));
330
331         require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
332         $snoopy = new Snoopy();
333         $snoopy->fetch($url);
334
335         if( $snoopy->status != '200' ){
336                 fclose($handle);
337                 unlink($tmpfname);
338                 return new WP_Error('http_404', trim($snoopy->response_code));
339         }
340         fwrite($handle, $snoopy->results);
341         fclose($handle);
342
343         return $tmpfname;
344 }
345
346 function unzip_file($file, $to) {
347         global $wp_filesystem;
348
349         if ( ! $wp_filesystem || !is_object($wp_filesystem) )
350                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
351
352         $fs =& $wp_filesystem;
353
354         require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
355
356         $archive = new PclZip($file);
357
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));
361
362         if ( 0 == count($archive_files) )
363                 return new WP_Error('empty_archive', __('Empty archive'));
364
365         $to = trailingslashit($to);
366         $path = explode('/', $to);
367         $tmppath = '';
368         for ( $j = 0; $j < count($path) - 1; $j++ ) {
369                 $tmppath .= $path[$j] . '/';
370                 if ( ! $fs->is_dir($tmppath) )
371                         $fs->mkdir($tmppath, 0755);
372         }
373
374         foreach ($archive_files as $file) {
375                 $path = explode('/', $file['filename']);
376                 $tmppath = '';
377
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);
384                 }
385
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);
391         }
392
393         return true;
394 }
395
396 function copy_dir($from, $to) {
397         global $wp_filesystem;
398
399         $dirlist = $wp_filesystem->dirlist($from);
400
401         $from = trailingslashit($from);
402         $to = trailingslashit($to);
403
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) )
414                                 return $result;
415                 }
416         }
417 }
418
419 function WP_Filesystem( $args = false ) {
420         global $wp_filesystem;
421
422         require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
423
424         $method = get_filesystem_method();
425
426         if ( ! $method )
427                 return false;
428
429         $abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-'.$method.'.php', $method);
430         if( ! file_exists($abstraction_file) )
431                 return;
432
433         require_once($abstraction_file);
434         $method = "WP_Filesystem_$method";
435
436         $wp_filesystem = new $method($args);
437
438         if ( $wp_filesystem->errors->get_error_code() )
439                 return false;
440
441         if ( !$wp_filesystem->connect() )
442                 return false; //There was an erorr connecting to the server.
443
444         return true;
445 }
446
447 function get_filesystem_method() {
448         $method = false;
449         if( function_exists('getmyuid') && function_exists('fileowner') ){
450                 $temp_file = wp_tempnam();
451                 if ( getmyuid() == fileowner($temp_file) )
452                         $method = 'direct';
453                 unlink($temp_file);
454         }
455
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);
459 }
460
461 ?>