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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
|
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
/**
* @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);
}
/**
* 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();
$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']);
}
$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 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);
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,
);
foreach ($tables as $table)
{
$query = '
DELETE FROM '.$table.'
WHERE user_id = '.$user_id.'
;';
pwg_query($query);
}
// purge of sessions
$query = '
DELETE FROM '.SESSIONS_TABLE.'
WHERE data LIKE \'pwg_uid|i:'.(int)$user_id.';%\'
;';
pwg_query($query);
// 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);
}
/**
* 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
;';
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
;';
$result = pwg_query($query);
$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);
}
}
/**
* 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',
)
);
$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;
}
/**
* 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 = create_function('$m', 'global $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
*/
function set_cat_visible($categories, $value)
{
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)
{
$uppercats = get_uppercat_ids($categories);
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET visible = \'true\'
WHERE id IN ('.implode(',', $uppercats).')';
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;
}
/**
* Set a new random representant to the categories.
*
* @param int[] $categories
*/
function set_random_representant($categories)
{
$datas = array();
foreach ($categories as $category_id)
{
$query = '
SELECT image_id
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id = '.$category_id.'
ORDER BY '.DB_RANDOM_FUNCTION.'()
LIMIT 1
;';
list($representative) = pwg_db_fetch_row(pwg_query($query));
$datas[] = array(
'id' => $category_id,
'representative_picture_id' => $representative,
);
}
mass_updates(
CATEGORIES_TABLE,
array(
'primary' => array('id'),
'update' => array('representative_picture_id')
),
$datas
);
}
/**
* Returns the fulldir for each given category id.
*
* @param int[] intcat_ids
* @return string[]
*/
function get_fulldirs($cat_ids)
{
if (count($cat_ids) == 0)
{
return array();
}
// caching directories of existing categories
global $cat_dirs; // used in preg_replace callback
$query = '
SELECT id, dir
FROM '.CATEGORIES_TABLE.'
WHERE dir IS NOT NULL
;';
$cat_dirs = query2array($query, 'id', 'dir');
// caching galleries_url
$query = '
SELECT id, galleries_url
FROM '.SITES_TABLE.'
;';
$galleries_url = query2array($query, 'id', 'galleries_url');
// categories : id, site_id, uppercats
$query = '
SELECT id, uppercats, site_id
FROM '.CATEGORIES_TABLE.'
WHERE dir IS NOT NULL
AND id IN (
'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
;';
$categories = query2array($query);
// filling $cat_fulldirs
$cat_dirs_callback = create_function('$m', 'global $cat_dirs; return $cat_dirs[$m[1]];');
$cat_fulldirs = array();
foreach ($categories as $category)
{
$uppercats = str_replace(',', '/', $category['uppercats']);
$cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']];
$cat_fulldirs[$category['id']].= preg_replace_callback(
'/(\d+)/',
$cat_dirs_callback,
$uppercats
);
}
unset($cat_dirs);
return $cat_fulldirs;
}
/**
* Returns an array with all file system files according to $conf['file_ext']
*
* @param string $path
* @param bool $recursive
* @return array
*/
function get_fs($path, $recursive = true)
{
global $conf;
// because isset is faster than in_array...
if (!isset($conf['flip_picture_ext']))
{
$conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
}
if (!isset($conf['flip_file_ext']))
{
$conf['flip_file_ext'] = array_flip($conf['file_ext']);
}
$fs['elements'] = array();
$fs['thumbnails'] = array();
$fs['representatives'] = array();
$subdirs = array();
if (is_dir($path))
{
if ($contents = opendir($path))
{
while (($node = readdir($contents)) !== false)
{
if ($node == '.' or $node == '..') continue;
if (is_file($path.'/'.$node))
{
$extension = get_extension($node);
if (isset($conf['flip_picture_ext'][$extension]))
{
if (basename($path) == 'thumbnail')
{
$fs['thumbnails'][] = $path.'/'.$node;
}
elseif (basename($path) == 'pwg_representative')
{
$fs['representatives'][] = $path.'/'.$node;
}
else
{
$fs['elements'][] = $path.'/'.$node;
}
}
elseif (isset($conf['flip_file_ext'][$extension]))
{
$fs['elements'][] = $path.'/'.$node;
}
}
elseif (is_dir($path.'/'.$node) and $node != 'pwg_high' and $recursive)
{
$subdirs[] = $node;
}
}
}
closedir($contents);
foreach ($subdirs as $subdir)
{
$tmp_fs = get_fs($path.'/'.$subdir);
$fs['elements'] = array_merge($fs['elements'],
$tmp_fs['elements']);
$fs['thumbnails'] = array_merge($fs['thumbnails'],
$tmp_fs['thumbnails']);
$fs['representatives'] = array_merge($fs['representatives'],
$tmp_fs['representatives']);
}
}
return $fs;
}
/**
* Synchronize base users list and related users list.
*
* Compares and synchronizes base users table (USERS_TABLE) with its child
* tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each
* base user must be present in child tables, users in child tables not
* present in base table must be deleted.
*/
function sync_users()
{
global $conf;
$query = '
SELECT '.$conf['user_fields']['id'].' AS id
FROM '.USERS_TABLE.'
;';
$base_users = query2array($query, null, 'id');
$query = '
SELECT user_id
FROM '.USER_INFOS_TABLE.'
;';
$infos_users = query2array($query, null, 'user_id');
// users present in $base_users and not in $infos_users must be added
$to_create = array_diff($base_users, $infos_users);
if (count($to_create) > 0)
{
create_user_infos($to_create);
}
// users present in user related tables must be present in the base user
// table
$tables = array(
USER_MAIL_NOTIFICATION_TABLE,
USER_FEED_TABLE,
USER_INFOS_TABLE,
USER_ACCESS_TABLE,
USER_CACHE_TABLE,
USER_CACHE_CATEGORIES_TABLE,
USER_GROUP_TABLE
);
foreach ($tables as $table)
{
$query = '
SELECT DISTINCT user_id
FROM '.$table.'
;';
$to_delete = array_diff(
query2array($query, null, 'user_id'),
$base_users
);
if (count($to_delete) > 0)
{
$query = '
DELETE
FROM '.$table.'
WHERE user_id in ('.implode(',', $to_delete).')
;';
pwg_query($query);
}
}
}
/**
* Updates categories.uppercats field based on categories.id + categories.id_uppercat
*/
function update_uppercats()
{
$query = '
SELECT id, id_uppercat, uppercats
FROM '.CATEGORIES_TABLE.'
;';
$cat_map = query2array($query, 'id');
$datas = array();
foreach ($cat_map as $id => $cat)
{
$upper_list = array();
$uppercat = $id;
while ($uppercat)
{
$upper_list[] = $uppercat;
$uppercat = $cat_map[$uppercat]['id_uppercat'];
}
$new_uppercats = implode(',', array_reverse($upper_list));
if ($new_uppercats != $cat['uppercats'])
{
$datas[] = array(
'id' => $id,
'uppercats' => $new_uppercats
);
}
}
$fields = array('primary' => array('id'), 'update' => array('uppercats'));
mass_updates(CATEGORIES_TABLE, $fields, $datas);
}
/**
* Update images.path field base on images.file and storage categories fulldirs.
*/
function update_path()
{
$query = '
SELECT DISTINCT(storage_category_id)
FROM '.IMAGES_TABLE.'
WHERE storage_category_id IS NOT NULL
;';
$cat_ids = query2aray($query, null, 'storage_category_id');
$fulldirs = get_fulldirs($cat_ids);
foreach ($cat_ids as $cat_id)
{
$query = '
UPDATE '.IMAGES_TABLE.'
SET path = '.pwg_db_concat(array("'".$fulldirs[$cat_id]."/'",'file')).'
WHERE storage_category_id = '.$cat_id.'
;';
pwg_query($query);
}
}
/**
* Change the parent category of the given categories. The categories are
* supposed virtual.
*
* @param int[] $category_ids
* @param int $new_parent (-1 for root)
*/
function move_categories($category_ids, $new_parent = -1)
{
global $page;
if (count($category_ids) == 0)
{
return;
}
$new_parent = $new_parent < 1 ? 'NULL' : $new_parent;
$categories = array();
$query = '
SELECT id, id_uppercat, status, uppercats
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', $category_ids).')
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
$categories[$row['id']] =
array(
'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'],
'status' => $row['status'],
'uppercats' => $row['uppercats']
);
}
// is the movement possible? The movement is impossible if you try to move
// a category in a sub-category or itself
if ('NULL' != $new_parent)
{
$query = '
SELECT uppercats
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$new_parent.'
;';
list($new_parent_uppercats) = pwg_db_fetch_row(pwg_query($query));
foreach ($categories as $category)
{
// technically, you can't move a category with uppercats 12,125,13,14
// into a new parent category with uppercats 12,125,13,14,24
if (preg_match('/^'.$category['uppercats'].'(,|$)/', $new_parent_uppercats))
{
$page['errors'][] = l10n('You cannot move an album in its own sub album');
return;
}
}
}
$tables = array(
USER_ACCESS_TABLE => 'user_id',
GROUP_ACCESS_TABLE => 'group_id'
);
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET id_uppercat = '.$new_parent.'
WHERE id IN ('.implode(',', $category_ids).')
;';
pwg_query($query);
update_uppercats();
update_global_rank();
// status and related permissions management
if ('NULL' == $new_parent)
{
$parent_status = 'public';
}
else
{
$query = '
SELECT status
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$new_parent.'
;';
list($parent_status) = pwg_db_fetch_row(pwg_query($query));
}
if ('private' == $parent_status)
{
set_cat_status(array_keys($categories), 'private');
}
$page['infos'][] = l10n_dec(
'%d album moved', '%d albums moved',
count($categories)
);
}
/**
* Create a virtual category.
*
* @param string $category_name
* @param int $parent_id
* @param array $options
* - boolean commentable
* - boolean visible
* - string status
* - string comment
* - boolean inherit
* @return array ('info', 'id') or ('error')
*/
function create_virtual_category($category_name, $parent_id=null, $options=array())
{
global $conf, $user;
// is the given category name only containing blank spaces ?
if (preg_match('/^\s*$/', $category_name))
{
return array('error' => l10n('The name of an album must not be empty'));
}
$insert = array(
'name' => $category_name,
'rank' => 0,
'global_rank' => 0,
);
// is the album commentable?
if (isset($options['commentable']) and is_bool($options['commentable']))
{
$insert['commentable'] = $options['commentable'];
}
else
{
$insert['commentable'] = $conf['newcat_default_commentable'];
}
$insert['commentable'] = boolean_to_string($insert['commentable']);
// is the album temporarily locked? (only visible by administrators,
// whatever permissions) (may be overwritten if parent album is not
// visible)
if (isset($options['visible']) and is_bool($options['visible']))
{
$insert['visible'] = $options['visible'];
}
else
{
$insert['visible'] = $conf['newcat_default_visible'];
}
$insert['visible'] = boolean_to_string($insert['visible']);
// is the album private? (may be overwritten if parent album is private)
if (isset($options['status']) and 'private' == $options['status'])
{
$insert['status'] = 'private';
}
else
{
$insert['status'] = $conf['newcat_default_status'];
}
// any description for this album?
if (isset($options['comment']))
{
$insert['comment'] = $conf['allow_html_descriptions'] ? $options['comment'] : strip_tags($options['comment']);
}
if (!empty($parent_id) and is_numeric($parent_id))
{
$query = '
SELECT id, uppercats, global_rank, visible, status
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$parent_id.'
;';
$parent = pwg_db_fetch_assoc(pwg_query($query));
$insert['id_uppercat'] = $parent['id'];
$insert['global_rank'] = $parent['global_rank'].'.'.$insert['rank'];
// at creation, must a category be visible or not ? Warning : if the
// parent category is invisible, the category is automatically create
// invisible. (invisible = locked)
if ('false' == $parent['visible'])
{
$insert['visible'] = 'false';
}
// at creation, must a category be public or private ? Warning : if the
// parent category is private, the category is automatically create
// private.
if ('private' == $parent['status'])
{
$insert['status'] = 'private';
}
$uppercats_prefix = $parent['uppercats'].',';
}
else
{
$uppercats_prefix = '';
}
// we have then to add the virtual category
single_insert(CATEGORIES_TABLE, $insert);
$inserted_id = pwg_db_insert_id(CATEGORIES_TABLE);
single_update(
CATEGORIES_TABLE,
array('uppercats' => $uppercats_prefix.$inserted_id),
array('id' => $inserted_id)
);
update_global_rank();
if ('private' == $insert['status'] and !empty($insert['id_uppercat']) and ((isset($options['inherit']) and $options['inherit']) or $conf['inheritance_by_default']) )
{
$query = '
SELECT group_id
FROM '.GROUP_ACCESS_TABLE.'
WHERE cat_id = '.$insert['id_uppercat'].'
;';
$granted_grps = query2array($query, null, 'group_id');
$inserts = array();
foreach ($granted_grps as $granted_grp)
{
$inserts[] = array(
'group_id' => $granted_grp,
'cat_id' => $inserted_id
);
}
mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $inserts);
$query = '
SELECT user_id
FROM '.USER_ACCESS_TABLE.'
WHERE cat_id = '.$insert['id_uppercat'].'
;';
$granted_users = query2array($query, null, 'user_id');
add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id']), $granted_users)));
}
elseif ('private' == $insert['status'])
{
add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id']))));
}
return array(
'info' => l10n('Virtual album added'),
'id' => $inserted_id,
);
}
/**
* Set tags to an image.
* Warning: given tags are all tags associated to the image, not additionnal tags.
*
* @param int[] $tags
* @param int $image_id
*/
function set_tags($tags, $image_id)
{
set_tags_of( array($image_id=>$tags) );
}
/**
* Add new tags to a set of images.
*
* @param int[] $tags
* @param int[] $images
*/
function add_tags($tags, $images)
{
if (count($tags) == 0 or count($images) == 0)
{
return;
}
// we can't insert twice the same {image_id,tag_id} so we must first
// delete lines we'll insert later
$query = '
DELETE
FROM '.IMAGE_TAG_TABLE.'
WHERE image_id IN ('.implode(',', $images).')
AND tag_id IN ('.implode(',', $tags).')
;';
pwg_query($query);
$inserts = array();
foreach ($images as $image_id)
{
foreach ( array_unique($tags) as $tag_id)
{
$inserts[] = array(
'image_id' => $image_id,
'tag_id' => $tag_id,
);
}
}
mass_inserts(
IMAGE_TAG_TABLE,
array_keys($inserts[0]),
$inserts
);
invalidate_user_cache_nb_tags();
}
/**
* Delete tags and tags associations.
*
* @param int[] $tag_ids
*/
function delete_tags($tag_ids)
{
if (is_numeric($tag_ids))
{
$tag_ids = array($tag_ids);
}
if (!is_array($tag_ids))
{
return false;
}
$query = '
DELETE
FROM '.IMAGE_TAG_TABLE.'
WHERE tag_id IN ('.implode(',', $tag_ids).')
;';
pwg_query($query);
$query = '
DELETE
FROM '.TAGS_TABLE.'
WHERE id IN ('.implode(',', $tag_ids).')
;';
pwg_query($query);
invalidate_user_cache_nb_tags();
}
/**
* Returns a tag id from its name. If nothing found, create a new tag.
*
* @param string $tag_name
* @return int
*/
function tag_id_from_tag_name($tag_name)
{
global $page;
$tag_name = trim($tag_name);
if (isset($page['tag_id_from_tag_name_cache'][$tag_name]))
{
return $page['tag_id_from_tag_name_cache'][$tag_name];
}
// search existing by exact name
$query = '
SELECT id
FROM '.TAGS_TABLE.'
WHERE name = \''.$tag_name.'\'
;';
if (count($existing_tags = query2array($query, null, 'id')) == 0)
{
$url_name = trigger_change('render_tag_url', $tag_name);
// search existing by url name
$query = '
SELECT id
FROM '.TAGS_TABLE.'
WHERE url_name = \''.$url_name.'\'
;';
if (count($existing_tags = query2array($query, null, 'id')) == 0)
{
// search by extended description (plugin sub name)
$sub_name_where = trigger_change('get_tag_name_like_where', array(), $tag_name);
if (count($sub_name_where))
{
$query = '
SELECT id
FROM '.TAGS_TABLE.'
WHERE '.implode(' OR ', $sub_name_where).'
;';
$existing_tags = query2array($query, null, 'id');
}
if (count($existing_tags) == 0)
{// finally create the tag
mass_inserts(
TAGS_TABLE,
array('name', 'url_name'),
array(
array(
'name' => $tag_name,
'url_name' => $url_name,
)
)
);
$page['tag_id_from_tag_name_cache'][$tag_name] = pwg_db_insert_id(TAGS_TABLE);
invalidate_user_cache_nb_tags();
return $page['tag_id_from_tag_name_cache'][$tag_name];
}
}
}
$page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0];
return $page['tag_id_from_tag_name_cache'][$tag_name];
}
/**
* Set tags of images. Overwrites all existing associations.
*
* @param array $tags_of - keys are image ids, values are array of tag ids
*/
function set_tags_of($tags_of)
{
if (count($tags_of) > 0)
{
$query = '
DELETE
FROM '.IMAGE_TAG_TABLE.'
WHERE image_id IN ('.implode(',', array_keys($tags_of)).')
;';
pwg_query($query);
$inserts = array();
foreach ($tags_of as $image_id => $tag_ids)
{
foreach (array_unique($tag_ids) as $tag_id)
{
$inserts[] = array(
'image_id' => $image_id,
'tag_id' => $tag_id,
);
}
}
if (count($inserts))
{
mass_inserts(
IMAGE_TAG_TABLE,
array_keys($inserts[0]),
$inserts
);
}
invalidate_user_cache_nb_tags();
}
}
/**
* Associate a list of images to a list of categories.
* The function will not duplicate links and will preserve ranks.
*
* @param int[] $images
* @param int[] $categories
*/
function associate_images_to_categories($images, $categories)
{
if (count($images) == 0
or count($categories) == 0)
{
return false;
}
// get existing associations
$query = '
SELECT
image_id,
category_id
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE image_id IN ('.implode(',', $images).')
AND category_id IN ('.implode(',', $categories).')
;';
$result = pwg_query($query);
$existing = array();
while ($row = pwg_db_fetch_assoc($result))
{
$existing[ $row['category_id'] ][] = $row['image_id'];
}
// get max rank of each categories
$query = '
SELECT
category_id,
MAX(rank) AS max_rank
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE rank IS NOT NULL
AND category_id IN ('.implode(',', $categories).')
GROUP BY category_id
;';
$current_rank_of = query2array(
$query,
'category_id',
'max_rank'
);
// associate only not already associated images
$inserts = array();
foreach ($categories as $category_id)
{
if (!isset($current_rank_of[$category_id]))
{
$current_rank_of[$category_id] = 0;
}
if (!isset($existing[$category_id]))
{
$existing[$category_id] = array();
}
foreach ($images as $image_id)
{
if (!in_array($image_id, $existing[$category_id]))
{
$rank = ++$current_rank_of[$category_id];
$inserts[] = array(
'image_id' => $image_id,
'category_id' => $category_id,
'rank' => $rank,
);
}
}
}
if (count($inserts))
{
mass_inserts(
IMAGE_CATEGORY_TABLE,
array_keys($inserts[0]),
$inserts
);
update_category($categories);
}
}
/**
* Dissociate images from all old categories except their storage category and
* associate to new categories.
* This function will preserve ranks.
*
* @param int[] $images
* @param int[] $categories
*/
function move_images_to_categories($images, $categories)
{
if (count($images) == 0)
{
return false;
}
// let's first break links with all old albums but their "storage album"
$query = '
DELETE '.IMAGE_CATEGORY_TABLE.'.*
FROM '.IMAGE_CATEGORY_TABLE.'
JOIN '.IMAGES_TABLE.' ON image_id=id
WHERE id IN ('.implode(',', $images).')
';
if (is_array($categories) and count($categories) > 0)
{
$query.= '
AND category_id NOT IN ('.implode(',', $categories).')
';
}
$query.= '
AND (storage_category_id IS NULL OR storage_category_id != category_id)
;';
pwg_query($query);
if (is_array($categories) and count($categories) > 0)
{
associate_images_to_categories($images, $categories);
}
}
/**
* Associate images associated to a list of source categories to a list of
* destination categories.
*
* @param int[] $sources
* @param int[] $destinations
*/
function associate_categories_to_categories($sources, $destinations)
{
if (count($sources) == 0)
{
return false;
}
$query = '
SELECT image_id
FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id IN ('.implode(',', $sources).')
;';
$images = query2array($query, null, 'image_id');
associate_images_to_categories($images, $destinations);
}
/**
* Refer main Piwigo URLs (currently PHPWG_DOMAIN domain)
*
* @return string[]
*/
function pwg_URL()
{
$urls = array(
'HOME' => PHPWG_URL,
'WIKI' => PHPWG_URL.'/doc',
'DEMO' => PHPWG_URL.'/demo',
'FORUM' => PHPWG_URL.'/forum',
'BUGS' => PHPWG_URL.'/bugs',
'EXTENSIONS' => PHPWG_URL.'/ext',
);
return $urls;
}
/**
* Invalidates cached data (permissions and category counts) for all users.
*/
function invalidate_user_cache($full = true)
{
if ($full)
{
$query = '
TRUNCATE TABLE '.USER_CACHE_CATEGORIES_TABLE.';';
pwg_query($query);
$query = '
TRUNCATE TABLE '.USER_CACHE_TABLE.';';
pwg_query($query);
}
else
{
$query = '
UPDATE '.USER_CACHE_TABLE.'
SET need_update = \'true\';';
pwg_query($query);
}
trigger_notify('invalidate_user_cache', $full);
}
/**
* Invalidates cached tags counter for all users.
*/
function invalidate_user_cache_nb_tags()
{
global $user;
unset($user['nb_available_tags']);
$query = '
UPDATE '.USER_CACHE_TABLE.'
SET nb_available_tags = NULL';
pwg_query($query);
}
/**
* Adds the caracter set to a create table sql query.
* All CREATE TABLE queries must call this function
*
* @param string $query
* @return string
*/
function create_table_add_character_set($query)
{
defined('DB_CHARSET') or fatal_error('create_table_add_character_set DB_CHARSET undefined');
if ('DB_CHARSET'!='')
{
if ( version_compare(pwg_get_db_version(), '4.1.0', '<') )
{
return $query;
}
$charset_collate = " DEFAULT CHARACTER SET ".DB_CHARSET;
if (DB_COLLATE!='')
{
$charset_collate .= " COLLATE ".DB_COLLATE;
}
if ( is_array($query) )
{
foreach( $query as $id=>$q)
{
$q=trim($q);
$q=trim($q, ';');
if (preg_match('/^CREATE\s+TABLE/i',$q))
{
$q.=$charset_collate;
}
$q .= ';';
$query[$id] = $q;
}
}
else
{
$query=trim($query);
$query=trim($query, ';');
if (preg_match('/^CREATE\s+TABLE/i',$query))
{
$query.=$charset_collate;
}
$query .= ';';
}
}
return $query;
}
/**
* Returns access levels as array used on template with html_options functions.
*
* @param int $MinLevelAccess
* @param int $MaxLevelAccess
* @return array
*/
function get_user_access_level_html_options($MinLevelAccess = ACCESS_FREE, $MaxLevelAccess = ACCESS_CLOSED)
{
$tpl_options = array();
for ($level = $MinLevelAccess; $level <= $MaxLevelAccess; $level++)
{
$tpl_options[$level] = l10n(sprintf('ACCESS_%d', $level));
}
return $tpl_options;
}
/**
* returns a list of templates currently available in template-extension.
* Each .tpl file is extracted from template-extension.
*
* @param string $start (internal use)
* @return string[]
*/
function get_extents($start='')
{
if ($start == '') { $start = './template-extension'; }
$dir = opendir($start);
$extents = array();
while (($file = readdir($dir)) !== false)
{
if ( $file == '.' or $file == '..' or $file == '.svn') continue;
$path = $start . '/' . $file;
if (is_dir($path))
{
$extents = array_merge($extents, get_extents($path));
}
elseif ( !is_link($path) and file_exists($path)
and get_extension($path) == 'tpl' )
{
$extents[] = substr($path, 21);
}
}
return $extents;
}
/**
* Create a new tag.
*
* @param string $tag_name
* @return array ('id', info') or ('error')
*/
function create_tag($tag_name)
{
// does the tag already exists?
$query = '
SELECT id
FROM '.TAGS_TABLE.'
WHERE name = \''.$tag_name.'\'
;';
$existing_tags = query2array($query, null, 'id');
if (count($existing_tags) == 0)
{
single_insert(
TAGS_TABLE,
array(
'name' => $tag_name,
'url_name' => trigger_change('render_tag_url', $tag_name),
)
);
$inserted_id = pwg_db_insert_id(TAGS_TABLE);
return array(
'info' => l10n('Tag "%s" was added', stripslashes($tag_name)),
'id' => $inserted_id,
);
}
else
{
return array(
'error' => l10n('Tag "%s" already exists', stripslashes($tag_name))
);
}
}
/**
* Is the category accessible to the (Admin) user ?
* Note : if the user is not authorized to see this category, category jump
* will be replaced by admin cat_modify page
*
* @param int $category_id
* @return bool
*/
function cat_admin_access($category_id)
{
global $user;
// $filter['visible_categories'] and $filter['visible_images']
// are not used because it's not necessary (filter <> restriction)
if (in_array($category_id, explode(',', $user['forbidden_categories'])))
{
return false;
}
return true;
}
/**
* Retrieve data from external URL.
*
* @param string $src
* @param string|Ressource $dest - can be a file ressource or string
* @param array $get_data - data added to request url
* @param array $post_data - data transmitted with POST
* @param string $user_agent
* @param int $step (internal use)
* @return bool
*/
function fetchRemote($src, &$dest, $get_data=array(), $post_data=array(), $user_agent='Piwigo', $step=0)
{
// Try to retrieve data from local file?
if (!url_is_remote($src))
{
$content = @file_get_contents($src);
if ($content !== false)
{
is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
return true;
}
else
{
return false;
}
}
// After 3 redirections, return false
if ($step > 3) return false;
// Initialization
$method = empty($post_data) ? 'GET' : 'POST';
$request = empty($post_data) ? '' : http_build_query($post_data, '', '&');
if (!empty($get_data))
{
$src .= strpos($src, '?') === false ? '?' : '&';
$src .= http_build_query($get_data, '', '&');
}
// Initialize $dest
is_resource($dest) or $dest = '';
// Try curl to read remote file
// TODO : remove all these @
if (function_exists('curl_init') && function_exists('curl_exec'))
{
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $src);
@curl_setopt($ch, CURLOPT_HEADER, 1);
@curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($method == 'POST')
{
@curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
}
$content = @curl_exec($ch);
$header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
@curl_close($ch);
if ($content !== false and $status >= 200 and $status < 400)
{
if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m))
{
return fetchRemote($m[1], $dest, array(), array(), $user_agent, $step+1);
}
$content = substr($content, $header_length);
is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
return true;
}
}
// Try file_get_contents to read remote file
if (ini_get('allow_url_fopen'))
{
$opts = array(
'http' => array(
'method' => $method,
'user_agent' => $user_agent,
)
);
if ($method == 'POST')
{
$opts['http']['content'] = $request;
}
$context = @stream_context_create($opts);
$content = @file_get_contents($src, false, $context);
if ($content !== false)
{
is_resource($dest) ? @fwrite($dest, $content) : $dest = $content;
return true;
}
}
// Try fsockopen to read remote file
$src = parse_url($src);
$host = $src['host'];
$path = isset($src['path']) ? $src['path'] : '/';
$path .= isset($src['query']) ? '?'.$src['query'] : '';
if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false)
{
return false;
}
$http_request = $method." ".$path." HTTP/1.0\r\n";
$http_request .= "Host: ".$host."\r\n";
if ($method == 'POST')
{
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: ".strlen($request)."\r\n";
}
$http_request .= "User-Agent: ".$user_agent."\r\n";
$http_request .= "Accept: */*\r\n";
$http_request .= "\r\n";
$http_request .= $request;
fwrite($s, $http_request);
$i = 0;
$in_content = false;
while (!feof($s))
{
$line = fgets($s);
if (rtrim($line,"\r\n") == '' && !$in_content)
{
$in_content = true;
$i++;
continue;
}
if ($i == 0)
{
if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m))
{
fclose($s);
return false;
}
$status = (integer) $m[2];
if ($status < 200 || $status >= 400)
{
fclose($s);
return false;
}
}
if (!$in_content)
{
if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m))
{
fclose($s);
return fetchRemote(trim($m[1]),$dest,array(),array(),$user_agent,$step+1);
}
$i++;
continue;
}
is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line;
$i++;
}
fclose($s);
return true;
}
/**
* Returns the groupname corresponding to the given group identifier if exists.
*
* @param int $group_id
* @return string|false
*/
function get_groupname($group_id)
{
$query = '
SELECT name
FROM '.GROUPS_TABLE.'
WHERE id = '.intval($group_id).'
;';
$result = pwg_query($query);
if (pwg_db_num_rows($result) > 0)
{
list($groupname) = pwg_db_fetch_row($result);
}
else
{
return false;
}
return $groupname;
}
/**
* Returns the username corresponding to the given user identifier if exists.
*
* @param int $user_id
* @return string|false
*/
function get_username($user_id)
{
global $conf;
$query = '
SELECT '.$conf['user_fields']['username'].'
FROM '.USERS_TABLE.'
WHERE '.$conf['user_fields']['id'].' = '.intval($user_id).'
;';
$result = pwg_query($query);
if (pwg_db_num_rows($result) > 0)
{
list($username) = pwg_db_fetch_row($result);
}
else
{
return false;
}
return stripslashes($username);
}
/**
* Get url on piwigo.org for newsletter subscription
*
* @param string $language (unused)
* @return string
*/
function get_newsletter_subscribe_base_url($language='en_UK')
{
return PHPWG_URL.'/announcement/subscribe/';
}
/**
* Return admin menu id for accordion.
*
* @param string $menu_page
* @return int
*/
function get_active_menu($menu_page)
{
global $page;
if (isset($page['active_menu']))
{
return $page['active_menu'];
}
switch ($menu_page)
{
case 'photo':
case 'photos_add':
case 'rating':
case 'tags':
case 'batch_manager':
return 0;
case 'album':
case 'cat_list':
case 'cat_move':
case 'cat_options':
case 'permalinks':
return 1;
case 'user_list':
case 'user_perm':
case 'group_list':
case 'group_perm':
case 'notification_by_mail':
return 2;
case 'plugins':
case 'plugin':
return 3;
case 'site_manager':
case 'site_update':
case 'stats':
case 'history':
case 'maintenance':
case 'comments':
case 'updates':
return 4;
case 'configuration':
case 'derivatives':
case 'extend_for_templates':
case 'menubar':
case 'themes':
case 'theme':
case 'languages':
return 5;
default:
return 0;
}
}
/**
* Get tags list from SQL query (ids are surrounded by ~~, for get_tag_ids()).
*
* @param string $query
* @param boolean $only_user_language - if true, only local name is returned for
* multilingual tags (if ExtendedDescription plugin is active)
* @return array[] ('id', 'name')
*/
function get_taglist($query, $only_user_language=true)
{
$result = pwg_query($query);
$taglist = array();
$altlist = array();
while ($row = pwg_db_fetch_assoc($result))
{
$raw_name = $row['name'];
$name = trigger_change('render_tag_name', $raw_name, $row);
$taglist[] = array(
'name' => $name,
'id' => '~~'.$row['id'].'~~',
);
if (!$only_user_language)
{
$alt_names = trigger_change('get_tag_alt_names', array(), $raw_name);
foreach( array_diff( array_unique($alt_names), array($name) ) as $alt)
{
$altlist[] = array(
'name' => $alt,
'id' => '~~'.$row['id'].'~~',
);
}
}
}
usort($taglist, 'tag_alpha_compare');
if (count($altlist))
{
usort($altlist, 'tag_alpha_compare');
$taglist = array_merge($taglist, $altlist);
}
return $taglist;
}
/**
* Get tags ids from a list of raw tags (existing tags or new tags).
*
* In $raw_tags we receive something like array('~~6~~', '~~59~~', 'New
* tag', 'Another new tag') The ~~34~~ means that it is an existing
* tag. We added the surrounding ~~ to permit creation of tags like "10"
* or "1234" (numeric characters only)
*
* @param string|string[] $raw_tags - array or comma separated string
* @param boolean $allow_create
* @return int[]
*/
function get_tag_ids($raw_tags, $allow_create=true)
{
$tag_ids = array();
if (!is_array($raw_tags))
{
$raw_tags = explode(',',$raw_tags);
}
foreach ($raw_tags as $raw_tag)
{
if (preg_match('/^~~(\d+)~~$/', $raw_tag, $matches))
{
$tag_ids[] = $matches[1];
}
elseif ($allow_create)
{
// we have to create a new tag
$tag_ids[] = tag_id_from_tag_name($raw_tag);
}
}
return $tag_ids;
}
/**
* Returns the argument_ids array with new sequenced keys based on related
* names. Sequence is not case sensitive.
* Warning: By definition, this function breaks original keys.
*
* @param int[] $elements_ids
* @param string[] $name - names of elements, indexed by ids
* @return int[]
*/
function order_by_name($element_ids, $name)
{
$ordered_element_ids = array();
foreach ($element_ids as $k_id => $element_id)
{
$key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
$ordered_element_ids[$key] = $element_id;
}
ksort($ordered_element_ids);
return $ordered_element_ids;
}
/**
* Grant access to a list of categories for a list of users.
*
* @param int[] $category_ids
* @param int[] $user_ids
*/
function add_permission_on_category($category_ids, $user_ids)
{
if (!is_array($category_ids))
{
$category_ids = array($category_ids);
}
if (!is_array($user_ids))
{
$user_ids = array($user_ids);
}
// check for emptiness
if (count($category_ids) == 0 or count($user_ids) == 0)
{
return;
}
// make sure categories are private and select uppercats or subcats
$cat_ids = get_uppercat_ids($category_ids);
if (isset($_POST['apply_on_sub']))
{
$cat_ids = array_merge($cat_ids, get_subcat_ids($category_ids));
}
$query = '
SELECT id
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.implode(',', $cat_ids).')
AND status = \'private\'
;';
$private_cats = query2array($query, null, 'id');
if (count($private_cats) == 0)
{
return;
}
$inserts = array();
foreach ($private_cats as $cat_id)
{
foreach ($user_ids as $user_id)
{
$inserts[] = array(
'user_id' => $user_id,
'cat_id' => $cat_id
);
}
}
mass_inserts(
USER_ACCESS_TABLE,
array('user_id','cat_id'),
$inserts,
array('ignore'=>true)
);
}
/**
* Returns the list of admin users.
*
* @param boolean $include_webmaster
* @return int[]
*/
function get_admins($include_webmaster=true)
{
$status_list = array('admin');
if ($include_webmaster)
{
$status_list[] = 'webmaster';
}
$query = '
SELECT
user_id
FROM '.USER_INFOS_TABLE.'
WHERE status in (\''.implode("','", $status_list).'\')
;';
return query2array($query, null, 'user_id');
}
/**
* Delete all derivative files for one or several types
*
* @param 'all'|int[] $types
*/
function clear_derivative_cache($types='all')
{
if ($types === 'all')
{
$types = ImageStdParams::get_all_types();
$types[] = IMG_CUSTOM;
}
elseif (!is_array($types))
{
$types = array($types);
}
for ($i=0; $i<count($types); $i++)
{
$type = $types[$i];
if ($type == IMG_CUSTOM)
{
$type = derivative_to_url($type).'[a-zA-Z0-9]+';
}
elseif (in_array($type, ImageStdParams::get_all_types()))
{
$type = derivative_to_url($type);
}
else
{//assume a custom type
$type = derivative_to_url(IMG_CUSTOM).'_'.$type;
}
$types[$i] = $type;
}
$pattern='#.*-';
if (count($types)>1)
{
$pattern .= '(' . implode('|',$types) . ')';
}
else
{
$pattern .= $types[0];
}
$pattern.='\.[a-zA-Z0-9]{3,4}$#';
if ($contents = @opendir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR))
{
while (($node = readdir($contents)) !== false)
{
if ($node != '.'
and $node != '..'
and is_dir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$node))
{
clear_derivative_cache_rec(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$node, $pattern);
}
}
closedir($contents);
}
}
/**
* Used by clear_derivative_cache()
* @ignore
*/
function clear_derivative_cache_rec($path, $pattern)
{
$rmdir = true;
$rm_index = false;
if ($contents = opendir($path))
{
while (($node = readdir($contents)) !== false)
{
if ($node == '.' or $node == '..')
continue;
if (is_dir($path.'/'.$node))
{
$rmdir &= clear_derivative_cache_rec($path.'/'.$node, $pattern);
}
else
{
if (preg_match($pattern, $node))
{
unlink($path.'/'.$node);
}
elseif ($node=='index.htm')
{
$rm_index = true;
}
else
{
$rmdir = false;
}
}
}
closedir($contents);
if ($rmdir)
{
if ($rm_index)
{
unlink($path.'/index.htm');
}
clearstatcache();
@rmdir($path);
}
return $rmdir;
}
}
/**
* Deletes derivatives of a particular element
*
* @param array $infos ('path'[, 'representative_ext'])
* @param 'all'|int $type
*/
function delete_element_derivatives($infos, $type='all')
{
$path = $infos['path'];
if (!empty($infos['representative_ext']))
{
$path = original_to_representative( $path, $infos['representative_ext']);
}
if (substr_compare($path, '../', 0, 3)==0)
{
$path = substr($path, 3);
}
$dot = strrpos($path, '.');
if ($type=='all')
{
$pattern = '-*';
}
else
{
$pattern = '-'.derivative_to_url($type).'*';
}
$path = substr_replace($path, $pattern, $dot, 0);
if ( ($glob=glob(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$path)) !== false)
{
foreach( $glob as $file)
{
@unlink($file);
}
}
}
/**
* Returns an array containing sub-directories, excluding ".svn"
*
* @param string $directory
* @return string[]
*/
function get_dirs($directory)
{
$sub_dirs = array();
if ($opendir = opendir($directory))
{
while ($file = readdir($opendir))
{
if ($file != '.'
and $file != '..'
and is_dir($directory.'/'.$file)
and $file != '.svn')
{
$sub_dirs[] = $file;
}
}
closedir($opendir);
}
return $sub_dirs;
}
/**
* Recursively delete a directory.
*
* @param string $path
* @param string $trash_path, try to move the directory to this path if it cannot be delete
*/
function deltree($path, $trash_path=null)
{
if (is_dir($path))
{
$fh = opendir($path);
while ($file = readdir($fh))
{
if ($file != '.' and $file != '..')
{
$pathfile = $path . '/' . $file;
if (is_dir($pathfile))
{
deltree($pathfile, $trash_path);
}
else
{
@unlink($pathfile);
}
}
}
closedir($fh);
if (@rmdir($path))
{
return true;
}
elseif (!empty($trash_path))
{
if (!is_dir($trash_path))
{
@mkgetdir($trash_path, MKGETDIR_RECURSIVE|MKGETDIR_DIE_ON_ERROR|MKGETDIR_PROTECT_HTACCESS);
}
while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
{
if (!is_dir($r))
{
@rename($path, $r);
break;
}
}
}
else
{
return false;
}
}
}
/**
* Returns keys to identify the state of main tables. A key consists of the
* last modification timestamp and the total of items (separated by a _).
* Additionally returns the hash of root path.
* Used to invalidate LocalStorage cache on admin pages.
*
* @param string|string[] list of keys to retrieve (categories,groups,images,tags,users)
* @return string[]
*/
function get_admin_client_cache_keys($requested=array())
{
$tables = array(
'categories' => CATEGORIES_TABLE,
'groups' => GROUPS_TABLE,
'images' => IMAGES_TABLE,
'tags' => TAGS_TABLE,
'users' => USER_INFOS_TABLE
);
if (!is_array($requested))
{
$requested = array($requested);
}
if (empty($requested))
{
$requested = array_keys($tables);
}
else
{
$requested = array_intersect($requested, array_keys($tables));
}
$keys = array(
'_hash' => md5(get_absolute_root_url()),
);
foreach ($requested as $item)
{
$query = '
SELECT CONCAT(
UNIX_TIMESTAMP(MAX(lastmodified)),
"_",
COUNT(*)
)
FROM '. $tables[$item] .'
;';
list($keys[$item]) = pwg_db_fetch_row(pwg_query($query));
}
return $keys;
}
|