[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezutils/classes/ -> ezhttpfile.php (source)

   1  <?php
   2  //
   3  // Definition of eZHTTPFile class
   4  //
   5  // Created on: <06-May-2002 10:06:57 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZHTTPFile ezhttpfile.php
  31    \ingroup eZHTTP
  32    \brief Provides access to HTTP post files
  33  
  34    This class provides easy access to files posted by clients over HTTP.
  35    The HTTP file will be present as a temporary file which can be moved
  36    to store it, if not the file will be removed when the PHP script is done.
  37  
  38  */
  39  
  40  include_once ( "lib/ezutils/classes/ezdebug.php" );
  41  include_once ( "lib/ezutils/classes/ezini.php" );
  42  
  43  define( 'EZ_UPLOADEDFILE_OK', 0 );
  44  define( 'EZ_UPLOADEDFILE_DOES_NOT_EXIST', -1 );
  45  define( 'EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT', -2 );
  46  define( 'EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE', -3 );
  47  
  48  class eZHTTPFile
  49  {
  50      /*!
  51       Initializes with a name and http variable.
  52      */
  53      function eZHTTPFile( /*! Name of the HTTP variable */ $http_name,
  54                           /*! The HTTP variable structure */ $variable )
  55      {
  56          $this->HTTPName = $http_name;
  57          $this->OriginalFilename = $variable["name"];
  58          $this->Type = $variable["type"];
  59          $mime = explode( "/", $this->Type );
  60          $this->MimeCategory = $mime[0];
  61          $this->MimePart = $mime[1];
  62          $this->Filename = $variable["tmp_name"];
  63          $this->Size = $variable["size"];
  64          $this->IsTemporary = true;
  65      }
  66  
  67      /*!
  68       \return the directory where the file should be stored.
  69      */
  70      function storageDir( $sub_dir = false )
  71      {
  72          $sys =& eZSys::instance();
  73          $storage_dir = $sys->storageDirectory();
  74          if ( $sub_dir !== false )
  75              $dir = $storage_dir . "/$sub_dir/" . $this->MimeCategory;
  76          else
  77              $dir = $storage_dir . "/" . $this->MimeCategory;
  78          return $dir;
  79      }
  80  
  81      /*!
  82       Stores the temporary file to the destination dir $dir.
  83      */
  84      function store( $sub_dir = false, $suffix = false, $mimeData = false )
  85      {
  86          include_once ( 'lib/ezfile/classes/ezdir.php' );
  87          if ( !$this->IsTemporary )
  88          {
  89              eZDebug::writeError( "Cannot store non temporary file: " . $this->Filename,
  90                                   "eZHTTPFile" );
  91              return false;
  92          }
  93          $this->IsTemporary = false;
  94  
  95          $ini =& eZINI::instance();
  96  //         $storage_dir = $ini->variable( "FileSettings", "VarDir" ) . '/' . $ini->variable( "FileSettings", "StorageDir" );
  97          $storage_dir = eZSys::storageDirectory();
  98          if ( $sub_dir !== false )
  99              $dir = $storage_dir . "/$sub_dir/";
 100          else
 101              $dir = $storage_dir . "/";
 102          if ( $mimeData )
 103              $dir = $mimeData['dirpath'];
 104  
 105          // VS-DBFILE : TODO
 106  
 107          if ( !file_exists( $dir ) )
 108          {
 109              $oldumask = umask( 0 );
 110              if ( !eZDir::mkdir( $dir, eZDir::directoryPermission(), true ) )
 111              {
 112                  umask( $oldumask );
 113                  return false;
 114              }
 115              umask( $oldumask );
 116          }
 117  
 118          if ( !$mimeData )
 119          {
 120              $dir .= $this->MimeCategory;
 121              if ( !file_exists( $dir ) )
 122              {
 123                  $oldumask = umask( 0 );
 124                  $perm = $ini->variable( "FileSettings", "StorageDirPermissions" );
 125                  if ( !eZDir::mkdir( $dir, octdec( $perm ), true ) )
 126                  {
 127                      umask( $oldumask );
 128                      return false;
 129                  }
 130                  umask( $oldumask );
 131              }
 132          }
 133  
 134          $suffixString = false;
 135          if ( $suffix != false )
 136              $suffixString = ".$suffix";
 137  
 138          if ( $mimeData )
 139          {
 140              $dest_name = $mimeData['url'];
 141          }
 142          else
 143          {
 144              $dest_name = $dir . "/" . md5( basename( $this->Filename ) . microtime() . mt_rand() ) . $suffixString;
 145          }
 146  
 147          // VS-DBFILE : TODO
 148  
 149          if ( !move_uploaded_file( $this->Filename, $dest_name ) )
 150          {
 151              eZDebug::writeError( "Failed moving uploaded file " . $this->Filename . " to destination $dest_name" );
 152              unlink( $dest_name );
 153              $ret = false;
 154          }
 155          else
 156          {
 157              $ret = true;
 158              $this->Filename = $dest_name;
 159              $perm = $ini->variable( "FileSettings", "StorageFilePermissions" );
 160              $oldumask = umask( 0 );
 161              chmod( $dest_name, octdec( $perm ) );
 162              umask( $oldumask );
 163  
 164              // Write log message to storage.log
 165              include_once ( 'lib/ezutils/classes/ezlog.php' );
 166              $storageDir = $dir . "/";
 167              eZLog::writeStorageLog( basename( $this->Filename ), $storageDir );
 168          }
 169          return $ret;
 170      }
 171  
 172      /*!
 173       \return an array with the attributes for this object.
 174      */
 175      function attributes()
 176      {
 177          return array( "original_filename",
 178                        "filename",
 179                        "filesize",
 180                        "is_temporary",
 181                        "mime_type",
 182                        "mime_type_category",
 183                        "mime_type_part" );
 184      }
 185  
 186      /*!
 187       \return true if the attribute $attr exists
 188      */
 189      function hasAttribute( $attr )
 190      {
 191          return in_array( $attr, $this->attributes() );
 192      }
 193  
 194      /*!
 195       \return the value for the attribute $attr or null if the attribute does not exist.
 196      */
 197      function &attribute( $attr )
 198      {
 199          switch ( $attr )
 200          {
 201              case "original_filename":
 202                  return $this->OriginalFilename;
 203              case "mime_type":
 204                  return $this->Type;
 205              case "mime_type_category":
 206                  return $this->MimeCategory;
 207              case "mime_type_part":
 208                  return $this->MimePart;
 209              case "filename":
 210                  return $this->Filename;
 211              case "filesize":
 212                  return $this->Size;
 213              case "is_temporary":
 214                  return $this->IsTemporary;
 215              default:
 216              {
 217                  eZDebug::writeError( "Attribute '$attr' does not exist", 'eZHTTPFile::attribute' );
 218                  $retValue = null;
 219                  return $retValue;
 220              } break;
 221          };
 222      }
 223  
 224      /*!
 225       \return true if the HTTP file $http_name can be fetched. If $maxSize is given,
 226       the function returns
 227          0 (EZ_UPLOADEDFILE_OK) if the file can be fetched,
 228         -1 (EZ_UPLOADEDFILE_DOES_NOT_EXIST) if there has been no file uploaded,
 229         -2 (EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT) if the file was uploaded but size
 230            exceeds the upload_max_size limit (set in the PHP configuration),
 231         -3 (EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE) if the file was uploaded but size
 232            exceeds $maxSize or MAX_FILE_SIZE variable in the form.
 233      */
 234      function canFetch( $http_name, $maxSize = false )
 235      {
 236          $file =& $GLOBALS["eZHTTPFile-$http_name"];
 237          if ( get_class( $file ) != "ezhttpfile" )
 238          {
 239              if ( $maxSize === false )
 240              {
 241                  return isset( $_FILES[$http_name] ) and $_FILES[$http_name]['name'] != "" and $_FILES[$http_name]['error'] == 0;
 242              }
 243  
 244              if ( isset( $_FILES[$http_name] ) and $_FILES[$http_name]['name'] != "" )
 245              {
 246                  switch ( $_FILES[$http_name]['error'] )
 247                  {
 248                      case ( UPLOAD_ERR_NO_FILE ):
 249                      {
 250                          return EZ_UPLOADEDFILE_DOES_NOT_EXIST;
 251                      }break;
 252  
 253                      case ( UPLOAD_ERR_INI_SIZE ):
 254                      {
 255                          return EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT;
 256                      }break;
 257  
 258                      case ( UPLOAD_ERR_FORM_SIZE ):
 259                      {
 260                          return EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE;
 261                      }break;
 262  
 263                      default:
 264                      {
 265                          return ( $maxSize == 0 || $_FILES[$http_name]['size'] <= $maxSize )? EZ_UPLOADEDFILE_OK:
 266                                                                                               EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE;
 267                      }
 268                  }
 269              }
 270              else
 271              {
 272                  return EZ_UPLOADEDFILE_DOES_NOT_EXIST;
 273              }
 274          }
 275          if ( $maxSize === false )
 276              return EZ_UPLOADEDFILE_OK;
 277          else
 278              return true;
 279      }
 280  
 281      /*!
 282       Fetches the HTTP file named $http_name and returns a eZHTTPFile object,
 283       or null if the file could not be fetched.
 284      */
 285      function &fetch( $http_name )
 286      {
 287          $file =& $GLOBALS["eZHTTPFile-$http_name"];
 288          if ( get_class( $file ) != "ezhttpfile" )
 289          {
 290              $file = null;
 291  
 292              if ( isset( $_FILES[$http_name] ) and
 293                   $_FILES[$http_name]["name"] != "" )
 294              {
 295                  include_once ( 'lib/ezutils/classes/ezmimetype.php' );
 296                  include_once ( 'lib/ezfile/classes/ezfile.php' );
 297                  $mimeType = eZMimeType::findByURL( $_FILES[$http_name]['name'] );
 298                  $_FILES[$http_name]['type'] = $mimeType['name'];
 299                  $file = new eZHTTPFile( $http_name, $_FILES[$http_name] );
 300              }
 301              else
 302                  eZDebug::writeError( "Unknown file for post variable: $http_name",
 303                                       "eZHTTPFile" );
 304          }
 305          return $file;
 306      }
 307  
 308      /*!
 309       Changes the MIME-Type to $mime.
 310      */
 311      function setMimeType( $mime )
 312      {
 313          $this->Type = $mime;
 314          list ( $this->MimeCategory, $this->MimePart ) = explode( '/', $mime, 2 );
 315      }
 316  
 317      /// The name of the HTTP file
 318      var $HTTPName;
 319      /// The original name of the file from the client
 320      var $OriginalFilename;
 321      /// The mime type of the file
 322      var $Type;
 323      /// The mimetype category (first part)
 324      var $MimeCategory;
 325      /// The mimetype type (second part)
 326      var $MimePart;
 327      /// The local filename
 328      var $Filename;
 329      /// The size of the local file
 330      var $Size;
 331      /// Whether the file is a temporary file or if it has been moved(stored).
 332      var $IsTemporary;
 333  }
 334  
 335  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7