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: 2825: 2826: 2827: 2828: 2829: 2830: 2831: 2832: 2833: 2834: 2835: 2836: 2837: 2838: 2839: 2840: 2841: 2842: 2843: 2844: 2845: 2846: 2847: 2848: 2849: 2850: 2851: 2852: 2853: 2854: 2855: 2856: 2857: 2858: 2859: 2860: 2861: 2862: 2863: 2864: 2865: 2866: 2867: 2868: 2869: 2870: 2871: 2872: 2873: 2874: 2875: 2876: 2877: 2878: 2879: 2880: 2881: 2882: 2883: 2884: 2885: 2886: 2887: 2888: 2889: 2890: 2891: 2892: 2893: 2894: 2895: 2896: 2897: 2898: 2899: 2900: 2901: 2902: 2903: 2904: 2905: 2906: 2907: 2908: 2909: 2910: 2911: 2912: 2913: 2914: 2915: 2916: 2917: 2918: 2919: 2920: 2921: 2922: 2923: 2924: 2925: 2926: 2927: 2928: 2929: 2930: 2931: 2932: 2933: 2934: 2935: 2936: 2937: 2938: 2939: 2940: 2941: 2942: 2943: 2944: 2945: 2946: 2947: 2948: 2949: 2950: 2951: 2952: 2953: 2954: 2955: 2956: 2957: 2958: 2959: 2960: 2961: 2962: 2963: 2964: 2965: 2966: 2967: 2968: 2969: 2970: 2971: 2972: 2973: 2974: 2975: 2976: 2977: 2978: 2979: 2980: 2981: 2982: 2983: 2984: 2985: 2986: 2987: 2988: 2989: 2990: 2991: 2992: 2993: 2994: 2995: 2996: 2997: 2998: 2999: 3000: 3001: 3002: 3003: 3004: 3005: 3006: 3007: 3008: 3009: 3010: 3011: 3012: 3013: 3014: 3015: 3016: 3017: 3018: 3019: 3020: 3021: 3022:
<?php
if (!defined('SMF'))
die('No direct access...');
function preparsecode(&$message, $previewing = false)
{
global $user_info, $modSettings, $context, $sourcedir;
static $tags_regex, $disallowed_tags_regex;
if (empty($context['utf8']))
$message = preg_replace('~&#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $message);
$message = preg_replace_callback('~\[nobbc\](.+?)\[/nobbc\]~is', function($a)
{
return '[nobbc]' . strtr($a[1], array('[' => '[', ']' => ']', ':' => ':', '@' => '@')) . '[/nobbc]';
}, $message);
$message = strtr($message, array("\r" => ''));
$message = preg_replace('~\.{100,}~', '...', $message);
while (substr($message, -7) == '[quote]')
$message = substr($message, 0, -7);
while (substr($message, 0, 8) == '[/quote]')
$message = substr($message, 8);
if (strpos($message, '[cowsay') !== false && !allowedTo('bbc_cowsay'))
$message = preg_replace('~\[(/?)cowsay[^\]]*\]~iu', '[$1pre]', $message);
$in_tag = false;
$had_tag = false;
$codeopen = 0;
if (preg_match_all('~(\[(/)*code(?:=[^\]]+)?\])~is', $message, $matches))
foreach ($matches[0] as $index => $dummy)
{
if (!empty($matches[2][$index]))
{
if (!$in_tag)
$codeopen = true;
$in_tag = false;
}
else
{
$had_tag = true;
if (!$in_tag)
$in_tag = true;
}
}
if ($in_tag)
$message .= '[/code]';
if ($codeopen && !$had_tag)
$message = '[code]' . $message;
$parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0, $n = count($parts); $i < $n; $i++)
{
if ($i % 4 == 2)
{
$code_tag = $parts[$i - 1] . $parts[$i] . $parts[$i + 1];
$substitute = $parts[$i - 1] . $i . $parts[$i + 1];
$code_tags[$substitute] = $code_tag;
$parts[$i] = $i;
}
}
$message = implode('', $parts);
$non_breaking_space = $context['utf8'] ? '\x{A0}' : '\xA0';
fixTags($message);
if (strpos($user_info['name'], '[') !== false || strpos($user_info['name'], ']') !== false || strpos($user_info['name'], '\'') !== false || strpos($user_info['name'], '"') !== false)
$message = preg_replace('~(\A|\n)/me(?: | )([^\n]*)(?:\z)?~i', '$1[me="' . $user_info['name'] . '"]$2[/me]', $message);
else
$message = preg_replace('~(\A|\n)/me(?: | )([^\n]*)(?:\z)?~i', '$1[me=' . $user_info['name'] . ']$2[/me]', $message);
if (!$previewing && strpos($message, '[html]') !== false)
{
if (allowedTo('bbc_html'))
$message = preg_replace_callback('~\[html\](.+?)\[/html\]~is', function($m)
{
return '[html]' . strtr(un_htmlspecialchars($m[1]), array("\n" => ' ', ' ' => '  ', '[' => '[', ']' => ']')) . '[/html]';
}, $message);
else
{
while (strpos($message, '[html]') !== false)
$message = preg_replace('~\[[/]?html\]~i', '', $message);
}
}
$message = preg_replace_callback('~\[time(?:=(absolute))*\](.+?)\[/time\]~i', function($m) use ($modSettings, $user_info)
{
return "[time]" . (is_numeric("$m[2]") || @strtotime("$m[2]") == 0 ? "$m[2]" : strtotime("$m[2]") - ("$m[1]" == "absolute" ? 0 : (($modSettings["time_offset"] + $user_info["time_offset"]) * 3600))) . "[/time]";
}, $message);
$message = preg_replace('~\[(black|blue|green|red|white)\]~', '[color=$1]', $message);
$message = preg_replace('~\[/(black|blue|green|red|white)\]~', '[/color]', $message);
if (empty($disallowed_tags_regex))
{
$disallowed_bbc = $context['legacy_bbc'];
foreach ($context['restricted_bbc'] as $bbc)
{
if ($bbc === 'html')
continue;
if (!allowedTo('bbc_' . $bbc))
$disallowed_bbc[] = $bbc;
}
$disallowed_tags_regex = build_regex(array_unique($disallowed_bbc), '~');
}
if (!empty($disallowed_tags_regex))
$message = preg_replace('~\[(?=/?' . $disallowed_tags_regex . '\b)~i', '[', $message);
$message = preg_replace_callback('~\[(/?)(list|li|table|tr|td)\b([^\]]*)\]~i', function($m)
{
return "[$m[1]" . strtolower("$m[2]") . "$m[3]]";
}, $message);
$list_open = substr_count($message, '[list]') + substr_count($message, '[list ');
$list_close = substr_count($message, '[/list]');
if ($list_close - $list_open > 0)
$message = str_repeat('[list]', $list_close - $list_open) . $message;
if ($list_open - $list_close > 0)
$message = $message . str_repeat('[/list]', $list_open - $list_close);
$mistake_fixes = array(
'~\[table\](?![\s' . $non_breaking_space . ']*\[tr\])~s' . ($context['utf8'] ? 'u' : '') => '[table][tr]',
'~\[tr\](?![\s' . $non_breaking_space . ']*\[td\])~s' . ($context['utf8'] ? 'u' : '') => '[tr][td]',
'~\[/td\](?![\s' . $non_breaking_space . ']*(?:\[td\]|\[/tr\]|\[/table\]))~s' . ($context['utf8'] ? 'u' : '') => '[/td][/tr]',
'~\[/tr\](?![\s' . $non_breaking_space . ']*(?:\[tr\]|\[/table\]))~s' . ($context['utf8'] ? 'u' : '') => '[/tr][/table]',
'~\[/td\][\s' . $non_breaking_space . ']*\[/table\]~s' . ($context['utf8'] ? 'u' : '') => '[/td][/tr][/table]',
'~\[(table|tr|/td)\]([\s' . $non_breaking_space . ']*)\[td\]~s' . ($context['utf8'] ? 'u' : '') => '[$1]$2[_td_]',
'~\[td\]~s' => '[tr][td]',
'~\[(table|/tr)\]([\s' . $non_breaking_space . ']*)\[tr\]~s' . ($context['utf8'] ? 'u' : '') => '[$1]$2[_tr_]',
'~\[tr\]~s' => '[table][tr]',
'~\[/td\]([\s' . $non_breaking_space . ']*)\[/tr\]~s' . ($context['utf8'] ? 'u' : '') => '[/td]$1[_/tr_]',
'~\[/tr\]~s' => '[/td][/tr]',
'~\[li\]([^\[\]]+?)\[li\]~s' => '[li]$1[_/li_][_li_]',
'~\[li\]([^\[\]]+?)\[/list\]~s' => '[_li_]$1[_/li_][/list]',
'~\[li\]([^\[\]]+?)$~s' => '[li]$1[/li]',
'~\[/li\]([\s' . $non_breaking_space . ']*)\[/list\]~s' . ($context['utf8'] ? 'u' : '') => '[_/li_]$1[/list]',
'~\[/li\]([\s' . $non_breaking_space . ']*)\[li\]~s' . ($context['utf8'] ? 'u' : '') => '[_/li_]$1[_li_]',
'~\[(list(?: [^\]]*?)?|/li)\]([\s' . $non_breaking_space . ']*)\[li\]~s' . ($context['utf8'] ? 'u' : '') => '[$1]$2[_li_]',
'~\[/li\]([\s' . $non_breaking_space . ']*)\[list\]~' . ($context['utf8'] ? 'u' : '') => '[_/li_]$1[list]',
'~\[/list\]([\s' . $non_breaking_space . ']*)\[li\]~' . ($context['utf8'] ? 'u' : '') => '[/list]$1[_li_]',
'~\[li\]~' => '[list][li]',
'~\[/li\]~' => '[/li][/list]',
'~\[_(li|/li|td|tr|/tr)_\]~' => '[$1]',
'~\[img\]https?://.{0,7}\[/img\]~' => '',
);
for ($j = 0; $j < 3; $j++)
$message = preg_replace(array_keys($mistake_fixes), $mistake_fixes, $message);
if (empty($tags_regex))
{
require_once($sourcedir . '/Subs.php');
$allowed_empty = array('anchor', 'td',);
$tags = array();
foreach (($codes = parse_bbc(false)) as $code)
if (!in_array($code['tag'], $allowed_empty))
$tags[] = $code['tag'];
$tags_regex = build_regex($tags, '~');
}
while (preg_match('~\[(' . $tags_regex . ')\b[^\]]*\]\s*\[/\1\]\s?~i', $message))
$message = preg_replace('~\[(' . $tags_regex . ')[^\]]*\]\s*\[/\1\]\s?~i', '', $message);
if (!empty($code_tags))
$message = str_replace(array_keys($code_tags), array_values($code_tags), $message);
if (!$previewing)
$message = strtr($message, array(' ' => ' ', "\n" => '<br>', $context['utf8'] ? "\xC2\xA0" : "\xA0" => ' '));
else
$message = strtr($message, array(' ' => ' ', $context['utf8'] ? "\xC2\xA0" : "\xA0" => ' '));
$message = strtr($message, array('[]' => '[]', '['' => '[''));
call_integration_hook('integrate_preparsecode', array(&$message, $previewing));
}
function un_preparsecode($message)
{
global $smcFunc;
call_integration_hook('integrate_unpreparsecode', array(&$message));
$parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0, $n = count($parts); $i < $n; $i++)
{
if ($i % 4 == 2)
{
$code_tag = $parts[$i - 1] . $parts[$i] . $parts[$i + 1];
$substitute = $parts[$i - 1] . $i . $parts[$i + 1];
$code_tags[$substitute] = $code_tag;
$parts[$i] = $i;
}
}
$message = implode('', $parts);
$message = preg_replace_callback('~\[html\](.+?)\[/html\]~i', function($m) use ($smcFunc)
{
return "[html]" . strtr($smcFunc['htmlspecialchars']("$m[1]", ENT_QUOTES), array("\\"" => """, "&#13;" => "<br>", "&#32;" => " ", "&#91;" => "[", "&#93;" => "]")) . "[/html]";
}, $message);
if (strpos($message, '[cowsay') !== false && !allowedTo('bbc_cowsay'))
$message = preg_replace('~\[(/?)cowsay[^\]]*\]~iu', '[$1pre]', $message);
$message = preg_replace_callback('~\[time\](\d{0,10})\[/time\]~i', function($m)
{
return "[time]" . timeformat("$m[1]", false) . "[/time]";
}, $message);
if (!empty($code_tags))
$message = strtr($message, $code_tags);
return preg_replace('~<br\s*/?' . '>~', "\n", str_replace(' ', ' ', $message));
}
function fixTags(&$message)
{
global $modSettings;
$fixArray = array(
array(
'tag' => 'img',
'protocols' => array('http', 'https'),
'embeddedUrl' => false,
'hasEqualSign' => false,
'hasExtra' => true,
),
array(
'tag' => 'url',
'protocols' => array('http', 'https'),
'embeddedUrl' => false,
'hasEqualSign' => false,
),
array(
'tag' => 'url',
'protocols' => array('http', 'https'),
'embeddedUrl' => true,
'hasEqualSign' => true,
),
array(
'tag' => 'iurl',
'protocols' => array('http', 'https'),
'embeddedUrl' => false,
'hasEqualSign' => false,
),
array(
'tag' => 'iurl',
'protocols' => array('http', 'https'),
'embeddedUrl' => true,
'hasEqualSign' => true,
),
array(
'tag' => 'ftp',
'protocols' => array('ftp', 'ftps', 'sftp'),
'embeddedUrl' => false,
'hasEqualSign' => false,
),
array(
'tag' => 'ftp',
'protocols' => array('ftp', 'ftps', 'sftp'),
'embeddedUrl' => true,
'hasEqualSign' => true,
),
array(
'tag' => 'flash',
'protocols' => array('http', 'https'),
'embeddedUrl' => false,
'hasEqualSign' => false,
'hasExtra' => true,
),
);
foreach ($fixArray as $param)
fixTag($message, $param['tag'], $param['protocols'], $param['embeddedUrl'], $param['hasEqualSign'], !empty($param['hasExtra']));
$message = preg_replace_callback('~(\[img.*?\])(.+?)\[/img\]~is', function($m)
{
return "$m[1]" . preg_replace("~action(=|%3d)(?!dlattach)~i", "action-", "$m[2]") . "[/img]";
}, $message);
}
function fixTag(&$message, $myTag, $protocols, $embeddedUrl = false, $hasEqualSign = false, $hasExtra = false)
{
global $boardurl, $scripturl;
if (preg_match('~^([^:]+://[^/]+)~', $boardurl, $match) != 0)
$domain_url = $match[1];
else
$domain_url = $boardurl . '/';
$replaces = array();
if ($hasEqualSign && $embeddedUrl)
{
$quoted = preg_match('~\[(' . $myTag . ')="~', $message);
preg_match_all('~\[(' . $myTag . ')=' . ($quoted ? '"(.*?)"' : '([^\]]*?)') . '\](?:(.+?)\[/(' . $myTag . ')\])?~is', $message, $matches);
}
elseif ($hasEqualSign)
preg_match_all('~\[(' . $myTag . ')=([^\]]*?)\](?:(.+?)\[/(' . $myTag . ')\])?~is', $message, $matches);
else
preg_match_all('~\[(' . $myTag . ($hasExtra ? '(?:[^\]]*?)' : '') . ')\](.+?)\[/(' . $myTag . ')\]~is', $message, $matches);
foreach ($matches[0] as $k => $dummy)
{
$replace = trim($matches[2][$k]);
$this_tag = $matches[1][$k];
$this_close = $hasEqualSign ? (empty($matches[4][$k]) ? '' : $matches[4][$k]) : $matches[3][$k];
$found = false;
foreach ($protocols as $protocol)
{
$found = strncasecmp($replace, $protocol . '://', strlen($protocol) + 3) === 0;
if ($found)
break;
}
if (!$found && $protocols[0] == 'http')
{
$parse_url_replace = parse_url($replace, PHP_URL_SCHEME);
if (substr($replace, 0, 1) == '/' && substr($replace, 0, 2) != '//')
$replace = $domain_url . $replace;
elseif (substr($replace, 0, 1) == '?')
$replace = $scripturl . $replace;
elseif (substr($replace, 0, 1) == '#' && $embeddedUrl)
{
$replace = '#' . preg_replace('~[^A-Za-z0-9_\-#]~', '', substr($replace, 1));
$this_tag = 'iurl';
$this_close = 'iurl';
}
elseif (substr($replace, 0, 2) != '//' && empty($parse_url_replace))
$replace = $protocols[0] . '://' . $replace;
}
elseif (!$found && $protocols[0] == 'ftp')
$replace = $protocols[0] . '://' . preg_replace('~^(?!ftps?)[^:]+://~', '', $replace);
elseif (!$found && empty($parse_url_replace))
$replace = $protocols[0] . '://' . $replace;
if ($hasEqualSign && $embeddedUrl)
$replaces[$matches[0][$k]] = '[' . $this_tag . '="' . $replace . '"]' . (empty($matches[4][$k]) ? '' : $matches[3][$k] . '[/' . $this_close . ']');
elseif ($hasEqualSign)
$replaces['[' . $matches[1][$k] . '=' . $matches[2][$k] . ']'] = '[' . $this_tag . '=' . $replace . ']';
elseif ($embeddedUrl)
$replaces['[' . $matches[1][$k] . ']' . $matches[2][$k] . '[/' . $matches[3][$k] . ']'] = '[' . $this_tag . '=' . $replace . ']' . $matches[2][$k] . '[/' . $this_close . ']';
else
$replaces['[' . $matches[1][$k] . ']' . $matches[2][$k] . '[/' . $matches[3][$k] . ']'] = '[' . $this_tag . ']' . $replace . '[/' . $this_close . ']';
}
foreach ($replaces as $k => $v)
{
if ($k == $v)
unset($replaces[$k]);
}
if (!empty($replaces))
$message = strtr($message, $replaces);
}
function sendmail($to, $subject, $message, $from = null, $message_id = null, $send_html = false, $priority = 3, $hotmail_fix = null, $is_private = false)
{
global $webmaster_email, $context, $modSettings, $txt, $scripturl;
$use_sendmail = empty($modSettings['mail_type']) || $modSettings['smtp_host'] == '';
$line_break = $context['server']['is_windows'] || !$use_sendmail ? "\r\n" : "\n";
$mail_result = true;
$to_array = is_array($to) ? $to : array($to);
foreach ($to_array as $k => $v)
{
if (trim($v) == '')
{
unset($to_array[$k]);
}
}
if (empty($to_array))
return true;
if ($hotmail_fix === null)
{
$hotmail_to = array();
foreach ($to_array as $i => $to_address)
{
if (preg_match('~@(att|comcast|bellsouth)\.[a-zA-Z\.]{2,6}$~i', $to_address) === 1)
{
$hotmail_to[] = $to_address;
$to_array = array_diff($to_array, array($to_address));
}
}
if (!empty($hotmail_to))
$mail_result = sendmail($hotmail_to, $subject, $message, $from, $message_id, $send_html, $priority, true, $is_private);
$hotmail_fix = false;
if (empty($to_array))
return $mail_result;
}
$subject = un_htmlspecialchars($subject);
$message = str_replace(array("\r", "\n"), array('', $line_break), $message);
if ($hotmail_fix && !$send_html)
{
$send_html = true;
$message = strtr($message, array($line_break => '<br>' . $line_break));
$message = preg_replace('~(' . preg_quote($scripturl, '~') . '(?:[?/][\w\-_%\.,\?&;=#]+)?)~', '<a href="$1">$1</a>', $message);
}
list (, $from_name) = mimespecialchars(addcslashes($from !== null ? $from : $context['forum_name'], '<>()\'\\"'), true, $hotmail_fix, $line_break);
list (, $subject) = mimespecialchars($subject, true, $hotmail_fix, $line_break);
$headers = 'From: ' . $from_name . ' <' . (empty($modSettings['mail_from']) ? $webmaster_email : $modSettings['mail_from']) . '>' . $line_break;
$headers .= $from !== null ? 'Reply-To: <' . $from . '>' . $line_break : '';
$headers .= 'Return-Path: ' . (empty($modSettings['mail_from']) ? $webmaster_email : $modSettings['mail_from']) . $line_break;
$headers .= 'Date: ' . gmdate('D, d M Y H:i:s') . ' -0000' . $line_break;
if ($message_id !== null && empty($modSettings['mail_no_message_id']))
$headers .= 'Message-ID: <' . md5($scripturl . microtime()) . '-' . $message_id . strstr(empty($modSettings['mail_from']) ? $webmaster_email : $modSettings['mail_from'], '@') . '>' . $line_break;
$headers .= 'X-Mailer: SMF' . $line_break;
if (in_array(false, call_integration_hook('integrate_outgoing_email', array(&$subject, &$message, &$headers, &$to_array)), true))
return false;
$orig_message = $message;
$mime_boundary = 'SMF-' . md5($message . time());
$headers .= 'Mime-Version: 1.0' . $line_break;
$headers .= 'content-type: multipart/alternative; boundary="' . $mime_boundary . '"' . $line_break;
$headers .= 'content-transfer-encoding: 7bit' . $line_break;
if ($send_html)
{
$no_html_message = un_htmlspecialchars(strip_tags(strtr($orig_message, array('</title>' => $line_break))));
list(, $plain_message) = mimespecialchars($no_html_message, false, true, $line_break);
$message = $plain_message . $line_break . '--' . $mime_boundary . $line_break;
list($charset, $plain_charset_message, $encoding) = mimespecialchars($no_html_message, false, false, $line_break);
$message .= 'content-type: text/plain; charset=' . $charset . $line_break;
$message .= 'content-transfer-encoding: ' . $encoding . $line_break . $line_break;
$message .= $plain_charset_message . $line_break . '--' . $mime_boundary . $line_break;
list($charset, $html_message, $encoding) = mimespecialchars($orig_message, false, $hotmail_fix, $line_break);
$message .= 'content-type: text/html; charset=' . $charset . $line_break;
$message .= 'content-transfer-encoding: ' . ($encoding == '' ? '7bit' : $encoding) . $line_break . $line_break;
$message .= $html_message . $line_break . '--' . $mime_boundary . '--';
}
else
{
list(, $plain_message) = mimespecialchars($orig_message, false, true, $line_break);
$message = $plain_message . $line_break . '--' . $mime_boundary . $line_break;
list ($charset, $encoded_message, $encoding) = mimespecialchars($orig_message, false, false, $line_break);
$message .= 'content-type: text/plain; charset=' . $charset . $line_break;
$message .= 'content-transfer-encoding: ' . $encoding . $line_break . $line_break;
$message .= $encoded_message . $line_break . '--' . $mime_boundary . '--';
}
if ($priority != 0)
return AddMailQueue(false, $to_array, $subject, $message, $headers, $send_html, $priority, $is_private);
elseif (!empty($modSettings['mail_limit']))
{
list ($last_mail_time, $mails_this_minute) = @explode('|', $modSettings['mail_recent']);
if (empty($mails_this_minute) || time() > $last_mail_time + 60)
$new_queue_stat = time() . '|' . 1;
else
$new_queue_stat = $last_mail_time . '|' . ((int) $mails_this_minute + 1);
updateSettings(array('mail_recent' => $new_queue_stat));
}
if ($use_sendmail)
{
$subject = strtr($subject, array("\r" => '', "\n" => ''));
if (!empty($modSettings['mail_strip_carriage']))
{
$message = strtr($message, array("\r" => ''));
$headers = strtr($headers, array("\r" => ''));
}
foreach ($to_array as $to)
{
set_error_handler(function($errno, $errstr, $errfile, $errline)
{
if (0 === error_reporting())
{
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
);
try
{
if (!mail(strtr($to, array("\r" => '', "\n" => '')), $subject, $message, $headers))
{
log_error(sprintf($txt['mail_send_unable'], $to));
$mail_result = false;
}
}
catch (ErrorException $e)
{
log_error($e->getMessage(), 'general', $e->getFile(), $e->getLine());
log_error(sprintf($txt['mail_send_unable'], $to));
$mail_result = false;
}
restore_error_handler();
@set_time_limit(300);
if (function_exists('apache_reset_timeout'))
@apache_reset_timeout();
}
}
else
$mail_result = $mail_result && smtp_mail($to_array, $subject, $message, $headers);
return $mail_result;
}
function AddMailQueue($flush = false, $to_array = array(), $subject = '', $message = '', $headers = '', $send_html = false, $priority = 3, $is_private = false)
{
global $context, $smcFunc;
static $cur_insert = array();
static $cur_insert_len = 0;
if ($cur_insert_len == 0)
$cur_insert = array();
if (($flush || $cur_insert_len > 800000) && !empty($cur_insert))
{
$cur_insert_len = 0;
$smcFunc['db_insert']('',
'{db_prefix}mail_queue',
array(
'time_sent' => 'int', 'recipient' => 'string-255', 'body' => 'string', 'subject' => 'string-255',
'headers' => 'string-65534', 'send_html' => 'int', 'priority' => 'int', 'private' => 'int',
),
$cur_insert,
array('id_mail')
);
$cur_insert = array();
$context['flush_mail'] = false;
}
if ($flush)
{
$nextSendTime = time() + 10;
$smcFunc['db_query']('', '
UPDATE {db_prefix}settings
SET value = {string:nextSendTime}
WHERE variable = {literal:mail_next_send}
AND value = {string:no_outstanding}',
array(
'nextSendTime' => $nextSendTime,
'no_outstanding' => '0',
)
);
return true;
}
$context['flush_mail'] = true;
foreach ($to_array as $to)
{
$this_insert_len = strlen($to) + strlen($message) + strlen($headers) + 700;
if ($this_insert_len + $cur_insert_len > 1000000)
{
$smcFunc['db_insert']('',
'{db_prefix}mail_queue',
array(
'time_sent' => 'int', 'recipient' => 'string-255', 'body' => 'string', 'subject' => 'string-255',
'headers' => 'string-65534', 'send_html' => 'int', 'priority' => 'int', 'private' => 'int',
),
$cur_insert,
array('id_mail')
);
$cur_insert = array();
$cur_insert_len = 0;
}
$cur_insert[] = array(time(), (string) $to, (string) $message, (string) $subject, (string) $headers, ($send_html ? 1 : 0), $priority, (int) $is_private);
$cur_insert_len += $this_insert_len;
}
if (SMF === 'SSI' || SMF === 'BACKGROUND')
return AddMailQueue(true);
return true;
}
function sendpm($recipients, $subject, $message, $store_outbox = false, $from = null, $pm_head = 0)
{
global $scripturl, $txt, $user_info, $language, $sourcedir;
global $modSettings, $smcFunc;
loadLanguage('PersonalMessage');
$log = array(
'failed' => array(),
'sent' => array()
);
if ($from === null)
$from = array(
'id' => $user_info['id'],
'name' => $user_info['name'],
'username' => $user_info['username']
);
$htmlmessage = $smcFunc['htmlspecialchars']($message, ENT_QUOTES);
preparsecode($htmlmessage);
$htmlsubject = strtr($smcFunc['htmlspecialchars']($subject), array("\r" => '', "\n" => '', "\t" => ''));
if ($smcFunc['strlen']($htmlsubject) > 100)
$htmlsubject = $smcFunc['substr']($htmlsubject, 0, 100);
if (!is_array($recipients))
$recipients = array($recipients);
call_integration_hook('integrate_personal_message', array(&$recipients, &$from, &$subject, &$message));
$usernames = array();
foreach ($recipients as $rec_type => $rec)
{
foreach ($rec as $id => $member)
{
if (!is_numeric($recipients[$rec_type][$id]))
{
$recipients[$rec_type][$id] = $smcFunc['strtolower'](trim(preg_replace('~[<>&"\'=\\\]~', '', $recipients[$rec_type][$id])));
$usernames[$recipients[$rec_type][$id]] = 0;
}
}
}
if (!empty($usernames))
{
$request = $smcFunc['db_query']('pm_find_username', '
SELECT id_member, member_name
FROM {db_prefix}members
WHERE ' . ($smcFunc['db_case_sensitive'] ? 'LOWER(member_name)' : 'member_name') . ' IN ({array_string:usernames})',
array(
'usernames' => array_keys($usernames),
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
if (isset($usernames[$smcFunc['strtolower']($row['member_name'])]))
$usernames[$smcFunc['strtolower']($row['member_name'])] = $row['id_member'];
$smcFunc['db_free_result']($request);
foreach ($recipients as $rec_type => $rec)
foreach ($rec as $id => $member)
{
if (is_numeric($recipients[$rec_type][$id]))
continue;
if (!empty($usernames[$member]))
$recipients[$rec_type][$id] = $usernames[$member];
else
{
$log['failed'][$id] = sprintf($txt['pm_error_user_not_found'], $recipients[$rec_type][$id]);
unset($recipients[$rec_type][$id]);
}
}
}
$recipients['to'] = array_unique($recipients['to']);
$recipients['bcc'] = array_diff(array_unique($recipients['bcc']), $recipients['to']);
$all_to = array_merge($recipients['to'], $recipients['bcc']);
$request = $smcFunc['db_query']('', '
SELECT
id_member, criteria, is_or
FROM {db_prefix}pm_rules
WHERE id_member IN ({array_int:to_members})
AND delete_pm = {int:delete_pm}',
array(
'to_members' => $all_to,
'delete_pm' => 1,
)
);
$deletes = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$criteria = $smcFunc['json_decode']($row['criteria'], true);
$delete = false;
foreach ($criteria as $criterium)
{
if (($criterium['t'] == 'mid' && $criterium['v'] == $from['id']) || ($criterium['t'] == 'gid' && in_array($criterium['v'], $user_info['groups'])) || ($criterium['t'] == 'sub' && strpos($subject, $criterium['v']) !== false) || ($criterium['t'] == 'msg' && strpos($message, $criterium['v']) !== false))
$delete = true;
elseif (!$row['is_or'])
{
$delete = false;
break;
}
}
if ($delete)
$deletes[$row['id_member']] = 1;
}
$smcFunc['db_free_result']($request);
static $message_limit_cache = array();
if (!allowedTo('moderate_forum') && empty($message_limit_cache))
{
$request = $smcFunc['db_query']('', '
SELECT id_group, max_messages
FROM {db_prefix}membergroups',
array(
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$message_limit_cache[$row['id_group']] = $row['max_messages'];
$smcFunc['db_free_result']($request);
}
require_once($sourcedir . '/Subs-Members.php');
$pmReadGroups = groupsAllowedTo('pm_read');
if (empty($modSettings['permission_enable_deny']))
$pmReadGroups['denied'] = array();
require_once($sourcedir . '/Subs-Notify.php');
$notifyPrefs = getNotifyPrefs($all_to, array('pm_new', 'pm_reply', 'pm_notify'), true);
$request = $smcFunc['db_query']('', '
SELECT
member_name, real_name, id_member, email_address, lngfile,
instant_messages,' . (allowedTo('moderate_forum') ? ' 0' : '
(pm_receive_from = {int:admins_only}' . (empty($modSettings['enable_buddylist']) ? '' : ' OR
(pm_receive_from = {int:buddies_only} AND FIND_IN_SET({string:from_id}, buddy_list) = 0) OR
(pm_receive_from = {int:not_on_ignore_list} AND FIND_IN_SET({string:from_id}, pm_ignore_list) != 0)') . ')') . ' AS ignored,
FIND_IN_SET({string:from_id}, buddy_list) != 0 AS is_buddy, is_activated,
additional_groups, id_group, id_post_group
FROM {db_prefix}members
WHERE id_member IN ({array_int:recipients})
ORDER BY lngfile
LIMIT {int:count_recipients}',
array(
'not_on_ignore_list' => 1,
'buddies_only' => 2,
'admins_only' => 3,
'recipients' => $all_to,
'count_recipients' => count($all_to),
'from_id' => $from['id'],
)
);
$notifications = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (isset($deletes[$row['id_member']]))
continue;
$prefs = !empty($notifyPrefs[$row['id_member']]) ? $notifyPrefs[$row['id_member']] : array();
$prefs = array_merge(array(
'pm_new' => 0,
'pm_reply' => 0,
'pm_notify' => 0,
), $prefs);
$groups = explode(',', $row['additional_groups']);
$groups[] = $row['id_group'];
$groups[] = $row['id_post_group'];
$message_limit = -1;
if (!in_array(1, $groups))
{
foreach ($groups as $id)
{
if (isset($message_limit_cache[$id]) && $message_limit != 0 && $message_limit < $message_limit_cache[$id])
$message_limit = $message_limit_cache[$id];
}
if ($message_limit > 0 && $message_limit <= $row['instant_messages'])
{
$log['failed'][$row['id_member']] = sprintf($txt['pm_error_data_limit_reached'], $row['real_name']);
unset($all_to[array_search($row['id_member'], $all_to)]);
continue;
}
if (count(array_intersect($pmReadGroups['allowed'], $groups)) == 0 || count(array_intersect($pmReadGroups['denied'], $groups)) != 0)
{
$log['failed'][$row['id_member']] = sprintf($txt['pm_error_user_cannot_read'], $row['real_name']);
unset($all_to[array_search($row['id_member'], $all_to)]);
continue;
}
}
if (!empty($row['ignored']) && $row['ignored'] != 'f' && $row['id_member'] != $from['id'])
{
$log['failed'][$row['id_member']] = sprintf($txt['pm_error_ignored_by_user'], $row['real_name']);
unset($all_to[array_search($row['id_member'], $all_to)]);
continue;
}
if ($row['is_activated'] >= 10 || ($row['is_activated'] == 4 && !$user_info['is_admin']))
{
$log['failed'][$row['id_member']] = sprintf($txt['pm_error_user_cannot_read'], $row['real_name']);
unset($all_to[array_search($row['id_member'], $all_to)]);
continue;
}
if (!empty($row['email_address'])
&& ((empty($pm_head) && $prefs['pm_new'] & 0x02) || (!empty($pm_head) && $prefs['pm_reply'] & 0x02))
&& ($prefs['pm_notify'] <= 1 || ($prefs['pm_notify'] > 1 && (!empty($modSettings['enable_buddylist']) && $row['is_buddy']))) && $row['is_activated'] == 1)
{
$notifications[empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']][] = $row['email_address'];
}
$log['sent'][$row['id_member']] = sprintf(isset($txt['pm_successfully_sent']) ? $txt['pm_successfully_sent'] : '', $row['real_name']);
}
$smcFunc['db_free_result']($request);
if (empty($all_to))
return $log;
$id_pm = $smcFunc['db_insert']('',
'{db_prefix}personal_messages',
array(
'id_pm_head' => 'int', 'id_member_from' => 'int', 'deleted_by_sender' => 'int',
'from_name' => 'string-255', 'msgtime' => 'int', 'subject' => 'string-255', 'body' => 'string-65534',
),
array(
$pm_head, $from['id'], ($store_outbox ? 0 : 1),
$from['username'], time(), $htmlsubject, $htmlmessage,
),
array('id_pm'),
1
);
if (!empty($id_pm))
{
if (empty($pm_head))
$smcFunc['db_query']('', '
UPDATE {db_prefix}personal_messages
SET id_pm_head = {int:id_pm_head}
WHERE id_pm = {int:id_pm_head}',
array(
'id_pm_head' => $id_pm,
)
);
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_recipients
WHERE id_pm = {int:id_pm}',
array(
'id_pm' => $id_pm,
)
);
$insertRows = array();
$to_list = array();
foreach ($all_to as $to)
{
$insertRows[] = array($id_pm, $to, in_array($to, $recipients['bcc']) ? 1 : 0, isset($deletes[$to]) ? 1 : 0, 1);
if (!in_array($to, $recipients['bcc']))
$to_list[] = $to;
}
$smcFunc['db_insert']('insert',
'{db_prefix}pm_recipients',
array(
'id_pm' => 'int', 'id_member' => 'int', 'bcc' => 'int', 'deleted' => 'int', 'is_new' => 'int'
),
$insertRows,
array('id_pm', 'id_member')
);
}
$to_names = array();
if (count($to_list) > 1)
{
$request = $smcFunc['db_query']('', '
SELECT real_name
FROM {db_prefix}members
WHERE id_member IN ({array_int:to_members})
AND id_member != {int:from}',
array(
'to_members' => $to_list,
'from' => $from['id'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$to_names[] = un_htmlspecialchars($row['real_name']);
$smcFunc['db_free_result']($request);
}
$replacements = array(
'SUBJECT' => $subject,
'MESSAGE' => $message,
'SENDER' => un_htmlspecialchars($from['name']),
'READLINK' => $scripturl . '?action=pm;pmsg=' . $id_pm . '#msg' . $id_pm,
'REPLYLINK' => $scripturl . '?action=pm;sa=send;f=inbox;pmsg=' . $id_pm . ';quote;u=' . $from['id'],
'TOLIST' => implode(', ', $to_names),
);
$email_template = 'new_pm' . (empty($modSettings['disallow_sendBody']) ? '_body' : '') . (!empty($to_names) ? '_tolist' : '');
$notification_texts = array();
foreach ($notifications as $lang => $notification_list)
{
if (empty($notification_texts[$lang]))
{
if ($lang != $user_info['language'])
loadLanguage('index+Modifications', $lang, false);
$notification_texts[$lang]['subject'] = $subject;
censorText($notification_texts[$lang]['subject']);
if (empty($modSettings['disallow_sendBody']))
{
$notification_texts[$lang]['body'] = $message;
censorText($notification_texts[$lang]['body']);
$notification_texts[$lang]['body'] = trim(un_htmlspecialchars(strip_tags(strtr(parse_bbc($smcFunc['htmlspecialchars']($notification_texts[$lang]['body']), false), array('<br>' => "\n", '</div>' => "\n", '</li>' => "\n", '[' => '[', ']' => ']')))));
}
else
$notification_texts[$lang]['body'] = '';
if ($lang != $user_info['language'])
loadLanguage('index+Modifications', $user_info['language'], false);
}
$replacements['SUBJECT'] = $notification_texts[$lang]['subject'];
$replacements['MESSAGE'] = $notification_texts[$lang]['body'];
$emaildata = loadEmailTemplate($email_template, $replacements, $lang);
sendmail($notification_list, $emaildata['subject'], $emaildata['body'], null, 'p' . $id_pm, $emaildata['is_html'], 2, null, true);
}
call_integration_hook('integrate_personal_message_after', array(&$id_pm, &$log, &$recipients, &$from, &$subject, &$message));
loadLanguage('index+PersonalMessage');
foreach ($all_to as $k => $id)
if (isset($deletes[$id]))
unset($all_to[$k]);
if (!empty($all_to))
updateMemberData($all_to, array('instant_messages' => '+', 'unread_messages' => '+', 'new_pm' => 1));
return $log;
}
function mimespecialchars($string, $with_charset = true, $hotmail_fix = false, $line_break = "\r\n", $custom_charset = null)
{
global $context;
$charset = $custom_charset !== null ? $custom_charset : $context['character_set'];
if (preg_match_all('~&#(\d{3,8});~', $string, $matches) !== 0 && !$hotmail_fix)
{
$simple = true;
foreach ($matches[1] as $entity)
if ($entity > 128)
$simple = false;
unset($matches);
if ($simple)
$string = preg_replace_callback('~&#(\d{3,8});~', function($m)
{
return chr("$m[1]");
}, $string);
else
{
if (!$context['utf8'] && function_exists('iconv'))
{
$newstring = @iconv($context['character_set'], 'UTF-8', $string);
if ($newstring)
$string = $newstring;
}
$string = preg_replace_callback('~&#(\d{3,8});~', 'fixchar__callback', $string);
$charset = 'UTF-8';
}
}
if ($hotmail_fix && ($context['utf8'] || function_exists('iconv') || $context['character_set'] === 'ISO-8859-1'))
{
if (!$context['utf8'] && function_exists('iconv'))
{
$newstring = @iconv($context['character_set'], 'UTF-8', $string);
if ($newstring)
$string = $newstring;
}
$entityConvert = function($m)
{
$c = $m[1];
if (strlen($c) === 1 && ord($c[0]) <= 0x7F)
return $c;
elseif (strlen($c) === 2 && ord($c[0]) >= 0xC0 && ord($c[0]) <= 0xDF)
return "&#" . (((ord($c[0]) ^ 0xC0) << 6) + (ord($c[1]) ^ 0x80)) . ";";
elseif (strlen($c) === 3 && ord($c[0]) >= 0xE0 && ord($c[0]) <= 0xEF)
return "&#" . (((ord($c[0]) ^ 0xE0) << 12) + ((ord($c[1]) ^ 0x80) << 6) + (ord($c[2]) ^ 0x80)) . ";";
elseif (strlen($c) === 4 && ord($c[0]) >= 0xF0 && ord($c[0]) <= 0xF7)
return "&#" . (((ord($c[0]) ^ 0xF0) << 18) + ((ord($c[1]) ^ 0x80) << 12) + ((ord($c[2]) ^ 0x80) << 6) + (ord($c[3]) ^ 0x80)) . ";";
else
return "";
};
return array($charset, preg_replace_callback('~([\x80-\x{10FFFF}])~u', $entityConvert, $string), '7bit');
}
elseif (!$hotmail_fix && preg_match('~([^\x09\x0A\x0D\x20-\x7F])~', $string) === 1)
{
$string = base64_encode($string);
if ($with_charset)
$string = '=?' . $charset . '?B?' . $string . '?=';
else
$string = chunk_split($string, 76, $line_break);
return array($charset, $string, 'base64');
}
else
return array($charset, $string, '7bit');
}
function smtp_mail($mail_to_array, $subject, $message, $headers)
{
global $modSettings, $webmaster_email, $txt, $boardurl;
static $helo;
$modSettings['smtp_host'] = trim($modSettings['smtp_host']);
if ($modSettings['mail_type'] == 3 && $modSettings['smtp_username'] != '' && $modSettings['smtp_password'] != '')
{
$socket = fsockopen($modSettings['smtp_host'], 110, $errno, $errstr, 2);
if (!$socket && (substr($modSettings['smtp_host'], 0, 5) == 'smtp.' || substr($modSettings['smtp_host'], 0, 11) == 'ssl://smtp.'))
$socket = fsockopen(strtr($modSettings['smtp_host'], array('smtp.' => 'pop.')), 110, $errno, $errstr, 2);
if ($socket)
{
fgets($socket, 256);
fputs($socket, 'USER ' . $modSettings['smtp_username'] . "\r\n");
fgets($socket, 256);
fputs($socket, 'PASS ' . base64_decode($modSettings['smtp_password']) . "\r\n");
fgets($socket, 256);
fputs($socket, 'QUIT' . "\r\n");
fclose($socket);
}
}
if (!$socket = fsockopen($modSettings['smtp_host'], empty($modSettings['smtp_port']) ? 25 : $modSettings['smtp_port'], $errno, $errstr, 3))
{
if (substr($modSettings['smtp_host'], 0, 4) == 'ssl:' && (empty($modSettings['smtp_port']) || $modSettings['smtp_port'] == 25))
{
if (substr($modSettings['smtp_host'], 0, 6) != 'ssl://')
$modSettings['smtp_host'] = str_replace('ssl:', 'ss://', $modSettings['smtp_host']);
if ($socket = fsockopen($modSettings['smtp_host'], 465, $errno, $errstr, 3))
log_error($txt['smtp_port_ssl']);
}
if (!$socket)
{
log_error($txt['smtp_no_connect'] . ': ' . $errno . ' : ' . $errstr);
return false;
}
}
if (!server_parse(null, $socket, '220'))
return false;
if (empty($helo))
{
if (function_exists('gethostname'))
$helo = gethostname();
elseif (function_exists('php_uname'))
$helo = php_uname('n');
if (empty($helo) || strpos($helo, '.') === false || substr_compare($helo, '.local', -6) === 0 || (!empty($modSettings['tld_regex']) && !preg_match('/\.' . $modSettings['tld_regex'] . '$/u', $helo)))
$helo = parse_url($boardurl, PHP_URL_HOST);
if (strpos($helo, 'www.') === 0)
$helo = substr($helo, 4);
}
if (in_array($modSettings['mail_type'], array(1, 2)) && $modSettings['smtp_username'] != '' && $modSettings['smtp_password'] != '')
{
if (server_parse('EHLO ' . $helo, $socket, null, $response) == '250')
{
if ($modSettings['mail_type'] == 2 && preg_match("~250( |-)STARTTLS~mi", $response))
{
if (!server_parse('STARTTLS', $socket, '220'))
return false;
$crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT'))
{
$crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
$crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
}
if (!@stream_socket_enable_crypto($socket, true, $crypto_method))
return false;
if (!server_parse('EHLO ' . $helo, $socket, null) == '250')
return false;
}
if (!server_parse('AUTH LOGIN', $socket, '334'))
return false;
if (!server_parse(base64_encode($modSettings['smtp_username']), $socket, '334'))
return false;
if (!server_parse($modSettings['smtp_password'], $socket, '235'))
return false;
}
elseif (!server_parse('HELO ' . $helo, $socket, '250'))
return false;
}
else
{
if (!server_parse('HELO ' . $helo, $socket, '250'))
return false;
}
$message = strtr($message, array("\r\n" . '.' => "\r\n" . '..'));
$mail_to_array = array_values($mail_to_array);
foreach ($mail_to_array as $i => $mail_to)
{
if ($i != 0)
{
if (!server_parse('RSET', $socket, '250'))
return false;
}
if (!server_parse('MAIL FROM: <' . (empty($modSettings['mail_from']) ? $webmaster_email : $modSettings['mail_from']) . '>', $socket, '250'))
return false;
if (!server_parse('RCPT TO: <' . $mail_to . '>', $socket, '250'))
return false;
if (!server_parse('DATA', $socket, '354'))
return false;
fputs($socket, 'Subject: ' . $subject . "\r\n");
if (strlen($mail_to) > 0)
fputs($socket, 'To: <' . $mail_to . '>' . "\r\n");
fputs($socket, $headers . "\r\n\r\n");
fputs($socket, $message . "\r\n");
if (!server_parse('.', $socket, '250'))
return false;
@set_time_limit(300);
if (function_exists('apache_reset_timeout'))
@apache_reset_timeout();
}
fputs($socket, 'QUIT' . "\r\n");
fclose($socket);
return true;
}
function server_parse($message, $socket, $code, &$response = null)
{
global $txt;
if ($message !== null)
fputs($socket, $message . "\r\n");
$server_response = '';
while (substr($server_response, 3, 1) != ' ')
{
if (!($server_response = fgets($socket, 256)))
{
log_error($txt['smtp_bad_response']);
return false;
}
$response .= $server_response;
}
if ($code === null)
return substr($server_response, 0, 3);
if (substr($server_response, 0, 3) != $code)
{
log_error($txt['smtp_error'] . $server_response);
return false;
}
return true;
}
function SpellCheck()
{
global $txt, $context, $smcFunc;
$known_words = array('smf', 'php', 'mysql', 'www', 'gif', 'jpeg', 'png', 'http', 'smfisawesome', 'grandia', 'terranigma', 'rpgs');
loadLanguage('Post');
loadTemplate('Post');
$dict = spell_init();
if (!isset($_POST['spellstring']) || !$dict)
die;
$context['spell_js'] = '
var txt = {"done": "' . $txt['spellcheck_done'] . '"};
var mispstr = window.opener.spellCheckGetText(spell_fieldname);
var misps = Array(';
$alphas = explode("\n", strtr($_POST['spellstring'], array("\r" => '')));
$found_words = false;
for ($i = 0, $n = count($alphas); $i < $n; $i++)
{
$check_word = explode('|', $alphas[$i]);
if (in_array($smcFunc['strtolower']($check_word[0]), $known_words) || spell_check($dict, $check_word[0]) || !isset($check_word[2]))
continue;
$found_words = true;
$context['spell_js'] .= '
new misp("' . strtr($check_word[0], array('\\' => '\\\\', '"' => '\\"', '<' => '', '>' => '')) . '", ' . (int) $check_word[1] . ', ' . (int) $check_word[2] . ', [';
$suggestions = spell_suggest($dict, $check_word[0]);
if (!empty($suggestions))
{
foreach ($suggestions as $k => $word)
if ($suggestions[$k] != censorText($word))
unset($suggestions[$k]);
if (!empty($suggestions))
$context['spell_js'] .= '"' . implode('", "', $suggestions) . '"';
}
$context['spell_js'] .= ']),';
}
if ($found_words)
$context['spell_js'] = substr($context['spell_js'], 0, -1);
$context['spell_js'] .= '
);';
$context['template_layers'] = array();
$context['sub_template'] = 'spellcheck';
if (isset($context['enchant_broker']))
{
enchant_broker_free_dict($dict);
enchant_broker_free($context['enchant_broker']);
}
}
function sendNotifications($topics, $type, $exclude = array(), $members_only = array())
{
global $user_info, $smcFunc;
if (empty($topics))
return;
if (!is_array($topics))
$topics = array($topics);
$result = $smcFunc['db_query']('', '
SELECT mf.subject, ml.body, ml.id_member, t.id_last_msg, t.id_topic, t.id_board,
COALESCE(mem.real_name, ml.poster_name) AS poster_name, mf.id_msg
FROM {db_prefix}topics AS t
INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ml.id_member)
WHERE t.id_topic IN ({array_int:topic_list})
LIMIT 1',
array(
'topic_list' => $topics,
)
);
$task_rows = array();
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$task_rows[] = array(
'$sourcedir/tasks/CreatePost-Notify.php', 'CreatePost_Notify_Background', $smcFunc['json_encode'](array(
'msgOptions' => array(
'id' => $row['id_msg'],
'subject' => $row['subject'],
'body' => $row['body'],
),
'topicOptions' => array(
'id' => $row['id_topic'],
'board' => $row['id_board'],
),
'posterOptions' => array(
'id' => $user_info['id'],
'name' => $user_info['name'],
),
'type' => $type,
'members_only' => $members_only,
)), 0
);
}
$smcFunc['db_free_result']($result);
if (!empty($task_rows))
$smcFunc['db_insert']('',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
$task_rows,
array('id_task')
);
}
function createPost(&$msgOptions, &$topicOptions, &$posterOptions)
{
global $user_info, $txt, $modSettings, $smcFunc, $sourcedir;
require_once($sourcedir . '/Mentions.php');
$msgOptions['icon'] = empty($msgOptions['icon']) ? 'xx' : $msgOptions['icon'];
$msgOptions['smileys_enabled'] = !empty($msgOptions['smileys_enabled']);
$msgOptions['attachments'] = empty($msgOptions['attachments']) ? array() : $msgOptions['attachments'];
$msgOptions['approved'] = isset($msgOptions['approved']) ? (int) $msgOptions['approved'] : 1;
$topicOptions['id'] = empty($topicOptions['id']) ? 0 : (int) $topicOptions['id'];
$topicOptions['poll'] = isset($topicOptions['poll']) ? (int) $topicOptions['poll'] : null;
$topicOptions['lock_mode'] = isset($topicOptions['lock_mode']) ? $topicOptions['lock_mode'] : null;
$topicOptions['sticky_mode'] = isset($topicOptions['sticky_mode']) ? $topicOptions['sticky_mode'] : null;
$topicOptions['redirect_expires'] = isset($topicOptions['redirect_expires']) ? $topicOptions['redirect_expires'] : null;
$topicOptions['redirect_topic'] = isset($topicOptions['redirect_topic']) ? $topicOptions['redirect_topic'] : null;
$posterOptions['id'] = empty($posterOptions['id']) ? 0 : (int) $posterOptions['id'];
$posterOptions['ip'] = empty($posterOptions['ip']) ? $user_info['ip'] : $posterOptions['ip'];
$msgOptions['send_notifications'] = isset($msgOptions['send_notifications']) ? (bool) $msgOptions['send_notifications'] : true;
if (!$modSettings['postmod_active'])
$topicOptions['is_approved'] = true;
elseif (!empty($topicOptions['id']) && !isset($topicOptions['is_approved']))
{
$request = $smcFunc['db_query']('', '
SELECT approved
FROM {db_prefix}topics
WHERE id_topic = {int:id_topic}
LIMIT 1',
array(
'id_topic' => $topicOptions['id'],
)
);
list ($topicOptions['is_approved']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
if (!isset($posterOptions['name']) || $posterOptions['name'] == '' || (empty($posterOptions['email']) && !empty($posterOptions['id'])))
{
if (empty($posterOptions['id']))
{
$posterOptions['id'] = 0;
$posterOptions['name'] = $txt['guest_title'];
$posterOptions['email'] = '';
}
elseif ($posterOptions['id'] != $user_info['id'])
{
$request = $smcFunc['db_query']('', '
SELECT member_name, email_address
FROM {db_prefix}members
WHERE id_member = {int:id_member}
LIMIT 1',
array(
'id_member' => $posterOptions['id'],
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
trigger_error('createPost(): Invalid member id ' . $posterOptions['id'], E_USER_NOTICE);
$posterOptions['id'] = 0;
$posterOptions['name'] = $txt['guest_title'];
$posterOptions['email'] = '';
}
else
list ($posterOptions['name'], $posterOptions['email']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
else
{
$posterOptions['name'] = $user_info['name'];
$posterOptions['email'] = $user_info['email'];
}
}
if (!empty($modSettings['enable_mentions']))
{
$msgOptions['mentioned_members'] = Mentions::getMentionedMembers($msgOptions['body']);
if (!empty($msgOptions['mentioned_members']))
$msgOptions['body'] = Mentions::getBody($msgOptions['body'], $msgOptions['mentioned_members']);
}
$previous_ignore_user_abort = ignore_user_abort(true);
$new_topic = empty($topicOptions['id']);
$message_columns = array(
'id_board' => 'int', 'id_topic' => 'int', 'id_member' => 'int', 'subject' => 'string-255', 'body' => (!empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] > 65534 ? 'string-' . $modSettings['max_messageLength'] : (empty($modSettings['max_messageLength']) ? 'string' : 'string-65534')),
'poster_name' => 'string-255', 'poster_email' => 'string-255', 'poster_time' => 'int', 'poster_ip' => 'inet',
'smileys_enabled' => 'int', 'modified_name' => 'string', 'icon' => 'string-16', 'approved' => 'int',
);
$message_parameters = array(
$topicOptions['board'], $topicOptions['id'], $posterOptions['id'], $msgOptions['subject'], $msgOptions['body'],
$posterOptions['name'], $posterOptions['email'], time(), $posterOptions['ip'],
$msgOptions['smileys_enabled'] ? 1 : 0, '', $msgOptions['icon'], $msgOptions['approved'],
);
call_integration_hook('integrate_create_post', array(&$msgOptions, &$topicOptions, &$posterOptions, &$message_columns, &$message_parameters));
$msgOptions['id'] = $smcFunc['db_insert']('',
'{db_prefix}messages',
$message_columns,
$message_parameters,
array('id_msg'),
1
);
if (empty($msgOptions['id']))
return false;
if (!empty($msgOptions['attachments']))
$smcFunc['db_query']('', '
UPDATE {db_prefix}attachments
SET id_msg = {int:id_msg}
WHERE id_attach IN ({array_int:attachment_list})',
array(
'attachment_list' => $msgOptions['attachments'],
'id_msg' => $msgOptions['id'],
)
);
call_integration_hook('integrate_after_create_post', array($msgOptions, $topicOptions, $posterOptions, $message_columns, $message_parameters));
if ($new_topic)
{
$topic_columns = array(
'id_board' => 'int', 'id_member_started' => 'int', 'id_member_updated' => 'int', 'id_first_msg' => 'int',
'id_last_msg' => 'int', 'locked' => 'int', 'is_sticky' => 'int', 'num_views' => 'int',
'id_poll' => 'int', 'unapproved_posts' => 'int', 'approved' => 'int',
'redirect_expires' => 'int', 'id_redirect_topic' => 'int',
);
$topic_parameters = array(
$topicOptions['board'], $posterOptions['id'], $posterOptions['id'], $msgOptions['id'],
$msgOptions['id'], $topicOptions['lock_mode'] === null ? 0 : $topicOptions['lock_mode'], $topicOptions['sticky_mode'] === null ? 0 : $topicOptions['sticky_mode'], 0,
$topicOptions['poll'] === null ? 0 : $topicOptions['poll'], $msgOptions['approved'] ? 0 : 1, $msgOptions['approved'],
$topicOptions['redirect_expires'] === null ? 0 : $topicOptions['redirect_expires'], $topicOptions['redirect_topic'] === null ? 0 : $topicOptions['redirect_topic'],
);
call_integration_hook('integrate_before_create_topic', array(&$msgOptions, &$topicOptions, &$posterOptions, &$topic_columns, &$topic_parameters));
$topicOptions['id'] = $smcFunc['db_insert']('',
'{db_prefix}topics',
$topic_columns,
$topic_parameters,
array('id_topic'),
1
);
if (empty($topicOptions['id']))
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}messages
WHERE id_msg = {int:id_msg}',
array(
'id_msg' => $msgOptions['id'],
)
);
return false;
}
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET id_topic = {int:id_topic}
WHERE id_msg = {int:id_msg}',
array(
'id_topic' => $topicOptions['id'],
'id_msg' => $msgOptions['id'],
)
);
trackStats(array('topics' => '+', 'posts' => '+'));
updateStats('topic', true);
updateStats('subject', $topicOptions['id'], $msgOptions['subject']);
call_integration_hook('integrate_create_topic', array(&$msgOptions, &$topicOptions, &$posterOptions));
}
else
{
$update_parameters = array(
'poster_id' => $posterOptions['id'],
'id_msg' => $msgOptions['id'],
'locked' => $topicOptions['lock_mode'],
'is_sticky' => $topicOptions['sticky_mode'],
'id_topic' => $topicOptions['id'],
'counter_increment' => 1,
);
if ($msgOptions['approved'])
$topics_columns = array(
'id_member_updated = {int:poster_id}',
'id_last_msg = {int:id_msg}',
'num_replies = num_replies + {int:counter_increment}',
);
else
$topics_columns = array(
'unapproved_posts = unapproved_posts + {int:counter_increment}',
);
if ($topicOptions['lock_mode'] !== null)
$topics_columns[] = 'locked = {int:locked}';
if ($topicOptions['sticky_mode'] !== null)
$topics_columns[] = 'is_sticky = {int:is_sticky}';
call_integration_hook('integrate_modify_topic', array(&$topics_columns, &$update_parameters, &$msgOptions, &$topicOptions, &$posterOptions));
$smcFunc['db_query']('', '
UPDATE {db_prefix}topics
SET
' . implode(', ', $topics_columns) . '
WHERE id_topic = {int:id_topic}',
$update_parameters
);
trackStats(array('posts' => '+'));
}
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET id_msg_modified = {int:id_msg}
WHERE id_msg = {int:id_msg}',
array(
'id_msg' => $msgOptions['id'],
)
);
if ($msgOptions['approved'])
$smcFunc['db_query']('', '
UPDATE {db_prefix}boards
SET num_posts = num_posts + 1' . ($new_topic ? ', num_topics = num_topics + 1' : '') . '
WHERE id_board = {int:id_board}',
array(
'id_board' => $topicOptions['board'],
)
);
else
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}boards
SET unapproved_posts = unapproved_posts + 1' . ($new_topic ? ', unapproved_topics = unapproved_topics + 1' : '') . '
WHERE id_board = {int:id_board}',
array(
'id_board' => $topicOptions['board'],
)
);
$smcFunc['db_insert']('',
'{db_prefix}approval_queue',
array(
'id_msg' => 'int',
),
array(
$msgOptions['id'],
),
array()
);
$smcFunc['db_insert']('',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
array(
'$sourcedir/tasks/ApprovePost-Notify.php', 'ApprovePost_Notify_Background', $smcFunc['json_encode'](array(
'msgOptions' => $msgOptions,
'topicOptions' => $topicOptions,
'posterOptions' => $posterOptions,
'type' => $new_topic ? 'topic' : 'post',
)), 0
),
array('id_task')
);
}
if (!empty($topicOptions['mark_as_read']) && !$user_info['is_guest'])
{
if (!$new_topic)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_topics
SET id_msg = {int:id_msg}
WHERE id_member = {int:current_member}
AND id_topic = {int:id_topic}',
array(
'current_member' => $posterOptions['id'],
'id_msg' => $msgOptions['id'],
'id_topic' => $topicOptions['id'],
)
);
$flag = $smcFunc['db_affected_rows']() != 0;
}
if (empty($flag))
{
$smcFunc['db_insert']('ignore',
'{db_prefix}log_topics',
array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
array($topicOptions['id'], $posterOptions['id'], $msgOptions['id']),
array('id_topic', 'id_member')
);
}
}
if ($msgOptions['approved'] && empty($topicOptions['is_approved']) && $posterOptions['id'] != $user_info['id'])
$smcFunc['db_insert']('',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
array(
'$sourcedir/tasks/ApproveReply-Notify.php', 'ApproveReply_Notify_Background', $smcFunc['json_encode'](array(
'msgOptions' => $msgOptions,
'topicOptions' => $topicOptions,
'posterOptions' => $posterOptions,
)), 0
),
array('id_task')
);
require_once($sourcedir . '/Search.php');
$searchAPI = findSearchAPI();
if (is_callable(array($searchAPI, 'postCreated')))
$searchAPI->postCreated($msgOptions, $topicOptions, $posterOptions);
if (!empty($posterOptions['update_post_count']) && !empty($posterOptions['id']) && $msgOptions['approved'])
{
if ($user_info['id'] == $posterOptions['id'])
$user_info['posts']++;
updateMemberData($posterOptions['id'], array('posts' => '+'));
}
$_SESSION['last_read_topic'] = 0;
if (isset($_SESSION['topicseen_cache'][$topicOptions['board']]))
$_SESSION['topicseen_cache'][$topicOptions['board']]--;
updateStats('message', true, $msgOptions['id']);
if ($msgOptions['approved'])
updateLastMessages($topicOptions['board'], $new_topic || !empty($topicOptions['is_approved']) ? $msgOptions['id'] : 0);
if ($msgOptions['send_notifications'] && $msgOptions['approved'])
$smcFunc['db_insert']('',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
array('$sourcedir/tasks/CreatePost-Notify.php', 'CreatePost_Notify_Background', $smcFunc['json_encode'](array(
'msgOptions' => $msgOptions,
'topicOptions' => $topicOptions,
'posterOptions' => $posterOptions,
'type' => $new_topic ? 'topic' : 'reply',
)), 0),
array('id_task')
);
ignore_user_abort($previous_ignore_user_abort);
return true;
}
function modifyPost(&$msgOptions, &$topicOptions, &$posterOptions)
{
global $user_info, $modSettings, $smcFunc, $sourcedir;
$topicOptions['poll'] = isset($topicOptions['poll']) ? (int) $topicOptions['poll'] : null;
$topicOptions['lock_mode'] = isset($topicOptions['lock_mode']) ? $topicOptions['lock_mode'] : null;
$topicOptions['sticky_mode'] = isset($topicOptions['sticky_mode']) ? $topicOptions['sticky_mode'] : null;
$messages_columns = array();
if (isset($posterOptions['name']))
$messages_columns['poster_name'] = $posterOptions['name'];
if (isset($posterOptions['email']))
$messages_columns['poster_email'] = $posterOptions['email'];
if (isset($msgOptions['icon']))
$messages_columns['icon'] = $msgOptions['icon'];
if (isset($msgOptions['subject']))
$messages_columns['subject'] = $msgOptions['subject'];
if (isset($msgOptions['body']))
{
$messages_columns['body'] = $msgOptions['body'];
if (!empty($modSettings['search_custom_index_config']))
{
$request = $smcFunc['db_query']('', '
SELECT body
FROM {db_prefix}messages
WHERE id_msg = {int:id_msg}',
array(
'id_msg' => $msgOptions['id'],
)
);
list ($msgOptions['old_body']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
}
if (!empty($msgOptions['modify_time']))
{
$messages_columns['modified_time'] = $msgOptions['modify_time'];
$messages_columns['modified_name'] = $msgOptions['modify_name'];
$messages_columns['modified_reason'] = $msgOptions['modify_reason'];
$messages_columns['id_msg_modified'] = $modSettings['maxMsgID'];
}
if (isset($msgOptions['smileys_enabled']))
$messages_columns['smileys_enabled'] = empty($msgOptions['smileys_enabled']) ? 0 : 1;
$messageInts = array('modified_time', 'id_msg_modified', 'smileys_enabled');
$update_parameters = array(
'id_msg' => $msgOptions['id'],
);
require_once($sourcedir . '/Search.php');
$searchAPI = findSearchAPI();
if ($searchAPI->supportsMethod('postRemoved'))
$searchAPI->postRemoved($msgOptions['id']);
if (!empty($modSettings['enable_mentions']) && isset($msgOptions['body']))
{
require_once($sourcedir . '/Mentions.php');
$oldmentions = array();
if (!empty($msgOptions['old_body']))
{
preg_match_all('/\[member\=([0-9]+)\]([^\[]*)\[\/member\]/U', $msgOptions['old_body'], $match);
if (isset($match[1]) && isset($match[2]) && is_array($match[1]) && is_array($match[2]))
foreach ($match[1] as $i => $oldID)
$oldmentions[$oldID] = array('id' => $oldID, 'real_name' => $match[2][$i]);
if (empty($modSettings['search_custom_index_config']))
unset($msgOptions['old_body']);
}
$mentions = Mentions::getMentionedMembers($msgOptions['body']);
$messages_columns['body'] = $msgOptions['body'] = Mentions::getBody($msgOptions['body'], $mentions);
if (isset($mentions[$user_info['id']]))
unset($mentions[$user_info['id']]);
if (isset($oldmentions[$user_info['id']]))
unset($oldmentions[$user_info['id']]);
if (is_array($mentions) && is_array($oldmentions) && count(array_diff_key($mentions, $oldmentions)) > 0 && count($mentions) > count($oldmentions))
{
$msgOptions['mentioned_members'] = array_diff_key($mentions, $oldmentions);
$smcFunc['db_insert']('',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
array('$sourcedir/tasks/CreatePost-Notify.php', 'CreatePost_Notify_Background', $smcFunc['json_encode'](array(
'msgOptions' => $msgOptions,
'topicOptions' => $topicOptions,
'posterOptions' => $posterOptions,
'type' => 'edit',
)), 0),
array('id_task')
);
}
}
call_integration_hook('integrate_modify_post', array(&$messages_columns, &$update_parameters, &$msgOptions, &$topicOptions, &$posterOptions, &$messageInts));
foreach ($messages_columns as $var => $val)
{
$messages_columns[$var] = $var . ' = {' . (in_array($var, $messageInts) ? 'int' : 'string') . ':var_' . $var . '}';
$update_parameters['var_' . $var] = $val;
}
if (empty($messages_columns))
return true;
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET ' . implode(', ', $messages_columns) . '
WHERE id_msg = {int:id_msg}',
$update_parameters
);
if ($topicOptions['sticky_mode'] !== null || $topicOptions['lock_mode'] !== null || $topicOptions['poll'] !== null)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}topics
SET
is_sticky = {raw:is_sticky},
locked = {raw:locked},
id_poll = {raw:id_poll}
WHERE id_topic = {int:id_topic}',
array(
'is_sticky' => $topicOptions['sticky_mode'] === null ? 'is_sticky' : (int) $topicOptions['sticky_mode'],
'locked' => $topicOptions['lock_mode'] === null ? 'locked' : (int) $topicOptions['lock_mode'],
'id_poll' => $topicOptions['poll'] === null ? 'id_poll' : (int) $topicOptions['poll'],
'id_topic' => $topicOptions['id'],
)
);
}
if (!empty($topicOptions['mark_as_read']) && !$user_info['is_guest'])
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}log_topics
SET id_msg = {int:id_msg}
WHERE id_member = {int:current_member}
AND id_topic = {int:id_topic}',
array(
'current_member' => $user_info['id'],
'id_msg' => $modSettings['maxMsgID'],
'id_topic' => $topicOptions['id'],
)
);
$flag = $smcFunc['db_affected_rows']() != 0;
if (empty($flag))
{
$smcFunc['db_insert']('ignore',
'{db_prefix}log_topics',
array('id_topic' => 'int', 'id_member' => 'int', 'id_msg' => 'int'),
array($topicOptions['id'], $user_info['id'], $modSettings['maxMsgID']),
array('id_topic', 'id_member')
);
}
}
require_once($sourcedir . '/Search.php');
$searchAPI = findSearchAPI();
if (is_callable(array($searchAPI, 'postModified')))
$searchAPI->postModified($msgOptions, $topicOptions, $posterOptions);
if (isset($msgOptions['subject']))
{
$request = $smcFunc['db_query']('', '
SELECT id_topic
FROM {db_prefix}topics
WHERE id_first_msg = {int:id_first_msg}
LIMIT 1',
array(
'id_first_msg' => $msgOptions['id'],
)
);
if ($smcFunc['db_num_rows']($request) == 1)
updateStats('subject', $topicOptions['id'], $msgOptions['subject']);
$smcFunc['db_free_result']($request);
}
if ($modSettings['postmod_active'] && isset($msgOptions['approved']))
approvePosts($msgOptions['id'], $msgOptions['approved']);
return true;
}
function approvePosts($msgs, $approve = true, $notify = true)
{
global $smcFunc;
if (!is_array($msgs))
$msgs = array($msgs);
if (empty($msgs))
return false;
$request = $smcFunc['db_query']('', '
SELECT m.id_msg, m.approved, m.id_topic, m.id_board, t.id_first_msg, t.id_last_msg,
m.body, m.subject, COALESCE(mem.real_name, m.poster_name) AS poster_name, m.id_member,
t.approved AS topic_approved, b.count_posts
FROM {db_prefix}messages AS m
INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
WHERE m.id_msg IN ({array_int:message_list})
AND m.approved = {int:approved_state}',
array(
'message_list' => $msgs,
'approved_state' => $approve ? 0 : 1,
)
);
$msgs = array();
$topics = array();
$topic_changes = array();
$board_changes = array();
$notification_topics = array();
$notification_posts = array();
$member_post_changes = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$msgs[] = $row['id_msg'];
$topics[] = $row['id_topic'];
if (!isset($topic_changes[$row['id_topic']]))
$topic_changes[$row['id_topic']] = array(
'id_last_msg' => $row['id_last_msg'],
'approved' => $row['topic_approved'],
'replies' => 0,
'unapproved_posts' => 0,
);
if (!isset($board_changes[$row['id_board']]))
$board_changes[$row['id_board']] = array(
'posts' => 0,
'topics' => 0,
'unapproved_posts' => 0,
'unapproved_topics' => 0,
);
if ($row['id_msg'] == $row['id_first_msg'])
{
$topic_changes[$row['id_topic']]['approved'] = $approve ? 1 : 0;
$board_changes[$row['id_board']]['unapproved_topics'] += $approve ? -1 : 1;
$board_changes[$row['id_board']]['topics'] += $approve ? 1 : -1;
$notification_topics[] = array(
'body' => $row['body'],
'subject' => $row['subject'],
'name' => $row['poster_name'],
'board' => $row['id_board'],
'topic' => $row['id_topic'],
'msg' => $row['id_first_msg'],
'poster' => $row['id_member'],
'new_topic' => true,
);
}
else
{
$topic_changes[$row['id_topic']]['replies'] += $approve ? 1 : -1;
if ($row['id_msg'] > $row['id_last_msg'])
$notification_posts[$row['id_topic']] = array(
'id' => $row['id_msg'],
'body' => $row['body'],
'subject' => $row['subject'],
'name' => $row['poster_name'],
'topic' => $row['id_topic'],
'board' => $row['id_board'],
'poster' => $row['id_member'],
'new_topic' => false,
'msg' => $row['id_msg'],
);
}
if ($approve && $row['id_msg'] > $topic_changes[$row['id_topic']]['id_last_msg'])
$topic_changes[$row['id_topic']]['id_last_msg'] = $row['id_msg'];
elseif (!$approve)
$topic_changes[$row['id_topic']]['id_last_msg'] = $row['id_first_msg'];
$topic_changes[$row['id_topic']]['unapproved_posts'] += $approve ? -1 : 1;
$board_changes[$row['id_board']]['unapproved_posts'] += $approve ? -1 : 1;
$board_changes[$row['id_board']]['posts'] += $approve ? 1 : -1;
if ($row['id_member'] && empty($row['count_posts']))
$member_post_changes[$row['id_member']] = isset($member_post_changes[$row['id_member']]) ? $member_post_changes[$row['id_member']] + 1 : 1;
}
$smcFunc['db_free_result']($request);
if (empty($msgs))
return;
$smcFunc['db_query']('', '
UPDATE {db_prefix}messages
SET approved = {int:approved_state}
WHERE id_msg IN ({array_int:message_list})',
array(
'message_list' => $msgs,
'approved_state' => $approve ? 1 : 0,
)
);
if (!$approve)
{
$request = $smcFunc['db_query']('', '
SELECT id_topic, MAX(id_msg) AS id_last_msg
FROM {db_prefix}messages
WHERE id_topic IN ({array_int:topic_list})
AND approved = {int:approved}
GROUP BY id_topic',
array(
'topic_list' => $topics,
'approved' => 1,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$topic_changes[$row['id_topic']]['id_last_msg'] = $row['id_last_msg'];
$smcFunc['db_free_result']($request);
}
foreach ($topic_changes as $id => $changes)
$smcFunc['db_query']('', '
UPDATE {db_prefix}topics
SET approved = {int:approved}, unapproved_posts = unapproved_posts + {int:unapproved_posts},
num_replies = num_replies + {int:num_replies}, id_last_msg = {int:id_last_msg}
WHERE id_topic = {int:id_topic}',
array(
'approved' => $changes['approved'],
'unapproved_posts' => $changes['unapproved_posts'],
'num_replies' => $changes['replies'],
'id_last_msg' => $changes['id_last_msg'],
'id_topic' => $id,
)
);
foreach ($board_changes as $id => $changes)
$smcFunc['db_query']('', '
UPDATE {db_prefix}boards
SET num_posts = num_posts + {int:num_posts}, unapproved_posts = unapproved_posts + {int:unapproved_posts},
num_topics = num_topics + {int:num_topics}, unapproved_topics = unapproved_topics + {int:unapproved_topics}
WHERE id_board = {int:id_board}',
array(
'num_posts' => $changes['posts'],
'unapproved_posts' => $changes['unapproved_posts'],
'num_topics' => $changes['topics'],
'unapproved_topics' => $changes['unapproved_topics'],
'id_board' => $id,
)
);
if ($approve)
{
$task_rows = array();
foreach (array_merge($notification_topics, $notification_posts) as $topic)
$task_rows[] = array(
'$sourcedir/tasks/CreatePost-Notify.php', 'CreatePost_Notify_Background', $smcFunc['json_encode'](array(
'msgOptions' => array(
'id' => $topic['msg'],
'body' => $topic['body'],
'subject' => $topic['subject'],
),
'topicOptions' => array(
'id' => $topic['topic'],
'board' => $topic['board'],
),
'posterOptions' => array(
'id' => $topic['poster'],
'name' => $topic['name'],
),
'type' => $topic['new_topic'] ? 'topic' : 'reply',
)), 0
);
if ($notify)
$smcFunc['db_insert']('',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
$task_rows,
array('id_task')
);
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}approval_queue
WHERE id_msg IN ({array_int:message_list})
AND id_attach = {int:id_attach}',
array(
'message_list' => $msgs,
'id_attach' => 0,
)
);
}
else
{
$msgInserts = array();
foreach ($msgs as $msg)
$msgInserts[] = array($msg);
$smcFunc['db_insert']('ignore',
'{db_prefix}approval_queue',
array('id_msg' => 'int'),
$msgInserts,
array('id_msg')
);
}
updateLastMessages(array_keys($board_changes));
if (!empty($member_post_changes))
foreach ($member_post_changes as $id_member => $count_change)
updateMemberData($id_member, array('posts' => 'posts ' . ($approve ? '+' : '-') . ' ' . $count_change));
return true;
}
function approveTopics($topics, $approve = true)
{
global $smcFunc;
if (!is_array($topics))
$topics = array($topics);
if (empty($topics))
return false;
$approve_type = $approve ? 0 : 1;
$request = $smcFunc['db_query']('', '
SELECT id_msg
FROM {db_prefix}messages
WHERE id_topic IN ({array_int:topic_list})
AND approved = {int:approve_type}',
array(
'topic_list' => $topics,
'approve_type' => $approve_type,
)
);
$msgs = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$msgs[] = $row['id_msg'];
$smcFunc['db_free_result']($request);
return approvePosts($msgs, $approve);
}
function updateLastMessages($setboards, $id_msg = 0)
{
global $board_info, $board, $smcFunc;
if (empty($setboards))
return false;
if (!is_array($setboards))
$setboards = array($setboards);
if (!$id_msg)
{
$request = $smcFunc['db_query']('', '
SELECT id_board, MAX(id_last_msg) AS id_msg
FROM {db_prefix}topics
WHERE id_board IN ({array_int:board_list})
AND approved = {int:approved}
GROUP BY id_board',
array(
'board_list' => $setboards,
'approved' => 1,
)
);
$lastMsg = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$lastMsg[$row['id_board']] = $row['id_msg'];
$smcFunc['db_free_result']($request);
}
else
{
foreach ($setboards as $id_board)
$lastMsg[$id_board] = $id_msg;
}
$parent_boards = array();
$lastModified = $lastMsg;
foreach ($setboards as $id_board)
{
if (!isset($lastMsg[$id_board]))
{
$lastMsg[$id_board] = 0;
$lastModified[$id_board] = 0;
}
if (!empty($board) && $id_board == $board)
$parents = $board_info['parent_boards'];
else
$parents = getBoardParents($id_board);
foreach ($parents as $id => $parent)
{
if ($parent['level'] != 0)
{
if (isset($lastModified[$id]) && $lastModified[$id_board] > $lastModified[$id])
$lastModified[$id] = $lastModified[$id_board];
elseif (!isset($lastModified[$id]) && (!isset($parent_boards[$id]) || $parent_boards[$id] < $lastModified[$id_board]))
$parent_boards[$id] = $lastModified[$id_board];
}
}
}
$board_updates = array();
$parent_updates = array();
foreach ($parent_boards as $id => $msg)
{
if (!isset($parent_updates[$msg]))
$parent_updates[$msg] = array($id);
else
$parent_updates[$msg][] = $id;
}
foreach ($lastMsg as $id => $msg)
{
if (!isset($board_updates[$msg . '-' . $lastModified[$id]]))
$board_updates[$msg . '-' . $lastModified[$id]] = array(
'id' => $msg,
'updated' => $lastModified[$id],
'boards' => array($id)
);
else
$board_updates[$msg . '-' . $lastModified[$id]]['boards'][] = $id;
}
foreach ($parent_updates as $id_msg => $boards)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}boards
SET id_msg_updated = {int:id_msg_updated}
WHERE id_board IN ({array_int:board_list})
AND id_msg_updated < {int:id_msg_updated}',
array(
'board_list' => $boards,
'id_msg_updated' => $id_msg,
)
);
}
foreach ($board_updates as $board_data)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}boards
SET id_last_msg = {int:id_last_msg}, id_msg_updated = {int:id_msg_updated}
WHERE id_board IN ({array_int:board_list})',
array(
'board_list' => $board_data['boards'],
'id_last_msg' => $board_data['id'],
'id_msg_updated' => $board_data['updated'],
)
);
}
}
function adminNotify($type, $memberID, $member_name = null)
{
global $smcFunc;
if ($member_name == null)
{
$request = $smcFunc['db_query']('', '
SELECT real_name
FROM {db_prefix}members
WHERE id_member = {int:id_member}
LIMIT 1',
array(
'id_member' => $memberID,
)
);
list ($member_name) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
}
$smcFunc['db_insert']('insert',
'{db_prefix}background_tasks',
array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
array('$sourcedir/tasks/Register-Notify.php', 'Register_Notify_Background', $smcFunc['json_encode'](array(
'new_member_id' => $memberID,
'new_member_name' => $member_name,
'notify_type' => $type,
'time' => time(),
)), 0),
array('id_task')
);
}
function loadEmailTemplate($template, $replacements = array(), $lang = '', $loadLang = true)
{
global $txt, $mbname, $scripturl, $settings;
if ($loadLang)
loadLanguage('EmailTemplates', $lang);
if (!isset($txt[$template . '_subject']) || !isset($txt[$template . '_body']))
fatal_lang_error('email_no_template', 'template', array($template));
$ret = array(
'subject' => $txt[$template . '_subject'],
'body' => $txt[$template . '_body'],
'is_html' => !empty($txt[$template . '_html']),
);
$replacements += array(
'FORUMNAME' => $mbname,
'SCRIPTURL' => $scripturl,
'THEMEURL' => $settings['theme_url'],
'IMAGESURL' => $settings['images_url'],
'DEFAULT_THEMEURL' => $settings['default_theme_url'],
'REGARDS' => $txt['regards_team'],
);
$find = array();
$replace = array();
foreach ($replacements as $f => $r)
{
$find[] = '{' . $f . '}';
$replace[] = $r;
}
$ret['subject'] = str_replace($find, $replace, $ret['subject']);
$ret['body'] = str_replace($find, $replace, $ret['body']);
$ret['subject'] = preg_replace_callback('~{USER.([^}]+)}~', 'user_info_callback', $ret['subject']);
$ret['body'] = preg_replace_callback('~{USER.([^}]+)}~', 'user_info_callback', $ret['body']);
return $ret;
}
function user_info_callback($matches)
{
global $user_info;
if (empty($matches[1]))
return '';
$use_ref = true;
$ref = &$user_info;
foreach (explode('.', $matches[1]) as $index)
{
if ($use_ref && isset($ref[$index]))
$ref = &$ref[$index];
else
{
$use_ref = false;
break;
}
}
return $use_ref ? $ref : $matches[0];
}
function spell_init()
{
global $context, $txt;
$context['spell_utf8'] = ($txt['lang_character_set'] == 'UTF-8');
$lang_locale = str_replace('.utf8', '', $txt['lang_locale']);
if (function_exists('enchant_broker_init') && ($context['spell_utf8'] || function_exists('iconv')))
{
$context['enchant_broker'] = enchant_broker_init();
if (!empty($lang_locale) && enchant_broker_dict_exists($context['enchant_broker'], $lang_locale))
{
$enchant_link = enchant_broker_request_dict($context['enchant_broker'], $lang_locale);
}
elseif (enchant_broker_dict_exists($context['enchant_broker'], $txt['lang_dictionary']))
{
$enchant_link = enchant_broker_request_dict($context['enchant_broker'], $txt['lang_dictionary']);
}
if (!empty($enchant_link))
{
$context['provider'] = 'enchant';
return $enchant_link;
}
else
{
@enchant_broker_free($context['enchant_broker']);
}
}
if (function_exists('pspell_new'))
{
ob_start();
$old = error_reporting(0);
pspell_new('en');
$pspell_link = pspell_new($txt['lang_dictionary'], $txt['lang_spelling'], '', strtr($context['character_set'], array('iso-' => 'iso', 'ISO-' => 'iso')), PSPELL_FAST | PSPELL_RUN_TOGETHER);
if (!$pspell_link)
$pspell_link = pspell_new('en', '', '', '', PSPELL_FAST | PSPELL_RUN_TOGETHER);
error_reporting($old);
ob_end_clean();
if ($pspell_link)
{
$context['provider'] = 'pspell';
return $pspell_link;
}
}
return false;
}
function spell_check($dict, $word)
{
global $context, $txt;
if ($context['provider'] == 'enchant')
{
if (!$context['spell_utf8'])
{
$word = iconv($txt['lang_character_set'], 'UTF-8', $word);
}
return enchant_dict_check($dict, $word);
}
elseif ($context['provider'] == 'pspell')
{
return pspell_check($dict, $word);
}
}
function spell_suggest($dict, $word)
{
global $context, $txt;
if ($context['provider'] == 'enchant')
{
if (!$context['spell_utf8'])
{
$word = iconv($txt['lang_character_set'], 'UTF-8', $word);
$suggestions = enchant_dict_suggest($dict, $word);
foreach ($suggestions as $index => $suggestion)
{
$suggestions[$index] = iconv('UTF-8', $txt['lang_character_set'] . '//TRANSLIT', $suggestion);
}
return $suggestions;
}
else
{
return enchant_dict_suggest($dict, $word);
}
}
elseif ($context['provider'] == 'pspell')
{
return pspell_suggest($dict, $word);
}
}
?>