2
0

ManagePaid.php 57 KB

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