ManagePaid.php 55 KB

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