Profile-Actions.php 27 KB

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