PageRenderTime 179ms CodeModel.GetById 23ms RepoModel.GetById 5ms app.codeStats 1ms

/Sources/PersonalMessage.php

https://github.com/smf-portal/SMF2.1
PHP | 3713 lines | 2788 code | 458 blank | 467 comment | 657 complexity | e79c22de05705a2e7ed95b31e2fdc30f MD5 | raw file

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

  1. <?php
  2. /**
  3. * This file is mainly meant for controlling the actions related to personal
  4. * messages. It allows viewing, sending, deleting, and marking personal
  5. * messages. For compatibility reasons, they are often called "instant messages".
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2012 Simple Machines
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('Hacking attempt...');
  18. /**
  19. * This helps organize things...
  20. * @todo this should be a simple dispatcher....
  21. */
  22. function MessageMain()
  23. {
  24. global $txt, $scripturl, $sourcedir, $context, $user_info, $user_settings, $smcFunc, $modSettings;
  25. // No guests!
  26. is_not_guest();
  27. // You're not supposed to be here at all, if you can't even read PMs.
  28. isAllowedTo('pm_read');
  29. // This file contains the basic functions for sending a PM.
  30. require_once($sourcedir . '/Subs-Post.php');
  31. loadLanguage('PersonalMessage+Drafts');
  32. if (WIRELESS && WIRELESS_PROTOCOL == 'wap')
  33. fatal_lang_error('wireless_error_notyet', false);
  34. elseif (WIRELESS)
  35. $context['sub_template'] = WIRELESS_PROTOCOL . '_pm';
  36. elseif (!isset($_REQUEST['xml']))
  37. loadTemplate('PersonalMessage');
  38. // Load up the members maximum message capacity.
  39. if ($user_info['is_admin'])
  40. $context['message_limit'] = 0;
  41. elseif (($context['message_limit'] = cache_get_data('msgLimit:' . $user_info['id'], 360)) === null)
  42. {
  43. // @todo Why do we do this? It seems like if they have any limit we should use it.
  44. $request = $smcFunc['db_query']('', '
  45. SELECT MAX(max_messages) AS top_limit, MIN(max_messages) AS bottom_limit
  46. FROM {db_prefix}membergroups
  47. WHERE id_group IN ({array_int:users_groups})',
  48. array(
  49. 'users_groups' => $user_info['groups'],
  50. )
  51. );
  52. list ($maxMessage, $minMessage) = $smcFunc['db_fetch_row']($request);
  53. $smcFunc['db_free_result']($request);
  54. $context['message_limit'] = $minMessage == 0 ? 0 : $maxMessage;
  55. // Save us doing it again!
  56. cache_put_data('msgLimit:' . $user_info['id'], $context['message_limit'], 360);
  57. }
  58. // Prepare the context for the capacity bar.
  59. if (!empty($context['message_limit']))
  60. {
  61. $bar = ($user_info['messages'] * 100) / $context['message_limit'];
  62. $context['limit_bar'] = array(
  63. 'messages' => $user_info['messages'],
  64. 'allowed' => $context['message_limit'],
  65. 'percent' => $bar,
  66. 'bar' => min(100, (int) $bar),
  67. 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], round($bar, 1)),
  68. );
  69. }
  70. // a previous message was sent successfully? show a small indication.
  71. if (isset($_GET['done']) && ($_GET['done'] == 'sent'))
  72. $context['pm_sent'] = true;
  73. // Now we have the labels, and assuming we have unsorted mail, apply our rules!
  74. if ($user_settings['new_pm'])
  75. {
  76. $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
  77. foreach ($context['labels'] as $id_label => $label_name)
  78. $context['labels'][(int) $id_label] = array(
  79. 'id' => $id_label,
  80. 'name' => trim($label_name),
  81. 'messages' => 0,
  82. 'unread_messages' => 0,
  83. );
  84. $context['labels'][-1] = array(
  85. 'id' => -1,
  86. 'name' => $txt['pm_msg_label_inbox'],
  87. 'messages' => 0,
  88. 'unread_messages' => 0,
  89. );
  90. ApplyRules();
  91. updateMemberData($user_info['id'], array('new_pm' => 0));
  92. $smcFunc['db_query']('', '
  93. UPDATE {db_prefix}pm_recipients
  94. SET is_new = {int:not_new}
  95. WHERE id_member = {int:current_member}',
  96. array(
  97. 'current_member' => $user_info['id'],
  98. 'not_new' => 0,
  99. )
  100. );
  101. }
  102. // Load the label data.
  103. if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
  104. {
  105. $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
  106. foreach ($context['labels'] as $id_label => $label_name)
  107. $context['labels'][(int) $id_label] = array(
  108. 'id' => $id_label,
  109. 'name' => trim($label_name),
  110. 'messages' => 0,
  111. 'unread_messages' => 0,
  112. );
  113. $context['labels'][-1] = array(
  114. 'id' => -1,
  115. 'name' => $txt['pm_msg_label_inbox'],
  116. 'messages' => 0,
  117. 'unread_messages' => 0,
  118. );
  119. // Looks like we need to reseek!
  120. $result = $smcFunc['db_query']('', '
  121. SELECT labels, is_read, COUNT(*) AS num
  122. FROM {db_prefix}pm_recipients
  123. WHERE id_member = {int:current_member}
  124. AND deleted = {int:not_deleted}
  125. GROUP BY labels, is_read',
  126. array(
  127. 'current_member' => $user_info['id'],
  128. 'not_deleted' => 0,
  129. )
  130. );
  131. while ($row = $smcFunc['db_fetch_assoc']($result))
  132. {
  133. $this_labels = explode(',', $row['labels']);
  134. foreach ($this_labels as $this_label)
  135. {
  136. $context['labels'][(int) $this_label]['messages'] += $row['num'];
  137. if (!($row['is_read'] & 1))
  138. $context['labels'][(int) $this_label]['unread_messages'] += $row['num'];
  139. }
  140. }
  141. $smcFunc['db_free_result']($result);
  142. // Store it please!
  143. cache_put_data('labelCounts:' . $user_info['id'], $context['labels'], 720);
  144. }
  145. // This determines if we have more labels than just the standard inbox.
  146. $context['currently_using_labels'] = count($context['labels']) > 1 ? 1 : 0;
  147. // Some stuff for the labels...
  148. $context['current_label_id'] = isset($_REQUEST['l']) && isset($context['labels'][(int) $_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
  149. $context['current_label'] = &$context['labels'][(int) $context['current_label_id']]['name'];
  150. $context['folder'] = !isset($_REQUEST['f']) || $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent';
  151. // This is convenient. Do you know how annoying it is to do this every time?!
  152. $context['current_label_redirect'] = 'action=pm;f=' . $context['folder'] . (isset($_GET['start']) ? ';start=' . $_GET['start'] : '') . (isset($_REQUEST['l']) ? ';l=' . $_REQUEST['l'] : '');
  153. $context['can_issue_warning'] = in_array('w', $context['admin_features']) && allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
  154. // Are PM drafts enabled?
  155. $context['drafts_pm_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']) && allowedTo('pm_draft');
  156. $context['drafts_autosave'] = !empty($context['drafts_pm_save']) && !empty($modSettings['drafts_autosave_enabled']) && allowedTo('pm_autosave_draft');
  157. // Build the linktree for all the actions...
  158. $context['linktree'][] = array(
  159. 'url' => $scripturl . '?action=pm',
  160. 'name' => $txt['personal_messages']
  161. );
  162. // Preferences...
  163. $context['display_mode'] = WIRELESS ? 0 : $user_settings['pm_prefs'] & 3;
  164. $subActions = array(
  165. 'addbuddy' => 'WirelessAddBuddy',
  166. 'manlabels' => 'ManageLabels',
  167. 'manrules' => 'ManageRules',
  168. 'pmactions' => 'MessageActionsApply',
  169. 'prune' => 'MessagePrune',
  170. 'removeall' => 'MessageKillAllQuery',
  171. 'removeall2' => 'MessageKillAll',
  172. 'report' => 'ReportMessage',
  173. 'search' => 'MessageSearch',
  174. 'search2' => 'MessageSearch2',
  175. 'send' => 'MessagePost',
  176. 'send2' => 'MessagePost2',
  177. 'settings' => 'MessageSettings',
  178. 'showpmdrafts' => 'MessageDrafts',
  179. );
  180. if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
  181. MessageFolder();
  182. else
  183. {
  184. if (!isset($_REQUEST['xml']))
  185. messageIndexBar($_REQUEST['sa']);
  186. $subActions[$_REQUEST['sa']]();
  187. }
  188. }
  189. /**
  190. * A menu to easily access different areas of the PM section
  191. *
  192. * @param string $area
  193. */
  194. function messageIndexBar($area)
  195. {
  196. global $txt, $context, $scripturl, $sourcedir, $sc, $modSettings, $settings, $user_info, $options;
  197. $pm_areas = array(
  198. 'folders' => array(
  199. 'title' => $txt['pm_messages'],
  200. 'areas' => array(
  201. 'send' => array(
  202. 'label' => $txt['new_message'],
  203. 'custom_url' => $scripturl . '?action=pm;sa=send',
  204. 'permission' => allowedTo('pm_send'),
  205. ),
  206. 'inbox' => array(
  207. 'label' => $txt['inbox'],
  208. 'custom_url' => $scripturl . '?action=pm',
  209. ),
  210. 'sent' => array(
  211. 'label' => $txt['sent_items'],
  212. 'custom_url' => $scripturl . '?action=pm;f=sent',
  213. ),
  214. 'drafts' => array(
  215. 'label' => $txt['drafts_show'],
  216. 'custom_url' => $scripturl . '?action=pm;sa=showpmdrafts',
  217. 'permission' => allowedTo('pm_draft'),
  218. 'enabled' => !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_pm_enabled']),
  219. ),
  220. ),
  221. ),
  222. 'labels' => array(
  223. 'title' => $txt['pm_labels'],
  224. 'areas' => array(),
  225. ),
  226. 'actions' => array(
  227. 'title' => $txt['pm_actions'],
  228. 'areas' => array(
  229. 'search' => array(
  230. 'label' => $txt['pm_search_bar_title'],
  231. 'custom_url' => $scripturl . '?action=pm;sa=search',
  232. ),
  233. 'prune' => array(
  234. 'label' => $txt['pm_prune'],
  235. 'custom_url' => $scripturl . '?action=pm;sa=prune'
  236. ),
  237. ),
  238. ),
  239. 'pref' => array(
  240. 'title' => $txt['pm_preferences'],
  241. 'areas' => array(
  242. 'manlabels' => array(
  243. 'label' => $txt['pm_manage_labels'],
  244. 'custom_url' => $scripturl . '?action=pm;sa=manlabels',
  245. ),
  246. 'manrules' => array(
  247. 'label' => $txt['pm_manage_rules'],
  248. 'custom_url' => $scripturl . '?action=pm;sa=manrules',
  249. ),
  250. 'settings' => array(
  251. 'label' => $txt['pm_settings'],
  252. 'custom_url' => $scripturl . '?action=pm;sa=settings',
  253. ),
  254. ),
  255. ),
  256. );
  257. // Handle labels.
  258. if (empty($context['currently_using_labels']))
  259. unset($pm_areas['labels']);
  260. else
  261. {
  262. // Note we send labels by id as it will have less problems in the querystring.
  263. $unread_in_labels = 0;
  264. foreach ($context['labels'] as $label)
  265. {
  266. if ($label['id'] == -1)
  267. continue;
  268. // Count the amount of unread items in labels.
  269. $unread_in_labels += $label['unread_messages'];
  270. // Add the label to the menu.
  271. $pm_areas['labels']['areas']['label' . $label['id']] = array(
  272. 'label' => $label['name'] . (!empty($label['unread_messages']) ? ' (<strong>' . $label['unread_messages'] . '</strong>)' : ''),
  273. 'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
  274. 'unread_messages' => $label['unread_messages'],
  275. 'messages' => $label['messages'],
  276. );
  277. }
  278. if (!empty($unread_in_labels))
  279. $pm_areas['labels']['title'] .= ' (' . $unread_in_labels . ')';
  280. }
  281. $pm_areas['folders']['areas']['inbox']['unread_messages'] = &$context['labels'][-1]['unread_messages'];
  282. $pm_areas['folders']['areas']['inbox']['messages'] = &$context['labels'][-1]['messages'];
  283. if (!empty($context['labels'][-1]['unread_messages']))
  284. {
  285. $pm_areas['folders']['areas']['inbox']['label'] .= ' (<strong>' . $context['labels'][-1]['unread_messages'] . '</strong>)';
  286. $pm_areas['folders']['title'] .= ' (' . $context['labels'][-1]['unread_messages'] . ')';
  287. }
  288. // Do we have a limit on the amount of messages we can keep?
  289. if (!empty($context['message_limit']))
  290. {
  291. $bar = round(($user_info['messages'] * 100) / $context['message_limit'], 1);
  292. $context['limit_bar'] = array(
  293. 'messages' => $user_info['messages'],
  294. 'allowed' => $context['message_limit'],
  295. 'percent' => $bar,
  296. 'bar' => $bar > 100 ? 100 : (int) $bar,
  297. 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], $bar)
  298. );
  299. }
  300. require_once($sourcedir . '/Subs-Menu.php');
  301. // What page is this, again?
  302. $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'] : '');
  303. // Set a few options for the menu.
  304. $menuOptions = array(
  305. 'current_area' => $area,
  306. 'disable_url_session_check' => true,
  307. );
  308. // Actually create the menu!
  309. $pm_include_data = createMenu($pm_areas, $menuOptions);
  310. unset($pm_areas);
  311. // No menu means no access.
  312. if (!$pm_include_data && (!$user_info['is_guest'] || validateSession()))
  313. fatal_lang_error('no_access', false);
  314. // Make a note of the Unique ID for this menu.
  315. $context['pm_menu_id'] = $context['max_menu_id'];
  316. $context['pm_menu_name'] = 'menu_data_' . $context['pm_menu_id'];
  317. // Set the selected item.
  318. $current_area = $pm_include_data['current_area'];
  319. $context['menu_item_selected'] = $current_area;
  320. // Set the template for this area and add the profile layer.
  321. if (!WIRELESS && !isset($_REQUEST['xml']))
  322. $context['template_layers'][] = 'pm';
  323. }
  324. /**
  325. * A folder, ie. inbox/sent etc.
  326. */
  327. function MessageFolder()
  328. {
  329. global $txt, $scripturl, $modSettings, $context, $subjects_request;
  330. global $messages_request, $user_info, $recipients, $options, $smcFunc, $memberContext, $user_settings;
  331. // Changing view?
  332. if (isset($_GET['view']))
  333. {
  334. $context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
  335. updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
  336. }
  337. // Make sure the starting location is valid.
  338. if (isset($_GET['start']) && $_GET['start'] != 'new')
  339. $_GET['start'] = (int) $_GET['start'];
  340. elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first']))
  341. $_GET['start'] = 0;
  342. else
  343. $_GET['start'] = 'new';
  344. // Set up some basic theme stuff.
  345. $context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
  346. $context['get_pmessage'] = 'prepareMessageContext';
  347. $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
  348. $context['disabled_fields'] = isset($modSettings['disabled_profile_fields']) ? array_flip(explode(',', $modSettings['disabled_profile_fields'])) : array();
  349. $labelQuery = $context['folder'] != 'sent' ? '
  350. AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
  351. // Set the index bar correct!
  352. messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
  353. // Sorting the folder.
  354. $sort_methods = array(
  355. 'date' => 'pm.id_pm',
  356. 'name' => 'IFNULL(mem.real_name, \'\')',
  357. 'subject' => 'pm.subject',
  358. );
  359. // They didn't pick one, use the forum default.
  360. if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']]))
  361. {
  362. $context['sort_by'] = 'date';
  363. $_GET['sort'] = 'pm.id_pm';
  364. // An overriding setting?
  365. $descending = !empty($options['view_newest_pm_first']);
  366. }
  367. // Otherwise use the defaults: ascending, by date.
  368. else
  369. {
  370. $context['sort_by'] = $_GET['sort'];
  371. $_GET['sort'] = $sort_methods[$_GET['sort']];
  372. $descending = isset($_GET['desc']);
  373. }
  374. $context['sort_direction'] = $descending ? 'down' : 'up';
  375. // Set the text to resemble the current folder.
  376. $pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
  377. $txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
  378. // Now, build the link tree!
  379. if ($context['current_label_id'] == -1)
  380. $context['linktree'][] = array(
  381. 'url' => $scripturl . '?action=pm;f=' . $context['folder'],
  382. 'name' => $pmbox
  383. );
  384. // Build it further for a label.
  385. if ($context['current_label_id'] != -1)
  386. $context['linktree'][] = array(
  387. 'url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'],
  388. 'name' => $txt['pm_current_label'] . ': ' . $context['current_label']
  389. );
  390. // Figure out how many messages there are.
  391. if ($context['folder'] == 'sent')
  392. $request = $smcFunc['db_query']('', '
  393. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  394. FROM {db_prefix}personal_messages AS pm
  395. WHERE pm.id_member_from = {int:current_member}
  396. AND pm.deleted_by_sender = {int:not_deleted}',
  397. array(
  398. 'current_member' => $user_info['id'],
  399. 'not_deleted' => 0,
  400. )
  401. );
  402. else
  403. $request = $smcFunc['db_query']('', '
  404. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  405. FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
  406. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
  407. WHERE pmr.id_member = {int:current_member}
  408. AND pmr.deleted = {int:not_deleted}' . $labelQuery,
  409. array(
  410. 'current_member' => $user_info['id'],
  411. 'not_deleted' => 0,
  412. )
  413. );
  414. list ($max_messages) = $smcFunc['db_fetch_row']($request);
  415. $smcFunc['db_free_result']($request);
  416. // Only show the button if there are messages to delete.
  417. $context['show_delete'] = $max_messages > 0;
  418. // Start on the last page.
  419. if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages)
  420. $_GET['start'] = ($max_messages - 1) - (($max_messages - 1) % $modSettings['defaultMaxMessages']);
  421. elseif ($_GET['start'] < 0)
  422. $_GET['start'] = 0;
  423. // ... but wait - what if we want to start from a specific message?
  424. if (isset($_GET['pmid']))
  425. {
  426. $pmID = (int) $_GET['pmid'];
  427. // Make sure you have access to this PM.
  428. if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
  429. fatal_lang_error('no_access', false);
  430. $context['current_pm'] = $pmID;
  431. // With only one page of PM's we're gonna want page 1.
  432. if ($max_messages <= $modSettings['defaultMaxMessages'])
  433. $_GET['start'] = 0;
  434. // If we pass kstart we assume we're in the right place.
  435. elseif (!isset($_GET['kstart']))
  436. {
  437. if ($context['folder'] == 'sent')
  438. $request = $smcFunc['db_query']('', '
  439. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  440. FROM {db_prefix}personal_messages
  441. WHERE id_member_from = {int:current_member}
  442. AND deleted_by_sender = {int:not_deleted}
  443. AND id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
  444. array(
  445. 'current_member' => $user_info['id'],
  446. 'not_deleted' => 0,
  447. 'id_pm' => $pmID,
  448. )
  449. );
  450. else
  451. $request = $smcFunc['db_query']('', '
  452. SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
  453. FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
  454. INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
  455. WHERE pmr.id_member = {int:current_member}
  456. AND pmr.deleted = {int:not_deleted}' . $labelQuery . '
  457. AND pmr.id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
  458. array(
  459. 'current_member' => $user_info['id'],
  460. 'not_deleted' => 0,
  461. 'id_pm' => $pmID,
  462. )
  463. );
  464. list ($_GET['start']) = $smcFunc['db_fetch_row']($request);
  465. $smcFunc['db_free_result']($request);
  466. // To stop the page index's being abnormal, start the page on the page the message would normally be located on...
  467. $_GET['start'] = $modSettings['defaultMaxMessages'] * (int) ($_GET['start'] / $modSettings['defaultMaxMessages']);
  468. }
  469. }
  470. // Sanitize and validate pmsg variable if set.
  471. if (isset($_GET['pmsg']))
  472. {
  473. $pmsg = (int) $_GET['pmsg'];
  474. if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
  475. fatal_lang_error('no_access', false);
  476. }
  477. // Set up the page index.
  478. $context['page_index'] = constructPageIndex($scripturl . '?action=pm;f=' . $context['folder'] . (isset($_REQUEST['l']) ? ';l=' . (int) $_REQUEST['l'] : '') . ';sort=' . $context['sort_by'] . ($descending ? ';desc' : ''), $_GET['start'], $max_messages, $modSettings['defaultMaxMessages']);
  479. $context['start'] = $_GET['start'];
  480. // Determine the navigation context (especially useful for the wireless template).
  481. $context['links'] = array(
  482. 'first' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=0' : '',
  483. 'prev' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $modSettings['defaultMaxMessages']) : '',
  484. 'next' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $modSettings['defaultMaxMessages']) : '',
  485. 'last' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) * $modSettings['defaultMaxMessages']) : '',
  486. 'up' => $scripturl,
  487. );
  488. $context['page_info'] = array(
  489. 'current_page' => $_GET['start'] / $modSettings['defaultMaxMessages'] + 1,
  490. 'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1
  491. );
  492. // First work out what messages we need to see - if grouped is a little trickier...
  493. if ($context['display_mode'] == 2)
  494. {
  495. // On a non-default sort due to PostgreSQL we have to do a harder sort.
  496. if ($smcFunc['db_title'] == 'PostgreSQL' && $_GET['sort'] != 'pm.id_pm')
  497. {
  498. $sub_request = $smcFunc['db_query']('', '
  499. SELECT MAX({raw:sort}) AS sort_param, pm.id_pm_head
  500. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['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:not_deleted}
  505. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  506. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
  507. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
  508. AND pm.deleted_by_sender = {int:not_deleted}' : '1=1') . (empty($pmsg) ? '' : '
  509. AND pm.id_pm = {int:id_pm}') . '
  510. GROUP BY pm.id_pm_head
  511. ORDER BY sort_param' . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  512. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  513. array(
  514. 'current_member' => $user_info['id'],
  515. 'not_deleted' => 0,
  516. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  517. 'id_pm' => isset($pmsg) ? $pmsg : '0',
  518. 'sort' => $_GET['sort'],
  519. )
  520. );
  521. $sub_pms = array();
  522. while ($row = $smcFunc['db_fetch_assoc']($sub_request))
  523. $sub_pms[$row['id_pm_head']] = $row['sort_param'];
  524. $smcFunc['db_free_result']($sub_request);
  525. $request = $smcFunc['db_query']('', '
  526. SELECT pm.id_pm AS id_pm, pm.id_pm_head
  527. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
  528. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  529. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  530. AND pmr.id_member = {int:current_member}
  531. AND pmr.deleted = {int:not_deleted}
  532. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  533. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
  534. WHERE ' . (empty($sub_pms) ? '0=1' : 'pm.id_pm IN ({array_int:pm_list})') . '
  535. ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  536. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  537. array(
  538. 'current_member' => $user_info['id'],
  539. 'pm_list' => array_keys($sub_pms),
  540. 'not_deleted' => 0,
  541. 'sort' => $_GET['sort'],
  542. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  543. )
  544. );
  545. }
  546. else
  547. {
  548. $request = $smcFunc['db_query']('pm_conversation_list', '
  549. SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
  550. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
  551. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  552. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  553. AND pmr.id_member = {int:current_member}
  554. AND pmr.deleted = {int:deleted_by}
  555. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  556. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
  557. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
  558. AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
  559. AND pm.id_pm = {int:pmsg}') . '
  560. GROUP BY pm.id_pm_head
  561. ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
  562. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  563. array(
  564. 'current_member' => $user_info['id'],
  565. 'deleted_by' => 0,
  566. 'sort' => $_GET['sort'],
  567. 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  568. 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
  569. )
  570. );
  571. }
  572. }
  573. // This is kinda simple!
  574. else
  575. {
  576. // @todo SLOW This query uses a filesort. (inbox only.)
  577. $request = $smcFunc['db_query']('', '
  578. SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
  579. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($context['sort_by'] == 'name' ? '
  580. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
  581. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
  582. AND pmr.id_member = {int:current_member}
  583. AND pmr.deleted = {int:is_deleted}
  584. ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
  585. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
  586. WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {raw:current_member}
  587. AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
  588. AND pm.id_pm = {int:pmsg}') . '
  589. ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
  590. LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
  591. array(
  592. 'current_member' => $user_info['id'],
  593. 'is_deleted' => 0,
  594. 'sort' => $_GET['sort'],
  595. 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  596. 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
  597. )
  598. );
  599. }
  600. // Load the id_pms and initialize recipients.
  601. $pms = array();
  602. $lastData = array();
  603. $posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
  604. $recipients = array();
  605. while ($row = $smcFunc['db_fetch_assoc']($request))
  606. {
  607. if (!isset($recipients[$row['id_pm']]))
  608. {
  609. if (isset($row['id_member_from']))
  610. $posters[$row['id_pm']] = $row['id_member_from'];
  611. $pms[$row['id_pm']] = $row['id_pm'];
  612. $recipients[$row['id_pm']] = array(
  613. 'to' => array(),
  614. 'bcc' => array()
  615. );
  616. }
  617. // Keep track of the last message so we know what the head is without another query!
  618. if ((empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData))) || empty($lastData) || (!empty($pmID) && $pmID == $row['id_pm']))
  619. $lastData = array(
  620. 'id' => $row['id_pm'],
  621. 'head' => $row['id_pm_head'],
  622. );
  623. }
  624. $smcFunc['db_free_result']($request);
  625. // Make sure that we have been given a correct head pm id!
  626. if ($context['display_mode'] == 2 && !empty($pmID) && $pmID != $lastData['id'])
  627. fatal_lang_error('no_access', false);
  628. if (!empty($pms))
  629. {
  630. // Select the correct current message.
  631. if (empty($pmID))
  632. $context['current_pm'] = $lastData['id'];
  633. // This is a list of the pm's that are used for "full" display.
  634. if ($context['display_mode'] == 0)
  635. $display_pms = $pms;
  636. else
  637. $display_pms = array($context['current_pm']);
  638. // At this point we know the main id_pm's. But - if we are looking at conversations we need the others!
  639. if ($context['display_mode'] == 2)
  640. {
  641. $request = $smcFunc['db_query']('', '
  642. SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
  643. FROM {db_prefix}personal_messages AS pm
  644. INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
  645. WHERE pm.id_pm_head = {int:id_pm_head}
  646. AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
  647. OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
  648. ORDER BY pm.id_pm',
  649. array(
  650. 'current_member' => $user_info['id'],
  651. 'id_pm_head' => $lastData['head'],
  652. 'not_deleted' => 0,
  653. )
  654. );
  655. while ($row = $smcFunc['db_fetch_assoc']($request))
  656. {
  657. // This is, frankly, a joke. We will put in a workaround for people sending to themselves - yawn!
  658. if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1)
  659. continue;
  660. elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1)
  661. continue;
  662. if (!isset($recipients[$row['id_pm']]))
  663. $recipients[$row['id_pm']] = array(
  664. 'to' => array(),
  665. 'bcc' => array()
  666. );
  667. $display_pms[] = $row['id_pm'];
  668. $posters[$row['id_pm']] = $row['id_member_from'];
  669. }
  670. $smcFunc['db_free_result']($request);
  671. }
  672. // This is pretty much EVERY pm!
  673. $all_pms = array_merge($pms, $display_pms);
  674. $all_pms = array_unique($all_pms);
  675. // Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
  676. $request = $smcFunc['db_query']('', '
  677. 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
  678. FROM {db_prefix}pm_recipients AS pmr
  679. LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
  680. WHERE pmr.id_pm IN ({array_int:pm_list})',
  681. array(
  682. 'pm_list' => $all_pms,
  683. )
  684. );
  685. $context['message_labels'] = array();
  686. $context['message_replied'] = array();
  687. $context['message_unread'] = array();
  688. while ($row = $smcFunc['db_fetch_assoc']($request))
  689. {
  690. if ($context['folder'] == 'sent' || empty($row['bcc']))
  691. $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>';
  692. if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
  693. {
  694. $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
  695. $context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
  696. $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
  697. foreach ($row['labels'] as $v)
  698. {
  699. if (isset($context['labels'][(int) $v]))
  700. $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
  701. }
  702. }
  703. }
  704. $smcFunc['db_free_result']($request);
  705. // Make sure we don't load unnecessary data.
  706. if ($context['display_mode'] == 1)
  707. {
  708. foreach ($posters as $k => $v)
  709. if (!in_array($k, $display_pms))
  710. unset($posters[$k]);
  711. }
  712. // Load any users....
  713. $posters = array_unique($posters);
  714. if (!empty($posters))
  715. loadMemberData($posters);
  716. // If we're on grouped/restricted view get a restricted list of messages.
  717. if ($context['display_mode'] != 0)
  718. {
  719. // Get the order right.
  720. $orderBy = array();
  721. foreach (array_reverse($pms) as $pm)
  722. $orderBy[] = 'pm.id_pm = ' . $pm;
  723. // Seperate query for these bits!
  724. $subjects_request = $smcFunc['db_query']('', '
  725. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.msgtime, IFNULL(mem.real_name, pm.from_name) AS from_name,
  726. IFNULL(mem.id_member, 0) AS not_guest
  727. FROM {db_prefix}personal_messages AS pm
  728. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
  729. WHERE pm.id_pm IN ({array_int:pm_list})
  730. ORDER BY ' . implode(', ', $orderBy) . '
  731. LIMIT ' . count($pms),
  732. array(
  733. 'pm_list' => $pms,
  734. )
  735. );
  736. }
  737. // Execute the query!
  738. $messages_request = $smcFunc['db_query']('', '
  739. SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
  740. FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
  741. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($context['sort_by'] == 'name' ? '
  742. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
  743. WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
  744. GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
  745. ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : $_GET['sort']) . ($descending ? ' DESC' : ' ASC') . '
  746. LIMIT ' . count($display_pms),
  747. array(
  748. 'display_pms' => $display_pms,
  749. 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
  750. )
  751. );
  752. }
  753. else
  754. $messages_request = false;
  755. $context['can_send_pm'] = allowedTo('pm_send');
  756. $context['can_send_email'] = allowedTo('send_email_to_members');
  757. if (!WIRELESS)
  758. $context['sub_template'] = 'folder';
  759. $context['page_title'] = $txt['pm_inbox'];
  760. // Finally mark the relevant messages as read.
  761. if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages']))
  762. {
  763. // If the display mode is "old sk00l" do them all...
  764. if ($context['display_mode'] == 0)
  765. markMessages(null, $context['current_label_id']);
  766. // Otherwise do just the current one!
  767. elseif (!empty($context['current_pm']))
  768. markMessages($display_pms, $context['current_label_id']);
  769. }
  770. // Build the conversation button array.
  771. if ($context['display_mode'] == 2)
  772. {
  773. $context['conversation_buttons'] = array(
  774. 'reply' => array('text' => 'reply_to_all', 'image' => 'reply.png', 'lang' => true, 'url' => $scripturl . '?action=pm;sa=send;f=' . $context['folder'] . ($context['current_label_id'] != -1 ? ';l=' . $context['current_label_id'] : '') . ';pmsg=' . $context['current_pm'] . ';u=all', 'active' => true),
  775. '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']) . '?\');"'),
  776. );
  777. // Allow mods to add additional buttons here
  778. call_integration_hook('integrate_conversation_buttons');
  779. }
  780. }
  781. /**
  782. * Get a personal message for the theme. (used to save memory.)
  783. *
  784. * @param $type
  785. * @param $reset
  786. */
  787. function prepareMessageContext($type = 'subject', $reset = false)
  788. {
  789. global $txt, $scripturl, $modSettings, $settings, $context, $messages_request, $memberContext, $recipients, $smcFunc;
  790. global $user_info, $subjects_request;
  791. // Count the current message number....
  792. static $counter = null;
  793. if ($counter === null || $reset)
  794. $counter = $context['start'];
  795. static $temp_pm_selected = null;
  796. if ($temp_pm_selected === null)
  797. {
  798. $temp_pm_selected = isset($_SESSION['pm_selected']) ? $_SESSION['pm_selected'] : array();
  799. $_SESSION['pm_selected'] = array();
  800. }
  801. // If we're in non-boring view do something exciting!
  802. if ($context['display_mode'] != 0 && $subjects_request && $type == 'subject')
  803. {
  804. $subject = $smcFunc['db_fetch_assoc']($subjects_request);
  805. if (!$subject)
  806. {
  807. $smcFunc['db_free_result']($subjects_request);
  808. return false;
  809. }
  810. $subject['subject'] = $subject['subject'] == '' ? $txt['no_subject'] : $subject['subject'];
  811. censorText($subject['subject']);
  812. $output = array(
  813. 'id' => $subject['id_pm'],
  814. 'member' => array(
  815. 'id' => $subject['id_member_from'],
  816. 'name' => $subject['from_name'],
  817. 'link' => $subject['not_guest'] ? '<a href="' . $scripturl . '?action=profile;u=' . $subject['id_member_from'] . '">' . $subject['from_name'] . '</a>' : $subject['from_name'],
  818. ),
  819. 'recipients' => &$recipients[$subject['id_pm']],
  820. 'subject' => $subject['subject'],
  821. 'time' => timeformat($subject['msgtime']),
  822. 'timestamp' => forum_time(true, $subject['msgtime']),
  823. 'number_recipients' => count($recipients[$subject['id_pm']]['to']),
  824. 'labels' => &$context['message_labels'][$subject['id_pm']],
  825. 'fully_labeled' => count($context['message_labels'][$subject['id_pm']]) == count($context['labels']),
  826. 'is_replied_to' => &$context['message_replied'][$subject['id_pm']],
  827. 'is_unread' => &$context['message_unread'][$subject['id_pm']],
  828. 'is_selected' => !empty($temp_pm_selected) && in_array($subject['id_pm'], $temp_pm_selected),
  829. );
  830. return $output;
  831. }
  832. // Bail if it's false, ie. no messages.
  833. if ($messages_request == false)
  834. return false;
  835. // Reset the data?
  836. if ($reset == true)
  837. return @$smcFunc['db_data_seek']($messages_request, 0);
  838. // Get the next one... bail if anything goes wrong.
  839. $message = $smcFunc['db_fetch_assoc']($messages_request);
  840. if (!$message)
  841. {
  842. if ($type != 'subject')
  843. $smcFunc['db_free_result']($messages_request);
  844. return false;
  845. }
  846. // Use '(no subject)' if none was specified.
  847. $message['subject'] = $message['subject'] == '' ? $txt['no_subject'] : $message['subject'];
  848. // Load the message's information - if it's not there, load the guest information.
  849. if (!loadMemberContext($message['id_member_from'], true))
  850. {
  851. $memberContext[$message['id_member_from']]['name'] = $message['from_name'];
  852. $memberContext[$message['id_member_from']]['id'] = 0;
  853. // Sometimes the forum sends messages itself (Warnings are an example) - in this case don't label it from a guest.
  854. $memberContext[$message['id_member_from']]['group'] = $message['from_name'] == $context['forum_name'] ? '' : $txt['guest_title'];
  855. $memberContext[$message['id_member_from']]['link'] = $message['from_name'];
  856. $memberContext[$message['id_member_from']]['email'] = '';
  857. $memberContext[$message['id_member_from']]['show_email'] = showEmailAddress(true, 0);
  858. $memberContext[$message['id_member_from']]['is_guest'] = true;
  859. }
  860. else
  861. {
  862. $memberContext[$message['id_member_from']]['can_view_profile'] = allowedTo('profile_view_any') || ($message['id_member_from'] == $user_info['id'] && allowedTo('profile_view_own'));
  863. $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'])));
  864. }
  865. $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']);
  866. // Censor all the important text...
  867. censorText($message['body']);
  868. censorText($message['subject']);
  869. // Run UBBC interpreter on the message.
  870. $message['body'] = parse_bbc($message['body'], true, 'pm' . $message['id_pm']);
  871. // Send the array.
  872. $output = array(
  873. 'alternate' => $counter % 2,
  874. 'id' => $message['id_pm'],
  875. 'member' => &$memberContext[$message['id_member_from']],
  876. 'subject' => $message['subject'],
  877. 'time' => timeformat($message['msgtime']),
  878. 'timestamp' => forum_time(true, $message['msgtime']),
  879. 'counter' => $counter,
  880. 'body' => $message['body'],
  881. 'recipients' => &$recipients[$message['id_pm']],
  882. 'number_recipients' => count($recipients[$message['id_pm']]['to']),
  883. 'labels' => &$context['message_labels'][$message['id_pm']],
  884. 'fully_labeled' => count($context['message_labels'][$message['id_pm']]) == count($context['labels']),
  885. 'is_replied_to' => &$context['message_replied'][$message['id_pm']],
  886. 'is_unread' => &$context['message_unread'][$message['id_pm']],
  887. 'is_selected' => !empty($temp_pm_selected) && in_array($message['id_pm'], $temp_pm_selected),
  888. 'is_message_author' => $message['id_member_from'] == $user_info['id'],
  889. 'can_report' => !empty($modSettings['enableReportPM']),
  890. 'can_see_ip' => allowedTo('moderate_forum') || ($message['id_member'] == $user_info['id'] && !empty($user_info['id'])),
  891. );
  892. $counter++;
  893. return $output;
  894. }
  895. /**
  896. * Allows to search through personal messages.
  897. */
  898. function MessageSearch()
  899. {
  900. global $context, $txt, $scripturl, $modSettings, $smcFunc;
  901. if (isset($_REQUEST['params']))
  902. {
  903. $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
  904. $context['search_params'] = array();
  905. foreach ($temp_params as $i => $data)
  906. {
  907. @list ($k, $v) = explode('|\'|', $data);
  908. $context['search_params'][$k] = $v;
  909. }
  910. }
  911. if (isset($_REQUEST['search']))
  912. $context['search_params']['search'] = un_htmlspecialchars($_REQUEST['search']);
  913. if (isset($context['search_params']['search']))
  914. $context['search_params']['search'] = htmlspecialchars($context['search_params']['search']);
  915. if (isset($context['search_params']['userspec']))
  916. $context['search_params']['userspec'] = htmlspecialchars($context['search_params']['userspec']);
  917. if (!empty($context['search_params']['searchtype']))
  918. $context['search_params']['searchtype'] = 2;
  919. if (!empty($context['search_params']['minage']))
  920. $context['search_params']['minage'] = (int) $context['search_params']['minage'];
  921. if (!empty($context['search_params']['maxage']))
  922. $context['search_params']['maxage'] = (int) $context['search_params']['maxage'];
  923. $context['search_params']['subject_only'] = !empty($context['search_params']['subject_only']);
  924. $context['search_params']['show_complete'] = !empty($context['search_params']['show_complete']);
  925. // Create the array of labels to be searched.
  926. $context['search_labels'] = array();
  927. $searchedLabels = isset($context['search_params']['labels']) && $context['search_params']['labels'] != '' ? explode(',', $context['search_params']['labels']) : array();
  928. foreach ($context['labels'] as $label)
  929. {
  930. $context['search_labels'][] = array(
  931. 'id' => $label['id'],
  932. 'name' => $label['name'],
  933. 'checked' => !empty($searchedLabels) ? in_array($label['id'], $searchedLabels) : true,
  934. );
  935. }
  936. // Are all the labels checked?
  937. $context['check_all'] = empty($searchedLabels) || count($context['search_labels']) == count($searchedLabels);
  938. // Load the error text strings if there were errors in the search.
  939. if (!empty($context['search_errors']))
  940. {
  941. loadLanguage('Errors');
  942. $context['search_errors']['messages'] = array();
  943. foreach ($context['search_errors'] as $search_error => $dummy)
  944. {
  945. if ($search_error == 'messages')
  946. continue;
  947. $context['search_errors']['messages'][] = $txt['error_' . $search_error];
  948. }
  949. }
  950. $context['simple_search'] = isset($context['search_params']['advanced']) ? empty($context['search_params']['advanced']) : !empty($modSettings['simpleSearch']) && !isset($_REQUEST['advanced']);
  951. $context['page_title'] = $txt['pm_search_title'];
  952. $context['sub_template'] = 'search';
  953. $context['linktree'][] = array(
  954. 'url' => $scripturl . '?action=pm;sa=search',
  955. 'name' => $txt['pm_search_bar_title'],
  956. );
  957. }
  958. /**
  959. * Actually do the search of personal messages.
  960. */
  961. function MessageSearch2()
  962. {
  963. global $scripturl, $modSettings, $user_info, $context, $txt;
  964. global $memberContext, $smcFunc;
  965. if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search'])
  966. fatal_lang_error('loadavg_search_disabled', false);
  967. /**
  968. * @todo For the moment force the folder to the inbox.
  969. * @todo Maybe set the inbox based on a cookie or theme setting?
  970. */
  971. $context['folder'] = 'inbox';
  972. // Some useful general permissions.
  973. $context['can_send_pm'] = allowedTo('pm_send');
  974. // Some hardcoded veriables that can be tweaked if required.
  975. $maxMembersToSearch = 500;
  976. // Extract all the search parameters.
  977. $search_params = array();
  978. if (isset($_REQUEST['params']))
  979. {
  980. $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
  981. foreach ($temp_params as $i => $data)
  982. {
  983. @list ($k, $v) = explode('|\'|', $data);
  984. $search_params[$k] = $v;
  985. }
  986. }
  987. $context['start'] = isset($_GET['start']) ? (int) $_GET['start'] : 0;
  988. // Store whether simple search was used (needed if the user wants to do another query).
  989. if (!isset($search_params['advanced']))
  990. $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1;
  991. // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'.
  992. if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2))
  993. $search_params['searchtype'] = 2;
  994. // Minimum age of messages. Default to zero (don't set param in that case).
  995. if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0))
  996. $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage'];
  997. // Maximum age of messages. Default to infinite (9999 days: param not set).
  998. if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] != 9999))
  999. $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage'];
  1000. $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']);
  1001. $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']);
  1002. // Default the user name to a wildcard matching every user (*).
  1003. if (!empty($search_params['user_spec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*'))
  1004. $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec'];
  1005. // This will be full of all kinds of parameters!
  1006. $searchq_parameters = array();
  1007. // If there's no specific user, then don't mention it in the main query.
  1008. if (empty($search_params['userspec']))
  1009. $userQuery = '';
  1010. else
  1011. {
  1012. $userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('&quot;' => '"'));
  1013. $userString = strtr($userString, array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_'));
  1014. preg_match_all('~"([^"]+)"~', $userString, $matches);
  1015. $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString)));
  1016. for ($k = 0, $n = count($possible_users); $k < $n; $k++)
  1017. {
  1018. $possible_users[$k] = trim($possible_users[$k]);
  1019. if (strlen($possible_users[$k]) == 0)
  1020. unset($possible_users[$k]);
  1021. }
  1022. // Who matches those criteria?
  1023. // @todo This doesn't support sent item searching.
  1024. $request = $smcFunc['db_query']('', '
  1025. SELECT id_member
  1026. FROM {db_prefix}members
  1027. WHERE real_name LIKE {raw:real_name_implode}',
  1028. array(
  1029. 'real_name_implode' => '\'' . implode('\' OR real_name LIKE \'', $possible_users) . '\'',
  1030. )
  1031. );
  1032. // Simply do nothing if there're too many members matching the criteria.
  1033. if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch)
  1034. $userQuery = '';
  1035. elseif ($smcFunc['db_num_rows']($request) == 0)
  1036. {
  1037. $userQuery = 'AND pm.id_member_from = 0 AND (pm.from_name LIKE {raw:guest_user_name_implode})';
  1038. $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
  1039. }
  1040. else
  1041. {
  1042. $memberlist = array();
  1043. while ($row = $smcFunc['db_fetch_assoc']($request))
  1044. $memberlist[] = $row['id_member'];
  1045. $userQuery = 'AND (pm.id_member_from IN ({array_int:member_list}) OR (pm.id_member_from = 0 AND (pm.from_name LIKE {raw:guest_user_name_implode})))';
  1046. $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
  1047. $searchq_parameters['member_list'] = $memberlist;
  1048. }
  1049. $smcFunc['db_free_result']($request);
  1050. }
  1051. // Setup the sorting variables...
  1052. // @todo Add more in here!
  1053. $sort_columns = array(
  1054. 'pm.id_pm',
  1055. );
  1056. if (empty($search_params['sort']) && !empty($_REQUEST['sort']))
  1057. list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, '');
  1058. $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'pm.id_pm';
  1059. $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc';
  1060. // Sort out any labels we may be searching by.
  1061. $labelQuery = '';
  1062. if ($context['folder'] == 'inbox' && !empty($search_params['advanced']) && $context['currently_using_labels'])
  1063. {
  1064. // Came here from pagination? Put them back into $_REQUEST for sanitization.
  1065. if (isset($search_params['labels']))
  1066. $_REQUEST['searchlabel'] = explode(',', $search_params['labels']);
  1067. // Assuming we have some labels - make them all integers.
  1068. if (!empty($_REQUEST['searchlabel']) && is_array($_REQUEST['searchlabel']))
  1069. {
  1070. foreach ($_REQUEST['searchlabel'] as $key => $id)
  1071. $_REQUEST['searchlabel'][$key] = (int) $id;
  1072. }
  1073. else
  1074. $_REQUEST['searchlabel'] = array();
  1075. // Now that everything is cleaned up a bit, make the labels a param.
  1076. $search_params['labels'] = implode(',', $_REQUEST['searchlabel']);
  1077. // No labels selected? That must be an error!
  1078. if (empty($_REQUEST['searchlabel']))
  1079. $context['search_errors']['no_labels_selected'] = true;
  1080. // Otherwise prepare the query!
  1081. elseif (count($_REQUEST['searchlabel']) != count($context['labels']))
  1082. {
  1083. $labelQuery = '
  1084. AND {raw:label_implode}';
  1085. $labelStatements = array();
  1086. foreach ($_REQUEST['searchlabel'] as $label)
  1087. $labelStatements[] = $smcFunc['db_quote']('FIND_IN_SET({string:label}, pmr.labels) != 0', array(
  1088. 'label' => $label,
  1089. ));
  1090. $searchq_parameters['label_implode'] = '(' . implode(' OR ', $labelStatements) . ')';
  1091. }
  1092. }
  1093. // What are we actually searching for?
  1094. $search_params['search'] = !empty($search_params['search']) ? $search_params['search'] : (isset($_REQUEST['search']) ? $_REQUEST['search'] : '');
  1095. // If we ain't got nothing - we should error!
  1096. if (!isset($search_params['search']) || $search_params['search'] == '')
  1097. $context['search_errors']['invalid_search_string'] = true;
  1098. // Extract phrase parts first (e.g. some words "this is a phrase" some more words.)
  1099. preg_match_all('~(?:^|\s)([-]?)"([^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), $search_params['search'], $matches, PREG_PATTERN_O

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