00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 include_once( "lib/ezutils/classes/ezdebug.php" );
00043 include_once( "lib/ezutils/classes/ezini.php" );
00044
00045 define( 'EZ_UPLOADEDFILE_OK', 0 );
00046 define( 'EZ_UPLOADEDFILE_DOES_NOT_EXIST', -1 );
00047 define( 'EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT', -2 );
00048 define( 'EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE', -3 );
00049
00050 class eZHTTPFile
00051 {
00052
00053
00054
00055 function eZHTTPFile( $http_name,
00056 $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
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
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
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
00108
00109 if ( !$mimeData )
00110 {
00111 $dir .= $this->MimeCategory;
00112 }
00113
00114 if ( !file_exists( $dir ) )
00115 {
00116 if ( !eZDir::mkdir( $dir, eZDir::directoryPermission(), true ) )
00117 {
00118 return false;
00119 }
00120 }
00121
00122 $suffixString = false;
00123 if ( $suffix != false )
00124 $suffixString = ".$suffix";
00125
00126 if ( $mimeData )
00127 {
00128 $dest_name = $mimeData['url'];
00129 }
00130 else
00131 {
00132 $dest_name = $dir . "/" . md5( basename( $this->Filename ) . microtime() . mt_rand() ) . $suffixString;
00133 }
00134
00135
00136
00137 if ( !move_uploaded_file( $this->Filename, $dest_name ) )
00138 {
00139 eZDebug::writeError( "Failed moving uploaded file " . $this->Filename . " to destination $dest_name" );
00140 unlink( $dest_name );
00141 $ret = false;
00142 }
00143 else
00144 {
00145 $ret = true;
00146 $this->Filename = $dest_name;
00147 $perm = $ini->variable( "FileSettings", "StorageFilePermissions" );
00148 $oldumask = umask( 0 );
00149 chmod( $dest_name, octdec( $perm ) );
00150 umask( $oldumask );
00151
00152
00153 include_once( 'lib/ezutils/classes/ezlog.php' );
00154 $storageDir = $dir . "/";
00155 eZLog::writeStorageLog( basename( $this->Filename ), $storageDir );
00156 }
00157 return $ret;
00158 }
00159
00160
00161
00162
00163 function attributes()
00164 {
00165 return array( "original_filename",
00166 "filename",
00167 "filesize",
00168 "is_temporary",
00169 "mime_type",
00170 "mime_type_category",
00171 "mime_type_part" );
00172 }
00173
00174
00175
00176
00177 function hasAttribute( $attr )
00178 {
00179 return in_array( $attr, $this->attributes() );
00180 }
00181
00182
00183
00184
00185 function &attribute( $attr )
00186 {
00187 switch ( $attr )
00188 {
00189 case "original_filename":
00190 return $this->OriginalFilename;
00191 case "mime_type":
00192 return $this->Type;
00193 case "mime_type_category":
00194 return $this->MimeCategory;
00195 case "mime_type_part":
00196 return $this->MimePart;
00197 case "filename":
00198 return $this->Filename;
00199 case "filesize":
00200 return $this->Size;
00201 case "is_temporary":
00202 return $this->IsTemporary;
00203 default:
00204 {
00205 eZDebug::writeError( "Attribute '$attr' does not exist", 'eZHTTPFile::attribute' );
00206 $retValue = null;
00207 return $retValue;
00208 } break;
00209 };
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 function canFetch( $http_name, $maxSize = false )
00223 {
00224 $file =& $GLOBALS["eZHTTPFile-$http_name"];
00225 if ( get_class( $file ) != "ezhttpfile" )
00226 {
00227 if ( $maxSize === false )
00228 {
00229 return isset( $_FILES[$http_name] ) and $_FILES[$http_name]['name'] != "" and $_FILES[$http_name]['error'] == 0;
00230 }
00231
00232 if ( isset( $_FILES[$http_name] ) and $_FILES[$http_name]['name'] != "" )
00233 {
00234 switch ( $_FILES[$http_name]['error'] )
00235 {
00236 case ( UPLOAD_ERR_NO_FILE ):
00237 {
00238 return EZ_UPLOADEDFILE_DOES_NOT_EXIST;
00239 }break;
00240
00241 case ( UPLOAD_ERR_INI_SIZE ):
00242 {
00243 return EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT;
00244 }break;
00245
00246 case ( UPLOAD_ERR_FORM_SIZE ):
00247 {
00248 return EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE;
00249 }break;
00250
00251 default:
00252 {
00253 return ( $maxSize == 0 || $_FILES[$http_name]['size'] <= $maxSize )? EZ_UPLOADEDFILE_OK:
00254 EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE;
00255 }
00256 }
00257 }
00258 else
00259 {
00260 return EZ_UPLOADEDFILE_DOES_NOT_EXIST;
00261 }
00262 }
00263 if ( $maxSize === false )
00264 return EZ_UPLOADEDFILE_OK;
00265 else
00266 return true;
00267 }
00268
00269
00270
00271
00272
00273 function &fetch( $http_name )
00274 {
00275 $file =& $GLOBALS["eZHTTPFile-$http_name"];
00276 if ( get_class( $file ) != "ezhttpfile" )
00277 {
00278 $file = null;
00279
00280 if ( isset( $_FILES[$http_name] ) and
00281 $_FILES[$http_name]["name"] != "" )
00282 {
00283 include_once( 'lib/ezutils/classes/ezmimetype.php' );
00284 include_once( 'lib/ezfile/classes/ezfile.php' );
00285 $mimeType = eZMimeType::findByURL( $_FILES[$http_name]['name'] );
00286 $_FILES[$http_name]['type'] = $mimeType['name'];
00287 $file = new eZHTTPFile( $http_name, $_FILES[$http_name] );
00288 }
00289 else
00290 eZDebug::writeError( "Unknown file for post variable: $http_name",
00291 "eZHTTPFile" );
00292 }
00293 return $file;
00294 }
00295
00296
00297
00298
00299 function setMimeType( $mime )
00300 {
00301 $this->Type = $mime;
00302 list ( $this->MimeCategory, $this->MimePart ) = explode( '/', $mime, 2 );
00303 }
00304
00305
00306 var $HTTPName;
00307
00308 var $OriginalFilename;
00309
00310 var $Type;
00311
00312 var $MimeCategory;
00313
00314 var $MimePart;
00315
00316 var $Filename;
00317
00318 var $Size;
00319
00320 var $IsTemporary;
00321 }
00322
00323 ?>