Profile-Actions.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. <?php
  2. /**
  3. * This file handles actions made on a user's profile.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Activate an account.
  18. *
  19. * @param int $memID the member ID
  20. */
  21. function activateAccount($memID)
  22. {
  23. global $sourcedir, $context, $user_profile, $modSettings;
  24. isAllowedTo('moderate_forum');
  25. if (isset($_REQUEST['save']) && isset($user_profile[$memID]['is_activated']) && $user_profile[$memID]['is_activated'] != 1)
  26. {
  27. // If we are approving the deletion of an account, we do something special ;)
  28. if ($user_profile[$memID]['is_activated'] == 4)
  29. {
  30. require_once($sourcedir . '/Subs-Members.php');
  31. deleteMembers($context['id_member']);
  32. redirectexit();
  33. }
  34. // Let the integrations know of the activation.
  35. call_integration_hook('integrate_activate', array($user_profile[$memID]['member_name']));
  36. // Actually update this member now, as it guarantees the unapproved count can't get corrupted.
  37. updateMemberData($context['id_member'], array('is_activated' => $user_profile[$memID]['is_activated'] >= 10 ? 11 : 1, 'validation_code' => ''));
  38. // If we are doing approval, update the stats for the member just in case.
  39. if (in_array($user_profile[$memID]['is_activated'], array(3, 4, 13, 14)))
  40. updateSettings(array('unapprovedMembers' => ($modSettings['unapprovedMembers'] > 1 ? $modSettings['unapprovedMembers'] - 1 : 0)));
  41. // Make sure we update the stats too.
  42. updateStats('member', false);
  43. }
  44. // Leave it be...
  45. redirectexit('action=profile;u=' . $memID . ';area=summary');
  46. }
  47. /**
  48. * Issue/manage an user's warning status.
  49. *
  50. * @param int $memID
  51. */
  52. function issueWarning($memID)
  53. {
  54. global $txt, $scripturl, $modSettings, $user_info, $mbname;
  55. global $context, $cur_profile, $memberContext, $smcFunc, $sourcedir;
  56. // Get all the actual settings.
  57. list ($modSettings['warning_enable'], $modSettings['user_limit']) = explode(',', $modSettings['warning_settings']);
  58. // This stores any legitimate errors.
  59. $issueErrors = array();
  60. // Doesn't hurt to be overly cautious.
  61. if (empty($modSettings['warning_enable']) || ($context['user']['is_owner'] && !$cur_profile['warning']) || !allowedTo('issue_warning'))
  62. fatal_lang_error('no_access', false);
  63. // Make sure things which are disabled stay disabled.
  64. $modSettings['warning_watch'] = !empty($modSettings['warning_watch']) ? $modSettings['warning_watch'] : 110;
  65. $modSettings['warning_moderate'] = !empty($modSettings['warning_moderate']) && !empty($modSettings['postmod_active']) ? $modSettings['warning_moderate'] : 110;
  66. $modSettings['warning_mute'] = !empty($modSettings['warning_mute']) ? $modSettings['warning_mute'] : 110;
  67. $context['warning_limit'] = allowedTo('admin_forum') ? 0 : $modSettings['user_limit'];
  68. $context['member']['warning'] = $cur_profile['warning'];
  69. $context['member']['name'] = $cur_profile['real_name'];
  70. // What are the limits we can apply?
  71. $context['min_allowed'] = 0;
  72. $context['max_allowed'] = 100;
  73. if ($context['warning_limit'] > 0)
  74. {
  75. // Make sure we cannot go outside of our limit for the day.
  76. $request = $smcFunc['db_query']('', '
  77. SELECT SUM(counter)
  78. FROM {db_prefix}log_comments
  79. WHERE id_recipient = {int:selected_member}
  80. AND id_member = {int:current_member}
  81. AND comment_type = {string:warning}
  82. AND log_time > {int:day_time_period}',
  83. array(
  84. 'current_member' => $user_info['id'],
  85. 'selected_member' => $memID,
  86. 'day_time_period' => time() - 86400,
  87. 'warning' => 'warning',
  88. )
  89. );
  90. list ($current_applied) = $smcFunc['db_fetch_row']($request);
  91. $smcFunc['db_free_result']($request);
  92. $context['min_allowed'] = max(0, $cur_profile['warning'] - $current_applied - $context['warning_limit']);
  93. $context['max_allowed'] = min(100, $cur_profile['warning'] - $current_applied + $context['warning_limit']);
  94. }
  95. // Defaults.
  96. $context['warning_data'] = array(
  97. 'reason' => '',
  98. 'notify' => '',
  99. 'notify_subject' => '',
  100. 'notify_body' => '',
  101. );
  102. // Are we saving?
  103. if (isset($_POST['save']))
  104. {
  105. // Security is good here.
  106. checkSession('post');
  107. // This cannot be empty!
  108. $_POST['warn_reason'] = isset($_POST['warn_reason']) ? trim($_POST['warn_reason']) : '';
  109. if ($_POST['warn_reason'] == '' && !$context['user']['is_owner'])
  110. $issueErrors[] = 'warning_no_reason';
  111. $_POST['warn_reason'] = $smcFunc['htmlspecialchars']($_POST['warn_reason']);
  112. // If the value hasn't changed it's either no JS or a real no change (Which this will pass)
  113. if ($_POST['warning_level'] == 'SAME')
  114. $_POST['warning_level'] = $_POST['warning_level_nojs'];
  115. $_POST['warning_level'] = (int) $_POST['warning_level'];
  116. $_POST['warning_level'] = max(0, min(100, $_POST['warning_level']));
  117. if ($_POST['warning_level'] < $context['min_allowed'])
  118. $_POST['warning_level'] = $context['min_allowed'];
  119. elseif ($_POST['warning_level'] > $context['max_allowed'])
  120. $_POST['warning_level'] = $context['max_allowed'];
  121. // Do we actually have to issue them with a PM?
  122. $id_notice = 0;
  123. if (!empty($_POST['warn_notify']) && empty($issueErrors))
  124. {
  125. $_POST['warn_sub'] = trim($_POST['warn_sub']);
  126. $_POST['warn_body'] = trim($_POST['warn_body']);
  127. if (empty($_POST['warn_sub']) || empty($_POST['warn_body']))
  128. $issueErrors[] = 'warning_notify_blank';
  129. // Send the PM?
  130. else
  131. {
  132. require_once($sourcedir . '/Subs-Post.php');
  133. $from = array(
  134. 'id' => 0,
  135. 'name' => $context['forum_name'],
  136. 'username' => $context['forum_name'],
  137. );
  138. sendpm(array('to' => array($memID), 'bcc' => array()), $_POST['warn_sub'], $_POST['warn_body'], false, $from);
  139. // Log the notice!
  140. $smcFunc['db_insert']('',
  141. '{db_prefix}log_member_notices',
  142. array(
  143. 'subject' => 'string-255', 'body' => 'string-65534',
  144. ),
  145. array(
  146. $smcFunc['htmlspecialchars']($_POST['warn_sub']), $smcFunc['htmlspecialchars']($_POST['warn_body']),
  147. ),
  148. array('id_notice')
  149. );
  150. $id_notice = $smcFunc['db_insert_id']('{db_prefix}log_member_notices', 'id_notice');
  151. }
  152. }
  153. // Just in case - make sure notice is valid!
  154. $id_notice = (int) $id_notice;
  155. // What have we changed?
  156. $level_change = $_POST['warning_level'] - $cur_profile['warning'];
  157. // No errors? Proceed! Only log if you're not the owner.
  158. if (empty($issueErrors))
  159. {
  160. // Log what we've done!
  161. if (!$context['user']['is_owner'])
  162. $smcFunc['db_insert']('',
  163. '{db_prefix}log_comments',
  164. array(
  165. 'id_member' => 'int', 'member_name' => 'string', 'comment_type' => 'string', 'id_recipient' => 'int', 'recipient_name' => 'string-255',
  166. 'log_time' => 'int', 'id_notice' => 'int', 'counter' => 'int', 'body' => 'string-65534',
  167. ),
  168. array(
  169. $user_info['id'], $user_info['name'], 'warning', $memID, $cur_profile['real_name'],
  170. time(), $id_notice, $level_change, $_POST['warn_reason'],
  171. ),
  172. array('id_comment')
  173. );
  174. // Make the change.
  175. updateMemberData($memID, array('warning' => $_POST['warning_level']));
  176. // Leave a lovely message.
  177. $context['profile_updated'] = $context['user']['is_owner'] ? $txt['profile_updated_own'] : $txt['profile_warning_success'];
  178. }
  179. else
  180. {
  181. // Get the base stuff done.
  182. loadLanguage('Errors');
  183. $context['custom_error_title'] = $txt['profile_warning_errors_occured'];
  184. // Fill in the suite of errors.
  185. $context['post_errors'] = array();
  186. foreach ($issueErrors as $error)
  187. $context['post_errors'][] = $txt[$error];
  188. // Try to remember some bits.
  189. $context['warning_data'] = array(
  190. 'reason' => $_POST['warn_reason'],
  191. 'notify' => !empty($_POST['warn_notify']),
  192. 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '',
  193. 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '',
  194. );
  195. }
  196. // Show the new improved warning level.
  197. $context['member']['warning'] = $_POST['warning_level'];
  198. }
  199. $context['page_title'] = $txt['profile_issue_warning'];
  200. // Work our the various levels.
  201. $context['level_effects'] = array(
  202. 0 => $txt['profile_warning_effect_none'],
  203. $modSettings['warning_watch'] => $txt['profile_warning_effect_watch'],
  204. $modSettings['warning_moderate'] => $txt['profile_warning_effect_moderation'],
  205. $modSettings['warning_mute'] => $txt['profile_warning_effect_mute'],
  206. );
  207. $context['current_level'] = 0;
  208. foreach ($context['level_effects'] as $limit => $dummy)
  209. if ($context['member']['warning'] >= $limit)
  210. $context['current_level'] = $limit;
  211. // Load up all the old warnings - count first!
  212. $context['total_warnings'] = list_getUserWarningCount($memID);
  213. // Make the page index.
  214. $context['start'] = (int) $_REQUEST['start'];
  215. $perPage = (int) $modSettings['defaultMaxMessages'];
  216. $context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=issuewarning', $context['start'], $context['total_warnings'], $perPage);
  217. // Now do the data itself.
  218. $context['previous_warnings'] = list_getUserWarnings($context['start'], $perPage, 'log_time DESC', $memID);
  219. // Are they warning because of a message?
  220. if (isset($_REQUEST['msg']) && 0 < (int) $_REQUEST['msg'])
  221. {
  222. $request = $smcFunc['db_query']('', '
  223. SELECT subject
  224. FROM {db_prefix}messages AS m
  225. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  226. WHERE id_msg = {int:message}
  227. AND {query_see_board}
  228. LIMIT 1',
  229. array(
  230. 'message' => (int) $_REQUEST['msg'],
  231. )
  232. );
  233. if ($smcFunc['db_num_rows']($request) != 0)
  234. {
  235. $context['warning_for_message'] = (int) $_REQUEST['msg'];
  236. list ($context['warned_message_subject']) = $smcFunc['db_fetch_row']($request);
  237. }
  238. $smcFunc['db_free_result']($request);
  239. }
  240. // Didn't find the message?
  241. if (empty($context['warning_for_message']))
  242. {
  243. $context['warning_for_message'] = 0;
  244. $context['warned_message_subject'] = '';
  245. }
  246. // Any custom templates?
  247. $context['notification_templates'] = array();
  248. $request = $smcFunc['db_query']('', '
  249. SELECT recipient_name AS template_title, body
  250. FROM {db_prefix}log_comments
  251. WHERE comment_type = {string:warntpl}
  252. AND (id_recipient = {int:generic} OR id_recipient = {int:current_member})',
  253. array(
  254. 'warntpl' => 'warntpl',
  255. 'generic' => 0,
  256. 'current_member' => $user_info['id'],
  257. )
  258. );
  259. while ($row = $smcFunc['db_fetch_assoc']($request))
  260. {
  261. // If we're not warning for a message skip any that are.
  262. if (!$context['warning_for_message'] && strpos($row['body'], '{MESSAGE}') !== false)
  263. continue;
  264. $context['notification_templates'][] = array(
  265. 'title' => $row['template_title'],
  266. 'body' => $row['body'],
  267. );
  268. }
  269. $smcFunc['db_free_result']($request);
  270. // Setup the "default" templates.
  271. foreach (array('spamming', 'offence', 'insulting') as $type)
  272. $context['notification_templates'][] = array(
  273. 'title' => $txt['profile_warning_notify_title_' . $type],
  274. 'body' => sprintf($txt['profile_warning_notify_template_outline' . (!empty($context['warning_for_message']) ? '_post' : '')], $txt['profile_warning_notify_for_' . $type]),
  275. );
  276. // Replace all the common variables in the templates.
  277. foreach ($context['notification_templates'] as $k => $name)
  278. $context['notification_templates'][$k]['body'] = strtr($name['body'], array('{MEMBER}' => un_htmlspecialchars($context['member']['name']), '{MESSAGE}' => '[url=' . $scripturl . '?msg=' . $context['warning_for_message'] . ']' . un_htmlspecialchars($context['warned_message_subject']) . '[/url]', '{SCRIPTURL}' => $scripturl, '{FORUMNAME}' => $mbname, '{REGARDS}' => $txt['regards_team']));
  279. }
  280. /**
  281. * Get the number of warnings a user has.
  282. *
  283. * @param int $memID
  284. * @return int Total number of warnings for the user
  285. */
  286. function list_getUserWarningCount($memID)
  287. {
  288. global $smcFunc;
  289. $request = $smcFunc['db_query']('', '
  290. SELECT COUNT(*)
  291. FROM {db_prefix}log_comments
  292. WHERE id_recipient = {int:selected_member}
  293. AND comment_type = {string:warning}',
  294. array(
  295. 'selected_member' => $memID,
  296. 'warning' => 'warning',
  297. )
  298. );
  299. list ($total_warnings) = $smcFunc['db_fetch_row']($request);
  300. $smcFunc['db_free_result']($request);
  301. return $total_warnings;
  302. }
  303. /**
  304. * Get the data about a users warnings.
  305. *
  306. * @param int $start
  307. * @param int $items_per_page
  308. * @param string $sort
  309. * @param int $memID, the member ID
  310. * @return array the preview warnings
  311. */
  312. function list_getUserWarnings($start, $items_per_page, $sort, $memID)
  313. {
  314. global $smcFunc, $scripturl;
  315. $request = $smcFunc['db_query']('', '
  316. SELECT IFNULL(mem.id_member, 0) AS id_member, IFNULL(mem.real_name, lc.member_name) AS member_name,
  317. lc.log_time, lc.body, lc.counter, lc.id_notice
  318. FROM {db_prefix}log_comments AS lc
  319. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lc.id_member)
  320. WHERE lc.id_recipient = {int:selected_member}
  321. AND lc.comment_type = {string:warning}
  322. ORDER BY ' . $sort . '
  323. LIMIT ' . $start . ', ' . $items_per_page,
  324. array(
  325. 'selected_member' => $memID,
  326. 'warning' => 'warning',
  327. )
  328. );
  329. $previous_warnings = array();
  330. while ($row = $smcFunc['db_fetch_assoc']($request))
  331. {
  332. $previous_warnings[] = array(
  333. 'issuer' => array(
  334. 'id' => $row['id_member'],
  335. 'link' => $row['id_member'] ? ('<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['member_name'] . '</a>') : $row['member_name'],
  336. ),
  337. 'time' => timeformat($row['log_time']),
  338. 'reason' => $row['body'],
  339. 'counter' => $row['counter'] > 0 ? '+' . $row['counter'] : $row['counter'],
  340. 'id_notice' => $row['id_notice'],
  341. );
  342. }
  343. $smcFunc['db_free_result']($request);
  344. return $previous_warnings;
  345. }
  346. /**
  347. * Present a screen to make sure the user wants to be deleted
  348. *
  349. * @param int $memID, the member ID
  350. */
  351. function deleteAccount($memID)
  352. {
  353. global $txt, $context, $user_info, $modSettings, $cur_profile, $smcFunc;
  354. if (!$context['user']['is_owner'])
  355. isAllowedTo('profile_remove_any');
  356. elseif (!allowedTo('profile_remove_any'))
  357. isAllowedTo('profile_remove_own');
  358. // Permissions for removing stuff...
  359. $context['can_delete_posts'] = !$context['user']['is_owner'] && allowedTo('moderate_forum');
  360. // Can they do this, or will they need approval?
  361. $context['needs_approval'] = $context['user']['is_owner'] && !empty($modSettings['approveAccountDeletion']) && !allowedTo('moderate_forum');
  362. $context['page_title'] = $txt['deleteAccount'] . ': ' . $cur_profile['real_name'];
  363. }
  364. /**
  365. * Actually delete an account.
  366. *
  367. * @param $profile_vars
  368. * @param $post_errors
  369. * @param int $memID, the member ID
  370. */
  371. function deleteAccount2($profile_vars, $post_errors, $memID)
  372. {
  373. global $user_info, $sourcedir, $context, $cur_profile, $modSettings, $smcFunc;
  374. // Try get more time...
  375. @set_time_limit(600);
  376. /**
  377. * @todo Add a way to delete pms as well?
  378. */
  379. if (!$context['user']['is_owner'])
  380. isAllowedTo('profile_remove_any');
  381. elseif (!allowedTo('profile_remove_any'))
  382. isAllowedTo('profile_remove_own');
  383. checkSession();
  384. $old_profile = &$cur_profile;
  385. // Too often, people remove/delete their own only account.
  386. if (in_array(1, explode(',', $old_profile['additional_groups'])) || $old_profile['id_group'] == 1)
  387. {
  388. // Are you allowed to administrate the forum, as they are?
  389. isAllowedTo('admin_forum');
  390. $request = $smcFunc['db_query']('', '
  391. SELECT id_member
  392. FROM {db_prefix}members
  393. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  394. AND id_member != {int:selected_member}
  395. LIMIT 1',
  396. array(
  397. 'admin_group' => 1,
  398. 'selected_member' => $memID,
  399. )
  400. );
  401. list ($another) = $smcFunc['db_fetch_row']($request);
  402. $smcFunc['db_free_result']($request);
  403. if (empty($another))
  404. fatal_lang_error('at_least_one_admin', 'critical');
  405. }
  406. // This file is needed for the deleteMembers function.
  407. require_once($sourcedir . '/Subs-Members.php');
  408. // Do you have permission to delete others profiles, or is that your profile you wanna delete?
  409. if ($memID != $user_info['id'])
  410. {
  411. isAllowedTo('profile_remove_any');
  412. // Now, have you been naughty and need your posts deleting?
  413. // @todo Should this check board permissions?
  414. if ($_POST['remove_type'] != 'none' && allowedTo('moderate_forum'))
  415. {
  416. // Include RemoveTopics - essential for this type of work!
  417. require_once($sourcedir . '/RemoveTopic.php');
  418. // First off we delete any topics the member has started - if they wanted topics being done.
  419. if ($_POST['remove_type'] == 'topics')
  420. {
  421. // Fetch all topics started by this user within the time period.
  422. $request = $smcFunc['db_query']('', '
  423. SELECT t.id_topic
  424. FROM {db_prefix}topics AS t
  425. WHERE t.id_member_started = {int:selected_member}',
  426. array(
  427. 'selected_member' => $memID,
  428. )
  429. );
  430. $topicIDs = array();
  431. while ($row = $smcFunc['db_fetch_assoc']($request))
  432. $topicIDs[] = $row['id_topic'];
  433. $smcFunc['db_free_result']($request);
  434. // Actually remove the topics.
  435. // @todo This needs to check permissions, but we'll let it slide for now because of moderate_forum already being had.
  436. removeTopics($topicIDs);
  437. }
  438. // Now delete the remaining messages.
  439. $request = $smcFunc['db_query']('', '
  440. SELECT m.id_msg
  441. FROM {db_prefix}messages AS m
  442. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic
  443. AND t.id_first_msg != m.id_msg)
  444. WHERE m.id_member = {int:selected_member}',
  445. array(
  446. 'selected_member' => $memID,
  447. )
  448. );
  449. // This could take a while... but ya know it's gonna be worth it in the end.
  450. while ($row = $smcFunc['db_fetch_assoc']($request))
  451. {
  452. if (function_exists('apache_reset_timeout'))
  453. @apache_reset_timeout();
  454. removeMessage($row['id_msg']);
  455. }
  456. $smcFunc['db_free_result']($request);
  457. }
  458. // Only delete this poor members account if they are actually being booted out of camp.
  459. if (isset($_POST['deleteAccount']))
  460. deleteMembers($memID);
  461. }
  462. // Do they need approval to delete?
  463. elseif (empty($post_errors) && !empty($modSettings['approveAccountDeletion']) && !allowedTo('moderate_forum'))
  464. {
  465. // Setup their account for deletion ;)
  466. updateMemberData($memID, array('is_activated' => 4));
  467. // Another account needs approval...
  468. updateSettings(array('unapprovedMembers' => true), true);
  469. }
  470. // Also check if you typed your password correctly.
  471. elseif (empty($post_errors))
  472. {
  473. deleteMembers($memID);
  474. require_once($sourcedir . '/LogInOut.php');
  475. LogOut(true);
  476. redirectexit();
  477. }
  478. }
  479. /**
  480. * Function for doing all the paid subscription stuff - kinda.
  481. *
  482. * @param int $memID
  483. */
  484. function subscriptions($memID)
  485. {
  486. global $context, $txt, $sourcedir, $modSettings, $smcFunc, $scripturl;
  487. // Load the paid template anyway.
  488. loadTemplate('ManagePaid');
  489. loadLanguage('ManagePaid');
  490. // Load all of the subscriptions.
  491. require_once($sourcedir . '/ManagePaid.php');
  492. loadSubscriptions();
  493. $context['member']['id'] = $memID;
  494. // Remove any invalid ones.
  495. foreach ($context['subscriptions'] as $id => $sub)
  496. {
  497. // Work out the costs.
  498. $costs = @unserialize($sub['real_cost']);
  499. $cost_array = array();
  500. if ($sub['real_length'] == 'F')
  501. {
  502. foreach ($costs as $duration => $cost)
  503. {
  504. if ($cost != 0)
  505. $cost_array[$duration] = $cost;
  506. }
  507. }
  508. else
  509. {
  510. $cost_array['fixed'] = $costs['fixed'];
  511. }
  512. if (empty($cost_array))
  513. unset($context['subscriptions'][$id]);
  514. else
  515. {
  516. $context['subscriptions'][$id]['member'] = 0;
  517. $context['subscriptions'][$id]['subscribed'] = false;
  518. $context['subscriptions'][$id]['costs'] = $cost_array;
  519. }
  520. }
  521. // Work out what gateways are enabled.
  522. $gateways = loadPaymentGateways();
  523. foreach ($gateways as $id => $gateway)
  524. {
  525. $gateways[$id] = new $gateway['display_class']();
  526. if (!$gateways[$id]->gatewayEnabled())
  527. unset($gateways[$id]);
  528. }
  529. // No gateways yet?
  530. if (empty($gateways))
  531. fatal_error($txt['paid_admin_not_setup_gateway']);
  532. // Get the current subscriptions.
  533. $request = $smcFunc['db_query']('', '
  534. SELECT id_sublog, id_subscribe, start_time, end_time, status, payments_pending, pending_details
  535. FROM {db_prefix}log_subscribed
  536. WHERE id_member = {int:selected_member}',
  537. array(
  538. 'selected_member' => $memID,
  539. )
  540. );
  541. $context['current'] = array();
  542. while ($row = $smcFunc['db_fetch_assoc']($request))
  543. {
  544. // The subscription must exist!
  545. if (!isset($context['subscriptions'][$row['id_subscribe']]))
  546. continue;
  547. $context['current'][$row['id_subscribe']] = array(
  548. 'id' => $row['id_sublog'],
  549. 'sub_id' => $row['id_subscribe'],
  550. 'hide' => $row['status'] == 0 && $row['end_time'] == 0 && $row['payments_pending'] == 0,
  551. 'name' => $context['subscriptions'][$row['id_subscribe']]['name'],
  552. 'start' => timeformat($row['start_time'], false),
  553. 'end' => $row['end_time'] == 0 ? $txt['not_applicable'] : timeformat($row['end_time'], false),
  554. 'pending_details' => $row['pending_details'],
  555. 'status' => $row['status'],
  556. 'status_text' => $row['status'] == 0 ? ($row['payments_pending'] ? $txt['paid_pending'] : $txt['paid_finished']) : $txt['paid_active'],
  557. );
  558. if ($row['status'] == 1)
  559. $context['subscriptions'][$row['id_subscribe']]['subscribed'] = true;
  560. }
  561. $smcFunc['db_free_result']($request);
  562. // Simple "done"?
  563. if (isset($_GET['done']))
  564. {
  565. $_GET['sub_id'] = (int) $_GET['sub_id'];
  566. // Must exist but let's be sure...
  567. if (isset($context['current'][$_GET['sub_id']]))
  568. {
  569. // What are the details like?
  570. $current_pending = @unserialize($context['current'][$_GET['sub_id']]['pending_details']);
  571. if (!empty($current_pending))
  572. {
  573. $current_pending = array_reverse($current_pending);
  574. foreach ($current_pending as $id => $sub)
  575. {
  576. // Just find one and change it.
  577. if ($sub[0] == $_GET['sub_id'] && $sub[3] == 'prepay')
  578. {
  579. $current_pending[$id][3] = 'payback';
  580. break;
  581. }
  582. }
  583. // Save the details back.
  584. $pending_details = serialize($current_pending);
  585. $smcFunc['db_query']('', '
  586. UPDATE {db_prefix}log_subscribed
  587. SET payments_pending = payments_pending + 1, pending_details = {string:pending_details}
  588. WHERE id_sublog = {int:current_subscription_id}
  589. AND id_member = {int:selected_member}',
  590. array(
  591. 'current_subscription_id' => $context['current'][$_GET['sub_id']]['id'],
  592. 'selected_member' => $memID,
  593. 'pending_details' => $pending_details,
  594. )
  595. );
  596. }
  597. }
  598. $context['sub_template'] = 'paid_done';
  599. return;
  600. }
  601. // If this is confirmation then it's simpler...
  602. if (isset($_GET['confirm']) && isset($_POST['sub_id']) && is_array($_POST['sub_id']))
  603. {
  604. // Hopefully just one.
  605. foreach ($_POST['sub_id'] as $k => $v)
  606. $ID_SUB = (int) $k;
  607. if (!isset($context['subscriptions'][$ID_SUB]) || $context['subscriptions'][$ID_SUB]['active'] == 0)
  608. fatal_lang_error('paid_sub_not_active');
  609. // Simplify...
  610. $context['sub'] = $context['subscriptions'][$ID_SUB];
  611. $period = 'xx';
  612. if ($context['sub']['flexible'])
  613. $period = isset($_POST['cur'][$ID_SUB]) && isset($context['sub']['costs'][$_POST['cur'][$ID_SUB]]) ? $_POST['cur'][$ID_SUB] : 'xx';
  614. // Check we have a valid cost.
  615. if ($context['sub']['flexible'] && $period == 'xx')
  616. fatal_lang_error('paid_sub_not_active');
  617. // Sort out the cost/currency.
  618. $context['currency'] = $modSettings['paid_currency_code'];
  619. $context['recur'] = $context['sub']['repeatable'];
  620. if ($context['sub']['flexible'])
  621. {
  622. // Real cost...
  623. $context['value'] = $context['sub']['costs'][$_POST['cur'][$ID_SUB]];
  624. $context['cost'] = sprintf($modSettings['paid_currency_symbol'], $context['value']) . '/' . $txt[$_POST['cur'][$ID_SUB]];
  625. // The period value for paypal.
  626. $context['paypal_period'] = strtoupper(substr($_POST['cur'][$ID_SUB], 0, 1));
  627. }
  628. else
  629. {
  630. // Real cost...
  631. $context['value'] = $context['sub']['costs']['fixed'];
  632. $context['cost'] = sprintf($modSettings['paid_currency_symbol'], $context['value']);
  633. // Recur?
  634. preg_match('~(\d*)(\w)~', $context['sub']['real_length'], $match);
  635. $context['paypal_unit'] = $match[1];
  636. $context['paypal_period'] = $match[2];
  637. }
  638. // Setup the gateway context.
  639. $context['gateways'] = array();
  640. foreach ($gateways as $id => $gateway)
  641. {
  642. $fields = $gateways[$id]->fetchGatewayFields($context['sub']['id'] . '+' . $memID, $context['sub'], $context['value'], $period, $scripturl . '?action=profile;u=' . $memID . ';area=subscriptions;sub_id=' . $context['sub']['id'] . ';done');
  643. if (!empty($fields['form']))
  644. $context['gateways'][] = $fields;
  645. }
  646. // Bugger?!
  647. if (empty($context['gateways']))
  648. fatal_error($txt['paid_admin_not_setup_gateway']);
  649. // Now we are going to assume they want to take this out ;)
  650. $new_data = array($context['sub']['id'], $context['value'], $period, 'prepay');
  651. if (isset($context['current'][$context['sub']['id']]))
  652. {
  653. // What are the details like?
  654. $current_pending = array();
  655. if ($context['current'][$context['sub']['id']]['pending_details'] != '')
  656. $current_pending = @unserialize($context['current'][$context['sub']['id']]['pending_details']);
  657. // Don't get silly.
  658. if (count($current_pending) > 9)
  659. $current_pending = array();
  660. $pending_count = 0;
  661. // Only record real pending payments as will otherwise confuse the admin!
  662. foreach ($current_pending as $pending)
  663. if ($pending[3] == 'payback')
  664. $pending_count++;
  665. if (!in_array($new_data, $current_pending))
  666. {
  667. $current_pending[] = $new_data;
  668. $pending_details = serialize($current_pending);
  669. $smcFunc['db_query']('', '
  670. UPDATE {db_prefix}log_subscribed
  671. SET payments_pending = {int:pending_count}, pending_details = {string:pending_details}
  672. WHERE id_sublog = {int:current_subscription_item}
  673. AND id_member = {int:selected_member}',
  674. array(
  675. 'pending_count' => $pending_count,
  676. 'current_subscription_item' => $context['current'][$context['sub']['id']]['id'],
  677. 'selected_member' => $memID,
  678. 'pending_details' => $pending_details,
  679. )
  680. );
  681. }
  682. }
  683. // Never had this before, lovely.
  684. else
  685. {
  686. $pending_details = serialize(array($new_data));
  687. $smcFunc['db_insert']('',
  688. '{db_prefix}log_subscribed',
  689. array(
  690. 'id_subscribe' => 'int', 'id_member' => 'int', 'status' => 'int', 'payments_pending' => 'int', 'pending_details' => 'string-65534',
  691. 'start_time' => 'int', 'vendor_ref' => 'string-255',
  692. ),
  693. array(
  694. $context['sub']['id'], $memID, 0, 0, $pending_details,
  695. time(), '',
  696. ),
  697. array('id_sublog')
  698. );
  699. }
  700. // Change the template.
  701. $context['sub_template'] = 'choose_payment';
  702. // Quit.
  703. return;
  704. }
  705. else
  706. $context['sub_template'] = 'user_subscription';
  707. }
  708. ?>