1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890: 891: 892: 893: 894: 895: 896: 897: 898: 899: 900: 901: 902: 903: 904: 905: 906: 907: 908: 909: 910: 911: 912: 913: 914: 915: 916: 917: 918: 919: 920: 921: 922: 923: 924: 925: 926: 927: 928: 929: 930: 931: 932: 933: 934: 935: 936: 937: 938: 939: 940: 941: 942: 943: 944: 945: 946: 947: 948: 949: 950: 951: 952: 953: 954: 955: 956: 957: 958: 959: 960: 961: 962: 963: 964: 965: 966: 967: 968: 969: 970: 971: 972: 973: 974: 975: 976: 977: 978: 979: 980: 981: 982: 983: 984: 985: 986: 987: 988: 989: 990: 991: 992: 993: 994: 995: 996: 997: 998: 999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030: 1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052: 1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 1085: 1086: 1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101: 1102: 1103: 1104: 1105: 1106: 1107: 1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149: 1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 1192: 1193: 1194: 1195: 1196: 1197: 1198: 1199: 1200: 1201: 1202: 1203: 1204: 1205: 1206: 1207: 1208: 1209: 1210: 1211: 1212: 1213: 1214: 1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229: 1230: 1231: 1232: 1233: 1234: 1235: 1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 1249: 1250: 1251: 1252: 1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270: 1271: 1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279: 1280: 1281: 1282: 1283: 1284: 1285: 1286: 1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302: 1303: 1304: 1305: 1306: 1307: 1308: 1309: 1310: 1311: 1312: 1313: 1314: 1315: 1316: 1317: 1318: 1319: 1320: 1321: 1322: 1323: 1324: 1325: 1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342: 1343: 1344: 1345: 1346: 1347: 1348: 1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360: 1361: 1362: 1363: 1364: 1365: 1366: 1367: 1368: 1369: 1370: 1371: 1372: 1373: 1374: 1375: 1376: 1377: 1378: 1379: 1380: 1381: 1382: 1383: 1384: 1385: 1386: 1387: 1388: 1389: 1390: 1391: 1392: 1393: 1394: 1395: 1396: 1397: 1398: 1399: 1400: 1401: 1402: 1403: 1404: 1405: 1406: 1407: 1408: 1409: 1410: 1411: 1412: 1413: 1414: 1415: 1416: 1417: 1418: 1419: 1420: 1421: 1422: 1423: 1424: 1425: 1426: 1427: 1428: 1429: 1430: 1431: 1432: 1433: 1434: 1435: 1436: 1437: 1438: 1439: 1440: 1441: 1442: 1443: 1444: 1445: 1446: 1447: 1448: 1449: 1450: 1451: 1452: 1453: 1454: 1455: 1456: 1457: 1458: 1459: 1460: 1461: 1462: 1463: 1464: 1465: 1466: 1467: 1468: 1469: 1470: 1471: 1472: 1473: 1474: 1475: 1476: 1477: 1478: 1479: 1480: 1481: 1482: 1483: 1484: 1485: 1486: 1487: 1488: 1489: 1490: 1491: 1492: 1493: 1494: 1495: 1496: 1497: 1498: 1499: 1500: 1501: 1502: 1503: 1504: 1505: 1506: 1507: 1508: 1509: 1510: 1511: 1512: 1513: 1514: 1515: 1516: 1517: 1518: 1519: 1520: 1521: 1522: 1523: 1524: 1525: 1526: 1527: 1528: 1529: 1530: 1531: 1532: 1533: 1534: 1535: 1536: 1537: 1538: 1539: 1540: 1541: 1542: 1543: 1544: 1545: 1546: 1547: 1548: 1549: 1550: 1551: 1552: 1553: 1554: 1555: 1556: 1557: 1558: 1559: 1560: 1561: 1562: 1563: 1564: 1565: 1566: 1567: 1568: 1569: 1570: 1571: 1572: 1573: 1574: 1575: 1576: 1577: 1578: 1579: 1580: 1581: 1582: 1583: 1584: 1585: 1586: 1587: 1588: 1589: 1590: 1591: 1592: 1593: 1594: 1595: 1596: 1597: 1598: 1599: 1600: 1601: 1602: 1603: 1604: 1605: 1606: 1607: 1608: 1609: 1610: 1611: 1612: 1613: 1614: 1615: 1616: 1617: 1618: 1619: 1620: 1621: 1622: 1623: 1624: 1625: 1626: 1627: 1628: 1629: 1630: 1631: 1632: 1633: 1634: 1635: 1636: 1637: 1638: 1639: 1640: 1641: 1642: 1643: 1644: 1645: 1646: 1647: 1648: 1649: 1650: 1651: 1652: 1653: 1654: 1655: 1656: 1657: 1658: 1659: 1660: 1661: 1662: 1663: 1664: 1665: 1666: 1667: 1668: 1669: 1670: 1671: 1672: 1673: 1674: 1675: 1676: 1677: 1678: 1679: 1680: 1681: 1682: 1683: 1684: 1685: 1686: 1687: 1688: 1689: 1690: 1691: 1692: 1693: 1694: 1695: 1696: 1697: 1698: 1699: 1700: 1701: 1702: 1703: 1704: 1705: 1706: 1707: 1708: 1709: 1710: 1711: 1712: 1713: 1714: 1715: 1716: 1717: 1718: 1719: 1720: 1721: 1722: 1723: 1724: 1725: 1726: 1727: 1728: 1729: 1730: 1731: 1732: 1733: 1734: 1735: 1736: 1737: 1738: 1739: 1740: 1741: 1742: 1743: 1744: 1745: 1746: 1747: 1748: 1749: 1750: 1751: 1752: 1753: 1754: 1755: 1756: 1757: 1758: 1759: 1760: 1761: 1762: 1763: 1764: 1765: 1766: 1767: 1768: 1769: 1770: 1771: 1772: 1773: 1774: 1775: 1776: 1777: 1778: 1779: 1780: 1781: 1782: 1783: 1784: 1785: 1786: 1787: 1788: 1789: 1790: 1791: 1792: 1793: 1794: 1795: 1796: 1797: 1798: 1799: 1800: 1801: 1802: 1803: 1804: 1805: 1806: 1807: 1808: 1809: 1810: 1811: 1812: 1813: 1814: 1815: 1816: 1817: 1818: 1819: 1820: 1821: 1822: 1823: 1824: 1825: 1826: 1827: 1828: 1829: 1830: 1831: 1832: 1833: 1834: 1835: 1836: 1837: 1838: 1839: 1840: 1841: 1842: 1843: 1844: 1845: 1846: 1847: 1848: 1849: 1850: 1851: 1852: 1853: 1854: 1855: 1856: 1857: 1858: 1859: 1860: 1861: 1862: 1863: 1864: 1865: 1866: 1867: 1868: 1869: 1870: 1871: 1872: 1873: 1874: 1875: 1876: 1877: 1878: 1879: 1880: 1881: 1882: 1883: 1884: 1885: 1886: 1887: 1888: 1889: 1890: 1891: 1892: 1893: 1894: 1895: 1896: 1897: 1898: 1899: 1900: 1901: 1902: 1903: 1904: 1905: 1906: 1907: 1908: 1909: 1910: 1911: 1912: 1913: 1914: 1915: 1916: 1917: 1918: 1919: 1920: 1921: 1922: 1923: 1924: 1925: 1926: 1927: 1928: 1929: 1930: 1931: 1932: 1933: 1934: 1935: 1936: 1937: 1938: 1939: 1940: 1941: 1942: 1943: 1944: 1945: 1946: 1947: 1948: 1949: 1950: 1951: 1952: 1953: 1954: 1955: 1956: 1957: 1958: 1959: 1960: 1961: 1962: 1963: 1964: 1965: 1966: 1967: 1968: 1969: 1970: 1971: 1972: 1973: 1974: 1975: 1976: 1977: 1978: 1979: 1980: 1981: 1982: 1983: 1984: 1985: 1986: 1987: 1988: 1989: 1990: 1991: 1992: 1993: 1994: 1995: 1996: 1997: 1998: 1999: 2000: 2001: 2002: 2003: 2004: 2005: 2006: 2007: 2008: 2009: 2010: 2011: 2012: 2013: 2014: 2015: 2016: 2017: 2018: 2019: 2020: 2021: 2022: 2023: 2024: 2025: 2026: 2027: 2028: 2029: 2030: 2031: 2032: 2033: 2034: 2035: 2036: 2037: 2038: 2039: 2040: 2041: 2042: 2043: 2044: 2045: 2046: 2047: 2048: 2049: 2050: 2051: 2052: 2053: 2054: 2055: 2056: 2057: 2058: 2059: 2060: 2061: 2062: 2063: 2064: 2065: 2066: 2067: 2068: 2069: 2070: 2071: 2072: 2073: 2074: 2075: 2076: 2077: 2078: 2079: 2080: 2081: 2082: 2083: 2084: 2085: 2086: 2087: 2088: 2089: 2090: 2091: 2092: 2093: 2094: 2095: 2096: 2097: 2098: 2099: 2100: 2101: 2102: 2103: 2104: 2105: 2106: 2107: 2108: 2109: 2110: 2111: 2112: 2113: 2114: 2115: 2116: 2117: 2118: 2119: 2120: 2121: 2122: 2123: 2124: 2125: 2126: 2127: 2128: 2129: 2130: 2131: 2132: 2133: 2134: 2135: 2136: 2137: 2138: 2139: 2140: 2141: 2142: 2143: 2144: 2145: 2146: 2147: 2148: 2149: 2150: 2151: 2152: 2153: 2154: 2155: 2156: 2157: 2158: 2159: 2160: 2161: 2162: 2163: 2164: 2165: 2166: 2167: 2168: 2169: 2170: 2171: 2172: 2173: 2174: 2175: 2176: 2177: 2178: 2179: 2180: 2181: 2182: 2183: 2184: 2185: 2186: 2187: 2188: 2189: 2190: 2191: 2192: 2193: 2194: 2195: 2196: 2197: 2198: 2199: 2200: 2201: 2202: 2203: 2204: 2205: 2206: 2207: 2208: 2209: 2210: 2211: 2212: 2213: 2214: 2215: 2216: 2217: 2218: 2219: 2220: 2221: 2222: 2223: 2224: 2225: 2226: 2227: 2228: 2229: 2230: 2231: 2232: 2233: 2234: 2235: 2236: 2237: 2238: 2239: 2240: 2241: 2242: 2243: 2244: 2245: 2246: 2247: 2248: 2249: 2250: 2251: 2252: 2253: 2254: 2255: 2256: 2257: 2258: 2259: 2260: 2261: 2262: 2263: 2264: 2265: 2266: 2267: 2268: 2269: 2270: 2271: 2272: 2273: 2274: 2275: 2276: 2277: 2278: 2279: 2280: 2281: 2282: 2283: 2284: 2285: 2286: 2287: 2288: 2289: 2290: 2291: 2292: 2293: 2294: 2295: 2296: 2297: 2298: 2299: 2300: 2301: 2302: 2303: 2304: 2305: 2306: 2307: 2308: 2309: 2310: 2311: 2312: 2313: 2314: 2315: 2316: 2317: 2318: 2319: 2320: 2321: 2322: 2323: 2324: 2325: 2326: 2327: 2328: 2329: 2330: 2331: 2332: 2333: 2334: 2335: 2336: 2337: 2338: 2339: 2340: 2341: 2342: 2343: 2344: 2345: 2346: 2347: 2348: 2349: 2350: 2351: 2352: 2353: 2354: 2355: 2356: 2357: 2358: 2359: 2360: 2361: 2362: 2363: 2364: 2365: 2366: 2367: 2368: 2369: 2370: 2371: 2372: 2373: 2374: 2375: 2376: 2377: 2378: 2379: 2380: 2381: 2382: 2383: 2384: 2385: 2386: 2387: 2388: 2389: 2390: 2391: 2392: 2393: 2394: 2395: 2396: 2397: 2398: 2399: 2400: 2401: 2402: 2403: 2404: 2405: 2406: 2407: 2408: 2409: 2410: 2411: 2412: 2413: 2414: 2415: 2416: 2417: 2418: 2419: 2420: 2421: 2422: 2423: 2424: 2425: 2426: 2427: 2428: 2429: 2430: 2431: 2432: 2433: 2434: 2435: 2436: 2437: 2438: 2439: 2440: 2441: 2442: 2443: 2444: 2445: 2446: 2447: 2448: 2449: 2450: 2451: 2452: 2453: 2454: 2455: 2456: 2457: 2458: 2459: 2460: 2461: 2462: 2463: 2464: 2465: 2466: 2467: 2468: 2469: 2470: 2471: 2472: 2473: 2474: 2475: 2476: 2477: 2478: 2479: 2480: 2481: 2482: 2483: 2484: 2485: 2486: 2487: 2488: 2489: 2490: 2491: 2492: 2493: 2494: 2495: 2496: 2497: 2498: 2499: 2500: 2501: 2502: 2503: 2504: 2505: 2506: 2507: 2508: 2509: 2510: 2511: 2512: 2513: 2514: 2515: 2516: 2517: 2518: 2519: 2520: 2521: 2522: 2523: 2524: 2525: 2526: 2527: 2528: 2529: 2530: 2531: 2532: 2533: 2534: 2535: 2536: 2537: 2538: 2539: 2540: 2541: 2542: 2543: 2544: 2545: 2546: 2547: 2548: 2549: 2550: 2551: 2552: 2553: 2554: 2555: 2556: 2557: 2558: 2559: 2560: 2561: 2562: 2563: 2564: 2565: 2566: 2567: 2568: 2569: 2570: 2571: 2572: 2573: 2574: 2575: 2576: 2577: 2578: 2579: 2580: 2581: 2582: 2583: 2584: 2585: 2586: 2587: 2588: 2589: 2590: 2591: 2592: 2593: 2594: 2595: 2596: 2597: 2598: 2599: 2600: 2601: 2602: 2603: 2604: 2605: 2606: 2607: 2608: 2609: 2610: 2611: 2612: 2613: 2614: 2615: 2616: 2617: 2618: 2619: 2620: 2621: 2622: 2623: 2624: 2625: 2626: 2627: 2628: 2629: 2630: 2631: 2632: 2633: 2634: 2635: 2636: 2637: 2638: 2639: 2640: 2641: 2642: 2643: 2644: 2645: 2646: 2647: 2648: 2649: 2650: 2651: 2652: 2653: 2654: 2655: 2656: 2657: 2658: 2659: 2660: 2661: 2662: 2663: 2664: 2665: 2666: 2667: 2668: 2669: 2670: 2671: 2672: 2673: 2674: 2675: 2676: 2677: 2678: 2679: 2680: 2681: 2682: 2683: 2684: 2685: 2686: 2687: 2688: 2689: 2690: 2691: 2692: 2693: 2694: 2695: 2696: 2697: 2698: 2699: 2700: 2701: 2702: 2703: 2704: 2705: 2706: 2707: 2708: 2709: 2710: 2711: 2712: 2713: 2714: 2715: 2716: 2717: 2718: 2719: 2720: 2721: 2722: 2723: 2724: 2725: 2726: 2727: 2728: 2729: 2730: 2731: 2732: 2733: 2734: 2735: 2736: 2737: 2738: 2739: 2740: 2741: 2742: 2743: 2744: 2745: 2746: 2747: 2748: 2749: 2750: 2751: 2752: 2753: 2754: 2755: 2756: 2757: 2758: 2759: 2760: 2761: 2762: 2763: 2764: 2765: 2766: 2767: 2768: 2769: 2770: 2771: 2772: 2773: 2774: 2775: 2776: 2777: 2778: 2779: 2780: 2781: 2782: 2783: 2784: 2785: 2786: 2787: 2788: 2789: 2790: 2791: 2792: 2793: 2794: 2795: 2796: 2797: 2798: 2799: 2800: 2801: 2802: 2803: 2804: 2805: 2806: 2807: 2808: 2809: 2810: 2811: 2812: 2813: 2814: 2815: 2816: 2817: 2818: 2819: 2820: 2821: 2822: 2823: 2824:
<?php
if (!defined('SMF'))
die('No direct access...');
function ManageAttachments()
{
global $txt, $context;
isAllowedTo('manage_attachments');
loadTemplate('ManageAttachments');
$subActions = array(
'attachments' => 'ManageAttachmentSettings',
'attachpaths' => 'ManageAttachmentPaths',
'avatars' => 'ManageAvatarSettings',
'browse' => 'BrowseFiles',
'byAge' => 'RemoveAttachmentByAge',
'bySize' => 'RemoveAttachmentBySize',
'maintenance' => 'MaintainFiles',
'repair' => 'RepairAttachments',
'remove' => 'RemoveAttachment',
'removeall' => 'RemoveAllAttachments',
'transfer' => 'TransferAttachments',
);
$context[$context['admin_menu_name']]['tab_data'] = array(
'title' => $txt['attachments_avatars'],
'help' => 'manage_files',
'description' => $txt['attachments_desc'],
);
call_integration_hook('integrate_manage_attachments', array(&$subActions));
if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
$context['sub_action'] = $_REQUEST['sa'];
else
$context['sub_action'] = 'browse';
$context['page_title'] = $txt['attachments_avatars'];
call_helper($subActions[$context['sub_action']]);
}
function ManageAttachmentSettings($return_config = false)
{
global $smcFunc, $txt, $modSettings, $scripturl, $context, $sourcedir, $boarddir;
require_once($sourcedir . '/Subs-Attachments.php');
$context['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']];
if (!isset($_GET['save']) && empty($modSettings['basedirectory_for_attachments']))
if (is_dir($modSettings['attachmentUploadDir'][1]))
$modSettings['basedirectory_for_attachments'] = $modSettings['attachmentUploadDir'][1];
else
$modSettings['basedirectory_for_attachments'] = $context['attachmentUploadDir'];
$context['valid_upload_dir'] = is_dir($context['attachmentUploadDir']) && is_writable($context['attachmentUploadDir']);
if (!empty($modSettings['automanage_attachments']))
$context['valid_basedirectory'] = !empty($modSettings['basedirectory_for_attachments']) && is_writable($modSettings['basedirectory_for_attachments']);
else
$context['valid_basedirectory'] = true;
$txt['attachment_path'] = $context['attachmentUploadDir'];
$txt['basedirectory_for_attachments_path'] = isset($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : '';
$txt['use_subdirectories_for_attachments_note'] = empty($modSettings['attachment_basedirectories']) || empty($modSettings['use_subdirectories_for_attachments']) ? $txt['use_subdirectories_for_attachments_note'] : '';
$txt['attachmentUploadDir_multiple_configure'] = '<a href="' . $scripturl . '?action=admin;area=manageattachments;sa=attachpaths">[' . $txt['attachmentUploadDir_multiple_configure'] . ']</a>';
$txt['attach_current_dir'] = empty($modSettings['automanage_attachments']) ? $txt['attach_current_dir'] : $txt['attach_last_dir'];
$txt['attach_current_dir_warning'] = $txt['attach_current_dir'] . $txt['attach_current_dir_warning'];
$txt['basedirectory_for_attachments_warning'] = $txt['basedirectory_for_attachments_current'] . $txt['basedirectory_for_attachments_warning'];
$testImg = get_extension_funcs('gd') || class_exists('Imagick') || get_extension_funcs('MagickWand');
$post_max_kb = floor(memoryReturnBytes(ini_get('post_max_size')) / 1024);
$file_max_kb = floor(memoryReturnBytes(ini_get('upload_max_filesize')) / 1024);
$config_vars = array(
array('title', 'attachment_manager_settings'),
array('select', 'attachmentEnable', array($txt['attachmentEnable_deactivate'], $txt['attachmentEnable_enable_all'], $txt['attachmentEnable_disable_new'])),
'',
array('select', 'automanage_attachments', array(0 => $txt['attachments_normal'], 1 => $txt['attachments_auto_space'], 2 => $txt['attachments_auto_years'], 3 => $txt['attachments_auto_months'], 4 => $txt['attachments_auto_16'])),
array('check', 'use_subdirectories_for_attachments', 'subtext' => $txt['use_subdirectories_for_attachments_note']),
(empty($modSettings['attachment_basedirectories']) ? array('text', 'basedirectory_for_attachments', 40,) : array('var_message', 'basedirectory_for_attachments', 'message' => 'basedirectory_for_attachments_path', 'invalid' => empty($context['valid_basedirectory']), 'text_label' => (!empty($context['valid_basedirectory']) ? $txt['basedirectory_for_attachments_current'] : $txt['basedirectory_for_attachments_warning']))),
empty($modSettings['attachment_basedirectories']) && $modSettings['currentAttachmentUploadDir'] == 1 && count($modSettings['attachmentUploadDir']) == 1 ? array('json', 'attachmentUploadDir', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 40, 'invalid' => !$context['valid_upload_dir'], 'disabled' => true) : array('var_message', 'attach_current_directory', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 'message' => 'attachment_path', 'invalid' => empty($context['valid_upload_dir']), 'text_label' => (!empty($context['valid_upload_dir']) ? $txt['attach_current_dir'] : $txt['attach_current_dir_warning'])),
array('int', 'attachmentDirFileLimit', 'subtext' => $txt['zero_for_no_limit'], 6),
array('int', 'attachmentDirSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']),
array('check', 'dont_show_attach_under_post', 'subtext' => $txt['dont_show_attach_under_post_sub']),
'',
array('int', 'attachmentPostLimit', 'subtext' => sprintf($txt['attachment_ini_max'], $post_max_kb . ' ' . $txt['kilobyte']), 6, 'postinput' => $txt['kilobyte'], 'min' => 1, 'max' => $post_max_kb, 'disabled' => empty($post_max_kb)),
array('int', 'attachmentSizeLimit', 'subtext' => sprintf($txt['attachment_ini_max'], $file_max_kb . ' ' . $txt['kilobyte']), 6, 'postinput' => $txt['kilobyte'], 'min' => 1, 'max' => $file_max_kb, 'disabled' => empty($file_max_kb)),
array('int', 'attachmentNumPerPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6),
array('title', 'attachment_security_settings'),
array('check', 'attachmentCheckExtensions'),
array('text', 'attachmentExtensions', 40),
'',
array('warning', empty($testImg) ? 'attachment_img_enc_warning' : ''),
array('check', 'attachment_image_reencode'),
'',
array('warning', 'attachment_image_paranoid_warning'),
array('check', 'attachment_image_paranoid'),
array('title', 'attachment_thumbnail_settings'),
array('check', 'attachmentShowImages'),
array('check', 'attachmentThumbnails'),
array('check', 'attachment_thumb_png'),
array('check', 'attachment_thumb_memory'),
array('warning', 'attachment_thumb_memory_note'),
array('text', 'attachmentThumbWidth', 6),
array('text', 'attachmentThumbHeight', 6),
'',
array('int', 'max_image_width', 'subtext' => $txt['zero_for_no_limit']),
array('int', 'max_image_height', 'subtext' => $txt['zero_for_no_limit']),
);
$context['settings_post_javascript'] = '
var storing_type = document.getElementById(\'automanage_attachments\');
var base_dir = document.getElementById(\'use_subdirectories_for_attachments\');
createEventListener(storing_type)
storing_type.addEventListener("change", toggleSubDir, false);
createEventListener(base_dir)
base_dir.addEventListener("change", toggleSubDir, false);
toggleSubDir();';
call_integration_hook('integrate_modify_attachment_settings', array(&$config_vars));
if ($return_config)
return $config_vars;
require_once($sourcedir . '/ManagePermissions.php');
require_once($sourcedir . '/ManageServer.php');
if (isset($_GET['save']))
{
checkSession();
if (isset($_POST['attachmentUploadDir']))
unset($_POST['attachmentUploadDir']);
if (!empty($_POST['use_subdirectories_for_attachments']))
{
if (isset($_POST['use_subdirectories_for_attachments']) && empty($_POST['basedirectory_for_attachments']))
$_POST['basedirectory_for_attachments'] = (!empty($modSettings['basedirectory_for_attachments']) ? ($modSettings['basedirectory_for_attachments']) : $boarddir);
if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($modSettings['attachment_basedirectories']))
{
if (!is_array($modSettings['attachment_basedirectories']))
$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
}
else
$modSettings['attachment_basedirectories'] = array();
if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($_POST['basedirectory_for_attachments']) && !in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']))
{
$currentAttachmentUploadDir = $modSettings['currentAttachmentUploadDir'];
if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachmentUploadDir']))
{
if (!automanage_attachments_create_directory($_POST['basedirectory_for_attachments']))
$_POST['basedirectory_for_attachments'] = $modSettings['basedirectory_for_attachments'];
}
if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']))
{
$modSettings['attachment_basedirectories'][$modSettings['currentAttachmentUploadDir']] = $_POST['basedirectory_for_attachments'];
updateSettings(array(
'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
'currentAttachmentUploadDir' => $currentAttachmentUploadDir,
));
$_POST['use_subdirectories_for_attachments'] = 1;
$_POST['attachmentUploadDir'] = $smcFunc['json_encode']($modSettings['attachmentUploadDir']);
}
}
}
call_integration_hook('integrate_save_attachment_settings');
saveDBSettings($config_vars);
$_SESSION['adm-save'] = true;
redirectexit('action=admin;area=manageattachments;sa=attachments');
}
$context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=attachments';
prepareDBSettingContext($config_vars);
$context['sub_template'] = 'show_settings';
}
function ManageAvatarSettings($return_config = false)
{
global $txt, $context, $modSettings, $sourcedir, $scripturl;
global $boarddir, $boardurl;
$testImg = get_extension_funcs('gd') || class_exists('Imagick');
$context['valid_avatar_dir'] = is_dir($modSettings['avatar_directory']);
$context['valid_custom_avatar_dir'] = !empty($modSettings['custom_avatar_dir']) && is_dir($modSettings['custom_avatar_dir']) && is_writable($modSettings['custom_avatar_dir']);
$config_vars = array(
array('title', 'avatar_server_stored'),
array('warning', empty($testImg) ? 'avatar_img_enc_warning' : ''),
array('permissions', 'profile_server_avatar', 0, $txt['avatar_server_stored_groups']),
array('warning', !$context['valid_avatar_dir'] ? 'avatar_directory_wrong' : ''),
array('text', 'avatar_directory', 40, 'invalid' => !$context['valid_avatar_dir']),
array('text', 'avatar_url', 40),
array('title', 'avatar_external'),
array('permissions', 'profile_remote_avatar', 0, $txt['avatar_external_url_groups']),
array('check', 'avatar_download_external', 0, 'onchange' => 'fUpdateStatus();'),
array('text', 'avatar_max_width_external', 'subtext' => $txt['zero_for_no_limit'], 6),
array('text', 'avatar_max_height_external', 'subtext' => $txt['zero_for_no_limit'], 6),
array('select', 'avatar_action_too_large',
array(
'option_refuse' => $txt['option_refuse'],
'option_css_resize' => $txt['option_css_resize'],
'option_download_and_resize' => $txt['option_download_and_resize'],
),
),
array('title', 'avatar_upload'),
array('permissions', 'profile_upload_avatar', 0, $txt['avatar_upload_groups']),
array('text', 'avatar_max_width_upload', 'subtext' => $txt['zero_for_no_limit'], 6),
array('text', 'avatar_max_height_upload', 'subtext' => $txt['zero_for_no_limit'], 6),
array('check', 'avatar_resize_upload', 'subtext' => $txt['avatar_resize_upload_note']),
array('check', 'avatar_download_png'),
array('check', 'avatar_reencode'),
'',
array('warning', 'avatar_paranoid_warning'),
array('check', 'avatar_paranoid'),
'',
array('warning', !$context['valid_custom_avatar_dir'] ? 'custom_avatar_dir_wrong' : ''),
array('text', 'custom_avatar_dir', 40, 'subtext' => $txt['custom_avatar_dir_desc'], 'invalid' => !$context['valid_custom_avatar_dir']),
array('text', 'custom_avatar_url', 40),
array('title', 'gravatar_settings'),
array('check', 'gravatarEnabled'),
array('check', 'gravatarOverride'),
array('check', 'gravatarAllowExtraEmail'),
'',
array('select', 'gravatarMaxRating',
array(
'G' => $txt['gravatar_maxG'],
'PG' => $txt['gravatar_maxPG'],
'R' => $txt['gravatar_maxR'],
'X' => $txt['gravatar_maxX'],
),
),
array('select', 'gravatarDefault',
array(
'mm' => $txt['gravatar_mm'],
'identicon' => $txt['gravatar_identicon'],
'monsterid' => $txt['gravatar_monsterid'],
'wavatar' => $txt['gravatar_wavatar'],
'retro' => $txt['gravatar_retro'],
'blank' => $txt['gravatar_blank'],
),
),
);
call_integration_hook('integrate_modify_avatar_settings', array(&$config_vars));
if ($return_config)
return $config_vars;
require_once($sourcedir . '/ManageServer.php');
if (isset($_GET['save']))
{
checkSession();
if (empty($_POST['custom_avatar_dir']))
$_POST['custom_avatar_dir'] = $boarddir . '/custom_avatar';
if (empty($_POST['custom_avatar_url']))
$_POST['custom_avatar_url'] = $boardurl . '/custom_avatar';
if (empty($_POST['avatar_directory']))
$_POST['avatar_directory'] = $boarddir . '/avatars';
if (empty($_POST['avatar_url']))
$_POST['avatar_url'] = $boardurl . '/avatars';
call_integration_hook('integrate_save_avatar_settings');
saveDBSettings($config_vars);
$_SESSION['adm-save'] = true;
redirectexit('action=admin;area=manageattachments;sa=avatars');
}
$context['settings_save_onclick'] = 'return (document.getElementById(\'custom_avatar_dir\').value == \'\' || document.getElementById(\'custom_avatar_url\').value == \'\') ? confirm(\'' . $txt['custom_avatar_check_empty'] . '\') : true;';
createToken('admin-mp');
$context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=avatars';
prepareDBSettingContext($config_vars);
$context['template_layers'][] = 'avatar_settings';
$context['sub_template'] = 'show_settings';
}
function BrowseFiles()
{
global $context, $txt, $scripturl, $modSettings;
global $smcFunc, $sourcedir, $settings;
$context['browse_type'] = isset($_REQUEST['avatars']) ? 'avatars' : (isset($_REQUEST['thumbs']) ? 'thumbs' : 'attachments');
$titles = array(
'attachments' => array('?action=admin;area=manageattachments;sa=browse', $txt['attachment_manager_attachments']),
'avatars' => array('?action=admin;area=manageattachments;sa=browse;avatars', $txt['attachment_manager_avatars']),
'thumbs' => array('?action=admin;area=manageattachments;sa=browse;thumbs', $txt['attachment_manager_thumbs']),
);
$list_title = $txt['attachment_manager_browse_files'] . ': ';
foreach ($titles as $browse_type => $details)
{
if ($browse_type != 'attachments')
$list_title .= ' | ';
if ($context['browse_type'] == $browse_type)
$list_title .= '<img src="' . $settings['images_url'] . '/selected.png" alt=">"> ';
$list_title .= '<a href="' . $scripturl . $details[0] . '">' . $details[1] . '</a>';
}
$listOptions = array(
'id' => 'file_list',
'title' => $list_title,
'items_per_page' => $modSettings['defaultMaxListItems'],
'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=browse' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
'default_sort_col' => 'name',
'no_items_label' => $txt['attachment_manager_' . ($context['browse_type'] === 'avatars' ? 'avatars' : ($context['browse_type'] === 'thumbs' ? 'thumbs' : 'attachments')) . '_no_entries'],
'get_items' => array(
'function' => 'list_getFiles',
'params' => array(
$context['browse_type'],
),
),
'get_count' => array(
'function' => 'list_getNumFiles',
'params' => array(
$context['browse_type'],
),
),
'columns' => array(
'name' => array(
'header' => array(
'value' => $txt['attachment_name'],
),
'data' => array(
'function' => function($rowData) use ($modSettings, $context, $scripturl, $smcFunc)
{
$link = '<a href="';
if ($rowData['attachment_type'] == 1)
$link .= sprintf('%1$s/%2$s', $modSettings['custom_avatar_url'], $rowData['filename']);
elseif ($context['browse_type'] == 'avatars')
$link .= sprintf('%1$s?action=dlattach;type=avatar;attach=%2$d', $scripturl, $rowData['id_attach']);
else
$link .= sprintf('%1$s?action=dlattach;topic=%2$d.0;attach=%3$d', $scripturl, $rowData['id_topic'], $rowData['id_attach']);
$link .= '"';
if (!empty($rowData['width']) && !empty($rowData['height']))
$link .= sprintf(' onclick="return reqWin(this.href' . ($rowData['attachment_type'] == 1 ? '' : ' + \';image\'') . ', %1$d, %2$d, true);"', $rowData['width'] + 20, $rowData['height'] + 20);
$link .= sprintf('>%1$s</a>', preg_replace('~&#(\\\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\\\1;', $smcFunc['htmlspecialchars']($rowData['filename'])));
if (!empty($rowData['width']) && !empty($rowData['height']))
$link .= sprintf(' <span class="smalltext">%1$dx%2$d</span>', $rowData['width'], $rowData['height']);
return $link;
},
),
'sort' => array(
'default' => 'a.filename',
'reverse' => 'a.filename DESC',
),
),
'filesize' => array(
'header' => array(
'value' => $txt['attachment_file_size'],
),
'data' => array(
'function' => function($rowData) use ($txt)
{
return sprintf('%1$s%2$s', round($rowData['size'] / 1024, 2), $txt['kilobyte']);
},
),
'sort' => array(
'default' => 'a.size',
'reverse' => 'a.size DESC',
),
),
'member' => array(
'header' => array(
'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_member'] : $txt['posted_by'],
),
'data' => array(
'function' => function($rowData) use ($scripturl, $smcFunc)
{
if (empty($rowData['id_member']))
return $smcFunc['htmlspecialchars']($rowData['poster_name']);
else
return sprintf('<a href="%1$s?action=profile;u=%2$d">%3$s</a>', $scripturl, $rowData['id_member'], $rowData['poster_name']);
},
),
'sort' => array(
'default' => 'mem.real_name',
'reverse' => 'mem.real_name DESC',
),
),
'date' => array(
'header' => array(
'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_last_active'] : $txt['date'],
),
'data' => array(
'function' => function($rowData) use ($txt, $context, $scripturl)
{
$date = empty($rowData['poster_time']) ? $txt['never'] : timeformat($rowData['poster_time']);
if ($context['browse_type'] !== 'avatars')
$date .= sprintf('<br>%1$s <a href="%2$s?topic=%3$d.msg%4$d#msg%4$d">%5$s</a>', $txt['in'], $scripturl, $rowData['id_topic'], $rowData['id_msg'], $rowData['subject']);
return $date;
},
),
'sort' => array(
'default' => $context['browse_type'] === 'avatars' ? 'mem.last_login' : 'm.id_msg',
'reverse' => $context['browse_type'] === 'avatars' ? 'mem.last_login DESC' : 'm.id_msg DESC',
),
),
'downloads' => array(
'header' => array(
'value' => $txt['downloads'],
),
'data' => array(
'db' => 'downloads',
'comma_format' => true,
),
'sort' => array(
'default' => 'a.downloads',
'reverse' => 'a.downloads DESC',
),
),
'check' => array(
'header' => array(
'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
'class' => 'centercol',
),
'data' => array(
'sprintf' => array(
'format' => '<input type="checkbox" name="remove[%1$d]">',
'params' => array(
'id_attach' => false,
),
),
'class' => 'centercol',
),
),
),
'form' => array(
'href' => $scripturl . '?action=admin;area=manageattachments;sa=remove' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
'include_sort' => true,
'include_start' => true,
'hidden_fields' => array(
'type' => $context['browse_type'],
),
),
'additional_rows' => array(
array(
'position' => 'above_table_headers',
'value' => '<input type="submit" name="remove_submit" class="button you_sure" value="' . $txt['quickmod_delete_selected'] . '" data-confirm="' . $txt['confirm_delete_attachments'] . '">',
),
array(
'position' => 'below_table_data',
'value' => '<input type="submit" name="remove_submit" class="button you_sure" value="' . $txt['quickmod_delete_selected'] . '" data-confirm="' . $txt['confirm_delete_attachments'] . '">',
),
),
);
call_integration_hook('integrate_attachments_browse', array(&$listOptions, &$titles, &$list_title));
require_once($sourcedir . '/Subs-List.php');
createList($listOptions);
$context['sub_template'] = 'show_list';
$context['default_list'] = 'file_list';
}
function list_getFiles($start, $items_per_page, $sort, $browse_type)
{
global $smcFunc, $txt;
if ($browse_type === 'avatars')
$request = $smcFunc['db_query']('', '
SELECT
{string:blank_text} AS id_msg, COALESCE(mem.real_name, {string:not_applicable_text}) AS poster_name,
mem.last_login AS poster_time, 0 AS id_topic, a.id_member, a.id_attach, a.filename, a.file_hash, a.attachment_type,
a.size, a.width, a.height, a.downloads, {string:blank_text} AS subject, 0 AS id_board
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
WHERE a.id_member != {int:guest_id}
ORDER BY {raw:sort}
LIMIT {int:start}, {int:per_page}',
array(
'guest_id' => 0,
'blank_text' => '',
'not_applicable_text' => $txt['not_applicable'],
'sort' => $sort,
'start' => $start,
'per_page' => $items_per_page,
)
);
else
$request = $smcFunc['db_query']('', '
SELECT
m.id_msg, COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.id_topic, m.id_member,
a.id_attach, a.filename, a.file_hash, a.attachment_type, a.size, a.width, a.height, a.downloads, mf.subject, t.id_board
FROM {db_prefix}attachments AS a
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE a.attachment_type = {int:attachment_type}
ORDER BY {raw:sort}
LIMIT {int:start}, {int:per_page}',
array(
'attachment_type' => $browse_type == 'thumbs' ? '3' : '0',
'sort' => $sort,
'start' => $start,
'per_page' => $items_per_page,
)
);
$files = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$files[] = $row;
$smcFunc['db_free_result']($request);
return $files;
}
function list_getNumFiles($browse_type)
{
global $smcFunc;
if ($browse_type === 'avatars')
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}attachments
WHERE id_member != {int:guest_id_member}',
array(
'guest_id_member' => 0,
)
);
else
$request = $smcFunc['db_query']('', '
SELECT COUNT(*) AS num_attach
FROM {db_prefix}attachments AS a
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
WHERE a.attachment_type = {int:attachment_type}
AND a.id_member = {int:guest_id_member}',
array(
'attachment_type' => $browse_type === 'thumbs' ? '3' : '0',
'guest_id_member' => 0,
)
);
list ($num_files) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
return $num_files;
}
function MaintainFiles()
{
global $context, $modSettings, $smcFunc;
$context['sub_template'] = 'maintenance';
$attach_dirs = $modSettings['attachmentUploadDir'];
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}attachments
WHERE attachment_type = {int:attachment_type}
AND id_member = {int:guest_id_member}',
array(
'attachment_type' => 0,
'guest_id_member' => 0,
)
);
list ($context['num_attachments']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$context['num_attachments'] = comma_format($context['num_attachments'], 0);
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}attachments
WHERE id_member != {int:guest_id_member}',
array(
'guest_id_member' => 0,
)
);
list ($context['num_avatars']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$context['num_avatars'] = comma_format($context['num_avatars'], 0);
$request = $smcFunc['db_query']('', '
SELECT SUM(size)
FROM {db_prefix}attachments
WHERE attachment_type != {int:type}',
array(
'type' => 1,
)
);
list ($attachmentDirSize) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$attachmentDirSize /= 1024;
$context['attachment_total_size'] = comma_format($attachmentDirSize, 2);
$request = $smcFunc['db_query']('', '
SELECT COUNT(*), SUM(size)
FROM {db_prefix}attachments
WHERE id_folder = {int:folder_id}
AND attachment_type != {int:type}',
array(
'folder_id' => $modSettings['currentAttachmentUploadDir'],
'type' => 1,
)
);
list ($current_dir_files, $current_dir_size) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$current_dir_size /= 1024;
if (!empty($modSettings['attachmentDirSizeLimit']))
$context['attachment_space'] = comma_format(max($modSettings['attachmentDirSizeLimit'] - $current_dir_size, 0), 2);
$context['attachment_current_size'] = comma_format($current_dir_size, 2);
if (!empty($modSettings['attachmentDirFileLimit']))
$context['attachment_files'] = comma_format(max($modSettings['attachmentDirFileLimit'] - $current_dir_files, 0), 0);
$context['attachment_current_files'] = comma_format($current_dir_files, 0);
$context['attach_multiple_dirs'] = count($attach_dirs) > 1 ? true : false;
$context['attach_dirs'] = $attach_dirs;
$context['base_dirs'] = !empty($modSettings['attachment_basedirectories']) ? $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true) : array();
$context['checked'] = isset($_SESSION['checked']) ? $_SESSION['checked'] : true;
if (!empty($_SESSION['results']))
{
$context['results'] = implode('<br>', $_SESSION['results']);
unset($_SESSION['results']);
}
}
function RemoveAttachmentByAge()
{
global $smcFunc;
checkSession('post', 'admin');
if ($_REQUEST['type'] != 'avatars')
{
$messages = removeAttachments(array('attachment_type' => 0, 'poster_time' => (time() - 24 * 60 * 60 * $_POST['age'])), 'messages', true);
if (!empty($messages) && !empty($_POST['notice']))
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = CONCAT(body, {string:notice})
WHERE id_msg IN ({array_int:messages})',
array(
'messages' => $messages,
'notice' => '<br><br>' . $_POST['notice'],
)
);
}
else
{
removeAttachments(array('not_id_member' => 0, 'last_login' => (time() - 24 * 60 * 60 * $_POST['age'])), 'members');
}
redirectexit('action=admin;area=manageattachments' . (empty($_REQUEST['avatars']) ? ';sa=maintenance' : ';avatars'));
}
function RemoveAttachmentBySize()
{
global $smcFunc;
checkSession('post', 'admin');
$messages = removeAttachments(array('attachment_type' => 0, 'size' => 1024 * $_POST['size']), 'messages', true);
if (!empty($messages) && !empty($_POST['notice']))
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = CONCAT(body, {string:notice})
WHERE id_msg IN ({array_int:messages})',
array(
'messages' => $messages,
'notice' => '<br><br>' . $_POST['notice'],
)
);
redirectexit('action=admin;area=manageattachments;sa=maintenance');
}
function RemoveAttachment()
{
global $txt, $smcFunc, $language, $user_info;
checkSession();
if (!empty($_POST['remove']))
{
$attachments = array();
foreach ($_POST['remove'] as $removeID => $dummy)
$attachments[] = (int) $removeID;
$filesRemoved = false;
call_integration_hook('integrate_attachment_remove', array(&$filesRemoved, $attachments));
if ($_REQUEST['type'] == 'avatars' && !empty($attachments))
removeAttachments(array('id_attach' => $attachments));
elseif (!empty($attachments))
{
$messages = removeAttachments(array('id_attach' => $attachments), 'messages', true);
if (!empty($messages))
{
loadLanguage('index', $language, true);
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = CONCAT(body, {string:deleted_message})
WHERE id_msg IN ({array_int:messages_affected})',
array(
'messages_affected' => $messages,
'deleted_message' => '<br><br>' . $txt['attachment_delete_admin'],
)
);
loadLanguage('index', $user_info['language'], true);
}
}
}
$_GET['sort'] = isset($_GET['sort']) ? $_GET['sort'] : 'date';
redirectexit('action=admin;area=manageattachments;sa=browse;' . $_REQUEST['type'] . ';sort=' . $_GET['sort'] . (isset($_GET['desc']) ? ';desc' : '') . ';start=' . $_REQUEST['start']);
}
function RemoveAllAttachments()
{
global $txt, $smcFunc;
checkSession('get', 'admin');
$messages = removeAttachments(array('attachment_type' => 0), '', true);
if (!isset($_POST['notice']))
$_POST['notice'] = $txt['attachment_delete_admin'];
if (!empty($messages))
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET body = CONCAT(body, {string:deleted_message})
WHERE id_msg IN ({array_int:messages})',
array(
'messages' => $messages,
'deleted_message' => '<br><br>' . $_POST['notice'],
)
);
redirectexit('action=admin;area=manageattachments;sa=maintenance');
}
function removeAttachments($condition, $query_type = '', $return_affected_messages = false, $autoThumbRemoval = true)
{
global $modSettings, $smcFunc;
$new_condition = array();
$query_parameter = array(
'thumb_attachment_type' => 3,
);
$do_logging = array();
if (is_array($condition))
{
foreach ($condition as $real_type => $restriction)
{
$is_not = substr($real_type, 0, 4) == 'not_';
$type = $is_not ? substr($real_type, 4) : $real_type;
if (in_array($type, array('id_member', 'id_attach', 'id_msg')))
$new_condition[] = 'a.' . $type . ($is_not ? ' NOT' : '') . ' IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
elseif ($type == 'attachment_type')
$new_condition[] = 'a.attachment_type = {int:' . $real_type . '}';
elseif ($type == 'poster_time')
$new_condition[] = 'm.poster_time < {int:' . $real_type . '}';
elseif ($type == 'last_login')
$new_condition[] = 'mem.last_login < {int:' . $real_type . '}';
elseif ($type == 'size')
$new_condition[] = 'a.size > {int:' . $real_type . '}';
elseif ($type == 'id_topic')
$new_condition[] = 'm.id_topic IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
$query_parameter[$real_type] = $restriction;
if ($type == 'do_logging')
$do_logging = $condition['id_attach'];
}
$condition = implode(' AND ', $new_condition);
}
$msgs = array();
$attach = array();
$parents = array();
$request = $smcFunc['db_query']('', '
SELECT
a.id_folder, a.filename, a.file_hash, a.attachment_type, a.id_attach, a.id_member' . ($query_type == 'messages' ? ', m.id_msg' : ', a.id_msg') . ',
thumb.id_folder AS thumb_folder, COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.filename AS thumb_filename, thumb.file_hash AS thumb_file_hash, thumb_parent.id_attach AS id_parent
FROM {db_prefix}attachments AS a' . ($query_type == 'members' ? '
INNER JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)' : ($query_type == 'messages' ? '
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)' : '')) . '
LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
LEFT JOIN {db_prefix}attachments AS thumb_parent ON (thumb.attachment_type = {int:thumb_attachment_type} AND thumb_parent.id_thumb = a.id_attach)
WHERE ' . $condition,
$query_parameter
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($row['attachment_type'] == 1)
{
@unlink($modSettings['custom_avatar_dir'] . '/' . $row['filename']);
}
else
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
@unlink($filename);
if (!empty($row['id_parent']))
$parents[] = $row['id_parent'];
if (!empty($row['id_thumb']) && $autoThumbRemoval)
{
$thumb_filename = getAttachmentFilename($row['thumb_filename'], $row['id_thumb'], $row['thumb_folder'], false, $row['thumb_file_hash']);
@unlink($thumb_filename);
$attach[] = $row['id_thumb'];
}
}
if ($return_affected_messages && empty($row['attachment_type']))
$msgs[] = $row['id_msg'];
$attach[] = $row['id_attach'];
}
$smcFunc['db_free_result']($request);
$parents = array_diff($parents, $attach);
if (!empty($parents))
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_thumb = {int:no_thumb}
WHERE id_attach IN ({array_int:parent_attachments})',
array(
'parent_attachments' => $parents,
'no_thumb' => 0,
)
);
if (!empty($do_logging))
{
$request = $smcFunc['db_query']('', '
SELECT m.id_msg, a.filename
FROM {db_prefix}attachments AS a
INNER JOIN {db_prefix}messages AS m ON (a.id_msg = m.id_msg)
WHERE a.id_attach IN ({array_int:attachments})
AND a.attachment_type = {int:attachment_type}',
array(
'attachments' => $do_logging,
'attachment_type' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
logAction(
'remove_attach',
array(
'message' => $row['id_msg'],
'filename' => preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($row['filename'])),
)
);
$smcFunc['db_free_result']($request);
}
if (!empty($attach))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}attachments
WHERE id_attach IN ({array_int:attachment_list})',
array(
'attachment_list' => $attach,
)
);
call_integration_hook('integrate_remove_attachments', array($attach));
if ($return_affected_messages)
return array_unique($msgs);
}
function RepairAttachments()
{
global $modSettings, $context, $txt, $smcFunc;
checkSession('get');
if (isset($_POST['cancel']))
redirectexit('action=admin;area=manageattachments;sa=maintenance');
@set_time_limit(600);
$_GET['step'] = empty($_GET['step']) ? 0 : (int) $_GET['step'];
$context['starting_substep'] = $_GET['substep'] = empty($_GET['substep']) ? 0 : (int) $_GET['substep'];
if ($_GET['step'] == 0 && $_GET['substep'] == 0)
{
unset($_SESSION['attachments_to_fix'], $_SESSION['attachments_to_fix2']);
if (isset($_GET['fixErrors']))
{
if (empty($_POST['to_fix']))
redirectexit('action=admin;area=manageattachments;sa=maintenance');
$_SESSION['attachments_to_fix'] = array();
foreach ($_POST['to_fix'] as $value)
$_SESSION['attachments_to_fix'][] = $value;
}
}
$context['repair_errors'] = array(
'missing_thumbnail_parent' => 0,
'parent_missing_thumbnail' => 0,
'file_missing_on_disk' => 0,
'file_wrong_size' => 0,
'file_size_of_zero' => 0,
'attachment_no_msg' => 0,
'avatar_no_member' => 0,
'wrong_folder' => 0,
'files_without_attachment' => 0,
);
$to_fix = !empty($_SESSION['attachments_to_fix']) ? $_SESSION['attachments_to_fix'] : array();
$context['repair_errors'] = isset($_SESSION['attachments_to_fix2']) ? $_SESSION['attachments_to_fix2'] : $context['repair_errors'];
$fix_errors = isset($_GET['fixErrors']) ? true : false;
if ($_GET['step'] <= 0)
{
$result = $smcFunc['db_query']('', '
SELECT MAX(id_attach)
FROM {db_prefix}attachments
WHERE attachment_type = {int:thumbnail}',
array(
'thumbnail' => 3,
)
);
list ($thumbnails) = $smcFunc['db_fetch_row']($result);
$smcFunc['db_free_result']($result);
for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
{
$to_remove = array();
$result = $smcFunc['db_query']('', '
SELECT thumb.id_attach, thumb.id_folder, thumb.filename, thumb.file_hash
FROM {db_prefix}attachments AS thumb
LEFT JOIN {db_prefix}attachments AS tparent ON (tparent.id_thumb = thumb.id_attach)
WHERE thumb.id_attach BETWEEN {int:substep} AND {int:substep} + 499
AND thumb.attachment_type = {int:thumbnail}
AND tparent.id_attach IS NULL',
array(
'thumbnail' => 3,
'substep' => $_GET['substep'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
if (!isset($to_remove[$row['id_attach']]))
{
$to_remove[$row['id_attach']] = $row['id_attach'];
$context['repair_errors']['missing_thumbnail_parent']++;
if ($fix_errors && in_array('missing_thumbnail_parent', $to_fix))
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
@unlink($filename);
}
}
}
if ($smcFunc['db_num_rows']($result) != 0)
$to_fix[] = 'missing_thumbnail_parent';
$smcFunc['db_free_result']($result);
if ($fix_errors && !empty($to_remove) && in_array('missing_thumbnail_parent', $to_fix))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}attachments
WHERE id_attach IN ({array_int:to_remove})
AND attachment_type = {int:attachment_type}',
array(
'to_remove' => $to_remove,
'attachment_type' => 3,
)
);
pauseAttachmentMaintenance($to_fix, $thumbnails);
}
$_GET['step'] = 1;
$_GET['substep'] = 0;
pauseAttachmentMaintenance($to_fix);
}
if ($_GET['step'] <= 1)
{
$result = $smcFunc['db_query']('', '
SELECT MAX(id_attach)
FROM {db_prefix}attachments
WHERE id_thumb != {int:no_thumb}',
array(
'no_thumb' => 0,
)
);
list ($thumbnails) = $smcFunc['db_fetch_row']($result);
$smcFunc['db_free_result']($result);
for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
{
$to_update = array();
$result = $smcFunc['db_query']('', '
SELECT a.id_attach
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
AND a.id_thumb != {int:no_thumb}
AND thumb.id_attach IS NULL',
array(
'no_thumb' => 0,
'substep' => $_GET['substep'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$to_update[] = $row['id_attach'];
$context['repair_errors']['parent_missing_thumbnail']++;
}
if ($smcFunc['db_num_rows']($result) != 0)
$to_fix[] = 'parent_missing_thumbnail';
$smcFunc['db_free_result']($result);
if ($fix_errors && !empty($to_update) && in_array('parent_missing_thumbnail', $to_fix))
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_thumb = {int:no_thumb}
WHERE id_attach IN ({array_int:to_update})',
array(
'to_update' => $to_update,
'no_thumb' => 0,
)
);
pauseAttachmentMaintenance($to_fix, $thumbnails);
}
$_GET['step'] = 2;
$_GET['substep'] = 0;
pauseAttachmentMaintenance($to_fix);
}
if ($_GET['step'] <= 2)
{
$result = $smcFunc['db_query']('', '
SELECT MAX(id_attach)
FROM {db_prefix}attachments',
array(
)
);
list ($thumbnails) = $smcFunc['db_fetch_row']($result);
$smcFunc['db_free_result']($result);
for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 250)
{
$to_remove = array();
$errors_found = array();
$result = $smcFunc['db_query']('', '
SELECT id_attach, id_folder, filename, file_hash, size, attachment_type
FROM {db_prefix}attachments
WHERE id_attach BETWEEN {int:substep} AND {int:substep} + 249',
array(
'substep' => $_GET['substep'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
if ($row['attachment_type'] == 1)
$filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
else
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
if (!file_exists($filename))
{
if (!empty($modSettings['currentAttachmentUploadDir']))
{
$attachment_name = $row['id_attach'] . '_' . $row['file_hash'] . '.dat';
foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
if (file_exists($dir . '/' . $attachment_name))
{
$context['repair_errors']['wrong_folder']++;
$errors_found[] = 'wrong_folder';
if ($fix_errors && in_array('wrong_folder', $to_fix))
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_folder = {int:new_folder}
WHERE id_attach = {int:id_attach}',
array(
'new_folder' => $id,
'id_attach' => $row['id_attach'],
)
);
continue 2;
}
}
$to_remove[] = $row['id_attach'];
$context['repair_errors']['file_missing_on_disk']++;
$errors_found[] = 'file_missing_on_disk';
}
elseif (filesize($filename) == 0)
{
$context['repair_errors']['file_size_of_zero']++;
$errors_found[] = 'file_size_of_zero';
if ($fix_errors && in_array('file_size_of_zero', $to_fix))
{
$to_remove[] = $row['id_attach'];
@unlink($filename);
}
}
elseif (filesize($filename) != $row['size'])
{
$context['repair_errors']['file_wrong_size']++;
$errors_found[] = 'file_wrong_size';
if ($fix_errors && in_array('file_wrong_size', $to_fix))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET size = {int:filesize}
WHERE id_attach = {int:id_attach}',
array(
'filesize' => filesize($filename),
'id_attach' => $row['id_attach'],
)
);
}
}
}
if (in_array('file_missing_on_disk', $errors_found))
$to_fix[] = 'file_missing_on_disk';
if (in_array('file_size_of_zero', $errors_found))
$to_fix[] = 'file_size_of_zero';
if (in_array('file_wrong_size', $errors_found))
$to_fix[] = 'file_wrong_size';
if (in_array('wrong_folder', $errors_found))
$to_fix[] = 'wrong_folder';
$smcFunc['db_free_result']($result);
if ($fix_errors && !empty($to_remove))
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}attachments
WHERE id_attach IN ({array_int:to_remove})',
array(
'to_remove' => $to_remove,
)
);
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_thumb = {int:no_thumb}
WHERE id_thumb IN ({array_int:to_remove})',
array(
'to_remove' => $to_remove,
'no_thumb' => 0,
)
);
}
pauseAttachmentMaintenance($to_fix, $thumbnails);
}
$_GET['step'] = 3;
$_GET['substep'] = 0;
pauseAttachmentMaintenance($to_fix);
}
if ($_GET['step'] <= 3)
{
$result = $smcFunc['db_query']('', '
SELECT MAX(id_attach)
FROM {db_prefix}attachments',
array(
)
);
list ($thumbnails) = $smcFunc['db_fetch_row']($result);
$smcFunc['db_free_result']($result);
for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
{
$to_remove = array();
$result = $smcFunc['db_query']('', '
SELECT a.id_attach, a.id_folder, a.filename, a.file_hash, a.attachment_type
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
AND a.id_member != {int:no_member}
AND a.id_msg = {int:no_msg}
AND mem.id_member IS NULL',
array(
'no_member' => 0,
'no_msg' => 0,
'substep' => $_GET['substep'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$to_remove[] = $row['id_attach'];
$context['repair_errors']['avatar_no_member']++;
if ($fix_errors && in_array('avatar_no_member', $to_fix))
{
if ($row['attachment_type'] == 1)
$filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
else
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
@unlink($filename);
}
}
if ($smcFunc['db_num_rows']($result) != 0)
$to_fix[] = 'avatar_no_member';
$smcFunc['db_free_result']($result);
if ($fix_errors && !empty($to_remove) && in_array('avatar_no_member', $to_fix))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}attachments
WHERE id_attach IN ({array_int:to_remove})
AND id_member != {int:no_member}
AND id_msg = {int:no_msg}',
array(
'to_remove' => $to_remove,
'no_member' => 0,
'no_msg' => 0,
)
);
pauseAttachmentMaintenance($to_fix, $thumbnails);
}
$_GET['step'] = 4;
$_GET['substep'] = 0;
pauseAttachmentMaintenance($to_fix);
}
if ($_GET['step'] <= 4)
{
$result = $smcFunc['db_query']('', '
SELECT MAX(id_attach)
FROM {db_prefix}attachments',
array(
)
);
list ($thumbnails) = $smcFunc['db_fetch_row']($result);
$smcFunc['db_free_result']($result);
for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
{
$to_remove = array();
$ignore_ids = array(0);
call_integration_hook('integrate_repair_attachments_nomsg', array(&$ignore_ids, $_GET['substep'], $_GET['substep'] + 500));
$result = $smcFunc['db_query']('', '
SELECT a.id_attach, a.id_folder, a.filename, a.file_hash
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
AND a.id_member = {int:no_member}
AND (a.id_msg = {int:no_msg} OR m.id_msg IS NULL)
AND a.id_attach NOT IN ({array_int:ignore_ids})
AND a.attachment_type IN ({array_int:attach_thumb})',
array(
'no_member' => 0,
'no_msg' => 0,
'substep' => $_GET['substep'],
'ignore_ids' => $ignore_ids,
'attach_thumb' => array(0, 3),
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$to_remove[] = $row['id_attach'];
$context['repair_errors']['attachment_no_msg']++;
if ($fix_errors && in_array('attachment_no_msg', $to_fix))
{
$filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
@unlink($filename);
}
}
if ($smcFunc['db_num_rows']($result) != 0)
$to_fix[] = 'attachment_no_msg';
$smcFunc['db_free_result']($result);
if ($fix_errors && !empty($to_remove) && in_array('attachment_no_msg', $to_fix))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}attachments
WHERE id_attach IN ({array_int:to_remove})
AND id_member = {int:no_member}
AND attachment_type IN ({array_int:attach_thumb})',
array(
'to_remove' => $to_remove,
'no_member' => 0,
'attach_thumb' => array(0, 3),
)
);
pauseAttachmentMaintenance($to_fix, $thumbnails);
}
$_GET['step'] = 5;
$_GET['substep'] = 0;
pauseAttachmentMaintenance($to_fix);
}
if ($_GET['step'] <= 5)
{
$attach_dirs = $modSettings['attachmentUploadDir'];
$current_check = 0;
$max_checks = 500;
$files_checked = empty($_GET['substep']) ? 0 : $_GET['substep'];
foreach ($attach_dirs as $attach_dir)
{
if ($dir = @opendir($attach_dir))
{
while ($file = readdir($dir))
{
if (in_array($file, array('.', '..', '.htaccess', 'index.php')))
continue;
if ($files_checked <= $current_check)
{
if (strpos($file, 'post_tmp_') !== false)
{
if (filemtime($attach_dir . '/' . $file) < time() - 18000)
@unlink($attach_dir . '/' . $file);
}
elseif (strpos($file, '_') !== false)
{
$attachID = (int) substr($file, 0, strpos($file, '_'));
if (!empty($attachID))
{
$request = $smcFunc['db_query']('', '
SELECT id_attach
FROM {db_prefix}attachments
WHERE id_attach = {int:attachment_id}
LIMIT 1',
array(
'attachment_id' => $attachID,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
if ($fix_errors && in_array('files_without_attachment', $to_fix))
{
@unlink($attach_dir . '/' . $file);
}
else
{
$context['repair_errors']['files_without_attachment']++;
$to_fix[] = 'files_without_attachment';
}
}
$smcFunc['db_free_result']($request);
}
}
else
{
if ($fix_errors && in_array('files_without_attachment', $to_fix))
{
@unlink($attach_dir . '/' . $file);
}
else
{
$context['repair_errors']['files_without_attachment']++;
$to_fix[] = 'files_without_attachment';
}
}
}
$current_check++;
$_GET['substep'] = $current_check;
if ($current_check - $files_checked >= $max_checks)
pauseAttachmentMaintenance($to_fix);
}
closedir($dir);
}
}
$_GET['step'] = 5;
$_GET['substep'] = 0;
pauseAttachmentMaintenance($to_fix);
}
$context['page_title'] = $txt['repair_attachments'];
$context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
$context['sub_template'] = 'attachment_repair';
$context['completed'] = $fix_errors ? true : false;
$context['errors_found'] = !empty($to_fix) ? true : false;
}
function pauseAttachmentMaintenance($to_fix, $max_substep = 0)
{
global $context, $txt;
@set_time_limit(600);
if (function_exists('apache_reset_timeout'))
@apache_reset_timeout();
if ((time() - TIME_START) < 3 || $context['starting_substep'] == $_GET['substep'])
return;
$context['continue_get_data'] = '?action=admin;area=manageattachments;sa=repair' . (isset($_GET['fixErrors']) ? ';fixErrors' : '') . ';step=' . $_GET['step'] . ';substep=' . $_GET['substep'] . ';' . $context['session_var'] . '=' . $context['session_id'];
$context['page_title'] = $txt['not_done_title'];
$context['continue_post_data'] = '';
$context['continue_countdown'] = '2';
$context['sub_template'] = 'not_done';
$context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
if (empty($max_substep))
$context['continue_percent'] = round(($_GET['step'] * 100) / 25);
else
$context['continue_percent'] = round(($_GET['step'] * 100 + ($_GET['substep'] * 100) / $max_substep) / 25);
$context['continue_percent'] = min($context['continue_percent'], 100);
$_SESSION['attachments_to_fix'] = $to_fix;
$_SESSION['attachments_to_fix2'] = $context['repair_errors'];
obExit();
}
function ApproveAttach()
{
global $smcFunc;
checkSession('get');
$is_approve = !isset($_GET['sa']) || $_GET['sa'] != 'reject' ? true : false;
$attachments = array();
if ($_GET['sa'] == 'all' && !empty($_GET['mid']))
{
$id_msg = (int) $_GET['mid'];
$request = $smcFunc['db_query']('', '
SELECT id_attach
FROM {db_prefix}attachments
WHERE id_msg = {int:id_msg}
AND approved = {int:is_approved}
AND attachment_type = {int:attachment_type}',
array(
'id_msg' => $id_msg,
'is_approved' => 0,
'attachment_type' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$attachments[] = $row['id_attach'];
$smcFunc['db_free_result']($request);
}
elseif (!empty($_GET['aid']))
$attachments[] = (int) $_GET['aid'];
if (empty($attachments))
fatal_lang_error('no_access', false);
$allowed_boards = boardsAllowedTo('approve_posts');
$request = $smcFunc['db_query']('', '
SELECT a.id_attach, m.id_board, m.id_msg, m.id_topic
FROM {db_prefix}attachments AS a
INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
WHERE a.id_attach IN ({array_int:attachments})
AND a.attachment_type = {int:attachment_type}
AND a.approved = {int:is_approved}',
array(
'attachments' => $attachments,
'attachment_type' => 0,
'is_approved' => 0,
)
);
$attachments = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($allowed_boards = array(0) || in_array($row['id_board'], $allowed_boards))
{
$attachments[] = $row['id_attach'];
$redirect = 'topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'];
}
}
$smcFunc['db_free_result']($request);
if (empty($attachments))
fatal_lang_error('no_access', false);
if ($is_approve)
{
ApproveAttachments($attachments);
}
else
removeAttachments(array('id_attach' => $attachments, 'do_logging' => true));
redirectexit($redirect);
}
function ApproveAttachments($attachments)
{
global $smcFunc;
if (empty($attachments))
return 0;
$request = $smcFunc['db_query']('', '
SELECT
a.id_attach, a.id_member, COALESCE(thumb.id_attach, 0) AS id_thumb
FROM {db_prefix}attachments AS a
LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
WHERE a.id_attach IN ({array_int:attachments})
AND a.attachment_type = {int:attachment_type}',
array(
'attachments' => $attachments,
'attachment_type' => 0,
)
);
$attachments = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (!empty($row['id_thumb']))
$attachments[] = $row['id_thumb'];
$attachments[] = $row['id_attach'];
}
$smcFunc['db_free_result']($request);
if (empty($attachments))
return 0;
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET approved = {int:is_approved}
WHERE id_attach IN ({array_int:attachments})',
array(
'attachments' => $attachments,
'is_approved' => 1,
)
);
$request = $smcFunc['db_query']('', '
SELECT m.id_msg, a.filename
FROM {db_prefix}attachments AS a
INNER JOIN {db_prefix}messages AS m ON (a.id_msg = m.id_msg)
WHERE a.id_attach IN ({array_int:attachments})
AND a.attachment_type = {int:attachment_type}',
array(
'attachments' => $attachments,
'attachment_type' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
logAction(
'approve_attach',
array(
'message' => $row['id_msg'],
'filename' => preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($row['filename'])),
)
);
$smcFunc['db_free_result']($request);
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}approval_queue
WHERE id_attach IN ({array_int:attachments})',
array(
'attachments' => $attachments,
)
);
call_integration_hook('integrate_approve_attachments', array($attachments));
}
function ManageAttachmentPaths()
{
global $modSettings, $scripturl, $context, $txt, $sourcedir, $boarddir, $smcFunc, $settings;
if (!isset($modSettings['attachment_basedirectories']))
$modSettings['attachment_basedirectories'] = array();
elseif (!is_array($modSettings['attachment_basedirectories']))
$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
$errors = array();
if (isset($_REQUEST['save']))
{
checkSession();
$_POST['current_dir'] = (int) $_POST['current_dir'];
$new_dirs = array();
foreach ($_POST['dirs'] as $id => $path)
{
$error = '';
$id = (int) $id;
if ($id < 1)
continue;
$invalid_dirs = array($boarddir, $settings['default_theme_dir'], $sourcedir);
if (in_array($path, $invalid_dirs))
{
$errors[] = $path . ': ' . $txt['attach_dir_invalid'];
continue;
}
if (!array_key_exists($id, $modSettings['attachmentUploadDir']) && !empty($path))
{
if (in_array($path, $modSettings['attachmentUploadDir']) || in_array($boarddir . DIRECTORY_SEPARATOR . $path, $modSettings['attachmentUploadDir']))
{
$errors[] = $path . ': ' . $txt['attach_dir_duplicate_msg'];
continue;
}
elseif (empty($path))
{
continue;
}
require_once($sourcedir . '/Subs-Attachments.php');
if (automanage_attachments_create_directory($path))
$_POST['current_dir'] = $modSettings['currentAttachmentUploadDir'];
else
$errors[] = $path . ': ' . $txt[$context['dir_creation_error']];
}
if (!empty($modSettings['attachmentUploadDir'][$id]) && !empty($path) && $path != $modSettings['attachmentUploadDir'][$id])
{
if ($path != $modSettings['attachmentUploadDir'][$id] && !is_dir($path))
{
if (!@rename($modSettings['attachmentUploadDir'][$id], $path))
{
$errors[] = $path . ': ' . $txt['attach_dir_no_rename'];
$path = $modSettings['attachmentUploadDir'][$id];
}
}
else
{
$errors[] = $path . ': ' . $txt['attach_dir_exists_msg'];
$path = $modSettings['attachmentUploadDir'][$id];
}
if (!empty($modSettings['attachment_basedirectories']) && array_key_exists($id, $modSettings['attachment_basedirectories']))
{
$base = $modSettings['basedirectory_for_attachments'] == $modSettings['attachmentUploadDir'][$id] ? $path : $modSettings['basedirectory_for_attachments'];
$modSettings['attachment_basedirectories'][$id] = $path;
updateSettings(array(
'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
'basedirectory_for_attachments' => $base,
));
$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
}
}
if (empty($path))
{
$path = $modSettings['attachmentUploadDir'][$id];
if ($id == (!empty($_POST['current_dir']) ? $_POST['current_dir'] : $modSettings['currentAttachmentUploadDir']))
$errors[] = $path . ': ' . $txt['attach_dir_is_current'];
elseif (!empty($modSettings['basedirectory_for_attachments']) && $modSettings['basedirectory_for_attachments'] == $modSettings['attachmentUploadDir'][$id])
$errors[] = $path . ': ' . $txt['attach_dir_is_current_bd'];
else
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(id_attach) AS num_attach
FROM {db_prefix}attachments
WHERE id_folder = {int:id_folder}',
array(
'id_folder' => (int) $id,
)
);
list ($num_attach) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
if (!empty($modSettings['attachment_basedirectories']))
{
foreach ($modSettings['attachmentUploadDir'] as $sub)
if (strpos($sub, $path . DIRECTORY_SEPARATOR) !== false)
$num_attach++;
}
if ($num_attach == 0)
{
if (is_dir($path))
$doit = true;
elseif (is_dir($boarddir . DIRECTORY_SEPARATOR . $path))
{
$doit = true;
$path = $boarddir . DIRECTORY_SEPARATOR . $path;
}
if (isset($doit) && realpath($path) != realpath($boarddir))
{
unlink($path . '/.htaccess');
unlink($path . '/index.php');
if (!@rmdir($path))
$error = $path . ': ' . $txt['attach_dir_no_delete'];
}
if (empty($error) && !empty($modSettings['attachment_basedirectories']))
{
unset($modSettings['attachment_basedirectories'][$id]);
updateSettings(array('attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories'])));
$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
}
}
else
$error = $path . ': ' . $txt['attach_dir_no_remove'];
if (empty($error))
continue;
else
$errors[] = $error;
}
}
$new_dirs[$id] = $path;
}
if (empty($_POST['current_dir']) && !empty($modSettings['currentAttachmentUploadDir']))
$_POST['current_dir'] = $modSettings['currentAttachmentUploadDir'];
if (empty($_POST['current_dir']) || empty($new_dirs[$_POST['current_dir']]))
{
if (array_key_exists($modSettings['currentAttachmentUploadDir'], $modSettings['attachmentUploadDir']))
$_POST['current_dir'] = $modSettings['currentAttachmentUploadDir'];
else
$_POST['current_dir'] = max(array_keys($modSettings['attachmentUploadDir']));
}
if ($_POST['current_dir'] != $modSettings['currentAttachmentUploadDir'] && !empty($modSettings['last_attachments_directory']) && (isset($modSettings['last_attachments_directory'][$_POST['current_dir']]) || isset($modSettings['last_attachments_directory'][0])))
{
if (!is_array($modSettings['last_attachments_directory']))
$modSettings['last_attachments_directory'] = $smcFunc['json_decode']($modSettings['last_attachments_directory'], true);
$num = substr(strrchr($modSettings['attachmentUploadDir'][$_POST['current_dir']], '_'), 1);
if (is_numeric($num))
{
$bid = -1;
$use_subdirectories_for_attachments = 0;
if (!empty($modSettings['attachment_basedirectories']))
foreach ($modSettings['attachment_basedirectories'] as $bid => $base)
if (strpos($modSettings['attachmentUploadDir'][$_POST['current_dir']], $base . DIRECTORY_SEPARATOR) !== false)
{
$use_subdirectories_for_attachments = 1;
break;
}
if ($use_subdirectories_for_attachments == 0 && strpos($modSettings['attachmentUploadDir'][$_POST['current_dir']], $boarddir . DIRECTORY_SEPARATOR) !== false)
$bid = 0;
$modSettings['last_attachments_directory'][$bid] = (int) $num;
$modSettings['basedirectory_for_attachments'] = !empty($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : '';
$modSettings['use_subdirectories_for_attachments'] = !empty($modSettings['use_subdirectories_for_attachments']) ? $modSettings['use_subdirectories_for_attachments'] : 0;
updateSettings(array(
'last_attachments_directory' => $smcFunc['json_encode']($modSettings['last_attachments_directory']),
'basedirectory_for_attachments' => $bid == 0 ? $modSettings['basedirectory_for_attachments'] : $modSettings['attachment_basedirectories'][$bid],
'use_subdirectories_for_attachments' => $use_subdirectories_for_attachments,
));
}
}
if (count($new_dirs) == 1)
{
foreach ($new_dirs as $id => $dir)
{
if ($id != 1)
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_folder = {int:default_folder}
WHERE id_folder = {int:current_folder}',
array(
'default_folder' => 1,
'current_folder' => $id,
)
);
$update = array(
'currentAttachmentUploadDir' => 1,
'attachmentUploadDir' => $smcFunc['json_encode'](array(1 => $dir)),
);
}
}
else
{
$update = array(
'currentAttachmentUploadDir' => $_POST['current_dir'],
'attachmentUploadDir' => $smcFunc['json_encode']($new_dirs),
);
}
if (!empty($update))
updateSettings($update);
if (!empty($errors))
$_SESSION['errors']['dir'] = $errors;
redirectexit('action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id']);
}
if (isset($_REQUEST['save2']))
{
checkSession();
$_POST['current_base_dir'] = isset($_POST['current_base_dir']) ? (int) $_POST['current_base_dir'] : 1;
if (empty($_POST['new_base_dir']) && !empty($_POST['current_base_dir']))
{
if ($modSettings['basedirectory_for_attachments'] != $modSettings['attachmentUploadDir'][$_POST['current_base_dir']])
$update = (array(
'basedirectory_for_attachments' => $modSettings['attachmentUploadDir'][$_POST['current_base_dir']],
));
}
if (isset($_POST['base_dir']))
{
foreach ($_POST['base_dir'] as $id => $dir)
{
if (!empty($dir) && $dir != $modSettings['attachmentUploadDir'][$id])
{
if (@rename($modSettings['attachmentUploadDir'][$id], $dir))
{
$modSettings['attachmentUploadDir'][$id] = $dir;
$modSettings['attachment_basedirectories'][$id] = $dir;
$update = (array(
'attachmentUploadDir' => $smcFunc['json_encode']($modSettings['attachmentUploadDir']),
'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
'basedirectory_for_attachments' => $modSettings['attachmentUploadDir'][$_POST['current_base_dir']],
));
}
}
if (empty($dir))
{
if ($id == $_POST['current_base_dir'])
{
$errors[] = $modSettings['attachmentUploadDir'][$id] . ': ' . $txt['attach_dir_is_current'];
continue;
}
unset($modSettings['attachment_basedirectories'][$id]);
$update = (array(
'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
'basedirectory_for_attachments' => $modSettings['attachmentUploadDir'][$_POST['current_base_dir']],
));
}
}
}
if (!empty($_POST['new_base_dir']))
{
require_once($sourcedir . '/Subs-Attachments.php');
$_POST['new_base_dir'] = $smcFunc['htmlspecialchars']($_POST['new_base_dir'], ENT_QUOTES);
$current_dir = $modSettings['currentAttachmentUploadDir'];
if (!in_array($_POST['new_base_dir'], $modSettings['attachmentUploadDir']))
{
if (!automanage_attachments_create_directory($_POST['new_base_dir']))
$errors[] = $_POST['new_base_dir'] . ': ' . $txt['attach_dir_base_no_create'];
}
$modSettings['currentAttachmentUploadDir'] = array_search($_POST['new_base_dir'], $modSettings['attachmentUploadDir']);
if (!in_array($_POST['new_base_dir'], $modSettings['attachment_basedirectories']))
$modSettings['attachment_basedirectories'][$modSettings['currentAttachmentUploadDir']] = $_POST['new_base_dir'];
ksort($modSettings['attachment_basedirectories']);
$update = (array(
'attachment_basedirectories' => $smcFunc['json_encode']($modSettings['attachment_basedirectories']),
'basedirectory_for_attachments' => $_POST['new_base_dir'],
'currentAttachmentUploadDir' => $current_dir,
));
}
if (!empty($errors))
$_SESSION['errors']['base'] = $errors;
if (!empty($update))
updateSettings($update);
redirectexit('action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id']);
}
if (isset($_SESSION['errors']))
{
if (is_array($_SESSION['errors']))
{
$errors = array();
if (!empty($_SESSION['errors']['dir']))
foreach ($_SESSION['errors']['dir'] as $error)
$errors['dir'][] = $smcFunc['htmlspecialchars']($error, ENT_QUOTES);
if (!empty($_SESSION['errors']['base']))
foreach ($_SESSION['errors']['base'] as $error)
$errors['base'][] = $smcFunc['htmlspecialchars']($error, ENT_QUOTES);
}
unset($_SESSION['errors']);
}
$listOptions = array(
'id' => 'attach_paths',
'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
'title' => $txt['attach_paths'],
'get_items' => array(
'function' => 'list_getAttachDirs',
),
'columns' => array(
'current_dir' => array(
'header' => array(
'value' => $txt['attach_current'],
'class' => 'centercol',
),
'data' => array(
'function' => function($rowData)
{
return '<input type="radio" name="current_dir" value="' . $rowData['id'] . '"' . ($rowData['current'] ? ' checked' : '') . (!empty($rowData['disable_current']) ? ' disabled' : '') . '>';
},
'style' => 'width: 10%;',
'class' => 'centercol',
),
),
'path' => array(
'header' => array(
'value' => $txt['attach_path'],
),
'data' => array(
'function' => function($rowData)
{
return '<input type="hidden" name="dirs[' . $rowData['id'] . ']" value="' . $rowData['path'] . '"><input type="text" size="40" name="dirs[' . $rowData['id'] . ']" value="' . $rowData['path'] . '"' . (!empty($rowData['disable_base_dir']) ? ' disabled' : '') . ' style="width: 100%">';
},
'style' => 'width: 40%;',
),
),
'current_size' => array(
'header' => array(
'value' => $txt['attach_current_size'],
),
'data' => array(
'db' => 'current_size',
'style' => 'width: 15%;',
),
),
'num_files' => array(
'header' => array(
'value' => $txt['attach_num_files'],
),
'data' => array(
'db' => 'num_files',
'style' => 'width: 15%;',
),
),
'status' => array(
'header' => array(
'value' => $txt['attach_dir_status'],
'class' => 'centercol',
),
'data' => array(
'db' => 'status',
'style' => 'width: 25%;',
'class' => 'centercol',
),
),
),
'form' => array(
'href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
),
'additional_rows' => array(
array(
'position' => 'below_table_data',
'value' => '
<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '">
<input type="submit" name="save" value="' . $txt['save'] . '" class="button">
<input type="submit" name="new_path" value="' . $txt['attach_add_path'] . '" class="button">',
),
empty($errors['dir']) ? array(
'position' => 'top_of_list',
'value' => $txt['attach_dir_desc'],
'class' => 'information'
) : array(
'position' => 'top_of_list',
'value' => $txt['attach_dir_save_problem'] . '<br>' . implode('<br>', $errors['dir']),
'style' => 'padding-left: 35px;',
'class' => 'noticebox',
),
),
);
require_once($sourcedir . '/Subs-List.php');
createList($listOptions);
if (!empty($modSettings['attachment_basedirectories']))
{
$listOptions2 = array(
'id' => 'base_paths',
'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
'title' => $txt['attach_base_paths'],
'get_items' => array(
'function' => 'list_getBaseDirs',
),
'columns' => array(
'current_dir' => array(
'header' => array(
'value' => $txt['attach_current'],
'class' => 'centercol',
),
'data' => array(
'function' => function($rowData)
{
return '<input type="radio" name="current_base_dir" value="' . $rowData['id'] . '"' . ($rowData['current'] ? ' checked' : '') . '>';
},
'style' => 'width: 10%;',
'class' => 'centercol',
),
),
'path' => array(
'header' => array(
'value' => $txt['attach_path'],
),
'data' => array(
'db' => 'path',
'style' => 'width: 45%;',
),
),
'num_dirs' => array(
'header' => array(
'value' => $txt['attach_num_dirs'],
),
'data' => array(
'db' => 'num_dirs',
'style' => 'width: 15%;',
),
),
'status' => array(
'header' => array(
'value' => $txt['attach_dir_status'],
),
'data' => array(
'db' => 'status',
'style' => 'width: 15%;',
'class' => 'centercol',
),
),
),
'form' => array(
'href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
),
'additional_rows' => array(
array(
'position' => 'below_table_data',
'value' => '<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '"><input type="submit" name="save2" value="' . $txt['save'] . '" class="button">
<input type="submit" name="new_base_path" value="' . $txt['attach_add_path'] . '" class="button">',
),
empty($errors['base']) ? array(
'position' => 'top_of_list',
'value' => $txt['attach_dir_base_desc'],
'style' => 'padding: 5px 10px;',
'class' => 'windowbg smalltext'
) : array(
'position' => 'top_of_list',
'value' => $txt['attach_dir_save_problem'] . '<br>' . implode('<br>', $errors['base']),
'style' => 'padding-left: 35px',
'class' => 'noticebox',
),
),
);
createList($listOptions2);
}
$context[$context['admin_menu_name']]['current_subsection'] = 'attachpaths';
$context['page_title'] = $txt['attach_path_manage'];
$context['sub_template'] = 'attachment_paths';
}
function list_getAttachDirs()
{
global $smcFunc, $modSettings, $context, $txt;
$request = $smcFunc['db_query']('', '
SELECT id_folder, COUNT(id_attach) AS num_attach, SUM(size) AS size_attach
FROM {db_prefix}attachments
WHERE attachment_type != {int:type}
GROUP BY id_folder',
array(
'type' => 1,
)
);
$expected_files = array();
$expected_size = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$expected_files[$row['id_folder']] = $row['num_attach'];
$expected_size[$row['id_folder']] = $row['size_attach'];
}
$smcFunc['db_free_result']($request);
$attachdirs = array();
foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
{
if (!isset($expected_files[$id]))
$expected_files[$id] = 0;
list ($status, $error, $files) = attachDirStatus($dir, $expected_files[$id]);
$sub_dirs = 0;
$is_base_dir = false;
if (!empty($modSettings['attachment_basedirectories']))
{
$is_base_dir = in_array($dir, $modSettings['attachment_basedirectories']);
foreach ($modSettings['attachmentUploadDir'] as $sid => $sub)
if (strpos($sub, $dir . DIRECTORY_SEPARATOR) !== false)
{
$expected_files[$id]++;
$sub_dirs++;
}
}
$attachdirs[] = array(
'id' => $id,
'current' => $id == $modSettings['currentAttachmentUploadDir'],
'disable_current' => isset($modSettings['automanage_attachments']) && $modSettings['automanage_attachments'] > 1,
'disable_base_dir' => $is_base_dir && $sub_dirs > 0 && !empty($files) && empty($error) && empty($save_errors),
'path' => $dir,
'current_size' => !empty($expected_size[$id]) ? comma_format($expected_size[$id] / 1024, 0) : 0,
'num_files' => comma_format($expected_files[$id] - $sub_dirs, 0) . ($sub_dirs > 0 ? ' (' . $sub_dirs . ')' : ''),
'status' => ($is_base_dir ? $txt['attach_dir_basedir'] . '<br>' : '') . ($error ? '<div class="error">' : '') . sprintf($txt['attach_dir_' . $status], $context['session_id'], $context['session_var']) . ($error ? '</div>' : ''),
);
}
if (isset($_REQUEST['new_path']))
$attachdirs[] = array(
'id' => max(array_merge(array_keys($expected_files), array_keys($modSettings['attachmentUploadDir']))) + 1,
'current' => false,
'path' => '',
'current_size' => '',
'num_files' => '',
'status' => '',
);
return $attachdirs;
}
function list_getBaseDirs()
{
global $modSettings, $txt;
if (empty($modSettings['attachment_basedirectories']))
return;
$basedirs = array();
foreach ($modSettings['attachment_basedirectories'] as $id => $dir)
{
$expected_dirs = 0;
foreach ($modSettings['attachmentUploadDir'] as $sid => $sub)
if (strpos($sub, $dir . DIRECTORY_SEPARATOR) !== false)
$expected_dirs++;
if (!is_dir($dir))
$status = 'does_not_exist';
elseif (!is_writeable($dir))
$status = 'not_writable';
else
$status = 'ok';
$basedirs[] = array(
'id' => $id,
'current' => $dir == $modSettings['basedirectory_for_attachments'],
'path' => $expected_dirs > 0 ? $dir : ('<input type="text" name="base_dir[' . $id . ']" value="' . $dir . '" size="40">'),
'num_dirs' => $expected_dirs,
'status' => $status == 'ok' ? $txt['attach_dir_ok'] : ('<span class="error">' . $txt['attach_dir_' . $status] . '</span>'),
);
}
if (isset($_REQUEST['new_base_path']))
$basedirs[] = array(
'id' => '',
'current' => false,
'path' => '<input type="text" name="new_base_dir" value="" size="40">',
'num_dirs' => '',
'status' => '',
);
return $basedirs;
}
function attachDirStatus($dir, $expected_files)
{
if (!is_dir($dir))
return array('does_not_exist', true, '');
elseif (!is_writable($dir))
return array('not_writable', true, '');
$num_files = 0;
$dir_handle = dir($dir);
while ($file = $dir_handle->read())
{
if (in_array($file, array('.', '..', '.htaccess', 'index.php')))
continue;
$num_files++;
}
$dir_handle->close();
if ($num_files < $expected_files)
return array('files_missing', true, $num_files);
elseif ($expected_files == 0)
return array('unused', false, $num_files);
else
return array('ok', false, $num_files);
}
function TransferAttachments()
{
global $modSettings, $smcFunc, $sourcedir, $txt, $boarddir;
checkSession();
if (!empty($modSettings['attachment_basedirectories']))
$modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true);
else
$modSettings['basedirectory_for_attachments'] = array();
$_POST['from'] = (int) $_POST['from'];
$_POST['auto'] = !empty($_POST['auto']) ? (int) $_POST['auto'] : 0;
$_POST['to'] = (int) $_POST['to'];
$start = !empty($_POST['empty_it']) ? 0 : $modSettings['attachmentDirFileLimit'];
$_SESSION['checked'] = !empty($_POST['empty_it']) ? true : false;
$limit = 501;
$results = array();
$dir_files = 0;
$current_progress = 0;
$total_moved = 0;
$total_not_moved = 0;
if (empty($_POST['from']) || (empty($_POST['auto']) && empty($_POST['to'])))
$results[] = $txt['attachment_transfer_no_dir'];
if ($_POST['from'] == $_POST['to'])
$results[] = $txt['attachment_transfer_same_dir'];
if (empty($results))
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}attachments
WHERE id_folder = {int:folder_id}
AND attachment_type != {int:attachment_type}',
array(
'folder_id' => $_POST['from'],
'attachment_type' => 1,
)
);
list ($total_progress) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$total_progress -= $start;
if ($total_progress < 1)
$results[] = $txt['attachment_transfer_no_find'];
}
if (empty($results))
{
if (!empty($_POST['auto']))
{
require_once($sourcedir . '/Subs-Attachments.php');
$modSettings['automanage_attachments'] = 1;
$modSettings['use_subdirectories_for_attachments'] = $_POST['auto'] == -1 ? 0 : 1;
$modSettings['basedirectory_for_attachments'] = $_POST['auto'] > 0 ? $modSettings['attachmentUploadDir'][$_POST['auto']] : $modSettings['basedirectory_for_attachments'];
automanage_attachments_check_directory();
$new_dir = $modSettings['currentAttachmentUploadDir'];
}
else
$new_dir = $_POST['to'];
$modSettings['currentAttachmentUploadDir'] = $new_dir;
$break = false;
while ($break == false)
{
@set_time_limit(300);
if (function_exists('apache_reset_timeout'))
@apache_reset_timeout();
if ($dir_files <= 0 && (!empty($modSettings['attachmentDirSizeLimit']) || !empty($modSettings['attachmentDirFileLimit'])))
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(*), SUM(size)
FROM {db_prefix}attachments
WHERE id_folder = {int:folder_id}
AND attachment_type != {int:attachment_type}',
array(
'folder_id' => $new_dir,
'attachment_type' => 1,
)
);
list ($dir_files, $dir_size) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
$request = $smcFunc['db_query']('', '
SELECT id_attach, filename, id_folder, file_hash, size
FROM {db_prefix}attachments
WHERE id_folder = {int:folder}
AND attachment_type != {int:attachment_type}
LIMIT {int:start}, {int:limit}',
array(
'folder' => $_POST['from'],
'attachment_type' => 1,
'start' => $start,
'limit' => $limit,
)
);
if ($smcFunc['db_num_rows']($request) === 0)
{
if (empty($current_progress))
$results[] = $txt['attachment_transfer_no_find'];
break;
}
if ($smcFunc['db_num_rows']($request) < $limit)
$break = true;
$moved = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$source = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
$dest = $modSettings['attachmentUploadDir'][$new_dir] . '/' . basename($source);
if (!empty($modSettings['attachmentDirSizeLimit']) || !empty($modSettings['attachmentDirFileLimit']))
{
$dir_files++;
$dir_size += !empty($row['size']) ? $row['size'] : filesize($source);
if (!empty($modSettings['attachmentDirSizeLimit']) && $dir_size > $modSettings['attachmentDirSizeLimit'] * 1024 || (!empty($modSettings['attachmentDirFileLimit']) && $dir_files > $modSettings['attachmentDirFileLimit']))
{
if (!empty($_POST['auto']))
{
automanage_attachments_by_space();
$results[] = sprintf($txt['attachments_transferred'], $total_moved, $modSettings['attachmentUploadDir'][$new_dir]);
if (!empty($total_not_moved))
$results[] = sprintf($txt['attachments_not_transferred'], $total_not_moved);
$dir_files = 0;
$total_moved = 0;
$total_not_moved = 0;
$break = false;
break;
}
else
{
$results[] = $txt['attachment_transfer_no_room'];
$break = true;
break;
}
}
}
if (@rename($source, $dest))
{
$total_moved++;
$current_progress++;
$moved[] = $row['id_attach'];
}
else
$total_not_moved++;
}
$smcFunc['db_free_result']($request);
if (!empty($moved))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_folder = {int:new}
WHERE id_attach IN ({array_int:attachments})',
array(
'attachments' => $moved,
'new' => $new_dir,
)
);
}
$new_dir = $modSettings['currentAttachmentUploadDir'];
if (!$break)
{
$percent_done = min(round($current_progress / $total_progress * 100, 0), 100);
$prog_bar = '
<div class="progress_bar">
<div class="bar" style="width: ' . $percent_done . '%;"></div>
<span>' . $percent_done . '%</span>
</div>';
$fp = fopen($boarddir . '/progress.php', "w");
fwrite($fp, $prog_bar);
fclose($fp);
usleep(500000);
}
}
$results[] = sprintf($txt['attachments_transferred'], $total_moved, $modSettings['attachmentUploadDir'][$new_dir]);
if (!empty($total_not_moved))
$results[] = sprintf($txt['attachments_not_transferred'], $total_not_moved);
}
$_SESSION['results'] = $results;
if (file_exists($boarddir . '/progress.php'))
unlink($boarddir . '/progress.php');
redirectexit('action=admin;area=manageattachments;sa=maintenance#transfer');
}
?>