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