ManagePaid.php 56 KB

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