PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/sources/controllers/ProfileAccount.controller.php

https://github.com/Arantor/Elkarte
PHP | 519 lines | 362 code | 68 blank | 89 comment | 54 complexity | 2b974f99bd6e30b3d4dc834bbc92e3b8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file handles actions made on a user's profile.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Issue/manage an user's warning status.
  22. *
  23. * @param int $memID
  24. */
  25. function action_issuewarning($memID)
  26. {
  27. global $txt, $scripturl, $modSettings, $user_info, $mbname;
  28. global $context, $cur_profile, $memberContext, $smcFunc;
  29. // make sure the sub-template is set...
  30. $context['sub_template'] = 'issueWarning';
  31. // Get all the actual settings.
  32. list ($modSettings['warning_enable'], $modSettings['user_limit']) = explode(',', $modSettings['warning_settings']);
  33. // This stores any legitimate errors.
  34. $issueErrors = array();
  35. // Doesn't hurt to be overly cautious.
  36. if (empty($modSettings['warning_enable']) || ($context['user']['is_owner'] && !$cur_profile['warning']) || !allowedTo('issue_warning'))
  37. fatal_lang_error('no_access', false);
  38. // Get the base (errors related) stuff done.
  39. loadLanguage('Errors');
  40. $context['custom_error_title'] = $txt['profile_warning_errors_occured'];
  41. // Make sure things which are disabled stay disabled.
  42. $modSettings['warning_watch'] = !empty($modSettings['warning_watch']) ? $modSettings['warning_watch'] : 110;
  43. $modSettings['warning_moderate'] = !empty($modSettings['warning_moderate']) && !empty($modSettings['postmod_active']) ? $modSettings['warning_moderate'] : 110;
  44. $modSettings['warning_mute'] = !empty($modSettings['warning_mute']) ? $modSettings['warning_mute'] : 110;
  45. $context['warning_limit'] = allowedTo('admin_forum') ? 0 : $modSettings['user_limit'];
  46. $context['member']['warning'] = $cur_profile['warning'];
  47. $context['member']['name'] = $cur_profile['real_name'];
  48. // What are the limits we can apply?
  49. $context['min_allowed'] = 0;
  50. $context['max_allowed'] = 100;
  51. if ($context['warning_limit'] > 0)
  52. {
  53. // Make sure we cannot go outside of our limit for the day.
  54. $request = $smcFunc['db_query']('', '
  55. SELECT SUM(counter)
  56. FROM {db_prefix}log_comments
  57. WHERE id_recipient = {int:selected_member}
  58. AND id_member = {int:current_member}
  59. AND comment_type = {string:warning}
  60. AND log_time > {int:day_time_period}',
  61. array(
  62. 'current_member' => $user_info['id'],
  63. 'selected_member' => $memID,
  64. 'day_time_period' => time() - 86400,
  65. 'warning' => 'warning',
  66. )
  67. );
  68. list ($current_applied) = $smcFunc['db_fetch_row']($request);
  69. $smcFunc['db_free_result']($request);
  70. $context['min_allowed'] = max(0, $cur_profile['warning'] - $current_applied - $context['warning_limit']);
  71. $context['max_allowed'] = min(100, $cur_profile['warning'] - $current_applied + $context['warning_limit']);
  72. }
  73. // Defaults.
  74. $context['warning_data'] = array(
  75. 'reason' => '',
  76. 'notify' => '',
  77. 'notify_subject' => '',
  78. 'notify_body' => '',
  79. );
  80. // Are we saving?
  81. if (isset($_POST['save']))
  82. {
  83. // Security is good here.
  84. checkSession('post');
  85. // This cannot be empty!
  86. $_POST['warn_reason'] = isset($_POST['warn_reason']) ? trim($_POST['warn_reason']) : '';
  87. if ($_POST['warn_reason'] == '' && !$context['user']['is_owner'])
  88. $issueErrors[] = 'warning_no_reason';
  89. $_POST['warn_reason'] = $smcFunc['htmlspecialchars']($_POST['warn_reason']);
  90. // If the value hasn't changed it's either no JS or a real no change (Which this will pass)
  91. if ($_POST['warning_level'] == 'SAME')
  92. $_POST['warning_level'] = $_POST['warning_level_nojs'];
  93. $_POST['warning_level'] = (int) $_POST['warning_level'];
  94. $_POST['warning_level'] = max(0, min(100, $_POST['warning_level']));
  95. if ($_POST['warning_level'] < $context['min_allowed'])
  96. $_POST['warning_level'] = $context['min_allowed'];
  97. elseif ($_POST['warning_level'] > $context['max_allowed'])
  98. $_POST['warning_level'] = $context['max_allowed'];
  99. require_once(SUBSDIR . '/Moderation.subs.php');
  100. // Do we actually have to issue them with a PM?
  101. $id_notice = 0;
  102. if (!empty($_POST['warn_notify']) && empty($issueErrors))
  103. {
  104. $_POST['warn_sub'] = trim($_POST['warn_sub']);
  105. $_POST['warn_body'] = trim($_POST['warn_body']);
  106. if (empty($_POST['warn_sub']) || empty($_POST['warn_body']))
  107. $issueErrors[] = 'warning_notify_blank';
  108. // Send the PM?
  109. else
  110. {
  111. require_once(SUBSDIR . '/PersonalMessage.subs.php');
  112. $from = array(
  113. 'id' => 0,
  114. 'name' => $context['forum_name'],
  115. 'username' => $context['forum_name'],
  116. );
  117. sendpm(array('to' => array($memID), 'bcc' => array()), $_POST['warn_sub'], $_POST['warn_body'], false, $from);
  118. // Log the notice.
  119. $id_notice = logWarningNotice($_POST['warn_sub'], $_POST['warn_body']);
  120. }
  121. }
  122. // Just in case - make sure notice is valid!
  123. $id_notice = (int) $id_notice;
  124. // What have we changed?
  125. $level_change = $_POST['warning_level'] - $cur_profile['warning'];
  126. // No errors? Proceed! Only log if you're not the owner.
  127. if (empty($issueErrors))
  128. {
  129. // Log what we've done!
  130. if (!$context['user']['is_owner'])
  131. logWarning($memID, $cur_profile['real_name'], $id_notice, $level_change, $_POST['warn_reason']);
  132. // Make the change.
  133. updateMemberData($memID, array('warning' => $_POST['warning_level']));
  134. // Leave a lovely message.
  135. $context['profile_updated'] = $context['user']['is_owner'] ? $txt['profile_updated_own'] : $txt['profile_warning_success'];
  136. }
  137. else
  138. {
  139. // Try to remember some bits.
  140. $context['warning_data'] = array(
  141. 'reason' => $_POST['warn_reason'],
  142. 'notify' => !empty($_POST['warn_notify']),
  143. 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '',
  144. 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '',
  145. );
  146. }
  147. // Show the new improved warning level.
  148. $context['member']['warning'] = $_POST['warning_level'];
  149. }
  150. if (isset($_POST['preview']))
  151. {
  152. $warning_body = !empty($_POST['warn_body']) ? trim(censorText($_POST['warn_body'])) : '';
  153. $context['preview_subject'] = !empty($_POST['warn_sub']) ? trim($smcFunc['htmlspecialchars']($_POST['warn_sub'])) : '';
  154. if (empty($_POST['warn_sub']) || empty($_POST['warn_body']))
  155. $issueErrors[] = 'warning_notify_blank';
  156. if (!empty($_POST['warn_body']))
  157. {
  158. require_once(SUBSDIR . '/Post.subs.php');
  159. preparsecode($warning_body);
  160. $warning_body = parse_bbc($warning_body, true);
  161. }
  162. // Try to remember some bits.
  163. $context['warning_data'] = array(
  164. 'reason' => $_POST['warn_reason'],
  165. 'notify' => !empty($_POST['warn_notify']),
  166. 'notify_subject' => isset($_POST['warn_sub']) ? $_POST['warn_sub'] : '',
  167. 'notify_body' => isset($_POST['warn_body']) ? $_POST['warn_body'] : '',
  168. 'body_preview' => $warning_body,
  169. );
  170. }
  171. if (!empty($issueErrors))
  172. {
  173. // Fill in the suite of errors.
  174. $context['post_errors'] = array();
  175. foreach ($issueErrors as $error)
  176. $context['post_errors'][] = $txt[$error];
  177. }
  178. $context['page_title'] = $txt['profile_issue_warning'];
  179. // Let's use a generic list to get all the current warnings
  180. require_once(SUBSDIR . '/List.subs.php');
  181. require_once(SUBSDIR . '/Profile.subs.php');
  182. // Work our the various levels.
  183. $context['level_effects'] = array(
  184. 0 => $txt['profile_warning_effect_none'],
  185. $modSettings['warning_watch'] => $txt['profile_warning_effect_watch'],
  186. $modSettings['warning_moderate'] => $txt['profile_warning_effect_moderation'],
  187. $modSettings['warning_mute'] => $txt['profile_warning_effect_mute'],
  188. );
  189. $context['current_level'] = 0;
  190. foreach ($context['level_effects'] as $limit => $dummy)
  191. if ($context['member']['warning'] >= $limit)
  192. $context['current_level'] = $limit;
  193. $listOptions = array(
  194. 'id' => 'view_warnings',
  195. 'title' => $txt['profile_viewwarning_previous_warnings'],
  196. 'items_per_page' => $modSettings['defaultMaxMessages'],
  197. 'no_items_label' => $txt['profile_viewwarning_no_warnings'],
  198. 'base_href' => $scripturl . '?action=profile;area=issuewarning;sa=user;u=' . $memID,
  199. 'default_sort_col' => 'log_time',
  200. 'get_items' => array(
  201. 'function' => 'list_getUserWarnings',
  202. 'params' => array(
  203. $memID,
  204. ),
  205. ),
  206. 'get_count' => array(
  207. 'function' => 'list_getUserWarningCount',
  208. 'params' => array(
  209. $memID,
  210. ),
  211. ),
  212. 'columns' => array(
  213. 'issued_by' => array(
  214. 'header' => array(
  215. 'value' => $txt['profile_warning_previous_issued'],
  216. 'style' => 'width: 20%;',
  217. ),
  218. 'data' => array(
  219. 'function' => create_function('$warning', '
  220. return $warning[\'issuer\'][\'link\'];
  221. '
  222. ),
  223. ),
  224. 'sort' => array(
  225. 'default' => 'lc.member_name DESC',
  226. 'reverse' => 'lc.member_name',
  227. ),
  228. ),
  229. 'log_time' => array(
  230. 'header' => array(
  231. 'value' => $txt['profile_warning_previous_time'],
  232. 'style' => 'width: 30%;',
  233. ),
  234. 'data' => array(
  235. 'db' => 'time',
  236. ),
  237. 'sort' => array(
  238. 'default' => 'lc.log_time DESC',
  239. 'reverse' => 'lc.log_time',
  240. ),
  241. ),
  242. 'reason' => array(
  243. 'header' => array(
  244. 'value' => $txt['profile_warning_previous_reason'],
  245. ),
  246. 'data' => array(
  247. 'function' => create_function('$warning', '
  248. global $scripturl, $txt, $settings;
  249. $ret = \'
  250. <div class="floatleft">
  251. \' . $warning[\'reason\'] . \'
  252. </div>\';
  253. if (!empty($warning[\'id_notice\']))
  254. $ret .= \'
  255. <div class="floatright">
  256. <a href="\' . $scripturl . \'?action=moderate;area=notice;nid=\' . $warning[\'id_notice\'] . \'" onclick="window.open(this.href, \\\'\\\', \\\'scrollbars=yes,resizable=yes,width=400,height=250\\\');return false;" target="_blank" class="new_win" title="\' . $txt[\'profile_warning_previous_notice\'] . \'"><img src="\' . $settings[\'images_url\'] . \'/filter.png" alt="" /></a>
  257. </div>\';
  258. return $ret;'),
  259. ),
  260. ),
  261. 'level' => array(
  262. 'header' => array(
  263. 'value' => $txt['profile_warning_previous_level'],
  264. 'style' => 'width: 6%;',
  265. ),
  266. 'data' => array(
  267. 'db' => 'counter',
  268. ),
  269. 'sort' => array(
  270. 'default' => 'lc.counter DESC',
  271. 'reverse' => 'lc.counter',
  272. ),
  273. ),
  274. ),
  275. );
  276. // Create the list for viewing.
  277. require_once(SUBSDIR . '/List.subs.php');
  278. createList($listOptions);
  279. // Are they warning because of a message?
  280. if (isset($_REQUEST['msg']) && 0 < (int) $_REQUEST['msg'])
  281. {
  282. $request = $smcFunc['db_query']('', '
  283. SELECT subject
  284. FROM {db_prefix}messages AS m
  285. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  286. WHERE id_msg = {int:message}
  287. AND {query_see_board}
  288. LIMIT 1',
  289. array(
  290. 'message' => (int) $_REQUEST['msg'],
  291. )
  292. );
  293. if ($smcFunc['db_num_rows']($request) != 0)
  294. {
  295. $context['warning_for_message'] = (int) $_REQUEST['msg'];
  296. list ($context['warned_message_subject']) = $smcFunc['db_fetch_row']($request);
  297. }
  298. $smcFunc['db_free_result']($request);
  299. }
  300. // Didn't find the message?
  301. if (empty($context['warning_for_message']))
  302. {
  303. $context['warning_for_message'] = 0;
  304. $context['warned_message_subject'] = '';
  305. }
  306. // Any custom templates?
  307. $context['notification_templates'] = array();
  308. $request = $smcFunc['db_query']('', '
  309. SELECT recipient_name AS template_title, body
  310. FROM {db_prefix}log_comments
  311. WHERE comment_type = {string:warntpl}
  312. AND (id_recipient = {int:generic} OR id_recipient = {int:current_member})',
  313. array(
  314. 'warntpl' => 'warntpl',
  315. 'generic' => 0,
  316. 'current_member' => $user_info['id'],
  317. )
  318. );
  319. while ($row = $smcFunc['db_fetch_assoc']($request))
  320. {
  321. // If we're not warning for a message skip any that are.
  322. if (!$context['warning_for_message'] && strpos($row['body'], '{MESSAGE}') !== false)
  323. continue;
  324. $context['notification_templates'][] = array(
  325. 'title' => $row['template_title'],
  326. 'body' => $row['body'],
  327. );
  328. }
  329. $smcFunc['db_free_result']($request);
  330. // Setup the "default" templates.
  331. foreach (array('spamming', 'offence', 'insulting') as $type)
  332. $context['notification_templates'][] = array(
  333. 'title' => $txt['profile_warning_notify_title_' . $type],
  334. 'body' => sprintf($txt['profile_warning_notify_template_outline' . (!empty($context['warning_for_message']) ? '_post' : '')], $txt['profile_warning_notify_for_' . $type]),
  335. );
  336. // Replace all the common variables in the templates.
  337. foreach ($context['notification_templates'] as $k => $name)
  338. $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']));
  339. }
  340. /**
  341. * Present a screen to make sure the user wants to be deleted.
  342. *
  343. * @param int $memID the member ID
  344. */
  345. function action_deleteaccount($memID)
  346. {
  347. global $txt, $context, $user_info, $modSettings, $cur_profile, $smcFunc;
  348. if (!$context['user']['is_owner'])
  349. isAllowedTo('profile_remove_any');
  350. elseif (!allowedTo('profile_remove_any'))
  351. isAllowedTo('profile_remove_own');
  352. // Permissions for removing stuff...
  353. $context['can_delete_posts'] = !$context['user']['is_owner'] && allowedTo('moderate_forum');
  354. // Can they do this, or will they need approval?
  355. $context['needs_approval'] = $context['user']['is_owner'] && !empty($modSettings['approveAccountDeletion']) && !allowedTo('moderate_forum');
  356. $context['page_title'] = $txt['deleteAccount'] . ': ' . $cur_profile['real_name'];
  357. // make sure the sub-template is set...
  358. $context['sub_template'] = 'deleteAccount';
  359. }
  360. /**
  361. * Actually delete an account.
  362. *
  363. * @param int $memID the member ID
  364. */
  365. function action_deleteaccount2($memID)
  366. {
  367. global $user_info, $context, $cur_profile, $modSettings, $smcFunc;
  368. // Try get more time...
  369. @set_time_limit(600);
  370. // @todo Add a way to delete pms as well?
  371. if (!$context['user']['is_owner'])
  372. isAllowedTo('profile_remove_any');
  373. elseif (!allowedTo('profile_remove_any'))
  374. isAllowedTo('profile_remove_own');
  375. checkSession();
  376. $old_profile = &$cur_profile;
  377. // This file is needed for our utility functions.
  378. require_once(SUBSDIR . '/Members.subs.php');
  379. // Too often, people remove/delete their own only administrative account.
  380. if (in_array(1, explode(',', $old_profile['additional_groups'])) || $old_profile['id_group'] == 1)
  381. {
  382. // Are you allowed to administrate the forum, as they are?
  383. isAllowedTo('admin_forum');
  384. $another = isAnotherAdmin($memID);
  385. if (empty($another))
  386. fatal_lang_error('at_least_one_admin', 'critical');
  387. }
  388. // Do you have permission to delete others profiles, or is that your profile you wanna delete?
  389. if ($memID != $user_info['id'])
  390. {
  391. isAllowedTo('profile_remove_any');
  392. // Now, have you been naughty and need your posts deleting?
  393. // @todo Should this check board permissions?
  394. if ($_POST['remove_type'] != 'none' && allowedTo('moderate_forum'))
  395. {
  396. // Include subs/Topic.subs.php - essential for this type of work!
  397. require_once(SUBSDIR . '/Topic.subs.php');
  398. require_once(SUBSDIR . '/Messages.subs.php');
  399. // First off we delete any topics the member has started - if they wanted topics being done.
  400. if ($_POST['remove_type'] == 'topics')
  401. {
  402. // Fetch all topics started by this user.
  403. $topicIDs = topicsStartedBy($memID);
  404. // Actually remove the topics.
  405. // @todo This needs to check permissions, but we'll let it slide for now because of moderate_forum already being had.
  406. removeTopics($topicIDs);
  407. }
  408. // Now delete the remaining messages.
  409. $request = $smcFunc['db_query']('', '
  410. SELECT m.id_msg
  411. FROM {db_prefix}messages AS m
  412. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic
  413. AND t.id_first_msg != m.id_msg)
  414. WHERE m.id_member = {int:selected_member}',
  415. array(
  416. 'selected_member' => $memID,
  417. )
  418. );
  419. // This could take a while... but ya know it's gonna be worth it in the end.
  420. while ($row = $smcFunc['db_fetch_assoc']($request))
  421. {
  422. if (function_exists('apache_reset_timeout'))
  423. @apache_reset_timeout();
  424. removeMessage($row['id_msg']);
  425. }
  426. $smcFunc['db_free_result']($request);
  427. }
  428. // Only delete this poor member's account if they are actually being booted out of camp.
  429. if (isset($_POST['deleteAccount']))
  430. deleteMembers($memID);
  431. }
  432. // Do they need approval to delete?
  433. elseif (!empty($modSettings['approveAccountDeletion']) && !allowedTo('moderate_forum'))
  434. {
  435. // Setup their account for deletion ;)
  436. updateMemberData($memID, array('is_activated' => 4));
  437. // Another account needs approval...
  438. updateSettings(array('unapprovedMembers' => true), true);
  439. }
  440. // Also check if you typed your password correctly.
  441. else
  442. {
  443. deleteMembers($memID);
  444. require_once(CONTROLLERDIR . '/LogInOut.controller.php');
  445. action_logout(true);
  446. redirectexit();
  447. }
  448. }