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

/**
 * This file contains all the administration functions for subscriptions.
 * (and some more than that :P)
 *
 * 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...');

/**
 * The main entrance point for the 'Paid Subscription' screen, calling
 * the right function based on the given sub-action.
 * It defaults to sub-action 'view'.
 * Accessed from ?action=admin;area=paidsubscribe.
 * It requires admin_forum permission for admin based actions.
 */
function ManagePaidSubscriptions()
{
    global $context, $txt, $modSettings;

    // Load the required language and template.
    loadLanguage('ManagePaid');
    loadTemplate('ManagePaid');

    if (!empty($modSettings['paid_enabled']))
        $subActions = array(
            'modify' => array('ModifySubscription', 'admin_forum'),
            'modifyuser' => array('ModifyUserSubscription', 'admin_forum'),
            'settings' => array('ModifySubscriptionSettings', 'admin_forum'),
            'view' => array('ViewSubscriptions', 'admin_forum'),
            'viewsub' => array('ViewSubscribedUsers', 'admin_forum'),
        );
    else
        $subActions = array(
            'settings' => array('ModifySubscriptionSettings', 'admin_forum'),
        );

    // Default the sub-action to 'view subscriptions', but only if they have already set things up..
    $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (!empty($modSettings['paid_currency_symbol']) && !empty($modSettings['paid_enabled']) ? 'view' : 'settings');

    // Make sure you can do this.
    isAllowedTo($subActions[$_REQUEST['sa']][1]);

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

    // Tabs for browsing the different subscription functions.
    $context[$context['admin_menu_name']]['tab_data'] = array(
        'title' => $txt['paid_subscriptions'],
        'help' => '',
        'description' => $txt['paid_subscriptions_desc'],
    );
    if (!empty($modSettings['paid_enabled']))
        $context[$context['admin_menu_name']]['tab_data']['tabs'] = array(
            'view' => array(
                'description' => $txt['paid_subs_view_desc'],
            ),
            'settings' => array(
                'description' => $txt['paid_subs_settings_desc'],
            ),
        );

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

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

/**
 * Set any setting related to paid subscriptions, i.e.
 * modify which payment methods are to be used.
 * It requires the moderate_forum permission
 * Accessed from ?action=admin;area=paidsubscribe;sa=settings.
 *
 * @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 ModifySubscriptionSettings($return_config = false)
{
    global $context, $txt, $modSettings, $sourcedir, $smcFunc, $scripturl;

    if (!empty($modSettings['paid_enabled']))
    {
        // If the currency is set to something different then we need to set it to other for this to work and set it back shortly.
        $modSettings['paid_currency'] = !empty($modSettings['paid_currency_code']) ? $modSettings['paid_currency_code'] : '';
        if (!empty($modSettings['paid_currency_code']) && !in_array($modSettings['paid_currency_code'], array('usd', 'eur', 'gbp', 'cad', 'aud')))
            $modSettings['paid_currency'] = 'other';

        // These are all the default settings.
        $config_vars = array(
            array(
                'check',
                'paid_enabled'
            ),
            '',

            array(
                'select',
                'paid_email',
                array(
                    0 => $txt['paid_email_no'],
                    1 => $txt['paid_email_error'],
                    2 => $txt['paid_email_all']
                ),
                'subtext' => $txt['paid_email_desc']
            ),
            array(
                'email',
                'paid_email_to',
                'subtext' => $txt['paid_email_to_desc'],
                'size' => 60
            ),
            '',

            'dummy_currency' => array(
                'select',
                'paid_currency',
                array(
                    'usd' => $txt['usd'],
                    'eur' => $txt['eur'],
                    'gbp' => $txt['gbp'],
                    'cad' => $txt['cad'],
                    'aud' => $txt['aud'],
                    'other' => $txt['other']
                ),
                'javascript' => 'onchange="toggleOther();"'
            ),
            array(
                'text',
                'paid_currency_code',
                'subtext' => $txt['paid_currency_code_desc'],
                'size' => 5,
                'force_div_id' => 'custom_currency_code_div'
            ),
            array(
                'text',
                'paid_currency_symbol',
                'subtext' => $txt['paid_currency_symbol_desc'],
                'size' => 8,
                'force_div_id' => 'custom_currency_symbol_div'
            ),
            array(
                'check',
                'paidsubs_test',
                'subtext' => $txt['paidsubs_test_desc'],
                'onclick' => 'return document.getElementById(\'paidsubs_test\').checked ? confirm(\'' . $txt['paidsubs_test_confirm'] . '\') : true;'
            ),
        );

        // Now load all the other gateway settings.
        $gateways = loadPaymentGateways();
        foreach ($gateways as $gateway)
        {
            $gatewayClass = new $gateway['display_class']();
            $setting_data = $gatewayClass->getGatewaySettings();
            if (!empty($setting_data))
            {
                $config_vars[] = array('title', $gatewayClass->title, 'text_label' => (isset($txt['paidsubs_gateway_title_' . $gatewayClass->title]) ? $txt['paidsubs_gateway_title_' . $gatewayClass->title] : $gatewayClass->title));
                $config_vars = array_merge($config_vars, $setting_data);
            }
        }

        $context['settings_message'] = $txt['paid_note'];
        $context[$context['admin_menu_name']]['current_subsection'] = 'settings';
        $context['settings_title'] = $txt['settings'];

        // We want javascript for our currency options.
        addInlineJavaScript('
        function toggleOther()
        {
            var otherOn = document.getElementById("paid_currency").value == \'other\';
            var currencydd = document.getElementById("custom_currency_code_div_dd");

            if (otherOn)
            {
                document.getElementById("custom_currency_code_div").style.display = "";
                document.getElementById("custom_currency_symbol_div").style.display = "";

                if (currencydd)
                {
                    document.getElementById("custom_currency_code_div_dd").style.display = "";
                    document.getElementById("custom_currency_symbol_div_dd").style.display = "";
                }
            }
            else
            {
                document.getElementById("custom_currency_code_div").style.display = "none";
                document.getElementById("custom_currency_symbol_div").style.display = "none";

                if (currencydd)
                {
                    document.getElementById("custom_currency_symbol_div_dd").style.display = "none";
                    document.getElementById("custom_currency_code_div_dd").style.display = "none";
                }
            }
        }
        toggleOther();', true);
    }
    else
    {
        $config_vars = array(
            array('check', 'paid_enabled'),
        );
        $context['settings_title'] = $txt['paid_subscriptions'];
    }

    // Just searching?
    if ($return_config)
        return $config_vars;

    // Get the settings template fired up.
    require_once($sourcedir . '/ManageServer.php');

    // Some important context stuff
    $context['page_title'] = $txt['settings'];
    $context['sub_template'] = 'show_settings';

    // Get the final touches in place.
    $context['post_url'] = $scripturl . '?action=admin;area=paidsubscribe;save;sa=settings';

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

        $old = !empty($modSettings['paid_enabled']);
        $new = !empty($_POST['paid_enabled']);
        if ($old != $new)
        {
            // So we're changing this fundamental status. Great.
            $smcFunc['db_query']('', '
                UPDATE {db_prefix}scheduled_tasks
                SET disabled = {int:disabled}
                WHERE task = {string:task}',
                array(
                    'disabled' => $new ? 0 : 1,
                    'task' => 'paid_subscriptions',
                )
            );

            // This may well affect the next trigger, whether we're enabling or not.
            require_once($sourcedir . '/ScheduledTasks.php');
            CalculateNextTrigger('paid_subscriptions');
        }

        // Check the email addresses were actually email addresses.
        if (!empty($_POST['paid_email_to']))
        {
            $email_addresses = array();
            foreach (explode(',', $_POST['paid_email_to']) as $email)
            {
                $email = trim($email);
                if (!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL))
                    $email_addresses[] = $email;
                $_POST['paid_email_to'] = implode(',', $email_addresses);
            }
        }

        // Can only handle this stuff if it's already enabled...
        if (!empty($modSettings['paid_enabled']))
        {
            // Sort out the currency stuff.
            if ($_POST['paid_currency'] != 'other')
            {
                $_POST['paid_currency_code'] = $_POST['paid_currency'];
                $_POST['paid_currency_symbol'] = $txt[$_POST['paid_currency'] . '_symbol'];
            }
            unset($config_vars['dummy_currency']);
        }

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

        redirectexit('action=admin;area=paidsubscribe;sa=settings');
    }

    // Prepare the settings...
    prepareDBSettingContext($config_vars);
}

/**
 * View a list of all the current subscriptions
 * Requires the admin_forum permission.
 * Accessed from ?action=admin;area=paidsubscribe;sa=view.
 */
function ViewSubscriptions()
{
    global $context, $txt, $modSettings, $sourcedir, $scripturl;

    // Not made the settings yet?
    if (empty($modSettings['paid_currency_symbol']))
        fatal_lang_error('paid_not_set_currency', false, $scripturl . '?action=admin;area=paidsubscribe;sa=settings');

    // Some basic stuff.
    $context['page_title'] = $txt['paid_subs_view'];
    loadSubscriptions();

    $listOptions = array(
        'id' => 'subscription_list',
        'title' => $txt['subscriptions'],
        'items_per_page' => $modSettings['defaultMaxListItems'],
        'base_href' => $scripturl . '?action=admin;area=paidsubscribe;sa=view',
        'get_items' => array(
            'function' => function($start, $items_per_page) use ($context)
            {
                $subscriptions = array();
                $counter = 0;
                $start++;

                foreach ($context['subscriptions'] as $data)
                {
                    if (++$counter < $start)
                        continue;
                    elseif ($counter == $start + $items_per_page)
                        break;

                    $subscriptions[] = $data;
                }
                return $subscriptions;
            },
        ),
        'get_count' => array(
            'function' => function() use ($context)
            {
                return count($context['subscriptions']);
            },
        ),
        'no_items_label' => $txt['paid_none_yet'],
        'columns' => array(
            'name' => array(
                'header' => array(
                    'value' => $txt['paid_name'],
                    'style' => 'width: 35%;',
                ),
                'data' => array(
                    'function' => function($rowData) use ($scripturl)
                    {
                        return sprintf('<a href="%1$s?action=admin;area=paidsubscribe;sa=viewsub;sid=%2$s">%3$s</a>', $scripturl, $rowData['id'], $rowData['name']);
                    },
                ),
            ),
            'cost' => array(
                'header' => array(
                    'value' => $txt['paid_cost'],
                ),
                'data' => array(
                    'function' => function($rowData) use ($txt)
                    {
                        return $rowData['flexible'] ? '<em>' . $txt['flexible'] . '</em>' : $rowData['cost'] . ' / ' . $rowData['length'];
                    },
                ),
            ),
            'pending' => array(
                'header' => array(
                    'value' => $txt['paid_pending'],
                    'style' => 'width: 18%;',
                    'class' => 'centercol',
                ),
                'data' => array(
                    'db_htmlsafe' => 'pending',
                    'class' => 'centercol',
                ),
            ),
            'finished' => array(
                'header' => array(
                    'value' => $txt['paid_finished'],
                    'class' => 'centercol',
                ),
                'data' => array(
                    'db_htmlsafe' => 'finished',
                    'class' => 'centercol',
                ),
            ),
            'total' => array(
                'header' => array(
                    'value' => $txt['paid_active'],
                    'class' => 'centercol',
                ),
                'data' => array(
                    'db_htmlsafe' => 'total',
                    'class' => 'centercol',
                ),
            ),
            'is_active' => array(
                'header' => array(
                    'value' => $txt['paid_is_active'],
                    'class' => 'centercol',
                ),
                'data' => array(
                    'function' => function($rowData) use ($txt)
                    {
                        return '<span style="color: ' . ($rowData['active'] ? 'green' : 'red') . '">' . ($rowData['active'] ? $txt['yes'] : $txt['no']) . '</span>';
                    },
                    'class' => 'centercol',
                ),
            ),
            'modify' => array(
                'data' => array(
                    'function' => function($rowData) use ($txt, $scripturl)
                    {
                        return '<a href="' . $scripturl . '?action=admin;area=paidsubscribe;sa=modify;sid=' . $rowData['id'] . '">' . $txt['modify'] . '</a>';
                    },
                    'class' => 'centercol',
                ),
            ),
            'delete' => array(
                'data' => array(
                    'function' => function($rowData) use ($scripturl, $txt)
                    {
                        return '<a href="' . $scripturl . '?action=admin;area=paidsubscribe;sa=modify;delete;sid=' . $rowData['id'] . '">' . $txt['delete'] . '</a>';
                    },
                    'class' => 'centercol',
                ),
            ),
        ),
        'form' => array(
            'href' => $scripturl . '?action=admin;area=paidsubscribe;sa=modify',
        ),
        'additional_rows' => array(
            array(
                'position' => 'above_table_headers',
                'value' => '<input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="button">',
            ),
            array(
                'position' => 'below_table_data',
                'value' => '<input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="button">',
            ),
        ),
    );

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

    $context['sub_template'] = 'show_list';
    $context['default_list'] = 'subscription_list';
}

/**
 * Adding, editing and deleting subscriptions.
 * Accessed from ?action=admin;area=paidsubscribe;sa=modify.
 */
function ModifySubscription()
{
    global $context, $txt, $smcFunc;

    $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
    $context['action_type'] = $context['sub_id'] ? (isset($_REQUEST['delete']) ? 'delete' : 'edit') : 'add';

    // Setup the template.
    $context['sub_template'] = $context['action_type'] == 'delete' ? 'delete_subscription' : 'modify_subscription';
    $context['page_title'] = $txt['paid_' . $context['action_type'] . '_subscription'];

    // Delete it?
    if (isset($_POST['delete_confirm']) && isset($_REQUEST['delete']))
    {
        checkSession();
        validateToken('admin-pmsd');

        // Before we delete the subscription we need to find out if anyone currently has said subscription.
        $request = $smcFunc['db_query']('', '
            SELECT ls.id_member, ls.old_id_group, mem.id_group, mem.additional_groups
            FROM {db_prefix}log_subscribed AS ls
                INNER JOIN {db_prefix}members AS mem ON (ls.id_member = mem.id_member)
            WHERE id_subscribe = {int:current_subscription}
                AND status = {int:is_active}',
            array(
                'current_subscription' => $context['sub_id'],
                'is_active' => 1,
            )
        );
        $members = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            $id_member = array_shift($row);
            $members[$id_member] = $row;
        }
        $smcFunc['db_free_result']($request);

        // If there are any members with this subscription, we have to do some more work before we go any further.
        if (!empty($members))
        {
            $request = $smcFunc['db_query']('', '
                SELECT id_group, add_groups
                FROM {db_prefix}subscriptions
                WHERE id_subscribe = {int:current_subscription}',
                array(
                    'current_subscription' => $context['sub_id'],
                )
            );
            $id_group = 0;
            $add_groups = '';
            if ($smcFunc['db_num_rows']($request))
                list ($id_group, $add_groups) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);

            $changes = array();

            // Is their group changing? This subscription may not have changed primary group.
            if (!empty($id_group))
            {
                foreach ($members as $id_member => $member_data)
                {
                    // If their current primary group isn't what they had before the subscription, and their current group was
                    // granted by the sub, remove it.
                    if ($member_data['old_id_group'] != $member_data['id_group'] && $member_data['id_group'] == $id_group)
                        $changes[$id_member]['id_group'] = $member_data['old_id_group'];
                }
            }

            // Did this subscription add secondary groups?
            if (!empty($add_groups))
            {
                $add_groups = explode(',', $add_groups);
                foreach ($members as $id_member => $member_data)
                {
                    // First let's get their groups sorted.
                    $current_groups = explode(',', $member_data['additional_groups']);
                    $new_groups = implode(',', array_diff($current_groups, $add_groups));
                    if ($new_groups != $member_data['additional_groups'])
                        $changes[$id_member]['additional_groups'] = $new_groups;
                }
            }

            // We're going through changes...
            if (!empty($changes))
                foreach ($changes as $id_member => $new_values)
                    updateMemberData($id_member, $new_values);
        }

        // Delete the subscription
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}subscriptions
            WHERE id_subscribe = {int:current_subscription}',
            array(
                'current_subscription' => $context['sub_id'],
            )
        );

        // And delete any subscriptions to it to clear the phantom data too.
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}log_subscribed
            WHERE id_subscribe = {int:current_subscription}',
            array(
                'current_subscription' => $context['sub_id'],
            )
        );

        call_integration_hook('integrate_delete_subscription', array($context['sub_id']));

        redirectexit('action=admin;area=paidsubscribe;view');
    }

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

        // Some cleaning...
        $isActive = isset($_POST['active']) ? 1 : 0;
        $isRepeatable = isset($_POST['repeatable']) ? 1 : 0;
        $allowpartial = isset($_POST['allow_partial']) ? 1 : 0;
        $reminder = isset($_POST['reminder']) ? (int) $_POST['reminder'] : 0;
        $emailComplete = strlen($_POST['emailcomplete']) > 10 ? trim($_POST['emailcomplete']) : '';

        // Is this a fixed one?
        if ($_POST['duration_type'] == 'fixed')
        {
            // There are sanity check limits on these things.
            $limits = array(
                'D' => 90,
                'W' => 52,
                'M' => 24,
                'Y' => 5,
            );
            if (empty($_POST['span_unit']) || empty($limits[$_POST['span_unit']]) || empty($_POST['span_value']) || $_POST['span_value'] < 1)
                fatal_lang_error('paid_invalid_duration', false);

            if ($_POST['span_value'] > $limits[$_POST['span_unit']])
                fatal_lang_error('paid_invalid_duration_' . $_POST['span_unit'], false);

            // Clean the span.
            $span = $_POST['span_value'] . $_POST['span_unit'];

            // Sort out the cost.
            $cost = array('fixed' => sprintf('%01.2f', strtr($_POST['cost'], ',', '.')));

            // There needs to be something.
            if (empty($_POST['span_value']) || empty($_POST['cost']))
                fatal_lang_error('paid_no_cost_value');
        }
        // Flexible is harder but more fun ;)
        else
        {
            $span = 'F';

            $cost = array(
                'day' => sprintf('%01.2f', strtr($_POST['cost_day'], ',', '.')),
                'week' => sprintf('%01.2f', strtr($_POST['cost_week'], ',', '.')),
                'month' => sprintf('%01.2f', strtr($_POST['cost_month'], ',', '.')),
                'year' => sprintf('%01.2f', strtr($_POST['cost_year'], ',', '.')),
            );

            if (empty($_POST['cost_day']) && empty($_POST['cost_week']) && empty($_POST['cost_month']) && empty($_POST['cost_year']))
                fatal_lang_error('paid_all_freq_blank');
        }
        $cost = $smcFunc['json_encode']($cost);

        // Having now validated everything that might throw an error, let's also now deal with the token.
        validateToken('admin-pms');

        // Yep, time to do additional groups.
        $addgroups = array();
        if (!empty($_POST['addgroup']))
            foreach ($_POST['addgroup'] as $id => $dummy)
                $addgroups[] = (int) $id;
        $addgroups = implode(',', $addgroups);

        // Is it new?!
        if ($context['action_type'] == 'add')
        {
            $id_subscribe = $smcFunc['db_insert']('',
                '{db_prefix}subscriptions',
                array(
                    'name' => 'string-60', 'description' => 'string-255', 'active' => 'int', 'length' => 'string-4', 'cost' => 'string',
                    'id_group' => 'int', 'add_groups' => 'string-40', 'repeatable' => 'int', 'allow_partial' => 'int', 'email_complete' => 'string',
                    'reminder' => 'int',
                ),
                array(
                    $_POST['name'], $_POST['desc'], $isActive, $span, $cost,
                    $_POST['prim_group'], $addgroups, $isRepeatable, $allowpartial, $emailComplete,
                    $reminder,
                ),
                array('id_subscribe'),
                1
            );
        }
        // Otherwise must be editing.
        else
        {
            // Don't do groups if there are active members
            $request = $smcFunc['db_query']('', '
                SELECT COUNT(*)
                FROM {db_prefix}log_subscribed
                WHERE id_subscribe = {int:current_subscription}
                    AND status = {int:is_active}',
                array(
                    'current_subscription' => $context['sub_id'],
                    'is_active' => 1,
                )
            );
            list ($disableGroups) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);

            $smcFunc['db_query']('substring', '
                UPDATE {db_prefix}subscriptions
                    SET name = SUBSTRING({string:name}, 1, 60), description = SUBSTRING({string:description}, 1, 255), active = {int:is_active},
                    length = SUBSTRING({string:length}, 1, 4), cost = {string:cost}' . ($disableGroups ? '' : ', id_group = {int:id_group},
                    add_groups = {string:additional_groups}') . ', repeatable = {int:repeatable}, allow_partial = {int:allow_partial},
                    email_complete = {string:email_complete}, reminder = {int:reminder}
                WHERE id_subscribe = {int:current_subscription}',
                array(
                    'is_active' => $isActive,
                    'id_group' => !empty($_POST['prim_group']) ? $_POST['prim_group'] : 0,
                    'repeatable' => $isRepeatable,
                    'allow_partial' => $allowpartial,
                    'reminder' => $reminder,
                    'current_subscription' => $context['sub_id'],
                    'name' => $_POST['name'],
                    'description' => $_POST['desc'],
                    'length' => $span,
                    'cost' => $cost,
                    'additional_groups' => !empty($addgroups) ? $addgroups : '',
                    'email_complete' => $emailComplete,
                )
            );
        }
        call_integration_hook('integrate_save_subscription', array(($context['action_type'] == 'add' ? $id_subscribe : $context['sub_id']), $_POST['name'], $_POST['desc'], $isActive, $span, $cost, $_POST['prim_group'], $addgroups, $isRepeatable, $allowpartial, $emailComplete, $reminder));

        redirectexit('action=admin;area=paidsubscribe;view');
    }

    // Defaults.
    if ($context['action_type'] == 'add')
    {
        $context['sub'] = array(
            'name' => '',
            'desc' => '',
            'cost' => array(
                'fixed' => 0,
            ),
            'span' => array(
                'value' => '',
                'unit' => 'D',
            ),
            'prim_group' => 0,
            'add_groups' => array(),
            'active' => 1,
            'repeatable' => 1,
            'allow_partial' => 0,
            'duration' => 'fixed',
            'email_complete' => '',
            'reminder' => 0,
        );
    }
    // Otherwise load up all the details.
    else
    {
        $request = $smcFunc['db_query']('', '
            SELECT name, description, cost, length, id_group, add_groups, active, repeatable, allow_partial, email_complete, reminder
            FROM {db_prefix}subscriptions
            WHERE id_subscribe = {int:current_subscription}
            LIMIT 1',
            array(
                'current_subscription' => $context['sub_id'],
            )
        );
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            // Sort the date.
            preg_match('~(\d*)(\w)~', $row['length'], $match);
            if (isset($match[2]))
            {
                $span_value = $match[1];
                $span_unit = $match[2];
            }
            else
            {
                $span_value = 0;
                $span_unit = 'D';
            }

            // Is this a flexible one?
            if ($row['length'] == 'F')
                $isFlexible = true;
            else
                $isFlexible = false;

            $context['sub'] = array(
                'name' => $row['name'],
                'desc' => $row['description'],
                'cost' => $smcFunc['json_decode']($row['cost'], true),
                'span' => array(
                    'value' => $span_value,
                    'unit' => $span_unit,
                ),
                'prim_group' => $row['id_group'],
                'add_groups' => explode(',', $row['add_groups']),
                'active' => $row['active'],
                'repeatable' => $row['repeatable'],
                'allow_partial' => $row['allow_partial'],
                'duration' => $isFlexible ? 'flexible' : 'fixed',
                'email_complete' => $smcFunc['htmlspecialchars']($row['email_complete']),
                'reminder' => $row['reminder'],
            );
        }
        $smcFunc['db_free_result']($request);

        // Does this have members who are active?
        $request = $smcFunc['db_query']('', '
            SELECT COUNT(*)
            FROM {db_prefix}log_subscribed
            WHERE id_subscribe = {int:current_subscription}
                AND status = {int:is_active}',
            array(
                'current_subscription' => $context['sub_id'],
                'is_active' => 1,
            )
        );
        list ($context['disable_groups']) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
    }

    // Load up all the groups.
    $request = $smcFunc['db_query']('', '
        SELECT id_group, group_name
        FROM {db_prefix}membergroups
        WHERE id_group != {int:moderator_group}
            AND min_posts = {int:min_posts}',
        array(
            'moderator_group' => 3,
            'min_posts' => -1,
        )
    );
    $context['groups'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $context['groups'][$row['id_group']] = $row['group_name'];
    $smcFunc['db_free_result']($request);

    // This always happens.
    createToken($context['action_type'] == 'delete' ? 'admin-pmsd' : 'admin-pms');
}

/**
 * View all the users subscribed to a particular subscription.
 * Requires the admin_forum permission.
 * Accessed from ?action=admin;area=paidsubscribe;sa=viewsub.
 *
 * Subscription ID is required, in the form of $_GET['sid'].
 */
function ViewSubscribedUsers()
{
    global $context, $txt, $scripturl, $smcFunc, $sourcedir, $modSettings;

    // Setup the template.
    $context['page_title'] = $txt['viewing_users_subscribed'];

    // ID of the subscription.
    $context['sub_id'] = (int) $_REQUEST['sid'];

    // Load the subscription information.
    $request = $smcFunc['db_query']('', '
        SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active
        FROM {db_prefix}subscriptions
        WHERE id_subscribe = {int:current_subscription}',
        array(
            'current_subscription' => $context['sub_id'],
        )
    );
    // Something wrong?
    if ($smcFunc['db_num_rows']($request) == 0)
        fatal_lang_error('no_access', false);
    // Do the subscription context.
    $row = $smcFunc['db_fetch_assoc']($request);
    $context['subscription'] = array(
        'id' => $row['id_subscribe'],
        'name' => $row['name'],
        'desc' => $row['description'],
        'active' => $row['active'],
    );
    $smcFunc['db_free_result']($request);

    // Are we searching for people?
    $search_string = isset($_POST['ssearch']) && !empty($_POST['sub_search']) ? ' AND COALESCE(mem.real_name, {string:guest}) LIKE {string:search}' : '';
    $search_vars = empty($_POST['sub_search']) ? array() : array('search' => '%' . $_POST['sub_search'] . '%', 'guest' => $txt['guest']);

    $listOptions = array(
        'id' => 'subscribed_users_list',
        'title' => sprintf($txt['view_users_subscribed'], $row['name']),
        'items_per_page' => $modSettings['defaultMaxListItems'],
        'base_href' => $scripturl . '?action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id'],
        'default_sort_col' => 'name',
        'get_items' => array(
            'function' => 'list_getSubscribedUsers',
            'params' => array(
                $context['sub_id'],
                $search_string,
                $search_vars,
            ),
        ),
        'get_count' => array(
            'function' => 'list_getSubscribedUserCount',
            'params' => array(
                $context['sub_id'],
                $search_string,
                $search_vars,
            ),
        ),
        'no_items_label' => $txt['no_subscribers'],
        'columns' => array(
            'name' => array(
                'header' => array(
                    'value' => $txt['who_member'],
                    'style' => 'width: 20%;',
                ),
                'data' => array(
                    'function' => function($rowData) use ($scripturl, $txt)
                    {
                        return $rowData['id_member'] == 0 ? $txt['guest'] : '<a href="' . $scripturl . '?action=profile;u=' . $rowData['id_member'] . '">' . $rowData['name'] . '</a>';
                    },
                ),
                'sort' => array(
                    'default' => 'name',
                    'reverse' => 'name DESC',
                ),
            ),
            'status' => array(
                'header' => array(
                    'value' => $txt['paid_status'],
                    'style' => 'width: 10%;',
                ),
                'data' => array(
                    'db_htmlsafe' => 'status_text',
                ),
                'sort' => array(
                    'default' => 'status',
                    'reverse' => 'status DESC',
                ),
            ),
            'payments_pending' => array(
                'header' => array(
                    'value' => $txt['paid_payments_pending'],
                    'style' => 'width: 15%;',
                ),
                'data' => array(
                    'db_htmlsafe' => 'pending',
                ),
                'sort' => array(
                    'default' => 'payments_pending',
                    'reverse' => 'payments_pending DESC',
                ),
            ),
            'start_time' => array(
                'header' => array(
                    'value' => $txt['start_date'],
                    'style' => 'width: 20%;',
                ),
                'data' => array(
                    'db_htmlsafe' => 'start_date',
                    'class' => 'smalltext',
                ),
                'sort' => array(
                    'default' => 'start_time',
                    'reverse' => 'start_time DESC',
                ),
            ),
            'end_time' => array(
                'header' => array(
                    'value' => $txt['end_date'],
                    'style' => 'width: 20%;',
                ),
                'data' => array(
                    'db_htmlsafe' => 'end_date',
                    'class' => 'smalltext',
                ),
                'sort' => array(
                    'default' => 'end_time',
                    'reverse' => 'end_time DESC',
                ),
            ),
            'modify' => array(
                'header' => array(
                    'style' => 'width: 10%;',
                    'class' => 'centercol',
                ),
                'data' => array(
                    'function' => function($rowData) use ($scripturl, $txt)
                    {
                        return '<a href="' . $scripturl . '?action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $rowData['id'] . '">' . $txt['modify'] . '</a>';
                    },
                    'class' => 'centercol',
                ),
            ),
            'delete' => array(
                'header' => array(
                    'style' => 'width: 4%;',
                    'class' => 'centercol',
                ),
                'data' => array(
                    'function' => function($rowData)
                    {
                        return '<input type="checkbox" name="delsub[' . $rowData['id'] . ']">';
                    },
                    'class' => 'centercol',
                ),
            ),
        ),
        'form' => array(
            'href' => $scripturl . '?action=admin;area=paidsubscribe;sa=modifyuser;sid=' . $context['sub_id'],
        ),
        'additional_rows' => array(
            array(
                'position' => 'below_table_data',
                'value' => '
                    <input type="submit" name="add" value="' . $txt['add_subscriber'] . '" class="button">
                    <input type="submit" name="finished" value="' . $txt['complete_selected'] . '" data-confirm="' . $txt['complete_are_sure'] . '" class="button you_sure">
                    <input type="submit" name="delete" value="' . $txt['delete_selected'] . '" data-confirm="' . $txt['delete_are_sure'] . '" class="button you_sure">
                ',
            ),
            array(
                'position' => 'top_of_list',
                'value' => '
                    <div class="flow_auto">
                        <input type="submit" name="ssearch" value="' . $txt['search_sub'] . '" class="button" style="margin-top: 3px;">
                        <input type="text" name="sub_search" value="" class="floatright">
                    </div>
                ',
            ),
        ),
    );

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

    $context['sub_template'] = 'show_list';
    $context['default_list'] = 'subscribed_users_list';
}

/**
 * Returns how many people are subscribed to a paid subscription.
 *
 * @todo refactor away
 *
 * @param int $id_sub The ID of the subscription
 * @param string $search_string A search string
 * @param array $search_vars An array of variables for the search string
 * @return int The number of subscribed users matching the given parameters
 */
function list_getSubscribedUserCount($id_sub, $search_string, $search_vars = array())
{
    global $smcFunc;

    // Get the total amount of users.
    $request = $smcFunc['db_query']('', '
        SELECT COUNT(*) AS total_subs
        FROM {db_prefix}log_subscribed AS ls
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
        WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
            AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_pending_payments})',
        array_merge($search_vars, array(
            'current_subscription' => $id_sub,
            'no_end_time' => 0,
            'no_pending_payments' => 0,
        ))
    );
    list ($memberCount) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    return $memberCount;
}

/**
 * Return the subscribed users list, for the given parameters.
 *
 * @todo refactor outta here
 *
 * @param int $start The item to start with (for pagination purposes)
 * @param int $items_per_page How many items to show on each page
 * @param string $sort A string indicating how to sort the results
 * @param int $id_sub The ID of the subscription
 * @param string $search_string A search string
 * @param array $search_vars The variables for the search string
 * @return array An array of information about the subscribed users matching the given parameters
 */
function list_getSubscribedUsers($start, $items_per_page, $sort, $id_sub, $search_string, $search_vars = array())
{
    global $smcFunc, $txt;

    $request = $smcFunc['db_query']('', '
        SELECT ls.id_sublog, COALESCE(mem.id_member, 0) AS id_member, COALESCE(mem.real_name, {string:guest}) AS name, ls.start_time, ls.end_time,
            ls.status, ls.payments_pending
        FROM {db_prefix}log_subscribed AS ls
            LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
        WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
            AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_payments_pending})
        ORDER BY {raw:sort}
        LIMIT {int:start}, {int:max}',
        array_merge($search_vars, array(
            'current_subscription' => $id_sub,
            'no_end_time' => 0,
            'no_payments_pending' => 0,
            'guest' => $txt['guest'],
            'sort' => $sort,
            'start' => $start,
            'max' => $items_per_page,
        ))
    );
    $subscribers = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
        $subscribers[] = array(
            'id' => $row['id_sublog'],
            'id_member' => $row['id_member'],
            'name' => $row['name'],
            'start_date' => timeformat($row['start_time'], false),
            'end_date' => $row['end_time'] == 0 ? 'N/A' : timeformat($row['end_time'], false),
            'pending' => $row['payments_pending'],
            'status' => $row['status'],
            'status_text' => $row['status'] == 0 ? ($row['payments_pending'] == 0 ? $txt['paid_finished'] : $txt['paid_pending']) : $txt['paid_active'],
        );
    $smcFunc['db_free_result']($request);

    return $subscribers;
}

/**
 * Edit or add a user subscription.
 * Accessed from ?action=admin;area=paidsubscribe;sa=modifyuser.
 */
function ModifyUserSubscription()
{
    global $context, $txt, $modSettings, $smcFunc;

    loadSubscriptions();

    $context['log_id'] = isset($_REQUEST['lid']) ? (int) $_REQUEST['lid'] : 0;
    $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
    $context['action_type'] = $context['log_id'] ? 'edit' : 'add';

    // Setup the template.
    $context['sub_template'] = 'modify_user_subscription';
    $context['page_title'] = $txt[$context['action_type'] . '_subscriber'];

    // If we haven't been passed the subscription ID get it.
    if ($context['log_id'] && !$context['sub_id'])
    {
        $request = $smcFunc['db_query']('', '
            SELECT id_subscribe
            FROM {db_prefix}log_subscribed
            WHERE id_sublog = {int:current_log_item}',
            array(
                'current_log_item' => $context['log_id'],
            )
        );
        if ($smcFunc['db_num_rows']($request) == 0)
            fatal_lang_error('no_access', false);
        list ($context['sub_id']) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
    }

    if (!isset($context['subscriptions'][$context['sub_id']]))
        fatal_lang_error('no_access', false);
    $context['current_subscription'] = $context['subscriptions'][$context['sub_id']];

    // Searching?
    if (isset($_POST['ssearch']))
    {
        return ViewSubscribedUsers();
    }
    // Saving?
    elseif (isset($_REQUEST['save_sub']))
    {
        checkSession();

        // Work out the dates...
        $starttime = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'], $_POST['day'], $_POST['year']);
        $endtime = mktime($_POST['hourend'], $_POST['minuteend'], 0, $_POST['monthend'], $_POST['dayend'], $_POST['yearend']);

        // Status.
        $status = $_POST['status'];

        // New one?
        if (empty($context['log_id']))
        {
            // Find the user...
            $request = $smcFunc['db_query']('', '
                SELECT id_member, id_group
                FROM {db_prefix}members
                WHERE real_name = {string:name}
                LIMIT 1',
                array(
                    'name' => $_POST['name'],
                )
            );
            if ($smcFunc['db_num_rows']($request) == 0)
                fatal_lang_error('error_member_not_found');

            list ($id_member, $id_group) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);

            // Ensure the member doesn't already have a subscription!
            $request = $smcFunc['db_query']('', '
                SELECT id_subscribe
                FROM {db_prefix}log_subscribed
                WHERE id_subscribe = {int:current_subscription}
                    AND id_member = {int:current_member}',
                array(
                    'current_subscription' => $context['sub_id'],
                    'current_member' => $id_member,
                )
            );
            if ($smcFunc['db_num_rows']($request) != 0)
                fatal_lang_error('member_already_subscribed');
            $smcFunc['db_free_result']($request);

            // Actually put the subscription in place.
            if ($status == 1)
                addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
            else
            {
                $smcFunc['db_insert']('',
                    '{db_prefix}log_subscribed',
                    array(
                        'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
                        'end_time' => 'int', 'status' => 'int', 'pending_details' => 'string-65534'
                    ),
                    array(
                        $context['sub_id'], $id_member, $id_group, $starttime,
                        $endtime, $status, $smcFunc['json_encode'](array())
                    ),
                    array('id_sublog')
                );

            }
        }
        // Updating.
        else
        {
            $request = $smcFunc['db_query']('', '
                SELECT id_member, status
                FROM {db_prefix}log_subscribed
                WHERE id_sublog = {int:current_log_item}',
                array(
                    'current_log_item' => $context['log_id'],
                )
            );
            if ($smcFunc['db_num_rows']($request) == 0)
                fatal_lang_error('no_access', false);

            list ($id_member, $old_status) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);

            // Pick the right permission stuff depending on what the status is changing from/to.
            if ($old_status == 1 && $status != 1)
                removeSubscription($context['sub_id'], $id_member);
            elseif ($status == 1 && $old_status != 1)
            {
                addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
            }
            else
            {
                $smcFunc['db_query']('', '
                    UPDATE {db_prefix}log_subscribed
                    SET start_time = {int:start_time}, end_time = {int:end_time}, status = {int:status}
                    WHERE id_sublog = {int:current_log_item}',
                    array(
                        'start_time' => $starttime,
                        'end_time' => $endtime,
                        'status' => $status,
                        'current_log_item' => $context['log_id'],
                    )
                );
            }
        }

        // Done - redirect...
        redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
    }
    // Deleting?
    elseif (isset($_REQUEST['delete']) || isset($_REQUEST['finished']))
    {
        checkSession();

        // Do the actual deletes!
        if (!empty($_REQUEST['delsub']))
        {
            $toDelete = array();
            foreach ($_REQUEST['delsub'] as $id => $dummy)
                $toDelete[] = (int) $id;

            $request = $smcFunc['db_query']('', '
                SELECT id_subscribe, id_member
                FROM {db_prefix}log_subscribed
                WHERE id_sublog IN ({array_int:subscription_list})',
                array(
                    'subscription_list' => $toDelete,
                )
            );
            while ($row = $smcFunc['db_fetch_assoc']($request))
                removeSubscription($row['id_subscribe'], $row['id_member'], isset($_REQUEST['delete']));
            $smcFunc['db_free_result']($request);
        }
        redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
    }

    // Default attributes.
    if ($context['action_type'] == 'add')
    {
        $context['sub'] = array(
            'id' => 0,
            'start' => array(
                'year' => (int) strftime('%Y', time()),
                'month' => (int) strftime('%m', time()),
                'day' => (int) strftime('%d', time()),
                'hour' => (int) strftime('%H', time()),
                'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
                'last_day' => 0,
            ),
            'end' => array(
                'year' => (int) strftime('%Y', time()),
                'month' => (int) strftime('%m', time()),
                'day' => (int) strftime('%d', time()),
                'hour' => (int) strftime('%H', time()),
                'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
                'last_day' => 0,
            ),
            'status' => 1,
        );
        $context['sub']['start']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['sub']['start']['month'] == 12 ? 1 : $context['sub']['start']['month'] + 1, 0, $context['sub']['start']['month'] == 12 ? $context['sub']['start']['year'] + 1 : $context['sub']['start']['year']));
        $context['sub']['end']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['sub']['end']['month'] == 12 ? 1 : $context['sub']['end']['month'] + 1, 0, $context['sub']['end']['month'] == 12 ? $context['sub']['end']['year'] + 1 : $context['sub']['end']['year']));

        if (isset($_GET['uid']))
        {
            $request = $smcFunc['db_query']('', '
                SELECT real_name
                FROM {db_prefix}members
                WHERE id_member = {int:current_member}',
                array(
                    'current_member' => (int) $_GET['uid'],
                )
            );
            list ($context['sub']['username']) = $smcFunc['db_fetch_row']($request);
            $smcFunc['db_free_result']($request);
        }
        else
            $context['sub']['username'] = '';
    }
    // Otherwise load the existing info.
    else
    {
        $request = $smcFunc['db_query']('', '
            SELECT ls.id_sublog, ls.id_subscribe, ls.id_member, start_time, end_time, status, payments_pending, pending_details,
                COALESCE(mem.real_name, {string:blank_string}) AS username
            FROM {db_prefix}log_subscribed AS ls
                LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
            WHERE ls.id_sublog = {int:current_subscription_item}
            LIMIT 1',
            array(
                'current_subscription_item' => $context['log_id'],
                'blank_string' => '',
            )
        );
        if ($smcFunc['db_num_rows']($request) == 0)
            fatal_lang_error('no_access', false);
        $row = $smcFunc['db_fetch_assoc']($request);
        $smcFunc['db_free_result']($request);

        // Any pending payments?
        $context['pending_payments'] = array();
        if (!empty($row['pending_details']))
        {
            $pending_details = $smcFunc['json_decode']($row['pending_details'], true);
            foreach ($pending_details as $id => $pending)
            {
                // Only this type need be displayed.
                if ($pending[3] == 'payback')
                {
                    // Work out what the options were.
                    $costs = $smcFunc['json_decode']($context['current_subscription']['real_cost'], true);

                    if ($context['current_subscription']['real_length'] == 'F')
                    {
                        foreach ($costs as $duration => $cost)
                        {
                            if ($cost != 0 && $cost == $pending[1] && $duration == $pending[2])
                                $context['pending_payments'][$id] = array(
                                    'desc' => sprintf($modSettings['paid_currency_symbol'], $cost . '/' . $txt[$duration]),
                                );
                        }
                    }
                    elseif ($costs['fixed'] == $pending[1])
                    {
                        $context['pending_payments'][$id] = array(
                            'desc' => sprintf($modSettings['paid_currency_symbol'], $costs['fixed']),
                        );
                    }
                }
            }

            // Check if we are adding/removing any.
            if (isset($_GET['pending']))
            {
                foreach ($pending_details as $id => $pending)
                {
                    // Found the one to action?
                    if ($_GET['pending'] == $id && $pending[3] == 'payback' && isset($context['pending_payments'][$id]))
                    {
                        // Flexible?
                        if (isset($_GET['accept']))
                            addSubscription($context['current_subscription']['id'], $row['id_member'], $context['current_subscription']['real_length'] == 'F' ? strtoupper(substr($pending[2], 0, 1)) : 0);
                        unset($pending_details[$id]);

                        $new_details = $smcFunc['json_encode']($pending_details);

                        // Update the entry.
                        $smcFunc['db_query']('', '
                            UPDATE {db_prefix}log_subscribed
                            SET payments_pending = payments_pending - 1, pending_details = {string:pending_details}
                            WHERE id_sublog = {int:current_subscription_item}',
                            array(
                                'current_subscription_item' => $context['log_id'],
                                'pending_details' => $new_details,
                            )
                        );

                        // Reload
                        redirectexit('action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $context['log_id']);
                    }
                }
            }
        }

        $context['sub_id'] = $row['id_subscribe'];
        $context['sub'] = array(
            'id' => 0,
            'start' => array(
                'year' => (int) strftime('%Y', $row['start_time']),
                'month' => (int) strftime('%m', $row['start_time']),
                'day' => (int) strftime('%d', $row['start_time']),
                'hour' => (int) strftime('%H', $row['start_time']),
                'min' => (int) strftime('%M', $row['start_time']) < 10 ? '0' . (int) strftime('%M', $row['start_time']) : (int) strftime('%M', $row['start_time']),
                'last_day' => 0,
            ),
            'end' => array(
                'year' => (int) strftime('%Y', $row['end_time']),
                'month' => (int) strftime('%m', $row['end_time']),
                'day' => (int) strftime('%d', $row['end_time']),
                'hour' => (int) strftime('%H', $row['end_time']),
                'min' => (int) strftime('%M', $row['end_time']) < 10 ? '0' . (int) strftime('%M', $row['end_time']) : (int) strftime('%M', $row['end_time']),
                'last_day' => 0,
            ),
            'status' => $row['status'],
            'username' => $row['username'],
        );
        $context['sub']['start']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['sub']['start']['month'] == 12 ? 1 : $context['sub']['start']['month'] + 1, 0, $context['sub']['start']['month'] == 12 ? $context['sub']['start']['year'] + 1 : $context['sub']['start']['year']));
        $context['sub']['end']['last_day'] = (int) strftime('%d', mktime(0, 0, 0, $context['sub']['end']['month'] == 12 ? 1 : $context['sub']['end']['month'] + 1, 0, $context['sub']['end']['month'] == 12 ? $context['sub']['end']['year'] + 1 : $context['sub']['end']['year']));
    }

    loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
}

/**
 * Reapplies all subscription rules for each of the users.
 *
 * @param array $users An array of user IDs
 */
function reapplySubscriptions($users)
{
    global $smcFunc;

    // Make it an array.
    if (!is_array($users))
        $users = array($users);

    // Get all the members current groups.
    $groups = array();
    $request = $smcFunc['db_query']('', '
        SELECT id_member, id_group, additional_groups
        FROM {db_prefix}members
        WHERE id_member IN ({array_int:user_list})',
        array(
            'user_list' => $users,
        )
    );
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $groups[$row['id_member']] = array(
            'primary' => $row['id_group'],
            'additional' => explode(',', $row['additional_groups']),
        );
    }
    $smcFunc['db_free_result']($request);

    $request = $smcFunc['db_query']('', '
        SELECT ls.id_member, ls.old_id_group, s.id_group, s.add_groups
        FROM {db_prefix}log_subscribed AS ls
            INNER JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
        WHERE ls.id_member IN ({array_int:user_list})
            AND ls.end_time > {int:current_time}',
        array(
            'user_list' => $users,
            'current_time' => time(),
        )
    );
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        // Specific primary group?
        if ($row['id_group'] != 0)
        {
            // If this is changing - add the old one to the additional groups so it's not lost.
            if ($row['id_group'] != $groups[$row['id_member']]['primary'])
                $groups[$row['id_member']]['additional'][] = $groups[$row['id_member']]['primary'];
            $groups[$row['id_member']]['primary'] = $row['id_group'];
        }

        // Additional groups.
        if (!empty($row['add_groups']))
            $groups[$row['id_member']]['additional'] = array_merge($groups[$row['id_member']]['additional'], explode(',', $row['add_groups']));
    }
    $smcFunc['db_free_result']($request);

    // Update all the members.
    foreach ($groups as $id => $group)
    {
        $group['additional'] = array_unique($group['additional']);
        foreach ($group['additional'] as $key => $value)
            if (empty($value))
                unset($group['additional'][$key]);
        $addgroups = implode(',', $group['additional']);

        $smcFunc['db_query']('', '
            UPDATE {db_prefix}members
            SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
            WHERE id_member = {int:current_member}
            LIMIT 1',
            array(
                'primary_group' => $group['primary'],
                'current_member' => $id,
                'additional_groups' => $addgroups,
            )
        );
    }
}

/**
 * Add or extend a subscription of a user.
 *
 * @param int $id_subscribe The subscription ID
 * @param int $id_member The ID of the member
 * @param int|string $renewal 0 if we're forcing start/end time, otherwise a string indicating how long to renew the subscription for ('D', 'W', 'M' or 'Y')
 * @param int $forceStartTime If set, forces the subscription to start at the specified time
 * @param int $forceEndTime If set, forces the subscription to end at the specified time
 */
function addSubscription($id_subscribe, $id_member, $renewal = 0, $forceStartTime = 0, $forceEndTime = 0)
{
    global $context, $smcFunc;

    // Take the easy way out...
    loadSubscriptions();

    // Exists, yes?
    if (!isset($context['subscriptions'][$id_subscribe]))
        return;

    $curSub = $context['subscriptions'][$id_subscribe];

    // Grab the duration.
    $duration = $curSub['num_length'];

    // If this is a renewal change the duration to be correct.
    if (!empty($renewal))
    {
        switch ($renewal)
        {
            case 'D':
                $duration = 86400;
                break;
            case 'W':
                $duration = 604800;
                break;
            case 'M':
                $duration = 2629743;
                break;
            case 'Y':
                $duration = 31556926;
                break;
            default:
                break;
        }
    }

    // Firstly, see whether it exists, and is active. If so then this is meerly an extension.
    $request = $smcFunc['db_query']('', '
        SELECT id_sublog, end_time, start_time
        FROM {db_prefix}log_subscribed
        WHERE id_subscribe = {int:current_subscription}
            AND id_member = {int:current_member}
            AND status = {int:is_active}',
        array(
            'current_subscription' => $id_subscribe,
            'current_member' => $id_member,
            'is_active' => 1,
        )
    );

    if ($smcFunc['db_num_rows']($request) != 0)
    {
        list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);

        // If this has already expired but is active, extension means the period from now.
        if ($endtime < time())
            $endtime = time();
        if ($starttime == 0)
            $starttime = time();

        // Work out the new expiry date.
        $endtime += $duration;

        if ($forceEndTime != 0)
            $endtime = $forceEndTime;

        // As everything else should be good, just update!
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}log_subscribed
            SET end_time = {int:end_time}, start_time = {int:start_time}, reminder_sent = {int:no_reminder_sent}
            WHERE id_sublog = {int:current_subscription_item}',
            array(
                'end_time' => $endtime,
                'start_time' => $starttime,
                'current_subscription_item' => $id_sublog,
                'no_reminder_sent' => 0,
            )
        );

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

    // If we're here, that means we don't have an active subscription - that means we need to do some work!
    $request = $smcFunc['db_query']('', '
        SELECT m.id_group, m.additional_groups
        FROM {db_prefix}members AS m
        WHERE m.id_member = {int:current_member}',
        array(
            'current_member' => $id_member,
        )
    );

    // Just in case the member doesn't exist.
    if ($smcFunc['db_num_rows']($request) == 0)
        return;

    list ($old_id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    // Prepare additional groups.
    $newAddGroups = explode(',', $curSub['add_groups']);
    $curAddGroups = explode(',', $additional_groups);

    $newAddGroups = array_merge($newAddGroups, $curAddGroups);

    // Simple, simple, simple - hopefully... id_group first.
    if ($curSub['prim_group'] != 0)
    {
        $id_group = $curSub['prim_group'];

        // Ensure their old privileges are maintained.
        if ($old_id_group != 0)
            $newAddGroups[] = $old_id_group;
    }
    else
        $id_group = $old_id_group;

    // Yep, make sure it's unique, and no empties.
    foreach ($newAddGroups as $k => $v)
        if (empty($v))
            unset($newAddGroups[$k]);
    $newAddGroups = array_unique($newAddGroups);
    $newAddGroups = implode(',', $newAddGroups);

    // Store the new settings.
    $smcFunc['db_query']('', '
        UPDATE {db_prefix}members
        SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
        WHERE id_member = {int:current_member}',
        array(
            'primary_group' => $id_group,
            'current_member' => $id_member,
            'additional_groups' => $newAddGroups,
        )
    );

    // Now log the subscription - maybe we have a dorment subscription we can restore?
    $request = $smcFunc['db_query']('', '
        SELECT id_sublog, end_time, start_time
        FROM {db_prefix}log_subscribed
        WHERE id_subscribe = {int:current_subscription}
            AND id_member = {int:current_member}',
        array(
            'current_subscription' => $id_subscribe,
            'current_member' => $id_member,
        )
    );

    /**
     * @todo Don't really need to do this twice...
     */
    if ($smcFunc['db_num_rows']($request) != 0)
    {
        list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);

        // If this has already expired but is active, extension means the period from now.
        if ($endtime < time())
            $endtime = time();
        if ($starttime == 0)
            $starttime = time();

        // Work out the new expiry date.
        $endtime += $duration;

        if ($forceEndTime != 0)
            $endtime = $forceEndTime;

        // As everything else should be good, just update!
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}log_subscribed
            SET start_time = {int:start_time}, end_time = {int:end_time}, old_id_group = {int:old_id_group}, status = {int:is_active}, reminder_sent = {int:no_reminder_sent}
            WHERE id_sublog = {int:current_subscription_item}',
            array(
                'start_time' => $starttime,
                'end_time' => $endtime,
                'old_id_group' => $old_id_group,
                'is_active' => 1,
                'no_reminder_sent' => 0,
                'current_subscription_item' => $id_sublog,
            )
        );

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

    // Otherwise a very simple insert.
    $endtime = time() + $duration;
    if ($forceEndTime != 0)
        $endtime = $forceEndTime;

    if ($forceStartTime == 0)
        $starttime = time();
    else
        $starttime = $forceStartTime;

    $smcFunc['db_insert']('',
        '{db_prefix}log_subscribed',
        array(
            'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
            'end_time' => 'int', 'status' => 'int', 'pending_details' => 'string',
        ),
        array(
            $id_subscribe, $id_member, $old_id_group, $starttime,
            $endtime, 1, '',
        ),
        array('id_sublog')
    );
}

/**
 * Removes a subscription from a user, as in removes the groups.
 *
 * @param int $id_subscribe The ID of the subscription
 * @param int $id_member The ID of the member
 * @param bool $delete Whether to delete the subscription or just disable it
 */
function removeSubscription($id_subscribe, $id_member, $delete = false)
{
    global $context, $smcFunc;

    loadSubscriptions();

    // Load the user core bits.
    $request = $smcFunc['db_query']('', '
        SELECT m.id_group, m.additional_groups
        FROM {db_prefix}members AS m
        WHERE m.id_member = {int:current_member}',
        array(
            'current_member' => $id_member,
        )
    );

    // Just in case of errors.
    if ($smcFunc['db_num_rows']($request) == 0)
    {
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}log_subscribed
            WHERE id_member = {int:current_member}',
            array(
                'current_member' => $id_member,
            )
        );
        return;
    }
    list ($id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);

    // Get all of the subscriptions for this user that are active - it will be necessary!
    $request = $smcFunc['db_query']('', '
        SELECT id_subscribe, old_id_group
        FROM {db_prefix}log_subscribed
        WHERE id_member = {int:current_member}
            AND status = {int:is_active}',
        array(
            'current_member' => $id_member,
            'is_active' => 1,
        )
    );

    // These variables will be handy, honest ;)
    $removals = array();
    $allowed = array();
    $old_id_group = 0;
    $new_id_group = -1;
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        if (!isset($context['subscriptions'][$row['id_subscribe']]))
            continue;

        // The one we're removing?
        if ($row['id_subscribe'] == $id_subscribe)
        {
            $removals = explode(',', $context['subscriptions'][$row['id_subscribe']]['add_groups']);
            if ($context['subscriptions'][$row['id_subscribe']]['prim_group'] != 0)
                $removals[] = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
            $old_id_group = $row['old_id_group'];
        }
        // Otherwise things we allow.
        else
        {
            $allowed = array_merge($allowed, explode(',', $context['subscriptions'][$row['id_subscribe']]['add_groups']));
            if ($context['subscriptions'][$row['id_subscribe']]['prim_group'] != 0)
            {
                $allowed[] = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
                $new_id_group = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
            }
        }
    }
    $smcFunc['db_free_result']($request);

    // Now, for everything we are removing check they definitely are not allowed it.
    $existingGroups = explode(',', $additional_groups);
    foreach ($existingGroups as $key => $group)
        if (empty($group) || (in_array($group, $removals) && !in_array($group, $allowed)))
            unset($existingGroups[$key]);

    // Finally, do something with the current primary group.
    if (in_array($id_group, $removals))
    {
        // If this primary group is actually allowed keep it.
        if (in_array($id_group, $allowed))
            $existingGroups[] = $id_group;

        // Either way, change the id_group back.
        if ($new_id_group < 1)
        {
            // If we revert to the old id-group we need to ensure it wasn't from a subscription.
            foreach ($context['subscriptions'] as $id => $group)
                // It was? Make them a regular member then!
                if ($group['prim_group'] == $old_id_group)
                    $old_id_group = 0;

            $id_group = $old_id_group;
        }
        else
            $id_group = $new_id_group;
    }

    // Crazy stuff, we seem to have our groups fixed, just make them unique
    $existingGroups = array_unique($existingGroups);
    $existingGroups = implode(',', $existingGroups);

    // Update the member
    $smcFunc['db_query']('', '
        UPDATE {db_prefix}members
        SET id_group = {int:primary_group}, additional_groups = {string:existing_groups}
        WHERE id_member = {int:current_member}',
        array(
            'primary_group' => $id_group,
            'current_member' => $id_member,
            'existing_groups' => $existingGroups,
        )
    );

    // Disable the subscription.
    if (!$delete)
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}log_subscribed
            SET status = {int:not_active}
            WHERE id_member = {int:current_member}
                AND id_subscribe = {int:current_subscription}',
            array(
                'not_active' => 0,
                'current_member' => $id_member,
                'current_subscription' => $id_subscribe,
            )
        );
    // Otherwise delete it!
    else
        $smcFunc['db_query']('', '
            DELETE FROM {db_prefix}log_subscribed
            WHERE id_member = {int:current_member}
                AND id_subscribe = {int:current_subscription}',
            array(
                'current_member' => $id_member,
                'current_subscription' => $id_subscribe,
            )
        );
}

/**
 * This just kind of caches all the subscription data.
 */
function loadSubscriptions()
{
    global $context, $txt, $modSettings, $smcFunc;

    if (!empty($context['subscriptions']))
        return;

    // Make sure this is loaded, just in case.
    loadLanguage('ManagePaid');

    $request = $smcFunc['db_query']('', '
        SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active, repeatable
        FROM {db_prefix}subscriptions',
        array(
        )
    );
    $context['subscriptions'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        // Pick a cost.
        $costs = $smcFunc['json_decode']($row['cost'], true);

        if ($row['length'] != 'F' && !empty($modSettings['paid_currency_symbol']) && !empty($costs['fixed']))
            $cost = sprintf($modSettings['paid_currency_symbol'], $costs['fixed']);
        else
            $cost = '???';

        // Do the span.
        preg_match('~(\d*)(\w)~', $row['length'], $match);
        if (isset($match[2]))
        {
            $num_length = $match[1];
            $length = $match[1] . ' ';
            switch ($match[2])
            {
                case 'D':
                    $length .= $txt['paid_mod_span_days'];
                    $num_length *= 86400;
                    break;
                case 'W':
                    $length .= $txt['paid_mod_span_weeks'];
                    $num_length *= 604800;
                    break;
                case 'M':
                    $length .= $txt['paid_mod_span_months'];
                    $num_length *= 2629743;
                    break;
                case 'Y':
                    $length .= $txt['paid_mod_span_years'];
                    $num_length *= 31556926;
                    break;
            }
        }
        else
            $length = '??';

        $context['subscriptions'][$row['id_subscribe']] = array(
            'id' => $row['id_subscribe'],
            'name' => $row['name'],
            'desc' => $row['description'],
            'cost' => $cost,
            'real_cost' => $row['cost'],
            'length' => $length,
            'num_length' => $num_length,
            'real_length' => $row['length'],
            'pending' => 0,
            'finished' => 0,
            'total' => 0,
            'active' => $row['active'],
            'prim_group' => $row['id_group'],
            'add_groups' => $row['add_groups'],
            'flexible' => $row['length'] == 'F' ? true : false,
            'repeatable' => $row['repeatable'],
        );
    }
    $smcFunc['db_free_result']($request);

    // Do the counts.
    $request = $smcFunc['db_query']('', '
        SELECT COUNT(id_sublog) AS member_count, id_subscribe, status
        FROM {db_prefix}log_subscribed
        GROUP BY id_subscribe, status',
        array(
        )
    );
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        $ind = $row['status'] == 0 ? 'finished' : 'total';

        if (isset($context['subscriptions'][$row['id_subscribe']]))
            $context['subscriptions'][$row['id_subscribe']][$ind] = $row['member_count'];
    }
    $smcFunc['db_free_result']($request);

    // How many payments are we waiting on?
    $request = $smcFunc['db_query']('', '
        SELECT SUM(payments_pending) AS total_pending, id_subscribe
        FROM {db_prefix}log_subscribed
        GROUP BY id_subscribe',
        array(
        )
    );
    while ($row = $smcFunc['db_fetch_assoc']($request))
    {
        if (isset($context['subscriptions'][$row['id_subscribe']]))
            $context['subscriptions'][$row['id_subscribe']]['pending'] = $row['total_pending'];
    }
    $smcFunc['db_free_result']($request);
}

/**
 * Load all the payment gateways.
 * Checks the Sources directory for any files fitting the format of a payment gateway,
 * loads each file to check it's valid, includes each file and returns the
 * function name and whether it should work with this version of SMF.
 *
 * @return array An array of information about available payment gateways
 */
function loadPaymentGateways()
{
    global $sourcedir;

    $gateways = array();
    if ($dh = opendir($sourcedir))
    {
        while (($file = readdir($dh)) !== false)
        {
            if (is_file($sourcedir . '/' . $file) && preg_match('~^Subscriptions-([A-Za-z\d]+)\.php$~', $file, $matches))
            {
                // Check this is definitely a valid gateway!
                $fp = fopen($sourcedir . '/' . $file, 'rb');
                $header = fread($fp, 4096);
                fclose($fp);

                if (strpos($header, '// SMF Payment Gateway: ' . strtolower($matches[1])) !== false)
                {
                    require_once($sourcedir . '/' . $file);

                    $gateways[] = array(
                        'filename' => $file,
                        'code' => strtolower($matches[1]),
                        // Don't need anything snazzier than this yet.
                        'valid_version' => class_exists(strtolower($matches[1]) . '_payment') && class_exists(strtolower($matches[1]) . '_display'),
                        'payment_class' => strtolower($matches[1]) . '_payment',
                        'display_class' => strtolower($matches[1]) . '_display',
                    );
                }
            }
        }
    }
    closedir($dh);

    return $gateways;
}

?>