ManagePaid.php 55 KB

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