/
class-albumbase.php
1565 lines (1471 loc) · 41.3 KB
/
class-albumbase.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
/**
* Album Base Class
* @package zpcore\classes\objects
*/
class AlbumBase extends MediaObject {
public $name; // Folder name of the album (full path from the albums folder)
public $linkname; // may have the .alb suffix stripped off
public $localpath; // Latin1 full server path to the album
public $exists = true; // Does the folder exist?
public $images = null; // Full images array storage.
public $parent = null; // The parent album name
public $parentalbum = null; // The parent album's album object (lazy)
public $parentalbums = null; // Array of objects of parent albums (lazy)
public $urparentalbum = null; // The ur parent album's album object (lazy)
public $sidecars = array(); // keeps the list of suffixes associated with this album
public $manage_rights = MANAGE_ALL_ALBUM_RIGHTS;
public $manage_some_rights = ALBUM_RIGHTS;
public $view_rights = ALL_ALBUMS_RIGHTS;
protected $subalbums = null; // Full album array storage.
protected $index;
protected $lastimagesort = NULL; // remember the order for the last album/image sorts
protected $lastsubalbumsort = NULL;
protected $albumthumbnail = NULL; // remember the album thumb for the duration of the script
protected $subrights = NULL; // cache for album subrights
protected $num_allalbums = null; // count of all subalbums of all sublevels
protected $num_allimages = null; // count of all images of all sublevels
protected $firstpageimages = null;
protected $firstpageimages_oneimagepage = null;
function __construct($folder8, $cache = true) {
$this->linkname = $this->name = $folder8;
$this->instantiate('albums', array('folder' => $this->name), 'folder', false, true);
$this->exists = false;
}
/**
* Wrapper instantiation function for albums. Do not instantiate directly
*
* @since ZnephotoCMS 1.6 - Moved to AlbumBase class as static method
*
* @param string $folder8 the name of the folder (inernal character set)
* @param bool $cache true if the album should be fetched from the cache
* @param bool $quiet true to supress error messages
* @return Album
*/
static function newAlbum($folder8, $cache = true, $quiet = false) {
global $_zp_album_handlers;
$suffix = getSuffix($folder8);
if (!$suffix || !array_key_exists($suffix, $_zp_album_handlers) || is_dir(ALBUM_FOLDER_SERVERPATH . internalToFilesystem($folder8))) {
return new Album($folder8, $cache, $quiet);
} else {
return new $_zp_album_handlers[$suffix]($folder8, $cache, $quiet);
}
}
/**
* Returns true if the object is a zenphoto 'album'
*
* @since ZnephotoCMS 1.6 - Moved to AlbumBase class as static method
*
* @param object $album
* @return bool
*/
static function isAlbumClass($album = NULL) {
global $_zp_current_album;
if (is_null($album)) {
if (!in_context(ZP_ALBUM))
return false;
$album = $_zp_current_album;
}
return is_object($album) && ($album->table == 'albums');
}
/**
* Sets default values for a new album
*
* @return bool
*/
protected function setDefaults() {
global $_zp_gallery;
if (TEST_RELEASE) {
$bt = debug_backtrace();
$good = false;
foreach ($bt as $b) {
if ($b['function'] == "newAlbum") {
$good = true;
break;
}
}
if (!$good) {
zp_error(gettext('An album object was instantiated without using the newAlbum() function.'), E_USER_WARNING);
}
}
// Set default data for a new Album (title and parent_id)
$parentalbum = NULL;
$this->setPublished($_zp_gallery->getAlbumPublish());
$this->set('mtime', time());
$this->setLastChange();
$title = trim($this->name);
$this->set('title', sanitize($title, 2));
return true;
}
/**
* Returns the folder on the filesystem
*
* @since 1.6
* @return string
*/
function getName() {
return $this->name;
}
/**
* Returns the folder on the filesystem
*
* @deprecated 2.0 – Use getName() instead
*
* @return string
*/
function getFileName() {
deprecationNotice(gettext('Use getName() instead'));
return $this->getName();
}
/**
* Returns the folder on the filesystem
*
* @deprecated 2.0 – Use getName() instead
*
* @return string
*/
function getFolder() {
deprecationNotice(gettext('Use getName() instead'));
return $this->getName();
}
/**
* Returns The parent Album of this Album. NULL if this is a top-level album.
*
* @return object|null
*/
function getParent() {
if (is_null($this->parentalbum)) {
$slashpos = strrpos($this->name, "/");
if ($slashpos) {
$parent = substr($this->name, 0, $slashpos);
$parentalbum = AlbumBase::newAlbum($parent, true, true);
if ($parentalbum->exists) {
return $this->parentalbum = $parentalbum;
}
}
} else if ($this->parentalbum->exists) {
return $this->parentalbum;
}
return NULL;
}
/**
* Gets an array of parent album objects
*
* @since 1.5.5
*
* @return array|null
*/
function getParents() {
if (is_null($this->parentalbums)) {
$albumarray = getAlbumArray($this->name, false);
if (count($albumarray) == 1) {
$parent = $this->getParent();
$this->urparentalbum = $parent;
return $this->parentalbums = array($parent);
}
$parents = array();
$album = $this;
while (!is_null($album = $album->getParent())) {
array_unshift($parents, $album);
}
return $this->parentalbums = $parents;
} else {
return $this->parentalbums;
}
return $this->parentalbums = array();
}
function getParentID() {
return $this->get('parentid');
}
/**
* Returns the oldest ancestor of an album. Returns the object of the album itself if there is no urparent
*
* @since 1.6.1 Replaces getUrAlbum() to align all classes
*
* @return object
*/
function getUrParent() {
if (is_null($this->urparentalbum)) {
if (!$this->getParentID()) {
return $this->urparentalbum = $this;
}
if (is_null($this->parentalbums)) {
$albumarray = getAlbumArray($this->name, false);
if (count($albumarray) == 1) {
$urparent = $this->getParent();
$this->parentalbums = array($urparent);
return $this->urparentalbum = $urparent;
}
$urparent = AlbumBase::newAlbum($albumarray[0], true, true);
if ($urparent->exists) {
return $this->urparentalbum = $urparent;
}
} else {
return $this->urlparentalbum = $this->parentalbums[0];
}
} else {
return $this->urparentalbum;
}
}
/**
* Returns the oldest ancestor of an alubm;
*
* @deprecated 2.0 Use getUrParent() instead
* @since 1.6
*
* @return object
*/
function getUrAlbum() {
deprecationNotice(gettext('Use getUrParent() instead'));
return $this->getUrParent();
}
/**
* Returns the place data of an album
*
* @return string
*/
function getLocation($locale = NULL) {
$text = $this->get('location');
if ($locale !== 'all') {
$text = get_language_string($text, $locale);
}
$text = unTagURLs($text);
return $text;
}
/**
* Stores the album place
*
* @param string $place text for the place field
*/
function setLocation($place) {
$this->set('location', tagURLs($place));
}
/**
* Returns either the subalbum sort direction or the image sort direction of the album
*
* @param string $what 'image_' if you want the image direction,
* 'album' if you want it for the album
*
* @return string
*/
function getSortDirection($what = 'image') {
global $_zp_gallery;
if ($what == 'image') {
$direction = $this->get('image_sortdirection');
$type = $this->get('sort_type');
} else {
$direction = $this->get('album_sortdirection');
$type = $this->get('subalbum_sort_type');
}
if (empty($type)) {
// using inherited type, so use inherited direction
$parentalbum = $this->getParent();
if (is_null($parentalbum)) {
if ($what == 'image') {
$direction = IMAGE_SORT_DIRECTION;
} else {
$direction = $_zp_gallery->getSortDirection();
}
} else {
$direction = $parentalbum->getSortDirection($what);
}
}
return $direction;
}
/**
* Returns the sort type of the album images
* Will return a parent sort type if the sort type for this album is empty
*
* @return string
*/
function getSortType($what = 'image') {
global $_zp_gallery;
if ($what == 'image') {
$type = $this->get('sort_type');
} else {
$type = $this->get('subalbum_sort_type');
}
if (empty($type)) {
$parentalbum = $this->getParent();
if (is_null($parentalbum)) {
if ($what == 'image') {
$type = IMAGE_SORT_TYPE;
} else {
$type = $_zp_gallery->getSortType();
}
} else {
$type = $parentalbum->getSortType($what);
}
}
return $type;
}
/**
* sets sort directions for the album
*
* @param bool $val the direction
* @param string $what 'image_sortdirection' if you want the image direction,
* 'album_sortdirection' if you want it for the album
*/
function setSortDirection($val, $what = 'image') {
if ($what == 'image') {
$this->set('image_sortdirection', (int) ($val && true));
} else {
$this->set('album_sortdirection', (int) ($val && true));
}
}
/**
* Stores the sort type for the album
*
* @param string $sorttype the album sort type
* @param string $what 'Description'image' or 'album'
*/
function setSortType($sorttype, $what = 'image') {
if ($what == 'image') {
$this->set('sort_type', $sorttype);
} else {
$this->set('subalbum_sort_type', $sorttype);
}
}
/**
* Returns the DB key associated with the image sort type
*
* @param string $sorttype the sort type
* @return string
*/
function getImageSortKey($sorttype = null) {
if (is_null($sorttype)) {
$sorttype = $this->getSortType();
}
return lookupSortKey($sorttype, 'filename', 'images');
}
/**
* Returns the DB key associated with the subalbum sort type
*
* @param string $sorttype subalbum sort type
* @return string
*/
function getAlbumSortKey($sorttype = null) {
if (empty($sorttype)) {
$sorttype = $this->getSortType('album');
}
return lookupSortKey($sorttype, 'sort_order', 'albums');
}
/**
* Returns all folder names for all the subdirectories.
*
* @param string $page Which page of subalbums to display.
* @param string $sorttype The sort strategy
* @param string $sortdirection The direction of the sort
* @param bool $care set to false if the order does not matter
* @param bool $mine set true/false to override ownership
* @return array
*/
function getAlbums($page = 0, $sorttype = null, $sortdirection = null, $care = true, $mine = NULL) {
if ($page == 0) {
return $this->subalbums;
} else {
$albums_per_page = max(1, getOption('albums_per_page'));
return array_slice($this->subalbums, $albums_per_page * ($page - 1), $albums_per_page);
}
}
/**
* Returns the count of direct child subalbums
*
* @return int
*/
function getNumAlbums() {
return count($this->getAlbums(0, NULL, NULL, false));
}
/**
* Returns the count of all subalbums of all sublevels
* Note that dynamic albums are not counted
*
* @since 1.5.2
*/
function getNumAllAlbums() {
if (!is_null($this->num_allalbums)) {
return $this->num_allalbums;
} else {
$count = $this->getNumAlbums();
$subalbums = $this->getAlbums();
foreach ($subalbums as $folder) {
$subalbum = AlbumBase::newAlbum($folder);
if (!$subalbum->isDynamic()) {
$count += $subalbum->getNumAllAlbums();
}
}
return $count;
}
}
/**
* Returns a of a slice of the images for this album. They will
* also be sorted according to the sort type of this album, or by filename if none
* has been set.
*
* @param string $page Which page of images should be returned. If zero, all images are returned.
* @param int $firstPageCount count of images that go on the album/image transition page
* @param string $sorttype optional sort type
* @param string $sortdirection optional sort direction
* @param bool $care set to false if the order of the images does not matter
* @param bool $mine set true/false to override ownership
*
* @return array
*/
function getImages($page = 0, $firstPageCount = 0, $sorttype = null, $sortdirection = null, $care = true, $mine = NULL) {
// Return the cut of images based on $page. Page 0 means show all.
if ($page == 0) {
return $this->images;
} else {
// Only return $firstPageCount images if we are on the first page and $firstPageCount > 0
if (($page == 1) && ($firstPageCount > 0)) {
$pageStart = 0;
$images_per_page = $firstPageCount;
} else {
if ($firstPageCount > 0) {
$fetchPage = $page - 2;
} else {
$fetchPage = $page - 1;
}
$images_per_page = max(1, getOption('images_per_page'));
$pageStart = (int) ($firstPageCount + $images_per_page * $fetchPage);
}
return array_slice($this->images, $pageStart, $images_per_page);
}
}
/**
* Returns the number of images in this album (not counting its subalbums)
*
* @return int
*/
function getNumImages() {
if (is_null($this->images)) {
return count($this->getImages(0, 0, NULL, NULL, false));
}
return count($this->images);
}
/**
* Returns the number of images in this album and subalbums of all levels
* Note that dynamic albums are not counted.
*
* @since 1.5.2
*/
function getNumAllImages() {
if (!is_null($this->num_allimages)) {
return $this->num_allimages;
} else {
$count = $this->getNumImages();
$subalbums = $this->getAlbums();
foreach ($subalbums as $folder) {
$subalbum = AlbumBase::newAlbum($folder);
if (!$subalbum->isDynamic()) {
$count += $subalbum->getNumAllImages();
}
}
return $count;
}
}
/**
* Returns an image from the album based on the index passed.
*
* @param int $index
* @return int
*/
function getImage($index) {
$images = $this->getImages();
if ($index >= 0 && $index < count($images)) {
return Image::newImage($this, $this->images[$index]);
}
return false;
}
/**
* Gets the album's set thumbnail image from the database if one exists,
* otherwise, finds the first image in the album or sub-album and returns it
* as an Image object.
*
* @return Image
*/
function getAlbumThumbImage() {
global $_zp_albumthumb_selector, $_zp_gallery;
if (!is_null($this->albumthumbnail)) {
return $this->albumthumbnail;
}
$albumdir = $this->localpath;
$thumb = $this->get('thumb');
if (is_null($thumb)) {
$this->set('thumb', $thumb = getOption('AlbumThumbSelect'));
}
$i = strpos($thumb, '/');
if ($root = ($i === 0)) {
$thumb = substr($thumb, 1); // strip off the slash
$albumdir = ALBUM_FOLDER_SERVERPATH;
}
if (!empty($thumb) && !is_numeric($thumb)) {
if (file_exists($albumdir . internalToFilesystem($thumb))) {
if ($i === false) {
return Image::newImage($this, $thumb);
} else {
$pieces = explode('/', $thumb);
$i = count($pieces);
$thumb = $pieces[$i - 1];
unset($pieces[$i - 1]);
$albumdir = implode('/', $pieces);
if (!$root) {
$albumdir = $this->name . "/" . $albumdir;
} else {
$albumdir = $albumdir . "/";
}
$this->albumthumbnail = Image::newImage(AlbumBase::newAlbum($albumdir), $thumb);
return $this->albumthumbnail;
}
} else {
$this->set('thumb', $thumb = getOption('AlbumThumbSelect'));
}
}
if ($shuffle = empty($thumb)) {
$thumbs = $this->getImages(0, 0, NULL, NULL, false);
} else {
$thumbs = $this->getImages(0, 0, $_zp_albumthumb_selector[(int) $thumb]['field'], $_zp_albumthumb_selector[(int) $thumb]['direction']);
}
if (!is_null($thumbs)) {
if ($shuffle) {
shuffle($thumbs);
}
$mine = $this->isMyItem(LIST_RIGHTS);
$other = NULL;
foreach($thumbs as $thumb) {
// first check for images
$thumb = Image::newImage($this, $thumb);
if ($mine || $thumb->isPublished()) {
if ($thumb->isPhoto()) {
// legitimate image
$this->albumthumbnail = $thumb;
return $this->albumthumbnail;
} else {
if (!is_null($thumb->objectsThumb)) {
// "other" image with a thumb sidecar
$this->albumthumbnail = $thumb;
return $this->albumthumbnail;
} else {
if (is_null($other)) {
$other = $thumb;
}
}
}
}
}
if (!is_null($other)) {
// "other" image, default thumb
$this->albumthumbnail = $other;
return $this->albumthumbnail;
}
}
// Otherwise, look in sub-albums.
$subalbums = $this->getAlbums();
if (!is_null($subalbums)) {
if ($shuffle) {
shuffle($subalbums);
}
while (count($subalbums) > 0) {
$folder = array_pop($subalbums);
$subalbum = AlbumBase::newAlbum($folder);
$pwd = $subalbum->getPassword();
if (($subalbum->isPublished() && empty($pwd)) || $subalbum->isMyItem(LIST_RIGHTS)) {
$thumb = $subalbum->getAlbumThumbImage();
if (strtolower(get_class($thumb)) !== 'transientimage' && $thumb->exists) {
$this->albumthumbnail = $thumb;
return $thumb;
}
}
}
}
$nullimage = SERVERPATH . '/' . ZENFOLDER . '/images/imageDefault.png';
// check for theme imageDefault.png
$theme = '';
$uralbum = $this->getUrParent();
$albumtheme = $uralbum->getAlbumTheme();
if (!empty($albumtheme)) {
$theme = $albumtheme;
} else {
$theme = $_zp_gallery->getCurrentTheme();
}
if (!empty($theme)) {
$themeimage = SERVERPATH . '/' . THEMEFOLDER . '/' . $theme . '/images/imageDefault.png';
if (file_exists(internalToFilesystem($themeimage))) {
$nullimage = $themeimage;
}
}
$this->albumthumbnail = new transientimage($this, $nullimage);
return $this->albumthumbnail;
}
/**
* Gets the thumbnail URL for the album thumbnail image as returned by $this->getAlbumThumbImage();
* @return string
*/
function getThumb() {
$image = $this->getAlbumThumbImage();
return $image->getThumb('album');
}
/**
* Stores the thumbnail path for an album thumg
*
* @param string $filename thumbnail path
*/
function setThumb($filename) {
$this->set('thumb', $filename);
}
/**
* Returns an URL to the album, including the current page number
*
* @param string $page if not null, apppend as page #
* @param string $path Default null, optionally pass a path constant like WEBPATH or FULLWEBPATH
* @return string
*/
function getLink($page = NULL, $path = null) {
global $_zp_current_album, $_zp_page;
if (is_null($page) && $_zp_current_album && $_zp_current_album->name == $this->name) {
$page = $_zp_page;
}
$rewrite = pathurlencode($this->linkname) . '/';
$plain = '/index.php?album=' . pathurlencode($this->name);
if ($page > 1) {
$rewrite .= _PAGE_ . '/' . $page . '/';
$plain .= "&page=$page";
}
return zp_apply_filter('getLink', rewrite_path($rewrite, $plain, $path), $this, $page);
}
/**
* Delete the entire album PERMANENTLY. Be careful! This is unrecoverable.
* Returns true if successful
*
* @return bool
*/
function remove() {
global $_zp_db;
$rslt = false;
if (PersistentObject::remove()) {
$_zp_db->query("DELETE FROM " . $_zp_db->prefix('options') . "WHERE `ownerid`=" . $this->id);
$_zp_db->query("DELETE FROM " . $_zp_db->prefix('comments') . "WHERE `type`='albums' AND `ownerid`=" . $this->id);
$_zp_db->query("DELETE FROM " . $_zp_db->prefix('obj_to_tag') . "WHERE `type`='albums' AND `objectid`=" . $this->id);
$rslt = true;
$filestoremove = safe_glob(substr($this->localpath, 0, -1) . '.*');
foreach ($filestoremove as $file) {
if (in_array(strtolower(getSuffix($file)), $this->sidecars)) {
@chmod($file, 0777);
unlink($file);
}
}
$this->setUpdatedDateParents();
}
return $rslt;
}
/**
* common album move code
* @param type $newfolder
* @return int
*/
protected function _move($newfolder) {
// First, ensure the new base directory exists.
$dest = ALBUM_FOLDER_SERVERPATH . internalToFilesystem($newfolder);
// Check to see if the destination already exists
if (file_exists($dest)) {
// Disallow moving an album over an existing one.
if (!(CASE_INSENSITIVE && strtolower($dest) == strtolower(rtrim($this->localpath, '/')))) {
return 3;
}
}
if(!$this->isValidMoveCopyDestination($newfolder)) {
// Disallow moving to a subfolder of the current folder.
return 4;
}
$filemask = substr($this->localpath, 0, -1) . '.*';
$perms = FOLDER_MOD;
@chmod($this->localpath, 0777);
$success = @rename(rtrim($this->localpath, '/'), $dest);
@chmod($dest, $perms);
if ($success) {
$this->localpath = $dest . "/";
$filestomove = safe_glob($filemask);
foreach ($filestomove as $file) {
if (in_array(strtolower(getSuffix($file)), $this->sidecars)) {
$d = stripslashes($dest) . '.' . getSuffix($file);
@chmod($file, 0777);
$success = $success && @rename($file, $d);
@chmod($d, FILE_MOD);
}
}
clearstatcache();
$success = self::move($newfolder);
if ($success) {
// must be first as updates below changes the object and paths
$this->moveCacheFolder($newfolder);
// Update old parent(s) that "lost" an album!
$this->setUpdatedDateParents();
$this->save();
$this->updateParent($newfolder);
return 0;
}
}
return 1;
}
/**
* Move this album to the location specified by $newfolder, copying all
* metadata, subalbums, and subalbums' metadata with it.
* @param $newfolder string the folder to move to, including the name of the current folder (possibly renamed).
* @return int 0 on success and error indicator on failure.
*
*/
function move($newfolder) {
return parent::move(array('folder' => $newfolder));
}
/**
* Rename this album folder. Alias for move($newfoldername);
* @param string $newfolder the new folder name of this album (including subalbum paths)
* @return boolean true on success or false on failure.
*/
function rename($newfolder) {
return $this->move($newfolder);
}
protected function succeed($dest) {
return false;
}
/**
* Copy this album to the location specified by $newfolder, copying all
* metadata, subalbums, and subalbums' metadata with it.
* @param $newfolder string the folder to copy to, including the name of the current folder (possibly renamed).
* @return int 0 on success and error indicator on failure.
*
*/
function copy($newfolder) {
// album name to destination folder
if (substr($newfolder, -1, 1) != '/') {
$newfolder .= '/';
}
$newfolder .= basename($this->localpath);
// First, ensure the new base directory exists.
$dest = ALBUM_FOLDER_SERVERPATH . internalToFilesystem($newfolder);
// Check to see if the destination directory already exists
if (file_exists($dest)) {
// Disallow moving an album over an existing one.
return 3;
}
if(!$this->isValidMoveCopyDestination($newfolder)) {
// Disallow copying to a subfolder of the current folder (infinite loop).
return 4;
}
$success = $this->succeed($dest);
$filemask = substr($this->localpath, 0, -1) . '.*';
if ($success) {
// replicate the album metadata and sub-files
$uniqueset = array('folder' => $newfolder);
$parentname = dirname($newfolder);
if (empty($parentname) || $parentname == '/' || $parentname == '.') {
$uniqueset['parentid'] = NULL;
} else {
$parent = AlbumBase::newAlbum($parentname);
$uniqueset['parentid'] = $parent->getID();
}
$newID = parent::copy($uniqueset);
if ($newID) {
//replicate the tags
storeTags(readTags($this->getID(), 'albums'), $newID, 'albums');
//copy the sidecar files
$filestocopy = safe_glob($filemask);
foreach ($filestocopy as $file) {
if (in_array(strtolower(getSuffix($file)), $this->sidecars)) {
$success = $success && @copy($file, dirname($dest) . '/' . basename($file));
}
}
}
}
if ($success) {
$newalbum = AlbumBase::newAlbum($newfolder);
$newalbum->setUpdatedDate();
$newalbum->setUpdatedDateParents();
$this->copyCacheFolder($newfolder);
return 0;
} else {
return 1;
}
}
/**
* Checks is the destination is not a subfolder of the current folder itself
*
* @since 1.5.5
*
* @param string $destination album name to move or copy to
* @return boolean
*/
function isValidMoveCopyDestination($destination) {
$oldfolders = explode('/', $this->name);
$newfolders = explode('/', $destination);
$sub = count($newfolders) > count($oldfolders);
if ($sub) {
for ($i = 0; $i < count($oldfolders); $i++) {
if ($newfolders[$i] != $oldfolders[$i]) {
$sub = false;
break;
}
}
if ($sub) {
// Disallow moving to a subfolder of the current folder.
return false;
}
}
return true;
}
/**
* Gets the SERVERPATH to the cache folder of the album with trailing slash
* Note that his does not check for existance!
*
* @since 1.6.1
*
* @return string
*/
function getCacheFolder() {
return SERVERCACHE . '/' . pathurlencode($this->name) . '/';
}
/**
* Copies the cache folder of the album
*
* @since 1.6.1
*
* @param string $newfolder New folder path name
* @return bool
*/
function copyCacheFolder($newfolder) {
if (file_exists($this->getCacheFolder())) {
$foldercopy = SERVERCACHE . '/' . $newfolder . '/';
if (!file_exists($foldercopy)) {
return @copy($this->getCacheFolder(), $foldercopy);
}
}
return false;
}
/**
* Moves the cache folder of the album
*
* @since 1.6.1
*
* @param string $newfolder New folder path name
* @return bool
*/
function moveCacheFolder($newfolder) {
if (file_exists($this->getCacheFolder())) {
$movedfolder = SERVERCACHE . '/' . $newfolder . '/';
if (!file_exists($movedfolder)) {
return @rename($this->getCacheFolder(), $movedfolder);
}
}
return false;
}
/**
* Renames the cache folder of the album
* Alias of moveCacheFolder();
*
* @since 1.6.1
*
* @param string $newfolder New folder path name
* @return bool
*/
function renameCacheFolder($newfolder) {
return $this->moveCacheFolder($newfolder);
}
/**
* Removes the cache folder of the album including all contents
*
* @since 1.6.1
*/
function removeCacheFolder() {
removeDir($this->getCacheFolder(), true);
}
/**
* Removes cache image files from this album's folder but not any subalbums or their cache files
*
* @since 1.6.1
*/
function clearCacheFolder() {
chdir($this->getCacheFolder());
// Try tot clear the cache folder of subfolders and files
$filelist = safe_glob('*');
foreach ($filelist as $file) {
if (is_file($file)) {
@chmod($file, 0777);
@unlink($file);
}
}
}
/**
* For every image in the album, look for its file. Delete from the database
* if the file does not exist. Same for each sub-directory/album.
*
* @param bool $deep set to true for a thorough cleansing
*/
function garbageCollect($deep = false) {
}
/**
* Load all of the filenames that are found in this Albums directory on disk.
* Returns an array with all the names.
*
* @param $dirs Whether or not to return directories ONLY with the file array.
* @return array
*/
protected function loadFileNames($dirs = false) {
}
/**
* Returns true if the album is "dynamic"
*
* @return bool
*/
function isDynamic() {
return false;
}
/**
* Returns the search parameters for a dynamic album
*
* @return string
*/
function getSearchParams() {
return NULL;
}
/**
* Sets the search parameters of a dynamic album
*
* @param string $params The search string to produce the dynamic album
*/
function setSearchParams($params) {
}
/**
* Returns the search engine for a dynamic album