PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sources/controllers/PersonalMessage.controller.php

https://github.com/Arantor/Elkarte
PHP | 2770 lines | 2052 code | 346 blank | 372 comment | 492 complexity | 3a25487873b7ba521b145170ac6f87a0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0

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

  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 is mainly meant for controlling the actions related to personal
  16. * messages. It allows viewing, sending, deleting, and marking personal
  17. * messages. For compatibility reasons, they are often called "instant messages".
  18. *
  19. */
  20. if (!defined('ELKARTE'))
  21. die('No access...');
  22. /**
  23. * This is the main function of personal messages, called before the action handler.
  24. * It should set the context, load templates and language file(s), as necessary
  25. * for the function that will be called.
  26. *
  27. * @todo this should be a (pre)dispatcher.
  28. */
  29. function action_pm()
  30. {
  31. global $txt, $scripturl, $context, $user_info, $user_settings, $smcFunc, $modSettings;
  32. // No guests!
  33. is_not_guest();
  34. // You're not supposed to be here at all, if you can't even read PMs.
  35. isAllowedTo('pm_read');
  36. // This file contains the our PM functions such as mark, send, delete
  37. require_once(SUBSDIR . '/PersonalMessage.subs.php');
  38. loadLanguage('PersonalMessage+Drafts');
  39. loadJavascriptFile(array('PersonalMessage.js', 'suggest.js'));
  40. if (!isset($_REQUEST['xml']))
  41. loadTemplate('PersonalMessage');
  42. // Load up the members maximum message capacity.
  43. loadMessageLimit();
  44. // Prepare the context for the capacity bar.
  45. if (!empty($context['message_limit']))
  46. {
  47. $bar = ($user_info['messages'] * 100) / $context['message_limit'];
  48. $context['limit_bar'] = array(
  49. 'messages' => $user_info['messages'],
  50. 'allowed' => $context['message_limit'],
  51. 'percent' => $bar,
  52. 'bar' => min(100, (int) $bar),
  53. 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], round($bar, 1)),
  54. );
  55. }
  56. // a previous message was sent successfully? show a small indication.
  57. if (isset($_GET['done']) && ($_GET['done'] == 'sent'))
  58. $context['pm_sent'] = true;
  59. // Now we have the labels, and assuming we have unsorted mail, apply our rules!
  60. if ($user_settings['new_pm'])
  61. {
  62. $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
  63. foreach ($context['labels'] as $id_label => $label_name)
  64. $context['labels'][(int) $id_label] = array(
  65. 'id' => $id_label,
  66. 'name' => trim($label_name),
  67. 'messages' => 0,
  68. 'unread_messages' => 0,
  69. );
  70. $context['labels'][-1] = array(
  71. 'id' => -1,
  72. 'name' => $txt['pm_msg_label_inbox'],
  73. 'messages' => 0,
  74. 'unread_messages' => 0,
  75. );
  76. applyRules();
  77. updateMemberData($user_info['id'], array('new_pm' => 0));
  78. $smcFunc['db_query']('', '
  79. UPDATE {db_prefix}pm_recipients
  80. SET is_new = {int:not_new}
  81. WHERE id_member = {int:current_member}',
  82. array(
  83. 'current_member' => $user_info['id'],
  84. 'not_new' => 0,
  85. )
  86. );
  87. }
  88. // Load the label data.
  89. if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
  90. {
  91. $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
  92. foreach ($context['labels'] as $id_label => $label_name)
  93. {
  94. $context['labels'][(int) $id_label] = array(
  95. 'id' => $id_label,
  96. 'name' => trim($label_name),
  97. 'messages' => 0,
  98. 'unread_messages' => 0,
  99. );
  100. }
  101. $context['labels'][-1] = array(
  102. 'id' => -1,
  103. 'name' => $txt['pm_msg_label_inbox'],
  104. 'messages' => 0,
  105. 'unread_messages' => 0,
  106. );
  107. loadPMLabels();
  108. }
  109. // This determines if we have more labels than just the standard inbox.
  110. $context['currently_using_labels'] = count($context['labels']) > 1 ? 1 : 0;
  111. // Some stuff for the labels...
  112. $context['current_label_id'] = isset($_REQUEST['l']) && isset($context['labels'][(int) $_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
  113. $context['current_label'] = &$context['labels'][(int) $context['current_label_id']]['name'];
  114. $context['folder'] = !isset($_REQUEST['f']) || $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent';
  115. // This is convenient. Do you know how annoying it is to do this every time?!
  116. $context['current_label_redirect'] = 'action=pm;f=' . $context['folder'] . (isset($_GET['start']) ? ';start=' . $_GET['start'] : '') . (isset($_REQUEST['l']) ? ';l=' . $_REQUEST['l'] : '');
  117. $context['can_issue_warning'] = in_array('w', $context['admin_features']) && allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
  118. // Are PM drafts enabled?
  119. $context['drafts_pm_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft');
  120. $context['drafts_autosave'] = !empty($context['drafts_pm_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('pm_autosave_draft');
  121. // Build the linktree for all the actions...
  122. $context['linktree'][] = array(
  123. 'url' => $scripturl . '?action=pm',
  124. 'name' => $txt['personal_messages']
  125. );
  126. // Preferences...
  127. $context['display_mode'] = $user_settings['pm_prefs'] & 3;
  128. // Finally all the things we know how to do
  129. $subActions = array(
  130. 'manlabels' => 'action_messagelabels',
  131. 'manrules' => 'action_messagerules',
  132. 'pmactions' => 'action_messageactions',
  133. 'prune' => 'action_messageprune',
  134. 'removeall' => 'action_removemessage',
  135. 'removeall2' => 'action_removemessage2',
  136. 'report' => 'action_reportmessage',
  137. 'search' => array('Search.controller.php','MessageSearch'),
  138. 'search2' => array('Search.controller.php','MessageSearch2'),
  139. 'send' => 'action_sendmessage',
  140. 'send2' => 'action_sendmessage2',
  141. 'settings' => 'action_messagesettings',
  142. 'showpmdrafts' => 'action_messagedrafts',
  143. );
  144. // Known action, go to it, otherwise the inbox for you
  145. if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
  146. action_messagefolder();
  147. else
  148. {
  149. if (!isset($_REQUEST['xml']))
  150. messageIndexBar($_REQUEST['sa']);
  151. // So it was set - let's go to that action.
  152. if (is_array($subActions[$_REQUEST['sa']]))
  153. {
  154. require_once(CONTROLLERDIR . '/' . $subActions[$_REQUEST['sa']][0]);
  155. $subActions[$_REQUEST['sa']][1]();
  156. }
  157. else
  158. $subActions[$_REQUEST['sa']]();
  159. }
  160. }
  161. /**
  162. * A menu to easily access different areas of the PM section
  163. *
  164. * @param string $area
  165. */
  166. function messageIndexBar($area)
  167. {
  168. global $txt, $context, $scripturl, $sc, $modSettings, $settings, $user_info, $options;
  169. $pm_areas = array(
  170. 'folders' => array(
  171. 'title' => $txt['pm_messages'],
  172. 'areas' => array(
  173. 'send' => array(
  174. 'label' => $txt['new_message'],
  175. 'custom_url' => $scripturl . '?action=pm;sa=send',
  176. 'permission' => allowedTo('pm_send'),
  177. ),
  178. 'inbox' => array(
  179. 'label' => $txt['inbox'],
  180. 'custom_url' => $scripturl . '?action=pm',
  181. ),
  182. 'sent' => array(
  183. 'label' => $txt['sent_items'],
  184. 'custom_url' => $scripturl . '?action=pm;f=sent',
  185. ),
  186. 'drafts' => array(
  187. 'label' => $txt['drafts_show'],
  188. 'custom_url' => $scripturl . '?action=pm;sa=showpmdrafts',
  189. 'permission' => allowedTo('pm_draft'),
  190. 'enabled' => !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']),
  191. ),
  192. ),
  193. ),
  194. 'labels' => array(
  195. 'title' => $txt['pm_labels'],
  196. 'areas' => array(),
  197. ),
  198. 'actions' => array(
  199. 'title' => $txt['pm_actions'],
  200. 'areas' => array(
  201. 'search' => array(
  202. 'label' => $txt['pm_search_bar_title'],
  203. 'custom_url' => $scripturl . '?action=pm;sa=search',
  204. ),
  205. 'prune' => array(
  206. 'label' => $txt['pm_prune'],
  207. 'custom_url' => $scripturl . '?action=pm;sa=prune'
  208. ),
  209. ),
  210. ),
  211. 'pref' => array(
  212. 'title' => $txt['pm_preferences'],
  213. 'areas' => array(
  214. 'manlabels' => array(
  215. 'label' => $txt['pm_manage_labels'],
  216. 'custom_url' => $scripturl . '?action=pm;sa=manlabels',
  217. ),
  218. 'manrules' => array(
  219. 'label' => $txt['pm_manage_rules'],
  220. 'custom_url' => $scripturl . '?action=pm;sa=manrules',
  221. ),
  222. 'settings' => array(
  223. 'label' => $txt['pm_settings'],
  224. 'custom_url' => $scripturl . '?action=pm;sa=settings',
  225. ),
  226. ),
  227. ),
  228. );
  229. // Handle labels.
  230. if (empty($context['currently_using_labels']))
  231. unset($pm_areas['labels']);
  232. else
  233. {
  234. // Note we send labels by id as it will have less problems in the querystring.
  235. $unread_in_labels = 0;
  236. foreach ($context['labels'] as $label)
  237. {
  238. if ($label['id'] == -1)
  239. continue;
  240. // Count the amount of unread items in labels.
  241. $unread_in_labels += $label['unread_messages'];
  242. // Add the label to the menu.
  243. $pm_areas['labels']['areas']['label' . $label['id']] = array(
  244. 'label' => $label['name'] . (!empty($label['unread_messages']) ? ' (<strong>' . $label['unread_messages'] . '</strong>)' : ''),
  245. 'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
  246. 'unread_messages' => $label['unread_messages'],
  247. 'messages' => $label['messages'],
  248. );
  249. }
  250. if (!empty($unread_in_labels))
  251. $pm_areas['labels']['title'] .= ' (' . $unread_in_labels . ')';
  252. }
  253. $pm_areas['folders']['areas']['inbox']['unread_messages'] = &$context['labels'][-1]['unread_messages'];
  254. $pm_areas['folders']['areas']['inbox']['messages'] = &$context['labels'][-1]['messages'];
  255. // If we have unread messages, make note of the number in the menus
  256. if (!empty($context['labels'][-1]['unread_messages']))
  257. {
  258. $pm_areas['folders']['areas']['inbox']['label'] .= ' (<strong>' . $context['labels'][-1]['unread_messages'] . '</strong>)';
  259. $pm_areas['folders']['title'] .= ' (' . $context['labels'][-1]['unread_messages'] . ')';
  260. }
  261. // Do we have a limit on the amount of messages we can keep?
  262. if (!empty($context['message_limit']))
  263. {
  264. $bar = round(($user_info['messages'] * 100) / $context['message_limit'], 1);
  265. $context['limit_bar'] = array(
  266. 'messages' => $user_info['messages'],
  267. 'allowed' => $context['message_limit'],
  268. 'percent' => $bar,
  269. 'bar' => $bar > 100 ? 100 : (int) $bar,
  270. 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], $bar)
  271. );
  272. }
  273. require_once(SUBSDIR . '/Menu.subs.php');
  274. // What page is this, again?
  275. $current_page = $scripturl . '?action=pm' . (!empty($_REQUEST['sa']) ? ';sa=' . $_REQUEST['sa'] : '') . (!empty($context['folder']) ? ';f=' . $context['folder'] : '') . (!empty($context['current_label_id']) ? ';l=' . $context['current_label_id'] : '');
  276. // Set a few options for the menu.
  277. $menuOptions = array(
  278. 'current_area' => $area,
  279. 'disable_url_session_check' => true,
  280. );
  281. // Actually create the menu!
  282. $pm_include_data = createMenu($pm_areas, $menuOptions);
  283. unset($pm_areas);
  284. // No menu means no access.
  285. if (!$pm_include_data && (!$user_info['is_guest'] || validateSession()))
  286. fatal_lang_error('no_access', false);
  287. // Make a note of the Unique ID for this menu.
  288. $context['pm_menu_id'] = $context['max_menu_id'];
  289. $context['pm_menu_name'] = 'menu_data_' . $context['pm_menu_id'];
  290. // Set the selected item.
  291. $current_area = $pm_include_data['current_area'];
  292. $context['menu_item_selected'] = $current_area;
  293. // Set the template for this area and add the profile layer.
  294. if (!isset($_REQUEST['xml']))
  295. $context['template_layers'][] = 'pm';
  296. }
  297. /**
  298. * A folder, ie. inbox/sent etc.
  299. */
  300. function action_messagefolder()
  301. {
  302. global $txt, $scripturl, $modSettings, $context, $subjects_request;
  303. global $messages_request, $user_info, $recipients, $options, $smcFunc, $memberContext, $user_settings;
  304. // Changing view?
  305. if (isset($_GET['view']))
  306. {
  307. $context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
  308. updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
  309. }
  310. // Make sure the starting location is valid.
  311. $start = '';
  312. if (isset($_GET['start']) && $_GET['start'] != 'new')
  313. $start = (int) $_GET['start'];
  314. elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first']))
  315. $start = 0;
  316. else
  317. $start = 'new';
  318. // Set up some basic theme stuff.
  319. $context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
  320. $context['get_pmessage'] = 'prepareMessageContext';
  321. $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
  322. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  323. $labelQuery = $context['folder'] != 'sent' ? '
  324. AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
  325. // Set the index bar correct!
  326. messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
  327. // They didn't pick a sort, use the forum default.
  328. if (!isset($_GET['sort']))
  329. {
  330. $sort_by = 'date';
  331. $descending = !empty($options['view_newest_pm_first']);
  332. }
  333. // Otherwise use the defaults: ascending, by date.
  334. else
  335. {
  336. $sort_by = $_GET['sort'];
  337. $descending = isset($_GET['desc']);
  338. }
  339. // Set our sort by query
  340. switch ($sort_by)
  341. {
  342. case 'date':
  343. $sort_by_query = 'pm.id_pm';
  344. case 'name':
  345. $sort_by_query = 'IFNULL(mem.real_name, \'\')';
  346. case 'subject':
  347. $sort_by_query = 'pm.subject';
  348. default:
  349. $sort_by_query = 'pm.id_pm';
  350. }
  351. // Set the text to resemble the current folder.
  352. $pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
  353. $txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
  354. // Now, build the link tree!
  355. if ($context['current_label_id'] == -1)
  356. $context['linktree'][] = array(
  357. 'url' => $scripturl . '?action=pm;f=' . $context['folder'],
  358. 'name' => $pmbox
  359. );
  360. // Build it further for a label.
  361. if ($context['current_label_id'] != -1)
  362. $context['linktree'][] = array(
  363. 'url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'],
  364. 'name' => $txt['pm_current_label'] . ': ' . $context['current_label']
  365. );
  366. // Figure out how many messages there are.
  367. $max_messages = getPMCount(false, null, $labelQuery);
  368. // Only show the button if there are messages to delete.
  369. $context['show_delete'] = $max_messages > 0;
  370. // Start on the last page.
  371. if (!is_numeric($start) || $start >= $max_messages)
  372. $start = ($max_messages - 1) - (($max_messages - 1) % $modSettings['defaultMaxMessages']);
  373. elseif ($start < 0)
  374. $start = 0;
  375. // ... but wait - what if we want to start from a specific message?
  376. if (isset($_GET['pmid']))
  377. {
  378. $pmID = (int) $_GET['pmid'];
  379. // Make sure you have access to this PM.
  380. if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
  381. fatal_lang_error('no_access', false);
  382. $context['current_pm'] = $pmID;
  383. // With only one page of PM's we're gonna want page 1.
  384. if ($max_messages <= $modSettings['defaultMaxMessages'])
  385. $start = 0;
  386. // If we pass kstart we assume we're in the right place.
  387. elseif (!isset($_GET['kstart']))
  388. {
  389. $start = getPMCount($descending, $pmID, $labelQuery);
  390. // To stop the page index's being abnormal, start the page on the page the message would normally be located on...
  391. $start = $modSettings['defaultMaxMessages'] * (int) ($start / $modSettings['defaultMaxMessages']);
  392. }
  393. }
  394. // Sanitize and validate pmsg variable if set.
  395. if (isset($_GET['pmsg']))
  396. {
  397. $pmsg = (int) $_GET['pmsg'];
  398. if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
  399. fatal_lang_error('no_access', false);
  400. }
  401. // Determine the navigation context (especially useful for the wireless template).
  402. $context['links'] = array(
  403. 'first' => $start >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=0' : '',
  404. 'prev' => $start >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($start - $modSettings['defaultMaxMessages']) : '',
  405. 'next' => $start + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($start + $modSettings['defaultMaxMessages']) : '',
  406. 'last' => $start + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) * $modSettings['defaultMaxMessages']) : '',
  407. 'up' => $scripturl,
  408. );
  409. $context['page_info'] = array(
  410. 'current_page' => $start / $modSettings['defaultMaxMessages'] + 1,
  411. 'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1
  412. );
  413. // First work out what messages we need to see - if grouped is a little trickier...
  414. if ($context['display_mode'] == 2)
  415. {
  416. // On a non-default sort due to PostgreSQL we have to do a harder sort.
  417. if ($smcFunc['db_title'] == 'PostgreSQL' && $sort_by_query != 'pm.id_pm')
  418. {
  419. $sub_request = $smcFunc['db_query']('', '
  420. SELECT MAX({raw:sort}) AS sort_param, pm.id_pm_head
  421. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($sort_by == 'name' ? '
  422. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  423. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  424. AND pmr.id_member = {int:current_member}
  425. AND pmr.deleted = {int:not_deleted}
  426. ' . $labelQuery . ')') . ($sort_by == 'name' ? ( '
  427. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
  428. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
  429. AND pm.deleted_by_sender = {int:not_deleted}' : '1=1') . (empty($pmsg) ? '' : '
  430. AND pm.id_pm = {int:id_pm}') . '
  431. GROUP BY pm.id_pm_head
  432. ORDER BY sort_param' . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  433. LIMIT ' . $start . ', ' . $modSettings['defaultMaxMessages'] : ''),
  434. array(
  435. 'current_member' => $user_info['id'],
  436. 'not_deleted' => 0,
  437. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  438. 'id_pm' => isset($pmsg) ? $pmsg : '0',
  439. 'sort' => $sort_by_query,
  440. )
  441. );
  442. $sub_pms = array();
  443. while ($row = $smcFunc['db_fetch_assoc']($sub_request))
  444. $sub_pms[$row['id_pm_head']] = $row['sort_param'];
  445. $smcFunc['db_free_result']($sub_request);
  446. $request = $smcFunc['db_query']('', '
  447. SELECT pm.id_pm AS id_pm, pm.id_pm_head
  448. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($sort_by == 'name' ? '
  449. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  450. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  451. AND pmr.id_member = {int:current_member}
  452. AND pmr.deleted = {int:not_deleted}
  453. ' . $labelQuery . ')') . ($sort_by == 'name' ? ( '
  454. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
  455. WHERE ' . (empty($sub_pms) ? '0=1' : 'pm.id_pm IN ({array_int:pm_list})') . '
  456. ORDER BY ' . ($sort_by_query == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  457. LIMIT ' . $start . ', ' . $modSettings['defaultMaxMessages'] : ''),
  458. array(
  459. 'current_member' => $user_info['id'],
  460. 'pm_list' => array_keys($sub_pms),
  461. 'not_deleted' => 0,
  462. 'sort' => $sort_by_query,
  463. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  464. )
  465. );
  466. }
  467. else
  468. {
  469. $request = $smcFunc['db_query']('pm_conversation_list', '
  470. SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
  471. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($sort_by == 'name' ? '
  472. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  473. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  474. AND pmr.id_member = {int:current_member}
  475. AND pmr.deleted = {int:deleted_by}
  476. ' . $labelQuery . ')') . ($sort_by == 'name' ? ( '
  477. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
  478. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
  479. AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
  480. AND pm.id_pm = {int:pmsg}') . '
  481. GROUP BY pm.id_pm_head
  482. ORDER BY ' . ($sort_by_query == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (isset($pmsg) ? '
  483. LIMIT ' . $start . ', ' . $modSettings['defaultMaxMessages'] : ''),
  484. array(
  485. 'current_member' => $user_info['id'],
  486. 'deleted_by' => 0,
  487. 'sort' => $sort_by_query,
  488. 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  489. 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
  490. )
  491. );
  492. }
  493. }
  494. // This is kinda simple!
  495. else
  496. {
  497. // @todo SLOW This query uses a filesort. (inbox only.)
  498. $request = $smcFunc['db_query']('', '
  499. SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
  500. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($sort_by == 'name' ? '
  501. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  502. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  503. AND pmr.id_member = {int:current_member}
  504. AND pmr.deleted = {int:is_deleted}
  505. ' . $labelQuery . ')') . ($sort_by == 'name' ? ( '
  506. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
  507. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {raw:current_member}
  508. AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
  509. AND pm.id_pm = {int:pmsg}') . '
  510. ORDER BY ' . ($sort_by_query == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (isset($pmsg) ? '
  511. LIMIT ' . $start . ', ' . $modSettings['defaultMaxMessages'] : ''),
  512. array(
  513. 'current_member' => $user_info['id'],
  514. 'is_deleted' => 0,
  515. 'sort' => $sort_by_query,
  516. 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  517. 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
  518. )
  519. );
  520. }
  521. // Load the id_pms and initialize recipients.
  522. $pms = array();
  523. $lastData = array();
  524. $posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
  525. $recipients = array();
  526. while ($row = $smcFunc['db_fetch_assoc']($request))
  527. {
  528. if (!isset($recipients[$row['id_pm']]))
  529. {
  530. if (isset($row['id_member_from']))
  531. $posters[$row['id_pm']] = $row['id_member_from'];
  532. $pms[$row['id_pm']] = $row['id_pm'];
  533. $recipients[$row['id_pm']] = array(
  534. 'to' => array(),
  535. 'bcc' => array()
  536. );
  537. }
  538. // Keep track of the last message so we know what the head is without another query!
  539. if ((empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData))) || empty($lastData) || (!empty($pmID) && $pmID == $row['id_pm']))
  540. $lastData = array(
  541. 'id' => $row['id_pm'],
  542. 'head' => $row['id_pm_head'],
  543. );
  544. }
  545. $smcFunc['db_free_result']($request);
  546. // Make sure that we have been given a correct head pm id!
  547. if ($context['display_mode'] === 2 && !empty($pmID) && $pmID != $lastData['id'])
  548. fatal_lang_error('no_access', false);
  549. if (!empty($pms))
  550. {
  551. // Select the correct current message.
  552. if (empty($pmID))
  553. $context['current_pm'] = $lastData['id'];
  554. // This is a list of the pm's that are used for "full" display.
  555. if ($context['display_mode'] == 0)
  556. $display_pms = $pms;
  557. else
  558. $display_pms = array($context['current_pm']);
  559. // At this point we know the main id_pm's. But - if we are looking at conversations we need the others!
  560. if ($context['display_mode'] == 2)
  561. {
  562. $request = $smcFunc['db_query']('', '
  563. SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
  564. FROM {db_prefix}personal_messages AS pm
  565. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
  566. WHERE pm.id_pm_head = {int:id_pm_head}
  567. AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
  568. OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
  569. ORDER BY pm.id_pm',
  570. array(
  571. 'current_member' => $user_info['id'],
  572. 'id_pm_head' => $lastData['head'],
  573. 'not_deleted' => 0,
  574. )
  575. );
  576. while ($row = $smcFunc['db_fetch_assoc']($request))
  577. {
  578. // This is, frankly, a joke. We will put in a workaround for people sending to themselves - yawn!
  579. if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1)
  580. continue;
  581. elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1)
  582. continue;
  583. if (!isset($recipients[$row['id_pm']]))
  584. $recipients[$row['id_pm']] = array(
  585. 'to' => array(),
  586. 'bcc' => array()
  587. );
  588. $display_pms[] = $row['id_pm'];
  589. $posters[$row['id_pm']] = $row['id_member_from'];
  590. }
  591. $smcFunc['db_free_result']($request);
  592. }
  593. // This is pretty much EVERY pm!
  594. $all_pms = array_merge($pms, $display_pms);
  595. $all_pms = array_unique($all_pms);
  596. // Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
  597. $request = $smcFunc['db_query']('', '
  598. SELECT pmr.id_pm, mem_to.id_member AS id_member_to, mem_to.real_name AS to_name, pmr.bcc, pmr.labels, pmr.is_read
  599. FROM {db_prefix}pm_recipients AS pmr
  600. LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
  601. WHERE pmr.id_pm IN ({array_int:pm_list})',
  602. array(
  603. 'pm_list' => $all_pms,
  604. )
  605. );
  606. $context['message_labels'] = array();
  607. $context['message_replied'] = array();
  608. $context['message_unread'] = array();
  609. while ($row = $smcFunc['db_fetch_assoc']($request))
  610. {
  611. if ($context['folder'] == 'sent' || empty($row['bcc']))
  612. $recipients[$row['id_pm']][empty($row['bcc']) ? 'to' : 'bcc'][] = empty($row['id_member_to']) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_to'] . '">' . $row['to_name'] . '</a>';
  613. if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
  614. {
  615. $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
  616. $context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
  617. $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
  618. foreach ($row['labels'] as $v)
  619. {
  620. if (isset($context['labels'][(int) $v]))
  621. $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
  622. }
  623. }
  624. }
  625. $smcFunc['db_free_result']($request);
  626. // Make sure we don't load unnecessary data.
  627. if ($context['display_mode'] == 1)
  628. {
  629. foreach ($posters as $k => $v)
  630. if (!in_array($k, $display_pms))
  631. unset($posters[$k]);
  632. }
  633. // Load any users....
  634. $posters = array_unique($posters);
  635. if (!empty($posters))
  636. loadMemberData($posters);
  637. // If we're on grouped/restricted view get a restricted list of messages.
  638. if ($context['display_mode'] != 0)
  639. {
  640. // Get the order right.
  641. $orderBy = array();
  642. foreach (array_reverse($pms) as $pm)
  643. $orderBy[] = 'pm.id_pm = ' . $pm;
  644. // Seperate query for these bits!
  645. $subjects_request = $smcFunc['db_query']('', '
  646. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.msgtime, IFNULL(mem.real_name, pm.from_name) AS from_name,
  647. IFNULL(mem.id_member, 0) AS not_guest
  648. FROM {db_prefix}personal_messages AS pm
  649. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  650. WHERE pm.id_pm IN ({array_int:pm_list})
  651. ORDER BY ' . implode(', ', $orderBy) . '
  652. LIMIT ' . count($pms),
  653. array(
  654. 'pm_list' => $pms,
  655. )
  656. );
  657. }
  658. // Execute the query!
  659. $messages_request = $smcFunc['db_query']('', '
  660. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
  661. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
  662. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($sort_by == 'name' ? '
  663. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
  664. WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
  665. GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
  666. ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : $sort_by_query) . ($descending ? ' DESC' : ' ASC') . '
  667. LIMIT ' . count($display_pms),
  668. array(
  669. 'display_pms' => $display_pms,
  670. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  671. )
  672. );
  673. }
  674. else
  675. $messages_request = false;
  676. // prepare some items for the template
  677. $context['can_send_pm'] = allowedTo('pm_send');
  678. $context['can_send_email'] = allowedTo('send_email_to_members');
  679. $context['sub_template'] = 'folder';
  680. $context['page_title'] = $txt['pm_inbox'];
  681. $context['sort_direction'] = $descending ? 'down' : 'up';
  682. $context['sort_by'] = $sort_by;
  683. // Set up the page index.
  684. $context['page_index'] = constructPageIndex($scripturl . '?action=pm;f=' . $context['folder'] . (isset($_REQUEST['l']) ? ';l=' . (int) $_REQUEST['l'] : '') . ';sort=' . $context['sort_by'] . ($descending ? ';desc' : ''), $start, $max_messages, $modSettings['defaultMaxMessages']);
  685. $context['start'] = $start;
  686. // Finally mark the relevant messages as read.
  687. if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages']))
  688. {
  689. // If the display mode is "old sk00l" do them all...
  690. if ($context['display_mode'] == 0)
  691. markMessages(null, $context['current_label_id']);
  692. // Otherwise do just the current one!
  693. elseif (!empty($context['current_pm']))
  694. markMessages($display_pms, $context['current_label_id']);
  695. }
  696. // Build the conversation button array.
  697. if ($context['display_mode'] === 2 && !empty($context['current_pm']))
  698. {
  699. $context['conversation_buttons'] = array(
  700. 'delete' => array('text' => 'delete_conversation', 'image' => 'delete.png', 'lang' => true, 'url' => $scripturl . '?action=pm;sa=pmactions;pm_actions[' . $context['current_pm'] . ']=delete;conversation;f=' . $context['folder'] . ';start=' . $context['start'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';' . $context['session_var'] . '=' . $context['session_id'], 'custom' => 'onclick="return confirm(\'' . addslashes($txt['remove_message']) . '?\');"'),
  701. );
  702. // Allow mods to add additional buttons here
  703. call_integration_hook('integrate_conversation_buttons');
  704. }
  705. }
  706. /**
  707. * Get a personal message for the theme. (used to save memory.)
  708. *
  709. * @param $type
  710. * @param $reset
  711. */
  712. function prepareMessageContext($type = 'subject', $reset = false)
  713. {
  714. global $txt, $scripturl, $modSettings, $settings, $context, $messages_request, $memberContext, $recipients, $smcFunc;
  715. global $user_info, $subjects_request;
  716. // Count the current message number....
  717. static $counter = null;
  718. if ($counter === null || $reset)
  719. $counter = $context['start'];
  720. static $temp_pm_selected = null;
  721. if ($temp_pm_selected === null)
  722. {
  723. $temp_pm_selected = isset($_SESSION['pm_selected']) ? $_SESSION['pm_selected'] : array();
  724. $_SESSION['pm_selected'] = array();
  725. }
  726. // If we're in non-boring view do something exciting!
  727. if ($context['display_mode'] != 0 && $subjects_request && $type == 'subject')
  728. {
  729. $subject = $smcFunc['db_fetch_assoc']($subjects_request);
  730. if (!$subject)
  731. {
  732. $smcFunc['db_free_result']($subjects_request);
  733. return false;
  734. }
  735. $subject['subject'] = $subject['subject'] == '' ? $txt['no_subject'] : $subject['subject'];
  736. censorText($subject['subject']);
  737. $output = array(
  738. 'id' => $subject['id_pm'],
  739. 'member' => array(
  740. 'id' => $subject['id_member_from'],
  741. 'name' => $subject['from_name'],
  742. 'link' => $subject['not_guest'] ? '<a href="' . $scripturl . '?action=profile;u=' . $subject['id_member_from'] . '">' . $subject['from_name'] . '</a>' : $subject['from_name'],
  743. ),
  744. 'recipients' => &$recipients[$subject['id_pm']],
  745. 'subject' => $subject['subject'],
  746. 'time' => timeformat($subject['msgtime']),
  747. 'timestamp' => forum_time(true, $subject['msgtime']),
  748. 'number_recipients' => count($recipients[$subject['id_pm']]['to']),
  749. 'labels' => &$context['message_labels'][$subject['id_pm']],
  750. 'fully_labeled' => count($context['message_labels'][$subject['id_pm']]) == count($context['labels']),
  751. 'is_replied_to' => &$context['message_replied'][$subject['id_pm']],
  752. 'is_unread' => &$context['message_unread'][$subject['id_pm']],
  753. 'is_selected' => !empty($temp_pm_selected) && in_array($subject['id_pm'], $temp_pm_selected),
  754. );
  755. return $output;
  756. }
  757. // Bail if it's false, ie. no messages.
  758. if ($messages_request == false)
  759. return false;
  760. // Reset the data?
  761. if ($reset == true)
  762. return @$smcFunc['db_data_seek']($messages_request, 0);
  763. // Get the next one... bail if anything goes wrong.
  764. $message = $smcFunc['db_fetch_assoc']($messages_request);
  765. if (!$message)
  766. {
  767. if ($type != 'subject')
  768. $smcFunc['db_free_result']($messages_request);
  769. return false;
  770. }
  771. // Use '(no subject)' if none was specified.
  772. $message['subject'] = $message['subject'] == '' ? $txt['no_subject'] : $message['subject'];
  773. // Load the message's information - if it's not there, load the guest information.
  774. if (!loadMemberContext($message['id_member_from'], true))
  775. {
  776. $memberContext[$message['id_member_from']]['name'] = $message['from_name'];
  777. $memberContext[$message['id_member_from']]['id'] = 0;
  778. // Sometimes the forum sends messages itself (Warnings are an example) - in this case don't label it from a guest.
  779. $memberContext[$message['id_member_from']]['group'] = $message['from_name'] == $context['forum_name'] ? '' : $txt['guest_title'];
  780. $memberContext[$message['id_member_from']]['link'] = $message['from_name'];
  781. $memberContext[$message['id_member_from']]['email'] = '';
  782. $memberContext[$message['id_member_from']]['show_email'] = showEmailAddress(true, 0);
  783. $memberContext[$message['id_member_from']]['is_guest'] = true;
  784. }
  785. else
  786. {
  787. $memberContext[$message['id_member_from']]['can_view_profile'] = allowedTo('profile_view_any') || ($message['id_member_from'] == $user_info['id'] && allowedTo('profile_view_own'));
  788. $memberContext[$message['id_member_from']]['can_see_warning'] = !isset($context['disabled_fields']['warning_status']) && $memberContext[$message['id_member_from']]['warning_status'] && ($context['user']['can_mod'] || (!empty($modSettings['warning_show']) && ($modSettings['warning_show'] > 1 || $message['id_member_from'] == $user_info['id'])));
  789. }
  790. $memberContext[$message['id_member_from']]['show_profile_buttons'] = $settings['show_profile_buttons'] && (!empty($memberContext[$message['id_member_from']]['can_view_profile']) || (!empty($memberContext[$message['id_member_from']]['website']['url']) && !isset($context['disabled_fields']['website'])) || (in_array($memberContext[$message['id_member_from']]['show_email'], array('yes', 'yes_permission_override', 'no_through_forum'))) || $context['can_send_pm']);
  791. // Censor all the important text...
  792. censorText($message['body']);
  793. censorText($message['subject']);
  794. // Run UBBC interpreter on the message.
  795. $message['body'] = parse_bbc($message['body'], true, 'pm' . $message['id_pm']);
  796. // Send the array.
  797. $output = array(
  798. 'alternate' => $counter % 2,
  799. 'id' => $message['id_pm'],
  800. 'member' => &$memberContext[$message['id_member_from']],
  801. 'subject' => $message['subject'],
  802. 'time' => timeformat($message['msgtime']),
  803. 'timestamp' => forum_time(true, $message['msgtime']),
  804. 'counter' => $counter,
  805. 'body' => $message['body'],
  806. 'recipients' => &$recipients[$message['id_pm']],
  807. 'number_recipients' => count($recipients[$message['id_pm']]['to']),
  808. 'labels' => &$context['message_labels'][$message['id_pm']],
  809. 'fully_labeled' => count($context['message_labels'][$message['id_pm']]) == count($context['labels']),
  810. 'is_replied_to' => &$context['message_replied'][$message['id_pm']],
  811. 'is_unread' => &$context['message_unread'][$message['id_pm']],
  812. 'is_selected' => !empty($temp_pm_selected) && in_array($message['id_pm'], $temp_pm_selected),
  813. 'is_message_author' => $message['id_member_from'] == $user_info['id'],
  814. 'can_report' => !empty($modSettings['enableReportPM']),
  815. 'can_see_ip' => allowedTo('moderate_forum') || ($message['id_member_from'] == $user_info['id'] && !empty($user_info['id'])),
  816. );
  817. $counter++;
  818. return $output;
  819. }
  820. /**
  821. * Send a new message?
  822. */
  823. function action_sendmessage()
  824. {
  825. global $txt, $scripturl, $modSettings;
  826. global $context, $options, $smcFunc, $language, $user_info;
  827. isAllowedTo('pm_send');
  828. loadLanguage('PersonalMessage');
  829. // Just in case it was loaded from somewhere else.
  830. loadTemplate('PersonalMessage');
  831. $context['sub_template'] = 'send';
  832. // Extract out the spam settings - cause it's neat.
  833. list ($modSettings['max_pm_recipients'], $modSettings['pm_posts_verification'], $modSettings['pm_posts_per_hour']) = explode(',', $modSettings['pm_spam_settings']);
  834. // Set the title...
  835. $context['page_title'] = $txt['send_message'];
  836. $context['reply'] = isset($_REQUEST['pmsg']) || isset($_REQUEST['quote']);
  837. // Check whether we've gone over the limit of messages we can send per hour.
  838. if (!empty($modSettings['pm_posts_per_hour']) && !allowedTo(array('admin_forum', 'moderate_forum', 'send_mail')) && $user_info['mod_cache']['bq'] == '0=1' && $user_info['mod_cache']['gq'] == '0=1')
  839. {
  840. // How many messages have they sent this last hour?
  841. $request = $smcFunc['db_query']('', '
  842. SELECT COUNT(pr.id_pm) AS post_count
  843. FROM {db_prefix}personal_messages AS pm
  844. INNER JOIN {db_prefix}pm_recipients AS pr ON (pr.id_pm = pm.id_pm)
  845. WHERE pm.id_member_from = {int:current_member}
  846. AND pm.msgtime > {int:msgtime}',
  847. array(
  848. 'current_member' => $user_info['id'],
  849. 'msgtime' => time() - 3600,
  850. )
  851. );
  852. list ($postCount) = $smcFunc['db_fetch_row']($request);
  853. $smcFunc['db_free_result']($request);
  854. if (!empty($postCount) && $postCount >= $modSettings['pm_posts_per_hour'])
  855. fatal_lang_error('pm_too_many_per_hour', true, array($modSettings['pm_posts_per_hour']));
  856. }
  857. // Quoting/Replying to a message?
  858. if (!empty($_REQUEST['pmsg']))
  859. {
  860. $pmsg = (int) $_REQUEST['pmsg'];
  861. // Make sure this is yours.
  862. if (!isAccessiblePM($pmsg))
  863. fatal_lang_error('no_access', false);
  864. // Work out whether this is one you've received?
  865. $request = $smcFunc['db_query']('', '
  866. SELECT
  867. id_pm
  868. FROM {db_prefix}pm_recipients
  869. WHERE id_pm = {int:id_pm}
  870. AND id_member = {int:current_member}
  871. LIMIT 1',
  872. array(
  873. 'current_member' => $user_info['id'],
  874. 'id_pm' => $pmsg,
  875. )
  876. );
  877. $isReceived = $smcFunc['db_num_rows']($request) != 0;
  878. $smcFunc['db_free_result']($request);
  879. // Get the quoted message (and make sure you're allowed to see this quote!).
  880. $request = $smcFunc['db_query']('', '
  881. SELECT
  882. pm.id_pm, CASE WHEN pm.id_pm_head = {int:id_pm_head_empty} THEN pm.id_pm ELSE pm.id_pm_head END AS pm_head,
  883. pm.body, pm.subject, pm.msgtime, mem.member_name, IFNULL(mem.id_member, 0) AS id_member,
  884. IFNULL(mem.real_name, pm.from_name) AS real_name
  885. FROM {db_prefix}personal_messages AS pm' . (!$isReceived ? '' : '
  886. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:id_pm})') . '
  887. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  888. WHERE pm.id_pm = {int:id_pm}' . (!$isReceived ? '
  889. AND pm.id_member_from = {int:current_member}' : '
  890. AND pmr.id_member = {int:current_member}') . '
  891. LIMIT 1',
  892. array(
  893. 'current_member' => $user_info['id'],
  894. 'id_pm_head_empty' => 0,
  895. 'id_pm' => $pmsg,
  896. )
  897. );
  898. if ($smcFunc['db_num_rows']($request) == 0)
  899. fatal_lang_error('pm_not_yours', false);
  900. $row_quoted = $smcFunc['db_fetch_assoc']($request);
  901. $smcFunc['db_free_result']($request);
  902. // Censor the message.
  903. censorText($row_quoted['subject']);
  904. censorText($row_quoted['body']);
  905. // Add 'Re: ' to it....
  906. if (!isset($context['response_prefix']) && !($context['response_prefix'] = cache_get_data('response_prefix')))
  907. {
  908. if ($language === $user_info['language'])
  909. $context['response_prefix'] = $txt['response_prefix'];
  910. else
  911. {
  912. loadLanguage('index', $language, false);
  913. $context['response_prefix'] = $txt['response_prefix'];
  914. loadLanguage('index');
  915. }
  916. cache_put_data('response_prefix', $context['response_prefix'], 600);
  917. }
  918. $form_subject = $row_quoted['subject'];
  919. if ($context['reply'] && trim($context['response_prefix']) != '' && $smcFunc['strpos']($form_subject, trim($context['response_prefix'])) !== 0)
  920. $form_subject = $context['response_prefix'] . $form_subject;
  921. if (isset($_REQUEST['quote']))
  922. {
  923. // Remove any nested quotes and <br />...
  924. $form_message = preg_replace('~<br ?/?' . '>~i', "\n", $row_quoted['body']);
  925. if (!empty($modSettings['removeNestedQuotes']))
  926. $form_message = preg_replace(array('~\n?\[quote.*?\].+?\[/quote\]\n?~is', '~^\n~', '~\[/quote\]~'), '', $form_message);
  927. if (empty($row_quoted['id_member']))
  928. $form_message = '[quote author=&quot;' . $row_quoted['real_name'] . '&quot;]' . "\n" . $form_message . "\n" . '[/quote]';
  929. else
  930. $form_message = '[quote author=' . $row_quoted['real_name'] . ' link=action=profile;u=' . $row_quoted['id_member'] . ' date=' . $row_quoted['msgtime'] . ']' . "\n" . $form_message . "\n" . '[/quote]';
  931. }
  932. else
  933. $form_message = '';
  934. // Do the BBC thang on the message.
  935. $row_quoted['body'] = parse_bbc($row_quoted['body'], true, 'pm' . $row_quoted['id_pm']);
  936. // Set up the quoted message array.
  937. $context['quoted_message'] = array(
  938. 'id' => $row_quoted['id_pm'],
  939. 'pm_head' => $row_quoted['pm_head'],
  940. 'member' => array(
  941. 'name' => $row_quoted['real_name'],
  942. 'username' => $row_quoted['member_name'],
  943. 'id' => $row_quoted['id_member'],
  944. 'href' => !empty($row_quoted['id_member']) ? $scripturl . '?action=profile;u=' . $row_quoted['id_member'] : '',
  945. 'link' => !empty($row_quoted['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row_quoted['id_member'] . '">' . $row_quoted['real_name'] . '</a>' : $row_quoted['real_name'],
  946. ),
  947. 'subject' => $row_quoted['subject'],
  948. 'time' => timeformat($row_quoted['msgtime']),
  949. 'timestamp' => forum_time(true, $row_quoted['msgtime']),
  950. 'body' => $row_quoted['body']
  951. );
  952. }
  953. else
  954. {
  955. $context['quoted_message'] = false;
  956. $form_subject = '';
  957. $form_message = '';
  958. }
  959. $context['recipients'] = array(
  960. 'to' => array(),
  961. 'bcc' => array(),
  962. );
  963. // Sending by ID? Replying to all? Fetch the real_name(s).
  964. if (isset($_REQUEST['u']))
  965. {
  966. // If the user is replying to all, get all the other members this was sent to..
  967. if ($_REQUEST['u'] == 'all' && isset($row_quoted))
  968. {
  969. // Firstly, to reply to all we clearly already have $row_quoted - so have the original member from.
  970. if ($row_quoted['id_member'] != $user_info['id'])
  971. $context['recipients']['to'][] = array(
  972. 'id' => $row_quoted['id_member'],
  973. 'name' => htmlspecialchars($row_quoted['real_name']),
  974. );
  975. // Now to get the others.
  976. $request = $smcFunc['db_query']('', '
  977. SELECT mem.id_member, mem.real_name
  978. FROM {db_prefix}pm_recipients AS pmr
  979. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = pmr.id_member)
  980. WHERE pmr.id_pm = {int:id_pm}
  981. AND pmr.id_member != {int:current_member}
  982. AND pmr.bcc = {int:not_bcc}',
  983. array(
  984. 'current_member' => $user_info['id'],
  985. 'id_pm' => $pmsg,
  986. 'not_bcc' => 0,
  987. )
  988. );
  989. while ($row = $smcFunc['db_fetch_assoc']($request))
  990. $context['recipients']['to'][] = array(
  991. 'id' => $row['id_member'],
  992. 'name' => $row['real_name'],
  993. );
  994. $smcFunc['db_free_result']($request);
  995. }
  996. else
  997. {
  998. $_REQUEST['u'] = explode(',', $_REQUEST['u']);
  999. foreach ($_REQUEST['u'] as $key => $uID)
  1000. $_REQUEST['u'][$key] = (int) $uID;
  1001. $_REQUEST['u'] = array_unique($_REQUEST['u']);
  1002. $request = $smcFunc['db_query']('', '
  1003. SELECT id_member, real_name
  1004. FROM {db_prefix}members
  1005. WHERE id_member IN ({array_int:member_list})
  1006. LIMIT ' . count($_REQUEST['u']),
  1007. array(
  1008. 'member_list' => $_REQUEST['u'],
  1009. )
  1010. );
  1011. while ($row = $smcFunc['db_fetch_assoc']($request))
  1012. $context['recipients']['to'][] = array(
  1013. 'id' => $row['id_member'],
  1014. 'name' => $row['real_name'],
  1015. );
  1016. $smcFunc['db_free_result']($request);
  1017. }
  1018. // Get a literal name list in case the user has JavaScript disabled.
  1019. $names = array();
  1020. foreach ($context['recipients']['to'] as $to)
  1021. $names[] = $to['name'];
  1022. $context['to_value'] = empty($names) ? '' : '&quot;' . implode('&quot;, &quot;', $names) . '&quot;';
  1023. }
  1024. else
  1025. $context['to_value'] = '';
  1026. // Set the defaults...
  1027. $context['subject'] = $form_subject;
  1028. $context['message'] = str_replace(array('"', '<', '>', '&nbsp;'), array('&quot;', '&lt;', '&gt;', ' '), $form_message);
  1029. $context['copy_to_outbox'] = !empty($options['copy_to_outbox']);
  1030. // And build the link tree.
  1031. $context['linktree'][] = array(
  1032. 'url' => $scripturl . '?action=pm;sa=send',
  1033. 'name' => $txt['new_message']
  1034. );
  1035. $modSettings['disable_wysiwyg'] = !empty($modSettings['disable_wysiwyg']) || empty($modSettings['enableBBC']);
  1036. // Generate a list of drafts that they can load in to the editor
  1037. if (!empty($context['drafts_pm_save']))
  1038. {
  1039. require_once(CONTROLLERDIR . '/Drafts.controller.php');
  1040. $pm_seed = isset($_REQUEST['pmsg']) ? $_REQUEST['pmsg'] : (isset($_REQUEST['quote']) ? $_REQUEST['quote'] : 0);
  1041. action_showDrafts($user_info['id'], $pm_seed, 1);
  1042. }
  1043. // Needed for the WYSIWYG editor.
  1044. require_once(SUBSDIR . '/Editor.subs.php');
  1045. // Now create the editor.
  1046. $editorOptions = array(
  1047. 'id' => 'message',
  1048. 'value' => $context['message'],
  1049. 'height' => '250px',
  1050. 'width' => '100%',
  1051. 'labels' => array(
  1052. 'post_button' => $txt['send_message'],
  1053. ),
  1054. 'preview_type' => 2,
  1055. );
  1056. create_control_richedit($editorOptions);
  1057. // Store the ID for old compatibility.
  1058. $context['post_box_name'] = $editorOptions['id'];
  1059. $context['bcc_value'] = '';
  1060. $context['require_verification'] = !$user_info['is_admin'] && !empty($modSettings['pm_posts_verification']) && $user_info['posts'] < $modSettings['pm_posts_verification'];
  1061. if ($context['require_verification'])
  1062. {
  1063. $verificationOptions = array(
  1064. 'id' => 'pm',
  1065. );
  1066. $context['require_verification'] = create_control_verification($verificationOptions);
  1067. $context['visual_verification_id'] = $verificationOptions['id'];
  1068. }
  1069. // Register this form and get a sequence number in $context.
  1070. checkSubmitOnce('register');
  1071. }
  1072. /**
  1073. * This function allows the user to view their PM drafts
  1074. * Accessed by ?action=pm;sa=showpmdrafts
  1075. */
  1076. function action_messagedrafts()
  1077. {
  1078. global $user_info;
  1079. // validate with loadMemberData()
  1080. $memberResult = loadMemberData($user_info['id'], false);
  1081. if (!is_array($memberResult))
  1082. fatal_lang_error('not_a_user', false);
  1083. list ($memID) = $memberResult;
  1084. // drafts is where the functions reside
  1085. require_once(CONTROLLERDIR . '/Drafts.controller.php');
  1086. action_showPMDrafts($memID);
  1087. }
  1088. /**
  1089. * An error in the message...
  1090. *
  1091. * @param $named_recipients
  1092. * @param $recipient_ids
  1093. */
  1094. function messagePostError($named_recipients, $recipient_ids = array())
  1095. {
  1096. global $txt, $context, $scripturl, $modSettings;
  1097. global $smcFunc, $user_info;
  1098. if (!isset($_REQUEST['xml']))
  1099. $context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'send';
  1100. if (!isset($_REQUEST['xml']))
  1101. $context['sub_template'] = 'send';
  1102. elseif (isset($_REQUEST['xml']))
  1103. $context['sub_template'] = 'pm';
  1104. $context['page_title'] = $txt['send_message'];
  1105. $error_types = error_context::context('pm', 1);
  1106. // Got some known members?
  1107. $context['recipients'] = array(
  1108. 'to' => array(),
  1109. 'bcc' => array(),
  1110. );
  1111. if (!empty($recipient_ids['to']) || !empty($recipient_ids['bcc']))
  1112. {
  1113. $allRecipients = array_merge($recipient_ids['to'], $recipient_ids['bcc']);
  1114. $request = $smcFunc['db_query']('', '
  1115. SELECT id_member, real_name
  1116. FROM {db_prefix}members
  1117. WHERE id_member IN ({array_int:member_list})',
  1118. array(
  1119. 'member_list' => $allRecipients,
  1120. )
  1121. );
  1122. while ($row = $smcFunc['db_fetch_assoc']($request))
  1123. {
  1124. $recipientType = in_array($row['id_member'], $recipient_ids['bcc']) ? 'bcc' : 'to';
  1125. $context['recipients'][$recipientType][] = array(
  1126. 'id' => $row['id_member'],
  1127. 'name' => $row['real_name'],
  1128. );
  1129. }
  1130. $smcFunc['db_free_result']($request);
  1131. }
  1132. // Set everything up like before....
  1133. $context['subject'] = isset($_REQUEST['subject']) ? $smcFunc['htmlspecialchars']($_REQUEST['subject']) : '';
  1134. $context['message'] = isset($_REQUEST['message']) ? str_replace(array(' '), array('&nbsp; '), $smcFunc['htmlspecialchars']($_REQUEST['message'])) : '';
  1135. $context['copy_to_outbox'] = !empty($_REQUEST['outbox']);
  1136. $context['reply'] = !empty($_REQUEST['replied_to']);
  1137. if ($context['reply'])
  1138. {
  1139. $_REQUEST['replied_to'] = (int) $_REQUEST['replied_to'];
  1140. $request = $smcFunc['db_query']('', '
  1141. SELECT
  1142. pm.id_pm, CASE WHEN pm.id_pm_head = {int:no_id_pm_head} THEN pm.id_pm ELSE pm.id_pm_head END AS pm_head,
  1143. pm.body, pm.subject, pm.msgtime, mem.member_name, IFNULL(mem.id_member, 0) AS id_member,
  1144. IFNULL(mem.real_name, pm.from_name) AS real_name
  1145. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' : '
  1146. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = {int:replied_to})') . '
  1147. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  1148. WHERE pm.id_pm = {int:replied_to}' . ($context['folder'] == 'sent' ? '
  1149. AND pm.id_member_from = {int:current_member}' : '
  1150. AND pmr.id_member = {int:current_member}') . '
  1151. LIMIT 1',
  1152. array(
  1153. 'current_member' => $user_info['id'],
  1154. 'no_id_pm_head' => 0,
  1155. 'replied_to' => $_REQUEST['replied_to'],
  1156. )
  1157. );
  1158. if ($smcFunc['db_num_rows']($request) == 0)
  1159. {
  1160. if (!isset($_REQUEST['xml']))
  1161. fatal_lang_error('pm_not_yours', false);
  1162. else
  1163. $error_types->addError('pm_not_yours');
  1164. }
  1165. $row_quoted = $smcFunc['db_fetch_assoc']($request);
  1166. $smcFunc['db_free_result']($request);
  1167. censorText($row_quoted['subject']);
  1168. censorText($

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