/
functions.php
3670 lines (3265 loc) · 82.1 KB
/
functions.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
// +-----------------------------------------------------------------------+
// | This file is part of Piwigo. |
// | |
// | For copyright and license information, please view the COPYING.txt |
// | file that was distributed with this source code. |
// +-----------------------------------------------------------------------+
/**
* @package functions\admin\___
*/
include_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
/**
* Deletes a site and call delete_categories for each primary category of the site
*
* @param int $id
*/
function delete_site($id)
{
// destruction of the categories of the site
$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
WHERE site_id = '.$id.'
;';
$category_ids = query2array($query, null, 'id');
delete_categories($category_ids);
// destruction of the site
$query = '
DELETE FROM '.SITES_TABLE.'
WHERE id = '.$id.'
;';
pwg_query($query);
}
/**
* Recursively deletes one or more categories.
* It also deletes :
* - all the elements physically linked to the category (with delete_elements)
* - all the links between elements and this category
* - all the restrictions linked to the category
*
* @param int[] $ids
* @param string $photo_deletion_mode
* - no_delete : delete no photo, may create orphans
* - delete_orphans : delete photos that are no longer linked to any category
* - force_delete : delete photos even if they are linked to another category
*/
function delete_categories($ids, $photo_deletion_mode='no_delete')
{
if (count($ids) == 0)
{
return;
}
// add sub-category ids to the given ids : if a category is deleted, all
// sub-categories must be so
$ids = get_subcat_ids($ids);
// destruction of all photos physically linked to the category
$query = '
SELECT id
FROM '.IMAGES_TABLE.'
WHERE storage_category_id IN (
'.wordwrap(implode(', ', $ids), 80, "\n").')
;';
$element_ids = query2array($query, null, 'id');
delete_elements($element_ids);
// now, should we delete photos that are virtually linked to the category?
if ('delete_orphans' == $photo_deletion_mode or 'force_delete' == $photo_deletion_mode)
{
$query = '
SELECT
DISTINCT(image_id)
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id IN ('.implode(',', $ids).')
;';
$image_ids_linked = query2array($query, null, 'image_id');
if (count($image_ids_linked) > 0)
{
if ('delete_orphans' == $photo_deletion_mode)
{
$query = '
SELECT
DISTINCT(image_id)
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE image_id IN ('.implode(',', $image_ids_linked).')
AND category_id NOT IN ('.implode(',', $ids).')
;';
$image_ids_not_orphans = query2array($query, null, 'image_id');
$image_ids_to_delete = array_diff($image_ids_linked, $image_ids_not_orphans);
}
if ('force_delete' == $photo_deletion_mode)
{
$image_ids_to_delete = $image_ids_linked;
}
delete_elements($image_ids_to_delete, true);
}
}
// destruction of the links between images and this category
$query = '
DELETE FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id IN (
'.wordwrap(implode(', ', $ids), 80, "\n").')
;';
pwg_query($query);
// destruction of the access linked to the category
$query = '
DELETE FROM '.USER_ACCESS_TABLE.'
WHERE cat_id IN (
'.wordwrap(implode(', ', $ids), 80, "\n").')
;';
pwg_query($query);
$query = '
DELETE FROM '.GROUP_ACCESS_TABLE.'
WHERE cat_id IN (
'.wordwrap(implode(', ', $ids), 80, "\n").')
;';
pwg_query($query);
// destruction of the category
$query = '
DELETE FROM '.CATEGORIES_TABLE.'
WHERE id IN (
'.wordwrap(implode(', ', $ids), 80, "\n").')
;';
pwg_query($query);
$query='
DELETE FROM '.OLD_PERMALINKS_TABLE.'
WHERE cat_id IN ('.implode(',',$ids).')';
pwg_query($query);
$query='
DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.'
WHERE cat_id IN ('.implode(',',$ids).')';
pwg_query($query);
trigger_notify('delete_categories', $ids);
pwg_activity('album', $ids, 'delete', array('photo_deletion_mode'=>$photo_deletion_mode));
}
/**
* Deletes all files (on disk) related to given image ids.
*
* @param int[] $ids
* @return 0|int[] image ids where files were successfully deleted
*/
function delete_element_files($ids)
{
global $conf;
if (count($ids) == 0)
{
return 0;
}
$new_ids = array();
$formats_of = array();
$query = '
SELECT
image_id,
ext
FROM '.IMAGE_FORMAT_TABLE.'
WHERE image_id IN ('.implode(',', $ids).')
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if (!isset($formats_of[ $row['image_id'] ]))
{
$formats_of[ $row['image_id'] ] = array();
}
$formats_of[ $row['image_id'] ][] = $row['ext'];
}
$query = '
SELECT
id,
path,
representative_ext
FROM '.IMAGES_TABLE.'
WHERE id IN ('.implode(',', $ids).')
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if (url_is_remote($row['path']))
{
continue;
}
$files = array();
$files[] = get_element_path($row);
if (!empty($row['representative_ext']))
{
$files[] = original_to_representative( $files[0], $row['representative_ext']);
}
if (isset($formats_of[ $row['id'] ]))
{
foreach ($formats_of[ $row['id'] ] as $format_ext)
{
$files[] = original_to_format($files[0], $format_ext);
}
}
$ok = true;
if (!isset($conf['never_delete_originals']))
{
foreach ($files as $path)
{
if (is_file($path) and !unlink($path))
{
$ok = false;
trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
break;
}
}
}
if ($ok)
{
delete_element_derivatives($row);
$new_ids[] = $row['id'];
}
else
{
break;
}
}
return $new_ids;
}
/**
* Deletes elements from database.
* It also deletes :
* - all the comments related to elements
* - all the links between categories/tags and elements
* - all the favorites/rates associated to elements
* - removes elements from caddie
*
* @param int[] $ids
* @param bool $physical_deletion
* @return int number of deleted elements
*/
function delete_elements($ids, $physical_deletion=false)
{
if (count($ids) == 0)
{
return 0;
}
trigger_notify('begin_delete_elements', $ids);
if ($physical_deletion)
{
$ids = delete_element_files($ids);
if (count($ids)==0)
{
return 0;
}
}
$ids_str = wordwrap(implode(', ', $ids), 80, "\n");
// destruction of the comments on the image
$query = '
DELETE FROM '.COMMENTS_TABLE.'
WHERE image_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the links between images and categories
$query = '
DELETE FROM '.IMAGE_CATEGORY_TABLE.'
WHERE image_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the formats
$query = '
DELETE FROM '.IMAGE_FORMAT_TABLE.'
WHERE image_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the links between images and tags
$query = '
DELETE FROM '.IMAGE_TAG_TABLE.'
WHERE image_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the favorites associated with the picture
$query = '
DELETE FROM '.FAVORITES_TABLE.'
WHERE image_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the rates associated to this element
$query = '
DELETE FROM '.RATE_TABLE.'
WHERE element_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the caddie associated to this element
$query = '
DELETE FROM '.CADDIE_TABLE.'
WHERE element_id IN ('. $ids_str .')
;';
pwg_query($query);
// destruction of the image
$query = '
DELETE FROM '.IMAGES_TABLE.'
WHERE id IN ('. $ids_str .')
;';
pwg_query($query);
// are the photo used as category representant?
$query = '
SELECT
id
FROM '.CATEGORIES_TABLE.'
WHERE representative_picture_id IN ('. $ids_str .')
;';
$category_ids = query2array($query, null, 'id');
if (count($category_ids) > 0)
{
update_category($category_ids);
}
trigger_notify('delete_elements', $ids);
pwg_activity('photo', $ids, 'delete');
return count($ids);
}
/**
* Deletes an user.
* It also deletes all related data (accesses, favorites, permissions, etc.)
* @todo : accept array input
*
* @param int $user_id
*/
function delete_user($user_id)
{
global $conf;
$tables = array(
// destruction of the access linked to the user
USER_ACCESS_TABLE,
// destruction of data notification by mail for this user
USER_MAIL_NOTIFICATION_TABLE,
// destruction of data RSS notification for this user
USER_FEED_TABLE,
// deletion of calculated permissions linked to the user
USER_CACHE_TABLE,
// deletion of computed cache data linked to the user
USER_CACHE_CATEGORIES_TABLE,
// destruction of the group links for this user
USER_GROUP_TABLE,
// destruction of the favorites associated with the user
FAVORITES_TABLE,
// destruction of the caddie associated with the user
CADDIE_TABLE,
// deletion of piwigo specific informations
USER_INFOS_TABLE,
USER_AUTH_KEYS_TABLE
);
foreach ($tables as $table)
{
$query = '
DELETE FROM '.$table.'
WHERE user_id = '.$user_id.'
;';
pwg_query($query);
}
// purge of sessions
delete_user_sessions($user_id);
// destruction of the user
$query = '
DELETE FROM '.USERS_TABLE.'
WHERE '.$conf['user_fields']['id'].' = '.$user_id.'
;';
pwg_query($query);
trigger_notify('delete_user', $user_id);
pwg_activity('user', $user_id, 'delete');
}
/**
* Deletes all tags linked to no photo
*/
function delete_orphan_tags()
{
$orphan_tags = get_orphan_tags();
if (count($orphan_tags) > 0)
{
$orphan_tag_ids = array();
foreach ($orphan_tags as $tag)
{
$orphan_tag_ids[] = $tag['id'];
}
delete_tags($orphan_tag_ids);
}
}
/**
* Get all tags (id + name) linked to no photo
*/
function get_orphan_tags()
{
$query = '
SELECT
id,
name
FROM '.TAGS_TABLE.'
LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = tag_id
WHERE tag_id IS NULL
AND lastmodified < SUBDATE(NOW(), INTERVAL 1 DAY)
;';
return query2array($query);
}
/**
* Verifies that the representative picture really exists in the db and
* picks up a random representative if possible and based on config.
*
* @param 'all'|int|int[] $ids
*/
function update_category($ids = 'all')
{
global $conf;
if ($ids=='all')
{
$where_cats = '1=1';
}
elseif ( !is_array($ids) )
{
$where_cats = '%s='.$ids;
}
else
{
if (count($ids) == 0)
{
return false;
}
$where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')';
}
// find all categories where the setted representative is not possible :
// the picture does not exist
$query = '
SELECT DISTINCT c.id
FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i
ON c.representative_picture_id = i.id
WHERE representative_picture_id IS NOT NULL
AND '.sprintf($where_cats, 'c.id').'
AND i.id IS NULL
;';
$wrong_representant = query2array($query, null, 'id');
if (count($wrong_representant) > 0)
{
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET representative_picture_id = NULL
WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 120, "\n").')
;';
pwg_query($query);
}
if (!$conf['allow_random_representative'])
{
// If the random representant is not allowed, we need to find
// categories with elements and with no representant. Those categories
// must be added to the list of categories to set to a random
// representant.
$query = '
SELECT DISTINCT id
FROM '.CATEGORIES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.'
ON id = category_id
WHERE representative_picture_id IS NULL
AND '.sprintf($where_cats, 'category_id').'
;';
$to_rand = query2array($query, null, 'id');
if (count($to_rand) > 0)
{
set_random_representant($to_rand);
}
}
}
/**
* Checks and repairs IMAGE_CATEGORY_TABLE integrity.
* Removes all entries from the table which correspond to a deleted image.
*/
function images_integrity()
{
$query = '
SELECT
image_id
FROM '.IMAGE_CATEGORY_TABLE.'
LEFT JOIN '.IMAGES_TABLE.' ON id = image_id
WHERE id IS NULL
;';
$orphan_image_ids = query2array($query, null, 'image_id');
if (count($orphan_image_ids) > 0)
{
$query = '
DELETE
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE image_id IN ('.implode(',', $orphan_image_ids).')
;';
pwg_query($query);
}
}
/**
* Checks and repairs integrity on categories.
* Removes all entries from related tables which correspond to a deleted category.
*/
function categories_integrity()
{
$related_columns = array(
IMAGE_CATEGORY_TABLE.'.category_id',
USER_ACCESS_TABLE.'.cat_id',
GROUP_ACCESS_TABLE.'.cat_id',
OLD_PERMALINKS_TABLE.'.cat_id',
USER_CACHE_CATEGORIES_TABLE.'.cat_id',
);
foreach ($related_columns as $fullcol)
{
list($table, $column) = explode('.', $fullcol);
$query = '
SELECT
'.$column.'
FROM '.$table.'
LEFT JOIN '.CATEGORIES_TABLE.' ON id = '.$column.'
WHERE id IS NULL
;';
$orphans = array_unique(query2array($query, null, $column));
if (count($orphans) > 0)
{
$query = '
DELETE
FROM '.$table.'
WHERE '.$column.' IN ('.implode(',', $orphans).')
;';
pwg_query($query);
}
}
}
/**
* Returns an array containing sub-directories which are potentially
* a category.
* Directories named ".svn", "thumbnail", "pwg_high" or "pwg_representative"
* are omitted.
*
* @param string $basedir (eg: ./galleries)
* @return string[]
*/
function get_fs_directories($path, $recursive = true)
{
global $conf;
$dirs = array();
$path = rtrim($path, '/');
$exclude_folders = array_merge(
$conf['sync_exclude_folders'],
array(
'.', '..', '.svn',
'thumbnail', 'pwg_high',
'pwg_representative',
'pwg_format',
)
);
$exclude_folders = array_flip($exclude_folders);
if (is_dir($path))
{
if ($contents = opendir($path))
{
while (($node = readdir($contents)) !== false)
{
if (is_dir($path.'/'.$node) and !isset($exclude_folders[$node]))
{
$dirs[] = $path.'/'.$node;
if ($recursive)
{
$dirs = array_merge($dirs, get_fs_directories($path.'/'.$node));
}
}
}
closedir($contents);
}
}
return $dirs;
}
/**
* save the rank depending on given categories order
*
* The list of ordered categories id is supposed to be in the same parent
* category
*
* @param array categories
* @return void
*/
function save_categories_order($categories)
{
$current_rank_for_id_uppercat = array();
$current_rank = 0;
$datas = array();
foreach ($categories as $category)
{
if (is_array($category))
{
$id = $category['id'];
$id_uppercat = $category['id_uppercat'];
if (!isset($current_rank_for_id_uppercat[$id_uppercat]))
{
$current_rank_for_id_uppercat[$id_uppercat] = 0;
}
$current_rank = ++$current_rank_for_id_uppercat[$id_uppercat];
}
else
{
$id = $category;
$current_rank++;
}
$datas[] = array('id' => $id, 'rank' => $current_rank);
}
$fields = array('primary' => array('id'), 'update' => array('rank'));
mass_updates(CATEGORIES_TABLE, $fields, $datas);
update_global_rank();
}
/**
* Orders categories (update categories.rank and global_rank database fields)
* so that rank field are consecutive integers starting at 1 for each child.
*/
function update_global_rank()
{
$query = '
SELECT id, id_uppercat, uppercats, `rank`, global_rank
FROM '.CATEGORIES_TABLE.'
ORDER BY id_uppercat, `rank`, name';
global $cat_map; // used in preg_replace callback
$cat_map = array();
$current_rank = 0;
$current_uppercat = '';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if ($row['id_uppercat'] != $current_uppercat)
{
$current_rank = 0;
$current_uppercat = $row['id_uppercat'];
}
++$current_rank;
$cat =
array(
'rank' => $current_rank,
'rank_changed' =>$current_rank!=$row['rank'],
'global_rank' => $row['global_rank'],
'uppercats' => $row['uppercats'],
);
$cat_map[ $row['id'] ] = $cat;
}
$datas = array();
$cat_map_callback = function($m) use ($cat_map) { return $cat_map[$m[1]]["rank"]; };
foreach( $cat_map as $id=>$cat )
{
$new_global_rank = preg_replace_callback(
'/(\d+)/',
$cat_map_callback,
str_replace(',', '.', $cat['uppercats'] )
);
if ($cat['rank_changed'] or $new_global_rank !== $cat['global_rank'])
{
$datas[] = array(
'id' => $id,
'rank' => $cat['rank'],
'global_rank' => $new_global_rank,
);
}
}
unset($cat_map);
mass_updates(
CATEGORIES_TABLE,
array(
'primary' => array('id'),
'update' => array('rank', 'global_rank')
),
$datas
);
return count($datas);
}
/**
* Change the **visible** property on a set of categories.
*
* @param int[] $categories
* @param boolean|string $value
* @param boolean $unlock_child optional default false
*/
function set_cat_visible($categories, $value, $unlock_child = false)
{
if ( ($value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) === null )
{
trigger_error("set_cat_visible invalid param $value", E_USER_WARNING);
return false;
}
// unlocking a category => all its parent categories become unlocked
if ($value)
{
$cats = get_uppercat_ids($categories);
if ($unlock_child) {
$cats = array_merge($cats, get_subcat_ids($categories));
}
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET visible = \'true\'
WHERE id IN ('.implode(',', $cats).')';
pwg_query($query);
}
// locking a category => all its child categories become locked
else
{
$subcats = get_subcat_ids($categories);
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET visible = \'false\'
WHERE id IN ('.implode(',', $subcats).')';
pwg_query($query);
}
}
/**
* Change the **status** property on a set of categories : private or public.
*
* @param int[] $categories
* @param string $value
*/
function set_cat_status($categories, $value)
{
if (!in_array($value, array('public', 'private')))
{
trigger_error("set_cat_status invalid param $value", E_USER_WARNING);
return false;
}
// make public a category => all its parent categories become public
if ($value == 'public')
{
$uppercats = get_uppercat_ids($categories);
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET status = \'public\'
WHERE id IN ('.implode(',', $uppercats).')
;';
pwg_query($query);
}
// make a category private => all its child categories become private
if ($value == 'private')
{
$subcats = get_subcat_ids($categories);
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET status = \'private\'
WHERE id IN ('.implode(',', $subcats).')';
pwg_query($query);
// We have to keep permissions consistant: a sub-album can't be
// permitted to a user or group if its parent album is not permitted to
// the same user or group. Let's remove all permissions on sub-albums if
// it is not consistant. Let's take the following example:
//
// A1 permitted to U1,G1
// A1/A2 permitted to U1,U2,G1,G2
// A1/A2/A3 permitted to U3,G1
// A1/A2/A4 permitted to U2
// A1/A5 permitted to U4
// A6 permitted to U4
// A6/A7 permitted to G1
//
// (we consider that it can be possible to start with inconsistant
// permission, given that public albums can have hidden permissions,
// revealed once the album returns to private status)
//
// The admin selects A2,A3,A4,A5,A6,A7 to become private (all but A1,
// which is private, which can be true if we're moving A2 into A1). The
// result must be:
//
// A2 permission removed to U2,G2
// A3 permission removed to U3
// A4 permission removed to U2
// A5 permission removed to U2
// A6 permission removed to U4
// A7 no permission removed
//
// 1) we must extract "top albums": A2, A5 and A6
// 2) for each top album, decide which album is the reference for permissions
// 3) remove all inconsistant permissions from sub-albums of each top-album
// step 1, search top albums
$top_categories = array();
$parent_ids = array();
$query = '
SELECT
id,
name,
id_uppercat,
uppercats,
global_rank
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', $categories).')
;';
$all_categories = query2array($query);
usort($all_categories, 'global_rank_compare');
foreach ($all_categories as $cat)
{
$is_top = true;
if (!empty($cat['id_uppercat']))
{
foreach (explode(',', $cat['uppercats']) as $id_uppercat)
{
if (isset($top_categories[$id_uppercat]))
{
$is_top = false;
break;
}
}
}
if ($is_top)
{
$top_categories[$cat['id']] = $cat;
if (!empty($cat['id_uppercat']))
{
$parent_ids[] = $cat['id_uppercat'];
}
}
}
// step 2, search the reference album for permissions
//
// to find the reference of each top album, we will need the parent albums
$parent_cats = array();
if (count($parent_ids) > 0)
{
$query = '
SELECT
id,
status
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', $parent_ids).')
;';
$parent_cats= query2array($query, 'id');
}
$tables = array(
USER_ACCESS_TABLE => 'user_id',
GROUP_ACCESS_TABLE => 'group_id'
);
foreach ($top_categories as $top_category)
{
// what is the "reference" for list of permissions? The parent album
// if it is private, else the album itself
$ref_cat_id = $top_category['id'];
if (!empty($top_category['id_uppercat'])
and isset($parent_cats[ $top_category['id_uppercat'] ])
and 'private' == $parent_cats[ $top_category['id_uppercat'] ]['status'])
{
$ref_cat_id = $top_category['id_uppercat'];
}
$subcats = get_subcat_ids(array($top_category['id']));
foreach ($tables as $table => $field)
{
// what are the permissions user/group of the reference album
$query = '
SELECT '.$field.'
FROM '.$table.'
WHERE cat_id = '.$ref_cat_id.'
;';
$ref_access = query2array($query, null, $field);
if (count($ref_access) == 0)
{
$ref_access[] = -1;
}
// step 3, remove the inconsistant permissions from sub-albums
$query = '
DELETE
FROM '.$table.'
WHERE '.$field.' NOT IN ('.implode(',', $ref_access).')
AND cat_id IN ('.implode(',', $subcats).')
;';
pwg_query($query);
}
}
}
}
/**
* Returns all uppercats category ids of the given category ids.
*
* @param int[] $cat_ids
* @return int[]
*/
function get_uppercat_ids($cat_ids)
{
if (!is_array($cat_ids) or count($cat_ids) < 1)
{
return array();
}
$uppercats = array();
$query = '
SELECT uppercats
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', $cat_ids).')
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
$uppercats = array_merge($uppercats,
explode(',', $row['uppercats']));
}
$uppercats = array_unique($uppercats);
return $uppercats;
}
/**
*/
function get_category_representant_properties($image_id, $size = NULL)
{
$query = '
SELECT id,representative_ext,path
FROM '.IMAGES_TABLE.'
WHERE id = '.$image_id.'
;';
$row = pwg_db_fetch_assoc(pwg_query($query));