| [ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZImageType class 4 // 5 // Created on: <30-Apr-2002 13:06:21 bf> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZImageType ezimagetype.php 31 \ingroup eZDatatype 32 \brief The class eZImageType handles image accounts and association with content objects 33 34 \note The method initializeObjectAttribute was removed in 3.8, the new 35 storage technique removes the need to have it. 36 */ 37 38 include_once ( "kernel/classes/ezdatatype.php" ); 39 include_once ( "lib/ezfile/classes/ezdir.php" ); 40 include_once ( "lib/ezutils/classes/ezhttpfile.php" ); 41 42 define( 'EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD', 'data_int1' ); 43 define( 'EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_VARIABLE', '_ezimage_max_filesize_' ); 44 define( "EZ_DATATYPESTRING_IMAGE", "ezimage" ); 45 46 class eZImageType extends eZDataType 47 { 48 function eZImageType() 49 { 50 $this->eZDataType( EZ_DATATYPESTRING_IMAGE, ezi18n( 'kernel/classes/datatypes', "Image", 'Datatype name' ), 51 array( 'serialize_supported' => true ) ); 52 } 53 54 function repairContentObjectAttribute( &$contentObjectAttribute ) 55 { 56 include_once ( "kernel/classes/datatypes/ezimage/ezimage.php" ); 57 $image = eZImage::fetch( $contentObjectAttribute->attribute( 'id' ), 58 $contentObjectAttribute->attribute( 'version' ) ); 59 if ( !is_object( $image ) ) 60 { 61 $list = eZContentObjectAttribute::fetchSameClassAttributeIDList( $contentObjectAttribute->attribute( 'contentclassattribute_id' ), 62 true, 63 $contentObjectAttribute->attribute( 'version' ) ); 64 $language = eZContentObject::defaultLanguage(); 65 $attribute = false; 66 foreach ( array_keys( $list ) as $listKey ) 67 { 68 $listItem =& $list[$listKey]; 69 if ( $listItem->attribute( 'language_code' ) == $language ) 70 { 71 $attribute =& $listItem; 72 break; 73 } 74 } 75 if ( $attribute === false ) 76 { 77 $attribute =& $list[0]; 78 } 79 if ( $attribute ) 80 { 81 $originalImage = eZImage::fetch( $attribute->attribute( 'id' ), 82 $attribute->attribute( 'version' ) ); 83 if ( is_object( $originalImage ) ) 84 { 85 $originalImage->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) ); 86 $originalImage->store(); 87 return true; 88 } 89 } 90 } 91 return false; 92 } 93 94 /*! 95 \reimp 96 */ 97 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute ) 98 { 99 if ( $currentVersion != false ) 100 { 101 $dataText = $originalContentObjectAttribute->attribute( "data_text" ); 102 $contentObjectAttribute->setAttribute( "data_text", $dataText ); 103 } 104 } 105 106 /*! 107 \reimp 108 */ 109 function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null ) 110 { 111 if ( $version === null ) 112 { 113 include_once ( "kernel/classes/datatypes/ezimage/ezimagealiashandler.php" ); 114 eZImageAliasHandler::removeAllAliases( $contentObjectAttribute ); 115 } 116 else 117 { 118 $imageHandler =& $contentObjectAttribute->attribute( 'content' ); 119 if ( $imageHandler ) 120 $imageHandler->removeAliases( $contentObjectAttribute ); 121 } 122 } 123 124 /*! 125 \reimp 126 */ 127 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 128 { 129 $classAttribute =& $contentObjectAttribute->contentClassAttribute(); 130 $httpFileName = $base . "_data_imagename_" . $contentObjectAttribute->attribute( "id" ); 131 $maxSize = 1024 * 1024 * $classAttribute->attribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD ); 132 $mustUpload = false; 133 134 if( $contentObjectAttribute->validateIsRequired() ) 135 { 136 $tmpImgObj =& $contentObjectAttribute->attribute( 'content' ); 137 $original =& $tmpImgObj->attribute( 'original' ); 138 if ( !$original['is_valid'] ) 139 { 140 $mustUpload = true; 141 } 142 } 143 144 $canFetchResult = eZHTTPFile::canFetch( $httpFileName, $maxSize ); 145 if ( isset( $_FILES[$httpFileName] ) and $_FILES[$httpFileName]["tmp_name"] != "" ) 146 { 147 $imagefile = $_FILES[$httpFileName]['tmp_name']; 148 if ( !$_FILES[$httpFileName]["size"] ) 149 { 150 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 151 'The image file must have non-zero size.' ) ); 152 return EZ_INPUT_VALIDATOR_STATE_INVALID; 153 } 154 if ( function_exists( 'getimagesize' ) ) 155 { 156 $info = getimagesize( $imagefile ); 157 if ( !$info ) 158 { 159 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 160 'A valid image file is required.' ) ); 161 return EZ_INPUT_VALIDATOR_STATE_INVALID; 162 } 163 } 164 else 165 { 166 include_once ( 'lib/ezutils/classes/ezmimetype.php' ); 167 $mimeType = eZMimeType::findByURL( $_FILES[$httpFileName]['name'] ); 168 $nameMimeType = $mimeType['name']; 169 $nameMimeTypes = explode("/", $nameMimeType); 170 if ( $nameMimeTypes[0] != 'image' ) 171 { 172 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 173 'A valid image file is required.' ) ); 174 return EZ_INPUT_VALIDATOR_STATE_INVALID; 175 } 176 } 177 } 178 if ( $mustUpload && $canFetchResult == EZ_UPLOADEDFILE_DOES_NOT_EXIST ) 179 { 180 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 181 'A valid image file is required.' ) ); 182 return EZ_INPUT_VALIDATOR_STATE_INVALID; 183 } 184 if ( $canFetchResult == EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT ) 185 { 186 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 187 'The size of the uploaded image exceeds limit set by upload_max_filesize directive in php.ini. Please contact the site administrator.' ) ); 188 return EZ_INPUT_VALIDATOR_STATE_INVALID; 189 } 190 if ( $canFetchResult == EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE ) 191 { 192 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 193 'The size of the uploaded file exceeds the limit set for this site: %1 bytes.' ), $maxSize ); 194 return EZ_INPUT_VALIDATOR_STATE_INVALID; 195 } 196 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED; 197 } 198 199 /*! 200 \reimp 201 */ 202 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute ) 203 { 204 $result = false; 205 $imageAltText = false; 206 $hasImageAltText = false; 207 if ( $http->hasPostVariable( $base . "_data_imagealttext_" . $contentObjectAttribute->attribute( "id" ) ) ) 208 { 209 $imageAltText = eZHTTPTool::postVariable( $base . "_data_imagealttext_" . $contentObjectAttribute->attribute( "id" ) ); 210 $hasImageAltText = true; 211 } 212 213 $content =& $contentObjectAttribute->attribute( 'content' ); 214 $httpFileName = $base . "_data_imagename_" . $contentObjectAttribute->attribute( "id" ); 215 216 if ( eZHTTPFile::canFetch( $httpFileName ) ) 217 { 218 $httpFile =& eZHTTPFile::fetch( $httpFileName ); 219 if ( $httpFile ) 220 { 221 if ( $content ) 222 { 223 $content->setHTTPFile( $httpFile ); 224 $result = true; 225 } 226 } 227 } 228 229 if ( $content ) 230 { 231 if ( $hasImageAltText ) 232 $content->setAttribute( 'alternative_text', $imageAltText ); 233 $result = true; 234 } 235 236 return $result; 237 } 238 239 /*! 240 \reimp 241 */ 242 function storeObjectAttribute( &$contentObjectAttribute ) 243 { 244 $imageHandler =& $contentObjectAttribute->attribute( 'content' ); 245 if ( $imageHandler ) 246 { 247 $httpFile =& $imageHandler->httpFile( true ); 248 if ( $httpFile ) 249 { 250 $imageAltText = $imageHandler->attribute( 'alternative_text' ); 251 252 $imageHandler->initializeFromHTTPFile( $httpFile, $imageAltText ); 253 } 254 if ( $imageHandler->isStorageRequired() ) 255 { 256 $imageHandler->store( $contentObjectAttribute ); 257 } 258 } 259 } 260 261 /*! 262 \reimp 263 HTTP file insertion is supported. 264 */ 265 function isHTTPFileInsertionSupported() 266 { 267 return true; 268 } 269 270 /*! 271 \reimp 272 Regular file insertion is supported. 273 */ 274 function isRegularFileInsertionSupported() 275 { 276 return true; 277 } 278 279 /*! 280 \reimp 281 Inserts the file using the Image Handler eZImageAliasHandler. 282 */ 283 function insertHTTPFile( &$object, $objectVersion, $objectLanguage, 284 &$objectAttribute, &$httpFile, $mimeData, 285 &$result ) 286 { 287 $result = array( 'errors' => array(), 288 'require_storage' => false ); 289 $errors =& $result['errors']; 290 291 $handler =& $objectAttribute->content(); 292 if ( !$handler ) 293 { 294 $errors[] = array( 'description' => ezi18n( 'kernel/classe/datatypes/ezimage', 295 'Failed to fetch Image Handler. Please contact the site administrator.' ) ); 296 return false; 297 } 298 299 $status = $handler->initializeFromHTTPFile( $httpFile ); 300 $result['require_storage'] = $handler->isStorageRequired(); 301 return $status; 302 } 303 304 /*! 305 \reimp 306 Inserts the file using the Image Handler eZImageAliasHandler. 307 */ 308 function insertRegularFile( &$object, $objectVersion, $objectLanguage, 309 &$objectAttribute, $filePath, 310 &$result ) 311 { 312 $result = array( 'errors' => array(), 313 'require_storage' => false ); 314 $errors =& $result['errors']; 315 316 $handler =& $objectAttribute->content(); 317 if ( !$handler ) 318 { 319 $errors[] = array( 'description' => ezi18n( 'kernel/classe/datatypes/ezimage', 320 'Failed to fetch Image Handler. Please contact the site administrator.' ) ); 321 return false; 322 } 323 324 $status = $handler->initializeFromFile( $filePath, false, $filePath ); 325 $result['require_storage'] = $handler->isStorageRequired(); 326 return $status; 327 } 328 329 /*! 330 \reimp 331 We support file information 332 */ 333 function hasStoredFileInformation( &$object, $objectVersion, $objectLanguage, 334 &$objectAttribute ) 335 { 336 return true; 337 } 338 339 /*! 340 \reimp 341 Extracts file information for the image entry. 342 */ 343 function storedFileInformation( &$object, $objectVersion, $objectLanguage, 344 &$objectAttribute ) 345 { 346 $content =& $objectAttribute->content(); 347 if ( $content ) 348 { 349 $original = $content->attribute( 'original' ); 350 $fileName = $original['filename']; 351 $filePath = $original['full_path']; 352 $mimeType = $original['mime_type']; 353 $originalFileName = $original['original_filename']; 354 355 return array( 'filename' => $fileName, 356 'original_filename' => $originalFileName, 357 'filepath' => $filePath, 358 'mime_type' => $mimeType ); 359 } 360 return false; 361 } 362 363 /*! 364 \reimp 365 */ 366 function onPublish( &$contentObjectAttribute, &$contentObject, &$publishedNodes ) 367 { 368 $hasContent = $contentObjectAttribute->hasContent(); 369 if ( $hasContent ) 370 { 371 $imageHandler =& $contentObjectAttribute->attribute( 'content' ); 372 $mainNode = false; 373 foreach ( array_keys( $publishedNodes ) as $publishedNodeKey ) 374 { 375 $publishedNode =& $publishedNodes[$publishedNodeKey]; 376 if ( $publishedNode->attribute( 'main_node_id' ) ) 377 { 378 $mainNode =& $publishedNode; 379 break; 380 } 381 } 382 if ( $mainNode ) 383 { 384 $dirpath = $imageHandler->imagePathByNode( $contentObjectAttribute, $mainNode ); 385 $oldDirpath = $imageHandler->directoryPath(); 386 if ( $oldDirpath != $dirpath ) 387 { 388 $name = $imageHandler->imageNameByNode( $contentObjectAttribute, $mainNode ); 389 $imageHandler->updateAliasPath( $dirpath, $name ); 390 } 391 } 392 if ( $imageHandler->isStorageRequired() ) 393 { 394 $imageHandler->store( $contentObjectAttribute ); 395 $contentObjectAttribute->store(); 396 } 397 } 398 } 399 400 /*! 401 \reimp 402 */ 403 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute ) 404 { 405 $filesizeName = $base . EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_VARIABLE . $classAttribute->attribute( 'id' ); 406 if ( $http->hasPostVariable( $filesizeName ) ) 407 { 408 $filesizeValue = $http->postVariable( $filesizeName ); 409 $classAttribute->setAttribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD, $filesizeValue ); 410 return true; 411 } 412 return false; 413 } 414 415 /*! 416 \reimp 417 */ 418 function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute ) 419 { 420 if( $action == "delete_image" ) 421 { 422 $content =& $contentObjectAttribute->attribute( 'content' ); 423 if ( $content ) 424 { 425 $content->removeAliases( $contentObjectAttribute ); 426 } 427 } 428 } 429 430 /*! 431 \reimp 432 Will return one of the following items from the original alias. 433 - alternative_text - If it's not empty 434 - Default paramater in \a $name if it exists 435 - original_filename, this is the default fallback. 436 */ 437 function title( &$contentObjectAttribute, $name = 'original_filename' ) 438 { 439 $content =& $contentObjectAttribute->content(); 440 $original = $content->attribute( 'original' ); 441 $value = $original['alternative_text']; 442 if ( trim( $value ) == '' ) 443 { 444 if ( array_key_exists( $name, $original ) ) 445 $value = $original[$name]; 446 else 447 $value = $original['original_filename']; 448 } 449 450 return $value; 451 } 452 453 function hasObjectAttributeContent( &$contentObjectAttribute ) 454 { 455 $handler =& $contentObjectAttribute->content(); 456 if ( !$handler ) 457 return false; 458 return $handler->attribute( 'is_valid' ); 459 } 460 461 /*! 462 \reimp 463 */ 464 function &objectAttributeContent( &$contentObjectAttribute ) 465 { 466 include_once ( "kernel/classes/datatypes/ezimage/ezimagealiashandler.php" ); 467 $imageHandler = new eZImageAliasHandler( $contentObjectAttribute ); 468 469 return $imageHandler; 470 } 471 472 /*! 473 \reimp 474 */ 475 function metaData( $contentObjectAttribute ) 476 { 477 $content =& $contentObjectAttribute->content(); 478 $original = $content->attribute( 'original' ); 479 $value = $original['alternative_text']; 480 return $value; 481 } 482 483 /*! 484 \reimp 485 */ 486 function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode ) 487 { 488 $maxSize = $classAttribute->attribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD ); 489 $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'max-size', $maxSize, 490 array( 'unit-size' => 'mega' ) ) ); 491 } 492 493 /*! 494 \reimp 495 */ 496 function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode ) 497 { 498 $maxSize = $attributeParametersNode->elementTextContentByName( 'max-size' ); 499 $sizeNode = $attributeParametersNode->elementByName( 'max-size' ); 500 $unitSize = $sizeNode->attributeValue( 'unit-size' ); 501 $classAttribute->setAttribute( EZ_DATATYPESTRING_MAX_IMAGE_FILESIZE_FIELD, $maxSize ); 502 } 503 504 505 /*! 506 \reimp 507 \return a DOM representation of the content object attribute 508 */ 509 function serializeContentObjectAttribute( &$package, &$objectAttribute ) 510 { 511 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute ); 512 513 $content = $objectAttribute->content(); 514 $original = $content->attribute( 'original' ); 515 $imageKey = md5( mt_rand() ); 516 517 $package->appendSimpleFile( $imageKey, $original['url'] ); 518 $node->appendAttribute( eZDomDocument::createAttributeNode( 'image-file-key', $imageKey ) ); 519 $node->appendAttribute( eZDomDocument::createAttributeNode( 'alternative-text', $original['alternative_text'] ) ); 520 521 return $node; 522 } 523 524 /*! 525 \reimp 526 \param package 527 \param contentobject attribute object 528 \param ezdomnode object 529 */ 530 function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode ) 531 { 532 $alternativeText = $attributeNode->attributeValue( 'alternative-text' ); 533 // Backwards compatability with older node name 534 if ( $alternativeText === false ) 535 $alternativeText = $attributeNode->attributeValue( 'alternativ-text' ); 536 $content =& $objectAttribute->attribute( 'content' ); 537 $content->initializeFromFile( $package->simpleFilePath( $attributeNode->attributeValue( 'image-file-key' ) ), $alternativeText ); 538 $content->store( $objectAttribute ); 539 } 540 541 /*! 542 \return string representation of an contentobjectattribute data for simplified export 543 544 */ 545 function toString( $objectAttribute ) 546 { 547 $content = $objectAttribute->content(); 548 $original = $content->attribute( 'original' ); 549 return $original['url']; 550 } 551 552 function fromString( &$objectAttribute, $string ) 553 { 554 $content =& $objectAttribute->attribute( 'content' ); 555 $content->initializeFromFile( $string, "" ); 556 $content->store( $objectAttribute ); 557 return true; 558 } 559 560 } 561 562 eZDataType::register( EZ_DATATYPESTRING_IMAGE, "ezimagetype" ); 563 564 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |