[ Index ]
 

Code source de phpBB 2.0.22

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ -> privmsg.php (source)

   1  <?php
   2  /***************************************************************************
   3   *                               privmsgs.php
   4   *                            -------------------
   5   *   begin                : Saturday, Jun 9, 2001
   6   *   copyright            : (C) 2001 The phpBB Group
   7   *   email                : support@phpbb.com
   8   *
   9   *   $Id: privmsg.php,v 1.96.2.50 2006/12/16 13:11:25 acydburn Exp $
  10   *
  11   *
  12   ***************************************************************************/
  13  
  14  /***************************************************************************
  15   *
  16   *   This program is free software; you can redistribute it and/or modify
  17   *   it under the terms of the GNU General Public License as published by
  18   *   the Free Software Foundation; either version 2 of the License, or
  19   *   (at your option) any later version.
  20   *
  21   ***************************************************************************/
  22  
  23  define('IN_PHPBB', true);
  24  $phpbb_root_path = './';
  25  include ($phpbb_root_path . 'extension.inc');
  26  include($phpbb_root_path . 'common.'.$phpEx);
  27  include($phpbb_root_path . 'includes/bbcode.'.$phpEx);
  28  include($phpbb_root_path . 'includes/functions_post.'.$phpEx);
  29  
  30  //
  31  // Is PM disabled?
  32  //
  33  if ( !empty($board_config['privmsg_disable']) )
  34  {
  35      message_die(GENERAL_MESSAGE, 'PM_disabled');
  36  }
  37  
  38  $html_entities_match = array('#&(?!(\#[0-9]+;))#', '#<#', '#>#', '#"#');
  39  $html_entities_replace = array('&amp;', '&lt;', '&gt;', '&quot;');
  40  
  41  //
  42  // Parameters
  43  //
  44  $submit = ( isset($HTTP_POST_VARS['post']) ) ? TRUE : 0;
  45  $submit_search = ( isset($HTTP_POST_VARS['usersubmit']) ) ? TRUE : 0; 
  46  $submit_msgdays = ( isset($HTTP_POST_VARS['submit_msgdays']) ) ? TRUE : 0;
  47  $cancel = ( isset($HTTP_POST_VARS['cancel']) ) ? TRUE : 0;
  48  $preview = ( isset($HTTP_POST_VARS['preview']) ) ? TRUE : 0;
  49  $confirm = ( isset($HTTP_POST_VARS['confirm']) ) ? TRUE : 0;
  50  $delete = ( isset($HTTP_POST_VARS['delete']) ) ? TRUE : 0;
  51  $delete_all = ( isset($HTTP_POST_VARS['deleteall']) ) ? TRUE : 0;
  52  $save = ( isset($HTTP_POST_VARS['save']) ) ? TRUE : 0;
  53  $sid = (isset($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : 0;
  54  
  55  $refresh = $preview || $submit_search;
  56  
  57  $mark_list = ( !empty($HTTP_POST_VARS['mark']) ) ? $HTTP_POST_VARS['mark'] : 0;
  58  
  59  if ( isset($HTTP_POST_VARS['folder']) || isset($HTTP_GET_VARS['folder']) )
  60  {
  61      $folder = ( isset($HTTP_POST_VARS['folder']) ) ? $HTTP_POST_VARS['folder'] : $HTTP_GET_VARS['folder'];
  62      $folder = htmlspecialchars($folder);
  63  
  64      if ( $folder != 'inbox' && $folder != 'outbox' && $folder != 'sentbox' && $folder != 'savebox' )
  65      {
  66          $folder = 'inbox';
  67      }
  68  }
  69  else
  70  {
  71      $folder = 'inbox';
  72  }
  73  
  74  //
  75  // Start session management
  76  //
  77  $userdata = session_pagestart($user_ip, PAGE_PRIVMSGS);
  78  init_userprefs($userdata);
  79  //
  80  // End session management
  81  //
  82  
  83  //
  84  // Cancel 
  85  //
  86  if ( $cancel )
  87  {
  88      redirect(append_sid("privmsg.$phpEx?folder=$folder", true));
  89  }
  90  
  91  //
  92  // Var definitions
  93  //
  94  if ( !empty($HTTP_POST_VARS['mode']) || !empty($HTTP_GET_VARS['mode']) )
  95  {
  96      $mode = ( !empty($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
  97      $mode = htmlspecialchars($mode);
  98  }
  99  else
 100  {
 101      $mode = '';
 102  }
 103  
 104  $start = ( !empty($HTTP_GET_VARS['start']) ) ? intval($HTTP_GET_VARS['start']) : 0;
 105  $start = ($start < 0) ? 0 : $start;
 106  
 107  if ( isset($HTTP_POST_VARS[POST_POST_URL]) || isset($HTTP_GET_VARS[POST_POST_URL]) )
 108  {
 109      $privmsg_id = ( isset($HTTP_POST_VARS[POST_POST_URL]) ) ? intval($HTTP_POST_VARS[POST_POST_URL]) : intval($HTTP_GET_VARS[POST_POST_URL]);
 110  }
 111  else
 112  {
 113      $privmsg_id = '';
 114  }
 115  
 116  $error = FALSE;
 117  
 118  //
 119  // Define the box image links
 120  //
 121  $inbox_img = ( $folder != 'inbox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=inbox") . '"><img src="' . $images['pm_inbox'] . '" border="0" alt="' . $lang['Inbox'] . '" /></a>' : '<img src="' . $images['pm_inbox'] . '" border="0" alt="' . $lang['Inbox'] . '" />';
 122  $inbox_url = ( $folder != 'inbox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=inbox") . '">' . $lang['Inbox'] . '</a>' : $lang['Inbox'];
 123  
 124  $outbox_img = ( $folder != 'outbox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=outbox") . '"><img src="' . $images['pm_outbox'] . '" border="0" alt="' . $lang['Outbox'] . '" /></a>' : '<img src="' . $images['pm_outbox'] . '" border="0" alt="' . $lang['Outbox'] . '" />';
 125  $outbox_url = ( $folder != 'outbox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=outbox") . '">' . $lang['Outbox'] . '</a>' : $lang['Outbox'];
 126  
 127  $sentbox_img = ( $folder != 'sentbox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=sentbox") . '"><img src="' . $images['pm_sentbox'] . '" border="0" alt="' . $lang['Sentbox'] . '" /></a>' : '<img src="' . $images['pm_sentbox'] . '" border="0" alt="' . $lang['Sentbox'] . '" />';
 128  $sentbox_url = ( $folder != 'sentbox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=sentbox") . '">' . $lang['Sentbox'] . '</a>' : $lang['Sentbox'];
 129  
 130  $savebox_img = ( $folder != 'savebox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=savebox") . '"><img src="' . $images['pm_savebox'] . '" border="0" alt="' . $lang['Savebox'] . '" /></a>' : '<img src="' . $images['pm_savebox'] . '" border="0" alt="' . $lang['Savebox'] . '" />';
 131  $savebox_url = ( $folder != 'savebox' || $mode != '' ) ? '<a href="' . append_sid("privmsg.$phpEx?folder=savebox") . '">' . $lang['Savebox'] . '</a>' : $lang['Savebox'];
 132  
 133  // ----------
 134  // Start main
 135  //
 136  if ( $mode == 'newpm' )
 137  {
 138      $gen_simple_header = TRUE;
 139  
 140      $page_title = $lang['Private_Messaging'];
 141      include($phpbb_root_path . 'includes/page_header.'.$phpEx);
 142  
 143      $template->set_filenames(array(
 144          'body' => 'privmsgs_popup.tpl')
 145      );
 146  
 147      if ( $userdata['session_logged_in'] )
 148      {
 149          if ( $userdata['user_new_privmsg'] )
 150          {
 151              $l_new_message = ( $userdata['user_new_privmsg'] == 1 ) ? $lang['You_new_pm'] : $lang['You_new_pms'];
 152          }
 153          else
 154          {
 155              $l_new_message = $lang['You_no_new_pm'];
 156          }
 157  
 158          $l_new_message .= '<br /><br />' . sprintf($lang['Click_view_privmsg'], '<a href="' . append_sid("privmsg.".$phpEx."?folder=inbox") . '" onclick="jump_to_inbox();return false;" target="_new">', '</a>');
 159      }
 160      else
 161      {
 162          $l_new_message = $lang['Login_check_pm'];
 163      }
 164  
 165      $template->assign_vars(array(
 166          'L_CLOSE_WINDOW' => $lang['Close_window'], 
 167          'L_MESSAGE' => $l_new_message)
 168      );
 169  
 170      $template->pparse('body');
 171  
 172      include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
 173      
 174  }
 175  else if ( $mode == 'read' )
 176  {
 177      if ( !empty($HTTP_GET_VARS[POST_POST_URL]) )
 178      {
 179          $privmsgs_id = intval($HTTP_GET_VARS[POST_POST_URL]);
 180      }
 181      else
 182      {
 183          message_die(GENERAL_ERROR, $lang['No_post_id']);
 184      }
 185  
 186      if ( !$userdata['session_logged_in'] )
 187      {
 188          redirect(append_sid("login.$phpEx?redirect=privmsg.$phpEx&folder=$folder&mode=$mode&" . POST_POST_URL . "=$privmsgs_id", true));
 189      }
 190  
 191      //
 192      // SQL to pull appropriate message, prevents nosey people
 193      // reading other peoples messages ... hopefully!
 194      //
 195      switch( $folder )
 196      {
 197          case 'inbox':
 198              $l_box_name = $lang['Inbox'];
 199              $pm_sql_user = "AND pm.privmsgs_to_userid = " . $userdata['user_id'] . " 
 200                  AND ( pm.privmsgs_type = " . PRIVMSGS_READ_MAIL . " 
 201                      OR pm.privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
 202                      OR pm.privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
 203              break;
 204          case 'outbox':
 205              $l_box_name = $lang['Outbox'];
 206              $pm_sql_user = "AND pm.privmsgs_from_userid =  " . $userdata['user_id'] . " 
 207                  AND ( pm.privmsgs_type = " . PRIVMSGS_NEW_MAIL . "
 208                      OR pm.privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " ) ";
 209              break;
 210          case 'sentbox':
 211              $l_box_name = $lang['Sentbox'];
 212              $pm_sql_user = "AND pm.privmsgs_from_userid =  " . $userdata['user_id'] . " 
 213                  AND pm.privmsgs_type = " . PRIVMSGS_SENT_MAIL;
 214              break;
 215          case 'savebox':
 216              $l_box_name = $lang['Savebox'];
 217              $pm_sql_user = "AND ( ( pm.privmsgs_to_userid = " . $userdata['user_id'] . "
 218                      AND pm.privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " ) 
 219                  OR ( pm.privmsgs_from_userid = " . $userdata['user_id'] . "
 220                      AND pm.privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " ) 
 221                  )";
 222              break;
 223          default:
 224              message_die(GENERAL_ERROR, $lang['No_such_folder']);
 225              break;
 226      }
 227  
 228      //
 229      // Major query obtains the message ...
 230      //
 231      $sql = "SELECT u.username AS username_1, u.user_id AS user_id_1, u2.username AS username_2, u2.user_id AS user_id_2, u.user_sig_bbcode_uid, u.user_posts, u.user_from, u.user_website, u.user_email, u.user_icq, u.user_aim, u.user_yim, u.user_regdate, u.user_msnm, u.user_viewemail, u.user_rank, u.user_sig, u.user_avatar, pm.*, pmt.privmsgs_bbcode_uid, pmt.privmsgs_text
 232          FROM " . PRIVMSGS_TABLE . " pm, " . PRIVMSGS_TEXT_TABLE . " pmt, " . USERS_TABLE . " u, " . USERS_TABLE . " u2 
 233          WHERE pm.privmsgs_id = $privmsgs_id
 234              AND pmt.privmsgs_text_id = pm.privmsgs_id 
 235              $pm_sql_user 
 236              AND u.user_id = pm.privmsgs_from_userid 
 237              AND u2.user_id = pm.privmsgs_to_userid";
 238      if ( !($result = $db->sql_query($sql)) )
 239      {
 240          message_die(GENERAL_ERROR, 'Could not query private message post information', '', __LINE__, __FILE__, $sql);
 241      }
 242  
 243      //
 244      // Did the query return any data?
 245      //
 246      if ( !($privmsg = $db->sql_fetchrow($result)) )
 247      {
 248          redirect(append_sid("privmsg.$phpEx?folder=$folder", true));
 249      }
 250  
 251      $privmsg_id = $privmsg['privmsgs_id'];
 252  
 253      //
 254      // Is this a new message in the inbox? If it is then save
 255      // a copy in the posters sent box
 256      //
 257      if (($privmsg['privmsgs_type'] == PRIVMSGS_NEW_MAIL || $privmsg['privmsgs_type'] == PRIVMSGS_UNREAD_MAIL) && $folder == 'inbox')
 258      {
 259          // Update appropriate counter
 260          switch ($privmsg['privmsgs_type'])
 261          {
 262              case PRIVMSGS_NEW_MAIL:
 263                  $sql = "user_new_privmsg = user_new_privmsg - 1";
 264                  break;
 265              case PRIVMSGS_UNREAD_MAIL:
 266                  $sql = "user_unread_privmsg = user_unread_privmsg - 1";
 267                  break;
 268          }
 269  
 270          $sql = "UPDATE " . USERS_TABLE . " 
 271              SET $sql 
 272              WHERE user_id = " . $userdata['user_id'];
 273          if ( !$db->sql_query($sql) )
 274          {
 275              message_die(GENERAL_ERROR, 'Could not update private message read status for user', '', __LINE__, __FILE__, $sql);
 276          }
 277  
 278          $sql = "UPDATE " . PRIVMSGS_TABLE . "
 279              SET privmsgs_type = " . PRIVMSGS_READ_MAIL . "
 280              WHERE privmsgs_id = " . $privmsg['privmsgs_id'];
 281          if ( !$db->sql_query($sql) )
 282          {
 283              message_die(GENERAL_ERROR, 'Could not update private message read status', '', __LINE__, __FILE__, $sql);
 284          }
 285  
 286          // Check to see if the poster has a 'full' sent box
 287          $sql = "SELECT COUNT(privmsgs_id) AS sent_items, MIN(privmsgs_date) AS oldest_post_time 
 288              FROM " . PRIVMSGS_TABLE . " 
 289              WHERE privmsgs_type = " . PRIVMSGS_SENT_MAIL . " 
 290                  AND privmsgs_from_userid = " . $privmsg['privmsgs_from_userid'];
 291          if ( !($result = $db->sql_query($sql)) )
 292          {
 293              message_die(GENERAL_ERROR, 'Could not obtain sent message info for sendee', '', __LINE__, __FILE__, $sql);
 294          }
 295  
 296          $sql_priority = ( SQL_LAYER == 'mysql' ) ? 'LOW_PRIORITY' : '';
 297  
 298          if ( $sent_info = $db->sql_fetchrow($result) )
 299          {
 300              if ($board_config['max_sentbox_privmsgs'] && $sent_info['sent_items'] >= $board_config['max_sentbox_privmsgs'])
 301              {
 302                  $sql = "SELECT privmsgs_id FROM " . PRIVMSGS_TABLE . " 
 303                      WHERE privmsgs_type = " . PRIVMSGS_SENT_MAIL . " 
 304                          AND privmsgs_date = " . $sent_info['oldest_post_time'] . " 
 305                          AND privmsgs_from_userid = " . $privmsg['privmsgs_from_userid'];
 306                  if ( !$result = $db->sql_query($sql) )
 307                  {
 308                      message_die(GENERAL_ERROR, 'Could not find oldest privmsgs', '', __LINE__, __FILE__, $sql);
 309                  }
 310                  $old_privmsgs_id = $db->sql_fetchrow($result);
 311                  $old_privmsgs_id = $old_privmsgs_id['privmsgs_id'];
 312              
 313                  $sql = "DELETE $sql_priority FROM " . PRIVMSGS_TABLE . " 
 314                      WHERE privmsgs_id = $old_privmsgs_id";
 315                  if ( !$db->sql_query($sql) )
 316                  {
 317                      message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs (sent)', '', __LINE__, __FILE__, $sql);
 318                  }
 319  
 320                  $sql = "DELETE $sql_priority FROM " . PRIVMSGS_TEXT_TABLE . " 
 321                      WHERE privmsgs_text_id = $old_privmsgs_id";
 322                  if ( !$db->sql_query($sql) )
 323                  {
 324                      message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs text (sent)', '', __LINE__, __FILE__, $sql);
 325                  }
 326              }
 327          }
 328  
 329          //
 330          // This makes a copy of the post and stores it as a SENT message from the sendee. Perhaps
 331          // not the most DB friendly way but a lot easier to manage, besides the admin will be able to
 332          // set limits on numbers of storable posts for users ... hopefully!
 333          //
 334          $sql = "INSERT $sql_priority INTO " . PRIVMSGS_TABLE . " (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_ip, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig)
 335              VALUES (" . PRIVMSGS_SENT_MAIL . ", '" . str_replace("\'", "''", addslashes($privmsg['privmsgs_subject'])) . "', " . $privmsg['privmsgs_from_userid'] . ", " . $privmsg['privmsgs_to_userid'] . ", " . $privmsg['privmsgs_date'] . ", '" . $privmsg['privmsgs_ip'] . "', " . $privmsg['privmsgs_enable_html'] . ", " . $privmsg['privmsgs_enable_bbcode'] . ", " . $privmsg['privmsgs_enable_smilies'] . ", " .  $privmsg['privmsgs_attach_sig'] . ")";
 336          if ( !$db->sql_query($sql) )
 337          {
 338              message_die(GENERAL_ERROR, 'Could not insert private message sent info', '', __LINE__, __FILE__, $sql);
 339          }
 340  
 341          $privmsg_sent_id = $db->sql_nextid();
 342  
 343          $sql = "INSERT $sql_priority INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
 344              VALUES ($privmsg_sent_id, '" . $privmsg['privmsgs_bbcode_uid'] . "', '" . str_replace("\'", "''", addslashes($privmsg['privmsgs_text'])) . "')";
 345          if ( !$db->sql_query($sql) )
 346          {
 347              message_die(GENERAL_ERROR, 'Could not insert private message sent text', '', __LINE__, __FILE__, $sql);
 348          }
 349      }
 350  
 351      //
 352      // Pick a folder, any folder, so long as it's one below ...
 353      //
 354      $post_urls = array(
 355          'post' => append_sid("privmsg.$phpEx?mode=post"),
 356          'reply' => append_sid("privmsg.$phpEx?mode=reply&amp;" . POST_POST_URL . "=$privmsg_id"),
 357          'quote' => append_sid("privmsg.$phpEx?mode=quote&amp;" . POST_POST_URL . "=$privmsg_id"),
 358          'edit' => append_sid("privmsg.$phpEx?mode=edit&amp;" . POST_POST_URL . "=$privmsg_id")
 359      );
 360      $post_icons = array(
 361          'post_img' => '<a href="' . $post_urls['post'] . '"><img src="' . $images['pm_postmsg'] . '" alt="' . $lang['Post_new_pm'] . '" border="0" /></a>',
 362          'post' => '<a href="' . $post_urls['post'] . '">' . $lang['Post_new_pm'] . '</a>',
 363          'reply_img' => '<a href="' . $post_urls['reply'] . '"><img src="' . $images['pm_replymsg'] . '" alt="' . $lang['Post_reply_pm'] . '" border="0" /></a>',
 364          'reply' => '<a href="' . $post_urls['reply'] . '">' . $lang['Post_reply_pm'] . '</a>',
 365          'quote_img' => '<a href="' . $post_urls['quote'] . '"><img src="' . $images['pm_quotemsg'] . '" alt="' . $lang['Post_quote_pm'] . '" border="0" /></a>',
 366          'quote' => '<a href="' . $post_urls['quote'] . '">' . $lang['Post_quote_pm'] . '</a>',
 367          'edit_img' => '<a href="' . $post_urls['edit'] . '"><img src="' . $images['pm_editmsg'] . '" alt="' . $lang['Edit_pm'] . '" border="0" /></a>',
 368          'edit' => '<a href="' . $post_urls['edit'] . '">' . $lang['Edit_pm'] . '</a>'
 369      );
 370  
 371      if ( $folder == 'inbox' )
 372      {
 373          $post_img = $post_icons['post_img'];
 374          $reply_img = $post_icons['reply_img'];
 375          $quote_img = $post_icons['quote_img'];
 376          $edit_img = '';
 377          $post = $post_icons['post'];
 378          $reply = $post_icons['reply'];
 379          $quote = $post_icons['quote'];
 380          $edit = '';
 381          $l_box_name = $lang['Inbox'];
 382      }
 383      else if ( $folder == 'outbox' )
 384      {
 385          $post_img = $post_icons['post_img'];
 386          $reply_img = '';
 387          $quote_img = '';
 388          $edit_img = $post_icons['edit_img'];
 389          $post = $post_icons['post'];
 390          $reply = '';
 391          $quote = '';
 392          $edit = $post_icons['edit'];
 393          $l_box_name = $lang['Outbox'];
 394      }
 395      else if ( $folder == 'savebox' )
 396      {
 397          if ( $privmsg['privmsgs_type'] == PRIVMSGS_SAVED_IN_MAIL )
 398          {
 399              $post_img = $post_icons['post_img'];
 400              $reply_img = $post_icons['reply_img'];
 401              $quote_img = $post_icons['quote_img'];
 402              $edit_img = '';
 403              $post = $post_icons['post'];
 404              $reply = $post_icons['reply'];
 405              $quote = $post_icons['quote'];
 406              $edit = '';
 407          }
 408          else
 409          {
 410              $post_img = $post_icons['post_img'];
 411              $reply_img = '';
 412              $quote_img = '';
 413              $edit_img = '';
 414              $post = $post_icons['post'];
 415              $reply = '';
 416              $quote = '';
 417              $edit = '';
 418          }
 419          $l_box_name = $lang['Saved'];
 420      }
 421      else if ( $folder == 'sentbox' )
 422      {
 423          $post_img = $post_icons['post_img'];
 424          $reply_img = '';
 425          $quote_img = '';
 426          $edit_img = '';
 427          $post = $post_icons['post'];
 428          $reply = '';
 429          $quote = '';
 430          $edit = '';
 431          $l_box_name = $lang['Sent'];
 432      }
 433  
 434      $s_hidden_fields = '<input type="hidden" name="mark[]" value="' . $privmsgs_id . '" />';
 435  
 436      $page_title = $lang['Read_pm'];
 437      include($phpbb_root_path . 'includes/page_header.'.$phpEx);
 438  
 439      //
 440      // Load templates
 441      //
 442      $template->set_filenames(array(
 443          'body' => 'privmsgs_read_body.tpl')
 444      );
 445      make_jumpbox('viewforum.'.$phpEx);
 446  
 447      $template->assign_vars(array(
 448          'INBOX_IMG' => $inbox_img, 
 449          'SENTBOX_IMG' => $sentbox_img, 
 450          'OUTBOX_IMG' => $outbox_img, 
 451          'SAVEBOX_IMG' => $savebox_img, 
 452          'INBOX' => $inbox_url, 
 453  
 454          'POST_PM_IMG' => $post_img, 
 455          'REPLY_PM_IMG' => $reply_img, 
 456          'EDIT_PM_IMG' => $edit_img, 
 457          'QUOTE_PM_IMG' => $quote_img, 
 458          'POST_PM' => $post, 
 459          'REPLY_PM' => $reply, 
 460          'EDIT_PM' => $edit, 
 461          'QUOTE_PM' => $quote, 
 462  
 463          'SENTBOX' => $sentbox_url, 
 464          'OUTBOX' => $outbox_url, 
 465          'SAVEBOX' => $savebox_url, 
 466  
 467          'BOX_NAME' => $l_box_name, 
 468  
 469          'L_MESSAGE' => $lang['Message'], 
 470          'L_INBOX' => $lang['Inbox'],
 471          'L_OUTBOX' => $lang['Outbox'],
 472          'L_SENTBOX' => $lang['Sent'],
 473          'L_SAVEBOX' => $lang['Saved'],
 474          'L_FLAG' => $lang['Flag'],
 475          'L_SUBJECT' => $lang['Subject'],
 476          'L_POSTED' => $lang['Posted'], 
 477          'L_DATE' => $lang['Date'],
 478          'L_FROM' => $lang['From'],
 479          'L_TO' => $lang['To'], 
 480          'L_SAVE_MSG' => $lang['Save_message'], 
 481          'L_DELETE_MSG' => $lang['Delete_message'], 
 482  
 483          'S_PRIVMSGS_ACTION' => append_sid("privmsg.$phpEx?folder=$folder"),
 484          'S_HIDDEN_FIELDS' => $s_hidden_fields)
 485      );
 486      
 487      $username_from = $privmsg['username_1'];
 488      $user_id_from = $privmsg['user_id_1'];
 489      $username_to = $privmsg['username_2'];
 490      $user_id_to = $privmsg['user_id_2'];
 491  
 492      $post_date = create_date($board_config['default_dateformat'], $privmsg['privmsgs_date'], $board_config['board_timezone']);
 493  
 494      $temp_url = append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . '=' . $user_id_from);
 495      $profile_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_profile'] . '" alt="' . $lang['Read_profile'] . '" title="' . $lang['Read_profile'] . '" border="0" /></a>';
 496      $profile = '<a href="' . $temp_url . '">' . $lang['Read_profile'] . '</a>';
 497  
 498      $temp_url = append_sid("privmsg.$phpEx?mode=post&amp;" . POST_USERS_URL . "=$user_id_from");
 499      $pm_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_pm'] . '" alt="' . $lang['Send_private_message'] . '" title="' . $lang['Send_private_message'] . '" border="0" /></a>';
 500      $pm = '<a href="' . $temp_url . '">' . $lang['Send_private_message'] . '</a>';
 501  
 502      if ( !empty($privmsg['user_viewemail']) || $userdata['user_level'] == ADMIN )
 503      {
 504          $email_uri = ( $board_config['board_email_form'] ) ? append_sid("profile.$phpEx?mode=email&amp;" . POST_USERS_URL .'=' . $user_id_from) : 'mailto:' . $privmsg['user_email'];
 505  
 506          $email_img = '<a href="' . $email_uri . '"><img src="' . $images['icon_email'] . '" alt="' . $lang['Send_email'] . '" title="' . $lang['Send_email'] . '" border="0" /></a>';
 507          $email = '<a href="' . $email_uri . '">' . $lang['Send_email'] . '</a>';
 508      }
 509      else
 510      {
 511          $email_img = '';
 512          $email = '';
 513      }
 514  
 515      $www_img = ( $privmsg['user_website'] ) ? '<a href="' . $privmsg['user_website'] . '" target="_userwww"><img src="' . $images['icon_www'] . '" alt="' . $lang['Visit_website'] . '" title="' . $lang['Visit_website'] . '" border="0" /></a>' : '';
 516      $www = ( $privmsg['user_website'] ) ? '<a href="' . $privmsg['user_website'] . '" target="_userwww">' . $lang['Visit_website'] . '</a>' : '';
 517  
 518      if ( !empty($privmsg['user_icq']) )
 519      {
 520          $icq_status_img = '<a href="http://wwp.icq.com/' . $privmsg['user_icq'] . '#pager"><img src="http://web.icq.com/whitepages/online?icq=' . $privmsg['user_icq'] . '&img=5" width="18" height="18" border="0" /></a>';
 521          $icq_img = '<a href="http://wwp.icq.com/scripts/search.dll?to=' . $privmsg['user_icq'] . '"><img src="' . $images['icon_icq'] . '" alt="' . $lang['ICQ'] . '" title="' . $lang['ICQ'] . '" border="0" /></a>';
 522          $icq =  '<a href="http://wwp.icq.com/scripts/search.dll?to=' . $privmsg['user_icq'] . '">' . $lang['ICQ'] . '</a>';
 523      }
 524      else
 525      {
 526          $icq_status_img = '';
 527          $icq_img = '';
 528          $icq = '';
 529      }
 530  
 531      $aim_img = ( $privmsg['user_aim'] ) ? '<a href="aim:goim?screenname=' . $privmsg['user_aim'] . '&amp;message=Hello+Are+you+there?"><img src="' . $images['icon_aim'] . '" alt="' . $lang['AIM'] . '" title="' . $lang['AIM'] . '" border="0" /></a>' : '';
 532      $aim = ( $privmsg['user_aim'] ) ? '<a href="aim:goim?screenname=' . $privmsg['user_aim'] . '&amp;message=Hello+Are+you+there?">' . $lang['AIM'] . '</a>' : '';
 533  
 534      $temp_url = append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$user_id_from");
 535      $msn_img = ( $privmsg['user_msnm'] ) ? '<a href="' . $temp_url . '"><img src="' . $images['icon_msnm'] . '" alt="' . $lang['MSNM'] . '" title="' . $lang['MSNM'] . '" border="0" /></a>' : '';
 536      $msn = ( $privmsg['user_msnm'] ) ? '<a href="' . $temp_url . '">' . $lang['MSNM'] . '</a>' : '';
 537  
 538      $yim_img = ( $privmsg['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $privmsg['user_yim'] . '&amp;.src=pg"><img src="' . $images['icon_yim'] . '" alt="' . $lang['YIM'] . '" title="' . $lang['YIM'] . '" border="0" /></a>' : '';
 539      $yim = ( $privmsg['user_yim'] ) ? '<a href="http://edit.yahoo.com/config/send_webmesg?.target=' . $privmsg['user_yim'] . '&amp;.src=pg">' . $lang['YIM'] . '</a>' : '';
 540  
 541      $temp_url = append_sid("search.$phpEx?search_author=" . urlencode($username_from) . "&amp;showresults=posts");
 542      $search_img = '<a href="' . $temp_url . '"><img src="' . $images['icon_search'] . '" alt="' . sprintf($lang['Search_user_posts'], $username_from) . '" title="' . sprintf($lang['Search_user_posts'], $username_from) . '" border="0" /></a>';
 543      $search = '<a href="' . $temp_url . '">' . sprintf($lang['Search_user_posts'], $username_from) . '</a>';
 544  
 545      //
 546      // Processing of post
 547      //
 548      $post_subject = $privmsg['privmsgs_subject'];
 549  
 550      $private_message = $privmsg['privmsgs_text'];
 551      $bbcode_uid = $privmsg['privmsgs_bbcode_uid'];
 552  
 553      if ( $board_config['allow_sig'] )
 554      {
 555          $user_sig = ( $privmsg['privmsgs_from_userid'] == $userdata['user_id'] ) ? $userdata['user_sig'] : $privmsg['user_sig'];
 556      }
 557      else
 558      {
 559          $user_sig = '';
 560      }
 561  
 562      $user_sig_bbcode_uid = ( $privmsg['privmsgs_from_userid'] == $userdata['user_id'] ) ? $userdata['user_sig_bbcode_uid'] : $privmsg['user_sig_bbcode_uid'];
 563  
 564      //
 565      // If the board has HTML off but the post has HTML
 566      // on then we process it, else leave it alone
 567      //
 568      if ( !$board_config['allow_html'] || !$userdata['user_allowhtml'])
 569      {
 570          if ( $user_sig != '')
 571          {
 572              $user_sig = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $user_sig);
 573          }
 574  
 575          if ( $privmsg['privmsgs_enable_html'] )
 576          {
 577              $private_message = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $private_message);
 578          }
 579      }
 580  
 581      if ( $user_sig != '' && $privmsg['privmsgs_attach_sig'] && $user_sig_bbcode_uid != '' )
 582      {
 583          $user_sig = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($user_sig, $user_sig_bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $user_sig);
 584      }
 585  
 586      if ( $bbcode_uid != '' )
 587      {
 588          $private_message = ( $board_config['allow_bbcode'] ) ? bbencode_second_pass($private_message, $bbcode_uid) : preg_replace('/\:[0-9a-z\:]+\]/si', ']', $private_message);
 589      }
 590  
 591      $private_message = make_clickable($private_message);
 592  
 593      if ( $privmsg['privmsgs_attach_sig'] && $user_sig != '' )
 594      {
 595          $private_message .= '<br /><br />_________________<br />' . make_clickable($user_sig);
 596      }
 597  
 598      $orig_word = array();
 599      $replacement_word = array();
 600      obtain_word_list($orig_word, $replacement_word);
 601  
 602      if ( count($orig_word) )
 603      {
 604          $post_subject = preg_replace($orig_word, $replacement_word, $post_subject);
 605          $private_message = preg_replace($orig_word, $replacement_word, $private_message);
 606      }
 607  
 608      if ( $board_config['allow_smilies'] && $privmsg['privmsgs_enable_smilies'] )
 609      {
 610          $private_message = smilies_pass($private_message);
 611      }
 612  
 613      $private_message = str_replace("\n", '<br />', $private_message);
 614  
 615      //
 616      // Dump it to the templating engine
 617      //
 618      $template->assign_vars(array(
 619          'MESSAGE_TO' => $username_to,
 620          'MESSAGE_FROM' => $username_from,
 621          'RANK_IMAGE' => $rank_image,
 622          'POSTER_JOINED' => $poster_joined,
 623          'POSTER_POSTS' => $poster_posts,
 624          'POSTER_FROM' => $poster_from,
 625          'POSTER_AVATAR' => $poster_avatar,
 626          'POST_SUBJECT' => $post_subject,
 627          'POST_DATE' => $post_date, 
 628          'MESSAGE' => $private_message,
 629  
 630          'PROFILE_IMG' => $profile_img, 
 631          'PROFILE' => $profile, 
 632          'SEARCH_IMG' => $search_img,
 633          'SEARCH' => $search,
 634          'EMAIL_IMG' => $email_img,
 635          'EMAIL' => $email,
 636          'WWW_IMG' => $www_img,
 637          'WWW' => $www,
 638          'ICQ_STATUS_IMG' => $icq_status_img,
 639          'ICQ_IMG' => $icq_img, 
 640          'ICQ' => $icq, 
 641          'AIM_IMG' => $aim_img,
 642          'AIM' => $aim,
 643          'MSN_IMG' => $msn_img,
 644          'MSN' => $msn,
 645          'YIM_IMG' => $yim_img,
 646          'YIM' => $yim)
 647      );
 648  
 649      $template->pparse('body');
 650  
 651      include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
 652  
 653  }
 654  else if ( ( $delete && $mark_list ) || $delete_all )
 655  {
 656      if ( !$userdata['session_logged_in'] )
 657      {
 658          redirect(append_sid("login.$phpEx?redirect=privmsg.$phpEx&folder=inbox", true));
 659      }
 660  
 661      if ( isset($mark_list) && !is_array($mark_list) )
 662      {
 663          // Set to empty array instead of '0' if nothing is selected.
 664          $mark_list = array();
 665      }
 666  
 667      if ( !$confirm )
 668      {
 669          $s_hidden_fields = '<input type="hidden" name="mode" value="' . $mode . '" />';
 670          $s_hidden_fields .= ( isset($HTTP_POST_VARS['delete']) ) ? '<input type="hidden" name="delete" value="true" />' : '<input type="hidden" name="deleteall" value="true" />';
 671  
 672          for($i = 0; $i < count($mark_list); $i++)
 673          {
 674              $s_hidden_fields .= '<input type="hidden" name="mark[]" value="' . intval($mark_list[$i]) . '" />';
 675          }
 676  
 677          //
 678          // Output confirmation page
 679          //
 680          include($phpbb_root_path . 'includes/page_header.'.$phpEx);
 681  
 682          $template->set_filenames(array(
 683              'confirm_body' => 'confirm_body.tpl')
 684          );
 685          $template->assign_vars(array(
 686              'MESSAGE_TITLE' => $lang['Information'],
 687              'MESSAGE_TEXT' => ( count($mark_list) == 1 ) ? $lang['Confirm_delete_pm'] : $lang['Confirm_delete_pms'], 
 688  
 689              'L_YES' => $lang['Yes'],
 690              'L_NO' => $lang['No'],
 691  
 692              'S_CONFIRM_ACTION' => append_sid("privmsg.$phpEx?folder=$folder"),
 693              'S_HIDDEN_FIELDS' => $s_hidden_fields)
 694          );
 695  
 696          $template->pparse('confirm_body');
 697  
 698          include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
 699  
 700      }
 701      else if ( $confirm )
 702      {
 703          $delete_sql_id = '';
 704  
 705          if (!$delete_all)
 706          {
 707              for ($i = 0; $i < count($mark_list); $i++)
 708              {
 709                  $delete_sql_id .= (($delete_sql_id != '') ? ', ' : '') . intval($mark_list[$i]);
 710              }
 711              $delete_sql_id = "AND privmsgs_id IN ($delete_sql_id)";
 712          }
 713  
 714          switch($folder)
 715          {
 716              case 'inbox':
 717                  $delete_type = "privmsgs_to_userid = " . $userdata['user_id'] . " AND (
 718                  privmsgs_type = " . PRIVMSGS_READ_MAIL . " OR privmsgs_type = " . PRIVMSGS_NEW_MAIL . " OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
 719                  break;
 720  
 721              case 'outbox':
 722                  $delete_type = "privmsgs_from_userid = " . $userdata['user_id'] . " AND ( privmsgs_type = " . PRIVMSGS_NEW_MAIL . " OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
 723                  break;
 724  
 725              case 'sentbox':
 726                  $delete_type = "privmsgs_from_userid = " . $userdata['user_id'] . " AND privmsgs_type = " . PRIVMSGS_SENT_MAIL;
 727                  break;
 728  
 729              case 'savebox':
 730                  $delete_type = "( ( privmsgs_from_userid = " . $userdata['user_id'] . " 
 731                      AND privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " ) 
 732                  OR ( privmsgs_to_userid = " . $userdata['user_id'] . " 
 733                      AND privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " ) )";
 734                  break;
 735          }
 736  
 737          $sql = "SELECT privmsgs_id
 738              FROM " . PRIVMSGS_TABLE . "
 739              WHERE $delete_type $delete_sql_id";
 740  
 741          if ( !($result = $db->sql_query($sql)) )
 742          {
 743              message_die(GENERAL_ERROR, 'Could not obtain id list to delete messages', '', __LINE__, __FILE__, $sql);
 744          }
 745  
 746          $mark_list = array();
 747          while ( $row = $db->sql_fetchrow($result) )
 748          {
 749              $mark_list[] = $row['privmsgs_id'];
 750          }
 751  
 752          unset($delete_type);
 753  
 754          if ( count($mark_list) )
 755          {
 756              $delete_sql_id = '';
 757              for ($i = 0; $i < sizeof($mark_list); $i++)
 758              {
 759                  $delete_sql_id .= (($delete_sql_id != '') ? ', ' : '') . intval($mark_list[$i]);
 760              }
 761  
 762              if ($folder == 'inbox' || $folder == 'outbox')
 763              {
 764                  switch ($folder)
 765                  {
 766                      case 'inbox':
 767                          $sql = "privmsgs_to_userid = " . $userdata['user_id'];
 768                          break;
 769                      case 'outbox':
 770                          $sql = "privmsgs_from_userid = " . $userdata['user_id'];
 771                          break;
 772                  }
 773  
 774                  // Get information relevant to new or unread mail
 775                  // so we can adjust users counters appropriately
 776                  $sql = "SELECT privmsgs_to_userid, privmsgs_type 
 777                      FROM " . PRIVMSGS_TABLE . " 
 778                      WHERE privmsgs_id IN ($delete_sql_id) 
 779                          AND $sql  
 780                          AND privmsgs_type IN (" . PRIVMSGS_NEW_MAIL . ", " . PRIVMSGS_UNREAD_MAIL . ")";
 781                  if ( !($result = $db->sql_query($sql)) )
 782                  {
 783                      message_die(GENERAL_ERROR, 'Could not obtain user id list for outbox messages', '', __LINE__, __FILE__, $sql);
 784                  }
 785  
 786                  if ( $row = $db->sql_fetchrow($result))
 787                  {
 788                      $update_users = $update_list = array();
 789                  
 790                      do
 791                      {
 792                          switch ($row['privmsgs_type'])
 793                          {
 794                              case PRIVMSGS_NEW_MAIL:
 795                                  $update_users['new'][$row['privmsgs_to_userid']]++;
 796                                  break;
 797  
 798                              case PRIVMSGS_UNREAD_MAIL:
 799                                  $update_users['unread'][$row['privmsgs_to_userid']]++;
 800                                  break;
 801                          }
 802                      }
 803                      while ($row = $db->sql_fetchrow($result));
 804  
 805                      if (sizeof($update_users))
 806                      {
 807                          while (list($type, $users) = each($update_users))
 808                          {
 809                              while (list($user_id, $dec) = each($users))
 810                              {
 811                                  $update_list[$type][$dec][] = $user_id;
 812                              }
 813                          }
 814                          unset($update_users);
 815  
 816                          while (list($type, $dec_ary) = each($update_list))
 817                          {
 818                              switch ($type)
 819                              {
 820                                  case 'new':
 821                                      $type = "user_new_privmsg";
 822                                      break;
 823  
 824                                  case 'unread':
 825                                      $type = "user_unread_privmsg";
 826                                      break;
 827                              }
 828  
 829                              while (list($dec, $user_ary) = each($dec_ary))
 830                              {
 831                                  $user_ids = implode(', ', $user_ary);
 832  
 833                                  $sql = "UPDATE " . USERS_TABLE . " 
 834                                      SET $type = $type - $dec 
 835                                      WHERE user_id IN ($user_ids)";
 836                                  if ( !$db->sql_query($sql) )
 837                                  {
 838                                      message_die(GENERAL_ERROR, 'Could not update user pm counters', '', __LINE__, __FILE__, $sql);
 839                                  }
 840                              }
 841                          }
 842                          unset($update_list);
 843                      }
 844                  }
 845                  $db->sql_freeresult($result);
 846              }
 847  
 848              // Delete the messages
 849              $delete_text_sql = "DELETE FROM " . PRIVMSGS_TEXT_TABLE . "
 850                  WHERE privmsgs_text_id IN ($delete_sql_id)";
 851              $delete_sql = "DELETE FROM " . PRIVMSGS_TABLE . "
 852                  WHERE privmsgs_id IN ($delete_sql_id)
 853                      AND ";
 854  
 855              switch( $folder )
 856              {
 857                  case 'inbox':
 858                      $delete_sql .= "privmsgs_to_userid = " . $userdata['user_id'] . " AND (
 859                          privmsgs_type = " . PRIVMSGS_READ_MAIL . " OR privmsgs_type = " . PRIVMSGS_NEW_MAIL . " OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
 860                      break;
 861  
 862                  case 'outbox':
 863                      $delete_sql .= "privmsgs_from_userid = " . $userdata['user_id'] . " AND ( 
 864                          privmsgs_type = " . PRIVMSGS_NEW_MAIL . " OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
 865                      break;
 866  
 867                  case 'sentbox':
 868                      $delete_sql .= "privmsgs_from_userid = " . $userdata['user_id'] . " AND privmsgs_type = " . PRIVMSGS_SENT_MAIL;
 869                      break;
 870  
 871                  case 'savebox':
 872                      $delete_sql .= "( ( privmsgs_from_userid = " . $userdata['user_id'] . " 
 873                          AND privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " ) 
 874                      OR ( privmsgs_to_userid = " . $userdata['user_id'] . " 
 875                          AND privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " ) )";
 876                      break;
 877              }
 878  
 879              if ( !$db->sql_query($delete_sql, BEGIN_TRANSACTION) )
 880              {
 881                  message_die(GENERAL_ERROR, 'Could not delete private message info', '', __LINE__, __FILE__, $delete_sql);
 882              }
 883  
 884              if ( !$db->sql_query($delete_text_sql, END_TRANSACTION) )
 885              {
 886                  message_die(GENERAL_ERROR, 'Could not delete private message text', '', __LINE__, __FILE__, $delete_text_sql);
 887              }
 888          }
 889      }
 890  }
 891  else if ( $save && $mark_list && $folder != 'savebox' && $folder != 'outbox' )
 892  {
 893      if ( !$userdata['session_logged_in'] )
 894      {
 895          redirect(append_sid("login.$phpEx?redirect=privmsg.$phpEx&folder=inbox", true));
 896      }
 897      
 898      if (sizeof($mark_list))
 899      {
 900          // See if recipient is at their savebox limit
 901          $sql = "SELECT COUNT(privmsgs_id) AS savebox_items, MIN(privmsgs_date) AS oldest_post_time 
 902              FROM " . PRIVMSGS_TABLE . " 
 903              WHERE ( ( privmsgs_to_userid = " . $userdata['user_id'] . " 
 904                      AND privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " )
 905                  OR ( privmsgs_from_userid = " . $userdata['user_id'] . " 
 906                      AND privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . ") )";
 907          if ( !($result = $db->sql_query($sql)) )
 908          {
 909              message_die(GENERAL_ERROR, 'Could not obtain sent message info for sendee', '', __LINE__, __FILE__, $sql);
 910          }
 911  
 912          $sql_priority = ( SQL_LAYER == 'mysql' ) ? 'LOW_PRIORITY' : '';
 913  
 914          if ( $saved_info = $db->sql_fetchrow($result) )
 915          {
 916              if ($board_config['max_savebox_privmsgs'] && $saved_info['savebox_items'] >= $board_config['max_savebox_privmsgs'] )
 917              {
 918                  $sql = "SELECT privmsgs_id FROM " . PRIVMSGS_TABLE . " 
 919                      WHERE ( ( privmsgs_to_userid = " . $userdata['user_id'] . " 
 920                                  AND privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " )
 921                              OR ( privmsgs_from_userid = " . $userdata['user_id'] . " 
 922                                  AND privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . ") ) 
 923                          AND privmsgs_date = " . $saved_info['oldest_post_time'];
 924                  if ( !$result = $db->sql_query($sql) )
 925                  {
 926                      message_die(GENERAL_ERROR, 'Could not find oldest privmsgs (save)', '', __LINE__, __FILE__, $sql);
 927                  }
 928                  $old_privmsgs_id = $db->sql_fetchrow($result);
 929                  $old_privmsgs_id = $old_privmsgs_id['privmsgs_id'];
 930              
 931                  $sql = "DELETE $sql_priority FROM " . PRIVMSGS_TABLE . " 
 932                      WHERE privmsgs_id = $old_privmsgs_id";
 933                  if ( !$db->sql_query($sql) )
 934                  {
 935                      message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs (save)', '', __LINE__, __FILE__, $sql);
 936                  }
 937  
 938                  $sql = "DELETE $sql_priority FROM " . PRIVMSGS_TEXT_TABLE . " 
 939                      WHERE privmsgs_text_id = $old_privmsgs_id";
 940                  if ( !$db->sql_query($sql) )
 941                  {
 942                      message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs text (save)', '', __LINE__, __FILE__, $sql);
 943                  }
 944              }
 945          }
 946      
 947          $saved_sql_id = '';
 948          for ($i = 0; $i < sizeof($mark_list); $i++)
 949          {
 950              $saved_sql_id .= (($saved_sql_id != '') ? ', ' : '') . intval($mark_list[$i]);
 951          }
 952  
 953          // Process request
 954          $saved_sql = "UPDATE " . PRIVMSGS_TABLE;
 955  
 956          // Decrement read/new counters if appropriate
 957          if ($folder == 'inbox' || $folder == 'outbox')
 958          {
 959              switch ($folder)
 960              {
 961                  case 'inbox':
 962                      $sql = "privmsgs_to_userid = " . $userdata['user_id'];
 963                      break;
 964                  case 'outbox':
 965                      $sql = "privmsgs_from_userid = " . $userdata['user_id'];
 966                      break;
 967              }
 968  
 969              // Get information relevant to new or unread mail
 970              // so we can adjust users counters appropriately
 971              $sql = "SELECT privmsgs_to_userid, privmsgs_type 
 972                  FROM " . PRIVMSGS_TABLE . " 
 973                  WHERE privmsgs_id IN ($saved_sql_id) 
 974                      AND $sql  
 975                      AND privmsgs_type IN (" . PRIVMSGS_NEW_MAIL . ", " . PRIVMSGS_UNREAD_MAIL . ")";
 976              if ( !($result = $db->sql_query($sql)) )
 977              {
 978                  message_die(GENERAL_ERROR, 'Could not obtain user id list for outbox messages', '', __LINE__, __FILE__, $sql);
 979              }
 980  
 981              if ( $row = $db->sql_fetchrow($result))
 982              {
 983                  $update_users = $update_list = array();
 984              
 985                  do
 986                  {
 987                      switch ($row['privmsgs_type'])
 988                      {
 989                          case PRIVMSGS_NEW_MAIL:
 990                              $update_users['new'][$row['privmsgs_to_userid']]++;
 991                              break;
 992  
 993                          case PRIVMSGS_UNREAD_MAIL:
 994                              $update_users['unread'][$row['privmsgs_to_userid']]++;
 995                              break;
 996                      }
 997                  }
 998                  while ($row = $db->sql_fetchrow($result));
 999  
1000                  if (sizeof($update_users))
1001                  {
1002                      while (list($type, $users) = each($update_users))
1003                      {
1004                          while (list($user_id, $dec) = each($users))
1005                          {
1006                              $update_list[$type][$dec][] = $user_id;
1007                          }
1008                      }
1009                      unset($update_users);
1010  
1011                      while (list($type, $dec_ary) = each($update_list))
1012                      {
1013                          switch ($type)
1014                          {
1015                              case 'new':
1016                                  $type = "user_new_privmsg";
1017                                  break;
1018  
1019                              case 'unread':
1020                                  $type = "user_unread_privmsg";
1021                                  break;
1022                          }
1023  
1024                          while (list($dec, $user_ary) = each($dec_ary))
1025                          {
1026                              $user_ids = implode(', ', $user_ary);
1027  
1028                              $sql = "UPDATE " . USERS_TABLE . " 
1029                                  SET $type = $type - $dec 
1030                                  WHERE user_id IN ($user_ids)";
1031                              if ( !$db->sql_query($sql) )
1032                              {
1033                                  message_die(GENERAL_ERROR, 'Could not update user pm counters', '', __LINE__, __FILE__, $sql);
1034                              }
1035                          }
1036                      }
1037                      unset($update_list);
1038                  }
1039              }
1040              $db->sql_freeresult($result);
1041          }
1042  
1043          switch ($folder)
1044          {
1045              case 'inbox':
1046                  $saved_sql .= " SET privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " 
1047                      WHERE privmsgs_to_userid = " . $userdata['user_id'] . " 
1048                          AND ( privmsgs_type = " . PRIVMSGS_READ_MAIL . " 
1049                              OR privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
1050                              OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . ")";
1051                  break;
1052  
1053              case 'outbox':
1054                  $saved_sql .= " SET privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " 
1055                      WHERE privmsgs_from_userid = " . $userdata['user_id'] . " 
1056                          AND ( privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
1057                              OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " ) ";
1058                  break;
1059  
1060              case 'sentbox':
1061                  $saved_sql .= " SET privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " 
1062                      WHERE privmsgs_from_userid = " . $userdata['user_id'] . " 
1063                          AND privmsgs_type = " . PRIVMSGS_SENT_MAIL;
1064                  break;
1065          }
1066  
1067          $saved_sql .= " AND privmsgs_id IN ($saved_sql_id)";
1068  
1069          if ( !$db->sql_query($saved_sql) )
1070          {
1071              message_die(GENERAL_ERROR, 'Could not save private messages', '', __LINE__, __FILE__, $saved_sql);
1072          }
1073  
1074          redirect(append_sid("privmsg.$phpEx?folder=savebox", true));
1075      }
1076  }
1077  else if ( $submit || $refresh || $mode != '' )
1078  {
1079      if ( !$userdata['session_logged_in'] )
1080      {
1081          $user_id = ( isset($HTTP_GET_VARS[POST_USERS_URL]) ) ? '&' . POST_USERS_URL . '=' . intval($HTTP_GET_VARS[POST_USERS_URL]) : '';
1082          redirect(append_sid("login.$phpEx?redirect=privmsg.$phpEx&folder=$folder&mode=$mode" . $user_id, true));
1083      }
1084      
1085      //
1086      // Toggles
1087      //
1088      if ( !$board_config['allow_html'] )
1089      {
1090          $html_on = 0;
1091      }
1092      else
1093      {
1094          $html_on = ( $submit || $refresh ) ? ( ( !empty($HTTP_POST_VARS['disable_html']) ) ? 0 : TRUE ) : $userdata['user_allowhtml'];
1095      }
1096  
1097      if ( !$board_config['allow_bbcode'] )
1098      {
1099          $bbcode_on = 0;
1100      }
1101      else
1102      {
1103          $bbcode_on = ( $submit || $refresh ) ? ( ( !empty($HTTP_POST_VARS['disable_bbcode']) ) ? 0 : TRUE ) : $userdata['user_allowbbcode'];
1104      }
1105  
1106      if ( !$board_config['allow_smilies'] )
1107      {
1108          $smilies_on = 0;
1109      }
1110      else
1111      {
1112          $smilies_on = ( $submit || $refresh ) ? ( ( !empty($HTTP_POST_VARS['disable_smilies']) ) ? 0 : TRUE ) : $userdata['user_allowsmile'];
1113      }
1114  
1115      $attach_sig = ( $submit || $refresh ) ? ( ( !empty($HTTP_POST_VARS['attach_sig']) ) ? TRUE : 0 ) : $userdata['user_attachsig'];
1116      $user_sig = ( $userdata['user_sig'] != '' && $board_config['allow_sig'] ) ? $userdata['user_sig'] : "";
1117      
1118      if ( $submit && $mode != 'edit' )
1119      {
1120          //
1121          // Flood control
1122          //
1123          $sql = "SELECT MAX(privmsgs_date) AS last_post_time
1124              FROM " . PRIVMSGS_TABLE . "
1125              WHERE privmsgs_from_userid = " . $userdata['user_id'];
1126          if ( $result = $db->sql_query($sql) )
1127          {
1128              $db_row = $db->sql_fetchrow($result);
1129  
1130              $last_post_time = $db_row['last_post_time'];
1131              $current_time = time();
1132  
1133              if ( ( $current_time - $last_post_time ) < $board_config['flood_interval'])
1134              {
1135                  message_die(GENERAL_MESSAGE, $lang['Flood_Error']);
1136              }
1137          }
1138          //
1139          // End Flood control
1140          //
1141      }
1142  
1143      if ($submit && $mode == 'edit')
1144      {
1145          $sql = 'SELECT privmsgs_from_userid
1146              FROM ' . PRIVMSGS_TABLE . '
1147              WHERE privmsgs_id = ' . (int) $privmsg_id . '
1148                  AND privmsgs_from_userid = ' . $userdata['user_id'];
1149  
1150          if (!($result = $db->sql_query($sql)))
1151          {
1152              message_die(GENERAL_ERROR, "Could not obtain message details", "", __LINE__, __FILE__, $sql);
1153          }
1154  
1155          if (!($row = $db->sql_fetchrow($result)))
1156          {
1157              message_die(GENERAL_MESSAGE, $lang['No_such_post']);
1158          }
1159          $db->sql_freeresult($result);
1160  
1161          unset($row);
1162      }
1163  
1164      if ( $submit )
1165      {
1166          // session id check
1167          if ($sid == '' || $sid != $userdata['session_id'])
1168          {
1169              $error = true;
1170              $error_msg .= ( ( !empty($error_msg) ) ? '<br />' : '' ) . $lang['Session_invalid'];
1171          }
1172  
1173          if ( !empty($HTTP_POST_VARS['username']) )
1174          {
1175              $to_username = phpbb_clean_username($HTTP_POST_VARS['username']);
1176  
1177              $sql = "SELECT user_id, user_notify_pm, user_email, user_lang, user_active 
1178                  FROM " . USERS_TABLE . "
1179                  WHERE username = '" . str_replace("\'", "''", $to_username) . "'
1180                      AND user_id <> " . ANONYMOUS;
1181              if ( !($result = $db->sql_query($sql)) )
1182              {
1183                  $error = TRUE;
1184                  $error_msg = $lang['No_such_user'];
1185              }
1186  
1187              if (!($to_userdata = $db->sql_fetchrow($result)))
1188              {
1189                  $error = TRUE;
1190                  $error_msg = $lang['No_such_user'];
1191              }
1192          }
1193          else
1194          {
1195              $error = TRUE;
1196              $error_msg .= ( ( !empty($error_msg) ) ? '<br />' : '' ) . $lang['No_to_user'];
1197          }
1198  
1199          $privmsg_subject = trim(htmlspecialchars($HTTP_POST_VARS['subject']));
1200          if ( empty($privmsg_subject) )
1201          {
1202              $error = TRUE;
1203              $error_msg .= ( ( !empty($error_msg) ) ? '<br />' : '' ) . $lang['Empty_subject'];
1204          }
1205  
1206          if ( !empty($HTTP_POST_VARS['message']) )
1207          {
1208              if ( !$error )
1209              {
1210                  if ( $bbcode_on )
1211                  {
1212                      $bbcode_uid = make_bbcode_uid();
1213                  }
1214  
1215                  $privmsg_message = prepare_message($HTTP_POST_VARS['message'], $html_on, $bbcode_on, $smilies_on, $bbcode_uid);
1216  
1217              }
1218          }
1219          else
1220          {
1221              $error = TRUE;
1222              $error_msg .= ( ( !empty($error_msg) ) ? '<br />' : '' ) . $lang['Empty_message'];
1223          }
1224      }
1225  
1226      if ( $submit && !$error )
1227      {
1228          //
1229          // Has admin prevented user from sending PM's?
1230          //
1231          if ( !$userdata['user_allow_pm'] )
1232          {
1233              $message = $lang['Cannot_send_privmsg'];
1234              message_die(GENERAL_MESSAGE, $message);
1235          }
1236  
1237          $msg_time = time();
1238  
1239          if ( $mode != 'edit' )
1240          {
1241              //
1242              // See if recipient is at their inbox limit
1243              //
1244              $sql = "SELECT COUNT(privmsgs_id) AS inbox_items, MIN(privmsgs_date) AS oldest_post_time 
1245                  FROM " . PRIVMSGS_TABLE . " 
1246                  WHERE ( privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
1247                          OR privmsgs_type = " . PRIVMSGS_READ_MAIL . "  
1248                          OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " ) 
1249                      AND privmsgs_to_userid = " . $to_userdata['user_id'];
1250              if ( !($result = $db->sql_query($sql)) )
1251              {
1252                  message_die(GENERAL_MESSAGE, $lang['No_such_user']);
1253              }
1254  
1255              $sql_priority = ( SQL_LAYER == 'mysql' ) ? 'LOW_PRIORITY' : '';
1256  
1257              if ( $inbox_info = $db->sql_fetchrow($result) )
1258              {
1259                  if ($board_config['max_inbox_privmsgs'] && $inbox_info['inbox_items'] >= $board_config['max_inbox_privmsgs'])
1260                  {
1261                      $sql = "SELECT privmsgs_id FROM " . PRIVMSGS_TABLE . " 
1262                          WHERE ( privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
1263                                  OR privmsgs_type = " . PRIVMSGS_READ_MAIL . " 
1264                                  OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . "  ) 
1265                              AND privmsgs_date = " . $inbox_info['oldest_post_time'] . " 
1266                              AND privmsgs_to_userid = " . $to_userdata['user_id'];
1267                      if ( !$result = $db->sql_query($sql) )
1268                      {
1269                          message_die(GENERAL_ERROR, 'Could not find oldest privmsgs (inbox)', '', __LINE__, __FILE__, $sql);
1270                      }
1271                      $old_privmsgs_id = $db->sql_fetchrow($result);
1272                      $old_privmsgs_id = $old_privmsgs_id['privmsgs_id'];
1273                  
1274                      $sql = "DELETE $sql_priority FROM " . PRIVMSGS_TABLE . " 
1275                          WHERE privmsgs_id = $old_privmsgs_id";
1276                      if ( !$db->sql_query($sql) )
1277                      {
1278                          message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs (inbox)'.$sql, '', __LINE__, __FILE__, $sql);
1279                      }
1280  
1281                      $sql = "DELETE $sql_priority FROM " . PRIVMSGS_TEXT_TABLE . " 
1282                          WHERE privmsgs_text_id = $old_privmsgs_id";
1283                      if ( !$db->sql_query($sql) )
1284                      {
1285                          message_die(GENERAL_ERROR, 'Could not delete oldest privmsgs text (inbox)', '', __LINE__, __FILE__, $sql);
1286                      }
1287                  }
1288              }
1289  
1290              $sql_info = "INSERT INTO " . PRIVMSGS_TABLE . " (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_ip, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig)
1291                  VALUES (" . PRIVMSGS_NEW_MAIL . ", '" . str_replace("\'", "''", $privmsg_subject) . "', " . $userdata['user_id'] . ", " . $to_userdata['user_id'] . ", $msg_time, '$user_ip', $html_on, $bbcode_on, $smilies_on, $attach_sig)";
1292          }
1293          else
1294          {
1295              $sql_info = "UPDATE " . PRIVMSGS_TABLE . "
1296                  SET privmsgs_type = " . PRIVMSGS_NEW_MAIL . ", privmsgs_subject = '" . str_replace("\'", "''", $privmsg_subject) . "', privmsgs_from_userid = " . $userdata['user_id'] . ", privmsgs_to_userid = " . $to_userdata['user_id'] . ", privmsgs_date = $msg_time, privmsgs_ip = '$user_ip', privmsgs_enable_html = $html_on, privmsgs_enable_bbcode = $bbcode_on, privmsgs_enable_smilies = $smilies_on, privmsgs_attach_sig = $attach_sig 
1297                  WHERE privmsgs_id = $privmsg_id";
1298          }
1299  
1300          if ( !($result = $db->sql_query($sql_info, BEGIN_TRANSACTION)) )
1301          {
1302              message_die(GENERAL_ERROR, "Could not insert/update private message sent info.", "", __LINE__, __FILE__, $sql_info);
1303          }
1304  
1305          if ( $mode != 'edit' )
1306          {
1307              $privmsg_sent_id = $db->sql_nextid();
1308  
1309              $sql = "INSERT INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
1310                  VALUES ($privmsg_sent_id, '" . $bbcode_uid . "', '" . str_replace("\'", "''", $privmsg_message) . "')";
1311          }
1312          else
1313          {
1314              $sql = "UPDATE " . PRIVMSGS_TEXT_TABLE . "
1315                  SET privmsgs_text = '" . str_replace("\'", "''", $privmsg_message) . "', privmsgs_bbcode_uid = '$bbcode_uid' 
1316                  WHERE privmsgs_text_id = $privmsg_id";
1317          }
1318  
1319          if ( !$db->sql_query($sql, END_TRANSACTION) )
1320          {
1321              message_die(GENERAL_ERROR, "Could not insert/update private message sent text.", "", __LINE__, __FILE__, $sql);
1322          }
1323  
1324          if ( $mode != 'edit' )
1325          {
1326              //
1327              // Add to the users new pm counter
1328              //
1329              $sql = "UPDATE " . USERS_TABLE . "
1330                  SET user_new_privmsg = user_new_privmsg + 1, user_last_privmsg = " . time() . "  
1331                  WHERE user_id = " . $to_userdata['user_id']; 
1332              if ( !$status = $db->sql_query($sql) )
1333              {
1334                  message_die(GENERAL_ERROR, 'Could not update private message new/read status for user', '', __LINE__, __FILE__, $sql);
1335              }
1336  
1337              if ( $to_userdata['user_notify_pm'] && !empty($to_userdata['user_email']) && $to_userdata['user_active'] )
1338              {
1339                  $script_name = preg_replace('/^\/?(.*?)\/?$/', "\\1", trim($board_config['script_path']));
1340                  $script_name = ( $script_name != '' ) ? $script_name . '/privmsg.'.$phpEx : 'privmsg.'.$phpEx;
1341                  $server_name = trim($board_config['server_name']);
1342                  $server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
1343                  $server_port = ( $board_config['server_port'] <> 80 ) ? ':' . trim($board_config['server_port']) . '/' : '/';
1344  
1345                  include($phpbb_root_path . 'includes/emailer.'.$phpEx);
1346                  $emailer = new emailer($board_config['smtp_delivery']);
1347                      
1348                  $emailer->from($board_config['board_email']);
1349                  $emailer->replyto($board_config['board_email']);
1350  
1351                  $emailer->use_template('privmsg_notify', $to_userdata['user_lang']);
1352                  $emailer->email_address($to_userdata['user_email']);
1353                  $emailer->set_subject($lang['Notification_subject']);
1354                      
1355                  $emailer->assign_vars(array(
1356                      'USERNAME' => stripslashes($to_username), 
1357                      'SITENAME' => $board_config['sitename'],
1358                      'EMAIL_SIG' => (!empty($board_config['board_email_sig'])) ? str_replace('<br />', "\n", "-- \n" . $board_config['board_email_sig']) : '', 
1359  
1360                      'U_INBOX' => $server_protocol . $server_name . $server_port . $script_name . '?folder=inbox')
1361                  );
1362  
1363                  $emailer->send();
1364                  $emailer->reset();
1365              }
1366          }
1367  
1368          $template->assign_vars(array(
1369              'META' => '<meta http-equiv="refresh" content="3;url=' . append_sid("privmsg.$phpEx?folder=inbox") . '">')
1370          );
1371  
1372          $msg = $lang['Message_sent'] . '<br /><br />' . sprintf($lang['Click_return_inbox'], '<a href="' . append_sid("privmsg.$phpEx?folder=inbox") . '">', '</a> ') . '<br /><br />' . sprintf($lang['Click_return_index'], '<a href="' . append_sid("index.$phpEx") . '">', '</a>');
1373  
1374          message_die(GENERAL_MESSAGE, $msg);
1375      }
1376      else if ( $preview || $refresh || $error )
1377      {
1378  
1379          //
1380          // If we're previewing or refreshing then obtain the data
1381          // passed to the script, process it a little, do some checks
1382          // where neccessary, etc.
1383          //
1384          $to_username = (isset($HTTP_POST_VARS['username']) ) ? trim(htmlspecialchars(stripslashes($HTTP_POST_VARS['username']))) : '';
1385  
1386          $privmsg_subject = ( isset($HTTP_POST_VARS['subject']) ) ? trim(htmlspecialchars(stripslashes($HTTP_POST_VARS['subject']))) : '';
1387          $privmsg_message = ( isset($HTTP_POST_VARS['message']) ) ? trim($HTTP_POST_VARS['message']) : '';
1388          // $privmsg_message = preg_replace('#<textarea>#si', '&lt;textarea&gt;', $privmsg_message);
1389          if ( !$preview )
1390          {
1391              $privmsg_message = stripslashes($privmsg_message);
1392          }
1393  
1394          //
1395          // Do mode specific things
1396          //
1397          if ( $mode == 'post' )
1398          {
1399              $page_title = $lang['Post_new_pm'];
1400  
1401              $user_sig = ( $userdata['user_sig'] != '' && $board_config['allow_sig'] ) ? $userdata['user_sig'] : '';
1402  
1403          }
1404          else if ( $mode == 'reply' )
1405          {
1406              $page_title = $lang['Post_reply_pm'];
1407  
1408              $user_sig = ( $userdata['user_sig'] != '' && $board_config['allow_sig'] ) ? $userdata['user_sig'] : '';
1409  
1410          }
1411          else if ( $mode == 'edit' )
1412          {
1413              $page_title = $lang['Edit_pm'];
1414  
1415              $sql = "SELECT u.user_id, u.user_sig 
1416                  FROM " . PRIVMSGS_TABLE . " pm, " . USERS_TABLE . " u 
1417                  WHERE pm.privmsgs_id = $privmsg_id 
1418                      AND u.user_id = pm.privmsgs_from_userid";
1419              if ( !($result = $db->sql_query($sql)) )
1420              {
1421                  message_die(GENERAL_ERROR, "Could not obtain post and post text", "", __LINE__, __FILE__, $sql);
1422              }
1423  
1424              if ( $postrow = $db->sql_fetchrow($result) )
1425              {
1426                  if ( $userdata['user_id'] != $postrow['user_id'] )
1427                  {
1428                      message_die(GENERAL_MESSAGE, $lang['Edit_own_posts']);
1429                  }
1430  
1431                  $user_sig = ( $postrow['user_sig'] != '' && $board_config['allow_sig'] ) ? $postrow['user_sig'] : '';
1432              }
1433          }
1434      }
1435      else 
1436      {
1437          if ( !$privmsg_id && ( $mode == 'reply' || $mode == 'edit' || $mode == 'quote' ) )
1438          {
1439              message_die(GENERAL_ERROR, $lang['No_post_id']);
1440          }
1441  
1442          if ( !empty($HTTP_GET_VARS[POST_USERS_URL]) )
1443          {
1444              $user_id = intval($HTTP_GET_VARS[POST_USERS_URL]);
1445  
1446              $sql = "SELECT username
1447                  FROM " . USERS_TABLE . "
1448                  WHERE user_id = $user_id
1449                      AND user_id <> " . ANONYMOUS;
1450              if ( !($result = $db->sql_query($sql)) )
1451              {
1452                  $error = TRUE;
1453                  $error_msg = $lang['No_such_user'];
1454              }
1455  
1456              if ( $row = $db->sql_fetchrow($result) )
1457              {
1458                  $to_username = $row['username'];
1459              }
1460          }
1461          else if ( $mode == 'edit' )
1462          {
1463              $sql = "SELECT pm.*, pmt.privmsgs_bbcode_uid, pmt.privmsgs_text, u.username, u.user_id, u.user_sig 
1464                  FROM " . PRIVMSGS_TABLE . " pm, " . PRIVMSGS_TEXT_TABLE . " pmt, " . USERS_TABLE . " u
1465                  WHERE pm.privmsgs_id = $privmsg_id
1466                      AND pmt.privmsgs_text_id = pm.privmsgs_id
1467                      AND pm.privmsgs_from_userid = " . $userdata['user_id'] . "
1468                      AND ( pm.privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
1469                          OR pm.privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " ) 
1470                      AND u.user_id = pm.privmsgs_to_userid";
1471              if ( !($result = $db->sql_query($sql)) )
1472              {
1473                  message_die(GENERAL_ERROR, 'Could not obtain private message for editing', '', __LINE__, __FILE__, $sql);
1474              }
1475  
1476              if ( !($privmsg = $db->sql_fetchrow($result)) )
1477              {
1478                  redirect(append_sid("privmsg.$phpEx?folder=$folder", true));
1479              }
1480  
1481              $privmsg_subject = $privmsg['privmsgs_subject'];
1482              $privmsg_message = $privmsg['privmsgs_text'];
1483              $privmsg_bbcode_uid = $privmsg['privmsgs_bbcode_uid'];
1484              $privmsg_bbcode_enabled = ($privmsg['privmsgs_enable_bbcode'] == 1);
1485  
1486              if ( $privmsg_bbcode_enabled )
1487              {
1488                  $privmsg_message = preg_replace("/\:(([a-z0-9]:)?)$privmsg_bbcode_uid/si", '', $privmsg_message);
1489              }
1490              
1491              $privmsg_message = str_replace('<br />', "\n", $privmsg_message);
1492              // $privmsg_message = preg_replace('#</textarea>#si', '&lt;/textarea&gt;', $privmsg_message);
1493  
1494              $user_sig = ( $board_config['allow_sig'] ) ? (($privmsg['privmsgs_type'] == PRIVMSGS_NEW_MAIL) ? $user_sig : $privmsg['user_sig']) : '';
1495  
1496              $to_username = $privmsg['username'];
1497              $to_userid = $privmsg['user_id'];
1498  
1499          }
1500          else if ( $mode == 'reply' || $mode == 'quote' )
1501          {
1502  
1503              $sql = "SELECT pm.privmsgs_subject, pm.privmsgs_date, pmt.privmsgs_bbcode_uid, pmt.privmsgs_text, u.username, u.user_id
1504                  FROM " . PRIVMSGS_TABLE . " pm, " . PRIVMSGS_TEXT_TABLE . " pmt, " . USERS_TABLE . " u
1505                  WHERE pm.privmsgs_id = $privmsg_id
1506                      AND pmt.privmsgs_text_id = pm.privmsgs_id
1507                      AND pm.privmsgs_to_userid = " . $userdata['user_id'] . "
1508                      AND u.user_id = pm.privmsgs_from_userid";
1509              if ( !($result = $db->sql_query($sql)) )
1510              {
1511                  message_die(GENERAL_ERROR, 'Could not obtain private message for editing', '', __LINE__, __FILE__, $sql);
1512              }
1513  
1514              if ( !($privmsg = $db->sql_fetchrow($result)) )
1515              {
1516                  redirect(append_sid("privmsg.$phpEx?folder=$folder", true));
1517              }
1518  
1519              $orig_word = $replacement_word = array();
1520              obtain_word_list($orig_word, $replacement_word);
1521  
1522              $privmsg_subject = ( ( !preg_match('/^Re:/', $privmsg['privmsgs_subject']) ) ? 'Re: ' : '' ) . $privmsg['privmsgs_subject'];
1523              $privmsg_subject = preg_replace($orig_word, $replacement_word, $privmsg_subject);
1524  
1525              $to_username = $privmsg['username'];
1526              $to_userid = $privmsg['user_id'];
1527  
1528              if ( $mode == 'quote' )
1529              {
1530                  $privmsg_message = $privmsg['privmsgs_text'];
1531                  $privmsg_bbcode_uid = $privmsg['privmsgs_bbcode_uid'];
1532  
1533                  $privmsg_message = preg_replace("/\:(([a-z0-9]:)?)$privmsg_bbcode_uid/si", '', $privmsg_message);
1534                  $privmsg_message = str_replace('<br />', "\n", $privmsg_message);
1535                  // $privmsg_message = preg_replace('#</textarea>#si', '&lt;/textarea&gt;', $privmsg_message);
1536                  $privmsg_message = preg_replace($orig_word, $replacement_word, $privmsg_message);
1537                  
1538                  $msg_date =  create_date($board_config['default_dateformat'], $privmsg['privmsgs_date'], $board_config['board_timezone']); 
1539  
1540                  $privmsg_message = '[quote="' . $to_username . '"]' . $privmsg_message . '[/quote]';
1541  
1542                  $mode = 'reply';
1543              }
1544          }
1545          else
1546          {
1547              $privmsg_subject = $privmsg_message = $to_username = '';
1548          }
1549      }
1550  
1551      //
1552      // Has admin prevented user from sending PM's?
1553      //
1554      if ( !$userdata['user_allow_pm'] && $mode != 'edit' )
1555      {
1556          $message = $lang['Cannot_send_privmsg'];
1557          message_die(GENERAL_MESSAGE, $message);
1558      }
1559  
1560      //
1561      // Start output, first preview, then errors then post form
1562      //
1563      $page_title = $lang['Send_private_message'];
1564      include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1565  
1566      if ( $preview && !$error )
1567      {
1568          $orig_word = array();
1569          $replacement_word = array();
1570          obtain_word_list($orig_word, $replacement_word);
1571  
1572          if ( $bbcode_on )
1573          {
1574              $bbcode_uid = make_bbcode_uid();
1575          }
1576  
1577          $preview_message = stripslashes(prepare_message($privmsg_message, $html_on, $bbcode_on, $smilies_on, $bbcode_uid));
1578          $privmsg_message = stripslashes(preg_replace($html_entities_match, $html_entities_replace, $privmsg_message));
1579  
1580          //
1581          // Finalise processing as per viewtopic
1582          //
1583          if ( !$html_on || !$board_config['allow_html'] || !$userdata['user_allowhtml'] )
1584          {
1585              if ( $user_sig != '' )
1586              {
1587                  $user_sig = preg_replace('#(<)([\/]?.*?)(>)#is', "&lt;\\2&gt;", $user_sig);
1588              }
1589          }
1590  
1591          if ( $attach_sig && $user_sig != '' && $userdata['user_sig_bbcode_uid'] )
1592          {
1593              $user_sig = bbencode_second_pass($user_sig, $userdata['user_sig_bbcode_uid']);
1594          }
1595  
1596          if ( $bbcode_on )
1597          {
1598              $preview_message = bbencode_second_pass($preview_message, $bbcode_uid);
1599          }
1600  
1601          if ( $attach_sig && $user_sig != '' )
1602          {
1603              $preview_message = $preview_message . '<br /><br />_________________<br />' . $user_sig;
1604          }
1605          
1606          if ( count($orig_word) )
1607          {
1608              $preview_subject = preg_replace($orig_word, $replacement_word, $privmsg_subject);
1609              $preview_message = preg_replace($orig_word, $replacement_word, $preview_message);
1610          }
1611          else
1612          {
1613              $preview_subject = $privmsg_subject;
1614          }
1615  
1616          if ( $smilies_on )
1617          {
1618              $preview_message = smilies_pass($preview_message);
1619          }
1620  
1621          $preview_message = make_clickable($preview_message);
1622          $preview_message = str_replace("\n", '<br />', $preview_message);
1623  
1624          $s_hidden_fields = '<input type="hidden" name="folder" value="' . $folder . '" />';
1625          $s_hidden_fields .= '<input type="hidden" name="mode" value="' . $mode . '" />';
1626  
1627          if ( isset($privmsg_id) )
1628          {
1629              $s_hidden_fields .= '<input type="hidden" name="' . POST_POST_URL . '" value="' . $privmsg_id . '" />';
1630          }
1631  
1632          $template->set_filenames(array(
1633              "preview" => 'privmsgs_preview.tpl')
1634          );
1635  
1636          $template->assign_vars(array(
1637              'TOPIC_TITLE' => $preview_subject,
1638              'POST_SUBJECT' => $preview_subject,
1639              'MESSAGE_TO' => $to_username, 
1640              'MESSAGE_FROM' => $userdata['username'], 
1641              'POST_DATE' => create_date($board_config['default_dateformat'], time(), $board_config['board_timezone']),
1642              'MESSAGE' => $preview_message,
1643  
1644              'S_HIDDEN_FIELDS' => $s_hidden_fields,
1645  
1646              'L_SUBJECT' => $lang['Subject'],
1647              'L_DATE' => $lang['Date'],
1648              'L_FROM' => $lang['From'],
1649              'L_TO' => $lang['To'],
1650              'L_PREVIEW' => $lang['Preview'],
1651              'L_POSTED' => $lang['Posted'])
1652          );
1653  
1654          $template->assign_var_from_handle('POST_PREVIEW_BOX', 'preview');
1655      }
1656  
1657      //
1658      // Start error handling
1659      //
1660      if ($error)
1661      {
1662          $privmsg_message = htmlspecialchars($privmsg_message);
1663          $template->set_filenames(array(
1664              'reg_header' => 'error_body.tpl')
1665          );
1666          $template->assign_vars(array(
1667              'ERROR_MESSAGE' => $error_msg)
1668          );
1669          $template->assign_var_from_handle('ERROR_BOX', 'reg_header');
1670      }
1671  
1672      //
1673      // Load templates
1674      //
1675      $template->set_filenames(array(
1676          'body' => 'posting_body.tpl')
1677      );
1678      make_jumpbox('viewforum.'.$phpEx);
1679  
1680      //
1681      // Enable extensions in posting_body
1682      //
1683      $template->assign_block_vars('switch_privmsg', array());
1684  
1685      //
1686      // HTML toggle selection
1687      //
1688      if ( $board_config['allow_html'] )
1689      {
1690          $html_status = $lang['HTML_is_ON'];
1691          $template->assign_block_vars('switch_html_checkbox', array());
1692      }
1693      else
1694      {
1695          $html_status = $lang['HTML_is_OFF'];
1696      }
1697  
1698      //
1699      // BBCode toggle selection
1700      //
1701      if ( $board_config['allow_bbcode'] )
1702      {
1703          $bbcode_status = $lang['BBCode_is_ON'];
1704          $template->assign_block_vars('switch_bbcode_checkbox', array());
1705      }
1706      else
1707      {
1708          $bbcode_status = $lang['BBCode_is_OFF'];
1709      }
1710  
1711      //
1712      // Smilies toggle selection
1713      //
1714      if ( $board_config['allow_smilies'] )
1715      {
1716          $smilies_status = $lang['Smilies_are_ON'];
1717          $template->assign_block_vars('switch_smilies_checkbox', array());
1718      }
1719      else
1720      {
1721          $smilies_status = $lang['Smilies_are_OFF'];
1722      }
1723  
1724      //
1725      // Signature toggle selection - only show if
1726      // the user has a signature
1727      //
1728      if ( $user_sig != '' )
1729      {
1730          $template->assign_block_vars('switch_signature_checkbox', array());
1731      }
1732  
1733      if ( $mode == 'post' )
1734      {
1735          $post_a = $lang['Send_a_new_message'];
1736      }
1737      else if ( $mode == 'reply' )
1738      {
1739          $post_a = $lang['Send_a_reply'];
1740          $mode = 'post';
1741      }
1742      else if ( $mode == 'edit' )
1743      {
1744          $post_a = $lang['Edit_message'];
1745      }
1746  
1747      $s_hidden_fields = '<input type="hidden" name="folder" value="' . $folder . '" />';
1748      $s_hidden_fields .= '<input type="hidden" name="mode" value="' . $mode . '" />';
1749      $s_hidden_fields .= '<input type="hidden" name="sid" value="' . $userdata['session_id'] . '" />';
1750      if ( $mode == 'edit' )
1751      {
1752          $s_hidden_fields .= '<input type="hidden" name="' . POST_POST_URL . '" value="' . $privmsg_id . '" />';
1753      }
1754  
1755      //
1756      // Send smilies to template
1757      //
1758      generate_smilies('inline', PAGE_PRIVMSGS);
1759  
1760      $template->assign_vars(array(
1761          'SUBJECT' => $privmsg_subject, 
1762          'USERNAME' => $to_username,
1763          'MESSAGE' => $privmsg_message,
1764          'HTML_STATUS' => $html_status, 
1765          'SMILIES_STATUS' => $smilies_status, 
1766          'BBCODE_STATUS' => sprintf($bbcode_status, '<a href="' . append_sid("faq.$phpEx?mode=bbcode") . '" target="_phpbbcode">', '</a>'), 
1767          'FORUM_NAME' => $lang['Private_Message'], 
1768  
1769          'BOX_NAME' => $l_box_name, 
1770          'INBOX_IMG' => $inbox_img, 
1771          'SENTBOX_IMG' => $sentbox_img, 
1772          'OUTBOX_IMG' => $outbox_img, 
1773          'SAVEBOX_IMG' => $savebox_img, 
1774          'INBOX' => $inbox_url, 
1775          'SENTBOX' => $sentbox_url, 
1776          'OUTBOX' => $outbox_url, 
1777          'SAVEBOX' => $savebox_url, 
1778  
1779          'L_SUBJECT' => $lang['Subject'],
1780          'L_MESSAGE_BODY' => $lang['Message_body'],
1781          'L_OPTIONS' => $lang['Options'],
1782          'L_SPELLCHECK' => $lang['Spellcheck'],
1783          'L_PREVIEW' => $lang['Preview'],
1784          'L_SUBMIT' => $lang['Submit'],
1785          'L_CANCEL' => $lang['Cancel'],
1786          'L_POST_A' => $post_a,
1787          'L_FIND_USERNAME' => $lang['Find_username'],
1788          'L_FIND' => $lang['Find'],
1789          'L_DISABLE_HTML' => $lang['Disable_HTML_pm'], 
1790          'L_DISABLE_BBCODE' => $lang['Disable_BBCode_pm'], 
1791          'L_DISABLE_SMILIES' => $lang['Disable_Smilies_pm'], 
1792          'L_ATTACH_SIGNATURE' => $lang['Attach_signature'], 
1793  
1794          'L_BBCODE_B_HELP' => $lang['bbcode_b_help'], 
1795          'L_BBCODE_I_HELP' => $lang['bbcode_i_help'], 
1796          'L_BBCODE_U_HELP' => $lang['bbcode_u_help'], 
1797          'L_BBCODE_Q_HELP' => $lang['bbcode_q_help'], 
1798          'L_BBCODE_C_HELP' => $lang['bbcode_c_help'], 
1799          'L_BBCODE_L_HELP' => $lang['bbcode_l_help'], 
1800          'L_BBCODE_O_HELP' => $lang['bbcode_o_help'], 
1801          'L_BBCODE_P_HELP' => $lang['bbcode_p_help'], 
1802          'L_BBCODE_W_HELP' => $lang['bbcode_w_help'], 
1803          'L_BBCODE_A_HELP' => $lang['bbcode_a_help'], 
1804          'L_BBCODE_S_HELP' => $lang['bbcode_s_help'], 
1805          'L_BBCODE_F_HELP' => $lang['bbcode_f_help'], 
1806          'L_EMPTY_MESSAGE' => $lang['Empty_message'],
1807  
1808          'L_FONT_COLOR' => $lang['Font_color'], 
1809          'L_COLOR_DEFAULT' => $lang['color_default'], 
1810          'L_COLOR_DARK_RED' => $lang['color_dark_red'], 
1811          'L_COLOR_RED' => $lang['color_red'], 
1812          'L_COLOR_ORANGE' => $lang['color_orange'], 
1813          'L_COLOR_BROWN' => $lang['color_brown'], 
1814          'L_COLOR_YELLOW' => $lang['color_yellow'], 
1815          'L_COLOR_GREEN' => $lang['color_green'], 
1816          'L_COLOR_OLIVE' => $lang['color_olive'], 
1817          'L_COLOR_CYAN' => $lang['color_cyan'], 
1818          'L_COLOR_BLUE' => $lang['color_blue'], 
1819          'L_COLOR_DARK_BLUE' => $lang['color_dark_blue'], 
1820          'L_COLOR_INDIGO' => $lang['color_indigo'], 
1821          'L_COLOR_VIOLET' => $lang['color_violet'], 
1822          'L_COLOR_WHITE' => $lang['color_white'], 
1823          'L_COLOR_BLACK' => $lang['color_black'], 
1824  
1825          'L_FONT_SIZE' => $lang['Font_size'], 
1826          'L_FONT_TINY' => $lang['font_tiny'], 
1827          'L_FONT_SMALL' => $lang['font_small'], 
1828          'L_FONT_NORMAL' => $lang['font_normal'], 
1829          'L_FONT_LARGE' => $lang['font_large'], 
1830          'L_FONT_HUGE' => $lang['font_huge'], 
1831  
1832          'L_BBCODE_CLOSE_TAGS' => $lang['Close_Tags'], 
1833          'L_STYLES_TIP' => $lang['Styles_tip'], 
1834  
1835          'S_HTML_CHECKED' => ( !$html_on ) ? ' checked="checked"' : '', 
1836          'S_BBCODE_CHECKED' => ( !$bbcode_on ) ? ' checked="checked"' : '', 
1837          'S_SMILIES_CHECKED' => ( !$smilies_on ) ? ' checked="checked"' : '', 
1838          'S_SIGNATURE_CHECKED' => ( $attach_sig ) ? ' checked="checked"' : '', 
1839          'S_HIDDEN_FORM_FIELDS' => $s_hidden_fields,
1840          'S_POST_ACTION' => append_sid("privmsg.$phpEx"),
1841              
1842          'U_SEARCH_USER' => append_sid("search.$phpEx?mode=searchuser"), 
1843          'U_VIEW_FORUM' => append_sid("privmsg.$phpEx"))
1844      );
1845  
1846      $template->pparse('body');
1847  
1848      include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
1849  }
1850  
1851  //
1852  // Default page
1853  //
1854  if ( !$userdata['session_logged_in'] )
1855  {
1856      redirect(append_sid("login.$phpEx?redirect=privmsg.$phpEx&folder=inbox", true));
1857  }
1858  
1859  //
1860  // Update unread status 
1861  //
1862  $sql = "UPDATE " . USERS_TABLE . "
1863      SET user_unread_privmsg = user_unread_privmsg + user_new_privmsg, user_new_privmsg = 0, user_last_privmsg = " . $userdata['session_start'] . " 
1864      WHERE user_id = " . $userdata['user_id'];
1865  if ( !$db->sql_query($sql) )
1866  {
1867      message_die(GENERAL_ERROR, 'Could not update private message new/read status for user', '', __LINE__, __FILE__, $sql);
1868  }
1869  
1870  $sql = "UPDATE " . PRIVMSGS_TABLE . "
1871      SET privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " 
1872      WHERE privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
1873          AND privmsgs_to_userid = " . $userdata['user_id'];
1874  if ( !$db->sql_query($sql) )
1875  {
1876      message_die(GENERAL_ERROR, 'Could not update private message new/read status (2) for user', '', __LINE__, __FILE__, $sql);
1877  }
1878  
1879  //
1880  // Reset PM counters
1881  //
1882  $userdata['user_new_privmsg'] = 0;
1883  $userdata['user_unread_privmsg'] = ( $userdata['user_new_privmsg'] + $userdata['user_unread_privmsg'] );
1884  
1885  //
1886  // Generate page
1887  //
1888  $page_title = $lang['Private_Messaging'];
1889  include($phpbb_root_path . 'includes/page_header.'.$phpEx);
1890  
1891  //
1892  // Load templates
1893  //
1894  $template->set_filenames(array(
1895      'body' => 'privmsgs_body.tpl')
1896  );
1897  make_jumpbox('viewforum.'.$phpEx);
1898  
1899  $orig_word = array();
1900  $replacement_word = array();
1901  obtain_word_list($orig_word, $replacement_word);
1902  
1903  //
1904  // New message
1905  //
1906  $post_new_mesg_url = '<a href="' . append_sid("privmsg.$phpEx?mode=post") . '"><img src="' . $images['post_new'] . '" alt="' . $lang['Send_a_new_message'] . '" border="0" /></a>';
1907  
1908  //
1909  // General SQL to obtain messages
1910  //
1911  $sql_tot = "SELECT COUNT(privmsgs_id) AS total 
1912      FROM " . PRIVMSGS_TABLE . " ";
1913  $sql = "SELECT pm.privmsgs_type, pm.privmsgs_id, pm.privmsgs_date, pm.privmsgs_subject, u.user_id, u.username 
1914      FROM " . PRIVMSGS_TABLE . " pm, " . USERS_TABLE . " u ";
1915  switch( $folder )
1916  {
1917      case 'inbox':
1918          $sql_tot .= "WHERE privmsgs_to_userid = " . $userdata['user_id'] . "
1919              AND ( privmsgs_type =  " . PRIVMSGS_NEW_MAIL . "
1920                  OR privmsgs_type = " . PRIVMSGS_READ_MAIL . " 
1921                  OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
1922  
1923          $sql .= "WHERE pm.privmsgs_to_userid = " . $userdata['user_id'] . "
1924              AND u.user_id = pm.privmsgs_from_userid
1925              AND ( pm.privmsgs_type =  " . PRIVMSGS_NEW_MAIL . "
1926                  OR pm.privmsgs_type = " . PRIVMSGS_READ_MAIL . " 
1927                  OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
1928          break;
1929  
1930      case 'outbox':
1931          $sql_tot .= "WHERE privmsgs_from_userid = " . $userdata['user_id'] . "
1932              AND ( privmsgs_type =  " . PRIVMSGS_NEW_MAIL . "
1933                  OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
1934  
1935          $sql .= "WHERE pm.privmsgs_from_userid = " . $userdata['user_id'] . "
1936              AND u.user_id = pm.privmsgs_to_userid
1937              AND ( pm.privmsgs_type =  " . PRIVMSGS_NEW_MAIL . "
1938                  OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " )";
1939          break;
1940  
1941      case 'sentbox':
1942          $sql_tot .= "WHERE privmsgs_from_userid = " . $userdata['user_id'] . "
1943              AND privmsgs_type =  " . PRIVMSGS_SENT_MAIL;
1944  
1945          $sql .= "WHERE pm.privmsgs_from_userid = " . $userdata['user_id'] . "
1946              AND u.user_id = pm.privmsgs_to_userid
1947              AND pm.privmsgs_type =  " . PRIVMSGS_SENT_MAIL;
1948          break;
1949  
1950      case 'savebox':
1951          $sql_tot .= "WHERE ( ( privmsgs_to_userid = " . $userdata['user_id'] . " 
1952                  AND privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " )
1953              OR ( privmsgs_from_userid = " . $userdata['user_id'] . " 
1954                  AND privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . ") )";
1955  
1956          $sql .= "WHERE u.user_id = pm.privmsgs_from_userid 
1957              AND ( ( pm.privmsgs_to_userid = " . $userdata['user_id'] . " 
1958                  AND pm.privmsgs_type = " . PRIVMSGS_SAVED_IN_MAIL . " ) 
1959              OR ( pm.privmsgs_from_userid = " . $userdata['user_id'] . " 
1960                  AND pm.privmsgs_type = " . PRIVMSGS_SAVED_OUT_MAIL . " ) )";
1961          break;
1962  
1963      default:
1964          message_die(GENERAL_MESSAGE, $lang['No_such_folder']);
1965          break;
1966  }
1967  
1968  //
1969  // Show messages over previous x days/months
1970  //
1971  if ( $submit_msgdays && ( !empty($HTTP_POST_VARS['msgdays']) || !empty($HTTP_GET_VARS['msgdays']) ) )
1972  {
1973      $msg_days = ( !empty($HTTP_POST_VARS['msgdays']) ) ? intval($HTTP_POST_VARS['msgdays']) : intval($HTTP_GET_VARS['msgdays']);
1974      $min_msg_time = time() - ($msg_days * 86400);
1975  
1976      $limit_msg_time_total = " AND privmsgs_date > $min_msg_time";
1977      $limit_msg_time = " AND pm.privmsgs_date > $min_msg_time ";
1978  
1979      if ( !empty($HTTP_POST_VARS['msgdays']) )
1980      {
1981          $start = 0;
1982      }
1983  }
1984  else
1985  {
1986      $limit_msg_time = $limit_msg_time_total = '';
1987      $msg_days = 0;
1988  }
1989  
1990  $sql .= $limit_msg_time . " ORDER BY pm.privmsgs_date DESC LIMIT $start, " . $board_config['topics_per_page'];
1991  $sql_all_tot = $sql_tot;
1992  $sql_tot .= $limit_msg_time_total;
1993  
1994  //
1995  // Get messages
1996  //
1997  if ( !($result = $db->sql_query($sql_tot)) )
1998  {
1999      message_die(GENERAL_ERROR, 'Could not query private message information', '', __LINE__, __FILE__, $sql_tot);
2000  }
2001  
2002  $pm_total = ( $row = $db->sql_fetchrow($result) ) ? $row['total'] : 0;
2003  
2004  if ( !($result = $db->sql_query($sql_all_tot)) )
2005  {
2006      message_die(GENERAL_ERROR, 'Could not query private message information', '', __LINE__, __FILE__, $sql_tot);
2007  }
2008  
2009  $pm_all_total = ( $row = $db->sql_fetchrow($result) ) ? $row['total'] : 0;
2010  
2011  //
2012  // Build select box
2013  //
2014  $previous_days = array(0, 1, 7, 14, 30, 90, 180, 364);
2015  $previous_days_text = array($lang['All_Posts'], $lang['1_Day'], $lang['7_Days'], $lang['2_Weeks'], $lang['1_Month'], $lang['3_Months'], $lang['6_Months'], $lang['1_Year']);
2016  
2017  $select_msg_days = '';
2018  for($i = 0; $i < count($previous_days); $i++)
2019  {
2020      $selected = ( $msg_days == $previous_days[$i] ) ? ' selected="selected"' : '';
2021      $select_msg_days .= '<option value="' . $previous_days[$i] . '"' . $selected . '>' . $previous_days_text[$i] . '</option>';
2022  }
2023  
2024  //
2025  // Define correct icons
2026  //
2027  switch ( $folder )
2028  {
2029      case 'inbox':
2030          $l_box_name = $lang['Inbox'];
2031          break;
2032      case 'outbox':
2033          $l_box_name = $lang['Outbox'];
2034          break;
2035      case 'savebox':
2036          $l_box_name = $lang['Savebox'];
2037          break;
2038      case 'sentbox':
2039          $l_box_name = $lang['Sentbox'];
2040          break;
2041  }
2042  $post_pm = append_sid("privmsg.$phpEx?mode=post");
2043  $post_pm_img = '<a href="' . $post_pm . '"><img src="' . $images['pm_postmsg'] . '" alt="' . $lang['Post_new_pm'] . '" border="0" /></a>';
2044  $post_pm = '<a href="' . $post_pm . '">' . $lang['Post_new_pm'] . '</a>';
2045  
2046  //
2047  // Output data for inbox status
2048  //
2049  if ( $folder != 'outbox' )
2050  {
2051      $inbox_limit_pct = ( $board_config['max_' . $folder . '_privmsgs'] > 0 ) ? round(( $pm_all_total / $board_config['max_' . $folder . '_privmsgs'] ) * 100) : 100;
2052      $inbox_limit_img_length = ( $board_config['max_' . $folder . '_privmsgs'] > 0 ) ? round(( $pm_all_total / $board_config['max_' . $folder . '_privmsgs'] ) * $board_config['privmsg_graphic_length']) : $board_config['privmsg_graphic_length'];
2053      $inbox_limit_remain = ( $board_config['max_' . $folder . '_privmsgs'] > 0 ) ? $board_config['max_' . $folder . '_privmsgs'] - $pm_all_total : 0;
2054  
2055      $template->assign_block_vars('switch_box_size_notice', array());
2056  
2057      switch( $folder )
2058      {
2059          case 'inbox':
2060              $l_box_size_status = sprintf($lang['Inbox_size'], $inbox_limit_pct);
2061              break;
2062          case 'sentbox':
2063              $l_box_size_status = sprintf($lang['Sentbox_size'], $inbox_limit_pct);
2064              break;
2065          case 'savebox':
2066              $l_box_size_status = sprintf($lang['Savebox_size'], $inbox_limit_pct);
2067              break;
2068          default:
2069              $l_box_size_status = '';
2070              break;
2071      }
2072  }
2073  else
2074  {
2075      $inbox_limit_img_length = $inbox_limit_pct = $l_box_size_status = '';
2076  }
2077  
2078  //
2079  // Dump vars to template
2080  //
2081  $template->assign_vars(array(
2082      'BOX_NAME' => $l_box_name, 
2083      'INBOX_IMG' => $inbox_img, 
2084      'SENTBOX_IMG' => $sentbox_img, 
2085      'OUTBOX_IMG' => $outbox_img, 
2086      'SAVEBOX_IMG' => $savebox_img, 
2087      'INBOX' => $inbox_url, 
2088      'SENTBOX' => $sentbox_url, 
2089      'OUTBOX' => $outbox_url, 
2090      'SAVEBOX' => $savebox_url, 
2091  
2092      'POST_PM_IMG' => $post_pm_img, 
2093      'POST_PM' => $post_pm, 
2094  
2095      'INBOX_LIMIT_IMG_WIDTH' => $inbox_limit_img_length, 
2096      'INBOX_LIMIT_PERCENT' => $inbox_limit_pct, 
2097  
2098      'BOX_SIZE_STATUS' => $l_box_size_status, 
2099  
2100      'L_INBOX' => $lang['Inbox'],
2101      'L_OUTBOX' => $lang['Outbox'],
2102      'L_SENTBOX' => $lang['Sent'],
2103      'L_SAVEBOX' => $lang['Saved'],
2104      'L_MARK' => $lang['Mark'],
2105      'L_FLAG' => $lang['Flag'],
2106      'L_SUBJECT' => $lang['Subject'],
2107      'L_DATE' => $lang['Date'],
2108      'L_DISPLAY_MESSAGES' => $lang['Display_messages'],
2109      'L_FROM_OR_TO' => ( $folder == 'inbox' || $folder == 'savebox' ) ? $lang['From'] : $lang['To'], 
2110      'L_MARK_ALL' => $lang['Mark_all'], 
2111      'L_UNMARK_ALL' => $lang['Unmark_all'], 
2112      'L_DELETE_MARKED' => $lang['Delete_marked'], 
2113      'L_DELETE_ALL' => $lang['Delete_all'], 
2114      'L_SAVE_MARKED' => $lang['Save_marked'], 
2115  
2116      'S_PRIVMSGS_ACTION' => append_sid("privmsg.$phpEx?folder=$folder"),
2117      'S_HIDDEN_FIELDS' => '',
2118      'S_POST_NEW_MSG' => $post_new_mesg_url,
2119      'S_SELECT_MSG_DAYS' => $select_msg_days,
2120  
2121      'U_POST_NEW_TOPIC' => append_sid("privmsg.$phpEx?mode=post"))
2122  );
2123  
2124  //
2125  // Okay, let's build the correct folder
2126  //
2127  if ( !($result = $db->sql_query($sql)) )
2128  {
2129      message_die(GENERAL_ERROR, 'Could not query private messages', '', __LINE__, __FILE__, $sql);
2130  }
2131  
2132  if ( $row = $db->sql_fetchrow($result) )
2133  {
2134      $i = 0;
2135      do
2136      {
2137          $privmsg_id = $row['privmsgs_id'];
2138  
2139          $flag = $row['privmsgs_type'];
2140  
2141          $icon_flag = ( $flag == PRIVMSGS_NEW_MAIL || $flag == PRIVMSGS_UNREAD_MAIL ) ? $images['pm_unreadmsg'] : $images['pm_readmsg'];
2142          $icon_flag_alt = ( $flag == PRIVMSGS_NEW_MAIL || $flag == PRIVMSGS_UNREAD_MAIL ) ? $lang['Unread_message'] : $lang['Read_message'];
2143  
2144          $msg_userid = $row['user_id'];
2145          $msg_username = $row['username'];
2146  
2147          $u_from_user_profile = append_sid("profile.$phpEx?mode=viewprofile&amp;" . POST_USERS_URL . "=$msg_userid");
2148  
2149          $msg_subject = $row['privmsgs_subject'];
2150  
2151          if ( count($orig_word) )
2152          {
2153              $msg_subject = preg_replace($orig_word, $replacement_word, $msg_subject);
2154          }
2155          
2156          $u_subject = append_sid("privmsg.$phpEx?folder=$folder&amp;mode=read&amp;" . POST_POST_URL . "=$privmsg_id");
2157  
2158          $msg_date = create_date($board_config['default_dateformat'], $row['privmsgs_date'], $board_config['board_timezone']);
2159  
2160          if ( $flag == PRIVMSGS_NEW_MAIL && $folder == 'inbox' )
2161          {
2162              $msg_subject = '<b>' . $msg_subject . '</b>';
2163              $msg_date = '<b>' . $msg_date . '</b>';
2164              $msg_username = '<b>' . $msg_username . '</b>';
2165          }
2166  
2167          $row_color = ( !($i % 2) ) ? $theme['td_color1'] : $theme['td_color2'];
2168          $row_class = ( !($i % 2) ) ? $theme['td_class1'] : $theme['td_class2'];
2169          $i++;
2170  
2171          $template->assign_block_vars('listrow', array(
2172              'ROW_COLOR' => '#' . $row_color,
2173              'ROW_CLASS' => $row_class,
2174              'FROM' => $msg_username,
2175              'SUBJECT' => $msg_subject,
2176              'DATE' => $msg_date,
2177              'PRIVMSG_FOLDER_IMG' => $icon_flag,
2178  
2179              'L_PRIVMSG_FOLDER_ALT' => $icon_flag_alt, 
2180  
2181              'S_MARK_ID' => $privmsg_id, 
2182  
2183              'U_READ' => $u_subject,
2184              'U_FROM_USER_PROFILE' => $u_from_user_profile)
2185          );
2186      }
2187      while( $row = $db->sql_fetchrow($result) );
2188  
2189      $template->assign_vars(array(
2190          'PAGINATION' => generate_pagination("privmsg.$phpEx?folder=$folder", $pm_total, $board_config['topics_per_page'], $start),
2191          'PAGE_NUMBER' => sprintf($lang['Page_of'], ( floor( $start / $board_config['topics_per_page'] ) + 1 ), ceil( $pm_total / $board_config['topics_per_page'] )), 
2192  
2193          'L_GOTO_PAGE' => $lang['Goto_page'])
2194      );
2195  
2196  }
2197  else
2198  {
2199      $template->assign_vars(array(
2200          'L_NO_MESSAGES' => $lang['No_messages_folder'])
2201      );
2202  
2203      $template->assign_block_vars("switch_no_messages", array() );
2204  }
2205  
2206  $template->pparse('body');
2207  
2208  include($phpbb_root_path . 'includes/page_tail.'.$phpEx);
2209  
2210  ?>


Généré le : Sat Nov 24 22:41:46 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics