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 include_once( "kernel/classes/ezdatatype.php" );
00041 include_once( "lib/ezfile/classes/ezdir.php" );
00042 include_once( "lib/ezutils/classes/ezhttpfile.php" );
00043
00044 define( 'EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD', 'data_int1' );
00045 define( 'EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_VARIABLE', '_ezimage_max_filesize_' );
00046 define( "EZ_DATATYPESTRING_IMAGE", "ezimage" );
00047
00048 class eZImageType extends eZDataType
00049 {
00050 function eZImageType()
00051 {
00052 $this->eZDataType( EZ_DATATYPESTRING_IMAGE, ezi18n( 'kernel/classes/datatypes', "Image", 'Datatype name' ),
00053 array( 'serialize_supported' => true ) );
00054 }
00055
00056 function repairContentObjectAttribute( &$contentObjectAttribute )
00057 {
00058 include_once( "kernel/classes/datatypes/ezimage/ezimage.php" );
00059 $image = eZImage::fetch( $contentObjectAttribute->attribute( 'id' ),
00060 $contentObjectAttribute->attribute( 'version' ) );
00061 if ( !is_object( $image ) )
00062 {
00063 $list = eZContentObjectAttribute::fetchSameClassAttributeIDList( $contentObjectAttribute->attribute( 'contentclassattribute_id' ),
00064 true,
00065 $contentObjectAttribute->attribute( 'version' ) );
00066 $language = eZContentObject::defaultLanguage();
00067 $attribute = false;
00068 foreach ( array_keys( $list ) as $listKey )
00069 {
00070 $listItem =& $list[$listKey];
00071 if ( $listItem->attribute( 'language_code' ) == $language )
00072 {
00073 $attribute =& $listItem;
00074 break;
00075 }
00076 }
00077 if ( $attribute === false )
00078 {
00079 $attribute =& $list[0];
00080 }
00081 if ( $attribute )
00082 {
00083 $originalImage = eZImage::fetch( $attribute->attribute( 'id' ),
00084 $attribute->attribute( 'version' ) );
00085 if ( is_object( $originalImage ) )
00086 {
00087 $originalImage->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) );
00088 $originalImage->store();
00089 return true;
00090 }
00091 }
00092 }
00093 return false;
00094 }
00095
00096
00097
00098
00099 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00100 {
00101 if ( $currentVersion != false )
00102 {
00103 $dataText = $originalContentObjectAttribute->attribute( "data_text" );
00104 $contentObjectAttribute->setAttribute( "data_text", $dataText );
00105 }
00106 }
00107
00108
00109
00110
00111 function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null )
00112 {
00113 if ( $version === null )
00114 {
00115 include_once( "kernel/classes/datatypes/ezimage/ezimagealiashandler.php" );
00116 eZImageAliasHandler::removeAllAliases( $contentObjectAttribute );
00117 }
00118 else
00119 {
00120 $imageHandler =& $contentObjectAttribute->attribute( 'content' );
00121 if ( $imageHandler )
00122 $imageHandler->removeAliases( $contentObjectAttribute );
00123 }
00124 }
00125
00126
00127
00128
00129 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00130 {
00131 $classAttribute =& $contentObjectAttribute->contentClassAttribute();
00132 $httpFileName = $base . "_data_imagename_" . $contentObjectAttribute->attribute( "id" );
00133 $maxSize = 1024 * 1024 * $classAttribute->attribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD );
00134 $mustUpload = false;
00135
00136 if( $contentObjectAttribute->validateIsRequired() )
00137 {
00138 $tmpImgObj =& $contentObjectAttribute->attribute( 'content' );
00139 $original =& $tmpImgObj->attribute( 'original' );
00140 if ( !$original['is_valid'] )
00141 {
00142 $mustUpload = true;
00143 }
00144 }
00145
00146 $canFetchResult = eZHTTPFile::canFetch( $httpFileName, $maxSize );
00147 if ( isset( $_FILES[$httpFileName] ) and $_FILES[$httpFileName]["tmp_name"] != "" )
00148 {
00149 $imagefile = $_FILES[$httpFileName]['tmp_name'];
00150 if ( !$_FILES[$httpFileName]["size"] )
00151 {
00152 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00153 'The image file must have non-zero size.' ) );
00154 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00155 }
00156 if ( function_exists( 'getimagesize' ) )
00157 {
00158 $info = getimagesize( $imagefile );
00159 if ( !$info )
00160 {
00161 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00162 'A valid image file is required.' ) );
00163 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00164 }
00165 }
00166 else
00167 {
00168 include_once( 'lib/ezutils/classes/ezmimetype.php' );
00169 $mimeType = eZMimeType::findByURL( $_FILES[$httpFileName]['name'] );
00170 $nameMimeType = $mimeType['name'];
00171 $nameMimeTypes = explode("/", $nameMimeType);
00172 if ( $nameMimeTypes[0] != 'image' )
00173 {
00174 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00175 'A valid image file is required.' ) );
00176 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00177 }
00178 }
00179 }
00180 if ( $mustUpload && $canFetchResult == EZ_UPLOADEDFILE_DOES_NOT_EXIST )
00181 {
00182 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00183 'A valid image file is required.' ) );
00184 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00185 }
00186 if ( $canFetchResult == EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT )
00187 {
00188 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00189 'The size of the uploaded image exceeds limit set by upload_max_filesize directive in php.ini. Please contact the site administrator.' ) );
00190 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00191 }
00192 if ( $canFetchResult == EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE )
00193 {
00194 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00195 'The size of the uploaded file exceeds the limit set for this site: %1 bytes.' ), $maxSize );
00196 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00197 }
00198 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00199 }
00200
00201
00202
00203
00204 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00205 {
00206 $result = false;
00207 $imageAltText = false;
00208 $hasImageAltText = false;
00209 if ( $http->hasPostVariable( $base . "_data_imagealttext_" . $contentObjectAttribute->attribute( "id" ) ) )
00210 {
00211 $imageAltText = eZHTTPTool::postVariable( $base . "_data_imagealttext_" . $contentObjectAttribute->attribute( "id" ) );
00212 $hasImageAltText = true;
00213 }
00214
00215 $content =& $contentObjectAttribute->attribute( 'content' );
00216 $httpFileName = $base . "_data_imagename_" . $contentObjectAttribute->attribute( "id" );
00217
00218 if ( eZHTTPFile::canFetch( $httpFileName ) )
00219 {
00220 $httpFile =& eZHTTPFile::fetch( $httpFileName );
00221 if ( $httpFile )
00222 {
00223 if ( $content )
00224 {
00225 $content->setHTTPFile( $httpFile );
00226 $result = true;
00227 }
00228 }
00229 }
00230
00231 if ( $content )
00232 {
00233 if ( $hasImageAltText )
00234 $content->setAttribute( 'alternative_text', $imageAltText );
00235 $result = true;
00236 }
00237
00238 return $result;
00239 }
00240
00241
00242
00243
00244 function storeObjectAttribute( &$contentObjectAttribute )
00245 {
00246 $imageHandler =& $contentObjectAttribute->attribute( 'content' );
00247 if ( $imageHandler )
00248 {
00249 $httpFile =& $imageHandler->httpFile( true );
00250 if ( $httpFile )
00251 {
00252 $imageAltText = $imageHandler->attribute( 'alternative_text' );
00253
00254 $imageHandler->initializeFromHTTPFile( $httpFile, $imageAltText );
00255 }
00256 if ( $imageHandler->isStorageRequired() )
00257 {
00258 $imageHandler->store( $contentObjectAttribute );
00259 }
00260 }
00261 }
00262
00263
00264
00265
00266
00267 function isHTTPFileInsertionSupported()
00268 {
00269 return true;
00270 }
00271
00272
00273
00274
00275
00276 function isRegularFileInsertionSupported()
00277 {
00278 return true;
00279 }
00280
00281
00282
00283
00284
00285 function insertHTTPFile( &$object, $objectVersion, $objectLanguage,
00286 &$objectAttribute, &$httpFile, $mimeData,
00287 &$result )
00288 {
00289 $result = array( 'errors' => array(),
00290 'require_storage' => false );
00291 $errors =& $result['errors'];
00292
00293 $handler =& $objectAttribute->content();
00294 if ( !$handler )
00295 {
00296 $errors[] = array( 'description' => ezi18n( 'kernel/classes/datatypes/ezimage',
00297 'Failed to fetch Image Handler. Please contact the site administrator.' ) );
00298 return false;
00299 }
00300
00301 $status = $handler->initializeFromHTTPFile( $httpFile );
00302 $result['require_storage'] = $handler->isStorageRequired();
00303 return $status;
00304 }
00305
00306
00307
00308
00309
00310 function insertRegularFile( &$object, $objectVersion, $objectLanguage,
00311 &$objectAttribute, $filePath,
00312 &$result )
00313 {
00314 $result = array( 'errors' => array(),
00315 'require_storage' => false );
00316 $errors =& $result['errors'];
00317
00318 $handler =& $objectAttribute->content();
00319 if ( !$handler )
00320 {
00321 $errors[] = array( 'description' => ezi18n( 'kernel/classes/datatypes/ezimage',
00322 'Failed to fetch Image Handler. Please contact the site administrator.' ) );
00323 return false;
00324 }
00325
00326 $status = $handler->initializeFromFile( $filePath, false, basename( $filePath ) );
00327 $result['require_storage'] = $handler->isStorageRequired();
00328 return $status;
00329 }
00330
00331
00332
00333
00334
00335 function hasStoredFileInformation( &$object, $objectVersion, $objectLanguage,
00336 &$objectAttribute )
00337 {
00338 return true;
00339 }
00340
00341
00342
00343
00344
00345 function storedFileInformation( &$object, $objectVersion, $objectLanguage,
00346 &$objectAttribute )
00347 {
00348 $content =& $objectAttribute->content();
00349 if ( $content )
00350 {
00351 $original = $content->attribute( 'original' );
00352 $fileName = $original['filename'];
00353 $filePath = $original['full_path'];
00354 $mimeType = $original['mime_type'];
00355 $originalFileName = $original['original_filename'];
00356
00357 return array( 'filename' => $fileName,
00358 'original_filename' => $originalFileName,
00359 'filepath' => $filePath,
00360 'mime_type' => $mimeType );
00361 }
00362 return false;
00363 }
00364
00365
00366
00367
00368 function onPublish( &$contentObjectAttribute, &$contentObject, &$publishedNodes )
00369 {
00370 $hasContent = $contentObjectAttribute->hasContent();
00371 if ( $hasContent )
00372 {
00373 $imageHandler =& $contentObjectAttribute->attribute( 'content' );
00374 $mainNode = false;
00375 foreach ( array_keys( $publishedNodes ) as $publishedNodeKey )
00376 {
00377 $publishedNode =& $publishedNodes[$publishedNodeKey];
00378 if ( $publishedNode->attribute( 'main_node_id' ) )
00379 {
00380 $mainNode =& $publishedNode;
00381 break;
00382 }
00383 }
00384 if ( $mainNode )
00385 {
00386 $dirpath = $imageHandler->imagePathByNode( $contentObjectAttribute, $mainNode );
00387 $oldDirpath = $imageHandler->directoryPath();
00388 if ( $oldDirpath != $dirpath )
00389 {
00390 $name = $imageHandler->imageNameByNode( $contentObjectAttribute, $mainNode );
00391 $imageHandler->updateAliasPath( $dirpath, $name );
00392 }
00393 }
00394 if ( $imageHandler->isStorageRequired() )
00395 {
00396 $imageHandler->store( $contentObjectAttribute );
00397 $contentObjectAttribute->store();
00398 }
00399 }
00400 }
00401
00402
00403
00404
00405 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00406 {
00407 $filesizeName = $base . EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_VARIABLE . $classAttribute->attribute( 'id' );
00408 if ( $http->hasPostVariable( $filesizeName ) )
00409 {
00410 $filesizeValue = $http->postVariable( $filesizeName );
00411 $classAttribute->setAttribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD, $filesizeValue );
00412 return true;
00413 }
00414 return false;
00415 }
00416
00417
00418
00419
00420 function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
00421 {
00422 if( $action == "delete_image" )
00423 {
00424 $content =& $contentObjectAttribute->attribute( 'content' );
00425 if ( $content )
00426 {
00427 $content->removeAliases( $contentObjectAttribute );
00428 }
00429 }
00430 }
00431
00432
00433
00434
00435
00436
00437
00438
00439 function title( &$contentObjectAttribute, $name = 'original_filename' )
00440 {
00441 $content =& $contentObjectAttribute->content();
00442 $original = $content->attribute( 'original' );
00443 $value = $original['alternative_text'];
00444 if ( trim( $value ) == '' )
00445 {
00446 if ( array_key_exists( $name, $original ) )
00447 $value = $original[$name];
00448 else
00449 $value = $original['original_filename'];
00450 }
00451
00452 return $value;
00453 }
00454
00455 function hasObjectAttributeContent( &$contentObjectAttribute )
00456 {
00457 $handler =& $contentObjectAttribute->content();
00458 if ( !$handler )
00459 return false;
00460 return $handler->attribute( 'is_valid' );
00461 }
00462
00463
00464
00465
00466 function &objectAttributeContent( &$contentObjectAttribute )
00467 {
00468 include_once( "kernel/classes/datatypes/ezimage/ezimagealiashandler.php" );
00469 $imageHandler = new eZImageAliasHandler( $contentObjectAttribute );
00470
00471 return $imageHandler;
00472 }
00473
00474
00475
00476
00477 function metaData( $contentObjectAttribute )
00478 {
00479 $content =& $contentObjectAttribute->content();
00480 $original = $content->attribute( 'original' );
00481 $value = $original['alternative_text'];
00482 return $value;
00483 }
00484
00485
00486
00487
00488 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00489 {
00490 $maxSize = $classAttribute->attribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD );
00491 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'max-size', $maxSize,
00492 array( 'unit-size' => 'mega' ) ) );
00493 }
00494
00495
00496
00497
00498 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
00499 {
00500 $maxSize = $attributeParametersNode->elementTextContentByName( 'max-size' );
00501 $sizeNode = $attributeParametersNode->elementByName( 'max-size' );
00502 $unitSize = $sizeNode->attributeValue( 'unit-size' );
00503 $classAttribute->setAttribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD, $maxSize );
00504 }
00505
00506
00507
00508
00509
00510
00511 function serializeContentObjectAttribute( &$package, &$objectAttribute )
00512 {
00513 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
00514
00515 $content = $objectAttribute->content();
00516 $original = $content->attribute( 'original' );
00517
00518 if ( $original['url'] )
00519 {
00520 $imageKey = md5( mt_rand() );
00521
00522 $package->appendSimpleFile( $imageKey, $original['url'] );
00523 $node->appendAttribute( eZDomDocument::createAttributeNode( 'image-file-key', $imageKey ) );
00524 }
00525
00526 $node->appendAttribute( eZDomDocument::createAttributeNode( 'alternative-text', $original['alternative_text'] ) );
00527
00528 return $node;
00529 }
00530
00531
00532
00533
00534
00535
00536
00537 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
00538 {
00539
00540
00541 $objectAttribute->setAttribute( 'data_text', '' );
00542
00543 $alternativeText = $attributeNode->attributeValue( 'alternative-text' );
00544
00545 if ( $alternativeText === false )
00546 $alternativeText = $attributeNode->attributeValue( 'alternativ-text' );
00547 $content =& $objectAttribute->attribute( 'content' );
00548 $imageFileKey = $attributeNode->attributeValue( 'image-file-key' );
00549 if ( $imageFileKey )
00550 {
00551 $content->initializeFromFile( $package->simpleFilePath( $imageFileKey ), $alternativeText );
00552 }
00553 else
00554 {
00555 $content->setAttribute( 'alternative_text', $alternativeText );
00556 }
00557 $content->store( $objectAttribute );
00558 }
00559
00560
00561
00562
00563
00564 function toString( $objectAttribute )
00565 {
00566 $content = $objectAttribute->content();
00567 $original = $content->attribute( 'original' );
00568 return $original['url'];
00569 }
00570
00571 function fromString( &$objectAttribute, $string )
00572 {
00573 $content =& $objectAttribute->attribute( 'content' );
00574 $content->initializeFromFile( $string, "" );
00575 $content->store( $objectAttribute );
00576 return true;
00577 }
00578
00579 }
00580
00581 eZDataType::register( EZ_DATATYPESTRING_IMAGE, "ezimagetype" );
00582
00583 ?>