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 2012 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: 15%;',
  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. <input type="submit" name="add" value="' . $txt['add_subscriber'] . '" class="button_submit" />
  690. <input type="submit" name="finished" value="' . $txt['complete_selected'] . '" onclick="return confirm(\'' . $txt['complete_are_sure'] . '\');" class="button_submit" />
  691. <input type="submit" name="delete" value="' . $txt['delete_selected'] . '" onclick="return confirm(\'' . $txt['delete_are_sure'] . '\');" class="button_submit" />
  692. ',
  693. ),
  694. array(
  695. 'position' => 'top_of_list',
  696. 'value' => '
  697. <div class="flow_auto">
  698. <input type="submit" name="ssearch" value="' . $txt['search_sub'] . '" class="button_submit" style="margin-top: 3px;" />
  699. <input type="text" name="sub_search" value="" class="input_text floatright" />
  700. </div>
  701. ',
  702. ),
  703. ),
  704. );
  705. require_once($sourcedir . '/Subs-List.php');
  706. createList($listOptions);
  707. $context['sub_template'] = 'show_list';
  708. $context['default_list'] = 'subscribed_users_list';
  709. }
  710. /**
  711. * Returns how many people are subscribed to a paid subscription.
  712. * @todo refactor away
  713. *
  714. * @param int $id_sub
  715. * @param string $search_string
  716. * @param array $search_vars = array()
  717. */
  718. function list_getSubscribedUserCount($id_sub, $search_string, $search_vars = array())
  719. {
  720. global $smcFunc;
  721. // Get the total amount of users.
  722. $request = $smcFunc['db_query']('', '
  723. SELECT COUNT(*) AS total_subs
  724. FROM {db_prefix}log_subscribed AS ls
  725. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  726. WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
  727. AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_pending_payments})',
  728. array_merge($search_vars, array(
  729. 'current_subscription' => $id_sub,
  730. 'no_end_time' => 0,
  731. 'no_pending_payments' => 0,
  732. ))
  733. );
  734. list ($memberCount) = $smcFunc['db_fetch_row']($request);
  735. $smcFunc['db_free_result']($request);
  736. return $memberCount;
  737. }
  738. /**
  739. * Return the subscribed users list, for the given parameters.
  740. * @todo refactor outta here
  741. *
  742. * @param int $start
  743. * @param int $items_per_page
  744. * @param string $sort
  745. * @param int $id_sub
  746. * @param string $search_string
  747. * @param string $search_vars
  748. */
  749. function list_getSubscribedUsers($start, $items_per_page, $sort, $id_sub, $search_string, $search_vars = array())
  750. {
  751. global $smcFunc, $txt;
  752. $request = $smcFunc['db_query']('', '
  753. 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,
  754. ls.status, ls.payments_pending
  755. FROM {db_prefix}log_subscribed AS ls
  756. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  757. WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
  758. AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_payments_pending})
  759. ORDER BY ' . $sort . '
  760. LIMIT ' . $start . ', ' . $items_per_page,
  761. array_merge($search_vars, array(
  762. 'current_subscription' => $id_sub,
  763. 'no_end_time' => 0,
  764. 'no_payments_pending' => 0,
  765. 'guest' => $txt['guest'],
  766. ))
  767. );
  768. $subscribers = array();
  769. while ($row = $smcFunc['db_fetch_assoc']($request))
  770. $subscribers[] = array(
  771. 'id' => $row['id_sublog'],
  772. 'id_member' => $row['id_member'],
  773. 'name' => $row['name'],
  774. 'start_date' => timeformat($row['start_time'], false),
  775. 'end_date' => $row['end_time'] == 0 ? 'N/A' : timeformat($row['end_time'], false),
  776. 'pending' => $row['payments_pending'],
  777. 'status' => $row['status'],
  778. 'status_text' => $row['status'] == 0 ? ($row['payments_pending'] == 0 ? $txt['paid_finished'] : $txt['paid_pending']) : $txt['paid_active'],
  779. );
  780. $smcFunc['db_free_result']($request);
  781. return $subscribers;
  782. }
  783. /**
  784. * Edit or add a user subscription.
  785. * Accessed from ?action=admin;area=paidsubscribe;sa=modifyuser.
  786. */
  787. function ModifyUserSubscription()
  788. {
  789. global $context, $txt, $modSettings, $smcFunc;
  790. loadSubscriptions();
  791. $context['log_id'] = isset($_REQUEST['lid']) ? (int) $_REQUEST['lid'] : 0;
  792. $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
  793. $context['action_type'] = $context['log_id'] ? 'edit' : 'add';
  794. // Setup the template.
  795. $context['sub_template'] = 'modify_user_subscription';
  796. $context['page_title'] = $txt[$context['action_type'] . '_subscriber'];
  797. // If we haven't been passed the subscription ID get it.
  798. if ($context['log_id'] && !$context['sub_id'])
  799. {
  800. $request = $smcFunc['db_query']('', '
  801. SELECT id_subscribe
  802. FROM {db_prefix}log_subscribed
  803. WHERE id_sublog = {int:current_log_item}',
  804. array(
  805. 'current_log_item' => $context['log_id'],
  806. )
  807. );
  808. if ($smcFunc['db_num_rows']($request) == 0)
  809. fatal_lang_error('no_access', false);
  810. list ($context['sub_id']) = $smcFunc['db_fetch_row']($request);
  811. $smcFunc['db_free_result']($request);
  812. }
  813. if (!isset($context['subscriptions'][$context['sub_id']]))
  814. fatal_lang_error('no_access', false);
  815. $context['current_subscription'] = $context['subscriptions'][$context['sub_id']];
  816. // Searching?
  817. if (isset($_POST['ssearch']))
  818. {
  819. return ViewSubscribedUsers();
  820. }
  821. // Saving?
  822. elseif (isset($_REQUEST['save_sub']))
  823. {
  824. checkSession();
  825. // Work out the dates...
  826. $starttime = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'], $_POST['day'], $_POST['year']);
  827. $endtime = mktime($_POST['hourend'], $_POST['minuteend'], 0, $_POST['monthend'], $_POST['dayend'], $_POST['yearend']);
  828. // Status.
  829. $status = $_POST['status'];
  830. // New one?
  831. if (empty($context['log_id']))
  832. {
  833. // Find the user...
  834. $request = $smcFunc['db_query']('', '
  835. SELECT id_member, id_group
  836. FROM {db_prefix}members
  837. WHERE real_name = {string:name}
  838. LIMIT 1',
  839. array(
  840. 'name' => $_POST['name'],
  841. )
  842. );
  843. if ($smcFunc['db_num_rows']($request) == 0)
  844. fatal_lang_error('error_member_not_found');
  845. list ($id_member, $id_group) = $smcFunc['db_fetch_row']($request);
  846. $smcFunc['db_free_result']($request);
  847. // Ensure the member doesn't already have a subscription!
  848. $request = $smcFunc['db_query']('', '
  849. SELECT id_subscribe
  850. FROM {db_prefix}log_subscribed
  851. WHERE id_subscribe = {int:current_subscription}
  852. AND id_member = {int:current_member}',
  853. array(
  854. 'current_subscription' => $context['sub_id'],
  855. 'current_member' => $id_member,
  856. )
  857. );
  858. if ($smcFunc['db_num_rows']($request) != 0)
  859. fatal_lang_error('member_already_subscribed');
  860. $smcFunc['db_free_result']($request);
  861. // Actually put the subscription in place.
  862. if ($status == 1)
  863. addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
  864. else
  865. {
  866. $smcFunc['db_insert']('',
  867. '{db_prefix}log_subscribed',
  868. array(
  869. 'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
  870. 'end_time' => 'int', 'status' => 'int',
  871. ),
  872. array(
  873. $context['sub_id'], $id_member, $id_group, $starttime,
  874. $endtime, $status,
  875. ),
  876. array('id_sublog')
  877. );
  878. }
  879. }
  880. // Updating.
  881. else
  882. {
  883. $request = $smcFunc['db_query']('', '
  884. SELECT id_member, status
  885. FROM {db_prefix}log_subscribed
  886. WHERE id_sublog = {int:current_log_item}',
  887. array(
  888. 'current_log_item' => $context['log_id'],
  889. )
  890. );
  891. if ($smcFunc['db_num_rows']($request) == 0)
  892. fatal_lang_error('no_access', false);
  893. list ($id_member, $old_status) = $smcFunc['db_fetch_row']($request);
  894. $smcFunc['db_free_result']($request);
  895. // Pick the right permission stuff depending on what the status is changing from/to.
  896. if ($old_status == 1 && $status != 1)
  897. removeSubscription($context['sub_id'], $id_member);
  898. elseif ($status == 1 && $old_status != 1)
  899. {
  900. addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
  901. }
  902. else
  903. {
  904. $smcFunc['db_query']('', '
  905. UPDATE {db_prefix}log_subscribed
  906. SET start_time = {int:start_time}, end_time = {int:end_time}, status = {int:status}
  907. WHERE id_sublog = {int:current_log_item}',
  908. array(
  909. 'start_time' => $starttime,
  910. 'end_time' => $endtime,
  911. 'status' => $status,
  912. 'current_log_item' => $context['log_id'],
  913. )
  914. );
  915. }
  916. }
  917. // Done - redirect...
  918. redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
  919. }
  920. // Deleting?
  921. elseif (isset($_REQUEST['delete']) || isset($_REQUEST['finished']))
  922. {
  923. checkSession();
  924. // Do the actual deletes!
  925. if (!empty($_REQUEST['delsub']))
  926. {
  927. $toDelete = array();
  928. foreach ($_REQUEST['delsub'] as $id => $dummy)
  929. $toDelete[] = (int) $id;
  930. $request = $smcFunc['db_query']('', '
  931. SELECT id_subscribe, id_member
  932. FROM {db_prefix}log_subscribed
  933. WHERE id_sublog IN ({array_int:subscription_list})',
  934. array(
  935. 'subscription_list' => $toDelete,
  936. )
  937. );
  938. while ($row = $smcFunc['db_fetch_assoc']($request))
  939. removeSubscription($row['id_subscribe'], $row['id_member'], isset($_REQUEST['delete']));
  940. $smcFunc['db_free_result']($request);
  941. }
  942. redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
  943. }
  944. // Default attributes.
  945. if ($context['action_type'] == 'add')
  946. {
  947. $context['sub'] = array(
  948. 'id' => 0,
  949. 'start' => array(
  950. 'year' => (int) strftime('%Y', time()),
  951. 'month' => (int) strftime('%m', time()),
  952. 'day' => (int) strftime('%d', time()),
  953. 'hour' => (int) strftime('%H', time()),
  954. 'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
  955. 'last_day' => 0,
  956. ),
  957. 'end' => array(
  958. 'year' => (int) strftime('%Y', time()),
  959. 'month' => (int) strftime('%m', time()),
  960. 'day' => (int) strftime('%d', time()),
  961. 'hour' => (int) strftime('%H', time()),
  962. 'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
  963. 'last_day' => 0,
  964. ),
  965. 'status' => 1,
  966. );
  967. $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']));
  968. $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']));
  969. if (isset($_GET['uid']))
  970. {
  971. $request = $smcFunc['db_query']('', '
  972. SELECT real_name
  973. FROM {db_prefix}members
  974. WHERE id_member = {int:current_member}',
  975. array(
  976. 'current_member' => (int) $_GET['uid'],
  977. )
  978. );
  979. list ($context['sub']['username']) = $smcFunc['db_fetch_row']($request);
  980. $smcFunc['db_free_result']($request);
  981. }
  982. else
  983. $context['sub']['username'] = '';
  984. }
  985. // Otherwise load the existing info.
  986. else
  987. {
  988. $request = $smcFunc['db_query']('', '
  989. SELECT ls.id_sublog, ls.id_subscribe, ls.id_member, start_time, end_time, status, payments_pending, pending_details,
  990. IFNULL(mem.real_name, {string:blank_string}) AS username
  991. FROM {db_prefix}log_subscribed AS ls
  992. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  993. WHERE ls.id_sublog = {int:current_subscription_item}
  994. LIMIT 1',
  995. array(
  996. 'current_subscription_item' => $context['log_id'],
  997. 'blank_string' => '',
  998. )
  999. );
  1000. if ($smcFunc['db_num_rows']($request) == 0)
  1001. fatal_lang_error('no_access', false);
  1002. $row = $smcFunc['db_fetch_assoc']($request);
  1003. $smcFunc['db_free_result']($request);
  1004. // Any pending payments?
  1005. $context['pending_payments'] = array();
  1006. if (!empty($row['pending_details']))
  1007. {
  1008. $pending_details = @unserialize($row['pending_details']);
  1009. foreach ($pending_details as $id => $pending)
  1010. {
  1011. // Only this type need be displayed.
  1012. if ($pending[3] == 'payback')
  1013. {
  1014. // Work out what the options were.
  1015. $costs = @unserialize($context['current_subscription']['real_cost']);
  1016. if ($context['current_subscription']['real_length'] == 'F')
  1017. {
  1018. foreach ($costs as $duration => $cost)
  1019. {
  1020. if ($cost != 0 && $cost == $pending[1] && $duration == $pending[2])
  1021. $context['pending_payments'][$id] = array(
  1022. 'desc' => sprintf($modSettings['paid_currency_symbol'], $cost . '/' . $txt[$duration]),
  1023. );
  1024. }
  1025. }
  1026. elseif ($costs['fixed'] == $pending[1])
  1027. {
  1028. $context['pending_payments'][$id] = array(
  1029. 'desc' => sprintf($modSettings['paid_currency_symbol'], $costs['fixed']),
  1030. );
  1031. }
  1032. }
  1033. }
  1034. // Check if we are adding/removing any.
  1035. if (isset($_GET['pending']))
  1036. {
  1037. foreach ($pending_details as $id => $pending)
  1038. {
  1039. // Found the one to action?
  1040. if ($_GET['pending'] == $id && $pending[3] == 'payback' && isset($context['pending_payments'][$id]))
  1041. {
  1042. // Flexible?
  1043. if (isset($_GET['accept']))
  1044. addSubscription($context['current_subscription']['id'], $row['id_member'], $context['current_subscription']['real_length'] == 'F' ? strtoupper(substr($pending[2], 0, 1)) : 0);
  1045. unset($pending_details[$id]);
  1046. $new_details = serialize($pending_details);
  1047. // Update the entry.
  1048. $smcFunc['db_query']('', '
  1049. UPDATE {db_prefix}log_subscribed
  1050. SET payments_pending = payments_pending - 1, pending_details = {string:pending_details}
  1051. WHERE id_sublog = {int:current_subscription_item}',
  1052. array(
  1053. 'current_subscription_item' => $context['log_id'],
  1054. 'pending_details' => $new_details,
  1055. )
  1056. );
  1057. // Reload
  1058. redirectexit('action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $context['log_id']);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. $context['sub_id'] = $row['id_subscribe'];
  1064. $context['sub'] = array(
  1065. 'id' => 0,
  1066. 'start' => array(
  1067. 'year' => (int) strftime('%Y', $row['start_time']),
  1068. 'month' => (int) strftime('%m', $row['start_time']),
  1069. 'day' => (int) strftime('%d', $row['start_time']),
  1070. 'hour' => (int) strftime('%H', $row['start_time']),
  1071. 'min' => (int) strftime('%M', $row['start_time']) < 10 ? '0' . (int) strftime('%M', $row['start_time']) : (int) strftime('%M', $row['start_time']),
  1072. 'last_day' => 0,
  1073. ),
  1074. 'end' => array(
  1075. 'year' => (int) strftime('%Y', $row['end_time']),
  1076. 'month' => (int) strftime('%m', $row['end_time']),
  1077. 'day' => (int) strftime('%d', $row['end_time']),
  1078. 'hour' => (int) strftime('%H', $row['end_time']),
  1079. 'min' => (int) strftime('%M', $row['end_time']) < 10 ? '0' . (int) strftime('%M', $row['end_time']) : (int) strftime('%M', $row['end_time']),
  1080. 'last_day' => 0,
  1081. ),
  1082. 'status' => $row['status'],
  1083. 'username' => $row['username'],
  1084. );
  1085. $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']));
  1086. $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']));
  1087. }
  1088. }
  1089. /**
  1090. * Reapplies all subscription rules for each of the users.
  1091. *
  1092. * @param array $users
  1093. */
  1094. function reapplySubscriptions($users)
  1095. {
  1096. global $smcFunc;
  1097. // Make it an array.
  1098. if (!is_array($users))
  1099. $users = array($users);
  1100. // Get all the members current groups.
  1101. $groups = array();
  1102. $request = $smcFunc['db_query']('', '
  1103. SELECT id_member, id_group, additional_groups
  1104. FROM {db_prefix}members
  1105. WHERE id_member IN ({array_int:user_list})',
  1106. array(
  1107. 'user_list' => $users,
  1108. )
  1109. );
  1110. while ($row = $smcFunc['db_fetch_assoc']($request))
  1111. {
  1112. $groups[$row['id_member']] = array(
  1113. 'primary' => $row['id_group'],
  1114. 'additional' => explode(',', $row['additional_groups']),
  1115. );
  1116. }
  1117. $smcFunc['db_free_result']($request);
  1118. $request = $smcFunc['db_query']('', '
  1119. SELECT ls.id_member, ls.old_id_group, s.id_group, s.add_groups
  1120. FROM {db_prefix}log_subscribed AS ls
  1121. INNER JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
  1122. WHERE ls.id_member IN ({array_int:user_list})
  1123. AND ls.end_time > {int:current_time}',
  1124. array(
  1125. 'user_list' => $users,
  1126. 'current_time' => time(),
  1127. )
  1128. );
  1129. while ($row = $smcFunc['db_fetch_assoc']($request))
  1130. {
  1131. // Specific primary group?
  1132. if ($row['id_group'] != 0)
  1133. {
  1134. // If this is changing - add the old one to the additional groups so it's not lost.
  1135. if ($row['id_group'] != $groups[$row['id_member']]['primary'])
  1136. $groups[$row['id_member']]['additional'][] = $groups[$row['id_member']]['primary'];
  1137. $groups[$row['id_member']]['primary'] = $row['id_group'];
  1138. }
  1139. // Additional groups.
  1140. if (!empty($row['add_groups']))
  1141. $groups[$row['id_member']]['additional'] = array_merge($groups[$row['id_member']]['additional'], explode(',', $row['add_groups']));
  1142. }
  1143. $smcFunc['db_free_result']($request);
  1144. // Update all the members.
  1145. foreach ($groups as $id => $group)
  1146. {
  1147. $group['additional'] = array_unique($group['additional']);
  1148. foreach ($group['additional'] as $key => $value)
  1149. if (empty($value))
  1150. unset($group['additional'][$key]);
  1151. $addgroups = implode(',', $group['additional']);
  1152. $smcFunc['db_query']('', '
  1153. UPDATE {db_prefix}members
  1154. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  1155. WHERE id_member = {int:current_member}
  1156. LIMIT 1',
  1157. array(
  1158. 'primary_group' => $group['primary'],
  1159. 'current_member' => $id,
  1160. 'additional_groups' => $addgroups,
  1161. )
  1162. );
  1163. }
  1164. }
  1165. /**
  1166. * Add or extend a subscription of a user.
  1167. *
  1168. * @param int $id_subscribe
  1169. * @param int $id_member
  1170. * @param string $renewal = 0, options 'D', 'W', 'M', 'Y'
  1171. * @param int $forceStartTime = 0
  1172. * @param int $forceEndTime = 0
  1173. */
  1174. function addSubscription($id_subscribe, $id_member, $renewal = 0, $forceStartTime = 0, $forceEndTime = 0)
  1175. {
  1176. global $context, $smcFunc;
  1177. // Take the easy way out...
  1178. loadSubscriptions();
  1179. // Exists, yes?
  1180. if (!isset($context['subscriptions'][$id_subscribe]))
  1181. return;
  1182. $curSub = $context['subscriptions'][$id_subscribe];
  1183. // Grab the duration.
  1184. $duration = $curSub['num_length'];
  1185. // If this is a renewal change the duration to be correct.
  1186. if (!empty($renewal))
  1187. {
  1188. switch ($renewal)
  1189. {
  1190. case 'D':
  1191. $duration = 86400;
  1192. break;
  1193. case 'W':
  1194. $duration = 604800;
  1195. break;
  1196. case 'M':
  1197. $duration = 2629743;
  1198. break;
  1199. case 'Y':
  1200. $duration = 31556926;
  1201. break;
  1202. default:
  1203. break;
  1204. }
  1205. }
  1206. // Firstly, see whether it exists, and is active. If so then this is meerly an extension.
  1207. $request = $smcFunc['db_query']('', '
  1208. SELECT id_sublog, end_time, start_time
  1209. FROM {db_prefix}log_subscribed
  1210. WHERE id_subscribe = {int:current_subscription}
  1211. AND id_member = {int:current_member}
  1212. AND status = {int:is_active}',
  1213. array(
  1214. 'current_subscription' => $id_subscribe,
  1215. 'current_member' => $id_member,
  1216. 'is_active' => 1,
  1217. )
  1218. );
  1219. if ($smcFunc['db_num_rows']($request) != 0)
  1220. {
  1221. list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);
  1222. // If this has already expired but is active, extension means the period from now.
  1223. if ($endtime < time())
  1224. $endtime = time();
  1225. if ($starttime == 0)
  1226. $starttime = time();
  1227. // Work out the new expiry date.
  1228. $endtime += $duration;
  1229. if ($forceEndTime != 0)
  1230. $endtime = $forceEndTime;
  1231. // As everything else should be good, just update!
  1232. $smcFunc['db_query']('', '
  1233. UPDATE {db_prefix}log_subscribed
  1234. SET end_time = {int:end_time}, start_time = {int:start_time}
  1235. WHERE id_sublog = {int:current_subscription_item}',
  1236. array(
  1237. 'end_time' => $endtime,
  1238. 'start_time' => $starttime,
  1239. 'current_subscription_item' => $id_sublog,
  1240. )
  1241. );
  1242. return;
  1243. }
  1244. $smcFunc['db_free_result']($request);
  1245. // If we're here, that means we don't have an active subscription - that means we need to do some work!
  1246. $request = $smcFunc['db_query']('', '
  1247. SELECT m.id_group, m.additional_groups
  1248. FROM {db_prefix}members AS m
  1249. WHERE m.id_member = {int:current_member}',
  1250. array(
  1251. 'current_member' => $id_member,
  1252. )
  1253. );
  1254. // Just in case the member doesn't exist.
  1255. if ($smcFunc['db_num_rows']($request) == 0)
  1256. return;
  1257. list ($old_id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
  1258. $smcFunc['db_free_result']($request);
  1259. // Prepare additional groups.
  1260. $newAddGroups = explode(',', $curSub['add_groups']);
  1261. $curAddGroups = explode(',', $additional_groups);
  1262. $newAddGroups = array_merge($newAddGroups, $curAddGroups);
  1263. // Simple, simple, simple - hopefully... id_group first.
  1264. if ($curSub['prim_group'] != 0)
  1265. {
  1266. $id_group = $curSub['prim_group'];
  1267. // Ensure their old privileges are maintained.
  1268. if ($old_id_group != 0)
  1269. $newAddGroups[] = $old_id_group;
  1270. }
  1271. else
  1272. $id_group = $old_id_group;
  1273. // Yep, make sure it's unique, and no empties.
  1274. foreach ($newAddGroups as $k => $v)
  1275. if (empty($v))
  1276. unset($newAddGroups[$k]);
  1277. $newAddGroups = array_unique($newAddGroups);
  1278. $newAddGroups = implode(',', $newAddGroups);
  1279. // Store the new settings.
  1280. $smcFunc['db_query']('', '
  1281. UPDATE {db_prefix}members
  1282. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  1283. WHERE id_member = {int:current_member}',
  1284. array(
  1285. 'primary_group' => $id_group,
  1286. 'current_member' => $id_member,
  1287. 'additional_groups' => $newAddGroups,
  1288. )
  1289. );
  1290. // Now log the subscription - maybe we have a dorment subscription we can restore?
  1291. $request = $smcFunc['db_query']('', '
  1292. SELECT id_sublog, end_time, start_time
  1293. FROM {db_prefix}log_subscribed
  1294. WHERE id_subscribe = {int:current_subscription}
  1295. AND id_member = {int:current_member}',
  1296. array(
  1297. 'current_subscription' => $id_subscribe,
  1298. 'current_member' => $id_member,
  1299. )
  1300. );
  1301. /**
  1302. * @todo Don't really need to do this twice...
  1303. */
  1304. if ($smcFunc['db_num_rows']($request) != 0)
  1305. {
  1306. list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);
  1307. // If this has already expired but is active, extension means the period from now.
  1308. if ($endtime < time())
  1309. $endtime = time();
  1310. if ($starttime == 0)
  1311. $starttime = time();
  1312. // Work out the new expiry date.
  1313. $endtime += $duration;
  1314. if ($forceEndTime != 0)
  1315. $endtime = $forceEndTime;
  1316. // As everything else should be good, just update!
  1317. $smcFunc['db_query']('', '
  1318. UPDATE {db_prefix}log_subscribed
  1319. SET start_time = {int:start_time}, end_time = {int:end_time}, old_id_group = {int:old_id_group}, status = {int:is_active},
  1320. reminder_sent = {int:no_reminder_sent}
  1321. WHERE id_sublog = {int:current_subscription_item}',
  1322. array(
  1323. 'start_time' => $starttime,
  1324. 'end_time' => $endtime,
  1325. 'old_id_group' => $old_id_group,
  1326. 'is_active' => 1,
  1327. 'no_reminder_sent' => 0,
  1328. 'current_subscription_item' => $id_sublog,
  1329. )
  1330. );
  1331. return;
  1332. }
  1333. $smcFunc['db_free_result']($request);
  1334. // Otherwise a very simple insert.
  1335. $endtime = time() + $duration;
  1336. if ($forceEndTime != 0)
  1337. $endtime = $forceEndTime;
  1338. if ($forceStartTime == 0)
  1339. $starttime = time();
  1340. else
  1341. $starttime = $forceStartTime;
  1342. $smcFunc['db_insert']('',
  1343. '{db_prefix}log_subscribed',
  1344. array(
  1345. 'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
  1346. 'end_time' => 'int', 'status' => 'int', 'pending_details' => 'string',
  1347. ),
  1348. array(
  1349. $id_subscribe, $id_member, $old_id_group, $starttime,
  1350. $endtime, 1, '',
  1351. ),
  1352. array('id_sublog')
  1353. );
  1354. }
  1355. /**
  1356. * Removes a subscription from a user, as in removes the groups.
  1357. *
  1358. * @param $id_subscribe
  1359. * @param $id_member
  1360. * @param $delete
  1361. */
  1362. function removeSubscription($id_subscribe, $id_member, $delete = false)
  1363. {
  1364. global $context, $smcFunc;
  1365. loadSubscriptions();
  1366. // Load the user core bits.
  1367. $request = $smcFunc['db_query']('', '
  1368. SELECT m.id_group, m.additional_groups
  1369. FROM {db_prefix}members AS m
  1370. WHERE m.id_member = {int:current_member}',
  1371. array(
  1372. 'current_member' => $id_member,
  1373. )
  1374. );
  1375. // Just in case of errors.
  1376. if ($smcFunc['db_num_rows']($request) == 0)
  1377. {
  1378. $smcFunc['db_query']('', '
  1379. DELETE FROM {db_prefix}log_subscribed
  1380. WHERE id_member = {int:current_member}',
  1381. array(
  1382. 'current_member' => $id_member,
  1383. )
  1384. );
  1385. return;
  1386. }
  1387. list ($id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
  1388. $smcFunc['db_free_result']($request);
  1389. // Get all of the subscriptions for this user that are active - it will be necessary!
  1390. $request = $smcFunc['db_query']('', '
  1391. SELECT id_subscribe, old_id_group
  1392. FROM {db_prefix}log_subscribed
  1393. WHERE id_member = {int:current_member}
  1394. AND status = {int:is_active}',
  1395. array(
  1396. 'current_member' => $id_member,
  1397. 'is_active' => 1,
  1398. )
  1399. );
  1400. // These variables will be handy, honest ;)
  1401. $removals = array();
  1402. $allowed = array();
  1403. $old_id_group = 0;
  1404. $new_id_group = -1;
  1405. while ($row = $smcFunc['db_fetch_assoc']($request))
  1406. {
  1407. if (!isset($context['subscriptions'][$row['id_subscribe']]))
  1408. continue;
  1409. // The one we're removing?
  1410. if ($row['id_subscribe'] == $id_subscribe)
  1411. {
  1412. $removals = explode(',', $context['subscriptions'][$row['id_subscribe']]['add_groups']);
  1413. if ($context['subscriptions'][$row['id_subscribe']]['prim_group'] != 0)
  1414. $removals[] = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
  1415. $old_id_group = $row['old_id_group'];
  1416. }
  1417. // Otherwise things we allow.
  1418. else
  1419. {
  1420. $allowed = array_merge($allowed, explode(',', $context['subscriptions'][$row['id_subscribe']]['add_groups']));
  1421. if ($context['subscriptions'][$row['id_subscribe']]['prim_group'] != 0)
  1422. {
  1423. $allowed[] = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
  1424. $new_id_group = $context['subscriptions'][$row['id_subscribe']]['prim_group'];
  1425. }
  1426. }
  1427. }
  1428. $smcFunc['db_free_result']($request);
  1429. // Now, for everything we are removing check they defintely are not allowed it.
  1430. $existingGroups = explode(',', $additional_groups);
  1431. foreach ($existingGroups as $key => $group)
  1432. if (empty($group) || (in_array($group, $removals) && !in_array($group, $allowed)))
  1433. unset($existingGroups[$key]);
  1434. // Finally, do something with the current primary group.
  1435. if (in_array($id_group, $removals))
  1436. {
  1437. // If this primary group is actually allowed keep it.
  1438. if (in_array($id_group, $allowed))
  1439. $existingGroups[] = $id_group;
  1440. // Either way, change the id_group back.
  1441. if ($new_id_group < 1)
  1442. {
  1443. // If we revert to the old id-group we need to ensure it wasn't from a subscription.
  1444. foreach ($context['subscriptions'] as $id => $group)
  1445. // It was? Make them a regular member then!
  1446. if ($group['prim_group'] == $old_id_group)
  1447. $old_id_group = 0;
  1448. $id_group = $old_id_group;
  1449. }
  1450. else
  1451. $id_group = $new_id_group;
  1452. }
  1453. // Crazy stuff, we seem to have our groups fixed, just make them unique
  1454. $existingGroups = array_unique($existingGroups);
  1455. $existingGroups = implode(',', $existingGroups);
  1456. // Update the member
  1457. $smcFunc['db_query']('', '
  1458. UPDATE {db_prefix}members
  1459. SET id_group = {int:primary_group}, additional_groups = {string:existing_groups}
  1460. WHERE id_member = {int:current_member}',
  1461. array(
  1462. 'primary_group' => $id_group,
  1463. 'current_member' => $id_member,
  1464. 'existing_groups' => $existingGroups,
  1465. )
  1466. );
  1467. // Disable the subscription.
  1468. if (!$delete)
  1469. $smcFunc['db_query']('', '
  1470. UPDATE {db_prefix}log_subscribed
  1471. SET status = {int:not_active}
  1472. WHERE id_member = {int:current_member}
  1473. AND id_subscribe = {int:current_subscription}',
  1474. array(
  1475. 'not_active' => 0,
  1476. 'current_member' => $id_member,
  1477. 'current_subscription' => $id_subscribe,
  1478. )
  1479. );
  1480. // Otherwise delete it!
  1481. else
  1482. $smcFunc['db_query']('', '
  1483. DELETE FROM {db_prefix}log_subscribed
  1484. WHERE id_member = {int:current_member}
  1485. AND id_subscribe = {int:current_subscription}',
  1486. array(
  1487. 'current_member' => $id_member,
  1488. 'current_subscription' => $id_subscribe,
  1489. )
  1490. );
  1491. }
  1492. /**
  1493. * This just kind of caches all the subscription data.
  1494. */
  1495. function loadSubscriptions()
  1496. {
  1497. global $context, $txt, $modSettings, $smcFunc;
  1498. if (!empty($context['subscriptions']))
  1499. return;
  1500. // Make sure this is loaded, just in case.
  1501. loadLanguage('ManagePaid');
  1502. $request = $smcFunc['db_query']('', '
  1503. SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active, repeatable
  1504. FROM {db_prefix}subscriptions',
  1505. array(
  1506. )
  1507. );
  1508. $context['subscriptions'] = array();
  1509. while ($row = $smcFunc['db_fetch_assoc']($request))
  1510. {
  1511. // Pick a cost.
  1512. $costs = @unserialize($row['cost']);
  1513. if ($row['length'] != 'F' && !empty($modSettings['paid_currency_symbol']) && !empty($costs['fixed']))
  1514. $cost = sprintf($modSettings['paid_currency_symbol'], $costs['fixed']);
  1515. else
  1516. $cost = '???';
  1517. // Do the span.
  1518. preg_match('~(\d*)(\w)~', $row['length'], $match);
  1519. if (isset($match[2]))
  1520. {
  1521. $num_length = $match[1];
  1522. $length = $match[1] . ' ';
  1523. switch ($match[2])
  1524. {
  1525. case 'D':
  1526. $length .= $txt['paid_mod_span_days'];
  1527. $num_length *= 86400;
  1528. break;
  1529. case 'W':
  1530. $length .= $txt['paid_mod_span_weeks'];
  1531. $num_length *= 604800;
  1532. break;
  1533. case 'M':
  1534. $length .= $txt['paid_mod_span_months'];
  1535. $num_length *= 2629743;
  1536. break;
  1537. case 'Y':
  1538. $length .= $txt['paid_mod_span_years'];
  1539. $num_length *= 31556926;
  1540. break;
  1541. }
  1542. }
  1543. else
  1544. $length = '??';
  1545. $context['subscriptions'][$row['id_subscribe']] = array(
  1546. 'id' => $row['id_subscribe'],
  1547. 'name' => $row['name'],
  1548. 'desc' => $row['description'],
  1549. 'cost' => $cost,
  1550. 'real_cost' => $row['cost'],
  1551. 'length' => $length,
  1552. 'num_length' => $num_length,
  1553. 'real_length' => $row['length'],
  1554. 'pending' => 0,
  1555. 'finished' => 0,
  1556. 'total' => 0,
  1557. 'active' => $row['active'],
  1558. 'prim_group' => $row['id_group'],
  1559. 'add_groups' => $row['add_groups'],
  1560. 'flexible' => $row['length'] == 'F' ? true : false,
  1561. 'repeatable' => $row['repeatable'],
  1562. );
  1563. }
  1564. $smcFunc['db_free_result']($request);
  1565. // Do the counts.
  1566. $request = $smcFunc['db_query']('', '
  1567. SELECT COUNT(id_sublog) AS member_count, id_subscribe, status
  1568. FROM {db_prefix}log_subscribed
  1569. GROUP BY id_subscribe, status',
  1570. array(
  1571. )
  1572. );
  1573. while ($row = $smcFunc['db_fetch_assoc']($request))
  1574. {
  1575. $ind = $row['status'] == 0 ? 'finished' : 'total';
  1576. if (isset($context['subscriptions'][$row['id_subscribe']]))
  1577. $context['subscriptions'][$row['id_subscribe']][$ind] = $row['member_count'];
  1578. }
  1579. $smcFunc['db_free_result']($request);
  1580. // How many payments are we waiting on?
  1581. $request = $smcFunc['db_query']('', '
  1582. SELECT SUM(payments_pending) AS total_pending, id_subscribe
  1583. FROM {db_prefix}log_subscribed
  1584. GROUP BY id_subscribe',
  1585. array(
  1586. )
  1587. );
  1588. while ($row = $smcFunc['db_fetch_assoc']($request))
  1589. {
  1590. if (isset($context['subscriptions'][$row['id_subscribe']]))
  1591. $context['subscriptions'][$row['id_subscribe']]['pending'] = $row['total_pending'];
  1592. }
  1593. $smcFunc['db_free_result']($request);
  1594. }
  1595. /**
  1596. * Load all the payment gateways.
  1597. * Checks the Sources directory for any files fitting the format of a payment gateway,
  1598. * loads each file to check it's valid, includes each file and returns the
  1599. * function name and whether it should work with this version of SMF.
  1600. *
  1601. * @return array
  1602. */
  1603. function loadPaymentGateways()
  1604. {
  1605. global $sourcedir;
  1606. $gateways = array();
  1607. if ($dh = opendir($sourcedir))
  1608. {
  1609. while (($file = readdir($dh)) !== false)
  1610. {
  1611. if (is_file($sourcedir .'/'. $file) && preg_match('~^Subscriptions-([A-Za-z\d]+)\.php$~', $file, $matches))
  1612. {
  1613. // Check this is definitely a valid gateway!
  1614. $fp = fopen($sourcedir . '/' . $file, 'rb');
  1615. $header = fread($fp, 4096);
  1616. fclose($fp);
  1617. if (strpos($header, '// SMF Payment Gateway: ' . strtolower($matches[1])) !== false)
  1618. {
  1619. require_once($sourcedir . '/' . $file);
  1620. $gateways[] = array(
  1621. 'filename' => $file,
  1622. 'code' => strtolower($matches[1]),
  1623. // Don't need anything snazier than this yet.
  1624. 'valid_version' => class_exists(strtolower($matches[1]) . '_payment') && class_exists(strtolower($matches[1]) . '_display'),
  1625. 'payment_class' => strtolower($matches[1]) . '_payment',
  1626. 'display_class' => strtolower($matches[1]) . '_display',
  1627. );
  1628. }
  1629. }
  1630. }
  1631. }
  1632. closedir($dh);
  1633. return $gateways;
  1634. }
  1635. ?>