ManagePaid.php 54 KB

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