]> scripts.mit.edu Git - autoinstallsdev/wordpress.git/blob - wp-admin/includes/file.php
Wordpress 2.5.1
[autoinstallsdev/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 = ABSPATH.$file;
40         }
41
42         return $real_file;
43 }
44
45 function get_temp_dir() {
46         if ( defined('WP_TEMP_DIR') )
47                 return trailingslashit(WP_TEMP_DIR);
48
49         $temp = ABSPATH . 'wp-content/';
50         if ( is_dir($temp) && is_writable($temp) )
51                 return $temp;
52
53         if  ( function_exists('sys_get_temp_dir') )
54                 return trailingslashit(sys_get_temp_dir());
55
56         return '/tmp/';
57 }
58
59 function validate_file( $file, $allowed_files = '' ) {
60         if ( false !== strpos( $file, '..' ))
61                 return 1;
62
63         if ( false !== strpos( $file, './' ))
64                 return 1;
65
66         if (':' == substr( $file, 1, 1 ))
67                 return 2;
68
69         if (!empty ( $allowed_files ) && (!in_array( $file, $allowed_files ) ) )
70                 return 3;
71
72         return 0;
73 }
74
75 function validate_file_to_edit( $file, $allowed_files = '' ) {
76         $file = stripslashes( $file );
77
78         $code = validate_file( $file, $allowed_files );
79
80         if (!$code )
81                 return $file;
82
83         switch ( $code ) {
84                 case 1 :
85                         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.' ));
86
87                 case 2 :
88                         wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
89
90                 case 3 :
91                         wp_die( __('Sorry, that file cannot be edited.' ));
92         }
93 }
94
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 );
105                 }
106         }
107
108         // You may define your own function and pass the name in $overrides['upload_error_handler']
109         $upload_error_handler = 'wp_handle_upload_error';
110
111         // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
112         $action = 'wp_handle_upload';
113
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." ));
122
123         // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
124         $test_form = true;
125         $test_size = true;
126
127         // If you override this, you must provide $ext and $type!!!!
128         $test_type = true;
129         $mimes = false;
130
131         // Install user overrides. Did we mention that this voids your warranty?
132         if ( is_array( $overrides ) )
133                 extract( $overrides, EXTR_OVERWRITE );
134
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.' ));
138
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']] );
142
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.' ));
146
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.' ));
150
151         // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
152         if ( $test_type ) {
153                 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
154
155                 extract( $wp_filetype );
156
157                 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
158                         return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
159
160                 if ( !$ext )
161                         $ext = ltrim(strrchr($file['name'], '.'), '.');
162
163                 if ( !$type )
164                         $type = $file['type'];
165         }
166
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'] );
170
171         $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
172
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'] ) );
177         }
178
179         // Set correct file permissions
180         $stat = stat( dirname( $new_file ));
181         $perms = $stat['mode'] & 0000666;
182         @ chmod( $new_file, $perms );
183
184         // Compute the URL
185         $url = $uploads['url'] . "/$filename";
186
187         $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
188
189         return $return;
190 }
191
192 /**
193 * Downloads a url to a local file using the Snoopy HTTP Class
194 *
195 * @param string $url the URL of the file to download
196 * @return mixed WP_Error on failure, string Filename on success.
197 */
198 function download_url( $url ) {
199         //WARNING: The file is not automatically deleted, The script must unlink() the file.
200         if( ! $url )
201                 return new WP_Error('http_no_url', __('Invalid URL Provided'));
202
203         $tmpfname = tempnam(get_temp_dir(), 'wpupdate');
204         if( ! $tmpfname )
205                 return new WP_Error('http_no_file', __('Could not create Temporary file'));
206
207         $handle = @fopen($tmpfname, 'w');
208         if( ! $handle )
209                 return new WP_Error('http_no_file', __('Could not create Temporary file'));
210
211         require_once( ABSPATH . 'wp-includes/class-snoopy.php' );
212         $snoopy = new Snoopy();
213         $snoopy->fetch($url);
214
215         if( $snoopy->status != '200' ){
216                 fclose($handle);
217                 unlink($tmpfname);
218                 return new WP_Error('http_404', trim($snoopy->response_code));
219         }
220         fwrite($handle, $snoopy->results);
221         fclose($handle);
222
223         return $tmpfname;
224 }
225
226 function unzip_file($file, $to) {
227         global $wp_filesystem;
228
229         if ( ! $wp_filesystem || !is_object($wp_filesystem) )
230                 return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
231
232         $fs =& $wp_filesystem;
233
234         require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
235
236         $archive = new PclZip($file);
237
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));
241
242         if ( 0 == count($archive_files) )
243                 return new WP_Error('empty_archive', __('Empty archive'));
244
245         $to = trailingslashit($to);
246         $path = explode('/', $to);
247         $tmppath = '';
248         for ( $j = 0; $j < count($path) - 1; $j++ ) {
249                 $tmppath .= $path[$j] . '/';
250                 if ( ! $fs->is_dir($tmppath) )
251                         $fs->mkdir($tmppath, 0755);
252         }
253
254         foreach ($archive_files as $file) {
255                 $path = explode('/', $file['filename']);
256                 $tmppath = '';
257
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'));
264                 }
265
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);
271         }
272
273         return true;
274 }
275
276 function copy_dir($from, $to) {
277         global $wp_filesystem;
278
279         $dirlist = $wp_filesystem->dirlist($from);
280
281         $from = trailingslashit($from);
282         $to = trailingslashit($to);
283
284         foreach ( (array) $dirlist as $filename => $fileinfo ) {
285                 if ( 'f' == $fileinfo['type'] ) {
286                         if ( ! $wp_filesystem->copy($from . $filename, $to . $filename, true) )
287                                 return false;
288                         $wp_filesystem->chmod($to . $filename, 0644);
289                 } elseif ( 'd' == $fileinfo['type'] ) {
290                         if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
291                                 return false;
292                         if ( !copy_dir($from . $filename, $to . $filename) )
293                                 return false;
294                 }
295         }
296
297         return true;
298 }
299
300 function WP_Filesystem( $args = false, $preference = false ) {
301         global $wp_filesystem;
302
303         $method = get_filesystem_method($preference);
304         if ( ! $method )
305                 return false;
306
307         require_once('class-wp-filesystem-'.$method.'.php');
308         $method = "WP_Filesystem_$method";
309
310         $wp_filesystem = new $method($args);
311
312         if ( $wp_filesystem->errors->get_error_code() )
313                 return false;
314
315         if ( !$wp_filesystem->connect() )
316                 return false; //There was an erorr connecting to the server.
317
318         return true;
319 }
320
321 function get_filesystem_method() {
322         $tempFile = tempnam(get_temp_dir(), 'WPU');
323
324         if ( getmyuid() == fileowner($tempFile) ) {
325                 unlink($tempFile);
326                 return 'direct';
327         } else {
328                 unlink($tempFile);
329         }
330
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
333         return false;
334 }
335
336 ?>