ManagePaid.php 55 KB

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