PageRenderTime 195ms CodeModel.GetById 35ms RepoModel.GetById 3ms app.codeStats 1ms

/forum/Sources/ManagePaid.php

https://github.com/leftnode/nooges.com
PHP | 1782 lines | 1421 code | 155 blank | 206 comment | 173 complexity | e49e1178c3e6857cb81b7c58eaca183f MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**********************************************************************************
  3. * ManagePaid.php *
  4. ***********************************************************************************
  5. * SMF: Simple Machines Forum *
  6. * Open-Source Project Inspired by Zef Hemel (zef@zefhemel.com) *
  7. * =============================================================================== *
  8. * Software Version: SMF 2.0 RC2 *
  9. * Software by: Simple Machines (http://www.simplemachines.org) *
  10. * Copyright 2006-2009 by: Simple Machines LLC (http://www.simplemachines.org) *
  11. * 2001-2006 by: Lewis Media (http://www.lewismedia.com) *
  12. * Support, News, Updates at: http://www.simplemachines.org *
  13. ***********************************************************************************
  14. * This program is free software; you may redistribute it and/or modify it under *
  15. * the terms of the provided license as published by Simple Machines LLC. *
  16. * *
  17. * This program is distributed in the hope that it is and will be useful, but *
  18. * WITHOUT ANY WARRANTIES; without even any implied warranty of MERCHANTABILITY *
  19. * or FITNESS FOR A PARTICULAR PURPOSE. *
  20. * *
  21. * See the "license.txt" file for details of the Simple Machines license. *
  22. * The latest version can always be found at http://www.simplemachines.org. *
  23. **********************************************************************************/
  24. if (!defined('SMF'))
  25. die('Hacking attempt...');
  26. /* This file contains all the screens that control settings for topics and
  27. posts.
  28. void ManagePaidSubscriptions()
  29. - the main entrance point for the 'Paid Subscription' screen.
  30. - accessed from ?action=admin;area=paidsubscribe.
  31. - calls the right function based on the given sub-action.
  32. - defaults to sub-action 'view'.
  33. - requires admin_forum permission for admin based actions.
  34. void ModifySubscriptionSettings()
  35. - set any setting related to paid subscriptions.
  36. - requires the moderate_forum permission
  37. - accessed from ?action=admin;area=paidsubscribe;sa=settings.
  38. void ViewSubscriptions()
  39. - view a list of all the current subscriptions
  40. - requires the admin_forum permission
  41. - accessed from ?action=admin;area=paidsubscribe;sa=view.
  42. void ModifySubscription()
  43. - edit a subscription.
  44. - accessed from ?action=admin;area=paidsubscribe;sa=modify.
  45. void ViewSubscribedUsers()
  46. - view a list of all the users who currently have a subscription.
  47. - requires the admin_forum permission.
  48. - subscription ID is required, in the form of $_GET['sid'].
  49. - accessed from ?action=admin;area=paidsubscribe;sa=viewsub.
  50. int list_getSubscribedUserCount()
  51. // !!
  52. array list_getSubscribedUsers()
  53. // !!
  54. void ModifyUserSubscription()
  55. - edit a users subscription.
  56. - accessed from ?action=admin;area=paidsubscribe;sa=modifyuser.
  57. void reapplySubscriptions(array users)
  58. - reapplies all subscription rules for each of the users.
  59. void addSubscription(int id_subscribe, int id_member)
  60. - add/extend a subscription for a member.
  61. void removeSubscription(int id_subscribe, int id_member)
  62. - remove a subscription from a user.
  63. array loadPaymentGateways()
  64. - checks the Sources directory for any files fitting the format of a payment gateway.
  65. - loads each file to check it's valid.
  66. - includes each file and returns the function name and whether it should work with this version of SMF.
  67. */
  68. function ManagePaidSubscriptions()
  69. {
  70. global $context, $txt, $scripturl, $sourcedir, $smcFunc, $modSettings;
  71. // Load the required language and template.
  72. loadLanguage('ManagePaid');
  73. loadTemplate('ManagePaid');
  74. $subActions = array(
  75. 'modify' => array('ModifySubscription', 'admin_forum'),
  76. 'modifyuser' => array('ModifyUserSubscription', 'admin_forum'),
  77. 'settings' => array('ModifySubscriptionSettings', 'admin_forum'),
  78. 'view' => array('ViewSubscriptions', 'admin_forum'),
  79. 'viewsub' => array('ViewSubscribedUsers', 'admin_forum'),
  80. );
  81. // Default the sub-action to 'view subscriptions', but only if they have already set things up..
  82. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (!empty($modSettings['paid_currency_symbol']) ? 'view' : 'settings');
  83. // Make sure you can do this.
  84. isAllowedTo($subActions[$_REQUEST['sa']][1]);
  85. $context['page_title'] = $txt['paid_subscriptions'];
  86. // Tabs for browsing the different subscription functions.
  87. $context[$context['admin_menu_name']]['tab_data'] = array(
  88. 'title' => $txt['paid_subscriptions'],
  89. 'help' => '',
  90. 'description' => $txt['paid_subscriptions_desc'],
  91. 'tabs' => array(
  92. 'view' => array(
  93. 'description' => $txt['paid_subs_view_desc'],
  94. ),
  95. 'settings' => array(
  96. 'description' => $txt['paid_subs_settings_desc'],
  97. ),
  98. ),
  99. );
  100. // Call the right function for this sub-acton.
  101. $subActions[$_REQUEST['sa']][0]();
  102. }
  103. // Modify which payment methods are to be used.
  104. function ModifySubscriptionSettings($return_config = false)
  105. {
  106. global $context, $txt, $modSettings, $sourcedir, $smcFunc, $scripturl;
  107. // 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.
  108. $modSettings['paid_currency'] = !empty($modSettings['paid_currency_code']) ? $modSettings['paid_currency_code'] : '';
  109. if (!empty($modSettings['paid_currency_code']) && !in_array($modSettings['paid_currency_code'], array('usd', 'eur', 'gbp')))
  110. $modSettings['paid_currency'] = 'other';
  111. // These are all the default settings.
  112. $config_vars = array(
  113. 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']),
  114. array('text', 'paid_email_to', 'subtext' => $txt['paid_email_to_desc'], 'size' => 60),
  115. '',
  116. 'dummy_currency' => array('select', 'paid_currency', array('usd' => $txt['usd'], 'eur' => $txt['eur'], 'gbp' => $txt['gbp'], 'other' => $txt['other']), 'javascript' => 'onchange="toggleOther();"'),
  117. array('text', 'paid_currency_code', 'subtext' => $txt['paid_currency_code_desc'], 'size' => 5, 'force_div_id' => 'custom_currency_code_div'),
  118. array('text', 'paid_currency_symbol', 'subtext' => $txt['paid_currency_symbol_desc'], 'size' => 8, 'force_div_id' => 'custom_currency_symbol_div'),
  119. );
  120. // Now load all the other gateway settings.
  121. $gateways = loadPaymentGateways();
  122. foreach ($gateways as $gateway)
  123. {
  124. $gatewayClass = new $gateway['display_class']();
  125. $setting_data = $gatewayClass->getGatewaySettings();
  126. if (!empty($setting_data))
  127. {
  128. $config_vars[] = array('title', $gatewayClass->title, 'text_label' => (isset($txt['paidsubs_gateway_title_' . $gatewayClass->title]) ? $txt['paidsubs_gateway_title_' . $gatewayClass->title] : $gatewayClass->title));
  129. $config_vars = array_merge($config_vars, $setting_data);
  130. }
  131. }
  132. // Just searching?
  133. if ($return_config)
  134. return $config_vars;
  135. // Get the settings template fired up.
  136. require_once($sourcedir .'/ManageServer.php');
  137. // Some important context stuff
  138. $context['page_title'] = $txt['settings'];
  139. $context['sub_template'] = 'show_settings';
  140. $context['settings_message'] = '<span class="smalltext">' . $txt['paid_note'] . '</span>';
  141. $context[$context['admin_menu_name']]['current_subsection'] = 'settings';
  142. // Get the final touches in place.
  143. $context['post_url'] = $scripturl . '?action=admin;area=paidsubscribe;save;sa=settings';
  144. $context['settings_title'] = $txt['settings'];
  145. // We want javascript for our currency options.
  146. $context['settings_insert_below'] = '
  147. <script type="text/javascript"><!-- // --><![CDATA[
  148. function toggleOther()
  149. {
  150. var otherOn = document.getElementById("paid_currency").value == \'other\';
  151. var currencydd = document.getElementById("custom_currency_code_div_dd");
  152. if (otherOn)
  153. {
  154. document.getElementById("custom_currency_code_div").style.display = "";
  155. document.getElementById("custom_currency_symbol_div").style.display = "";
  156. if (currencydd)
  157. {
  158. document.getElementById("custom_currency_code_div_dd").style.display = "";
  159. document.getElementById("custom_currency_symbol_div_dd").style.display = "";
  160. }
  161. }
  162. else
  163. {
  164. document.getElementById("custom_currency_code_div").style.display = "none";
  165. document.getElementById("custom_currency_symbol_div").style.display = "none";
  166. if (currencydd)
  167. {
  168. document.getElementById("custom_currency_symbol_div_dd").style.display = "none";
  169. document.getElementById("custom_currency_code_div_dd").style.display = "none";
  170. }
  171. }
  172. }
  173. toggleOther();
  174. // ]]></script>';
  175. // Saving the settings?
  176. if (isset($_GET['save']))
  177. {
  178. checkSession();
  179. // Sort out the currency stuff.
  180. if ($_POST['paid_currency'] != 'other')
  181. {
  182. $_POST['paid_currency_code'] = $_POST['paid_currency'];
  183. $_POST['paid_currency_symbol'] = $txt[$_POST['paid_currency'] . '_symbol'];
  184. }
  185. unset($config_vars['dummy_currency']);
  186. saveDBSettings($config_vars);
  187. redirectexit('action=admin;area=paidsubscribe;sa=settings');
  188. }
  189. // Prepare the settings...
  190. prepareDBSettingContext($config_vars);
  191. }
  192. // Are we looking at viewing the subscriptions?
  193. function ViewSubscriptions()
  194. {
  195. global $context, $txt, $modSettings, $smcFunc, $sourcedir, $scripturl;
  196. // Not made the settings yet?
  197. if (empty($modSettings['paid_currency_symbol']))
  198. fatal_lang_error('paid_not_set_currency', false, $scripturl . '?action=admin;area=paidsubscribe;sa=settings');
  199. // Some basic stuff.
  200. $context['page_title'] = $txt['paid_subs_view'];
  201. loadSubscriptions();
  202. $listOptions = array(
  203. 'id' => 'subscription_list',
  204. 'items_per_page' => 20,
  205. 'base_href' => $scripturl . '?action=admin;area=paidsubscribe;sa=view',
  206. 'get_items' => array(
  207. 'function' => create_function('', '
  208. global $context;
  209. return $context[\'subscriptions\'];
  210. '),
  211. ),
  212. 'get_count' => array(
  213. 'function' => create_function('', '
  214. global $context;
  215. return count($context[\'subscriptions\']);
  216. '),
  217. ),
  218. 'no_items_label' => $txt['paid_none_yet'],
  219. 'columns' => array(
  220. 'name' => array(
  221. 'header' => array(
  222. 'value' => $txt['paid_name'],
  223. 'style' => 'text-align: left; width: 35%;',
  224. ),
  225. 'data' => array(
  226. 'function' => create_function('$rowData', '
  227. global $scripturl;
  228. return sprintf(\'<a href="%1$s?action=admin;area=paidsubscribe;sa=viewsub;sid=%2$s">%3$s</a>\', $scripturl, $rowData[\'id\'], $rowData[\'name\']);
  229. '),
  230. ),
  231. ),
  232. 'cost' => array(
  233. 'header' => array(
  234. 'value' => $txt['paid_cost'],
  235. 'style' => 'text-align: left;',
  236. ),
  237. 'data' => array(
  238. 'function' => create_function('$rowData', '
  239. global $context, $txt;
  240. return $rowData[\'flexible\'] ? \'<em>\' . $txt[\'flexible\'] . \'</em>\' : $rowData[\'cost\'] . \' / \' . $rowData[\'length\'];
  241. '),
  242. ),
  243. ),
  244. 'pending' => array(
  245. 'header' => array(
  246. 'value' => $txt['paid_pending'],
  247. ),
  248. 'data' => array(
  249. 'db_htmlsafe' => 'pending',
  250. 'style' => 'text-align: center;',
  251. ),
  252. ),
  253. 'finished' => array(
  254. 'header' => array(
  255. 'value' => $txt['paid_finished'],
  256. ),
  257. 'data' => array(
  258. 'db_htmlsafe' => 'finished',
  259. 'style' => 'text-align: center;',
  260. ),
  261. ),
  262. 'total' => array(
  263. 'header' => array(
  264. 'value' => $txt['paid_active'],
  265. ),
  266. 'data' => array(
  267. 'db_htmlsafe' => 'total',
  268. 'style' => 'text-align: center;',
  269. ),
  270. ),
  271. 'is_active' => array(
  272. 'header' => array(
  273. 'value' => $txt['paid_is_active'],
  274. ),
  275. 'data' => array(
  276. 'function' => create_function('$rowData', '
  277. global $context, $txt;
  278. return \'<span style="color: \' . ($rowData[\'active\'] ? \'green\' : \'red\') . \'">\' . ($rowData[\'active\'] ? $txt[\'yes\'] : $txt[\'no\']) . \'</span>\';
  279. '),
  280. 'style' => 'text-align: center;',
  281. ),
  282. ),
  283. 'modify' => array(
  284. 'data' => array(
  285. 'function' => create_function('$rowData', '
  286. global $context, $txt, $scripturl;
  287. return \'<a href="\' . $scripturl . \'?action=admin;area=paidsubscribe;sa=modify;sid=\' . $rowData[\'id\'] . \'">\' . $txt[\'modify\'] . \'</a>\';
  288. '),
  289. 'style' => 'text-align: center;',
  290. ),
  291. ),
  292. 'delete' => array(
  293. 'data' => array(
  294. 'function' => create_function('$rowData', '
  295. global $context, $txt, $scripturl;
  296. return \'<a href="\' . $scripturl . \'?action=admin;area=paidsubscribe;sa=modify;delete;sid=\' . $rowData[\'id\'] . \'">\' . $txt[\'delete\'] . \'</a>\';
  297. '),
  298. 'style' => 'text-align: center;',
  299. ),
  300. ),
  301. ),
  302. 'form' => array(
  303. 'href' => $scripturl . '?action=admin;area=paidsubscribe;sa=modify',
  304. ),
  305. 'additional_rows' => array(
  306. array(
  307. 'position' => 'below_table_data',
  308. 'value' => '
  309. <input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="button_submit" />
  310. ',
  311. 'class' => 'titlebg',
  312. 'style' => 'text-align: right;',
  313. ),
  314. ),
  315. );
  316. require_once($sourcedir . '/Subs-List.php');
  317. createList($listOptions);
  318. $context['sub_template'] = 'show_list';
  319. $context['default_list'] = 'subscription_list';
  320. }
  321. // Adding, editing and deleting subscriptions.
  322. function ModifySubscription()
  323. {
  324. global $context, $txt, $modSettings, $smcFunc;
  325. $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
  326. $context['action_type'] = $context['sub_id'] ? (isset($_REQUEST['delete']) ? 'delete' : 'edit') : 'add';
  327. // Setup the template.
  328. $context['sub_template'] = $context['action_type'] == 'delete' ? 'delete_subscription' : 'modify_subscription';
  329. $context['page_title'] = $txt['paid_' . $context['action_type'] . '_subscription'];
  330. // Delete it?
  331. if (isset($_POST['delete_confirm']) && isset($_REQUEST['delete']))
  332. {
  333. $smcFunc['db_query']('delete_subscription', '
  334. DELETE FROM {db_prefix}subscriptions
  335. WHERE id_subscribe = {int:current_subscription}',
  336. array(
  337. 'current_subscription' => $context['sub_id'],
  338. )
  339. );
  340. redirectexit('action=admin;area=paidsubscribe;view');
  341. }
  342. // Saving?
  343. if (isset($_POST['save']))
  344. {
  345. // Some cleaning...
  346. $isActive = isset($_POST['active']) ? 1 : 0;
  347. $isRepeatable = isset($_POST['repeatable']) ? 1 : 0;
  348. $allowpartial = isset($_POST['allow_partial']) ? 1 : 0;
  349. $reminder = isset($_POST['reminder']) ? (int) $_POST['reminder'] : 0;
  350. $emailComplete = strlen($_POST['emailcomplete']) > 10 ? trim($_POST['emailcomplete']) : '';
  351. // Is this a fixed one?
  352. if ($_POST['duration_type'] == 'fixed')
  353. {
  354. // Clean the span.
  355. $span = $_POST['span_value'] . $_POST['span_unit'];
  356. // Sort out the cost.
  357. $cost = array('fixed' => sprintf('%01.2f', strtr($_POST['cost'], ',', '.')));
  358. // There needs to be something.
  359. if (empty($_POST['span_value']) || empty($_POST['cost']))
  360. fatal_lang_error('paid_no_cost_value');
  361. }
  362. // Flexible is harder but more fun ;)
  363. else
  364. {
  365. $span = 'F';
  366. $cost = array(
  367. 'day' => sprintf('%01.2f', strtr($_POST['cost_day'], ',', '.')),
  368. 'week' => sprintf('%01.2f', strtr($_POST['cost_week'], ',', '.')),
  369. 'month' => sprintf('%01.2f', strtr($_POST['cost_month'], ',', '.')),
  370. 'year' => sprintf('%01.2f', strtr($_POST['cost_year'], ',', '.')),
  371. );
  372. if (empty($_POST['cost_day']) && empty($_POST['cost_week']) && empty($_POST['cost_month']) && empty($_POST['cost_year']))
  373. fatal_lang_error('paid_all_freq_blank');
  374. }
  375. $cost = serialize($cost);
  376. // Yep, time to do additional groups.
  377. $addgroups = array();
  378. if (!empty($_POST['addgroup']))
  379. foreach ($_POST['addgroup'] as $id => $dummy)
  380. $addgroups[] = (int) $id;
  381. $addgroups = implode(',', $addgroups);
  382. // Is it new?!
  383. if ($context['action_type'] == 'add')
  384. {
  385. $smcFunc['db_insert']('',
  386. '{db_prefix}subscriptions',
  387. array(
  388. 'name' => 'string-60', 'description' => 'string-255', 'active' => 'int', 'length' => 'string-4', 'cost' => 'string',
  389. 'id_group' => 'int', 'add_groups' => 'string-40', 'repeatable' => 'int', 'allow_partial' => 'int', 'email_complete' => 'string',
  390. 'reminder' => 'int',
  391. ),
  392. array(
  393. $_POST['name'], $_POST['desc'], $isActive, $span, $cost,
  394. $_POST['prim_group'], $addgroups, $isRepeatable, $allowpartial, $emailComplete,
  395. $reminder,
  396. ),
  397. array('id_subscribe')
  398. );
  399. }
  400. // Otherwise must be editing.
  401. else
  402. {
  403. // Don't do groups if there are active members
  404. $request = $smcFunc['db_query']('', '
  405. SELECT COUNT(*)
  406. FROM {db_prefix}log_subscribed
  407. WHERE id_subscribe = {int:current_subscription}
  408. AND status = {int:is_active}',
  409. array(
  410. 'current_subscription' => $context['sub_id'],
  411. 'is_active' => 1,
  412. )
  413. );
  414. list ($disableGroups) = $smcFunc['db_fetch_row']($request);
  415. $smcFunc['db_free_result']($request);
  416. $smcFunc['db_query']('substring', '
  417. UPDATE {db_prefix}subscriptions
  418. SET name = SUBSTRING({string:name}, 1, 60), description = SUBSTRING({string:description}, 1, 255), active = {int:is_active},
  419. length = SUBSTRING({string:length}, 1, 4), cost = {string:cost}' . ($disableGroups ? '' : ', id_group = {int:id_group},
  420. add_groups = {string:additional_groups}') . ', repeatable = {int:repeatable}, allow_partial = {int:allow_partial},
  421. email_complete = {string:email_complete}, reminder = {int:reminder}
  422. WHERE id_subscribe = {int:current_subscription}',
  423. array(
  424. 'is_active' => $isActive,
  425. 'id_group' => !empty($_POST['prim_group']) ? $_POST['prim_group'] : 0,
  426. 'repeatable' => $isRepeatable,
  427. 'allow_partial' => $allowpartial,
  428. 'reminder' => $reminder,
  429. 'current_subscription' => $context['sub_id'],
  430. 'name' => $_POST['name'],
  431. 'description' => $_POST['desc'],
  432. 'length' => $span,
  433. 'cost' => $cost,
  434. 'additional_groups' => !empty($addgroups) ? $addgroups : '',
  435. 'email_complete' => $emailComplete,
  436. )
  437. );
  438. }
  439. redirectexit('action=admin;area=paidsubscribe;view');
  440. }
  441. // Defaults.
  442. if ($context['action_type'] == 'add')
  443. {
  444. $context['sub'] = array(
  445. 'name' => '',
  446. 'desc' => '',
  447. 'cost' => array(
  448. 'fixed' => 0,
  449. ),
  450. 'span' => array(
  451. 'value' => '',
  452. 'unit' => 'D',
  453. ),
  454. 'prim_group' => 0,
  455. 'add_groups' => array(),
  456. 'active' => 1,
  457. 'repeatable' => 1,
  458. 'allow_partial' => 0,
  459. 'duration' => 'fixed',
  460. 'email_complete' => '',
  461. 'reminder' => 0,
  462. );
  463. }
  464. // Otherwise load up all the details.
  465. else
  466. {
  467. $request = $smcFunc['db_query']('', '
  468. SELECT name, description, cost, length, id_group, add_groups, active, repeatable, allow_partial, email_complete, reminder
  469. FROM {db_prefix}subscriptions
  470. WHERE id_subscribe = {int:current_subscription}
  471. LIMIT 1',
  472. array(
  473. 'current_subscription' => $context['sub_id'],
  474. )
  475. );
  476. while ($row = $smcFunc['db_fetch_assoc']($request))
  477. {
  478. // Sort the date.
  479. preg_match('~(\d*)(\w)~', $row['length'], $match);
  480. if (isset($match[2]))
  481. {
  482. $span_value = $match[1];
  483. $span_unit = $match[2];
  484. }
  485. else
  486. {
  487. $span_value = 0;
  488. $span_unit = 'D';
  489. }
  490. // Is this a flexible one?
  491. if ($row['length'] == 'F')
  492. $isFlexible = true;
  493. else
  494. $isFlexible = false;
  495. $context['sub'] = array(
  496. 'name' => $row['name'],
  497. 'desc' => $row['description'],
  498. 'cost' => @unserialize($row['cost']),
  499. 'span' => array(
  500. 'value' => $span_value,
  501. 'unit' => $span_unit,
  502. ),
  503. 'prim_group' => $row['id_group'],
  504. 'add_groups' => explode(',', $row['add_groups']),
  505. 'active' => $row['active'],
  506. 'repeatable' => $row['repeatable'],
  507. 'allow_partial' => $row['allow_partial'],
  508. 'duration' => $isFlexible ? 'flexible' : 'fixed',
  509. 'email_complete' => htmlspecialchars($row['email_complete']),
  510. 'reminder' => $row['reminder'],
  511. );
  512. }
  513. $smcFunc['db_free_result']($request);
  514. // Does this have members who are active?
  515. $request = $smcFunc['db_query']('', '
  516. SELECT COUNT(*)
  517. FROM {db_prefix}log_subscribed
  518. WHERE id_subscribe = {int:current_subscription}
  519. AND status = {int:is_active}',
  520. array(
  521. 'current_subscription' => $context['sub_id'],
  522. 'is_active' => 1,
  523. )
  524. );
  525. list ($context['disable_groups']) = $smcFunc['db_fetch_row']($request);
  526. $smcFunc['db_free_result']($request);
  527. }
  528. // Load up all the groups.
  529. $request = $smcFunc['db_query']('', '
  530. SELECT id_group, group_name
  531. FROM {db_prefix}membergroups
  532. WHERE id_group != {int:moderator_group}
  533. AND min_posts = {int:min_posts}',
  534. array(
  535. 'moderator_group' => 3,
  536. 'min_posts' => -1,
  537. )
  538. );
  539. $context['groups'] = array();
  540. while ($row = $smcFunc['db_fetch_assoc']($request))
  541. $context['groups'][$row['id_group']] = $row['group_name'];
  542. $smcFunc['db_free_result']($request);
  543. }
  544. // View all the users subscribed to a particular subscription!
  545. function ViewSubscribedUsers()
  546. {
  547. global $context, $txt, $modSettings, $scripturl, $options, $smcFunc, $sourcedir;
  548. // Setup the template.
  549. $context['page_title'] = $txt['viewing_users_subscribed'];
  550. // ID of the subscription.
  551. $context['sub_id'] = (int) $_REQUEST['sid'];
  552. // Load the subscription information.
  553. $request = $smcFunc['db_query']('', '
  554. SELECT id_subscribe, name, description, cost, length, id_group, add_groups, active
  555. FROM {db_prefix}subscriptions
  556. WHERE id_subscribe = {int:current_subscription}',
  557. array(
  558. 'current_subscription' => $context['sub_id'],
  559. )
  560. );
  561. // Something wrong?
  562. if ($smcFunc['db_num_rows']($request) == 0)
  563. fatal_lang_error('no_access', false);
  564. // Do the subscription context.
  565. $row = $smcFunc['db_fetch_assoc']($request);
  566. $context['subscription'] = array(
  567. 'id' => $row['id_subscribe'],
  568. 'name' => $row['name'],
  569. 'desc' => $row['description'],
  570. 'active' => $row['active'],
  571. );
  572. $smcFunc['db_free_result']($request);
  573. // Are we searching for people?
  574. $search_string = isset($_POST['ssearch']) && !empty($_POST['sub_search']) ? ' AND IFNULL(mem.real_name, {string:guest}) LIKE {string:search}' : '';
  575. $search_vars = empty($_POST['sub_search']) ? array() : array('search' => '%' . $_POST['sub_search'] . '%', 'guest' => $txt['guest']);
  576. $listOptions = array(
  577. 'id' => 'subscribed_users_list',
  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' => 'text-align: left; 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' => 'text-align: left; 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' => 'text-align: left; width: 10%;',
  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' => 'text-align: left; 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' => 'text-align: left; 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. ),
  673. 'data' => array(
  674. 'function' => create_function('$rowData', '
  675. global $context, $txt, $scripturl;
  676. return \'<a href="\' . $scripturl . \'?action=admin;area=paidsubscribe;sa=modifyuser;lid=\' . $rowData[\'id\'] . \'">\' . $txt[\'modify\'] . \'</a>\';
  677. '),
  678. 'style' => 'text-align: center;',
  679. ),
  680. ),
  681. 'delete' => array(
  682. 'header' => array(
  683. 'style' => 'width: 4%;',
  684. ),
  685. 'data' => array(
  686. 'function' => create_function('$rowData', '
  687. global $context, $txt, $scripturl;
  688. return \'<input type="checkbox" name="delsub[\' . $rowData[\'id\'] . \']" class="input_check" />\';
  689. '),
  690. 'style' => 'text-align: center;',
  691. ),
  692. ),
  693. ),
  694. 'form' => array(
  695. 'href' => $scripturl . '?action=admin;area=paidsubscribe;sa=modifyuser;sid=' . $context['sub_id'],
  696. ),
  697. 'additional_rows' => array(
  698. array(
  699. 'position' => 'below_table_data',
  700. 'value' => '
  701. <div class="floatleft">
  702. <input type="submit" name="add" value="' . $txt['paid_add_subscription'] . '" class="button_submit" />
  703. </div>
  704. <div class="floatright">
  705. <input type="submit" name="finished" value="' . $txt['complete_selected'] . '" onclick="return confirm(\'' . $txt['complete_are_sure'] . '\');" class="button_submit" />
  706. <input type="submit" name="delete" value="' . $txt['delete_selected'] . '" onclick="return confirm(\'' . $txt['delete_are_sure'] . '\');" class="button_submit" />
  707. </div>
  708. ',
  709. 'class' => 'titlebg',
  710. ),
  711. array(
  712. 'position' => 'top_of_list',
  713. 'value' => '
  714. <div class="floatleft">
  715. ' . sprintf($txt['view_users_subscribed'], $row['name']) . '
  716. </div>
  717. <div class="floatright">
  718. <input type="text" name="sub_search" value="" class="input_text" />
  719. <input type="submit" name="ssearch" value="' . $txt['search_sub'] . '" class="button_submit" />
  720. </div>
  721. ',
  722. 'class' => 'titlebg',
  723. ),
  724. ),
  725. );
  726. require_once($sourcedir . '/Subs-List.php');
  727. createList($listOptions);
  728. $context['sub_template'] = 'show_list';
  729. $context['default_list'] = 'subscribed_users_list';
  730. }
  731. // Returns how many people are subscribed to a paid subscription.
  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. function list_getSubscribedUsers($start, $items_per_page, $sort, $id_sub, $search_string, $search_vars = array())
  753. {
  754. global $smcFunc, $txt;
  755. $request = $smcFunc['db_query']('', '
  756. SELECT ls.id_sublog, IFNULL(mem.id_member, 0) AS id_member, IFNULL(mem.real_name, {string:guest}) AS name, ls.start_time, ls.end_time,
  757. ls.status, ls.payments_pending
  758. FROM {db_prefix}log_subscribed AS ls
  759. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  760. WHERE ls.id_subscribe = {int:current_subscription} ' . $search_string . '
  761. AND (ls.end_time != {int:no_end_time} OR ls.payments_pending != {int:no_payments_pending})
  762. ORDER BY ' . $sort . '
  763. LIMIT ' . $start . ', ' . $items_per_page,
  764. array_merge($search_vars, array(
  765. 'current_subscription' => $id_sub,
  766. 'no_end_time' => 0,
  767. 'no_payments_pending' => 0,
  768. 'guest' => $txt['guest'],
  769. ))
  770. );
  771. $subscribers = array();
  772. while ($row = $smcFunc['db_fetch_assoc']($request))
  773. $subscribers[] = array(
  774. 'id' => $row['id_sublog'],
  775. 'id_member' => $row['id_member'],
  776. 'name' => $row['name'],
  777. 'start_date' => timeformat($row['start_time'], false),
  778. 'end_date' => $row['end_time'] == 0 ? 'N/A' : timeformat($row['end_time'], false),
  779. 'pending' => $row['payments_pending'],
  780. 'status' => $row['status'],
  781. 'status_text' => $row['status'] == 0 ? ($row['payments_pending'] == 0 ? $txt['paid_finished'] : $txt['paid_pending']) : $txt['paid_active'],
  782. );
  783. $smcFunc['db_free_result']($request);
  784. return $subscribers;
  785. }
  786. // Edit or add a user subscription.
  787. function ModifyUserSubscription()
  788. {
  789. global $context, $txt, $modSettings, $smcFunc;
  790. loadSubscriptions();
  791. $context['log_id'] = isset($_REQUEST['lid']) ? (int) $_REQUEST['lid'] : 0;
  792. $context['sub_id'] = isset($_REQUEST['sid']) ? (int) $_REQUEST['sid'] : 0;
  793. $context['action_type'] = $context['log_id'] ? 'edit' : 'add';
  794. // Setup the template.
  795. $context['sub_template'] = 'modify_user_subscription';
  796. $context['page_title'] = $txt[$context['action_type'] . '_subscriber'];
  797. // If we haven't been passed the subscription ID get it.
  798. if ($context['log_id'] && !$context['sub_id'])
  799. {
  800. $request = $smcFunc['db_query']('', '
  801. SELECT id_subscribe
  802. FROM {db_prefix}log_subscribed
  803. WHERE id_sublog = {int:current_log_item}',
  804. array(
  805. 'current_log_item' => $context['log_id'],
  806. )
  807. );
  808. if ($smcFunc['db_num_rows']($request) == 0)
  809. fatal_lang_error('no_access', false);
  810. list ($context['sub_id']) = $smcFunc['db_fetch_row']($request);
  811. $smcFunc['db_free_result']($request);
  812. }
  813. if (!isset($context['subscriptions'][$context['sub_id']]))
  814. fatal_lang_error('no_access', false);
  815. $context['current_subscription'] = $context['subscriptions'][$context['sub_id']];
  816. // Searching?
  817. if (isset($_POST['ssearch']))
  818. {
  819. return ViewSubscribedUsers();
  820. }
  821. // Saving?
  822. elseif (isset($_REQUEST['save_sub']))
  823. {
  824. // Work out the dates...
  825. $starttime = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'], $_POST['day'], $_POST['year']);
  826. $endtime = mktime($_POST['hourend'], $_POST['minuteend'], 0, $_POST['monthend'], $_POST['dayend'], $_POST['yearend']);
  827. // Status.
  828. $status = $_POST['status'];
  829. // New one?
  830. if (empty($context['log_id']))
  831. {
  832. // Find the user...
  833. $request = $smcFunc['db_query']('', '
  834. SELECT id_member, id_group
  835. FROM {db_prefix}members
  836. WHERE real_name = {string:name}
  837. LIMIT 1',
  838. array(
  839. 'name' => $_POST['name'],
  840. )
  841. );
  842. if ($smcFunc['db_num_rows']($request) == 0)
  843. fatal_lang_error('error_member_not_found');
  844. list ($id_member, $id_group) = $smcFunc['db_fetch_row']($request);
  845. $smcFunc['db_free_result']($request);
  846. // Ensure the member doesn't already have a subscription!
  847. $request = $smcFunc['db_query']('', '
  848. SELECT id_subscribe
  849. FROM {db_prefix}log_subscribed
  850. WHERE id_subscribe = {int:current_subscription}
  851. AND id_member = {int:current_member}',
  852. array(
  853. 'current_subscription' => $context['sub_id'],
  854. 'current_member' => $id_member,
  855. )
  856. );
  857. if ($smcFunc['db_num_rows']($request) != 0)
  858. fatal_lang_error('member_already_subscribed');
  859. $smcFunc['db_free_result']($request);
  860. // Actually put the subscription in place.
  861. if ($status == 1)
  862. addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
  863. else
  864. {
  865. $smcFunc['db_insert']('',
  866. '{db_prefix}log_subscribed',
  867. array(
  868. 'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
  869. 'end_time' => 'int', 'status' => 'int',
  870. ),
  871. array(
  872. $context['sub_id'], $id_member, $id_group, $starttime,
  873. $endtime, $status,
  874. ),
  875. array('id_sublog')
  876. );
  877. }
  878. }
  879. // Updating.
  880. else
  881. {
  882. $request = $smcFunc['db_query']('', '
  883. SELECT id_member, status
  884. FROM {db_prefix}log_subscribed
  885. WHERE id_sublog = {int:current_log_item}',
  886. array(
  887. 'current_log_item' => $context['log_id'],
  888. )
  889. );
  890. if ($smcFunc['db_num_rows']($request) == 0)
  891. fatal_lang_error('no_access', false);
  892. list ($id_member, $old_status) = $smcFunc['db_fetch_row']($request);
  893. $smcFunc['db_free_result']($request);
  894. // Pick the right permission stuff depending on what the status is changing from/to.
  895. if ($old_status == 1 && $status != 1)
  896. removeSubscription($context['sub_id'], $id_member);
  897. elseif ($status == 1 && $old_status != 1)
  898. {
  899. addSubscription($context['sub_id'], $id_member, 0, $starttime, $endtime);
  900. }
  901. else
  902. {
  903. $smcFunc['db_query']('', '
  904. UPDATE {db_prefix}log_subscribed
  905. SET start_time = {int:start_time}, end_time = {int:end_time}, status = {int:status}
  906. WHERE id_sublog = {int:current_log_item}',
  907. array(
  908. 'start_time' => $starttime,
  909. 'end_time' => $endtime,
  910. 'status' => $status,
  911. 'current_log_item' => $context['log_id'],
  912. )
  913. );
  914. }
  915. }
  916. // Done - redirect...
  917. redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
  918. }
  919. // Deleting?
  920. elseif (isset($_REQUEST['delete']) || isset($_REQUEST['finished']))
  921. {
  922. // Do the actual deletes!
  923. if (!empty($_REQUEST['delsub']))
  924. {
  925. $toDelete = array();
  926. foreach ($_REQUEST['delsub'] as $id => $dummy)
  927. $toDelete[] = (int) $id;
  928. $request = $smcFunc['db_query']('', '
  929. SELECT id_subscribe, id_member
  930. FROM {db_prefix}log_subscribed
  931. WHERE id_sublog IN ({array_int:subscription_list})',
  932. array(
  933. 'subscription_list' => $toDelete,
  934. )
  935. );
  936. while ($row = $smcFunc['db_fetch_assoc']($request))
  937. removeSubscription($row['id_subscribe'], $row['id_member'], isset($_REQUEST['delete']));
  938. $smcFunc['db_free_result']($request);
  939. }
  940. redirectexit('action=admin;area=paidsubscribe;sa=viewsub;sid=' . $context['sub_id']);
  941. }
  942. // Default attributes.
  943. if ($context['action_type'] == 'add')
  944. {
  945. $context['sub'] = array(
  946. 'id' => 0,
  947. 'start' => array(
  948. 'year' => (int) strftime('%Y', time()),
  949. 'month' => (int) strftime('%m', time()),
  950. 'day' => (int) strftime('%d', time()),
  951. 'hour' => (int) strftime('%H', time()),
  952. 'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
  953. 'last_day' => 0,
  954. ),
  955. 'end' => array(
  956. 'year' => (int) strftime('%Y', time()),
  957. 'month' => (int) strftime('%m', time()),
  958. 'day' => (int) strftime('%d', time()),
  959. 'hour' => (int) strftime('%H', time()),
  960. 'min' => (int) strftime('%M', time()) < 10 ? '0' . (int) strftime('%M', time()) : (int) strftime('%M', time()),
  961. 'last_day' => 0,
  962. ),
  963. 'status' => 1,
  964. );
  965. $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']));
  966. $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']));
  967. if (isset($_GET['uid']))
  968. {
  969. $request = $smcFunc['db_query']('', '
  970. SELECT real_name
  971. FROM {db_prefix}members
  972. WHERE id_member = {int:current_member}',
  973. array(
  974. 'current_member' => (int) $_GET['uid'],
  975. )
  976. );
  977. list ($context['sub']['username']) = $smcFunc['db_fetch_row']($request);
  978. $smcFunc['db_free_result']($request);
  979. }
  980. else
  981. $context['sub']['username'] = '';
  982. }
  983. // Otherwise load the existing info.
  984. else
  985. {
  986. $request = $smcFunc['db_query']('', '
  987. SELECT ls.id_sublog, ls.id_subscribe, ls.id_member, start_time, end_time, status, payments_pending, pending_details,
  988. IFNULL(mem.real_name, {string:blank_string}) AS username
  989. FROM {db_prefix}log_subscribed AS ls
  990. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ls.id_member)
  991. WHERE ls.id_sublog = {int:current_subscription_item}
  992. LIMIT 1',
  993. array(
  994. 'current_subscription_item' => $context['log_id'],
  995. 'blank_string' => '',
  996. )
  997. );
  998. if ($smcFunc['db_num_rows']($request) == 0)
  999. fatal_lang_error('no_access', false);
  1000. $row = $smcFunc['db_fetch_assoc']($request);
  1001. $smcFunc['db_free_result']($request);
  1002. // Any pending payments?
  1003. $context['pending_payments'] = array();
  1004. if (!empty($row['pending_details']))
  1005. {
  1006. $pending_details = @unserialize($row['pending_details']);
  1007. foreach ($pending_details as $id => $pending)
  1008. {
  1009. // Only this type need be displayed.
  1010. if ($pending[3] == 'payback')
  1011. {
  1012. // Work out what the options were.
  1013. $costs = @unserialize($context['current_subscription']['real_cost']);
  1014. if ($context['current_subscription']['real_length'] == 'F')
  1015. {
  1016. foreach ($costs as $duration => $cost)
  1017. {
  1018. if ($cost != 0 && $cost == $pending[1] && $duration == $pending[2])
  1019. $context['pending_payments'][$id] = array(
  1020. 'desc' => sprintf($modSettings['paid_currency_symbol'], $cost . '/' . $txt[$duration]),
  1021. );
  1022. }
  1023. }
  1024. elseif ($costs['fixed'] == $pending[1])
  1025. {
  1026. $context['pending_payments'][$id] = array(
  1027. 'desc' => sprintf($modSettings['paid_currency_symbol'], $costs['fixed']),
  1028. );
  1029. }
  1030. }
  1031. }
  1032. // Check if we are adding/removing any.
  1033. if (isset($_GET['pending']))
  1034. {
  1035. foreach ($pending_details as $id => $pending)
  1036. {
  1037. // Found the one to action?
  1038. if ($_GET['pending'] == $id && $pending[3] == 'payback' && isset($context['pending_payments'][$id]))
  1039. {
  1040. // Flexible?
  1041. if (isset($_GET['accept']))
  1042. addSubscription($context['current_subscription']['id'], $row['id_member'], $context['current_subscription']['real_length'] == 'F' ? strtoupper(substr($pending[2], 0, 1)) : 0);
  1043. unset($pending_details[$id]);
  1044. $new_details = serialize($pending_details);
  1045. // Update the entry.
  1046. $smcFunc['db_query']('', '
  1047. UPDATE {db_prefix}log_subscribed
  1048. SET payments_pending = payments_pending - 1, pending_details = {string:pending_details}
  1049. WHERE id_sublog = {int:current_subscription_item}',
  1050. array(
  1051. 'current_subscription_item' => $context['log_id'],
  1052. 'pending_details' => $new_details,
  1053. )
  1054. );
  1055. // Reload
  1056. redirectexit('action=admin;area=paidsubscribe;sa=modifyuser;lid=' . $context['log_id']);
  1057. }
  1058. }
  1059. }
  1060. }
  1061. $context['sub_id'] = $row['id_subscribe'];
  1062. $context['sub'] = array(
  1063. 'id' => 0,
  1064. 'start' => array(
  1065. 'year' => (int) strftime('%Y', $row['start_time']),
  1066. 'month' => (int) strftime('%m', $row['start_time']),
  1067. 'day' => (int) strftime('%d', $row['start_time']),
  1068. 'hour' => (int) strftime('%H', $row['start_time']),
  1069. 'min' => (int) strftime('%M', $row['start_time']) < 10 ? '0' . (int) strftime('%M', $row['start_time']) : (int) strftime('%M', $row['start_time']),
  1070. 'last_day' => 0,
  1071. ),
  1072. 'end' => array(
  1073. 'year' => (int) strftime('%Y', $row['end_time']),
  1074. 'month' => (int) strftime('%m', $row['end_time']),
  1075. 'day' => (int) strftime('%d', $row['end_time']),
  1076. 'hour' => (int) strftime('%H', $row['end_time']),
  1077. 'min' => (int) strftime('%M', $row['end_time']) < 10 ? '0' . (int) strftime('%M', $row['end_time']) : (int) strftime('%M', $row['end_time']),
  1078. 'last_day' => 0,
  1079. ),
  1080. 'status' => $row['status'],
  1081. 'username' => $row['username'],
  1082. );
  1083. $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']));
  1084. $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']));
  1085. }
  1086. }
  1087. // Re-apply subscription rules.
  1088. function reapplySubscriptions($users)
  1089. {
  1090. global $smcFunc;
  1091. // Make it an array.
  1092. if (!is_array($users))
  1093. $users = array($users);
  1094. // Get all the members current groups.
  1095. $groups = array();
  1096. $request = $smcFunc['db_query']('', '
  1097. SELECT id_member, id_group, additional_groups
  1098. FROM {db_prefix}members
  1099. WHERE id_member IN ({array_int:user_list})',
  1100. array(
  1101. 'user_list' => $users,
  1102. )
  1103. );
  1104. while ($row = $smcFunc['db_fetch_assoc']($request))
  1105. {
  1106. $groups[$row['id_member']] = array(
  1107. 'primary' => $row['id_group'],
  1108. 'additional' => explode(',', $row['additional_groups']),
  1109. );
  1110. }
  1111. $smcFunc['db_free_result']($request);
  1112. $request = $smcFunc['db_query']('', '
  1113. SELECT ls.id_member, ls.old_id_group, s.id_group, s.add_groups
  1114. FROM {db_prefix}log_subscribed AS ls
  1115. INNER JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe)
  1116. WHERE ls.id_member IN ({array_int:user_list})
  1117. AND ls.end_time > {int:current_time}',
  1118. array(
  1119. 'user_list' => $users,
  1120. 'current_time' => time(),
  1121. )
  1122. );
  1123. while ($row = $smcFunc['db_fetch_assoc']($request))
  1124. {
  1125. // Specific primary group?
  1126. if ($row['id_group'] != 0)
  1127. {
  1128. // If this is changing - add the old one to the additional groups so it's not lost.
  1129. if ($row['id_group'] != $groups[$row['id_member']]['primary'])
  1130. $groups[$row['id_member']]['additional'][] = $groups[$row['id_member']]['primary'];
  1131. $groups[$row['id_member']]['primary'] = $row['id_group'];
  1132. }
  1133. // Additional groups.
  1134. if (!empty($row['add_groups']))
  1135. $groups[$row['id_member']]['additional'] = array_merge($groups[$row['id_member']]['additional'], explode(',', $row['add_groups']));
  1136. }
  1137. $smcFunc['db_free_result']($request);
  1138. // Update all the members.
  1139. foreach ($groups as $id => $group)
  1140. {
  1141. $group['additional'] = array_unique($group['additional']);
  1142. foreach ($group['additional'] as $key => $value)
  1143. if (empty($value))
  1144. unset($group['additional'][$key]);
  1145. $addgroups = implode(',', $group['additional']);
  1146. $smcFunc['db_query']('', '
  1147. UPDATE {db_prefix}members
  1148. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  1149. WHERE id_member = {int:current_member}
  1150. LIMIT 1',
  1151. array(
  1152. 'primary_group' => $group['primary'],
  1153. 'current_member' => $id,
  1154. 'additional_groups' => $addgroups,
  1155. )
  1156. );
  1157. }
  1158. }
  1159. // Add or extend a subscription of a user.
  1160. function addSubscription($id_subscribe, $id_member, $renewal = 0, $forceStartTime = 0, $forceEndTime = 0)
  1161. {
  1162. global $context, $smcFunc;
  1163. // Take the easy way out...
  1164. loadSubscriptions();
  1165. // Exists, yes?
  1166. if (!isset($context['subscriptions'][$id_subscribe]))
  1167. return;
  1168. $curSub = $context['subscriptions'][$id_subscribe];
  1169. // Grab the duration.
  1170. $duration = $curSub['num_length'];
  1171. // If this is a renewal change the duration to be correct.
  1172. if (!empty($renewal))
  1173. {
  1174. switch ($renewal)
  1175. {
  1176. case 'D':
  1177. $duration = 86400;
  1178. break;
  1179. case 'W':
  1180. $duration = 604800;
  1181. break;
  1182. case 'M':
  1183. $duration = 2629743;
  1184. break;
  1185. case 'Y':
  1186. $duration = 31556926;
  1187. break;
  1188. default:
  1189. break;
  1190. }
  1191. }
  1192. // Firstly, see whether it exists, and is active. If so then this is meerly an extension.
  1193. $request = $smcFunc['db_query']('', '
  1194. SELECT id_sublog, end_time, start_time
  1195. FROM {db_prefix}log_subscribed
  1196. WHERE id_subscribe = {int:current_subscription}
  1197. AND id_member = {int:current_member}
  1198. AND status = {int:is_active}',
  1199. array(
  1200. 'current_subscription' => $id_subscribe,
  1201. 'current_member' => $id_member,
  1202. 'is_active' => 1,
  1203. )
  1204. );
  1205. if ($smcFunc['db_num_rows']($request) != 0)
  1206. {
  1207. list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);
  1208. // If this has already expired but is active, extension means the period from now.
  1209. if ($endtime < time())
  1210. $endtime = time();
  1211. if ($starttime == 0)
  1212. $starttime = time();
  1213. // Work out the new expiry date.
  1214. $endtime += $duration;
  1215. if ($forceEndTime != 0)
  1216. $endtime = $forceEndTime;
  1217. // As everything else should be good, just update!
  1218. $smcFunc['db_query']('', '
  1219. UPDATE {db_prefix}log_subscribed
  1220. SET end_time = {int:end_time}, start_time = {int:start_time}
  1221. WHERE id_sublog = {int:current_subscription_item}',
  1222. array(
  1223. 'end_time' => $endtime,
  1224. 'start_time' => $starttime,
  1225. 'current_subscription_item' => $id_sublog,
  1226. )
  1227. );
  1228. return;
  1229. }
  1230. $smcFunc['db_free_result']($request);
  1231. // If we're here, that means we don't have an active subscription - that means we need to do some work!
  1232. $request = $smcFunc['db_query']('', '
  1233. SELECT m.id_group, m.additional_groups
  1234. FROM {db_prefix}members AS m
  1235. WHERE m.id_member = {int:current_member}',
  1236. array(
  1237. 'current_member' => $id_member,
  1238. )
  1239. );
  1240. // Just in case the member doesn't exist.
  1241. if ($smcFunc['db_num_rows']($request) == 0)
  1242. return;
  1243. list ($old_id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
  1244. $smcFunc['db_free_result']($request);
  1245. // Prepare additional groups.
  1246. $newAddGroups = explode(',', $curSub['add_groups']);
  1247. $curAddGroups = explode(',', $additional_groups);
  1248. $newAddGroups = array_merge($newAddGroups, $curAddGroups);
  1249. // Simple, simple, simple - hopefully... id_group first.
  1250. if ($curSub['prim_group'] != 0)
  1251. {
  1252. $id_group = $curSub['prim_group'];
  1253. // Ensure their old privileges are maintained.
  1254. if ($old_id_group != 0)
  1255. $newAddGroups[] = $old_id_group;
  1256. }
  1257. else
  1258. $id_group = $old_id_group;
  1259. // Yep, make sure it's unique, and no empties.
  1260. foreach ($newAddGroups as $k => $v)
  1261. if (empty($v))
  1262. unset($newAddGroups[$k]);
  1263. $newAddGroups = array_unique($newAddGroups);
  1264. $newAddGroups = implode(',', $newAddGroups);
  1265. // Store the new settings.
  1266. $smcFunc['db_query']('', '
  1267. UPDATE {db_prefix}members
  1268. SET id_group = {int:primary_group}, additional_groups = {string:additional_groups}
  1269. WHERE id_member = {int:current_member}',
  1270. array(
  1271. 'primary_group' => $id_group,
  1272. 'current_member' => $id_member,
  1273. 'additional_groups' => $newAddGroups,
  1274. )
  1275. );
  1276. // Now log the subscription - maybe we have a dorment subscription we can restore?
  1277. $request = $smcFunc['db_query']('', '
  1278. SELECT id_sublog, end_time, start_time
  1279. FROM {db_prefix}log_subscribed
  1280. WHERE id_subscribe = {int:current_subscription}
  1281. AND id_member = {int:current_member}',
  1282. array(
  1283. 'current_subscription' => $id_subscribe,
  1284. 'current_member' => $id_member,
  1285. )
  1286. );
  1287. //!!! Don't really need to do this twice...
  1288. if ($smcFunc['db_num_rows']($request) != 0)
  1289. {
  1290. list ($id_sublog, $endtime, $starttime) = $smcFunc['db_fetch_row']($request);
  1291. // If this has already expired but is active, extension means the period from now.
  1292. if ($endtime < time())
  1293. $endtime = time();
  1294. if ($starttime == 0)
  1295. $starttime = time();
  1296. // Work out the new expiry date.
  1297. $endtime += $duration;
  1298. if ($forceEndTime != 0)
  1299. $endtime = $forceEndTime;
  1300. // As everything else should be good, just update!
  1301. $smcFunc['db_query']('', '
  1302. UPDATE {db_prefix}log_subscribed
  1303. SET start_time = {int:start_time}, end_time = {int:end_time}, old_id_group = {int:old_id_group}, status = {int:is_active},
  1304. reminder_sent = {int:no_reminder_sent}
  1305. WHERE id_sublog = {int:current_subscription_item}',
  1306. array(
  1307. 'start_time' => $starttime,
  1308. 'end_time' => $endtime,
  1309. 'old_id_group' => $old_id_group,
  1310. 'is_active' => 1,
  1311. 'no_reminder_sent' => 0,
  1312. 'current_subscription_item' => $id_sublog,
  1313. )
  1314. );
  1315. return;
  1316. }
  1317. $smcFunc['db_free_result']($request);
  1318. // Otherwise a very simple insert.
  1319. $endtime = time() + $duration;
  1320. if ($forceEndTime != 0)
  1321. $endtime = $forceEndTime;
  1322. if ($forceStartTime == 0)
  1323. $starttime = time();
  1324. else
  1325. $starttime = $forceStartTime;
  1326. $smcFunc['db_insert']('',
  1327. '{db_prefix}log_subscribed',
  1328. array(
  1329. 'id_subscribe' => 'int', 'id_member' => 'int', 'old_id_group' => 'int', 'start_time' => 'int',
  1330. 'end_time' => 'int', 'status' => 'int',
  1331. ),
  1332. array(
  1333. $id_subscribe, $id_member, $old_id_group, $starttime,
  1334. $endtime, 1,
  1335. ),
  1336. array('id_sublog')
  1337. );
  1338. }
  1339. // Removes a subscription from a user, as in removes the groups.
  1340. function removeSubscription($id_subscribe, $id_member, $delete = false)
  1341. {
  1342. global $context, $smcFunc;
  1343. loadSubscriptions();
  1344. // Load the user core bits.
  1345. $request = $smcFunc['db_query']('', '
  1346. SELECT m.id_group, m.additional_groups
  1347. FROM {db_prefix}members AS m
  1348. WHERE m.id_member = {int:current_member}',
  1349. array(
  1350. 'current_member' => $id_member,
  1351. )
  1352. );
  1353. // Just in case of errors.
  1354. if ($smcFunc['db_num_rows']($request) == 0)
  1355. {
  1356. $smcFunc['db_query']('', '
  1357. DELETE FROM {db_prefix}log_subscribed
  1358. WHERE id_member = {int:current_member}',
  1359. array(
  1360. 'current_member' => $id_member,
  1361. )
  1362. );
  1363. return;
  1364. }
  1365. list ($id_group, $additional_groups) = $smcFunc['db_fetch_row']($request);
  1366. $smcFunc['db_free_result']($request);
  1367. // Get all of the subscriptions for this user that are active - it will be necessary!
  1368. $request = $smcFunc['db_query']('', '
  1369. SELECT id_subscribe, old_id_group
  1370. FROM {db_prefix}log_subscribed
  1371. WHERE id_member = {int:current_member}
  1372. AND status = {int:is_active}',
  1373. array(
  1374. 'current_member' => $id_member,
  1375. 'is_active' => 1,
  1376. )
  1377. );
  1378. // These variables will be handy, honest ;)
  1379. $removals = array();
  1380. $allowed = array();
  1381. $old_id_group = 0;
  1382. $new_id_group = -1;
  1383. while ($row = $smcFunc['d…

Large files files are truncated, but you can click here to view the full file