fixing some annoying bugs
[phpbb.git] / phpBB / includes / ucp / ucp_pm.php
blob9ad6766ee86427df6eed15abe06ba06711bf2a9f
1 <?php
2 /**
3 * @package ucp
4 * @version $Id$
5 * @copyright (c) 2005 phpBB Group
6 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 */
10 /**
11 * @ignore
13 if (!defined('IN_PHPBB'))
15 exit;
18 /**
19 * Private Message Class
21 * $_REQUEST['folder'] display folder with the id used
22 * $_REQUEST['folder'] inbox|outbox|sentbox display folder with the associated name
24 * Display Messages (default to inbox) - mode=view
25 * Display single message - mode=view&p=[msg_id] or &p=[msg_id] (short linkage)
27 * if the folder id with (&f=[folder_id]) is used when displaying messages, one query will be saved. If it is not used, phpBB needs to grab
28 * the folder id first in order to display the input boxes and folder names and such things. ;) phpBB always checks this against the database to make
29 * sure the user is able to view the message.
31 * Composing Messages (mode=compose):
32 * To specific user (u=[user_id])
33 * To specific group (g=[group_id])
34 * Quoting a post (action=quotepost&p=[post_id])
35 * Quoting a PM (action=quote&p=[msg_id])
36 * Forwarding a PM (action=forward&p=[msg_id])
38 * @package ucp
40 class ucp_pm
42 var $u_action;
44 function main($id, $mode)
46 global $user, $template, $phpbb_root_path, $auth, $phpEx, $db, $config;
48 if (!$user->data['is_registered'])
50 trigger_error('NO_MESSAGE');
53 // Is PM disabled?
54 if (!$config['allow_privmsg'])
56 trigger_error('PM_DISABLED');
59 $user->add_lang('posting');
60 $template->assign_var('S_PRIVMSGS', true);
62 // Folder directly specified?
63 $folder_specified = request_var('folder', '');
65 if (!in_array($folder_specified, array('inbox', 'outbox', 'sentbox')))
67 $folder_specified = (int) $folder_specified;
69 else
71 $folder_specified = ($folder_specified == 'inbox') ? PRIVMSGS_INBOX : (($folder_specified == 'outbox') ? PRIVMSGS_OUTBOX : PRIVMSGS_SENTBOX);
74 if (!$folder_specified)
76 $mode = (!$mode) ? request_var('mode', 'view') : $mode;
78 else
80 $mode = 'view';
83 include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
85 switch ($mode)
87 // New private messages popup
88 case 'popup':
90 $l_new_message = '';
91 if ($user->data['is_registered'])
93 if ($user->data['user_new_privmsg'])
95 $l_new_message = ($user->data['user_new_privmsg'] == 1 ) ? $user->lang['YOU_NEW_PM'] : $user->lang['YOU_NEW_PMS'];
97 else
99 $l_new_message = $user->lang['YOU_NO_NEW_PM'];
103 $template->assign_vars(array(
104 'MESSAGE' => $l_new_message,
105 'S_NOT_LOGGED_IN' => ($user->data['user_id'] == ANONYMOUS) ? true : false,
106 'CLICK_TO_VIEW' => sprintf($user->lang['CLICK_VIEW_PRIVMSG'], '<a href="' . append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=inbox') . '" onclick="jump_to_inbox(this.href); return false;">', '</a>'),
107 'U_INBOX' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=inbox'),
108 'UA_INBOX' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&folder=inbox', false))
111 $tpl_file = 'ucp_pm_popup';
112 break;
114 // Compose message
115 case 'compose':
116 $action = request_var('action', 'post');
118 get_folder($user->data['user_id']);
120 if (!$auth->acl_get('u_sendpm'))
122 trigger_error('NO_AUTH_SEND_MESSAGE');
125 include($phpbb_root_path . 'includes/ucp/ucp_pm_compose.' . $phpEx);
126 compose_pm($id, $mode, $action);
128 $tpl_file = 'posting_body';
129 break;
131 case 'options':
132 $sql = 'SELECT group_message_limit
133 FROM ' . GROUPS_TABLE . '
134 WHERE group_id = ' . $user->data['group_id'];
135 $result = $db->sql_query($sql, 3600);
136 $message_limit = (int) $db->sql_fetchfield('group_message_limit');
137 $db->sql_freeresult($result);
139 $user->data['message_limit'] = (!$message_limit) ? $config['pm_max_msgs'] : $message_limit;
141 get_folder($user->data['user_id']);
143 include($phpbb_root_path . 'includes/ucp/ucp_pm_options.' . $phpEx);
144 message_options($id, $mode, $global_privmsgs_rules, $global_rule_conditions);
146 $tpl_file = 'ucp_pm_options';
147 break;
149 case 'drafts':
151 get_folder($user->data['user_id']);
152 $this->p_name = 'pm';
154 // Call another module... please do not try this at home... Hoochie Coochie Man
155 include($phpbb_root_path . 'includes/ucp/ucp_main.' . $phpEx);
157 $module = new ucp_main($this);
158 $module->u_action = $this->u_action;
159 $module->main($id, $mode);
161 $this->tpl_name = $module->tpl_name;
162 $this->page_title = 'UCP_PM_DRAFTS';
164 unset($module);
165 return;
167 break;
169 case 'view':
171 $sql = 'SELECT group_message_limit
172 FROM ' . GROUPS_TABLE . '
173 WHERE group_id = ' . $user->data['group_id'];
174 $result = $db->sql_query($sql, 3600);
175 $message_limit = (int) $db->sql_fetchfield('group_message_limit');
176 $db->sql_freeresult($result);
178 $user->data['message_limit'] = (!$message_limit) ? $config['pm_max_msgs'] : $message_limit;
180 if ($folder_specified)
182 $folder_id = $folder_specified;
183 $action = 'view_folder';
185 else
187 $folder_id = request_var('f', PRIVMSGS_NO_BOX);
188 $action = request_var('action', 'view_folder');
191 $msg_id = request_var('p', 0);
192 $view = request_var('view', '');
194 // View message if specified
195 if ($msg_id)
197 $action = 'view_message';
200 if (!$auth->acl_get('u_readpm'))
202 trigger_error('NO_AUTH_READ_MESSAGE');
205 // Do not allow hold messages to be seen
206 if ($folder_id == PRIVMSGS_HOLD_BOX)
208 trigger_error('NO_AUTH_READ_HOLD_MESSAGE');
212 // First Handle Mark actions and moving messages
213 $submit_mark = (isset($_POST['submit_mark'])) ? true : false;
214 $move_pm = (isset($_POST['move_pm'])) ? true : false;
215 $mark_option = request_var('mark_option', '');
216 $dest_folder = request_var('dest_folder', PRIVMSGS_NO_BOX);
218 // Is moving PM triggered through mark options?
219 if (!in_array($mark_option, array('mark_important', 'delete_marked')) && $submit_mark)
221 $move_pm = true;
222 $dest_folder = (int) $mark_option;
223 $submit_mark = false;
226 // Move PM
227 if ($move_pm)
229 $move_msg_ids = (isset($_POST['marked_msg_id'])) ? request_var('marked_msg_id', array(0)) : array();
230 $cur_folder_id = request_var('cur_folder_id', PRIVMSGS_NO_BOX);
232 if (move_pm($user->data['user_id'], $user->data['message_limit'], $move_msg_ids, $dest_folder, $cur_folder_id))
234 // Return to folder view if single message moved
235 if ($action == 'view_message')
237 $msg_id = 0;
238 $folder_id = request_var('cur_folder_id', PRIVMSGS_NO_BOX);
239 $action = 'view_folder';
244 // Message Mark Options
245 if ($submit_mark)
247 handle_mark_actions($user->data['user_id'], $mark_option);
250 // If new messages arrived, place them into the appropriate folder
251 $num_not_moved = $num_removed = 0;
252 $release = request_var('release', 0);
254 if ($user->data['user_new_privmsg'] && $action == 'view_folder')
256 $return = place_pm_into_folder($global_privmsgs_rules, $release);
257 $num_not_moved = $return['not_moved'];
259 // Make sure num_not_moved is valid.
260 if ($user->data['user_new_privmsg'] < 0 || $num_not_moved < 0)
262 $sql = 'UPDATE ' . USERS_TABLE . '
263 SET user_new_privmsg = 0, user_unread_privmsg = 0
264 WHERE user_id = ' . $user->data['user_id'];
265 $db->sql_query($sql);
267 $num_not_moved = $user->data['user_new_privmsg'] = $user->data['user_unread_privmsg'] = 0;
270 // Assign the number of private messages being removed due to rules.
271 $num_removed = $return['deleted'];
274 // If user released the message, we will re-calculate the statistics (again)
275 if ($release)
277 fix_pm_counts();
280 if (!$msg_id && $folder_id == PRIVMSGS_NO_BOX)
282 $folder_id = PRIVMSGS_INBOX;
284 else if ($msg_id && $folder_id == PRIVMSGS_NO_BOX)
286 $sql = 'SELECT folder_id
287 FROM ' . PRIVMSGS_TO_TABLE . "
288 WHERE msg_id = $msg_id
289 AND folder_id <> " . PRIVMSGS_NO_BOX . '
290 AND user_id = ' . $user->data['user_id'];
291 $result = $db->sql_query($sql);
292 $row = $db->sql_fetchrow($result);
293 $db->sql_freeresult($result);
295 if (!$row)
297 trigger_error('NO_MESSAGE');
299 $folder_id = (int) $row['folder_id'];
302 $message_row = array();
303 if ($action == 'view_message' && $msg_id)
305 // Get Message user want to see
306 if ($view == 'next' || $view == 'previous')
308 $sql_condition = ($view == 'next') ? '>' : '<';
309 $sql_ordering = ($view == 'next') ? 'ASC' : 'DESC';
311 $sql = 'SELECT t.msg_id
312 FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . PRIVMSGS_TABLE . " p2
313 WHERE p2.msg_id = $msg_id
314 AND t.folder_id = $folder_id
315 AND t.user_id = " . $user->data['user_id'] . "
316 AND t.msg_id = p.msg_id
317 AND p.message_time $sql_condition p2.message_time
318 ORDER BY p.message_time $sql_ordering";
319 $result = $db->sql_query_limit($sql, 1);
320 $row = $db->sql_fetchrow($result);
321 $db->sql_freeresult($result);
323 if (!$row)
325 $message = ($view == 'next') ? 'NO_NEWER_PM' : 'NO_OLDER_PM';
326 trigger_error($message);
328 else
330 $msg_id = $row['msg_id'];
334 $sql = 'SELECT t.*, p.*, u.*
335 FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . ' u
336 WHERE t.user_id = ' . $user->data['user_id'] . "
337 AND p.author_id = u.user_id
338 AND t.folder_id = $folder_id
339 AND t.msg_id = p.msg_id
340 AND p.msg_id = $msg_id";
341 $result = $db->sql_query($sql);
342 $message_row = $db->sql_fetchrow($result);
343 $db->sql_freeresult($result);
345 if (!$message_row)
347 trigger_error('NO_MESSAGE');
350 // Update unread status
351 update_unread_status($message_row['pm_unread'], $message_row['msg_id'], $user->data['user_id'], $folder_id);
354 $folder = get_folder($user->data['user_id'], $folder_id);
356 $s_folder_options = $s_to_folder_options = '';
357 foreach ($folder as $f_id => $folder_ary)
359 $option = '<option' . ((!in_array($f_id, array(PRIVMSGS_INBOX, PRIVMSGS_OUTBOX, PRIVMSGS_SENTBOX))) ? ' class="sep"' : '') . ' value="' . $f_id . '"' . (($f_id == $folder_id) ? ' selected="selected"' : '') . '>' . $folder_ary['folder_name'] . (($folder_ary['unread_messages']) ? ' [' . $folder_ary['unread_messages'] . '] ' : '') . '</option>';
361 $s_to_folder_options .= ($f_id != PRIVMSGS_OUTBOX && $f_id != PRIVMSGS_SENTBOX) ? $option : '';
362 $s_folder_options .= $option;
364 clean_sentbox($folder[PRIVMSGS_SENTBOX]['num_messages']);
366 // Header for message view - folder and so on
367 $folder_status = get_folder_status($folder_id, $folder);
369 $template->assign_vars(array(
370 'CUR_FOLDER_ID' => $folder_id,
371 'CUR_FOLDER_NAME' => $folder_status['folder_name'],
372 'NUM_NOT_MOVED' => $num_not_moved,
373 'NUM_REMOVED' => $num_removed,
374 'RELEASE_MESSAGE_INFO' => sprintf($user->lang['RELEASE_MESSAGES'], '<a href="' . $this->u_action . '&amp;folder=' . $folder_id . '&amp;release=1">', '</a>'),
375 'NOT_MOVED_MESSAGES' => ($num_not_moved == 1) ? $user->lang['NOT_MOVED_MESSAGE'] : sprintf($user->lang['NOT_MOVED_MESSAGES'], $num_not_moved),
376 'RULE_REMOVED_MESSAGES' => ($num_removed == 1) ? $user->lang['RULE_REMOVED_MESSAGE'] : sprintf($user->lang['RULE_REMOVED_MESSAGES'], $num_removed),
378 'S_FOLDER_OPTIONS' => $s_folder_options,
379 'S_TO_FOLDER_OPTIONS' => $s_to_folder_options,
380 'S_FOLDER_ACTION' => $this->u_action . '&amp;action=view_folder',
381 'S_PM_ACTION' => $this->u_action . '&amp;action=' . $action,
383 'U_INBOX' => $this->u_action . '&amp;folder=inbox',
384 'U_OUTBOX' => $this->u_action . '&amp;folder=outbox',
385 'U_SENTBOX' => $this->u_action . '&amp;folder=sentbox',
386 'U_CREATE_FOLDER' => $this->u_action . '&amp;mode=options',
387 'U_CURRENT_FOLDER' => $this->u_action . '&amp;folder=' . $folder_id,
389 'S_IN_INBOX' => ($folder_id == PRIVMSGS_INBOX) ? true : false,
390 'S_IN_OUTBOX' => ($folder_id == PRIVMSGS_OUTBOX) ? true : false,
391 'S_IN_SENTBOX' => ($folder_id == PRIVMSGS_SENTBOX) ? true : false,
393 'FOLDER_STATUS' => $folder_status['message'],
394 'FOLDER_MAX_MESSAGES' => $folder_status['max'],
395 'FOLDER_CUR_MESSAGES' => $folder_status['cur'],
396 'FOLDER_REMAINING_MESSAGES' => $folder_status['remaining'],
397 'FOLDER_PERCENT' => $folder_status['percent'])
400 if ($action == 'view_folder')
402 include($phpbb_root_path . 'includes/ucp/ucp_pm_viewfolder.' . $phpEx);
403 view_folder($id, $mode, $folder_id, $folder);
405 $tpl_file = 'ucp_pm_viewfolder';
407 else if ($action == 'view_message')
409 $template->assign_vars(array(
410 'S_VIEW_MESSAGE' => true,
411 'MSG_ID' => $msg_id)
414 if (!$msg_id)
416 trigger_error('NO_MESSAGE');
419 include($phpbb_root_path . 'includes/ucp/ucp_pm_viewmessage.' . $phpEx);
420 view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row);
422 $tpl_file = ($view == 'print') ? 'ucp_pm_viewmessage_print' : 'ucp_pm_viewmessage';
425 break;
427 default:
428 trigger_error('NO_ACTION_MODE', E_USER_ERROR);
429 break;
432 $template->assign_vars(array(
433 'L_TITLE' => $user->lang['UCP_PM_' . strtoupper($mode)],
434 'S_UCP_ACTION' => $this->u_action . ((isset($action)) ? "&amp;action=$action" : ''))
437 // Set desired template
438 $this->tpl_name = $tpl_file;
439 $this->page_title = 'UCP_PM_' . strtoupper($mode);