ManagePaid.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. <?php
  2. /**
  3. * This file contains all the administration functions for subscriptions.
  4. * (and some more than that :P)
  5. *
  6. * Simple Machines Forum (SMF)
  7. *
  8. * @package SMF
  9. * @author Simple Machines http://www.simplemachines.org
  10. * @copyright 2011 Simple Machines
  11. * @license http://www.simplemachines.org/about/smf/license.php BSD
  12. *
  13. * @version 2.0
  14. */
  15. if (!defined('SMF'))
  16. die('Hacking attempt...');
  17. /**
  18. * The main entrance point for the 'Paid Subscription' screen, calling
  19. * the right function based on the given sub-action.
  20. * It defaults to sub-action 'view'.
  21. * Accessed from ?action=admin;area=paidsubscribe.
  22. * It requires admin_forum permission for admin based actions.
  23. */
  24. function ManagePaidSubscriptions()
  25. {
  26. global $context, $txt, $scripturl, $sourcedir, $smcFunc, $modSettings;
  27. // Load the required language and template.
  28. loadLanguage('ManagePaid');
  29. loadTemplate('ManagePaid');
  30. $subActions = array(
  31. 'modify' => array('ModifySubscription', 'admin_forum'),
  32. 'modifyuser' => array('ModifyUserSubscription', 'admin_forum'),
  33. 'settings' => array('ModifySubscriptionSettings', 'admin_forum'),
  34. 'view' => array('ViewSubscriptions', 'admin_forum'),
  35. 'viewsub' => array('ViewSubscribedUsers', 'admin_forum'),
  36. );
  37. // Default the sub-action to 'view subscriptions', but only if they have already set things up..
  38. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (!empty($modSettings['paid_currency_symbol']) ? 'view' : 'settings');
  39. // Make sure you can do this.
  40. isAllowedTo($subActions[$_REQUEST['sa']][1]);
  41. $context['page_title'] = $txt['paid_subscriptions'];
  42. // Tabs for browsing the different subscription functions.
  43. $context[$context['admin_menu_name']]['tab_data'] = array(
  44. 'title' => $txt['paid_subscriptions'],
  45. 'help' => '',
  46. 'description' => $txt['paid_subscriptions_desc'],
  47. 'tabs' => array(
  48. 'view' => array(
  49. 'description' => $txt['paid_subs_view_desc'],
  50. ),
  51. 'settings' => array(
  52. 'description' => $txt['paid_subs_settings_desc'],
  53. ),
  54. ),
  55. );
  56. // Call the right function for this sub-acton.
  57. $subActions[$_REQUEST['sa']][0]();
  58. }
  59. /**
  60. * Set any setting related to paid subscriptions, i.e.
  61. * modify which payment methods are to be used.
  62. * It requires the moderate_forum permission
  63. * Accessed from ?action=admin;area=paidsubscribe;sa=settings.
  64. *
  65. * @param bool $return_config = false
  66. */
  67. function ModifySubscriptionSettings($return_config = false)
  68. {
  69. global $context, $txt, $modSettings, $sourcedir, $smcFunc, $scripturl;
  70. // 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.
  71. $modSettings['paid_currency'] = !empty($modSettings['paid_currency_code']) ? $modSettings['paid_currency_code'] : '';
  72. if (!empty($modSettings['paid_currency_code']) && !in_array($modSettings['paid_currency_code'], array('usd', 'eur', 'gbp')))
  73. $modSettings['paid_currency'] = 'other';
  74. // These are all the default settings.
  75. $config_vars = array(
  76. 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']),
  77. array('text', 'paid_email_to', 'subtext' => $txt['paid_email_to_desc'], 'size' => 60),
  78. '',
  79. 'dummy_currency' => array('select', 'paid_currency', array('usd' => $txt['usd'], 'eur' => $txt['eur'], 'gbp' => $txt['gbp'], 'other' => $txt['other']), 'javascript' => 'onchange="toggleOther();"'),
  80. array('text', 'paid_currency_code', 'subtext' => $txt['paid_currency_code_desc'], 'size' => 5, 'force_div_id' => 'custom_currency_code_div'),
  81. array('text', 'paid_currency_symbol', 'subtext' => $txt['paid_currency_symbol_desc'], 'size' => 8, 'force_div_id' => 'custom_currency_symbol_div'),
  82. array('check', 'paidsubs_test', 'subtext' => $txt['paidsubs_test_desc'], 'onclick' => 'return document.getElementById(\'paidsubs_test\').checked ? confirm(\'' . $txt['paidsubs_test_confirm'] . '\') : true;'),
  83. );
  84. // Now load all the other gateway settings.
  85. $gateways = loadPaymentGateways();
  86. foreach ($gateways as $gateway)
  87. {
  88. $gatewayClass = new $gateway['display_class']();
  89. $setting_data = $gatewayClass->getGatewaySettings();
  90. if (!empty($setting_data))
  91. {
  92. $config_vars[] = array('title', $gatewayClass->title, 'text_label' => (isset($txt['paidsubs_gateway_title_' . $gatewayClass->title]) ? $txt['paidsubs_gateway_title_' . $gatewayClass->title] : $gatewayClass->title));
  93. $config_vars = array_merge($config_vars, $setting_data);
  94. }
  95. }
  96. // Just searching?
  97. if ($return_config)
  98. return $config_vars;
  99. // Get the settings template fired up.
  100. require_once($sourcedir . '/ManageServer.php');
  101. // Some important context stuff
  102. $context['page_title'] = $txt['settings'];
  103. $context['sub_template'] = 'show_settings';
  104. $context['settings_message'] = '<span class="smalltext">' . $txt['paid_note'] . '</span>';
  105. $context[$context['admin_menu_name']]['current_subsection'] = 'settings';
  106. // Get the final touches in place.
  107. $context['post_url'] = $scripturl . '?action=admin;area=paidsubscribe;save;sa=settings';
  108. $context['settings_title'] = $txt['settings'];
  109. // We want javascript for our currency options.
  110. $context['settings_insert_below'] = '
  111. <script type="text/javascript"><!-- // --><![CDATA[
  112. function toggleOther()
  113. {
  114. var otherOn = document.getElementById("paid_currency").value == \'other\';
  115. var currencydd = document.getElementById("custom_currency_code_div_dd");
  116. if (otherOn)
  117. {
  118. document.getElementById("custom_currency_code_div").style.display = "";
  119. document.getElementById("custom_currency_symbol_div").style.display = "";
  120. if (currencydd)
  121. {
  122. document.getElementById("custom_currency_code_div_dd").style.display = "";
  123. document.getElementById("custom_currency_symbol_div_dd").style.display = "";
  124. }
  125. }
  126. else
  127. {
  128. document.getElementById("custom_currency_code_div").style.display = "none";
  129. document.getElementById("custom_currency_symbol_div").style.display = "none";
  130. if (currencydd)
  131. {
  132. document.getElementById("custom_currency_symbol_div_dd").style.display = "none";
  133. document.getElementById("custom_currency_code_div_dd").style.display = "none";
  134. }
  135. }
  136. }
  137. toggleOther();
  138. // ]]></script>';
  139. // Saving the settings?
  140. if (isset($_GET['save']))
  141. {
  142. checkSession();
  143. // Sort out the currency stuff.
  144. if ($_POST['paid_currency'] != 'other')
  145. {
  146. $_POST['paid_currency_code'] = $_POST['paid_currency'];
  147. $_POST['paid_currency_symbol'] = $txt[$_POST['paid_currency'] . '_symbol'];
  148. }
  149. unset($config_vars['dummy_currency']);
  150. saveDBSettings($config_vars);
  151. redirectexit('action=admin;area=paidsubscribe;sa=settings');
  152. }
  153. // Prepare the settings...
  154. prepareDBSettingContext($config_vars);
  155. }
  156. /**
  157. * View a list of all the current subscriptions
  158. * Requires the admin_forum permission.
  159. * Accessed from ?action=admin;area=paidsubscribe;sa=view.
  160. */
  161. function ViewSubscriptions()
  162. {
  163. global $context, $txt, $modSettings, $smcFunc, $sourcedir, $scripturl;
  164. // Not made the settings yet?
  165. if (empty($modSettings['paid_currency_symbol']))
  166. fatal_lang_error('paid_not_set_currency', false, $scripturl . '?action=admin;area=paidsubscribe;sa=settings');
  167. // Some basic stuff.
  168. $context['page_title'] = $txt['paid_subs_view'];
  169. loadSubscriptions();
  170. $listOptions = array(
  171. 'id' => 'subscription_list',
  172. 'items_per_page' => 20,
  173. 'base_href' => $scripturl . '?action=admin;area=paidsubscribe;sa=view',
  174. 'get_items' => array(
  175. 'function' => create_function('', '
  176. global $context;
  177. return $context[\'subscriptions\'];
  178. '),
  179. ),
  180. 'get_count' => array(
  181. 'function' => create_function('', '
  182. global $context;
  183. return count($context[\'subscriptions\']);
  184. '),
  185. ),
  186. 'no_items_label' => $txt['paid_none_yet'],
  187. 'columns' => array(
  188. 'name' => array(
  189. 'header' => array(
  190. 'value' => $txt['paid_name'],
  191. 'style' => 'text-align: left; width: 35%;',
  192. ),
  193. 'data' => array(
  194. 'function' => create_function('$rowData', '
  195. global $scripturl;
  196. return sprintf(\'<a href="%1$s?action=admin;area=paidsubscribe;sa=viewsub;sid=%2$s">%3$s</a>\', $scripturl, $rowData[\'id\'], $rowData[\'name\']);
  197. '),
  198. ),
  199. ),
  200. 'cost' => array(
  201. 'header' => array(
  202. 'value' => $txt['paid_cost'],
  203. 'style' => 'text-align: left;',
  204. ),
  205. 'data' => array(
  206. 'function' => create_function('$rowData', '
  207. global $context, $txt;
  208. return $rowData[\'flexible\'] ? \'<em>\' . $txt[\'flexible\'] . \'</em>\' : $rowData[\'cost\'] . \' / \' . $rowData[\'length\'];
  209. '),
  210. ),
  211. ),
  212. 'pending' => array(
  213. 'header' => array(
  214. 'value' => $txt['paid_pending'],
  215. 'style' => 'width: 18%;',
  216. ),
  217. 'data' => array(
  218. 'db_htmlsafe' => 'pending',
  219. 'style' => 'text-align: center;',
  220. ),
  221. ),
  222. 'finished' => array(
  223. 'header' => array(
  224. 'value' => $txt['paid_finished'],
  225. ),
  226. 'data' => array(
  227. 'db_htmlsafe' => 'finished',
  228. 'style' => 'text-align: center;',
  229. ),
  230. ),
  231. 'total' => array(
  232. 'header' => array(
  233. 'value' => $txt['paid_active'],
  234. ),
  235. 'data' => array(
  236. 'db_htmlsafe' => 'total',
  237. 'style' => 'text-align: center;',
  238. ),
  239. ),
  240. 'is_active' => array(
  241. 'header' => array(
  242. 'value' => $txt['paid_is_active'],
  243. ),
  244. 'data' => array(
  245. 'function' => create_function('$rowData', '
  246. global $context, $txt;
  247. return \'<span style="color: \' . ($rowData[\'active\'] ? \'green\' : \'red\') . \'">\' . ($rowData[\'active\'] ? $txt[\'yes\'] : $txt[\'no\']) . \'</span>\';
  248. '),
  249. 'style' => 'text-align: center;',
  250. ),
  251. ),
  252. 'modify' => array(
  253. 'data' => array(
  254. 'function' => create_function('$rowData', '
  255. global $context, $txt, $scripturl;
  256. return \'<a href="\' . $scripturl . \'?action=admin;area=paidsubscribe;sa=modify;sid=\' . $rowData[\'id\'] . \'">\' . $txt[\'modify\'] . \'</a>\';
  257. '),
  258. 'style' => 'text-align: center;',
  259. ),
  260. ),
  261. 'delete' => array(
  262. 'data' => array(
  263. 'function' => create_function('$rowData', '
  264. global $context, $txt, $scripturl;
  265. return \'<a href="\' . $scripturl . \'?action=admin;area=paidsubscribe;sa=modify;delete;sid=\' . $rowData[\'id\'] . \'">\' . $txt[\'delete\'] . \'</a>\';
  266. '),
  267. 'style' => 'text-align: center;',
  268. ),
  269. ),
  270. ),
  271. 'form' => array(
  272. 'href' => $scripturl . '?action=admin;area=paidsubscribe;sa=modify',
  273. ),
  274. 'additional_rows' => array(
  275. array(
  276. 'position' => 'below_table_data',
  277. 'value' => '
  278. <input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="button_submit" />
  279. ',
  280. 'style' => 'text-align: right;',
  281. ),
  282. ),
  283. );
  284. require_once($sourcedir . '/Subs-List.php');
  285. createList($listOptions);
  286. $context['sub_template'] = 'show_list';
  287. $context['default_list'] = 'subscription_list';
  288. }
  289. /**
  290. * Adding, editing and deleting subscriptions.
  291. * Accessed from ?action=admin;area=paidsubscribe;sa=modify.
  292. */
  293. function ModifySubscription()
  294. {
  295. global $context, $txt, $modSettings, $smcFunc;
  296. $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
  297. $context['action_type'] = $context['sub_id'] ? (isset($_REQUEST['delete']) ? 'delete' : 'edit') : 'add';
  298. // Setup the template.
  299. $context['sub_template'] = $context['action_type'] == 'delete' ? 'delete_subscription' : 'modify_subscription';
  300. $context['page_title'] = $txt['paid_' . $context['action_type'] . '_subscription'];
  301. // Delete it?
  302. if (isset($_POST['delete_confirm']) && isset($_REQUEST['delete']))
  303. {
  304. checkSession();
  305. $smcFunc['db_query']('delete_subscription', '
  306. DELETE FROM {db_prefix}subscriptions
  307. WHERE id_subscribe = {int:current_subscription}',
  308. array(
  309. 'current_subscription' => $context['sub_id'],
  310. )
  311. );
  312. redirectexit('action=admin;area=paidsubscribe;view');
  313. }
  314. // Saving?
  315. if (isset($_POST['save']))
  316. {
  317. checkSession();
  318. // Some cleaning...
  319. $isActive = isset($_POST['active']) ? 1 : 0;
  320. $isRepeatable = isset($_POST['repeatable']) ? 1 : 0;
  321. $allowpartial = isset($_POST['allow_partial']) ? 1 : 0;
  322. $reminder = isset($_POST['reminder']) ? (int) $_POST['reminder'] : 0;
  323. $emailComplete = strlen($_POST['emailcomplete']) > 10 ? trim($_POST['emailcomplete']) : '';
  324. // Is this a fixed one?
  325. if ($_POST['duration_type'] == 'fixed')
  326. {
  327. // Clean the span.
  328. $span = $_POST['span_value'] . $_POST['span_unit'];
  329. // Sort out the cost.
  330. $cost = array('fixed' => sprintf('%01.2f', strtr($_POST['cost'], ',', '.')));
  331. // There needs to be something.
  332. if (empty($_POST['span_value']) || empty($_POST['cost']))
  333. fatal_lang_error('paid_no_cost_value');
  334. }
  335. // Flexible is harder but more fun ;)
  336. else
  337. {
  338. $span = 'F';
  339. $cost = array(
  340. 'day' => sprintf('%01.2f', strtr($_POST['cost_day'], ',', '.')),
  341. 'week' => sprintf('%01.2f', strtr($_POST['cost_week'], ',', '.')),
  342. 'month' => sprintf('%01.2f', strtr($_POST['cost_month'], ',', '.')),
  343. 'year' => sprintf('%01.2f', strtr($_POST['cost_year'], ',', '.')),
  344. );
  345. if (empty($_POST['cost_day']) && empty($_POST['cost_week']) && empty($_POST['cost_month']) && empty($_POST['cost_year']))
  346. fatal_lang_error('paid_all_freq_blank');
  347. }
  348. $cost = serialize($cost);
  349. // Yep, time to do additional groups.
  350. $addgroups = array();
  351. if (!empty($_POST['addgroup']))
  352. foreach ($_POST['addgroup'] as $id => $dummy)
  353. $addgroups[] = (int) $id;
  354. $addgroups = implode(',', $addgroups);
  355. // Is it new?!
  356. if ($context['action_type'] == 'add')
  357. {
  358. $smcFunc['db_insert']('',
  359. '{db_prefix}subscriptions',
  360. array(
  361. 'name' => 'string-60', 'description' => 'string-255', 'active' => 'int', 'length' => 'string-4', 'cost' => 'string',
  362. 'id_group' => 'int', 'add_groups' => 'string-40', 'repeatable' => 'int', 'allow_partial' => 'int', 'email_complete' => 'string',
  363. 'reminder' => 'int',
  364. ),
  365. array(
  366. $_POST['name'], $_POST['desc'], $isActive, $span, $cost,
  367. $_POST['prim_group'], $addgroups, $isRepeatable, $allowpartial, $emailComplete,
  368. $reminder,
  369. ),
  370. array('id_subscribe')
  371. );
  372. }
  373. // Otherwise must be editing.
  374. else
  375. {
  376. // Don't do groups if there are active members
  377. $request = $smcFunc['db_query']('', '
  378. SELECT COUNT(*)
  379. FROM {db_prefix}log_subscribed
  380. WHERE id_subscribe = {int:current_subscription}
  381. AND status = {int:is_active}',
  382. array(
  383. 'current_subscription' => $context['sub_id'],
  384. 'is_active' => 1,
  385. )
  386. );
  387. list ($disableGroups) = $smcFunc['db_fetch_row']($request);
  388. $smcFunc['db_free_result']($request);
  389. $smcFunc['db_query']('substring', '
  390. UPDATE {db_prefix}subscriptions
  391. SET name = SUBSTRING({string:name}, 1, 60), description = SUBSTRING({string:description}, 1, 255), active = {int:is_active},
  392. length = SUBSTRING({string:length}, 1, 4), cost = {string:cost}' . ($disableGroups ? '' : ', id_group = {int:id_group},
  393. add_groups = {string:additional_groups}') . ', repeatable = {int:repeatable}, allow_partial = {int:allow_partial},
  394. email_complete = {string:email_complete}, reminder = {int:reminder}
  395. WHERE id_subscribe = {int:current_subscription}',
  396. array(
  397. 'is_active' => $isActive,
  398. 'id_group' => !empty($_POST['prim_group']) ? $_POST['prim_group'] : 0,
  399. 'repeatable' => $isRepeatable,
  400. 'allow_partial' => $allowpartial,
  401. 'reminder' => $reminder,
  402. 'current_subscription' => $context['sub_id'],
  403. 'name' => $_POST['name'],
  404. 'description' => $_POST['desc'],
  405. 'length' => $span,
  406. 'cost' => $cost,
  407. 'additional_groups' => !empty($addgroups) ? $addgroups : '',
  408. 'email_complete' => $emailComplete,
  409. )
  410. );
  411. }
  412. redirectexit('action=admin;area=paidsubscribe;view');
  413. }
  414. // Defaults.
  415. if ($context['action_type'] == 'add')
  416. {
  417. $context['sub'] = array(
  418. 'name' => '',
  419. 'desc' => '',
  420. 'cost' => array(
  421. 'fixed' => 0,
  422. ),
  423. 'span' => array(
  424. 'value' => '',
  425. 'unit' => 'D',
  426. ),
  427. 'prim_group' => 0,
  428. 'add_groups' => array(),
  429. 'active' => 1,
  430. 'repeatable' => 1,
  431. 'allow_partial' => 0,
  432. 'duration' => 'fixed',
  433. 'email_complete' => '',
  434. 'reminder' => 0,
  435. );
  436. }
  437. // Otherwise load up all the details.
  438. else
  439. {
  440. $request = $smcFunc['db_query']('', '
  441. SELECT name, description, cost, length, id_group, add_groups, active, repeatable, allow_partial, email_complete, reminder
  442. FROM {db_prefix}subscriptions
  443. WHERE id_subscribe = {int:current_subscription}
  444. LIMIT 1',
  445. array(
  446. 'current_subscription' => $context['sub_id'],
  447. )
  448. );
  449. while ($row = $smcFunc['db_fetch_assoc']($request))
  450. {
  451. // Sort the date.
  452. preg_match('~(\d*)(\w)~', $row['length'], $match);
  453. if (isset($match[2]))
  454. {
  455. $span_value = $match[1];
  456. $span_unit = $match[2];
  457. }
  458. else
  459. {
  460. $span_value = 0;
  461. $span_unit = 'D';
  462. }
  463. // Is this a flexible one?
  464. if ($row['length'] == 'F')
  465. $isFlexible = true;
  466. else
  467. $isFlexible = false;
  468. $context['sub'] = array(
  469. 'name' => $row['name'],
  470. 'desc' => $row['description'],
  471. 'cost' => @unserialize($row['cost']),
  472. 'span' => array(
  473. 'value' => $span_value,
  474. 'unit' => $span_unit,
  475. ),
  476. 'prim_group' => $row['id_group'],
  477. 'add_groups' => explode(',', $row['add_groups']),
  478. 'active' => $row['active'],
  479. 'repeatable' => $row['repeatable'],
  480. 'allow_partial' => $row['allow_partial'],
  481. 'duration' => $isFlexible ? 'flexible' : 'fixed',
  482. 'email_complete' => htmlspecialchars($row['email_complete']),
  483. 'reminder' => $row['reminder'],
  484. );
  485. }
  486. $smcFunc['db_free_result']($request);
  487. // Does this have members who are active?
  488. $request = $smcFunc['db_query']('', '
  489. SELECT COUNT(*)
  490. FROM {db_prefix}log_subscribed
  491. WHERE id_subscribe = {int:current_subscription}
  492. AND status = {int:is_active}',
  493. array(
  494. 'current_subscription' => $context['sub_id'],
  495. 'is_active' => 1,
  496. )
  497. );
  498. list ($context['disable_groups']) = $smcFunc['db_fetch_row']($request);
  499. $smcFunc['db_free_result']($request);
  500. }
  501. // Load up all the groups.
  502. $request = $smcFunc['db_query']('', '
  503. SELECT id_group, group_name
  504. FROM {db_prefix}membergroups
  505. WHERE id_group != {int:moderator_group}
  506. AND min_posts = {int:min_posts}',
  507. array(
  508. 'moderator_group' => 3,
  509. 'min_posts' => -1,
  510. )
  511. );
  512. $context['groups'] = array();
  513. while ($row = $smcFunc['db_fetch_assoc']($request))
  514. $context['groups'][$row['id_group']] = $row['group_name'];
  515. $smcFunc['db_free_result']($request);
  516. }
  517. /**
  518. * View all the users subscribed to a particular subscription.
  519. * Requires the admin_forum permission.
  520. * Accessed from ?action=admin;area=paidsubscribe;sa=viewsub.
  521. *
  522. * Subscription ID is required, in the form of $_GET['sid'].
  523. */
  524. function ViewSubscribedUsers()
  525. {
  526. global $context, $txt, $modSettings, $scripturl, $options, $smcFunc, $sourcedir;
  527. // Setup the template.
  528. $context['page_title'] = $txt['viewing_users_subscribed'];
  529. // ID of the subscription.
  530. $context['sub_id'] = (int) $_REQUEST['sid'];
  531. // Load the subscription information.
  532. $request = $smcFunc['db_query']('', '
  533. SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active
  534. FROM {db_prefix}subscriptions
  535. WHERE id_subscribe = {int:current_subscription}',
  536. array(
  537. 'current_subscription' => $context['sub_id'],
  538. )
  539. );
  540. // Something wrong?
  541. if ($smcFunc['db_num_rows']($request) == 0)
  542. fatal_lang_error('no_access', false);
  543. // Do the subscription context.
  544. $row = $smcFunc['db_fetch_assoc']($request);
  545. $context['subscription'] = array(
  546. 'id' => $row['id_subscribe'],
  547. 'name' => $row['name'],
  548. 'desc' => $row['description'],
  549. 'active' => $row['active'],
  550. );
  551. $smcFunc['db_free_result']($request);
  552. // Are we searching for people?
  553. $search_string = isset($_POST['ssearch']) && !empty($_POST['sub_search']) ? ' AND IFNULL(mem.real_name, {string:guest}) LIKE {string:search}' : '';
  554. $search_vars = empty($_POST['sub_search']) ? array() : array('search' => '%' . $_POST['sub_search'] . '%', 'guest' => $txt['guest']);
  555. $listOptions = array(
  556. 'id' => 'subscribed_users_list',
  557. 'items_per_page' => 20,
  558. 'base_href' => $scripturl . '?action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id'],
  559. 'default_sort_col' => 'name',
  560. 'get_items' => array(
  561. 'function' => 'list_getSubscribedUsers',
  562. 'params' => array(
  563. $context['sub_id'],
  564. $search_string,
  565. $search_vars,
  566. ),
  567. ),
  568. 'get_count' => array(
  569. 'function' => 'list_getSubscribedUserCount',
  570. 'params' => array(
  571. $context['sub_id'],
  572. $search_string,
  573. $search_vars,
  574. ),
  575. ),
  576. 'no_items_label' => $txt['no_subscribers'],
  577. 'columns' => array(
  578. 'name' => array(
  579. 'header' => array(
  580. 'value' => $txt['who_member'],
  581. 'style' => 'text-align: left; width: 20%;',
  582. ),
  583. 'data' => array(
  584. 'function' => create_function('$rowData', '
  585. global $context, $txt, $scripturl;
  586. return $rowData[\'id_member\'] == 0 ? $txt[\'guest\'] : \'<a href="\' . $scripturl . \'?action=profile;u=\' . $rowData[\'id_member\'] . \'">\' . $rowData[\'name\'] . \'</a>\';
  587. '),
  588. ),
  589. 'sort' => array(
  590. 'default' => 'name',
  591. 'reverse' => 'name DESC',
  592. ),
  593. ),
  594. 'status' => array(
  595. 'header' => array(
  596. 'value' => $txt['paid_status'],
  597. 'style' => 'text-align: left; width: 10%;',
  598. ),
  599. 'data' => array(
  600. 'db_htmlsafe' => 'status_text',
  601. ),
  602. 'sort' => array(
  603. 'default' => 'status',
  604. 'reverse' => 'status DESC',
  605. ),
  606. ),
  607. 'payments_pending' => array(
  608. 'header' => array(
  609. 'value' => $txt['paid_payments_pending'],
  610. 'style' => 'text-align: left; width: 10%;',
  611. ),
  612. 'data' => array(
  613. 'db_htmlsafe' => 'pending',
  614. ),
  615. 'sort' => array(
  616. 'default' => 'payments_pending',
  617. 'reverse' => 'payments_pending DESC',
  618. ),
  619. ),
  620. 'start_time' => array(
  621. 'header' => array(
  622. 'value' => $txt['start_date'],
  623. 'style' => 'text-align: left; width: 20%;',
  624. ),
  625. 'data' => array(
  626. 'db_htmlsafe' => 'start_date',
  627. 'class' => 'smalltext',
  628. ),
  629. 'sort' => array(
  630. 'default' => 'start_time',
  631. 'reverse' => 'start_time DESC',
  632. ),
  633. ),
  634. 'end_time' => array(
  635. 'header' => array(
  636. 'value' => $txt['end_date'],
  637. 'style' => 'text-align: left; width: 20%;',
  638. ),
  639. 'data' => array(
  640. 'db_htmlsafe' => 'end_date',
  641. 'class' => 'smalltext',
  642. ),
  643. 'sort' => array(
  644. 'default' => 'end_time',
  645. 'reverse' => 'end_time DESC',
  646. ),
  647. ),
  648. 'modify' => array(
  649. 'header' => array(
  650. 'style' => 'width: 10%;',
  651. ),
  652. 'data' => array(
  653. 'function' => create_function('$rowData', '
  654. global $context, $txt, $scripturl;
  655. return \'<a href="\' . $scripturl . \'?action=admin;area=paidsubscribe;sa=modifyuser;lid=\' . $rowData[\'id\'] . \'">\' . $txt[\'modify\'] . \'</a>\';
  656. '),
  657. 'style' => 'text-align: center;',
  658. ),
  659. ),
  660. 'delete' => array(
  661. 'header' => array(
  662. 'style' => 'width: 4%;',
  663. ),
  664. 'data' => array(
  665. 'function' => create_function('$rowData', '
  666. global $context, $txt, $scripturl;
  667. return \'<input type="checkbox" name="delsub[\' . $rowData[\'id\'] . \']" class="input_check" />\';
  668. '),
  669. 'style' => 'text-align: center;',
  670. ),
  671. ),
  672. ),
  673. 'form' => array(
  674. 'href' => $scripturl . '?action=admin;area=paidsubscribe;sa=modifyuser;sid=' . $context['sub_id'],
  675. ),
  676. 'additional_rows' => array(
  677. array(
  678. 'position' => 'below_table_data',
  679. 'value' => '
  680. <div class="floatleft">
  681. <input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="button_submit" />
  682. </div>
  683. <div class="floatright">
  684. <input type="submit" name="finished" value="' . $txt['complete_selected'] . '" onclick="return confirm(\'' . $txt['complete_are_sure'] . '\');" class="button_submit" />
  685. <input type="submit" name="delete" value="' . $txt['delete_selected'] . '" onclick="return confirm(\'' . $txt['delete_are_sure'] . '\');" class="button_submit" />
  686. </div>
  687. ',
  688. ),
  689. array(
  690. 'position' => 'top_of_list',
  691. 'value' => '
  692. <div class="title_bar">
  693. <h3 class="titlebg">' . sprintf($txt['view_users_subscribed'], $row['name']) . '</h3>
  694. </div>
  695. <div class="floatright">
  696. <input type="text" name="sub_search" value="" class="input_text" />
  697. <input type="submit" name="ssearch" value="' . $txt['search_sub'] . '" class="button_submit" />
  698. </div>
  699. ',
  700. ),
  701. ),
  702. );
  703. require_once($sourcedir . '/Subs-List.php');
  704. createList($listOptions);
  705. $context['sub_template'] = 'show_list';
  706. $context['default_list'] = 'subscribed_users_list';
  707. }
  708. /**
  709. * Returns how many people are subscribed to a paid subscription.
  710. * @todo refactor away
  711. *
  712. * @param int $id_sub
  713. * @param string $search_string
  714. * @param array $search_vars = array()
  715. */
  716. function list_getSubscribedUserCount($id_sub, $search_string, $search_vars = array())
  717. {
  718. global $smcFunc;
  719. // Get the total amount of users.
  720. $request = $smcFunc['db_query']('', '
  721. SELECT COUNT(*) AS total_subs
  722. FROM {db_prefix}log_subscribed AS ls
  723. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  724. WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
  725. AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_pending_payments})',
  726. array_merge($search_vars, array(
  727. 'current_subscription' => $id_sub,
  728. 'no_end_time' => 0,
  729. 'no_pending_payments' => 0,
  730. ))
  731. );
  732. list ($memberCount) = $smcFunc['db_fetch_row']($request);
  733. $smcFunc['db_free_result']($request);
  734. return $memberCount;
  735. }
  736. /**
  737. * Return the subscribed users list, for the given parameters.
  738. * @todo refactor outta here
  739. *
  740. * @param int $start
  741. * @param int $items_per_page
  742. * @param string $sort
  743. * @param int $id_sub
  744. * @param string $search_string
  745. * @param string $search_vars
  746. */
  747. function list_getSubscribedUsers($start, $items_per_page, $sort, $id_sub, $search_string, $search_vars = array())
  748. {
  749. global $smcFunc, $txt;
  750. $request = $smcFunc['db_query']('', '
  751. SELECT ls.id_sublog, IFNULL(mem.id_member, 0) AS id_member, IFNULL(mem.real_name, {string:guest}) AS name, ls.start_time, ls.end_time,
  752. ls.status, ls.payments_pending
  753. FROM {db_prefix}log_subscribed AS ls
  754. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  755. WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
  756. AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_payments_pending})
  757. ORDER BY ' . $sort . '
  758. LIMIT ' . $start . ', ' . $items_per_page,
  759. array_merge($search_vars, array(
  760. 'current_subscription' => $id_sub,
  761. 'no_end_time' => 0,
  762. 'no_payments_pending' => 0,
  763. 'guest' => $txt['guest'],
  764. ))
  765. );
  766. $subscribers = array();
  767. while ($row = $smcFunc['db_fetch_assoc']($request))
  768. $subscribers[] = array(
  769. 'id' => $row['id_sublog'],
  770. 'id_member' => $row['id_member'],
  771. 'name' => $row['name'],
  772. 'start_date' => timeformat($row['start_time'], false),
  773. 'end_date' => $row['end_time'] == 0 ? 'N/A' : timeformat($row['end_time'], false),
  774. 'pending' => $row['payments_pending'],
  775. 'status' => $row['status'],
  776. 'status_text' => $row['status'] == 0 ? ($row['payments_pending'] == 0 ? $txt['paid_finished'] : $txt['paid_pending']) : $txt['paid_active'],
  777. );
  778. $smcFunc['db_free_result']($request);
  779. return $subscribers;
  780. }
  781. /**
  782. * Edit or add a user subscription.
  783. * Accessed from ?action=admin;area=paidsubscribe;sa=modifyuser.
  784. */
  785. function ModifyUserSubscription()
  786. {
  787. global $context, $txt, $modSettings, $smcFunc;
  788. loadSubscriptions();
  789. $context['log_id'] = isset($_REQUEST['lid']) ? (int) $_REQUEST['lid'] : 0;
  790. $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
  791. $context['action_type'] = $context['log_id'] ? 'edit' : 'add';
  792. // Setup the template.
  793. $context['sub_template'] = 'modify_user_subscription';
  794. $context['page_title'] = $txt[$context['action_type'] . '_subscriber'];
  795. // If we haven't been passed the subscription ID get it.
  796. if ($context['log_id'] && !$context['sub_id'])
  797. {
  798. $request = $smcFunc['db_query']('', '
  799. SELECT id_subscribe
  800. FROM {db_prefix}log_subscribed
  801. WHERE id_sublog = {int:current_log_item}',
  802. array(
  803. 'current_log_item' => $context['log_id'],
  804. )
  805. );
  806. if ($smcFunc['db_num_rows']($request) == 0)
  807. fatal_lang_error('no_access', false);
  808. list ($context['sub_id']) = $smcFunc['db_fetch_row']($request);
  809. $smcFunc['db_free_result']($request);
  810. }
  811. if (!isset($context['subscriptions'][$context['sub_id']]))
  812. fatal_lang_error('no_access', false);
  813. $context['current_subscription'] = $context['subscriptions'][$context['sub_id']];
  814. // Searching?
  815. if (isset($_POST['ssearch']))
  816. {
  817. return ViewSubscribedUsers();
  818. }
  819. // Saving?
  820. elseif (isset($_REQUEST['save_sub']))
  821. {
  822. checkSession();
  823. // Work out the dates...
  824. $starttime = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'], $_POST['day'], $_POST['year']);
  825. $endtime = mktime($_POST['hourend'], $_POST['minuteend'], 0, $_POST['monthend'], $_POST['dayend'], $_POST['yearend']);
  826. // Status.
  827. $status = $_POST['status'];
  828. // New one?
  829. if (empty($context['log_id']))
  830. {
  831. // Find the user...
  832. $request = $smcFunc['db_query']('', '
  833. SELECT id_member, id_group
  834. FROM {db_prefix}members
  835. WHERE real_name = {string:name}
  836. LIMIT 1',
  837. array(
  838. 'name' => $_POST['name'],
  839. )
  840. );
  841. if ($smcFunc['db_num_rows']($request) == 0)
  842. fatal_lang_error('error_member_not_found');
  843. list ($id_member, $id_group) = $smcFunc['db_fetch_row']($request);
  844. $smcFunc['db_free_result']($request);
  845. // Ensure the member doesn't already have a subscription!
  846. $request = $smcFunc['db_query']('', '
  847. SELECT id_subscribe
  848. FROM {db_prefix}log_subscribed
  849. WHERE id_subscribe = {int:current_subscription}
  850. AND id_member = {int:current_member}',
  851. array(
  852. 'current_subscription' => $context['sub_id'],
  853. 'current_member' => $id_member,
  854. )
  855. );
  856. if ($smcFunc['db_num_rows']($request) != 0)
  857. fatal_lang_error('member_already_subscribed');
  858. $smcFunc['db_free_result']($request);
  859. // Actually put the subscription in place.
  860. if ($status == 1)
  861. addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
  862. else
  863. {
  864. $smcFunc['db_insert']('',
  865. '{db_prefix}log_subscribed',
  866. array(
  867. 'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
  868. 'end_time' => 'int', 'status' => 'int',
  869. ),
  870. array(
  871. $context['sub_id'], $id_member, $id_group, $starttime,
  872. $endtime, $status,
  873. ),
  874. array('id_sublog')
  875. );
  876. }
  877. }
  878. // Updating.
  879. else
  880. {
  881. $request = $smcFunc['db_query']('', '
  882. SELECT id_member, status
  883. FROM {db_prefix}log_subscribed
  884. WHERE id_sublog = {int:current_log_item}',
  885. array(
  886. 'current_log_item' => $context['log_id'],
  887. )
  888. );
  889. if ($smcFunc['db_num_rows']($request) == 0)
  890. fatal_lang_error('no_access', false);
  891. list ($id_member, $old_status) = $smcFunc['db_fetch_row']($request);
  892. $smcFunc['db_free_result']($request);
  893. // Pick the right permission stuff depending on what the status is changing from/to.
  894. if ($old_status == 1 && $status != 1)
  895. removeSubscription($context['sub_id'], $id_member);
  896. elseif ($status == 1 && $old_status != 1)
  897. {
  898. addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
  899. }
  900. else
  901. {
  902. $smcFunc['db_query']('', '
  903. UPDATE {db_prefix}log_subscribed
  904. SET start_time = {int:start_time}, end_time = {int:end_time}, status = {int:status}
  905. WHERE id_sublog = {int:current_log_item}',
  906. array(
  907. 'start_time' => $starttime,
  908. 'end_time' => $endtime,
  909. 'status' => $status,
  910. 'current_log_item' => $context['log_id'],
  911. )
  912. );
  913. }
  914. }
  915. // Done - redirect...
  916. redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
  917. }
  918. // Deleting?
  919. elseif (isset($_REQUEST['delete']) || isset($_REQUEST['finished']))
  920. {
  921. checkSession();
  922. // Do the actual deletes!
  923. if (!empty($_REQUEST['delsub']))
  924. {
  925. $toDelete = array();
  926. foreach ($_REQUEST['delsub'] as $id => $dummy)
  927. $toDelete[] = (int) $id;
  928. $request = $smcFunc['db_query']('', '
  929. SELECT id_subscribe, id_member
  930. FROM {db_prefix}log_subscribed
  931. WHERE id_sublog IN ({array_int:subscription_list})',
  932. array(
  933. 'subscription_list' => $toDelete,
  934. )
  935. );
  936. while ($row = $smcFunc['db_fetch_assoc']($request))
  937. removeSubscription($row['id_subscribe'], $row['id_member'], isset($_REQUEST['delete']));
  938. $smcFunc['db_free_result']($request);
  939. }
  940. redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
  941. }
  942. // Default attributes.
  943. if ($context['action_type'] == 'add')
  944. {
  945. $context['sub'] = array(
  946. 'id' => 0,
  947. 'start' => array(
  948. 'year' => (int) strftime('%Y', time()),
  949. 'month' => (int) strftime('%m', time()),
  950. 'day' => (int) strftime('%d', time()),
  951. 'hour' => (int) strftime('%H', time()),
  952. 'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
  953. 'last_day' => 0,
  954. ),
  955. 'end' => array(
  956. 'year' => (int) strftime('%Y', time()),
  957. 'month' => (int) strftime('%m', time()),
  958. 'day' => (int) strftime('%d', time()),
  959. 'hour' => (int) strftime('%H', time()),
  960. 'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
  961. 'last_day' => 0,
  962. ),
  963. 'status' => 1,
  964. );
  965. $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']));
  966. $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']));
  967. if (isset($_GET['uid']))
  968. {
  969. $request = $smcFunc['db_query']('', '
  970. SELECT real_name
  971. FROM {db_prefix}members
  972. WHERE id_member = {int:current_member}',
  973. array(
  974. 'current_member' => (int) $_GET['uid'],
  975. )
  976. );
  977. list ($context['sub']['username']) = $smcFunc['db_fetch_row']($request);
  978. $smcFunc['db_free_result']($request);
  979. }
  980. else
  981. $context['sub']['username'] = '';
  982. }
  983. // Otherwise load the existing info.
  984. else
  985. {
  986. $request = $smcFunc['db_query']('', '
  987. SELECT ls.id_sublog, ls.id_subscribe, ls.id_member, start_time, end_time, status, payments_pending, pending_details,
  988. IFNULL(mem.real_name, {string:blank_string}) AS username
  989. FROM {db_prefix}log_subscribed AS ls
  990. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  991. WHERE ls.id_sublog = {int:current_subscription_item}
  992. LIMIT 1',
  993. array(
  994. 'current_subscription_item' => $context['log_id'],
  995. 'blank_string' => '',
  996. )
  997. );
  998. if ($smcFunc['db_num_rows']($request) == 0)
  999. fatal_lang_error('no_access', false);
  1000. $row = $smcFunc['db_fetch_assoc']($request);
  1001. $smcFunc['db_free_result']($request);
  1002. // Any pending payments?
  1003. $context['pending_payments'] = array();
  1004. if (!empty($row['pending_details']))
  1005. {
  1006. $pending_details = @unserialize($row['pending_details']);
  1007. foreach ($pending_details as $id => $pending)
  1008. {
  1009. // Only this type need be displayed.
  1010. if ($pending[3] == 'payback')
  1011. {
  1012. // Work out what the options were.
  1013. $costs = @unserialize($context['current_subscription']['real_cost']);
  1014. if ($context['current_subscription']['real_length'] == 'F')
  1015. {
  1016. foreach ($costs as $duration => $cost)
  1017. {
  1018. if ($cost != 0 && $cost == $pending[1] && $duration == $pending[2])
  1019. $context['pending_payments'][$id] = array(
  1020. 'desc' => sprintf($modSettings['paid_currency_symbol'], $cost . '/' . $txt[$duration]),
  1021. );
  1022. }
  1023. }
  1024. elseif ($costs['fixed'] == $pending[1])
  1025. {
  1026. $context['pending_payments'][$id] = array(
  1027. 'desc' => sprintf($modSettings['paid_currency_symbol'], $costs['fixed']),
  1028. );
  1029. }
  1030. }
  1031. }
  1032. // Check if we are adding/removing any.
  1033. if (isset($_GET['pending']))
  1034. {
  1035. foreach ($pending_details as $id => $pending)
  1036. {
  1037. // Found the one to action?
  1038. if ($_GET['pending'] == $id && $pending[3] == 'payback' && isset($context['pending_payments'][$id]))
  1039. {
  1040. // Flexible?
  1041. if (isset($_GET['accept']))
  1042. addSubscription($context['current_subscription']['id'], $row['id_member'], $context['current_subscription']['real_length'] == 'F' ? strtoupper(substr($pending[2], 0, 1)) : 0);
  1043. unset($pending_details[$id]);
  1044. $new_details = serialize($pending_details);
  1045. // Update the entry.
  1046. $smcFunc['db_query']('', '
  1047. UPDATE {db_prefix}log_subscribed
  1048. SET payments_pending = payments_pending - 1, pending_details = {string:pending_details}
  1049. WHERE id_sublog = {int:current_subscription_item}',
  1050. array(
  1051. 'current_subscription_item' => $context['log_id'],
  1052. 'pending_details' => $new_details,
  1053. )
  1054. );
  1055. // Reload
  1056. redirectexit('action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $context['log_id']);
  1057. }
  1058. }
  1059. }
  1060. }
  1061. $context['sub_id'] = $row['id_subscribe'];
  1062. $context['sub'] = array(
  1063. 'id' => 0,
  1064. 'start' => array(
  1065. 'year' => (int) strftime('%Y', $row['start_time']),
  1066. 'month' => (int) strftime('%m', $row['start_time']),
  1067. 'day' => (int) strftime('%d', $row['start_time']),
  1068. 'hour' => (int) strftime('%H', $row['start_time']),
  1069. 'min' => (int) strftime('%M', $row['start_time']) < 10 ? '0' . (int) strftime('%M', $row['start_time']) : (int) strftime('%M', $row['start_time']),
  1070. 'last_day' => 0,
  1071. ),
  1072. 'end' => array(
  1073. 'year' => (int) strftime('%Y', $row['end_time']),
  1074. 'month' => (int) strftime('%m', $row['end_time']),
  1075. 'day' => (int) strftime('%d', $row['end_time']),
  1076. 'hour' => (int) strftime('%H', $row['end_time']),
  1077. 'min' => (int) strftime('%M', $row['end_time']) < 10 ? '0' . (int) strftime('%M', $row['end_time']) : (int) strftime('%M', $row['end_time']),
  1078. 'last_day' => 0,
  1079. ),
  1080. 'status' => $row['status'],
  1081. 'username' => $row['username'],
  1082. );
  1083. $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']));
  1084. $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']));
  1085. }
  1086. }
  1087. /**
  1088. * Reapplies all subscription rules for each of the users.
  1089. *
  1090. * @param array $users
  1091. */
  1092. function reapplySubscriptions($users)
  1093. {
  1094. global $smcFunc;
  1095. // Make it an array.
  1096. if (!is_array($users))
  1097. $users = array($users);
  1098. // Get all the members current groups.
  1099. $groups = array();
  1100. $request = $smcFunc['db_query']('', '
  1101. SELECT id_member, id_group, additional_groups
  1102. FROM {db_prefix}members
  1103. WHERE id_member IN ({array_int:user_list})',
  1104. array(
  1105. 'user_list' => $users,
  1106. )
  1107. );
  1108. while ($row = $smcFunc['db_fetch_assoc']($request))
  1109. {
  1110. $groups[$row['id_member']] = array(
  1111. 'primary' => $row['id_group'],
  1112. 'additional' => explode(',', $row['additional_groups']),
  1113. );
  1114. }
  1115. $smcFunc['db_free_result']($request);
  1116. $request = $smcFunc['db_query']('', '
  1117. SELECT ls.id_member, ls.old_id_group, s.id_group, s.add_groups
  1118. FROM {db_prefix}log_subscribed AS ls
  1119. INNER JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
  1120. WHERE ls.id_member IN ({array_int:user_list})
  1121. AND ls.end_time > {int:current_time}',
  1122. array(
  1123. 'user_list' => $users,
  1124. 'current_time' => time(),
  1125. )
  1126. );
  1127. while ($row = $smcFunc['db_fetch_assoc']($request))
  1128. {
  1129. // Specific primary group?
  1130. if ($row['id_group'] != 0)
  1131. {
  1132. // If this is changing - add the old one to the additional groups so it's not lost.
  1133. if ($row['id_group'] != $groups[$row['id_member']]['primary'])
  1134. $groups[$row['id_member']]['additional'][] = $groups[$row['id_member']]['primary'];
  1135. $groups[$row['id_member']]['primary'] = $row['id_group'];
  1136. }
  1137. // Additional groups.
  1138. if (!empty($row['add_groups']))
  1139. $groups[$row['id_member']]['additional'] = array_merge($groups[$row['id_member']]['additional'], explode(',', $row['add_groups']));
  1140. }
  1141. $smcFunc['db_free_result']($request);
  1142. // Update all the members.
  1143. foreach ($groups as $id => $group)
  1144. {
  1145. $group['additional'] = array_unique($group['additional']);
  1146. foreach ($group['additional'] as $key => $value)
  1147. if (empty($value))
  1148. unset($group['additional'][$key]);
  1149. $addgroups = implode(',', $group['additional']);
  1150. $smcFunc['db_query']('', '
  1151. UPDATE {db_prefix}members
  1152. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  1153. WHERE id_member = {int:current_member}
  1154. LIMIT 1',
  1155. array(
  1156. 'primary_group' => $group['primary'],
  1157. 'current_member' => $id,
  1158. 'additional_groups' => $addgroups,
  1159. )
  1160. );
  1161. }
  1162. }
  1163. /**
  1164. * Add or extend a subscription of a user.
  1165. *
  1166. * @param int $id_subscribe
  1167. * @param int $id_member
  1168. * @param string $renewal = 0, options 'D', 'W', 'M', 'Y'
  1169. * @param int $forceStartTime = 0
  1170. * @param int $forceEndTime = 0
  1171. */
  1172. function addSubscription($id_subscribe, $id_member, $renewal = 0, $forceStartTime = 0, $forceEndTime = 0)
  1173. {
  1174. global $context, $smcFunc;
  1175. // Take the easy way out...
  1176. loadSubscriptions();
  1177. // Exists, yes?
  1178. if (!isset($context['subscriptions'][$id_subscribe]))
  1179. return;
  1180. $curSub = $context['subscriptions'][$id_subscribe];
  1181. // Grab the duration.
  1182. $duration = $curSub['num_length'];
  1183. // If this is a renewal change the duration to be correct.
  1184. if (!empty($renewal))
  1185. {
  1186. switch ($renewal)
  1187. {
  1188. case 'D':
  1189. $duration = 86400;
  1190. break;
  1191. case 'W':
  1192. $duration = 604800;
  1193. break;
  1194. case 'M':
  1195. $duration = 2629743;
  1196. break;
  1197. case 'Y':
  1198. $duration = 31556926;
  1199. break;
  1200. default:
  1201. break;
  1202. }
  1203. }
  1204. // Firstly, see whether it exists, and is active. If so then this is meerly an extension.
  1205. $request = $smcFunc['db_query']('', '
  1206. SELECT id_sublog, end_time, start_time
  1207. FROM {db_prefix}log_subscribed
  1208. WHERE id_subscribe = {int:current_subscription}
  1209. AND id_member = {int:current_member}
  1210. AND status = {int:is_active}',
  1211. array(
  1212. 'current_subscription' => $id_subscribe,
  1213. 'current_member' => $id_member,
  1214. 'is_active' => 1,
  1215. )
  1216. );
  1217. if ($smcFunc['db_num_rows']($request) != 0)
  1218. {
  1219. list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);
  1220. // If this has already expired but is active, extension means the period from now.
  1221. if ($endtime < time())
  1222. $endtime = time();
  1223. if ($starttime == 0)
  1224. $starttime = time();
  1225. // Work out the new expiry date.
  1226. $endtime += $duration;
  1227. if ($forceEndTime != 0)
  1228. $endtime = $forceEndTime;
  1229. // As everything else should be good, just update!
  1230. $smcFunc['db_query']('', '
  1231. UPDATE {db_prefix}log_subscribed
  1232. SET end_time = {int:end_time}, start_time = {int:start_time}
  1233. WHERE id_sublog = {int:current_subscription_item}',
  1234. array(
  1235. 'end_time' => $endtime,
  1236. 'start_time' => $starttime,
  1237. 'current_subscription_item' => $id_sublog,
  1238. )
  1239. );
  1240. return;
  1241. }
  1242. $smcFunc['db_free_result']($request);
  1243. // If we're here, that means we don't have an active subscription - that means we need to do some work!
  1244. $request = $smcFunc['db_query']('', '
  1245. SELECT m.id_group, m.additional_groups
  1246. FROM {db_prefix}members AS m
  1247. WHERE m.id_member = {int:current_member}',
  1248. array(
  1249. 'current_member' => $id_member,
  1250. )
  1251. );
  1252. // Just in case the member doesn't exist.
  1253. if ($smcFunc['db_num_rows']($request) == 0)
  1254. return;
  1255. list ($old_id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
  1256. $smcFunc['db_free_result']($request);
  1257. // Prepare additional groups.
  1258. $newAddGroups = explode(',', $curSub['add_groups']);
  1259. $curAddGroups = explode(',', $additional_groups);
  1260. $newAddGroups = array_merge($newAddGroups, $curAddGroups);
  1261. // Simple, simple, simple - hopefully... id_group first.
  1262. if ($curSub['prim_group'] != 0)
  1263. {
  1264. $id_group = $curSub['prim_group'];
  1265. // Ensure their old privileges are maintained.
  1266. if ($old_id_group != 0)
  1267. $newAddGroups[] = $old_id_group;
  1268. }
  1269. else
  1270. $id_group = $old_id_group;
  1271. // Yep, make sure it's unique, and no empties.
  1272. foreach ($newAddGroups as $k => $v)
  1273. if (empty($v))
  1274. unset($newAddGroups[$k]);
  1275. $newAddGroups = array_unique($newAddGroups);
  1276. $newAddGroups = implode(',', $newAddGroups);
  1277. // Store the new settings.
  1278. $smcFunc['db_query']('', '
  1279. UPDATE {db_prefix}members
  1280. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  1281. WHERE id_member = {int:current_member}',
  1282. array(
  1283. 'primary_group' => $id_group,
  1284. 'current_member' => $id_member,
  1285. 'additional_groups' => $newAddGroups,
  1286. )
  1287. );
  1288. // Now log the subscription - maybe we have a dorment subscription we can restore?
  1289. $request = $smcFunc['db_query']('', '
  1290. SELECT id_sublog, end_time, start_time
  1291. FROM {db_prefix}log_subscribed
  1292. WHERE id_subscribe = {int:current_subscription}
  1293. AND id_member = {int:current_member}',
  1294. array(
  1295. 'current_subscription' => $id_subscribe,
  1296. 'current_member' => $id_member,
  1297. )
  1298. );
  1299. /**
  1300. * @todo Don't really need to do this twice...
  1301. */
  1302. if ($smcFunc['db_num_rows']($request) != 0)
  1303. {
  1304. list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);
  1305. // If this has already expired but is active, extension means the period from now.
  1306. if ($endtime < time())
  1307. $endtime = time();
  1308. if ($starttime == 0)
  1309. $starttime = time();
  1310. // Work out the new expiry date.
  1311. $endtime += $duration;
  1312. if ($forceEndTime != 0)
  1313. $endtime = $forceEndTime;
  1314. // As everything else should be good, just update!
  1315. $smcFunc['db_query']('', '
  1316. UPDATE {db_prefix}log_subscribed
  1317. SET start_time = {int:start_time}, end_time = {int:end_time}, old_id_group = {int:old_id_group}, status = {int:is_active},
  1318. reminder_sent = {int:no_reminder_sent}
  1319. WHERE id_sublog = {int:current_subscription_item}',
  1320. array(
  1321. 'start_time' => $starttime,
  1322. 'end_time' => $endtime,
  1323. 'old_id_group' => $old_id_group,
  1324. 'is_active' => 1,
  1325. 'no_reminder_sent' => 0,
  1326. 'current_subscription_item' => $id_sublog,
  1327. )
  1328. );
  1329. return;
  1330. }
  1331. $smcFunc['db_free_result']($request);
  1332. // Otherwise a very simple insert.
  1333. $endtime = time() + $duration;
  1334. if ($forceEndTime != 0)
  1335. $endtime = $forceEndTime;
  1336. if ($forceStartTime == 0)
  1337. $starttime = time();
  1338. else
  1339. $starttime = $forceStartTime;
  1340. $smcFunc['db_insert']('',
  1341. '{db_prefix}log_subscribed',
  1342. array(
  1343. 'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
  1344. 'end_time' => 'int', 'status' => 'int', 'pending_details' => 'string',
  1345. ),
  1346. array(
  1347. $id_subscribe, $id_member, $old_id_group, $starttime,
  1348. $endtime, 1, '',
  1349. ),
  1350. array('id_sublog')
  1351. );
  1352. }
  1353. /**
  1354. * Removes a subscription from a user, as in removes the groups.
  1355. *
  1356. * @param $id_subscribe
  1357. * @param $id_member
  1358. * @param $delete
  1359. */
  1360. function removeSubscription($id_subscribe, $id_member, $delete = false)
  1361. {
  1362. global $context, $smcFunc;
  1363. loadSubscriptions();
  1364. // Load the user core bits.
  1365. $request = $smcFunc['db_query']('', '
  1366. SELECT m.id_group, m.additional_groups
  1367. FROM {db_prefix}members AS m
  1368. WHERE m.id_member = {int:current_member}',
  1369. array(
  1370. 'current_member' => $id_member,
  1371. )
  1372. );
  1373. // Just in case of errors.
  1374. if ($smcFunc['db_num_rows']($request) == 0)
  1375. {
  1376. $smcFunc['db_query']('', '
  1377. DELETE FROM {db_prefix}log_subscribed
  1378. WHERE id_member = {int:current_member}',
  1379. array(
  1380. 'current_member' => $id_member,
  1381. )
  1382. );
  1383. return;
  1384. }
  1385. list ($id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
  1386. $smcFunc['db_free_result']($request);
  1387. // Get all of the subscriptions for this user that are active - it will be necessary!
  1388. $request = $smcFunc['db_query']('', '
  1389. SELECT id_subscribe, old_id_group
  1390. FROM {db_prefix}log_subscribed
  1391. WHERE id_member = {int:current_member}
  1392. AND status = {int:is_active}',
  1393. array(
  1394. 'current_member' => $id_member,
  1395. 'is_active' => 1,
  1396. )
  1397. );
  1398. // These variables will be handy, honest ;)
  1399. $removals = array();
  1400. $allowed = array();
  1401. $old_id_group = 0;
  1402. $new_id_group = -1;
  1403. while ($row = $smcFunc['db_fetch_assoc']($request))
  1404. {
  1405. if (!isset($context['subscriptions'][$row['id_subscribe']]))
  1406. continue;
  1407. // The one we're removing?
  1408. if ($row['id_subscribe'] == $id_subscribe)
  1409. {
  1410. $removals = explode(',', $context['subscriptions'][$row['id_subscribe']]['add_groups']);
  1411. if ($context['subscriptions'][$row['id_subscribe']]['prim_group'] != 0)
  1412. $removals[] = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
  1413. $old_id_group = $row['old_id_group'];
  1414. }
  1415. // Otherwise things we allow.
  1416. else
  1417. {
  1418. $allowed = array_merge($allowed, explode(',', $context['subscriptions'][$row['id_subscribe']]['add_groups']));
  1419. if ($context['subscriptions'][$row['id_subscribe']]['prim_group'] != 0)
  1420. {
  1421. $allowed[] = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
  1422. $new_id_group = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
  1423. }
  1424. }
  1425. }
  1426. $smcFunc['db_free_result']($request);
  1427. // Now, for everything we are removing check they defintely are not allowed it.
  1428. $existingGroups = explode(',', $additional_groups);
  1429. foreach ($existingGroups as $key => $group)
  1430. if (empty($group) || (in_array($group, $removals) && !in_array($group, $allowed)))
  1431. unset($existingGroups[$key]);
  1432. // Finally, do something with the current primary group.
  1433. if (in_array($id_group, $removals))
  1434. {
  1435. // If this primary group is actually allowed keep it.
  1436. if (in_array($id_group, $allowed))
  1437. $existingGroups[] = $id_group;
  1438. // Either way, change the id_group back.
  1439. if ($new_id_group < 1)
  1440. {
  1441. // If we revert to the old id-group we need to ensure it wasn't from a subscription.
  1442. foreach ($context['subscriptions'] as $id => $group)
  1443. // It was? Make them a regular member then!
  1444. if ($group['prim_group'] == $old_id_group)
  1445. $old_id_group = 0;
  1446. $id_group = $old_id_group;
  1447. }
  1448. else
  1449. $id_group = $new_id_group;
  1450. }
  1451. // Crazy stuff, we seem to have our groups fixed, just make them unique
  1452. $existingGroups = array_unique($existingGroups);
  1453. $existingGroups = implode(',', $existingGroups);
  1454. // Update the member
  1455. $smcFunc['db_query']('', '
  1456. UPDATE {db_prefix}members
  1457. SET id_group = {int:primary_group}, additional_groups = {string:existing_groups}
  1458. WHERE id_member = {int:current_member}',
  1459. array(
  1460. 'primary_group' => $id_group,
  1461. 'current_member' => $id_member,
  1462. 'existing_groups' => $existingGroups,
  1463. )
  1464. );
  1465. // Disable the subscription.
  1466. if (!$delete)
  1467. $smcFunc['db_query']('', '
  1468. UPDATE {db_prefix}log_subscribed
  1469. SET status = {int:not_active}
  1470. WHERE id_member = {int:current_member}
  1471. AND id_subscribe = {int:current_subscription}',
  1472. array(
  1473. 'not_active' => 0,
  1474. 'current_member' => $id_member,
  1475. 'current_subscription' => $id_subscribe,
  1476. )
  1477. );
  1478. // Otherwise delete it!
  1479. else
  1480. $smcFunc['db_query']('', '
  1481. DELETE FROM {db_prefix}log_subscribed
  1482. WHERE id_member = {int:current_member}
  1483. AND id_subscribe = {int:current_subscription}',
  1484. array(
  1485. 'current_member' => $id_member,
  1486. 'current_subscription' => $id_subscribe,
  1487. )
  1488. );
  1489. }
  1490. /**
  1491. * This just kind of caches all the subscription data.
  1492. */
  1493. function loadSubscriptions()
  1494. {
  1495. global $context, $txt, $modSettings, $smcFunc;
  1496. if (!empty($context['subscriptions']))
  1497. return;
  1498. // Make sure this is loaded, just in case.
  1499. loadLanguage('ManagePaid');
  1500. $request = $smcFunc['db_query']('', '
  1501. SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active, repeatable
  1502. FROM {db_prefix}subscriptions',
  1503. array(
  1504. )
  1505. );
  1506. $context['subscriptions'] = array();
  1507. while ($row = $smcFunc['db_fetch_assoc']($request))
  1508. {
  1509. // Pick a cost.
  1510. $costs = @unserialize($row['cost']);
  1511. if ($row['length'] != 'F' && !empty($modSettings['paid_currency_symbol']) && !empty($costs['fixed']))
  1512. $cost = sprintf($modSettings['paid_currency_symbol'], $costs['fixed']);
  1513. else
  1514. $cost = '???';
  1515. // Do the span.
  1516. preg_match('~(\d*)(\w)~', $row['length'], $match);
  1517. if (isset($match[2]))
  1518. {
  1519. $num_length = $match[1];
  1520. $length = $match[1] . ' ';
  1521. switch ($match[2])
  1522. {
  1523. case 'D':
  1524. $length .= $txt['paid_mod_span_days'];
  1525. $num_length *= 86400;
  1526. break;
  1527. case 'W':
  1528. $length .= $txt['paid_mod_span_weeks'];
  1529. $num_length *= 604800;
  1530. break;
  1531. case 'M':
  1532. $length .= $txt['paid_mod_span_months'];
  1533. $num_length *= 2629743;
  1534. break;
  1535. case 'Y':
  1536. $length .= $txt['paid_mod_span_years'];
  1537. $num_length *= 31556926;
  1538. break;
  1539. }
  1540. }
  1541. else
  1542. $length = '??';
  1543. $context['subscriptions'][$row['id_subscribe']] = array(
  1544. 'id' => $row['id_subscribe'],
  1545. 'name' => $row['name'],
  1546. 'desc' => $row['description'],
  1547. 'cost' => $cost,
  1548. 'real_cost' => $row['cost'],
  1549. 'length' => $length,
  1550. 'num_length' => $num_length,
  1551. 'real_length' => $row['length'],
  1552. 'pending' => 0,
  1553. 'finished' => 0,
  1554. 'total' => 0,
  1555. 'active' => $row['active'],
  1556. 'prim_group' => $row['id_group'],
  1557. 'add_groups' => $row['add_groups'],
  1558. 'flexible' => $row['length'] == 'F' ? true : false,
  1559. 'repeatable' => $row['repeatable'],
  1560. );
  1561. }
  1562. $smcFunc['db_free_result']($request);
  1563. // Do the counts.
  1564. $request = $smcFunc['db_query']('', '
  1565. SELECT COUNT(id_sublog) AS member_count, id_subscribe, status
  1566. FROM {db_prefix}log_subscribed
  1567. GROUP BY id_subscribe, status',
  1568. array(
  1569. )
  1570. );
  1571. while ($row = $smcFunc['db_fetch_assoc']($request))
  1572. {
  1573. $ind = $row['status'] == 0 ? 'finished' : 'total';
  1574. if (isset($context['subscriptions'][$row['id_subscribe']]))
  1575. $context['subscriptions'][$row['id_subscribe']][$ind] = $row['member_count'];
  1576. }
  1577. $smcFunc['db_free_result']($request);
  1578. // How many payments are we waiting on?
  1579. $request = $smcFunc['db_query']('', '
  1580. SELECT SUM(payments_pending) AS total_pending, id_subscribe
  1581. FROM {db_prefix}log_subscribed
  1582. GROUP BY id_subscribe',
  1583. array(
  1584. )
  1585. );
  1586. while ($row = $smcFunc['db_fetch_assoc']($request))
  1587. {
  1588. if (isset($context['subscriptions'][$row['id_subscribe']]))
  1589. $context['subscriptions'][$row['id_subscribe']]['pending'] = $row['total_pending'];
  1590. }
  1591. $smcFunc['db_free_result']($request);
  1592. }
  1593. /**
  1594. * Load all the payment gateways.
  1595. * Checks the Sources directory for any files fitting the format of a payment gateway,
  1596. * loads each file to check it's valid, includes each file and returns the
  1597. * function name and whether it should work with this version of SMF.
  1598. *
  1599. * @return array
  1600. */
  1601. function loadPaymentGateways()
  1602. {
  1603. global $sourcedir;
  1604. $gateways = array();
  1605. if ($dh = opendir($sourcedir))
  1606. {
  1607. while (($file = readdir($dh)) !== false)
  1608. {
  1609. if (is_file($sourcedir .'/'. $file) && preg_match('~Subscriptions-([A-Za-z\d]+)\.php~', $file, $matches))
  1610. {
  1611. // Check this is definitely a valid gateway!
  1612. $fp = fopen($sourcedir . '/' . $file, 'rb');
  1613. $header = fread($fp, 4096);
  1614. fclose($fp);
  1615. if (strpos($header, '// SMF Payment Gateway: ' . strtolower($matches[1])) !== false)
  1616. {
  1617. loadClassFile($file);
  1618. $gateways[] = array(
  1619. 'filename' => $file,
  1620. 'code' => strtolower($matches[1]),
  1621. // Don't need anything snazier than this yet.
  1622. 'valid_version' => class_exists(strtolower($matches[1]) . '_payment') && class_exists(strtolower($matches[1]) . '_display'),
  1623. 'payment_class' => strtolower($matches[1]) . '_payment',
  1624. 'display_class' => strtolower($matches[1]) . '_display',
  1625. );
  1626. }
  1627. }
  1628. }
  1629. }
  1630. closedir($dh);
  1631. return $gateways;
  1632. }
  1633. ?>