ManagePaid.php 57 KB

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