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: 
<?php

/**
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC1
 */

// Don't do anything if SMF is already loaded.
if (defined('SMF'))
    return true;

define('SMF', 'SSI');
define('SMF_VERSION', '2.1 RC1');
define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
define('SMF_SOFTWARE_YEAR', '2019');

// We're going to want a few globals... these are all set later.
global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
global $db_type, $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
global $db_connection, $db_port, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cache_enable, $cachedir;

// Remember the current configuration so it can be set back.
$time_start = microtime(true);

// Just being safe...
foreach (array('db_character_set', 'cachedir') as $variable)
    if (isset($GLOBALS[$variable]))
        unset($GLOBALS[$variable]);

// Get the forum's settings for database and file paths.
require_once(dirname(__FILE__) . '/Settings.php');

// Make absolutely sure the cache directory is defined.
if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
    $cachedir = $boarddir . '/cache';

$ssi_error_reporting = error_reporting(E_ALL);
/* Set this to one of three values depending on what you want to happen in the case of a fatal error.
    false:  Default, will just load the error sub template and die - not putting any theme layers around it.
    true:   Will load the error sub template AND put the SMF layers around it (Not useful if on total custom pages).
    string: Name of a callback function to call in the event of an error to allow you to define your own methods. Will die after function returns.
*/
$ssi_on_error_method = false;

// Don't do john didley if the forum's been shut down completely.
if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
    die($mmessage);

// Fix for using the current directory as a path.
if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
    $sourcedir = dirname(__FILE__) . substr($sourcedir, 1);

// Load the important includes.
require_once($sourcedir . '/QueryString.php');
require_once($sourcedir . '/Session.php');
require_once($sourcedir . '/Subs.php');
require_once($sourcedir . '/Errors.php');
require_once($sourcedir . '/Logging.php');
require_once($sourcedir . '/Load.php');
require_once($sourcedir . '/Security.php');
require_once($sourcedir . '/Class-BrowserDetect.php');
require_once($sourcedir . '/Subs-Auth.php');

// Create a variable to store some SMF specific functions in.
$smcFunc = array();

// Initiate the database connection and define some database functions to use.
loadDatabase();

// Load installed 'Mods' settings.
reloadSettings();
// Clean the request variables.
cleanRequest();

// Seed the random generator?
if (empty($modSettings['rand_seed']) || mt_rand(1, 250) == 69)
    smf_seed_generator();

// Check on any hacking attempts.
if (isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS']))
    die('No direct access...');
elseif (isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme)
    die('No direct access...');
elseif (isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme)
    die('No direct access...');
elseif (isset($_REQUEST['ssi_layers'], $ssi_layers) && (@get_magic_quotes_gpc() ? stripslashes($_REQUEST['ssi_layers']) : $_REQUEST['ssi_layers']) == $ssi_layers)
    die('No direct access...');
if (isset($_REQUEST['context']))
    die('No direct access...');

// Gzip output? (because it must be boolean and true, this can't be hacked.)
if (isset($ssi_gzip) && $ssi_gzip === true && ini_get('zlib.output_compression') != '1' && ini_get('output_handler') != 'ob_gzhandler' && version_compare(PHP_VERSION, '4.2.0', '>='))
    ob_start('ob_gzhandler');
else
    $modSettings['enableCompressedOutput'] = '0';

/**
 * An autoloader for certain classes.
 *
 * @param string $class The fully-qualified class name.
 */
spl_autoload_register(function($class) use ($sourcedir)
{
    $classMap = array(
        'ReCaptcha\\' => 'ReCaptcha/',
        'MatthiasMullie\\Minify\\' => 'minify/src/',
        'MatthiasMullie\\PathConverter\\' => 'minify/path-converter/src/',
    );

    // Do any third-party scripts want in on the fun?
    call_integration_hook('integrate_autoload', array(&$classMap));

    foreach ($classMap as $prefix => $dirName)
    {
        // does the class use the namespace prefix?
        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) !== 0)
        {
            continue;
        }

        // get the relative class name
        $relativeClass = substr($class, $len);

        // replace the namespace prefix with the base directory, replace namespace
        // separators with directory separators in the relative class name, append
        // with .php
        $fileName = $dirName . strtr($relativeClass, '\\', '/') . '.php';

        // if the file exists, require it
        if (file_exists($fileName = $sourcedir . '/' . $fileName))
        {
            require_once $fileName;

            return;
        }
    }
});

// Primarily, this is to fix the URLs...
ob_start('ob_sessrewrite');

// Start the session... known to scramble SSI includes in cases...
if (!headers_sent())
    loadSession();
else
{
    if (isset($_COOKIE[session_name()]) || isset($_REQUEST[session_name()]))
    {
        // Make a stab at it, but ignore the E_WARNINGs generated because we can't send headers.
        $temp = error_reporting(error_reporting() & !E_WARNING);
        loadSession();
        error_reporting($temp);
    }

    if (!isset($_SESSION['session_value']))
    {
        $_SESSION['session_var'] = substr(md5($smcFunc['random_int']() . session_id() . $smcFunc['random_int']()), 0, rand(7, 12));
        $_SESSION['session_value'] = md5(session_id() . $smcFunc['random_int']());
    }
    $sc = $_SESSION['session_value'];
}

// Get rid of $board and $topic... do stuff loadBoard would do.
unset($board, $topic);
$user_info['is_mod'] = false;
$context['user']['is_mod'] = &$user_info['is_mod'];
$context['linktree'] = array();

// Load the user and their cookie, as well as their settings.
loadUserSettings();

// Load the current user's permissions....
loadPermissions();

// Load the current or SSI theme. (just use $ssi_theme = id_theme;)
loadTheme(isset($ssi_theme) ? (int) $ssi_theme : 0);

// @todo: probably not the best place, but somewhere it should be set...
if (!headers_sent())
    header('content-type: text/html; charset=' . (empty($modSettings['global_character_set']) ? (empty($txt['lang_character_set']) ? 'ISO-8859-1' : $txt['lang_character_set']) : $modSettings['global_character_set']));

// Take care of any banning that needs to be done.
if (isset($_REQUEST['ssi_ban']) || (isset($ssi_ban) && $ssi_ban === true))
    is_not_banned();

// Do we allow guests in here?
if (empty($ssi_guest_access) && empty($modSettings['allow_guestAccess']) && $user_info['is_guest'] && basename($_SERVER['PHP_SELF']) != 'SSI.php')
{
    require_once($sourcedir . '/Subs-Auth.php');
    KickGuest();
    obExit(null, true);
}

// Load the stuff like the menu bar, etc.
if (isset($ssi_layers))
{
    $context['template_layers'] = $ssi_layers;
    template_header();
}
else
    setupThemeContext();

// Make sure they didn't muss around with the settings... but only if it's not cli.
if (isset($_SERVER['REMOTE_ADDR']) && !isset($_SERVER['is_cli']) && session_id() == '')
    trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);

// Without visiting the forum this session variable might not be set on submit.
if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
    $_SESSION['USER_AGENT'] = $_SERVER['HTTP_USER_AGENT'];

// Have the ability to easily add functions to SSI.
call_integration_hook('integrate_SSI');

// Ignore a call to ssi_* functions if we are not accessing SSI.php directly.
if (basename($_SERVER['PHP_SELF']) == 'SSI.php')
{
    // You shouldn't just access SSI.php directly by URL!!
    if (!isset($_GET['ssi_function']))
        die(sprintf($txt['ssi_not_direct'], $user_info['is_admin'] ? '\'' . addslashes(__FILE__) . '\'' : '\'SSI.php\''));
    // Call a function passed by GET.
    if (function_exists('ssi_' . $_GET['ssi_function']) && (!empty($modSettings['allow_guestAccess']) || !$user_info['is_guest']))
        call_user_func('ssi_' . $_GET['ssi_function']);
    exit;
}

// To avoid side effects later on.
unset($_GET['ssi_function']);

error_reporting($ssi_error_reporting);

return true;

/**
 * This shuts down the SSI and shows the footer.
 *
 * @return void
 */
function ssi_shutdown()
{
    if (!isset($_GET['ssi_function']) || $_GET['ssi_function'] != 'shutdown')
        template_footer();
}

/**
 * Show the SMF version.
 */
function ssi_version($output_method = 'echo')
{
    if ($output_method == 'echo')
        echo SMF_VERSION;
    else
        return SMF_VERSION;
}

/**
 * Show the full SMF version string.
 */
function ssi_full_version($output_method = 'echo')
{
    if ($output_method == 'echo')
        echo SMF_FULL_VERSION;
    else
        return SMF_FULL_VERSION;
}

/**
 * Show the SMF software year.
 */
function ssi_software_year($output_method = 'echo')
{
    if ($output_method == 'echo')
        echo SMF_SOFTWARE_YEAR;
    else
        return SMF_SOFTWARE_YEAR;
}

/**
 * Show the forum copyright. Only used in our ssi_examples files.
 */
function ssi_copyright($output_method = 'echo')
{
    global $forum_copyright;

    if ($output_method == 'echo')
        printf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR);
    else
        return sprintf($forum_copyright, SMF_FULL_VERSION, SMF_SOFTWARE_YEAR);
}

/**
 * Display a welcome message, like: Hey, User, you have 0 messages, 0 are new.
 *
 * @param string $output_method The output method. If 'echo', will display everything. Otherwise returns an array of user info.
 * @return void|array Displays a welcome message or returns an array of user data depending on output_method.
 */
function ssi_welcome($output_method = 'echo')
{
    global $context, $txt, $scripturl;

    if ($output_method == 'echo')
    {
        if ($context['user']['is_guest'])
            echo sprintf($txt[$context['can_register'] ? 'welcome_guest_register' : 'welcome_guest'], $txt['guest_title'], $context['forum_name_html_safe'], $scripturl . '?action=login', 'return reqOverlayDiv(this.href, ' . JavaScriptEscape($txt['login']) . ');', $scripturl . '?action=signup');
        else
            echo $txt['hello_member'], ' <strong>', $context['user']['name'], '</strong>', allowedTo('pm_read') ? ', ' . (empty($context['user']['messages']) ? $txt['msg_alert_no_messages'] : (($context['user']['messages'] == 1 ? sprintf($txt['msg_alert_one_message'], $scripturl . '?action=pm') : sprintf($txt['msg_alert_many_message'], $scripturl . '?action=pm', $context['user']['messages'])) . ', ' . ($context['user']['unread_messages'] == 1 ? $txt['msg_alert_one_new'] : sprintf($txt['msg_alert_many_new'], $context['user']['unread_messages'])))) : '';
    }
    // Don't echo... then do what?!
    else
        return $context['user'];
}

/**
 * Display a menu bar, like is displayed at the top of the forum.
 *
 * @param string $output_method The output method. If 'echo', will display the menu, otherwise returns an array of menu data.
 * @return void|array Displays the menu or returns an array of menu data depending on output_method.
 */
function ssi_menubar($output_method = 'echo')
{
    global $context;

    if ($output_method == 'echo')
        template_menu();
    // What else could this do?
    else
        return $context['menu_buttons'];
}

/**
 * Show a logout link.
 *
 * @param string $redirect_to A URL to redirect the user to after they log out.
 * @param string $output_method The output method. If 'echo', shows a logout link, otherwise returns the HTML for it.
 * @return void|string Displays a logout link or returns its HTML depending on output_method.
 */
function ssi_logout($redirect_to = '', $output_method = 'echo')
{
    global $context, $txt, $scripturl;

    if ($redirect_to != '')
        $_SESSION['logout_url'] = $redirect_to;

    // Guests can't log out.
    if ($context['user']['is_guest'])
        return false;

    $link = '<a href="' . $scripturl . '?action=logout;' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['logout'] . '</a>';

    if ($output_method == 'echo')
        echo $link;
    else
        return $link;
}

/**
 * Recent post list:   [board] Subject by Poster    Date
 *
 * @param int $num_recent How many recent posts to display
 * @param null|array $exclude_boards If set, doesn't show posts from the specified boards
 * @param null|array $include_boards If set, only includes posts from the specified boards
 * @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of information about them.
 * @param bool $limit_body Whether or not to only show the first 384 characters of each post
 * @return void|array Displays a list of recent posts or returns an array of information about them depending on output_method.
 */
function ssi_recentPosts($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo', $limit_body = true)
{
    global $modSettings, $context;

    // Excluding certain boards...
    if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
        $exclude_boards = array($modSettings['recycle_board']);
    else
        $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));

    // What about including certain boards - note we do some protection here as pre-2.0 didn't have this parameter.
    if (is_array($include_boards) || (int) $include_boards === $include_boards)
    {
        $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
    }
    elseif ($include_boards != null)
    {
        $include_boards = array();
    }

    // Let's restrict the query boys (and girls)
    $query_where = '
        m.id_msg >= {int:min_message_id}
        ' . (empty($exclude_boards) ? '' : '
        AND b.id_board NOT IN ({array_int:exclude_boards})') . '
        ' . ($include_boards === null ? '' : '
        AND b.id_board IN ({array_int:include_boards})') . '
        AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
        AND m.approved = {int:is_approved}' : '');

    $query_where_params = array(
        'is_approved' => 1,
        'include_boards' => $include_boards === null ? '' : $include_boards,
        'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
        'min_message_id' => $modSettings['maxMsgID'] - (!empty($context['min_message_posts']) ? $context['min_message_posts'] : 25) * min($num_recent, 5),
    );

    // Past to this simpleton of a function...
    return ssi_queryPosts($query_where, $query_where_params, $num_recent, 'm.id_msg DESC', $output_method, $limit_body);
}

/**
 * Fetches one or more posts by ID.
 *
 * @param array $post_ids An array containing the IDs of the posts to show
 * @param bool $override_permissions Whether to ignore permissions. If true, will show posts even if the user doesn't have permission to see them.
 * @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of info about them
 * @return void|array Displays the specified posts or returns an array of info about them, depending on output_method.
 */
function ssi_fetchPosts($post_ids = array(), $override_permissions = false, $output_method = 'echo')
{
    global $modSettings;

    if (empty($post_ids))
        return;

    // Allow the user to request more than one - why not?
    $post_ids = is_array($post_ids) ? $post_ids : array($post_ids);

    // Restrict the posts required...
    $query_where = '
        m.id_msg IN ({array_int:message_list})' . ($override_permissions ? '' : '
            AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
            AND m.approved = {int:is_approved}' : '');
    $query_where_params = array(
        'message_list' => $post_ids,
        'is_approved' => 1,
    );

    // Then make the query and dump the data.
    return ssi_queryPosts($query_where, $query_where_params, '', 'm.id_msg DESC', $output_method, false, $override_permissions);
}

/**
 * This handles actually pulling post info. Called from other functions to eliminate duplication.
 *
 * @param string $query_where The WHERE clause for the query
 * @param array $query_where_params An array of parameters for the WHERE clause
 * @param int $query_limit The maximum number of rows to return
 * @param string $query_order The ORDER BY clause for the query
 * @param string $output_method The output method. If 'echo', displays the posts, otherwise returns an array of info about them.
 * @param bool $limit_body If true, will only show the first 384 characters of the post rather than all of it
 * @param bool|false $override_permissions Whether or not to ignore permissions. If true, will show all posts regardless of whether the user can actually see them
 * @return void|array Displays the posts or returns an array of info about them, depending on output_method
 */
function ssi_queryPosts($query_where = '', $query_where_params = array(), $query_limit = 10, $query_order = 'm.id_msg DESC', $output_method = 'echo', $limit_body = false, $override_permissions = false)
{
    global $scripturl, $txt, $user_info;
    global $modSettings, $smcFunc, $context;

    if (!empty($modSettings['enable_likes']))
        $context['can_like'] = allowedTo('likes_like');

    // Find all the posts. Newer ones will have higher IDs.
    $request = $smcFunc['db_query']('substring', '
        SELECT
            m.poster_time, m.subject, m.id_topic, m.id_member, m.id_msg, m.id_board, m.likes, b.name AS board_name,
            COALESCE(mem.real_name, m.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
            COALESCE(lt.id_msg, lmr.id_msg, 0) >= m.id_msg_modified AS is_read,
            COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from') . ', ' . ($limit_body ? 'SUBSTRING(m.body, 1, 384) AS body' : 'm.body') . ', m.smileys_enabled
        FROM {db_prefix}messages AS m
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (!$user_info['is_guest'] ? '
            LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = m.id_topic AND lt.id_member = {int:current_member})
            LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = m.id_board AND lmr.id_member = {int:current_member})' : '') . '
        WHERE 1=1 ' . ($override_permissions ? '' : '
            AND {query_wanna_see_board}') . ($modSettings['postmod_active'] ? '
            AND m.approved = {int:is_approved}' : '') . '
        ' . (empty($query_where) ? '' : 'AND ' . $query_where) . '
        ORDER BY ' . $query_order . '
        ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
        array_merge($query_where_params, array(
            'current_member' => $user_info['id'],
            'is_approved' => 1,
        ))
    );
    $posts = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

        // Censor it!
        censorText($row['subject']);
        censorText($row['body']);

        $preview = strip_tags(strtr($row['body'], array('<br>' => '&#10;')));

        // Build the array.
        $posts[$row['id_msg']] = array(
            'id' => $row['id_msg'],
            'board' => array(
                'id' => $row['id_board'],
                'name' => $row['board_name'],
                'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
                'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['board_name'] . '</a>'
            ),
            'topic' => $row['id_topic'],
            'poster' => array(
                'id' => $row['id_member'],
                'name' => $row['poster_name'],
                'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
                'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
            ),
            'subject' => $row['subject'],
            'short_subject' => shorten_subject($row['subject'], 25),
            'preview' => $smcFunc['strlen']($preview) > 128 ? $smcFunc['substr']($preview, 0, 128) . '...' : $preview,
            'body' => $row['body'],
            'time' => timeformat($row['poster_time']),
            'timestamp' => forum_time(true, $row['poster_time']),
            'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
            'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '" rel="nofollow">' . $row['subject'] . '</a>',
            'new' => !empty($row['is_read']),
            'is_new' => empty($row['is_read']),
            'new_from' => $row['new_from'],
        );

        // Get the likes for each message.
        if (!empty($modSettings['enable_likes']))
            $posts[$row['id_msg']]['likes'] = array(
                'count' => $row['likes'],
                'you' => in_array($row['id_msg'], prepareLikesContext($row['id_topic'])),
                'can_like' => !$context['user']['is_guest'] && $row['id_member'] != $context['user']['id'] && !empty($context['can_like']),
            );
    }
    $smcFunc['db_free_result']($request);

    // If mods want to do something with this list of posts, let them do that now.
    call_integration_hook('integrate_ssi_queryPosts', array(&$posts));

    // Just return it.
    if ($output_method != 'echo' || empty($posts))
        return $posts;

    echo '
        <table style="border: none" class="ssi_table">';
    foreach ($posts as $post)
        echo '
            <tr>
                <td style="text-align: right; vertical-align: top; white-space: nowrap">
                    [', $post['board']['link'], ']
                </td>
                <td style="vertical-align: top">
                    <a href="', $post['href'], '">', $post['subject'], '</a>
                    ', $txt['by'], ' ', $post['poster']['link'], '
                    ', $post['is_new'] ? '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow" class="new_posts">' . $txt['new'] . '</a>' : '', '
                </td>
                <td style="text-align: right; white-space: nowrap">
                    ', $post['time'], '
                </td>
            </tr>';
    echo '
        </table>';
}

/**
 * Recent topic list:   [board] Subject by Poster   Date
 *
 * @param int $num_recent How many recent topics to show
 * @param null|array $exclude_boards If set, exclude topics from the specified board(s)
 * @param null|array $include_boards If set, only include topics from the specified board(s)
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
 * @return void|array Either displays a list of topics or returns an array of info about them, depending on output_method.
 */
function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boards = null, $output_method = 'echo')
{
    global $settings, $scripturl, $txt, $user_info;
    global $modSettings, $smcFunc, $context;

    if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0)
        $exclude_boards = array($modSettings['recycle_board']);
    else
        $exclude_boards = empty($exclude_boards) ? array() : (is_array($exclude_boards) ? $exclude_boards : array($exclude_boards));

    // Only some boards?.
    if (is_array($include_boards) || (int) $include_boards === $include_boards)
    {
        $include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
    }
    elseif ($include_boards != null)
    {
        $output_method = $include_boards;
        $include_boards = array();
    }

    $icon_sources = array();
    foreach ($context['stable_icons'] as $icon)
        $icon_sources[$icon] = 'images_url';

    // Find all the posts in distinct topics.  Newer ones will have higher IDs.
    $request = $smcFunc['db_query']('substring', '
        SELECT
            t.id_topic, b.id_board, b.name AS board_name
        FROM {db_prefix}topics AS t
            INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
            LEFT JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
        WHERE t.id_last_msg >= {int:min_message_id}' . (empty($exclude_boards) ? '' : '
            AND b.id_board NOT IN ({array_int:exclude_boards})') . '' . (empty($include_boards) ? '' : '
            AND b.id_board IN ({array_int:include_boards})') . '
            AND {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
            AND t.approved = {int:is_approved}
            AND ml.approved = {int:is_approved}' : '') . '
        ORDER BY t.id_last_msg DESC
        LIMIT ' . $num_recent,
        array(
            'include_boards' => empty($include_boards) ? '' : $include_boards,
            'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
            'min_message_id' => $modSettings['maxMsgID'] - (!empty($context['min_message_topics']) ? $context['min_message_topics'] : 35) * min($num_recent, 5),
            'is_approved' => 1,
        )
    );
    $topics = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $topics[$row['id_topic']] = $row;
    $smcFunc['db_free_result']($request);

    // Did we find anything? If not, bail.
    if (empty($topics))
        return array();

    $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;

    // Find all the posts in distinct topics.  Newer ones will have higher IDs.
    $request = $smcFunc['db_query']('substring', '
        SELECT
            mf.poster_time, mf.subject, ml.id_topic, mf.id_member, ml.id_msg, t.num_replies, t.num_views, mg.online_color, t.id_last_msg,
            COALESCE(mem.real_name, mf.poster_name) AS poster_name, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
            COALESCE(lt.id_msg, lmr.id_msg, 0) >= ml.id_msg_modified AS is_read,
            COALESCE(lt.id_msg, lmr.id_msg, -1) + 1 AS new_from') . ', SUBSTRING(mf.body, 1, 384) AS body, mf.smileys_enabled, mf.icon
        FROM {db_prefix}topics AS t
            INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
            INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_last_msg)
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mf.id_member)' . (!$user_info['is_guest'] ? '
            LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
            LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = t.id_board AND lmr.id_member = {int:current_member})' : '') . '
            LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
        WHERE t.id_topic IN ({array_int:topic_list})
        ORDER BY t.id_last_msg DESC',
        array(
            'current_member' => $user_info['id'],
            'topic_list' => array_keys($topics),
        )
    );
    $posts = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br>' => '&#10;')));
        if ($smcFunc['strlen']($row['body']) > 128)
            $row['body'] = $smcFunc['substr']($row['body'], 0, 128) . '...';

        // Censor the subject.
        censorText($row['subject']);
        censorText($row['body']);

        // Recycled icon
        if (!empty($recycle_board) && $topics[$row['id_topic']]['id_board'])
            $row['icon'] = 'recycled';

        if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
            $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
        elseif (!isset($icon_sources[$row['icon']]))
            $icon_sources[$row['icon']] = 'images_url';

        // Build the array.
        $posts[] = array(
            'board' => array(
                'id' => $topics[$row['id_topic']]['id_board'],
                'name' => $topics[$row['id_topic']]['board_name'],
                'href' => $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0',
                'link' => '<a href="' . $scripturl . '?board=' . $topics[$row['id_topic']]['id_board'] . '.0">' . $topics[$row['id_topic']]['board_name'] . '</a>',
            ),
            'topic' => $row['id_topic'],
            'poster' => array(
                'id' => $row['id_member'],
                'name' => $row['poster_name'],
                'href' => empty($row['id_member']) ? '' : $scripturl . '?action=profile;u=' . $row['id_member'],
                'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
            ),
            'subject' => $row['subject'],
            'replies' => $row['num_replies'],
            'views' => $row['num_views'],
            'short_subject' => shorten_subject($row['subject'], 25),
            'preview' => $row['body'],
            'time' => timeformat($row['poster_time']),
            'timestamp' => forum_time(true, $row['poster_time']),
            'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . ';topicseen#new',
            'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#new" rel="nofollow">' . $row['subject'] . '</a>',
            // Retained for compatibility - is technically incorrect!
            'new' => !empty($row['is_read']),
            'is_new' => empty($row['is_read']),
            'new_from' => $row['new_from'],
            'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" style="vertical-align:middle;" alt="' . $row['icon'] . '">',
        );
    }
    $smcFunc['db_free_result']($request);

    // If mods want to do somthing with this list of topics, let them do that now.
    call_integration_hook('integrate_ssi_recentTopics', array(&$posts));

    // Just return it.
    if ($output_method != 'echo' || empty($posts))
        return $posts;

    echo '
        <table style="border: none" class="ssi_table">';
    foreach ($posts as $post)
        echo '
            <tr>
                <td style="text-align: right; vertical-align: top; white-space: nowrap">
                    [', $post['board']['link'], ']
                </td>
                <td style="vertical-align: top">
                    <a href="', $post['href'], '">', $post['subject'], '</a>
                    ', $txt['by'], ' ', $post['poster']['link'], '
                    ', !$post['is_new'] ? '' : '<a href="' . $scripturl . '?topic=' . $post['topic'] . '.msg' . $post['new_from'] . ';topicseen#new" rel="nofollow" class="new_posts">' . $txt['new'] . '</a>', '
                </td>
                <td style="text-align: right; white-space: nowrap">
                    ', $post['time'], '
                </td>
            </tr>';
    echo '
        </table>';
}

/**
 * Shows a list of top posters
 *
 * @param int $topNumber How many top posters to list
 * @param string $output_method The output method. If 'echo', will display a list of users, otherwise returns an array of info about them.
 * @return void|array Either displays a list of users or returns an array of info about them, depending on output_method.
 */
function ssi_topPoster($topNumber = 1, $output_method = 'echo')
{
    global $scripturl, $smcFunc;

    // Find the latest poster.
    $request = $smcFunc['db_query']('', '
        SELECT id_member, real_name, posts
        FROM {db_prefix}members
        ORDER BY posts DESC
        LIMIT ' . $topNumber,
        array(
        )
    );
    $return = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $return[] = array(
            'id' => $row['id_member'],
            'name' => $row['real_name'],
            'href' => $scripturl . '?action=profile;u=' . $row['id_member'],
            'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name'] . '</a>',
            'posts' => $row['posts']
        );
    $smcFunc['db_free_result']($request);

    // If mods want to do somthing with this list of members, let them do that now.
    call_integration_hook('integrate_ssi_topPoster', array(&$return));

    // Just return all the top posters.
    if ($output_method != 'echo')
        return $return;

    // Make a quick array to list the links in.
    $temp_array = array();
    foreach ($return as $member)
        $temp_array[] = $member['link'];

    echo implode(', ', $temp_array);
}

/**
 * Shows a list of top boards based on activity
 *
 * @param int $num_top How many boards to display
 * @param string $output_method The output method. If 'echo', displays a list of boards, otherwise returns an array of info about them.
 * @return void|array Displays a list of the top boards or returns an array of info about them, depending on output_method.
 */
function ssi_topBoards($num_top = 10, $output_method = 'echo')
{
    global $txt, $scripturl, $user_info, $modSettings, $smcFunc;

    // Find boards with lots of posts.
    $request = $smcFunc['db_query']('', '
        SELECT
            b.name, b.num_topics, b.num_posts, b.id_board,' . (!$user_info['is_guest'] ? ' 1 AS is_read' : '
            (COALESCE(lb.id_msg, 0) >= b.id_last_msg) AS is_read') . '
        FROM {db_prefix}boards AS b
            LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
        WHERE {query_wanna_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
            AND b.id_board != {int:recycle_board}' : '') . '
        ORDER BY b.num_posts DESC
        LIMIT ' . $num_top,
        array(
            'current_member' => $user_info['id'],
            'recycle_board' => (int) $modSettings['recycle_board'],
        )
    );
    $boards = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $boards[] = array(
            'id' => $row['id_board'],
            'num_posts' => $row['num_posts'],
            'num_topics' => $row['num_topics'],
            'name' => $row['name'],
            'new' => empty($row['is_read']),
            'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
            'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>'
        );
    $smcFunc['db_free_result']($request);

    // If mods want to do somthing with this list of boards, let them do that now.
    call_integration_hook('integrate_ssi_topBoards', array(&$boards));

    // If we shouldn't output or have nothing to output, just jump out.
    if ($output_method != 'echo' || empty($boards))
        return $boards;

    echo '
        <table class="ssi_table">
            <tr>
                <th style="text-align: left">', $txt['board'], '</th>
                <th style="text-align: left">', $txt['board_topics'], '</th>
                <th style="text-align: left">', $txt['posts'], '</th>
            </tr>';
    foreach ($boards as $sBoard)
        echo '
            <tr>
                <td>', $sBoard['link'], $sBoard['new'] ? ' <a href="' . $sBoard['href'] . '" class="new_posts">' . $txt['new'] . '</a>' : '', '</td>
                <td style="text-align: right">', comma_format($sBoard['num_topics']), '</td>
                <td style="text-align: right">', comma_format($sBoard['num_posts']), '</td>
            </tr>';
    echo '
        </table>';
}

// Shows the top topics.
/**
 * Shows a list of top topics based on views or replies
 *
 * @param string $type Can be either replies or views
 * @param int $num_topics How many topics to display
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them.
 * @return void|array Either displays a list of topics or returns an array of info about them, depending on output_method.
 */
function ssi_topTopics($type = 'replies', $num_topics = 10, $output_method = 'echo')
{
    global $txt, $scripturl, $modSettings, $smcFunc;

    if ($modSettings['totalMessages'] > 100000)
    {
        // @todo Why don't we use {query(_wanna)_see_board}?
        $request = $smcFunc['db_query']('', '
            SELECT id_topic
            FROM {db_prefix}topics
            WHERE num_' . ($type != 'replies' ? 'views' : 'replies') . ' != 0' . ($modSettings['postmod_active'] ? '
                AND approved = {int:is_approved}' : '') . '
            ORDER BY num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
            LIMIT {int:limit}',
            array(
                'is_approved' => 1,
                'limit' => $num_topics > 100 ? ($num_topics + ($num_topics / 2)) : 100,
            )
        );
        $topic_ids = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
            $topic_ids[] = $row['id_topic'];
        $smcFunc['db_free_result']($request);
    }
    else
        $topic_ids = array();

    $request = $smcFunc['db_query']('', '
        SELECT m.subject, m.id_topic, t.num_views, t.num_replies
        FROM {db_prefix}topics AS t
            INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
        WHERE {query_wanna_see_board}' . ($modSettings['postmod_active'] ? '
            AND t.approved = {int:is_approved}' : '') . (!empty($topic_ids) ? '
            AND t.id_topic IN ({array_int:topic_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
            AND b.id_board != {int:recycle_enable}' : '') . '
        ORDER BY t.num_' . ($type != 'replies' ? 'views' : 'replies') . ' DESC
        LIMIT {int:limit}',
        array(
            'topic_list' => $topic_ids,
            'is_approved' => 1,
            'recycle_enable' => $modSettings['recycle_board'],
            'limit' => $num_topics,
        )
    );
    $topics = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        censorText($row['subject']);

        $topics[] = array(
            'id' => $row['id_topic'],
            'subject' => $row['subject'],
            'num_replies' => $row['num_replies'],
            'num_views' => $row['num_views'],
            'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
            'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
        );
    }
    $smcFunc['db_free_result']($request);

    // If mods want to do somthing with this list of topics, let them do that now.
    call_integration_hook('integrate_ssi_topTopics', array(&$topics, $type));

    if ($output_method != 'echo' || empty($topics))
        return $topics;

    echo '
        <table class="ssi_table">
            <tr>
                <th style="text-align: left"></th>
                <th style="text-align: left">', $txt['views'], '</th>
                <th style="text-align: left">', $txt['replies'], '</th>
            </tr>';
    foreach ($topics as $sTopic)
        echo '
            <tr>
                <td style="text-align: left">
                    ', $sTopic['link'], '
                </td>
                <td style="text-align: right">', comma_format($sTopic['num_views']), '</td>
                <td style="text-align: right">', comma_format($sTopic['num_replies']), '</td>
            </tr>';
    echo '
        </table>';
}

/**
 * Top topics based on replies
 *
 * @param int $num_topics How many topics to show
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
 * @return void|array Either displays a list of top topics or returns an array of info about them, depending on output_method.
 */
function ssi_topTopicsReplies($num_topics = 10, $output_method = 'echo')
{
    return ssi_topTopics('replies', $num_topics, $output_method);
}

/**
 * Top topics based on views
 *
 * @param int $num_topics How many topics to show
 * @param string $output_method The output method. If 'echo', displays a list of topics, otherwise returns an array of info about them
 * @return void|array Either displays a list of top topics or returns an array of info about them, depending on output_method.
 */
function ssi_topTopicsViews($num_topics = 10, $output_method = 'echo')
{
    return ssi_topTopics('views', $num_topics, $output_method);
}

/**
 * Show a link to the latest member: Please welcome, Someone, our latest member.
 *
 * @param string $output_method The output method. If 'echo', returns a string with a link to the latest member's profile, otherwise returns an array of info about them.
 * @return void|array Displays a "welcome" message for the latest member or returns an array of info about them, depending on output_method.
 */
function ssi_latestMember($output_method = 'echo')
{
    global $txt, $context;

    if ($output_method == 'echo')
        echo '
    ', sprintf($txt['welcome_newest_member'], $context['common_stats']['latest_member']['link']), '<br>';
    else
        return $context['common_stats']['latest_member'];
}

/**
 * Fetches a random member.
 *
 * @param string $random_type If 'day', only fetches a new random member once a day.
 * @param string $output_method The output method. If 'echo', displays a link to the member's profile, otherwise returns an array of info about them.
 * @return void|array Displays a link to a random member's profile or returns an array of info about them depending on output_method.
 */
function ssi_randomMember($random_type = '', $output_method = 'echo')
{
    global $modSettings;

    // If we're looking for something to stay the same each day then seed the generator.
    if ($random_type == 'day')
    {
        // Set the seed to change only once per day.
        mt_srand(floor(time() / 86400));
    }

    // Get the lowest ID we're interested in.
    $member_id = mt_rand(1, $modSettings['latestMember']);

    $where_query = '
        id_member >= {int:selected_member}
        AND is_activated = {int:is_activated}';

    $query_where_params = array(
        'selected_member' => $member_id,
        'is_activated' => 1,
    );

    $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member ASC', $output_method);

    // If we got nothing do the reverse - in case of unactivated members.
    if (empty($result))
    {
        $where_query = '
            id_member <= {int:selected_member}
            AND is_activated = {int:is_activated}';

        $query_where_params = array(
            'selected_member' => $member_id,
            'is_activated' => 1,
        );

        $result = ssi_queryMembers($where_query, $query_where_params, 1, 'id_member DESC', $output_method);
    }

    // Just to be sure put the random generator back to something... random.
    if ($random_type != '')
        mt_srand(time());

    return $result;
}

/**
 * Fetch specific members
 *
 * @param array $member_ids The IDs of the members to fetch
 * @param string $output_method The output method. If 'echo', displays a list of links to the members' profiles, otherwise returns an array of info about them.
 * @return void|array Displays links to the specified members' profiles or returns an array of info about them, depending on output_method.
 */
function ssi_fetchMember($member_ids = array(), $output_method = 'echo')
{
    if (empty($member_ids))
        return;

    // Can have more than one member if you really want...
    $member_ids = is_array($member_ids) ? $member_ids : array($member_ids);

    // Restrict it right!
    $query_where = '
        id_member IN ({array_int:member_list})';

    $query_where_params = array(
        'member_list' => $member_ids,
    );

    // Then make the query and dump the data.
    return ssi_queryMembers($query_where, $query_where_params, '', 'id_member', $output_method);
}

/**
 * Get al members in the specified group
 *
 * @param int $group_id The ID of the group to get members from
 * @param string $output_method The output method. If 'echo', returns a list of group members, otherwise returns an array of info about them.
 * @return void|array Displays a list of group members or returns an array of info about them, depending on output_method.
 */
function ssi_fetchGroupMembers($group_id = null, $output_method = 'echo')
{
    if ($group_id === null)
        return;

    $query_where = '
        id_group = {int:id_group}
        OR id_post_group = {int:id_group}
        OR FIND_IN_SET({int:id_group}, additional_groups) != 0';

    $query_where_params = array(
        'id_group' => $group_id,
    );

    return ssi_queryMembers($query_where, $query_where_params, '', 'real_name', $output_method);
}

/**
 * Pulls info about members based on the specified parameters. Used by other functions to eliminate duplication.
 *
 * @param string $query_where The info for the WHERE clause of the query
 * @param array $query_where_params The parameters for the WHERE clause
 * @param string|int $query_limit The number of rows to return or an empty string to return all
 * @param string $query_order The info for the ORDER BY clause of the query
 * @param string $output_method The output method. If 'echo', displays a list of members, otherwise returns an array of info about them
 * @return void|array Displays a list of members or returns an array of info about them, depending on output_method.
 */
function ssi_queryMembers($query_where = null, $query_where_params = array(), $query_limit = '', $query_order = 'id_member DESC', $output_method = 'echo')
{
    global $smcFunc, $memberContext;

    if ($query_where === null)
        return;

    // Fetch the members in question.
    $request = $smcFunc['db_query']('', '
        SELECT id_member
        FROM {db_prefix}members
        WHERE ' . $query_where . '
        ORDER BY ' . $query_order . '
        ' . ($query_limit == '' ? '' : 'LIMIT ' . $query_limit),
        array_merge($query_where_params, array(
        ))
    );
    $members = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $members[] = $row['id_member'];
    $smcFunc['db_free_result']($request);

    if (empty($members))
        return array();

    // If mods want to do somthing with this list of members, let them do that now.
    call_integration_hook('integrate_ssi_queryMembers', array(&$members));

    // Load the members.
    loadMemberData($members);

    // Draw the table!
    if ($output_method == 'echo')
        echo '
        <table style="border: none" class="ssi_table">';

    $query_members = array();
    foreach ($members as $member)
    {
        // Load their context data.
        if (!loadMemberContext($member))
            continue;

        // Store this member's information.
        $query_members[$member] = $memberContext[$member];

        // Only do something if we're echo'ing.
        if ($output_method == 'echo')
            echo '
            <tr>
                <td style="text-align: right; vertical-align: top; white-space: nowrap">
                    ', $query_members[$member]['link'], '
                    <br>', $query_members[$member]['blurb'], '
                    <br>', $query_members[$member]['avatar']['image'], '
                </td>
            </tr>';
    }

    // End the table if appropriate.
    if ($output_method == 'echo')
        echo '
        </table>';

    // Send back the data.
    return $query_members;
}

/**
 * Show some basic stats:   Total This: XXXX, etc.
 *
 * @param string $output_method The output method. If 'echo', displays the stats, otherwise returns an array of info about them
 * @return void|array Doesn't return anything if the user can't view stats. Otherwise either displays the stats or returns an array of info about them, depending on output_method.
 */
function ssi_boardStats($output_method = 'echo')
{
    global $txt, $scripturl, $modSettings, $smcFunc;

    if (!allowedTo('view_stats'))
        return;

    $totals = array(
        'members' => $modSettings['totalMembers'],
        'posts' => $modSettings['totalMessages'],
        'topics' => $modSettings['totalTopics']
    );

    $result = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}boards',
        array(
        )
    );
    list ($totals['boards']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    $result = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}categories',
        array(
        )
    );
    list ($totals['categories']) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    // If mods want to do somthing with the board stats, let them do that now.
    call_integration_hook('integrate_ssi_boardStats', array(&$totals));

    if ($output_method != 'echo')
        return $totals;

    echo '
        ', $txt['total_members'], ': <a href="', $scripturl . '?action=mlist">', comma_format($totals['members']), '</a><br>
        ', $txt['total_posts'], ': ', comma_format($totals['posts']), '<br>
        ', $txt['total_topics'], ': ', comma_format($totals['topics']), ' <br>
        ', $txt['total_cats'], ': ', comma_format($totals['categories']), '<br>
        ', $txt['total_boards'], ': ', comma_format($totals['boards']);
}

/**
 * Shows a list of online users:  YY Guests, ZZ Users and then a list...
 *
 * @param string $output_method The output method. If 'echo', displays a list, otherwise returns an array of info about the online users.
 * @return void|array Either displays a list of online users or returns an array of info about them, depending on output_method.
 */
function ssi_whosOnline($output_method = 'echo')
{
    global $user_info, $txt, $sourcedir, $settings;

    require_once($sourcedir . '/Subs-MembersOnline.php');
    $membersOnlineOptions = array(
        'show_hidden' => allowedTo('moderate_forum'),
    );
    $return = getMembersOnlineStats($membersOnlineOptions);

    // If mods want to do somthing with the list of who is online, let them do that now.
    call_integration_hook('integrate_ssi_whosOnline', array(&$return));

    // Add some redundancy for backwards compatibility reasons.
    if ($output_method != 'echo')
        return $return + array(
            'users' => $return['users_online'],
            'guests' => $return['num_guests'],
            'hidden' => $return['num_users_hidden'],
            'buddies' => $return['num_buddies'],
            'num_users' => $return['num_users_online'],
            'total_users' => $return['num_users_online'] + $return['num_guests'],
        );

    echo '
        ', comma_format($return['num_guests']), ' ', $return['num_guests'] == 1 ? $txt['guest'] : $txt['guests'], ', ', comma_format($return['num_users_online']), ' ', $return['num_users_online'] == 1 ? $txt['user'] : $txt['users'];

    $bracketList = array();
    if (!empty($user_info['buddies']))
        $bracketList[] = comma_format($return['num_buddies']) . ' ' . ($return['num_buddies'] == 1 ? $txt['buddy'] : $txt['buddies']);
    if (!empty($return['num_spiders']))
        $bracketList[] = comma_format($return['num_spiders']) . ' ' . ($return['num_spiders'] == 1 ? $txt['spider'] : $txt['spiders']);
    if (!empty($return['num_users_hidden']))
        $bracketList[] = comma_format($return['num_users_hidden']) . ' ' . $txt['hidden'];

    if (!empty($bracketList))
        echo ' (' . implode(', ', $bracketList) . ')';

    echo '<br>
            ', implode(', ', $return['list_users_online']);

    // Showing membergroups?
    if (!empty($settings['show_group_key']) && !empty($return['membergroups']))
        echo '<br>
            [' . implode(']&nbsp;&nbsp;[', $return['membergroups']) . ']';
}

/**
 * Just like whosOnline except it also logs the online presence.
 *
 * @param string $output_method The output method. If 'echo', displays a list, otherwise returns an array of info about the online users.
 * @return void|array Either displays a list of online users or returns an aray of info about them, depending on output_method.
 */
function ssi_logOnline($output_method = 'echo')
{
    writeLog();

    if ($output_method != 'echo')
        return ssi_whosOnline($output_method);
    else
        ssi_whosOnline($output_method);
}

// Shows a login box.
/**
 * Shows a login box
 *
 * @param string $redirect_to The URL to redirect the user to after they login
 * @param string $output_method The output method. If 'echo' and the user is a guest, displays a login box, otherwise returns whether the user is a guest
 * @return void|bool Either displays a login box or returns whether the user is a guest, depending on whether the user is logged in and output_method.
 */
function ssi_login($redirect_to = '', $output_method = 'echo')
{
    global $scripturl, $txt, $user_info, $context;

    if ($redirect_to != '')
        $_SESSION['login_url'] = $redirect_to;

    if ($output_method != 'echo' || !$user_info['is_guest'])
        return $user_info['is_guest'];

    // Create a login token
    createToken('login');

    echo '
        <form action="', $scripturl, '?action=login2" method="post" accept-charset="', $context['character_set'], '">
            <table style="border: none" class="ssi_table">
                <tr>
                    <td style="text-align: right; border-spacing: 1"><label for="user">', $txt['username'], ':</label>&nbsp;</td>
                    <td><input type="text" id="user" name="user" size="9" value="', $user_info['username'], '"></td>
                </tr><tr>
                    <td style="text-align: right; border-spacing: 1"><label for="passwrd">', $txt['password'], ':</label>&nbsp;</td>
                    <td><input type="password" name="passwrd" id="passwrd" size="9"></td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="cookielength" value="-1">
                        <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '" />
                        <input type="hidden" name="', $context['login_token_var'], '" value="', $context['login_token'], '">
                    </td>
                    <td><input type="submit" value="', $txt['login'], '" class="button"></td>
                </tr>
            </table>
        </form>';

}

/**
 * Show the top poll based on votes
 *
 * @param string $output_method The output method. If 'echo', displays the poll, otherwise returns an array of info about it
 * @return void|array Either shows the top poll or returns an array of info about it, depending on output_method.
 */
function ssi_topPoll($output_method = 'echo')
{
    // Just use recentPoll, no need to duplicate code...
    return ssi_recentPoll(true, $output_method);
}

// Show the most recently posted poll.
/**
 * Shows the most recent poll
 *
 * @param bool $topPollInstead Whether to show the top poll (based on votes) instead of the most recent one
 * @param string $output_method The output method. If 'echo', displays the poll, otherwise returns an array of info about it.
 * @return void|array Either shows the poll or returns an array of info about it, depending on output_method.
 */
function ssi_recentPoll($topPollInstead = false, $output_method = 'echo')
{
    global $txt, $boardurl, $user_info, $context, $smcFunc, $modSettings;

    $boardsAllowed = array_intersect(boardsAllowedTo('poll_view'), boardsAllowedTo('poll_vote'));

    if (empty($boardsAllowed))
        return array();

    $request = $smcFunc['db_query']('', '
        SELECT p.id_poll, p.question, t.id_topic, p.max_votes, p.guest_vote, p.hide_results, p.expire_time
        FROM {db_prefix}polls AS p
            INNER JOIN {db_prefix}topics AS t ON (t.id_poll = p.id_poll' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)' . ($topPollInstead ? '
            INNER JOIN {db_prefix}poll_choices AS pc ON (pc.id_poll = p.id_poll)' : '') . '
            LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member > {int:no_member} AND lp.id_member = {int:current_member})
        WHERE p.voting_locked = {int:voting_opened}
            AND (p.expire_time = {int:no_expiration} OR {int:current_time} < p.expire_time)
            AND ' . ($user_info['is_guest'] ? 'p.guest_vote = {int:guest_vote_allowed}' : 'lp.id_choice IS NULL') . '
            AND {query_wanna_see_board}' . (!in_array(0, $boardsAllowed) ? '
            AND b.id_board IN ({array_int:boards_allowed_list})' : '') . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
            AND b.id_board != {int:recycle_enable}' : '') . '
        ORDER BY ' . ($topPollInstead ? 'pc.votes' : 'p.id_poll') . ' DESC
        LIMIT 1',
        array(
            'current_member' => $user_info['id'],
            'boards_allowed_list' => $boardsAllowed,
            'is_approved' => 1,
            'guest_vote_allowed' => 1,
            'no_member' => 0,
            'voting_opened' => 0,
            'no_expiration' => 0,
            'current_time' => time(),
            'recycle_enable' => $modSettings['recycle_board'],
        )
    );
    $row = $smcFunc['db_fetch_assoc']($request);
    $smcFunc['db_free_result']($request);

    // This user has voted on all the polls.
    if (empty($row) || !is_array($row))
        return array();

    // If this is a guest who's voted we'll through ourselves to show poll to show the results.
    if ($user_info['is_guest'] && (!$row['guest_vote'] || (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))))
        return ssi_showPoll($row['id_topic'], $output_method);

    $request = $smcFunc['db_query']('', '
        SELECT COUNT(DISTINCT id_member)
        FROM {db_prefix}log_polls
        WHERE id_poll = {int:current_poll}',
        array(
            'current_poll' => $row['id_poll'],
        )
    );
    list ($total) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    $request = $smcFunc['db_query']('', '
        SELECT id_choice, label, votes
        FROM {db_prefix}poll_choices
        WHERE id_poll = {int:current_poll}',
        array(
            'current_poll' => $row['id_poll'],
        )
    );
    $sOptions = array();
    while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
    {
        censorText($rowChoice['label']);

        $sOptions[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
    }
    $smcFunc['db_free_result']($request);

    // Can they view it?
    $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
    $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || $is_expired;

    $return = array(
        'id' => $row['id_poll'],
        'image' => 'poll',
        'question' => $row['question'],
        'total_votes' => $total,
        'is_locked' => false,
        'topic' => $row['id_topic'],
        'allow_view_results' => $allow_view_results,
        'options' => array()
    );

    // Calculate the percentages and bar lengths...
    $divisor = $return['total_votes'] == 0 ? 1 : $return['total_votes'];
    foreach ($sOptions as $i => $option)
    {
        $bar = floor(($option[1] * 100) / $divisor);
        $return['options'][$i] = array(
            'id' => 'options-' . ($topPollInstead ? 'top-' : 'recent-') . $i,
            'percent' => $bar,
            'votes' => $option[1],
            'option' => parse_bbc($option[0]),
            'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . ($topPollInstead ? 'top-' : 'recent-') . $i . '" value="' . $i . '">'
        );
    }

    $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options_limit'], min(count($sOptions), $row['max_votes'])) : '';

    // If mods want to do somthing with this list of polls, let them do that now.
    call_integration_hook('integrate_ssi_recentPoll', array(&$return, $topPollInstead));

    if ($output_method != 'echo')
        return $return;

    if ($allow_view_results)
    {
        echo '
        <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
            <strong>', $return['question'], '</strong><br>
            ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br>' : '';

        foreach ($return['options'] as $option)
            echo '
            <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br>';

        echo '
            <input type="submit" value="', $txt['poll_vote'], '" class="button">
            <input type="hidden" name="poll" value="', $return['id'], '">
            <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
        </form>';
    }
    else
        echo $txt['poll_cannot_see'];
}

/**
 * Shows the poll from the specified topic
 *
 * @param null|int $topic The topic to show the poll from. If null, $_REQUEST['ssi_topic'] will be used instead.
 * @param string $output_method The output method. If 'echo', displays the poll, otherwise returns an array of info about it.
 * @return void|array Either displays the poll or returns an array of info about it, depending on output_method.
 */
function ssi_showPoll($topic = null, $output_method = 'echo')
{
    global $txt, $boardurl, $user_info, $context, $smcFunc, $modSettings;

    $boardsAllowed = boardsAllowedTo('poll_view');

    if (empty($boardsAllowed))
        return array();

    if ($topic === null && isset($_REQUEST['ssi_topic']))
        $topic = (int) $_REQUEST['ssi_topic'];
    else
        $topic = (int) $topic;

    $request = $smcFunc['db_query']('', '
        SELECT
            p.id_poll, p.question, p.voting_locked, p.hide_results, p.expire_time, p.max_votes, p.guest_vote, b.id_board
        FROM {db_prefix}topics AS t
            INNER JOIN {db_prefix}polls AS p ON (p.id_poll = t.id_poll)
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
        WHERE t.id_topic = {int:current_topic}
            AND {query_see_board}' . (!in_array(0, $boardsAllowed) ? '
            AND b.id_board IN ({array_int:boards_allowed_see})' : '') . ($modSettings['postmod_active'] ? '
            AND t.approved = {int:is_approved}' : '') . '
        LIMIT 1',
        array(
            'current_topic' => $topic,
            'boards_allowed_see' => $boardsAllowed,
            'is_approved' => 1,
        )
    );

    // Either this topic has no poll, or the user cannot view it.
    if ($smcFunc['db_num_rows']($request) == 0)
        return array();

    $row = $smcFunc['db_fetch_assoc']($request);
    $smcFunc['db_free_result']($request);

    // Check if they can vote.
    $already_voted = false;
    if (!empty($row['expire_time']) && $row['expire_time'] < time())
        $allow_vote = false;
    elseif ($user_info['is_guest'])
    {
        // There's a difference between "allowed to vote" and "already voted"...
        $allow_vote = $row['guest_vote'];

        // Did you already vote?
        if (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
        {
            $already_voted = true;
        }
    }
    elseif (!empty($row['voting_locked']) || !allowedTo('poll_vote', $row['id_board']))
        $allow_vote = false;
    else
    {
        $request = $smcFunc['db_query']('', '
            SELECT id_member
            FROM {db_prefix}log_polls
            WHERE id_poll = {int:current_poll}
                AND id_member = {int:current_member}
            LIMIT 1',
            array(
                'current_member' => $user_info['id'],
                'current_poll' => $row['id_poll'],
            )
        );
        $allow_vote = $smcFunc['db_num_rows']($request) == 0;
        $already_voted = $allow_vote;
        $smcFunc['db_free_result']($request);
    }

    // Can they view?
    $is_expired = !empty($row['expire_time']) && $row['expire_time'] < time();
    $allow_view_results = allowedTo('moderate_board') || $row['hide_results'] == 0 || ($row['hide_results'] == 1 && $already_voted) || $is_expired;

    $request = $smcFunc['db_query']('', '
        SELECT COUNT(DISTINCT id_member)
        FROM {db_prefix}log_polls
        WHERE id_poll = {int:current_poll}',
        array(
            'current_poll' => $row['id_poll'],
        )
    );
    list ($total) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    $request = $smcFunc['db_query']('', '
        SELECT id_choice, label, votes
        FROM {db_prefix}poll_choices
        WHERE id_poll = {int:current_poll}',
        array(
            'current_poll' => $row['id_poll'],
        )
    );
    $sOptions = array();
    $total_votes = 0;
    while ($rowChoice = $smcFunc['db_fetch_assoc']($request))
    {
        censorText($rowChoice['label']);

        $sOptions[$rowChoice['id_choice']] = array($rowChoice['label'], $rowChoice['votes']);
        $total_votes += $rowChoice['votes'];
    }
    $smcFunc['db_free_result']($request);

    $return = array(
        'id' => $row['id_poll'],
        'image' => empty($row['voting_locked']) ? 'poll' : 'locked_poll',
        'question' => $row['question'],
        'total_votes' => $total,
        'is_locked' => !empty($row['voting_locked']),
        'allow_vote' => $allow_vote,
        'allow_view_results' => $allow_view_results,
        'topic' => $topic
    );

    // Calculate the percentages and bar lengths...
    $divisor = $total_votes == 0 ? 1 : $total_votes;
    foreach ($sOptions as $i => $option)
    {
        $bar = floor(($option[1] * 100) / $divisor);
        $return['options'][$i] = array(
            'id' => 'options-' . $i,
            'percent' => $bar,
            'votes' => $option[1],
            'option' => parse_bbc($option[0]),
            'vote_button' => '<input type="' . ($row['max_votes'] > 1 ? 'checkbox' : 'radio') . '" name="options[]" id="options-' . $i . '" value="' . $i . '">'
        );
    }

    $return['allowed_warning'] = $row['max_votes'] > 1 ? sprintf($txt['poll_options_limit'], min(count($sOptions), $row['max_votes'])) : '';

    // If mods want to do somthing with this poll, let them do that now.
    call_integration_hook('integrate_ssi_showPoll', array(&$return));

    if ($output_method != 'echo')
        return $return;

    if ($return['allow_vote'])
    {
        echo '
            <form class="ssi_poll" action="', $boardurl, '/SSI.php?ssi_function=pollVote" method="post" accept-charset="', $context['character_set'], '">
                <strong>', $return['question'], '</strong><br>
                ', !empty($return['allowed_warning']) ? $return['allowed_warning'] . '<br>' : '';

        foreach ($return['options'] as $option)
            echo '
                <label for="', $option['id'], '">', $option['vote_button'], ' ', $option['option'], '</label><br>';

        echo '
                <input type="submit" value="', $txt['poll_vote'], '" class="button">
                <input type="hidden" name="poll" value="', $return['id'], '">
                <input type="hidden" name="', $context['session_var'], '" value="', $context['session_id'], '">
            </form>';
    }
    else
    {
        echo '
            <div class="ssi_poll">
                <strong>', $return['question'], '</strong>
                <dl>';

        foreach ($return['options'] as $option)
        {
            echo '
                    <dt>', $option['option'], '</dt>
                    <dd>';

            if ($return['allow_view_results'])
            {
                echo '
                        <div class="ssi_poll_bar" style="border: 1px solid #666; height: 1em">
                            <div class="ssi_poll_bar_fill" style="background: #ccf; height: 1em; width: ', $option['percent'], '%;">
                            </div>
                        </div>
                        ', $option['votes'], ' (', $option['percent'], '%)';
            }

            echo '
                    </dd>';
        }

        echo '
                </dl>', ($return['allow_view_results'] ? '
                <strong>' . $txt['poll_total_voters'] . ': ' . $return['total_votes'] . '</strong>' : ''), '
            </div>';
    }
}

/**
 * Handles voting in a poll (done automatically)
 */
function ssi_pollVote()
{
    global $context, $db_prefix, $user_info, $sc, $smcFunc, $sourcedir, $modSettings;

    if (!isset($_POST[$context['session_var']]) || $_POST[$context['session_var']] != $sc || empty($_POST['options']) || !isset($_POST['poll']))
    {
        echo '<!DOCTYPE html>
<html>
<head>
    <script>
        history.go(-1);
    </script>
</head>
<body>&laquo;</body>
</html>';
        return;
    }

    // This can cause weird errors! (ie. copyright missing.)
    checkSession();

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

    // Check if they have already voted, or voting is locked.
    $request = $smcFunc['db_query']('', '
        SELECT
            p.id_poll, p.voting_locked, p.expire_time, p.max_votes, p.guest_vote,
            t.id_topic,
            COALESCE(lp.id_choice, -1) AS selected
        FROM {db_prefix}polls AS p
            INNER JOIN {db_prefix}topics AS t ON (t.id_poll = {int:current_poll})
            INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
            LEFT JOIN {db_prefix}log_polls AS lp ON (lp.id_poll = p.id_poll AND lp.id_member = {int:current_member})
        WHERE p.id_poll = {int:current_poll}
            AND {query_see_board}' . ($modSettings['postmod_active'] ? '
            AND t.approved = {int:is_approved}' : '') . '
        LIMIT 1',
        array(
            'current_member' => $user_info['id'],
            'current_poll' => $_POST['poll'],
            'is_approved' => 1,
        )
    );
    if ($smcFunc['db_num_rows']($request) == 0)
        die;
    $row = $smcFunc['db_fetch_assoc']($request);
    $smcFunc['db_free_result']($request);

    if (!empty($row['voting_locked']) || ($row['selected'] != -1 && !$user_info['is_guest']) || (!empty($row['expire_time']) && time() > $row['expire_time']))
        redirectexit('topic=' . $row['id_topic'] . '.0');

    // Too many options checked?
    if (count($_REQUEST['options']) > $row['max_votes'])
        redirectexit('topic=' . $row['id_topic'] . '.0');

    // It's a guest who has already voted?
    if ($user_info['is_guest'])
    {
        // Guest voting disabled?
        if (!$row['guest_vote'])
            redirectexit('topic=' . $row['id_topic'] . '.0');
        // Already voted?
        elseif (isset($_COOKIE['guest_poll_vote']) && in_array($row['id_poll'], explode(',', $_COOKIE['guest_poll_vote'])))
            redirectexit('topic=' . $row['id_topic'] . '.0');
    }

    $sOptions = array();
    $inserts = array();
    foreach ($_REQUEST['options'] as $id)
    {
        $id = (int) $id;

        $sOptions[] = $id;
        $inserts[] = array($_POST['poll'], $user_info['id'], $id);
    }

    // Add their vote in to the tally.
    $smcFunc['db_insert']('insert',
        $db_prefix . 'log_polls',
        array('id_poll' => 'int', 'id_member' => 'int', 'id_choice' => 'int'),
        $inserts,
        array('id_poll', 'id_member', 'id_choice')
    );
    $smcFunc['db_query']('', '
        UPDATE {db_prefix}poll_choices
        SET votes = votes + 1
        WHERE id_poll = {int:current_poll}
            AND id_choice IN ({array_int:option_list})',
        array(
            'option_list' => $sOptions,
            'current_poll' => $_POST['poll'],
        )
    );

    // Track the vote if a guest.
    if ($user_info['is_guest'])
    {
        $_COOKIE['guest_poll_vote'] = !empty($_COOKIE['guest_poll_vote']) ? ($_COOKIE['guest_poll_vote'] . ',' . $row['id_poll']) : $row['id_poll'];

        require_once($sourcedir . '/Subs-Auth.php');
        $cookie_url = url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies']));
        smf_setcookie('guest_poll_vote', $_COOKIE['guest_poll_vote'], time() + 2500000, $cookie_url[1], $cookie_url[0], false, false);
    }

    redirectexit('topic=' . $row['id_topic'] . '.0');
}

// Show a search box.
/**
 * Shows a search box
 *
 * @param string $output_method The output method. If 'echo', displays a search box, otherwise returns the URL of the search page.
 * @return void|string Displays a search box or returns the URL to the search page depending on output_method. If you don't have permission to search, the function won't return anything.
 */
function ssi_quickSearch($output_method = 'echo')
{
    global $scripturl, $txt, $context;

    if (!allowedTo('search_posts'))
        return;

    if ($output_method != 'echo')
        return $scripturl . '?action=search';

    echo '
        <form action="', $scripturl, '?action=search2" method="post" accept-charset="', $context['character_set'], '">
            <input type="hidden" name="advanced" value="0"><input type="text" name="ssi_search" size="30"> <input type="submit" value="', $txt['search'], '" class="button">
        </form>';
}

/**
 * Show a random forum news item
 *
 * @param string $output_method The output method. If 'echo', shows the news item, otherwise returns it.
 * @return void|string Shows or returns a random forum news item, depending on output_method.
 */
function ssi_news($output_method = 'echo')
{
    global $context;

    $context['random_news_line'] = !empty($context['news_lines']) ? $context['news_lines'][mt_rand(0, count($context['news_lines']) - 1)] : '';

    // If mods want to do somthing with the news, let them do that now. Don't need to pass the news line itself, since it is already in $context.
    call_integration_hook('integrate_ssi_news');

    if ($output_method != 'echo')
        return $context['random_news_line'];

    echo $context['random_news_line'];
}

/**
 * Show today's birthdays.
 *
 * @param string $output_method The output method. If 'echo', displays a list of users, otherwise returns an array of info about them.
 * @return void|array Displays a list of users or returns an array of info about them depending on output_method.
 */
function ssi_todaysBirthdays($output_method = 'echo')
{
    global $scripturl, $modSettings, $user_info;

    if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view') || !allowedTo('profile_view'))
        return;

    $eventOptions = array(
        'include_birthdays' => true,
        'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
    );
    $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));

    // The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
    call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));

    if ($output_method != 'echo')
        return $return['calendar_birthdays'];

    foreach ($return['calendar_birthdays'] as $member)
        echo '
            <a href="', $scripturl, '?action=profile;u=', $member['id'], '"><span class="fix_rtl_names">' . $member['name'] . '</span>' . (isset($member['age']) ? ' (' . $member['age'] . ')' : '') . '</a>' . (!$member['is_last'] ? ', ' : '');
}

/**
 * Shows today's holidays.
 *
 * @param string $output_method The output method. If 'echo', displays a list of holidays, otherwise returns an array of info about them.
 * @return void|array Displays a list of holidays or returns an array of info about them depending on output_method
 */
function ssi_todaysHolidays($output_method = 'echo')
{
    global $modSettings, $user_info;

    if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
        return;

    $eventOptions = array(
        'include_holidays' => true,
        'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
    );
    $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));

    // The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
    call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));

    if ($output_method != 'echo')
        return $return['calendar_holidays'];

    echo '
        ', implode(', ', $return['calendar_holidays']);
}

/**
 * @param string $output_method The output method. If 'echo', displays a list of events, otherwise returns an array of info about them.
 * @return void|array Displays a list of events or returns an array of info about them depending on output_method
 */
function ssi_todaysEvents($output_method = 'echo')
{
    global $modSettings, $user_info;

    if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
        return;

    $eventOptions = array(
        'include_events' => true,
        'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
    );
    $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));

    // The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
    call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));

    if ($output_method != 'echo')
        return $return['calendar_events'];

    foreach ($return['calendar_events'] as $event)
    {
        if ($event['can_edit'])
            echo '
    <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
        echo '
    ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
    }
}

/**
 * Shows today's calendar items (events, birthdays and holidays)
 *
 * @param string $output_method The output method. If 'echo', displays a list of calendar items, otherwise returns an array of info about them.
 * @return void|array Displays a list of calendar items or returns an array of info about them depending on output_method
 */
function ssi_todaysCalendar($output_method = 'echo')
{
    global $modSettings, $txt, $scripturl, $user_info;

    if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
        return;

    $eventOptions = array(
        'include_birthdays' => allowedTo('profile_view'),
        'include_holidays' => true,
        'include_events' => true,
        'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
    );
    $return = cache_quick_get('calendar_index_offset_' . ($user_info['time_offset'] + $modSettings['time_offset']), 'Subs-Calendar.php', 'cache_getRecentEvents', array($eventOptions));

    // The ssi_todaysCalendar variants all use the same hook and just pass on $eventOptions so the hooked code can distinguish different cases if necessary
    call_integration_hook('integrate_ssi_calendar', array(&$return, $eventOptions));

    if ($output_method != 'echo')
        return $return;

    if (!empty($return['calendar_holidays']))
        echo '
            <span class="holiday">' . $txt['calendar_prompt'] . ' ' . implode(', ', $return['calendar_holidays']) . '<br></span>';
    if (!empty($return['calendar_birthdays']))
    {
        echo '
            <span class="birthday">' . $txt['birthdays_upcoming'] . '</span> ';
        foreach ($return['calendar_birthdays'] as $member)
            echo '
            <a href="', $scripturl, '?action=profile;u=', $member['id'], '"><span class="fix_rtl_names">', $member['name'], '</span>', isset($member['age']) ? ' (' . $member['age'] . ')' : '', '</a>', !$member['is_last'] ? ', ' : '';
        echo '
            <br>';
    }
    if (!empty($return['calendar_events']))
    {
        echo '
            <span class="event">' . $txt['events_upcoming'] . '</span> ';
        foreach ($return['calendar_events'] as $event)
        {
            if ($event['can_edit'])
                echo '
            <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';
            echo '
            ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
        }
    }
}

/**
 * Show the latest news, with a template... by board.
 *
 * @param null|int $board The ID of the board to get the info from. Defaults to $board or $_GET['board'] if not set.
 * @param null|int $limit How many items to show. Defaults to $_GET['limit'] or 5 if not set.
 * @param null|int $start Start with the specified item. Defaults to $_GET['start'] or 0 if not set.
 * @param null|int $length How many characters to show from each post. Defaults to $_GET['length'] or 0 (no limit) if not set.
 * @param string $output_method The output method. If 'echo', displays the news items, otherwise returns an array of info about them.
 * @return void|array Displays the news items or returns an array of info about them, depending on output_method.
 */
function ssi_boardNews($board = null, $limit = null, $start = null, $length = null, $output_method = 'echo')
{
    global $scripturl, $txt, $settings, $modSettings, $context;
    global $smcFunc;

    loadLanguage('Stats');

    // Must be integers....
    if ($limit === null)
        $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
    else
        $limit = (int) $limit;

    if ($start === null)
        $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
    else
        $start = (int) $start;

    if ($board !== null)
        $board = (int) $board;
    elseif (isset($_GET['board']))
        $board = (int) $_GET['board'];

    if ($length === null)
        $length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
    else
        $length = (int) $length;

    $limit = max(0, $limit);
    $start = max(0, $start);

    // Make sure guests can see this board.
    $request = $smcFunc['db_query']('', '
        SELECT id_board
        FROM {db_prefix}boards
        WHERE ' . ($board === null ? '' : 'id_board = {int:current_board}
            AND ') . 'FIND_IN_SET(-1, member_groups) != 0
        LIMIT 1',
        array(
            'current_board' => $board,
        )
    );
    if ($smcFunc['db_num_rows']($request) == 0)
    {
        if ($output_method == 'echo')
            die($txt['ssi_no_guests']);
        else
            return array();
    }
    list ($board) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    $icon_sources = array();
    foreach ($context['stable_icons'] as $icon)
        $icon_sources[$icon] = 'images_url';

    if (!empty($modSettings['enable_likes']))
    {
        $context['can_like'] = allowedTo('likes_like');
    }

    // Find the post ids.
    $request = $smcFunc['db_query']('', '
        SELECT t.id_first_msg
        FROM {db_prefix}topics as t
            LEFT JOIN {db_prefix}boards as b ON (b.id_board = t.id_board)
        WHERE t.id_board = {int:current_board}' . ($modSettings['postmod_active'] ? '
            AND t.approved = {int:is_approved}' : '') . '
            AND {query_see_board}
        ORDER BY t.id_first_msg DESC
        LIMIT ' . $start . ', ' . $limit,
        array(
            'current_board' => $board,
            'is_approved' => 1,
        )
    );
    $posts = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $posts[] = $row['id_first_msg'];
    $smcFunc['db_free_result']($request);

    if (empty($posts))
        return array();

    // Find the posts.
    $request = $smcFunc['db_query']('', '
        SELECT
            m.icon, m.subject, m.body, COALESCE(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.likes,
            t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, m.id_msg, t.locked, t.id_last_msg, m.id_board
        FROM {db_prefix}topics AS t
            INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
        WHERE t.id_first_msg IN ({array_int:post_list})
        ORDER BY t.id_first_msg DESC
        LIMIT ' . count($posts),
        array(
            'post_list' => $posts,
        )
    );
    $return = array();
    $recycle_board = !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) ? (int) $modSettings['recycle_board'] : 0;
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        // If we want to limit the length of the post.
        if (!empty($length) && $smcFunc['strlen']($row['body']) > $length)
        {
            $row['body'] = $smcFunc['substr']($row['body'], 0, $length);
            $cutoff = false;

            $last_space = strrpos($row['body'], ' ');
            $last_open = strrpos($row['body'], '<');
            $last_close = strrpos($row['body'], '>');
            if (empty($last_space) || ($last_space == $last_open + 3 && (empty($last_close) || (!empty($last_close) && $last_close < $last_open))) || $last_space < $last_open || $last_open == $length - 6)
                $cutoff = $last_open;
            elseif (empty($last_close) || $last_close < $last_open)
                $cutoff = $last_space;

            if ($cutoff !== false)
                $row['body'] = $smcFunc['substr']($row['body'], 0, $cutoff);
            $row['body'] .= '...';
        }

        $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);

        if (!empty($recycle_board) && $row['id_board'] == $recycle_board)
            $row['icon'] = 'recycled';

        // Check that this message icon is there...
        if (!empty($modSettings['messageIconChecks_enable']) && !isset($icon_sources[$row['icon']]))
            $icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.png') ? 'images_url' : 'default_images_url';
        elseif (!isset($icon_sources[$row['icon']]))
            $icon_sources[$row['icon']] = 'images_url';

        censorText($row['subject']);
        censorText($row['body']);

        $return[] = array(
            'id' => $row['id_topic'],
            'message_id' => $row['id_msg'],
            'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.png" alt="' . $row['icon'] . '">',
            'subject' => $row['subject'],
            'time' => timeformat($row['poster_time']),
            'timestamp' => forum_time(true, $row['poster_time']),
            'body' => $row['body'],
            'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
            'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>',
            'replies' => $row['num_replies'],
            'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'],
            'comment_link' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';last_msg=' . $row['id_last_msg'] . '">' . $txt['ssi_write_comment'] . '</a>',
            'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>',
            'poster' => array(
                'id' => $row['id_member'],
                'name' => $row['poster_name'],
                'href' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
                'link' => !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name']
            ),
            'locked' => !empty($row['locked']),
            'is_last' => false,
            // Nasty ternary for likes not messing around the "is_last" check.
            'likes' => !empty($modSettings['enable_likes']) ? array(
                'count' => $row['likes'],
                'you' => in_array($row['id_msg'], prepareLikesContext((int) $row['id_topic'])),
                'can_like' => !$context['user']['is_guest'] && $row['id_member'] != $context['user']['id'] && !empty($context['can_like']),
            ) : array(),
        );
    }
    $smcFunc['db_free_result']($request);

    if (empty($return))
        return $return;

    $return[count($return) - 1]['is_last'] = true;

    // If mods want to do somthing with this list of posts, let them do that now.
    call_integration_hook('integrate_ssi_boardNews', array(&$return));

    if ($output_method != 'echo')
        return $return;

    foreach ($return as $news)
    {
        echo '
            <div class="news_item">
                <h3 class="news_header">
                    ', $news['icon'], '
                    <a href="', $news['href'], '">', $news['subject'], '</a>
                </h3>
                <div class="news_timestamp">', $news['time'], ' ', $txt['by'], ' ', $news['poster']['link'], '</div>
                <div class="news_body" style="padding: 2ex 0;">', $news['body'], '</div>
                ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '';

        // Is there any likes to show?
        if (!empty($modSettings['enable_likes']))
        {
            echo '
                    <ul>';

            if (!empty($news['likes']['can_like']))
            {
                echo '
                        <li class="smflikebutton" id="msg_', $news['message_id'], '_likes"><a href="', $scripturl, '?action=likes;ltype=msg;sa=like;like=', $news['message_id'], ';', $context['session_var'], '=', $context['session_id'], '" class="msg_like"><span class="', $news['likes']['you'] ? 'unlike' : 'like', '"></span>', $news['likes']['you'] ? $txt['unlike'] : $txt['like'], '</a></li>';
            }

            if (!empty($news['likes']['count']))
            {
                $context['some_likes'] = true;
                $count = $news['likes']['count'];
                $base = 'likes_';
                if ($news['likes']['you'])
                {
                    $base = 'you_' . $base;
                    $count--;
                }
                $base .= (isset($txt[$base . $count])) ? $count : 'n';

                echo '
                        <li class="like_count smalltext">', sprintf($txt[$base], $scripturl . '?action=likes;sa=view;ltype=msg;like=' . $news['message_id'] . ';' . $context['session_var'] . '=' . $context['session_id'], comma_format($count)), '</li>';
            }

            echo '
                    </ul>';
        }

        // Close the main div.
        echo '
            </div>';

        if (!$news['is_last'])
            echo '
            <hr>';
    }
}

/**
 * Show the most recent events
 *
 * @param int $max_events The maximum number of events to show
 * @param string $output_method The output method. If 'echo', displays the events, otherwise returns an array of info about them.
 * @return void|array Displays the events or returns an array of info about them, depending on output_method.
 */
function ssi_recentEvents($max_events = 7, $output_method = 'echo')
{
    global $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;

    if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
        return;

    // Find all events which are happening in the near future that the member can see.
    $request = $smcFunc['db_query']('', '
        SELECT
            cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
            cal.start_time, cal.end_time, cal.timezone, cal.location,
            cal.id_board, t.id_first_msg, t.approved
        FROM {db_prefix}calendar AS cal
            LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
            LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
        WHERE cal.start_date <= {date:current_date}
            AND cal.end_date >= {date:current_date}
            AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
        ORDER BY cal.start_date DESC
        LIMIT ' . $max_events,
        array(
            'current_date' => strftime('%Y-%m-%d', forum_time(false)),
            'no_board' => 0,
        )
    );
    $return = array();
    $duplicates = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        // Check if we've already come by an event linked to this same topic with the same title... and don't display it if we have.
        if (!empty($duplicates[$row['title'] . $row['id_topic']]))
            continue;

        // Censor the title.
        censorText($row['title']);

        if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
            $date = strftime('%Y-%m-%d', forum_time(false));
        else
            $date = $row['start_date'];

        // If the topic it is attached to is not approved then don't link it.
        if (!empty($row['id_first_msg']) && !$row['approved'])
            $row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;

        $allday = (empty($row['start_time']) || empty($row['end_time']) || empty($row['timezone']) || !in_array($row['timezone'], timezone_identifiers_list(DateTimeZone::ALL_WITH_BC))) ? true : false;

        $return[$date][] = array(
            'id' => $row['id_event'],
            'title' => $row['title'],
            'location' => $row['location'],
            'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
            'modify_href' => $scripturl . '?action=' . ($row['id_board'] == 0 ? 'calendar;sa=post;' : 'post;msg=' . $row['id_first_msg'] . ';topic=' . $row['id_topic'] . '.0;calendar;') . 'eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'],
            'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
            'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
            'start_date' => $row['start_date'],
            'end_date' => $row['end_date'],
            'start_time' => !$allday ? $row['start_time'] : null,
            'end_time' => !$allday ? $row['end_time'] : null,
            'tz' => !$allday ? $row['timezone'] : null,
            'allday' => $allday,
            'is_last' => false
        );

        // Let's not show this one again, huh?
        $duplicates[$row['title'] . $row['id_topic']] = true;
    }
    $smcFunc['db_free_result']($request);

    foreach ($return as $mday => $array)
        $return[$mday][count($array) - 1]['is_last'] = true;

    // If mods want to do somthing with this list of events, let them do that now.
    call_integration_hook('integrate_ssi_recentEvents', array(&$return));

    if ($output_method != 'echo' || empty($return))
        return $return;

    // Well the output method is echo.
    echo '
            <span class="event">' . $txt['events'] . '</span> ';
    foreach ($return as $mday => $array)
        foreach ($array as $event)
        {
            if ($event['can_edit'])
                echo '
                <a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';

            echo '
                ' . $event['link'] . (!$event['is_last'] ? ', ' : '');
        }
}

/**
 * Checks whether the specified password is correct for the specified user.
 *
 * @param int|string $id The ID or username of a user
 * @param string $password The password to check
 * @param bool $is_username If true, treats $id as a username rather than a user ID
 * @return bool Whether or not the password is correct.
 */
function ssi_checkPassword($id = null, $password = null, $is_username = false)
{
    global $smcFunc;

    // If $id is null, this was most likely called from a query string and should do nothing.
    if ($id === null)
        return;

    $request = $smcFunc['db_query']('', '
        SELECT passwd, member_name, is_activated
        FROM {db_prefix}members
        WHERE ' . ($is_username ? 'member_name' : 'id_member') . ' = {string:id}
        LIMIT 1',
        array(
            'id' => $id,
        )
    );
    list ($pass, $user, $active) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    return hash_verify_password($user, $password, $pass) && $active == 1;
}

/**
 * Shows the most recent attachments that the user can see
 *
 * @param int $num_attachments How many to show
 * @param array $attachment_ext Only shows attachments with the specified extensions ('jpg', 'gif', etc.) if set
 * @param string $output_method The output method. If 'echo', displays a table with links/info, otherwise returns an array with information about the attachments
 * @return void|array Displays a table of attachment info or returns an array containing info about the attachments, depending on output_method.
 */
function ssi_recentAttachments($num_attachments = 10, $attachment_ext = array(), $output_method = 'echo')
{
    global $smcFunc, $modSettings, $scripturl, $txt, $settings;

    // We want to make sure that we only get attachments for boards that we can see *if* any.
    $attachments_boards = boardsAllowedTo('view_attachments');

    // No boards?  Adios amigo.
    if (empty($attachments_boards))
        return array();

    // Is it an array?
    $attachment_ext = (array) $attachment_ext;

    // Lets build the query.
    $request = $smcFunc['db_query']('', '
        SELECT
            att.id_attach, att.id_msg, att.filename, COALESCE(att.size, 0) AS filesize, att.downloads, mem.id_member,
            COALESCE(mem.real_name, m.poster_name) AS poster_name, m.id_topic, m.subject, t.id_board, m.poster_time,
            att.width, att.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . '
        FROM {db_prefix}attachments AS att
            INNER JOIN {db_prefix}messages AS m ON (m.id_msg = att.id_msg)
            INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : '
            LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = att.id_thumb)') . '
        WHERE att.attachment_type = 0' . ($attachments_boards === array(0) ? '' : '
            AND m.id_board IN ({array_int:boards_can_see})') . (!empty($attachment_ext) ? '
            AND att.fileext IN ({array_string:attachment_ext})' : '') .
            (!$modSettings['postmod_active'] || allowedTo('approve_posts') ? '' : '
            AND t.approved = {int:is_approved}
            AND m.approved = {int:is_approved}
            AND att.approved = {int:is_approved}') . '
        ORDER BY att.id_attach DESC
        LIMIT {int:num_attachments}',
        array(
            'boards_can_see' => $attachments_boards,
            'attachment_ext' => $attachment_ext,
            'num_attachments' => $num_attachments,
            'is_approved' => 1,
        )
    );

    // We have something.
    $attachments = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $filename = preg_replace('~&amp;#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', htmlspecialchars($row['filename']));

        // Is it an image?
        $attachments[$row['id_attach']] = array(
            'member' => array(
                'id' => $row['id_member'],
                'name' => $row['poster_name'],
                'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>',
            ),
            'file' => array(
                'filename' => $filename,
                'filesize' => round($row['filesize'] / 1024, 2) . $txt['kilobyte'],
                'downloads' => $row['downloads'],
                'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'],
                'link' => '<img src="' . $settings['images_url'] . '/icons/clip.png" alt=""> <a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . '">' . $filename . '</a>',
                'is_image' => !empty($row['width']) && !empty($row['height']) && !empty($modSettings['attachmentShowImages']),
            ),
            'topic' => array(
                'id' => $row['id_topic'],
                'subject' => $row['subject'],
                'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
                'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>',
                'time' => timeformat($row['poster_time']),
            ),
        );

        // Images.
        if ($attachments[$row['id_attach']]['file']['is_image'])
        {
            $id_thumb = empty($row['id_thumb']) ? $row['id_attach'] : $row['id_thumb'];
            $attachments[$row['id_attach']]['file']['image'] = array(
                'id' => $id_thumb,
                'width' => $row['width'],
                'height' => $row['height'],
                'img' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image" alt="' . $filename . '">',
                'thumb' => '<img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '">',
                'href' => $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image',
                'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $row['id_attach'] . ';image"><img src="' . $scripturl . '?action=dlattach;topic=' . $row['id_topic'] . '.0;attach=' . $id_thumb . ';image" alt="' . $filename . '"></a>',
            );
        }
    }
    $smcFunc['db_free_result']($request);

    // If mods want to do somthing with this list of attachments, let them do that now.
    call_integration_hook('integrate_ssi_recentAttachments', array(&$attachments));

    // So you just want an array?  Here you can have it.
    if ($output_method == 'array' || empty($attachments))
        return $attachments;

    // Give them the default.
    echo '
        <table class="ssi_downloads">
            <tr>
                <th style="text-align: left; padding: 2">', $txt['file'], '</th>
                <th style="text-align: left; padding: 2">', $txt['posted_by'], '</th>
                <th style="text-align: left; padding: 2">', $txt['downloads'], '</th>
                <th style="text-align: left; padding: 2">', $txt['filesize'], '</th>
            </tr>';
    foreach ($attachments as $attach)
        echo '
            <tr>
                <td>', $attach['file']['link'], '</td>
                <td>', $attach['member']['link'], '</td>
                <td style="text-align: center">', $attach['file']['downloads'], '</td>
                <td>', $attach['file']['filesize'], '</td>
            </tr>';
    echo '
        </table>';
}

?>