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
00043
00044
00045
00046
00047 include_once( 'lib/ezdb/classes/ezdb.php' );
00048 include_once( 'lib/ezfile/classes/ezfilehandler.php' );
00049 include_once( "lib/ezxml/classes/ezxml.php" );
00050 include_once( "kernel/classes/datatypes/ezimage/ezimagefile.php" );
00051
00052 class eZImageAliasHandler
00053 {
00054
00055
00056
00057 function eZImageAliasHandler( &$contentObjectAttribute )
00058 {
00059 $this->ContentObjectAttributeData = array();
00060 if ( is_object( $contentObjectAttribute ) )
00061 {
00062 $this->ContentObjectAttributeData['id'] = $contentObjectAttribute->attribute( 'id' );
00063 $this->ContentObjectAttributeData['contentobject_id'] = $contentObjectAttribute->attribute( 'contentobject_id' );
00064 $this->ContentObjectAttributeData['version'] = $contentObjectAttribute->attribute( 'version' );
00065 $this->ContentObjectAttributeData['language_code'] = $contentObjectAttribute->attribute( 'language_code' );
00066 $this->ContentObjectAttributeData['can_translate'] = $contentObjectAttribute->attribute( 'can_translate' );
00067 $this->ContentObjectAttributeData['data_text'] = $contentObjectAttribute->attribute( 'data_text' );
00068 $this->ContentObjectAttributeData['DataTypeCustom'] = $contentObjectAttribute->DataTypeCustom;
00069 if ( !is_array( $this->ContentObjectAttributeData['DataTypeCustom'] ) )
00070 {
00071 $this->ContentObjectAttributeData['DataTypeCustom'] = array();
00072 }
00073 }
00074 else
00075 {
00076 eZDebug::writeWarning( 'Invalid eZContentObjectAttribute', 'eZImageAliasHandler::eZImageAliasHandler' );
00077 }
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 function attributes()
00087 {
00088 include_once( 'kernel/common/image.php' );
00089 $imageManager =& imageInit();
00090 $aliasList = $imageManager->aliasList();
00091 return array_merge( array( 'alternative_text',
00092 'original_filename',
00093 'is_valid' ),
00094 array_keys( $aliasList ) );
00095 }
00096
00097
00098
00099
00100
00101 function hasAttribute( $attributeName )
00102 {
00103 if ( in_array( $attributeName,
00104 array( 'alternative_text',
00105 'original_filename',
00106 'is_valid' ) ) )
00107 return true;
00108 include_once( 'kernel/common/image.php' );
00109 $imageManager =& imageInit();
00110 if ( $imageManager->hasAlias( $attributeName ) )
00111 return true;
00112 return false;
00113 }
00114
00115
00116
00117
00118
00119 function &attribute( $attributeName )
00120 {
00121 if ( in_array( $attributeName,
00122 array( 'alternative_text',
00123 'original_filename',
00124 'is_valid' ) ) )
00125 {
00126 $originalAttribute =& $this->attributeFromOriginal( $attributeName );
00127 return $originalAttribute;
00128 }
00129 $aliasName = $attributeName;
00130 $imageAlias =& $this->imageAlias( $aliasName );
00131 return $imageAlias;
00132 }
00133
00134
00135
00136
00137
00138
00139 function &attributeFromOriginal( $attributeName )
00140 {
00141 $originalAlias =& $this->attribute( 'original' );
00142 if ( $originalAlias )
00143 return $originalAlias[$attributeName];
00144 $retValue = null;
00145 return $retValue;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155 function setAttribute( $attributeName, $attributeValue )
00156 {
00157 if ( in_array( $attributeName,
00158 array( 'alternative_text',
00159 'original_filename' ) ) )
00160 {
00161 $aliasList =& $this->aliasList();
00162 foreach ( array_keys( $aliasList ) as $aliasName )
00163 {
00164 $alias =& $aliasList[$aliasName];
00165 $alias[$attributeName] = $attributeValue;
00166 }
00167 if ( $attributeName == 'alternative_text' )
00168 {
00169 $text = $this->displayText( $attributeValue );
00170 foreach ( array_keys( $aliasList ) as $aliasName )
00171 {
00172 $alias =& $aliasList[$aliasName];
00173 $alias['text'] = $text;
00174 }
00175 }
00176 $this->recreateDOMTree();
00177 $this->setStorageRequired();
00178 return true;
00179 }
00180 return false;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190 function isImageOwner()
00191 {
00192 $originalData = $this->originalAttributeData();
00193 return ( $originalData['attribute_id'] == false ||
00194 ( $originalData['attribute_id'] == $this->ContentObjectAttributeData['id'] &&
00195 $originalData['attribute_version'] == $this->ContentObjectAttributeData['version'] &&
00196 $originalData['attribute_language'] == $this->ContentObjectAttributeData['language_code'] ) );
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 function imageSerialNumber()
00211 {
00212 $serialNumber = $this->imageSerialNumberRaw();
00213 if ( $serialNumber < 1 )
00214 $serialNumber = 1;
00215 return $serialNumber;
00216 }
00217
00218
00219
00220
00221 function increaseImageSerialNumber()
00222 {
00223 $serialNumber =& $this->imageSerialNumberRaw();
00224 ++$serialNumber;
00225 }
00226
00227
00228
00229
00230 function resetImageSerialNumber()
00231 {
00232 $serialNumber =& $this->imageSerialNumberRaw();
00233 $serialNumber = 0;
00234 }
00235
00236
00237
00238
00239 function setImageSerialNumber( $number )
00240 {
00241 $serialNumber =& $this->imageSerialNumberRaw();
00242 $serialNumber = $number;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 function displayText( $alternativeText = null )
00252 {
00253 if ( $alternativeText === null )
00254 $text = $this->attribute( 'alternative_text' );
00255 else
00256 $text = $alternativeText;
00257
00258 return $text;
00259 }
00260
00261
00262
00263
00264 function directoryPath()
00265 {
00266 $aliasList =& $this->aliasList();
00267 if ( isset( $aliasList['original'] ) )
00268 {
00269 return $aliasList['original']['dirpath'];
00270 }
00271 return false;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 function imageName( &$contentObjectAttribute, &$contentVersion, $language = false )
00285 {
00286 if ( $language === false )
00287 {
00288 if ( is_object( $contentObjectAttribute ) )
00289 {
00290 $language = $contentObjectAttribute->attribute( 'language_code' );
00291 }
00292 else
00293 {
00294 $language = $contentObjectAttribute['language_code'];
00295 }
00296 }
00297 $objectName = $contentVersion->versionName( $language );
00298 if ( !$objectName )
00299 {
00300 $objectName = $contentVersion->name( $language );
00301 if ( !$objectName )
00302 {
00303 $objectName = $this->attribute( 'alternative_text' );
00304 if ( !$objectName )
00305 {
00306 $objectName = ezi18n( 'kernel/classes/datatypes', 'image', 'Default image name' );
00307 }
00308 }
00309 }
00310 $objectName = eZImageAliasHandler::normalizeImageName( $objectName );
00311 $objectName .= $this->imageSerialNumber();
00312 return $objectName;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322 function imageNameByNode( &$contentObjectAttribute, &$mainNode, $language = false )
00323 {
00324 if ( $language === false )
00325 {
00326 if ( is_object( $contentObjectAttribute ) )
00327 {
00328 $language = $contentObjectAttribute->attribute( 'language_code' );
00329 }
00330 else
00331 {
00332 $language = $contentObjectAttribute['language_code'];
00333 }
00334 }
00335 $objectName = $mainNode->getName( $language );
00336 if ( !$objectName )
00337 {
00338 $objectName = $this->attribute( 'alternative_text' );
00339 if ( !$objectName )
00340 {
00341 $objectName = ezi18n( 'kernel/classes/datatypes', 'image', 'Default image name' );
00342 }
00343 }
00344 $objectName = eZImageAliasHandler::normalizeImageName( $objectName );
00345 return $objectName;
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355 function imagePath( &$contentObjectAttribute, &$contentVersion, $isImageOwner = null )
00356 {
00357 $useVersion = false;
00358 if ( $isImageOwner === null )
00359 $isImageOwner = $this->isImageOwner();
00360 if ( $contentVersion->attribute( 'status' ) == EZ_VERSION_STATUS_PUBLISHED or
00361 !$isImageOwner )
00362 {
00363 $contentObject =& $contentVersion->attribute( 'contentobject' );
00364 $mainNode =& $contentObject->attribute( 'main_node' );
00365 if ( !$mainNode )
00366 {
00367 $ini =& eZINI::instance( 'image.ini' );
00368 $contentImageSubtree = $ini->variable( 'FileSettings', 'VersionedImages' );
00369 $pathString = $contentImageSubtree;
00370 $useVersion = true;
00371 }
00372 else
00373 {
00374 $ini =& eZINI::instance( 'image.ini' );
00375 $contentImageSubtree = $ini->variable( 'FileSettings', 'PublishedImages' );
00376 $pathString = $contentImageSubtree . '/' . $mainNode->attribute( 'path_identification_string' );
00377 }
00378 }
00379 else
00380 {
00381 $ini =& eZINI::instance( 'image.ini' );
00382 $contentImageSubtree = $ini->variable( 'FileSettings', 'VersionedImages' );
00383 $pathString = $contentImageSubtree;
00384 $useVersion = true;
00385 }
00386 $attributeData = $this->originalAttributeData();
00387 $attributeID = $attributeData['attribute_id'];
00388 $attributeVersion = $attributeData['attribute_version'];
00389 $attributeLanguage = $attributeData['attribute_language'];
00390 if ( $useVersion )
00391 $identifierString = $attributeID . '/' . $attributeVersion . '-' . $attributeLanguage;
00392 else
00393 $identifierString = $attributeID . '-' . $attributeVersion . '-' . $attributeLanguage;
00394 $imagePath = eZSys::storageDirectory() . '/' . $pathString . '/' . $identifierString;
00395 return $imagePath;
00396 }
00397
00398
00399
00400
00401
00402
00403 function imagePathByNode( &$contentObjectAttribute, &$mainNode )
00404 {
00405 $pathString = $mainNode->attribute( 'path_identification_string' );
00406 $ini =& eZINI::instance( 'image.ini' );
00407 $contentImageSubtree = $ini->variable( 'FileSettings', 'PublishedImages' );
00408 $attributeData = $this->originalAttributeData();
00409 $attributeID = $attributeData['attribute_id'];
00410 $attributeVersion = $attributeData['attribute_version'];
00411 $attributeLanguage = $attributeData['attribute_language'];
00412 $imagePath = eZSys::storageDirectory() . '/' . $contentImageSubtree . '/' . $pathString . '/' . $attributeID . '-' . $attributeVersion . '-' . $attributeLanguage;
00413 return $imagePath;
00414 }
00415
00416
00417
00418
00419
00420
00421
00422 function &imageAlias( $aliasName )
00423 {
00424 include_once( 'kernel/common/image.php' );
00425 $imageManager =& imageInit();
00426 if ( !$imageManager->hasAlias( $aliasName ) )
00427 {
00428 $retValue = null;
00429 return $retValue;
00430 }
00431
00432 $aliasList =& $this->aliasList();
00433 if ( array_key_exists( $aliasName, $aliasList ) )
00434 {
00435 $alias =& $aliasList[$aliasName];
00436 return $alias;
00437 }
00438 else
00439 {
00440 $imageManager =& imageInit();
00441 if ( $imageManager->hasAlias( $aliasName ) )
00442 {
00443 $original =& $aliasList['original'];
00444 $basename = $original['basename'];
00445 if ( $imageManager->createImageAlias( $aliasName, $aliasList,
00446 array( 'basename' => $basename ) ) )
00447 {
00448
00449
00450 $text = $this->displayText( $original['alternative_text'] );
00451 $originalFilename = $original['original_filename'];
00452 foreach ( array_keys( $aliasList ) as $aliasName )
00453 {
00454 $alias =& $aliasList[$aliasName];
00455 $alias['original_filename'] = $originalFilename;
00456 $alias['text'] = $text;
00457 if ( $alias['url'] )
00458 {
00459 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00460 $aliasFile = eZClusterFileHandler::instance( $alias['url'] );
00461 if( $aliasFile->exists() )
00462 $alias['filesize'] = $aliasFile->size();
00463 }
00464 if ( $alias['is_new'] )
00465 {
00466
00467 eZImageFile::appendFilepath( $this->ContentObjectAttributeData['id'], $alias['url'] );
00468 }
00469 }
00470 $this->addImageAliases( $aliasList );
00471 $aliasList3 =& $this->aliasList();
00472 return $aliasList[$aliasName];
00473 }
00474 }
00475 }
00476
00477 $imageAlias = null;
00478 return $imageAlias;
00479 }
00480
00481
00482
00483
00484
00485
00486
00487
00488 function &aliasList()
00489 {
00490 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
00491 if ( isset( $contentObjectAttributeData['DataTypeCustom']['alias_list'] ) )
00492 {
00493 $aliasList =& $contentObjectAttributeData['DataTypeCustom']['alias_list'];
00494 return $aliasList;
00495 }
00496
00497 eZDebug::AccumulatorStart('imageparse', 'XML', 'Image XML parsing' );
00498 $xml = new eZXML();
00499 $xmlString =& $contentObjectAttributeData['data_text'];
00500 $domTree =& $xml->domTree( $xmlString, array(), true );
00501
00502 if ( $domTree == false )
00503 {
00504 $this->generateXMLData();
00505 $domTree =& $xml->domTree( $xmlString, array(), false );
00506 }
00507
00508 $contentObjectAttributeData['DataTypeCustom']['dom_tree'] =& $domTree;
00509 $imageNodeArray = $domTree->get_elements_by_tagname( "ezimage" );
00510 $imageInfoNodeArray = $domTree->get_elements_by_tagname( "information" );
00511 $imageVariationNodeArray = $domTree->get_elements_by_tagname( "alias" );
00512 $imageOriginalArray = $domTree->get_elements_by_tagname( "original" );
00513
00514 $aliasList = array();
00515
00516 $aliasEntry = array();
00517 $alternativeText = $imageNodeArray[0]->get_attribute( 'alternative_text' );
00518 $originalFilename = $imageNodeArray[0]->get_attribute( 'original_filename' );
00519 $basename = $imageNodeArray[0]->get_attribute( 'basename' );
00520 $displayText = $this->displayText( $alternativeText );
00521
00522 $originalData = array( 'attribute_id' => '',
00523 'attribute_version' => '',
00524 'attribute_language' => '' );
00525 if ( isset( $imageOriginalArray[0] ) )
00526 {
00527 $originalData = array( 'attribute_id' => $imageOriginalArray[0]->get_attribute( 'attribute_id' ),
00528 'attribute_version' => $imageOriginalArray[0]->get_attribute( 'attribute_version' ),
00529 'attribute_language' => $imageOriginalArray[0]->get_attribute( 'attribute_language' ) );
00530
00531 }
00532 if ( strlen( $originalData['attribute_id'] ) == 0 ||
00533 strlen( $originalData['attribute_version'] ) == 0 ||
00534 strlen( $originalData['attribute_language'] ) == 0 )
00535 {
00536 $originalData = array( 'attribute_id' => $contentObjectAttributeData['id'],
00537 'attribute_version' => $contentObjectAttributeData['version'],
00538 'attribute_language' => $contentObjectAttributeData['language_code'] );
00539 }
00540 $this->setOriginalAttributeData( $originalData );
00541
00542 $aliasEntry['name'] = 'original';
00543 $aliasEntry['width'] = $imageNodeArray[0]->get_attribute( 'width' );
00544 $aliasEntry['height'] = $imageNodeArray[0]->get_attribute( 'height' );
00545 $aliasEntry['mime_type'] = $imageNodeArray[0]->get_attribute( 'mime_type' );
00546 $aliasEntry['filename'] = $imageNodeArray[0]->get_attribute( 'filename' );
00547 $aliasEntry['suffix'] = $imageNodeArray[0]->get_attribute( 'suffix' );
00548 $aliasEntry['dirpath'] = $imageNodeArray[0]->get_attribute( 'dirpath' );
00549 $aliasEntry['basename'] = $basename;
00550 $aliasEntry['alternative_text'] = $alternativeText;
00551 $aliasEntry['text'] = $displayText;
00552 $aliasEntry['original_filename'] = $originalFilename;
00553 $aliasEntry['url'] = $imageNodeArray[0]->get_attribute( 'url' );
00554 $aliasEntry['alias_key'] = $imageNodeArray[0]->get_attribute( 'alias_key' );
00555 $aliasEntry['timestamp'] = $imageNodeArray[0]->get_attribute( 'timestamp' );
00556 $aliasEntry['full_path'] =& $aliasEntry['url'];
00557 $aliasEntry['is_valid'] = $imageNodeArray[0]->get_attribute( 'is_valid' );
00558 $aliasEntry['is_new'] = false;
00559 $aliasEntry['filesize'] = false;
00560
00561
00562 if ( $aliasEntry['url'] )
00563 {
00564 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00565 $aliasFile = eZClusterFileHandler::instance( $aliasEntry['url'] );
00566
00567 if ( $aliasFile->exists() )
00568 $aliasEntry['filesize'] = $aliasFile->size();
00569 }
00570
00571 $imageInformation = false;
00572 if ( count( $imageInfoNodeArray ) > 0 )
00573 {
00574 $imageInfoNode =& $imageInfoNodeArray[0];
00575 $this->parseInformationNode( $imageInfoNode, $imageInformation );
00576 }
00577 $aliasEntry['info'] =& $imageInformation;
00578
00579 $serialNumber = $imageNodeArray[0]->get_attribute( 'serial_number' );
00580 if ( $serialNumber )
00581 $this->setImageSerialNumber( $serialNumber );
00582
00583 $aliasList['original'] = $aliasEntry;
00584
00585 if ( is_array( $imageVariationNodeArray ) )
00586 {
00587 foreach ( $imageVariationNodeArray as $imageVariation )
00588 {
00589 $aliasEntry = array();
00590 $aliasEntry['name'] = $imageVariation->get_attribute( 'name' );
00591 $aliasEntry['width'] = $imageVariation->get_attribute( 'width' );
00592 $aliasEntry['height'] = $imageVariation->get_attribute( 'height' );
00593 $aliasEntry['mime_type'] = $imageVariation->get_attribute( 'mime_type' );
00594 $aliasEntry['filename'] = $imageVariation->get_attribute( 'filename' );
00595 $aliasEntry['suffix'] = $imageVariation->get_attribute( 'suffix' );
00596 $aliasEntry['dirpath'] = $imageVariation->get_attribute( 'dirpath' );
00597 $aliasEntry['alias_key'] = $imageVariation->get_attribute( 'alias_key' );
00598 $aliasEntry['timestamp'] = $imageVariation->get_attribute( 'timestamp' );
00599 $aliasEntry['is_valid'] = $imageVariation->get_attribute( 'is_valid' );
00600 $aliasEntry['url'] = $imageVariation->get_attribute( 'url' );
00601 $aliasEntry['basename'] = $basename;
00602 $aliasEntry['alternative_text'] = $alternativeText;
00603 $aliasEntry['text'] = $displayText;
00604 $aliasEntry['original_filename'] = $originalFilename;
00605 $aliasEntry['full_path'] =& $aliasEntry['url'];
00606 $aliasEntry['is_new'] = false;
00607 $aliasEntry['info'] =& $imageInformation;
00608
00609 if ( $aliasEntry['url'] )
00610 {
00611
00612
00613 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00614 $aliasFile = eZClusterFileHandler::instance( $aliasEntry['url'] );
00615
00616 if ( $aliasFile->exists() )
00617 $aliasEntry['filesize'] = $aliasFile->size();
00618 }
00619
00620 include_once( 'kernel/common/image.php' );
00621 $imageManager =& imageInit();
00622 if ( $imageManager->isImageAliasValid( $aliasEntry ) )
00623 {
00624 $aliasList[$aliasEntry['name']] = $aliasEntry;
00625 }
00626 }
00627 }
00628 $contentObjectAttributeData['DataTypeCustom']['alias_list'] =& $aliasList;
00629 eZDebug::AccumulatorStop( 'imageparse' );
00630 return $aliasList;
00631 }
00632
00633
00634
00635
00636
00637
00638
00639 function removeAllAliases( &$contentObjectAttribute )
00640 {
00641
00642
00643 $handler =& $contentObjectAttribute->attribute( 'content' );
00644 if ( !$handler->isImageOwner() )
00645 {
00646 return;
00647 }
00648 $attributeData = $handler->originalAttributeData();
00649 $files = eZImageFile::fetchForContentObjectAttribute( $attributeData['attribute_id'], false );
00650 $dirs = array();
00651
00652 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00653 foreach ( $files as $filepath )
00654 {
00655 $file = eZClusterFileHandler::instance( $filepath );
00656 if ( $file->exists() )
00657 {
00658
00659 $file->delete();
00660 $dirs[] = eZDir::dirpath( $filepath );
00661 }
00662 }
00663 $dirs = array_unique( $dirs );
00664 foreach ( $dirs as $dirpath )
00665 {
00666 eZDir::cleanupEmptyDirectories( $dirpath );
00667 }
00668 eZImageFile::removeForContentObjectAttribute( $attributeData['attribute_id'] );
00669 }
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 function removeAliases( &$contentObjectAttribute )
00682 {
00683 $aliasList =& $this->aliasList();
00684 $alternativeText = false;
00685
00686 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
00687 $contentObjectAttributeVersion = $contentObjectAttributeData['version'];
00688 $contentObjectAttributeID = $contentObjectAttributeData['id'];
00689
00690 $isImageOwner = $this->isImageOwner();
00691 foreach ( array_keys( $aliasList ) as $aliasName )
00692 {
00693 $alias =& $aliasList[$aliasName];
00694 $dirpath = $alias['dirpath'];
00695 $doNotDelete = false;
00696
00697 if ( $aliasName == 'original' )
00698 $alternativeText = $alias['alternative_text'];
00699 if ( $alias['is_valid'] )
00700 {
00701
00702
00703 $filepath = $alias['url'];
00704
00705
00706
00707 $dbResult = eZImageFile::fetchImageAttributesByFilepath( $filepath, $contentObjectAttributeID );
00708
00709 if ( count( $dbResult ) > 0 )
00710 {
00711 $doNotDelete = true;
00712 foreach ( $dbResult as $res )
00713 {
00714
00715 if ( $res['id'] == $contentObjectAttributeID and
00716 $res['version'] == $contentObjectAttributeVersion )
00717 continue;
00718
00719 eZImageFile::appendFilepath( $res['id'], $filepath, true );
00720 }
00721 }
00722
00723 if ( !$doNotDelete )
00724 {
00725 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00726 $file = eZClusterFileHandler::instance( $filepath );
00727 if ( $file->exists() )
00728 {
00729 $file->delete();
00730 eZImageFile::removeFilepath( $contentObjectAttributeID, $filepath );
00731 eZDir::cleanupEmptyDirectories( $dirpath );
00732 }
00733 else
00734 {
00735 eZDebug::writeError( "Image file $filepath for alias $aliasName does not exist, could not remove from disk",
00736 'eZImageAliasHandler::removeAliases' );
00737 }
00738 }
00739 }
00740 }
00741 unset( $aliasList );
00742
00743 $doc = new eZDOMDocument();
00744 $imageNode = $doc->createElementNode( "ezimage" );
00745 $doc->setRoot( $imageNode );
00746
00747 $imageNode->appendAttribute( $doc->createAttributeNode( 'serial_number', false ) );
00748 $imageNode->appendAttribute( $doc->createAttributeNode( 'is_valid', false ) );
00749 $imageNode->appendAttribute( $doc->createAttributeNode( 'filename', false ) );
00750 $imageNode->appendAttribute( $doc->createAttributeNode( 'suffix', false ) );
00751 $imageNode->appendAttribute( $doc->createAttributeNode( 'basename', false ) );
00752 $imageNode->appendAttribute( $doc->createAttributeNode( 'dirpath', false ) );
00753 $imageNode->appendAttribute( $doc->createAttributeNode( 'url', false ) );
00754 $imageNode->appendAttribute( $doc->createAttributeNode( 'original_filename', false ) );
00755 $imageNode->appendAttribute( $doc->createAttributeNode( 'mime_type', false ) );
00756 $imageNode->appendAttribute( $doc->createAttributeNode( 'width', false ) );
00757 $imageNode->appendAttribute( $doc->createAttributeNode( 'height', false ) );
00758 $imageNode->appendAttribute( $doc->createAttributeNode( 'alternative_text', $alternativeText ) );
00759 $imageNode->appendAttribute( $doc->createAttributeNode( 'alias_key', false ) );
00760 $imageNode->appendAttribute( $doc->createAttributeNode( 'timestamp', false ) );
00761
00762 $contentObjectAttributeData['DataTypeCustom']['dom_tree'] =& $doc;
00763 unset( $contentObjectAttributeData['DataTypeCustom']['alias_list'] );
00764
00765 $this->storeDOMTree( $doc, true, $contentObjectAttribute );
00766 }
00767
00768
00769
00770
00771
00772
00773 function updateAliasPath( $dirpath, $name )
00774 {
00775 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
00776
00777 if ( !file_exists( $dirpath ) )
00778 {
00779 eZDir::mkdir( $dirpath, eZDir::directoryPermission(), true );
00780 }
00781 include_once( 'lib/ezutils/classes/ezmimetype.php' );
00782 $aliasList =& $this->aliasList();
00783 $this->resetImageSerialNumber();
00784
00785 foreach ( array_keys( $aliasList ) as $aliasName )
00786 {
00787 $alias =& $aliasList[$aliasName];
00788 if ( $alias['dirpath'] != $dirpath )
00789 {
00790 $oldDirpath = $alias['url'];
00791 $oldURL = $alias['url'];
00792 $basename = $name;
00793 if ( $aliasName != 'original' )
00794 $basename .= '_' . $aliasName;
00795 eZMimeType::changeFileData( $alias, $dirpath, $basename );
00796 $url = $alias['url'];
00797 if ( $this->isImageOwner() )
00798 {
00799 if ( $oldURL == '' )
00800 {
00801 continue;
00802 }
00803
00804
00805
00806 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00807 $fileHandler = eZClusterFileHandler::instance();
00808 $fileHandler->fileMove( $oldURL, $alias['url'] );
00809
00810 eZDir::cleanupEmptyDirectories( $oldDirpath );
00811 eZImageFile::moveFilepath( $contentObjectAttributeData['id'], $oldURL, $alias['url'] );
00812 }
00813 else
00814 {
00815
00816
00817 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00818 $fileHandler = eZClusterFileHandler::instance();
00819 $fileHandler->fileLinkCopy( $oldURL, $alias['url'], false );
00820 eZImageFile::appendFilepath( $contentObjectAttributeData['id'], $alias['url'] );
00821 }
00822 }
00823 }
00824
00825 $this->recreateDOMTree();
00826 $this->setStorageRequired();
00827 }
00828
00829
00830
00831
00832
00833
00834
00835 function createOriginalAttributeXMLData( &$originalNode, $originalData )
00836 {
00837 $originalNode->set_attribute( 'attribute_id', $originalData['attribute_id'] );
00838 $originalNode->set_attribute( 'attribute_version', $originalData['attribute_version'] );
00839 $originalNode->set_attribute( 'attribute_language', $originalData['attribute_language'] );
00840
00841 }
00842
00843
00844
00845
00846
00847
00848 function recreateDOMTree()
00849 {
00850 $aliasList =& $this->aliasList();
00851
00852 $doc = new eZDOMDocument();
00853 $imageNode = $doc->createElementNode( "ezimage" );
00854 $doc->setRoot( $imageNode );
00855
00856 $originalNode = $doc->createElementNode( "original" );
00857 $imageNode->appendChild( $originalNode );
00858
00859 include_once( 'kernel/common/image.php' );
00860 $imageManager =& imageInit();
00861
00862 $aliasName = 'original';
00863
00864 $originalData = $this->originalAttributeData();
00865 $this->createOriginalAttributeXMLData( $originalNode, $originalData );
00866
00867 $imageNode->appendAttribute( $doc->createAttributeNode( 'serial_number', $this->imageSerialNumber() ) );
00868 $imageNode->appendAttribute( $doc->createAttributeNode( 'is_valid', $aliasList[$aliasName]['is_valid'] ) );
00869 $imageNode->appendAttribute( $doc->createAttributeNode( 'filename', $aliasList[$aliasName]['filename'] ) );
00870 $imageNode->appendAttribute( $doc->createAttributeNode( 'suffix', $aliasList[$aliasName]['suffix'] ) );
00871 $imageNode->appendAttribute( $doc->createAttributeNode( 'basename', $aliasList[$aliasName]['basename'] ) );
00872 $imageNode->appendAttribute( $doc->createAttributeNode( 'dirpath', $aliasList[$aliasName]['dirpath'] ) );
00873 $imageNode->appendAttribute( $doc->createAttributeNode( 'url', $aliasList[$aliasName]['url'] ) );
00874 $imageNode->appendAttribute( $doc->createAttributeNode( 'original_filename', $aliasList[$aliasName]['original_filename'] ) );
00875 $imageNode->appendAttribute( $doc->createAttributeNode( 'mime_type', $aliasList[$aliasName]['mime_type'] ) );
00876 $imageNode->appendAttribute( $doc->createAttributeNode( 'width', $aliasList[$aliasName]['width'] ) );
00877 $imageNode->appendAttribute( $doc->createAttributeNode( 'height', $aliasList[$aliasName]['height'] ) );
00878 $imageNode->appendAttribute( $doc->createAttributeNode( 'alternative_text', $aliasList[$aliasName]['alternative_text'] ) );
00879 $imageNode->appendAttribute( $doc->createAttributeNode( 'alias_key', $imageManager->createImageAliasKey( $imageManager->alias( $aliasName ) ) ) );
00880 $imageNode->appendAttribute( $doc->createAttributeNode( 'timestamp', $aliasList[$aliasName]['timestamp'] ) );
00881
00882 $filename = $aliasList[$aliasName]['url'];
00883 if ( $filename )
00884 {
00885
00886
00887 require_once( 'kernel/classes/ezclusterfilehandler.php' );
00888 $imageFile = eZClusterFileHandler::instance( $filename );
00889
00890 $fetchedFilePath = $imageFile->fetchUnique();
00891
00892
00893 include_once( 'lib/ezutils/classes/ezmimetype.php' );
00894 $mimeDataTemp = eZMimeType::findByFileContents( $fetchedFilePath );
00895 $imageManager->analyzeImage( $mimeDataTemp );
00896
00897
00898 $mimeData = eZMimeType::findByURL( $filename );
00899 if ( isset( $mimeDataTemp['info'] ) )
00900 $mimeData['info'] = $mimeDataTemp['info'];
00901
00902 $this->createImageInformationNode( $imageNode, $mimeData );
00903 $imageFile->fileDeleteLocal( $fetchedFilePath );
00904
00905
00906
00907
00908
00909
00910 }
00911
00912 foreach ( array_keys( $aliasList ) as $aliasName )
00913 {
00914 if ( $aliasName == 'original' )
00915 continue;
00916 $imageAlias =& $aliasList[$aliasName];
00917 $this->addImageAliasToXML( $doc, $imageAlias );
00918 }
00919
00920 $this->setDOMTree( $doc );
00921 }
00922
00923
00924
00925
00926
00927
00928 function &domTree()
00929 {
00930 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
00931 if ( isset( $contentObjectAttributeData['DataTypeCustom']['dom_tree'] ) )
00932 return $contentObjectAttributeData['DataTypeCustom']['dom_tree'];
00933
00934 $xml = new eZXML();
00935 $xmlString =& $contentObjectAttributeData['data_text'];
00936 $domTree =& $xml->domTree( $xmlString );
00937 if ( $domTree == false )
00938 {
00939 $this->generateXMLData();
00940 $domTree =& $xml->domTree( $xmlString );
00941 }
00942
00943 $contentObjectAttributeData['DataTypeCustom']['dom_tree'] =& $domTree;
00944
00945 return $domTree;
00946 }
00947
00948
00949
00950
00951
00952
00953
00954
00955 function parseInformationNode( &$imageInfoNode, &$imageInformation )
00956 {
00957 $imageInformation = array();
00958
00959 $attributes = $imageInfoNode->attributes();
00960 foreach ( $attributes as $attribute )
00961 {
00962 $imageInformation[$attribute->name()] = $attribute->value;
00963 }
00964
00965 $children = $imageInfoNode->children();
00966 foreach ( $children as $child )
00967 {
00968 $childName = false;
00969 if ( isset ( $child->tagname ) )
00970 {
00971 $childName = $child->tagname;
00972 }
00973
00974 if ( $childName == 'array' )
00975 {
00976 $name = $child->get_attribute( 'name' );
00977 $items = $child->get_elements_by_tagname( 'item' );
00978 $array = array();
00979 foreach ( $items as $item )
00980 {
00981 $itemValueNode = $item->first_child();
00982 if ( !is_object ( $itemValueNode ) )
00983 continue;
00984 $itemValue = $itemValueNode->node_value();
00985 if ( $item->get_attribute( 'base64' ) == '1' )
00986 {
00987 $array[$item->get_attribute( 'key' )] = base64_decode( $itemValue );
00988 }
00989 else
00990 {
00991 $array[$item->get_attribute( 'key' )] = $itemValue;
00992 }
00993 }
00994 ksort( $array );
00995 $imageInformation[$name] = $array;
00996 }
00997 else if ( $childName == 'serialized' )
00998 {
00999 $name = $child->get_attribute( 'name' );
01000 $data = $child->get_attribute( 'data' );
01001 $imageInformation[$name] = unserialize( $data );
01002 }
01003 }
01004 }
01005
01006
01007
01008
01009
01010
01011
01012 function normalizeImageName( $imageName )
01013 {
01014
01015 include_once( 'lib/ezi18n/classes/ezchartransform.php' );
01016 $trans =& eZCharTransform::instance();
01017
01018 $imageName = $trans->transformByGroup( $imageName, 'identifier' );
01019 return $imageName;
01020 }
01021
01022
01023
01024
01025
01026
01027 function setHTTPFile( &$httpFile )
01028 {
01029 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01030 $contentObjectAttributeData['DataTypeCustom']['http_file'] =& $httpFile;
01031 }
01032
01033
01034
01035
01036
01037 function &httpFile( $release = false )
01038 {
01039 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01040 if ( isset( $contentObjectAttributeData['DataTypeCustom']['http_file'] ) )
01041 {
01042 $httpFile =& $contentObjectAttributeData['DataTypeCustom']['http_file'];
01043 if ( $release )
01044 unset( $contentObjectAttributeData['DataTypeCustom']['http_file'] );
01045 return $httpFile;
01046 }
01047
01048 $httpFile = false;
01049 return $httpFile;
01050 }
01051
01052
01053
01054
01055
01056 function initializeFromHTTPFile( &$httpFile, $imageAltText = false )
01057 {
01058 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01059 $this->increaseImageSerialNumber();
01060
01061 include_once( 'lib/ezutils/classes/ezmimetype.php' );
01062 $mimeData = eZMimeType::findByFileContents( $httpFile->attribute( 'filename' ) );
01063 if ( !$mimeData['is_valid'] )
01064 {
01065 $mimeData = eZMimeType::findByName( $httpFile->attribute( 'mime_type' ) );
01066 if ( !$mimeData['is_valid'] )
01067 {
01068 $mimeData = eZMimeType::findByURL( $httpFile->attribute( 'original_filename' ) );
01069 }
01070 }
01071 $attr = false;
01072 $this->removeAliases( $attr );
01073 $this->setOriginalAttributeDataValues( $contentObjectAttributeData['id'],
01074 $contentObjectAttributeData['version'],
01075 $contentObjectAttributeData['language_code'] );
01076 $contentVersion = eZContentObjectVersion::fetchVersion( $contentObjectAttributeData['version'],
01077 $contentObjectAttributeData['contentobject_id'] );
01078 $objectName = $this->imageName( $contentObjectAttributeData, $contentVersion );
01079 $objectPathString = $this->imagePath( $contentObjectAttributeData, $contentVersion, true );
01080
01081 eZMimeType::changeBaseName( $mimeData, $objectName );
01082 eZMimeType::changeDirectoryPath( $mimeData, $objectPathString );
01083
01084 $httpFile->store( false, false, $mimeData );
01085
01086 $originalFilename = $httpFile->attribute( 'original_filename' );
01087 return $this->initialize( $mimeData, $originalFilename, $imageAltText );
01088 }
01089
01090
01091
01092
01093
01094
01095 function initializeFromFile( $filename, $imageAltText = false, $originalFilename = false )
01096 {
01097 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01098 if ( !file_exists( $filename ) )
01099 {
01100 eZDebug::writeError( "The image '$filename' does not exist, cannot initialize image attribute with it",
01101 'eZImageAliasHandler::initializeFromFile' );
01102 return false;
01103 }
01104
01105 $this->increaseImageSerialNumber();
01106
01107 if ( !$originalFilename )
01108 $originalFilename = basename( $filename );
01109 include_once( 'lib/ezutils/classes/ezmimetype.php' );
01110 $mimeData = eZMimeType::findByFileContents( $filename );
01111 if ( !$mimeData['is_valid'] and
01112 $originalFilename != $filename )
01113 {
01114 $mimeData = eZMimeType::findByFileContents( $originalFilename );
01115 }
01116
01117 $attr = false;
01118 $this->removeAliases( $attr );
01119 $this->setOriginalAttributeDataValues( $contentObjectAttributeData['id'],
01120 $contentObjectAttributeData['version'],
01121 $contentObjectAttributeData['language_code'] );
01122 $contentVersion = eZContentObjectVersion::fetchVersion( $contentObjectAttributeData['version'],
01123 $contentObjectAttributeData['contentobject_id'] );
01124 $objectName = $this->imageName( $contentObjectAttributeData, $contentVersion );
01125 $objectPathString = $this->imagePath( $contentObjectAttributeData, $contentVersion, true );
01126
01127 eZMimeType::changeBaseName( $mimeData, $objectName );
01128 eZMimeType::changeDirectoryPath( $mimeData, $objectPathString );
01129 if ( !file_exists( $mimeData['dirpath'] ) )
01130 {
01131 eZDir::mkdir( $mimeData['dirpath'], false, true );
01132 }
01133
01134 eZFileHandler::copy( $filename, $mimeData['url'] );
01135
01136 return $this->initialize( $mimeData, $originalFilename, $imageAltText );
01137 }
01138
01139
01140
01141
01142
01143
01144
01145
01146 function initialize( $mimeData, $originalFilename, $imageAltText = false )
01147 {
01148 include_once( 'kernel/common/image.php' );
01149 $imageManager =& imageInit();
01150
01151 $this->setOriginalAttributeDataValues( $this->ContentObjectAttributeData['id'],
01152 $this->ContentObjectAttributeData['version'],
01153 $this->ContentObjectAttributeData['language_code'] );
01154
01155 $aliasList = array( 'original' => $mimeData );
01156 $aliasList['original']['alternative_text'] = $imageAltText;
01157 $aliasList['original']['original_filename'] = $originalFilename;
01158
01159
01160
01161 require_once( 'kernel/classes/ezclusterfilehandler.php' );
01162 $fileHandler = eZClusterFileHandler::instance();
01163 $filePath = $mimeData['url'];
01164 $fileHandler->fileStore( $filePath, 'image', false, $mimeData['name'] );
01165
01166 if ( $imageManager->createImageAlias( 'original', $aliasList, array( 'basename' => $mimeData['basename'] ) ) )
01167 {
01168 $mimeData = $aliasList['original'];
01169 $mimeData['name'] = $mimeData['mime_type'];
01170 $aliasList['original']['original_filename'] = $originalFilename;
01171 }
01172
01173 if ( $aliasList['original']['url'] )
01174 {
01175 require_once( 'kernel/classes/ezclusterfilehandler.php' );
01176 $aliasFile = eZClusterFileHandler::instance( $aliasList['original']['url'] );
01177 if( $aliasFile->exists() )
01178 $aliasList['original']['filesize'] = $aliasFile->size();
01179 }
01180
01181
01182
01183
01184 $fileHandler->fileFetch( $filePath );
01185
01186 $imageManager->analyzeImage( $mimeData );
01187
01188 $doc = new eZDOMDocument();
01189 $imageNode = $doc->createElementNode( "ezimage" );
01190 $doc->setRoot( $imageNode );
01191
01192 $width = false;
01193 $height = false;
01194 $info = getimagesize( $mimeData['url'] );
01195 if ( $info )
01196 {
01197 $width = $info[0];
01198 $height = $info[1];
01199 }
01200
01201 $originalNode = $doc->createElementNode( "original" );
01202 $imageNode->appendChild( $originalNode );
01203 $attributeData = $this->originalAttributeData();
01204 $this->createOriginalAttributeXMLData( $originalNode, $attributeData );
01205
01206 $imageNode->appendAttribute( $doc->createAttributeNode( 'serial_number', $this->imageSerialNumber() ) );
01207 $imageNode->appendAttribute( $doc->createAttributeNode( 'is_valid', true ) );
01208 $imageNode->appendAttribute( $doc->createAttributeNode( 'filename', $mimeData['filename'] ) );
01209 $imageNode->appendAttribute( $doc->createAttributeNode( 'suffix', $mimeData['suffix'] ) );
01210 $imageNode->appendAttribute( $doc->createAttributeNode( 'basename', $mimeData['basename'] ) );
01211 $imageNode->appendAttribute( $doc->createAttributeNode( 'dirpath', $mimeData['dirpath'] ) );
01212 $imageNode->appendAttribute( $doc->createAttributeNode( 'url', $mimeData['url'] ) );
01213 $imageNode->appendAttribute( $doc->createAttributeNode( 'original_filename', $originalFilename ) );
01214 $imageNode->appendAttribute( $doc->createAttributeNode( 'mime_type', $mimeData['name'] ) );
01215 $imageNode->appendAttribute( $doc->createAttributeNode( 'width', $width ) );
01216 $imageNode->appendAttribute( $doc->createAttributeNode( 'height', $height ) );
01217 $imageNode->appendAttribute( $doc->createAttributeNode( 'alternative_text', $imageAltText ) );
01218 $imageNode->appendAttribute( $doc->createAttributeNode( 'alias_key', $imageManager->createImageAliasKey( $imageManager->alias( 'original' ) ) ) );
01219 $imageNode->appendAttribute( $doc->createAttributeNode( 'timestamp', time() ) );
01220
01221 $this->createImageInformationNode( $imageNode, $mimeData );
01222
01223 $this->setDOMTree( $doc );
01224
01225 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01226 $contentObjectAttributeData['DataTypeCustom']['alias_list'] =& $aliasList;
01227
01228 eZImageFile::appendFilepath( $contentObjectAttributeData['id'], $mimeData['url'] );
01229
01230
01231 $fileHandler->fileDeleteLocal( $filePath );
01232
01233 return true;
01234 }
01235
01236 function createImageInformationNode( &$imageNode, &$mimeData )
01237 {
01238 if ( isset( $mimeData['info'] ) and
01239 $mimeData['info'] )
01240 {
01241 $imageInfoNode = eZDOMDocument::createElementNode( 'information' );
01242 $info = $mimeData['info'];
01243 foreach ( $info as $infoItemName => $infoItem )
01244 {
01245 if ( is_array( $infoItem ) )
01246 {
01247 $hasScalarValues = true;
01248 foreach ( $infoItem as $infoArrayItem )
01249 {
01250 if ( is_array( $infoArrayItem ) )
01251 {
01252 $hasScalarValues = false;
01253 break;
01254 }
01255 }
01256 if ( !$hasScalarValues )
01257 {
01258 unset( $serializedNode );
01259 $serializedNode = eZDOMDocument::createElementNode( 'serialized',
01260 array( 'name' => $infoItemName,
01261 'data' => serialize( $infoItem ) ) );
01262 $imageInfoNode->appendChild( $serializedNode );
01263 }
01264 else
01265 {
01266 unset( $arrayNode );
01267 $arrayNode = eZDOMDocument::createElementNode( 'array',
01268 array( 'name' => $infoItemName ) );
01269 $imageInfoNode->appendChild( $arrayNode );
01270 foreach ( $infoItem as $infoArrayKey => $infoArrayItem )
01271 {
01272 unset( $arrayItemNode );
01273 $arrayItemNode = eZDOMDocument::createElementNode( 'item',
01274 array( 'key' => $infoArrayKey,
01275 'base64' => 1 ) );
01276 $arrayItemNode->appendChild( eZDOMDocument::createTextNode( base64_encode( $infoArrayItem ) ) );
01277 $arrayNode->appendChild( $arrayItemNode );
01278 }
01279 }
01280 }
01281 else
01282 {
01283 $imageInfoNode->appendAttribute( eZDOMDocument::createAttributeNode( $infoItemName, $infoItem ) );
01284 }
01285 }
01286 $imageNode->appendChild( $imageInfoNode );
01287 }
01288 }
01289
01290
01291
01292
01293 function addImageAliases( &$imageAliasList )
01294 {
01295 $domTree =& $this->domTree();
01296 foreach ( array_keys( $imageAliasList ) as $imageAliasName )
01297 {
01298 $imageAlias =& $imageAliasList[$imageAliasName];
01299 if ( $imageAlias['is_new'] )
01300 {
01301 $this->addImageAliasToXML( $domTree, $imageAlias );
01302 $imageAlias['is_new'] = false;
01303 }
01304 }
01305 $attr = false;
01306 $this->storeDOMTree( $domTree, true, $attr );
01307 }
01308
01309
01310
01311
01312 function addImageAlias( $imageAlias )
01313 {
01314 $domTree =& $this->domTree();
01315 $this->addImageAliasToXML( $domTree, $imageAlias );
01316 $attr = false;
01317 $this->storeDOMTree( $domTree, true, $attr );
01318
01319 }
01320
01321
01322
01323
01324 function addImageAliasToXML( &$domTree, $imageAlias )
01325 {
01326 $imageVariationNodeArray = $domTree->get_elements_by_tagname( 'alias' );
01327 $imageNode = false;
01328 if ( is_array( $imageVariationNodeArray ) )
01329 {
01330 foreach ( array_keys( $imageVariationNodeArray ) as $imageVariationKey )
01331 {
01332 $imageVariation =& $imageVariationNodeArray[$imageVariationKey];
01333 $aliasEntryName = $imageVariation->get_attribute( 'name' );
01334 if ( $aliasEntryName == $imageAlias['name'] )
01335 {
01336 $imageNode =& $imageVariation;
01337 break;
01338 }
01339 }
01340 }
01341 if ( !$imageNode )
01342 {
01343 if ( is_a( $domTree, 'domdocument' ) )
01344 {
01345 $rootNode = $domTree->root();
01346 }
01347 else
01348 {
01349 $rootNode =& $domTree->root();
01350 }
01351
01352 $imageNode = $domTree->create_element( "alias" );
01353 $rootNode->append_child( $imageNode );
01354 }
01355 else
01356 {
01357 $imageNode->remove_attribute( 'name' );
01358 $imageNode->remove_attribute( 'filename' );
01359 $imageNode->remove_attribute( 'suffix' );
01360 $imageNode->remove_attribute( 'dirpath' );
01361 $imageNode->remove_attribute( 'url' );
01362 $imageNode->remove_attribute( 'mime_type' );
01363 $imageNode->remove_attribute( 'width' );
01364 $imageNode->remove_attribute( 'height' );
01365 $imageNode->remove_attribute( 'alias_key' );
01366 $imageNode->remove_attribute( 'timestamp' );
01367 $imageNode->remove_attribute( 'is_valid' );
01368 }
01369 $imageNode->set_attribute( 'name', $imageAlias['name'] );
01370 $imageNode->set_attribute( 'filename', $imageAlias['filename'] );
01371 $imageNode->set_attribute( 'suffix', $imageAlias['suffix'] );
01372 $imageNode->set_attribute( 'dirpath', $imageAlias['dirpath'] );
01373 $imageNode->set_attribute( 'url', $imageAlias['url'] );
01374 $imageNode->set_attribute( 'mime_type', $imageAlias['mime_type'] );
01375 $imageNode->set_attribute( 'width', $imageAlias['width'] );
01376 $imageNode->set_attribute( 'height', $imageAlias['height'] );
01377 $imageNode->set_attribute( 'alias_key', $imageAlias['alias_key'] );
01378 $imageNode->set_attribute( 'timestamp', $imageAlias['timestamp'] );
01379 $imageNode->set_attribute( 'is_valid', $imageAlias['is_valid'] );
01380
01381
01382 }
01383
01384
01385
01386
01387 function setDOMTree( &$domTree )
01388 {
01389 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01390 $contentObjectAttributeData['DataTypeCustom']['dom_tree'] =& $domTree;
01391 $contentObjectAttributeData['DataTypeCustom']['is_storage_required'] = true;
01392 }
01393
01394
01395
01396
01397 function storeDOMTree( &$domTree, $storeAttribute, &$contentObjectAttributeRef )
01398 {
01399 if ( !$domTree )
01400 return false;
01401 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01402 $contentObjectAttributeData['DataTypeCustom']['dom_tree'] =& $domTree;
01403 $contentObjectAttributeData['DataTypeCustom']['is_storage_required'] = false;
01404 $xmlString = $domTree->dump_mem();
01405 $contentObjectAttributeData['data_text'] = $xmlString;
01406
01407 if ( $storeAttribute )
01408 {
01409 if ( is_object( $contentObjectAttributeRef ) )
01410 $contentObjectAttribute =& $contentObjectAttributeRef;
01411 else
01412 $contentObjectAttribute = eZContentObjectAttribute::fetch( $contentObjectAttributeData['id'], $contentObjectAttributeData['version'] );
01413
01414 if ( is_object( $contentObjectAttribute ) )
01415 {
01416 $contentObjectAttribute->setAttribute( 'data_text', $xmlString );
01417 $contentObjectAttribute->storeData();
01418 }
01419 else
01420 {
01421 eZDebug::writeError( "Invalid objectAttribute: id = " . $contentObjectAttributeData['id'] . " version = " . $contentObjectAttributeData['version'] , "eZImageAliasHandler::storeDOMTree" );
01422 }
01423 }
01424
01425 return true;
01426 }
01427
01428
01429
01430
01431
01432 function store( &$contentObjectAttribute )
01433 {
01434 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01435
01436 $domTree =& $this->domTree();
01437 if ( $domTree )
01438 {
01439 if ( is_object( $contentObjectAttribute ) )
01440 {
01441 $this->storeDOMTree( $domTree, false, $contentObjectAttribute );
01442
01443 $contentObjectAttribute->setAttribute( 'data_text', $contentObjectAttributeData['data_text'] );
01444 $contentObjectAttribute->storeData();
01445 }
01446 else
01447 {
01448 $this->storeDOMTree( $domTree, true, $contentObjectAttribute );
01449
01450 }
01451 }
01452
01453 $contentObjectAttributeData['DataTypeCustom']['is_storage_required'] = false;
01454 }
01455
01456
01457
01458
01459
01460 function isStorageRequired()
01461 {
01462 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01463 if ( isset( $contentObjectAttributeData['DataTypeCustom']['is_storage_required'] ) )
01464 return $contentObjectAttributeData['DataTypeCustom']['is_storage_required'];
01465 return false;
01466 }
01467
01468
01469
01470
01471
01472 function setStorageRequired( $require = true )
01473 {
01474 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01475 $contentObjectAttributeData['DataTypeCustom']['is_storage_required'] = $require;
01476 }
01477
01478
01479
01480
01481
01482
01483
01484
01485 function &originalAttributeData()
01486 {
01487 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01488 if ( isset( $contentObjectAttributeData['DataTypeCustom']['original_data'] ) )
01489 return $contentObjectAttributeData['DataTypeCustom']['original_data'];
01490
01491 $originalData = array( 'attribute_id' => $contentObjectAttributeData['id'],
01492 'attribute_version' => $contentObjectAttributeData['version'],
01493 'attribute_language' => $contentObjectAttributeData['language_code'] );
01494 $contentObjectAttributeData['DataTypeCustom']['original_data'] =& $originalData;
01495 return $originalData;
01496 }
01497
01498
01499
01500
01501
01502 function setOriginalAttributeData( $originalData )
01503 {
01504 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01505 $contentObjectAttributeData['DataTypeCustom']['original_data'] =& $originalData;
01506
01507 $domTree =& $this->domTree();
01508 $imageOriginalArray = $domTree->get_elements_by_tagname( "original" );
01509 if ( isset( $imageOriginalArray[0] ) )
01510 $this->createOriginalAttributeXMLData( $imageOriginalArray[0], $originalData );
01511 }
01512
01513
01514
01515
01516
01517
01518
01519 function setOriginalAttributeDataFromAttribute( &$contentObjectAttribute )
01520 {
01521 $originalImageHandler =& $contentObjectAttribute->attribute( 'content' );
01522 $originalAttributeData =& $originalImageHandler->originalAttributeData();
01523 $domTree =& $originalImageHandler->domTree();
01524 $this->setDOMTree( $domTree );
01525 if ( $originalAttributeData['attribute_id'] )
01526 {
01527 $this->setOriginalAttributeData( $originalAttributeData );
01528 }
01529 else
01530 {
01531 $this->setOriginalAttributeDataValues( $contentObjectAttribute->attribute( 'id' ),
01532 $contentObjectAttribute->attribute( 'version' ),
01533 $contentObjectAttribute->attribute( 'language_code' ),
01534 false );
01535 }
01536 }
01537
01538
01539
01540
01541
01542
01543 function setOriginalAttributeDataValues( $attributeID, $attributeVersion, $attributeLanguage )
01544 {
01545 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01546 $originalData = array( 'attribute_id' => $attributeID,
01547 'attribute_version' => $attributeVersion,
01548 'attribute_language' => $attributeLanguage );
01549 $this->setOriginalAttributeData( $originalData );
01550 }
01551
01552
01553
01554
01555
01556
01557
01558 function &imageSerialNumberRaw()
01559 {
01560 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01561
01562 if ( isset( $contentObjectAttributeData['DataTypeCustom']['serial_number'] ) and $contentObjectAttributeData['DataTypeCustom']['serial_number'] >= 0 )
01563 return $contentObjectAttributeData['DataTypeCustom']['serial_number'];
01564
01565 $contentObjectAttributeData['DataTypeCustom']['serial_number'] = 0;
01566 return $contentObjectAttributeData['DataTypeCustom']['serial_number'];
01567 }
01568
01569
01570
01571
01572 function generateXMLData()
01573 {
01574
01575
01576
01577 include_once( "lib/ezdb/classes/ezdb.php" );
01578
01579 $db =& eZDB::instance();
01580
01581 $contentObjectAttributeData =& $this->ContentObjectAttributeData;
01582 $attributeID = $contentObjectAttributeData['id'];
01583 $attributeVersion = $contentObjectAttributeData['version'];
01584
01585 if ( is_numeric( $attributeID ) )
01586 {
01587 $imageRow = $db->arrayQuery( "SELECT * FROM ezimage
01588 WHERE contentobject_attribute_id=$attributeID AND
01589 version=$attributeVersion" );
01590 }
01591
01592 $doc = new eZDOMDocument();
01593 $imageNode = $doc->createElementNode( "ezimage" );
01594 $doc->setRoot( $imageNode );
01595
01596 $isValid = false;
01597 $fileName = false;
01598 $suffix = false;
01599 $baseName = false;
01600 $dirPath = false;
01601 $filePath = false;
01602 $originalFileName = false;
01603 $mimeType = false;
01604 $width = false;
01605 $height = false;
01606 $altText = false;
01607
01608 include_once( 'lib/ezutils/classes/ezmimetype.php' );
01609 if ( count( $imageRow ) == 1 )
01610 {
01611 require_once( 'kernel/classes/ezclusterfilehandler.php' );
01612 $fileHandler = eZClusterFileHandler::instance();
01613
01614 $fileName = $imageRow[0]['filename'];
01615 $originalFileName = $imageRow[0]['original_filename'];
01616 $mimeType = $imageRow[0]['mime_type'];
01617 $altText = $imageRow[0]['alternative_text'];
01618
01619 $dirPath = eZSys::storageDirectory() . '/original/image';
01620 $filePath = $dirPath . '/' . $fileName;
01621
01622
01623
01624 $baseName = $fileName;
01625 $dotPosition = strrpos( $fileName, '.' );
01626 if ( $dotPosition !== false )
01627 {
01628 $baseName = substr( $fileName, 0, $dotPosition );
01629 $suffix = substr( $fileName, $dotPosition + 1 );
01630 }
01631
01632 $width = false;
01633 $height = false;
01634 if ( !file_exists( $filePath ) )
01635 {
01636 $referenceDirPath = eZSys::storageDirectory() . '/reference/image';
01637 $suffixList = array( 'jpg', 'png', 'gif' );
01638 foreach ( $suffixList as $suffix )
01639 {
01640
01641
01642 $referenceFilePath = $referenceDirPath . '/' . $baseName . '.' . $suffix;
01643 if ( file_exists( $referenceFilePath ) )
01644 {
01645 $filePath = $referenceFilePath;
01646 $dirPath = $referenceDirPath;
01647 break;
01648 }
01649 }
01650 }
01651
01652
01653
01654 if ( file_exists( $filePath ) )
01655 {
01656 $isValid = true;
01657 $info = getimagesize( $filePath );
01658 if ( $info )
01659 {
01660 $width = $info[0];
01661 $height = $info[1];
01662 }
01663 $mimeInfo = eZMimeType::findByFileContents( $filePath );
01664 $mimeType = $mimeInfo['name'];
01665
01666 $newFilePath = $filePath;
01667 $newSuffix = $suffix;
01668 $contentVersion = eZContentObjectVersion::fetchVersion( $contentObjectAttributeData['version'],
01669 $contentObjectAttributeData['contentobject_id'] );
01670 if ( $contentVersion )
01671 {
01672 $objectName = $this->imageName( $contentObjectAttributeData, $contentVersion );
01673 $objectPathString = $this->imagePath( $contentObjectAttributeData, $contentVersion );
01674
01675 $newDirPath = $objectPathString;
01676 $newFileName = $objectName . '.' . $mimeInfo['suffix'];
01677 $newSuffix = $mimeInfo['suffix'];
01678 $newFilePath = $newDirPath . '/' . $newFileName;
01679 $newBaseName = $objectName;
01680 }
01681
01682
01683
01684 if ( $newFilePath != $filePath )
01685 {
01686 if ( !file_exists( $newDirPath ) )
01687 {
01688 include_once( 'lib/ezfile/classes/ezdir.php' );
01689 eZDir::mkdir( $newDirPath, eZDir::directoryPermission(), true );
01690 }
01691 eZFileHandler::copy( $filePath, $newFilePath );
01692
01693
01694
01695
01696
01697
01698
01699 $filePath = $newFilePath;
01700 $fileName = $newFileName;
01701 $suffix = $newSuffix;
01702 $dirPath = $newDirPath;
01703 $baseName = $newBaseName;
01704 }
01705 }
01706
01707
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728 }
01729 include_once( 'kernel/common/image.php' );
01730 $imageManager =& imageInit();
01731
01732 $mimeData = eZMimeType::findByFileContents( $fileName );
01733
01734 $imageManager->analyzeImage( $mimeData );
01735
01736 $imageNode->appendAttribute( $doc->createAttributeNode( 'serial_number', false ) );
01737 $imageNode->appendAttribute( $doc->createAttributeNode( 'is_valid', $isValid ) );
01738 $imageNode->appendAttribute( $doc->createAttributeNode( 'filename', $fileName ) );
01739 $imageNode->appendAttribute( $doc->createAttributeNode( 'suffix', $suffix ) );
01740 $imageNode->appendAttribute( $doc->createAttributeNode( 'basename', $baseName ) );
01741 $imageNode->appendAttribute( $doc->createAttributeNode( 'dirpath', $dirPath ) );
01742 $imageNode->appendAttribute( $doc->createAttributeNode( 'url', $filePath ) );
01743 $imageNode->appendAttribute( $doc->createAttributeNode( 'original_filename', $originalFileName ) );
01744 $imageNode->appendAttribute( $doc->createAttributeNode( 'mime_type', $mimeType ) );
01745 $imageNode->appendAttribute( $doc->createAttributeNode( 'width', $width ) );
01746 $imageNode->appendAttribute( $doc->createAttributeNode( 'height', $height ) );
01747 $imageNode->appendAttribute( $doc->createAttributeNode( 'alternative_text', $altText ) );
01748 $imageNode->appendAttribute( $doc->createAttributeNode( 'alias_key', $imageManager->createImageAliasKey( $imageManager->alias( 'original' ) ) ) );
01749 $imageNode->appendAttribute( $doc->createAttributeNode( 'timestamp', time() ) );
01750
01751 $this->createImageInformationNode( $imageNode, $mimeData );
01752
01753 $attr = false;
01754 $this->storeDOMTree( $doc, true, $attr );
01755
01756
01757 eZImageFile::appendFilepath( $contentObjectAttributeData['id'], $filePath );
01758 }
01759
01760
01761
01762 var $ContentObjectAttributeData;
01763
01764 var $ContentObjectAttribute;
01765
01766 }
01767 ?>