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