-
Notifications
You must be signed in to change notification settings - Fork 243
/
ezimagealiashandler.php
1686 lines (1475 loc) · 65.2 KB
/
ezimagealiashandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File containing the eZImageAliasHandler class.
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package kernel
*/
/*!
\class eZImageAliasHandler ezimagealiashandler.php
\ingroup eZDatatype
\brief Internal manager for the eZImage datatype
Takes care of image conversion and serialization from and to
the internal XML format.
\note This handler was introduced in eZ Publish 3.3 and will detect older
eZImage structures and convert them on the fly.
\note The XML storage was improved in 3.8, from then it always stores the
attribute ID, version and language in the <original> tag.
This was required to get the new multi-language features to work.
*/
class eZImageAliasHandler
{
/*!
Creates the handler and creates a reference to the contentobject attribute that created it.
*/
function eZImageAliasHandler( $contentObjectAttribute )
{
$this->ContentObjectAttributeData = array();
if ( is_object( $contentObjectAttribute ) )
{
$this->ContentObjectAttributeData['id'] = $contentObjectAttribute->attribute( 'id' );
$this->ContentObjectAttributeData['contentobject_id'] = $contentObjectAttribute->attribute( 'contentobject_id' );
$this->ContentObjectAttributeData['version'] = $contentObjectAttribute->attribute( 'version' );
$this->ContentObjectAttributeData['language_code'] = $contentObjectAttribute->attribute( 'language_code' );
$this->ContentObjectAttributeData['can_translate'] = $contentObjectAttribute->attribute( 'can_translate' );
$this->ContentObjectAttributeData['data_text'] = $contentObjectAttribute->attribute( 'data_text' );
$this->ContentObjectAttributeData['DataTypeCustom'] = $contentObjectAttribute->DataTypeCustom;
if ( !is_array( $this->ContentObjectAttributeData['DataTypeCustom'] ) )
{
$this->ContentObjectAttributeData['DataTypeCustom'] = array();
}
}
else
{
eZDebug::writeWarning( 'Invalid eZContentObjectAttribute', __METHOD__ );
}
}
/*!
Lists all available image aliases as attributes as well as:
- alternative_text - The alternative text input by the user, can be empty
- original_filename - The name of image which it had on the users disk before it was uploaded
- is_valid - A boolean which says if there is an image here or not.
*/
function attributes()
{
$imageManager = eZImageManager::factory();
$aliasList = $imageManager->aliasList();
return array_merge( array( 'alternative_text',
'original_filename',
'is_valid' ),
array_keys( $aliasList ) );
}
/*!
\return true if the attribute named \a $attributeName exists.
See eZImageAliasHandler::attributes() for which attributes are available.
*/
function hasAttribute( $attributeName )
{
if ( in_array( $attributeName,
array( 'alternative_text',
'original_filename',
'is_valid' ) ) )
return true;
$imageManager = eZImageManager::factory();
if ( $imageManager->hasAlias( $attributeName ) )
return true;
return false;
}
/*!
\return the value of the attribute named \a $attributeName.
See eZImageAliasHandler::attributes() for which attributes are available.
*/
function attribute( $attributeName )
{
if ( in_array( $attributeName,
array( 'alternative_text',
'original_filename',
'is_valid' ) ) )
{
return $this->attributeFromOriginal( $attributeName );
}
$aliasName = $attributeName;
return $this->imageAlias( $aliasName );
}
/*!
\return The value of the attribute named \a $attributeName from the 'original' image alias.
This is a quick way for extracting information from the 'original' image alias.
*/
function &attributeFromOriginal( $attributeName )
{
$originalAlias = $this->attribute( 'original' );
if ( $originalAlias )
return $originalAlias[$attributeName];
$retValue = null;
return $retValue;
}
/*!
Sets the attribute named \a $attributeName to have the value \a $attributeValue.
The following attributes can be set:
- alternative text
- original_filename
*/
function setAttribute( $attributeName, $attributeValue )
{
if ( in_array( $attributeName,
array( 'alternative_text',
'original_filename' ) ) )
{
$aliasList = $this->aliasList();
foreach ( array_keys( $aliasList ) as $aliasName )
{
$this->setAliasAttribute( $aliasName, $attributeName, $attributeValue );
}
if ( $attributeName == 'alternative_text' )
{
$text = $this->displayText( $attributeValue );
foreach ( array_keys( $aliasList ) as $aliasName )
{
$this->setAliasAttribute( $aliasName, 'text', $text );
}
}
$this->recreateDOMTree();
$this->setStorageRequired();
return true;
}
return false;
}
/*!
\return \c true if this is considered to be owner of the image.
It will be considered an owner if attribute data is not a copy
of another attribute. For instance each time a new image is uploaded
this will return \c true.
*/
function isImageOwner()
{
$originalData = $this->originalAttributeData();
return ( $originalData['attribute_id'] == false ||
( $originalData['attribute_id'] == $this->ContentObjectAttributeData['id'] &&
$originalData['attribute_version'] == $this->ContentObjectAttributeData['version'] &&
$originalData['attribute_language'] == $this->ContentObjectAttributeData['language_code'] ) );
}
/*!
\return The current serial number, the value will be 1 or higher.
The serial number is used to create unique filenames for uploaded images,
it will be increased each time an image is uploaded.
\note This was required to get around the problem where browsers
caches image information, if two images were uploaded in one version (e.g. a draft)
the browser would not load the new image since it thought it had not changed.
*/
function imageSerialNumber()
{
$serialNumber = $this->imageSerialNumberRaw();
if ( $serialNumber < 1 )
{
$serialNumber = 1;
}
return $serialNumber;
}
/*!
Increases the serial by one.
*/
function increaseImageSerialNumber()
{
$this->setImageSerialNumber( $this->imageSerialNumberRaw() + 1 );
}
/*!
Resets the serial number to zero.
*/
function resetImageSerialNumber()
{
$this->setImageSerialNumber( 0 );
}
/*!
\return A text string which can be used as display for the image.
The text string will either contain the alternative text from the attribute
or the parameter \a $alternativeText if it is set.
*/
function displayText( $alternativeText = null )
{
if ( $alternativeText === null )
$text = $this->attribute( 'alternative_text' );
else
$text = $alternativeText;
return $text;
}
/*!
\return The full directory path to the image, this includes the var and storage directory.
*/
function directoryPath()
{
$aliasList = $this->aliasList();
if ( isset( $aliasList['original'] ) )
{
return $aliasList['original']['dirpath'];
}
return false;
}
/*!
\return A normalized name for the image.
The image name will generated from the name of the current version.
If this is empty it will use the object name or the alternative text.
This ensures that the image has a name which corresponds to the object it belongs to.
The normalization ensures that the name only contains filename and URL friendly characters.
*/
function imageName( $contentObjectAttribute, $contentVersion, $language = false )
{
if ( $language === false )
{
if ( is_object( $contentObjectAttribute ) ) // for backward compatibility when eZImageAliasHandler used $this->contentObjectAttribute
{
$language = $contentObjectAttribute->attribute( 'language_code' );
}
else
{
$language = $contentObjectAttribute['language_code'];
}
}
$objectName = $contentVersion->versionName( $language );
if ( !$objectName )
{
$objectName = $contentVersion->name( $language );
if ( !$objectName )
{
$objectName = $this->attribute( 'alternative_text' );
if ( !$objectName )
{
$objectName = ezpI18n::tr( 'kernel/classes/datatypes', 'image', 'Default image name' );
}
}
}
$objectName = eZImageAliasHandler::normalizeImageName( $objectName );
$objectName .= $this->imageSerialNumber();
return $objectName;
}
/*!
\return A normalized name for the image based on a node.
Similar to \a imageName() but fetches name information from the node \a $mainNode.
The normalization ensures that the name only contains filename and URL friendly characters.
*/
function imageNameByNode( $contentObjectAttribute, $mainNode, $language = false )
{
if ( $language === false )
{
if ( is_object( $contentObjectAttribute ) ) // for backward compatibility when eZImageAliasHandler used $this->contentObjectAttribute
{
$language = $contentObjectAttribute->attribute( 'language_code' );
}
else
{
$language = $contentObjectAttribute['language_code'];
}
}
$objectName = $mainNode->getName( $language );
if ( !$objectName )
{
$objectName = $this->attribute( 'alternative_text' );
if ( !$objectName )
{
$objectName = ezpI18n::tr( 'kernel/classes/datatypes', 'image', 'Default image name' );
}
}
$objectName = eZImageAliasHandler::normalizeImageName( $objectName );
return $objectName;
}
/*!
\return The storage path for the image.
The path is calculated by using information from the current object and version.
If the object is in the node tree it will contain a path that matches the node path,
if not it will be placed in the versioned storage repository.
*/
function imagePath( $contentObjectAttribute, $contentVersion, $isImageOwner = null )
{
$useVersion = false;
if ( $isImageOwner === null )
$isImageOwner = $this->isImageOwner();
if ( $contentVersion->attribute( 'status' ) == eZContentObjectVersion::STATUS_PUBLISHED or
!$isImageOwner )
{
$contentObject = $contentVersion->attribute( 'contentobject' );
$mainNode = $contentObject->attribute( 'main_node' );
if ( !$mainNode )
{
$ini = eZINI::instance( 'image.ini' );
$contentImageSubtree = $ini->variable( 'FileSettings', 'VersionedImages' );
$pathString = $contentImageSubtree;
$useVersion = true;
}
else
{
$ini = eZINI::instance( 'image.ini' );
$contentImageSubtree = $ini->variable( 'FileSettings', 'PublishedImages' );
$pathString = $mainNode->pathWithNames();
$pathString = function_exists( 'mb_strtolower' ) ? mb_strtolower( $pathString ) : strtolower( $pathString );
$pathString = $contentImageSubtree . '/' . $pathString;
}
}
else
{
$ini = eZINI::instance( 'image.ini' );
$contentImageSubtree = $ini->variable( 'FileSettings', 'VersionedImages' );
$pathString = $contentImageSubtree;
$useVersion = true;
}
$attributeData = $this->originalAttributeData();
$attributeID = $attributeData['attribute_id'];
$attributeVersion = $attributeData['attribute_version'];
$attributeLanguage = $attributeData['attribute_language'];
if ( $useVersion )
$identifierString = $attributeID . '/' . $attributeVersion . '-' . $attributeLanguage;
else
$identifierString = $attributeID . '-' . $attributeVersion . '-' . $attributeLanguage;
$imagePath = eZSys::storageDirectory() . '/' . $pathString . '/' . $identifierString;
return $imagePath;
}
/*!
\return The storage path for the image based on a node.
Similar to \a imagePath() but fetches name information from the node \a $mainNode.
*/
function imagePathByNode( $contentObjectAttribute, $mainNode )
{
$pathString = $mainNode->pathWithNames();
$pathString = function_exists( 'mb_strtolower' ) ? mb_strtolower( $pathString ) : strtolower( $pathString );
$ini = eZINI::instance( 'image.ini' );
$contentImageSubtree = $ini->variable( 'FileSettings', 'PublishedImages' );
$attributeData = $this->originalAttributeData();
$attributeID = $attributeData['attribute_id'];
$attributeVersion = $attributeData['attribute_version'];
$attributeLanguage = $attributeData['attribute_language'];
$pathParts = array( eZSys::storageDirectory(), $contentImageSubtree );
if ( $pathString != '' )
{
$pathParts[] = $pathString;
}
$pathParts[] = $attributeID . '-' . $attributeVersion . '-' . $attributeLanguage;
$imagePath = implode( '/', $pathParts );
return $imagePath;
}
/*!
\return The image alias structure for the alias named \a $aliasName.
This will create the image alias if it does not exist yet, this can involve
running image operations to for instance scale the image.
*/
function imageAlias( $aliasName )
{
$imageManager = eZImageManager::factory();
if ( !$imageManager->hasAlias( $aliasName ) )
{
return null;
}
$aliasList = $this->aliasList();
if ( array_key_exists( $aliasName, $aliasList ) )
{
return $aliasList[$aliasName];
}
else
{
$original = $aliasList['original'];
$basename = $original['basename'];
if ( $imageManager->createImageAlias( $aliasName, $aliasList,
array( 'basename' => $basename ) ) )
{
$text = $this->displayText( $original['alternative_text'] );
$originalFilename = $original['original_filename'];
foreach ( $aliasList as $aliasKey => $alias )
{
$alias['original_filename'] = $originalFilename;
$alias['text'] = $text;
if ( $alias['url'] )
{
$aliasFile = eZClusterFileHandler::instance( $alias['url'] );
if( $aliasFile->exists() )
$alias['filesize'] = $aliasFile->size();
}
if ( $alias['is_new'] )
{
eZImageFile::appendFilepath( $this->ContentObjectAttributeData['id'], $alias['url'] );
}
$aliasList[$aliasKey] = $alias;
}
$this->setAliasList( $aliasList );
$this->addImageAliases( $aliasList );
$aliasList = $this->aliasList();
return $aliasList[$aliasName];
}
}
return null;
}
/*!
Set alias list. Set alias list to current object
\param aliasList alias list
*/
protected function setAliasList( $aliasList )
{
$this->ContentObjectAttributeData['DataTypeCustom']['alias_list'] = $aliasList;
}
/*
Set alias variation
\param alias name
\param variation array
*/
protected function setAliasVariation( $aliasName, $variation )
{
$this->ContentObjectAttributeData['DataTypeCustom']['alias_list'][$aliasName] = $variation;
}
/*!
Set alias value.
\param aliasName alias name
\param attributeName attribute name
\param value attribute value
*/
protected function setAliasAttribute( $aliasName, $attributeName, $value )
{
$this->ContentObjectAttributeData['DataTypeCustom']['alias_list'][$aliasName][$attributeName] = $value;
}
/*!
\private
\return A list of aliases structures for the current attribute.ezxml
The first this is called the XML data will be parsed into the internal
structures. Subsequent calls will simply return the internal structure.
*/
function aliasList( $checkValidity = true )
{
if ( $checkValidity && isset( $this->ContentObjectAttributeData['DataTypeCustom']['alias_list'] ) )
{
return $this->ContentObjectAttributeData['DataTypeCustom']['alias_list'];
}
eZDebug::accumulatorStart('imageparse', 'XML', 'Image XML parsing' );
$domTree = new DOMDocument( '1.0', 'utf-8' );
$xmlString = $this->ContentObjectAttributeData['data_text'];
$success = false;
if ( $xmlString != '' )
{
$success = $domTree->loadXML( $xmlString );
}
if ( !$success )
{
$this->generateXMLData();
$xmlString = $this->ContentObjectAttributeData['data_text'];
$success = $domTree->loadXML( $xmlString );
}
$this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $domTree;
$imageNodeArray = $domTree->getElementsByTagName( "ezimage" );
$aliasList = array();
// I think this is a bug in the plain package or related to the bug I filed
// about the image datatype serialization
// http://ez.no/bugs/view/8821 ezpm Error: (eZFileHandler::copy) Unable to open source file in read mode
if ( $imageNodeArray->length == 0 )
{
return $aliasList;
}
$imageNode = $imageNodeArray->item( 0 );
$imageInfoNodeArray = $imageNode->getElementsByTagName( "information" );
$imageVariationNodeArray = $imageNode->getElementsByTagName( "alias" );
$imageOriginalArray = $imageNode->getElementsByTagName( "original" );
$aliasEntry = array();
$alternativeText = $imageNode->getAttribute( 'alternative_text' );
$originalFilename = $imageNode->getAttribute( 'original_filename' );
$basename = $imageNode->getAttribute( 'basename' );
$displayText = $this->displayText( $alternativeText );
$originalData = array( 'attribute_id' => '',
'attribute_version' => '',
'attribute_language' => '' );
if ( $imageOriginalArray->length > 0 )
{
$imageOriginalNode = $imageOriginalArray->item( 0 );
$originalData = array( 'attribute_id' => $imageOriginalNode->getAttribute( 'attribute_id' ),
'attribute_version' => $imageOriginalNode->getAttribute( 'attribute_version' ),
'attribute_language' => $imageOriginalNode->getAttribute( 'attribute_language' ) );
}
if ( strlen( $originalData['attribute_id'] ) == 0 ||
strlen( $originalData['attribute_version'] ) == 0 ||
strlen( $originalData['attribute_language'] ) == 0 )
{
$originalData = array( 'attribute_id' => $this->ContentObjectAttributeData['id'],
'attribute_version' => $this->ContentObjectAttributeData['version'],
'attribute_language' => $this->ContentObjectAttributeData['language_code'] );
}
$this->setOriginalAttributeData( $originalData );
$aliasEntry['name'] = 'original';
$aliasEntry['width'] = $imageNode->getAttribute( 'width' );
$aliasEntry['height'] = $imageNode->getAttribute( 'height' );
$aliasEntry['mime_type'] = $imageNode->getAttribute( 'mime_type' );
$aliasEntry['filename'] = $imageNode->getAttribute( 'filename' );
$aliasEntry['suffix'] = $imageNode->getAttribute( 'suffix' );
$aliasEntry['dirpath'] = $imageNode->getAttribute( 'dirpath' );
$aliasEntry['basename'] = $basename;
$aliasEntry['alternative_text'] = $alternativeText;
$aliasEntry['text'] = $displayText;
$aliasEntry['original_filename'] = $originalFilename;
$aliasEntry['url'] = $imageNode->getAttribute( 'url' );
$aliasEntry['alias_key'] = $imageNode->getAttribute( 'alias_key' );
$aliasEntry['timestamp'] = $imageNode->getAttribute( 'timestamp' );
$aliasEntry['full_path'] = $aliasEntry['url'];
$aliasEntry['is_valid'] = $imageNode->getAttribute( 'is_valid' );
$aliasEntry['is_new'] = false;
$aliasEntry['filesize'] = false;
if ( $aliasEntry['url'] )
{
$aliasFile = eZClusterFileHandler::instance( $aliasEntry['url'] );
if ( $aliasFile->exists() )
$aliasEntry['filesize'] = $aliasFile->size();
}
$imageInformation = false;
if ( $imageInfoNodeArray->length > 0 )
{
$imageInfoNode = $imageInfoNodeArray->item( 0 );
$imageInformation = $this->parseInformationNode( $imageInfoNode );
}
$aliasEntry['info'] = $imageInformation;
$serialNumber = $imageNode->getAttribute( 'serial_number' );
if ( $serialNumber )
{
$this->setImageSerialNumber( $serialNumber );
}
$aliasList['original'] = $aliasEntry;
if ( $imageVariationNodeArray->length > 0 )
{
$imageManager = eZImageManager::factory();
foreach ( $imageVariationNodeArray as $imageVariation )
{
$aliasEntry = array();
$aliasEntry['name'] = $imageVariation->getAttribute( 'name' );
$aliasEntry['width'] = $imageVariation->getAttribute( 'width' );
$aliasEntry['height'] = $imageVariation->getAttribute( 'height' );
$aliasEntry['mime_type'] = $imageVariation->getAttribute( 'mime_type' );
$aliasEntry['filename'] = $imageVariation->getAttribute( 'filename' );
$aliasEntry['suffix'] = $imageVariation->getAttribute( 'suffix' );
$aliasEntry['dirpath'] = $imageVariation->getAttribute( 'dirpath' );
$aliasEntry['alias_key'] = $imageVariation->getAttribute( 'alias_key' );
$aliasEntry['timestamp'] = $imageVariation->getAttribute( 'timestamp' );
$aliasEntry['is_valid'] = $imageVariation->getAttribute( 'is_valid' );
$aliasEntry['url'] = $imageVariation->getAttribute( 'url' );
$aliasEntry['basename'] = $basename;
$aliasEntry['alternative_text'] = $alternativeText;
$aliasEntry['text'] = $displayText;
$aliasEntry['original_filename'] = $originalFilename;
$aliasEntry['full_path'] = $aliasEntry['url'];
$aliasEntry['is_new'] = false;
$aliasEntry['info'] = $imageInformation;
if ( $aliasEntry['url'] )
{
$aliasFile = eZClusterFileHandler::instance( $aliasEntry['url'] );
if ( $aliasFile->exists() )
$aliasEntry['filesize'] = $aliasFile->size();
}
if ( $imageManager->isImageAliasValid( $aliasEntry ) || !$checkValidity )
{
$aliasList[$aliasEntry['name']] = $aliasEntry;
}
}
}
if ( $checkValidity )
$this->setAliasList( $aliasList );
eZDebug::accumulatorStop( 'imageparse' );
return $aliasList;
}
/**
* Removes all image alias files which the attribute refers to.
*
* @param eZContentObjectAttribute
* @note If you want to remove the alias information use removeAliases().
*/
static function removeAllAliases( $contentObjectAttribute )
{
$handler = $contentObjectAttribute->attribute( 'content' );
if ( !$handler->isImageOwner() )
{
return;
}
$attributeData = $handler->originalAttributeData();
$files = eZImageFile::fetchForContentObjectAttribute( $attributeData['attribute_id'], false );
$dirs = array();
foreach ( $files as $filepath )
{
$file = eZClusterFileHandler::instance( $filepath );
if ( $file->exists() )
{
$file->fileDelete( $filepath );
$dirs[] = eZDir::dirpath( $filepath );
}
}
$dirs = array_unique( $dirs );
foreach ( $dirs as $dirpath )
{
eZDir::cleanupEmptyDirectories( $dirpath );
}
eZImageFile::removeForContentObjectAttribute( $attributeData['attribute_id'] );
}
/**
* Removes the images alias while keeping the original image.
* @see eZCache::purgeAllAliases()
*
* @param eZContentObjectAttribute $contentObjectAttribute
*/
public function purgeAllAliases( eZContentObjectAttribute $contentObjectAttribute )
{
$aliasList = $this->aliasList( false );
unset( $aliasList['original'] ); // keeping original
foreach ( $aliasList as $aliasName => $alias )
{
$filepath = $alias['url'];
eZImageFile::removeFilepath( $this->ContentObjectAttributeData['id'], $filepath );
$file = eZClusterFileHandler::instance( $filepath );
if ( $file->exists() )
{
$file->purge();
eZDir::cleanupEmptyDirectories( $alias['dirpath'] );
}
else
{
eZDebug::writeError( "Image file $filepath for alias $aliasName does not exist, could not remove from disk", __METHOD__ );
}
}
$doc = $this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'];
foreach ( $doc->getElementsByTagName( 'alias' ) as $aliasNode )
{
$aliasNode->parentNode->removeChild( $aliasNode );
}
$this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $doc;
unset( $this->ContentObjectAttributeData['DataTypeCustom']['alias_list'] );
$this->storeDOMTree( $doc, true, $contentObjectAttribute );
}
/**
* Removes all the image aliases and their information.
* The stored images will also be removed if the attribute is the owner
* of the images.
*
* After the images are removed the attribute will containe an internal
* structure with empty data
*
* @param eZContentObjectAttribute $contentObjectAttribute
* Content object attribute to remove aliases for
*
* @return void
*/
function removeAliases( $contentObjectAttribute )
{
$aliasList = $this->aliasList();
$alternativeText = false;
$contentObjectAttributeVersion = $this->ContentObjectAttributeData['version'];
$contentObjectAttributeID = $this->ContentObjectAttributeData['id'];
$isImageOwner = $this->isImageOwner();
// We loop over each image alias, and look up the file in ezcontentobject_attribute
// Only images referenced by one version will be removed
foreach ( $aliasList as $aliasName => $alias )
{
$dirpath = $alias['dirpath'];
$doNotDelete = false; // Do not delete files from storage
if ( $aliasName == 'original' )
$alternativeText = $alias['alternative_text'];
if ( $alias['is_valid'] )
{
$filepath = $alias['url'];
// Fetch ezimage attributes that use $filepath
// Always returns current attribute (array of $contentObjectAttributeID and $contentObjectAttributeVersion)
$dbResult = eZImageFile::fetchImageAttributesByFilepath( $filepath, $contentObjectAttributeID );
$dbResultCount = count( $dbResult );
// Check if there are the attributes.
if ( $dbResultCount > 0 )
{
$doNotDelete = true;
foreach ( $dbResult as $res )
{
// We only look results where the version matches
if ( $res['version'] == $contentObjectAttributeVersion )
{
// If more than one result has been returned, it means
// that another version is using the same image,
// and we should not delete this file
if ( $dbResultCount > 1 )
{
continue;
}
// Only one result means that the current attribute
// & version are the only ones using this image,
// and it can be removed
else
{
$doNotDelete = false;
}
}
eZImageFile::appendFilepath( $res['id'], $filepath, true );
}
}
if ( !$doNotDelete )
{
eZImageFile::removeFilepath( $contentObjectAttributeID, $filepath );
$file = eZClusterFileHandler::instance( $filepath );
if ( $file->exists() )
{
$file->delete();
eZDir::cleanupEmptyDirectories( $dirpath );
}
else
{
eZDebug::writeError( "Image file $filepath for alias $aliasName does not exist, could not remove from disk", __METHOD__ );
}
}
}
}
$doc = new DOMDocument( '1.0', 'utf-8' );
$imageNode = $doc->createElement( "ezimage" );
$doc->appendChild( $imageNode );
$imageNode->setAttribute( 'serial_number', false );
$imageNode->setAttribute( 'is_valid', false );
$imageNode->setAttribute( 'filename', false );
$imageNode->setAttribute( 'suffix', false );
$imageNode->setAttribute( 'basename', false );
$imageNode->setAttribute( 'dirpath', false );
$imageNode->setAttribute( 'url', false );
$imageNode->setAttribute( 'original_filename', false );
$imageNode->setAttribute( 'mime_type', false );
$imageNode->setAttribute( 'width', false );
$imageNode->setAttribute( 'height', false );
$imageNode->setAttribute( 'alternative_text', $alternativeText );
$imageNode->setAttribute( 'alias_key', false );
$imageNode->setAttribute( 'timestamp', false );
$this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $doc;
unset( $this->ContentObjectAttributeData['DataTypeCustom']['alias_list'] );
$this->storeDOMTree( $doc, true, $contentObjectAttribute );
}
/*!
Will update the path for images to point to the new path \a $dirpath and filename \a $name.
This is usually called when the object contain the image attribute is moved in the tree.
*/
function updateAliasPath( $dirpath, $name )
{
if ( !file_exists( $dirpath ) )
{
eZDir::mkdir( $dirpath, false, true );
}
$aliasList = $this->aliasList();
$this->resetImageSerialNumber();
foreach ( $aliasList as $aliasName => $alias )
{
if ( $alias['dirpath'] != $dirpath )
{
$oldDirpath = $alias['url'];
$oldURL = $alias['url'];
$basename = $name;
if ( $aliasName != 'original' )
$basename .= '_' . $aliasName;
eZMimeType::changeFileData( $alias, $dirpath, $basename );
$alias['full_path'] = $alias['url'];
if ( $this->isImageOwner() )
{
if ( $oldURL == '' )
{
continue;
}
$fileHandler = eZClusterFileHandler::instance();
$fileHandler->fileMove( $oldURL, $alias['url'] );
eZDir::cleanupEmptyDirectories( $oldDirpath );
eZImageFile::moveFilepath( $this->ContentObjectAttributeData['id'], $oldURL, $alias['url'] );
}
else
{
$fileHandler = eZClusterFileHandler::instance();
$fileHandler->fileLinkCopy( $oldURL, $alias['url'], false );
eZImageFile::appendFilepath( $this->ContentObjectAttributeData['id'], $alias['url'] );
}
$this->setAliasVariation( $aliasName, $alias );
}
}
$this->recreateDOMTree();
$this->setStorageRequired();
}
/*!
\private
Creates XML attributes containing information on the original image attribute.
The new attributes will be appended to \a $originalNode.
*/
function createOriginalAttributeXMLData( $originalNode, $originalData )
{
$originalNode->setAttribute( 'attribute_id', $originalData['attribute_id'] );
$originalNode->setAttribute( 'attribute_version', $originalData['attribute_version'] );
$originalNode->setAttribute( 'attribute_language', $originalData['attribute_language'] );
}
/*!
\private
Recreates the DOM tree from the internal array structures and stores the DOM tree
in the 'data_text' field of the attribute.
*/
function recreateDOMTree()
{
$aliasList = $this->aliasList();
$doc = new DOMDocument( '1.0', 'utf-8' );
$imageNode = $doc->createElement( "ezimage" );
$doc->appendChild( $imageNode );
$originalNode = $doc->createElement( "original" );
$imageNode->appendChild( $originalNode );
$imageManager = eZImageManager::factory();
$aliasName = 'original';
$originalData = $this->originalAttributeData();
$this->createOriginalAttributeXMLData( $originalNode, $originalData );
$imageNode->setAttribute( 'serial_number', $this->imageSerialNumber() );
$imageNode->setAttribute( 'is_valid', $aliasList[$aliasName]['is_valid'] );
$imageNode->setAttribute( 'filename', $aliasList[$aliasName]['filename'] );
$imageNode->setAttribute( 'suffix', $aliasList[$aliasName]['suffix'] );
$imageNode->setAttribute( 'basename', $aliasList[$aliasName]['basename'] );
$imageNode->setAttribute( 'dirpath', $aliasList[$aliasName]['dirpath'] );
$imageNode->setAttribute( 'url', $aliasList[$aliasName]['url'] );
$imageNode->setAttribute( 'original_filename', $aliasList[$aliasName]['original_filename'] );
$imageNode->setAttribute( 'mime_type', $aliasList[$aliasName]['mime_type'] );
$imageNode->setAttribute( 'width', $aliasList[$aliasName]['width'] );
$imageNode->setAttribute( 'height', $aliasList[$aliasName]['height'] );
$imageNode->setAttribute( 'alternative_text', $aliasList[$aliasName]['alternative_text'] );
$imageNode->setAttribute( 'alias_key', $imageManager->createImageAliasKey( $imageManager->alias( $aliasName ) ) );
$imageNode->setAttribute( 'timestamp', $aliasList[$aliasName]['timestamp'] );
$filename = $aliasList[$aliasName]['url'];
if ( $filename )
{
$imageFile = eZClusterFileHandler::instance( $filename );
$fetchedFilePath = $imageFile->fetchUnique();
//(Cluster) Get mime data of real file, and fetch info by image analizer.
$mimeDataTemp = eZMimeType::findByFileContents( $fetchedFilePath );
$imageManager->analyzeImage( $mimeDataTemp );
//(Cluster) Get mime data of a file which does not really exist on file system. We need this to build correct imageInformationNode.
$mimeData = eZMimeType::findByURL( $filename );
if ( isset( $mimeDataTemp['info'] ) )
$mimeData['info'] = $mimeDataTemp['info'];
$this->createImageInformationNode( $imageNode, $mimeData );
$imageFile->fileDeleteLocal( $fetchedFilePath );
}
foreach ( $aliasList as $aliasName => $alias )
{
if ( $aliasName == 'original' )
continue;
$this->addImageAliasToXML( $doc, $alias );
}
$this->setDOMTree( $doc );
}
/*!
\return the DOM tree for the current content object attribute.
\note It will cache the result in the DataTypeCustom member variable of the
content object attribute in the 'dom_tree' key.
*/
function domTree()
{
$contentObjectAttributeData = $this->ContentObjectAttributeData;
if ( isset( $contentObjectAttributeData['DataTypeCustom']['dom_tree'] ) )
{
return $contentObjectAttributeData['DataTypeCustom']['dom_tree'];
}
$dom = new DOMDocument( '1.0', 'utf-8' );
$xmlString = $contentObjectAttributeData['data_text'];
$success = $xmlString == '' ? false : $dom->loadXML( $xmlString );
if ( !$success )
{
$this->generateXMLData();
$xmlString = $this->ContentObjectAttributeData['data_text'];
$success = $dom->loadXML( $xmlString );
}
$contentObjectAttributeData['DataTypeCustom']['dom_tree'] = $dom;
return $dom;
}
/*!
\private
Parses the information node and generates the internal information structures.
The information node contains information from the image itself, for instance
EXIF data from a JPEG or TIFF image.
\param $imageInfoNode
\return $imageInformation array
*/
function parseInformationNode( $imageInfoNode )
{