PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

/wp-admin/includes/ -> file.php (source)

   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      // You may define your own function and pass the name in $overrides['upload_error_handler']
  92      $upload_error_handler = 'wp_handle_upload_error';
  93  
  94      // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
  95      $action = 'wp_handle_upload';
  96  
  97      // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
  98      $upload_error_strings = array( false,
  99          __( "The uploaded file exceeds the <code>upload_max_filesize</code> directive in <code>php.ini</code>." ),
 100          __( "The uploaded file exceeds the <em>MAX_FILE_SIZE</em> directive that was specified in the HTML form." ),
 101          __( "The uploaded file was only partially uploaded." ),
 102          __( "No file was uploaded." ),
 103          __( "Missing a temporary folder." ),
 104          __( "Failed to write file to disk." ));
 105  
 106      // All tests are on by default. Most can be turned off by $override[{test_name}] = false;
 107      $test_form = true;
 108      $test_size = true;
 109  
 110      // If you override this, you must provide $ext and $type!!!!
 111      $test_type = true;
 112  
 113      // Install user overrides. Did we mention that this voids your warranty?
 114      if ( is_array( $overrides ) )
 115          extract( $overrides, EXTR_OVERWRITE );
 116  
 117      // A correct form post will pass this test.
 118      if ( $test_form && (!isset( $_POST['action'] ) || ($_POST['action'] != $action ) ) )
 119          return $upload_error_handler( $file, __( 'Invalid form submission.' ));
 120  
 121      // A successful upload will pass this test. It makes no sense to override this one.
 122      if ( $file['error'] > 0 )
 123          return $upload_error_handler( $file, $upload_error_strings[$file['error']] );
 124  
 125      // A non-empty file will pass this test.
 126      if ( $test_size && !($file['size'] > 0 ) )
 127          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.' ));
 128  
 129      // A properly uploaded file will pass this test. There should be no reason to override this one.
 130      if (! @ is_uploaded_file( $file['tmp_name'] ) )
 131          return $upload_error_handler( $file, __( 'Specified file failed upload test.' ));
 132  
 133      // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
 134      if ( $test_type ) {
 135          $wp_filetype = wp_check_filetype( $file['name'], $mimes );
 136  
 137          extract( $wp_filetype );
 138  
 139          if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
 140              return $upload_error_handler( $file, __( 'File type does not meet security guidelines. Try another.' ));
 141  
 142          if ( !$ext )
 143              $ext = ltrim(strrchr($file['name'], '.'), '.');
 144      }
 145  
 146      // A writable uploads dir will pass this test. Again, there's no point overriding this one.
 147      if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
 148          return $upload_error_handler( $file, $uploads['error'] );
 149  
 150      // Increment the file number until we have a unique file to save in $dir. Use $override['unique_filename_callback'] if supplied.
 151      if ( isset( $unique_filename_callback ) && function_exists( $unique_filename_callback ) ) {
 152          $filename = $unique_filename_callback( $uploads['path'], $file['name'] );
 153      } else {
 154          $number = '';
 155          $filename = str_replace( '#', '_', $file['name'] );
 156          $filename = str_replace( array( '\\', "'" ), '', $filename );
 157          if ( empty( $ext) )
 158              $ext = '';
 159          else
 160              $ext = ".$ext";
 161          while ( file_exists( $uploads['path'] . "/$filename" ) ) {
 162              if ( '' == "$number$ext" )
 163                  $filename = $filename . ++$number . $ext;
 164              else
 165                  $filename = str_replace( "$number$ext", ++$number . $ext, $filename );
 166          }
 167          $filename = str_replace( $ext, '', $filename );
 168          $filename = sanitize_title_with_dashes( $filename ) . $ext;
 169      }
 170  
 171      // Move the file to the uploads dir
 172      $new_file = $uploads['path'] . "/$filename";
 173      if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
 174          wp_die( printf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ));
 175  
 176      // Set correct file permissions
 177      $stat = stat( dirname( $new_file ));
 178      $perms = $stat['mode'] & 0000666;
 179      @ chmod( $new_file, $perms );
 180  
 181      // Compute the URL
 182      $url = $uploads['url'] . "/$filename";
 183  
 184      $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
 185  
 186      return $return;
 187  }
 188  
 189  ?>


Generated Thu Dec 6 06:47:08 2007 for RedAlt XRefs Cross-referenced by PHPXref 0.6 and RedAlt