root/tags/1.3/wp-admin/includes/file.php

Revision 1133, 7.3 kB (checked in by donncha, 1 year ago)

Added (back in?) "wp_handle_upload_prefilter" filter to filter uploads before they go in blog's directory, fixes #478

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