1:    2:    3:    4:    5:    6:    7:    8:    9:   10:   11:   12:   13:   14:   15:   16:   17:   18:   19:   20:   21:   22:   23:   24:   25:   26:   27:   28:   29:   30:   31:   32:   33:   34:   35:   36:   37:   38:   39:   40:   41:   42:   43:   44:   45:   46:   47:   48:   49:   50:   51:   52:   53:   54:   55:   56:   57:   58:   59:   60:   61:   62:   63:   64:   65:   66:   67:   68:   69:   70:   71:   72:   73:   74:   75:   76:   77:   78:   79:   80:   81:   82:   83:   84:   85:   86:   87:   88:   89:   90:   91:   92:   93:   94:   95:   96:   97:   98:   99:  100:  101:  102:  103:  104:  105:  106:  107:  108:  109:  110:  111:  112:  113:  114:  115:  116:  117:  118:  119:  120:  121:  122:  123:  124:  125:  126:  127:  128:  129:  130:  131:  132:  133:  134:  135:  136:  137:  138:  139:  140:  141:  142:  143:  144:  145:  146:  147:  148:  149:  150:  151:  152:  153:  154:  155:  156:  157:  158:  159:  160:  161:  162:  163:  164:  165:  166:  167:  168:  169:  170:  171:  172:  173:  174:  175:  176:  177:  178:  179:  180:  181:  182:  183:  184:  185:  186:  187:  188:  189:  190:  191:  192:  193:  194:  195:  196:  197:  198:  199:  200:  201:  202:  203:  204:  205:  206:  207:  208:  209:  210:  211:  212:  213:  214:  215:  216:  217:  218:  219:  220:  221:  222:  223:  224:  225:  226:  227:  228:  229:  230:  231:  232:  233:  234:  235:  236:  237:  238:  239:  240:  241:  242:  243:  244:  245:  246:  247:  248:  249:  250:  251:  252:  253:  254:  255:  256:  257:  258:  259:  260:  261:  262:  263:  264:  265:  266:  267:  268:  269:  270:  271:  272:  273:  274:  275:  276:  277:  278:  279:  280:  281:  282:  283:  284:  285:  286:  287:  288:  289:  290:  291:  292:  293:  294:  295:  296:  297:  298:  299:  300:  301:  302:  303:  304:  305:  306:  307:  308:  309:  310:  311:  312:  313:  314:  315:  316:  317:  318:  319:  320:  321:  322:  323:  324:  325:  326:  327:  328:  329:  330:  331:  332:  333:  334:  335:  336:  337:  338:  339:  340:  341:  342:  343:  344:  345:  346:  347:  348:  349:  350:  351:  352:  353:  354:  355:  356:  357:  358:  359:  360:  361:  362:  363:  364:  365:  366:  367:  368:  369:  370:  371:  372:  373:  374:  375:  376:  377:  378:  379:  380:  381:  382:  383:  384:  385:  386:  387:  388:  389:  390:  391:  392:  393:  394:  395:  396:  397:  398:  399:  400:  401:  402:  403:  404:  405:  406:  407:  408:  409:  410:  411:  412:  413:  414:  415:  416:  417:  418:  419:  420:  421:  422:  423:  424:  425:  426:  427:  428:  429:  430:  431:  432:  433:  434:  435:  436:  437:  438:  439:  440:  441:  442:  443:  444:  445:  446:  447:  448:  449:  450:  451:  452:  453:  454:  455:  456:  457:  458:  459:  460:  461:  462:  463:  464:  465:  466:  467:  468:  469:  470:  471:  472:  473:  474:  475:  476:  477:  478:  479:  480:  481:  482:  483:  484:  485:  486:  487:  488:  489:  490:  491:  492:  493:  494:  495:  496:  497:  498:  499:  500:  501:  502:  503:  504:  505:  506:  507:  508:  509:  510:  511:  512:  513:  514:  515:  516:  517:  518:  519:  520:  521:  522:  523:  524:  525:  526:  527:  528:  529:  530:  531:  532:  533:  534:  535:  536:  537:  538:  539:  540:  541:  542:  543:  544:  545:  546:  547:  548:  549:  550:  551:  552:  553:  554:  555:  556:  557:  558:  559:  560:  561:  562:  563:  564:  565:  566:  567:  568:  569:  570:  571:  572:  573:  574:  575:  576:  577:  578:  579:  580:  581:  582:  583:  584:  585:  586:  587:  588:  589:  590:  591:  592:  593:  594:  595:  596:  597:  598:  599:  600:  601:  602:  603:  604:  605:  606:  607:  608:  609:  610:  611:  612:  613:  614:  615:  616:  617:  618:  619:  620:  621:  622:  623:  624:  625:  626:  627:  628:  629:  630:  631:  632:  633:  634:  635:  636:  637:  638:  639:  640:  641:  642:  643:  644:  645:  646:  647:  648:  649:  650:  651:  652:  653:  654:  655:  656:  657:  658:  659:  660:  661:  662:  663:  664:  665:  666:  667:  668:  669:  670:  671:  672:  673:  674:  675:  676:  677:  678:  679:  680:  681:  682:  683:  684:  685:  686:  687:  688:  689:  690:  691:  692:  693:  694:  695:  696:  697:  698:  699:  700:  701:  702:  703:  704:  705:  706:  707:  708:  709:  710:  711:  712:  713:  714:  715:  716:  717:  718:  719:  720:  721:  722:  723:  724:  725:  726:  727:  728:  729:  730:  731:  732:  733:  734:  735:  736:  737:  738:  739:  740:  741:  742:  743:  744:  745:  746:  747:  748:  749:  750:  751:  752:  753:  754:  755:  756:  757:  758:  759:  760:  761:  762:  763:  764:  765:  766:  767:  768:  769:  770:  771:  772:  773:  774:  775:  776:  777:  778:  779:  780:  781:  782:  783:  784:  785:  786:  787:  788:  789:  790:  791:  792:  793:  794:  795:  796:  797:  798:  799:  800:  801:  802:  803:  804:  805:  806:  807:  808:  809:  810:  811:  812:  813:  814:  815:  816:  817:  818:  819:  820:  821:  822:  823:  824:  825:  826:  827:  828:  829:  830:  831:  832:  833:  834:  835:  836:  837:  838:  839:  840:  841:  842:  843:  844:  845:  846:  847:  848:  849:  850:  851:  852:  853:  854:  855:  856:  857:  858:  859:  860:  861:  862:  863:  864:  865:  866:  867:  868:  869:  870:  871:  872:  873:  874:  875:  876:  877:  878:  879:  880:  881:  882:  883:  884:  885:  886:  887:  888:  889:  890:  891:  892:  893:  894:  895:  896:  897:  898:  899:  900:  901:  902:  903:  904:  905:  906:  907:  908:  909:  910:  911:  912:  913:  914:  915:  916:  917:  918:  919:  920:  921:  922:  923:  924:  925:  926:  927:  928:  929:  930:  931:  932:  933:  934:  935:  936:  937:  938:  939:  940:  941:  942:  943:  944:  945:  946:  947:  948:  949:  950:  951:  952:  953:  954:  955:  956:  957:  958:  959:  960:  961:  962:  963:  964:  965:  966:  967:  968:  969:  970:  971:  972:  973:  974:  975:  976:  977:  978:  979:  980:  981:  982:  983:  984:  985:  986:  987:  988:  989:  990:  991:  992:  993:  994:  995:  996:  997:  998:  999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030: 1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052: 1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 1085: 1086: 1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101: 1102: 1103: 1104: 1105: 1106: 1107: 1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149: 1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 1192: 1193: 1194: 1195: 1196: 1197: 1198: 1199: 1200: 1201: 1202: 1203: 1204: 1205: 1206: 1207: 1208: 1209: 1210: 1211: 1212: 1213: 1214: 1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229: 1230: 1231: 1232: 1233: 1234: 1235: 1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 1249: 1250: 1251: 1252: 1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270: 1271: 1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279: 1280: 1281: 1282: 1283: 1284: 1285: 1286: 1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302: 1303: 1304: 1305: 1306: 1307: 1308: 1309: 1310: 1311: 1312: 1313: 1314: 1315: 1316: 1317: 1318: 1319: 1320: 1321: 1322: 1323: 1324: 1325: 1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342: 1343: 1344: 1345: 1346: 1347: 1348: 1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360: 1361: 1362: 1363: 1364: 1365: 1366: 1367: 1368: 1369: 1370: 1371: 1372: 1373: 1374: 1375: 1376: 1377: 1378: 1379: 1380: 1381: 1382: 1383: 1384: 1385: 1386: 1387: 1388: 1389: 1390: 1391: 1392: 1393: 1394: 1395: 1396: 1397: 1398: 1399: 1400: 1401: 1402: 1403: 1404: 1405: 1406: 1407: 1408: 1409: 1410: 1411: 1412: 1413: 1414: 1415: 1416: 1417: 1418: 1419: 1420: 1421: 1422: 1423: 1424: 1425: 1426: 1427: 1428: 1429: 1430: 1431: 1432: 1433: 1434: 1435: 1436: 1437: 1438: 1439: 1440: 1441: 1442: 1443: 1444: 1445: 1446: 1447: 1448: 1449: 1450: 1451: 1452: 1453: 1454: 1455: 1456: 1457: 1458: 1459: 1460: 1461: 1462: 1463: 1464: 1465: 1466: 1467: 1468: 1469: 1470: 1471: 1472: 1473: 1474: 1475: 1476: 1477: 1478: 1479: 1480: 1481: 1482: 1483: 1484: 1485: 1486: 1487: 1488: 1489: 1490: 1491: 1492: 1493: 1494: 1495: 1496: 1497: 1498: 1499: 1500: 1501: 1502: 1503: 1504: 1505: 1506: 1507: 1508: 1509: 1510: 1511: 1512: 1513: 1514: 1515: 1516: 1517: 1518: 1519: 1520: 1521: 1522: 1523: 1524: 1525: 1526: 1527: 1528: 1529: 1530: 1531: 1532: 1533: 1534: 1535: 1536: 1537: 1538: 1539: 1540: 1541: 1542: 1543: 1544: 1545: 1546: 1547: 1548: 1549: 1550: 1551: 1552: 1553: 1554: 1555: 1556: 1557: 1558: 1559: 1560: 1561: 1562: 1563: 1564: 1565: 1566: 1567: 1568: 1569: 1570: 1571: 1572: 1573: 1574: 1575: 1576: 1577: 1578: 1579: 1580: 1581: 1582: 1583: 1584: 1585: 1586: 1587: 1588: 1589: 1590: 1591: 1592: 1593: 1594: 1595: 1596: 1597: 1598: 1599: 1600: 1601: 1602: 1603: 1604: 1605: 1606: 1607: 1608: 1609: 1610: 1611: 1612: 1613: 1614: 1615: 1616: 1617: 1618: 1619: 1620: 1621: 1622: 1623: 1624: 1625: 1626: 1627: 1628: 1629: 1630: 1631: 1632: 1633: 1634: 1635: 1636: 1637: 1638: 1639: 1640: 1641: 1642: 1643: 1644: 1645: 1646: 1647: 1648: 1649: 1650: 1651: 1652: 1653: 1654: 1655: 1656: 1657: 1658: 1659: 1660: 1661: 1662: 1663: 1664: 1665: 1666: 1667: 1668: 1669: 1670: 1671: 1672: 1673: 1674: 1675: 1676: 1677: 1678: 1679: 1680: 1681: 1682: 1683: 1684: 1685: 1686: 1687: 1688: 1689: 1690: 1691: 1692: 1693: 1694: 1695: 1696: 1697: 1698: 1699: 1700: 1701: 1702: 1703: 1704: 1705: 1706: 1707: 1708: 1709: 1710: 1711: 1712: 1713: 1714: 1715: 1716: 1717: 1718: 1719: 1720: 1721: 1722: 1723: 1724: 1725: 1726: 1727: 1728: 1729: 1730: 1731: 1732: 1733: 1734: 1735: 1736: 1737: 1738: 1739: 1740: 1741: 1742: 1743: 1744: 1745: 1746: 1747: 1748: 1749: 1750: 1751: 1752: 1753: 1754: 1755: 1756: 1757: 1758: 1759: 1760: 1761: 1762: 1763: 1764: 1765: 1766: 1767: 1768: 1769: 1770: 1771: 1772: 1773: 1774: 1775: 1776: 1777: 1778: 1779: 1780: 1781: 1782: 1783: 1784: 1785: 1786: 1787: 1788: 1789: 1790: 1791: 1792: 1793: 1794: 1795: 1796: 1797: 1798: 1799: 1800: 1801: 1802: 1803: 1804: 1805: 1806: 1807: 1808: 1809: 1810: 1811: 1812: 1813: 1814: 1815: 1816: 1817: 1818: 1819: 1820: 1821: 1822: 1823: 1824: 1825: 1826: 1827: 1828: 1829: 1830: 1831: 1832: 1833: 1834: 1835: 1836: 1837: 1838: 1839: 1840: 1841: 1842: 1843: 1844: 1845: 1846: 1847: 1848: 1849: 1850: 1851: 1852: 1853: 1854: 1855: 1856: 1857: 1858: 1859: 1860: 1861: 1862: 1863: 1864: 1865: 1866: 1867: 1868: 1869: 1870: 1871: 1872: 1873: 1874: 1875: 1876: 1877: 1878: 1879: 1880: 1881: 1882: 1883: 1884: 1885: 1886: 1887: 1888: 1889: 1890: 1891: 1892: 1893: 1894: 1895: 1896: 1897: 1898: 1899: 1900: 1901: 1902: 1903: 1904: 1905: 1906: 1907: 1908: 1909: 1910: 1911: 1912: 1913: 1914: 1915: 1916: 1917: 1918: 1919: 1920: 1921: 1922: 1923: 1924: 1925: 1926: 1927: 1928: 1929: 1930: 1931: 1932: 1933: 1934: 1935: 1936: 1937: 1938: 1939: 1940: 1941: 1942: 1943: 1944: 1945: 1946: 1947: 1948: 1949: 1950: 1951: 1952: 1953: 1954: 1955: 1956: 1957: 1958: 1959: 1960: 1961: 1962: 1963: 1964: 1965: 1966: 1967: 1968: 1969: 1970: 1971: 1972: 1973: 1974: 1975: 1976: 1977: 1978: 1979: 1980: 1981: 1982: 1983: 1984: 1985: 1986: 1987: 1988: 1989: 1990: 1991: 1992: 1993: 1994: 1995: 1996: 1997: 1998: 1999: 2000: 2001: 2002: 2003: 2004: 2005: 2006: 2007: 2008: 2009: 2010: 2011: 2012: 2013: 2014: 2015: 2016: 2017: 2018: 2019: 2020: 2021: 2022: 2023: 2024: 2025: 2026: 2027: 2028: 2029: 2030: 2031: 2032: 2033: 2034: 2035: 2036: 2037: 2038: 2039: 2040: 2041: 2042: 2043: 2044: 2045: 2046: 2047: 2048: 2049: 2050: 2051: 2052: 2053: 2054: 2055: 2056: 2057: 2058: 2059: 2060: 2061: 2062: 2063: 2064: 2065: 2066: 2067: 2068: 2069: 2070: 2071: 2072: 2073: 2074: 2075: 2076: 2077: 2078: 2079: 2080: 2081: 2082: 2083: 2084: 2085: 2086: 2087: 2088: 2089: 2090: 2091: 2092: 2093: 2094: 2095: 2096: 2097: 2098: 2099: 2100: 2101: 2102: 2103: 2104: 2105: 2106: 2107: 2108: 2109: 2110: 2111: 2112: 2113: 2114: 2115: 2116: 2117: 2118: 2119: 2120: 2121: 2122: 2123: 2124: 2125: 2126: 2127: 2128: 2129: 2130: 2131: 2132: 2133: 2134: 2135: 2136: 2137: 2138: 2139: 2140: 2141: 2142: 2143: 2144: 2145: 2146: 2147: 2148: 2149: 2150: 2151: 2152: 2153: 2154: 2155: 2156: 2157: 2158: 2159: 2160: 2161: 2162: 2163: 2164: 2165: 2166: 2167: 2168: 2169: 2170: 2171: 2172: 2173: 2174: 2175: 2176: 2177: 2178: 2179: 2180: 2181: 2182: 2183: 2184: 2185: 2186: 2187: 2188: 2189: 2190: 2191: 2192: 2193: 2194: 2195: 2196: 2197: 2198: 2199: 2200: 2201: 2202: 2203: 2204: 2205: 2206: 2207: 2208: 2209: 2210: 2211: 2212: 2213: 2214: 2215: 2216: 2217: 2218: 2219: 2220: 2221: 2222: 2223: 2224: 2225: 2226: 2227: 2228: 2229: 2230: 2231: 2232: 2233: 2234: 2235: 2236: 2237: 2238: 2239: 2240: 2241: 2242: 2243: 2244: 2245: 2246: 2247: 2248: 2249: 2250: 2251: 2252: 2253: 2254: 2255: 2256: 2257: 2258: 2259: 2260: 2261: 2262: 2263: 2264: 2265: 2266: 2267: 2268: 2269: 2270: 2271: 2272: 2273: 2274: 2275: 2276: 2277: 2278: 2279: 2280: 2281: 2282: 2283: 2284: 2285: 2286: 2287: 2288: 2289: 2290: 2291: 2292: 2293: 2294: 2295: 2296: 2297: 2298: 2299: 2300: 2301: 2302: 2303: 2304: 2305: 2306: 2307: 2308: 2309: 2310: 2311: 2312: 2313: 2314: 2315: 2316: 2317: 2318: 2319: 2320: 2321: 2322: 2323: 2324: 2325: 2326: 2327: 2328: 2329: 2330: 2331: 2332: 2333: 2334: 2335: 2336: 2337: 2338: 2339: 2340: 2341: 2342: 2343: 2344: 2345: 2346: 2347: 2348: 2349: 2350: 2351: 2352: 2353: 2354: 2355: 2356: 2357: 2358: 2359: 2360: 2361: 2362: 2363: 2364: 2365: 2366: 2367: 2368: 2369: 2370: 2371: 2372: 2373: 2374: 2375: 2376: 2377: 2378: 2379: 2380: 2381: 2382: 2383: 2384: 2385: 2386: 2387: 2388: 2389: 2390: 2391: 2392: 2393: 2394: 2395: 2396: 2397: 2398: 2399: 2400: 2401: 2402: 2403: 2404: 2405: 2406: 2407: 2408: 2409: 2410: 2411: 2412: 2413: 2414: 2415: 2416: 2417: 2418: 2419: 2420: 2421: 2422: 2423: 2424: 2425: 2426: 2427: 2428: 2429: 2430: 2431: 2432: 2433: 2434: 2435: 2436: 2437: 2438: 2439: 2440: 2441: 2442: 2443: 2444: 2445: 2446: 2447: 2448: 2449: 2450: 2451: 2452: 2453: 2454: 2455: 2456: 2457: 2458: 2459: 2460: 2461: 2462: 2463: 2464: 2465: 2466: 2467: 2468: 2469: 2470: 2471: 2472: 2473: 2474: 2475: 2476: 2477: 2478: 2479: 2480: 2481: 2482: 2483: 2484: 2485: 2486: 2487: 2488: 2489: 2490: 2491: 2492: 2493: 2494: 2495: 2496: 2497: 2498: 2499: 2500: 2501: 2502: 2503: 2504: 2505: 2506: 2507: 2508: 2509: 2510: 2511: 2512: 2513: 2514: 2515: 2516: 2517: 2518: 2519: 2520: 2521: 2522: 2523: 2524: 2525: 2526: 2527: 2528: 2529: 2530: 2531: 2532: 2533: 2534: 2535: 2536: 2537: 2538: 2539: 2540: 2541: 2542: 2543: 2544: 2545: 2546: 2547: 2548: 2549: 2550: 2551: 2552: 2553: 2554: 2555: 2556: 2557: 2558: 2559: 2560: 2561: 2562: 2563: 2564: 2565: 2566: 2567: 2568: 2569: 2570: 2571: 2572: 2573: 2574: 2575: 2576: 2577: 2578: 2579: 2580: 2581: 2582: 2583: 2584: 2585: 2586: 2587: 2588: 2589: 2590: 2591: 2592: 2593: 2594: 2595: 2596: 2597: 2598: 2599: 2600: 2601: 2602: 2603: 2604: 2605: 2606: 2607: 2608: 2609: 2610: 2611: 2612: 2613: 2614: 2615: 2616: 2617: 2618: 2619: 2620: 2621: 2622: 2623: 2624: 2625: 2626: 2627: 2628: 2629: 2630: 2631: 2632: 2633: 2634: 2635: 2636: 2637: 2638: 2639: 2640: 2641: 2642: 2643: 2644: 2645: 2646: 2647: 2648: 2649: 2650: 2651: 2652: 2653: 2654: 2655: 2656: 2657: 2658: 2659: 2660: 2661: 2662: 2663: 2664: 2665: 2666: 2667: 2668: 2669: 2670: 2671: 2672: 2673: 2674: 2675: 2676: 2677: 2678: 2679: 2680: 2681: 2682: 2683: 2684: 2685: 2686: 2687: 2688: 2689: 2690: 2691: 2692: 2693: 2694: 2695: 2696: 2697: 2698: 2699: 2700: 2701: 2702: 2703: 2704: 2705: 2706: 2707: 2708: 2709: 2710: 2711: 2712: 2713: 2714: 2715: 2716: 2717: 2718: 2719: 2720: 2721: 2722: 2723: 2724: 2725: 2726: 2727: 2728: 2729: 2730: 2731: 2732: 2733: 2734: 2735: 2736: 2737: 2738: 2739: 2740: 2741: 2742: 2743: 2744: 2745: 2746: 2747: 2748: 2749: 2750: 2751: 2752: 2753: 2754: 2755: 2756: 2757: 2758: 2759: 2760: 2761: 2762: 2763: 2764: 2765: 2766: 2767: 2768: 2769: 2770: 2771: 2772: 2773: 2774: 2775: 2776: 2777: 2778: 2779: 2780: 2781: 2782: 2783: 2784: 2785: 2786: 2787: 2788: 2789: 2790: 2791: 2792: 2793: 2794: 2795: 2796: 2797: 2798: 2799: 2800: 2801: 2802: 2803: 2804: 2805: 2806: 2807: 2808: 2809: 2810: 2811: 2812: 2813: 2814: 2815: 2816: 2817: 2818: 2819: 2820: 2821: 2822: 2823: 2824: 2825: 2826: 2827: 2828: 2829: 2830: 2831: 2832: 2833: 2834: 2835: 2836: 2837: 2838: 2839: 2840: 2841: 2842: 2843: 2844: 2845: 2846: 2847: 2848: 2849: 2850: 2851: 2852: 2853: 2854: 2855: 2856: 2857: 2858: 2859: 2860: 2861: 2862: 2863: 2864: 2865: 2866: 2867: 2868: 2869: 2870: 2871: 2872: 2873: 2874: 2875: 2876: 2877: 2878: 2879: 2880: 2881: 2882: 2883: 2884: 2885: 2886: 2887: 2888: 2889: 2890: 2891: 2892: 2893: 2894: 2895: 2896: 2897: 2898: 2899: 2900: 2901: 2902: 2903: 2904: 2905: 2906: 2907: 2908: 2909: 2910: 2911: 2912: 2913: 2914: 2915: 2916: 2917: 2918: 2919: 2920: 2921: 2922: 2923: 2924: 2925: 2926: 2927: 2928: 2929: 2930: 2931: 2932: 2933: 2934: 2935: 2936: 2937: 2938: 2939: 2940: 2941: 2942: 2943: 2944: 2945: 2946: 2947: 2948: 2949: 2950: 2951: 2952: 2953: 2954: 2955: 2956: 2957: 2958: 2959: 2960: 2961: 2962: 2963: 2964: 2965: 2966: 2967: 2968: 2969: 2970: 2971: 2972: 2973: 2974: 2975: 2976: 2977: 2978: 2979: 2980: 2981: 2982: 2983: 2984: 2985: 2986: 2987: 2988: 2989: 2990: 2991: 2992: 2993: 2994: 2995: 2996: 2997: 2998: 2999: 3000: 3001: 3002: 3003: 3004: 3005: 3006: 3007: 3008: 3009: 3010: 3011: 3012: 3013: 3014: 3015: 3016: 3017: 3018: 3019: 3020: 3021: 3022: 3023: 3024: 3025: 3026: 3027: 3028: 3029: 3030: 3031: 3032: 3033: 3034: 3035: 3036: 3037: 3038: 3039: 3040: 3041: 3042: 3043: 3044: 3045: 3046: 3047: 3048: 3049: 3050: 3051: 3052: 3053: 3054: 3055: 3056: 3057: 3058: 3059: 3060: 3061: 3062: 3063: 3064: 3065: 3066: 3067: 3068: 3069: 3070: 3071: 3072: 3073: 3074: 3075: 3076: 3077: 3078: 3079: 3080: 3081: 3082: 3083: 3084: 3085: 3086: 3087: 3088: 3089: 3090: 3091: 3092: 3093: 3094: 3095: 3096: 3097: 3098: 3099: 3100: 3101: 3102: 3103: 3104: 3105: 3106: 3107: 3108: 3109: 3110: 3111: 3112: 3113: 3114: 3115: 3116: 3117: 3118: 3119: 3120: 3121: 3122: 3123: 3124: 3125: 3126: 3127: 3128: 3129: 3130: 3131: 3132: 3133: 3134: 3135: 3136: 3137: 3138: 3139: 3140: 3141: 3142: 3143: 3144: 3145: 3146: 3147: 3148: 3149: 3150: 3151: 3152: 3153: 3154: 3155: 3156: 3157: 3158: 3159: 3160: 3161: 3162: 3163: 3164: 3165: 3166: 3167: 3168: 3169: 3170: 3171: 3172: 3173: 3174: 3175: 3176: 3177: 3178: 3179: 3180: 3181: 3182: 3183: 3184: 3185: 3186: 3187: 3188: 3189: 3190: 3191: 3192: 3193: 3194: 3195: 3196: 3197: 3198: 3199: 3200: 3201: 3202: 3203: 3204: 3205: 3206: 3207: 3208: 3209: 3210: 3211: 3212: 3213: 3214: 3215: 3216: 3217: 3218: 3219: 3220: 3221: 3222: 3223: 3224: 3225: 3226: 3227: 3228: 3229: 3230: 3231: 3232: 3233: 3234: 3235: 3236: 3237: 3238: 3239: 3240: 3241: 3242: 3243: 3244: 3245: 3246: 3247: 3248: 3249: 3250: 3251: 3252: 3253: 3254: 3255: 3256: 3257: 3258: 3259: 3260: 3261: 3262: 3263: 3264: 3265: 3266: 3267: 3268: 3269: 3270: 3271: 3272: 3273: 3274: 3275: 3276: 3277: 3278: 3279: 3280: 3281: 3282: 3283: 3284: 3285: 3286: 3287: 3288: 3289: 3290: 3291: 3292: 3293: 3294: 3295: 3296: 3297: 3298: 3299: 3300: 3301: 3302: 3303: 3304: 3305: 3306: 3307: 3308: 3309: 3310: 3311: 3312: 3313: 3314: 3315: 3316: 3317: 3318: 3319: 3320: 3321: 3322: 3323: 3324: 3325: 3326: 3327: 3328: 3329: 3330: 3331: 3332: 3333: 3334: 3335: 3336: 3337: 3338: 3339: 3340: 3341: 3342: 3343: 3344: 3345: 3346: 3347: 3348: 3349: 3350: 3351: 3352: 3353: 3354: 3355: 3356: 3357: 3358: 3359: 3360: 3361: 3362: 3363: 3364: 3365: 3366: 3367: 3368: 3369: 3370: 3371: 3372: 3373: 3374: 3375: 3376: 3377: 3378: 3379: 3380: 3381: 3382: 3383: 3384: 3385: 3386: 3387: 3388: 3389: 3390: 3391: 3392: 3393: 3394: 3395: 3396: 3397: 3398: 3399: 3400: 3401: 3402: 3403: 3404: 3405: 3406: 3407: 3408: 3409: 3410: 3411: 3412: 3413: 3414: 3415: 3416: 3417: 3418: 3419: 3420: 3421: 3422: 3423: 3424: 3425: 3426: 3427: 3428: 3429: 3430: 3431: 3432: 3433: 3434: 3435: 3436: 3437: 3438: 3439: 3440: 3441: 3442: 3443: 3444: 3445: 3446: 3447: 3448: 3449: 3450: 3451: 3452: 3453: 3454: 3455: 3456: 3457: 3458: 3459: 3460: 3461: 3462: 3463: 3464: 3465: 3466: 3467: 3468: 3469: 3470: 3471: 3472: 3473: 3474: 3475: 3476: 3477: 3478: 3479: 3480: 3481: 3482: 3483: 3484: 3485: 3486: 3487: 3488: 3489: 3490: 3491: 3492: 3493: 3494: 3495: 3496: 3497: 3498: 3499: 3500: 3501: 3502: 3503: 3504: 3505: 3506: 3507: 3508: 3509: 3510: 3511: 3512: 3513: 3514: 3515: 3516: 3517: 3518: 3519: 3520: 3521: 3522: 3523: 3524: 3525: 3526: 3527: 3528: 3529: 3530: 3531: 3532: 3533: 3534: 3535: 3536: 3537: 3538: 3539: 3540: 3541: 3542: 3543: 3544: 3545: 3546: 3547: 3548: 3549: 3550: 3551: 3552: 3553: 3554: 3555: 3556: 3557: 3558: 3559: 3560: 3561: 3562: 3563: 3564: 3565: 3566: 3567: 3568: 3569: 3570: 3571: 3572: 3573: 3574: 3575: 3576: 3577: 3578: 3579: 3580: 3581: 3582: 3583: 3584: 3585: 3586: 3587: 3588: 3589: 3590: 3591: 3592: 3593: 3594: 3595: 3596: 3597: 3598: 3599: 3600: 3601: 3602: 3603: 3604: 3605: 3606: 3607: 3608: 3609: 3610: 3611: 3612: 3613: 3614: 3615: 3616: 3617: 3618: 3619: 3620: 3621: 3622: 3623: 3624: 3625: 3626: 3627: 3628: 3629: 3630: 3631: 3632: 3633: 3634: 3635: 3636: 3637: 3638: 3639: 3640: 3641: 3642: 3643: 3644: 3645: 3646: 3647: 3648: 3649: 3650: 3651: 3652: 3653: 3654: 3655: 3656: 3657: 3658: 3659: 3660: 3661: 3662: 3663: 3664: 3665: 3666: 3667: 3668: 3669: 3670: 3671: 3672: 3673: 3674: 3675: 3676: 3677: 3678: 3679: 3680: 3681: 3682: 3683: 3684: 3685: 3686: 3687: 3688: 3689: 3690: 3691: 3692: 3693: 3694: 3695: 3696: 3697: 3698: 3699: 3700: 3701: 3702: 3703: 3704: 3705: 3706: 3707: 3708: 3709: 3710: 3711: 3712: 3713: 3714: 3715: 
<?php
if (!defined('SMF'))
    die('No direct access...');
function reloadSettings()
{
    global $modSettings, $boarddir, $smcFunc, $txt, $db_character_set;
    global $cache_enable, $sourcedir, $context, $forum_version, $boardurl;
    global $image_proxy_enabled;
    
    if (!empty($db_character_set))
        $smcFunc['db_query']('', '
            SET NAMES {string:db_character_set}',
            array(
                'db_character_set' => $db_character_set,
            )
        );
    
    loadCacheAccelerator();
    
    if (($modSettings = cache_get_data('modSettings', 90)) == null)
    {
        $request = $smcFunc['db_query']('', '
            SELECT variable, value
            FROM {db_prefix}settings',
            array(
            )
        );
        $modSettings = array();
        if (!$request)
            display_db_error();
        foreach ($smcFunc['db_fetch_all']($request) as $row)
            $modSettings[$row['variable']] = $row['value'];
        $smcFunc['db_free_result']($request);
        
        if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999)
            $modSettings['defaultMaxTopics'] = 20;
        if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999)
            $modSettings['defaultMaxMessages'] = 15;
        if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999)
            $modSettings['defaultMaxMembers'] = 30;
        if (empty($modSettings['defaultMaxListItems']) || $modSettings['defaultMaxListItems'] <= 0 || $modSettings['defaultMaxListItems'] > 999)
            $modSettings['defaultMaxListItems'] = 15;
        
        if (!is_array($modSettings['attachmentUploadDir']))
        {
            $attachmentUploadDir = smf_json_decode($modSettings['attachmentUploadDir'], true);
            $modSettings['attachmentUploadDir'] = !empty($attachmentUploadDir) ? $attachmentUploadDir : $modSettings['attachmentUploadDir'];
        }
        if (!empty($cache_enable))
            cache_put_data('modSettings', $modSettings, 90);
    }
    
    if (!defined('SMF_INSTALLING') && (!isset($_REQUEST['action']) || $_REQUEST['action'] !== 'admin' || !isset($_REQUEST['area']) || $_REQUEST['area'] !== 'packages') && !empty($modSettings['smfVersion']) && version_compare(strtolower(strtr($modSettings['smfVersion'], array(' ' => '.'))), strtolower(strtr(SMF_VERSION, array(' ' => '.'))), '!='))
    {
        
        cache_put_data('modSettings', null);
        
        if (file_exists($boarddir . '/upgrade.php'))
            header('location: ' . $boardurl . '/upgrade.php');
        die('SMF file version (' . SMF_VERSION . ') does not match SMF database version (' . $modSettings['smfVersion'] . ').<br>Run the SMF upgrader to fix this.<br><a href="https://wiki.simplemachines.org/smf/Upgrading">More information</a>.');
    }
    $modSettings['cache_enable'] = $cache_enable;
    
    $modSettings['browser_cache'] = !empty($modSettings['browser_cache']) ? (int) $modSettings['browser_cache'] : 0;
    $context['browser_cache'] = '?' . preg_replace('~\W~', '', strtolower(SMF_FULL_VERSION)) . '_' . $modSettings['browser_cache'];
    
    if (empty($modSettings['force_ssl']))
        $image_proxy_enabled = false;
    
    $utf8 = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8';
    $context['utf8'] = $utf8;
    
    $ent_list = '&(?:#' . (empty($modSettings['disableEntityCheck']) ? '\d{1,7}' : '021') . '|quot|amp|lt|gt|nbsp);';
    $ent_check = empty($modSettings['disableEntityCheck']) ? function($string)
        {
            $string = preg_replace_callback('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~', 'entity_fix__callback', $string);
            return $string;
        } : function($string)
        {
            return $string;
        };
    $fix_utf8mb4 = function($string) use ($utf8, $smcFunc)
    {
        if (!$utf8 || $smcFunc['db_mb4'])
            return $string;
        $i = 0;
        $len = strlen($string);
        $new_string = '';
        while ($i < $len)
        {
            $ord = ord($string[$i]);
            if ($ord < 128)
            {
                $new_string .= $string[$i];
                $i++;
            }
            elseif ($ord < 224)
            {
                $new_string .= $string[$i] . $string[$i + 1];
                $i += 2;
            }
            elseif ($ord < 240)
            {
                $new_string .= $string[$i] . $string[$i + 1] . $string[$i + 2];
                $i += 3;
            }
            elseif ($ord < 248)
            {
                
                $val = (ord($string[$i]) & 0x07) << 18;
                $val += (ord($string[$i + 1]) & 0x3F) << 12;
                $val += (ord($string[$i + 2]) & 0x3F) << 6;
                $val += (ord($string[$i + 3]) & 0x3F);
                $new_string .= '&#' . $val . ';';
                $i += 4;
            }
        }
        return $new_string;
    };
    
    $smcFunc += array(
        'entity_fix' => function($string)
        {
            $num = $string[0] === 'x' ? hexdec(substr($string, 1)) : (int) $string;
            return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num === 0x202E || $num === 0x202D ? '' : '&#' . $num . ';';
        },
        'htmlspecialchars' => function($string, $quote_style = ENT_COMPAT, $charset = 'ISO-8859-1') use ($ent_check, $utf8, $fix_utf8mb4)
        {
            return $fix_utf8mb4($ent_check(htmlspecialchars($string, $quote_style, $utf8 ? 'UTF-8' : $charset)));
        },
        'htmltrim' => function($string) use ($utf8, $ent_check)
        {
            
            $space_chars = $utf8 ? '\p{Z}\p{C}' : '\x00-\x20\x80-\xA0';
            return preg_replace('~^(?:[' . $space_chars . ']| )+|(?:[' . $space_chars . ']| )+$~' . ($utf8 ? 'u' : ''), '', $ent_check($string));
        },
        'strlen' => function($string) use ($ent_list, $utf8, $ent_check)
        {
            return strlen(preg_replace('~' . $ent_list . ($utf8 ? '|.~u' : '~'), '_', $ent_check($string)));
        },
        'strpos' => function($haystack, $needle, $offset = 0) use ($utf8, $ent_check, $ent_list, $modSettings)
        {
            $haystack_arr = preg_split('~(' . $ent_list . '|.)~' . ($utf8 ? 'u' : ''), $ent_check($haystack), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
            if (strlen($needle) === 1)
            {
                $result = array_search($needle, array_slice($haystack_arr, $offset));
                return is_int($result) ? $result + $offset : false;
            }
            else
            {
                $needle_arr = preg_split('~(' . $ent_list . '|.)~' . ($utf8 ? 'u' : '') . '', $ent_check($needle), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
                $needle_size = count($needle_arr);
                $result = array_search($needle_arr[0], array_slice($haystack_arr, $offset));
                while ((int) $result === $result)
                {
                    $offset += $result;
                    if (array_slice($haystack_arr, $offset, $needle_size) === $needle_arr)
                        return $offset;
                    $result = array_search($needle_arr[0], array_slice($haystack_arr, ++$offset));
                }
                return false;
            }
        },
        'substr' => function($string, $start, $length = null) use ($utf8, $ent_check, $ent_list, $modSettings)
        {
            $ent_arr = preg_split('~(' . $ent_list . '|.)~' . ($utf8 ? 'u' : '') . '', $ent_check($string), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
            return $length === null ? implode('', array_slice($ent_arr, $start)) : implode('', array_slice($ent_arr, $start, $length));
        },
        'strtolower' => $utf8 ? function($string) use ($sourcedir)
        {
            if (!function_exists('mb_strtolower'))
            {
                require_once($sourcedir . '/Subs-Charset.php');
                return utf8_strtolower($string);
            }
            return mb_strtolower($string, 'UTF-8');
        } : 'strtolower',
        'strtoupper' => $utf8 ? function($string)
        {
            global $sourcedir;
            if (!function_exists('mb_strtolower'))
            {
                require_once($sourcedir . '/Subs-Charset.php');
                return utf8_strtoupper($string);
            }
            return mb_strtoupper($string, 'UTF-8');
        } : 'strtoupper',
        'truncate' => function($string, $length) use ($utf8, $ent_check, $ent_list, &$smcFunc)
        {
            $string = $ent_check($string);
            preg_match('~^(' . $ent_list . '|.){' . $smcFunc['strlen'](substr($string, 0, $length)) . '}~' . ($utf8 ? 'u' : ''), $string, $matches);
            $string = $matches[0];
            while (strlen($string) > $length)
                $string = preg_replace('~(?:' . $ent_list . '|.)$~' . ($utf8 ? 'u' : ''), '', $string);
            return $string;
        },
        'ucfirst' => $utf8 ? function($string) use (&$smcFunc)
        {
            return $smcFunc['strtoupper']($smcFunc['substr']($string, 0, 1)) . $smcFunc['substr']($string, 1);
        } : 'ucfirst',
        'ucwords' => $utf8 ? function($string) use (&$smcFunc)
        {
            $words = preg_split('~([\s\r\n\t]+)~', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
            for ($i = 0, $n = count($words); $i < $n; $i += 2)
                $words[$i] = $smcFunc['ucfirst']($words[$i]);
            return implode('', $words);
        } : 'ucwords',
        'json_decode' => 'smf_json_decode',
        'json_encode' => 'json_encode',
        'random_int' => function($min = 0, $max = PHP_INT_MAX)
        {
            global $sourcedir;
            
            if (!is_callable('random_int'))
                require_once($sourcedir . '/random_compat/random.php');
            return random_int($min, $max);
        },
        'random_bytes' => function($length = 64)
        {
            global $sourcedir;
            if (!is_callable('random_bytes'))
                require_once($sourcedir . '/random_compat/random.php');
            
            $length = max(1, (int) $length);
            return random_bytes($length);
        },
    );
    
    if (isset($modSettings['default_timezone']) && in_array($modSettings['default_timezone'], timezone_identifiers_list()))
        date_default_timezone_set($modSettings['default_timezone']);
    else
    {
        
        $ini_tz = ini_get('date.timezone');
        if (!empty($ini_tz))
            $modSettings['default_timezone'] = $ini_tz;
        else
            $modSettings['default_timezone'] = '';
        
        if (!in_array($modSettings['default_timezone'], timezone_identifiers_list()))
        {
            $server_offset = @mktime(0, 0, 0, 1, 1, 1970);
            $modSettings['default_timezone'] = timezone_name_from_abbr('', $server_offset, 0);
        }
        date_default_timezone_set($modSettings['default_timezone']);
    }
    
    if (!empty($modSettings['loadavg_enable']))
    {
        if (($modSettings['load_average'] = cache_get_data('loadavg', 90)) == null)
        {
            $modSettings['load_average'] = @file_get_contents('/proc/loadavg');
            if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) != 0)
                $modSettings['load_average'] = (float) $matches[1];
            elseif (($modSettings['load_average'] = @`uptime`) != null && preg_match('~load average[s]?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) != 0)
                $modSettings['load_average'] = (float) $matches[1];
            else
                unset($modSettings['load_average']);
            if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0)
                cache_put_data('loadavg', $modSettings['load_average'], 90);
        }
        if (!empty($modSettings['load_average']) || $modSettings['load_average'] === 0.0)
            call_integration_hook('integrate_load_average', array($modSettings['load_average']));
        if (!empty($modSettings['loadavg_forum']) && !empty($modSettings['load_average']) && $modSettings['load_average'] >= $modSettings['loadavg_forum'])
            display_loadavg_error();
    }
    
    $modSettings['postmod_active'] = !empty($modSettings['postmod_active']);
    
    
    
    if (empty($modSettings['currentAttachmentUploadDir']))
    {
        updateSettings(array(
            'attachmentUploadDir' => $smcFunc['json_encode'](array(1 => $modSettings['attachmentUploadDir'])),
            'currentAttachmentUploadDir' => 1,
        ));
    }
    
    $post_max_kb = floor(memoryReturnBytes(ini_get('post_max_size')) / 1024);
    $file_max_kb = floor(memoryReturnBytes(ini_get('upload_max_filesize')) / 1024);
    $modSettings['attachmentPostLimit'] = empty($modSettings['attachmentPostLimit']) ? $post_max_kb : min($modSettings['attachmentPostLimit'], $post_max_kb);
    $modSettings['attachmentSizeLimit'] = empty($modSettings['attachmentSizeLimit']) ? $file_max_kb : min($modSettings['attachmentSizeLimit'], $file_max_kb);
    
    if (defined('SMF_INTEGRATION_SETTINGS'))
    {
        $integration_settings = $smcFunc['json_decode'](SMF_INTEGRATION_SETTINGS, true);
        foreach ($integration_settings as $hook => $function)
            add_integration_function($hook, $function, false);
    }
    
    if (!empty($modSettings['integrate_pre_include']))
    {
        $pre_includes = explode(',', $modSettings['integrate_pre_include']);
        foreach ($pre_includes as $include)
        {
            $include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir));
            if (file_exists($include))
                require_once($include);
        }
    }
    
    $context['server'] = array(
        'is_iis' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== false,
        'is_apache' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false,
        'is_litespeed' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false,
        'is_lighttpd' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false,
        'is_nginx' => isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false,
        'is_cgi' => isset($_SERVER['SERVER_SOFTWARE']) && strpos(php_sapi_name(), 'cgi') !== false,
        'is_windows' => DIRECTORY_SEPARATOR === '\\',
        'iso_case_folding' => ord(strtolower(chr(138))) === 154,
    );
    
    $context['server']['needs_login_fix'] = $context['server']['is_cgi'] && $context['server']['is_iis'];
    
    $context['stable_icons'] = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'poll', 'moved', 'recycled', 'clip');
    
    $context['cust_profile_fields_placement'] = array(
        'standard',
        'icons',
        'above_signature',
        'below_signature',
        'below_avatar',
        'above_member',
        'bottom_poster',
        'before_member',
        'after_member',
    );
    
    $context['meta_tags'] = array();
    
    $context['allowed_html_tags'] = array(
        '<img>',
        '<div>',
    );
    
    
    $context['valid_image_types'] = array(
        IMAGETYPE_GIF => 'gif',
        IMAGETYPE_JPEG => 'jpeg',
        IMAGETYPE_PNG => 'png',
        IMAGETYPE_PSD => 'psd',
        IMAGETYPE_BMP => 'bmp',
        IMAGETYPE_TIFF_II => 'tiff',
        IMAGETYPE_TIFF_MM => 'tiff',
        IMAGETYPE_IFF => 'iff'
    );
    
    $context['description_allowed_tags'] = array(
        'abbr', 'anchor', 'b', 'center', 'color', 'font', 'hr', 'i', 'img',
        'iurl', 'left', 'li', 'list', 'ltr', 'pre', 'right', 's', 'sub',
        'sup', 'table', 'td', 'tr', 'u', 'url',
    );
    
    
    $context['legacy_bbc'] = array(
        'acronym', 'bdo', 'black', 'blue', 'flash', 'ftp', 'glow',
        'green', 'move', 'red', 'shadow', 'tt', 'white',
    );
    
    $context['restricted_bbc'] = array(
        'html',
    );
    
    call_integration_hook('integrate_pre_load');
}
function loadUserSettings()
{
    global $modSettings, $user_settings, $sourcedir, $smcFunc;
    global $cookiename, $user_info, $language, $context, $cache_enable;
    
    if (count($integration_ids = call_integration_hook('integrate_verify_user')) > 0)
    {
        $id_member = 0;
        foreach ($integration_ids as $integration_id)
        {
            $integration_id = (int) $integration_id;
            if ($integration_id > 0)
            {
                $id_member = $integration_id;
                $already_verified = true;
                break;
            }
        }
    }
    else
        $id_member = 0;
    if (empty($id_member) && isset($_COOKIE[$cookiename]))
    {
        
        $cookie_data = $smcFunc['json_decode']($_COOKIE[$cookiename], true, false);
        
        if (empty($cookie_data))
            $cookie_data = safe_unserialize($_COOKIE[$cookiename]);
        list($id_member, $password, $login_span, $cookie_domain, $cookie_path) = array_pad((array) $cookie_data, 5, '');
        $id_member = !empty($id_member) && strlen($password) > 0 ? (int) $id_member : 0;
        
        require_once($sourcedir . '/Subs-Auth.php');
        if (array($cookie_domain, $cookie_path) !== url_parts(!empty($modSettings['localCookies']), !empty($modSettings['globalCookies'])))
            setLoginCookie((int) $login_span - time(), $id_member);
    }
    elseif (empty($id_member) && isset($_SESSION['login_' . $cookiename]) && ($_SESSION['USER_AGENT'] == $_SERVER['HTTP_USER_AGENT'] || !empty($modSettings['disableCheckUA'])))
    {
        
        $cookie_data = $smcFunc['json_decode']($_SESSION['login_' . $cookiename], true);
        if (empty($cookie_data))
            $cookie_data = safe_unserialize($_SESSION['login_' . $cookiename]);
        list($id_member, $password, $login_span) = array_pad((array) $cookie_data, 3, '');
        $id_member = !empty($id_member) && strlen($password) == 128 && (int) $login_span > time() ? (int) $id_member : 0;
    }
    
    if ($id_member != 0)
    {
        
        if (empty($cache_enable) || $cache_enable < 2 || ($user_settings = cache_get_data('user_settings-' . $id_member, 60)) == null)
        {
            $request = $smcFunc['db_query']('', '
                SELECT mem.*, COALESCE(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type
                FROM {db_prefix}members AS mem
                    LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = {int:id_member})
                WHERE mem.id_member = {int:id_member}
                LIMIT 1',
                array(
                    'id_member' => $id_member,
                )
            );
            $user_settings = $smcFunc['db_fetch_assoc']($request);
            $smcFunc['db_free_result']($request);
            if (!empty($user_settings['avatar']))
                $user_settings['avatar'] = get_proxied_url($user_settings['avatar']);
            if (!empty($cache_enable) && $cache_enable >= 2)
                cache_put_data('user_settings-' . $id_member, $user_settings, 60);
        }
        
        if (!empty($user_settings))
        {
            
            if (!empty($already_verified) && $already_verified === true)
                $check = true;
            
            elseif (strlen($password) == 128)
                $check = hash_salt($user_settings['passwd'], $user_settings['password_salt']) == $password;
            else
                $check = false;
            
            $id_member = $check && ($user_settings['is_activated'] == 1 || $user_settings['is_activated'] == 11) ? (int) $user_settings['id_member'] : 0;
        }
        else
            $id_member = 0;
        
        $force_tfasetup = !empty($modSettings['tfa_mode']) && $modSettings['tfa_mode'] >= 2 && $id_member && empty($user_settings['tfa_secret']) && SMF != 'SSI' && !isset($_REQUEST['xml']) && (!isset($_REQUEST['action']) || $_REQUEST['action'] != '.xml');
        
        if ($force_tfasetup)
        {
            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'profile' && isset($_REQUEST['area']) && in_array($_REQUEST['area'], array('popup', 'alerts_popup')))
                $force_tfasetup = false;
            elseif (isset($_REQUEST['action']) && $_REQUEST['action'] == 'pm' && (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'popup'))
                $force_tfasetup = false;
            call_integration_hook('integrate_force_tfasetup', array(&$force_tfasetup));
        }
        
        if (!$id_member)
        {
            require_once($sourcedir . '/LogInOut.php');
            validatePasswordFlood(
                !empty($user_settings['id_member']) ? $user_settings['id_member'] : $id_member,
                !empty($user_settings['member_name']) ? $user_settings['member_name'] : '',
                !empty($user_settings['passwd_flood']) ? $user_settings['passwd_flood'] : false,
                $id_member != 0
            );
        }
        
        elseif (!empty($modSettings['tfa_mode']) && $id_member && !empty($user_settings['tfa_secret']) && (empty($_REQUEST['action']) || !in_array($_REQUEST['action'], array('login2', 'logintfa'))))
        {
            $tfacookie = $cookiename . '_tfa';
            $tfasecret = null;
            $verified = call_integration_hook('integrate_verify_tfa', array($id_member, $user_settings));
            if (empty($verified) || !in_array(true, $verified))
            {
                if (!empty($_COOKIE[$tfacookie]))
                {
                    $tfa_data = $smcFunc['json_decode']($_COOKIE[$tfacookie], true);
                    list ($tfamember, $tfasecret) = array_pad((array) $tfa_data, 2, '');
                    if (!isset($tfamember, $tfasecret) || (int) $tfamember != $id_member)
                        $tfasecret = null;
                }
                
                if (empty($tfasecret) || hash_salt($user_settings['tfa_backup'], $user_settings['password_salt']) != $tfasecret)
                {
                    setLoginCookie(-3600, $id_member);
                    $id_member = 0;
                    $user_settings = array();
                }
            }
        }
        
        elseif (!empty($modSettings['tfa_mode']) && $id_member && !empty($user_settings['tfa_secret']) && $_REQUEST['action'] == 'logintfa')
        {
            $id_member = 0;
            $context['tfa_member'] = $user_settings;
            $user_settings = array();
        }
        
        elseif ($force_tfasetup)
        {
            if ($modSettings['tfa_mode'] == 2) 
            {
                
                $full_groups = array($user_settings['id_group']);
                if (!empty($user_settings['additional_groups']))
                {
                    $full_groups = array_merge($full_groups, explode(',', $user_settings['additional_groups']));
                    $full_groups = array_unique($full_groups); 
                }
                
                $request = $smcFunc['db_query']('', '
                    SELECT COUNT(id_group) AS total
                    FROM {db_prefix}membergroups
                    WHERE tfa_required = {int:tfa_required}
                        AND id_group IN ({array_int:full_groups})',
                    array(
                        'tfa_required' => 1,
                        'full_groups' => $full_groups,
                    )
                );
                $row = $smcFunc['db_fetch_assoc']($request);
                $smcFunc['db_free_result']($request);
            }
            else
                $row['total'] = 1; 
            $area = !empty($_REQUEST['area']) ? $_REQUEST['area'] : '';
            $action = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
            if ($row['total'] > 0 && !in_array($action, array('profile', 'logout')) || ($action == 'profile' && $area != 'tfasetup'))
                redirectexit('action=profile;area=tfasetup;forced');
        }
    }
    
    if ($id_member != 0)
    {
        
        
        
        
        
        
        if (SMF != 'SSI' && !isset($_REQUEST['xml']) && (!isset($_REQUEST['action']) || !in_array($_REQUEST['action'], array('.xml', 'login2', 'logintfa'))) && empty($_SESSION['id_msg_last_visit']) && (empty($cache_enable) || ($_SESSION['id_msg_last_visit'] = cache_get_data('user_last_visit-' . $id_member, 5 * 3600)) === null))
        {
            
            
            $result = $smcFunc['db_query']('', '
                SELECT poster_time
                FROM {db_prefix}messages
                WHERE id_msg = {int:id_msg}
                LIMIT 1',
                array(
                    'id_msg' => $user_settings['id_msg_last_visit'],
                )
            );
            list ($visitTime) = $smcFunc['db_fetch_row']($result);
            $smcFunc['db_free_result']($result);
            $_SESSION['id_msg_last_visit'] = $user_settings['id_msg_last_visit'];
            
            if ($visitTime < time() - 5 * 3600)
            {
                updateMemberData($id_member, array('id_msg_last_visit' => (int) $modSettings['maxMsgID'], 'last_login' => time(), 'member_ip' => $_SERVER['REMOTE_ADDR'], 'member_ip2' => $_SERVER['BAN_CHECK_IP']));
                $user_settings['last_login'] = time();
                if (!empty($cache_enable) && $cache_enable >= 2)
                    cache_put_data('user_settings-' . $id_member, $user_settings, 60);
                if (!empty($cache_enable))
                    cache_put_data('user_last_visit-' . $id_member, $_SESSION['id_msg_last_visit'], 5 * 3600);
            }
        }
        elseif (empty($_SESSION['id_msg_last_visit']))
            $_SESSION['id_msg_last_visit'] = $user_settings['id_msg_last_visit'];
        $username = $user_settings['member_name'];
        if (empty($user_settings['additional_groups']))
            $user_info = array(
                'groups' => array($user_settings['id_group'], $user_settings['id_post_group'])
            );
        else
            $user_info = array(
                'groups' => array_merge(
                    array($user_settings['id_group'], $user_settings['id_post_group']),
                    explode(',', $user_settings['additional_groups'])
                )
            );
        
        $user_info['groups'] = array_map('intval', $user_info['groups']);
        
        $user_info['possibly_robot'] = false;
        
        if (!empty($user_settings['timezone']))
        {
            
            $tz_system = new DateTimeZone(@date_default_timezone_get());
            $tz_user = new DateTimeZone($user_settings['timezone']);
            $time_system = new DateTime('now', $tz_system);
            $time_user = new DateTime('now', $tz_user);
            $user_info['time_offset'] = ($tz_user->getOffset($time_user) - $tz_system->getOffset($time_system)) / 3600;
        }
        else
        {
            
            $user_info['time_offset'] = empty($user_settings['time_offset']) ? 0 : $user_settings['time_offset'];
        }
    }
    
    else
    {
        
        $username = '';
        $user_info = array('groups' => array(-1));
        $user_settings = array();
        if (isset($_COOKIE[$cookiename]) && empty($context['tfa_member']))
            $_COOKIE[$cookiename] = '';
        
        if (isset($_COOKIE[$cookiename . '_tfa']) && empty($context['tfa_member']))
        {
            $tfa_data = $smcFunc['json_decode']($_COOKIE[$cookiename . '_tfa'], true);
            list (,, $exp) = array_pad((array) $tfa_data, 3, 0);
            if (time() > $exp)
            {
                $_COOKIE[$cookiename . '_tfa'] = '';
                setTFACookie(-3600, 0, '');
            }
        }
        
        if (!isset($_SESSION['token']['post-login']))
            createToken('login');
        else
            list ($context['login_token_var'],,, $context['login_token']) = $_SESSION['token']['post-login'];
        
        if ((!empty($modSettings['spider_mode']) || !empty($modSettings['spider_group'])) && (!isset($_SESSION['robot_check']) || $_SESSION['robot_check'] < time() - 300))
        {
            require_once($sourcedir . '/ManageSearchEngines.php');
            $user_info['possibly_robot'] = SpiderCheck();
        }
        elseif (!empty($modSettings['spider_mode']))
            $user_info['possibly_robot'] = isset($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
        
        else
        {
            $ci_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
            $user_info['possibly_robot'] = (strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla') === false && strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') === false) || strpos($ci_user_agent, 'googlebot') !== false || strpos($ci_user_agent, 'slurp') !== false || strpos($ci_user_agent, 'crawl') !== false || strpos($ci_user_agent, 'bingbot') !== false || strpos($ci_user_agent, 'bingpreview') !== false || strpos($ci_user_agent, 'adidxbot') !== false || strpos($ci_user_agent, 'msnbot') !== false;
        }
        
        $user_info['time_offset'] = 0;
    }
    
    $context['login_cookie_times'] = array(
        3153600 => 'always_logged_in',
        60 => 'one_hour',
        1440 => 'one_day',
        10080 => 'one_week',
        43200 => 'one_month',
    );
    
    $user_info += array(
        'id' => $id_member,
        'username' => $username,
        'name' => isset($user_settings['real_name']) ? $user_settings['real_name'] : '',
        'email' => isset($user_settings['email_address']) ? $user_settings['email_address'] : '',
        'passwd' => isset($user_settings['passwd']) ? $user_settings['passwd'] : '',
        'language' => empty($user_settings['lngfile']) || empty($modSettings['userLanguage']) ? $language : $user_settings['lngfile'],
        'is_guest' => $id_member == 0,
        'is_admin' => in_array(1, $user_info['groups']),
        'theme' => empty($user_settings['id_theme']) ? 0 : $user_settings['id_theme'],
        'last_login' => empty($user_settings['last_login']) ? 0 : $user_settings['last_login'],
        'ip' => $_SERVER['REMOTE_ADDR'],
        'ip2' => $_SERVER['BAN_CHECK_IP'],
        'posts' => empty($user_settings['posts']) ? 0 : $user_settings['posts'],
        'time_format' => empty($user_settings['time_format']) ? $modSettings['time_format'] : $user_settings['time_format'],
        'avatar' => array(
            'url' => isset($user_settings['avatar']) ? $user_settings['avatar'] : '',
            'filename' => empty($user_settings['filename']) ? '' : $user_settings['filename'],
            'custom_dir' => !empty($user_settings['attachment_type']) && $user_settings['attachment_type'] == 1,
            'id_attach' => isset($user_settings['id_attach']) ? $user_settings['id_attach'] : 0
        ),
        'smiley_set' => isset($user_settings['smiley_set']) ? $user_settings['smiley_set'] : '',
        'messages' => empty($user_settings['instant_messages']) ? 0 : $user_settings['instant_messages'],
        'unread_messages' => empty($user_settings['unread_messages']) ? 0 : $user_settings['unread_messages'],
        'alerts' => empty($user_settings['alerts']) ? 0 : $user_settings['alerts'],
        'total_time_logged_in' => empty($user_settings['total_time_logged_in']) ? 0 : $user_settings['total_time_logged_in'],
        'buddies' => !empty($modSettings['enable_buddylist']) && !empty($user_settings['buddy_list']) ? explode(',', $user_settings['buddy_list']) : array(),
        'ignoreboards' => !empty($user_settings['ignore_boards']) && !empty($modSettings['allow_ignore_boards']) ? explode(',', $user_settings['ignore_boards']) : array(),
        'ignoreusers' => !empty($user_settings['pm_ignore_list']) ? explode(',', $user_settings['pm_ignore_list']) : array(),
        'warning' => isset($user_settings['warning']) ? $user_settings['warning'] : 0,
        'permissions' => array(),
    );
    $user_info['groups'] = array_unique($user_info['groups']);
    $user_info['can_manage_boards'] = !empty($user_info['is_admin']) || (!empty($modSettings['board_manager_groups']) && count(array_intersect($user_info['groups'], explode(',', $modSettings['board_manager_groups']))) > 0);
    
    if (!empty($user_info['ignoreboards']) && empty($user_info['ignoreboards'][$tmp = count($user_info['ignoreboards']) - 1]))
        unset($user_info['ignoreboards'][$tmp]);
    
    if (!empty($modSettings['userLanguage']))
    {
        $languages = getLanguages();
        
        if (!empty($_GET['language']) && isset($languages[strtr($_GET['language'], './\\:', '____')]))
        {
            $user_info['language'] = strtr($_GET['language'], './\\:', '____');
            
            if (!empty($user_info['id']))
                updateMemberData($user_info['id'], array('lngfile' => $user_info['language']));
            else
                $_SESSION['language'] = $user_info['language'];
        }
        elseif (!empty($_SESSION['language']) && isset($languages[strtr($_SESSION['language'], './\\:', '____')]))
            $user_info['language'] = strtr($_SESSION['language'], './\\:', '____');
    }
    $temp = build_query_board($user_info['id']);
    $user_info['query_see_board'] = $temp['query_see_board'];
    $user_info['query_see_message_board'] = $temp['query_see_message_board'];
    $user_info['query_see_topic_board'] = $temp['query_see_topic_board'];
    $user_info['query_wanna_see_board'] = $temp['query_wanna_see_board'];
    $user_info['query_wanna_see_message_board'] = $temp['query_wanna_see_message_board'];
    $user_info['query_wanna_see_topic_board'] = $temp['query_wanna_see_topic_board'];
    call_integration_hook('integrate_user_info');
}
function loadBoard()
{
    global $txt, $scripturl, $context, $modSettings;
    global $board_info, $board, $topic, $user_info, $smcFunc, $cache_enable;
    
    $user_info['is_mod'] = false;
    $context['user']['is_mod'] = &$user_info['is_mod'];
    
    $context['linktree'] = array();
    
    if (empty($_REQUEST['action']) && empty($topic) && empty($board) && !empty($_REQUEST['msg']))
    {
        
        $_REQUEST['msg'] = (int) $_REQUEST['msg'];
        
        if (($topic = cache_get_data('msg_topic-' . $_REQUEST['msg'], 120)) === null)
        {
            $request = $smcFunc['db_query']('', '
                SELECT id_topic
                FROM {db_prefix}messages
                WHERE id_msg = {int:id_msg}
                LIMIT 1',
                array(
                    'id_msg' => $_REQUEST['msg'],
                )
            );
            
            if ($smcFunc['db_num_rows']($request))
            {
                list ($topic) = $smcFunc['db_fetch_row']($request);
                $smcFunc['db_free_result']($request);
                
                cache_put_data('msg_topic-' . $_REQUEST['msg'], $topic, 120);
            }
        }
        
        if (!empty($topic))
            redirectexit('topic=' . $topic . '.msg' . $_REQUEST['msg'] . '#msg' . $_REQUEST['msg']);
        else
        {
            loadPermissions();
            loadTheme();
            fatal_lang_error('topic_gone', false);
        }
    }
    
    if (empty($board) && empty($topic))
    {
        $board_info = array('moderators' => array(), 'moderator_groups' => array());
        return;
    }
    if (!empty($cache_enable) && (empty($topic) || $cache_enable >= 3))
    {
        
        if (!empty($topic))
            $temp = cache_get_data('topic_board-' . $topic, 120);
        else
            $temp = cache_get_data('board-' . $board, 120);
        if (!empty($temp))
        {
            $board_info = $temp;
            $board = $board_info['id'];
        }
    }
    if (empty($temp))
    {
        $custom_column_selects = [];
        $custom_column_parameters = [
            'current_topic' => $topic,
            'board_link' => empty($topic) ? $smcFunc['db_quote']('{int:current_board}', array('current_board' => $board)) : 't.id_board',
        ];
        call_integration_hook('integrate_load_board', array(&$custom_column_selects, &$custom_column_parameters));
        $request = $smcFunc['db_query']('load_board_info', '
            SELECT
                c.id_cat, b.name AS bname, b.description, b.num_topics, b.member_groups, b.deny_member_groups,
                b.id_parent, c.name AS cname, COALESCE(mg.id_group, 0) AS id_moderator_group, mg.group_name,
                COALESCE(mem.id_member, 0) AS id_moderator,
                mem.real_name' . (!empty($topic) ? ', b.id_board' : '') . ', b.child_level,
                b.id_theme, b.override_theme, b.count_posts, b.id_profile, b.redirect,
                b.unapproved_topics, b.unapproved_posts' . (!empty($topic) ? ', t.approved, t.id_member_started' : '') . '
                ' . (!empty($custom_column_selects) ? (', ' . implode(', ', $custom_column_selects)) : '') . '
            FROM {db_prefix}boards AS b' . (!empty($topic) ? '
                INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:current_topic})' : '') . '
                LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
                LEFT JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_board = {raw:board_link})
                LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = modgs.id_group)
                LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = {raw:board_link})
                LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
            WHERE b.id_board = {raw:board_link}',
            $custom_column_parameters
        );
        
        if ($smcFunc['db_num_rows']($request) > 0)
        {
            $row = $smcFunc['db_fetch_assoc']($request);
            
            if (!empty($row['id_board']))
                $board = $row['id_board'];
            
            $board_info = array(
                'id' => $board,
                'moderators' => array(),
                'moderator_groups' => array(),
                'cat' => array(
                    'id' => $row['id_cat'],
                    'name' => $row['cname']
                ),
                'name' => $row['bname'],
                'description' => $row['description'],
                'num_topics' => $row['num_topics'],
                'unapproved_topics' => $row['unapproved_topics'],
                'unapproved_posts' => $row['unapproved_posts'],
                'unapproved_user_topics' => 0,
                'parent_boards' => getBoardParents($row['id_parent']),
                'parent' => $row['id_parent'],
                'child_level' => $row['child_level'],
                'theme' => $row['id_theme'],
                'override_theme' => !empty($row['override_theme']),
                'profile' => $row['id_profile'],
                'redirect' => $row['redirect'],
                'recycle' => !empty($modSettings['recycle_enable']) && !empty($modSettings['recycle_board']) && $modSettings['recycle_board'] == $board,
                'posts_count' => empty($row['count_posts']),
                'cur_topic_approved' => empty($topic) || $row['approved'],
                'cur_topic_starter' => empty($topic) ? 0 : $row['id_member_started'],
            );
            
            $board_info['groups'] = $row['member_groups'] == '' ? array() : explode(',', $row['member_groups']);
            $board_info['deny_groups'] = $row['deny_member_groups'] == '' ? array() : explode(',', $row['deny_member_groups']);
            call_integration_hook('integrate_board_info', array(&$board_info, $row));
            if (!empty($modSettings['board_manager_groups']))
            {
                $board_info['groups'] = array_unique(array_merge($board_info['groups'], explode(',', $modSettings['board_manager_groups'])));
                $board_info['deny_groups'] = array_diff($board_info['deny_groups'], explode(',', $modSettings['board_manager_groups']));
            }
            do
            {
                if (!empty($row['id_moderator']))
                    $board_info['moderators'][$row['id_moderator']] = array(
                        'id' => $row['id_moderator'],
                        'name' => $row['real_name'],
                        'href' => $scripturl . '?action=profile;u=' . $row['id_moderator'],
                        'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_moderator'] . '">' . $row['real_name'] . '</a>'
                    );
                if (!empty($row['id_moderator_group']))
                    $board_info['moderator_groups'][$row['id_moderator_group']] = array(
                        'id' => $row['id_moderator_group'],
                        'name' => $row['group_name'],
                        'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'],
                        'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'] . '">' . $row['group_name'] . '</a>'
                    );
            }
            while ($row = $smcFunc['db_fetch_assoc']($request));
            
            
            if ($board_info['num_topics'] == 0 && $modSettings['postmod_active'] && !allowedTo('approve_posts'))
            {
                
                $smcFunc['db_free_result']($request);
                
                
                $request = $smcFunc['db_query']('', '
                    SELECT COUNT(id_topic)
                    FROM {db_prefix}topics
                    WHERE id_member_started={int:id_member}
                        AND approved = {int:unapproved}
                        AND id_board = {int:board}',
                    array(
                        'id_member' => $user_info['id'],
                        'unapproved' => 0,
                        'board' => $board,
                    )
                );
                list ($board_info['unapproved_user_topics']) = $smcFunc['db_fetch_row']($request);
            }
            if (!empty($cache_enable) && (empty($topic) || $cache_enable >= 3))
            {
                
                if (!empty($topic))
                    cache_put_data('topic_board-' . $topic, $board_info, 120);
                cache_put_data('board-' . $board, $board_info, 120);
            }
        }
        else
        {
            
            $board_info = array(
                'moderators' => array(),
                'moderator_groups' => array(),
                'error' => 'exist'
            );
            $topic = null;
            $board = 0;
        }
        $smcFunc['db_free_result']($request);
    }
    if (!empty($topic))
        $_GET['board'] = (int) $board;
    if (!empty($board))
    {
        
        $moderator_groups = array_keys($board_info['moderator_groups']);
        
        $user_info['is_mod'] = isset($board_info['moderators'][$user_info['id']]) || count(array_intersect($user_info['groups'], $moderator_groups)) != 0;
        if (count(array_intersect($user_info['groups'], $board_info['groups'])) == 0 && !$user_info['is_admin'])
            $board_info['error'] = 'access';
        if (!empty($modSettings['deny_boards_access']) && count(array_intersect($user_info['groups'], $board_info['deny_groups'])) != 0 && !$user_info['is_admin'])
            $board_info['error'] = 'access';
        
        $context['linktree'] = array_merge(
            $context['linktree'],
            array(array(
                'url' => $scripturl . '#c' . $board_info['cat']['id'],
                'name' => $board_info['cat']['name']
            )),
            array_reverse($board_info['parent_boards']),
            array(array(
                'url' => $scripturl . '?board=' . $board . '.0',
                'name' => $board_info['name']
            ))
        );
    }
    
    $context['user']['is_mod'] = &$user_info['is_mod'];
    $context['current_topic'] = $topic;
    $context['current_board'] = $board;
    
    if (!empty($_REQUEST['action']) && $_REQUEST['action'] == 'post' && !empty($board_info['redirect']))
        $board_info['error'] = 'post_in_redirect';
    
    if (!empty($board_info['error']) && (!empty($modSettings['deny_boards_access']) || $board_info['error'] != 'access' || !$user_info['is_mod']))
    {
        
        loadPermissions();
        loadTheme();
        $_GET['board'] = '';
        $_GET['topic'] = '';
        
        $context['linktree'] = array(
            array(
                'url' => $scripturl,
                'name' => $context['forum_name_html_safe']
            )
        );
        
        if ((isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') || (!empty($_REQUEST['action']) && $_REQUEST['action'] === 'dlattach'))
        {
            ob_end_clean();
            send_http_status(403);
            die;
        }
        elseif ($board_info['error'] == 'post_in_redirect')
        {
            
            fatal_lang_error('cannot_post_redirect', false);
        }
        elseif ($user_info['is_guest'])
        {
            loadLanguage('Errors');
            is_not_guest($txt['topic_gone']);
        }
        else
            fatal_lang_error('topic_gone', false);
    }
    if ($user_info['is_mod'])
        $user_info['groups'][] = 3;
}
function loadPermissions()
{
    global $user_info, $board, $board_info, $modSettings, $smcFunc, $sourcedir, $cache_enable;
    if ($user_info['is_admin'])
    {
        banPermissions();
        return;
    }
    if (!empty($cache_enable))
    {
        $cache_groups = $user_info['groups'];
        asort($cache_groups);
        $cache_groups = implode(',', $cache_groups);
        
        if ($user_info['possibly_robot'])
            $cache_groups .= '-spider';
        if ($cache_enable >= 2 && !empty($board) && ($temp = cache_get_data('permissions:' . $cache_groups . ':' . $board, 240)) != null && time() - 240 > $modSettings['settings_updated'])
        {
            list ($user_info['permissions']) = $temp;
            banPermissions();
            return;
        }
        elseif (($temp = cache_get_data('permissions:' . $cache_groups, 240)) != null && time() - 240 > $modSettings['settings_updated'])
            list ($user_info['permissions'], $removals) = $temp;
    }
    
    $spider_restrict = $user_info['possibly_robot'] && !empty($modSettings['spider_group']) ? ' OR (id_group = {int:spider_group} AND add_deny = 0)' : '';
    if (empty($user_info['permissions']))
    {
        
        $request = $smcFunc['db_query']('', '
            SELECT permission, add_deny
            FROM {db_prefix}permissions
            WHERE id_group IN ({array_int:member_groups})
                ' . $spider_restrict,
            array(
                'member_groups' => $user_info['groups'],
                'spider_group' => !empty($modSettings['spider_group']) ? $modSettings['spider_group'] : 0,
            )
        );
        $removals = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            if (empty($row['add_deny']))
                $removals[] = $row['permission'];
            else
                $user_info['permissions'][] = $row['permission'];
        }
        $smcFunc['db_free_result']($request);
        if (isset($cache_groups))
            cache_put_data('permissions:' . $cache_groups, array($user_info['permissions'], $removals), 240);
    }
    
    if (!empty($board))
    {
        
        if (!isset($board_info['profile']))
            fatal_lang_error('no_board');
        $request = $smcFunc['db_query']('', '
            SELECT permission, add_deny
            FROM {db_prefix}board_permissions
            WHERE (id_group IN ({array_int:member_groups})
                ' . $spider_restrict . ')
                AND id_profile = {int:id_profile}',
            array(
                'member_groups' => $user_info['groups'],
                'id_profile' => $board_info['profile'],
                'spider_group' => !empty($modSettings['spider_group']) ? $modSettings['spider_group'] : 0,
            )
        );
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            if (empty($row['add_deny']))
                $removals[] = $row['permission'];
            else
                $user_info['permissions'][] = $row['permission'];
        }
        $smcFunc['db_free_result']($request);
    }
    
    if (!empty($modSettings['permission_enable_deny']))
        $user_info['permissions'] = array_diff($user_info['permissions'], $removals);
    if (isset($cache_groups) && !empty($board) && $cache_enable >= 2)
        cache_put_data('permissions:' . $cache_groups . ':' . $board, array($user_info['permissions'], null), 240);
    
    banPermissions();
    
    if (!$user_info['is_guest'])
    {
        if (!isset($_SESSION['mc']) || $_SESSION['mc']['time'] <= $modSettings['settings_updated'])
        {
            require_once($sourcedir . '/Subs-Auth.php');
            rebuildModCache();
        }
        else
            $user_info['mod_cache'] = $_SESSION['mc'];
        
        
        $user_info['permissions'][] = 'is_not_guest';
        
        $user_info['permissions'][] = 'profile_view_own';
        if (in_array('profile_view', $user_info['permissions']))
            $user_info['permissions'][] = 'profile_view_any';
    }
}
function loadMemberData($users, $is_name = false, $set = 'normal')
{
    global $user_profile, $modSettings, $board_info, $smcFunc, $context;
    global $user_info, $cache_enable;
    
    if (empty($users))
        return array();
    
    $context['loadMemberContext_set'] = $set;
    
    $users = !is_array($users) ? array($users) : array_unique($users);
    $loaded_ids = array();
    if (!$is_name && !empty($cache_enable) && $cache_enable >= 3)
    {
        $users = array_values($users);
        for ($i = 0, $n = count($users); $i < $n; $i++)
        {
            $data = cache_get_data('member_data-' . $set . '-' . $users[$i], 240);
            if ($data == null)
                continue;
            $loaded_ids[] = $data['id_member'];
            $user_profile[$data['id_member']] = $data;
            unset($users[$i]);
        }
    }
    
    $select_columns = '
            COALESCE(lo.log_time, 0) AS is_online, COALESCE(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type,
            mem.signature, mem.personal_text, mem.avatar, mem.id_member, mem.member_name,
            mem.real_name, mem.email_address, mem.date_registered, mem.website_title, mem.website_url,
            mem.birthdate, mem.member_ip, mem.member_ip2, mem.posts, mem.last_login, mem.id_post_group, mem.lngfile, mem.id_group, mem.time_offset, mem.timezone, mem.show_online,
            mg.online_color AS member_group_color, COALESCE(mg.group_name, {string:blank_string}) AS member_group,
            pg.online_color AS post_group_color, COALESCE(pg.group_name, {string:blank_string}) AS post_group,
            mem.is_activated, mem.warning, ' . (!empty($modSettings['titlesEnable']) ? 'mem.usertitle, ' : '') . '
            CASE WHEN mem.id_group = 0 OR mg.icons = {string:blank_string} THEN pg.icons ELSE mg.icons END AS icons';
    $select_tables = '
            LEFT JOIN {db_prefix}log_online AS lo ON (lo.id_member = mem.id_member)
            LEFT JOIN {db_prefix}attachments AS a ON (a.id_member = mem.id_member)
            LEFT JOIN {db_prefix}membergroups AS pg ON (pg.id_group = mem.id_post_group)
            LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)';
    
    switch ($set)
    {
        case 'normal':
            $select_columns .= ', mem.buddy_list,  mem.additional_groups';
            break;
        case 'profile':
            $select_columns .= ', mem.additional_groups, mem.id_theme, mem.pm_ignore_list, mem.pm_receive_from,
            mem.time_format, mem.timezone, mem.secret_question, mem.smiley_set, mem.tfa_secret,
            mem.total_time_logged_in, lo.url, mem.ignore_boards, mem.password_salt, mem.pm_prefs, mem.buddy_list, mem.alerts';
            break;
        case 'minimal':
            $select_columns = '
            mem.id_member, mem.member_name, mem.real_name, mem.email_address, mem.date_registered,
            mem.posts, mem.last_login, mem.member_ip, mem.member_ip2, mem.lngfile, mem.id_group';
            $select_tables = '';
            break;
        default:
            trigger_error('loadMemberData(): Invalid member data set \'' . $set . '\'', E_USER_WARNING);
    }
    
    call_integration_hook('integrate_load_member_data', array(&$select_columns, &$select_tables, &$set));
    if (!empty($users))
    {
        
        $request = $smcFunc['db_query']('', '
            SELECT' . $select_columns . '
            FROM {db_prefix}members AS mem' . $select_tables . '
            WHERE mem.' . ($is_name ? 'member_name' : 'id_member') . ' IN ({' . ($is_name ? 'array_string' : 'array_int') . ':users})',
            array(
                'blank_string' => '',
                'users' => $users,
            )
        );
        $new_loaded_ids = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            
            $row['avatar_original'] = !empty($row['avatar']) ? $row['avatar'] : '';
            
            if (!empty($row['avatar']))
                $row['avatar'] = get_proxied_url($row['avatar']);
            
            $row['primary_group'] = !empty($row['member_group']) ? $row['member_group'] : '';
            if (isset($row['member_ip']))
                $row['member_ip'] = inet_dtop($row['member_ip']);
            if (isset($row['member_ip2']))
                $row['member_ip2'] = inet_dtop($row['member_ip2']);
            $row['id_member'] = (int) $row['id_member'];
            $new_loaded_ids[] = $row['id_member'];
            $loaded_ids[] = $row['id_member'];
            $row['options'] = array();
            $user_profile[$row['id_member']] = $row;
        }
        $smcFunc['db_free_result']($request);
    }
    if (!empty($new_loaded_ids) && $set !== 'minimal')
    {
        $request = $smcFunc['db_query']('', '
            SELECT id_member, variable, value
            FROM {db_prefix}themes
            WHERE id_member IN ({array_int:loaded_ids})',
            array(
                'loaded_ids' => $new_loaded_ids,
            )
        );
        while ($row = $smcFunc['db_fetch_assoc']($request))
            $user_profile[$row['id_member']]['options'][$row['variable']] = $row['value'];
        $smcFunc['db_free_result']($request);
    }
    $additional_mods = array();
    
    if (!empty($loaded_ids) && !empty($board_info['moderator_groups']) && $set === 'normal')
    {
        foreach ($loaded_ids as $a_member)
        {
            if (!empty($user_profile[$a_member]['additional_groups']))
                $groups = array_merge(array($user_profile[$a_member]['id_group']), explode(',', $user_profile[$a_member]['additional_groups']));
            else
                $groups = array($user_profile[$a_member]['id_group']);
            $temp = array_intersect($groups, array_keys($board_info['moderator_groups']));
            if (!empty($temp))
            {
                $additional_mods[] = $a_member;
            }
        }
    }
    if (!empty($new_loaded_ids) && !empty($cache_enable) && $cache_enable >= 3)
    {
        for ($i = 0, $n = count($new_loaded_ids); $i < $n; $i++)
            cache_put_data('member_data-' . $set . '-' . $new_loaded_ids[$i], $user_profile[$new_loaded_ids[$i]], 240);
    }
    
    if (!empty($loaded_ids) && (!empty($board_info['moderators']) || !empty($board_info['moderator_groups'])) && $set === 'normal' && count($temp_mods = array_merge(array_intersect($loaded_ids, array_keys($board_info['moderators'])), $additional_mods)) !== 0)
    {
        if (($row = cache_get_data('moderator_group_info', 480)) == null)
        {
            $request = $smcFunc['db_query']('', '
                SELECT group_name AS member_group, online_color AS member_group_color, icons
                FROM {db_prefix}membergroups
                WHERE id_group = {int:moderator_group}
                LIMIT 1',
                array(
                    'moderator_group' => 3,
                )
            );
            $row = $smcFunc['db_fetch_assoc']($request);
            $smcFunc['db_free_result']($request);
            cache_put_data('moderator_group_info', $row, 480);
        }
        foreach ($temp_mods as $id)
        {
            
            if ($user_profile[$id]['id_group'] != 1 && $user_profile[$id]['id_group'] != 2)
                $user_profile[$id]['member_group'] = $row['member_group'];
            
            if (!empty($row['icons']))
                $user_profile[$id]['icons'] = $row['icons'];
            if (!empty($row['member_group_color']))
                $user_profile[$id]['member_group_color'] = $row['member_group_color'];
        }
    }
    return $loaded_ids;
}
function loadMemberContext($user, $display_custom_fields = false)
{
    global $memberContext, $user_profile, $txt, $scripturl, $user_info;
    global $context, $modSettings, $settings, $smcFunc;
    static $dataLoaded = array();
    static $loadedLanguages = array();
    
    if (isset($dataLoaded[$user]))
        return true;
    
    if ($user == 0)
        return false;
    if (!isset($user_profile[$user]))
    {
        trigger_error('loadMemberContext(): member id ' . $user . ' not previously loaded by loadMemberData()', E_USER_WARNING);
        return false;
    }
    
    $dataLoaded[$user] = true;
    $profile = $user_profile[$user];
    
    censorText($profile['signature']);
    censorText($profile['personal_text']);
    
    $profile['signature'] = str_replace(array("\n", "\r"), array('<br>', ''), $profile['signature']);
    $profile['signature'] = parse_bbc($profile['signature'], true, 'sig' . $profile['id_member']);
    $profile['is_online'] = (!empty($profile['show_online']) || allowedTo('moderate_forum')) && $profile['is_online'] > 0;
    $profile['icons'] = empty($profile['icons']) ? array('', '') : explode('#', $profile['icons']);
    
    $profile['buddy'] = in_array($profile['id_member'], $user_info['buddies']);
    $buddy_list = !empty($profile['buddy_list']) ? explode(',', $profile['buddy_list']) : array();
    
    if (isset($profile['icons'][1]) && file_exists($settings['actual_theme_dir'] . '/images/membericons/' . $profile['icons'][1])) 
        $group_icon_url = $settings['images_url'] . '/membericons/' . $profile['icons'][1];
    elseif (isset($profile['icons'][1])) 
        $group_icon_url = $settings['default_images_url'] . '/membericons/' . $profile['icons'][1];
    else 
        $group_icon_url = '';
    
    $memberContext[$user] = array(
        'username' => $profile['member_name'],
        'name' => $profile['real_name'],
        'id' => $profile['id_member'],
        'href' => $scripturl . '?action=profile;u=' . $profile['id_member'],
        'link' => '<a href="' . $scripturl . '?action=profile;u=' . $profile['id_member'] . '" title="' . $txt['profile_of'] . ' ' . $profile['real_name'] . '" ' . (!empty($modSettings['onlineEnable']) ? 'class="pm_icon"' : '') . '>' . $profile['real_name'] . '</a>',
        'email' => $profile['email_address'],
        'show_email' => !$user_info['is_guest'] && ($user_info['id'] == $profile['id_member'] || allowedTo('moderate_forum')),
        'registered' => empty($profile['date_registered']) ? $txt['not_applicable'] : timeformat($profile['date_registered']),
        'registered_timestamp' => empty($profile['date_registered']) ? 0 : forum_time(true, $profile['date_registered']),
    );
    
    if ($context['loadMemberContext_set'] != 'minimal')
    {
        
        if (empty($loadedLanguages))
            $loadedLanguages = getLanguages();
        
        if (!empty($profile['timezone']))
        {
            
            $tz_system = new DateTimeZone(@date_default_timezone_get());
            $tz_user = new DateTimeZone($profile['timezone']);
            $time_system = new DateTime('now', $tz_system);
            $time_user = new DateTime('now', $tz_user);
            $profile['time_offset'] = ($tz_user->getOffset($time_user) - $tz_system->getOffset($time_system)) / 3600;
        }
        else
        {
            
            $profile['time_offset'] = empty($profile['time_offset']) ? 0 : $profile['time_offset'];
        }
        $memberContext[$user] += array(
            'username_color' => '<span ' . (!empty($profile['member_group_color']) ? 'style="color:' . $profile['member_group_color'] . ';"' : '') . '>' . $profile['member_name'] . '</span>',
            'name_color' => '<span ' . (!empty($profile['member_group_color']) ? 'style="color:' . $profile['member_group_color'] . ';"' : '') . '>' . $profile['real_name'] . '</span>',
            'link_color' => '<a href="' . $scripturl . '?action=profile;u=' . $profile['id_member'] . '" title="' . $txt['profile_of'] . ' ' . $profile['real_name'] . '" ' . (!empty($profile['member_group_color']) ? 'style="color:' . $profile['member_group_color'] . ';"' : '') . '>' . $profile['real_name'] . '</a>',
            'is_buddy' => $profile['buddy'],
            'is_reverse_buddy' => in_array($user_info['id'], $buddy_list),
            'buddies' => $buddy_list,
            'title' => !empty($modSettings['titlesEnable']) ? $profile['usertitle'] : '',
            'blurb' => $profile['personal_text'],
            'website' => array(
                'title' => $profile['website_title'],
                'url' => $profile['website_url'],
            ),
            'birth_date' => empty($profile['birthdate']) ? '1004-01-01' : (substr($profile['birthdate'], 0, 4) === '0004' ? '1004' . substr($profile['birthdate'], 4) : $profile['birthdate']),
            'signature' => $profile['signature'],
            'real_posts' => $profile['posts'],
            'posts' => $profile['posts'] > 500000 ? $txt['geek'] : comma_format($profile['posts']),
            'last_login' => empty($profile['last_login']) ? $txt['never'] : timeformat($profile['last_login']),
            'last_login_timestamp' => empty($profile['last_login']) ? 0 : forum_time(0, $profile['last_login']),
            'ip' => $smcFunc['htmlspecialchars']($profile['member_ip']),
            'ip2' => $smcFunc['htmlspecialchars']($profile['member_ip2']),
            'online' => array(
                'is_online' => $profile['is_online'],
                'text' => $smcFunc['htmlspecialchars']($txt[$profile['is_online'] ? 'online' : 'offline']),
                'member_online_text' => sprintf($txt[$profile['is_online'] ? 'member_is_online' : 'member_is_offline'], $smcFunc['htmlspecialchars']($profile['real_name'])),
                'href' => $scripturl . '?action=pm;sa=send;u=' . $profile['id_member'],
                'link' => '<a href="' . $scripturl . '?action=pm;sa=send;u=' . $profile['id_member'] . '">' . $txt[$profile['is_online'] ? 'online' : 'offline'] . '</a>',
                'label' => $txt[$profile['is_online'] ? 'online' : 'offline']
            ),
            'language' => !empty($loadedLanguages[$profile['lngfile']]) && !empty($loadedLanguages[$profile['lngfile']]['name']) ? $loadedLanguages[$profile['lngfile']]['name'] : $smcFunc['ucwords'](strtr($profile['lngfile'], array('_' => ' ', '-utf8' => ''))),
            'is_activated' => isset($profile['is_activated']) ? $profile['is_activated'] : 1,
            'is_banned' => isset($profile['is_activated']) ? $profile['is_activated'] >= 10 : 0,
            'options' => $profile['options'],
            'is_guest' => false,
            'primary_group' => $profile['primary_group'],
            'group' => $profile['member_group'],
            'group_color' => $profile['member_group_color'],
            'group_id' => $profile['id_group'],
            'post_group' => $profile['post_group'],
            'post_group_color' => $profile['post_group_color'],
            'group_icons' => str_repeat('<img src="' . str_replace('$language', $context['user']['language'], isset($profile['icons'][1]) ? $group_icon_url : '') . '" alt="*">', empty($profile['icons'][0]) || empty($profile['icons'][1]) ? 0 : $profile['icons'][0]),
            'warning' => $profile['warning'],
            'warning_status' => !empty($modSettings['warning_mute']) && $modSettings['warning_mute'] <= $profile['warning'] ? 'mute' : (!empty($modSettings['warning_moderate']) && $modSettings['warning_moderate'] <= $profile['warning'] ? 'moderate' : (!empty($modSettings['warning_watch']) && $modSettings['warning_watch'] <= $profile['warning'] ? 'watch' : (''))),
            'local_time' => timeformat(time() + ($profile['time_offset'] - $user_info['time_offset']) * 3600, false),
            'custom_fields' => array(),
        );
    }
    
    if ($context['loadMemberContext_set'] != 'minimal')
    {
        if (!empty($modSettings['gravatarOverride']) || (!empty($modSettings['gravatarEnabled']) && stristr($profile['avatar'], 'gravatar://')))
        {
            if (!empty($modSettings['gravatarAllowExtraEmail']) && stristr($profile['avatar'], 'gravatar://') && strlen($profile['avatar']) > 11)
                $image = get_gravatar_url($smcFunc['substr']($profile['avatar'], 11));
            else
                $image = get_gravatar_url($profile['email_address']);
        }
        else
        {
            
            if (!empty($profile['avatar']))
            {
                $image = (stristr($profile['avatar'], 'http://') || stristr($profile['avatar'], 'https://')) ? $profile['avatar'] : $modSettings['avatar_url'] . '/' . $profile['avatar'];
            }
            elseif (!empty($profile['filename']))
                $image = $modSettings['custom_avatar_url'] . '/' . $profile['filename'];
            
            else
                $image = $modSettings['avatar_url'] . '/default.png';
        }
        if (!empty($image))
            $memberContext[$user]['avatar'] = array(
                'name' => $profile['avatar'],
                'image' => '<img class="avatar" src="' . $image . '" alt="">',
                'href' => $image,
                'url' => $image,
            );
    }
    
    if ($display_custom_fields && !empty($modSettings['displayFields']))
    {
        $memberContext[$user]['custom_fields'] = array();
        if (!isset($context['display_fields']))
            $context['display_fields'] = $smcFunc['json_decode']($modSettings['displayFields'], true);
        foreach ($context['display_fields'] as $custom)
        {
            if (!isset($custom['col_name']) || trim($custom['col_name']) == '' || empty($profile['options'][$custom['col_name']]))
                continue;
            $value = $profile['options'][$custom['col_name']];
            $fieldOptions = array();
            $currentKey = 0;
            
            if (!empty($custom['options']))
                foreach ($custom['options'] as $k => $v)
                {
                    $fieldOptions[] = $v;
                    if (empty($currentKey))
                        $currentKey = $v == $value ? $k : 0;
                }
            
            if ($custom['bbc'])
                $value = parse_bbc($value);
            
            elseif (isset($custom['type']) && $custom['type'] == 'check')
                $value = $value ? $txt['yes'] : $txt['no'];
            
            if (!empty($custom['enclose']))
                $value = strtr($custom['enclose'], array(
                    '{SCRIPTURL}' => $scripturl,
                    '{IMAGES_URL}' => $settings['images_url'],
                    '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
                    '{INPUT}' => $value,
                    '{KEY}' => $currentKey,
                ));
            $memberContext[$user]['custom_fields'][] = array(
                'title' => !empty($custom['title']) ? $custom['title'] : $custom['col_name'],
                'col_name' => $custom['col_name'],
                'value' => un_htmlspecialchars($value),
                'placement' => !empty($custom['placement']) ? $custom['placement'] : 0,
            );
        }
    }
    call_integration_hook('integrate_member_context', array(&$memberContext[$user], $user, $display_custom_fields));
    return true;
}
function loadMemberCustomFields($users, $params)
{
    global $smcFunc, $txt, $scripturl, $settings;
    
    if (empty($users) || empty($params))
        return false;
    
    $users = !is_array($users) ? array($users) : array_unique($users);
    $params = !is_array($params) ? array($params) : array_unique($params);
    $return = array();
    $request = $smcFunc['db_query']('', '
        SELECT c.id_field, c.col_name, c.field_name, c.field_desc, c.field_type, c.field_order, c.field_length, c.field_options, c.mask, show_reg,
        c.show_display, c.show_profile, c.private, c.active, c.bbc, c.can_search, c.default_value, c.enclose, c.placement, t.variable, t.value, t.id_member
        FROM {db_prefix}themes AS t
            LEFT JOIN {db_prefix}custom_fields AS c ON (c.col_name = t.variable)
        WHERE id_member IN ({array_int:loaded_ids})
            AND variable IN ({array_string:params})
        ORDER BY field_order',
        array(
            'loaded_ids' => $users,
            'params' => $params,
        )
    );
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $fieldOptions = array();
        $currentKey = 0;
        
        if (!empty($row['field_options']))
            foreach (explode(',', $row['field_options']) as $k => $v)
            {
                $fieldOptions[] = $v;
                if (empty($currentKey))
                    $currentKey = $v == $row['value'] ? $k : 0;
            }
        
        if (!empty($row['bbc']))
            $row['value'] = parse_bbc($row['value']);
        
        elseif (isset($row['type']) && $row['type'] == 'check')
            $row['value'] = !empty($row['value']) ? $txt['yes'] : $txt['no'];
        
        if (!empty($row['enclose']))
            $row['value'] = strtr($row['enclose'], array(
                '{SCRIPTURL}' => $scripturl,
                '{IMAGES_URL}' => $settings['images_url'],
                '{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
                '{INPUT}' => un_htmlspecialchars($row['value']),
                '{KEY}' => $currentKey,
            ));
        
        if (count($params) == 1)
            $return[$row['id_member']] = $row;
        
        else
        {
            if (!isset($return[$row['id_member']]))
                $return[$row['id_member']] = array();
            $return[$row['id_member']][$row['variable']] = $row;
        }
    }
    $smcFunc['db_free_result']($request);
    return !empty($return) ? $return : false;
}
function detectBrowser()
{
    
    $detector = new browser_detector;
    $detector->detectBrowser();
}
function isBrowser($browser)
{
    global $context;
    
    if (empty($context['browser']))
        detectBrowser();
    return !empty($context['browser'][$browser]) || !empty($context['browser']['is_' . $browser]) ? true : false;
}
function loadTheme($id_theme = 0, $initialize = true)
{
    global $user_info, $user_settings, $board_info, $boarddir, $maintenance;
    global $txt, $boardurl, $scripturl, $mbname, $modSettings;
    global $context, $settings, $options, $sourcedir, $smcFunc, $language, $board, $cache_enable;
    if (empty($id_theme))
    {
        
        if (!empty($board_info['theme']))
            $id_theme = $board_info['theme'];
        
        else
            $id_theme = $modSettings['theme_guests'];
        
        if (!empty($modSettings['theme_allow']) || allowedTo('admin_forum'))
        {
            
            if (!empty($_REQUEST['theme']) && (allowedTo('admin_forum') || in_array($_REQUEST['theme'], explode(',', $modSettings['knownThemes']))))
            {
                $id_theme = (int) $_REQUEST['theme'];
                $_SESSION['id_theme'] = $id_theme;
            }
            
            elseif (!empty($_SESSION['id_theme']))
                $id_theme = (int) $_SESSION['id_theme'];
            
            elseif (!empty($user_info['theme']))
                $id_theme = $user_info['theme'];
        }
        
        
        if (!empty($board_info['theme']) && $board_info['override_theme'])
            $id_theme = $board_info['theme'];
        elseif (!empty($modSettings['enableThemes']))
        {
            $themes = explode(',', $modSettings['enableThemes']);
            if (!in_array($id_theme, $themes))
                $id_theme = $modSettings['theme_guests'];
            else
                $id_theme = (int) $id_theme;
        }
    }
    
    call_integration_hook('integrate_pre_load_theme', array(&$id_theme));
    
    if (empty($settings['theme_id']) || $settings['theme_id'] != $id_theme)
    {
        $member = empty($user_info['id']) ? -1 : $user_info['id'];
        if (!empty($cache_enable) && $cache_enable >= 2 && ($temp = cache_get_data('theme_settings-' . $id_theme . ':' . $member, 60)) != null && time() - 60 > $modSettings['settings_updated'])
        {
            $themeData = $temp;
            $flag = true;
        }
        elseif (($temp = cache_get_data('theme_settings-' . $id_theme, 90)) != null && time() - 60 > $modSettings['settings_updated'])
            $themeData = $temp + array($member => array());
        else
            $themeData = array(-1 => array(), 0 => array(), $member => array());
        if (empty($flag))
        {
            
            $result = $smcFunc['db_query']('', '
                SELECT variable, value, id_member, id_theme
                FROM {db_prefix}themes
                WHERE id_member' . (empty($themeData[0]) ? ' IN (-1, 0, {int:id_member})' : ' = {int:id_member}') . '
                    AND id_theme' . ($id_theme == 1 ? ' = {int:id_theme}' : ' IN ({int:id_theme}, 1)') . '
                ORDER BY id_theme asc',
                array(
                    'id_theme' => $id_theme,
                    'id_member' => $member,
                )
            );
            
            foreach ($smcFunc['db_fetch_all']($result) as $row)
            {
                
                if ($row['id_member'] != 0 && in_array($row['variable'], array('actual_theme_url', 'actual_images_url', 'base_theme_dir', 'base_theme_url', 'default_images_url', 'default_theme_dir', 'default_theme_url', 'default_template', 'images_url', 'number_recent_posts', 'smiley_sets_default', 'theme_dir', 'theme_id', 'theme_layers', 'theme_templates', 'theme_url')))
                    continue;
                
                if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1' && empty($row['id_member']))
                    $themeData[0]['default_' . $row['variable']] = $row['value'];
                
                if (!isset($themeData[$row['id_member']][$row['variable']]) || $row['id_theme'] != '1')
                    $themeData[$row['id_member']][$row['variable']] = substr($row['variable'], 0, 5) == 'show_' ? $row['value'] == '1' : $row['value'];
            }
            $smcFunc['db_free_result']($result);
            if (!empty($themeData[-1]))
                foreach ($themeData[-1] as $k => $v)
                {
                    if (!isset($themeData[$member][$k]))
                        $themeData[$member][$k] = $v;
                }
            if (!empty($cache_enable) && $cache_enable >= 2)
                cache_put_data('theme_settings-' . $id_theme . ':' . $member, $themeData, 60);
            
            elseif (!isset($temp))
                cache_put_data('theme_settings-' . $id_theme, array(-1 => $themeData[-1], 0 => $themeData[0]), 90);
        }
        $settings = $themeData[0];
        $options = $themeData[$member];
        $settings['theme_id'] = $id_theme;
        $settings['actual_theme_url'] = $settings['theme_url'];
        $settings['actual_images_url'] = $settings['images_url'];
        $settings['actual_theme_dir'] = $settings['theme_dir'];
        $settings['template_dirs'] = array();
        
        $settings['template_dirs'][] = $settings['theme_dir'];
        
        if (!empty($settings['base_theme_dir']))
            $settings['template_dirs'][] = $settings['base_theme_dir'];
        
        if ($settings['theme_dir'] != $settings['default_theme_dir'])
            $settings['template_dirs'][] = $settings['default_theme_dir'];
    }
    if (!$initialize)
        return;
    
    if (!empty($modSettings['force_ssl']) && empty($maintenance) &&
        !httpsOn() && SMF != 'SSI')
    {
        if (isset($_GET['sslRedirect']))
        {
            loadLanguage('Errors');
            fatal_lang_error('login_ssl_required', false);
        }
        redirectexit(strtr($_SERVER['REQUEST_URL'], array('http://' => 'https://')) . (strpos($_SERVER['REQUEST_URL'], '?') > 0 ? ';' : '?') . 'sslRedirect');
    }
    
    if (isset($_SERVER['HTTP_HOST']) || isset($_SERVER['SERVER_NAME']))
    {
        $detected_url = httpsOn() ? 'https://' : 'http://';
        $detected_url .= empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']) : $_SERVER['HTTP_HOST'];
        $temp = preg_replace('~/' . basename($scripturl) . '(/.+)?$~', '', strtr(dirname($_SERVER['PHP_SELF']), '\\', '/'));
        if ($temp != '/')
            $detected_url .= $temp;
    }
    if (isset($detected_url) && $detected_url != $boardurl)
    {
        
        if (!empty($modSettings['forum_alias_urls']))
        {
            $aliases = explode(',', $modSettings['forum_alias_urls']);
            foreach ($aliases as $alias)
            {
                
                if ($detected_url == trim($alias) || strtr($detected_url, array('http://' => '', 'https://' => '')) == trim($alias))
                    $do_fix = true;
            }
        }
        
        if (empty($do_fix) && strtr($detected_url, array('://' => '://www.')) == $boardurl && (empty($_GET) || count($_GET) == 1) && SMF != 'SSI')
        {
            
            if (empty($_GET))
                redirectexit('wwwRedirect');
            else
            {
                $k = key($_GET);
                $v = current($_GET);
                if ($k != 'wwwRedirect')
                    redirectexit('wwwRedirect;' . $k . '=' . $v);
            }
        }
        
        if (strtr($detected_url, array('https://' => 'http://')) == $boardurl)
            $do_fix = true;
        
        if (!empty($do_fix) || preg_match('~^http[s]?://(?:[\d\.:]+|\[[\d:]+\](?::\d+)?)(?:$|/)~', $detected_url) == 1)
        {
            
            $oldurl = $boardurl;
            
            $boardurl = $detected_url;
            $scripturl = strtr($scripturl, array($oldurl => $boardurl));
            $_SERVER['REQUEST_URL'] = strtr($_SERVER['REQUEST_URL'], array($oldurl => $boardurl));
            
            $settings['theme_url'] = strtr($settings['theme_url'], array($oldurl => $boardurl));
            $settings['default_theme_url'] = strtr($settings['default_theme_url'], array($oldurl => $boardurl));
            $settings['actual_theme_url'] = strtr($settings['actual_theme_url'], array($oldurl => $boardurl));
            $settings['images_url'] = strtr($settings['images_url'], array($oldurl => $boardurl));
            $settings['default_images_url'] = strtr($settings['default_images_url'], array($oldurl => $boardurl));
            $settings['actual_images_url'] = strtr($settings['actual_images_url'], array($oldurl => $boardurl));
            
            $modSettings['smileys_url'] = strtr($modSettings['smileys_url'], array($oldurl => $boardurl));
            $modSettings['avatar_url'] = strtr($modSettings['avatar_url'], array($oldurl => $boardurl));
            $modSettings['custom_avatar_url'] = strtr($modSettings['custom_avatar_url'], array($oldurl => $boardurl));
            
            if (isset($board_info['moderators']))
            {
                foreach ($board_info['moderators'] as $k => $dummy)
                {
                    $board_info['moderators'][$k]['href'] = strtr($dummy['href'], array($oldurl => $boardurl));
                    $board_info['moderators'][$k]['link'] = strtr($dummy['link'], array('"' . $oldurl => '"' . $boardurl));
                }
            }
            foreach ($context['linktree'] as $k => $dummy)
                $context['linktree'][$k]['url'] = strtr($dummy['url'], array($oldurl => $boardurl));
        }
    }
    
    if (!empty($user_info))
    {
        $context['user'] = array(
            'id' => $user_info['id'],
            'is_logged' => !$user_info['is_guest'],
            'is_guest' => &$user_info['is_guest'],
            'is_admin' => &$user_info['is_admin'],
            'is_mod' => &$user_info['is_mod'],
            
            'can_mod' => allowedTo('access_mod_center') || (!$user_info['is_guest'] && ($user_info['mod_cache']['gq'] != '0=1' || $user_info['mod_cache']['bq'] != '0=1' || ($modSettings['postmod_active'] && !empty($user_info['mod_cache']['ap'])))),
            'name' => $user_info['username'],
            'language' => $user_info['language'],
            'email' => $user_info['email'],
            'ignoreusers' => $user_info['ignoreusers'],
        );
        if (!$context['user']['is_guest'])
            $context['user']['name'] = $user_info['name'];
        elseif ($context['user']['is_guest'] && !empty($txt['guest_title']))
            $context['user']['name'] = $txt['guest_title'];
        
        $smiley_sets_known = explode(',', $modSettings['smiley_sets_known']);
        $user_info['smiley_set'] = (!in_array($user_info['smiley_set'], $smiley_sets_known) && $user_info['smiley_set'] != 'none') || empty($modSettings['smiley_sets_enable']) ? (!empty($settings['smiley_sets_default']) ? $settings['smiley_sets_default'] : $modSettings['smiley_sets_default']) : $user_info['smiley_set'];
        $context['user']['smiley_set'] = $user_info['smiley_set'];
    }
    else
    {
        
        $context['user'] = array(
            'id' => -1,
            'is_logged' => false,
            'is_guest' => true,
            'is_mod' => false,
            'can_mod' => false,
            'name' => $txt['guest_title'],
            'language' => $language,
            'email' => '',
            'ignoreusers' => array(),
        );
        
        $user_info = array(
            'id' => 0,
            'is_guest' => true,
            'is_admin' => false,
            'is_mod' => false,
            'username' => $txt['guest_title'],
            'language' => $language,
            'email' => '',
            'smiley_set' => '',
            'permissions' => array(),
            'groups' => array(),
            'ignoreusers' => array(),
            'possibly_robot' => true,
            'time_offset' => 0,
            'time_format' => $modSettings['time_format'],
        );
    }
    
    if (!isset($context['html_headers']))
        $context['html_headers'] = '';
    if (!isset($context['javascript_files']))
        $context['javascript_files'] = array();
    if (!isset($context['css_files']))
        $context['css_files'] = array();
    if (!isset($context['css_header']))
        $context['css_header'] = array();
    if (!isset($context['javascript_inline']))
        $context['javascript_inline'] = array('standard' => array(), 'defer' => array());
    if (!isset($context['javascript_vars']))
        $context['javascript_vars'] = array();
    $context['login_url'] = $scripturl . '?action=login2';
    $context['menu_separator'] = !empty($settings['use_image_buttons']) ? ' ' : ' | ';
    $context['session_var'] = $_SESSION['session_var'];
    $context['session_id'] = $_SESSION['session_value'];
    $context['forum_name'] = $mbname;
    $context['forum_name_html_safe'] = $smcFunc['htmlspecialchars']($context['forum_name']);
    $context['header_logo_url_html_safe'] = empty($settings['header_logo_url']) ? '' : $smcFunc['htmlspecialchars']($settings['header_logo_url']);
    $context['current_action'] = isset($_REQUEST['action']) ? $smcFunc['htmlspecialchars']($_REQUEST['action']) : null;
    $context['current_subaction'] = isset($_REQUEST['sa']) ? $_REQUEST['sa'] : null;
    $context['can_register'] = empty($modSettings['registration_method']) || $modSettings['registration_method'] != 3;
    if (isset($modSettings['load_average']))
        $context['load_average'] = $modSettings['load_average'];
    
    detectBrowser();
    
    
    if (empty($context['linktree']))
        $context['linktree'] = array();
    array_unshift($context['linktree'], array(
        'url' => $scripturl,
        'name' => $context['forum_name_html_safe']
    ));
    
    $context['insert_after_template'] = '';
    if (!isset($txt))
        $txt = array();
    $simpleActions = array(
        'findmember',
        'helpadmin',
        'printpage',
        'spellcheck',
    );
    
    $simpleAreas = array(
        'profile' => array('popup', 'alerts_popup',),
    );
    
    $simpleSubActions = array(
        'pm' => array('popup',),
        'signup' => array('usernamecheck'),
    );
    
    $extraParams = array(
        'preview',
        'splitjs',
    );
    
    $xmlActions = array(
        'quotefast',
        'jsmodify',
        'xmlhttp',
        'post2',
        'suggest',
        'stats',
        'notifytopic',
        'notifyboard',
    );
    call_integration_hook('integrate_simple_actions', array(&$simpleActions, &$simpleAreas, &$simpleSubActions, &$extraParams, &$xmlActions));
    $context['simple_action'] = in_array($context['current_action'], $simpleActions) ||
        (isset($simpleAreas[$context['current_action']]) && isset($_REQUEST['area']) && in_array($_REQUEST['area'], $simpleAreas[$context['current_action']])) ||
        (isset($simpleSubActions[$context['current_action']]) && in_array($context['current_subaction'], $simpleSubActions[$context['current_action']]));
    
    $requiresXML = false;
    foreach ($extraParams as $key => $extra)
        if (isset($_REQUEST[$extra]))
            $requiresXML = true;
    
    if (isset($_REQUEST['xml']) && (in_array($context['current_action'], $xmlActions) || $requiresXML))
    {
        loadLanguage('index+Modifications');
        loadTemplate('Xml');
        $context['template_layers'] = array();
    }
    
    elseif (!empty($context['simple_action']))
    {
        loadLanguage('index+Modifications');
        $context['template_layers'] = array();
    }
    else
    {
        
        if (isset($settings['theme_templates']))
            $templates = explode(',', $settings['theme_templates']);
        else
            $templates = array('index');
        
        foreach ($templates as $template)
            loadTemplate($template);
        
        $required_files = implode('+', array_merge($templates, array('Modifications')));
        loadLanguage($required_files, '', false);
        
        if (isset($settings['theme_layers']))
            $context['template_layers'] = explode(',', $settings['theme_layers']);
        else
            $context['template_layers'] = array('html', 'body');
    }
    
    loadSubTemplate('init', 'ignore');
    
    if (empty($user_settings['time_format']) && !empty($txt['time_format']))
        $user_info['time_format'] = $txt['time_format'];
    
    $context['character_set'] = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
    $context['right_to_left'] = !empty($txt['lang_rtl']);
    
    if ($context['user']['is_guest'] && empty($context['user']['name']))
        $context['user']['name'] = $txt['guest_title'];
    
    if (!empty($settings['require_theme_strings']))
        loadLanguage('ThemeStrings', '', false);
    
    $settings['lang_images_url'] = $settings['images_url'] . '/' . (!empty($txt['image_lang']) ? $txt['image_lang'] : $user_info['language']);
    
    loadCSSFile('index.css', array('minimize' => true, 'order_pos' => 1), 'smf_index');
    
    loadCSSFile('responsive.css', array('force_current' => false, 'validate' => true, 'minimize' => true, 'order_pos' => 9000), 'smf_responsive');
    if ($context['right_to_left'])
        loadCSSFile('rtl.css', array('order_pos' => 4000), 'smf_rtl');
    
    $context['theme_variant'] = '';
    $context['theme_variant_url'] = '';
    if (!empty($settings['theme_variants']))
    {
        
        if (!empty($_REQUEST['variant']))
            $_SESSION['id_variant'] = $_REQUEST['variant'];
        
        if (empty($settings['disable_user_variant']) || allowedTo('admin_forum'))
            $context['theme_variant'] = !empty($_SESSION['id_variant']) ? $_SESSION['id_variant'] : (!empty($options['theme_variant']) ? $options['theme_variant'] : '');
        
        if ($context['theme_variant'] == '' || !in_array($context['theme_variant'], $settings['theme_variants']))
            $context['theme_variant'] = !empty($settings['default_variant']) && in_array($settings['default_variant'], $settings['theme_variants']) ? $settings['default_variant'] : $settings['theme_variants'][0];
        
        $context['theme_variant'] = '_' . $context['theme_variant'];
        $context['theme_variant_url'] = $context['theme_variant'] . '/';
        if (!empty($context['theme_variant']))
        {
            loadCSSFile('index' . $context['theme_variant'] . '.css', array('order_pos' => 300), 'smf_index' . $context['theme_variant']);
            if ($context['right_to_left'])
                loadCSSFile('rtl' . $context['theme_variant'] . '.css', array('order_pos' => 4200), 'smf_rtl' . $context['theme_variant']);
        }
    }
    
    if (!function_exists('template_html_above') && in_array('html', $context['template_layers']))
        $context['template_layers'] = array('main');
    $context['tabindex'] = 1;
    
    if (!isset($settings['theme_version']))
        $modSettings['memberCount'] = $modSettings['totalMembers'];
    
    $context['javascript_vars'] = array(
        'smf_theme_url' => '"' . $settings['theme_url'] . '"',
        'smf_default_theme_url' => '"' . $settings['default_theme_url'] . '"',
        'smf_images_url' => '"' . $settings['images_url'] . '"',
        'smf_smileys_url' => '"' . $modSettings['smileys_url'] . '"',
        'smf_smiley_sets' => '"' . $modSettings['smiley_sets_known'] . '"',
        'smf_smiley_sets_default' => '"' . $modSettings['smiley_sets_default'] . '"',
        'smf_scripturl' => '"' . $scripturl . '"',
        'smf_iso_case_folding' => $context['server']['iso_case_folding'] ? 'true' : 'false',
        'smf_charset' => '"' . $context['character_set'] . '"',
        'smf_session_id' => '"' . $context['session_id'] . '"',
        'smf_session_var' => '"' . $context['session_var'] . '"',
        'smf_member_id' => $context['user']['id'],
        'ajax_notification_text' => JavaScriptEscape($txt['ajax_in_progress']),
        'help_popup_heading_text' => JavaScriptEscape($txt['help_popup']),
        'banned_text' => JavaScriptEscape(sprintf($txt['your_ban'], $context['user']['name'])),
    );
    
    if (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'cdn')
        loadJavaScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/' . JQUERY_VERSION . '/jquery.min.js', array('external' => true), 'smf_jquery');
    elseif (isset($modSettings['jquery_source']) && $modSettings['jquery_source'] == 'local')
        loadJavaScriptFile('jquery-' . JQUERY_VERSION . '.min.js', array('seed' => false), 'smf_jquery');
    elseif (isset($modSettings['jquery_source'], $modSettings['jquery_custom']) && $modSettings['jquery_source'] == 'custom')
        loadJavaScriptFile($modSettings['jquery_custom'], array('external' => true), 'smf_jquery');
    
    else
        loadJavaScriptFile('https://ajax.googleapis.com/ajax/libs/jquery/' . JQUERY_VERSION . '/jquery.min.js', array('external' => true), 'smf_jquery');
    
    loadJavaScriptFile('smf_jquery_plugins.js', array('minimize' => true), 'smf_jquery_plugins');
    if (!$user_info['is_guest'])
    {
        loadJavaScriptFile('jquery.custom-scrollbar.js', array('minimize' => true), 'smf_jquery_scrollbar');
        loadCSSFile('jquery.custom-scrollbar.css', array('force_current' => false, 'validate' => true), 'smf_scrollbar');
    }
    
    loadJavaScriptFile('script.js', array('defer' => false, 'minimize' => true), 'smf_script');
    loadJavaScriptFile('theme.js', array('minimize' => true), 'smf_theme');
    
    if ((!empty($modSettings['mail_next_send']) && $modSettings['mail_next_send'] < time() && empty($modSettings['mail_queue_use_cron'])) || empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time())
    {
        if (isBrowser('possibly_robot'))
        {
            
            require_once($sourcedir . '/ScheduledTasks.php');
            
            if (empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time())
                AutoTask();
            else
                ReduceMailQueue();
        }
        else
        {
            $type = empty($modSettings['next_task_time']) || $modSettings['next_task_time'] < time() ? 'task' : 'mailq';
            $ts = $type == 'mailq' ? $modSettings['mail_next_send'] : $modSettings['next_task_time'];
            addInlineJavaScript('
        function smfAutoTask()
        {
            $.get(smf_scripturl + "?scheduled=' . $type . ';ts=' . $ts . '");
        }
        window.setTimeout("smfAutoTask();", 1);');
        }
    }
    
    if (empty($modSettings['cron_is_real_cron']))
    {
        $ts = time();
        $ts -= $ts % 15;
        addInlineJavaScript('
    function triggerCron()
    {
        $.get(' . JavaScriptEscape($boardurl) . ' + "/cron.php?ts=' . $ts . '");
    }
    window.setTimeout(triggerCron, 1);', true);
    }
    
    if (!$user_info['is_admin'] && !empty($board))
    {
        foreach ($context['linktree'] as $k => $element)
        {
            if (!empty($element['groups']) &&
                (count(array_intersect($user_info['groups'], $element['groups'])) == 0 ||
                    (!empty($modSettings['deny_boards_access']) && count(array_intersect($user_info['groups'], $element['deny_groups'])) != 0)))
            {
                $context['linktree'][$k]['name'] = $txt['restricted_board'];
                $context['linktree'][$k]['extra_before'] = '<i>';
                $context['linktree'][$k]['extra_after'] = '</i>';
                unset($context['linktree'][$k]['url']);
            }
        }
    }
    
    if (!empty($modSettings['integrate_theme_include']))
    {
        $theme_includes = explode(',', $modSettings['integrate_theme_include']);
        foreach ($theme_includes as $include)
        {
            $include = strtr(trim($include), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir']));
            if (file_exists($include))
                require_once($include);
        }
    }
    
    call_integration_hook('integrate_load_theme');
    
    $context['theme_loaded'] = true;
}
function loadTemplate($template_name, $style_sheets = array(), $fatal = true)
{
    global $context, $settings, $txt, $scripturl, $boarddir, $db_show_debug;
    
    if (!empty($style_sheets))
    {
        if (!is_array($style_sheets))
            $style_sheets = array($style_sheets);
        foreach ($style_sheets as $sheet)
            loadCSSFile($sheet . '.css', array(), $sheet);
    }
    
    if ($template_name === false)
        return true;
    $loaded = false;
    foreach ($settings['template_dirs'] as $template_dir)
    {
        if (file_exists($template_dir . '/' . $template_name . '.template.php'))
        {
            $loaded = true;
            template_include($template_dir . '/' . $template_name . '.template.php', true);
            break;
        }
    }
    if ($loaded)
    {
        if ($db_show_debug === true)
            $context['debug']['templates'][] = $template_name . ' (' . basename($template_dir) . ')';
        
        if (function_exists('template_' . $template_name . '_init'))
            call_user_func('template_' . $template_name . '_init');
    }
    
    elseif (!file_exists($settings['default_theme_dir']) && file_exists($boarddir . '/Themes/default'))
    {
        $settings['default_theme_dir'] = $boarddir . '/Themes/default';
        $settings['template_dirs'][] = $settings['default_theme_dir'];
        if (!empty($context['user']['is_admin']) && !isset($_GET['th']))
        {
            loadLanguage('Errors');
            echo '
<div class="alert errorbox">
    <a href="', $scripturl . '?action=admin;area=theme;sa=list;' . $context['session_var'] . '=' . $context['session_id'], '" class="alert">', $txt['theme_dir_wrong'], '</a>
</div>';
        }
        loadTemplate($template_name);
    }
    
    elseif ($template_name != 'Errors' && $template_name != 'index' && $fatal)
        fatal_lang_error('theme_template_error', 'template', array((string) $template_name));
    elseif ($fatal)
        die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load Themes/default/%s.template.php!', (string) $template_name), 'template'));
    else
        return false;
}
function loadSubTemplate($sub_template_name, $fatal = false)
{
    global $context, $txt, $db_show_debug;
    if ($db_show_debug === true)
        $context['debug']['sub_templates'][] = $sub_template_name;
    
    $theme_function = 'template_' . $sub_template_name;
    if (function_exists($theme_function))
        $theme_function();
    elseif ($fatal === false)
        fatal_lang_error('theme_template_error', 'template', array((string) $sub_template_name));
    elseif ($fatal !== 'ignore')
        die(log_error(sprintf(isset($txt['theme_template_error']) ? $txt['theme_template_error'] : 'Unable to load the %s sub template!', (string) $sub_template_name), 'template'));
    
    if (allowedTo('admin_forum') && isset($_REQUEST['debug']) && !in_array($sub_template_name, array('init', 'main_below')) && ob_get_length() > 0 && !isset($_REQUEST['xml']))
    {
        echo '
<div class="warningbox">---- ', $sub_template_name, ' ends ----</div>';
    }
}
function loadCSSFile($fileName, $params = array(), $id = '')
{
    global $settings, $context, $modSettings;
    if (empty($context['css_files_order']))
        $context['css_files_order'] = array();
    $params['seed'] = (!array_key_exists('seed', $params) || (array_key_exists('seed', $params) && $params['seed'] === true)) ? (array_key_exists('browser_cache', $context) ? $context['browser_cache'] : '') : (is_string($params['seed']) ? '?' . ltrim($params['seed'], '?') : '');
    $params['force_current'] = isset($params['force_current']) ? $params['force_current'] : false;
    $themeRef = !empty($params['default_theme']) ? 'default_theme' : 'theme';
    $params['minimize'] = isset($params['minimize']) ? $params['minimize'] : true;
    $params['external'] = isset($params['external']) ? $params['external'] : false;
    $params['validate'] = isset($params['validate']) ? $params['validate'] : true;
    $params['order_pos'] = isset($params['order_pos']) ? (int) $params['order_pos'] : 3000;
    
    if (!empty($params['external']))
        $params['minimize'] = false;
    
    $id = empty($id) ? strtr(str_replace('.css', '', basename($fileName)), '?', '_') : $id;
    $fileName = str_replace(pathinfo($fileName, PATHINFO_EXTENSION), strtok(pathinfo($fileName, PATHINFO_EXTENSION), '?'), $fileName);
    
    if (empty($params['external']))
    {
        
        if (!empty($params['validate']) && ($mtime = @filemtime($settings[$themeRef . '_dir'] . '/css/' . $fileName)) === false)
        {
            
            if ($themeRef === 'theme' && !$params['force_current'] && ($mtime = @filemtime($settings['default_theme_dir'] . '/css/' . $fileName) !== false))
            {
                $fileUrl = $settings['default_theme_url'] . '/css/' . $fileName;
                $filePath = $settings['default_theme_dir'] . '/css/' . $fileName;
            }
            else
            {
                $fileUrl = false;
                $filePath = false;
            }
        }
        else
        {
            $fileUrl = $settings[$themeRef . '_url'] . '/css/' . $fileName;
            $filePath = $settings[$themeRef . '_dir'] . '/css/' . $fileName;
            $mtime = @filemtime($filePath);
        }
    }
    
    else
    {
        $fileUrl = $fileName;
        $filePath = $fileName;
    }
    $mtime = empty($mtime) ? 0 : $mtime;
    
    if (!empty($fileName))
    {
        
        while (isset($context['css_files_order'][$params['order_pos']]))
            $params['order_pos']++;
        $context['css_files_order'][$params['order_pos']] = $id;
        $context['css_files'][$id] = array('fileUrl' => $fileUrl, 'filePath' => $filePath, 'fileName' => $fileName, 'options' => $params, 'mtime' => $mtime);
    }
    if (!empty($context['right_to_left']) && !empty($params['rtl']))
        loadCSSFile($params['rtl'], array_diff_key($params, array('rtl' => 0)));
    if ($mtime > $modSettings['browser_cache'])
        updateSettings(array('browser_cache' => $mtime));
}
function addInlineCss($css)
{
    global $context;
    
    if (empty($css))
        return false;
    $context['css_header'][] = $css;
}
function loadJavaScriptFile($fileName, $params = array(), $id = '')
{
    global $settings, $context, $modSettings;
    $params['seed'] = (!array_key_exists('seed', $params) || (array_key_exists('seed', $params) && $params['seed'] === true)) ? (array_key_exists('browser_cache', $context) ? $context['browser_cache'] : '') : (is_string($params['seed']) ? '?' . ltrim($params['seed'], '?') : '');
    $params['force_current'] = isset($params['force_current']) ? $params['force_current'] : false;
    $themeRef = !empty($params['default_theme']) ? 'default_theme' : 'theme';
    $params['async'] = isset($params['async']) ? $params['async'] : false;
    $params['minimize'] = isset($params['minimize']) ? $params['minimize'] : false;
    $params['external'] = isset($params['external']) ? $params['external'] : false;
    $params['validate'] = isset($params['validate']) ? $params['validate'] : true;
    
    if (!empty($params['external']))
        $params['minimize'] = false;
    
    $id = empty($id) ? strtr(str_replace('.js', '', basename($fileName)), '?', '_') : $id;
    $fileName = str_replace(pathinfo($fileName, PATHINFO_EXTENSION), strtok(pathinfo($fileName, PATHINFO_EXTENSION), '?'), $fileName);
    
    if (empty($params['external']))
    {
        
        if (!empty($params['validate']) && ($mtime = @filemtime($settings[$themeRef . '_dir'] . '/scripts/' . $fileName)) === false)
        {
            
            if ($themeRef === 'theme' && !$params['force_current'] && ($mtime = @filemtime($settings['default_theme_dir'] . '/scripts/' . $fileName)) !== false)
            {
                $fileUrl = $settings['default_theme_url'] . '/scripts/' . $fileName;
                $filePath = $settings['default_theme_dir'] . '/scripts/' . $fileName;
            }
            else
            {
                $fileUrl = false;
                $filePath = false;
            }
        }
        else
        {
            $fileUrl = $settings[$themeRef . '_url'] . '/scripts/' . $fileName;
            $filePath = $settings[$themeRef . '_dir'] . '/scripts/' . $fileName;
            $mtime = @filemtime($filePath);
        }
    }
    
    else
    {
        $fileUrl = $fileName;
        $filePath = $fileName;
    }
    $mtime = empty($mtime) ? 0 : $mtime;
    
    if (!empty($fileName))
        $context['javascript_files'][$id] = array('fileUrl' => $fileUrl, 'filePath' => $filePath, 'fileName' => $fileName, 'options' => $params, 'mtime' => $mtime);
    if ($mtime > $modSettings['browser_cache'])
        updateSettings(array('browser_cache' => $mtime));
}
function addJavaScriptVar($key, $value, $escape = false)
{
    global $context;
    if (!empty($key) && (!empty($value) || $value === '0'))
        $context['javascript_vars'][$key] = !empty($escape) ? JavaScriptEscape($value) : $value;
}
function addInlineJavaScript($javascript, $defer = false)
{
    global $context;
    if (empty($javascript))
        return false;
    $context['javascript_inline'][($defer === true ? 'defer' : 'standard')][] = $javascript;
}
function loadLanguage($template_name, $lang = '', $fatal = true, $force_reload = false)
{
    global $user_info, $language, $settings, $context, $modSettings;
    global $db_show_debug, $sourcedir, $txt, $birthdayEmails, $txtBirthdayEmails;
    static $already_loaded = array();
    
    if ($lang == '')
        $lang = isset($user_info['language']) ? $user_info['language'] : $language;
    
    if (empty($modSettings['disable_language_fallback']) && $lang != 'english')
        loadLanguage($template_name, 'english', false);
    if (!$force_reload && isset($already_loaded[$template_name]) && $already_loaded[$template_name] == $lang)
        return $lang;
    
    if (empty($settings['default_theme_dir']))
    {
        require_once($sourcedir . '/ScheduledTasks.php');
        loadEssentialThemeData();
    }
    
    $theme_name = basename($settings['theme_url']);
    if (empty($theme_name))
        $theme_name = 'unknown';
    
    foreach (explode('+', $template_name) as $template)
    {
        
        $attempts = array(
            array($settings['theme_dir'], $template, $lang, $settings['theme_url']),
            array($settings['theme_dir'], $template, $language, $settings['theme_url']),
        );
        
        if (isset($settings['base_theme_dir']))
        {
            $attempts[] = array($settings['base_theme_dir'], $template, $lang, $settings['base_theme_url']);
            $attempts[] = array($settings['base_theme_dir'], $template, $language, $settings['base_theme_url']);
        }
        
        $attempts[] = array($settings['default_theme_dir'], $template, $lang, $settings['default_theme_url']);
        $attempts[] = array($settings['default_theme_dir'], $template, $language, $settings['default_theme_url']);
        
        if (!in_array('english', array($lang, $language)))
        {
            $attempts[] = array($settings['theme_dir'], $template, 'english', $settings['theme_url']);
            $attempts[] = array($settings['default_theme_dir'], $template, 'english', $settings['default_theme_url']);
        }
        
        $found = false;
        foreach ($attempts as $k => $file)
        {
            if (file_exists($file[0] . '/languages/' . $file[1] . '.' . $file[2] . '.php'))
            {
                
                template_include($file[0] . '/languages/' . $file[1] . '.' . $file[2] . '.php');
                
                $found = true;
                
                if (!empty($txt['lang_locale']) && !empty($modSettings['global_character_set']))
                    setlocale(LC_CTYPE, $txt['lang_locale'] . '.' . $modSettings['global_character_set']);
                break;
            }
        }
        
        if (!$found && $fatal)
        {
            log_error(sprintf($txt['theme_language_error'], $template_name . '.' . $lang, 'template'));
            break;
        }
        
        if (!empty($txt['emails']))
        {
            foreach ($txt['emails'] as $key => $value)
            {
                $txt[$key . '_subject'] = $value['subject'];
                $txt[$key . '_body'] = $value['body'];
            }
            $txt['emails'] = array();
        }
        
        
        
        
        if (!empty($birthdayEmails))
        {
            foreach ($birthdayEmails as $key => $value)
            {
                $txtBirthdayEmails[$key . '_subject'] = $value['subject'];
                $txtBirthdayEmails[$key . '_body'] = $value['body'];
                $txtBirthdayEmails[$key . '_author'] = $value['author'];
            }
            $birthdayEmails = array();
        }
    }
    
    if ($db_show_debug === true)
        $context['debug']['language_files'][] = $template_name . '.' . $lang . ' (' . $theme_name . ')';
    
    $already_loaded[$template_name] = $lang;
    
    return $lang;
}
function getBoardParents($id_parent)
{
    global $scripturl, $smcFunc;
    
    if (($boards = cache_get_data('board_parents-' . $id_parent, 480)) === null)
    {
        $boards = array();
        $original_parent = $id_parent;
        
        while ($id_parent != 0)
        {
            $result = $smcFunc['db_query']('', '
                SELECT
                    b.id_parent, b.name, {int:board_parent} AS id_board, b.member_groups, b.deny_member_groups,
                    b.child_level, COALESCE(mem.id_member, 0) AS id_moderator, mem.real_name,
                    COALESCE(mg.id_group, 0) AS id_moderator_group, mg.group_name
                FROM {db_prefix}boards AS b
                    LEFT JOIN {db_prefix}moderators AS mods ON (mods.id_board = b.id_board)
                    LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
                    LEFT JOIN {db_prefix}moderator_groups AS modgs ON (modgs.id_board = b.id_board)
                    LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = modgs.id_group)
                WHERE b.id_board = {int:board_parent}',
                array(
                    'board_parent' => $id_parent,
                )
            );
            
            if ($smcFunc['db_num_rows']($result) == 0)
                fatal_lang_error('parent_not_found', 'critical');
            while ($row = $smcFunc['db_fetch_assoc']($result))
            {
                if (!isset($boards[$row['id_board']]))
                {
                    $id_parent = $row['id_parent'];
                    $boards[$row['id_board']] = array(
                        'url' => $scripturl . '?board=' . $row['id_board'] . '.0',
                        'name' => $row['name'],
                        'level' => $row['child_level'],
                        'groups' => explode(',', $row['member_groups']),
                        'deny_groups' => explode(',', $row['deny_member_groups']),
                        'moderators' => array(),
                        'moderator_groups' => array()
                    );
                }
                
                if (!empty($row['id_moderator']))
                    foreach ($boards as $id => $dummy)
                    {
                        $boards[$id]['moderators'][$row['id_moderator']] = array(
                            'id' => $row['id_moderator'],
                            'name' => $row['real_name'],
                            'href' => $scripturl . '?action=profile;u=' . $row['id_moderator'],
                            'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row['id_moderator'] . '">' . $row['real_name'] . '</a>'
                        );
                    }
                
                if (!empty($row['id_moderator_group']))
                    foreach ($boards as $id => $dummy)
                    {
                        $boards[$id]['moderator_groups'][$row['id_moderator_group']] = array(
                            'id' => $row['id_moderator_group'],
                            'name' => $row['group_name'],
                            'href' => $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'],
                            'link' => '<a href="' . $scripturl . '?action=groups;sa=members;group=' . $row['id_moderator_group'] . '">' . $row['group_name'] . '</a>'
                        );
                    }
            }
            $smcFunc['db_free_result']($result);
        }
        cache_put_data('board_parents-' . $original_parent, $boards, 480);
    }
    return $boards;
}
function getLanguages($use_cache = true)
{
    global $context, $smcFunc, $settings, $modSettings, $cache_enable;
    
    if (!$use_cache || ($context['languages'] = cache_get_data('known_languages', !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600)) == null)
    {
        
        if (empty($smcFunc['ucwords']))
            reloadSettings();
        
        if (empty($settings['default_theme_dir']))
            loadTheme(0, false);
        
        $language_directories = array(
            $settings['default_theme_dir'] . '/languages',
        );
        if (!empty($settings['actual_theme_dir']) && $settings['actual_theme_dir'] != $settings['default_theme_dir'])
            $language_directories[] = $settings['actual_theme_dir'] . '/languages';
        
        if (!empty($settings['base_theme_dir']))
            $language_directories[] = $settings['base_theme_dir'] . '/languages';
        
        $language_directories = array_unique($language_directories);
        foreach ($language_directories as $language_dir)
        {
            
            if (!file_exists($language_dir))
                continue;
            $dir = dir($language_dir);
            while ($entry = $dir->read())
            {
                
                if (!preg_match('~^index\.((?:.(?!-utf8))+)\.php$~', $entry, $matches))
                    continue;
                $langName = $smcFunc['ucwords'](strtr($matches[1], array('_' => ' ')));
                if (($spos = strpos($langName, ' ')) !== false)
                    $langName = substr($langName, 0, ++$spos) . '(' . substr($langName, $spos) . ')';
                
                $fp = @fopen($language_dir . '/' . $entry, 'r');
                
                if ($fp)
                {
                    while (($line = fgets($fp)) !== false)
                    {
                        if (strpos($line, '$txt[\'native_name\']') === false)
                            continue;
                        preg_match('~\$txt\[\'native_name\'\]\s*=\s*\'([^\']+)\';~', $line, $matchNative);
                        
                        if (!empty($matchNative) && !empty($matchNative[1]))
                        {
                            
                            if ($langName !== 'English' && $matchNative[1] === 'English')
                                break;
                            $langName = un_htmlspecialchars($matchNative[1]);
                            break;
                        }
                    }
                    fclose($fp);
                }
                
                $context['languages'][$matches[1]] = array(
                    'name' => $langName,
                    'selected' => false,
                    'filename' => $matches[1],
                    'location' => $language_dir . '/index.' . $matches[1] . '.php',
                );
            }
            $dir->close();
        }
        
        
        if (substr_count(implode(' ', array_keys($context['languages'])), 'english') > 1 && $context['languages']['english']['name'] === 'English')
            $context['languages']['english']['name'] = 'English (US)';
        
        if (!empty($cache_enable))
            cache_put_data('known_languages', $context['languages'], !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600);
    }
    return $context['languages'];
}
function censorText(&$text, $force = false)
{
    global $modSettings, $options, $txt;
    static $censor_vulgar = null, $censor_proper;
    if ((!empty($options['show_no_censored']) && !empty($modSettings['allow_no_censored']) && !$force) || empty($modSettings['censor_vulgar']) || trim($text) === '')
        return $text;
    
    if ($censor_vulgar == null)
    {
        $censor_vulgar = explode("\n", $modSettings['censor_vulgar']);
        $censor_proper = explode("\n", $modSettings['censor_proper']);
        
        if (!empty($modSettings['censorWholeWord']))
        {
            $charset = empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set'];
            for ($i = 0, $n = count($censor_vulgar); $i < $n; $i++)
            {
                $censor_vulgar[$i] = str_replace(array('\\\\\\*', '\\*', '&', '\''), array('[*]', '[^\s]*?', '&', '''), preg_quote($censor_vulgar[$i], '/'));
                
                $boundary_before = preg_match('/^\w/', $censor_vulgar[$i]) ? '\b' : ($charset === 'UTF-8' ? '(?<![\p{L}\p{M}\p{N}_])' : '(?<!\w)');
                $boundary_after = preg_match('/\w$/', $censor_vulgar[$i]) ? '\b' : ($charset === 'UTF-8' ? '(?![\p{L}\p{M}\p{N}_])' : '(?!\w)');
                $censor_vulgar[$i] = '/' . $boundary_before . $censor_vulgar[$i] . $boundary_after . '/' . (empty($modSettings['censorIgnoreCase']) ? '' : 'i') . ($charset === 'UTF-8' ? 'u' : '');
            }
        }
    }
    
    if (empty($modSettings['censorWholeWord']))
    {
        $func = !empty($modSettings['censorIgnoreCase']) ? 'str_ireplace' : 'str_replace';
        $text = $func($censor_vulgar, $censor_proper, $text);
    }
    else
        $text = preg_replace($censor_vulgar, $censor_proper, $text);
    return $text;
}
function template_include($filename, $once = false)
{
    global $context, $txt, $scripturl, $modSettings;
    global $boardurl, $boarddir;
    global $maintenance, $mtitle, $mmessage;
    static $templates = array();
    
    @ini_set('track_errors', '1');
    
    if ($once && in_array($filename, $templates))
        return;
    
    else
        $templates[] = $filename;
    $file_found = file_exists($filename);
    if ($once && $file_found)
        require_once($filename);
    elseif ($file_found)
        require($filename);
    if ($file_found !== true)
    {
        ob_end_clean();
        if (!empty($modSettings['enableCompressedOutput']))
            @ob_start('ob_gzhandler');
        else
            ob_start();
        if (isset($_GET['debug']))
            header('content-type: application/xhtml+xml; charset=' . (empty($context['character_set']) ? 'ISO-8859-1' : $context['character_set']));
        
        header('expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('last-modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('cache-control: no-cache');
        if (!isset($txt['template_parse_error']))
        {
            $txt['template_parse_error'] = 'Template Parse Error!';
            $txt['template_parse_error_message'] = 'It seems something has gone sour on the forum with the template system.  This problem should only be temporary, so please come back later and try again.  If you continue to see this message, please contact the administrator.<br><br>You can also try <a href="javascript:location.reload();">refreshing this page</a>.';
            $txt['template_parse_error_details'] = 'There was a problem loading the <pre><strong>%1$s</strong></pre> template or language file.  Please check the syntax and try again - remember, single quotes (<pre>\'</pre>) often have to be escaped with a slash (<pre>\\</pre>).  To see more specific error information from PHP, try <a href="' . $boardurl . '%1$s" class="extern">accessing the file directly</a>.<br><br>You may want to try to <a href="javascript:location.reload();">refresh this page</a> or <a href="' . $scripturl . '?theme=1">use the default theme</a>.';
            $txt['template_parse_errmsg'] = 'Unfortunately more information is not available at this time as to exactly what is wrong.';
        }
        
        echo '<!DOCTYPE html>
<html', !empty($context['right_to_left']) ? ' dir="rtl"' : '', '>
    <head>';
        if (isset($context['character_set']))
            echo '
        <meta charset="', $context['character_set'], '">';
        if (!empty($maintenance) && !allowedTo('admin_forum'))
            echo '
        <title>', $mtitle, '</title>
    </head>
    <body>
        <h3>', $mtitle, '</h3>
        ', $mmessage, '
    </body>
</html>';
        elseif (!allowedTo('admin_forum'))
            echo '
        <title>', $txt['template_parse_error'], '</title>
    </head>
    <body>
        <h3>', $txt['template_parse_error'], '</h3>
        ', $txt['template_parse_error_message'], '
    </body>
</html>';
        else
        {
            $error = fetch_web_data($boardurl . strtr($filename, array($boarddir => '', strtr($boarddir, '\\', '/') => '')));
            $error_array = error_get_last();
            if (empty($error) && ini_get('track_errors') && !empty($error_array))
                $error = $error_array['message'];
            if (empty($error))
                $error = $txt['template_parse_errmsg'];
            $error = strtr($error, array('<b>' => '<strong>', '</b>' => '</strong>'));
            echo '
        <title>', $txt['template_parse_error'], '</title>
    </head>
    <body>
        <h3>', $txt['template_parse_error'], '</h3>
        ', sprintf($txt['template_parse_error_details'], strtr($filename, array($boarddir => '', strtr($boarddir, '\\', '/') => '')));
            if (!empty($error))
                echo '
        <hr>
        <div style="margin: 0 20px;"><pre>', strtr(strtr($error, array('<strong>' . $boarddir => '<strong>...', '<strong>' . strtr($boarddir, '\\', '/') => '<strong>...')), '\\', '/'), '</pre></div>';
            
            if (preg_match('~ <strong>(\d+)</strong><br( /)?' . '>$~i', $error, $match) != 0)
            {
                $data = file($filename);
                $data2 = highlight_php_code(implode('', $data));
                $data2 = preg_split('~\<br( /)?\>~', $data2);
                
                if (!isBrowser('gecko'))
                    $data2 = str_replace("\t", '<span style="white-space: pre;">' . "\t" . '</span>', $data2);
                else
                    $data2 = str_replace('<pre style="display: inline;">' . "\t" . '</pre>', "\t", $data2);
                
                $j = -1;
                foreach ($data as $line)
                {
                    $j++;
                    if (substr_count($line, '<br>') == 0)
                        continue;
                    $n = substr_count($line, '<br>');
                    for ($i = 0; $i < $n; $i++)
                    {
                        $data2[$j] .= '<br />' . $data2[$j + $i + 1];
                        unset($data2[$j + $i + 1]);
                    }
                    $j += $n;
                }
                $data2 = array_values($data2);
                array_unshift($data2, '');
                echo '
        <div style="margin: 2ex 20px; width: 96%; overflow: auto;"><pre style="margin: 0;">';
                
                $line = max($match[1] - 9, 1);
                $last_line = '';
                for ($line2 = $line - 1; $line2 > 1; $line2--)
                    if (strpos($data2[$line2], '<') !== false)
                    {
                        if (preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line2], $color_match) != 0)
                            $last_line = $color_match[1];
                        break;
                    }
                
                for ($n = min($match[1] + 4, count($data2) + 1); $line <= $n; $line++)
                {
                    if ($line == $match[1])
                        echo '</pre><div style="background-color: #ffb0b5;"><pre style="margin: 0;">';
                    echo '<span style="color: black;">', sprintf('%' . strlen($n) . 's', $line), ':</span> ';
                    if (isset($data2[$line]) && $data2[$line] != '')
                        echo substr($data2[$line], 0, 2) == '</' ? preg_replace('~^</[^>]+>~', '', $data2[$line]) : $last_line . $data2[$line];
                    if (isset($data2[$line]) && preg_match('~(<[^/>]+>)[^<]*$~', $data2[$line], $color_match) != 0)
                    {
                        $last_line = $color_match[1];
                        echo '</', substr($last_line, 1, 4), '>';
                    }
                    elseif ($last_line != '' && strpos($data2[$line], '<') !== false)
                        $last_line = '';
                    elseif ($last_line != '' && $data2[$line] != '')
                        echo '</', substr($last_line, 1, 4), '>';
                    if ($line == $match[1])
                        echo '</pre></div><pre style="margin: 0;">';
                    else
                        echo "\n";
                }
                echo '</pre></div>';
            }
            echo '
    </body>
</html>';
        }
        die;
    }
}
function loadDatabase()
{
    global $db_persist, $db_connection, $db_server, $db_user, $db_passwd;
    global $db_type, $db_name, $ssi_db_user, $ssi_db_passwd, $sourcedir, $db_prefix, $db_port, $db_mb4;
    
    if (empty($db_type) || !file_exists($sourcedir . '/Subs-Db-' . $db_type . '.php'))
        $db_type = 'mysql';
    
    require_once($sourcedir . '/Subs-Db-' . $db_type . '.php');
    $db_options = array();
    
    if (!empty($db_port))
        $db_options['port'] = $db_port;
    if (!empty($db_mb4))
        $db_options['db_mb4'] = $db_mb4;
    
    if (SMF == 'SSI' && !empty($ssi_db_user) && !empty($ssi_db_passwd))
    {
        $options = array_merge($db_options, array('persist' => $db_persist, 'non_fatal' => true, 'dont_select_db' => true));
        $db_connection = smf_db_initiate($db_server, $db_name, $ssi_db_user, $ssi_db_passwd, $db_prefix, $options);
    }
    
    if (empty($db_connection))
    {
        $options = array_merge($db_options, array('persist' => $db_persist, 'dont_select_db' => SMF == 'SSI'));
        $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $options);
    }
    
    if (!$db_connection)
        display_db_error();
    
    if (SMF == 'SSI')
        db_fix_prefix($db_prefix, $db_name);
}
function loadCacheAccelerator($overrideCache = null, $fallbackSMF = true)
{
    global $sourcedir, $cacheAPI, $cache_accelerator, $cache_enable;
    
    if (empty($cache_enable) && empty($overrideCache))
        return false;
    
    if (empty($overrideCache) && is_object($cacheAPI))
        return $cacheAPI;
    elseif (is_null($cacheAPI))
        $cacheAPI = false;
    
    require_once($sourcedir . '/Class-CacheAPI.php');
    
    $tryAccelerator = !empty($overrideCache) ? $overrideCache : (!empty($cache_accelerator) ? $cache_accelerator : 'smf');
    $tryAccelerator = strtolower($tryAccelerator);
    
    if (file_exists($sourcedir . '/CacheAPI-' . $tryAccelerator . '.php'))
    {
        require_once($sourcedir . '/CacheAPI-' . $tryAccelerator . '.php');
        $cache_class_name = $tryAccelerator . '_cache';
        $testAPI = new $cache_class_name();
        
        if (!$testAPI->isSupported())
        {
            
            if (!empty($fallbackSMF) && is_null($overrideCache) && $tryAccelerator != 'smf')
                return loadCacheAccelerator(null, false);
            return false;
        }
        
        $testAPI->connect();
        
        if (is_null($overrideCache))
        {
            $cacheAPI = $testAPI;
            return $cacheAPI;
        }
        else
            return $testAPI;
    }
}
function cache_quick_get($key, $file, $function, $params, $level = 1)
{
    global $modSettings, $sourcedir, $cache_enable;
    
    if (function_exists('call_integration_hook'))
        call_integration_hook('pre_cache_quick_get', array(&$key, &$file, &$function, &$params, &$level));
    
    if (empty($cache_enable) || $cache_enable < $level || !is_array($cache_block = cache_get_data($key, 3600)) || (!empty($cache_block['refresh_eval']) && eval($cache_block['refresh_eval'])) || (!empty($cache_block['expires']) && $cache_block['expires'] < time()))
    {
        require_once($sourcedir . '/' . $file);
        $cache_block = call_user_func_array($function, $params);
        if (!empty($cache_enable) && $cache_enable >= $level)
            cache_put_data($key, $cache_block, $cache_block['expires'] - time());
    }
    
    if (!empty($cache_block['post_retri_eval']))
        eval($cache_block['post_retri_eval']);
    if (function_exists('call_integration_hook'))
        call_integration_hook('post_cache_quick_get', array(&$cache_block));
    return $cache_block['data'];
}
function cache_put_data($key, $value, $ttl = 120)
{
    global $smcFunc, $cache_enable, $cacheAPI;
    global $cache_hits, $cache_count, $db_show_debug;
    if (empty($cache_enable) || empty($cacheAPI))
        return;
    $cache_count = isset($cache_count) ? $cache_count + 1 : 1;
    if (isset($db_show_debug) && $db_show_debug === true)
    {
        $cache_hits[$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(isset($smcFunc['json_encode']) ? $smcFunc['json_encode']($value) : json_encode($value)));
        $st = microtime(true);
    }
    
    $value = $value === null ? null : (isset($smcFunc['json_encode']) ? $smcFunc['json_encode']($value) : json_encode($value));
    $cacheAPI->putData($key, $value, $ttl);
    if (function_exists('call_integration_hook'))
        call_integration_hook('cache_put_data', array(&$key, &$value, &$ttl));
    if (isset($db_show_debug) && $db_show_debug === true)
        $cache_hits[$cache_count]['t'] = microtime(true) - $st;
}
function cache_get_data($key, $ttl = 120)
{
    global $smcFunc, $cache_enable, $cacheAPI;
    global $cache_hits, $cache_count, $cache_misses, $cache_count_misses, $db_show_debug;
    if (empty($cache_enable) || empty($cacheAPI))
        return;
    $cache_count = isset($cache_count) ? $cache_count + 1 : 1;
    if (isset($db_show_debug) && $db_show_debug === true)
    {
        $cache_hits[$cache_count] = array('k' => $key, 'd' => 'get');
        $st = microtime(true);
        $original_key = $key;
    }
    
    $value = $cacheAPI->getData($key, $ttl);
    if (isset($db_show_debug) && $db_show_debug === true)
    {
        $cache_hits[$cache_count]['t'] = microtime(true) - $st;
        $cache_hits[$cache_count]['s'] = isset($value) ? strlen($value) : 0;
        if (empty($value))
        {
            if (!is_array($cache_misses))
                $cache_misses = array();
            $cache_count_misses = isset($cache_count_misses) ? $cache_count_misses + 1 : 1;
            $cache_misses[$cache_count_misses] = array('k' => $original_key, 'd' => 'get');
        }
    }
    if (function_exists('call_integration_hook') && isset($value))
        call_integration_hook('cache_get_data', array(&$key, &$ttl, &$value));
    return empty($value) ? null : (isset($smcFunc['json_decode']) ? $smcFunc['json_decode']($value, true) : smf_json_decode($value, true));
}
function clean_cache($type = '')
{
    global $cacheAPI;
    
    if (empty($cacheAPI))
        return;
    
    $cacheAPI->cleanCache($type);
    call_integration_hook('integrate_clean_cache');
    clearstatcache();
}
function set_avatar_data($data = array())
{
    global $modSettings, $smcFunc, $user_info;
    
    if (empty($data))
        return array();
    
    $image = '';
    
    if (!empty($modSettings['gravatarOverride']))
    {
        if (!empty($modSettings['gravatarAllowExtraEmail']) && !empty($data['avatar']) && stristr($data['avatar'], 'gravatar://'))
            $image = get_gravatar_url($smcFunc['substr']($data['avatar'], 11));
        elseif (!empty($data['email']))
            $image = get_gravatar_url($data['email']);
    }
    
    else
    {
        
        if (!empty($data['avatar']))
        {
            
            if (stristr($data['avatar'], 'gravatar://'))
            {
                if ($data['avatar'] == 'gravatar://')
                    $image = get_gravatar_url($data['email']);
                elseif (!empty($modSettings['gravatarAllowExtraEmail']))
                    $image = get_gravatar_url($smcFunc['substr']($data['avatar'], 11));
            }
            
            else
                $image = parse_url($data['avatar'], PHP_URL_SCHEME) !== null ? get_proxied_url($data['avatar']) : $modSettings['avatar_url'] . '/' . $data['avatar'];
        }
        
        elseif (!empty($data['filename']))
            $image = $modSettings['custom_avatar_url'] . '/' . $data['filename'];
        
        else
            $image = $modSettings['avatar_url'] . '/default.png';
    }
    call_integration_hook('integrate_set_avatar_data', array(&$image, &$data));
    
    if (!empty($image))
        return array(
            'name' => !empty($data['avatar']) ? $data['avatar'] : '',
            'image' => '<img class="avatar" src="' . $image . '" alt="">',
            'href' => $image,
            'url' => $image,
        );
    
    else
        return array(
            'name' => '',
            'image' => '',
            'href' => '',
            'url' => '',
        );
}
?>