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: 3023: 3024: 3025: 3026: 3027: 3028: 3029: 3030: 3031: 3032: 3033: 3034: 3035: 3036: 3037: 3038: 3039: 3040: 3041: 3042: 3043: 3044: 3045: 3046: 3047: 3048: 3049: 3050: 3051: 3052: 3053: 3054: 3055: 3056: 3057: 3058: 3059: 3060: 3061: 3062: 3063: 3064: 3065: 3066: 3067: 3068: 3069: 3070: 3071: 3072: 3073: 3074: 3075: 3076: 3077: 3078: 3079: 3080: 3081: 3082: 3083: 3084: 3085: 3086: 3087: 3088: 3089: 3090: 3091: 3092: 3093: 3094: 3095: 3096: 3097: 3098: 3099: 3100: 3101: 3102: 3103: 3104: 3105: 3106: 3107: 3108: 3109: 3110: 3111: 3112: 3113: 3114: 3115: 3116: 3117: 3118: 3119: 3120: 3121: 3122: 3123: 3124: 3125: 3126: 3127: 3128: 3129: 3130: 3131: 3132: 3133: 3134: 3135: 3136: 3137: 3138: 3139: 3140: 3141: 3142: 3143: 3144: 3145: 3146: 3147: 3148: 3149: 3150: 3151: 3152: 3153: 3154: 3155: 3156: 3157: 3158: 3159: 3160: 3161: 3162: 3163: 3164: 3165: 3166: 3167: 3168: 3169: 3170: 3171: 3172: 3173: 3174: 3175: 3176: 3177: 3178: 3179: 3180: 3181: 3182: 3183: 3184: 3185: 3186: 3187: 3188: 3189: 3190: 3191: 3192: 3193: 3194: 3195: 3196: 3197: 3198: 3199: 3200: 3201: 3202: 3203: 3204: 3205: 3206: 3207: 3208: 3209: 3210: 3211: 3212: 3213: 3214: 3215: 3216: 3217: 3218: 3219: 3220: 3221: 3222: 3223: 3224: 3225: 3226: 3227: 3228: 3229: 3230: 3231: 3232: 3233: 3234: 3235: 3236: 3237: 3238: 3239: 3240: 3241: 3242: 3243: 3244: 3245: 3246: 3247: 3248: 3249: 3250: 3251: 3252: 3253: 3254: 3255: 3256: 3257: 3258: 3259: 3260: 3261: 3262: 3263: 3264: 3265: 3266: 3267: 3268: 3269: 3270: 3271: 3272: 3273: 3274: 3275: 3276: 3277: 3278: 3279: 3280: 3281: 3282: 3283: 3284: 3285: 3286: 3287: 3288: 3289: 3290: 3291: 3292: 3293: 3294: 3295: 3296: 3297: 3298: 3299: 3300: 3301: 3302: 3303: 3304: 3305: 3306: 3307: 3308: 3309: 3310: 3311: 3312: 3313: 3314: 3315: 3316: 3317: 3318: 3319: 3320: 3321: 3322: 3323: 3324: 3325: 3326: 3327: 3328: 3329: 3330: 3331: 3332: 3333: 3334: 3335: 3336: 3337: 3338: 3339: 3340: 3341: 3342: 3343: 3344: 3345: 3346: 3347: 3348: 3349: 3350: 3351: 3352: 3353: 3354: 3355: 3356: 3357: 3358: 3359: 3360: 3361: 3362: 3363: 3364: 3365: 3366: 3367: 3368: 3369: 3370: 3371: 3372: 3373: 3374: 3375: 3376: 3377: 3378: 3379: 3380: 3381: 3382: 3383: 3384: 3385: 3386: 3387: 3388: 3389: 3390: 3391: 3392: 3393: 3394: 3395: 3396: 3397: 3398: 3399: 3400: 3401: 3402: 3403: 3404: 3405: 3406: 3407: 3408: 3409: 3410: 3411: 3412: 3413: 3414: 3415: 3416: 3417: 3418: 3419: 3420: 3421: 3422: 3423: 3424: 3425: 3426: 3427: 3428: 3429: 3430: 3431: 3432: 3433: 3434: 3435: 3436: 3437: 3438: 3439: 3440: 3441: 3442: 3443: 3444: 3445: 3446: 3447: 3448: 3449: 3450: 3451: 3452: 3453: 3454: 3455: 3456: 3457: 3458: 3459: 3460: 3461: 3462: 3463: 3464: 3465: 3466: 3467: 3468: 3469: 3470: 3471: 3472: 3473: 3474: 3475: 3476: 3477: 3478: 3479: 3480: 3481: 3482: 3483: 3484: 3485: 3486: 3487: 3488: 3489: 3490: 3491: 3492: 3493: 3494: 3495: 3496: 3497: 3498: 3499: 3500: 3501: 3502: 3503: 3504: 3505: 3506: 3507: 3508: 3509: 3510: 3511: 3512: 3513: 3514: 3515: 3516: 3517: 3518: 3519: 3520: 3521: 3522: 3523: 3524: 3525: 3526: 3527: 3528: 3529: 3530: 3531: 3532: 3533: 3534: 3535: 3536: 3537: 3538: 3539: 3540: 3541: 3542: 3543: 3544: 3545: 3546: 3547: 3548: 3549: 3550: 3551: 3552: 3553: 3554: 3555: 3556: 3557: 3558: 3559: 3560: 3561: 3562: 3563: 3564: 3565: 3566: 3567: 3568: 3569: 3570: 3571: 3572: 3573: 3574: 3575: 3576: 3577: 3578: 3579: 3580: 3581: 3582: 3583: 3584: 3585: 3586: 3587: 3588: 3589: 3590: 3591: 3592: 3593: 3594: 3595: 3596: 3597: 3598: 3599: 3600: 3601: 3602: 3603: 3604: 3605: 3606: 3607: 3608: 3609: 3610: 3611: 3612: 3613: 3614: 3615: 3616: 3617: 3618: 3619: 3620: 3621: 3622: 3623: 3624: 3625: 3626: 3627: 3628: 3629: 3630: 3631: 3632: 3633: 3634: 3635: 3636: 3637: 3638: 3639: 3640: 3641: 3642: 3643: 3644: 3645: 3646: 3647: 3648: 3649: 3650: 3651: 3652: 3653: 3654: 3655: 3656: 3657: 3658: 3659: 3660: 3661: 3662: 3663: 3664: 3665: 3666: 3667: 3668: 3669: 3670: 3671: 3672: 3673: 3674: 3675: 3676: 3677: 3678: 3679: 3680: 3681: 3682: 3683: 3684: 3685: 3686: 3687: 3688: 3689: 3690: 3691: 3692: 3693: 3694: 3695: 3696: 3697: 3698: 3699: 3700: 3701: 3702: 3703: 3704: 3705: 3706: 3707: 3708: 3709: 3710: 3711: 3712: 3713: 3714: 3715: 3716: 3717: 3718: 3719: 3720: 3721: 3722: 3723: 3724: 3725: 3726: 3727: 3728: 3729: 3730: 3731: 3732: 3733: 3734: 3735: 3736: 3737: 3738: 3739: 3740: 3741: 3742: 3743: 3744: 3745: 3746: 3747: 3748: 3749: 3750: 3751: 3752: 3753: 3754: 3755: 3756: 3757: 3758: 3759: 3760: 3761: 3762: 3763: 3764: 3765: 3766: 3767: 3768: 3769: 3770: 3771: 3772: 3773: 3774: 3775: 3776: 3777: 3778: 3779: 3780: 3781: 3782: 3783: 3784: 3785: 3786: 3787: 3788: 3789: 3790: 3791: 3792: 3793: 3794: 3795: 3796: 3797: 3798: 3799: 3800: 3801: 3802: 3803: 3804: 3805: 3806: 3807: 3808: 3809: 3810: 3811: 3812: 3813: 3814: 3815: 3816: 3817: 3818: 3819: 3820: 3821: 3822: 3823: 3824: 3825: 3826: 3827: 3828: 3829: 3830: 3831: 3832: 3833: 3834: 3835: 3836: 3837: 3838: 3839: 3840: 3841: 3842: 3843: 3844: 3845: 3846: 3847: 3848: 3849: 3850: 3851: 3852: 3853: 3854: 3855: 3856: 3857: 3858: 3859: 3860: 3861: 3862: 3863: 3864: 3865: 3866: 3867: 3868: 3869: 3870: 3871: 3872: 3873: 3874: 3875: 3876: 3877: 3878: 3879: 3880: 3881: 3882: 3883: 3884: 3885: 3886: 3887: 3888: 3889: 3890: 3891: 3892: 3893: 3894: 3895: 3896: 3897: 3898: 3899: 3900: 3901: 3902: 3903: 3904: 3905: 3906: 3907: 3908: 3909: 3910: 3911: 3912: 3913: 3914: 3915: 3916: 3917: 3918: 3919: 3920: 3921: 3922: 3923: 3924: 3925: 3926: 3927: 3928: 3929: 3930: 3931: 3932: 3933: 3934: 3935: 3936: 3937: 3938: 3939: 3940: 3941: 3942: 3943: 3944: 3945: 3946: 3947: 3948: 3949: 3950: 3951: 3952: 3953: 3954: 3955: 3956: 3957: 3958: 3959: 3960: 3961: 3962: 3963: 3964: 3965: 3966: 3967: 3968: 3969: 3970: 3971: 3972: 3973: 3974: 3975: 3976: 3977: 3978: 3979: 3980: 3981: 3982: 3983: 3984: 3985: 3986: 3987: 3988: 3989: 3990: 3991: 3992: 3993: 3994: 3995: 3996: 3997: 3998: 3999: 4000: 4001: 4002: 4003: 4004: 4005: 4006: 4007: 4008: 4009: 4010: 4011: 4012: 4013: 4014: 4015: 4016: 4017: 4018: 4019: 4020: 4021: 4022: 4023: 4024: 4025: 4026: 4027: 4028: 4029: 4030: 4031: 4032: 4033: 4034: 4035: 4036: 4037: 4038: 4039: 4040: 4041: 4042: 4043: 4044: 4045: 4046: 4047: 4048: 4049: 4050: 4051: 4052: 4053: 4054: 4055: 4056: 4057: 4058: 4059: 4060: 4061: 4062: 4063: 4064: 4065: 4066: 4067: 4068: 4069: 4070: 4071: 4072: 4073: 4074: 4075: 4076: 4077: 4078: 4079: 4080: 4081: 4082: 4083: 4084: 4085: 4086: 4087: 4088: 4089: 4090: 4091: 4092: 4093: 4094: 4095: 4096: 4097: 4098: 4099: 4100: 4101: 4102: 4103: 4104: 4105: 4106: 4107: 4108: 4109: 4110: 4111: 4112: 4113: 4114: 4115: 4116: 4117: 4118: 4119: 4120: 4121: 4122: 4123: 4124: 4125: 4126: 4127: 4128: 4129: 4130: 4131: 4132: 4133: 4134: 4135: 4136: 4137: 4138: 4139: 4140: 4141: 4142: 4143: 4144: 4145: 4146: 4147: 4148: 4149: 4150: 4151: 4152: 4153: 4154: 4155: 4156: 4157: 4158: 4159: 4160: 4161: 4162: 4163: 4164: 4165: 4166: 4167: 4168: 4169: 4170: 4171: 4172: 4173: 4174: 4175: 4176: 4177: 4178: 4179: 4180: 4181: 4182: 4183: 4184:
<?php
if (!defined('SMF'))
die('No direct access...');
function MessageMain()
{
global $txt, $scripturl, $sourcedir, $context, $user_info, $user_settings, $smcFunc, $modSettings;
is_not_guest();
isAllowedTo('pm_read');
require_once($sourcedir . '/Subs-Post.php');
loadLanguage('PersonalMessage+Drafts');
if (!isset($_REQUEST['xml']))
loadTemplate('PersonalMessage');
if ($user_info['is_admin'])
$context['message_limit'] = 0;
elseif (($context['message_limit'] = cache_get_data('msgLimit:' . $user_info['id'], 360)) === null)
{
$request = $smcFunc['db_query']('', '
SELECT MAX(max_messages) AS top_limit, MIN(max_messages) AS bottom_limit
FROM {db_prefix}membergroups
WHERE id_group IN ({array_int:users_groups})',
array(
'users_groups' => $user_info['groups'],
)
);
list ($maxMessage, $minMessage) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$context['message_limit'] = $minMessage == 0 ? 0 : $maxMessage;
cache_put_data('msgLimit:' . $user_info['id'], $context['message_limit'], 360);
}
if (!empty($context['message_limit']))
{
$bar = ($user_info['messages'] * 100) / $context['message_limit'];
$context['limit_bar'] = array(
'messages' => $user_info['messages'],
'allowed' => $context['message_limit'],
'percent' => $bar,
'bar' => min(100, (int) $bar),
'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], round($bar, 1)),
);
}
if (isset($_GET['done']) && ($_GET['done'] == 'sent'))
$context['pm_sent'] = true;
$context['labels'] = array();
if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
{
$context['labels'][-1] = array(
'id' => -1,
'name' => $txt['pm_msg_label_inbox'],
'messages' => 0,
'unread_messages' => 0,
);
$result = $smcFunc['db_query']('', '
SELECT COUNT(*) AS total, SUM(is_read & 1) AS num_read
FROM {db_prefix}pm_recipients
WHERE id_member = {int:current_member}
AND in_inbox = {int:in_inbox}
AND deleted = {int:not_deleted}',
array(
'current_member' => $user_info['id'],
'in_inbox' => 1,
'not_deleted' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$context['labels'][-1]['messages'] = $row['total'];
$context['labels'][-1]['unread_messages'] = $row['total'] - $row['num_read'];
}
$smcFunc['db_free_result']($result);
$result = $smcFunc['db_query']('', '
SELECT l.id_label, l.name, COALESCE(SUM(pr.is_read & 1), 0) AS num_read, COALESCE(COUNT(pr.id_pm), 0) AS total
FROM {db_prefix}pm_labels AS l
LEFT JOIN {db_prefix}pm_labeled_messages AS pl ON (pl.id_label = l.id_label)
LEFT JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pl.id_pm)
WHERE l.id_member = {int:current_member}
GROUP BY l.id_label, l.name',
array(
'current_member' => $user_info['id'],
)
);
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$context['labels'][$row['id_label']] = array(
'id' => $row['id_label'],
'name' => $row['name'],
'messages' => $row['total'],
'unread_messages' => $row['total'] - $row['num_read']
);
}
$smcFunc['db_free_result']($result);
cache_put_data('labelCounts:' . $user_info['id'], $context['labels'], 720);
}
if ($user_settings['new_pm'])
{
ApplyRules();
updateMemberData($user_info['id'], array('new_pm' => 0));
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET is_new = {int:not_new}
WHERE id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'not_new' => 0,
)
);
}
$context['currently_using_labels'] = count($context['labels']) > 1 ? 1 : 0;
$context['current_label_id'] = isset($_REQUEST['l']) && isset($context['labels'][$_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
$context['current_label'] = &$context['labels'][$context['current_label_id']]['name'];
$context['folder'] = !isset($_REQUEST['f']) || $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent';
$context['current_label_redirect'] = 'action=pm;f=' . $context['folder'] . (isset($_GET['start']) ? ';start=' . $_GET['start'] : '') . (isset($_REQUEST['l']) ? ';l=' . $_REQUEST['l'] : '');
$context['can_issue_warning'] = allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
$context['drafts_pm_save'] = !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft');
$context['drafts_autosave'] = !empty($context['drafts_pm_save']) && !empty($modSettings['drafts_autosave_enabled']);
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm',
'name' => $txt['personal_messages']
);
$context['display_mode'] = $user_settings['pm_prefs'] & 3;
$subActions = array(
'popup' => 'MessagePopup',
'manlabels' => 'ManageLabels',
'manrules' => 'ManageRules',
'pmactions' => 'MessageActionsApply',
'prune' => 'MessagePrune',
'removeall2' => 'MessageKillAll',
'report' => 'ReportMessage',
'search' => 'MessageSearch',
'search2' => 'MessageSearch2',
'send' => 'MessagePost',
'send2' => 'MessagePost2',
'settings' => 'MessageSettings',
'showpmdrafts' => 'MessageDrafts',
);
if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
{
$_REQUEST['sa'] = '';
MessageFolder();
}
else
{
if (!isset($_REQUEST['xml']) && $_REQUEST['sa'] != 'popup')
messageIndexBar($_REQUEST['sa']);
call_helper($subActions[$_REQUEST['sa']]);
}
}
function messageIndexBar($area)
{
global $txt, $context, $scripturl, $sourcedir, $modSettings, $user_info;
$pm_areas = array(
'folders' => array(
'title' => $txt['pm_messages'],
'areas' => array(
'inbox' => array(
'label' => $txt['inbox'],
'custom_url' => $scripturl . '?action=pm',
'amt' => 0,
),
'send' => array(
'label' => $txt['new_message'],
'custom_url' => $scripturl . '?action=pm;sa=send',
'permission' => 'pm_send',
'amt' => 0,
),
'sent' => array(
'label' => $txt['sent_items'],
'custom_url' => $scripturl . '?action=pm;f=sent',
'amt' => 0,
),
'drafts' => array(
'label' => $txt['drafts_show'],
'custom_url' => $scripturl . '?action=pm;sa=showpmdrafts',
'permission' => 'pm_draft',
'enabled' => !empty($modSettings['drafts_pm_enabled']),
'amt' => 0,
),
),
'amt' => 0,
),
'labels' => array(
'title' => $txt['pm_labels'],
'areas' => array(),
'amt' => 0,
),
'actions' => array(
'title' => $txt['pm_actions'],
'areas' => array(
'search' => array(
'label' => $txt['pm_search_bar_title'],
'custom_url' => $scripturl . '?action=pm;sa=search',
),
'prune' => array(
'label' => $txt['pm_prune'],
'custom_url' => $scripturl . '?action=pm;sa=prune'
),
),
),
'pref' => array(
'title' => $txt['pm_preferences'],
'areas' => array(
'manlabels' => array(
'label' => $txt['pm_manage_labels'],
'custom_url' => $scripturl . '?action=pm;sa=manlabels',
),
'manrules' => array(
'label' => $txt['pm_manage_rules'],
'custom_url' => $scripturl . '?action=pm;sa=manrules',
),
'settings' => array(
'label' => $txt['pm_settings'],
'custom_url' => $scripturl . '?action=pm;sa=settings',
),
),
),
);
if (empty($context['currently_using_labels']))
unset($pm_areas['labels']);
else
{
foreach ($context['labels'] as $label)
{
if ($label['id'] == -1)
continue;
$pm_areas['labels']['amt'] += $label['unread_messages'];
$pm_areas['labels']['areas']['label' . $label['id']] = array(
'label' => $label['name'],
'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
'amt' => $label['unread_messages'],
'unread_messages' => $label['unread_messages'],
'messages' => $label['messages'],
);
}
}
$pm_areas['folders']['areas']['inbox']['unread_messages'] = &$context['labels'][-1]['unread_messages'];
$pm_areas['folders']['areas']['inbox']['messages'] = &$context['labels'][-1]['messages'];
if (!empty($context['labels'][-1]['unread_messages']))
{
$pm_areas['folders']['areas']['inbox']['amt'] = $context['labels'][-1]['unread_messages'];
$pm_areas['folders']['amt'] = $context['labels'][-1]['unread_messages'];
}
if (!empty($context['message_limit']))
{
$bar = round(($user_info['messages'] * 100) / $context['message_limit'], 1);
$context['limit_bar'] = array(
'messages' => $user_info['messages'],
'allowed' => $context['message_limit'],
'percent' => $bar,
'bar' => $bar > 100 ? 100 : (int) $bar,
'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], $bar)
);
}
require_once($sourcedir . '/Subs-Menu.php');
$menuOptions = array(
'current_area' => $area,
'disable_url_session_check' => true,
);
$pm_include_data = createMenu($pm_areas, $menuOptions);
unset($pm_areas);
if (!$pm_include_data && (!$user_info['is_guest'] || validateSession()))
fatal_lang_error('no_access', false);
$context['pm_menu_id'] = $context['max_menu_id'];
$context['pm_menu_name'] = 'menu_data_' . $context['pm_menu_id'];
$current_area = $pm_include_data['current_area'];
$context['menu_item_selected'] = $current_area;
if (!isset($_REQUEST['xml']))
$context['template_layers'][] = 'pm';
}
function MessagePopup()
{
global $context, $modSettings, $smcFunc, $memberContext, $scripturl, $user_settings, $db_show_debug;
$db_show_debug = false;
$context['template_layers'] = array();
$context['sub_template'] = 'pm_popup';
$context['can_send_pm'] = allowedTo('pm_send');
$context['can_draft'] = allowedTo('pm_draft') && !empty($modSettings['drafts_pm_enabled']);
$request = $smcFunc['db_query']('', '
SELECT id_pm
FROM {db_prefix}pm_recipients AS pmr
WHERE pmr.id_member = {int:current_member}
AND is_read = {int:not_read}
ORDER BY id_pm',
array(
'current_member' => $context['user']['id'],
'not_read' => 0,
)
);
$pms = array();
while ($row = $smcFunc['db_fetch_row']($request))
$pms[] = $row[0];
$smcFunc['db_free_result']($request);
if (!empty($pms))
{
$count_unread = count($pms);
if ($count_unread != $user_settings['unread_messages'])
{
updateMemberData($context['user']['id'], array('unread_messages' => $count_unread));
$context['user']['unread_messages'] = count($pms);
}
$senders = array();
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.id_pm_head, COALESCE(mem.id_member, pm.id_member_from) AS id_member_from,
COALESCE(mem.real_name, pm.from_name) AS member_from, pm.msgtime AS timestamp, pm.subject
FROM {db_prefix}personal_messages AS pm
LEFT JOIN {db_prefix}members AS mem ON (pm.id_member_from = mem.id_member)
WHERE pm.id_pm IN ({array_int:id_pms})',
array(
'id_pms' => $pms,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (!empty($row['id_member_from']))
$senders[] = $row['id_member_from'];
$row['replied_to_you'] = $row['id_pm'] != $row['id_pm_head'];
$row['time'] = timeformat($row['timestamp']);
$row['pm_link'] = '<a href="' . $scripturl . '?action=pm;f=inbox;pmsg=' . $row['id_pm'] . '">' . $row['subject'] . '</a>';
$context['unread_pms'][$row['id_pm']] = $row;
}
$smcFunc['db_free_result']($request);
$senders = loadMemberData($senders);
foreach ($senders as $member)
loadMemberContext($member);
foreach ($context['unread_pms'] as $id_pm => $details)
if (!empty($memberContext[$details['id_member_from']]))
$context['unread_pms'][$id_pm]['member'] = &$memberContext[$details['id_member_from']];
}
}
function MessageFolder()
{
global $txt, $scripturl, $modSettings, $context, $subjects_request;
global $messages_request, $user_info, $recipients, $options, $smcFunc, $user_settings;
if (isset($_GET['view']))
{
$context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
}
if (isset($_GET['start']) && $_GET['start'] != 'new')
$_GET['start'] = (int) $_GET['start'];
elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first']))
$_GET['start'] = 0;
else
$_GET['start'] = 'new';
$context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
$context['get_pmessage'] = 'prepareMessageContext';
$context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
$context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
if ($context['signature_enabled'])
{
list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
$sig_limits = explode(',', $sig_limits);
if (!empty($sig_limits[5]) || !empty($sig_limits[6]))
addInlineCss('
.signature img { ' . (!empty($sig_limits[5]) ? 'max-width: ' . (int) $sig_limits[5] . 'px; ' : '') . (!empty($sig_limits[6]) ? 'max-height: ' . (int) $sig_limits[6] . 'px; ' : '') . '}');
}
$labelJoin = '';
$labelQuery = '';
$labelQuery2 = '';
if ($context['folder'] == 'inbox' && $context['current_label_id'] == -1)
{
$labelQuery = '
AND pmr.in_inbox = 1';
}
elseif ($context['folder'] != 'sent')
{
$labelJoin = '
INNER JOIN {db_prefix}pm_labeled_messages AS pl ON (pl.id_pm = pmr.id_pm)';
$labelQuery2 = '
AND pl.id_label = ' . $context['current_label_id'];
}
messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
$sort_methods = array(
'date' => 'pm.id_pm',
'name' => 'COALESCE(mem.real_name, \'\')',
'subject' => 'pm.subject',
);
if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']]))
{
$context['sort_by'] = 'date';
$_GET['sort'] = 'pm.id_pm';
$descending = !empty($options['view_newest_pm_first']);
}
else
{
$context['sort_by'] = $_GET['sort'];
$_GET['sort'] = $sort_methods[$_GET['sort']];
$descending = isset($_GET['desc']);
}
$context['sort_direction'] = $descending ? 'down' : 'up';
$pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
$txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
if ($context['current_label_id'] == -1)
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;f=' . $context['folder'],
'name' => $pmbox
);
if ($context['current_label_id'] != -1)
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'],
'name' => $txt['pm_current_label'] . ': ' . $context['current_label']
);
if ($context['folder'] == 'sent')
$request = $smcFunc['db_query']('', '
SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
FROM {db_prefix}personal_messages AS pm
WHERE pm.id_member_from = {int:current_member}
AND pm.deleted_by_sender = {int:not_deleted}',
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
)
);
else
$request = $smcFunc['db_query']('', '
SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . $labelJoin . '
WHERE pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}' . $labelQuery . $labelQuery2,
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
)
);
list ($max_messages) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$context['show_delete'] = $max_messages > 0;
$maxPerPage = empty($modSettings['disableCustomPerPage']) && !empty($options['messages_per_page']) ? $options['messages_per_page'] : $modSettings['defaultMaxMessages'];
if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages)
$_GET['start'] = ($max_messages - 1) - (($max_messages - 1) % $maxPerPage);
elseif ($_GET['start'] < 0)
$_GET['start'] = 0;
if (isset($_GET['pmid']))
{
$pmID = (int) $_GET['pmid'];
if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
fatal_lang_error('no_access', false);
$context['current_pm'] = $pmID;
if ($max_messages <= $maxPerPage)
$_GET['start'] = 0;
elseif (!isset($_GET['kstart']))
{
if ($context['folder'] == 'sent')
$request = $smcFunc['db_query']('', '
SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
FROM {db_prefix}personal_messages
WHERE id_member_from = {int:current_member}
AND deleted_by_sender = {int:not_deleted}
AND id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
'id_pm' => $pmID,
)
);
else
$request = $smcFunc['db_query']('', '
SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . $labelJoin . '
WHERE pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}' . $labelQuery . $labelQuery2 . '
AND pmr.id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
'id_pm' => $pmID,
)
);
list ($_GET['start']) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$_GET['start'] = $maxPerPage * (int) ($_GET['start'] / $maxPerPage);
}
}
if (isset($_GET['pmsg']))
{
$pmsg = (int) $_GET['pmsg'];
if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
fatal_lang_error('no_access', false);
}
$context['page_index'] = constructPageIndex($scripturl . '?action=pm;f=' . $context['folder'] . (isset($_REQUEST['l']) ? ';l=' . (int) $_REQUEST['l'] : '') . ';sort=' . $context['sort_by'] . ($descending ? ';desc' : ''), $_GET['start'], $max_messages, $maxPerPage);
$context['start'] = $_GET['start'];
$context['links'] = array(
'first' => $_GET['start'] >= $maxPerPage ? $scripturl . '?action=pm;start=0' : '',
'prev' => $_GET['start'] >= $maxPerPage ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $maxPerPage) : '',
'next' => $_GET['start'] + $maxPerPage < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $maxPerPage) : '',
'last' => $_GET['start'] + $maxPerPage < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $maxPerPage) * $maxPerPage) : '',
'up' => $scripturl,
);
$context['page_info'] = array(
'current_page' => $_GET['start'] / $maxPerPage + 1,
'num_pages' => floor(($max_messages - 1) / $maxPerPage) + 1
);
if ($context['display_mode'] == 2)
{
if ($context['folder'] != 'sent' && $context['folder'] != 'inbox')
{
$labelJoin = '
INNER JOIN {db_prefix}pm_labeled_messages AS pl ON (pl.id_pm = pm.id_pm)';
$labelQuery = '';
$labelQuery2 = '
AND pl.id_label = ' . $context['current_label_id'];
}
$request = $smcFunc['db_query']('', '
SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
AND pmr.id_member = {int:current_member}
AND pmr.deleted = {int:deleted_by}
' . $labelQuery . ')') . $labelJoin . ($context['sort_by'] == 'name' ? ('
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
AND pm.id_pm = {int:pmsg}') . $labelQuery2 . '
GROUP BY pm.id_pm_head' . ($_GET['sort'] != 'pm.id_pm' ? ',' . $_GET['sort'] : '') . '
ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
LIMIT ' . $_GET['start'] . ', ' . $maxPerPage : ''),
array(
'current_member' => $user_info['id'],
'deleted_by' => 0,
'sort' => $_GET['sort'],
'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
)
);
}
else
{
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($context['sort_by'] == 'name' ? '
LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
AND pmr.id_member = {int:current_member}
AND pmr.deleted = {int:is_deleted}
' . $labelQuery . ')') . $labelJoin . ($context['sort_by'] == 'name' ? ('
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
AND pm.id_pm = {int:pmsg}') . $labelQuery2 . '
ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
LIMIT ' . $_GET['start'] . ', ' . $maxPerPage : ''),
array(
'current_member' => $user_info['id'],
'is_deleted' => 0,
'sort' => $_GET['sort'],
'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
)
);
}
$pms = array();
$lastData = array();
$posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
$recipients = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (!isset($recipients[$row['id_pm']]))
{
if (isset($row['id_member_from']))
$posters[$row['id_pm']] = $row['id_member_from'];
$pms[$row['id_pm']] = $row['id_pm'];
$recipients[$row['id_pm']] = array(
'to' => array(),
'bcc' => array()
);
}
if ((empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData))) || empty($lastData) || (!empty($pmID) && $pmID == $row['id_pm']))
$lastData = array(
'id' => $row['id_pm'],
'head' => $row['id_pm_head'],
);
}
$smcFunc['db_free_result']($request);
if ($context['display_mode'] == 2 && !empty($pmID) && $pmID != $lastData['id'])
fatal_lang_error('no_access', false);
if (!empty($pms))
{
if (empty($pmID))
$context['current_pm'] = $lastData['id'];
if ($context['display_mode'] == 0)
$display_pms = $pms;
else
$display_pms = array($context['current_pm']);
if ($context['display_mode'] == 2)
{
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
FROM {db_prefix}personal_messages AS pm
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
WHERE pm.id_pm_head = {int:id_pm_head}
AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
ORDER BY pm.id_pm',
array(
'current_member' => $user_info['id'],
'id_pm_head' => $lastData['head'],
'not_deleted' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1)
continue;
elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1)
continue;
if (!isset($recipients[$row['id_pm']]))
$recipients[$row['id_pm']] = array(
'to' => array(),
'bcc' => array()
);
$display_pms[] = $row['id_pm'];
$posters[$row['id_pm']] = $row['id_member_from'];
}
$smcFunc['db_free_result']($request);
}
$all_pms = array_merge($pms, $display_pms);
$all_pms = array_unique($all_pms);
$request = $smcFunc['db_query']('', '
SELECT pmr.id_pm, mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc, pmr.in_inbox, pmr.is_read
FROM {db_prefix}pm_recipients AS pmr
LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
WHERE pmr.id_pm IN ({array_int:pm_list})',
array(
'pm_list' => $all_pms,
)
);
$context['message_labels'] = array();
$context['message_replied'] = array();
$context['message_unread'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($context['folder'] == 'sent' || empty($row['bcc']))
{
$recipients[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = empty($row['id_member_to']) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . '">' . $row['to_name'] . '</a>';
$context['folder'] == 'sent' && $context['display_mode'] != 2 ? $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2 : '';
}
if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
{
$context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
$context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
$request2 = $smcFunc['db_query']('', '
SELECT id_label
FROM {db_prefix}pm_labeled_messages
WHERE id_pm = {int:current_pm}',
array(
'current_pm' => $row['id_pm'],
)
);
while ($row2 = $smcFunc['db_fetch_assoc']($request2))
{
$l_id = $row2['id_label'];
if (isset($context['labels'][$l_id]))
$context['message_labels'][$row['id_pm']][$l_id] = array('id' => $l_id, 'name' => $context['labels'][$l_id]['name']);
}
$smcFunc['db_free_result']($request2);
if ($row['in_inbox'] == 1)
{
$context['message_labels'][$row['id_pm']][-1] = array('id' => -1, 'name' => $context['labels'][-1]['name']);
}
}
}
$smcFunc['db_free_result']($request);
if ($context['display_mode'] == 1)
{
foreach ($posters as $k => $v)
if (!in_array($k, $display_pms))
unset($posters[$k]);
}
loadMemberData($posters);
if ($context['display_mode'] != 0)
{
$orderBy = array();
foreach (array_reverse($pms) as $pm)
$orderBy[] = 'pm.id_pm = ' . $pm;
$subjects_request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.subject, COALESCE(pm.id_member_from, 0) AS id_member_from, pm.msgtime, COALESCE(mem.real_name, pm.from_name) AS from_name,
mem.id_member
FROM {db_prefix}personal_messages AS pm
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
WHERE pm.id_pm IN ({array_int:pm_list})
ORDER BY ' . implode(', ', $orderBy) . '
LIMIT {int:limit}',
array(
'pm_list' => $pms,
'limit' => count($pms),
)
);
}
$messages_request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($context['sort_by'] == 'name' ? '
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . '
LIMIT {int:limit}',
array(
'display_pms' => $display_pms,
'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
'limit' => count($display_pms),
'sort' => $_GET['sort'],
)
);
if ($context['display_mode'] == 2)
{
$context['conversation_buttons'] = array(
'delete' => array('text' => 'delete_conversation', 'image' => 'delete.png', 'url' => $scripturl . '?action=pm;sa=pmactions;pm_actions[' . $context['current_pm'] . ']=delete;conversation;f=' . $context['folder'] . ';start=' . $context['start'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'], 'custom' => 'data-confirm="' . $txt['remove_conversation'] . '"', 'class' => 'you_sure'),
);
call_integration_hook('integrate_conversation_buttons');
}
}
else
$messages_request = false;
$context['can_send_pm'] = allowedTo('pm_send');
$context['can_send_email'] = allowedTo('moderate_forum');
$context['sub_template'] = 'folder';
$context['page_title'] = $txt['pm_inbox'];
if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages']))
{
if ($context['display_mode'] == 0)
markMessages(null, $context['current_label_id']);
elseif (!empty($context['current_pm']))
markMessages($display_pms, $context['current_label_id']);
}
}
function prepareMessageContext($type = 'subject', $reset = false)
{
global $txt, $scripturl, $modSettings, $context, $messages_request, $memberContext, $recipients, $smcFunc;
global $user_info, $subjects_request;
static $counter = null;
if ($counter === null || $reset)
$counter = $context['start'];
static $temp_pm_selected = null;
if ($temp_pm_selected === null)
{
$temp_pm_selected = isset($_SESSION['pm_selected']) ? $_SESSION['pm_selected'] : array();
$_SESSION['pm_selected'] = array();
}
if ($context['display_mode'] != 0 && $subjects_request && $type == 'subject')
{
$subject = $smcFunc['db_fetch_assoc']($subjects_request);
if (!$subject)
{
$smcFunc['db_free_result']($subjects_request);
return false;
}
$subject['subject'] = $subject['subject'] == '' ? $txt['no_subject'] : $subject['subject'];
censorText($subject['subject']);
$output = array(
'id' => $subject['id_pm'],
'member' => array(
'id' => $subject['id_member_from'],
'name' => $subject['from_name'],
'link' => ($subject['id_member_from'] != 0) ? '<a href="' . $scripturl . '?action=profile;u=' . $subject['id_member_from'] . '">' . $subject['from_name'] . '</a>' : $subject['from_name'],
),
'recipients' => &$recipients[$subject['id_pm']],
'subject' => $subject['subject'],
'time' => timeformat($subject['msgtime']),
'timestamp' => forum_time(true, $subject['msgtime']),
'number_recipients' => count($recipients[$subject['id_pm']]['to']),
'labels' => &$context['message_labels'][$subject['id_pm']],
'fully_labeled' => count(isset($context['message_labels'][$subject['id_pm']]) ? $context['message_labels'][$subject['id_pm']] : array()) == count($context['labels']),
'is_replied_to' => &$context['message_replied'][$subject['id_pm']],
'is_unread' => &$context['message_unread'][$subject['id_pm']],
'is_selected' => !empty($temp_pm_selected) && in_array($subject['id_pm'], $temp_pm_selected),
);
return $output;
}
if ($messages_request == false)
return false;
if ($reset == true)
return @$smcFunc['db_data_seek']($messages_request, 0);
$message = $smcFunc['db_fetch_assoc']($messages_request);
if (!$message)
{
if ($type != 'subject')
$smcFunc['db_free_result']($messages_request);
return false;
}
$message['subject'] = $message['subject'] == '' ? $txt['no_subject'] : $message['subject'];
if (!loadMemberContext($message['id_member_from'], true))
{
$memberContext[$message['id_member_from']]['name'] = $message['from_name'];
$memberContext[$message['id_member_from']]['id'] = 0;
$memberContext[$message['id_member_from']]['group'] = $message['from_name'] == $context['forum_name_html_safe'] ? '' : $txt['guest_title'];
$memberContext[$message['id_member_from']]['link'] = $message['from_name'];
$memberContext[$message['id_member_from']]['email'] = '';
$memberContext[$message['id_member_from']]['show_email'] = false;
$memberContext[$message['id_member_from']]['is_guest'] = true;
}
else
{
$memberContext[$message['id_member_from']]['can_view_profile'] = allowedTo('profile_view') || ($message['id_member_from'] == $user_info['id'] && !$user_info['is_guest']);
$memberContext[$message['id_member_from']]['can_see_warning'] = !isset($context['disabled_fields']['warning_status']) && $memberContext[$message['id_member_from']]['warning_status'] && ($context['user']['can_mod'] || (!empty($modSettings['warning_show']) && ($modSettings['warning_show'] > 1 || $message['id_member_from'] == $user_info['id'])));
$memberContext[$message['id_member_from']]['show_email'] |= $message['id_member_from'] == $user_info['id'];
}
$memberContext[$message['id_member_from']]['show_profile_buttons'] = $modSettings['show_profile_buttons'] && (!empty($memberContext[$message['id_member_from']]['can_view_profile']) || (!empty($memberContext[$message['id_member_from']]['website']['url']) && !isset($context['disabled_fields']['website'])) || $memberContext[$message['id_member_from']]['show_email'] || $context['can_send_pm']);
censorText($message['body']);
censorText($message['subject']);
$message['body'] = parse_bbc($message['body'], true, 'pm' . $message['id_pm']);
$output = array(
'id' => $message['id_pm'],
'member' => &$memberContext[$message['id_member_from']],
'subject' => $message['subject'],
'time' => timeformat($message['msgtime']),
'timestamp' => forum_time(true, $message['msgtime']),
'counter' => $counter,
'body' => $message['body'],
'recipients' => &$recipients[$message['id_pm']],
'number_recipients' => count($recipients[$message['id_pm']]['to']),
'labels' => &$context['message_labels'][$message['id_pm']],
'fully_labeled' => count(isset($context['message_labels'][$message['id_pm']]) ? $context['message_labels'][$message['id_pm']] : array()) == count($context['labels']),
'is_replied_to' => &$context['message_replied'][$message['id_pm']],
'is_unread' => &$context['message_unread'][$message['id_pm']],
'is_selected' => !empty($temp_pm_selected) && in_array($message['id_pm'], $temp_pm_selected),
'is_message_author' => $message['id_member_from'] == $user_info['id'],
'can_report' => !empty($modSettings['enableReportPM']),
'can_see_ip' => allowedTo('moderate_forum') || ($message['id_member_from'] == $user_info['id'] && !empty($user_info['id'])),
);
$counter++;
if (!empty($memberContext[$message['id_member_from']]['custom_fields']))
foreach ($memberContext[$message['id_member_from']]['custom_fields'] as $custom)
$output['custom_fields'][$context['cust_profile_fields_placement'][$custom['placement']]][] = $custom;
call_integration_hook('integrate_prepare_pm_context', array(&$output, &$message, $counter));
$output['quickbuttons'] = array(
'reply_to_all' => array(
'label' => $txt['reply_to_all'],
'href' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $output['id'] . ($output['member']['id'] != $user_info['id'] ? ';quote' : '') . ';u=all',
'icon' => 'reply_all_button',
'show' => $context['can_send_pm'] && !$output['member']['is_guest'] && ($output['number_recipients'] > 1 || $output['member']['id'] == $user_info['id']),
),
'reply' => array(
'label' => $txt['reply'],
'href' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $output['id'] . ';u=' . $output['member']['id'],
'icon' => 'reply_button',
'show' => $context['can_send_pm'] && !$output['member']['is_guest'] && $output['member']['id'] != $user_info['id'],
),
'quote' => array(
'label' => $txt['quote_action'],
'href' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $output['id'] . ';quote' . ($output['number_recipients'] > 1 || $output['member']['id'] == $user_info['id'] ? ';u=all' : (!$output['member']['is_guest'] ? ';u=' . $output['member']['id'] : '')),
'icon' => 'quote',
'show' => $context['can_send_pm'],
),
'delete' => array(
'label' => $txt['delete'],
'href' => $scripturl . '?action=pm;sa=pmactions;pm_actions%5b' . $output['id'] . '%5D=delete;f=' . $context['folder'] . ';start=' . $context['start'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'],
'javascript' => 'data-confirm="' . JavaScriptEscape($txt['remove_message_question']) . '" class="you_sure"',
'icon' => 'remove_button',
),
'more' => array(
'report' => array(
'label' => $txt['pm_report_to_admin'],
'href' => $scripturl . '?action=pm;sa=report;l=' . $context['current_label_id'] . ';pmsg=' . $output['id'],
'icon' => 'error',
'show' => $output['can_report']
),
),
'quickmod' => array(
'content' => '<input type="checkbox" name="pms[]" id="deletedisplay' . $output['id'] . '" value="' . $output['id'] . '" onclick="document.getElementById(\'deletelisting' . $output['id'] . '\').checked = this.checked;">',
'show' => empty($context['display_mode'])
)
);
return $output;
}
function MessageSearch()
{
global $context, $txt, $scripturl, $smcFunc;
if (isset($_REQUEST['params']))
{
$temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
$context['search_params'] = array();
foreach ($temp_params as $i => $data)
{
@list ($k, $v) = explode('|\'|', $data);
$context['search_params'][$k] = $v;
}
}
if (isset($_REQUEST['search']))
$context['search_params']['search'] = un_htmlspecialchars($_REQUEST['search']);
if (isset($context['search_params']['search']))
$context['search_params']['search'] = $smcFunc['htmlspecialchars']($context['search_params']['search']);
if (isset($context['search_params']['userspec']))
$context['search_params']['userspec'] = $smcFunc['htmlspecialchars']($context['search_params']['userspec']);
if (!empty($context['search_params']['searchtype']))
$context['search_params']['searchtype'] = 2;
if (!empty($context['search_params']['minage']))
$context['search_params']['minage'] = (int) $context['search_params']['minage'];
if (!empty($context['search_params']['maxage']))
$context['search_params']['maxage'] = (int) $context['search_params']['maxage'];
$context['search_params']['subject_only'] = !empty($context['search_params']['subject_only']);
$context['search_params']['show_complete'] = !empty($context['search_params']['show_complete']);
$context['search_labels'] = array();
$searchedLabels = isset($context['search_params']['labels']) && $context['search_params']['labels'] != '' ? explode(',', $context['search_params']['labels']) : array();
foreach ($context['labels'] as $label)
{
$context['search_labels'][] = array(
'id' => $label['id'],
'name' => $label['name'],
'checked' => !empty($searchedLabels) ? in_array($label['id'], $searchedLabels) : true,
);
}
$context['check_all'] = empty($searchedLabels) || count($context['search_labels']) == count($searchedLabels);
if (!empty($context['search_errors']))
{
loadLanguage('Errors');
$context['search_errors']['messages'] = array();
foreach ($context['search_errors'] as $search_error => $dummy)
{
if ($search_error == 'messages')
continue;
$context['search_errors']['messages'][] = $txt['error_' . $search_error];
}
}
$context['page_title'] = $txt['pm_search_title'];
$context['sub_template'] = 'search';
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=search',
'name' => $txt['pm_search_bar_title'],
);
}
function MessageSearch2()
{
global $scripturl, $modSettings, $user_info, $context, $txt;
global $memberContext, $smcFunc;
if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search'])
fatal_lang_error('loadavg_search_disabled', false);
$context['folder'] = 'inbox';
$context['can_send_pm'] = allowedTo('pm_send');
$maxMembersToSearch = 500;
$search_params = array();
if (isset($_REQUEST['params']))
{
$temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
foreach ($temp_params as $i => $data)
{
@list ($k, $v) = explode('|\'|', $data);
$search_params[$k] = $v;
}
}
$context['start'] = isset($_GET['start']) ? (int) $_GET['start'] : 0;
if (!isset($search_params['advanced']))
$search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1;
if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2))
$search_params['searchtype'] = 2;
if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0))
$search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage'];
if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] != 9999))
$search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage'];
$search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']);
$search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']);
if (!empty($search_params['user_spec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*'))
$search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec'];
$searchq_parameters = array();
if (empty($search_params['userspec']))
$userQuery = '';
else
{
$userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('"' => '"'));
$userString = strtr($userString, array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_'));
preg_match_all('~"([^"]+)"~', $userString, $matches);
$possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString)));
for ($k = 0, $n = count($possible_users); $k < $n; $k++)
{
$possible_users[$k] = trim($possible_users[$k]);
if (strlen($possible_users[$k]) == 0)
unset($possible_users[$k]);
}
if (!empty($possible_users))
{
$where_params = array();
$where_clause = array();
foreach ($possible_users as $k => $v)
{
$where_params['name_' . $k] = $v;
$where_clause[] = '{raw:real_name} LIKE {string:name_' . $k . '}';
if (!isset($where_params['real_name']))
$where_params['real_name'] = $smcFunc['db_case_sensitive'] ? 'LOWER(real_name)' : 'real_name';
}
$request = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}members
WHERE ' . implode(' OR ', $where_clause),
$where_params
);
if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch)
$userQuery = '';
elseif ($smcFunc['db_num_rows']($request) == 0)
{
$userQuery = 'AND pm.id_member_from = 0 AND ({raw:pm_from_name} LIKE {raw:guest_user_name_implode})';
$searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR ' . ($smcFunc['db_case_sensitive'] ? 'LOWER(pm.from_name)' : 'pm.from_name') . ' LIKE \'', $possible_users) . '\'';
$searchq_parameters['pm_from_name'] = $smcFunc['db_case_sensitive'] ? 'LOWER(pm.from_name)' : 'pm.from_name';
}
else
{
$memberlist = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$memberlist[] = $row['id_member'];
$userQuery = 'AND (pm.id_member_from IN ({array_int:member_list}) OR (pm.id_member_from = 0 AND ({raw:pm_from_name} LIKE {raw:guest_user_name_implode})))';
$searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR ' . ($smcFunc['db_case_sensitive'] ? 'LOWER(pm.from_name)' : 'pm.from_name') . ' LIKE \'', $possible_users) . '\'';
$searchq_parameters['member_list'] = $memberlist;
$searchq_parameters['pm_from_name'] = $smcFunc['db_case_sensitive'] ? 'LOWER(pm.from_name)' : 'pm.from_name';
}
$smcFunc['db_free_result']($request);
}
else
$userQuery = '';
}
$sort_columns = array(
'pm.id_pm',
);
if (empty($search_params['sort']) && !empty($_REQUEST['sort']))
list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, '');
$search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'pm.id_pm';
$search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc';
$context['search_in'] = array();
$labelQuery = '';
$labelJoin = '';
if ($context['folder'] == 'inbox' && !empty($search_params['advanced']) && $context['currently_using_labels'])
{
if (isset($search_params['labels']))
$_REQUEST['searchlabel'] = explode(',', $search_params['labels']);
if (!empty($_REQUEST['searchlabel']) && is_array($_REQUEST['searchlabel']))
{
foreach ($_REQUEST['searchlabel'] as $key => $id)
$_REQUEST['searchlabel'][$key] = (int) $id;
}
else
$_REQUEST['searchlabel'] = array();
$search_params['labels'] = implode(',', $_REQUEST['searchlabel']);
if (empty($_REQUEST['searchlabel']))
$context['search_errors']['no_labels_selected'] = true;
elseif (count($_REQUEST['searchlabel']) != count($context['labels']))
{
if (in_array(-1, $_REQUEST['searchlabel']))
{
$context['search_in'][] = $context['labels'][-1]['name'];
$labelQuery = ' AND pmr.in_inbox = {int:in_inbox}';
$searchq_parameters['in_inbox'] = 1;
$temp = array_diff($_REQUEST['searchlabel'], array(-1));
$_REQUEST['searchlabel'] = $temp;
}
if (!empty($_REQUEST['searchlabel']))
{
if ($labelQuery == '')
{
$labelQuery = ' AND pml.id_label IN ({array_int:labels})';
$labelJoin = ' INNER JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_pm = pmr.id_pm)';
}
else
{
$labelQuery = ' AND (' . substr($labelQuery, 5) . ' OR pml.id_label IN ({array_int:labels}))';
$labelJoin = ' LEFT JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_pm = pmr.id_pm)';
}
$searchq_parameters['labels'] = $_REQUEST['searchlabel'];
foreach ($_REQUEST['searchlabel'] as $label_key)
$context['search_in'][] = $context['labels'][$label_key]['name'];
}
}
}
if (empty($context['search_in']))
$context['search_in'][] = $context['folder'];
$search_params['search'] = !empty($search_params['search']) ? $search_params['search'] : (isset($_REQUEST['search']) ? $_REQUEST['search'] : '');
if (!isset($search_params['search']) || $search_params['search'] == '')
$context['search_errors']['invalid_search_string'] = true;
preg_match_all('~(?:^|\s)([-]?)"([^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), $search_params['search'], $matches, PREG_PATTERN_ORDER);
$searchArray = $matches[2];
$tempSearch = explode(' ', preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']));
$excludedWords = array();
foreach ($matches[1] as $index => $word)
if ($word == '-')
{
$word = $smcFunc['strtolower'](trim($searchArray[$index]));
if (strlen($word) > 0)
$excludedWords[] = $word;
unset($searchArray[$index]);
}
foreach ($tempSearch as $index => $word)
{
if (strpos(trim($word), '-') === 0)
{
$word = substr($smcFunc['strtolower']($word), 1);
if (strlen($word) > 0)
$excludedWords[] = $word;
unset($tempSearch[$index]);
}
}
$searchArray = array_merge($searchArray, $tempSearch);
foreach ($searchArray as $index => $value)
{
$searchArray[$index] = $smcFunc['strtolower'](trim($value));
if ($searchArray[$index] == '')
unset($searchArray[$index]);
else
{
$searchArray[$index] = $smcFunc['htmlspecialchars']($searchArray[$index]);
}
}
$searchArray = array_unique($searchArray);
$context['mark'] = array();
foreach ($searchArray as $word)
$context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>';
$searchWords = array_merge($searchArray, $excludedWords);
if (empty($searchArray))
$context['search_errors']['invalid_search_string'] = true;
$context['search_params'] = $search_params;
if (isset($context['search_params']['search']))
$context['search_params']['search'] = $smcFunc['htmlspecialchars']($context['search_params']['search']);
if (isset($context['search_params']['userspec']))
$context['search_params']['userspec'] = $smcFunc['htmlspecialchars']($context['search_params']['userspec']);
$context['params'] = array();
foreach ($search_params as $k => $v)
$context['params'][] = $k . '|\'|' . $v;
$context['params'] = base64_encode(implode('|"|', $context['params']));
$andQueryParts = array();
foreach ($searchWords as $index => $word)
{
if ($word == '')
continue;
if ($search_params['subject_only'])
$andQueryParts[] = 'pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '}';
else
$andQueryParts[] = '(pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '} ' . (in_array($word, $excludedWords) ? 'AND pm.body NOT' : 'OR pm.body') . ' LIKE {string:search_' . $index . '})';
$searchq_parameters['search_' . $index] = '%' . strtr($word, array('_' => '\\_', '%' => '\\%')) . '%';
}
$searchQuery = ' 1=1';
if (!empty($andQueryParts))
$searchQuery = implode(!empty($search_params['searchtype']) && $search_params['searchtype'] == 2 ? ' OR ' : ' AND ', $andQueryParts);
$timeQuery = '';
if (!empty($search_params['minage']))
$timeQuery .= ' AND pm.msgtime < ' . (time() - $search_params['minage'] * 86400);
if (!empty($search_params['maxage']))
$timeQuery .= ' AND pm.msgtime > ' . (time() - $search_params['maxage'] * 86400);
if (!empty($context['search_errors']))
{
$_REQUEST['params'] = $context['params'];
return MessageSearch();
}
$request = $smcFunc['db_query']('', '
SELECT COUNT(*)
FROM {db_prefix}pm_recipients AS pmr
INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
' . $labelJoin . '
WHERE ' . ($context['folder'] == 'inbox' ? '
pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}' : '
pm.id_member_from = {int:current_member}
AND pm.deleted_by_sender = {int:not_deleted}') . '
' . $userQuery . $labelQuery . $timeQuery . '
AND (' . $searchQuery . ')',
array_merge($searchq_parameters, array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
))
);
list ($numResults) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
FROM {db_prefix}pm_recipients AS pmr
INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
' . $labelJoin . '
WHERE ' . ($context['folder'] == 'inbox' ? '
pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}' : '
pm.id_member_from = {int:current_member}
AND pm.deleted_by_sender = {int:not_deleted}') . '
' . $userQuery . $labelQuery . $timeQuery . '
AND (' . $searchQuery . ')
ORDER BY {raw:sort} {raw:sort_dir}
LIMIT {int:start}, {int:max}',
array_merge($searchq_parameters, array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
'sort' => $search_params['sort'],
'sort_dir' => $search_params['sort_dir'],
'start' => $context['start'],
'max' => $modSettings['search_results_per_page'],
))
);
$foundMessages = array();
$posters = array();
$head_pms = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$foundMessages[] = $row['id_pm'];
$posters[] = $row['id_member_from'];
$head_pms[$row['id_pm']] = $row['id_pm_head'];
}
$smcFunc['db_free_result']($request);
if ($context['display_mode'] == 2 && !empty($head_pms))
{
$request = $smcFunc['db_query']('', '
SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
FROM {db_prefix}personal_messages AS pm
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
WHERE pm.id_pm_head IN ({array_int:head_pms})
AND pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}
GROUP BY pm.id_pm_head
LIMIT {int:limit}',
array(
'head_pms' => array_unique($head_pms),
'current_member' => $user_info['id'],
'not_deleted' => 0,
'limit' => count($head_pms),
)
);
$real_pm_ids = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$real_pm_ids[$row['id_pm_head']] = $row['id_pm'];
$smcFunc['db_free_result']($request);
}
loadMemberData($posters);
$context['page_index'] = constructPageIndex($scripturl . '?action=pm;sa=search2;params=' . $context['params'], $_GET['start'], $numResults, $modSettings['search_results_per_page'], false);
$context['num_results'] = $numResults;
$context['message_labels'] = array();
$context['message_replied'] = array();
$context['personal_messages'] = array();
if (!empty($foundMessages))
{
$request = $smcFunc['db_query']('', '
SELECT
pmr.id_pm, mem_to.id_member AS id_member_to, mem_to.real_name AS to_name,
pmr.bcc, pmr.in_inbox, pmr.is_read
FROM {db_prefix}pm_recipients AS pmr
LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
WHERE pmr.id_pm IN ({array_int:message_list})',
array(
'message_list' => $foundMessages,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($context['folder'] == 'sent' || empty($row['bcc']))
$recipients[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = empty($row['id_member_to']) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . '">' . $row['to_name'] . '</a>';
if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
{
$context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
$row['labels'] = '';
$request2 = $smcFunc['db_query']('', '
SELECT id_label
FROM {db_prefix}pm_labeled_messages
WHERE id_pm = {int:current_pm}',
array(
'current_pm' => $row['id_pm'],
)
);
while ($row2 = $smcFunc['db_fetch_assoc']($request2))
{
$l_id = $row2['id_label'];
if (isset($context['labels'][$l_id]))
$context['message_labels'][$row['id_pm']][$l_id] = array('id' => $l_id, 'name' => $context['labels'][$l_id]['name']);
if (!isset($context['first_label'][$row['id_pm']]) && $row['in_inbox'] != 1)
$context['first_label'][$row['id_pm']] = $l_id;
}
$smcFunc['db_free_result']($request2);
if ($row['in_inbox'] == 1)
{
$context['message_labels'][$row['id_pm']][-1] = array('id' => -1, 'name' => $context['labels'][-1]['name']);
}
$row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
}
}
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
FROM {db_prefix}personal_messages AS pm
WHERE pm.id_pm IN ({array_int:message_list})
ORDER BY {raw:sort} {raw:sort_dir}
LIMIT {int:limit}',
array(
'message_list' => $foundMessages,
'limit' => count($foundMessages),
'sort' => $search_params['sort'],
'sort_dir' => $search_params['sort_dir'],
)
);
$counter = 0;
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$row['subject'] = $row['subject'] == '' ? $txt['no_subject'] : $row['subject'];
if (!loadMemberContext($row['id_member_from'], true))
{
$memberContext[$row['id_member_from']]['name'] = $row['from_name'];
$memberContext[$row['id_member_from']]['id'] = 0;
$memberContext[$row['id_member_from']]['group'] = $txt['guest_title'];
$memberContext[$row['id_member_from']]['link'] = $row['from_name'];
$memberContext[$row['id_member_from']]['email'] = '';
$memberContext[$row['id_member_from']]['is_guest'] = true;
}
else
{
$memberContext[$row['id_member_from']]['can_view_profile'] = allowedTo('profile_view') || ($row['id_member_from'] == $user_info['id'] && !$user_info['is_guest']);
$memberContext[$row['id_member_from']]['can_see_warning'] = !isset($context['disabled_fields']['warning_status']) && $memberContext[$row['id_member_from']]['warning_status'] && ($context['user']['can_mod'] || (!empty($modSettings['warning_show']) && ($modSettings['warning_show'] > 1 || $row['id_member_from'] == $user_info['id'])));
$memberContext[$row['id_member_from']]['show_email'] |= $row['id_member_from'] == $user_info['id'];
}
$memberContext[$row['id_member_from']]['show_profile_buttons'] = $modSettings['show_profile_buttons'] && (!empty($memberContext[$row['id_member_from']]['can_view_profile']) || (!empty($memberContext[$row['id_member_from']]['website']['url']) && !isset($context['disabled_fields']['website'])) || $memberContext[$row['id_member_from']]['show_email'] || $context['can_send_pm']);
censorText($row['body']);
censorText($row['subject']);
$row['body'] = parse_bbc($row['body'], true, 'pm' . $row['id_pm']);
$href = $scripturl . '?action=pm;f=' . $context['folder'] . (isset($context['first_label'][$row['id_pm']]) ? ';l=' . $context['first_label'][$row['id_pm']] : '') . ';pmid=' . ($context['display_mode'] == 2 && isset($real_pm_ids[$head_pms[$row['id_pm']]]) ? $real_pm_ids[$head_pms[$row['id_pm']]] : $row['id_pm']) . '#msg' . $row['id_pm'];
$context['personal_messages'][] = array(
'id' => $row['id_pm'],
'member' => &$memberContext[$row['id_member_from']],
'subject' => $row['subject'],
'body' => $row['body'],
'time' => timeformat($row['msgtime']),
'recipients' => &$recipients[$row['id_pm']],
'labels' => &$context['message_labels'][$row['id_pm']],
'fully_labeled' => count($context['message_labels'][$row['id_pm']]) == count($context['labels']),
'is_replied_to' => &$context['message_replied'][$row['id_pm']],
'href' => $href,
'link' => '<a href="' . $href . '">' . $row['subject'] . '</a>',
'counter' => ++$counter,
'can_see_ip' => allowedTo('moderate_forum') || ($row['id_member_from'] == $user_info['id'] && !empty($user_info['id'])),
'quickbuttons' => array(
'reply_to_all' => array(
'label' => $txt['reply_to_all'],
'href' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $row['id_pm'] . ($row['id_member_from'] != $user_info['id'] ? ';quote' : '') . ';u=all',
'icon' => 'reply_all_button',
'show' => $context['can_send_pm'] && !$memberContext[$row['id_member_from']]['is_guest'] && (count($recipients[$row['id_pm']]['to']) > 1 || $row['id_member_from'] == $user_info['id']),
),
'reply' => array(
'label' => $txt['reply'],
'href' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $row['id_pm'] . ';u=' . $row['id_member_from'],
'icon' => 'reply_button',
'show' => $context['can_send_pm'] && !$memberContext[$row['id_member_from']]['is_guest'] && $row['id_member_from'] != $user_info['id'],
),
'quote' => array(
'label' => $txt['quote_action'],
'href' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $row['id_pm'] . ';quote' . (count($recipients[$row['id_pm']]['to']) > 1 || $row['id_member_from'] == $user_info['id'] ? ';u=all' : (!$memberContext[$row['id_member_from']]['is_guest'] ? ';u=' . $row['id_member_from'] : '')),
'icon' => 'quote',
'show' => $context['can_send_pm'],
),
'delete' => array(
'label' => $txt['delete'],
'href' => $scripturl . '?action=pm;sa=pmactions;pm_actions%5b' . $row['id_pm'] . '%5D=delete;f=' . $context['folder'] . ';start=' . $context['start'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'],
'javascript' => 'data-confirm="' . JavaScriptEscape($txt['remove_message_question']) . '" class="you_sure"',
'icon' => 'remove_button',
),
'more' => array(
'report' => array(
'label' => $txt['pm_report_to_admin'],
'href' => $scripturl . '?action=pm;sa=report;l=' . $context['current_label_id'] . ';pmsg=' . $row['id_pm'],
'icon' => 'error',
'show' => !empty($modSettings['enableReportPM']),
),
),
)
);
}
$smcFunc['db_free_result']($request);
}
call_integration_hook('integrate_search_pm_context');
$context['page_title'] = $txt['pm_search_title'];
$context['sub_template'] = 'search_results';
$context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'search';
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=search',
'name' => $txt['pm_search_bar_title'],
);
}
function MessagePost()
{
global $txt, $sourcedir, $scripturl, $modSettings;
global $context, $smcFunc, $language, $user_info;
isAllowedTo('pm_send');
loadLanguage('PersonalMessage');
loadTemplate('PersonalMessage');
loadJavaScriptFile('PersonalMessage.js', array('defer' => false, 'minimize' => true), 'smf_pms');
loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
$context['sub_template'] = 'send';
list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
$context['page_title'] = $txt['send_message'];
$context['reply'] = isset($_REQUEST['pmsg']) || isset($_REQUEST['quote']);
if (!empty($modSettings['pm_posts_per_hour']) && !allowedTo(array('admin_forum', 'moderate_forum', 'send_mail')) && $user_info['mod_cache']['bq'] == '0=1' && $user_info['mod_cache']['gq'] == '0=1')
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(pr.id_pm) AS post_count
FROM {db_prefix}personal_messages AS pm
INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
WHERE pm.id_member_from = {int:current_member}
AND pm.msgtime > {int:msgtime}',
array(
'current_member' => $user_info['id'],
'msgtime' => time() - 3600,
)
);
list ($postCount) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
}
if (!empty($_REQUEST['pmsg']))
{
$pmsg = (int) $_REQUEST['pmsg'];
if (!isAccessiblePM($pmsg))
fatal_lang_error('no_access', false);
$request = $smcFunc['db_query']('', '
SELECT
id_pm
FROM {db_prefix}pm_recipients
WHERE id_pm = {int:id_pm}
AND id_member = {int:current_member}
LIMIT 1',
array(
'current_member' => $user_info['id'],
'id_pm' => $pmsg,
)
);
$isReceived = $smcFunc['db_num_rows']($request) != 0;
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']('', '
SELECT
pm.id_pm, CASE WHEN pm.id_pm_head = {int:id_pm_head_empty} THEN pm.id_pm ELSE pm.id_pm_head END AS pm_head,
pm.body, pm.subject, pm.msgtime, mem.member_name, COALESCE(mem.id_member, 0) AS id_member,
COALESCE(mem.real_name, pm.from_name) AS real_name
FROM {db_prefix}personal_messages AS pm' . (!$isReceived ? '' : '
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:id_pm})') . '
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
WHERE pm.id_pm = {int:id_pm}' . (!$isReceived ? '
AND pm.id_member_from = {int:current_member}' : '
AND pmr.id_member = {int:current_member}') . '
LIMIT 1',
array(
'current_member' => $user_info['id'],
'id_pm_head_empty' => 0,
'id_pm' => $pmsg,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('pm_not_yours', false);
$row_quoted = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
censorText($row_quoted['subject']);
censorText($row_quoted['body']);
if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
{
if ($language === $user_info['language'])
$context['response_prefix'] = $txt['response_prefix'];
else
{
loadLanguage('index', $language, false);
$context['response_prefix'] = $txt['response_prefix'];
loadLanguage('index');
}
cache_put_data('response_prefix', $context['response_prefix'], 600);
}
$form_subject = $row_quoted['subject'];
if ($context['reply'] && trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
$form_subject = $context['response_prefix'] . $form_subject;
if (isset($_REQUEST['quote']))
{
$form_message = preg_replace('~<br ?/?' . '>~i', "\n", $row_quoted['body']);
if (!empty($modSettings['removeNestedQuotes']))
$form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
if (empty($row_quoted['id_member']))
$form_message = '[quote author="' . $row_quoted['real_name'] . '"]' . "\n" . $form_message . "\n" . '[/quote]';
else
$form_message = '[quote author=' . $row_quoted['real_name'] . ' link=action=profile;u=' . $row_quoted['id_member'] . ' date=' . $row_quoted['msgtime'] . ']' . "\n" . $form_message . "\n" . '[/quote]';
}
else
$form_message = '';
$row_quoted['body'] = parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']);
$context['quoted_message'] = array(
'id' => $row_quoted['id_pm'],
'pm_head' => $row_quoted['pm_head'],
'member' => array(
'name' => $row_quoted['real_name'],
'username' => $row_quoted['member_name'],
'id' => $row_quoted['id_member'],
'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
'link' => !empty($row_quoted['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row_quoted['id_member'] . '">' . $row_quoted['real_name'] . '</a>' : $row_quoted['real_name'],
),
'subject' => $row_quoted['subject'],
'time' => timeformat($row_quoted['msgtime']),
'timestamp' => forum_time(true, $row_quoted['msgtime']),
'body' => $row_quoted['body']
);
}
else
{
$context['quoted_message'] = false;
$form_subject = '';
$form_message = '';
}
$context['recipients'] = array(
'to' => array(),
'bcc' => array(),
);
if (isset($_REQUEST['u']))
{
if ($_REQUEST['u'] == 'all' && isset($row_quoted))
{
if ($row_quoted['id_member'] != $user_info['id'])
$context['recipients']['to'][] = array(
'id' => $row_quoted['id_member'],
'name' => $smcFunc['htmlspecialchars']($row_quoted['real_name']),
);
$request = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name
FROM {db_prefix}pm_recipients AS pmr
INNER JOIN {db_prefix}members AS mem ON (mem.id_member = pmr.id_member)
WHERE pmr.id_pm = {int:id_pm}
AND pmr.id_member != {int:current_member}
AND pmr.bcc = {int:not_bcc}',
array(
'current_member' => $user_info['id'],
'id_pm' => $pmsg,
'not_bcc' => 0,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$context['recipients']['to'][] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
);
$smcFunc['db_free_result']($request);
}
else
{
$_REQUEST['u'] = explode(',', $_REQUEST['u']);
foreach ($_REQUEST['u'] as $key => $uID)
$_REQUEST['u'][$key] = (int) $uID;
$_REQUEST['u'] = array_unique($_REQUEST['u']);
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name
FROM {db_prefix}members
WHERE id_member IN ({array_int:member_list})
LIMIT {int:limit}',
array(
'member_list' => $_REQUEST['u'],
'limit' => count($_REQUEST['u']),
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$context['recipients']['to'][] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
);
$smcFunc['db_free_result']($request);
}
$names = array();
foreach ($context['recipients']['to'] as $to)
$names[] = $to['name'];
$context['to_value'] = empty($names) ? '' : '"' . implode('", "', $names) . '"';
}
else
$context['to_value'] = '';
$context['subject'] = $form_subject;
$context['message'] = str_replace(array('"', '<', '>', ' '), array('"', '<', '>', ' '), $form_message);
$context['post_error'] = array();
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=send',
'name' => $txt['new_message']
);
if (!empty($context['drafts_pm_save']))
{
require_once($sourcedir . '/Drafts.php');
$pm_seed = isset($_REQUEST['pmsg']) ? $_REQUEST['pmsg'] : (isset($_REQUEST['quote']) ? $_REQUEST['quote'] : 0);
ShowDrafts($user_info['id'], $pm_seed, 1);
}
require_once($sourcedir . '/Subs-Editor.php');
$editorOptions = array(
'id' => 'message',
'value' => $context['message'],
'height' => '175px',
'width' => '100%',
'labels' => array(
'post_button' => $txt['send_message'],
),
'preview_type' => 2,
'required' => true,
);
create_control_richedit($editorOptions);
$context['post_box_name'] = $editorOptions['id'];
$context['bcc_value'] = '';
$context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
if ($context['require_verification'])
{
$verificationOptions = array(
'id' => 'pm',
);
$context['require_verification'] = create_control_verification($verificationOptions);
$context['visual_verification_id'] = $verificationOptions['id'];
}
call_integration_hook('integrate_pm_post');
checkSubmitOnce('register');
}
function MessageDrafts()
{
global $sourcedir, $user_info;
$memberResult = loadMemberData($user_info['id'], false);
if (!$memberResult)
fatal_lang_error('not_a_user', false);
list ($memID) = $memberResult;
require_once($sourcedir . '/Drafts.php');
showPMDrafts($memID);
}
function messagePostError($error_types, $named_recipients, $recipient_ids = array())
{
global $txt, $context, $scripturl, $modSettings;
global $smcFunc, $user_info, $sourcedir;
if (!isset($_REQUEST['xml']))
{
$context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'send';
$context['sub_template'] = 'send';
loadJavaScriptFile('PersonalMessage.js', array('defer' => false, 'minimize' => true), 'smf_pms');
loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
}
else
$context['sub_template'] = 'pm';
$context['page_title'] = $txt['send_message'];
$context['recipients'] = array(
'to' => array(),
'bcc' => array(),
);
if (!empty($recipient_ids['to']) || !empty($recipient_ids['bcc']))
{
$allRecipients = array_merge($recipient_ids['to'], $recipient_ids['bcc']);
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name
FROM {db_prefix}members
WHERE id_member IN ({array_int:member_list})',
array(
'member_list' => $allRecipients,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$recipientType = in_array($row['id_member'], $recipient_ids['bcc']) ? 'bcc' : 'to';
$context['recipients'][$recipientType][] = array(
'id' => $row['id_member'],
'name' => $row['real_name'],
);
}
$smcFunc['db_free_result']($request);
}
$context['subject'] = isset($_REQUEST['subject']) ? $smcFunc['htmlspecialchars']($_REQUEST['subject']) : '';
$context['message'] = isset($_REQUEST['message']) ? str_replace(array(' '), array(' '), $smcFunc['htmlspecialchars']($_REQUEST['message'])) : '';
$context['reply'] = !empty($_REQUEST['replied_to']);
if ($context['reply'])
{
$_REQUEST['replied_to'] = (int) $_REQUEST['replied_to'];
$request = $smcFunc['db_query']('', '
SELECT
pm.id_pm, CASE WHEN pm.id_pm_head = {int:no_id_pm_head} THEN pm.id_pm ELSE pm.id_pm_head END AS pm_head,
pm.body, pm.subject, pm.msgtime, mem.member_name, COALESCE(mem.id_member, 0) AS id_member,
COALESCE(mem.real_name, pm.from_name) AS real_name
FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' : '
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:replied_to})') . '
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
WHERE pm.id_pm = {int:replied_to}' . ($context['folder'] == 'sent' ? '
AND pm.id_member_from = {int:current_member}' : '
AND pmr.id_member = {int:current_member}') . '
LIMIT 1',
array(
'current_member' => $user_info['id'],
'no_id_pm_head' => 0,
'replied_to' => $_REQUEST['replied_to'],
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
if (!isset($_REQUEST['xml']))
fatal_lang_error('pm_not_yours', false);
else
$error_types[] = 'pm_not_yours';
}
$row_quoted = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
censorText($row_quoted['subject']);
censorText($row_quoted['body']);
$context['quoted_message'] = array(
'id' => $row_quoted['id_pm'],
'pm_head' => $row_quoted['pm_head'],
'member' => array(
'name' => $row_quoted['real_name'],
'username' => $row_quoted['member_name'],
'id' => $row_quoted['id_member'],
'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
'link' => !empty($row_quoted['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row_quoted['id_member'] . '">' . $row_quoted['real_name'] . '</a>' : $row_quoted['real_name'],
),
'subject' => $row_quoted['subject'],
'time' => timeformat($row_quoted['msgtime']),
'timestamp' => forum_time(true, $row_quoted['msgtime']),
'body' => parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']),
);
}
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=send',
'name' => $txt['new_message']
);
loadLanguage('Errors');
$context['error_type'] = 'minor';
$context['post_error'] = array(
'messages' => array(),
'error_type' => '',
);
foreach ($error_types as $error_type)
{
$context['post_error'][$error_type] = true;
if (isset($txt['error_' . $error_type]))
{
if ($error_type == 'long_message')
$txt['error_' . $error_type] = sprintf($txt['error_' . $error_type], $modSettings['max_messageLength']);
$context['post_error']['messages'][] = $txt['error_' . $error_type];
}
if (!in_array($error_type, array('new_reply', 'not_approved', 'new_replies', 'old_topic', 'need_qr_verification', 'no_subject')))
$context['error_type'] = 'serious';
}
require_once($sourcedir . '/Subs-Editor.php');
$editorOptions = array(
'id' => 'message',
'value' => $context['message'],
'width' => '90%',
'height' => '175px',
'labels' => array(
'post_button' => $txt['send_message'],
),
'preview_type' => 2,
);
create_control_richedit($editorOptions);
$context['post_box_name'] = $editorOptions['id'];
$context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
if ($context['require_verification'] && !isset($_REQUEST['xml']))
{
require_once($sourcedir . '/Subs-Editor.php');
$verificationOptions = array(
'id' => 'pm',
);
$context['require_verification'] = create_control_verification($verificationOptions);
$context['visual_verification_id'] = $verificationOptions['id'];
}
$context['to_value'] = empty($named_recipients['to']) ? '' : '"' . implode('", "', $named_recipients['to']) . '"';
$context['bcc_value'] = empty($named_recipients['bcc']) ? '' : '"' . implode('", "', $named_recipients['bcc']) . '"';
call_integration_hook('integrate_pm_error');
checkSubmitOnce('free');
checkSubmitOnce('register');
}
function MessagePost2()
{
global $txt, $context, $sourcedir;
global $user_info, $modSettings, $smcFunc;
isAllowedTo('pm_send');
require_once($sourcedir . '/Subs-Auth.php');
if ($context['drafts_pm_save'] && (isset($_POST['save_draft']) || isset($_POST['id_pm_draft'])))
{
$context['id_pm_draft'] = !empty($_POST['id_pm_draft']) ? (int) $_POST['id_pm_draft'] : 0;
require_once($sourcedir . '/Drafts.php');
}
loadLanguage('PersonalMessage', '', false);
list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
$post_errors = array();
if (!empty($modSettings['pm_posts_per_hour']) && !allowedTo(array('admin_forum', 'moderate_forum', 'send_mail')) && $user_info['mod_cache']['bq'] == '0=1' && $user_info['mod_cache']['gq'] == '0=1')
{
$request = $smcFunc['db_query']('', '
SELECT COUNT(pr.id_pm) AS post_count
FROM {db_prefix}personal_messages AS pm
INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
WHERE pm.id_member_from = {int:current_member}
AND pm.msgtime > {int:msgtime}',
array(
'current_member' => $user_info['id'],
'msgtime' => time() - 3600,
)
);
list ($postCount) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
{
if (!isset($_REQUEST['xml']))
fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
else
$post_errors[] = 'pm_too_many_per_hour';
}
}
if (!isset($_REQUEST['xml']) && checkSession('post', '', false) != '')
$post_errors[] = 'session_timeout';
$_REQUEST['subject'] = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : '';
$_REQUEST['to'] = empty($_POST['to']) ? (empty($_GET['to']) ? '' : $_GET['to']) : $_POST['to'];
$_REQUEST['bcc'] = empty($_POST['bcc']) ? (empty($_GET['bcc']) ? '' : $_GET['bcc']) : $_POST['bcc'];
if (!empty($_POST['u']))
$_POST['recipient_to'] = explode(',', $_POST['u']);
$recipientList = array();
$namedRecipientList = array();
$namesNotFound = array();
foreach (array('to', 'bcc') as $recipientType)
{
$recipientList[$recipientType] = array();
if (!empty($_POST['recipient_' . $recipientType]) && is_array($_POST['recipient_' . $recipientType]))
{
foreach ($_POST['recipient_' . $recipientType] as $recipient)
$recipientList[$recipientType][] = (int) $recipient;
}
if (!empty($_REQUEST[$recipientType]))
{
$recipientString = strtr($_REQUEST[$recipientType], array('\\"' => '"'));
preg_match_all('~"([^"]+)"~', $recipientString, $matches);
$namedRecipientList[$recipientType] = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $recipientString))));
foreach ($namedRecipientList[$recipientType] as $index => $recipient)
{
if (strlen(trim($recipient)) > 0)
$namedRecipientList[$recipientType][$index] = $smcFunc['htmlspecialchars']($smcFunc['strtolower'](trim($recipient)));
else
unset($namedRecipientList[$recipientType][$index]);
}
if (!empty($namedRecipientList[$recipientType]))
{
$foundMembers = findMembers($namedRecipientList[$recipientType]);
$namesNotFound[$recipientType] = $namedRecipientList[$recipientType];
foreach ($foundMembers as $member)
{
$testNames = array(
$smcFunc['strtolower']($member['username']),
$smcFunc['strtolower']($member['name']),
$smcFunc['strtolower']($member['email']),
);
if (count(array_intersect($testNames, $namedRecipientList[$recipientType])) !== 0)
{
$recipientList[$recipientType][] = $member['id'];
$namesNotFound[$recipientType] = array_diff($namesNotFound[$recipientType], $testNames);
}
}
}
}
if (!empty($_POST['delete_recipient']))
$recipientList[$recipientType] = array_diff($recipientList[$recipientType], array((int) $_POST['delete_recipient']));
$recipientList[$recipientType] = array_unique($recipientList[$recipientType]);
}
$is_recipient_change = !empty($_POST['delete_recipient']) || !empty($_POST['to_submit']) || !empty($_POST['bcc_submit']);
if (empty($recipientList['to']) && empty($recipientList['bcc']))
$post_errors[] = 'no_to';
if (!$is_recipient_change)
{
foreach ($recipientList as $recipientType => $dummy)
{
if (!empty($namesNotFound[$recipientType]))
{
$post_errors[] = 'bad_' . $recipientType;
$post_errors = array_diff($post_errors, array('no_to'));
foreach ($namesNotFound[$recipientType] as $name)
$context['send_log']['failed'][] = sprintf($txt['pm_error_user_not_found'], $name);
}
}
}
if ($_REQUEST['subject'] == '')
$post_errors[] = 'no_subject';
if (!isset($_REQUEST['message']) || $_REQUEST['message'] == '')
$post_errors[] = 'no_message';
elseif (!empty($modSettings['max_messageLength']) && $smcFunc['strlen']($_REQUEST['message']) > $modSettings['max_messageLength'])
$post_errors[] = 'long_message';
else
{
$message = $_REQUEST['message'];
preparsecode($message);
if ($smcFunc['htmltrim'](strip_tags(parse_bbc($smcFunc['htmlspecialchars']($message, ENT_QUOTES), false), '<img>')) === '' && (!allowedTo('bbc_html') || strpos($message, '[html]') === false))
$post_errors[] = 'no_message';
}
if (!$user_info['is_admin'] && !isset($_REQUEST['xml']) && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'])
{
require_once($sourcedir . '/Subs-Editor.php');
$verificationOptions = array(
'id' => 'pm',
);
$context['require_verification'] = create_control_verification($verificationOptions, true);
if (is_array($context['require_verification']))
$post_errors = array_merge($post_errors, $context['require_verification']);
}
if (!empty($post_errors) && !$is_recipient_change && !isset($_REQUEST['preview']) && !isset($_REQUEST['xml']))
return messagePostError($post_errors, $namedRecipientList, $recipientList);
if (isset($_REQUEST['preview']))
{
$context['preview_subject'] = $smcFunc['htmlspecialchars']($_REQUEST['subject']);
$context['preview_message'] = $smcFunc['htmlspecialchars']($_REQUEST['message'], ENT_QUOTES);
preparsecode($context['preview_message'], true);
$context['preview_message'] = parse_bbc($context['preview_message']);
censorText($context['preview_subject']);
censorText($context['preview_message']);
$context['page_title'] = $txt['preview'] . ' - ' . $context['preview_subject'];
return messagePostError($post_errors, $namedRecipientList, $recipientList);
}
elseif ($is_recipient_change)
{
foreach ($namesNotFound as $recipientType => $names)
{
$post_errors[] = 'bad_' . $recipientType;
foreach ($names as $name)
$context['send_log']['failed'][] = sprintf($txt['pm_error_user_not_found'], $name);
}
return messagePostError(array(), $namedRecipientList, $recipientList);
}
if ($context['drafts_pm_save'] && isset($_POST['save_draft']))
{
SavePMDraft($post_errors, $recipientList);
return messagePostError($post_errors, $namedRecipientList, $recipientList);
}
elseif (!empty($modSettings['max_pm_recipients']) && count($recipientList['to']) + count($recipientList['bcc']) > $modSettings['max_pm_recipients'] && !allowedTo(array('moderate_forum', 'send_mail', 'admin_forum')))
{
$context['send_log'] = array(
'sent' => array(),
'failed' => array(sprintf($txt['pm_too_many_recipients'], $modSettings['max_pm_recipients'])),
);
return messagePostError($post_errors, $namedRecipientList, $recipientList);
}
spamProtection('pm');
checkSubmitOnce('check');
if (!empty($recipientList['to']) || !empty($recipientList['bcc']))
$context['send_log'] = sendpm($recipientList, $_REQUEST['subject'], $_REQUEST['message'], true, null, !empty($_REQUEST['pm_head']) ? (int) $_REQUEST['pm_head'] : 0);
else
$context['send_log'] = array(
'sent' => array(),
'failed' => array()
);
if (!empty($context['send_log']['sent']) && !empty($_REQUEST['replied_to']) && isset($_REQUEST['f']) && $_REQUEST['f'] == 'inbox')
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET is_read = is_read | 2
WHERE id_pm = {int:replied_to}
AND id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'replied_to' => (int) $_REQUEST['replied_to'],
)
);
}
if (!empty($context['send_log']['failed']))
return messagePostError($post_errors, $namesNotFound, array(
'to' => array_intersect($recipientList['to'], $context['send_log']['failed']),
'bcc' => array_intersect($recipientList['bcc'], $context['send_log']['failed'])
));
if (!empty($context['send_log']) && empty($context['send_log']['failed']))
{
$context['current_label_redirect'] = $context['current_label_redirect'] . ';done=sent';
if ($context['drafts_pm_save'] && !empty($_POST['id_pm_draft']))
DeleteDraft($_POST['id_pm_draft']);
}
redirectexit($context['current_label_redirect']);
}
function MessageActionsApply()
{
global $context, $user_info, $options, $smcFunc;
checkSession('request');
if (isset($_REQUEST['del_selected']))
$_REQUEST['pm_action'] = 'delete';
if (isset($_REQUEST['pm_action']) && $_REQUEST['pm_action'] != '' && !empty($_REQUEST['pms']) && is_array($_REQUEST['pms']))
{
foreach ($_REQUEST['pms'] as $pm)
$_REQUEST['pm_actions'][(int) $pm] = $_REQUEST['pm_action'];
}
if (empty($_REQUEST['pm_actions']))
redirectexit($context['current_label_redirect']);
if ($context['display_mode'] == 2 && isset($_REQUEST['conversation']))
{
$id_pms = array();
foreach ($_REQUEST['pm_actions'] as $pm => $dummy)
$id_pms[] = (int) $pm;
$request = $smcFunc['db_query']('', '
SELECT id_pm_head, id_pm
FROM {db_prefix}personal_messages
WHERE id_pm IN ({array_int:id_pms})',
array(
'id_pms' => $id_pms,
)
);
$pm_heads = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$pm_heads[$row['id_pm_head']] = $row['id_pm'];
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']('', '
SELECT id_pm, id_pm_head
FROM {db_prefix}personal_messages
WHERE id_pm_head IN ({array_int:pm_heads})',
array(
'pm_heads' => array_keys($pm_heads),
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (isset($pm_heads[$row['id_pm_head']]) && isset($_REQUEST['pm_actions'][$pm_heads[$row['id_pm_head']]]))
$_REQUEST['pm_actions'][$row['id_pm']] = $_REQUEST['pm_actions'][$pm_heads[$row['id_pm_head']]];
}
$smcFunc['db_free_result']($request);
}
$to_delete = array();
$to_label = array();
$label_type = array();
$labels = array();
foreach ($_REQUEST['pm_actions'] as $pm => $action)
{
if ($action === 'delete')
$to_delete[] = (int) $pm;
else
{
if (substr($action, 0, 4) == 'add_')
{
$type = 'add';
$action = substr($action, 4);
}
elseif (substr($action, 0, 4) == 'rem_')
{
$type = 'rem';
$action = substr($action, 4);
}
else
$type = 'unk';
if ($action == '-1' || (int) $action > 0)
{
$to_label[(int) $pm] = (int) $action;
$label_type[(int) $pm] = $type;
}
}
}
if (!empty($to_delete))
deleteMessages($to_delete, $context['display_mode'] == 2 ? null : $context['folder']);
if (!empty($to_label) && $context['folder'] == 'inbox')
{
if ($context['display_mode'] == 2)
{
$get_pms = $smcFunc['db_query']('', '
SELECT pm.id_pm_head, pm.id_pm
FROM {db_prefix}personal_messages AS pm
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
WHERE pm.id_pm_head IN ({array_int:head_pms})
AND pm.id_pm NOT IN ({array_int:head_pms})
AND pmr.id_member = {int:current_member}',
array(
'head_pms' => array_keys($to_label),
'current_member' => $user_info['id'],
)
);
while ($other_pms = $smcFunc['db_fetch_assoc']($get_pms))
{
$to_label[$other_pms['id_pm']] = $to_label[$other_pms['id_pm_head']];
}
$smcFunc['db_free_result']($get_pms);
}
$request = $smcFunc['db_query']('', '
SELECT id_pm, in_inbox
FROM {db_prefix}pm_recipients
WHERE id_member = {int:current_member}
AND id_pm IN ({array_int:to_label})
LIMIT ' . count($to_label),
array(
'current_member' => $user_info['id'],
'to_label' => array_keys($to_label),
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($to_label[$row['id_pm']] != '-1')
{
$request2 = $smcFunc['db_query']('', '
SELECT l.id_label, pml.id_pm
FROM {db_prefix}pm_labels AS l
INNER JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_label = l.id_label)
WHERE l.id_member = {int:current_member}
AND pml.id_pm = {int:current_pm}',
array(
'current_member' => $user_info['id'],
'current_pm' => $row['id_pm'],
)
);
while ($row2 = $smcFunc['db_fetch_assoc']($request2))
{
$labels[$row2['id_label']] = $row2['id_label'];
}
$smcFunc['db_free_result']($request2);
}
elseif ($type == 'rem')
{
$request2 = $smcFunc['db_query']('', '
SELECT COUNT(l.id_label)
FROM {db_prefix}pm_labels AS l
INNER JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_label = l.id_label)
WHERE l.id_member = {int:current_member}
AND pml.id_pm = {int:current_pm}',
array(
'current_member' => $user_info['id'],
'current_pm' => $row['id_pm'],
)
);
list ($num_labels) = $smcFunc['db_fetch_assoc']($request2);
if ($num_labels > 0)
$context['can_remove_inbox'] = true;
$smcFunc['db_free_result']($request2);
}
$original_labels = $labels;
if ($to_label[$row['id_pm']] != '-1')
{
if (array_key_exists($to_label[$row['id_pm']], $labels) && $type !== 'add')
unset($labels[$to_label[$row['id_pm']]]);
elseif ($type !== 'rem')
$labels[$to_label[$row['id_pm']]] = $to_label[$row['id_pm']];
}
if ($type == 'rem' && empty($labels))
$in_inbox = (empty($context['can_remove_inbox']) ? 1 : 0);
elseif ($type == 'add' && !empty($options['pm_remove_inbox_label']) && !empty($labels))
$in_inbox = 0;
else
$in_inbox = 1;
if ($in_inbox != $row['in_inbox'])
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET in_inbox = {int:in_inbox}
WHERE id_pm = {int:id_pm}
AND id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'id_pm' => $row['id_pm'],
'in_inbox' => $in_inbox,
)
);
}
$labels_to_remove = array_diff($original_labels, $labels);
$labels_to_apply = array_diff($labels, $original_labels);
if (!empty($labels_to_remove))
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_labeled_messages
WHERE id_pm = {int:current_pm}
AND id_label IN ({array_int:labels_to_remove})',
array(
'current_pm' => $row['id_pm'],
'labels_to_remove' => $labels_to_remove,
)
);
}
if (!empty($labels_to_apply))
{
$inserts = array();
foreach ($labels_to_apply as $label)
$inserts[] = array($row['id_pm'], $label);
$smcFunc['db_insert']('',
'{db_prefix}pm_labeled_messages',
array('id_pm' => 'int', 'id_label' => 'int'),
$inserts,
array()
);
}
}
$smcFunc['db_free_result']($request);
}
$_SESSION['pm_selected'] = array_keys($to_label);
redirectexit($context['current_label_redirect'] . (count($to_label) == 1 ? '#msg' . $_SESSION['pm_selected'][0] : ''), count($to_label) == 1 && isBrowser('ie'));
}
function MessageKillAll()
{
global $context;
checkSession();
deleteMessages(null, null);
redirectexit($context['current_label_redirect']);
}
function MessagePrune()
{
global $txt, $context, $user_info, $scripturl, $smcFunc;
if (isset($_REQUEST['age']))
{
checkSession();
$deleteTime = max(0, time() - (86400 * (int) $_REQUEST['age']));
$toDelete = array();
$request = $smcFunc['db_query']('', '
SELECT id_pm
FROM {db_prefix}personal_messages
WHERE deleted_by_sender = {int:not_deleted}
AND id_member_from = {int:current_member}
AND msgtime < {int:msgtime}',
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
'msgtime' => $deleteTime,
)
);
while ($row = $smcFunc['db_fetch_row']($request))
$toDelete[] = $row[0];
$smcFunc['db_free_result']($request);
$request = $smcFunc['db_query']('', '
SELECT pmr.id_pm
FROM {db_prefix}pm_recipients AS pmr
INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
WHERE pmr.deleted = {int:not_deleted}
AND pmr.id_member = {int:current_member}
AND pm.msgtime < {int:msgtime}',
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
'msgtime' => $deleteTime,
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$toDelete[] = $row['id_pm'];
$smcFunc['db_free_result']($request);
deleteMessages($toDelete);
redirectexit($context['current_label_redirect']);
}
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=prune',
'name' => $txt['pm_prune']
);
$context['sub_template'] = 'prune';
$context['page_title'] = $txt['pm_prune'];
}
function deleteMessages($personal_messages, $folder = null, $owner = null)
{
global $user_info, $smcFunc;
if ($owner === null)
$owner = array($user_info['id']);
elseif (empty($owner))
return;
elseif (!is_array($owner))
$owner = array($owner);
if ($personal_messages !== null)
{
if (empty($personal_messages) || !is_array($personal_messages))
return;
foreach ($personal_messages as $index => $delete_id)
$personal_messages[$index] = (int) $delete_id;
$where = '
AND id_pm IN ({array_int:pm_list})';
}
else
$where = '';
if ($folder == 'sent' || $folder === null)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}personal_messages
SET deleted_by_sender = {int:is_deleted}
WHERE id_member_from IN ({array_int:member_list})
AND deleted_by_sender = {int:not_deleted}' . $where,
array(
'member_list' => $owner,
'is_deleted' => 1,
'not_deleted' => 0,
'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
)
);
}
if ($folder != 'sent' || $folder === null)
{
$request = $smcFunc['db_query']('', '
SELECT id_member, COUNT(*) AS num_deleted_messages, CASE WHEN is_read & 1 >= 1 THEN 1 ELSE 0 END AS is_read
FROM {db_prefix}pm_recipients
WHERE id_member IN ({array_int:member_list})
AND deleted = {int:not_deleted}' . $where . '
GROUP BY id_member, is_read',
array(
'member_list' => $owner,
'not_deleted' => 0,
'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($row['is_read'])
updateMemberData($row['id_member'], array('instant_messages' => $where == '' ? 0 : 'instant_messages - ' . $row['num_deleted_messages']));
else
updateMemberData($row['id_member'], array('instant_messages' => $where == '' ? 0 : 'instant_messages - ' . $row['num_deleted_messages'], 'unread_messages' => $where == '' ? 0 : 'unread_messages - ' . $row['num_deleted_messages']));
if ($user_info['id'] == $row['id_member'])
{
$user_info['messages'] -= $row['num_deleted_messages'];
if (!($row['is_read']))
$user_info['unread_messages'] -= $row['num_deleted_messages'];
}
}
$smcFunc['db_free_result']($request);
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET deleted = {int:is_deleted}
WHERE id_member IN ({array_int:member_list})
AND deleted = {int:not_deleted}' . $where,
array(
'member_list' => $owner,
'is_deleted' => 1,
'not_deleted' => 0,
'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
)
);
$labels = array();
$get_labels = $smcFunc['db_query']('', '
SELECT pml.id_label
FROM {db_prefix}pm_labels AS l
INNER JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_label = l.id_label)
WHERE l.id_member IN ({array_int:member_list})' . $where,
array(
'member_list' => $owner,
'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
)
);
while ($row = $smcFunc['db_fetch_assoc']($get_labels))
{
$labels[] = $row['id_label'];
}
$smcFunc['db_free_result']($get_labels);
if (!empty($labels))
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_labeled_messages
WHERE id_label IN ({array_int:labels})' . $where,
array(
'labels' => $labels,
'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
)
);
}
}
$request = $smcFunc['db_query']('', '
SELECT pm.id_pm AS sender, pmr.id_pm
FROM {db_prefix}personal_messages AS pm
LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm AND pmr.deleted = {int:not_deleted})
WHERE pm.deleted_by_sender = {int:is_deleted} AND pmr.id_pm is null
' . str_replace('id_pm', 'pm.id_pm', $where),
array(
'not_deleted' => 0,
'is_deleted' => 1,
'pm_list' => $personal_messages !== null ? array_unique($personal_messages) : array(),
)
);
$remove_pms = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$remove_pms[] = $row['sender'];
$smcFunc['db_free_result']($request);
if (!empty($remove_pms))
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}personal_messages
WHERE id_pm IN ({array_int:pm_list})',
array(
'pm_list' => $remove_pms,
)
);
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_recipients
WHERE id_pm IN ({array_int:pm_list})',
array(
'pm_list' => $remove_pms,
)
);
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_labeled_messages
WHERE id_pm IN ({array_int:pm_list})',
array(
'pm_list' => $remove_pms,
)
);
}
cache_put_data('labelCounts:' . $user_info['id'], null, 720);
}
function markMessages($personal_messages = null, $label = null, $owner = null)
{
global $user_info, $context, $smcFunc;
if ($owner === null)
$owner = $user_info['id'];
$in_inbox = '';
if ($personal_messages === null && $label !== null && $label != '-1')
{
$personal_messages = array();
$get_messages = $smcFunc['db_query']('', '
SELECT id_pm
FROM {db_prefix}pm_labeled_messages
WHERE id_label = {int:current_label}',
array(
'current_label' => $label,
)
);
while ($row = $smcFunc['db_fetch_assoc']($get_messages))
{
$personal_messages[] = $row['id_pm'];
}
$smcFunc['db_free_result']($get_messages);
}
elseif ($label = '-1')
{
$in_inbox = '
AND in_inbox = {int:in_inbox}';
}
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET is_read = is_read | 1
WHERE id_member = {int:id_member}
AND NOT (is_read & 1 >= 1)' . ($personal_messages !== null ? '
AND id_pm IN ({array_int:personal_messages})' : '') . $in_inbox,
array(
'personal_messages' => $personal_messages,
'id_member' => $owner,
'in_inbox' => 1,
)
);
if ($smcFunc['db_affected_rows']() > 0)
{
if ($owner == $user_info['id'])
{
foreach ($context['labels'] as $label)
$context['labels'][(int) $label['id']]['unread_messages'] = 0;
}
$result = $smcFunc['db_query']('', '
SELECT id_pm, in_inbox, COUNT(*) AS num
FROM {db_prefix}pm_recipients
WHERE id_member = {int:id_member}
AND NOT (is_read & 1 >= 1)
AND deleted = {int:is_not_deleted}
GROUP BY id_pm, in_inbox',
array(
'id_member' => $owner,
'is_not_deleted' => 0,
)
);
$total_unread = 0;
while ($row = $smcFunc['db_fetch_assoc']($result))
{
$total_unread += $row['num'];
if ($owner != $user_info['id'] || empty($row['id_pm']))
continue;
$this_labels = array();
$result2 = $smcFunc['db_query']('', '
SELECT pml.id_label
FROM {db_prefix}pm_labels AS l
INNER JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_label = l.id_label)
WHERE l.id_member = {int:id_member}
AND pml.id_pm = {int:current_pm}',
array(
'id_member' => $owner,
'current_pm' => $row['id_pm'],
)
);
while ($row2 = $smcFunc['db_fetch_assoc']($result2))
{
$this_labels[] = $row2['id_label'];
}
$smcFunc['db_free_result']($result2);
foreach ($this_labels as $this_label)
$context['labels'][$this_label]['unread_messages'] += $row['num'];
if ($row['in_inbox'] == 1)
$context['labels'][-1]['unread_messages'] += $row['num'];
}
$smcFunc['db_free_result']($result);
cache_put_data('labelCounts:' . $owner, $context['labels'], 720);
updateMemberData($owner, array('unread_messages' => $total_unread));
if ($owner == $user_info['id'])
$user_info['unread_messages'] = $total_unread;
}
}
function ManageLabels()
{
global $txt, $context, $user_info, $scripturl, $smcFunc;
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=manlabels',
'name' => $txt['pm_manage_labels']
);
$context['page_title'] = $txt['pm_manage_labels'];
$context['sub_template'] = 'labels';
$the_labels = array();
$labels_to_add = array();
$labels_to_remove = array();
$label_updates = array();
foreach ($context['labels'] as $label)
{
if ($label['id'] != -1)
$the_labels[$label['id']] = $label['name'];
}
if (isset($_POST[$context['session_var']]))
{
checkSession();
$message_changes = array();
$rule_changes = array();
LoadRules();
if (isset($_POST['add']))
{
$_POST['label'] = strtr($smcFunc['htmlspecialchars'](trim($_POST['label'])), array(',' => ','));
if ($smcFunc['strlen']($_POST['label']) > 30)
$_POST['label'] = $smcFunc['substr']($_POST['label'], 0, 30);
if ($_POST['label'] != '')
{
$the_labels[] = $_POST['label'];
$labels_to_add[] = $_POST['label'];
}
}
elseif (isset($_POST['delete'], $_POST['delete_label']))
{
foreach ($_POST['delete_label'] AS $label => $dummy)
{
unset($the_labels[$label]);
$labels_to_remove[] = $label;
}
}
elseif (isset($_POST['save']) && !empty($_POST['label_name']))
{
foreach ($the_labels as $id => $name)
{
if ($id == -1)
continue;
elseif (isset($_POST['label_name'][$id]))
{
$_POST['label_name'][$id] = trim(strtr($smcFunc['htmlspecialchars']($_POST['label_name'][$id]), array(',' => ',')));
if ($smcFunc['strlen']($_POST['label_name'][$id]) > 30)
$_POST['label_name'][$id] = $smcFunc['substr']($_POST['label_name'][$id], 0, 30);
if ($_POST['label_name'][$id] != '')
{
if ($the_labels[$id] != $_POST['label_name'][$id])
$label_updates[$id] = $_POST['label_name'][$id];
$the_labels[(int) $id] = $_POST['label_name'][$id];
}
else
{
unset($the_labels[(int) $id]);
$labels_to_remove[] = $id;
$message_changes[(int) $id] = true;
}
}
}
}
if (!empty($labels_to_add))
{
$inserts = array();
foreach ($labels_to_add AS $label)
$inserts[] = array($user_info['id'], $label);
$smcFunc['db_insert']('', '{db_prefix}pm_labels', array('id_member' => 'int', 'name' => 'string-30'), $inserts, array());
}
if (!empty($label_upates))
{
foreach ($label_updates AS $id => $name)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}labels
SET name = {string:name}
WHERE id_label = {int:id_label}',
array(
'name' => $name,
'id' => $id
)
);
}
}
if (!empty($labels_to_remove))
{
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_labels
WHERE id_label IN ({array_int:labels_to_delete})',
array(
'labels_to_delete' => $labels_to_remove,
)
);
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_labeled_messages
WHERE id_label IN ({array_int:labels_to_delete})',
array(
'labels_to_delete' => $labels_to_remove,
)
);
$get_stranded_pms = $smcFunc['db_query']('', '
SELECT pmr.id_pm
FROM {db_prefix}pm_recipients AS pmr
LEFT JOIN {db_prefix}pm_labeled_messages AS pml ON (pml.id_pm = pmr.id_pm)
WHERE pml.id_label IS NULL
AND pmr.in_inbox = {int:not_in_inbox}
AND pmr.deleted = {int:not_deleted}
AND pmr.id_member = {int:current_member}',
array(
'not_in_inbox' => 0,
'not_deleted' => 0,
'current_member' => $user_info['id'],
)
);
$stranded_messages = array();
while ($row = $smcFunc['db_fetch_assoc']($get_stranded_pms))
{
$stranded_messages[] = $row['id_pm'];
}
$smcFunc['db_free_result']($get_stranded_pms);
if (!empty($stranded_messages))
{
$context['labels'][-1]['messages'] += count($stranded_messages);
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET in_inbox = {int:in_inbox}
WHERE id_pm IN ({array_int:stranded_messages})
AND id_member = {int:current_member}',
array(
'stranded_messages' => $stranded_messages,
'in_inbox' => 1,
)
);
}
foreach ($context['rules'] as $k => $rule)
{
foreach ($rule['actions'] as $k2 => $action)
{
if ($action['t'] != 'lab' || !in_array($action['v'], $labels_to_remove))
continue;
$rule_changes[] = $rule['id'];
unset($context['rules'][$k]['actions'][$k2]);
}
}
}
if (!empty($rule_changes))
{
$rule_changes = array_unique($rule_changes);
foreach ($rule_changes as $k => $id)
if (!empty($context['rules'][$id]['actions']))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_rules
SET actions = {string:actions}
WHERE id_rule = {int:id_rule}
AND id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'id_rule' => $id,
'actions' => $smcFunc['json_encode']($context['rules'][$id]['actions']),
)
);
unset($rule_changes[$k]);
}
if (!empty($rule_changes))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_rules
WHERE id_rule IN ({array_int:rule_list})
AND id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'rule_list' => $rule_changes,
)
);
}
cache_put_data('labelCounts:' . $user_info['id'], null, 720);
redirectexit('action=pm;sa=manlabels');
}
}
function MessageSettings()
{
global $txt, $user_info, $context, $sourcedir;
global $scripturl, $profile_vars, $cur_profile, $user_profile;
require_once($sourcedir . '/Profile.php');
require_once($sourcedir . '/Profile-Modify.php');
$context['profile_custom_submit_url'] = $scripturl . '?action=pm;sa=settings;save';
loadMemberData($user_info['id'], false, 'profile');
$cur_profile = $user_profile[$user_info['id']];
loadLanguage('Profile');
loadTemplate('Profile');
$context['page_title'] = $txt['pm_settings'];
$context['user']['is_owner'] = true;
$context['id_member'] = $user_info['id'];
$context['require_password'] = false;
$context['menu_item_selected'] = 'settings';
$context['submit_button_text'] = $txt['pm_settings'];
$context['profile_header_text'] = $txt['personal_messages'];
$context['sub_template'] = 'edit_options';
$context['page_desc'] = $txt['pm_settings_desc'];
loadThemeOptions($user_info['id']);
loadCustomFields($user_info['id'], 'pmprefs');
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=settings',
'name' => $txt['pm_settings']
);
if (isset($_REQUEST['save']))
{
checkSession();
$_POST = htmltrim__recursive($_POST);
$_POST = htmlspecialchars__recursive($_POST);
saveProfileFields();
if (!empty($profile_vars))
updateMemberData($user_info['id'], $profile_vars);
}
setupProfileContext(
array(
'pm_prefs',
)
);
}
function ReportMessage()
{
global $txt, $context, $scripturl;
global $user_info, $language, $modSettings, $smcFunc;
if (empty($modSettings['enableReportPM']) || empty($_REQUEST['pmsg']))
fatal_lang_error('no_access', false);
$pmsg = (int) $_REQUEST['pmsg'];
if (!isAccessiblePM($pmsg, 'inbox'))
fatal_lang_error('no_access', false);
$context['pm_id'] = $pmsg;
$context['page_title'] = $txt['pm_report_title'];
if (!isset($_POST['report']))
{
$context['sub_template'] = 'report_message';
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name
FROM {db_prefix}members
WHERE id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0
ORDER BY real_name',
array(
'admin_group' => 1,
)
);
$context['admins'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$context['admins'][$row['id_member']] = $row['real_name'];
$smcFunc['db_free_result']($request);
$context['admin_count'] = count($context['admins']);
}
else
{
checkSession();
$request = $smcFunc['db_query']('', '
SELECT pm.subject, pm.body, pm.msgtime, pm.id_member_from, COALESCE(m.real_name, pm.from_name) AS sender_name
FROM {db_prefix}personal_messages AS pm
INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
LEFT JOIN {db_prefix}members AS m ON (m.id_member = pm.id_member_from)
WHERE pm.id_pm = {int:id_pm}
AND pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}
LIMIT 1',
array(
'current_member' => $user_info['id'],
'id_pm' => $context['pm_id'],
'not_deleted' => 0,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('no_access', false);
list ($subject, $body, $time, $memberFromID, $memberFromName) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$body = preg_replace('~<br ?/?' . '>~i', "\n", $body);
$request = $smcFunc['db_query']('', '
SELECT mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc
FROM {db_prefix}pm_recipients AS pmr
LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
WHERE pmr.id_pm = {int:id_pm}
AND pmr.id_member != {int:current_member}',
array(
'current_member' => $user_info['id'],
'id_pm' => $context['pm_id'],
)
);
$recipients = array();
$hidden_recipients = 0;
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($row['bcc'])
$hidden_recipients++;
else
$recipients[] = '[url=' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . ']' . $row['to_name'] . '[/url]';
}
$smcFunc['db_free_result']($request);
if ($hidden_recipients)
$recipients[] = sprintf($txt['pm_report_pm_hidden'], $hidden_recipients);
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name, lngfile
FROM {db_prefix}members
WHERE (id_group = {int:admin_id} OR FIND_IN_SET({int:admin_id}, additional_groups) != 0)
' . (empty($_POST['id_admin']) ? '' : 'AND id_member = {int:specific_admin}') . '
ORDER BY lngfile',
array(
'admin_id' => 1,
'specific_admin' => isset($_POST['id_admin']) ? (int) $_POST['id_admin'] : 0,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
fatal_lang_error('no_access', false);
$memberFromName = un_htmlspecialchars($memberFromName);
$messagesToSend = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$cur_language = empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile'];
if (!isset($messagesToSend[$cur_language]))
{
loadLanguage('PersonalMessage', $cur_language, false);
$report_body = str_replace(array('{REPORTER}', '{SENDER}'), array(un_htmlspecialchars($user_info['name']), $memberFromName), $txt['pm_report_pm_user_sent']);
$report_body .= "\n" . '[b]' . $_POST['reason'] . '[/b]' . "\n\n";
if (!empty($recipients))
$report_body .= $txt['pm_report_pm_other_recipients'] . ' ' . implode(', ', $recipients) . "\n\n";
$report_body .= $txt['pm_report_pm_unedited_below'] . "\n" . '[quote author=' . (empty($memberFromID) ? '"' . $memberFromName . '"' : $memberFromName . ' link=action=profile;u=' . $memberFromID . ' date=' . $time) . ']' . "\n" . un_htmlspecialchars($body) . '[/quote]';
$messagesToSend[$cur_language] = array(
'subject' => ($smcFunc['strpos']($subject, $txt['pm_report_pm_subject']) === false ? $txt['pm_report_pm_subject'] : '') . un_htmlspecialchars($subject),
'body' => $report_body,
'recipients' => array(
'to' => array(),
'bcc' => array()
),
);
}
$messagesToSend[$cur_language]['recipients']['to'][$row['id_member']] = $row['id_member'];
}
$smcFunc['db_free_result']($request);
foreach ($messagesToSend as $lang => $message)
sendpm($message['recipients'], $message['subject'], $message['body']);
if (!empty($modSettings['userLanguage']))
loadLanguage('PersonalMessage', '', false);
$context['sub_template'] = 'report_message_complete';
}
}
function ManageRules()
{
global $txt, $context, $user_info, $scripturl, $smcFunc;
$context['linktree'][] = array(
'url' => $scripturl . '?action=pm;sa=manrules',
'name' => $txt['pm_manage_rules']
);
$context['page_title'] = $txt['pm_manage_rules'];
$context['sub_template'] = 'rules';
LoadRules();
$request = $smcFunc['db_query']('', '
SELECT mg.id_group, mg.group_name, COALESCE(gm.id_member, 0) AS can_moderate, mg.hidden
FROM {db_prefix}membergroups AS mg
LEFT JOIN {db_prefix}group_moderators AS gm ON (gm.id_group = mg.id_group AND gm.id_member = {int:current_member})
WHERE mg.min_posts = {int:min_posts}
AND mg.id_group != {int:moderator_group}
AND mg.hidden = {int:not_hidden}
ORDER BY mg.group_name',
array(
'current_member' => $user_info['id'],
'min_posts' => -1,
'moderator_group' => 3,
'not_hidden' => 0,
)
);
$context['groups'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if ($row['hidden'] && !$row['can_moderate'] && !allowedTo('manage_membergroups'))
continue;
$context['groups'][$row['id_group']] = $row['group_name'];
}
$smcFunc['db_free_result']($request);
if (isset($_GET['apply']))
{
checkSession('get');
ApplyRules(true);
redirectexit('action=pm;sa=manrules');
}
if (isset($_GET['add']))
{
$context['rid'] = isset($_GET['rid']) && isset($context['rules'][$_GET['rid']]) ? (int) $_GET['rid'] : 0;
$context['sub_template'] = 'add_rule';
if ($context['rid'])
{
$context['rule'] = $context['rules'][$context['rid']];
$members = array();
foreach ($context['rule']['criteria'] as $k => $criteria)
if ($criteria['t'] == 'mid' && !empty($criteria['v']))
$members[(int) $criteria['v']] = $k;
if (!empty($members))
{
$request = $smcFunc['db_query']('', '
SELECT id_member, member_name
FROM {db_prefix}members
WHERE id_member IN ({array_int:member_list})',
array(
'member_list' => array_keys($members),
)
);
while ($row = $smcFunc['db_fetch_assoc']($request))
$context['rule']['criteria'][$members[$row['id_member']]]['v'] = $row['member_name'];
$smcFunc['db_free_result']($request);
}
}
else
$context['rule'] = array(
'id' => '',
'name' => '',
'criteria' => array(),
'actions' => array(),
'logic' => 'and',
);
}
elseif (isset($_GET['save']))
{
checkSession();
$context['rid'] = isset($_GET['rid']) && isset($context['rules'][$_GET['rid']]) ? (int) $_GET['rid'] : 0;
$ruleName = $smcFunc['htmlspecialchars'](trim($_POST['rule_name']));
if (empty($ruleName))
fatal_lang_error('pm_rule_no_name', false);
if (empty($_POST['ruletype']) || empty($_POST['acttype']))
fatal_lang_error('pm_rule_no_criteria', false);
$criteria = array();
foreach ($_POST['ruletype'] as $ind => $type)
{
if ($type == 'gid' && (!isset($_POST['ruledefgroup'][$ind]) || !isset($context['groups'][$_POST['ruledefgroup'][$ind]])))
continue;
elseif ($type != 'bud' && !isset($_POST['ruledef'][$ind]))
continue;
if ($type == 'mid')
{
$name = trim($_POST['ruledef'][$ind]);
$request = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}members
WHERE real_name = {string:member_name}
OR member_name = {string:member_name}',
array(
'member_name' => $name,
)
);
if ($smcFunc['db_num_rows']($request) == 0)
{
loadLanguage('Errors');
fatal_lang_error('invalid_username', false);
}
list ($memID) = $smcFunc['db_fetch_row']($request);
$smcFunc['db_free_result']($request);
$criteria[] = array('t' => 'mid', 'v' => $memID);
}
elseif ($type == 'bud')
$criteria[] = array('t' => 'bud', 'v' => 1);
elseif ($type == 'gid')
$criteria[] = array('t' => 'gid', 'v' => (int) $_POST['ruledefgroup'][$ind]);
elseif (in_array($type, array('sub', 'msg')) && trim($_POST['ruledef'][$ind]) != '')
$criteria[] = array('t' => $type, 'v' => $smcFunc['htmlspecialchars'](trim($_POST['ruledef'][$ind])));
}
$actions = array();
$doDelete = 0;
$isOr = $_POST['rule_logic'] == 'or' ? 1 : 0;
foreach ($_POST['acttype'] as $ind => $type)
{
if ($type == 'lab' && (!ctype_digit((string) $ind) || !isset($_POST['labdef'][$ind]) || !isset($context['labels'][$_POST['labdef'][$ind]])))
continue;
if ($type == 'del')
$doDelete = 1;
elseif ($type == 'lab')
$actions[] = array('t' => 'lab', 'v' => (int) $_POST['labdef'][$ind]);
}
if (empty($criteria) || (empty($actions) && !$doDelete))
fatal_lang_error('pm_rule_no_criteria', false);
$criteria = $smcFunc['json_encode']($criteria);
$actions = $smcFunc['json_encode']($actions);
if (empty($context['rid']))
$smcFunc['db_insert']('',
'{db_prefix}pm_rules',
array(
'id_member' => 'int', 'rule_name' => 'string', 'criteria' => 'string', 'actions' => 'string',
'delete_pm' => 'int', 'is_or' => 'int',
),
array(
$user_info['id'], $ruleName, $criteria, $actions, $doDelete, $isOr,
),
array('id_rule')
);
else
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_rules
SET rule_name = {string:rule_name}, criteria = {string:criteria}, actions = {string:actions},
delete_pm = {int:delete_pm}, is_or = {int:is_or}
WHERE id_rule = {int:id_rule}
AND id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'delete_pm' => $doDelete,
'is_or' => $isOr,
'id_rule' => $context['rid'],
'rule_name' => $ruleName,
'criteria' => $criteria,
'actions' => $actions,
)
);
redirectexit('action=pm;sa=manrules');
}
elseif (isset($_POST['delselected']) && !empty($_POST['delrule']))
{
checkSession();
$toDelete = array();
foreach ($_POST['delrule'] as $k => $v)
$toDelete[] = (int) $k;
if (!empty($toDelete))
$smcFunc['db_query']('', '
DELETE FROM {db_prefix}pm_rules
WHERE id_rule IN ({array_int:delete_list})
AND id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
'delete_list' => $toDelete,
)
);
redirectexit('action=pm;sa=manrules');
}
}
function ApplyRules($all_messages = false)
{
global $user_info, $smcFunc, $context, $options;
loadRules();
if (empty($context['rules']))
return;
$ruleQuery = $all_messages ? '' : ' AND pmr.is_new = 1';
$request = $smcFunc['db_query']('', '
SELECT
pmr.id_pm, pm.id_member_from, pm.subject, pm.body, mem.id_group
FROM {db_prefix}pm_recipients AS pmr
INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
WHERE pmr.id_member = {int:current_member}
AND pmr.deleted = {int:not_deleted}
' . $ruleQuery,
array(
'current_member' => $user_info['id'],
'not_deleted' => 0,
)
);
$actions = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
foreach ($context['rules'] as $rule)
{
$match = false;
foreach ($rule['criteria'] as $criterium)
{
if (($criterium['t'] == 'mid' && $criterium['v'] == $row['id_member_from']) || ($criterium['t'] == 'gid' && $criterium['v'] == $row['id_group']) || ($criterium['t'] == 'sub' && strpos($row['subject'], $criterium['v']) !== false) || ($criterium['t'] == 'msg' && strpos($row['body'], $criterium['v']) !== false))
$match = true;
elseif ($rule['logic'] == 'and')
{
$match = false;
break;
}
}
if ($match)
{
if ($rule['delete'])
$actions['deletes'][] = $row['id_pm'];
else
{
foreach ($rule['actions'] as $ruleAction)
{
if ($ruleAction['t'] == 'lab')
{
if (!isset($actions['labels'][$row['id_pm']]))
$actions['labels'][$row['id_pm']] = array();
$actions['labels'][$row['id_pm']][] = $ruleAction['v'];
}
}
}
}
}
}
$smcFunc['db_free_result']($request);
if (!empty($actions['deletes']))
deleteMessages($actions['deletes']);
if (!empty($actions['labels']))
{
foreach ($actions['labels'] as $pm => $labels)
{
$realLabels = array();
foreach ($context['labels'] as $label)
{
if (in_array($label['id'], $labels) && $label['id'] != -1 || empty($options['pm_remove_inbox_label']))
{
if ($label['id'] == '-1')
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}pm_recipients
SET in_inbox = {int:in_inbox}
WHERE id_pm = {int:id_pm}
AND id_member = {int:current_member}',
array(
'in_inbox' => 1,
'id_pm' => $pm,
'current_member' => $user_info['id'],
)
);
}
else
{
$realLabels[] = $label['id'];
}
}
}
$inserts = array();
foreach ($realLabels as $a_label)
$inserts[] = array($pm, $a_label);
$smcFunc['db_insert']('ignore',
'{db_prefix}pm_labeled_messages',
array('id_pm' => 'int', 'id_label' => 'int'),
$inserts,
array()
);
}
}
}
function LoadRules($reload = false)
{
global $user_info, $context, $smcFunc;
if (isset($context['rules']) && !$reload)
return;
$request = $smcFunc['db_query']('', '
SELECT
id_rule, rule_name, criteria, actions, delete_pm, is_or
FROM {db_prefix}pm_rules
WHERE id_member = {int:current_member}',
array(
'current_member' => $user_info['id'],
)
);
$context['rules'] = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$context['rules'][$row['id_rule']] = array(
'id' => $row['id_rule'],
'name' => $row['rule_name'],
'criteria' => $smcFunc['json_decode']($row['criteria'], true),
'actions' => $smcFunc['json_decode']($row['actions'], true),
'delete' => $row['delete_pm'],
'logic' => $row['is_or'] ? 'or' : 'and',
);
if ($row['delete_pm'])
$context['rules'][$row['id_rule']]['actions'][] = array('t' => 'del', 'v' => 1);
}
$smcFunc['db_free_result']($request);
}
function isAccessiblePM($pmID, $validFor = 'in_or_outbox')
{
global $user_info, $smcFunc;
$request = $smcFunc['db_query']('', '
SELECT
pm.id_member_from = {int:id_current_member} AND pm.deleted_by_sender = {int:not_deleted} AS valid_for_outbox,
pmr.id_pm IS NOT NULL AS valid_for_inbox
FROM {db_prefix}personal_messages AS pm
LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm AND pmr.id_member = {int:id_current_member} AND pmr.deleted = {int:not_deleted})
WHERE pm.id_pm = {int:id_pm}
AND ((pm.id_member_from = {int:id_current_member} AND pm.deleted_by_sender = {int:not_deleted}) OR pmr.id_pm IS NOT NULL)',
array(
'id_pm' => $pmID,
'id_current_member' => $user_info['id'],
'not_deleted' => 0,
)
);
if ($smcFunc['db_num_rows']($request) === 0)
{
$smcFunc['db_free_result']($request);
return false;
}
$validationResult = $smcFunc['db_fetch_assoc']($request);
$smcFunc['db_free_result']($request);
switch ($validFor)
{
case 'inbox':
return !empty($validationResult['valid_for_inbox']);
break;
case 'outbox':
return !empty($validationResult['valid_for_outbox']);
break;
case 'in_or_outbox':
return !empty($validationResult['valid_for_inbox']) || !empty($validationResult['valid_for_outbox']);
break;
default:
trigger_error('Undefined validation type given', E_USER_ERROR);
break;
}
}
?>