ManagePaid.php 55 KB

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