eZ Publish  [4.0]
ezhttpfile.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZHTTPFile class
00004 //
00005 // Created on: <06-May-2002 10:06:57 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZHTTPFile ezhttpfile.php
00033   \ingroup eZHTTP
00034   \brief Provides access to HTTP post files
00035 
00036   This class provides easy access to files posted by clients over HTTP.
00037   The HTTP file will be present as a temporary file which can be moved
00038   to store it, if not the file will be removed when the PHP script is done.
00039 
00040 */
00041 
00042 require_once( "lib/ezutils/classes/ezdebug.php" );
00043 //include_once( "lib/ezutils/classes/ezini.php" );
00044 
00045 class eZHTTPFile
00046 {
00047     const UPLOADEDFILE_OK = 0;
00048     const UPLOADEDFILE_DOES_NOT_EXIST = -1;
00049     const UPLOADEDFILE_EXCEEDS_PHP_LIMIT = -2;
00050     const UPLOADEDFILE_EXCEEDS_MAX_SIZE = -3;
00051 
00052     /*!
00053      Initializes with a name and http variable.
00054     */
00055     function eZHTTPFile( /*! Name of the HTTP variable */ $http_name,
00056                          /*! The HTTP variable structure */ $variable )
00057     {
00058         $this->HTTPName = $http_name;
00059         $this->OriginalFilename = $variable["name"];
00060         $this->Type = $variable["type"];
00061         $mime = explode( "/", $this->Type );
00062         $this->MimeCategory = $mime[0];
00063         $this->MimePart = $mime[1];
00064         $this->Filename = $variable["tmp_name"];
00065         $this->Size = $variable["size"];
00066         $this->IsTemporary = true;
00067     }
00068 
00069     /*!
00070      \return the directory where the file should be stored.
00071     */
00072     function storageDir( $sub_dir = false )
00073     {
00074         $sys = eZSys::instance();
00075         $storage_dir = $sys->storageDirectory();
00076         if ( $sub_dir !== false )
00077             $dir = $storage_dir . "/$sub_dir/" . $this->MimeCategory;
00078         else
00079             $dir = $storage_dir . "/" . $this->MimeCategory;
00080         return $dir;
00081     }
00082 
00083     /*!
00084      Stores the temporary file to the destination dir $dir.
00085     */
00086     function store( $sub_dir = false, $suffix = false, $mimeData = false )
00087     {
00088         //include_once( 'lib/ezfile/classes/ezdir.php' );
00089         if ( !$this->IsTemporary )
00090         {
00091             eZDebug::writeError( "Cannot store non temporary file: " . $this->Filename,
00092                                  "eZHTTPFile" );
00093             return false;
00094         }
00095         $this->IsTemporary = false;
00096 
00097         $ini = eZINI::instance();
00098 //         $storage_dir = $ini->variable( "FileSettings", "VarDir" ) . '/' . $ini->variable( "FileSettings", "StorageDir" );
00099         $storage_dir = eZSys::storageDirectory();
00100         if ( $sub_dir !== false )
00101             $dir = $storage_dir . "/$sub_dir/";
00102         else
00103             $dir = $storage_dir . "/";
00104         if ( $mimeData )
00105             $dir = $mimeData['dirpath'];
00106 
00107         if ( !$mimeData )
00108         {
00109             $dir .= $this->MimeCategory;
00110         }
00111 
00112         if ( !file_exists( $dir ) )
00113         {
00114             if ( !eZDir::mkdir( $dir, false, true ) )
00115             {
00116                 return false;
00117             }
00118         }
00119 
00120         $suffixString = false;
00121         if ( $suffix != false )
00122             $suffixString = ".$suffix";
00123 
00124         if ( $mimeData )
00125         {
00126             $dest_name = $mimeData['url'];
00127         }
00128         else
00129         {
00130             $dest_name = $dir . "/" . md5( basename( $this->Filename ) . microtime() . mt_rand() ) . $suffixString;
00131         }
00132 
00133         if ( !move_uploaded_file( $this->Filename, $dest_name ) )
00134         {
00135             eZDebug::writeError( "Failed moving uploaded file " . $this->Filename . " to destination $dest_name" );
00136             unlink( $dest_name );
00137             $ret = false;
00138         }
00139         else
00140         {
00141             $ret = true;
00142             $this->Filename = $dest_name;
00143             $perm = $ini->variable( "FileSettings", "StorageFilePermissions" );
00144             $oldumask = umask( 0 );
00145             chmod( $dest_name, octdec( $perm ) );
00146             umask( $oldumask );
00147 
00148             // Write log message to storage.log
00149             //include_once( 'lib/ezfile/classes/ezlog.php' );
00150             $storageDir = $dir . "/";
00151             eZLog::writeStorageLog( basename( $this->Filename ), $storageDir );
00152         }
00153         return $ret;
00154     }
00155 
00156     /*!
00157      \return an array with the attributes for this object.
00158     */
00159     function attributes()
00160     {
00161         return array( "original_filename",
00162                       "filename",
00163                       "filesize",
00164                       "is_temporary",
00165                       "mime_type",
00166                       "mime_type_category",
00167                       "mime_type_part" );
00168     }
00169 
00170     /*!
00171      \return true if the attribute $attr exists
00172     */
00173     function hasAttribute( $attr )
00174     {
00175         return in_array( $attr, $this->attributes() );
00176     }
00177 
00178     /*!
00179      \return the value for the attribute $attr or null if the attribute does not exist.
00180     */
00181     function attribute( $attr )
00182     {
00183         switch ( $attr )
00184         {
00185             case "original_filename":
00186                 return $this->OriginalFilename;
00187             case "mime_type":
00188                 return $this->Type;
00189             case "mime_type_category":
00190                 return $this->MimeCategory;
00191             case "mime_type_part":
00192                 return $this->MimePart;
00193             case "filename":
00194                 return $this->Filename;
00195             case "filesize":
00196                 return $this->Size;
00197             case "is_temporary":
00198                 return $this->IsTemporary;
00199             default:
00200             {
00201                 eZDebug::writeError( "Attribute '$attr' does not exist", 'eZHTTPFile::attribute' );
00202                 return null;
00203             } break;
00204         };
00205     }
00206 
00207     /*!
00208      \return true if the HTTP file $http_name can be fetched. If $maxSize is given,
00209      the function returns
00210         0 (eZHTTPFile::UPLOADEDFILE_OK) if the file can be fetched,
00211        -1 (eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST) if there has been no file uploaded,
00212        -2 (eZHTTPFile::UPLOADEDFILE_EXCEEDS_PHP_LIMIT) if the file was uploaded but size
00213           exceeds the upload_max_size limit (set in the PHP configuration),
00214        -3 (eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE) if the file was uploaded but size
00215           exceeds $maxSize or MAX_FILE_SIZE variable in the form.
00216     */
00217     static function canFetch( $http_name, $maxSize = false )
00218     {
00219         if ( !isset( $GLOBALS["eZHTTPFile-$http_name"] ) ||
00220              !( $GLOBALS["eZHTTPFile-$http_name"] instanceof eZHTTPFile ) )
00221         {
00222             if ( $maxSize === false )
00223             {
00224                 return isset( $_FILES[$http_name] ) and $_FILES[$http_name]['name'] != "" and $_FILES[$http_name]['error'] == 0;
00225             }
00226 
00227             if ( isset( $_FILES[$http_name] ) and $_FILES[$http_name]['name'] != "" )
00228             {
00229                 switch ( $_FILES[$http_name]['error'] )
00230                 {
00231                     case ( UPLOAD_ERR_NO_FILE ):
00232                     {
00233                         return eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST;
00234                     }break;
00235 
00236                     case ( UPLOAD_ERR_INI_SIZE ):
00237                     {
00238                         return eZHTTPFile::UPLOADEDFILE_EXCEEDS_PHP_LIMIT;
00239                     }break;
00240 
00241                     case ( UPLOAD_ERR_FORM_SIZE ):
00242                     {
00243                         return eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE;
00244                     }break;
00245 
00246                     default:
00247                     {
00248                         return ( $maxSize == 0 || $_FILES[$http_name]['size'] <= $maxSize )? eZHTTPFile::UPLOADEDFILE_OK:
00249                                                                                              eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE;
00250                     }
00251                 }
00252             }
00253             else
00254             {
00255                 return eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST;
00256             }
00257         }
00258         if ( $maxSize === false )
00259             return eZHTTPFile::UPLOADEDFILE_OK;
00260         else
00261             return true;
00262     }
00263 
00264     /*!
00265      Fetches the HTTP file named $http_name and returns a eZHTTPFile object,
00266      or null if the file could not be fetched.
00267     */
00268     static function fetch( $http_name )
00269     {
00270         if ( !isset( $GLOBALS["eZHTTPFile-$http_name"] ) ||
00271              !( $GLOBALS["eZHTTPFile-$http_name"] instanceof eZHTTPFile ) )
00272         {
00273             $GLOBALS["eZHTTPFile-$http_name"] = null;
00274 
00275             if ( isset( $_FILES[$http_name] ) and
00276                  $_FILES[$http_name]["name"] != "" )
00277             {
00278                 //include_once( 'lib/ezutils/classes/ezmimetype.php' );
00279                 //include_once( 'lib/ezfile/classes/ezfile.php' );
00280                 $mimeType = eZMimeType::findByURL( $_FILES[$http_name]['name'] );
00281                 $_FILES[$http_name]['type'] = $mimeType['name'];
00282                 $GLOBALS["eZHTTPFile-$http_name"] = new eZHTTPFile( $http_name, $_FILES[$http_name] );
00283             }
00284             else
00285             {
00286                 eZDebug::writeError( "Unknown file for post variable: $http_name",
00287                                      "eZHTTPFile" );
00288             }
00289         }
00290         return $GLOBALS["eZHTTPFile-$http_name"];
00291     }
00292 
00293     /*!
00294      Changes the MIME-Type to $mime.
00295     */
00296     function setMimeType( $mime )
00297     {
00298         $this->Type = $mime;
00299         list ( $this->MimeCategory, $this->MimePart ) = explode( '/', $mime, 2 );
00300     }
00301 
00302     /// The name of the HTTP file
00303     public $HTTPName;
00304     /// The original name of the file from the client
00305     public $OriginalFilename;
00306     /// The mime type of the file
00307     public $Type;
00308     /// The mimetype category (first part)
00309     public $MimeCategory;
00310     /// The mimetype type (second part)
00311     public $MimePart;
00312     /// The local filename
00313     public $Filename;
00314     /// The size of the local file
00315     public $Size;
00316     /// Whether the file is a temporary file or if it has been moved(stored).
00317     public $IsTemporary;
00318 }
00319 
00320 ?>