]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/file.php
Wordpress 2.3.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' ), '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' ), 'single.php' => __( 'Single Post' ), '404.php' => __( '404 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 validate_file( $file, $allowed_files = '' ) {
46         if ( false !== strpos( $file, '..' ))
47                 return 1;
48
49         if ( false !== strpos( $file, './' ))
50                 return 1;
51
52         if (':' == substr( $file, 1, 1 ))
53                 return 2;
54
55         if (!empty ( $allowed_files ) && (!in_array( $file, $allowed_files ) ) )
56                 return 3;
57
58         return 0;
59 }
60
61 function validate_file_to_edit( $file, $allowed_files = '' ) {
62         $file = stripslashes( $file );
63
64         $code = validate_file( $file, $allowed_files );
65
66         if (!$code )
67                 return $file;
68
69         switch ( $code ) {
70                 case 1 :
71                         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.' ));
72
73                 case 2 :
74                         wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
75
76                 case 3 :
77                         wp_die( __('Sorry, that file cannot be edited.' ));
78         }
79 }
80
81 // array wp_handle_upload ( array &file [, array overrides] )
82 // file: reference to a single element of $_FILES. Call the function once for each uploaded file.
83 // overrides: an associative array of names=>values to override default variables with extract( $overrides, EXTR_OVERWRITE ).
84 // On success, returns an associative array of file attributes.
85 // On failure, returns $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
86 function wp_handle_upload( &$file, $overrides = false ) {
87         // The default error handler.
88         if (! function_exists( 'wp_handle_upload_error' ) ) {
89                 function wp_handle_upload_error( &$file, $message ) {
90                         return array( 'error'=>$message );
91                 }
92         }
93
94         // You may define your own function and pass the name in $overrides['upload_error_handler']
95         $upload_error_handler = 'wp_handle_upload_error';
96
97         // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
98         $action = 'wp_handle_upload';
99
100         // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
101         $upload_error_strings = array( false,
102                 __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
103                 __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
104                 __( "The uploaded file was only partially uploaded." ),
105                 __( "No file was uploaded." ),
106                 __( "Missing a temporary folder." ),
107                 __( "Failed to write file to disk." ));
108
109         // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
110         $test_form = true;
111         $test_size = true;
112
113         // If you override this, you must provide $ext and $type!!!!
114         $test_type = true;
115
116         // Install user overrides. Did we mention that this voids your warranty?
117         if ( is_array( $overrides ) )
118                 extract( $overrides, EXTR_OVERWRITE );
119
120         // A correct form post will pass this test.
121         if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
122                 return $upload_error_handler( $file, __( 'Invalid form submission.' ));
123
124         // A successful upload will pass this test. It makes no sense to override this one.
125         if ( $file['error'] > 0 )
126                 return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
127
128         // A non-empty file will pass this test.
129         if ( $test_size && !($file['size'] > 0 ) )
130                 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.' ));
131
132         // A properly uploaded file will pass this test. There should be no reason to override this one.
133         if (! @ is_uploaded_file( $file['tmp_name'] ) )
134                 return $upload_error_handler( $file, __( 'Specified file failed upload test.' ));
135
136         // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
137         if ( $test_type ) {
138                 $wp_filetype = wp_check_filetype( $file['name'], $mimes );
139
140                 extract( $wp_filetype );
141
142                 if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
143                         return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
144
145                 if ( !$ext )
146                         $ext = ltrim(strrchr($file['name'], '.'), '.');
147         }
148
149         // A writable uploads dir will pass this test. Again, there's no point overriding this one.
150         if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
151                 return $upload_error_handler( $file, $uploads['error'] );
152
153         // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
154         if ( isset( $unique_filename_callback ) && function_exists( $unique_filename_callback ) ) {
155                 $filename = $unique_filename_callback( $uploads['path'], $file['name'] );
156         } else {
157                 $number = '';
158                 $filename = str_replace( '#', '_', $file['name'] );
159                 $filename = str_replace( array( '\\', "'" ), '', $filename );
160                 if ( empty( $ext) )
161                         $ext = '';
162                 else
163                         $ext = ".$ext";
164                 while ( file_exists( $uploads['path'] . "/$filename" ) ) {
165                         if ( '' == "$number$ext" )
166                                 $filename = $filename . ++$number . $ext;
167                         else
168                                 $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
169                 }
170                 $filename = str_replace( $ext, '', $filename );
171                 $filename = sanitize_title_with_dashes( $filename ) . $ext;
172         }
173
174         // Move the file to the uploads dir
175         $new_file = $uploads['path'] . "/$filename";
176         if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
177                 wp_die( printf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ));
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 ?>