/php/Sources/PersonalMessage.php
PHP | 3621 lines | 2729 code | 447 blank | 445 comment | 621 complexity | f1ab7466f39b46dfab708e048d5b1a44 MD5 | raw file
Possible License(s): BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Simple Machines Forum (SMF)
- *
- * @package SMF
- * @author Simple Machines http://www.simplemachines.org
- * @copyright 2011 Simple Machines
- * @license http://www.simplemachines.org/about/smf/license.php BSD
- *
- * @version 2.0.5
- */
- if (!defined('SMF'))
- die('Hacking attempt...');
- /* This file is mainly meant for viewing personal messages. It also sends,
- deletes, and marks personal messages. For compatibility reasons, they are
- often called "instant messages". The following functions are used:
- void MessageMain()
- // !!! ?action=pm
- void messageIndexBar(string area)
- // !!!
- void MessageFolder()
- // !!! ?action=pm;sa=folder
- void prepareMessageContext(type reset = 'subject', bool reset = false)
- // !!!
- void MessageSearch()
- // !!!
- void MessageSearch2()
- // !!!
- void MessagePost()
- // !!! ?action=pm;sa=post
- void messagePostError(array error_types, array named_recipients, array recipient_ids)
- // !!!
- void MessagePost2()
- // !!! ?action=pm;sa=post2
- void WirelessAddBuddy()
- // !!!
- void MessageActionsApply()
- // !!! ?action=pm;sa=pmactions
- void MessageKillAllQuery()
- // !!! ?action=pm;sa=killall
- void MessageKillAll()
- // !!! ?action=pm;sa=killall2
- void MessagePrune()
- // !!! ?action=pm;sa=prune
- void deleteMessages(array personal_messages, string folder,
- int owner = user)
- // !!!
- void markMessages(array personal_messages = all, int label = all,
- int owner = user)
- - marks the specified personal_messages read.
- - if label is set, only marks messages with that label.
- - if owner is set, marks messages owned by that member id.
- void ManageLabels()
- // !!!
- void MessageSettings()
- // !!!
- void ReportMessage()
- - allows the user to report a personal message to an administrator.
- - in the first instance requires that the ID of the message to report
- is passed through $_GET.
- - allows the user to report to either a particular administrator - or
- the whole admin team.
- - will forward on a copy of the original message without allowing the
- reporter to make changes.
- - uses the report_message sub-template.
- void ManageRules()
- // !!!
- void LoadRules()
- // !!!
- void ApplyRules()
- // !!!
- */
- // This helps organize things...
- function MessageMain()
- {
- global $txt, $scripturl, $sourcedir, $context, $user_info, $user_settings, $smcFunc, $modSettings;
- // No guests!
- is_not_guest();
- // You're not supposed to be here at all, if you can't even read PMs.
- isAllowedTo('pm_read');
- // This file contains the basic functions for sending a PM.
- require_once($sourcedir . '/Subs-Post.php');
- loadLanguage('PersonalMessage');
- if (WIRELESS && WIRELESS_PROTOCOL == 'wap')
- fatal_lang_error('wireless_error_notyet', false);
- elseif (WIRELESS)
- $context['sub_template'] = WIRELESS_PROTOCOL . '_pm';
- else
- loadTemplate('PersonalMessage');
- // Load up the members maximum message capacity.
- if ($user_info['is_admin'])
- $context['message_limit'] = 0;
- elseif (($context['message_limit'] = cache_get_data('msgLimit:' . $user_info['id'], 360)) === null)
- {
- // !!! Why do we do this? It seems like if they have any limit we should use it.
- $request = $smcFunc['db_query']('', '
- SELECT MAX(max_messages) AS top_limit, MIN(max_messages) AS bottom_limit
- FROM {db_prefix}membergroups
- WHERE id_group IN ({array_int:users_groups})',
- array(
- 'users_groups' => $user_info['groups'],
- )
- );
- list ($maxMessage, $minMessage) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- $context['message_limit'] = $minMessage == 0 ? 0 : $maxMessage;
- // Save us doing it again!
- cache_put_data('msgLimit:' . $user_info['id'], $context['message_limit'], 360);
- }
- // Prepare the context for the capacity bar.
- if (!empty($context['message_limit']))
- {
- $bar = ($user_info['messages'] * 100) / $context['message_limit'];
- $context['limit_bar'] = array(
- 'messages' => $user_info['messages'],
- 'allowed' => $context['message_limit'],
- 'percent' => $bar,
- 'bar' => min(100, (int) $bar),
- 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], round($bar, 1)),
- );
- }
- // a previous message was sent successfully? show a small indication.
- if (isset($_GET['done']) && ($_GET['done'] == 'sent'))
- $context['pm_sent'] = true;
- // Now we have the labels, and assuming we have unsorted mail, apply our rules!
- if ($user_settings['new_pm'])
- {
- $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
- foreach ($context['labels'] as $id_label => $label_name)
- $context['labels'][(int) $id_label] = array(
- 'id' => $id_label,
- 'name' => trim($label_name),
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- $context['labels'][-1] = array(
- 'id' => -1,
- 'name' => $txt['pm_msg_label_inbox'],
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- ApplyRules();
- updateMemberData($user_info['id'], array('new_pm' => 0));
- $smcFunc['db_query']('', '
- UPDATE {db_prefix}pm_recipients
- SET is_new = {int:not_new}
- WHERE id_member = {int:current_member}',
- array(
- 'current_member' => $user_info['id'],
- 'not_new' => 0,
- )
- );
- }
- // Load the label data.
- if ($user_settings['new_pm'] || ($context['labels'] = cache_get_data('labelCounts:' . $user_info['id'], 720)) === null)
- {
- $context['labels'] = $user_settings['message_labels'] == '' ? array() : explode(',', $user_settings['message_labels']);
- foreach ($context['labels'] as $id_label => $label_name)
- $context['labels'][(int) $id_label] = array(
- 'id' => $id_label,
- 'name' => trim($label_name),
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- $context['labels'][-1] = array(
- 'id' => -1,
- 'name' => $txt['pm_msg_label_inbox'],
- 'messages' => 0,
- 'unread_messages' => 0,
- );
- // Looks like we need to reseek!
- $result = $smcFunc['db_query']('', '
- SELECT labels, is_read, COUNT(*) AS num
- FROM {db_prefix}pm_recipients
- WHERE id_member = {int:current_member}
- AND deleted = {int:not_deleted}
- GROUP BY labels, is_read',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- $this_labels = explode(',', $row['labels']);
- foreach ($this_labels as $this_label)
- {
- $context['labels'][(int) $this_label]['messages'] += $row['num'];
- if (!($row['is_read'] & 1))
- $context['labels'][(int) $this_label]['unread_messages'] += $row['num'];
- }
- }
- $smcFunc['db_free_result']($result);
- // Store it please!
- cache_put_data('labelCounts:' . $user_info['id'], $context['labels'], 720);
- }
- // This determines if we have more labels than just the standard inbox.
- $context['currently_using_labels'] = count($context['labels']) > 1 ? 1 : 0;
- // Some stuff for the labels...
- $context['current_label_id'] = isset($_REQUEST['l']) && isset($context['labels'][(int) $_REQUEST['l']]) ? (int) $_REQUEST['l'] : -1;
- $context['current_label'] = &$context['labels'][(int) $context['current_label_id']]['name'];
- $context['folder'] = !isset($_REQUEST['f']) || $_REQUEST['f'] != 'sent' ? 'inbox' : 'sent';
- // This is convenient. Do you know how annoying it is to do this every time?!
- $context['current_label_redirect'] = 'action=pm;f=' . $context['folder'] . (isset($_GET['start']) ? ';start=' . $_GET['start'] : '') . (isset($_REQUEST['l']) ? ';l=' . $_REQUEST['l'] : '');
- $context['can_issue_warning'] = in_array('w', $context['admin_features']) && allowedTo('issue_warning') && $modSettings['warning_settings'][0] == 1;
- // Build the linktree for all the actions...
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm',
- 'name' => $txt['personal_messages']
- );
- // Preferences...
- $context['display_mode'] = WIRELESS ? 0 : $user_settings['pm_prefs'] & 3;
- $subActions = array(
- 'addbuddy' => 'WirelessAddBuddy',
- 'manlabels' => 'ManageLabels',
- 'manrules' => 'ManageRules',
- 'pmactions' => 'MessageActionsApply',
- 'prune' => 'MessagePrune',
- 'removeall' => 'MessageKillAllQuery',
- 'removeall2' => 'MessageKillAll',
- 'report' => 'ReportMessage',
- 'search' => 'MessageSearch',
- 'search2' => 'MessageSearch2',
- 'send' => 'MessagePost',
- 'send2' => 'MessagePost2',
- 'settings' => 'MessageSettings',
- );
- if (!isset($_REQUEST['sa']) || !isset($subActions[$_REQUEST['sa']]))
- {
- $_REQUEST['sa'] = '';
- MessageFolder();
- }
- else
- {
- messageIndexBar($_REQUEST['sa']);
- $subActions[$_REQUEST['sa']]();
- }
- }
- // A sidebar to easily access different areas of the section
- function messageIndexBar($area)
- {
- global $txt, $context, $scripturl, $sourcedir, $sc, $modSettings, $settings, $user_info, $options;
- $pm_areas = array(
- 'folders' => array(
- 'title' => $txt['pm_messages'],
- 'areas' => array(
- 'send' => array(
- 'label' => $txt['new_message'],
- 'custom_url' => $scripturl . '?action=pm;sa=send',
- 'permission' => allowedTo('pm_send'),
- ),
- 'inbox' => array(
- 'label' => $txt['inbox'],
- 'custom_url' => $scripturl . '?action=pm',
- ),
- 'sent' => array(
- 'label' => $txt['sent_items'],
- 'custom_url' => $scripturl . '?action=pm;f=sent',
- ),
- ),
- ),
- 'labels' => array(
- 'title' => $txt['pm_labels'],
- 'areas' => array(),
- ),
- 'actions' => array(
- 'title' => $txt['pm_actions'],
- 'areas' => array(
- 'search' => array(
- 'label' => $txt['pm_search_bar_title'],
- 'custom_url' => $scripturl . '?action=pm;sa=search',
- ),
- 'prune' => array(
- 'label' => $txt['pm_prune'],
- 'custom_url' => $scripturl . '?action=pm;sa=prune'
- ),
- ),
- ),
- 'pref' => array(
- 'title' => $txt['pm_preferences'],
- 'areas' => array(
- 'manlabels' => array(
- 'label' => $txt['pm_manage_labels'],
- 'custom_url' => $scripturl . '?action=pm;sa=manlabels',
- ),
- 'manrules' => array(
- 'label' => $txt['pm_manage_rules'],
- 'custom_url' => $scripturl . '?action=pm;sa=manrules',
- ),
- 'settings' => array(
- 'label' => $txt['pm_settings'],
- 'custom_url' => $scripturl . '?action=pm;sa=settings',
- ),
- ),
- ),
- );
- // Handle labels.
- if (empty($context['currently_using_labels']))
- unset($pm_areas['labels']);
- else
- {
- // Note we send labels by id as it will have less problems in the querystring.
- $unread_in_labels = 0;
- foreach ($context['labels'] as $label)
- {
- if ($label['id'] == -1)
- continue;
- // Count the amount of unread items in labels.
- $unread_in_labels += $label['unread_messages'];
- // Add the label to the menu.
- $pm_areas['labels']['areas']['label' . $label['id']] = array(
- 'label' => $label['name'] . (!empty($label['unread_messages']) ? ' (<strong>' . $label['unread_messages'] . '</strong>)' : ''),
- 'custom_url' => $scripturl . '?action=pm;l=' . $label['id'],
- 'unread_messages' => $label['unread_messages'],
- 'messages' => $label['messages'],
- );
- }
- if (!empty($unread_in_labels))
- $pm_areas['labels']['title'] .= ' (' . $unread_in_labels . ')';
- }
- $pm_areas['folders']['areas']['inbox']['unread_messages'] = &$context['labels'][-1]['unread_messages'];
- $pm_areas['folders']['areas']['inbox']['messages'] = &$context['labels'][-1]['messages'];
- if (!empty($context['labels'][-1]['unread_messages']))
- {
- $pm_areas['folders']['areas']['inbox']['label'] .= ' (<strong>' . $context['labels'][-1]['unread_messages'] . '</strong>)';
- $pm_areas['folders']['title'] .= ' (' . $context['labels'][-1]['unread_messages'] . ')';
- }
- // Do we have a limit on the amount of messages we can keep?
- if (!empty($context['message_limit']))
- {
- $bar = round(($user_info['messages'] * 100) / $context['message_limit'], 1);
- $context['limit_bar'] = array(
- 'messages' => $user_info['messages'],
- 'allowed' => $context['message_limit'],
- 'percent' => $bar,
- 'bar' => $bar > 100 ? 100 : (int) $bar,
- 'text' => sprintf($txt['pm_currently_using'], $user_info['messages'], $bar)
- );
- }
- require_once($sourcedir . '/Subs-Menu.php');
- // What page is this, again?
- $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'] : '');
- // Set a few options for the menu.
- $menuOptions = array(
- 'current_area' => $area,
- 'disable_url_session_check' => true,
- 'toggle_url' => $current_page . ';togglebar',
- 'toggle_redirect_url' => $current_page,
- );
- // Actually create the menu!
- $pm_include_data = createMenu($pm_areas, $menuOptions);
- unset($pm_areas);
- // Make a note of the Unique ID for this menu.
- $context['pm_menu_id'] = $context['max_menu_id'];
- $context['pm_menu_name'] = 'menu_data_' . $context['pm_menu_id'];
- // Set the selected item.
- $context['menu_item_selected'] = $pm_include_data['current_area'];
- // obExit will know what to do!
- if (!WIRELESS)
- $context['template_layers'][] = 'pm';
- }
- // A folder, ie. inbox/sent etc.
- function MessageFolder()
- {
- global $txt, $scripturl, $modSettings, $context, $subjects_request;
- global $messages_request, $user_info, $recipients, $options, $smcFunc, $memberContext, $user_settings;
- // Changing view?
- if (isset($_GET['view']))
- {
- $context['display_mode'] = $context['display_mode'] > 1 ? 0 : $context['display_mode'] + 1;
- updateMemberData($user_info['id'], array('pm_prefs' => ($user_settings['pm_prefs'] & 252) | $context['display_mode']));
- }
- // Make sure the starting location is valid.
- if (isset($_GET['start']) && $_GET['start'] != 'new')
- $_GET['start'] = (int) $_GET['start'];
- elseif (!isset($_GET['start']) && !empty($options['view_newest_pm_first']))
- $_GET['start'] = 0;
- else
- $_GET['start'] = 'new';
- // Set up some basic theme stuff.
- $context['from_or_to'] = $context['folder'] != 'sent' ? 'from' : 'to';
- $context['get_pmessage'] = 'prepareMessageContext';
- $context['signature_enabled'] = substr($modSettings['signature_settings'], 0, 1) == 1;
- $labelQuery = $context['folder'] != 'sent' ? '
- AND FIND_IN_SET(' . $context['current_label_id'] . ', pmr.labels) != 0' : '';
- // Set the index bar correct!
- messageIndexBar($context['current_label_id'] == -1 ? $context['folder'] : 'label' . $context['current_label_id']);
- // Sorting the folder.
- $sort_methods = array(
- 'date' => 'pm.id_pm',
- 'name' => 'IFNULL(mem.real_name, \'\')',
- 'subject' => 'pm.subject',
- );
- // They didn't pick one, use the forum default.
- if (!isset($_GET['sort']) || !isset($sort_methods[$_GET['sort']]))
- {
- $context['sort_by'] = 'date';
- $_GET['sort'] = 'pm.id_pm';
- // An overriding setting?
- $descending = !empty($options['view_newest_pm_first']);
- }
- // Otherwise use the defaults: ascending, by date.
- else
- {
- $context['sort_by'] = $_GET['sort'];
- $_GET['sort'] = $sort_methods[$_GET['sort']];
- $descending = isset($_GET['desc']);
- }
- $context['sort_direction'] = $descending ? 'down' : 'up';
- // Why would you want access to your sent items if you're not allowed to send anything?
- if ($context['folder'] == 'sent')
- isAllowedTo('pm_send');
- // Set the text to resemble the current folder.
- $pmbox = $context['folder'] != 'sent' ? $txt['inbox'] : $txt['sent_items'];
- $txt['delete_all'] = str_replace('PMBOX', $pmbox, $txt['delete_all']);
- // Now, build the link tree!
- if ($context['current_label_id'] == -1)
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;f=' . $context['folder'],
- 'name' => $pmbox
- );
- // Build it further for a label.
- if ($context['current_label_id'] != -1)
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;f=' . $context['folder'] . ';l=' . $context['current_label_id'],
- 'name' => $txt['pm_current_label'] . ': ' . $context['current_label']
- );
- // Figure out how many messages there are.
- if ($context['folder'] == 'sent')
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}personal_messages AS pm
- WHERE pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:not_deleted}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- else
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
- WHERE pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}' . $labelQuery,
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- )
- );
- list ($max_messages) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // Only show the button if there are messages to delete.
- $context['show_delete'] = $max_messages > 0;
- // Start on the last page.
- if (!is_numeric($_GET['start']) || $_GET['start'] >= $max_messages)
- $_GET['start'] = ($max_messages - 1) - (($max_messages - 1) % $modSettings['defaultMaxMessages']);
- elseif ($_GET['start'] < 0)
- $_GET['start'] = 0;
- // ... but wait - what if we want to start from a specific message?
- if (isset($_GET['pmid']))
- {
- $pmID = (int) $_GET['pmid'];
- // Make sure you have access to this PM.
- if (!isAccessiblePM($pmID, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
- fatal_lang_error('no_access', false);
- $context['current_pm'] = $pmID;
- // With only one page of PM's we're gonna want page 1.
- if ($max_messages <= $modSettings['defaultMaxMessages'])
- $_GET['start'] = 0;
- // If we pass kstart we assume we're in the right place.
- elseif (!isset($_GET['kstart']))
- {
- if ($context['folder'] == 'sent')
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}personal_messages
- WHERE id_member_from = {int:current_member}
- AND deleted_by_sender = {int:not_deleted}
- AND id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'id_pm' => $pmID,
- )
- );
- else
- $request = $smcFunc['db_query']('', '
- SELECT COUNT(' . ($context['display_mode'] == 2 ? 'DISTINCT pm.id_pm_head' : '*') . ')
- FROM {db_prefix}pm_recipients AS pmr' . ($context['display_mode'] == 2 ? '
- INNER JOIN {db_prefix}personal_messages AS pm ON (pm.id_pm = pmr.id_pm)' : '') . '
- WHERE pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}' . $labelQuery . '
- AND pmr.id_pm ' . ($descending ? '>' : '<') . ' {int:id_pm}',
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'id_pm' => $pmID,
- )
- );
- list ($_GET['start']) = $smcFunc['db_fetch_row']($request);
- $smcFunc['db_free_result']($request);
- // To stop the page index's being abnormal, start the page on the page the message would normally be located on...
- $_GET['start'] = $modSettings['defaultMaxMessages'] * (int) ($_GET['start'] / $modSettings['defaultMaxMessages']);
- }
- }
- // Sanitize and validate pmsg variable if set.
- if (isset($_GET['pmsg']))
- {
- $pmsg = (int) $_GET['pmsg'];
- if (!isAccessiblePM($pmsg, $context['folder'] == 'sent' ? 'outbox' : 'inbox'))
- fatal_lang_error('no_access', false);
- }
- // Set up the page index.
- $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']);
- $context['start'] = $_GET['start'];
- // Determine the navigation context (especially useful for the wireless template).
- $context['links'] = array(
- 'first' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=0' : '',
- 'prev' => $_GET['start'] >= $modSettings['defaultMaxMessages'] ? $scripturl . '?action=pm;start=' . ($_GET['start'] - $modSettings['defaultMaxMessages']) : '',
- 'next' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . ($_GET['start'] + $modSettings['defaultMaxMessages']) : '',
- 'last' => $_GET['start'] + $modSettings['defaultMaxMessages'] < $max_messages ? $scripturl . '?action=pm;start=' . (floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) * $modSettings['defaultMaxMessages']) : '',
- 'up' => $scripturl,
- );
- $context['page_info'] = array(
- 'current_page' => $_GET['start'] / $modSettings['defaultMaxMessages'] + 1,
- 'num_pages' => floor(($max_messages - 1) / $modSettings['defaultMaxMessages']) + 1
- );
- // First work out what messages we need to see - if grouped is a little trickier...
- if ($context['display_mode'] == 2)
- {
- // On a non-default sort due to PostgreSQL we have to do a harder sort.
- if ($smcFunc['db_title'] == 'PostgreSQL' && $_GET['sort'] != 'pm.id_pm')
- {
- $sub_request = $smcFunc['db_query']('', '
- SELECT MAX({raw:sort}) AS sort_param, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
- WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:not_deleted}' : '1=1') . (empty($pmsg) ? '' : '
- AND pm.id_pm = {int:id_pm}') . '
- GROUP BY pm.id_pm_head
- ORDER BY sort_param' . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'not_deleted' => 0,
- 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- 'id_pm' => isset($pmsg) ? $pmsg : '0',
- 'sort' => $_GET['sort'],
- )
- );
- $sub_pms = array();
- while ($row = $smcFunc['db_fetch_assoc']($sub_request))
- $sub_pms[$row['id_pm_head']] = $row['sort_param'];
- $smcFunc['db_free_result']($sub_request);
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm AS id_pm, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:not_deleted}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})') : '') . '
- WHERE ' . (empty($sub_pms) ? '0=1' : 'pm.id_pm IN ({array_int:pm_list})') . '
- ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'pm_list' => array_keys($sub_pms),
- 'not_deleted' => 0,
- 'sort' => $_GET['sort'],
- 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- )
- );
- }
- else
- {
- $request = $smcFunc['db_query']('pm_conversation_list', '
- SELECT MAX(pm.id_pm) AS id_pm, pm.id_pm_head
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:deleted_by}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
- WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {int:current_member}
- AND pm.deleted_by_sender = {int:deleted_by}' : '1=1') . (empty($pmsg) ? '' : '
- AND pm.id_pm = {int:pmsg}') . '
- GROUP BY pm.id_pm_head
- ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($_GET['pmsg']) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'deleted_by' => 0,
- 'sort' => $_GET['sort'],
- 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
- )
- );
- }
- }
- // This is kinda simple!
- else
- {
- // !!!SLOW This query uses a filesort. (inbox only.)
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.id_pm_head, pm.id_member_from
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '' . ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') : '
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm
- AND pmr.id_member = {int:current_member}
- AND pmr.deleted = {int:is_deleted}
- ' . $labelQuery . ')') . ($context['sort_by'] == 'name' ? ( '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:pm_member})') : '') . '
- WHERE ' . ($context['folder'] == 'sent' ? 'pm.id_member_from = {raw:current_member}
- AND pm.deleted_by_sender = {int:is_deleted}' : '1=1') . (empty($pmsg) ? '' : '
- AND pm.id_pm = {int:pmsg}') . '
- ORDER BY ' . ($_GET['sort'] == 'pm.id_pm' && $context['folder'] != 'sent' ? 'pmr.id_pm' : '{raw:sort}') . ($descending ? ' DESC' : ' ASC') . (empty($pmsg) ? '
- LIMIT ' . $_GET['start'] . ', ' . $modSettings['defaultMaxMessages'] : ''),
- array(
- 'current_member' => $user_info['id'],
- 'is_deleted' => 0,
- 'sort' => $_GET['sort'],
- 'pm_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- 'pmsg' => isset($pmsg) ? (int) $pmsg : 0,
- )
- );
- }
- // Load the id_pms and initialize recipients.
- $pms = array();
- $lastData = array();
- $posters = $context['folder'] == 'sent' ? array($user_info['id']) : array();
- $recipients = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if (!isset($recipients[$row['id_pm']]))
- {
- if (isset($row['id_member_from']))
- $posters[$row['id_pm']] = $row['id_member_from'];
- $pms[$row['id_pm']] = $row['id_pm'];
- $recipients[$row['id_pm']] = array(
- 'to' => array(),
- 'bcc' => array()
- );
- }
- // Keep track of the last message so we know what the head is without another query!
- if ((empty($pmID) && (empty($options['view_newest_pm_first']) || !isset($lastData))) || empty($lastData) || (!empty($pmID) && $pmID == $row['id_pm']))
- $lastData = array(
- 'id' => $row['id_pm'],
- 'head' => $row['id_pm_head'],
- );
- }
- $smcFunc['db_free_result']($request);
- // Make sure that we have been given a correct head pm id!
- if ($context['display_mode'] == 2 && !empty($pmID) && $pmID != $lastData['id'])
- fatal_lang_error('no_access', false);
- if (!empty($pms))
- {
- // Select the correct current message.
- if (empty($pmID))
- $context['current_pm'] = $lastData['id'];
- // This is a list of the pm's that are used for "full" display.
- if ($context['display_mode'] == 0)
- $display_pms = $pms;
- else
- $display_pms = array($context['current_pm']);
- // At this point we know the main id_pm's. But - if we are looking at conversations we need the others!
- if ($context['display_mode'] == 2)
- {
- $request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.id_member_from, pm.deleted_by_sender, pmr.id_member, pmr.deleted
- FROM {db_prefix}personal_messages AS pm
- INNER JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)
- WHERE pm.id_pm_head = {int:id_pm_head}
- AND ((pm.id_member_from = {int:current_member} AND pm.deleted_by_sender = {int:not_deleted})
- OR (pmr.id_member = {int:current_member} AND pmr.deleted = {int:not_deleted}))
- ORDER BY pm.id_pm',
- array(
- 'current_member' => $user_info['id'],
- 'id_pm_head' => $lastData['head'],
- 'not_deleted' => 0,
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- // This is, frankly, a joke. We will put in a workaround for people sending to themselves - yawn!
- if ($context['folder'] == 'sent' && $row['id_member_from'] == $user_info['id'] && $row['deleted_by_sender'] == 1)
- continue;
- elseif ($row['id_member'] == $user_info['id'] & $row['deleted'] == 1)
- continue;
- if (!isset($recipients[$row['id_pm']]))
- $recipients[$row['id_pm']] = array(
- 'to' => array(),
- 'bcc' => array()
- );
- $display_pms[] = $row['id_pm'];
- $posters[$row['id_pm']] = $row['id_member_from'];
- }
- $smcFunc['db_free_result']($request);
- }
- // This is pretty much EVERY pm!
- $all_pms = array_merge($pms, $display_pms);
- $all_pms = array_unique($all_pms);
- // Get recipients (don't include bcc-recipients for your inbox, you're not supposed to know :P).
- $request = $smcFunc['db_query']('', '
- 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
- FROM {db_prefix}pm_recipients AS pmr
- LEFT JOIN {db_prefix}members AS mem_to ON (mem_to.id_member = pmr.id_member)
- WHERE pmr.id_pm IN ({array_int:pm_list})',
- array(
- 'pm_list' => $all_pms,
- )
- );
- $context['message_labels'] = array();
- $context['message_replied'] = array();
- $context['message_unread'] = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- if ($context['folder'] == 'sent' || empty($row['bcc']))
- $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>';
- if ($row['id_member_to'] == $user_info['id'] && $context['folder'] != 'sent')
- {
- $context['message_replied'][$row['id_pm']] = $row['is_read'] & 2;
- $context['message_unread'][$row['id_pm']] = $row['is_read'] == 0;
- $row['labels'] = $row['labels'] == '' ? array() : explode(',', $row['labels']);
- foreach ($row['labels'] as $v)
- {
- if (isset($context['labels'][(int) $v]))
- $context['message_labels'][$row['id_pm']][(int) $v] = array('id' => $v, 'name' => $context['labels'][(int) $v]['name']);
- }
- }
- }
- $smcFunc['db_free_result']($request);
- // Make sure we don't load unnecessary data.
- if ($context['display_mode'] == 1)
- {
- foreach ($posters as $k => $v)
- if (!in_array($k, $display_pms))
- unset($posters[$k]);
- }
- // Load any users....
- $posters = array_unique($posters);
- if (!empty($posters))
- loadMemberData($posters);
- // If we're on grouped/restricted view get a restricted list of messages.
- if ($context['display_mode'] != 0)
- {
- // Get the order right.
- $orderBy = array();
- foreach (array_reverse($pms) as $pm)
- $orderBy[] = 'pm.id_pm = ' . $pm;
- // Seperate query for these bits!
- $subjects_request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.msgtime, IFNULL(mem.real_name, pm.from_name) AS from_name,
- IFNULL(mem.id_member, 0) AS not_guest
- FROM {db_prefix}personal_messages AS pm
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = pm.id_member_from)
- WHERE pm.id_pm IN ({array_int:pm_list})
- ORDER BY ' . implode(', ', $orderBy) . '
- LIMIT ' . count($pms),
- array(
- 'pm_list' => $pms,
- )
- );
- }
- // Execute the query!
- $messages_request = $smcFunc['db_query']('', '
- SELECT pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name
- FROM {db_prefix}personal_messages AS pm' . ($context['folder'] == 'sent' ? '
- LEFT JOIN {db_prefix}pm_recipients AS pmr ON (pmr.id_pm = pm.id_pm)' : '') . ($context['sort_by'] == 'name' ? '
- LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = {raw:id_member})' : '') . '
- WHERE pm.id_pm IN ({array_int:display_pms})' . ($context['folder'] == 'sent' ? '
- GROUP BY pm.id_pm, pm.subject, pm.id_member_from, pm.body, pm.msgtime, pm.from_name' : '') . '
- ORDER BY ' . ($context['display_mode'] == 2 ? 'pm.id_pm' : $_GET['sort']) . ($descending ? ' DESC' : ' ASC') . '
- LIMIT ' . count($display_pms),
- array(
- 'display_pms' => $display_pms,
- 'id_member' => $context['folder'] == 'sent' ? 'pmr.id_member' : 'pm.id_member_from',
- )
- );
- }
- else
- $messages_request = false;
- $context['can_send_pm'] = allowedTo('pm_send');
- if (!WIRELESS)
- $context['sub_template'] = 'folder';
- $context['page_title'] = $txt['pm_inbox'];
- // Finally mark the relevant messages as read.
- if ($context['folder'] != 'sent' && !empty($context['labels'][(int) $context['current_label_id']]['unread_messages']))
- {
- // If the display mode is "old sk00l" do them all...
- if ($context['display_mode'] == 0)
- markMessages(null, $context['current_label_id']);
- // Otherwise do just the current one!
- elseif (!empty($context['current_pm']))
- markMessages($display_pms, $context['current_label_id']);
- }
- }
- // Get a personal message for the theme. (used to save memory.)
- function prepareMessageContext($type = 'subject', $reset = false)
- {
- global $txt, $scripturl, $modSettings, $context, $messages_request, $memberContext, $recipients, $smcFunc;
- global $user_info, $subjects_request;
- // Count the current message number....
- static $counter = null;
- if ($counter === null || $reset)
- $counter = $context['start'];
- static $temp_pm_selected = null;
- if ($temp_pm_selected === null)
- {
- $temp_pm_selected = isset($_SESSION['pm_selected']) ? $_SESSION['pm_selected'] : array();
- $_SESSION['pm_selected'] = array();
- }
- // If we're in non-boring view do something exciting!
- if ($context['display_mode'] != 0 && $subjects_request && $type == 'subject')
- {
- $subject = $smcFunc['db_fetch_assoc']($subjects_request);
- if (!$subject)
- {
- $smcFunc['db_free_result']($subjects_request);
- return false;
- }
- $subject['subject'] = $subject['subject'] == '' ? $txt['no_subject'] : $subject['subject'];
- censorText($subject['subject']);
- $output = array(
- 'id' => $subject['id_pm'],
- 'member' => array(
- 'id' => $subject['id_member_from'],
- 'name' => $subject['from_name'],
- 'link' => $subject['not_guest'] ? '<a href="' . $scripturl . '?action=profile;u=' . $subject['id_member_from'] . '">' . $subject['from_name'] . '</a>' : $subject['from_name'],
- ),
- 'recipients' => &$recipients[$subject['id_pm']],
- 'subject' => $subject['subject'],
- 'time' => timeformat($subject['msgtime']),
- 'timestamp' => forum_time(true, $subject['msgtime']),
- 'number_recipients' => count($recipients[$subject['id_pm']]['to']),
- 'labels' => &$context['message_labels'][$subject['id_pm']],
- 'fully_labeled' => count($context['message_labels'][$subject['id_pm']]) == count($context['labels']),
- 'is_replied_to' => &$context['message_replied'][$subject['id_pm']],
- 'is_unread' => &$context['message_unread'][$subject['id_pm']],
- 'is_selected' => !empty($temp_pm_selected) && in_array($subject['id_pm'], $temp_pm_selected),
- );
- return $output;
- }
- // Bail if it's false, ie. no messages.
- if ($messages_request == false)
- return false;
- // Reset the data?
- if ($reset == true)
- return @$smcFunc['db_data_seek']($messages_request, 0);
- // Get the next one... bail if anything goes wrong.
- $message = $smcFunc['db_fetch_assoc']($messages_request);
- if (!$message)
- {
- if ($type != 'subject')
- $smcFunc['db_free_result']($messages_request);
- return false;
- }
- // Use '(no subject)' if none was specified.
- $message['subject'] = $message['subject'] == '' ? $txt['no_subject'] : $message['subject'];
- // Load the message's information - if it's not there, load the guest information.
- if (!loadMemberContext($message['id_member_from'], true))
- {
- $memberContext[$message['id_member_from']]['name'] = $message['from_name'];
- $memberContext[$message['id_member_from']]['id'] = 0;
- // Sometimes the forum sends messages itself (Warnings are an example) - in this case don't label it from a guest.
- $memberContext[$message['id_member_from']]['group'] = $message['from_name'] == $context['forum_name'] ? '' : $txt['guest_title'];
- $memberContext[$message['id_member_from']]['link'] = $message['from_name'];
- $memberContext[$message['id_member_from']]['email'] = '';
- $memberContext[$message['id_member_from']]['show_email'] = showEmailAddress(true, 0);
- $memberContext[$message['id_member_from']]['is_guest'] = true;
- }
- else
- {
- $memberContext[$message['id_member_from']]['can_view_profile'] = allowedTo('profile_view_any') || ($message['id_member_from'] == $user_info['id'] && allowedTo('profile_view_own'));
- $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'])));
- }
- // Censor all the important text...
- censorText($message['body']);
- censorText($message['subject']);
- // Run UBBC interpreter on the message.
- $message['body'] = parse_bbc($message['body'], true, 'pm' . $message['id_pm']);
- // Send the array.
- $output = array(
- 'alternate' => $counter % 2,
- 'id' => $message['id_pm'],
- 'member' => &$memberContext[$message['id_member_from']],
- 'subject' => $message['subject'],
- 'time' => timeformat($message['msgtime']),
- 'timestamp' => forum_time(true, $message['msgtime']),
- 'counter' => $counter,
- 'body' => $message['body'],
- 'recipients' => &$recipients[$message['id_pm']],
- 'number_recipients' => count($recipients[$message['id_pm']]['to']),
- 'labels' => &$context['message_labels'][$message['id_pm']],
- 'fully_labeled' => count($context['message_labels'][$message['id_pm']]) == count($context['labels']),
- 'is_replied_to' => &$context['message_replied'][$message['id_pm']],
- 'is_unread' => &$context['message_unread'][$message['id_pm']],
- 'is_selected' => !empty($temp_pm_selected) && in_array($message['id_pm'], $temp_pm_selected),
- );
- $counter++;
- return $output;
- }
- function MessageSearch()
- {
- global $context, $txt, $scripturl, $modSettings, $smcFunc;
- if (isset($_REQUEST['params']))
- {
- $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
- $context['search_params'] = array();
- foreach ($temp_params as $i => $data)
- {
- @list ($k, $v) = explode('|\'|', $data);
- $context['search_params'][$k] = $v;
- }
- }
- if (isset($_REQUEST['search']))
- $context['search_params']['search'] = un_htmlspecialchars($_REQUEST['search']);
- if (isset($context['search_params']['search']))
- $context['search_params']['search'] = htmlspecialchars($context['search_params']['search']);
- if (isset($context['search_params']['userspec']))
- $context['search_params']['userspec'] = htmlspecialchars($context['search_params']['userspec']);
- if (!empty($context['search_params']['searchtype']))
- $context['search_params']['searchtype'] = 2;
- if (!empty($context['search_params']['minage']))
- $context['search_params']['minage'] = (int) $context['search_params']['minage'];
- if (!empty($context['search_params']['maxage']))
- $context['search_params']['maxage'] = (int) $context['search_params']['maxage'];
- $context['search_params']['subject_only'] = !empty($context['search_params']['subject_only']);
- $context['search_params']['show_complete'] = !empty($context['search_params']['show_complete']);
- // Create the array of labels to be searched.
- $context['search_labels'] = array();
- $searchedLabels = isset($context['search_params']['labels']) && $context['search_params']['labels'] != '' ? explode(',', $context['search_params']['labels']) : array();
- foreach ($context['labels'] as $label)
- {
- $context['search_labels'][] = array(
- 'id' => $label['id'],
- 'name' => $label['name'],
- 'checked' => !empty($searchedLabels) ? in_array($label['id'], $searchedLabels) : true,
- );
- }
- // Are all the labels checked?
- $context['check_all'] = empty($searchedLabels) || count($context['search_labels']) == count($searchedLabels);
- // Load the error text strings if there were errors in the search.
- if (!empty($context['search_errors']))
- {
- loadLanguage('Errors');
- $context['search_errors']['messages'] = array();
- foreach ($context['search_errors'] as $search_error => $dummy)
- {
- if ($search_error == 'messages')
- continue;
- $context['search_errors']['messages'][] = $txt['error_' . $search_error];
- }
- }
- $context['simple_search'] = isset($context['search_params']['advanced']) ? empty($context['search_params']['advanced']) : !empty($modSettings['simpleSearch']) && !isset($_REQUEST['advanced']);
- $context['page_title'] = $txt['pm_search_title'];
- $context['sub_template'] = 'search';
- $context['linktree'][] = array(
- 'url' => $scripturl . '?action=pm;sa=search',
- 'name' => $txt['pm_search_bar_title'],
- );
- }
- function MessageSearch2()
- {
- global $scripturl, $modSettings, $user_info, $context, $txt;
- global $memberContext, $smcFunc;
- if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search'])
- fatal_lang_error('loadavg_search_disabled', false);
- // !!! For the moment force the folder to the inbox.
- $context['folder'] = 'inbox';
- // Some useful general permissions.
- $context['can_send_pm'] = allowedTo('pm_send');
- // Some hardcoded veriables that can be tweaked if required.
- $maxMembersToSearch = 500;
- // Extract all the search parameters.
- $search_params = array();
- if (isset($_REQUEST['params']))
- {
- $temp_params = explode('|"|', base64_decode(strtr($_REQUEST['params'], array(' ' => '+'))));
- foreach ($temp_params as $i => $data)
- {
- @list ($k, $v) = explode('|\'|', $data);
- $search_params[$k] = $v;
- }
- }
- $context['start'] = isset($_GET['start']) ? (int) $_GET['start'] : 0;
- // Store whether simple search was used (needed if the user wants to do another query).
- if (!isset($search_params['advanced']))
- $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1;
- // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'.
- if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2))
- $search_params['searchtype'] = 2;
- // Minimum age of messages. Default to zero (don't set param in that case).
- if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0))
- $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage'];
- // Maximum age of messages. Default to infinite (9999 days: param not set).
- if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] != 9999))
- $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage'];
- $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']);
- $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']);
- // Default the user name to a wildcard matching every user (*).
- if (!empty($search_params['user_spec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*'))
- $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec'];
- // This will be full of all kinds of parameters!
- $searchq_parameters = array();
- // If there's no specific user, then don't mention it in the main query.
- if (empty($search_params['userspec']))
- $userQuery = '';
- else
- {
- $userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('"' => '"'));
- $userString = strtr($userString, array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_'));
- preg_match_all('~"([^"]+)"~', $userString, $matches);
- $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString)));
- for ($k = 0, $n = count($possible_users); $k < $n; $k++)
- {
- $possible_users[$k] = trim($possible_users[$k]);
- if (strlen($possible_users[$k]) == 0)
- unset($possible_users[$k]);
- }
- // Who matches those criteria?
- // !!! This doesn't support sent item searching.
- $request = $smcFunc['db_query']('', '
- SELECT id_member
- FROM {db_prefix}members
- WHERE real_name LIKE {raw:real_name_implode}',
- array(
- 'real_name_implode' => '\'' . implode('\' OR real_name LIKE \'', $possible_users) . '\'',
- )
- );
- // Simply do nothing if there're too many members matching the criteria.
- if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch)
- $userQuery = '';
- elseif ($smcFunc['db_num_rows']($request) == 0)
- {
- $userQuery = 'AND pm.id_member_from = 0 AND (pm.from_name LIKE {raw:guest_user_name_implode})';
- $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
- }
- else
- {
- $memberlist = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $memberlist[] = $row['id_member'];
- $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})))';
- $searchq_parameters['guest_user_name_implode'] = '\'' . implode('\' OR pm.from_name LIKE \'', $possible_users) . '\'';
- $searchq_parameters['member_list'] = $memberlist;
- }
- $smcFunc['db_free_result']($request);
- }
- // Setup the sorting variables...
- // !!! Add more in here!
- $sort_columns = array(
- 'pm.id_pm',
- );
- if (empty($search_params['sort']) && !empty($_REQUEST['sort']))
- list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, '');
- $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'pm.id_pm';
- $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc';
- // Sort out any labels we may be searching by.
- $labelQuery = '';
- if ($context['folder'] == 'inbox' && !empty($search_params['advanced']) && $context['currently_using_labels'])
- {
- // Came here from pagination? Put them back into $_REQUEST for sanitization.
- if (isset($search_params['labels']))
- $_REQUEST['searchlabel'] = explode(',', $search_params['labels']);
- // Assuming we have some labels - make them all integers.
- if (!empty($_REQUEST['searchlabel']) && is_array($_REQUEST['searchlabel']))
- {
- foreach ($_REQUEST['searchlabel'] as $key => $id)
- $_REQUEST['searchlabel'][$key] = (int) $id;
- }
- else
- $_REQUEST['searchlabel'] = array();
- // Now that everything is cleaned up a bit, make the labels a param.
- $search_params['labels'] = implode(',', $_REQUEST['searchlabel']);
- // No labels selected? That must be an error!
- if (empty($_REQUEST['searchlabel']))
- $context['search_errors']['no_labels_selected'] = true;
- // Otherwise prepare the query!
- elseif (count($_REQUEST['searchlabel']) != count($context['labels']))
- {
- $labelQuery = '
- AND {raw:label_implode}';
- $labelStatements = array();
- foreach ($_REQUEST['searchlabel'] as $label)
- $labelStatements[] = $smcFunc['db_quote']('FIND_IN_SET({string:label}, pmr.labels) != 0', array(
- 'label' => $label,
- ));
- $searchq_parameters['label_implode'] = '(' . implode(' OR ', $labelStatements) . ')';
- }
- }
- // What are we actually searching for?
- $search_params['search'] = !empty($search_params['search']) ? $search_params['search'] : (isset($_REQUEST['search']) ? $_REQUEST['search'] : '');
- // If we ain't got nothing - we should error!
- if (!isset($search_params['search']) || $search_params['search'] == '')
- $context['search_errors']['invalid_search_string'] = true;
- // Extract phrase parts first (e.g. some words "this is a phrase" some more words.)
- preg_match_all('~(?:^|\s)([-]?)"([^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), $search_params['search'], $matches, PREG_PATTERN_ORDER);
- $searchArray = $matches[2];
- // Remove the phrase parts and extract the words.
- $tempSearch = explode(' ', preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']));
- // A minus sign in front of a word excludes the word.... so...
- $excludedWords = array();
- // .. first, we check for things like -"some words", but not "-some words".
- foreach ($matches[1] as $index => $word)
- if ($word == '-')
- {
- $word = $smcFunc['strtolower'](trim($searchArray[$index]));
- if (strlen($word) > 0)
- $excludedWords[] = $word;
- unset($searchArray[$index]);
- }
- // Now we look for -test, etc.... normaller.
- foreach ($tempSearch as $index => $word)
- if (strpos(trim($word), '-') === 0)
- {
- $word = substr($smcFunc['strtolower'](trim($word)), 1);
- if (strlen($word) > 0)
- $excludedWords[] = $word;
- unset($tempSearch[$index]);
- }
- $searchArray = array_merge($searchArray, $tempSearch);
- // Trim everything and make sure there are no words that are the same.
- foreach ($searchArray as $index => $value)
- {
- $searchArray[$index] = $smcFunc['strtolower'](trim($value));
- if ($searchArray[$index] == '')
- unset($searchArray[$index]);
- else
- {
- // Sort out entities first.
- $searchArray[$index] = $smcFunc['htmlspecialchars']($searchArray[$index]);
- }…
Large files files are truncated, but you can click here to view the full file