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 define( "EZ_IMAGE_HANDLER_KEEP_SUFFIX", 1 );
00042 define( "EZ_IMAGE_HANDLER_REPLACE_SUFFIX", 2 );
00043 define( "EZ_IMAGE_HANDLER_PREPEND_TAG_REPLACE_SUFFIX", 3 );
00044
00045 class eZImageHandler
00046 {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 function eZImageHandler( $handlerName, $isEnabled = true, $outputRewriteType = EZ_IMAGE_HANDLER_REPLACE_SUFFIX,
00059 $supportedInputMIMETypes = false, $supportedOutputMIMETypes,
00060 $conversionRules = false, $filters = false, $mimeTagMap = false )
00061 {
00062 $this->HandlerName = $handlerName;
00063 $this->SupportedInputMIMETypes = $supportedInputMIMETypes;
00064 $this->SupportedOutputMIMETypes = $supportedOutputMIMETypes;
00065 $this->ConversionRules = $conversionRules;
00066 $this->OutputRewriteType = $outputRewriteType;
00067 $this->Filters = $filters;
00068 $this->FilterMap = array();
00069 $this->SupportImageFilters = array();
00070 $this->MIMETagMap = array();
00071 if ( $mimeTagMap )
00072 $this->MIMETagMap = $mimeTagMap;
00073 if ( $this->Filters)
00074 {
00075 foreach ( array_keys( $this->Filters ) as $filterKey )
00076 {
00077 $filter =& $this->Filters[$filterKey];
00078 $this->FilterMap[$filter['name']] =& $filter;
00079 $this->SupportImageFilters[] = $filter['name'];
00080 }
00081 }
00082 $this->SupportImageFilters = array_unique( $this->SupportImageFilters );
00083 $this->IsEnabled = $isEnabled;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 function isAvailable()
00096 {
00097 return $this->IsEnabled;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107 function tagForMIMEType( $mimeData )
00108 {
00109 $name = $mimeData['name'];
00110 if ( isset( $this->MIMETagMap[$name] ) )
00111 return $this->MIMETagMap[$name];
00112 $position = strpos( $name, '/' );
00113 if ( $position !== false )
00114 return substr( $name, $position + 1 );
00115 else
00116 return false;
00117 }
00118
00119
00120
00121
00122 function supportedImageFilters()
00123 {
00124 return $this->SupportImageFilters;
00125 }
00126
00127
00128
00129
00130 function conversionRules()
00131 {
00132 return $this->ConversionRules;
00133 }
00134
00135
00136
00137
00138
00139 function createFilterDefinitionFromINI( $filterText )
00140 {
00141 $equalPosition = strpos( $filterText, '=' );
00142 $filterData = false;
00143 if ( $equalPosition !== false )
00144 {
00145 $filterName = substr( $filterText, 0, $equalPosition );
00146 $filterData = substr( $filterText, $equalPosition + 1 );
00147 }
00148 else
00149 $filterName = $filterText;
00150 return array( 'name' => $filterName,
00151 'text' => $filterData );
00152 }
00153
00154
00155
00156
00157
00158 function convertFilterToText( $filterDefinition, $filterData )
00159 {
00160 $replaceList = array();
00161 if ( $filterData['data'] )
00162 {
00163 $counter = 1;
00164 foreach ( $filterData['data'] as $data )
00165 {
00166 $replaceList['%' . $counter] = $data;
00167 ++$counter;
00168 }
00169 }
00170 $argumentText = $filterDefinition['text'];
00171 $text = eZSys::createShellArgument( $argumentText, $replaceList );
00172 return $text;
00173 }
00174
00175
00176
00177
00178 function textForFilter( $filterData )
00179 {
00180 $text = false;
00181 if ( isset( $this->FilterMap[$filterData['name']] ) )
00182 {
00183 $filterDefinition =& $this->FilterMap[$filterData['name']];
00184 $text = $this->convertFilterToText( $filterDefinition, $filterData );
00185 }
00186 return $text;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 function rewriteURL( $originalMimeData, &$destinationMimeData, $rewriteType, $aliasName = false )
00199 {
00200 $extraText = false;
00201 if ( $aliasName and
00202 $aliasName != 'original' )
00203 $extraText = '_' . $aliasName;
00204 switch ( $rewriteType )
00205 {
00206 case EZ_IMAGE_HANDLER_KEEP_SUFFIX:
00207 {
00208 $destinationMimeData['basename'] = $originalMimeData['basename'];
00209 $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $originalMimeData['suffix'];
00210 $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
00211 if ( $originalMimeData['dirpath'] )
00212 $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
00213 else
00214 $destinationMimeData['url'] = $destinationMimeData['filename'];
00215 } break;
00216 case EZ_IMAGE_HANDLER_REPLACE_SUFFIX:
00217 {
00218 $destinationMimeData['basename'] = $originalMimeData['basename'];
00219 $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $destinationMimeData['suffixes'][0];
00220 $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
00221 if ( $originalMimeData['dirpath'] )
00222 $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
00223 else
00224 $destinationMimeData['url'] = $destinationMimeData['filename'];
00225 } break;
00226 case EZ_IMAGE_HANDLER_PREPEND_TAG_REPLACE_SUFFIX:
00227 {
00228 $tagName = $this->tagForMIMEType( $destinationMimeData );
00229 $destinationMimeData['basename'] = $originalMimeData['basename'];
00230 $destinationMimeData['filename'] = $originalMimeData['basename'] . $extraText . '.' . $destinationMimeData['suffixes'][0];
00231 $destinationMimeData['dirpath'] = $originalMimeData['dirpath'];
00232 if ( $originalMimeData['dirpath'] )
00233 $destinationMimeData['url'] = $originalMimeData['dirpath'] . '/' . $destinationMimeData['filename'];
00234 else
00235 $destinationMimeData['url'] = $destinationMimeData['filename'];
00236 $destinationMimeData['url'] = $tagName . ':' . $destinationMimeData['url'];
00237 } break;
00238 }
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248 function supportedInputMIMETypes()
00249 {
00250 return $this->SupportedInputMIMETypes;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260 function supportedOutputMIMETypes()
00261 {
00262 return $this->SupportedOutputMIMETypes;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271 function changeFilePermissions( $filepath )
00272 {
00273 if ( !file_exists( $filepath ) )
00274 return false;
00275 $ini =& eZINI::instance( 'image.ini' );
00276 $perm = $ini->variable( "FileSettings", "ImagePermissions" );
00277 $success = false;
00278 $oldmask = umask( 0 );
00279 if ( !chmod( $filepath, octdec( $perm ) ) )
00280 eZDebug::writeError( "Chmod $perm $filepath failed",
00281 'eZImageHandler::changeFilePermissions' );
00282 else
00283 $success = true;
00284 umask( $oldmask );
00285 return $success;
00286 }
00287
00288
00289
00290
00291
00292 function wildcardToRegexp( $wildcard, $separatorCharacter = false )
00293 {
00294 if ( !$separatorCharacter )
00295 $separatorCharacter = '#';
00296 $wildcardArray = preg_split( "#[*]#", $wildcard, -1, PREG_SPLIT_DELIM_CAPTURE );
00297 $wildcardList = array();
00298 $i = 0;
00299 foreach ( $wildcardArray as $wildcardElement )
00300 {
00301 if ( ( $i % 2 ) == 1 )
00302 $wildcardList[] = '(.+)';
00303 else
00304 $wildcardList[] = preg_quote( $wildcardElement, $separatorCharacter );
00305 ++$i;
00306 }
00307 $wildcardString = implode( '', $wildcardList );
00308 return $wildcardString;
00309 }
00310
00311
00312
00313
00314 function isOutputMIMETypeSupported( $mimeData )
00315 {
00316 $mimeTypes = $this->supportedOutputMIMETypes();
00317 $mimeName = $mimeData;
00318 if ( is_array( $mimeData ) )
00319 $mimeName = $mimeData['name'];
00320 foreach ( $mimeTypes as $mimeType )
00321 {
00322 if ( strpos( $mimeType, '*' ) !== false )
00323 {
00324 $matchString = eZImageHandler::wildcardToRegexp( $mimeType );
00325 if ( preg_match( "#^" . $matchString . "$#", $mimeName ) )
00326 {
00327 return true;
00328 }
00329 }
00330 else if ( $mimeType == $mimeName )
00331 {
00332 return true;
00333 }
00334 }
00335 return false;
00336 }
00337
00338
00339
00340
00341 function isInputMIMETypeSupported( $mimeData )
00342 {
00343 $mimeTypes = $this->supportedInputMIMETypes();
00344 $mimeName = $mimeData;
00345 if ( is_array( $mimeData ) )
00346 $mimeName = $mimeData['name'];
00347 foreach ( $mimeTypes as $mimeType )
00348 {
00349 if ( strpos( $mimeType, '*' ) !== false )
00350 {
00351 $matchString = eZImageHandler::wildcardToRegexp( $mimeType );
00352 if ( preg_match( "#^" . $matchString . "$#", $mimeName ) )
00353 {
00354 return true;
00355 }
00356 }
00357 else if ( $mimeType == $mimeName )
00358 {
00359 return true;
00360 }
00361 }
00362 return false;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 function outputMIMEType( &$manager, $currentMimeData, $wantedMimeData, $supportedFormatsOriginal, $aliasName = false )
00377 {
00378 $conversionRules = array_merge( $manager->conversionRules(), $this->conversionRules() );
00379 $mimeData = false;
00380 $mimeType = false;
00381 if ( !$this->isInputMIMETypeSupported( $currentMimeData ) )
00382 return false;
00383
00384 if ( $wantedMimeData )
00385 {
00386 $conversionRules = array_merge( array( array( 'from' => $currentMimeData['name'],
00387 'to' => $wantedMimeData['name'] ) ),
00388 $conversionRules );
00389 }
00390
00391 $supportedFormats = array();
00392 foreach ( $supportedFormatsOriginal as $supportedFormat )
00393 {
00394 if ( $this->isOutputMIMETypeSupported( $supportedFormat ) )
00395 {
00396 $supportedFormats[] = $supportedFormat;
00397 $conversionRules[] = array( 'from' => $supportedFormat,
00398 'to' => $supportedFormat );
00399 }
00400 }
00401
00402 if ( $wantedMimeData and
00403 in_array( $wantedMimeData['name'], $supportedFormats ) )
00404 {
00405 $mimeType = $wantedMimeData['name'];
00406 }
00407 else if ( is_array( $conversionRules ) )
00408 {
00409 foreach ( $conversionRules as $rule )
00410 {
00411 if ( !$this->isOutputMIMETypeSupported( $rule['to'] ) or
00412 !in_array( $rule['to'], $supportedFormats ) )
00413 continue;
00414
00415 $matchRule = false;
00416 if ( strpos( $rule['from'], '*' ) !== false )
00417 {
00418 $matchString = eZImageHandler::wildcardToRegexp( $rule['from'] );
00419 if ( preg_match( "#^" . $matchString . "$#", $currentMimeData['name'] ) )
00420 {
00421 $matchRule = $rule;
00422 }
00423 }
00424 else if ( $rule['from'] == $currentMimeData['name'] )
00425 {
00426 $matchRule = $rule;
00427 }
00428 if ( $matchRule )
00429 {
00430 if ( $mimeType )
00431 {
00432 if ( $wantedMimeData and
00433 $matchRule['to'] == $wantedMimeData['name'] )
00434 $mimeType = $matchRule['to'];
00435 }
00436 else
00437 $mimeType = $matchRule['to'];
00438 }
00439 }
00440 }
00441 if ( $mimeType )
00442 {
00443 $mimeData = eZMimeType::findByName( $mimeType );
00444 eZImageHandler::rewriteURL( $currentMimeData, $mimeData, $this->outputRewriteType(), $aliasName );
00445 }
00446 return $mimeData;
00447 }
00448
00449
00450
00451
00452 function outputRewriteType()
00453 {
00454 return $this->OutputRewriteType;
00455 }
00456
00457
00458
00459
00460 function isFilterSupported( $filter )
00461 {
00462 return isset( $this->FilterMap[$filter['name']] );
00463 }
00464
00465
00466
00467
00468
00469
00470 function convert( &$manager, $sourceMimeData, &$destinationMimeData, $filters = false )
00471 {
00472 }
00473
00474 }
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485 class eZImageFactory
00486 {
00487
00488
00489
00490 function eZImageFactory( $name )
00491 {
00492 $this->Name = $name;
00493 }
00494
00495
00496
00497
00498 function name()
00499 {
00500 return $this->Name;
00501 }
00502
00503
00504
00505
00506
00507
00508 function &produceFromINI( $iniGroup, $iniFilename = false )
00509 {
00510 $imageHandler = null;
00511 return $imageHandler;
00512 }
00513
00514
00515 var $Name;
00516 }
00517
00518 ?>