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

/**
 * This file is here to make it easier for installed mods to have
 * settings and options.
 *
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC1
 */

if (!defined('SMF'))
    die('No direct access...');

/**
 * This function makes sure the requested subaction does exists, if it doesn't, it sets a default action or.
 *
 * @param array $subActions An array containing all possible subactions.
 * @param string $defaultAction The default action to be called if no valid subaction was found.
 */
function loadGeneralSettingParameters($subActions = array(), $defaultAction = null)
{
    global $context, $sourcedir;

    // You need to be an admin to edit settings!
    isAllowedTo('admin_forum');

    // Will need the utility functions from here.
    require_once($sourcedir . '/ManageServer.php');

    $context['sub_template'] = 'show_settings';

    // If no fallback was specified, use the first subaction.
    $defaultAction = $defaultAction ?: key($subActions);

    // I want...
    $_REQUEST['sa'] = isset($_REQUEST['sa'], $subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : $defaultAction;
    $context['sub_action'] = $_REQUEST['sa'];
}

/**
 * This function passes control through to the relevant tab.
 */
function ModifyFeatureSettings()
{
    global $context, $txt, $settings;

    loadLanguage('Help');
    loadLanguage('ManageSettings');

    $context['page_title'] = $txt['modSettings_title'];

    $subActions = array(
        'basic' => 'ModifyBasicSettings',
        'bbc' => 'ModifyBBCSettings',
        'layout' => 'ModifyLayoutSettings',
        'sig' => 'ModifySignatureSettings',
        'profile' => 'ShowCustomProfiles',
        'profileedit' => 'EditCustomProfiles',
        'likes' => 'ModifyLikesSettings',
        'mentions' => 'ModifyMentionsSettings',
        'alerts' => 'ModifyAlertsSettings',
    );

    // Load up all the tabs...
    $context[$context['admin_menu_name']]['tab_data'] = array(
        'title' => $txt['modSettings_title'],
        'help' => 'featuresettings',
        'description' => sprintf($txt['modSettings_desc'], $settings['theme_id'], $context['session_id'], $context['session_var']),
        'tabs' => array(
            'basic' => array(
            ),
            'bbc' => array(
                'description' => $txt['manageposts_bbc_settings_description'],
            ),
            'layout' => array(
            ),
            'sig' => array(
                'description' => $txt['signature_settings_desc'],
            ),
            'profile' => array(
                'description' => $txt['custom_profile_desc'],
            ),
            'likes' => array(
            ),
            'mentions' => array(
            ),
            'alerts' => array(
                'description' => $txt['notifications_desc'],
            ),
        ),
    );

    call_integration_hook('integrate_modify_features', array(&$subActions));

    loadGeneralSettingParameters($subActions, 'basic');

    // Call the right function for this sub-action.
    call_helper($subActions[$_REQUEST['sa']]);
}

/**
 * This my friend, is for all the mod authors out there.
 */
function ModifyModSettings()
{
    global $context, $txt;

    loadLanguage('Help');
    loadLanguage('ManageSettings');

    $context['page_title'] = $txt['admin_modifications'];

    $subActions = array(
        'general' => 'ModifyGeneralModSettings',
        // Mod authors, once again, if you have a whole section to add do it AFTER this line, and keep a comma at the end.
    );

    // Load up all the tabs...
    $context[$context['admin_menu_name']]['tab_data'] = array(
        'title' => $txt['admin_modifications'],
        'help' => 'modsettings',
        'description' => $txt['modification_settings_desc'],
        'tabs' => array(
            'general' => array(
            ),
        ),
    );

    // Make it easier for mods to add new areas.
    call_integration_hook('integrate_modify_modifications', array(&$subActions));

    loadGeneralSettingParameters($subActions, 'general');

    // Call the right function for this sub-action.
    call_helper($subActions[$_REQUEST['sa']]);
}

/**
 * Config array for changing the basic forum settings
 * Accessed  from ?action=admin;area=featuresettings;sa=basic;
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyBasicSettings($return_config = false)
{
    global $txt, $scripturl, $context, $modSettings;

    // We need to know if personal text is enabled, and if it's in the registration fields option.
    // If admins have set it up as an on-registration thing, they can't set a default value (because it'll never be used)
    $disabled_fields = isset($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
    $reg_fields = isset($modSettings['registration_fields']) ? explode(',', $modSettings['registration_fields']) : array();
    $can_personal_text = !in_array('personal_text', $disabled_fields) && !in_array('personal_text', $reg_fields);

    $config_vars = array(
        // Big Options... polls, sticky, bbc....
        array('select', 'pollMode', array($txt['disable_polls'], $txt['enable_polls'], $txt['polls_as_topics'])),
        '',

        // Basic stuff, titles, flash, permissions...
        array('check', 'allow_guestAccess'),
        array('check', 'enable_buddylist'),
        array('check', 'allow_hideOnline'),
        array('check', 'titlesEnable'),
        array('text', 'default_personal_text', 'subtext' => $txt['default_personal_text_note'], 'disabled' => !$can_personal_text),
        array('check', 'topic_move_any'),
        array('int', 'defaultMaxListItems', 'step' => 1, 'min' => 1, 'max' => 999),
        '',

        // Jquery source
        array(
            'select',
            'jquery_source',
            array(
                'auto' => $txt['jquery_auto'],
                'local' => $txt['jquery_local'],
                'cdn' => $txt['jquery_cdn'],
                'custom' => $txt['jquery_custom']
            ),
            'onchange' => 'if (this.value == \'custom\'){document.getElementById(\'jquery_custom\').disabled = false; } else {document.getElementById(\'jquery_custom\').disabled = true;}'
        ),
        array(
            'text',
            'jquery_custom',
            'disabled' => isset($modSettings['jquery_source']) && $modSettings['jquery_source'] != 'custom', 'size' => 75
        ),
        '',

        // css and js minification.
        array('check', 'minimize_files'),
        '',

        // SEO stuff
        array('check', 'queryless_urls', 'subtext' => '<strong>' . $txt['queryless_urls_note'] . '</strong>'),
        array('text', 'meta_keywords', 'subtext' => $txt['meta_keywords_note'], 'size' => 50),
        '',

        // Number formatting, timezones.
        array('text', 'time_format'),
        array(
            'float',
            'time_offset',
            'subtext' => $txt['setting_time_offset_note'],
            6,
            'postinput' => $txt['hours'],
            'step' => 0.25,
            'min' => -23.5,
            'max' => 23.5
        ),
        'default_timezone' => array('select', 'default_timezone', array()),
        array('text', 'timezone_priority_countries', 'subtext' => $txt['setting_timezone_priority_countries_note']),
        '',

        // Who's online?
        array('check', 'who_enabled'),
        array('int', 'lastActive', 6, 'postinput' => $txt['minutes']),
        '',

        // Statistics.
        array('check', 'trackStats'),
        array('check', 'hitStats'),
        '',

        // Option-ish things... miscellaneous sorta.
        array('check', 'allow_disableAnnounce'),
        array('check', 'disallow_sendBody'),
        '',

        // Alerts stuff
        array('check', 'enable_ajax_alerts'),
    );

    // Get all the time zones.
    if (function_exists('timezone_identifiers_list') && function_exists('date_default_timezone_set'))
    {
        $all_zones = timezone_identifiers_list();
        // Make sure we set the value to the same as the printed value.
        foreach ($all_zones as $zone)
            $config_vars['default_timezone'][2][$zone] = $zone;
    }
    else
        unset($config_vars['default_timezone']);

    call_integration_hook('integrate_modify_basic_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        // Prevent absurd boundaries here - make it a day tops.
        if (isset($_POST['lastActive']))
            $_POST['lastActive'] = min((int) $_POST['lastActive'], 1440);

        call_integration_hook('integrate_save_basic_settings');

        saveDBSettings($config_vars);
        $_SESSION['adm-save'] = true;

        // Do a bit of housekeeping
        if (empty($_POST['minimize_files']) || $_POST['minimize_files'] != $modSettings['minimize_files'])
            deleteAllMinified();

        writeLog();
        redirectexit('action=admin;area=featuresettings;sa=basic');
    }

    $context['post_url'] = $scripturl . '?action=admin;area=featuresettings;save;sa=basic';
    $context['settings_title'] = $txt['mods_cat_features'];

    prepareDBSettingContext($config_vars);
}

/**
 * Set a few Bulletin Board Code settings. It loads a list of Bulletin Board Code tags to allow disabling tags.
 * Requires the admin_forum permission.
 * Accessed from ?action=admin;area=featuresettings;sa=bbc.
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 * uses Admin template, edit_bbc_settings sub-template.
 */
function ModifyBBCSettings($return_config = false)
{
    global $context, $txt, $modSettings, $scripturl, $sourcedir;

    $config_vars = array(
        // Main tweaks
        array('check', 'enableBBC'),
        array('check', 'enableBBC', 0, 'onchange' => 'toggleBBCDisabled(\'disabledBBC\', !this.checked); toggleBBCDisabled(\'legacyBBC\', !this.checked);'),
        array('check', 'enablePostHTML'),
        array('check', 'autoLinkUrls'),
        '',

        array('bbc', 'disabledBBC'),

        // This one is actually pretend...
        array('bbc', 'legacyBBC', 'help' => 'legacy_bbc'),
    );

    // Permissions for restricted BBC
    if (!empty($context['restricted_bbc']))
        $config_vars[] = '';

    foreach ($context['restricted_bbc'] as $bbc)
        $config_vars[] = array('permissions', 'bbc_' . $bbc, 'text_label' => sprintf($txt['groups_can_use'], '[' . $bbc . ']'));

    $context['settings_post_javascript'] = '
        toggleBBCDisabled(\'disabledBBC\', ' . (empty($modSettings['enableBBC']) ? 'true' : 'false') . ');
        toggleBBCDisabled(\'legacyBBC\', ' . (empty($modSettings['enableBBC']) ? 'true' : 'false') . ');';

    call_integration_hook('integrate_modify_bbc_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Setup the template.
    require_once($sourcedir . '/ManageServer.php');
    $context['sub_template'] = 'show_settings';
    $context['page_title'] = $txt['manageposts_bbc_settings_title'];

    // Make sure we check the right tags!
    $modSettings['bbc_disabled_disabledBBC'] = empty($modSettings['disabledBBC']) ? array() : explode(',', $modSettings['disabledBBC']);

    // Legacy BBC are listed separately, but we use the same info in both cases
    $modSettings['bbc_disabled_legacyBBC'] = $modSettings['bbc_disabled_disabledBBC'];

    $extra = '';
    if (isset($_REQUEST['cowsay']))
    {
        $config_vars[] = array('permissions', 'bbc_cowsay', 'text_label' => sprintf($txt['groups_can_use'], 'cowsay'));
        $extra = ';cowsay';
    }

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        // Clean up the tags.
        $bbcTags = array();
        foreach (parse_bbc(false) as $tag)
            $bbcTags[] = $tag['tag'];

        if (!isset($_POST['disabledBBC_enabledTags']))
            $_POST['disabledBBC_enabledTags'] = array();
        elseif (!is_array($_POST['disabledBBC_enabledTags']))
            $_POST['disabledBBC_enabledTags'] = array($_POST['disabledBBC_enabledTags']);

        if (!isset($_POST['legacyBBC_enabledTags']))
            $_POST['legacyBBC_enabledTags'] = array();
        elseif (!is_array($_POST['legacyBBC_enabledTags']))
            $_POST['legacyBBC_enabledTags'] = array($_POST['legacyBBC_enabledTags']);

        $_POST['disabledBBC_enabledTags'] = array_unique(array_merge($_POST['disabledBBC_enabledTags'], $_POST['legacyBBC_enabledTags']));

        // Work out what is actually disabled!
        $_POST['disabledBBC'] = implode(',', array_diff($bbcTags, $_POST['disabledBBC_enabledTags']));

        // $modSettings['legacyBBC'] isn't really a thing...
        unset($_POST['legacyBBC_enabledTags']);
        $config_vars = array_filter($config_vars, function($config_var)
        {
            return !isset($config_var[1]) || $config_var[1] != 'legacyBBC';
        });

        call_integration_hook('integrate_save_bbc_settings', array($bbcTags));

        saveDBSettings($config_vars);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=featuresettings;sa=bbc' . $extra);
    }

    $context['post_url'] = $scripturl . '?action=admin;area=featuresettings;save;sa=bbc' . $extra;
    $context['settings_title'] = $txt['manageposts_bbc_settings_title'];

    prepareDBSettingContext($config_vars);
}

/**
 * Allows modifying the global layout settings in the forum
 * Accessed through ?action=admin;area=featuresettings;sa=layout;
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyLayoutSettings($return_config = false)
{
    global $txt, $scripturl, $context;

    $config_vars = array(
        // Pagination stuff.
        array('check', 'compactTopicPagesEnable'),
        array(
            'int',
            'compactTopicPagesContiguous',
            null,
            $txt['contiguous_page_display'] . '<div class="smalltext">' . str_replace(' ', '&nbsp;', '"3" ' . $txt['to_display'] . ': <strong>1 ... 4 [5] 6 ... 9</strong>') . '<br>' . str_replace(' ', '&nbsp;', '"5" ' . $txt['to_display'] . ': <strong>1 ... 3 4 [5] 6 7 ... 9</strong>') . '</div>'
        ),
        array('int', 'defaultMaxMembers'),
        '',

        // Stuff that just is everywhere - today, search, online, etc.
        array('select', 'todayMod', array($txt['today_disabled'], $txt['today_only'], $txt['yesterday_today'])),
        array('check', 'onlineEnable'),
        '',

        // This is like debugging sorta.
        array('check', 'timeLoadPageEnable'),
    );

    call_integration_hook('integrate_layout_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        call_integration_hook('integrate_save_layout_settings');

        saveDBSettings($config_vars);
        $_SESSION['adm-save'] = true;
        writeLog();

        redirectexit('action=admin;area=featuresettings;sa=layout');
    }

    $context['post_url'] = $scripturl . '?action=admin;area=featuresettings;save;sa=layout';
    $context['settings_title'] = $txt['mods_cat_layout'];

    prepareDBSettingContext($config_vars);
}

/**
 * Config array for changing like settings
 * Accessed  from ?action=admin;area=featuresettings;sa=likes;
 *
 * @param bool $return_config Whether or not to return the config_vars array
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyLikesSettings($return_config = false)
{
    global $txt, $scripturl, $context;

    $config_vars = array(
        array('check', 'enable_likes'),
        array('permissions', 'likes_like'),
    );

    call_integration_hook('integrate_likes_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        call_integration_hook('integrate_save_likes_settings');

        saveDBSettings($config_vars);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=featuresettings;sa=likes');
    }

    $context['post_url'] = $scripturl . '?action=admin;area=featuresettings;save;sa=likes';
    $context['settings_title'] = $txt['likes'];

    prepareDBSettingContext($config_vars);
}

/**
 * Config array for changing like settings
 * Accessed  from ?action=admin;area=featuresettings;sa=mentions;
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyMentionsSettings($return_config = false)
{
    global $txt, $scripturl, $context;

    $config_vars = array(
        array('check', 'enable_mentions'),
        array('permissions', 'mention'),
    );

    call_integration_hook('integrate_mentions_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        call_integration_hook('integrate_save_mentions_settings');

        saveDBSettings($config_vars);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=featuresettings;sa=mentions');
    }

    $context['post_url'] = $scripturl . '?action=admin;area=featuresettings;save;sa=mentions';
    $context['settings_title'] = $txt['mentions'];

    prepareDBSettingContext($config_vars);
}

/**
 * Moderation type settings - although there are fewer than we have you believe ;)
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyWarningSettings($return_config = false)
{
    global $txt, $scripturl, $context, $modSettings, $sourcedir;

    // You need to be an admin to edit settings!
    isAllowedTo('admin_forum');

    loadLanguage('Help');
    loadLanguage('ManageSettings');

    // We need the existing ones for this
    list ($currently_enabled, $modSettings['user_limit'], $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']);

    $config_vars = array(
        // Warning system?
        'enable' => array('check', 'warning_enable'),
    );

    if (!empty($modSettings['warning_settings']) && $currently_enabled)
        $config_vars += array(
            '',

            array(
                'int',
                'warning_watch',
                'subtext' => $txt['setting_warning_watch_note'] . ' ' . $txt['zero_to_disable']
            ),
            'moderate' => array(
                'int',
                'warning_moderate',
                'subtext' => $txt['setting_warning_moderate_note'] . ' ' . $txt['zero_to_disable']
            ),
            array(
                'int',
                'warning_mute',
                'subtext' => $txt['setting_warning_mute_note'] . ' ' . $txt['zero_to_disable']
            ),
            'rem1' => array(
                'int',
                'user_limit',
                'subtext' => $txt['setting_user_limit_note']
            ),
            'rem2' => array(
                'int',
                'warning_decrement',
                'subtext' => $txt['setting_warning_decrement_note'] . ' ' . $txt['zero_to_disable']
            ),
            array('permissions', 'view_warning_any'),
            array('permissions', 'view_warning_own'),
        );

    call_integration_hook('integrate_warning_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Cannot use moderation if post moderation is not enabled.
    if (!$modSettings['postmod_active'])
        unset($config_vars['moderate']);

    // Will need the utility functions from here.
    require_once($sourcedir . '/ManageServer.php');

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        // Make sure these don't have an effect.
        if (!$currently_enabled && empty($_POST['warning_enable']))
        {
            $_POST['warning_watch'] = 0;
            $_POST['warning_moderate'] = 0;
            $_POST['warning_mute'] = 0;
        }
        // If it was disabled and we're enabling it now, set some sane defaults.
        elseif (!$currently_enabled && !empty($_POST['warning_enable']))
        {
            // Need to add these, these weren't there before...
            $vars = array(
                'warning_watch' => 10,
                'warning_mute' => 60,
            );
            if ($modSettings['postmod_active'])
                $vars['warning_moderate'] = 35;

            foreach ($vars as $var => $value)
            {
                $config_vars[] = array('int', $var);
                $_POST[$var] = $value;
            }
        }
        else
        {
            $_POST['warning_watch'] = min($_POST['warning_watch'], 100);
            $_POST['warning_moderate'] = $modSettings['postmod_active'] ? min($_POST['warning_moderate'], 100) : 0;
            $_POST['warning_mute'] = min($_POST['warning_mute'], 100);
        }

        // We might not have these already depending on how we got here.
        $_POST['user_limit'] = isset($_POST['user_limit']) ? (int) $_POST['user_limit'] : $modSettings['user_limit'];
        $_POST['warning_decrement'] = isset($_POST['warning_decrement']) ? (int) $_POST['warning_decrement'] : $modSettings['warning_decrement'];

        // Fix the warning setting array!
        $_POST['warning_settings'] = (!empty($_POST['warning_enable']) ? 1 : 0) . ',' . min(100, $_POST['user_limit']) . ',' . min(100, $_POST['warning_decrement']);
        $save_vars = $config_vars;
        $save_vars[] = array('text', 'warning_settings');
        unset($save_vars['enable'], $save_vars['rem1'], $save_vars['rem2']);

        call_integration_hook('integrate_save_warning_settings', array(&$save_vars));

        saveDBSettings($save_vars);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=warnings');
    }

    // We actually store lots of these together - for efficiency.
    list ($modSettings['warning_enable'], $modSettings['user_limit'], $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']);

    $context['sub_template'] = 'show_settings';
    $context['post_url'] = $scripturl . '?action=admin;area=warnings;save';
    $context['settings_title'] = $txt['warnings'];
    $context['page_title'] = $txt['warnings'];

    $context[$context['admin_menu_name']]['tab_data'] = array(
        'title' => $txt['warnings'],
        'help' => '',
        'description' => $txt['warnings_desc'],
    );

    prepareDBSettingContext($config_vars);
}

/**
 * Let's try keep the spam to a minimum ah Thantos?
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyAntispamSettings($return_config = false)
{
    global $txt, $scripturl, $context, $modSettings, $smcFunc, $language, $sourcedir;

    loadLanguage('Help');
    loadLanguage('ManageSettings');

    // Generate a sample registration image.
    $context['use_graphic_library'] = in_array('gd', get_loaded_extensions());
    $context['verification_image_href'] = $scripturl . '?action=verificationcode;rand=' . md5(mt_rand());

    $config_vars = array(
        array('check', 'reg_verification'),
        array('check', 'search_enable_captcha'),
        // This, my friend, is a cheat :p
        'guest_verify' => array(
            'check',
            'guests_require_captcha',
            'subtext' => $txt['setting_guests_require_captcha_desc']
        ),
        array(
            'int',
            'posts_require_captcha',
            'subtext' => $txt['posts_require_captcha_desc'],
            'onchange' => 'if (this.value > 0){ document.getElementById(\'guests_require_captcha\').checked = true; document.getElementById(\'guests_require_captcha\').disabled = true;} else {document.getElementById(\'guests_require_captcha\').disabled = false;}'
        ),
        '',

        // PM Settings
        'pm1' => array('int', 'max_pm_recipients', 'subtext' => $txt['max_pm_recipients_note']),
        'pm2' => array('int', 'pm_posts_verification', 'subtext' => $txt['pm_posts_verification_note']),
        'pm3' => array('int', 'pm_posts_per_hour', 'subtext' => $txt['pm_posts_per_hour_note']),
        // Visual verification.
        array('title', 'configure_verification_means'),
        array('desc', 'configure_verification_means_desc'),
        'vv' => array(
            'select',
            'visual_verification_type',
            array(
                $txt['setting_image_verification_off'],
                $txt['setting_image_verification_vsimple'],
                $txt['setting_image_verification_simple'],
                $txt['setting_image_verification_medium'],
                $txt['setting_image_verification_high'],
                $txt['setting_image_verification_extreme']
            ),
            'subtext' => $txt['setting_visual_verification_type_desc'],
            'onchange' => $context['use_graphic_library'] ? 'refreshImages();' : ''
        ),
        // reCAPTCHA
        array('title', 'recaptcha_configure'),
        array('desc', 'recaptcha_configure_desc', 'class' => 'windowbg'),
        array('check', 'recaptcha_enabled', 'subtext' => $txt['recaptcha_enable_desc']),
        array('text', 'recaptcha_site_key', 'subtext' => $txt['recaptcha_site_key_desc']),
        array('text', 'recaptcha_secret_key', 'subtext' => $txt['recaptcha_secret_key_desc']),
        array('select', 'recaptcha_theme', array('light' => $txt['recaptcha_theme_light'], 'dark' => $txt['recaptcha_theme_dark'])),
        // Clever Thomas, who is looking sheepy now? Not I, the mighty sword swinger did say.
        array('title', 'setup_verification_questions'),
        array('desc', 'setup_verification_questions_desc'),
        array('int', 'qa_verification_number', 'subtext' => $txt['setting_qa_verification_number_desc']),
        array('callback', 'question_answer_list'),
    );

    call_integration_hook('integrate_spam_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // You need to be an admin to edit settings!
    isAllowedTo('admin_forum');

    // Firstly, figure out what languages we're dealing with, and do a little processing for the form's benefit.
    getLanguages();
    $context['qa_languages'] = array();
    foreach ($context['languages'] as $lang_id => $lang)
    {
        $lang_id = strtr($lang_id, array('-utf8' => ''));
        $lang['name'] = strtr($lang['name'], array('-utf8' => ''));
        $context['qa_languages'][$lang_id] = $lang;
    }

    // Secondly, load any questions we currently have.
    $context['question_answers'] = array();
    $request = $smcFunc['db_query']('', '
        SELECT id_question, lngfile, question, answers
        FROM {db_prefix}qanda'
    );
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $lang = strtr($row['lngfile'], array('-utf8' => ''));
        $context['question_answers'][$row['id_question']] = array(
            'lngfile' => $lang,
            'question' => $row['question'],
            'answers' => $smcFunc['json_decode']($row['answers'], true),
        );
        $context['qa_by_lang'][$lang][] = $row['id_question'];
    }

    if (empty($context['qa_by_lang'][strtr($language, array('-utf8' => ''))]) && !empty($context['question_answers']))
    {
        if (empty($context['settings_insert_above']))
            $context['settings_insert_above'] = '';

        $context['settings_insert_above'] .= '<div class="noticebox">' . sprintf($txt['question_not_defined'], $context['languages'][$language]['name']) . '</div>';
    }

    // Thirdly, push some JavaScript for the form to make it work.
    addInlineJavaScript('
    var nextrow = ' . (!empty($context['question_answers']) ? max(array_keys($context['question_answers'])) + 1 : 1) . ';
    $(".qa_link a").click(function() {
        var id = $(this).parent().attr("id").substring(6);
        $("#qa_fs_" + id).show();
        $(this).parent().hide();
    });
    $(".qa_fieldset legend a").click(function() {
        var id = $(this).closest("fieldset").attr("id").substring(6);
        $("#qa_dt_" + id).show();
        $(this).closest("fieldset").hide();
    });
    $(".qa_add_question a").click(function() {
        var id = $(this).closest("fieldset").attr("id").substring(6);
        $(\'<dt><input type="text" name="question[\' + id + \'][\' + nextrow + \']" value="" size="50" class="verification_question"></dt><dd><input type="text" name="answer[\' + id + \'][\' + nextrow + \'][]" value="" size="50" class="verification_answer" / ><div class="qa_add_answer"><a href="javascript:void(0);">[ \' + ' . JavaScriptEscape($txt['setup_verification_add_answer']) . ' + \' ]</a></div></dd>\').insertBefore($(this).parent());
        nextrow++;
    });
    $(".qa_fieldset ").on("click", ".qa_add_answer a", function() {
        var attr = $(this).closest("dd").find(".verification_answer:last").attr("name");
        $(\'<input type="text" name="\' + attr + \'" value="" size="50" class="verification_answer">\').insertBefore($(this).closest("div"));
        return false;
    });
    $("#qa_dt_' . strtr($language, array('-utf8' => '')) . ' a").click();', true);

    // Will need the utility functions from here.
    require_once($sourcedir . '/ManageServer.php');

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        // Fix PM settings.
        $_POST['pm_spam_settings'] = (int) $_POST['max_pm_recipients'] . ',' . (int) $_POST['pm_posts_verification'] . ',' . (int) $_POST['pm_posts_per_hour'];

        // Hack in guest requiring verification!
        if (empty($_POST['posts_require_captcha']) && !empty($_POST['guests_require_captcha']))
            $_POST['posts_require_captcha'] = -1;

        $save_vars = $config_vars;
        unset($save_vars['pm1'], $save_vars['pm2'], $save_vars['pm3'], $save_vars['guest_verify']);

        $save_vars[] = array('text', 'pm_spam_settings');

        // Handle verification questions.
        $changes = array(
            'insert' => array(),
            'replace' => array(),
            'delete' => array(),
        );
        $qs_per_lang = array();
        foreach ($context['qa_languages'] as $lang_id => $dummy)
        {
            // If we had some questions for this language before, but don't now, delete everything from that language.
            if ((!isset($_POST['question'][$lang_id]) || !is_array($_POST['question'][$lang_id])) && !empty($context['qa_by_lang'][$lang_id]))
                $changes['delete'] = array_merge($changes['delete'], $context['qa_by_lang'][$lang_id]);

            // Now step through and see if any existing questions no longer exist.
            if (!empty($context['qa_by_lang'][$lang_id]))
                foreach ($context['qa_by_lang'][$lang_id] as $q_id)
                    if (empty($_POST['question'][$lang_id][$q_id]))
                        $changes['delete'][] = $q_id;

            // Now let's see if there are new questions or ones that need updating.
            if (isset($_POST['question'][$lang_id]))
            {
                foreach ($_POST['question'][$lang_id] as $q_id => $question)
                {
                    // Ignore junky ids.
                    $q_id = (int) $q_id;
                    if ($q_id <= 0)
                        continue;

                    // Check the question isn't empty (because they want to delete it?)
                    if (empty($question) || trim($question) == '')
                    {
                        if (isset($context['question_answers'][$q_id]))
                            $changes['delete'][] = $q_id;
                        continue;
                    }
                    $question = $smcFunc['htmlspecialchars'](trim($question));

                    // Get the answers. Firstly check there actually might be some.
                    if (!isset($_POST['answer'][$lang_id][$q_id]) || !is_array($_POST['answer'][$lang_id][$q_id]))
                    {
                        if (isset($context['question_answers'][$q_id]))
                            $changes['delete'][] = $q_id;
                        continue;
                    }
                    // Now get them and check that they might be viable.
                    $answers = array();
                    foreach ($_POST['answer'][$lang_id][$q_id] as $answer)
                        if (!empty($answer) && trim($answer) !== '')
                            $answers[] = $smcFunc['htmlspecialchars'](trim($answer));
                    if (empty($answers))
                    {
                        if (isset($context['question_answers'][$q_id]))
                            $changes['delete'][] = $q_id;
                        continue;
                    }
                    $answers = $smcFunc['json_encode']($answers);

                    // At this point we know we have a question and some answers. What are we doing with it?
                    if (!isset($context['question_answers'][$q_id]))
                    {
                        // New question. Now, we don't want to randomly consume ids, so we'll set those, rather than trusting the browser's supplied ids.
                        $changes['insert'][] = array($lang_id, $question, $answers);
                    }
                    else
                    {
                        // It's an existing question. Let's see what's changed, if anything.
                        if ($lang_id != $context['question_answers'][$q_id]['lngfile'] || $question != $context['question_answers'][$q_id]['question'] || $answers != $context['question_answers'][$q_id]['answers'])
                            $changes['replace'][$q_id] = array('lngfile' => $lang_id, 'question' => $question, 'answers' => $answers);
                    }

                    if (!isset($qs_per_lang[$lang_id]))
                        $qs_per_lang[$lang_id] = 0;
                    $qs_per_lang[$lang_id]++;
                }
            }
        }

        // OK, so changes?
        if (!empty($changes['delete']))
        {
            $smcFunc['db_query']('', '
                DELETE FROM {db_prefix}qanda
                WHERE id_question IN ({array_int:questions})',
                array(
                    'questions' => $changes['delete'],
                )
            );
        }

        if (!empty($changes['replace']))
        {
            foreach ($changes['replace'] as $q_id => $question)
            {
                $smcFunc['db_query']('', '
                    UPDATE {db_prefix}qanda
                    SET lngfile = {string:lngfile},
                        question = {string:question},
                        answers = {string:answers}
                    WHERE id_question = {int:id_question}',
                    array(
                        'id_question' => $q_id,
                        'lngfile' => $question['lngfile'],
                        'question' => $question['question'],
                        'answers' => $question['answers'],
                    )
                );
            }
        }

        if (!empty($changes['insert']))
        {
            $smcFunc['db_insert']('insert',
                '{db_prefix}qanda',
                array('lngfile' => 'string-50', 'question' => 'string-255', 'answers' => 'string-65534'),
                $changes['insert'],
                array('id_question')
            );
        }

        // Lastly, the count of messages needs to be no more than the lowest number of questions for any one language.
        $count_questions = empty($qs_per_lang) ? 0 : min($qs_per_lang);
        if (empty($count_questions) || $_POST['qa_verification_number'] > $count_questions)
            $_POST['qa_verification_number'] = $count_questions;

        call_integration_hook('integrate_save_spam_settings', array(&$save_vars));

        // Now save.
        saveDBSettings($save_vars);
        $_SESSION['adm-save'] = true;

        cache_put_data('verificationQuestions', null, 300);

        redirectexit('action=admin;area=antispam');
    }

    $character_range = array_merge(range('A', 'H'), array('K', 'M', 'N', 'P', 'R'), range('T', 'Y'));
    $_SESSION['visual_verification_code'] = '';
    for ($i = 0; $i < 6; $i++)
        $_SESSION['visual_verification_code'] .= $character_range[array_rand($character_range)];

    // Some javascript for CAPTCHA.
    $context['settings_post_javascript'] = '';
    if ($context['use_graphic_library'])
        $context['settings_post_javascript'] .= '
        function refreshImages()
        {
            var imageType = document.getElementById(\'visual_verification_type\').value;
            document.getElementById(\'verification_image\').src = \'' . $context['verification_image_href'] . ';type=\' + imageType;
        }';

    // Show the image itself, or text saying we can't.
    if ($context['use_graphic_library'])
        $config_vars['vv']['postinput'] = '<br><img src="' . $context['verification_image_href'] . ';type=' . (empty($modSettings['visual_verification_type']) ? 0 : $modSettings['visual_verification_type']) . '" alt="' . $txt['setting_image_verification_sample'] . '" id="verification_image"><br>';
    else
        $config_vars['vv']['postinput'] = '<br><span class="smalltext">' . $txt['setting_image_verification_nogd'] . '</span>';

    // Hack for PM spam settings.
    list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);

    // Hack for guests requiring verification.
    $modSettings['guests_require_captcha'] = !empty($modSettings['posts_require_captcha']);
    $modSettings['posts_require_captcha'] = !isset($modSettings['posts_require_captcha']) || $modSettings['posts_require_captcha'] == -1 ? 0 : $modSettings['posts_require_captcha'];

    // Some minor javascript for the guest post setting.
    if ($modSettings['posts_require_captcha'])
        $context['settings_post_javascript'] .= '
        document.getElementById(\'guests_require_captcha\').disabled = true;';

    // And everything else.
    $context['post_url'] = $scripturl . '?action=admin;area=antispam;save';
    $context['settings_title'] = $txt['antispam_Settings'];
    $context['page_title'] = $txt['antispam_title'];
    $context['sub_template'] = 'show_settings';

    $context[$context['admin_menu_name']]['tab_data'] = array(
        'title' => $txt['antispam_title'],
        'description' => $txt['antispam_Settings_desc'],
    );

    prepareDBSettingContext($config_vars);
}

/**
 * You'll never guess what this function does...
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifySignatureSettings($return_config = false)
{
    global $context, $txt, $modSettings, $sig_start, $smcFunc, $scripturl;

    $config_vars = array(
        // Are signatures even enabled?
        array('check', 'signature_enable'),
        '',

        // Tweaking settings!
        array('int', 'signature_max_length', 'subtext' => $txt['zero_for_no_limit']),
        array('int', 'signature_max_lines', 'subtext' => $txt['zero_for_no_limit']),
        array('int', 'signature_max_font_size', 'subtext' => $txt['zero_for_no_limit']),
        array('check', 'signature_allow_smileys', 'onclick' => 'document.getElementById(\'signature_max_smileys\').disabled = !this.checked;'),
        array('int', 'signature_max_smileys', 'subtext' => $txt['zero_for_no_limit']),
        '',

        // Image settings.
        array('int', 'signature_max_images', 'subtext' => $txt['signature_max_images_note']),
        array('int', 'signature_max_image_width', 'subtext' => $txt['zero_for_no_limit']),
        array('int', 'signature_max_image_height', 'subtext' => $txt['zero_for_no_limit']),
        '',

        array('bbc', 'signature_bbc'),
    );

    call_integration_hook('integrate_signature_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    // Setup the template.
    $context['page_title'] = $txt['signature_settings'];
    $context['sub_template'] = 'show_settings';

    // Disable the max smileys option if we don't allow smileys at all!
    $context['settings_post_javascript'] = 'document.getElementById(\'signature_max_smileys\').disabled = !document.getElementById(\'signature_allow_smileys\').checked;';

    // Load all the signature settings.
    list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
    $sig_limits = explode(',', $sig_limits);
    $disabledTags = !empty($sig_bbc) ? explode(',', $sig_bbc) : array();

    // Applying to ALL signatures?!!
    if (isset($_GET['apply']))
    {
        // Security!
        checkSession('get');

        $sig_start = time();
        // This is horrid - but I suppose some people will want the option to do it.
        $_GET['step'] = isset($_GET['step']) ? (int) $_GET['step'] : 0;
        $done = false;

        $request = $smcFunc['db_query']('', '
            SELECT MAX(id_member)
            FROM {db_prefix}members',
            array(
            )
        );
        list ($context['max_member']) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);

        while (!$done)
        {
            $changes = array();

            $request = $smcFunc['db_query']('', '
                SELECT id_member, signature
                FROM {db_prefix}members
                WHERE id_member BETWEEN {int:step} AND {int:step} + 49
                    AND id_group != {int:admin_group}
                    AND FIND_IN_SET({int:admin_group}, additional_groups) = 0',
                array(
                    'admin_group' => 1,
                    'step' => $_GET['step'],
                )
            );
            while ($row = $smcFunc['db_fetch_assoc']($request))
            {
                // Apply all the rules we can realistically do.
                $sig = strtr($row['signature'], array('<br>' => "\n"));

                // Max characters...
                if (!empty($sig_limits[1]))
                    $sig = $smcFunc['substr']($sig, 0, $sig_limits[1]);
                // Max lines...
                if (!empty($sig_limits[2]))
                {
                    $count = 0;
                    for ($i = 0; $i < strlen($sig); $i++)
                    {
                        if ($sig[$i] == "\n")
                        {
                            $count++;
                            if ($count >= $sig_limits[2])
                                $sig = substr($sig, 0, $i) . strtr(substr($sig, $i), array("\n" => ' '));
                        }
                    }
                }

                if (!empty($sig_limits[7]) && preg_match_all('~\[size=([\d\.]+)?(px|pt|em|x-large|larger)~i', $sig, $matches) !== false && isset($matches[2]))
                {
                    foreach ($matches[1] as $ind => $size)
                    {
                        $limit_broke = 0;
                        // Attempt to allow all sizes of abuse, so to speak.
                        if ($matches[2][$ind] == 'px' && $size > $sig_limits[7])
                            $limit_broke = $sig_limits[7] . 'px';
                        elseif ($matches[2][$ind] == 'pt' && $size > ($sig_limits[7] * 0.75))
                            $limit_broke = ((int) $sig_limits[7] * 0.75) . 'pt';
                        elseif ($matches[2][$ind] == 'em' && $size > ((float) $sig_limits[7] / 16))
                            $limit_broke = ((float) $sig_limits[7] / 16) . 'em';
                        elseif ($matches[2][$ind] != 'px' && $matches[2][$ind] != 'pt' && $matches[2][$ind] != 'em' && $sig_limits[7] < 18)
                            $limit_broke = 'large';

                        if ($limit_broke)
                            $sig = str_replace($matches[0][$ind], '[size=' . $sig_limits[7] . 'px', $sig);
                    }
                }

                // Stupid images - this is stupidly, stupidly challenging.
                if ((!empty($sig_limits[3]) || !empty($sig_limits[5]) || !empty($sig_limits[6])))
                {
                    $replaces = array();
                    $img_count = 0;
                    // Get all BBC tags...
                    preg_match_all('~\[img(\s+width=([\d]+))?(\s+height=([\d]+))?(\s+width=([\d]+))?\s*\](?:<br>)*([^<">]+?)(?:<br>)*\[/img\]~i', $sig, $matches);
                    // ... and all HTML ones.
                    preg_match_all('~&lt;img\s+src=(?:&quot;)?((?:http://|ftp://|https://|ftps://).+?)(?:&quot;)?(?:\s+alt=(?:&quot;)?(.*?)(?:&quot;)?)?(?:\s?/)?&gt;~i', $sig, $matches2, PREG_PATTERN_ORDER);
                    // And stick the HTML in the BBC.
                    if (!empty($matches2))
                    {
                        foreach ($matches2[0] as $ind => $dummy)
                        {
                            $matches[0][] = $matches2[0][$ind];
                            $matches[1][] = '';
                            $matches[2][] = '';
                            $matches[3][] = '';
                            $matches[4][] = '';
                            $matches[5][] = '';
                            $matches[6][] = '';
                            $matches[7][] = $matches2[1][$ind];
                        }
                    }
                    // Try to find all the images!
                    if (!empty($matches))
                    {
                        $image_count_holder = array();
                        foreach ($matches[0] as $key => $image)
                        {
                            $width = -1;
                            $height = -1;
                            $img_count++;
                            // Too many images?
                            if (!empty($sig_limits[3]) && $img_count > $sig_limits[3])
                            {
                                // If we've already had this before we only want to remove the excess.
                                if (isset($image_count_holder[$image]))
                                {
                                    $img_offset = -1;
                                    $rep_img_count = 0;
                                    while ($img_offset !== false)
                                    {
                                        $img_offset = strpos($sig, $image, $img_offset + 1);
                                        $rep_img_count++;
                                        if ($rep_img_count > $image_count_holder[$image])
                                        {
                                            // Only replace the excess.
                                            $sig = substr($sig, 0, $img_offset) . str_replace($image, '', substr($sig, $img_offset));
                                            // Stop looping.
                                            $img_offset = false;
                                        }
                                    }
                                }
                                else
                                    $replaces[$image] = '';

                                continue;
                            }

                            // Does it have predefined restraints? Width first.
                            if ($matches[6][$key])
                                $matches[2][$key] = $matches[6][$key];
                            if ($matches[2][$key] && $sig_limits[5] && $matches[2][$key] > $sig_limits[5])
                            {
                                $width = $sig_limits[5];
                                $matches[4][$key] = $matches[4][$key] * ($width / $matches[2][$key]);
                            }
                            elseif ($matches[2][$key])
                                $width = $matches[2][$key];
                            // ... and height.
                            if ($matches[4][$key] && $sig_limits[6] && $matches[4][$key] > $sig_limits[6])
                            {
                                $height = $sig_limits[6];
                                if ($width != -1)
                                    $width = $width * ($height / $matches[4][$key]);
                            }
                            elseif ($matches[4][$key])
                                $height = $matches[4][$key];

                            // If the dimensions are still not fixed - we need to check the actual image.
                            if (($width == -1 && $sig_limits[5]) || ($height == -1 && $sig_limits[6]))
                            {
                                $sizes = url_image_size($matches[7][$key]);
                                if (is_array($sizes))
                                {
                                    // Too wide?
                                    if ($sizes[0] > $sig_limits[5] && $sig_limits[5])
                                    {
                                        $width = $sig_limits[5];
                                        $sizes[1] = $sizes[1] * ($width / $sizes[0]);
                                    }
                                    // Too high?
                                    if ($sizes[1] > $sig_limits[6] && $sig_limits[6])
                                    {
                                        $height = $sig_limits[6];
                                        if ($width == -1)
                                            $width = $sizes[0];
                                        $width = $width * ($height / $sizes[1]);
                                    }
                                    elseif ($width != -1)
                                        $height = $sizes[1];
                                }
                            }

                            // Did we come up with some changes? If so remake the string.
                            if ($width != -1 || $height != -1)
                            {
                                $replaces[$image] = '[img' . ($width != -1 ? ' width=' . round($width) : '') . ($height != -1 ? ' height=' . round($height) : '') . ']' . $matches[7][$key] . '[/img]';
                            }

                            // Record that we got one.
                            $image_count_holder[$image] = isset($image_count_holder[$image]) ? $image_count_holder[$image] + 1 : 1;
                        }
                        if (!empty($replaces))
                            $sig = str_replace(array_keys($replaces), array_values($replaces), $sig);
                    }
                }
                // Try to fix disabled tags.
                if (!empty($disabledTags))
                {
                    $sig = preg_replace('~\[(?:' . implode('|', $disabledTags) . ').+?\]~i', '', $sig);
                    $sig = preg_replace('~\[/(?:' . implode('|', $disabledTags) . ')\]~i', '', $sig);
                }

                $sig = strtr($sig, array("\n" => '<br>'));
                call_integration_hook('integrate_apply_signature_settings', array(&$sig, $sig_limits, $disabledTags));
                if ($sig != $row['signature'])
                    $changes[$row['id_member']] = $sig;
            }
            if ($smcFunc['db_num_rows']($request) == 0)
                $done = true;
            $smcFunc['db_free_result']($request);

            // Do we need to delete what we have?
            if (!empty($changes))
            {
                foreach ($changes as $id => $sig)
                    $smcFunc['db_query']('', '
                        UPDATE {db_prefix}members
                        SET signature = {string:signature}
                        WHERE id_member = {int:id_member}',
                        array(
                            'id_member' => $id,
                            'signature' => $sig,
                        )
                    );
            }

            $_GET['step'] += 50;
            if (!$done)
                pauseSignatureApplySettings();
        }
        $settings_applied = true;
    }

    $context['signature_settings'] = array(
        'enable' => isset($sig_limits[0]) ? $sig_limits[0] : 0,
        'max_length' => isset($sig_limits[1]) ? $sig_limits[1] : 0,
        'max_lines' => isset($sig_limits[2]) ? $sig_limits[2] : 0,
        'max_images' => isset($sig_limits[3]) ? $sig_limits[3] : 0,
        'allow_smileys' => isset($sig_limits[4]) && $sig_limits[4] == -1 ? 0 : 1,
        'max_smileys' => isset($sig_limits[4]) && $sig_limits[4] != -1 ? $sig_limits[4] : 0,
        'max_image_width' => isset($sig_limits[5]) ? $sig_limits[5] : 0,
        'max_image_height' => isset($sig_limits[6]) ? $sig_limits[6] : 0,
        'max_font_size' => isset($sig_limits[7]) ? $sig_limits[7] : 0,
    );

    // Temporarily make each setting a modSetting!
    foreach ($context['signature_settings'] as $key => $value)
        $modSettings['signature_' . $key] = $value;

    // Make sure we check the right tags!
    $modSettings['bbc_disabled_signature_bbc'] = $disabledTags;

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        // Clean up the tag stuff!
        $bbcTags = array();
        foreach (parse_bbc(false) as $tag)
            $bbcTags[] = $tag['tag'];

        if (!isset($_POST['signature_bbc_enabledTags']))
            $_POST['signature_bbc_enabledTags'] = array();
        elseif (!is_array($_POST['signature_bbc_enabledTags']))
            $_POST['signature_bbc_enabledTags'] = array($_POST['signature_bbc_enabledTags']);

        $sig_limits = array();
        foreach ($context['signature_settings'] as $key => $value)
        {
            if ($key == 'allow_smileys')
                continue;
            elseif ($key == 'max_smileys' && empty($_POST['signature_allow_smileys']))
                $sig_limits[] = -1;
            else
                $sig_limits[] = !empty($_POST['signature_' . $key]) ? max(1, (int) $_POST['signature_' . $key]) : 0;
        }

        call_integration_hook('integrate_save_signature_settings', array(&$sig_limits, &$bbcTags));

        $_POST['signature_settings'] = implode(',', $sig_limits) . ':' . implode(',', array_diff($bbcTags, $_POST['signature_bbc_enabledTags']));

        // Even though we have practically no settings let's keep the convention going!
        $save_vars = array();
        $save_vars[] = array('text', 'signature_settings');

        saveDBSettings($save_vars);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=featuresettings;sa=sig');
    }

    $context['post_url'] = $scripturl . '?action=admin;area=featuresettings;save;sa=sig';
    $context['settings_title'] = $txt['signature_settings'];

    $context['settings_message'] = !empty($settings_applied) ? '<div class="infobox">' . $txt['signature_settings_applied'] . '</div>' : '<p class="centertext">' . sprintf($txt['signature_settings_warning'], $context['session_id'], $context['session_var']) . '</p>';

    prepareDBSettingContext($config_vars);
}

/**
 * Just pause the signature applying thing.
 */
function pauseSignatureApplySettings()
{
    global $context, $txt, $sig_start;

    // Try get more time...
    @set_time_limit(600);
    if (function_exists('apache_reset_timeout'))
        @apache_reset_timeout();

    // Have we exhausted all the time we allowed?
    if (time() - array_sum(explode(' ', $sig_start)) < 3)
        return;

    $context['continue_get_data'] = '?action=admin;area=featuresettings;sa=sig;apply;step=' . $_GET['step'] . ';' . $context['session_var'] . '=' . $context['session_id'];
    $context['page_title'] = $txt['not_done_title'];
    $context['continue_post_data'] = '';
    $context['continue_countdown'] = '2';
    $context['sub_template'] = 'not_done';

    // Specific stuff to not break this template!
    $context[$context['admin_menu_name']]['current_subsection'] = 'sig';

    // Get the right percent.
    $context['continue_percent'] = round(($_GET['step'] / $context['max_member']) * 100);

    // Never more than 100%!
    $context['continue_percent'] = min($context['continue_percent'], 100);

    obExit();
}

/**
 * Show all the custom profile fields available to the user.
 */
function ShowCustomProfiles()
{
    global $txt, $scripturl, $context;
    global $sourcedir;

    $context['page_title'] = $txt['custom_profile_title'];
    $context['sub_template'] = 'show_custom_profile';

    // What about standard fields they can tweak?
    $standard_fields = array('website', 'personal_text', 'timezone', 'posts', 'warning_status');
    // What fields can't you put on the registration page?
    $context['fields_no_registration'] = array('posts', 'warning_status');

    // Are we saving any standard field changes?
    if (isset($_POST['save']))
    {
        checkSession();
        validateToken('admin-scp');

        // Do the active ones first.
        $disable_fields = array_flip($standard_fields);
        if (!empty($_POST['active']))
        {
            foreach ($_POST['active'] as $value)
                if (isset($disable_fields[$value]))
                    unset($disable_fields[$value]);
        }
        // What we have left!
        $changes['disabled_profile_fields'] = empty($disable_fields) ? '' : implode(',', array_keys($disable_fields));

        // Things we want to show on registration?
        $reg_fields = array();
        if (!empty($_POST['reg']))
        {
            foreach ($_POST['reg'] as $value)
                if (in_array($value, $standard_fields) && !isset($disable_fields[$value]))
                    $reg_fields[] = $value;
        }
        // What we have left!
        $changes['registration_fields'] = empty($reg_fields) ? '' : implode(',', $reg_fields);

        $_SESSION['adm-save'] = true;
        if (!empty($changes))
            updateSettings($changes);
    }

    createToken('admin-scp');

    // Need to know the max order for custom fields
    $context['custFieldsMaxOrder'] = custFieldsMaxOrder();

    require_once($sourcedir . '/Subs-List.php');

    $listOptions = array(
        'id' => 'standard_profile_fields',
        'title' => $txt['standard_profile_title'],
        'base_href' => $scripturl . '?action=admin;area=featuresettings;sa=profile',
        'get_items' => array(
            'function' => 'list_getProfileFields',
            'params' => array(
                true,
            ),
        ),
        'columns' => array(
            'field' => array(
                'header' => array(
                    'value' => $txt['standard_profile_field'],
                ),
                'data' => array(
                    'db' => 'label',
                    'style' => 'width: 60%;',
                ),
            ),
            'active' => array(
                'header' => array(
                    'value' => $txt['custom_edit_active'],
                    'class' => 'centercol',
                ),
                'data' => array(
                    'function' => function($rowData)
                    {
                        $isChecked = $rowData['disabled'] ? '' : ' checked';
                        $onClickHandler = $rowData['can_show_register'] ? sprintf(' onclick="document.getElementById(\'reg_%1$s\').disabled = !this.checked;"', $rowData['id']) : '';
                        return sprintf('<input type="checkbox" name="active[]" id="active_%1$s" value="%1$s" %2$s%3$s>', $rowData['id'], $isChecked, $onClickHandler);
                    },
                    'style' => 'width: 20%;',
                    'class' => 'centercol',
                ),
            ),
            'show_on_registration' => array(
                'header' => array(
                    'value' => $txt['custom_edit_registration'],
                    'class' => 'centercol',
                ),
                'data' => array(
                    'function' => function($rowData)
                    {
                        $isChecked = $rowData['on_register'] && !$rowData['disabled'] ? ' checked' : '';
                        $isDisabled = $rowData['can_show_register'] ? '' : ' disabled';
                        return sprintf('<input type="checkbox" name="reg[]" id="reg_%1$s" value="%1$s" %2$s%3$s>', $rowData['id'], $isChecked, $isDisabled);
                    },
                    'style' => 'width: 20%;',
                    'class' => 'centercol',
                ),
            ),
        ),
        'form' => array(
            'href' => $scripturl . '?action=admin;area=featuresettings;sa=profile',
            'name' => 'standardProfileFields',
            'token' => 'admin-scp',
        ),
        'additional_rows' => array(
            array(
                'position' => 'below_table_data',
                'value' => '<input type="submit" name="save" value="' . $txt['save'] . '" class="button">',
            ),
        ),
    );
    createList($listOptions);

    $listOptions = array(
        'id' => 'custom_profile_fields',
        'title' => $txt['custom_profile_title'],
        'base_href' => $scripturl . '?action=admin;area=featuresettings;sa=profile',
        'default_sort_col' => 'field_order',
        'no_items_label' => $txt['custom_profile_none'],
        'items_per_page' => 25,
        'get_items' => array(
            'function' => 'list_getProfileFields',
            'params' => array(
                false,
            ),
        ),
        'get_count' => array(
            'function' => 'list_getProfileFieldSize',
        ),
        'columns' => array(
            'field_order' => array(
                'header' => array(
                    'value' => $txt['custom_profile_fieldorder'],
                ),
                'data' => array(
                    'function' => function($rowData) use ($context, $txt, $scripturl)
                    {
                        $return = '<p class="centertext bold_text">' . $rowData['field_order'] . '<br>';

                        if ($rowData['field_order'] > 1)
                            $return .= '<a href="' . $scripturl . '?action=admin;area=featuresettings;sa=profileedit;fid=' . $rowData['id_field'] . ';move=up"><span class="toggle_up" title="' . $txt['custom_edit_order_move'] . ' ' . $txt['custom_edit_order_up'] . '"></span></a>';

                        if ($rowData['field_order'] < $context['custFieldsMaxOrder'])
                            $return .= '<a href="' . $scripturl . '?action=admin;area=featuresettings;sa=profileedit;fid=' . $rowData['id_field'] . ';move=down"><span class="toggle_down" title="' . $txt['custom_edit_order_move'] . ' ' . $txt['custom_edit_order_down'] . '"></span></a>';

                        $return .= '</p>';

                        return $return;
                    },
                    'style' => 'width: 12%;',
                ),
                'sort' => array(
                    'default' => 'field_order',
                    'reverse' => 'field_order DESC',
                ),
            ),
            'field_name' => array(
                'header' => array(
                    'value' => $txt['custom_profile_fieldname'],
                ),
                'data' => array(
                    'function' => function($rowData) use ($scripturl)
                    {
                        return sprintf('<a href="%1$s?action=admin;area=featuresettings;sa=profileedit;fid=%2$d">%3$s</a><div class="smalltext">%4$s</div>', $scripturl, $rowData['id_field'], $rowData['field_name'], $rowData['field_desc']);
                    },
                    'style' => 'width: 62%;',
                ),
                'sort' => array(
                    'default' => 'field_name',
                    'reverse' => 'field_name DESC',
                ),
            ),
            'field_type' => array(
                'header' => array(
                    'value' => $txt['custom_profile_fieldtype'],
                ),
                'data' => array(
                    'function' => function($rowData) use ($txt)
                    {
                        $textKey = sprintf('custom_profile_type_%1$s', $rowData['field_type']);
                        return isset($txt[$textKey]) ? $txt[$textKey] : $textKey;
                    },
                    'style' => 'width: 15%;',
                ),
                'sort' => array(
                    'default' => 'field_type',
                    'reverse' => 'field_type DESC',
                ),
            ),
            'active' => array(
                'header' => array(
                    'value' => $txt['custom_profile_active'],
                ),
                'data' => array(
                    'function' => function($rowData) use ($txt)
                    {
                        return $rowData['active'] ? $txt['yes'] : $txt['no'];
                    },
                    'style' => 'width: 8%;',
                ),
                'sort' => array(
                    'default' => 'active DESC',
                    'reverse' => 'active',
                ),
            ),
            'placement' => array(
                'header' => array(
                    'value' => $txt['custom_profile_placement'],
                ),
                'data' => array(
                    'function' => function($rowData)
                    {
                        global $txt, $context;

                        return $txt['custom_profile_placement_' . (empty($rowData['placement']) ? 'standard' : $context['cust_profile_fields_placement'][$rowData['placement']])];
                    },
                    'style' => 'width: 8%;',
                ),
                'sort' => array(
                    'default' => 'placement DESC',
                    'reverse' => 'placement',
                ),
            ),
            'show_on_registration' => array(
                'data' => array(
                    'sprintf' => array(
                        'format' => '<a href="' . $scripturl . '?action=admin;area=featuresettings;sa=profileedit;fid=%1$s">' . $txt['modify'] . '</a>',
                        'params' => array(
                            'id_field' => false,
                        ),
                    ),
                    'style' => 'width: 15%;',
                ),
            ),
        ),
        'form' => array(
            'href' => $scripturl . '?action=admin;area=featuresettings;sa=profileedit',
            'name' => 'customProfileFields',
        ),
        'additional_rows' => array(
            array(
                'position' => 'below_table_data',
                'value' => '<input type="submit" name="new" value="' . $txt['custom_profile_make_new'] . '" class="button">',
            ),
        ),
    );
    createList($listOptions);

    // There are two different ways we could get to this point. To keep it simple, they both do
    // the same basic thing.
    if (isset($_SESSION['adm-save']))
    {
        $context['saved_successful'] = true;
        unset ($_SESSION['adm-save']);
    }
}

/**
 * Callback for createList().
 *
 * @param int $start The item to start with (used for pagination purposes)
 * @param int $items_per_page The number of items to display per page
 * @param string $sort A string indicating how to sort the results
 * @param bool $standardFields Whether or not to include standard fields as well
 * @return array An array of info about the various profile fields
 */
function list_getProfileFields($start, $items_per_page, $sort, $standardFields)
{
    global $txt, $modSettings, $smcFunc;

    $list = array();

    if ($standardFields)
    {
        $standard_fields = array('website', 'personal_text', 'timezone', 'posts', 'warning_status');
        $fields_no_registration = array('posts', 'warning_status');
        $disabled_fields = isset($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
        $registration_fields = isset($modSettings['registration_fields']) ? explode(',', $modSettings['registration_fields']) : array();

        foreach ($standard_fields as $field)
            $list[] = array(
                'id' => $field,
                'label' => isset($txt['standard_profile_field_' . $field]) ? $txt['standard_profile_field_' . $field] : (isset($txt[$field]) ? $txt[$field] : $field),
                'disabled' => in_array($field, $disabled_fields),
                'on_register' => in_array($field, $registration_fields) && !in_array($field, $fields_no_registration),
                'can_show_register' => !in_array($field, $fields_no_registration),
            );
    }
    else
    {
        // Load all the fields.
        $request = $smcFunc['db_query']('', '
            SELECT id_field, col_name, field_name, field_desc, field_type, field_order, active, placement
            FROM {db_prefix}custom_fields
            ORDER BY {raw:sort}
            LIMIT {int:start}, {int:items_per_page}',
            array(
                'sort' => $sort,
                'start' => $start,
                'items_per_page' => $items_per_page,
            )
        );
        while ($row = $smcFunc['db_fetch_assoc']($request))
            $list[] = $row;
        $smcFunc['db_free_result']($request);
    }

    return $list;
}

/**
 * Callback for createList().
 *
 * @return int The total number of custom profile fields
 */
function list_getProfileFieldSize()
{
    global $smcFunc;

    $request = $smcFunc['db_query']('', '
        SELECT COUNT(*)
        FROM {db_prefix}custom_fields',
        array(
        )
    );

    list ($numProfileFields) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    return $numProfileFields;
}

/**
 * Edit some profile fields?
 */
function EditCustomProfiles()
{
    global $txt, $scripturl, $context, $smcFunc;

    // Sort out the context!
    $context['fid'] = isset($_GET['fid']) ? (int) $_GET['fid'] : 0;
    $context[$context['admin_menu_name']]['current_subsection'] = 'profile';
    $context['page_title'] = $context['fid'] ? $txt['custom_edit_title'] : $txt['custom_add_title'];
    $context['sub_template'] = 'edit_profile_field';

    // Load the profile language for section names.
    loadLanguage('Profile');

    // There's really only a few places we can go...
    $move_to = array('up', 'down');

    // We need this for both moving and saving so put it right here.
    $order_count = custFieldsMaxOrder();

    if ($context['fid'])
    {
        $request = $smcFunc['db_query']('', '
            SELECT
                id_field, col_name, field_name, field_desc, field_type, field_order, field_length, field_options,
                show_reg, show_display, show_mlist, show_profile, private, active, default_value, can_search,
                bbc, mask, enclose, placement
            FROM {db_prefix}custom_fields
            WHERE id_field = {int:current_field}',
            array(
                'current_field' => $context['fid'],
            )
        );
        $context['field'] = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            if ($row['field_type'] == 'textarea')
                @list ($rows, $cols) = @explode(',', $row['default_value']);
            else
            {
                $rows = 3;
                $cols = 30;
            }

            $context['field'] = array(
                'name' => $row['field_name'],
                'desc' => $row['field_desc'],
                'col_name' => $row['col_name'],
                'profile_area' => $row['show_profile'],
                'reg' => $row['show_reg'],
                'display' => $row['show_display'],
                'mlist' => $row['show_mlist'],
                'type' => $row['field_type'],
                'order' => $row['field_order'],
                'max_length' => $row['field_length'],
                'rows' => $rows,
                'cols' => $cols,
                'bbc' => $row['bbc'] ? true : false,
                'default_check' => $row['field_type'] == 'check' && $row['default_value'] ? true : false,
                'default_select' => $row['field_type'] == 'select' || $row['field_type'] == 'radio' ? $row['default_value'] : '',
                'options' => strlen($row['field_options']) > 1 ? explode(',', $row['field_options']) : array('', '', ''),
                'active' => $row['active'],
                'private' => $row['private'],
                'can_search' => $row['can_search'],
                'mask' => $row['mask'],
                'regex' => substr($row['mask'], 0, 5) == 'regex' ? substr($row['mask'], 5) : '',
                'enclose' => $row['enclose'],
                'placement' => $row['placement'],
            );
        }
        $smcFunc['db_free_result']($request);
    }

    // Setup the default values as needed.
    if (empty($context['field']))
        $context['field'] = array(
            'name' => '',
            'col_name' => '???',
            'desc' => '',
            'profile_area' => 'forumprofile',
            'reg' => false,
            'display' => false,
            'mlist' => false,
            'type' => 'text',
            'order' => 0,
            'max_length' => 255,
            'rows' => 4,
            'cols' => 30,
            'bbc' => false,
            'default_check' => false,
            'default_select' => '',
            'options' => array('', '', ''),
            'active' => true,
            'private' => false,
            'can_search' => false,
            'mask' => 'nohtml',
            'regex' => '',
            'enclose' => '',
            'placement' => 0,
        );

    // Are we moving it?
    if (isset($_GET['move']) && in_array($smcFunc['htmlspecialchars']($_GET['move']), $move_to))
    {
        // Down is the new up.
        $new_order = ($_GET['move'] == 'up' ? ($context['field']['order'] - 1) : ($context['field']['order'] + 1));

        // Is this a valid position?
        if ($new_order <= 0 || $new_order > $order_count)
            redirectexit('action=admin;area=featuresettings;sa=profile'); // @todo implement an error handler

        // All good, proceed.
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}custom_fields
            SET field_order = {int:old_order}
            WHERE field_order = {int:new_order}',
            array(
                'new_order' => $new_order,
                'old_order' => $context['field']['order'],
            )
        );
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}custom_fields
            SET field_order = {int:new_order}
            WHERE id_field = {int:id_field}',
            array(
                'new_order' => $new_order,
                'id_field' => $context['fid'],
            )
        );
        redirectexit('action=admin;area=featuresettings;sa=profile'); // @todo perhaps a nice confirmation message, dunno.
    }

    // Are we saving?
    if (isset($_POST['save']))
    {
        checkSession();
        validateToken('admin-ecp');

        // Everyone needs a name - even the (bracket) unknown...
        if (trim($_POST['field_name']) == '')
            redirectexit($scripturl . '?action=admin;area=featuresettings;sa=profileedit;fid=' . $_GET['fid'] . ';msg=need_name');

        // Regex you say?  Do a very basic test to see if the pattern is valid
        if (!empty($_POST['regex']) && @preg_match($_POST['regex'], 'dummy') === false)
            redirectexit($scripturl . '?action=admin;area=featuresettings;sa=profileedit;fid=' . $_GET['fid'] . ';msg=regex_error');

        $_POST['field_name'] = $smcFunc['htmlspecialchars']($_POST['field_name']);
        $_POST['field_desc'] = $smcFunc['htmlspecialchars']($_POST['field_desc']);

        // Checkboxes...
        $show_reg = isset($_POST['reg']) ? (int) $_POST['reg'] : 0;
        $show_display = isset($_POST['display']) ? 1 : 0;
        $show_mlist = isset($_POST['mlist']) ? 1 : 0;
        $bbc = isset($_POST['bbc']) ? 1 : 0;
        $show_profile = $_POST['profile_area'];
        $active = isset($_POST['active']) ? 1 : 0;
        $private = isset($_POST['private']) ? (int) $_POST['private'] : 0;
        $can_search = isset($_POST['can_search']) ? 1 : 0;

        // Some masking stuff...
        $mask = isset($_POST['mask']) ? $_POST['mask'] : '';
        if ($mask == 'regex' && isset($_POST['regex']))
            $mask .= $_POST['regex'];

        $field_length = isset($_POST['max_length']) ? (int) $_POST['max_length'] : 255;
        $enclose = isset($_POST['enclose']) ? $_POST['enclose'] : '';
        $placement = isset($_POST['placement']) ? (int) $_POST['placement'] : 0;

        // Select options?
        $field_options = '';
        $newOptions = array();
        $default = isset($_POST['default_check']) && $_POST['field_type'] == 'check' ? 1 : '';
        if (!empty($_POST['select_option']) && ($_POST['field_type'] == 'select' || $_POST['field_type'] == 'radio'))
        {
            foreach ($_POST['select_option'] as $k => $v)
            {
                // Clean, clean, clean...
                $v = $smcFunc['htmlspecialchars']($v);
                $v = strtr($v, array(',' => ''));

                // Nada, zip, etc...
                if (trim($v) == '')
                    continue;

                // Otherwise, save it boy.
                $field_options .= $v . ',';
                // This is just for working out what happened with old options...
                $newOptions[$k] = $v;

                // Is it default?
                if (isset($_POST['default_select']) && $_POST['default_select'] == $k)
                    $default = $v;
            }
            $field_options = substr($field_options, 0, -1);
        }

        // Text area has default has dimensions
        if ($_POST['field_type'] == 'textarea')
            $default = (int) $_POST['rows'] . ',' . (int) $_POST['cols'];

        // Come up with the unique name?
        if (empty($context['fid']))
        {
            $col_name = $smcFunc['substr'](strtr($_POST['field_name'], array(' ' => '')), 0, 6);
            preg_match('~([\w\d_-]+)~', $col_name, $matches);

            // If there is nothing to the name, then let's start out own - for foreign languages etc.
            if (isset($matches[1]))
                $col_name = $initial_col_name = 'cust_' . strtolower($matches[1]);
            else
                $col_name = $initial_col_name = 'cust_' . mt_rand(1, 9999);

            // Make sure this is unique.
            $current_fields = array();
            $request = $smcFunc['db_query']('', '
                SELECT id_field, col_name
                FROM {db_prefix}custom_fields'
            );
            while ($row = $smcFunc['db_fetch_assoc']($request))
                $current_fields[$row['id_field']] = $row['col_name'];

            $smcFunc['db_free_result']($request);

            $unique = false;
            for ($i = 0; !$unique && $i < 9; $i++)
            {
                if (!in_array($col_name, $current_fields))
                    $unique = true;
                else
                    $col_name = $initial_col_name . $i;
            }

            // Still not a unique column name? Leave it up to the user, then.
            if (!$unique)
                fatal_lang_error('custom_option_not_unique');
        }
        // Work out what to do with the user data otherwise...
        else
        {
            // Anything going to check or select is pointless keeping - as is anything coming from check!
            if (($_POST['field_type'] == 'check' && $context['field']['type'] != 'check')
                || (($_POST['field_type'] == 'select' || $_POST['field_type'] == 'radio') && $context['field']['type'] != 'select' && $context['field']['type'] != 'radio')
                || ($context['field']['type'] == 'check' && $_POST['field_type'] != 'check'))
            {
                $smcFunc['db_query']('', '
                    DELETE FROM {db_prefix}themes
                    WHERE variable = {string:current_column}
                        AND id_member > {int:no_member}',
                    array(
                        'no_member' => 0,
                        'current_column' => $context['field']['col_name'],
                    )
                );
            }
            // Otherwise - if the select is edited may need to adjust!
            elseif ($_POST['field_type'] == 'select' || $_POST['field_type'] == 'radio')
            {
                $optionChanges = array();
                $takenKeys = array();
                // Work out what's changed!
                foreach ($context['field']['options'] as $k => $option)
                {
                    if (trim($option) == '')
                        continue;

                    // Still exists?
                    if (in_array($option, $newOptions))
                    {
                        $takenKeys[] = $k;
                        continue;
                    }
                }

                // Finally - have we renamed it - or is it really gone?
                foreach ($optionChanges as $k => $option)
                {
                    // Just been renamed?
                    if (!in_array($k, $takenKeys) && !empty($newOptions[$k]))
                        $smcFunc['db_query']('', '
                            UPDATE {db_prefix}themes
                            SET value = {string:new_value}
                            WHERE variable = {string:current_column}
                                AND value = {string:old_value}
                                AND id_member > {int:no_member}',
                            array(
                                'no_member' => 0,
                                'new_value' => $newOptions[$k],
                                'current_column' => $context['field']['col_name'],
                                'old_value' => $option,
                            )
                        );
                }
            }
            // @todo Maybe we should adjust based on new text length limits?
        }

        // Do the insertion/updates.
        if ($context['fid'])
        {
            $smcFunc['db_query']('', '
                UPDATE {db_prefix}custom_fields
                SET
                    field_name = {string:field_name}, field_desc = {string:field_desc},
                    field_type = {string:field_type}, field_length = {int:field_length},
                    field_options = {string:field_options}, show_reg = {int:show_reg},
                    show_display = {int:show_display}, show_mlist = {int:show_mlist}, show_profile = {string:show_profile},
                    private = {int:private}, active = {int:active}, default_value = {string:default_value},
                    can_search = {int:can_search}, bbc = {int:bbc}, mask = {string:mask},
                    enclose = {string:enclose}, placement = {int:placement}
                WHERE id_field = {int:current_field}',
                array(
                    'field_length' => $field_length,
                    'show_reg' => $show_reg,
                    'show_display' => $show_display,
                    'show_mlist' => $show_mlist,
                    'private' => $private,
                    'active' => $active,
                    'can_search' => $can_search,
                    'bbc' => $bbc,
                    'current_field' => $context['fid'],
                    'field_name' => $_POST['field_name'],
                    'field_desc' => $_POST['field_desc'],
                    'field_type' => $_POST['field_type'],
                    'field_options' => $field_options,
                    'show_profile' => $show_profile,
                    'default_value' => $default,
                    'mask' => $mask,
                    'enclose' => $enclose,
                    'placement' => $placement,
                )
            );

            // Just clean up any old selects - these are a pain!
            if (($_POST['field_type'] == 'select' || $_POST['field_type'] == 'radio') && !empty($newOptions))
                $smcFunc['db_query']('', '
                    DELETE FROM {db_prefix}themes
                    WHERE variable = {string:current_column}
                        AND value NOT IN ({array_string:new_option_values})
                        AND id_member > {int:no_member}',
                    array(
                        'no_member' => 0,
                        'new_option_values' => $newOptions,
                        'current_column' => $context['field']['col_name'],
                    )
                );
        }
        else
        {
            // Gotta figure it out the order.
            $new_order = $order_count > 1 ? ($order_count + 1) : 1;

            $smcFunc['db_insert']('',
                '{db_prefix}custom_fields',
                array(
                    'col_name' => 'string', 'field_name' => 'string', 'field_desc' => 'string',
                    'field_type' => 'string', 'field_length' => 'string', 'field_options' => 'string', 'field_order' => 'int',
                    'show_reg' => 'int', 'show_display' => 'int', 'show_mlist' => 'int', 'show_profile' => 'string',
                    'private' => 'int', 'active' => 'int', 'default_value' => 'string', 'can_search' => 'int',
                    'bbc' => 'int', 'mask' => 'string', 'enclose' => 'string', 'placement' => 'int',
                ),
                array(
                    $col_name, $_POST['field_name'], $_POST['field_desc'],
                    $_POST['field_type'], $field_length, $field_options, $new_order,
                    $show_reg, $show_display, $show_mlist, $show_profile,
                    $private, $active, $default, $can_search,
                    $bbc, $mask, $enclose, $placement,
                ),
                array('id_field')
            );
        }
    }
    // Deleting?
    elseif (isset($_POST['delete']) && $context['field']['col_name'])
    {
        checkSession();
        validateToken('admin-ecp');

        // Delete the user data first.
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}themes
            WHERE variable = {string:current_column}
                AND id_member > {int:no_member}',
            array(
                'no_member' => 0,
                'current_column' => $context['field']['col_name'],
            )
        );
        // Finally - the field itself is gone!
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}custom_fields
            WHERE id_field = {int:current_field}',
            array(
                'current_field' => $context['fid'],
            )
        );

        // Re-arrange the order.
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}custom_fields
            SET field_order = field_order - 1
            WHERE field_order > {int:current_order}',
            array(
                'current_order' => $context['field']['order'],
            )
        );
    }

    // Rebuild display cache etc.
    if (isset($_POST['delete']) || isset($_POST['save']))
    {
        checkSession();

        $request = $smcFunc['db_query']('', '
            SELECT col_name, field_name, field_type, field_order, bbc, enclose, placement, show_mlist, field_options
            FROM {db_prefix}custom_fields
            WHERE show_display = {int:is_displayed}
                AND active = {int:active}
                AND private != {int:not_owner_only}
                AND private != {int:not_admin_only}
            ORDER BY field_order',
            array(
                'is_displayed' => 1,
                'active' => 1,
                'not_owner_only' => 2,
                'not_admin_only' => 3,
            )
        );

        $fields = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $fields[] = array(
                'col_name' => strtr($row['col_name'], array('|' => '', ';' => '')),
                'title' => strtr($row['field_name'], array('|' => '', ';' => '')),
                'type' => $row['field_type'],
                'order' => $row['field_order'],
                'bbc' => $row['bbc'] ? '1' : '0',
                'placement' => !empty($row['placement']) ? $row['placement'] : '0',
                'enclose' => !empty($row['enclose']) ? $row['enclose'] : '',
                'mlist' => $row['show_mlist'],
                'options' => (!empty($row['field_options']) ? explode(',', $row['field_options']) : array()),
            );
        }
        $smcFunc['db_free_result']($request);

        updateSettings(array('displayFields' => $smcFunc['json_encode']($fields)));
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=featuresettings;sa=profile');
    }

    createToken('admin-ecp');
}

/**
 * Returns the maximum field_order value for the custom fields
 *
 * @return int The maximum value of field_order from the custom_fields table
 */
function custFieldsMaxOrder()
{
    global $smcFunc;

    // Gotta know the order limit
    $result = $smcFunc['db_query']('', '
        SELECT MAX(field_order)
        FROM {db_prefix}custom_fields',
        array()
    );

    list ($order_count) = $smcFunc['db_fetch_row']($result);
    $smcFunc['db_free_result']($result);

    return (int) $order_count;
}

/**
 * Allow to edit the settings on the pruning screen.
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyLogSettings($return_config = false)
{
    global $txt, $scripturl, $sourcedir, $context, $modSettings;

    // Make sure we understand what's going on.
    loadLanguage('ManageSettings');

    $context['page_title'] = $txt['log_settings'];

    $config_vars = array(
        array('check', 'modlog_enabled', 'help' => 'modlog'),
        array('check', 'adminlog_enabled', 'help' => 'adminlog'),
        array('check', 'userlog_enabled', 'help' => 'userlog'),
        // The error log is a wonderful thing.
        array('title', 'errlog'),
        array('desc', 'error_log_desc'),
        array('check', 'enableErrorLogging'),
        array('check', 'enableErrorQueryLogging'),
        array('check', 'log_ban_hits'),
        // Even do the pruning?
        array('title', 'pruning_title'),
        array('desc', 'pruning_desc'),
        // The array indexes are there so we can remove/change them before saving.
        'pruningOptions' => array('check', 'pruningOptions'),
        '',

        // Various logs that could be pruned.
        array('int', 'pruneErrorLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']), // Error log.
        array('int', 'pruneModLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']), // Moderation log.
        array('int', 'pruneBanLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']), // Ban hit log.
        array('int', 'pruneReportLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']), // Report to moderator log.
        array('int', 'pruneScheduledTaskLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']), // Log of the scheduled tasks and how long they ran.
        array('int', 'pruneSpiderHitLog', 'postinput' => $txt['days_word'], 'subtext' => $txt['zero_to_disable']), // Log of the scheduled tasks and how long they ran.
        // If you add any additional logs make sure to add them after this point.  Additionally, make sure you add them to the weekly scheduled task.
        // Mod Developers: Do NOT use the pruningOptions master variable for this as SMF Core may overwrite your setting in the future!
    );

    // We want to be toggling some of these for a nice user experience. If you want to add yours to the list of those magically hidden when the 'pruning' option is off, add to this.
    $prune_toggle = array('pruneErrorLog', 'pruneModLog', 'pruneBanLog', 'pruneReportLog', 'pruneScheduledTaskLog', 'pruneSpiderHitLog');

    call_integration_hook('integrate_prune_settings', array(&$config_vars, &$prune_toggle, false));

    $prune_toggle_dt = array();
    foreach ($prune_toggle as $item)
        $prune_toggle_dt[] = 'setting_' . $item;

    if ($return_config)
        return $config_vars;

    addInlineJavaScript('
    function togglePruned()
    {
        var newval = $("#pruningOptions").prop("checked");
        $("#' . implode(', #', $prune_toggle) . '").closest("dd").toggle(newval);
        $("#' . implode(', #', $prune_toggle_dt) . '").closest("dt").toggle(newval);
    };
    togglePruned();
    $("#pruningOptions").click(function() { togglePruned(); });', true);

    // We'll need this in a bit.
    require_once($sourcedir . '/ManageServer.php');

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        // Because of the excitement attached to combining pruning log items, we need to duplicate everything here.
        $savevar = array(
            array('check', 'modlog_enabled'),
            array('check', 'adminlog_enabled'),
            array('check', 'userlog_enabled'),
            array('check', 'enableErrorLogging'),
            array('check', 'enableErrorQueryLogging'),
            array('check', 'log_ban_hits'),
            array('text', 'pruningOptions')
        );

        call_integration_hook('integrate_prune_settings', array(&$savevar, &$prune_toggle, true));

        if (!empty($_POST['pruningOptions']))
        {
            $vals = array();
            foreach ($config_vars as $index => $dummy)
            {
                if (!is_array($dummy) || $index == 'pruningOptions' || !in_array($dummy[1], $prune_toggle))
                    continue;

                $vals[] = empty($_POST[$dummy[1]]) || $_POST[$dummy[1]] < 0 ? 0 : (int) $_POST[$dummy[1]];
            }
            $_POST['pruningOptions'] = implode(',', $vals);
        }
        else
            $_POST['pruningOptions'] = '';

        saveDBSettings($savevar);
        $_SESSION['adm-save'] = true;
        redirectexit('action=admin;area=logs;sa=settings');
    }

    $context['post_url'] = $scripturl . '?action=admin;area=logs;save;sa=settings';
    $context['settings_title'] = $txt['log_settings'];
    $context['sub_template'] = 'show_settings';

    // Get the actual values
    if (!empty($modSettings['pruningOptions']))
        @list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']);
    else
        $modSettings['pruneErrorLog'] = $modSettings['pruneModLog'] = $modSettings['pruneBanLog'] = $modSettings['pruneReportLog'] = $modSettings['pruneScheduledTaskLog'] = $modSettings['pruneSpiderHitLog'] = 0;

    prepareDBSettingContext($config_vars);
}

/**
 * If you have a general mod setting to add stick it here.
 *
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
 */
function ModifyGeneralModSettings($return_config = false)
{
    global $txt, $scripturl, $context;

    $config_vars = array(
        // Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
    );

    // Make it even easier to add new settings.
    call_integration_hook('integrate_general_mod_settings', array(&$config_vars));

    if ($return_config)
        return $config_vars;

    $context['post_url'] = $scripturl . '?action=admin;area=modsettings;save;sa=general';
    $context['settings_title'] = $txt['mods_cat_modifications_misc'];

    // No removing this line you, dirty unwashed mod authors. :p
    if (empty($config_vars))
    {
        $context['settings_save_dont_show'] = true;
        $context['settings_message'] = '<div class="centertext">' . $txt['modification_no_misc_settings'] . '</div>';

        return prepareDBSettingContext($config_vars);
    }

    // Saving?
    if (isset($_GET['save']))
    {
        checkSession();

        $save_vars = $config_vars;

        call_integration_hook('integrate_save_general_mod_settings', array(&$save_vars));

        // This line is to help mod authors do a search/add after if you want to add something here. Keyword: FOOT TAPPING SUCKS!
        saveDBSettings($save_vars);

        // This line is to remind mod authors that it's nice to let the users know when something has been saved.
        $_SESSION['adm-save'] = true;

        // This line is to help mod authors do a search/add after if you want to add something here. Keyword: I LOVE TEA!
        redirectexit('action=admin;area=modsettings;sa=general');
    }

    // This line is to help mod authors do a search/add after if you want to add something here. Keyword: RED INK IS FOR TEACHERS AND THOSE WHO LIKE PAIN!
    prepareDBSettingContext($config_vars);
}

/**
 * Handles modifying the alerts settings
 */
function ModifyAlertsSettings()
{
    global $context, $modSettings, $sourcedir, $txt;

    // Dummy settings for the template...
    $modSettings['allow_disableAnnounce'] = false;
    $context['user']['is_owner'] = false;
    $context['member'] = array();
    $context['id_member'] = 0;
    $context['menu_item_selected'] = 'alerts';
    $context['token_check'] = 'noti-admin';

    // Specify our action since we'll want to post back here instead of the profile
    $context['action'] = 'action=admin;area=featuresettings;sa=alerts;' . $context['session_var'] . '=' . $context['session_id'];

    loadTemplate('Profile');
    loadLanguage('Profile');

    include_once($sourcedir . '/Profile-Modify.php');
    alert_configuration(0);

    $context['page_title'] = $txt['notify_settings'];

    // Override the description
    $context['description'] = $txt['notifications_desc'];
    $context['sub_template'] = 'alert_configuration';
}

?>