[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/includes/ucp/ -> ucp_pm_compose.php (source)

   1  <?php
   2  /**
   3  *
   4  * @package ucp
   5  * @version $Id$
   6  * @copyright (c) 2005 phpBB Group
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
   8  *
   9  */
  10  
  11  /**
  12  * @ignore
  13  */
  14  if (!defined('IN_PHPBB'))
  15  {
  16      exit;
  17  }
  18  
  19  /**
  20  * Compose private message
  21  * Called from ucp_pm with mode == 'compose'
  22  */
  23  function compose_pm($id, $mode, $action, $user_folders = array())
  24  {
  25      global $template, $db, $auth, $user;
  26      global $phpbb_root_path, $phpEx, $config;
  27  
  28      // Damn php and globals - i know, this is horrible
  29      // Needed for handle_message_list_actions()
  30      global $refresh, $submit, $preview;
  31  
  32      include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
  33      include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
  34      include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
  35  
  36      if (!$action)
  37      {
  38          $action = 'post';
  39      }
  40      add_form_key('ucp_pm_compose');
  41  
  42      // Grab only parameters needed here
  43      $to_user_id        = request_var('u', 0);
  44      $to_group_id    = request_var('g', 0);
  45      $msg_id            = request_var('p', 0);
  46      $draft_id        = request_var('d', 0);
  47      $lastclick        = request_var('lastclick', 0);
  48  
  49      // Reply to all triggered (quote/reply)
  50      $reply_to_all    = request_var('reply_to_all', 0);
  51  
  52      // Do NOT use request_var or specialchars here
  53      $address_list    = isset($_REQUEST['address_list']) ? $_REQUEST['address_list'] : array();
  54  
  55      if (!is_array($address_list))
  56      {
  57          $address_list = array();
  58      }
  59  
  60      $submit        = (isset($_POST['post'])) ? true : false;
  61      $preview    = (isset($_POST['preview'])) ? true : false;
  62      $save        = (isset($_POST['save'])) ? true : false;
  63      $load        = (isset($_POST['load'])) ? true : false;
  64      $cancel        = (isset($_POST['cancel']) && !isset($_POST['save'])) ? true : false;
  65      $delete        = (isset($_POST['delete'])) ? true : false;
  66  
  67      $remove_u    = (isset($_REQUEST['remove_u'])) ? true : false;
  68      $remove_g    = (isset($_REQUEST['remove_g'])) ? true : false;
  69      $add_to        = (isset($_REQUEST['add_to'])) ? true : false;
  70      $add_bcc    = (isset($_REQUEST['add_bcc'])) ? true : false;
  71  
  72      $refresh    = isset($_POST['add_file']) || isset($_POST['delete_file']) || $save || $load
  73          || $remove_u || $remove_g || $add_to || $add_bcc;
  74  
  75      $action        = ($delete && !$preview && !$refresh && $submit) ? 'delete' : $action;
  76      $select_single = ($config['allow_mass_pm'] && $auth->acl_get('u_masspm')) ? false : true;
  77  
  78      $error = array();
  79      $current_time = time();
  80  
  81      // Was cancel pressed? If so then redirect to the appropriate page
  82      if ($cancel || ($current_time - $lastclick < 2 && $submit))
  83      {
  84          if ($msg_id)
  85          {
  86              redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=view&amp;action=view_message&amp;p=' . $msg_id));
  87          }
  88          redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm'));
  89      }
  90  
  91      // Since viewtopic.php language entries are used in several modes,
  92      // we include the language file here
  93      $user->add_lang('viewtopic');
  94  
  95      // Output PM_TO box if message composing
  96      if ($action != 'edit')
  97      {
  98          // Add groups to PM box
  99          if ($config['allow_mass_pm'] && $auth->acl_get('u_masspm_group'))
 100          {
 101              $sql = 'SELECT g.group_id, g.group_name, g.group_type
 102                  FROM ' . GROUPS_TABLE . ' g';
 103  
 104              if (!$auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
 105              {
 106                  $sql .= ' LEFT JOIN ' . USER_GROUP_TABLE . ' ug
 107                      ON (
 108                          g.group_id = ug.group_id
 109                          AND ug.user_id = ' . $user->data['user_id'] . '
 110                          AND ug.user_pending = 0
 111                      )
 112                      WHERE (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ')';
 113              }
 114  
 115              $sql .= ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) ? ' WHERE ' : ' AND ';
 116  
 117              $sql .= 'g.group_receive_pm = 1
 118                  ORDER BY g.group_type DESC, g.group_name ASC';
 119              $result = $db->sql_query($sql);
 120  
 121              $group_options = '';
 122              while ($row = $db->sql_fetchrow($result))
 123              {
 124                  $group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '">' . (($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
 125              }
 126              $db->sql_freeresult($result);
 127          }
 128  
 129          $template->assign_vars(array(
 130              'S_SHOW_PM_BOX'        => true,
 131              'S_ALLOW_MASS_PM'    => ($config['allow_mass_pm'] && $auth->acl_get('u_masspm')) ? true : false,
 132              'S_GROUP_OPTIONS'    => ($config['allow_mass_pm'] && $auth->acl_get('u_masspm_group')) ? $group_options : '',
 133              'U_FIND_USERNAME'    => append_sid("{$phpbb_root_path}memberlist.$phpEx", "mode=searchuser&amp;form=postform&amp;field=username_list&amp;select_single=$select_single"),
 134          ));
 135      }
 136  
 137      $sql = '';
 138      $folder_id = 0;
 139  
 140      // What is all this following SQL for? Well, we need to know
 141      // some basic information in all cases before we do anything.
 142      switch ($action)
 143      {
 144          case 'post':
 145              if (!$auth->acl_get('u_sendpm'))
 146              {
 147                  trigger_error('NO_AUTH_SEND_MESSAGE');
 148              }
 149          break;
 150  
 151          case 'reply':
 152          case 'quote':
 153          case 'forward':
 154          case 'quotepost':
 155              if (!$msg_id)
 156              {
 157                  trigger_error('NO_MESSAGE');
 158              }
 159  
 160              if (!$auth->acl_get('u_sendpm'))
 161              {
 162                  trigger_error('NO_AUTH_SEND_MESSAGE');
 163              }
 164  
 165              if ($action == 'quotepost')
 166              {
 167                  $sql = 'SELECT p.post_id as msg_id, p.forum_id, p.post_text as message_text, p.poster_id as author_id, p.post_time as message_time, p.bbcode_bitfield, p.bbcode_uid, p.enable_sig, p.enable_smilies, p.enable_magic_url, t.topic_title as message_subject, u.username as quote_username
 168                      FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t, ' . USERS_TABLE . " u
 169                      WHERE p.post_id = $msg_id
 170                          AND t.topic_id = p.topic_id
 171                          AND u.user_id = p.poster_id";
 172              }
 173              else
 174              {
 175                  $sql = 'SELECT t.folder_id, p.*, u.username as quote_username
 176                      FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p, ' . USERS_TABLE . ' u
 177                      WHERE t.user_id = ' . $user->data['user_id'] . "
 178                          AND p.author_id = u.user_id
 179                          AND t.msg_id = p.msg_id
 180                          AND p.msg_id = $msg_id";
 181              }
 182          break;
 183  
 184          case 'edit':
 185              if (!$msg_id)
 186              {
 187                  trigger_error('NO_MESSAGE');
 188              }
 189  
 190              // check for outbox (not read) status, we do not allow editing if one user already having the message
 191              $sql = 'SELECT p.*, t.folder_id
 192                  FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
 193                  WHERE t.user_id = ' . $user->data['user_id'] . '
 194                      AND t.folder_id = ' . PRIVMSGS_OUTBOX . "
 195                      AND t.msg_id = $msg_id
 196                      AND t.msg_id = p.msg_id";
 197          break;
 198  
 199          case 'delete':
 200              if (!$auth->acl_get('u_pm_delete'))
 201              {
 202                  trigger_error('NO_AUTH_DELETE_MESSAGE');
 203              }
 204  
 205              if (!$msg_id)
 206              {
 207                  trigger_error('NO_MESSAGE');
 208              }
 209  
 210              $sql = 'SELECT msg_id, pm_unread, pm_new, author_id, folder_id
 211                  FROM ' . PRIVMSGS_TO_TABLE . '
 212                  WHERE user_id = ' . $user->data['user_id'] . "
 213                      AND msg_id = $msg_id";
 214          break;
 215  
 216          case 'smilies':
 217              generate_smilies('window', 0);
 218          break;
 219  
 220          default:
 221              trigger_error('NO_ACTION_MODE', E_USER_ERROR);
 222          break;
 223      }
 224  
 225      if ($action == 'forward' && (!$config['forward_pm'] || !$auth->acl_get('u_pm_forward')))
 226      {
 227          trigger_error('NO_AUTH_FORWARD_MESSAGE');
 228      }
 229  
 230      if ($action == 'edit' && !$auth->acl_get('u_pm_edit'))
 231      {
 232          trigger_error('NO_AUTH_EDIT_MESSAGE');
 233      }
 234  
 235      if ($sql)
 236      {
 237          $result = $db->sql_query($sql);
 238          $post = $db->sql_fetchrow($result);
 239          $db->sql_freeresult($result);
 240  
 241          if (!$post)
 242          {
 243              // If editing it could be the recipient already read the message...
 244              if ($action == 'edit')
 245              {
 246                  $sql = 'SELECT p.*, t.folder_id
 247                      FROM ' . PRIVMSGS_TO_TABLE . ' t, ' . PRIVMSGS_TABLE . ' p
 248                      WHERE t.user_id = ' . $user->data['user_id'] . "
 249                          AND t.msg_id = $msg_id
 250                          AND t.msg_id = p.msg_id";
 251                  $result = $db->sql_query($sql);
 252                  $post = $db->sql_fetchrow($result);
 253                  $db->sql_freeresult($result);
 254  
 255                  if ($post)
 256                  {
 257                      trigger_error('NO_EDIT_READ_MESSAGE');
 258                  }
 259              }
 260  
 261              trigger_error('NO_MESSAGE');
 262          }
 263  
 264          if ($action == 'quotepost')
 265          {
 266              if (($post['forum_id'] && !$auth->acl_get('f_read', $post['forum_id'])) || (!$post['forum_id'] && !$auth->acl_getf_global('f_read')))
 267              {
 268                  trigger_error('NOT_AUTHORISED');
 269              }
 270  
 271              // Passworded forum?
 272              if ($post['forum_id'])
 273              {
 274                  $sql = 'SELECT forum_id, forum_name, forum_password
 275                      FROM ' . FORUMS_TABLE . '
 276                      WHERE forum_id = ' . (int) $post['forum_id'];
 277                  $result = $db->sql_query($sql);
 278                  $forum_data = $db->sql_fetchrow($result);
 279                  $db->sql_freeresult($result);
 280  
 281                  if (!empty($forum_data['forum_password']))
 282                  {
 283                      login_forum_box($forum_data);
 284                  }
 285              }
 286          }
 287  
 288          $msg_id            = (int) $post['msg_id'];
 289          $folder_id        = (isset($post['folder_id'])) ? $post['folder_id'] : 0;
 290          $message_text    = (isset($post['message_text'])) ? $post['message_text'] : '';
 291  
 292          if ((!$post['author_id'] || ($post['author_id'] == ANONYMOUS && $action != 'delete')) && $msg_id)
 293          {
 294              trigger_error('NO_AUTHOR');
 295          }
 296  
 297          if ($action == 'quotepost')
 298          {
 299              // Decode text for message display
 300              decode_message($message_text, $post['bbcode_uid']);
 301          }
 302  
 303          if ($action != 'delete')
 304          {
 305              $enable_urls = $post['enable_magic_url'];
 306              $enable_sig = (isset($post['enable_sig'])) ? $post['enable_sig'] : 0;
 307  
 308              $message_attachment = (isset($post['message_attachment'])) ? $post['message_attachment'] : 0;
 309              $message_subject = $post['message_subject'];
 310              $message_time = $post['message_time'];
 311              $bbcode_uid = $post['bbcode_uid'];
 312  
 313              $quote_username = (isset($post['quote_username'])) ? $post['quote_username'] : '';
 314              $icon_id = (isset($post['icon_id'])) ? $post['icon_id'] : 0;
 315  
 316              if (($action == 'reply' || $action == 'quote' || $action == 'quotepost') && !sizeof($address_list) && !$refresh && !$submit && !$preview)
 317              {
 318                  // Add the original author as the recipient if quoting a post or only replying and not having checked "reply to all"
 319                  if ($action == 'quotepost' || !$reply_to_all)
 320                  {
 321                      $address_list = array('u' => array($post['author_id'] => 'to'));
 322                  }
 323                  else
 324                  {
 325                      // We try to include every previously listed member from the TO Header - Reply to all
 326                      $address_list = rebuild_header(array('to' => $post['to_address']));
 327  
 328                      // Add the author (if he is already listed then this is no shame (it will be overwritten))
 329                      $address_list['u'][$post['author_id']] = 'to';
 330  
 331                      // Now, make sure the user itself is not listed. ;)
 332                      if (isset($address_list['u'][$user->data['user_id']]))
 333                      {
 334                          unset($address_list['u'][$user->data['user_id']]);
 335                      }
 336                  }
 337              }
 338              else if ($action == 'edit' && !sizeof($address_list) && !$refresh && !$submit && !$preview)
 339              {
 340                  // Rebuild TO and BCC Header
 341                  $address_list = rebuild_header(array('to' => $post['to_address'], 'bcc' => $post['bcc_address']));
 342              }
 343  
 344              if ($action == 'quotepost')
 345              {
 346                  $check_value = 0;
 347              }
 348              else
 349              {
 350                  $check_value = (($post['enable_bbcode']+1) << 8) + (($post['enable_smilies']+1) << 4) + (($enable_urls+1) << 2) + (($post['enable_sig']+1) << 1);
 351              }
 352          }
 353      }
 354      else
 355      {
 356          $message_attachment = 0;
 357          $message_text = $message_subject = '';
 358  
 359          if ($to_user_id && $to_user_id != ANONYMOUS && $action == 'post')
 360          {
 361              $address_list['u'][$to_user_id] = 'to';
 362          }
 363          else if ($to_group_id && $action == 'post')
 364          {
 365              $address_list['g'][$to_group_id] = 'to';
 366          }
 367          $check_value = 0;
 368      }
 369  
 370      if (($to_group_id || isset($address_list['g'])) && (!$config['allow_mass_pm'] || !$auth->acl_get('u_masspm_group')))
 371      {
 372          trigger_error('NO_AUTH_GROUP_MESSAGE');
 373      }
 374  
 375      if ($action == 'edit' && !$refresh && !$preview && !$submit)
 376      {
 377          if (!($message_time > time() - ($config['pm_edit_time'] * 60) || !$config['pm_edit_time']))
 378          {
 379              trigger_error('CANNOT_EDIT_MESSAGE_TIME');
 380          }
 381      }
 382  
 383      if ($action == 'post')
 384      {
 385          $template->assign_var('S_NEW_MESSAGE', true);
 386      }
 387  
 388      if (!isset($icon_id))
 389      {
 390          $icon_id = 0;
 391      }
 392  
 393      $message_parser = new parse_message();
 394  
 395      $message_parser->message = ($action == 'reply') ? '' : $message_text;
 396      unset($message_text);
 397  
 398      $s_action = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=$mode&amp;action=$action", true, $user->session_id);
 399      $s_action .= (($folder_id) ? "&amp;f=$folder_id" : '') . (($msg_id) ? "&amp;p=$msg_id" : '');
 400  
 401      // Delete triggered ?
 402      if ($action == 'delete')
 403      {
 404          // Folder id has been determined by the SQL Statement
 405          // $folder_id = request_var('f', PRIVMSGS_NO_BOX);
 406  
 407          // Do we need to confirm ?
 408          if (confirm_box(true))
 409          {
 410              delete_pm($user->data['user_id'], $msg_id, $folder_id);
 411  
 412              // jump to next message in "history"? nope, not for the moment. But able to be included later.
 413              $meta_info = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;folder=$folder_id");
 414              $message = $user->lang['MESSAGE_DELETED'];
 415  
 416              meta_refresh(3, $meta_info);
 417              $message .= '<br /><br />' . sprintf($user->lang['RETURN_FOLDER'], '<a href="' . $meta_info . '">', '</a>');
 418              trigger_error($message);
 419          }
 420          else
 421          {
 422              $s_hidden_fields = array(
 423                  'p'            => $msg_id,
 424                  'f'            => $folder_id,
 425                  'action'    => 'delete'
 426              );
 427  
 428              // "{$phpbb_root_path}ucp.$phpEx?i=pm&amp;mode=compose"
 429              confirm_box(false, 'DELETE_MESSAGE', build_hidden_fields($s_hidden_fields));
 430          }
 431  
 432          redirect(append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=view&amp;action=view_message&amp;p=' . $msg_id));
 433      }
 434  
 435      // Get maximum number of allowed recipients
 436      $sql = 'SELECT MAX(g.group_max_recipients) as max_recipients
 437          FROM ' . GROUPS_TABLE . ' g, ' . USER_GROUP_TABLE . ' ug
 438          WHERE ug.user_id = ' . $user->data['user_id'] . '
 439              AND ug.user_pending = 0
 440              AND ug.group_id = g.group_id';
 441      $result = $db->sql_query($sql);
 442      $max_recipients = (int) $db->sql_fetchfield('max_recipients');
 443      $db->sql_freeresult($result);
 444  
 445      $max_recipients = (!$max_recipients) ? $config['pm_max_recipients'] : $max_recipients;
 446  
 447      // If this is a quote/reply "to all"... we may increase the max_recpients to the number of original recipients
 448      if (($action == 'reply' || $action == 'quote') && $max_recipients && $reply_to_all)
 449      {
 450          // We try to include every previously listed member from the TO Header
 451          $list = rebuild_header(array('to' => $post['to_address']));
 452  
 453          // Can be an empty array too ;)
 454          $list = (!empty($list['u'])) ? $list['u'] : array();
 455          $list[$post['author_id']] = 'to';
 456  
 457          if (isset($list[$user->data['user_id']]))
 458          {
 459              unset($list[$user->data['user_id']]);
 460          }
 461  
 462          $max_recipients = ($max_recipients < sizeof($list)) ? sizeof($list) : $max_recipients;
 463  
 464          unset($list);
 465      }
 466  
 467      // Handle User/Group adding/removing
 468      handle_message_list_actions($address_list, $error, $remove_u, $remove_g, $add_to, $add_bcc);
 469  
 470      // Check mass pm to group permission
 471      if ((!$config['allow_mass_pm'] || !$auth->acl_get('u_masspm_group')) && !empty($address_list['g']))
 472      {
 473          $address_list = array();
 474          $error[] = $user->lang['NO_AUTH_GROUP_MESSAGE'];
 475      }
 476  
 477      // Check mass pm to users permission
 478      if ((!$config['allow_mass_pm'] || !$auth->acl_get('u_masspm')) && num_recipients($address_list) > 1)
 479      {
 480          $address_list = get_recipients($address_list, 1);
 481          $error[] = $user->lang('TOO_MANY_RECIPIENTS', 1);
 482      }
 483  
 484      // Check for too many recipients
 485      if (!empty($address_list['u']) && $max_recipients && sizeof($address_list['u']) > $max_recipients)
 486      {
 487          $address_list = get_recipients($address_list, $max_recipients);
 488          $error[] = $user->lang('TOO_MANY_RECIPIENTS', $max_recipients);
 489      }
 490  
 491      // Always check if the submitted attachment data is valid and belongs to the user.
 492      // Further down (especially in submit_post()) we do not check this again.
 493      $message_parser->get_submitted_attachment_data();
 494  
 495      if ($message_attachment && !$submit && !$refresh && !$preview && $action == 'edit')
 496      {
 497          // Do not change to SELECT *
 498          $sql = 'SELECT attach_id, is_orphan, attach_comment, real_filename
 499              FROM ' . ATTACHMENTS_TABLE . "
 500              WHERE post_msg_id = $msg_id
 501                  AND in_message = 1
 502                  AND is_orphan = 0
 503              ORDER BY filetime DESC";
 504          $result = $db->sql_query($sql);
 505          $message_parser->attachment_data = array_merge($message_parser->attachment_data, $db->sql_fetchrowset($result));
 506          $db->sql_freeresult($result);
 507      }
 508  
 509      if (!in_array($action, array('quote', 'edit', 'delete', 'forward')))
 510      {
 511          $enable_sig        = ($config['allow_sig'] && $config['allow_sig_pm'] && $auth->acl_get('u_sig') && $user->optionget('attachsig'));
 512          $enable_smilies    = ($config['allow_smilies'] && $auth->acl_get('u_pm_smilies') && $user->optionget('smilies'));
 513          $enable_bbcode    = ($config['allow_bbcode'] && $auth->acl_get('u_pm_bbcode') && $user->optionget('bbcode'));
 514          $enable_urls    = true;
 515      }
 516  
 517      $enable_magic_url = $drafts = false;
 518  
 519      // User own some drafts?
 520      if ($auth->acl_get('u_savedrafts') && $action != 'delete')
 521      {
 522          $sql = 'SELECT draft_id
 523              FROM ' . DRAFTS_TABLE . '
 524              WHERE forum_id = 0
 525                  AND topic_id = 0
 526                  AND user_id = ' . $user->data['user_id'] .
 527                  (($draft_id) ? " AND draft_id <> $draft_id" : '');
 528          $result = $db->sql_query_limit($sql, 1);
 529          $row = $db->sql_fetchrow($result);
 530          $db->sql_freeresult($result);
 531  
 532          if ($row)
 533          {
 534              $drafts = true;
 535          }
 536      }
 537  
 538      if ($action == 'edit')
 539      {
 540          $message_parser->bbcode_uid = $bbcode_uid;
 541      }
 542  
 543      $bbcode_status    = ($config['allow_bbcode'] && $config['auth_bbcode_pm'] && $auth->acl_get('u_pm_bbcode')) ? true : false;
 544      $smilies_status    = ($config['allow_smilies'] && $config['auth_smilies_pm'] && $auth->acl_get('u_pm_smilies')) ? true : false;
 545      $img_status        = ($config['auth_img_pm'] && $auth->acl_get('u_pm_img')) ? true : false;
 546      $flash_status    = ($config['auth_flash_pm'] && $auth->acl_get('u_pm_flash')) ? true : false;
 547      $url_status        = ($config['allow_post_links']) ? true : false;
 548  
 549      // Save Draft
 550      if ($save && $auth->acl_get('u_savedrafts'))
 551      {
 552          $subject = utf8_normalize_nfc(request_var('subject', '', true));
 553          $subject = (!$subject && $action != 'post') ? $user->lang['NEW_MESSAGE'] : $subject;
 554          $message = utf8_normalize_nfc(request_var('message', '', true));
 555  
 556          if ($subject && $message)
 557          {
 558              if (confirm_box(true))
 559              {
 560                  $sql = 'INSERT INTO ' . DRAFTS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
 561                      'user_id'        => $user->data['user_id'],
 562                      'topic_id'        => 0,
 563                      'forum_id'        => 0,
 564                      'save_time'        => $current_time,
 565                      'draft_subject'    => $subject,
 566                      'draft_message'    => $message
 567                      )
 568                  );
 569                  $db->sql_query($sql);
 570  
 571                  $redirect_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=pm&amp;mode=$mode");
 572  
 573                  meta_refresh(3, $redirect_url);
 574                  $message = $user->lang['DRAFT_SAVED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $redirect_url . '">', '</a>');
 575  
 576                  trigger_error($message);
 577              }
 578              else
 579              {
 580                  $s_hidden_fields = build_hidden_fields(array(
 581                      'mode'        => $mode,
 582                      'action'    => $action,
 583                      'save'        => true,
 584                      'subject'    => $subject,
 585                      'message'    => $message,
 586                      'u'            => $to_user_id,
 587                      'g'            => $to_group_id,
 588                      'p'            => $msg_id)
 589                  );
 590                  $s_hidden_fields .= build_address_field($address_list);
 591  
 592  
 593                  confirm_box(false, 'SAVE_DRAFT', $s_hidden_fields);
 594              }
 595          }
 596          else
 597          {
 598              if (utf8_clean_string($subject) === '')
 599              {
 600                  $error[] = $user->lang['EMPTY_MESSAGE_SUBJECT'];
 601              }
 602  
 603              if (utf8_clean_string($message) === '')
 604              {
 605                  $error[] = $user->lang['TOO_FEW_CHARS'];
 606              }
 607          }
 608  
 609          unset($subject, $message);
 610      }
 611  
 612      // Load Draft
 613      if ($draft_id && $auth->acl_get('u_savedrafts'))
 614      {
 615          $sql = 'SELECT draft_subject, draft_message
 616              FROM ' . DRAFTS_TABLE . "
 617              WHERE draft_id = $draft_id
 618                  AND topic_id = 0
 619                  AND forum_id = 0
 620                  AND user_id = " . $user->data['user_id'];
 621          $result = $db->sql_query_limit($sql, 1);
 622  
 623          if ($row = $db->sql_fetchrow($result))
 624          {
 625              $message_parser->message = $row['draft_message'];
 626              $message_subject = $row['draft_subject'];
 627  
 628              $template->assign_var('S_DRAFT_LOADED', true);
 629          }
 630          else
 631          {
 632              $draft_id = 0;
 633          }
 634          $db->sql_freeresult($result);
 635      }
 636  
 637      // Load Drafts
 638      if ($load && $drafts)
 639      {
 640          load_drafts(0, 0, $id, $action, $msg_id);
 641      }
 642  
 643      if ($submit || $preview || $refresh)
 644      {
 645          if (($submit || $preview) && !check_form_key('ucp_pm_compose'))
 646          {
 647              $error[] = $user->lang['FORM_INVALID'];
 648          }
 649          $subject = utf8_normalize_nfc(request_var('subject', '', true));
 650          $message_parser->message = utf8_normalize_nfc(request_var('message', '', true));
 651  
 652          $icon_id            = request_var('icon', 0);
 653  
 654          $enable_bbcode         = (!$bbcode_status || isset($_POST['disable_bbcode'])) ? false : true;
 655          $enable_smilies        = (!$smilies_status || isset($_POST['disable_smilies'])) ? false : true;
 656          $enable_urls         = (isset($_POST['disable_magic_url'])) ? 0 : 1;
 657          $enable_sig            = (!$config['allow_sig'] ||!$config['allow_sig_pm']) ? false : ((isset($_POST['attach_sig'])) ? true : false);
 658  
 659          if ($submit)
 660          {
 661              $status_switch    = (($enable_bbcode+1) << 8) + (($enable_smilies+1) << 4) + (($enable_urls+1) << 2) + (($enable_sig+1) << 1);
 662              $status_switch = ($status_switch != $check_value);
 663          }
 664          else
 665          {
 666              $status_switch = 1;
 667          }
 668  
 669          // Parse Attachments - before checksum is calculated
 670          $message_parser->parse_attachments('fileupload', $action, 0, $submit, $preview, $refresh, true);
 671  
 672          if (sizeof($message_parser->warn_msg) && !($remove_u || $remove_g || $add_to || $add_bcc))
 673          {
 674              $error[] = implode('<br />', $message_parser->warn_msg);
 675              $message_parser->warn_msg = array();
 676          }
 677  
 678          // Parse message
 679          $message_parser->parse($enable_bbcode, ($config['allow_post_links']) ? $enable_urls : false, $enable_smilies, $img_status, $flash_status, true, $config['allow_post_links']);
 680  
 681          // On a refresh we do not care about message parsing errors
 682          if (sizeof($message_parser->warn_msg) && !$refresh)
 683          {
 684              $error[] = implode('<br />', $message_parser->warn_msg);
 685          }
 686  
 687          if ($action != 'edit' && !$preview && !$refresh && $config['flood_interval'] && !$auth->acl_get('u_ignoreflood'))
 688          {
 689              // Flood check
 690              $last_post_time = $user->data['user_lastpost_time'];
 691  
 692              if ($last_post_time)
 693              {
 694                  if ($last_post_time && ($current_time - $last_post_time) < intval($config['flood_interval']))
 695                  {
 696                      $error[] = $user->lang['FLOOD_ERROR'];
 697                  }
 698              }
 699          }
 700  
 701          // Subject defined
 702          if ($submit)
 703          {
 704              if (utf8_clean_string($subject) === '')
 705              {
 706                  $error[] = $user->lang['EMPTY_MESSAGE_SUBJECT'];
 707              }
 708  
 709              if (!sizeof($address_list))
 710              {
 711                  $error[] = $user->lang['NO_RECIPIENT'];
 712              }
 713          }
 714  
 715          // Store message, sync counters
 716          if (!sizeof($error) && $submit)
 717          {
 718              $pm_data = array(
 719                  'msg_id'                => (int) $msg_id,
 720                  'from_user_id'            => $user->data['user_id'],
 721                  'from_user_ip'            => $user->ip,
 722                  'from_username'            => $user->data['username'],
 723                  'reply_from_root_level'    => (isset($post['root_level'])) ? (int) $post['root_level'] : 0,
 724                  'reply_from_msg_id'        => (int) $msg_id,
 725                  'icon_id'                => (int) $icon_id,
 726                  'enable_sig'            => (bool) $enable_sig,
 727                  'enable_bbcode'            => (bool) $enable_bbcode,
 728                  'enable_smilies'        => (bool) $enable_smilies,
 729                  'enable_urls'            => (bool) $enable_urls,
 730                  'bbcode_bitfield'        => $message_parser->bbcode_bitfield,
 731                  'bbcode_uid'            => $message_parser->bbcode_uid,
 732                  'message'                => $message_parser->message,
 733                  'attachment_data'        => $message_parser->attachment_data,
 734                  'filename_data'            => $message_parser->filename_data,
 735                  'address_list'            => $address_list
 736              );
 737  
 738              // ((!$message_subject) ? $subject : $message_subject)
 739              $msg_id = submit_pm($action, $subject, $pm_data);
 740  
 741              $return_message_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;mode=view&amp;p=' . $msg_id);
 742              $inbox_folder_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=inbox');
 743              $outbox_folder_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=outbox');
 744  
 745              $folder_url = '';
 746              if (($folder_id > 0) && isset($user_folders[$folder_id]))
 747              {
 748                  $folder_url = append_sid("{$phpbb_root_path}ucp.$phpEx", 'i=pm&amp;folder=' . $folder_id);
 749              }
 750  
 751              $return_box_url = ($action === 'post' || $action === 'edit') ? $outbox_folder_url : $inbox_folder_url;
 752              $return_box_lang = ($action === 'post' || $action === 'edit') ? 'PM_OUTBOX' : 'PM_INBOX';
 753  
 754  
 755              $save_message = ($action === 'edit') ? $user->lang['MESSAGE_EDITED'] : $user->lang['MESSAGE_STORED'];
 756              $message = $save_message . '<br /><br />' . $user->lang('VIEW_PRIVATE_MESSAGE', '<a href="' . $return_message_url . '">', '</a>');
 757  
 758              $last_click_type = 'CLICK_RETURN_FOLDER';
 759              if ($folder_url)
 760              {
 761                  $message .= '<br /><br />' . sprintf($user->lang['CLICK_RETURN_FOLDER'], '<a href="' . $folder_url . '">', '</a>', $user_folders[$folder_id]['folder_name']);
 762                  $last_click_type = 'CLICK_GOTO_FOLDER';
 763              }
 764              $message .= '<br /><br />' . sprintf($user->lang[$last_click_type], '<a href="' . $return_box_url . '">', '</a>', $user->lang[$return_box_lang]);
 765  
 766              meta_refresh(3, $return_message_url);
 767              trigger_error($message);
 768          }
 769  
 770          $message_subject = $subject;
 771      }
 772  
 773      // Preview
 774      if (!sizeof($error) && $preview)
 775      {
 776          $preview_message = $message_parser->format_display($enable_bbcode, $enable_urls, $enable_smilies, false);
 777  
 778          $preview_signature = $user->data['user_sig'];
 779          $preview_signature_uid = $user->data['user_sig_bbcode_uid'];
 780          $preview_signature_bitfield = $user->data['user_sig_bbcode_bitfield'];
 781  
 782          // Signature
 783          if ($enable_sig && $config['allow_sig'] && $preview_signature)
 784          {
 785              $parse_sig = new parse_message($preview_signature);
 786              $parse_sig->bbcode_uid = $preview_signature_uid;
 787              $parse_sig->bbcode_bitfield = $preview_signature_bitfield;
 788  
 789              $parse_sig->format_display($config['allow_sig_bbcode'], $config['allow_sig_links'], $config['allow_sig_smilies']);
 790              $preview_signature = $parse_sig->message;
 791              unset($parse_sig);
 792          }
 793          else
 794          {
 795              $preview_signature = '';
 796          }
 797  
 798          // Attachment Preview
 799          if (sizeof($message_parser->attachment_data))
 800          {
 801              $template->assign_var('S_HAS_ATTACHMENTS', true);
 802  
 803              $update_count = array();
 804              $attachment_data = $message_parser->attachment_data;
 805  
 806              parse_attachments(false, $preview_message, $attachment_data, $update_count, true);
 807  
 808              foreach ($attachment_data as $i => $attachment)
 809              {
 810                  $template->assign_block_vars('attachment', array(
 811                      'DISPLAY_ATTACHMENT'    => $attachment)
 812                  );
 813              }
 814              unset($attachment_data);
 815          }
 816  
 817          $preview_subject = censor_text($subject);
 818  
 819          if (!sizeof($error))
 820          {
 821              $template->assign_vars(array(
 822                  'PREVIEW_SUBJECT'        => $preview_subject,
 823                  'PREVIEW_MESSAGE'        => $preview_message,
 824                  'PREVIEW_SIGNATURE'        => $preview_signature,
 825  
 826                  'S_DISPLAY_PREVIEW'        => true)
 827              );
 828          }
 829          unset($message_text);
 830      }
 831  
 832      // Decode text for message display
 833      $bbcode_uid = (($action == 'quote' || $action == 'forward') && !$preview && !$refresh && (!sizeof($error) || (sizeof($error) && !$submit))) ? $bbcode_uid : $message_parser->bbcode_uid;
 834  
 835      $message_parser->decode_message($bbcode_uid);
 836  
 837      if (($action == 'quote' || $action == 'quotepost') && !$preview && !$refresh && !$submit)
 838      {
 839          if ($action == 'quotepost')
 840          {
 841              $post_id = request_var('p', 0);
 842              if ($config['allow_post_links'])
 843              {
 844                  $message_link = "[url=" . generate_board_url() . "/viewtopic.$phpEx?p={$post_id}#p{$post_id}]{$user->lang['SUBJECT']}: {$message_subject}[/url]\n\n";
 845              }
 846              else
 847              {
 848                  $message_link = $user->lang['SUBJECT'] . ': ' . $message_subject . " (" . generate_board_url() . "/viewtopic.$phpEx?p={$post_id}#p{$post_id})\n\n";
 849              }
 850          }
 851          else
 852          {
 853              $message_link = '';
 854          }
 855          $message_parser->message = $message_link . '[quote=&quot;' . $quote_username . '&quot;]' . censor_text(trim($message_parser->message)) . "[/quote]\n";
 856      }
 857  
 858      if (($action == 'reply' || $action == 'quote' || $action == 'quotepost') && !$preview && !$refresh)
 859      {
 860          $message_subject = ((!preg_match('/^Re:/', $message_subject)) ? 'Re: ' : '') . censor_text($message_subject);
 861      }
 862  
 863      if ($action == 'forward' && !$preview && !$refresh && !$submit)
 864      {
 865          $fwd_to_field = write_pm_addresses(array('to' => $post['to_address']), 0, true);
 866  
 867          if ($config['allow_post_links'])
 868          {
 869              $quote_username_text = '[url=' . generate_board_url() . "/memberlist.$phpEx?mode=viewprofile&amp;u={$post['author_id']}]{$quote_username}[/url]";
 870          }
 871          else
 872          {
 873              $quote_username_text = $quote_username . ' (' . generate_board_url() . "/memberlist.$phpEx?mode=viewprofile&amp;u={$post['author_id']})";
 874          }
 875  
 876          $forward_text = array();
 877          $forward_text[] = $user->lang['FWD_ORIGINAL_MESSAGE'];
 878          $forward_text[] = sprintf($user->lang['FWD_SUBJECT'], censor_text($message_subject));
 879          $forward_text[] = sprintf($user->lang['FWD_DATE'], $user->format_date($message_time, false, true));
 880          $forward_text[] = sprintf($user->lang['FWD_FROM'], $quote_username_text);
 881          $forward_text[] = sprintf($user->lang['FWD_TO'], implode(', ', $fwd_to_field['to']));
 882  
 883          $message_parser->message = implode("\n", $forward_text) . "\n\n[quote=&quot;{$quote_username}&quot;]\n" . censor_text(trim($message_parser->message)) . "\n[/quote]";
 884          $message_subject = ((!preg_match('/^Fwd:/', $message_subject)) ? 'Fwd: ' : '') . censor_text($message_subject);
 885      }
 886  
 887      $attachment_data = $message_parser->attachment_data;
 888      $filename_data = $message_parser->filename_data;
 889      $message_text = $message_parser->message;
 890  
 891      // MAIN PM PAGE BEGINS HERE
 892  
 893      // Generate smiley listing
 894      generate_smilies('inline', 0);
 895  
 896      // Generate PM Icons
 897      $s_pm_icons = false;
 898      if ($config['enable_pm_icons'])
 899      {
 900          $s_pm_icons = posting_gen_topic_icons($action, $icon_id);
 901      }
 902  
 903      // Generate inline attachment select box
 904      posting_gen_inline_attachments($attachment_data);
 905  
 906      // Build address list for display
 907      // array('u' => array($author_id => 'to'));
 908      if (sizeof($address_list))
 909      {
 910          // Get Usernames and Group Names
 911          $result = array();
 912          if (!empty($address_list['u']))
 913          {
 914              $sql = 'SELECT user_id as id, username as name, user_colour as colour
 915                  FROM ' . USERS_TABLE . '
 916                  WHERE ' . $db->sql_in_set('user_id', array_map('intval', array_keys($address_list['u']))) . '
 917                  ORDER BY username_clean ASC';
 918              $result['u'] = $db->sql_query($sql);
 919          }
 920  
 921          if (!empty($address_list['g']))
 922          {
 923              $sql = 'SELECT g.group_id AS id, g.group_name AS name, g.group_colour AS colour, g.group_type
 924                  FROM ' . GROUPS_TABLE . ' g';
 925  
 926              if (!$auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
 927              {
 928                  $sql .= ' LEFT JOIN ' . USER_GROUP_TABLE . ' ug
 929                      ON (
 930                          g.group_id = ug.group_id
 931                          AND ug.user_id = ' . $user->data['user_id'] . '
 932                          AND ug.user_pending = 0
 933                      )
 934                      WHERE (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ')';
 935              }
 936  
 937              $sql .= ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) ? ' WHERE ' : ' AND ';
 938  
 939              $sql .= 'g.group_receive_pm = 1
 940                  AND ' . $db->sql_in_set('g.group_id', array_map('intval', array_keys($address_list['g']))) . '
 941                  ORDER BY g.group_name ASC';
 942  
 943              $result['g'] = $db->sql_query($sql);
 944          }
 945  
 946          $u = $g = array();
 947          $_types = array('u', 'g');
 948          foreach ($_types as $type)
 949          {
 950              if (isset($result[$type]) && $result[$type])
 951              {
 952                  while ($row = $db->sql_fetchrow($result[$type]))
 953                  {
 954                      if ($type == 'g')
 955                      {
 956                          $row['name'] = ($row['group_type'] == GROUP_SPECIAL) ? $user->lang['G_' . $row['name']] : $row['name'];
 957                      }
 958  
 959                      ${$type}[$row['id']] = array('name' => $row['name'], 'colour' => $row['colour']);
 960                  }
 961                  $db->sql_freeresult($result[$type]);
 962              }
 963          }
 964  
 965          // Now Build the address list
 966          $plain_address_field = '';
 967          foreach ($address_list as $type => $adr_ary)
 968          {
 969              foreach ($adr_ary as $id => $field)
 970              {
 971                  if (!isset(${$type}[$id]))
 972                  {
 973                      unset($address_list[$type][$id]);
 974                      continue;
 975                  }
 976  
 977                  $field = ($field == 'to') ? 'to' : 'bcc';
 978                  $type = ($type == 'u') ? 'u' : 'g';
 979                  $id = (int) $id;
 980  
 981                  $tpl_ary = array(
 982                      'IS_GROUP'    => ($type == 'g') ? true : false,
 983                      'IS_USER'    => ($type == 'u') ? true : false,
 984                      'UG_ID'        => $id,
 985                      'NAME'        => ${$type}[$id]['name'],
 986                      'COLOUR'    => (${$type}[$id]['colour']) ? '#' . ${$type}[$id]['colour'] : '',
 987                      'TYPE'        => $type,
 988                  );
 989  
 990                  if ($type == 'u')
 991                  {
 992                      $tpl_ary = array_merge($tpl_ary, array(
 993                          'U_VIEW'        => get_username_string('profile', $id, ${$type}[$id]['name'], ${$type}[$id]['colour']),
 994                          'NAME_FULL'        => get_username_string('full', $id, ${$type}[$id]['name'], ${$type}[$id]['colour']),
 995                      ));
 996                  }
 997                  else
 998                  {
 999                      $tpl_ary = array_merge($tpl_ary, array(
1000                          'U_VIEW'        => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=group&amp;g=' . $id),
1001                      ));
1002                  }
1003  
1004                  $template->assign_block_vars($field . '_recipient', $tpl_ary);
1005              }
1006          }
1007      }
1008  
1009      // Build hidden address list
1010      $s_hidden_address_field = build_address_field($address_list);
1011  
1012  
1013      $bbcode_checked        = (isset($enable_bbcode)) ? !$enable_bbcode : (($config['allow_bbcode'] && $auth->acl_get('u_pm_bbcode')) ? !$user->optionget('bbcode') : 1);
1014      $smilies_checked    = (isset($enable_smilies)) ? !$enable_smilies : (($config['allow_smilies'] && $auth->acl_get('u_pm_smilies')) ? !$user->optionget('smilies') : 1);
1015      $urls_checked        = (isset($enable_urls)) ? !$enable_urls : 0;
1016      $sig_checked        = $enable_sig;
1017  
1018      switch ($action)
1019      {
1020          case 'post':
1021              $page_title = $user->lang['POST_NEW_PM'];
1022          break;
1023  
1024          case 'quote':
1025              $page_title = $user->lang['POST_QUOTE_PM'];
1026          break;
1027  
1028          case 'quotepost':
1029              $page_title = $user->lang['POST_PM_POST'];
1030          break;
1031  
1032          case 'reply':
1033              $page_title = $user->lang['POST_REPLY_PM'];
1034          break;
1035  
1036          case 'edit':
1037              $page_title = $user->lang['POST_EDIT_PM'];
1038          break;
1039  
1040          case 'forward':
1041              $page_title = $user->lang['POST_FORWARD_PM'];
1042          break;
1043  
1044          default:
1045              trigger_error('NO_ACTION_MODE', E_USER_ERROR);
1046          break;
1047      }
1048  
1049      $s_hidden_fields = '<input type="hidden" name="lastclick" value="' . $current_time . '" />';
1050      $s_hidden_fields .= (isset($check_value)) ? '<input type="hidden" name="status_switch" value="' . $check_value . '" />' : '';
1051      $s_hidden_fields .= ($draft_id || isset($_REQUEST['draft_loaded'])) ? '<input type="hidden" name="draft_loaded" value="' . ((isset($_REQUEST['draft_loaded'])) ? intval($_REQUEST['draft_loaded']) : $draft_id) . '" />' : '';
1052  
1053      $form_enctype = (@ini_get('file_uploads') == '0' || strtolower(@ini_get('file_uploads')) == 'off' || !$config['allow_pm_attach'] || !$auth->acl_get('u_pm_attach')) ? '' : ' enctype="multipart/form-data"';
1054  
1055      // Start assigning vars for main posting page ...
1056      $template->assign_vars(array(
1057          'L_POST_A'                    => $page_title,
1058          'L_ICON'                    => $user->lang['PM_ICON'],
1059          'L_MESSAGE_BODY_EXPLAIN'    => (intval($config['max_post_chars'])) ? sprintf($user->lang['MESSAGE_BODY_EXPLAIN'], intval($config['max_post_chars'])) : '',
1060  
1061          'SUBJECT'                => (isset($message_subject)) ? $message_subject : '',
1062          'MESSAGE'                => $message_text,
1063          'BBCODE_STATUS'            => ($bbcode_status) ? sprintf($user->lang['BBCODE_IS_ON'], '<a href="' . append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode') . '">', '</a>') : sprintf($user->lang['BBCODE_IS_OFF'], '<a href="' . append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode') . '">', '</a>'),
1064          'IMG_STATUS'            => ($img_status) ? $user->lang['IMAGES_ARE_ON'] : $user->lang['IMAGES_ARE_OFF'],
1065          'FLASH_STATUS'            => ($flash_status) ? $user->lang['FLASH_IS_ON'] : $user->lang['FLASH_IS_OFF'],
1066          'SMILIES_STATUS'        => ($smilies_status) ? $user->lang['SMILIES_ARE_ON'] : $user->lang['SMILIES_ARE_OFF'],
1067          'URL_STATUS'            => ($url_status) ? $user->lang['URL_IS_ON'] : $user->lang['URL_IS_OFF'],
1068          'MAX_FONT_SIZE'            => (int) $config['max_post_font_size'],
1069          'MINI_POST_IMG'            => $user->img('icon_post_target', $user->lang['PM']),
1070          'ERROR'                    => (sizeof($error)) ? implode('<br />', $error) : '',
1071          'MAX_RECIPIENTS'        => ($config['allow_mass_pm'] && ($auth->acl_get('u_masspm') || $auth->acl_get('u_masspm_group'))) ? $max_recipients : 0,
1072  
1073          'S_COMPOSE_PM'            => true,
1074          'S_EDIT_POST'            => ($action == 'edit'),
1075          'S_SHOW_PM_ICONS'        => $s_pm_icons,
1076          'S_BBCODE_ALLOWED'        => ($bbcode_status) ? 1 : 0,
1077          'S_BBCODE_CHECKED'        => ($bbcode_checked) ? ' checked="checked"' : '',
1078          'S_SMILIES_ALLOWED'        => $smilies_status,
1079          'S_SMILIES_CHECKED'        => ($smilies_checked) ? ' checked="checked"' : '',
1080          'S_SIG_ALLOWED'            => ($config['allow_sig'] && $config['allow_sig_pm'] && $auth->acl_get('u_sig')),
1081          'S_SIGNATURE_CHECKED'    => ($sig_checked) ? ' checked="checked"' : '',
1082          'S_LINKS_ALLOWED'        => $url_status,
1083          'S_MAGIC_URL_CHECKED'    => ($urls_checked) ? ' checked="checked"' : '',
1084          'S_SAVE_ALLOWED'        => ($auth->acl_get('u_savedrafts') && $action != 'edit') ? true : false,
1085          'S_HAS_DRAFTS'            => ($auth->acl_get('u_savedrafts') && $drafts),
1086          'S_FORM_ENCTYPE'        => $form_enctype,
1087  
1088          'S_BBCODE_IMG'            => $img_status,
1089          'S_BBCODE_FLASH'        => $flash_status,
1090          'S_BBCODE_QUOTE'        => true,
1091          'S_BBCODE_URL'            => $url_status,
1092  
1093          'S_POST_ACTION'                => $s_action,
1094          'S_HIDDEN_ADDRESS_FIELD'    => $s_hidden_address_field,
1095          'S_HIDDEN_FIELDS'            => $s_hidden_fields,
1096  
1097          'S_CLOSE_PROGRESS_WINDOW'    => isset($_POST['add_file']),
1098          'U_PROGRESS_BAR'            => append_sid("{$phpbb_root_path}posting.$phpEx", 'f=0&amp;mode=popup'),
1099          'UA_PROGRESS_BAR'            => addslashes(append_sid("{$phpbb_root_path}posting.$phpEx", 'f=0&amp;mode=popup')),
1100      ));
1101  
1102      // Build custom bbcodes array
1103      display_custom_bbcodes();
1104  
1105      // Show attachment box for adding attachments if true
1106      $allowed = ($auth->acl_get('u_pm_attach') && $config['allow_pm_attach'] && $form_enctype);
1107  
1108      // Attachment entry
1109      posting_gen_attachment_entry($attachment_data, $filename_data, $allowed);
1110  
1111      // Message History
1112      if ($action == 'reply' || $action == 'quote' || $action == 'forward')
1113      {
1114          if (message_history($msg_id, $user->data['user_id'], $post, array(), true))
1115          {
1116              $template->assign_var('S_DISPLAY_HISTORY', true);
1117          }
1118      }
1119  }
1120  
1121  /**
1122  * For composing messages, handle list actions
1123  */
1124  function handle_message_list_actions(&$address_list, &$error, $remove_u, $remove_g, $add_to, $add_bcc)
1125  {
1126      global $auth, $db, $user;
1127  
1128      // Delete User [TO/BCC]
1129      if ($remove_u && !empty($_REQUEST['remove_u']) && is_array($_REQUEST['remove_u']))
1130      {
1131          $remove_user_id = array_keys($_REQUEST['remove_u']);
1132  
1133          if (isset($remove_user_id[0]))
1134          {
1135              unset($address_list['u'][(int) $remove_user_id[0]]);
1136          }
1137      }
1138  
1139      // Delete Group [TO/BCC]
1140      if ($remove_g && !empty($_REQUEST['remove_g']) && is_array($_REQUEST['remove_g']))
1141      {
1142          $remove_group_id = array_keys($_REQUEST['remove_g']);
1143  
1144          if (isset($remove_group_id[0]))
1145          {
1146              unset($address_list['g'][(int) $remove_group_id[0]]);
1147          }
1148      }
1149  
1150      // Add Selected Groups
1151      $group_list = request_var('group_list', array(0));
1152  
1153      // Build usernames to add
1154      $usernames = request_var('username', '', true);
1155      $usernames = (empty($usernames)) ? array() : array($usernames);
1156  
1157      $username_list = request_var('username_list', '', true);
1158      if ($username_list)
1159      {
1160          $usernames = array_merge($usernames, explode("\n", $username_list));
1161      }
1162  
1163      // If add to or add bcc not pressed, users could still have usernames listed they want to add...
1164      if (!$add_to && !$add_bcc && (sizeof($group_list) || sizeof($usernames)))
1165      {
1166          $add_to = true;
1167  
1168          global $refresh, $submit, $preview;
1169  
1170          $refresh = true;
1171          $submit = false;
1172  
1173          // Preview is only true if there was also a message entered
1174          if (request_var('message', ''))
1175          {
1176              $preview = true;
1177          }
1178      }
1179  
1180      // Add User/Group [TO]
1181      if ($add_to || $add_bcc)
1182      {
1183          $type = ($add_to) ? 'to' : 'bcc';
1184  
1185          if (sizeof($group_list))
1186          {
1187              foreach ($group_list as $group_id)
1188              {
1189                  $address_list['g'][$group_id] = $type;
1190              }
1191          }
1192  
1193          // User ID's to add...
1194          $user_id_ary = array();
1195  
1196          // Reveal the correct user_ids
1197          if (sizeof($usernames))
1198          {
1199              $user_id_ary = array();
1200              user_get_id_name($user_id_ary, $usernames, array(USER_NORMAL, USER_FOUNDER, USER_INACTIVE));
1201  
1202              // If there are users not existing, we will at least print a notice...
1203              if (!sizeof($user_id_ary))
1204              {
1205                  $error[] = $user->lang['PM_NO_USERS'];
1206              }
1207          }
1208  
1209          // Add Friends if specified
1210          $friend_list = (isset($_REQUEST['add_' . $type]) && is_array($_REQUEST['add_' . $type])) ? array_map('intval', array_keys($_REQUEST['add_' . $type])) : array();
1211          $user_id_ary = array_merge($user_id_ary, $friend_list);
1212  
1213          foreach ($user_id_ary as $user_id)
1214          {
1215              if ($user_id == ANONYMOUS)
1216              {
1217                  continue;
1218              }
1219  
1220              $address_list['u'][$user_id] = $type;
1221          }
1222      }
1223  
1224      // Check for disallowed recipients
1225      if (!empty($address_list['u']))
1226      {
1227          // We need to check their PM status (do they want to receive PM's?)
1228          // Only check if not a moderator or admin, since they are allowed to override this user setting
1229          if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
1230          {
1231              $sql = 'SELECT user_id
1232                  FROM ' . USERS_TABLE . '
1233                  WHERE ' . $db->sql_in_set('user_id', array_keys($address_list['u'])) . '
1234                      AND user_allow_pm = 0';
1235              $result = $db->sql_query($sql);
1236  
1237              $removed = false;
1238              while ($row = $db->sql_fetchrow($result))
1239              {
1240                  $removed = true;
1241                  unset($address_list['u'][$row['user_id']]);
1242              }
1243              $db->sql_freeresult($result);
1244  
1245              // print a notice about users not being added who do not want to receive pms
1246              if ($removed)
1247              {
1248                  $error[] = $user->lang['PM_USERS_REMOVED_NO_PM'];
1249              }
1250          }
1251      }
1252  }
1253  
1254  /**
1255  * Build the hidden field for the recipients. Needed, as the variable is not read via request_var.
1256  */
1257  function build_address_field($address_list)
1258  {
1259      $s_hidden_address_field = '';
1260      foreach ($address_list as $type => $adr_ary)
1261      {
1262          foreach ($adr_ary as $id => $field)
1263          {
1264              $s_hidden_address_field .= '<input type="hidden" name="address_list[' . (($type == 'u') ? 'u' : 'g') . '][' . (int) $id . ']" value="' . (($field == 'to') ? 'to' : 'bcc') . '" />';
1265          }
1266      }
1267      return $s_hidden_address_field;
1268  }
1269  
1270  /**
1271  * Return number of private message recipients
1272  */
1273  function num_recipients($address_list)
1274  {
1275      $num_recipients = 0;
1276  
1277      foreach ($address_list as $field => $adr_ary)
1278      {
1279          $num_recipients += sizeof($adr_ary);
1280      }
1281  
1282      return $num_recipients;
1283  }
1284  
1285  /**
1286  * Get number of 'num_recipients' recipients from first position
1287  */
1288  function get_recipients($address_list, $num_recipients = 1)
1289  {
1290      $recipient = array();
1291  
1292      $count = 0;
1293      foreach ($address_list as $field => $adr_ary)
1294      {
1295          foreach ($adr_ary as $id => $type)
1296          {
1297              if ($count >= $num_recipients)
1298              {
1299                  break 2;
1300              }
1301              $recipient[$field][$id] = $type;
1302              $count++;
1303          }
1304      }
1305  
1306      return $recipient;
1307  }
1308  
1309  ?>


Generated: Wed Oct 2 15:03:47 2013 Cross-referenced by PHPXref 0.7.1