C:\$Recycle.Bin\S-1-5-21-3967099092-2644009230-141905953-500\$RVSVKU0\setup\cpg1419\upload.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
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
<?php
/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2008 Dev Team
  v1.1 originally written by Gregory DEMAR

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 3
  as published by the Free Software Foundation.
  
  ********************************************
  Coppermine version: 1.4.19
  $HeadURL: https://coppermine.svn.sourceforge.net/svnroot/coppermine/trunk/cpg1.4.x/upload.php $
  $Revision: 4424 $
  $Author: gaugau $
  $Date: 2008-04-30 07:37:24 +0200 (Mi, 30 Apr 2008) $
**********************************************/

// Confirm we are in Coppermine and set the language blocks.
define('IN_COPPERMINE'true);
define('UPLOAD_PHP'true);
define('DB_INPUT_PHP'true);
define('ADMIN_PHP'true);

// Call basic functions, etc.
require('include/init.inc.php');
require(
'include/picmgmt.inc.php');

// Some placeholders.
$customize CUSTOMIZE_UPLOAD_FORM;
$user_form USER_UPLOAD_FORM;
$allowed_URI_boxes NUM_URI_BOXES;
$allowed_file_boxes NUM_FILE_BOXES;

// Check to see if user can upload pictures.  Quit with an error if he cannot.
if (!USER_CAN_UPLOAD_PICTURES && !USER_CAN_CREATE_ALBUMS) {
    
cpg_die(ERROR$lang_errors['perm_denied'], __FILE____LINE__);
}

// Globalize $CONFIG.
global $CONFIG$lang_upload_php$user_form$max_file_size;

//___________________________________Function Block_______________________________________


// The form label creation function. Takes a non-array element form $data as its argument.
function form_label($text) {
    echo <<<EOT
        <tr>
                <td class="tableh2" colspan="2">
                        <b>
$text</b>
                </td>
        </tr>

EOT;
}

// The form statement creation function. Takes a non-array element form $data as its argument.
function form_statement($text) {
    echo <<<EOT
        <tr>
                <td class="tableb" colspan="2">
                        
$text
                </td>
        </tr>

EOT;
}

// The hidden form input function. Takes the hidden input field name and value.
function hidden_input($name$value) {
        echo <<<EOT
        <tr>
            <td colspan="2">
                   <input type="hidden" name="
$name" value="$value" />
            </td>
        </tr>

EOT;
}

// The text box form input function. Takes the text label for the box, the input name, the maximum length for text boxes,
// and the number of iterations.
function text_box_input($text$name$max_length$iterations$default='') {

    global 
$CONFIG;

    
$ordinal '';

    if ((
$text == '') and ($iterations == '')) {
        echo 
"        <input type=\"hidden\" name=\"$name\" value=\"$default\" />\n";
        return;
    }

    
// Begin loop
    
for ($counter=0$counter<$iterations$counter++) {

    
// Create a numbering system when necessary.
    
if ($text == '') {
        
$cardinal $counter 1;
        
$ordinal "".$cardinal.". ";
    }

    
// Create a text box.
    
echo <<<EOT
        <tr>
            <td width="40%" class="tableb">
                        
$text  $ordinal
        </td>
        <td width="60%" class="tableb" valign="top">
                <input type="text" style="width: 100%" name="
$name" maxlength="$max_length" value="$default" class="textinput" id="$name" />
                </td>
        </tr>

EOT;
    }
}

// The file input function. Takes the label, field name, and number of iterations as arguments.
function file_input($text$name$iterations) {

    
$ordinal '';

    
// Begin loop
    
for ($counter=0$counter<$iterations$counter++) {

    
// Create a numbering system when necessary.
    
if ($text == '') {
        
$cardinal $counter 1;
        
$ordinal "".$cardinal.". ";
    }

    
// Create the file input box.
    
echo <<<EOT
        <tr>
            <td class="tableb">
                        
$text  $ordinal
        </td>
        <td class="tableb" valign="top">
                        <input type="file" name="
$name" size="40" class="listbox" />
                </td>
        </tr>

EOT;
    }
}

// The function for text areas on forms. Takes the label, field name, and maximum length as arguments.
function text_area_input($text$name$max_length,$default='') {

    
// Create the text area.
    
echo <<<EOT
        <tr>
                <td class="tableb" valign="top">
                        
$text
                </td>
                <td class="tableb" valign="top">
                        <textarea name="
$name" rows="5" cols="40" class="textinput" style="width: 100%;" onKeyDown="textCounter(this, $max_length);" onKeyUp="textCounter(this, $max_length);">$default</textarea>
                </td>
        </tr>
EOT;
}

// The function to create the album list drop down.
function form_alb_list_box($text$name) {
// frogfoot re-wrote this function to present the list in categorized, sorted and nicely formatted order

    // Pull the $CONFIG array and the GET array into the function
    
global $CONFIG$lang_upload_php;

    
// Also pull the album lists into the function
    
global $user_albums_list$public_albums_list;

    
// Check to see if an album has been preselected by URL addition. If so, make $sel_album the album number. Otherwise, make $sel_album 0.
    
$sel_album = isset($_GET['album']) ? $_GET['album'] : 0;

    
// Create the opening of the drop down box
    
echo <<<EOT
    <tr>
        <td class="tableb">
            
$text
        </td>
        <td class="tableb" valign="top">
            <select name="
$name" class="listbox">

EOT;

    
// Reset counter
    
$list_count 0;

    
// Cycle through the User albums
    
foreach($user_albums_list as $album) {

        
// Add to multi-dim array for later sorting
        
$listArray[$list_count]['cat'] = $lang_upload_php['personal_albums'];
        
$listArray[$list_count]['aid'] = $album['aid'];
        
$listArray[$list_count]['title'] = $album['title'];
        
$list_count++;
    }

    
// Cycle through the public albums
    
foreach($public_albums_list as $album) {

        
// Set $album_id to the actual album ID
        
$album_id $album['aid'];

        
// Get the category name
        
$vQuery "SELECT cat.name FROM " $CONFIG['TABLE_CATEGORIES'] . " cat, " $CONFIG['TABLE_ALBUMS'] . " alb WHERE alb.aid='" $album_id "' AND cat.cid=alb.category";
        
$vRes cpg_db_query($vQuery);
        
$vRes mysql_fetch_array($vRes);

        
// Add to multi-dim array for sorting later
        
if ($vRes['name']) {
            
$listArray[$list_count]['cat'] = $vRes['name'];
        } else {
            
$listArray[$list_count]['cat'] = $lang_upload_php['albums_no_category'];
        }
        
$listArray[$list_count]['aid'] = $album['aid'];
        
$listArray[$list_count]['title'] = $album['title'];
        
$list_count++;
    }

    
// Sort the pulldown options by category and album name
    
$listArray array_csort($listArray,'cat','title');

    
// Finally, print out the nicely sorted and formatted drop down list
    
$alb_cat '';
        echo 
'                <option value="">' $lang_upload_php['select_album'] . "</option>\n";
    foreach (
$listArray as $val) {
        if (
$val['cat'] != $alb_cat) {
if (
$alb_cat) echo "                </optgroup>\n";
            echo 
'                <optgroup label="' $val['cat'] . '">' "\n";
            
$alb_cat $val['cat'];
        }
        echo 
'                <option value="' $val['aid'] . '"' . ($val['aid'] == $sel_album ' selected' '') . '>   ' $val['title'] . "</option>\n";
    }
    if (
$alb_cat) echo "                </optgroup>\n";

    
// Close the drop down
    
echo <<<EOT
            </select>
        </td>
    </tr>

EOT;
}


// The create form function. Takes the $data array as its object.
//
// Type:
// 0 => text box input
// 1 => file input
// 2 => album list
// 3 => text area input
// 4 => hidden input
function create_form(&$data) {

    global 
$CONFIG$lang_upload_php;

    
// Cycle through the elements in the data array.
    
foreach($data as $element) {

        
// If the element is another array, parse the definition contained within the array.
        
if ((is_array($element))) {
            
$element[2] = (isset($element[2])) ? $element[2] : '';
            
$element[3] = (isset($element[3])) ? $element[3] : '';
            
$element[4] = (isset($element[4])) ? $element[4] : '';

            
// Based on the type declared in the data array's third position, create a different form input.
            
switch ($element[2]) {

                
// If the type is a text box input
                
case :

                    
//Call the form input function.
                    
text_box_input($element[0], $element[1], $element[3], $element[4], (isset($element[5])) ? $element[5] : '');
                    break;

                
// If the type is a file input.
                
case :

                    
// Call the file input function.
                    
file_input($element[0], $element[1], $element[3]);
                    break;

                
// If the type is an album list dropdown.
                
case :

                    
// Call the album list function.
                    
form_alb_list_box($element[0], $element[1]);
                    break;

                
// If the type is a text area
                
case :

                    
// Call the text area function.
                    
text_area_input($element[0], $element[1], $element[3], (isset($element[4])) ? $element[4] : '');
                    break;

                
// If the type is a hidden form
                
case :

                    
// Call the hidden input funtion.
                    
hidden_input($element[0], $element[1]);
                    break;

                
// If the type is not present, kill the script.
                
default:
                    
cpg_die(ERROR$lang_upload_php['reg_instr_1'], __FILE____LINE__);
            } 
// switch
        
} else {

            
// If the element is not an array, it is a label, so call the label function.
            
form_label($element);
        }
    }
}

// The open_form function creates the Javascript verification code and the opening form tags.
// $path hold the form action path.
function open_form($path) {

    echo <<<EOT
    <script language="javascript" type="text/javascript">
    function textCounter(field, maxlimit) {
            if (field.value.length > maxlimit) // if too long...trim it!
            field.value = field.value.substring(0, maxlimit);
    }
    </script>
    <form method="post" action="
$path" enctype="multipart/form-data">
EOT;
}

// The close form function creates the submit button and the closing tags.
function close_form($button_value) {

// Pull the language array into the function.
global $lang_upload_php;

// Create the submit button and close the form.
echo <<<EOT
        <tr>
                <td colspan="2" align="center" class="tablef">
                        <input type="submit" value="
{$button_value}" class="button" />
                </td>

        </tr>

EOT;
}

function 
form_instructions() {

    global 
$CONFIG$lang_upload_php$user_form$max_file_size;

    echo 
"<tr><td colspan=\"2\">";

    
printf ($lang_upload_php['reg_instr_2'], $CONFIG['max_upl_size']);

    if (
$user_form '3') {

        echo 
"<br /><br />{$lang_upload_php['reg_instr_3']}";

    }

    if ((
$user_form == '2') or ($user_form == '3') or ($user_form == '5') or ($user_form == '6')) {

        echo 
"<br /><br />{$lang_upload_php['reg_instr_4']}";

    }

    echo 
"<br /><br />{$lang_upload_php['reg_instr_5']}";

    echo 
"</td></tr>";

}


// The get_and_convert_to_bytes function retrieves a limitng value from php.ini and converts the shorthand to bytes.
function get_and_convert_to_bytes ($ini_variable_name) {

    
// Get the variable from php.ini
    
$ini_string ini_get($ini_variable_name);

    
// Declare an array to store regex matches in.
    
$parsed_ini_size = array();

    
// Make sure the returned value is a string, then split the number and the unit in two.
    
if ((is_string($ini_string)) and (eregi('^([[:digit:]])+([[:alpha:]])*$'$ini_string$parsed_ini_size))) {

        
// Store the numerical component in $ini_limit cast as an integer
        
$ini_limit = (int) $parsed_ini_size[1];

        
// Convert the unit to lower case for analysis and store in $ini_limit_unit.
        
$ini_limit_unit strtolower($parsed_ini_size[2]);


        
// Check the unit to see if any conversion is necessary.
        
if ($ini_limit_unit == 'm') {

             
// The units indicate megabytes. Shift to bytes.
             
$ini_limit $ini_limit << 20;

        } elseif (
$ini_limit_unit == 'k') {

             
// The units indicate kilobytes. Shift to bytes.
             
$ini_limit $ini_limit << 10;
        }

        
// Return the modified value from php.ini
        
return $ini_limit;

    } else {

        
// The returned value is not a string or no pattern match was found. Return false.
        
return false;

    }
}
// Moved to 'logger.inc.php' - omni
// The function spring_cleaning is a garbage collection routine used to purge a directory of old files.
if (!function_exists('spring_cleaning')){
function 
spring_cleaning($directory_path$cache_time 86400$exclusion_list = array('index.html')) {

    
//Storage the deleted files
    
$deleted_list = array();

    
//First we get the transitory directory handle.
    
$directory_handle opendir($directory_path);

    
// Exit if the directory cannot be opened.
    
if(!$directory_handle) {

        
// Return.
        
return;

    }

    
// Now let's read through the directory contents.
    
while (!(($file readdir($directory_handle)) === false)) {

            
// Avoid deleting the index page of the directory.
            
if (in_array($file,$exclusion_list)) {

                
// This is the index file, so we move on.
                
continue;

            }

            
$dir_path $directory_path."/".$file;

            if (
is_dir($dir_path)) {

                
// This is a directory, so we move on.
                
continue;

            }

            
// We find out when the file was last accessed.
            
$access_time filemtime($dir_path); // fileatime() returned incorrect value on Windows

            // We find out the current time.
            
$current_time time();

            
// We calculate the the delete time. We will delete anything older than $cache_time.
            
$delete_time $current_time $access_time;

            
// Now we compare the two.
            
if ($delete_time >= $cache_time) {

                    
// The file is old. We delete it.
                    
$deleted_list[] = $dir_path// Store the name of the file getting deleted
                    
unlink($dir_path);
            }

    }

    
// Don't forget to close the directory.
    
closedir($directory_handle);
    return 
$deleted_list;
        }
}
// The create_record function. Takes the encoded string. Returns the unique record ID.
function create_record($encoded_string) {

    
// Globalize $CONFIG
    
global $CONFIG;

    
// Declare and initialize variables.
    
$unique_ID_array = array();
    
$generic_array = array();

    
// Get all IDs from the table.
    
$result cpg_db_query("SELECT unique_ID FROM {$CONFIG['TABLE_TEMPDATA']}");

    
// Create unique ID array.
    
if (mysql_num_rows($result)) {

        
// Move all values into $unique_ID_array.
        
while ($generic_array mysql_fetch_array($result)) {

            
// Store the values.
            
$unique_ID_array[] = $generic_array['unique_ID'];

        }

    } else {

        
// The table may be empty. Give it a value.
        
$unique_ID_array[] = 0;

    }

    
// Free resources.
    
mysql_free_result($result);

    
// Generate the unique ID. Keep generating new IDs until one that is not in use is found.
    
do {

        
// Create a random string by taking the first 8 characters of an MD5 hash of a concatenation of the current UNIX epoch time and the current server process ID.
        
$unique_ID substr(md5(uniqid("")), 08);

    } while (
in_array($unique_ID$unique_ID_array));

    
// Create a timestamp to track the record.
    
$timestamp time();

    
// Insert the new record.
    
$result cpg_db_query("INSERT INTO {$CONFIG['TABLE_TEMPDATA']} VALUES ('$unique_ID', '$encoded_string', '$timestamp')");

    
// Return the unique ID if insertion was successful. Otherwise, return false.
    
if ($result) {

        return 
$unique_ID;

    } else {

        return 
FALSE;

    }

}

// The update_record function. Takes the $unique_ID and $encoded_string.
function update_record($unique_ID$encoded_string) {

    
// Globalize $CONFIG
    
global $CONFIG;

    
// Update record.
    
$result cpg_db_query("UPDATE {$CONFIG['TABLE_TEMPDATA']} SET encoded_string = '$encoded_string' WHERE unique_ID = '$unique_ID'");

    
// Return true if successful.
    
if ($result) {

        return 
TRUE;

    } else {

        return 
FALSE;

    }

}

// The delete_record function. Takes the $unique_ID.
function delete_record($unique_ID) {

    
// Globalize $CONFIG
    
global $CONFIG;

    
// Delete record.
    
$result cpg_db_query("DELETE FROM {$CONFIG['TABLE_TEMPDATA']} WHERE unique_ID = '$unique_ID'");

    
// Return true if successful.
    
if ($result) {

        return 
TRUE;

    } else {

        return 
FALSE;

    }

}

// The retrieve_record function. Takes the $unique_ID.
function retrieve_record($unique_ID) {

    
// Globalize $CONFIG
    
global $CONFIG;

    
// Retrieve record.
    
$result cpg_db_query("SELECT encoded_string FROM {$CONFIG['TABLE_TEMPDATA']} WHERE unique_ID = '$unique_ID'");

    
// Return string if successful.
    
if (mysql_num_rows($result)) {

        
// Move value into $encoded_string.
        
while ($generic_array mysql_fetch_array($result)) {

            
// Store the value.
            
$encoded_string $generic_array['encoded_string'];

        }

        
// Free resources.
        
mysql_free_result($result);

        return 
$encoded_string;

    } else {

        
// Free resources.
        
mysql_free_result($result);

        return 
FALSE;

    }

}

// The clean_table function.
function clean_table() {

    
// Globalize $CONFIG
    
global $CONFIG;

    
// Create a timestamp from an hour ago.
    
$comparative_timestamp time() - 3600;

    
// Delete record.
    
$result cpg_db_query("DELETE FROM {$CONFIG['TABLE_TEMPDATA']} WHERE timestamp < $comparative_timestamp");

    
// Return true if successful.
    
if ($result) {

        return 
TRUE;

    } else {

        return 
FALSE;

    }

}

//The function check_status determines the status of a URI resource.
//It takes the URI as its argument and serves to give more specific error
//messages about unavailable resources.
function check_status($URI) {

    
// Parse the URI into it's requisite parts.
    
$parts = @parse_url($URI);

    
// If there is no detectable host, return FALSE.
    
if (empty($parts["host"])) {

        return 
FALSE;

    } else {

        
$host $parts["host"];

    }

    
// If a path is detected, make it $path. Otherwise, assume it is a directory.
    
if (!empty($parts["path"])) {

        
$path $parts["path"];

    } else {

        
$path "/";

    }

    
// Append any queries that might be attached.
    
if (!empty($parts["query"])) {

        
$path .= "?" $parts["query"];

    }

    
// Set the port if supplied. Default to port 80.
    
if (!empty($parts["port"])) {

        
$port $parts["port"];

    } else {

        
$port "80";

    }

    
// Attempt to open a socket to the host.
    
$socket = @fsockopen$host$port$errno$errstr30 );

    
// Verify that the socket opened.  Return false if it fails.
    
if (!$socket) {

        return 
FALSE;

    } else {

        
// Craft HTTP request.
        
$request "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host\r\nAccept: */*\r\n\r\n";

        
// Get request length.
        
$length strlen($request);

        
// Send request data to host. Return false if there is an error.
        
if(!(fwrite($socket$request$length))) {

            return 
FALSE;

        }

        
// Collect the host's response.
        
$response fgets($socket22);

        
// Close the socket.
        
fclose($socket);

        
// Return the response.
        
return $response;

    }

}
//################################# MAIN CODE BLOCK ##################################################

// Check to see if user customizations are allowed and if one the request has been made yet.

if ((CUSTOMIZE_UPLOAD_FORM) and (!isset($_REQUEST['file_upload_request'])) and (!isset($_REQUEST['URI_upload_request'])) and (!isset($_POST['control']))) {

    
// Check to see if the form type is configurable.  If it is, produce the configuration form. Otherwise, generate a warning.

    
if(!(USER_UPLOAD_FORM == 0) and !(USER_UPLOAD_FORM == 7)) {

        
// Create the box request page.
        
pageheader($lang_upload_php['custom_title']);
        
open_form($_SERVER['PHP_SELF']);
        
starttable("100%"$lang_upload_php['custom_title'], 2);
        echo 
"<tr><td colspan=\"2\">";
        echo 
"{$lang_upload_php['cust_instr_1']}<br /><br />";

        
$data = array();
        
$data[] = $lang_upload_php['cust_instr_2'];

        
// If the file upload type is only file uploads or is a dual mode, ask for the requisite number of file upload boxes.
        
if ((USER_UPLOAD_FORM == '1') or (USER_UPLOAD_FORM == '3') or (USER_UPLOAD_FORM == '4') or (USER_UPLOAD_FORM == '6')) {

            
// Add the file upload array element to the form array.
            
$data[] = array($lang_upload_php['cust_instr_6'],'file_upload_request''0''4''1');

            
// Print number of allowed file upload boxes.
            
printf ($lang_upload_php['cust_instr_3'], $allowed_file_boxes);

            echo 
"<br /><br />";

        }

        
// If the file upload type is only URI uploads or is a dual mode, ask for the requisite number of URI upload boxes.
        
if ((USER_UPLOAD_FORM == '2') or (USER_UPLOAD_FORM == '3') or (USER_UPLOAD_FORM == '5') or (USER_UPLOAD_FORM == '6')) {

            
// Add the URI upload array element to the form array.
            
$data[] = array($lang_upload_php['cust_instr_5'], 'URI_upload_request''0''4''1');

            
// Print number of allowed URI upload boxes.
            
printf ($lang_upload_php['cust_instr_4'], $allowed_URI_boxes);

            echo 
"<br /><br />";

        }

        echo 
"{$lang_upload_php['cust_instr_7']}<br /><br />";
        echo 
"</td></tr>";
        
create_form($data);
        
close_form($lang_continue);
        
endtable();
        echo 
"</form>";
        
pagefooter();

        
// Exit the script.
        
exit;

    } else {

        
//Use the default settings for the number of boxes.

        
$num_URI_boxes NUM_URI_BOXES;
        
$num_file_boxes NUM_FILE_BOXES;

    }

} elseif (
CUSTOMIZE_UPLOAD_FORM) {

// If the user is allowed to customize the form, check the incoming data for the number of requested boxes.

    //Check for the number of file upload boxes.

    
if (isset($_REQUEST['file_upload_request'])) {

        
// Do some validation.
        
$filtered_request max(0intval($_REQUEST['file_upload_request']));

        if (
$filtered_request NUM_FILE_BOXES) {
            
$num_file_boxes NUM_FILE_BOXES;
        } else {
            
$num_file_boxes $filtered_request;
        }

    }

    
//Check for the number of requested URI upload boxes.

    
if (isset($_REQUEST['URI_upload_request'])) {

        
// Do some validation.
        
$filtered_request max(0intval($_REQUEST['URI_upload_request']));

        if (
$filtered_request NUM_URI_BOXES) {
            
$num_URI_boxes NUM_URI_BOXES;
        } else {
            
$num_URI_boxes $filtered_request;
        }

    }

} else {

    
//Use the default settings for the number of boxes.

    
$num_URI_boxes NUM_URI_BOXES;
    
$num_file_boxes NUM_FILE_BOXES;

}
if (empty(
$num_URI_boxes) && empty($num_file_boxes)) {
    
$num_URI_boxes 0;
    
$num_file_boxes 1;
}

// Get public and private albums, and set maximum individual file size.

if (GALLERY_ADMIN_MODE) {
    
$public_albums cpg_db_query("SELECT aid, title FROM {$CONFIG['TABLE_ALBUMS']} WHERE category < " FIRST_USER_CAT " ORDER BY title");
} else {
        
$public_albums cpg_db_query("SELECT aid, title FROM {$CONFIG['TABLE_ALBUMS']} WHERE category < " FIRST_USER_CAT " AND uploads='YES' AND (visibility = '0' OR visibility IN ".USER_GROUP_SET.") ORDER BY title");
}
if (
mysql_num_rows($public_albums)) {
    
$public_albums_list cpg_db_fetch_rowset($public_albums);
} else {
    
$public_albums_list = array();
}

if (
USER_ID) {
    
$user_albums cpg_db_query("SELECT aid, title FROM {$CONFIG['TABLE_ALBUMS']} WHERE category='" . (FIRST_USER_CAT USER_ID) . "' ORDER BY title");
    if (
mysql_num_rows($user_albums)) {
        
$user_albums_list cpg_db_fetch_rowset($user_albums);
    } else {
        
$user_albums_list = array();
    }
} else {
    
$user_albums_list = array();
}

if (!
count($public_albums_list) && !count($user_albums_list)) {  // there's no album where the user is allowed to upload to
    
if (USER_CAN_CREATE_ALBUMS) {
        
cpg_die (ERROR$lang_upload_php['err_no_alb_uploadables'].'<br />&nbsp;<br /><a href="albmgr.php" title="'.$lang_user_admin_menu['albmgr_title'].'" class="admin_menu">'.$lang_user_admin_menu['albmgr_lnk'].'</a>'__FILE____LINE__);
    } else {
        
cpg_die (ERROR$lang_upload_php['err_no_alb_uploadables'], __FILE____LINE__);
    }
}

// Assign maximum file size for browser crontrols.
$max_file_size $CONFIG['max_upl_size'] << 10;

// Create the upload forms using the upload congfiguration.

if (!isset($_REQUEST['control'])) {

    
// Do some cleanup in the edit directory.
    
spring_cleaning('./'.$CONFIG['fullpath'].'edit',CPG_HOUR);

    
// Do some cleaning in the temp data table.
    
clean_table();

    
// Create upload form headers.
    
pageheader($lang_upload_php['title']);


    
// Select the form action.
    
if (USER_UPLOAD_FORM == '0') {

        
// The user has the single upload only form. Send the request to db_input.php.
        
open_form('db_input.php');
        
// Open the form table.
        
starttable("100%"$lang_upload_php['title'], 2);
    } else {

        
// Direct the request to this script and print the form instructions.
        
open_form($_SERVER['PHP_SELF']);
        
// Open the form table.
        
starttable("100%"$lang_upload_php['title'], 2);
        
form_instructions();

    }



    
// Use a if-then-else construct to create the upload form for the user based on the setting in the
    // groups panel.
    
if(USER_UPLOAD_FORM == '0') {

        
// The user should have the 'single upload only' form.

        // Declare an array containing the various upload form box definitions.
        
$captionLabel $lang_upload_php['description'];
        if (
$CONFIG['show_bbcode_help']) {$captionLabel .= '&nbsp;'cpg_display_help('f=index.html&amp;base=64&amp;h='.urlencode(base64_encode(serialize($lang_bbcode_help_title))).'&amp;t='.urlencode(base64_encode(serialize($lang_bbcode_help))),470,245);}
        
$form_array = array(
        
sprintf($lang_upload_php['max_fsize'], $CONFIG['max_upl_size']),
        array(
$lang_upload_php['album'], 'album'2),
        array(
'MAX_FILE_SIZE'$max_file_size4),
        array(
$lang_upload_php['picture'], 'userpicture'11),
        array(
$lang_upload_php['pic_title'], 'title'02551),
        array(
$captionLabel'caption'3$CONFIG['max_img_desc_length']),
        array(
$lang_upload_php['keywords'], 'keywords'02551),
        array(
'event''picture'4)
        );

        if(!empty(
$CONFIG['user_field1_name'])) {
            
$form_array[] = array($CONFIG['user_field1_name'], 'user1'02551);
        }

        if(!empty(
$CONFIG['user_field2_name'])) {
            
$form_array[] = array($CONFIG['user_field2_name'], 'user2'02551);
        }

        if(!empty(
$CONFIG['user_field3_name'])) {
            
$form_array[] = array($CONFIG['user_field3_name'], 'user3'02551);
        }

        if(!empty(
$CONFIG['user_field4_name'])) {
            
$form_array[] = array($CONFIG['user_field4_name'], 'user4'02551);
        }

    } else {

        
// Check for valid form number.
        
if ((USER_UPLOAD_FORM >= '0') and (USER_UPLOAD_FORM <= '7')) {

            
// Create form array, and insert MAX_FILE_SIZE control.
            
$form_array[] = array('MAX_FILE_SIZE'$max_file_size);

            
// Add each upload type depending on the form number,
            
if((USER_UPLOAD_FORM == '1') or (USER_UPLOAD_FORM == '3') or (USER_UPLOAD_FORM == '4') or (USER_UPLOAD_FORM == '6')) {

                if(
$num_file_boxes 0) {

                    
$form_array[] = $lang_upload_php['reg_instr_7'];

                    
$form_array[] = array('''file_upload_array[]'1$num_file_boxes);

                }

            }

            if((
USER_UPLOAD_FORM == '2') or (USER_UPLOAD_FORM == '3') or (USER_UPLOAD_FORM == '5') or (USER_UPLOAD_FORM == '6')) {

                if(
$num_URI_boxes 0) {

                    
$form_array[] = $lang_upload_php['reg_instr_8'];

                    
$form_array[] = array('''URI_array[]'0256$num_URI_boxes);

                }

            }

            if((
USER_UPLOAD_FORM == '4') or (USER_UPLOAD_FORM == '5') or (USER_UPLOAD_FORM == '6') or (USER_UPLOAD_FORM == '7')) {

                
$form_array[] = $lang_upload_php['reg_instr_6'];
                
$form_array[] = array('''ZIP_array[]'11);

            }

            
// Add the control device.
            
$form_array[] = array('control''phase_1'4);

        } else {

            
// Unknown form number.
            
cpg_die(ERROR$lang_upload_php['reg_instr_1'], __FILE____LINE__);

        }

    }

    
// Create the form.
    
create_form($form_array);



    
// Close the form.
    
if (USER_UPLOAD_FORM == '0') {

        
// The user has the single upload only form. Select proper language for button.
        
close_form($lang_upload_php['title']);

    } else {

        
// Make button say 'Continue.'
        
close_form($lang_continue);

    }

    
// Close the table, create footers, and flush the output buffer.
    
endtable();
    echo 
"</form>";
    
pagefooter();
    
ob_end_flush();

    
// Exit the script.

    
exit;

}

// Recieve incoming file uploads for phase I.

if ((isset($_POST['control'])) and ($_POST['control'] == 'phase_1')) {

    
// $_FILES['file_upload_array']['name'][$counter]
    // $_FILES['file_upload_array']['size'][$counter]
    // $_FILES['file_upload_array']['tmp_name'][$counter]
    // $_FILES['file_upload_array']['type'][$counter]
    // $_FILES['file_upload_array']['error'][$counter]
    //
    // Error values:
    // 0 - No error
    // 1 - Exceeded filesize allowed in php.ini
    // 2 - Exceeded filesize allowed by HTML MAX_FILE_SIZE
    // 3 - Only a partial upload
    // 4 - No upload occurred.

    
$file_upload_count count($_FILES['file_upload_array']['name']);

    if (
$file_upload_count 0) {


        
// Check for error code support. Set the error code.

        
if (count($_FILES['file_upload_array']['error']) == 0) {

            
// This version of PHP does not support error codes (PHP < 4.2.0).  Create our own error code.

            
$error_code 'default';

        } else {

            
// We have error support.
            
$error_support 'TRUE';

        }

        for (
$counter 0$counter $file_upload_count$counter++) {

            
// Check for error code support. Set the error code.

            
if ($error_support) {

                
$error_code $_FILES['file_upload_array']['error'][$counter];

            }

            
// Create the failure ordinal for ordering the report of failed uploads.

            
$failure_cardinal $counter 1;

            
$failure_ordinal ''.$failure_cardinal.'. ';

            
// If there is no file name, make a dummy name for the error reporting system.

            
if (($_FILES['file_upload_array']['name'][$counter] == '')) {

                
$file_name 'filename_unavailable';

            } else {

                
$file_name $_FILES['file_upload_array']['name'][$counter];

            }

            
// Test for a blank file upload box.
            
if (empty($_FILES['file_upload_array']['tmp_name'][$counter])) {

                
// There is no need for further tests or action as there was no uploaded file, so skip the remainder of the iteration.
                
continue;

            }

            
// Check to make sure the file was uploaded via POST.

            
if (!is_uploaded_file($_FILES['file_upload_array']['tmp_name'][$counter])) {

                
// We reject the file, and make a note of the error.
                
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['no_post']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;
            }

            
// Check filename and extension:

            // Check that the file uploaded has a valid name and extension, and replace forbidden chars with underscores.

                    // Initialise the $matches array.
                    
$matches = array();

                    
// If magic quotes is on, remove the slashes it added to the file name.
                    
if (get_magic_quotes_gpc()) $_FILES['file_upload_array']['name'][$counter] = stripslashes($_FILES['file_upload_array']['name'][$counter]);

                    
// Create the holder $picture_name by translating the file name. Translate any forbidden character into an underscore.
                    
$picture_name replace_forbidden($_FILES['file_upload_array']['name'][$counter]);

                    
// Analyze the file extension using regular expressions.
                    
if (!preg_match("/(.+)\.(.*?)\Z/"$picture_name$matches)) {

                        
// The file name is invalid.
                        
$matches[1] = 'invalid_fname';

                        
// Make a bogus file extension to trigger Coppermine's defenses.
                        
$matches[2] = 'xxx';
                    }

                    
// If there is no extension, or if the extension is unknown/not permitted by Coppermine, zap the intruder.
                    
if ($matches[2] == '' || !is_known_filetype($matches)) {

                        
// We reject the file, and make a note of the error.
                        
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['forb_ext']);

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    }

            
// Check for upload errors.

            
if (!($error_code == '0') and !($error_code == 'default')) {

                
// PHP has detected a file upload error.
                
if ($error_code == '1') {
                    
$error_message $lang_upload_php['exc_php_ini'];
                } elseif (
$error_code == '2') {
                    
$error_message $lang_upload_php['exc_file_size'];
                } elseif (
$error_code == '3') {
                    
$error_message $lang_upload_php['partial_upload'];
                } elseif (
$error_code == '4') {
                    
$error_message $lang_upload_php['no_upload'];
                } else {
                    
$error_message $lang_upload_php['unknown_code'];
                }

                
//Make a note in the error array.
                
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$error_message);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            } elseif (
$_FILES['file_upload_array']['tmp_name'][$counter] == '') {

                
// There is no temporary file, so the file did not upload. Make a note of it in the file_failure_arrray and flip the failure switch to generate the ordinal. .

                
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['no_temp_name']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            } elseif (
$_FILES['file_upload_array']['size'][$counter] <= 0) {

                
// The file contains no data or was corrupted. Make a note of it in the error array.

                
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['no_file_size']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            } elseif (
$_FILES['file_upload_array']['size'][$counter] > $max_file_size) {

                
// The file exceeds the amount specified by the max upload directive. Either the browser is stupid, or somebody isn't playing nice. (Ancient browser - MAX_UPLOAD forgery)

                
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['exc_file_size']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            }

            
// Now we need to move the file into the /edit directory.

            // We need specify the path for the transitory file.

            // Create a prefix for easier human recognition.
            
$prefix "mHTTP_temp_";

            
//Set the correct file extension.

            
$suffix $matches[2];

            
// Generate the unique name. Keep generating new names until one that is not in use is found.

            
do {

                
// Create a random seed by taking the first 8 characters of an MD5 hash of a concatenation of the current UNIX epoch time and the current server process ID.
                
$seed substr(md5(uniqid("")), 08);

                
// Assemble the file path.
                
$path_to_image './'.$CONFIG['fullpath'].'edit/'$prefix $seed '.' $suffix;

            } while (
file_exists($path_to_image));

            
// Create a holder called $tempname.
            
$tempname $prefix $seed '.' $suffix;

            
//Now we upload the file.
            
if (!(move_uploaded_file($_FILES['file_upload_array']['tmp_name'][$counter], $path_to_image))) {

                
// The file upload has failed.

                
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['impossible']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            }

            
// Change file permission
            
@chmod($path_to_imageoctdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled

            // Create a testing alias.
            
$picture_alias $matches[1].".".$matches[2];

            
// Check to see if the filename is consistent with that of a picture.
            
if (is_image($picture_alias)) {

                
// If it is, get the picture information
                
$imginfo getimagesize($path_to_image);

                
// If getimagesize does not recognize the file as a picture, delete the picture.
                
if ($imginfo === 'FALSE') {
                    @
unlink($path_to_image);

                    
// The file upload has failed -- the image is not an image or it is corrupt.
                    
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['not_image']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                
// JPEG and PNG only are allowed with GD. If the image is not allowed for GD,delete it.
                //} elseif ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && ($CONFIG['thumb_method'] == 'gd1' || $CONFIG['thumb_method'] == 'gd2')) {
                
} elseif ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && $CONFIG['GIF_support'] == 0) {
                    @
unlink($path_to_image);

                    
// The file upload has failed -- the image is not allowed with GD.
                    
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['not_GD']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                
// Check that picture size (in pixels) is lower than the maximum allowed. If not, delete it.
                
} elseif (max($imginfo[0], $imginfo[1]) > $CONFIG['max_upl_width_height']) {
                  if ((
USER_IS_ADMIN && $CONFIG['auto_resize'] == 1) || (!USER_IS_ADMIN && $CONFIG['auto_resize'] > 0)) //($CONFIG['auto_resize']==1)
                  
{
                    
//resize_image($uploaded_pic, $uploaded_pic, $CONFIG['max_upl_width_height'], $CONFIG['thumb_method'], $imginfo[0] > $CONFIG['max_upl_width_height'] ? 'wd' : 'ht');
                    
resize_image($uploaded_pic$uploaded_pic$CONFIG['max_upl_width_height'], $CONFIG['thumb_method'], $CONFIG['thumb_use']);
                  }
                  else
                  {
                    @
unlink($path_to_image);

                    
// The file upload has failed -- the image dimensions exceed the allowed amount.
                    
$file_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'file_name'=> $file_name'error_code'=>$lang_upload_php['pixel_allowance']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;
                  }
                }

            
// Image is ok
            
}

            
// Put array info for a successful upload in $escrow_array. Hold the actual file name and the name of the temporary image. We do not use the path for security reasons.
            
$escrow_array[] = array('actual_name'=>$picture_alias'temporary_name'=>$tempname);

        } 
// end for loop
    
// end if statement

    // Count the number of items in the URI array.
    
$URI_upload_count count($_POST['URI_array']);

    if (
$URI_upload_count 0) {

        for (
$counter 0$counter $URI_upload_count$counter++) {

            
// Create the failure ordinal for ordering the report of failed uploads.

            
$failure_cardinal $counter 1;

            
$failure_ordinal ''.$failure_cardinal.'. ';

            
// Initialize the $URI_MIME_type variable.
            
$URI_MIME_type "0";

            
// Check to make sure the URI box was not blank.
            
if (empty($_POST['URI_array'][$counter])) {

                
// The box was empty.
                // There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            }

            
// Check for magic quotes and remove slashes if necessary.
            
if (get_magic_quotes_gpc()) {

                
$_POST['URI_array'][$counter] = stripslashes($_POST['URI_array'][$counter]);

            }

            
// Remove excess whitespace.
            
$_POST['URI_array'][$counter] = trim($_POST['URI_array'][$counter]);

            
// Translate any interior spaces into hex replacements.
            
$_POST['URI_array'][$counter] = strtr($_POST['URI_array'][$counter], array(" "=>"%20"));

            
// We do some validation for the URI. First we check for http:// or ftp:// at the start of the URI.
            
if(!ereg('^http://|^ftp://',$_POST['URI_array'][$counter])) {

                
// The URL is malformed or not allowed in Coppermine. Note an error.
                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['incorrect_prefix']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;

            }

            
// To obtain the file name, we explode the URI into $pieces.
            
$pieces explode('/',$_POST['URI_array'][$counter]);

            
// We pop off the end of the $pieces array to obtain the possible file name.
            
$possible_file_name array_pop($pieces);

            
// Strip the hex equivalent for spaces from the possible file name and restore the spaces.
            
$possible_file_name strtr($possible_file_name, array("%20"=>" "));

            
// Check possible filename and extension:

            // Check that the possible file name has a valid name and extension, and replace forbidden chars with underscores.

            // Initialise the $matches array.
            
$matches = array();

            
// Create the holder $picture_name by translating the possible file name. Translate any forbidden character into an underscore.
            
$picture_name replace_forbidden($possible_file_name);

            
// Analyze the file extension using regular expressions.
            
if (!preg_match("/(.+)\.(.*?)\Z/"$picture_name$matches)) {

                
// The file name is invalid.
                
$matches[1] = 'invalid_fname';

                
// Make a bogus file extension to tell Coppermine to use a different name.
                
$matches[2] = 'xxx';
            }

            
// Set the variable $extension equal to $matches[2].
            
$extension $matches[2];

            
// If there is no extension, or if the extension is unknown/not permitted by Coppermine, attenpt to detect a MIME type.
            
if ($matches[2] == '' || !is_known_filetype($matches)) {

                
// Check for stream_get_meta_data support.
                
if (!function_exists('stream_get_meta_data')) {

                    
// We cannot get the header information for the file, so we reject the URI as unsafe.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['unsafe_URI']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                }

                
// Open a stream to the resource.
                
$fp fopen($_POST['URI_array'][$counter],"rb");

                
// Check to see if the resource was opened.
                
if (!$fp) {

                    
// Attempt to get the status of the resource.
                    
$response check_status($_POST['URI_array'][$counter]);

                    
// Try to parse header if we were able to get a response.
                    
if ($response) {

                        if(
strstr($response'401')) {

                            
// 401 Unauthorized - Authorization needed to obtain resource. Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_401']);

                        } elseif(
strstr($response'402')) {

                            
// 402 Payment Required -  Where's the cash? :-) Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_402']);

                        } elseif(
strstr($response'403')) {

                            
// 403 Forbidden - No permission to access the resource. Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_403']);

                        } elseif(
strstr($response'404')) {

                            
// 404 Not Found - The resource is missing. Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_404']);

                        } elseif(
strstr($response'500')) {

                            
// 500 Internal Server Error - The server has failed.  Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_500']);

                        } elseif(
strstr($response'503')) {

                            
// 503 Service Unavailable - The server is busy.  Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_503']);

                        } else {

                            
// Undocumented error. Note an error. Return status code.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$response);

                        }

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    } else {


                        
// The resource could not be opened.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['could_not_open_URI']);

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    }

                } else {

                    
$header stream_get_meta_data($fp);

                    if(
$header === 'FALSE') {

                        
// We could not get the meta data from the header. We must reject the URI as unsafe.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['meta_data_failure']);

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    }

                    
//Look for server response. Proceed if status code 200 is returned.
                    
if(!(strstr($header['wrapper_data'][0], '200'))) {

                        
// The resource is not available. Attempt to determine why, and
                        // generate the appropriate error.

                        
if(strstr($header['wrapper_data'][0], '401')) {

                            
// 401 Unauthorized - Authorization needed to obtain resource. Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_401']);

                        } elseif(
strstr($header['wrapper_data'][0], '402')) {

                            
// 402 Payment Required -  Where's the cash? :-) Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_402']);

                        } elseif(
strstr($header['wrapper_data'][0], '403')) {

                            
// 403 Forbidden - No permission to access the resource. Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_403']);

                        } elseif(
strstr($header['wrapper_data'][0], '404')) {

                            
// 404 Not Found - The resource is missing. Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_404']);

                        } elseif(
strstr($header['wrapper_data'][0], '500')) {

                            
// 500 Internal Server Error - The server has failed.  Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_500']);

                        } elseif(
strstr($header['wrapper_data'][0], '503')) {

                            
// 503 Service Unavailable - The server is busy.  Note an error.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_503']);

                        } else {

                            
// Undocumented error. Note an error. Return status code.
                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$header['wrapper_data'][0]);

                        }

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    }

                    
// Cycle through returned HTTP header. Look for the MIME type, which we will use to create a proper file extension.

                    
if (count($header['wrapper_data']) < 2) {

                        
// We could not get the meta data from the header. We must reject the URI as unsafe.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['meta_data_failure']);

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    } else {

                        
// Now we loop through each item returned in the wrapper data.
                        
for ($i=1; isset($header['wrapper_data'][$i]); $i++) {

                            
// We test each array element to see if it contains the content-type header.
                            
if (strstr(strtolower($header['wrapper_data'][$i]), 'content-type')) {

                                
// If we find it, we have found the MIME type.  Use regular expressions to extract it.
                                
if(!(eregi('^content-type: ([[:graph:]]+)'$header['wrapper_data'][$i], $MIME_extraction_array))) {

                                    
// We could not find a MIME type. Note an error and reject the URI as unsafe.
                                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['MIME_extraction_failure']);

                                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                                    
continue 2;

                                } else {

                                    
// We have found the MIME type, which we will store in $URI_MIME_type.
                                    
$URI_MIME_type $MIME_extraction_array[1];
                                }

                            
// While we are at it, let's see if we can get a content length from the server.
                            
} elseif (strstr(strtolower($header['wrapper_data'][$i]), 'content-length')) {

                                
// We have found the Content-Length header.  Use regular expressions to extract it.
                                
if(eregi('^content-length: ([[:digit:]]+)'$header['wrapper_data'][$i], $length_extraction_array)) {

                                    
// The content length should be available in bytes.  Cross compare with the maximum file size allowed in an upload.
                                    // Reject the file with an error if it is too large.
                                    
if ($length_extraction_array[1] > $max_file_size) {

                                        
// The content is too large. Reject it with an error.
                                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['exc_file_size']);

                                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                                        
continue 2;

                                    }

                                }

                            }

                        }

                    }

                    
// Close the file pointer if we were able to open it.
                    
fclose($fp);

                }

            }

            
// Check to see if MIME type was detected.
            
if($URI_MIME_type) {

                
// A mime type was detected.  Determine the appropriate file extension for the MIME type.
                // We will hard code the most common GD compatible image MIME types to reduce calls to the DB.
                
if(($URI_MIME_type == 'image/jpeg') or ($URI_MIME_type == 'image/jpg')) {

                    
//The file will need a .jpg extension.
                    
$extension 'jpg';

                } elseif (
$URI_MIME_type == 'image/png') {

                    
//The file will need a .png extension.
                    
$extension 'png';

                } elseif (
$URI_MIME_type == 'image/gif') {

                    
//The file will need a .gif extension.
                    
$extension 'gif';

                } else {
                
                        
$extension '';
                        
                        foreach (
$FILE_TYPES as $ext => $typedata){
                        
                            if (
$typedata['mime'] == $URI_MIME_type){
                                 
// Store the extension in $extension.
                                
$extension $ext;
                                break;
                            }
                            
                        }
                        
                        if (!
$extension){
                        
                        
// We cannot determine an extension from the MIME type provided, so note an error. Reject the file as unsafe.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['MIME_type_unknown']);
  
                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                      
continue;
                     
                        }

                }

            }

            
//Now we must create the temporary file name.  This will be the permanent file name if MIME type detection was used to establish the extension.

            // First, we create a prefix for easier human recognition.
            
$prefix "mURI_temp_";

            
//Set the correct file extension.

            
$suffix $extension;

            
// Generate the unique name. Keep generating new names until one that is not in use is found.

            
do {

                
// Create a random seed by taking the first 8 characters of an MD5 hash of a concatenation of the current UNIX epoch time and the current server process ID.
                
$seed substr(md5(uniqid("")), 08);

                
// Assemble the file path.
                
$path_to_image './'.$CONFIG['fullpath'].'edit/'$prefix $seed '.' $suffix;

            } while (
file_exists($path_to_image));

            
// Create a holder called $tempname.
            
$tempname $prefix $seed '.' $suffix;

            
// The file name $path_to_image has been created. We must prepare to download the resource. First, we will attemt to detect the status code for the resource.

            // Open a stream to the resource.
            
$fp fopen($_POST['URI_array'][$counter],"rb");

            
// Check to see if the resource was opened.
            
if (!$fp) {

                
// Attempt to get the status of the resource.
                
$response check_status($_POST['URI_array'][$counter]);

                
// Try to parse header if we were able to get a response.
                
if ($response) {

                    if(
strstr($response'401')) {

                        
// 401 Unauthorized - Authorization needed to obtain resource. Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_401']);

                    } elseif(
strstr($response'402')) {

                        
// 402 Payment Required -  Where's the cash? :-) Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_402']);

                    } elseif(
strstr($response'403')) {

                        
// 403 Forbidden - No permission to access the resource. Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_403']);

                    } elseif(
strstr($response'404')) {

                        
// 404 Not Found - The resource is missing. Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_404']);

                    } elseif(
strstr($response'500')) {

                        
// 500 Internal Server Error - The server has failed.  Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_500']);

                    } elseif(
strstr($response'503')) {

                        
// 503 Service Unavailable - The server is busy.  Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_503']);

                    } else {

                        
// Undocumented error. Note an error. Return status code.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$response);

                    }

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                } else {


                    
// The resource could not be opened.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['could_not_open_URI']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                }

            } else {

                
// Obtain header info if possible.
                
if (function_exists('stream_get_meta_data')) {

                    
// Store header data in $header.
                    
$header stream_get_meta_data($fp);

                    
// If data was returned, test it.
                    
if($header) {

                        
//Look for server response. Proceed if status code 200 is returned.
                        
if(!(strstr($header['wrapper_data'][0], '200'))) {

                            
// The resource is not available. Attempt to determine why, and
                            // generate the appropriate error.

                            
if(strstr($header['wrapper_data'][0], '401')) {

                                
// 401 Unauthorized - Authorization needed to obtain resource. Note an error.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_401']);

                            } elseif(
strstr($header['wrapper_data'][0], '402')) {

                                
// 402 Payment Required -  Where's the cash? :-) Note an error.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_402']);

                            } elseif(
strstr($header['wrapper_data'][0], '403')) {

                                
// 403 Forbidden - No permission to access the resource. Note an error.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_403']);

                            } elseif(
strstr($header['wrapper_data'][0], '404')) {

                                
// 404 Not Found - The resource is missing. Note an error.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_404']);

                            } elseif(
strstr($header['wrapper_data'][0], '500')) {

                                
// 500 Internal Server Error - The server has failed.  Note an error.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_500']);

                            } elseif(
strstr($header['wrapper_data'][0], '503')) {

                                
// 503 Service Unavailable - The server is busy.  Note an error.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['http_503']);

                            } else {

                                
// Undocumented error. Note an error. Return status code.
                                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$header['wrapper_data'][0]);

                            }

                            
// There is no need for further tests or action, so skip the remainder of the iteration.
                            
continue;

                        }

                        
// Cycle through returned HTTP header.
                        
if (count($header['wrapper_data']) > 1) {

                            
// Now we loop through each item returned in the wrapper data.
                            
for ($i=1; isset($header['wrapper_data'][$i]); $i++) {

                                
// Let's see if we can get a content length from the server.
                                
if (strstr(strtolower($header['wrapper_data'][$i]), 'content-length')) {

                                    
// We have found the Content-Length header.  Use regular expressions to extract it.
                                    
if(eregi('^content-length: ([[:digit:]]+)'$header['wrapper_data'][$i], $length_extraction_array)) {

                                        
// The content length should be available in bytes.  Cross compare with the maximum file size allowed in an upload.
                                        // Reject the file with an error if it is too large.
                                        
if ($length_extraction_array[1] > $max_file_size) {

                                            
// The content is too large. Reject it with an error.
                                            
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['exc_file_size']);

                                            
// There is no need for further tests or action, so skip the remainder of the iteration.
                                            
continue 2;

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

                
// Cannot get headers from meta data, or we have completed the metadata check and have found a 200 status code and appropriate content length.

                // Now we must create a file to write the data to.
                
touch($path_to_image);

                
// Conduct tests on write file.
                
if (!is_file($path_to_image)) {

                    
// The file was not created. Note an error.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['cant_create_write']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                
// Check for writability,
                
} elseif (!is_writable($path_to_image)) {

                    
// The file is not writeable. Note an error.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['not_writable']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                } else {

                    
// Initialize the $content variable.
                    
$content '';

                    
// The write file has been created and is writeable.  Let's get the content from the URI.
                    
while (!feof($fp)) {

                        
// Read the data into $content in 8KB chunks.
                        
$content .= fread($fp,8192);

                    }

                    
// Close the file pointer now that we are done reading it.
                    
fclose($fp);

                    
// Open the temp file for writing.
                    
$fpw fopen($path_to_image"wb");

                    
// Verify the file has opened.
                    
if (!$fpw) {

                        
// The file did not open. Make a note of the error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['cant_open_write_file']);

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    }

                    
// Write the data to the file.
                    
if (fwrite($fpw$contentstrlen($content)) === 'FALSE') {

                        
// We could not write the data to the file. Note an error.
                        
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['cant_write_write_file']);

                        
// There is no need for further tests or action, so skip the remainder of the iteration.
                        
continue;

                    }

                    
// The file now resides on the server. Let's close the write file.
                    
fclose($fpw);

                }

            }

            
// The file is located at $path_to_image.  We now need to continue with on server testing.

            // Change file permission
            
@chmod($path_to_imageoctdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled

            // Create a testing alias. Use the temp name if a MIME type eas discovered.
            
if ($URI_MIME_type) {

                
// The MIME type eas detected, so we use the temp name.
                
$picture_alias $tempname;

            } else {

                
$picture_alias $matches[1].".".$matches[2];

            }

            
// Test file size. Delete if too large.
            
if (filesize($path_to_image) > $max_file_size) {

                
// The file size is too large, delete it.
                
@unlink($uploaded_pic);

                
// The file upload has failed -- the file is too large. make a note of the error.
                
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['exc_file_size']);

                
// There is no need for further tests or action, so skip the remainder of the iteration.
                
continue;
            }

            
// Check to see if the filename is consistent with that of a picture.
            
if (is_image($picture_alias)) {

                
// If it is, get the picture information
                
$imginfo getimagesize($path_to_image);

                
// If getimagesize does not recognize the file as a picture, delete the picture.
                
if ($imginfo === 'FALSE') {
                    @
unlink($path_to_image);

                    
// The file upload has failed -- the image is not an image or it is corrupt.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['not_image']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                
// JPEG and PNG only are allowed with GD. If the image is not allowed for GD,delete it.
                //} elseif ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && ($CONFIG['thumb_method'] == 'gd1' || $CONFIG['thumb_method'] == 'gd2')) {
                
} elseif ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && $CONFIG['GIF_support'] == 0) {
                    @
unlink($path_to_image);

                    
// The file upload has failed -- the image is not allowed with GD.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['not_GD']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;

                
// Check that picture size (in pixels) is lower than the maximum allowed. If not, delete it.
                
} elseif (max($imginfo[0], $imginfo[1]) > $CONFIG['max_upl_width_height']) {
                    @
unlink($path_to_image);

                    
// The file upload has failed -- the image dimensions exceed the allowed amount.
                    
$URI_failure_array[] = array( 'failure_ordinal'=>$failure_ordinal'URI_name'=> $_POST['URI_array'][$counter], 'error_code'=>$lang_upload_php['pixel_allowance']);

                    
// There is no need for further tests or action, so skip the remainder of the iteration.
                    
continue;
                }

            
// Image is ok
            
}

            
// Put array info for a successful upload in $escrow_array. Array hold the actual file name and the name of the temporary image. We do not use the path for security reasons.
            
$escrow_array[] = array('actual_name'=>$picture_alias'temporary_name'=>$tempname);

        }

    }

    
// Decompressive ZIP uploading is disabled.
    // $zip_upload_count = count($_FILES['ZIP_array']['name']);

    //Now we must prepare the inital form for adding the pictures to the database, and we must move them to their final location.

    // Count errors in each error array and the escrow array.
    
$escrow_array_count count($escrow_array);
    
$file_error_count count($file_failure_array);
    
$URI_error_count count($URI_failure_array);
    
$zip_error_count count($zip_failure_array);

    
// Create page header.
    
pageheader($lang_upload_php['title']);

    
// Check for successful uploads.
    
if ($escrow_array_count '0') {

        
// Serialize and base64_encode the array.
        
$cayman_escrow base64_encode(serialize($escrow_array));

        
// Add temp data record to database.
        
$unique_ID create_record($cayman_escrow);

        
// Verify record was created.
        
if (!$unique_ID) {

            
cpg_die(CRITICAL_ERROR$lang_upload_php['cant_create_write'], __FILE____LINE__);

        }

        
// Prepare success data for user.
        
open_form($_SERVER['PHP_SELF']); // Set the form action to this script.
        
starttable("100%"$lang_upload_php['succ'], 2);
        echo 
"<tr><td colspan=\"2\">";
        
printf ($lang_upload_php['success'], $escrow_array_count);
        echo 
"<br /><br />";
        echo 
$lang_upload_php['add'];
        echo 
"</td></tr>";

        
$form_array = array(
             array(
'unique_ID'$unique_ID4),
             array(
'control''phase_2'4)
        );

        
create_form($form_array);
        
close_form($lang_continue);
        
endtable();

        
// Throw in an HTML break for aesthetics.
        
echo "<br />";

    } else {

        
// we had no successful uploads. We create a redirect box.
        
msg_box($lang_infosprintf($lang_upload_php['success'], $escrow_array_count), $lang_continue'index.php'"100%");

        
// Throw in an HTML break for aesthetics.
        
echo "<br />";

    }

    
// Create error report if we have errors.
    
if (($file_error_count $URI_error_count $zip_error_count) > 0) {

        
// Prepare error data for user.
        
starttable("100%"$lang_upload_php['error_report'], 2);
        
form_statement($lang_upload_php['error_instr']);

        
// Look for file upload errors.
        
if ($file_error_count 0) {

            
// There are file upload errors. Generate the section label.
            
form_label($lang_upload_php['reg_instr_7']);
            echo 
"<tr><td>{$lang_upload_php['file_name_url']}</td><td>{$lang_upload_php['error_message']}</td></tr>";

            
// Cycle through the file upload errors.
            
for ($i=0$i $file_error_count$i++) {

                
// Print the error ordinal, file name, and error code.
                
echo "<tr><td>{$file_failure_array[$i]['failure_ordinal']} {$file_failure_array[$i]['file_name']}</td><td>{$file_failure_array[$i]['error_code']}</td></tr>";

            }

        }

        
// Look for URI upload errors.
        
if ($URI_error_count 0) {

            
// There are URI upload errors. Generate the section label.
            
form_label($lang_upload_php['reg_instr_8']);
            echo 
"<tr><td>{$lang_upload_php['file_name_url']}</td><td>{$lang_upload_php['error_message']}</td></tr>";

            
// Cycle through the file upload errors.
            
for ($i=0$i $URI_error_count$i++) {

                
// Print the error ordinal, file name, and error code.
                
echo "<tr><td>{$URI_failure_array[$i]['failure_ordinal']} {$URI_failure_array[$i]['URI_name']}</td><td>{$URI_failure_array[$i]['error_code']}</td></tr>";

            }

        }

        
// Look for zip upload errors.
        
if ($zip_error_count 0) {

            
// There are file upload errors. Generate the section label.
            
form_label($lang_upload_php['reg_instr_6']);
            echo 
"<tr><td>{$lang_upload_php['file_name_url']}</td><td>{$lang_upload_php['error_message']}</td></tr>";

            
// Cycle through the file upload errors.
            
for ($i=0$i $zip_error_count$i++) {

                
// Print the error ordinal, file name, and error code.
                
echo "<tr><td>{$file_failure_array[$i]['failure_ordinal']} {$file_failure_array[$i]['file_name']}</td><td>{$file_failure_array[$i]['error_code']}</td></tr>";

            }

        }

        
// Close the error report table.
        
endtable();

    }

    
// Create the footer and flush the output buffer.
    
echo "</form>";
    
pagefooter();
    
ob_end_flush();

    
// Exit the script.

    
exit;
}

// Recieve incoming post information for phase II.
if ((isset($_POST['control'])) and ($_POST['control'] == 'phase_2')) {

    
// Check for incoming album placement data.
    
if ((isset($_POST['album'])) and (isset($_POST['unique_ID']))) {

            
// Check if user selected an album to upload picture to. If not, die with error.
        // added by frogfoot
        
$album = (int)$_POST['album'];
        if (!
$album){
                        
cpg_die(ERROR$lang_db_input_php['album_not_selected'], __FILE____LINE__);
                }

        if (isset(
$_POST['unique_ID'])) {

            
// The unique ID is set, so let us retrieve the record.
            
$cayman_string retrieve_record($_POST['unique_ID']);

            
// Verify record was retrieved.
            
if (!$cayman_string) {

                
cpg_die(CRITICAL_ERROR$lang_errors['param_missing'], __FILE____LINE__);

            }

        } else {

            
// The $_POST['unique_ID'] value is not present.  Die with an error.
            
cpg_die(CRITICAL_ERROR$lang_errors['param_missing'], __FILE____LINE__);

        }

        
// Now we decode the string.
        
$escrow_array unserialize(base64_decode($cayman_string));

        
// Now we need to pop a file set off $escrow_array.
        // The returned element will take the form: array('actual_name', 'temporary_name')

        // First, we test to make sure $escrow_array is an array.
        
if (!(is_array($escrow_array))) {

            
// The decoded information is not an array. Die with an error.
            
cpg_die(CRITICAL_ERROR$lang_errors['param_missing'], __FILE____LINE__);

        }

        
// Initialize $file_set as an array.
        
$file_set = array();

        
// Create array index.
        
$index count($escrow_array) - 1;

        
// Read the end of the $escrow_array array into $file_set.
        
$file_set[0] = $escrow_array[$index]['actual_name'];
        
$file_set[1] = $escrow_array[$index]['temporary_name'];

        
// Get the image preview path.
        
$preview_path $escrow_array[$index]['preview_path'];

        
// Remove end of $escrow_array.
        
unset($escrow_array[$index]['preview_path']);
        unset(
$escrow_array[$index]['actual_name']);
        unset(
$escrow_array[$index]['temporary_name']);
        unset(
$escrow_array[$index]);

        
// Re-encode the $escrow_array.
        
$cayman_escrow base64_encode(serialize($escrow_array));

        
// Update the record.
        
$update update_record($_POST['unique_ID'], $cayman_escrow);

        
// Verify that the update occurred.
        
if (!$update) {

            
// We cannot write to the temporary data file. Note a fatal error.
            
cpg_die(CRITICAL_ERROR$lang_upload_php['not_writable'], __FILE____LINE__);

        }

        
// We have incoming placement data. Let's capture it.

        
$album = (int)$_POST['album'];
        
$title addslashes($_POST['title']);
        
$caption addslashes($_POST['caption']);
        
$keywords addslashes($_POST['keywords']);
        
$user1 addslashes($_POST['user1']);
        
$user2 addslashes($_POST['user2']);
        
$user3 addslashes($_POST['user3']);
        
$user4 addslashes($_POST['user4']);

        
// Capture movie or audio width and height if sent.
        
if(isset($_POST['movie_wd'])) {

            
$movie_wd = (int)$_POST['movie_wd'];

        } else {

            
$movie_wd 320;

        }

        if(isset(
$_POST['movie_ht'])) {

            
$movie_ht = (int)$_POST['movie_ht'];

        } else {

            
$movie_ht 240;

        }

        
// Check if the album id provided is valid
        
if (!GALLERY_ADMIN_MODE) {
            
$result cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid='$album' and (uploads = 'YES' OR category = '" . (USER_ID FIRST_USER_CAT) . "')");
            if (
mysql_num_rows($result) == 0)cpg_die(ERROR$lang_db_input_php['unknown_album'], __FILE____LINE__);
            
$row mysql_fetch_array($result);
            
mysql_free_result($result);
            
$category $row['category'];
        } else {
            
$result cpg_db_query("SELECT category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid='$album'");
            if (
mysql_num_rows($result) == 0)cpg_die(ERROR$lang_db_input_php['unknown_album'], __FILE____LINE__);
            
$row mysql_fetch_array($result);
            
mysql_free_result($result);
            
$category $row['category'];
        }

        
// Pictures are moved in a directory named 10000 + USER_ID
        
if (USER_ID && !defined('SILLY_SAFE_MODE')) {
            
$filepath $CONFIG['userpics'] . (USER_ID FIRST_USER_CAT);
            
$dest_dir $CONFIG['fullpath'] . $filepath;
            if (!
is_dir($dest_dir)) {
                
mkdir($dest_diroctdec($CONFIG['default_dir_mode']));
                if (!
is_dir($dest_dir)) cpg_die(CRITICAL_ERRORsprintf($lang_db_input_php['err_mkdir'], $dest_dir), __FILE____LINE__true);
                @
chmod($dest_diroctdec($CONFIG['default_dir_mode'])); //silence the output in case chmod is disabled
                
$fp fopen($dest_dir '/index.html''w');
                
fwrite($fp' ');
                
fclose($fp);
            }
            
$dest_dir .= '/';
            
$filepath .= '/';
        } else {
            
$filepath $CONFIG['userpics'];
            
$dest_dir $CONFIG['fullpath'] . $filepath;
        }

        
// Check that target dir is writable
        
if (!is_writable($dest_dir)) cpg_die(CRITICAL_ERRORsprintf($lang_db_input_php['dest_dir_ro'], $dest_dir), __FILE____LINE__true);

        
//Add the Perl regex to break the actual name.
        
preg_match("/(.+)\.(.*?)\Z/"$file_set[0], $matches);

        
// Create a unique name for the uploaded file
        
$nr 0;
        
$picture_name $matches[1] . '.' $matches[2];
        while (
file_exists($dest_dir $picture_name)) {
            
$picture_name $matches[1] . '~' $nr++ . '.' $matches[2];
        }

        
// Create path for final location.
        
$uploaded_pic $dest_dir $picture_name;

        
// Form path to temporary image.
        
$path_to_image './'.$CONFIG['fullpath'].'edit/'.$file_set[1];

                
// prevent moving the edit directory...
                
if (is_dir($path_to_image)) cpg_die(CRITICAL_ERROR$lang_upload_php['failure'] . " - '$path_to_image'"__FILE____LINE__true);

        
// Move the picture into its final location
        
if (rename($path_to_image$uploaded_pic)) {

            
// Change file permission
            
@chmod($uploaded_picoctdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled

            // Create thumbnail and intermediate image and add the image into the DB
            
$result add_picture($album$filepath$picture_name0,$title$caption$keywords$user1$user2$user3$user4$category$raw_ip$hdr_ip$movie_wd$movie_ht);

            if (!
$result) {

                
// The file could not be placed.
                
$file_placement 'no';

            } else {

                
// The file was placed successfully.
                
$file_placement 'yes';

            }

        } else {

            
// The file was not placed successfully.
            
$file_placement 'no';

        }

        
// Time for garbage cleanup.

        // First, we delete the preview image.
        
if ((!strstr($preview_path'thumb')) and (file_exists($preview_path))) {

            
unlink($preview_path);

        }

        
// Check to see if this is the last one.
        
if(count($escrow_array) == '0') {

            
// Create the final message.
            
if ($PIC_NEED_APPROVAL) {

                if (
$file_placement == 'no') {

                    
$final_message ''.$lang_upload_php['no_place'].'<br /><br />'.$lang_db_input_php['upload_success'];

                } else {

                    
$final_message ''.$lang_upload_php['yes_place'].'<br /><br />'.$lang_db_input_php['upload_success'];

                }

            } else {

                if (
$file_placement == 'no') {

                    
$final_message ''.$lang_upload_php['no_place'].'<br /><br />'.$lang_upload_php['process_complete'];

                } else {

                    
$final_message ''.$lang_upload_php['yes_place'].'<br /><br />'.$lang_upload_php['process_complete'];

                }

            }

            
// Delete the temporary data file.
            
delete_record($_POST['unique_ID']);

            
// Send e-mail notification to the admin if requested (added by gaugau: 03-11-09).
            
if (($CONFIG['upl_notify_admin_email']) and ($PIC_NEED_APPROVAL)) {
                
// Encapsulate so included lang file doesn't interfere with global one
                
function cpg_send_upload_notification() {
                    global 
$CONFIG;
                    
$lang_db_input_php cpg_get_default_lang_var('lang_db_input_php');
                    
// Get the mail files.
                    
include_once('include/mailer.inc.php');

                    
// Send the message.
                    
cpg_mail('admin'sprintf($lang_db_input_php['notify_admin_email_subject'], $CONFIG['gallery_name']), sprintf($lang_db_input_php['notify_admin_email_body'], USER_NAME,  $CONFIG['ecards_more_pic_target']. (substr$CONFIG["ecards_more_pic_target"], -1) == '/' '' '/') .'editpics.php?mode=upload_approval' ));
                }
                
cpg_send_upload_notification();
            }

            
// That was the last one. Create a redirect box.
            
pageheader($lang_info);
            
msg_box($lang_info$final_message$lang_continue'index.php'"100%");
            
pagefooter();

            
// Exit the script.
            
exit;

        }

    }

    
// The user has files that need to be processed and placed in albums.
    // We must pull that information from the temporary data file
    // whose ID is in $_POST['unique_ID'].

    
if (isset($_POST['unique_ID'])) {

            
// The unique ID is set, so let us retrieve the record.
            
$cayman_string retrieve_record($_POST['unique_ID']);

            
// Verify record was retrieved.
            
if (!$cayman_string) {

                
cpg_die(CRITICAL_ERROR$lang_errors['param_missing'], __FILE____LINE__);

            }

    } else {

        
// The $_POST['cayman'] path is not present.  Die with an error.
        
cpg_die(CRITICAL_ERROR$lang_errors['param_missing'], __FILE____LINE__);

    }

    
// Now we decode the string.
    
$escrow_array unserialize(base64_decode($cayman_string));

    
// Now we need to detect the end file set of $escrow_array.
    // The returned element will take the form: array('actual_name', 'temporary_name')

    // First, we test to make sure $escrow_array is an array.
    
if (!(is_array($escrow_array))) {

        
// The decoded information is not an array. Die with an error.
        
cpg_die(CRITICAL_ERROR$lang_errors['param_missing'], __FILE____LINE__);

    }

    
// Initialize $file_set as an array.
    
$file_set = array();

    
// Create array index.
    
$index count($escrow_array) - 1;

    
// Read the end of the $escrow_array array into $file_set.
    
$file_set[0] = $escrow_array[$index]['actual_name'];
    
$file_set[1] = $escrow_array[$index]['temporary_name'];

    
// Create preview image.

    // Create path to image.
    
$path_to_image './'.$CONFIG['fullpath'].'edit/'.$file_set[1];

    
// Create the preview function.

    // Get the extension for the preview.

    // First we parse the file name to determine the file type.
    
$pieces explode('.',$file_set[1]);

    
// We pop off the end of the $pieces array to obtain the possible file name.
    
$extension array_pop($pieces);

    
// Detect if the file is an image.
    
if(is_image($file_set[1])) {

        
// Create preview image file name.

        
do {

            
// Create a random seed by taking the first 8 characters of an MD5 hash of a concatenation of the current UNIX epoch time and the current server process ID.
            
$seed substr(md5(uniqid("")), 08);

            
// Assemble the file path.
            
$path_to_preview './'.$CONFIG['fullpath'].'edit/preview_' $seed '.' $extension;

        } while (
file_exists($path_to_preview));

        
// Create secure preview path.
        
$s_preview_path 'preview_' $seed '.' $extension;

        
// The file is an image, we must resize it for a preview image.
        
resize_image($path_to_image$path_to_preview'150'$CONFIG['thumb_method'], 'wd');

        if (
$CONFIG['read_iptc_data']) {
           
$iptc get_IPTC($path_to_image);
        }

    } else {

        
// The file is not an image, so we will use the non-image thumbs
        // for preview images.

        // We create the path to the preview image.
        
$path_to_preview "images/thumb_{$extension}.jpg";

    }

    
// Add preview image path to $escrow_array.
    
$escrow_array[$index]['preview_path'] = $path_to_preview;

    
// Re-encode the $escrow_array.
    
$cayman_escrow base64_encode(serialize($escrow_array));

    
// Update the record.
    
$update update_record($_POST['unique_ID'], $cayman_escrow);

    
// Verify that the update occurred.
    
if (!$update) {

        
// We cannot write to the temporary data file. Note a fatal error.
        
cpg_die(CRITICAL_ERROR$lang_upload_php['not_writable'], __FILE____LINE__);

    }

    
// Create upload form headers.
    
pageheader($lang_upload_php['title']);
    
    
// Direct the request to this script.
    
open_form($_SERVER['PHP_SELF']);

    
// Open the form table.
    
starttable("100%"$lang_upload_php['title'], 2);

    
// Create image tag and echo it to the output buffer.
    
echo "<tr><td class=\"tableh2\"><img class=\"image\" src=\"".$path_to_preview."\"  /></td>";

    
// Echo instructions.
    
echo "<td class=\"tableh2\">{$lang_upload_php['picture']} - {$file_set[0]}<br /><br />{$lang_upload_php['place_instr_1']}<br /><br />";

    
// If we have previously placed a picture, give a brief message about its success or failure.
    
if (isset($file_placement)) {

        if (
$file_placement == 'yes') {

            
// The previous picture was placed successfully.
            
echo "{$lang_upload_php['yes_place']}";

        } elseif (
$file_placement == 'no') {

            
// The previous image placement failed.
            
echo "{$lang_upload_php['no_place']}";

        }

    }

    echo 
"</td></tr>";

    
// Declare an array containing the various upload form box definitions.
    
$captionLabel $lang_upload_php['description'];
    if (
$CONFIG['show_bbcode_help']) {$captionLabel .= '&nbsp;'cpg_display_help('f=index.html&amp;base=64&amp;h='.urlencode(base64_encode(serialize($lang_bbcode_help_title))).'&amp;t='.urlencode(base64_encode(serialize($lang_bbcode_help))),470,245);}
    
//$printed_file_name = "{$lang_upload_php['picture']} - {$file_set[0]}";

    //Use the IPTC title or headline for the Coppermine title if available.
    
if (isset($iptc['Title']) && !empty($iptc['Title'])) {
        
$title=$iptc['Title'];
    } elseif (isset(
$iptc['Headline']) && !empty($iptc['Headline'])) {
        
$title=$iptc['Headline'];
    } else {
        
$title='';
    }

    
$form_array = array(
    array(
$lang_upload_php['album'], 'album'2),
    array(
$lang_upload_php['pic_title'], 'title'02551$title),
    array(
$captionLabel'caption'3$CONFIG['max_img_desc_length'], (isset($iptc['Caption'])) ? $iptc['Caption'] : ''),
    array(
$lang_upload_php['keywords'], 'keywords'02551,(isset($iptc['Keywords'])) ? implode(' ',$iptc['Keywords']): ''),
    array(
'control''phase_2'4),
    array(
'unique_ID'$_POST['unique_ID'], 4),
    );

    
// Check for user defined fields.
    
if(!empty($CONFIG['user_field1_name'])) {
        
$form_array[] = array($CONFIG['user_field1_name'], 'user1'02551);
    }

    if(!empty(
$CONFIG['user_field2_name'])) {
        
$form_array[] = array($CONFIG['user_field2_name'], 'user2'02551);
    }

    if(!empty(
$CONFIG['user_field3_name'])) {
        
$form_array[] = array($CONFIG['user_field3_name'], 'user3'02551);
    }

    if(!empty(
$CONFIG['user_field4_name'])) {
        
$form_array[] = array($CONFIG['user_field4_name'], 'user4'02551);
    }


    
// Check for movies and audio, and create width and height boxes if true.
    
if((is_movie($file_set[1])) or (is_audio($file_set[1]))) {

        
//Add width and height boxes to the form.
        
$form_array[] = array($lang_admin_php['th_wd'],'movie_wd'041);
        
$form_array[] = array($lang_admin_php['th_ht'],'movie_ht'041);

    }

    
// Create the form and echo more instructions.
    
create_form($form_array);

    
// More instructions.
    
if(count($escrow_array) > '1') {

        
form_statement($lang_upload_php['place_instr_2']);

    }

    
// Make button say 'Continue.'
    
close_form($lang_continue);

    
// Close the table, create footers, and flush the output buffer.
    
endtable();
    echo 
"</form>";
    
pagefooter();
    
ob_end_flush();

    
// Exit the script.
    
exit;


}
?>