[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/ -> functions_posting.php (source)

   1  <?php
   2  /** 
   3  *
   4  * @package phpBB3
   5  * @version $Id: functions_posting.php,v 1.200 2006/11/12 15:35:43 acydburn Exp $
   6  * @copyright (c) 2005 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * Fill smiley templates (or just the variables) with smileys, either in a window or inline
  13  */
  14  function generate_smilies($mode, $forum_id)
  15  {
  16      global $auth, $db, $user, $config, $template;
  17      global $phpEx, $phpbb_root_path;
  18  
  19      if ($mode == 'window')
  20      {
  21          if ($forum_id)
  22          {
  23              $sql = 'SELECT forum_style
  24                  FROM ' . FORUMS_TABLE . "
  25                  WHERE forum_id = $forum_id";
  26              $result = $db->sql_query_limit($sql, 1);
  27              $row = $db->sql_fetchrow($result);
  28              $db->sql_freeresult($result);
  29          
  30              $user->setup('posting', (int) $row['forum_style']);
  31          }
  32          else
  33          {
  34              $user->setup('posting');
  35          }
  36  
  37          page_header($user->lang['SMILIES']);
  38  
  39          $template->set_filenames(array(
  40              'body' => 'posting_smilies.html')
  41          );
  42      }
  43  
  44      $display_link = false;
  45      if ($mode == 'inline')
  46      {
  47          $sql = 'SELECT smiley_id
  48              FROM ' . SMILIES_TABLE . '
  49              WHERE display_on_posting = 0';
  50          $result = $db->sql_query_limit($sql, 1, 0, 3600);
  51  
  52          if ($row = $db->sql_fetchrow($result))
  53          {
  54              $display_link = true;
  55          }
  56          $db->sql_freeresult($result);
  57      }
  58  
  59      $last_url = '';
  60  
  61      $sql = 'SELECT *
  62          FROM ' . SMILIES_TABLE . 
  63          (($mode == 'inline') ? ' WHERE display_on_posting = 1 ' : '') . '
  64          ORDER BY smiley_order';
  65      $result = $db->sql_query($sql, 3600);
  66  
  67      while ($row = $db->sql_fetchrow($result))
  68      {
  69          if ($row['smiley_url'] !== $last_url)
  70          {
  71              $template->assign_block_vars('smiley', array(
  72                  'SMILEY_CODE'    => $row['code'],
  73                  'A_SMILEY_CODE'    => addslashes($row['code']),
  74                  'SMILEY_IMG'    => $phpbb_root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
  75                  'SMILEY_WIDTH'    => $row['smiley_width'],
  76                  'SMILEY_HEIGHT'    => $row['smiley_height'],
  77                  'SMILEY_DESC'    => $row['emotion'])
  78              );
  79          }
  80          $last_url = $row['smiley_url'];
  81      }
  82      $db->sql_freeresult($result);
  83  
  84      if ($mode == 'inline' && $display_link)
  85      {
  86          $template->assign_vars(array(
  87              'S_SHOW_SMILEY_LINK'     => true,
  88              'U_MORE_SMILIES'         => append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id))
  89          );
  90      }
  91  
  92      if ($mode == 'window')
  93      {
  94          page_footer();
  95      }
  96  }
  97  
  98  /**
  99  * Update Post Informations (First/Last Post in topic/forum)
 100  * Should be used instead of sync() if only the last post informations are out of sync... faster
 101  *
 102  * @param string $type Can be forum|topic
 103  * @param mixed $ids topic/forum ids
 104  */
 105  function update_post_information($type, $ids, $return_update_sql = false)
 106  {
 107      global $db;
 108  
 109      if (!is_array($ids))
 110      {
 111          $ids = array($ids);
 112      }
 113  
 114      $update_sql = $empty_forums = $not_empty_forums = array();
 115  
 116      if (sizeof($ids) == 1)
 117      {
 118          $sql = 'SELECT MAX(post_id) as last_post_id
 119              FROM ' . POSTS_TABLE . '
 120              WHERE ' . $db->sql_in_set($type . '_id', $ids) . '
 121                  AND post_approved = 1';
 122      }
 123      else
 124      {
 125          $sql = 'SELECT ' . $type . '_id, MAX(post_id) as last_post_id
 126              FROM ' . POSTS_TABLE . '
 127              WHERE ' . $db->sql_in_set($type . '_id', $ids) . "
 128                  AND post_approved = 1
 129              GROUP BY {$type}_id";
 130      }
 131      $result = $db->sql_query($sql);
 132  
 133      $last_post_ids = array();
 134      while ($row = $db->sql_fetchrow($result))
 135      {
 136          if (sizeof($ids) == 1)
 137          {
 138              $row[$type . '_id'] = $ids[0];
 139          }
 140  
 141          if ($type == 'forum')
 142          {
 143              $not_empty_forums[] = $row['forum_id'];
 144  
 145              if (empty($row['last_post_id']))
 146              {
 147                  $empty_forums[] = $row['forum_id'];
 148              }
 149          }
 150  
 151          $last_post_ids[] = $row['last_post_id'];
 152      }
 153      $db->sql_freeresult($result);
 154  
 155      if ($type == 'forum')
 156      {
 157          $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
 158  
 159          foreach ($empty_forums as $void => $forum_id)
 160          {
 161              $update_sql[$forum_id][] = 'forum_last_post_id = 0';
 162              $update_sql[$forum_id][] = "forum_last_post_subject = ''";
 163              $update_sql[$forum_id][] = 'forum_last_post_time = 0';
 164              $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
 165              $update_sql[$forum_id][] = "forum_last_poster_name = ''";
 166              $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
 167          }
 168      }
 169  
 170      if (sizeof($last_post_ids))
 171      {
 172          $sql = 'SELECT p.' . $type . '_id, p.post_id, p.post_subject, p.post_time, p.poster_id, p.post_username, u.user_id, u.username, u.user_colour
 173              FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
 174              WHERE p.poster_id = u.user_id
 175                  AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
 176          $result = $db->sql_query($sql);
 177  
 178          while ($row = $db->sql_fetchrow($result))
 179          {
 180              $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
 181              $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
 182              $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
 183              $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
 184              $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
 185              $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
 186          }
 187          $db->sql_freeresult($result);
 188      }
 189      unset($empty_forums, $ids, $last_post_ids);
 190  
 191      if ($return_update_sql || !sizeof($update_sql))
 192      {
 193          return $update_sql;
 194      }
 195  
 196      $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
 197  
 198      foreach ($update_sql as $update_id => $update_sql_ary)
 199      {
 200          $sql = "UPDATE $table
 201              SET " . implode(', ', $update_sql_ary) . "
 202              WHERE {$type}_id = $update_id";
 203          $db->sql_query($sql);
 204      }
 205  
 206      return;
 207  }
 208  
 209  /**
 210  * Generate Topic Icons for display
 211  */
 212  function posting_gen_topic_icons($mode, $icon_id)
 213  {
 214      global $phpbb_root_path, $config, $template, $cache;
 215  
 216      // Grab icons
 217      $icons = $cache->obtain_icons();
 218  
 219      if (!$icon_id)
 220      {
 221          $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
 222      }
 223  
 224      if (sizeof($icons))
 225      {
 226          foreach ($icons as $id => $data)
 227          {
 228              if ($data['display'])
 229              {
 230                  $template->assign_block_vars('topic_icon', array(
 231                      'ICON_ID'        => $id,
 232                      'ICON_IMG'        => $phpbb_root_path . $config['icons_path'] . '/' . $data['img'],
 233                      'ICON_WIDTH'    => $data['width'],
 234                      'ICON_HEIGHT'    => $data['height'],
 235      
 236                      'S_CHECKED'            => ($id == $icon_id) ? true : false,
 237                      'S_ICON_CHECKED'    => ($id == $icon_id) ? ' checked="checked"' : '')
 238                  );
 239              }
 240          }
 241  
 242          return true;
 243      }
 244  
 245      return false;
 246  }
 247  
 248  /**
 249  * Build topic types able to be selected
 250  */
 251  function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
 252  {
 253      global $auth, $user, $template, $topic_type;
 254  
 255      $toggle = false;
 256  
 257      $topic_types = array(
 258          'sticky'    => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
 259          'announce'    => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
 260          'global'    => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
 261      );
 262  
 263      $topic_type_array = array();
 264  
 265      foreach ($topic_types as $auth_key => $topic_value)
 266      {
 267          // We do not have a special post global announcement permission
 268          $auth_key = ($auth_key == 'global') ? 'announce' : $auth_key;
 269  
 270          if ($auth->acl_get('f_' . $auth_key, $forum_id))
 271          {
 272              $toggle = true;
 273  
 274              $topic_type_array[] = array(
 275                  'VALUE'            => $topic_value['const'],
 276                  'S_CHECKED'        => ($cur_topic_type == $topic_value['const'] || ($forum_id == 0 && $topic_value['const'] == POST_GLOBAL)) ? ' checked="checked"' : '',
 277                  'L_TOPIC_TYPE'    => $user->lang[$topic_value['lang']]
 278              );
 279          }
 280      }
 281  
 282      if ($toggle)
 283      {
 284          $topic_type_array = array_merge(array(0 => array(
 285              'VALUE'            => POST_NORMAL,
 286              'S_CHECKED'        => ($topic_type == POST_NORMAL) ? ' checked="checked"' : '',
 287              'L_TOPIC_TYPE'    => $user->lang['POST_NORMAL'])), 
 288  
 289              $topic_type_array
 290          );
 291          
 292          foreach ($topic_type_array as $array)
 293          {
 294              $template->assign_block_vars('topic_type', $array);
 295          }
 296  
 297          $template->assign_vars(array(
 298              'S_TOPIC_TYPE_STICKY'    => ($auth->acl_get('f_sticky', $forum_id)),
 299              'S_TOPIC_TYPE_ANNOUNCE'    => ($auth->acl_get('f_announce', $forum_id)))
 300          );
 301      }
 302  
 303      return $toggle;
 304  }
 305  
 306  //
 307  // Attachment related functions
 308  //
 309  
 310  /**
 311  * Upload Attachment - filedata is generated here
 312  * Uses upload class
 313  */
 314  function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false)
 315  {
 316      global $auth, $user, $config, $db, $cache;
 317      global $phpbb_root_path, $phpEx;
 318  
 319      $filedata = array(
 320          'error'    => array()
 321      );
 322  
 323      include_once($phpbb_root_path . 'includes/functions_upload.' . $phpEx);
 324      $upload = new fileupload();
 325  
 326      if (!$local)
 327      {
 328          $filedata['post_attach'] = ($upload->is_valid($form_name)) ? true : false;
 329      }
 330      else
 331      {
 332          $filedata['post_attach'] = true;
 333      }
 334  
 335      if (!$filedata['post_attach'])
 336      {
 337          $filedata['error'][] = $user->lang['NO_UPLOAD_FORM_FOUND'];
 338          return $filedata;
 339      }
 340  
 341      $extensions = $cache->obtain_attach_extensions($forum_id);
 342      $upload->set_allowed_extensions(array_keys($extensions['_allowed_']));
 343  
 344      $file = ($local) ? $upload->local_upload($local_storage) : $upload->form_upload($form_name);
 345  
 346      if ($file->init_error)
 347      {
 348          $filedata['post_attach'] = false;
 349          return $filedata;
 350      }
 351  
 352      $cat_id = (isset($extensions[$file->get('extension')]['display_cat'])) ? $extensions[$file->get('extension')]['display_cat'] : ATTACHMENT_CATEGORY_NONE;
 353  
 354      // Make sure the image category only holds valid images...
 355      if ($cat_id == ATTACHMENT_CATEGORY_IMAGE && !$file->is_image())
 356      {
 357          $file->remove();
 358  
 359          // If this error occurs a user tried to exploit an IE Bug by renaming extensions
 360          // Since the image category is displaying content inline we need to catch this.
 361          trigger_error($user->lang['UNABLE_GET_IMAGE_SIZE']);
 362      }
 363  
 364      // Do we have to create a thumbnail?
 365      $filedata['thumbnail'] = ($cat_id == ATTACHMENT_CATEGORY_IMAGE && $config['img_create_thumbnail']) ? 1 : 0;
 366  
 367      // Check Image Size, if it is an image
 368      if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id) && $cat_id == ATTACHMENT_CATEGORY_IMAGE)
 369      {
 370          $file->upload->set_allowed_dimensions(0, 0, $config['img_max_width'], $config['img_max_height']);        
 371      }
 372  
 373      // Admins and mods are allowed to exceed the allowed filesize
 374      if (!$auth->acl_get('a_') && !$auth->acl_get('m_', $forum_id))
 375      {
 376          if (!empty($extensions[$file->get('extension')]['max_filesize']))
 377          {
 378              $allowed_filesize = $extensions[$file->get('extension')]['max_filesize'];
 379          }
 380          else
 381          {
 382              $allowed_filesize = ($is_message) ? $config['max_filesize_pm'] : $config['max_filesize'];
 383          }
 384  
 385          $file->upload->set_max_filesize($allowed_filesize);
 386      }
 387  
 388      $file->clean_filename('unique', $user->data['user_id'] . '_');
 389      $file->move_file($config['upload_path']);
 390  
 391      if (sizeof($file->error))
 392      {
 393          $file->remove();
 394          $filedata['error'] = array_merge($filedata['error'], $file->error);
 395          $filedata['post_attach'] = false;
 396  
 397          return $filedata;
 398      }
 399  
 400      $filedata['filesize'] = $file->get('filesize');
 401      $filedata['mimetype'] = $file->get('mimetype');
 402      $filedata['extension'] = $file->get('extension');
 403      $filedata['physical_filename'] = $file->get('realname');
 404      $filedata['real_filename'] = $file->get('uploadname');
 405      $filedata['filetime'] = time();
 406  
 407      // Check our complete quota
 408      if ($config['attachment_quota'])
 409      {
 410          if ($config['upload_dir_size'] + $file->get('filesize') > $config['attachment_quota'])
 411          {
 412              $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
 413              $filedata['post_attach'] = false;
 414  
 415              $file->remove();
 416  
 417              return $filedata;
 418          }
 419      }
 420  
 421      // Check free disk space
 422      if ($free_space = @disk_free_space($phpbb_root_path . $config['upload_path']))
 423      {
 424          if ($free_space <= $file->get('filesize'))
 425          {
 426              $filedata['error'][] = $user->lang['ATTACH_QUOTA_REACHED'];
 427              $filedata['post_attach'] = false;
 428  
 429              $file->remove();
 430  
 431              return $filedata;
 432          }
 433      }
 434  
 435      // Create Thumbnail
 436      if ($filedata['thumbnail'])
 437      {
 438          $source = $file->get('destination_file');
 439          $destination = $file->get('destination_path') . '/thumb_' . $file->get('realname');
 440  
 441          if (!create_thumbnail($source, $destination, $file->get('mimetype')))
 442          {
 443              $filedata['thumbnail'] = 0;
 444          }
 445      }
 446  
 447      return $filedata;
 448  }
 449  
 450  /**
 451  * Calculate the needed size for Thumbnail
 452  */
 453  function get_img_size_format($width, $height)
 454  {
 455      global $config;
 456  
 457      // Maximum Width the Image can take
 458      $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
 459  
 460      if ($width > $height)
 461      {
 462          return array(
 463              round($width * ($max_width / $width)),
 464              round($height * ($max_width / $width))
 465          );
 466      }
 467      else
 468      {
 469          return array(
 470              round($width * ($max_width / $height)),
 471              round($height * ($max_width / $height))
 472          );
 473      }
 474  }
 475  
 476  /**
 477  * Return supported image types
 478  */
 479  function get_supported_image_types($type = false)
 480  {
 481      if (@extension_loaded('gd'))
 482      {
 483          $format = imagetypes();
 484          $new_type = 0;
 485  
 486          if ($type !== false)
 487          {
 488              switch ($type)
 489              {
 490                  // GIF
 491                  case 1:
 492                      $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
 493                  break;
 494  
 495                  // JPG, JPC, JP2
 496                  case 2:
 497                  case 9:
 498                  case 10:
 499                  case 11:
 500                  case 12:
 501                      $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
 502                  break;
 503  
 504                  // PNG
 505                  case 3:
 506                      $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
 507                  break;
 508  
 509                  // BMP, WBMP
 510                  case 6:
 511                  case 15:
 512                      $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
 513                  break;
 514              }
 515          }
 516          else
 517          {
 518              $new_type = array();
 519              $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
 520  
 521              foreach ($go_through_types as $check_type)
 522              {
 523                  if ($format & $check_type)
 524                  {
 525                      $new_type[] = $check_type;
 526                  }
 527              }
 528          }
 529  
 530          return array(
 531              'gd'        => ($new_type) ? true : false,
 532              'format'    => $new_type,
 533              'version'    => (function_exists('imagecreatetruecolor')) ? 2 : 1
 534          );
 535      }
 536  
 537      return array('gd' => false);
 538  }
 539  
 540  /**
 541  * Create Thumbnail
 542  */
 543  function create_thumbnail($source, $destination, $mimetype) 
 544  {
 545      global $config;
 546  
 547      $min_filesize = (int) $config['img_min_thumb_filesize'];
 548      $img_filesize = (file_exists($source)) ? @filesize($source) : false;
 549  
 550      if (!$img_filesize || $img_filesize <= $min_filesize)
 551      {
 552          return false;
 553      }
 554  
 555      list($width, $height, $type, ) = @getimagesize($source);
 556  
 557      if (!$width || !$height)
 558      {
 559          return false;
 560      }
 561  
 562      list($new_width, $new_height) = get_img_size_format($width, $height);
 563  
 564      $used_imagick = false;
 565  
 566      // Only use imagemagick if defined and the passthru function not disabled
 567      if ($config['img_imagick'] && function_exists('passthru'))
 568      {
 569          @passthru(escapeshellcmd($config['img_imagick']) . 'convert' . ((defined('PHP_OS') && preg_match('#^win#i', PHP_OS)) ? '.exe' : '') . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" +profile "*" "' . str_replace('\\', '/', $destination) . '"');
 570  
 571          if (file_exists($destination))
 572          {
 573              $used_imagick = true;
 574          }
 575      }
 576  
 577      if (!$used_imagick) 
 578      {
 579          $type = get_supported_image_types($type);
 580  
 581          if ($type['gd'])
 582          {
 583              // If the type is not supported, we are not able to create a thumbnail
 584              if ($type['format'] === false)
 585              {
 586                  return false;
 587              }
 588  
 589              switch ($type['format']) 
 590              {
 591                  case IMG_GIF:
 592                      $image = @imagecreatefromgif($source);
 593                  break;
 594  
 595                  case IMG_JPG:
 596                      $image = @imagecreatefromjpeg($source);
 597                  break;
 598  
 599                  case IMG_PNG:
 600                      $image = @imagecreatefrompng($source);
 601                  break;
 602  
 603                  case IMG_WBMP:
 604                      $image = @imagecreatefromwbmp($source);
 605                  break;
 606              }
 607  
 608              if ($type['version'] == 1)
 609              {
 610                  $new_image = imagecreate($new_width, $new_height);
 611                  imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 612              }
 613              else
 614              {
 615                  $new_image = imagecreatetruecolor($new_width, $new_height);
 616                  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 617              }
 618  
 619              // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
 620              if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
 621              {
 622                  @touch($destination);
 623              }
 624  
 625              switch ($type['format'])
 626              {
 627                  case IMG_GIF:
 628                      imagegif($new_image, $destination);
 629                  break;
 630  
 631                  case IMG_JPG:
 632                      imagejpeg($new_image, $destination, 90);
 633                  break;
 634  
 635                  case IMG_PNG:
 636                      imagepng($new_image, $destination);
 637                  break;
 638  
 639                  case IMG_WBMP:
 640                      imagewbmp($new_image, $destination);
 641                  break;
 642              }
 643  
 644              imagedestroy($new_image);
 645          }
 646          else
 647          {
 648              return false;
 649          }
 650      }
 651  
 652      if (!file_exists($destination))
 653      {
 654          return false;
 655      }
 656  
 657      @chmod($destination, 0666);
 658  
 659      return true;
 660  }
 661  
 662  /**
 663  * Assign Inline attachments (build option fields)
 664  */
 665  function posting_gen_inline_attachments(&$attachment_data)
 666  {
 667      global $template;
 668  
 669      if (sizeof($attachment_data))
 670      {
 671          $s_inline_attachment_options = '';
 672  
 673          foreach ($attachment_data as $i => $attachment)
 674          {
 675              $s_inline_attachment_options .= '<option value="' . $i . '">' . basename($attachment['real_filename']) . '</option>';
 676          }
 677  
 678          $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
 679  
 680          return true;
 681      }
 682  
 683      return false;
 684  }
 685  
 686  /**
 687  * Generate inline attachment entry
 688  */
 689  function posting_gen_attachment_entry(&$attachment_data, &$filename_data)
 690  {
 691      global $template, $config, $phpbb_root_path, $phpEx, $user;
 692  
 693      $template->assign_vars(array(
 694          'S_SHOW_ATTACH_BOX'    => true)
 695      );
 696  
 697      if (sizeof($attachment_data))
 698      {
 699          $template->assign_vars(array(
 700              'S_HAS_ATTACHMENTS'    => true)
 701          );
 702  
 703          $count = 0;
 704          foreach ($attachment_data as $attach_row)
 705          {
 706              $hidden = '';
 707              $attach_row['real_filename'] = basename($attach_row['real_filename']);
 708  
 709              foreach ($attach_row as $key => $value)
 710              {
 711                  $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
 712              }
 713  
 714              $download_link = append_sid("{$phpbb_root_path}download.$phpEx", 'id=' . (int) $attach_row['attach_id'], false, ($attach_row['is_orphan']) ? $user->session_id : false);
 715  
 716              $template->assign_block_vars('attach_row', array(
 717                  'FILENAME'            => basename($attach_row['real_filename']),
 718                  'FILE_COMMENT'        => $attach_row['attach_comment'],
 719                  'ATTACH_ID'            => $attach_row['attach_id'],
 720                  'S_IS_ORPHAN'        => $attach_row['is_orphan'],
 721                  'ASSOC_INDEX'        => $count,
 722  
 723                  'U_VIEW_ATTACHMENT'    => $download_link,
 724                  'S_HIDDEN'            => $hidden)
 725              );
 726  
 727              $count++;
 728          }
 729      }
 730  
 731      $template->assign_vars(array(
 732          'FILE_COMMENT'    => $filename_data['filecomment'], 
 733          'FILESIZE'        => $config['max_filesize'])
 734      );
 735  
 736      return sizeof($attachment_data);
 737  }
 738  
 739  //
 740  // General Post functions
 741  //
 742  
 743  /**
 744  * Load Drafts
 745  */
 746  function load_drafts($topic_id = 0, $forum_id = 0, $id = 0)
 747  {
 748      global $user, $db, $template, $auth;
 749      global $phpbb_root_path, $phpEx;
 750  
 751      $topic_ids = $forum_ids = $draft_rows = array();
 752  
 753      // Load those drafts not connected to forums/topics
 754      // If forum_id == 0 AND topic_id == 0 then this is a PM draft
 755      if (!$topic_id && !$forum_id)
 756      {
 757          $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
 758      }
 759      else
 760      {
 761          $sql_and = '';
 762          $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
 763          $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
 764      }
 765  
 766      $sql = 'SELECT d.*, f.forum_id, f.forum_name
 767          FROM ' . DRAFTS_TABLE . ' d
 768          LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
 769              WHERE d.user_id = ' . $user->data['user_id'] . "
 770              $sql_and
 771          ORDER BY d.save_time DESC";
 772      $result = $db->sql_query($sql);
 773  
 774      while ($row = $db->sql_fetchrow($result))
 775      {
 776          if ($row['topic_id'])
 777          {
 778              $topic_ids[] = (int) $row['topic_id'];
 779          }
 780          $draft_rows[] = $row;
 781      }
 782      $db->sql_freeresult($result);
 783  
 784      if (!sizeof($draft_rows))
 785      {
 786          return;
 787      }
 788  
 789      $topic_rows = array();
 790      if (sizeof($topic_ids))
 791      {
 792          $sql = 'SELECT topic_id, forum_id, topic_title
 793              FROM ' . TOPICS_TABLE . '
 794              WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
 795          $result = $db->sql_query($sql);
 796  
 797          while ($row = $db->sql_fetchrow($result))
 798          {
 799              $topic_rows[$row['topic_id']] = $row;
 800          }
 801          $db->sql_freeresult($result);
 802      }
 803      unset($topic_ids);
 804  
 805      $template->assign_var('S_SHOW_DRAFTS', true);
 806  
 807      foreach ($draft_rows as $draft)
 808      {
 809          $link_topic = $link_forum = $link_pm = false;
 810          $insert_url = $view_url = $title = '';
 811  
 812          if (isset($topic_rows[$draft['topic_id']]) && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
 813          {
 814              $link_topic = true;
 815              $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_rows[$draft['topic_id']]['forum_id'] . '&amp;t=' . $draft['topic_id']);
 816              $title = $topic_rows[$draft['topic_id']]['topic_title'];
 817  
 818              $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_rows[$draft['topic_id']]['forum_id'] . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
 819          }
 820          else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
 821          {
 822              $link_forum = true;
 823              $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
 824              $title = $draft['forum_name'];
 825  
 826              $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
 827          }
 828          else
 829          {
 830              // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
 831              $link_pm = true;
 832              $insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=compose&amp;d={$draft['draft_id']}");
 833          }
 834  
 835          $template->assign_block_vars('draftrow', array(
 836              'DRAFT_ID'        => $draft['draft_id'],
 837              'DATE'            => $user->format_date($draft['save_time']),
 838              'DRAFT_SUBJECT'    => $draft['draft_subject'],
 839  
 840              'TITLE'            => $title,
 841              'U_VIEW'        => $view_url,
 842              'U_INSERT'        => $insert_url,
 843  
 844              'S_LINK_PM'        => $link_pm,
 845              'S_LINK_TOPIC'    => $link_topic,
 846              'S_LINK_FORUM'    => $link_forum)
 847          );
 848      }
 849  }
 850  
 851  /**
 852  * Topic Review
 853  */
 854  function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
 855  {
 856      global $user, $auth, $db, $template, $bbcode;
 857      global $config, $phpbb_root_path, $phpEx;
 858  
 859      // Go ahead and pull all data for this topic
 860      $sql = 'SELECT u.username, u.user_id, p.*
 861          FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
 862          WHERE p.topic_id = $topic_id
 863              AND p.poster_id = u.user_id
 864              " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1' : '') . '
 865              ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
 866          ORDER BY p.post_time DESC';
 867      $result = $db->sql_query_limit($sql, $config['posts_per_page']);
 868  
 869      if (!$row = $db->sql_fetchrow($result))
 870      {
 871          $db->sql_freeresult($result);
 872          return false;
 873      }
 874  
 875      $bbcode_bitfield = '';
 876      do
 877      {
 878          $rowset[] = $row;
 879          $bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
 880      }
 881      while ($row = $db->sql_fetchrow($result));
 882      $db->sql_freeresult($result);
 883  
 884      // Instantiate BBCode class
 885      if (!isset($bbcode) && $bbcode_bitfield !== '')
 886      {
 887          include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
 888          $bbcode = new bbcode(base64_encode($bbcode_bitfield));
 889      }
 890  
 891      foreach ($rowset as $i => $row)
 892      {
 893          $poster_id = $row['user_id'];
 894          $poster = $row['username'];
 895  
 896          // Handle anon users posting with usernames
 897          if ($poster_id == ANONYMOUS)
 898          {
 899              $poster = ($row['post_username']) ? $row['post_username'] : $user->lang['GUEST'];
 900              $poster_rank = ($row['post_username']) ? $user->lang['GUEST'] : '';
 901          }
 902  
 903          $post_subject = $row['post_subject'];
 904          $message = $row['post_text'];
 905          $message = censor_text($message);
 906          $message = str_replace("\n", '<br />', $message);
 907          $decoded_message = false;
 908  
 909          if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
 910          {
 911              $decoded_message = $message;
 912              decode_message($decoded_message, $row['bbcode_uid']);
 913  
 914              $decoded_message = censor_text($decoded_message);
 915              $decoded_message = str_replace("\n", "<br />", $decoded_message);
 916          }
 917  
 918          if ($row['bbcode_bitfield'])
 919          {
 920              $bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
 921          }
 922  
 923          $message = smiley_text($message, !$row['enable_smilies']);
 924  
 925          $post_subject = censor_text($post_subject);
 926  
 927          $template->assign_block_vars($mode . '_row', array(
 928              'POSTER_NAME'        => $poster,
 929              'POST_SUBJECT'        => $post_subject,
 930              'MINI_POST_IMG'        => $user->img('icon_post_target', $user->lang['POST']),
 931              'POST_DATE'            => $user->format_date($row['post_time']),
 932              'MESSAGE'            => $message,
 933              'DECODED_MESSAGE'    => $decoded_message,
 934              'U_POST_ID'            => $row['post_id'],
 935              'U_MINI_POST'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
 936              'U_MCP_DETAILS'        => ($auth->acl_get('m_info', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=post_details&amp;f=' . $forum_id . '&amp;p=' . $row['post_id'], true, $user->session_id) : '',
 937              'POSTER_QUOTE'        => ($show_quote_button && $auth->acl_get('f_reply', $forum_id)) ? addslashes($poster) : '')
 938          );
 939          unset($rowset[$i]);
 940      }
 941  
 942      if ($mode == 'topic_review')
 943      {
 944          $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
 945      }
 946  
 947      return true;
 948  }
 949  
 950  /**
 951  * User Notification
 952  */
 953  function user_notification($mode, $subject, $topic_title, $forum_name, $forum_id, $topic_id, $post_id)
 954  {
 955      global $db, $user, $config, $phpbb_root_path, $phpEx, $auth;
 956  
 957      $topic_notification = ($mode == 'reply' || $mode == 'quote') ? true : false;
 958      $forum_notification = ($mode == 'post') ? true : false;
 959  
 960      if (!$topic_notification && !$forum_notification)
 961      {
 962          trigger_error('WRONG_NOTIFICATION_MODE');
 963      }
 964  
 965      if (!$config['allow_topic_notify'])
 966      {
 967          return;
 968      }
 969  
 970      $topic_title = ($topic_notification) ? $topic_title : $subject;
 971      $topic_title = censor_text($topic_title);
 972  
 973      // Get banned User ID's
 974      $sql = 'SELECT ban_userid 
 975          FROM ' . BANLIST_TABLE;
 976      $result = $db->sql_query($sql);
 977  
 978      $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
 979      while ($row = $db->sql_fetchrow($result))
 980      {
 981          if (isset($row['ban_userid']))
 982          {
 983              $sql_ignore_users .= ', ' . $row['ban_userid'];
 984          }
 985      }
 986      $db->sql_freeresult($result);
 987  
 988      $notify_rows = array();
 989  
 990      // -- get forum_userids    || topic_userids
 991      $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber 
 992          FROM ' . (($topic_notification) ? TOPICS_WATCH_TABLE : FORUMS_WATCH_TABLE) . ' w, ' . USERS_TABLE . ' u
 993          WHERE w.' . (($topic_notification) ? 'topic_id' : 'forum_id') . ' = ' . (($topic_notification) ? $topic_id : $forum_id) . "
 994              AND w.user_id NOT IN ($sql_ignore_users)
 995              AND w.notify_status = 0
 996              AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
 997              AND u.user_id = w.user_id';
 998      $result = $db->sql_query($sql);
 999  
1000      while ($row = $db->sql_fetchrow($result))
1001      {
1002          $notify_rows[$row['user_id']] = array(
1003              'user_id'        => $row['user_id'],
1004              'username'        => $row['username'],
1005              'user_email'    => $row['user_email'],
1006              'user_jabber'    => $row['user_jabber'], 
1007              'user_lang'        => $row['user_lang'], 
1008              'notify_type'    => ($topic_notification) ? 'topic' : 'forum',
1009              'template'        => ($topic_notification) ? 'topic_notify' : 'newtopic_notify',
1010              'method'        => $row['user_notify_type'], 
1011              'allowed'        => false
1012          );
1013      }
1014      $db->sql_freeresult($result);
1015  
1016      // forum notification is sent to those not already receiving topic notifications
1017      if ($topic_notification)
1018      {
1019          if (sizeof($notify_rows))
1020          {
1021              $sql_ignore_users .= ', ' . implode(', ', array_keys($notify_rows));
1022          }
1023  
1024          $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber 
1025              FROM ' . FORUMS_WATCH_TABLE . ' fw, ' . USERS_TABLE . " u
1026              WHERE fw.forum_id = $forum_id
1027                  AND fw.user_id NOT IN ($sql_ignore_users)
1028                  AND fw.notify_status = 0
1029                  AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
1030                  AND u.user_id = fw.user_id';
1031          $result = $db->sql_query($sql);
1032  
1033          while ($row = $db->sql_fetchrow($result))
1034          {
1035              $notify_rows[$row['user_id']] = array(
1036                  'user_id'        => $row['user_id'],
1037                  'username'        => $row['username'],
1038                  'user_email'    => $row['user_email'],
1039                  'user_jabber'    => $row['user_jabber'], 
1040                  'user_lang'        => $row['user_lang'],
1041                  'notify_type'    => 'forum',
1042                  'template'        => 'forum_notify',
1043                  'method'        => $row['user_notify_type'], 
1044                  'allowed'        => false
1045              );
1046          }
1047          $db->sql_freeresult($result);
1048      }
1049  
1050      if (!sizeof($notify_rows))
1051      {
1052          return;
1053      }
1054  
1055      // Make sure users are allowed to read the forum
1056      foreach ($auth->acl_get_list(array_keys($notify_rows), 'f_read', $forum_id) as $forum_id => $forum_ary)
1057      {
1058          foreach ($forum_ary as $auth_option => $user_ary)
1059          {
1060              foreach ($user_ary as $user_id)
1061              {
1062                  $notify_rows[$user_id]['allowed'] = true;
1063              }
1064          }
1065      }
1066  
1067  
1068      // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
1069      $msg_users = $delete_ids = $update_notification = array();
1070      foreach ($notify_rows as $user_id => $row)
1071      {
1072          if (!$row['allowed'] || !trim($row['user_email']))
1073          {
1074              $delete_ids[$row['notify_type']][] = $row['user_id'];
1075          }
1076          else
1077          {
1078              $msg_users[] = $row;
1079              $update_notification[$row['notify_type']][] = $row['user_id'];
1080          }
1081      }
1082      unset($notify_rows);
1083  
1084      // Now, we are able to really send out notifications
1085      if (sizeof($msg_users))
1086      {
1087          include_once($phpbb_root_path . 'includes/functions_messenger.'.$phpEx);
1088          $messenger = new messenger();
1089  
1090          $msg_list_ary = array();
1091          foreach ($msg_users as $row)
1092          { 
1093              $pos = (!isset($msg_list_ary[$row['template']])) ? 0 : sizeof($msg_list_ary[$row['template']]);
1094  
1095              $msg_list_ary[$row['template']][$pos]['method']    = $row['method'];
1096              $msg_list_ary[$row['template']][$pos]['email']    = $row['user_email'];
1097              $msg_list_ary[$row['template']][$pos]['jabber']    = $row['user_jabber'];
1098              $msg_list_ary[$row['template']][$pos]['name']    = $row['username'];
1099              $msg_list_ary[$row['template']][$pos]['lang']    = $row['user_lang'];
1100          }
1101          unset($msg_users);
1102  
1103          foreach ($msg_list_ary as $email_template => $email_list)
1104          {
1105              foreach ($email_list as $addr)
1106              {
1107                  $messenger->template($email_template, $addr['lang']);
1108  
1109                  $messenger->replyto($config['board_email']);
1110                  $messenger->to($addr['email'], $addr['name']);
1111                  $messenger->im($addr['jabber'], $addr['name']);
1112  
1113                  $messenger->assign_vars(array(
1114                      'USERNAME'        => htmlspecialchars_decode($addr['name']),
1115                      'TOPIC_TITLE'    => htmlspecialchars_decode($topic_title),
1116                      'FORUM_NAME'    => htmlspecialchars_decode($forum_name),
1117  
1118                      'U_FORUM'                => generate_board_url() . "/viewforum.$phpEx?f=$forum_id&e=0",
1119                      'U_TOPIC'                => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&e=0",
1120                      'U_NEWEST_POST'            => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&p=$post_id&e=$post_id",
1121                      'U_STOP_WATCHING_TOPIC'    => generate_board_url() . "/viewtopic.$phpEx?f=$forum_id&t=$topic_id&unwatch=topic",
1122                      'U_STOP_WATCHING_FORUM'    => generate_board_url() . "/viewforum.$phpEx?f=$forum_id&unwatch=forum", 
1123                  ));
1124  
1125                  $messenger->send($addr['method']);
1126                  $messenger->reset();
1127              }
1128          }
1129          unset($msg_list_ary);
1130  
1131          $messenger->save_queue();
1132      }
1133  
1134      // Handle the DB updates
1135      $db->sql_transaction('begin');
1136  
1137      if (!empty($update_notification['topic']))
1138      {
1139          $sql = 'UPDATE ' . TOPICS_WATCH_TABLE . "
1140              SET notify_status = 1
1141              WHERE topic_id = $topic_id
1142                  AND " . $db->sql_in_set('user_id', $update_notification['topic']);
1143          $db->sql_query($sql);
1144      }
1145  
1146      if (!empty($update_notification['forum']))
1147      {
1148          $sql = 'UPDATE ' . FORUMS_WATCH_TABLE . "
1149              SET notify_status = 1
1150              WHERE forum_id = $forum_id
1151                  AND " . $db->sql_in_set('user_id', $update_notification['forum']);
1152          $db->sql_query($sql);
1153      }
1154  
1155      // Now delete the user_ids not authorized to receive notifications on this topic/forum
1156      if (!empty($delete_ids['topic']))
1157      {
1158          $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . "
1159              WHERE topic_id = $topic_id
1160                  AND " . $db->sql_in_set('user_id', $delete_ids['topic']);
1161          $db->sql_query($sql);
1162      }
1163  
1164      if (!empty($delete_ids['forum']))
1165      {
1166          $sql = 'DELETE FROM ' . FORUMS_WATCH_TABLE . "
1167              WHERE forum_id = $forum_id
1168                  AND " . $db->sql_in_set('user_id', $delete_ids['forum']);
1169          $db->sql_query($sql);
1170      }
1171  
1172      $db->sql_transaction('commit');
1173  }
1174  
1175  //
1176  // Post handling functions
1177  //
1178  
1179  /**
1180  * Delete Post
1181  */
1182  function delete_post($forum_id, $topic_id, $post_id, &$data)
1183  {
1184      global $db, $user, $auth;
1185      global $config, $phpEx, $phpbb_root_path;
1186  
1187      // Specify our post mode
1188      $post_mode = ($data['topic_first_post_id'] == $data['topic_last_post_id']) ? 'delete_topic' : (($data['topic_first_post_id'] == $post_id) ? 'delete_first_post' : (($data['topic_last_post_id'] == $post_id) ? 'delete_last_post' : 'delete'));
1189      $sql_data = array();
1190      $next_post_id = 0;
1191  
1192      include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1193  
1194      $db->sql_transaction('begin');
1195  
1196      if (!delete_posts('post_id', array($post_id), false, false))
1197      {
1198          // Try to delete topic, we may had an previous error causing inconsistency
1199          if ($post_mode == 'delete_topic')
1200          {
1201              delete_topics('topic_id', array($topic_id), false);
1202          }
1203          trigger_error('ALREADY_DELETED');
1204      }
1205  
1206      $db->sql_transaction('commit');
1207  
1208      // Collect the necessary information for updating the tables
1209      $sql_data[FORUMS_TABLE] = '';
1210      switch ($post_mode)
1211      {
1212          case 'delete_topic':
1213              delete_topics('topic_id', array($topic_id), false);
1214  
1215              if ($data['topic_type'] != POST_GLOBAL)
1216              {
1217                  $sql_data[FORUMS_TABLE] .= 'forum_posts = forum_posts - 1, forum_topics_real = forum_topics_real - 1';
1218                  $sql_data[FORUMS_TABLE] .= ($data['topic_approved']) ? ', forum_topics = forum_topics - 1' : '';
1219              }
1220  
1221              $update_sql = update_post_information('forum', $forum_id, true);
1222              if (sizeof($update_sql))
1223              {
1224                  $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1225                  $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1226              }
1227          break;
1228  
1229          case 'delete_first_post':
1230              $sql = 'SELECT p.post_id, p.poster_id, p.post_username, u.username, u.user_colour
1231                  FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1232                  WHERE p.topic_id = $topic_id
1233                      AND p.poster_id = u.user_id
1234                  ORDER BY p.post_time ASC";
1235              $result = $db->sql_query_limit($sql, 1);
1236              $row = $db->sql_fetchrow($result);
1237              $db->sql_freeresult($result);
1238  
1239              if ($data['topic_type'] != POST_GLOBAL)
1240              {
1241                  $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - 1';
1242              }
1243  
1244              $sql_data[TOPICS_TABLE] = 'topic_first_post_id = ' . intval($row['post_id']) . ", topic_first_poster_colour = '" . $db->sql_escape($row['user_colour']) . ", topic_first_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
1245              $sql_data[TOPICS_TABLE] .= ', topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1246  
1247              $next_post_id = (int) $row['post_id'];
1248          break;
1249  
1250          case 'delete_last_post':
1251              if ($data['topic_type'] != POST_GLOBAL)
1252              {
1253                  $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - 1';
1254              }
1255  
1256              $update_sql = update_post_information('forum', $forum_id, true);
1257              if (sizeof($update_sql))
1258              {
1259                  $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1260                  $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1261              }
1262  
1263              $sql_data[TOPICS_TABLE] = 'topic_bumped = 0, topic_bumper = 0, topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1264  
1265              $update_sql = update_post_information('topic', $topic_id, true);
1266              if (sizeof($update_sql))
1267              {
1268                  $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
1269                  $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
1270              }
1271              else
1272              {
1273                  $sql = 'SELECT MAX(post_id) as last_post_id
1274                      FROM ' . POSTS_TABLE . "
1275                      WHERE topic_id = $topic_id " .
1276                          ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '');
1277                  $result = $db->sql_query($sql);
1278                  $row = $db->sql_fetchrow($result);
1279                  $db->sql_freeresult($result);
1280  
1281                  $next_post_id = (int) $row['last_post_id'];
1282              }
1283          break;
1284  
1285          case 'delete':
1286              $sql = 'SELECT post_id
1287                  FROM ' . POSTS_TABLE . "
1288                  WHERE topic_id = $topic_id " .
1289                      ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND post_approved = 1' : '') . '
1290                      AND post_time > ' . $data['post_time'] . '
1291                  ORDER BY post_time ASC';
1292              $result = $db->sql_query_limit($sql, 1);
1293              $row = $db->sql_fetchrow($result);
1294              $db->sql_freeresult($result);
1295  
1296              if ($data['topic_type'] != POST_GLOBAL)
1297              {
1298                  $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - 1';
1299              }
1300  
1301              $sql_data[TOPICS_TABLE] = 'topic_replies_real = topic_replies_real - 1' . (($data['post_approved']) ? ', topic_replies = topic_replies - 1' : '');
1302              $next_post_id = (int) $row['post_id'];
1303          break;
1304      }
1305  
1306  //    $sql_data[USERS_TABLE] = ($data['post_postcount']) ? 'user_posts = user_posts - 1' : '';
1307  
1308      $db->sql_transaction('begin');
1309  
1310      $where_sql = array(
1311          FORUMS_TABLE    => "forum_id = $forum_id",
1312          TOPICS_TABLE    => "topic_id = $topic_id",
1313          USERS_TABLE        => 'user_id = ' . $data['poster_id']
1314      );
1315  
1316      foreach ($sql_data as $table => $update_sql)
1317      {
1318          if ($update_sql)
1319          {
1320              $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
1321          }
1322      }
1323  
1324      $db->sql_transaction('commit');
1325  
1326      // Adjust posted info for this user by looking for a post by him/her within this topic...
1327      if ($post_mode != 'delete_topic' && $config['load_db_track'] && $user->data['is_registered'])
1328      {
1329          $sql = 'SELECT poster_id
1330              FROM ' . POSTS_TABLE . '
1331              WHERE topic_id = ' . $topic_id . '
1332                  AND poster_id = ' . $user->data['user_id'];
1333          $result = $db->sql_query_limit($sql, 1);
1334          $poster_id = (int) $db->sql_fetchfield('poster_id');
1335          $db->sql_freeresult($result);
1336  
1337          // The user is not having any more posts within this topic
1338          if (!$poster_id)
1339          {
1340              $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1341                  WHERE topic_id = ' . $topic_id . '
1342                      AND user_id = ' . $user->data['user_id'];
1343              $db->sql_query($sql);
1344          }
1345      }
1346  
1347      if ($data['post_reported'] && ($post_mode != 'delete_topic'))
1348      {
1349          sync('topic_reported', 'topic_id', array($topic_id));
1350      }
1351  
1352      return $next_post_id;
1353  }
1354  
1355  /**
1356  * Submit Post
1357  */
1358  function submit_post($mode, $subject, $username, $topic_type, &$poll, &$data, $update_message = true)
1359  {
1360      global $db, $auth, $user, $config, $phpEx, $template, $phpbb_root_path;
1361  
1362      // We do not handle erasing posts here
1363      if ($mode == 'delete')
1364      {
1365          return false;
1366      }
1367  
1368      $current_time = time();
1369  
1370      if ($mode == 'post')
1371      {
1372          $post_mode = 'post';
1373          $update_message = true;
1374      }
1375      else if ($mode != 'edit')
1376      {
1377          $post_mode = 'reply';
1378          $update_message = true;
1379      }
1380      else if ($mode == 'edit')
1381      {
1382          $post_mode = ($data['topic_first_post_id'] == $data['topic_last_post_id']) ? 'edit_topic' : (($data['topic_first_post_id'] == $data['post_id']) ? 'edit_first_post' : (($data['topic_last_post_id'] == $data['post_id']) ? 'edit_last_post' : 'edit'));
1383      }
1384  
1385      // First of all make sure the subject and topic title are having the correct length.
1386      // To achive this without cutting off between special chars we convert to an array and then count the elements.
1387      $subject = truncate_string($subject);
1388      $data['topic_title'] = truncate_string($data['topic_title']);
1389  
1390      // Collect some basic informations about which tables and which rows to update/insert
1391      $sql_data = array();
1392      $poster_id = ($mode == 'edit') ? $data['poster_id'] : (int) $user->data['user_id'];
1393  
1394      // Collect Informations
1395      switch ($post_mode)
1396      {
1397          case 'post':
1398          case 'reply':
1399              $sql_data[POSTS_TABLE]['sql'] = array(
1400                  'forum_id'            => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1401                  'poster_id'            => (int) $user->data['user_id'],
1402                  'icon_id'            => $data['icon_id'],
1403                  'poster_ip'            => $user->ip,
1404                  'post_time'            => $current_time,
1405                  'post_approved'        => (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id'])) ? 0 : 1,
1406                  'enable_bbcode'        => $data['enable_bbcode'],
1407                  'enable_smilies'    => $data['enable_smilies'],
1408                  'enable_magic_url'    => $data['enable_urls'],
1409                  'enable_sig'        => $data['enable_sig'],
1410                  'post_username'        => (!$user->data['is_registered']) ? $username : '',
1411                  'post_subject'        => $subject,
1412                  'post_text'            => $data['message'],
1413                  'post_checksum'        => $data['message_md5'],
1414                  'post_attachment'    => (!empty($data['attachment_data'])) ? 1 : 0,
1415                  'bbcode_bitfield'    => $data['bbcode_bitfield'],
1416                  'bbcode_uid'        => $data['bbcode_uid'],
1417                  'post_postcount'    => ($auth->acl_get('f_postcount', $data['forum_id'])) ? 1 : 0,
1418                  'post_edit_locked'    => $data['post_edit_locked']
1419              );
1420          break;
1421  
1422          case 'edit_first_post':
1423          case 'edit':
1424  
1425              if (!$auth->acl_get('m_edit', $data['forum_id']) || $data['post_edit_reason'])
1426              {
1427                  $sql_data[POSTS_TABLE]['sql'] = array(
1428                      'post_edit_time'    => $current_time
1429                  );
1430  
1431                  $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1432              }
1433  
1434          // no break
1435  
1436          case 'edit_last_post':
1437          case 'edit_topic':
1438  
1439              if (($post_mode == 'edit_last_post' || $post_mode == 'edit_topic') && $data['post_edit_reason'])
1440              {
1441                  $sql_data[POSTS_TABLE]['sql'] = array(
1442                      'post_edit_time'    => $current_time
1443                  );
1444  
1445                  $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1446              }
1447  
1448              if (!isset($sql_data[POSTS_TABLE]['sql']))
1449              {
1450                  $sql_data[POSTS_TABLE]['sql'] = array();
1451              }
1452  
1453              $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1454                  'forum_id'            => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1455                  'poster_id'            => $data['poster_id'],
1456                  'icon_id'            => $data['icon_id'],
1457                  'post_approved'        => (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id'])) ? 0 : 1,
1458                  'enable_bbcode'        => $data['enable_bbcode'],
1459                  'enable_smilies'    => $data['enable_smilies'],
1460                  'enable_magic_url'    => $data['enable_urls'],
1461                  'enable_sig'        => $data['enable_sig'],
1462                  'post_username'        => ($username && $data['poster_id'] == ANONYMOUS) ? $username : '',
1463                  'post_subject'        => $subject,
1464                  'post_edit_reason'    => $data['post_edit_reason'],
1465                  'post_edit_user'    => (int) $data['post_edit_user'],
1466                  'post_checksum'        => $data['message_md5'],
1467                  'post_attachment'    => (!empty($data['attachment_data'])) ? 1 : 0,
1468                  'bbcode_bitfield'    => $data['bbcode_bitfield'],
1469                  'bbcode_uid'        => $data['bbcode_uid'],
1470                  'post_edit_locked'    => $data['post_edit_locked'])
1471              );
1472  
1473              if ($update_message)
1474              {
1475                  $sql_data[POSTS_TABLE]['sql']['post_text'] = $data['message'];
1476              }
1477  
1478          break;
1479      }
1480  
1481      // And the topic ladies and gentlemen
1482      switch ($post_mode)
1483      {
1484          case 'post':
1485              $sql_data[TOPICS_TABLE]['sql'] = array(
1486                  'topic_poster'                => (int) $user->data['user_id'],
1487                  'topic_time'                => $current_time,
1488                  'forum_id'                    => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1489                  'icon_id'                    => $data['icon_id'],
1490                  'topic_approved'            => (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id'])) ? 0 : 1,
1491                  'topic_title'                => $subject,
1492                  'topic_first_poster_name'    => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1493                  'topic_first_poster_colour'    => (($user->data['user_id'] != ANONYMOUS) ? $user->data['user_colour'] : ''),
1494                  'topic_type'                => $topic_type,
1495                  'topic_time_limit'            => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1496                  'topic_attachment'            => (!empty($data['attachment_data'])) ? 1 : 0,
1497              );
1498  
1499              if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1500              {
1501                  $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
1502                      'poll_title'        => $poll['poll_title'],
1503                      'poll_start'        => ($poll['poll_start']) ? $poll['poll_start'] : $current_time,
1504                      'poll_max_options'    => $poll['poll_max_options'],
1505                      'poll_length'        => ($poll['poll_length'] * 86400),
1506                      'poll_vote_change'    => $poll['poll_vote_change'])
1507                  );
1508              }
1509  
1510              $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id'])) ? ', user_posts = user_posts + 1' : '');
1511      
1512              if ($topic_type != POST_GLOBAL)
1513              {
1514                  if ($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id']))
1515                  {
1516                      $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1517                  }
1518                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id'])) ? ', forum_topics = forum_topics + 1' : '');
1519              }
1520          break;
1521  
1522          case 'reply':
1523              $sql_data[TOPICS_TABLE]['stat'][] = 'topic_replies_real = topic_replies_real + 1, topic_bumped = 0, topic_bumper = 0' . (($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id'])) ? ', topic_replies = topic_replies + 1' : '');
1524              $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data['forum_id'])) ? ', user_posts = user_posts + 1' : '');
1525  
1526              if (($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id'])) && $topic_type != POST_GLOBAL)
1527              {
1528                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + 1';
1529              }
1530          break;
1531  
1532          case 'edit_topic':
1533          case 'edit_first_post':
1534  
1535              $sql_data[TOPICS_TABLE]['sql'] = array(
1536                  'forum_id'                    => ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id'],
1537                  'icon_id'                    => $data['icon_id'],
1538                  'topic_approved'            => (!$auth->acl_get('f_noapprove', $data['forum_id']) && !$auth->acl_get('m_approve', $data['forum_id'])) ? 0 : 1,
1539                  'topic_title'                => $subject,
1540                  'topic_first_poster_name'    => $username,
1541                  'topic_type'                => $topic_type,
1542                  'topic_time_limit'            => ($topic_type == POST_STICKY || $topic_type == POST_ANNOUNCE) ? ($data['topic_time_limit'] * 86400) : 0,
1543                  'poll_title'                => (isset($poll['poll_options'])) ? $poll['poll_title'] : '',
1544                  'poll_start'                => (isset($poll['poll_options'])) ? (($poll['poll_start']) ? $poll['poll_start'] : $current_time) : 0,
1545                  'poll_max_options'            => (isset($poll['poll_options'])) ? $poll['poll_max_options'] : 1,
1546                  'poll_length'                => (isset($poll['poll_options'])) ? ($poll['poll_length'] * 86400) : 0,
1547                  'poll_vote_change'            => (isset($poll['poll_vote_change'])) ? $poll['poll_vote_change'] : 0,
1548  
1549                  'topic_attachment'            => (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),
1550              );
1551          break;
1552      }
1553  
1554      $db->sql_transaction('begin');
1555  
1556      // Submit new topic
1557      if ($post_mode == 'post')
1558      {
1559          $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
1560              $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
1561          $db->sql_query($sql);
1562  
1563          $data['topic_id'] = $db->sql_nextid();
1564  
1565          $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1566              'topic_id' => $data['topic_id'])
1567          );
1568          unset($sql_data[TOPICS_TABLE]['sql']);
1569      }
1570  
1571      // Submit new post
1572      if ($post_mode == 'post' || $post_mode == 'reply')
1573      {
1574          if ($post_mode == 'reply')
1575          {
1576              $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1577                  'topic_id' => $data['topic_id'])
1578              );
1579          }
1580  
1581          $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' .    $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
1582          $db->sql_query($sql);
1583          $data['post_id'] = $db->sql_nextid();
1584  
1585          if ($post_mode == 'post')
1586          {
1587              $sql_data[TOPICS_TABLE]['sql'] = array(
1588                  'topic_first_post_id'    => $data['post_id'],
1589                  'topic_last_post_id'    => $data['post_id'],
1590                  'topic_last_post_time'    => $current_time,
1591                  'topic_last_poster_id'    => (int) $user->data['user_id'],
1592                  'topic_last_poster_name'=> (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1593                  'topic_last_poster_colour'    => (($user->data['user_id'] != ANONYMOUS) ? $user->data['user_colour'] : ''),
1594              );
1595          }
1596  
1597          unset($sql_data[POSTS_TABLE]['sql']);
1598      }
1599  
1600      $make_global = false;
1601  
1602      // Are we globalising or unglobalising?
1603      if ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic')
1604      {
1605          $sql = 'SELECT topic_type, topic_replies_real, topic_approved
1606              FROM ' . TOPICS_TABLE . '
1607              WHERE topic_id = ' . $data['topic_id'];
1608          $result = $db->sql_query($sql);
1609          $row = $db->sql_fetchrow($result);
1610          $db->sql_freeresult($result);
1611  
1612          // globalise
1613          if ($row['topic_type'] != POST_GLOBAL && $topic_type == POST_GLOBAL)
1614          {
1615              // Decrement topic/post count
1616              $make_global = true;
1617              $sql_data[FORUMS_TABLE]['stat'] = array();
1618  
1619              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts - ' . ($row['topic_replies_real'] + 1);
1620              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real - 1' . (($row['topic_approved']) ? ', forum_topics = forum_topics - 1' : '');
1621  
1622              // Update forum_ids for all posts
1623              $sql = 'UPDATE ' . POSTS_TABLE . '
1624                  SET forum_id = 0
1625                  WHERE topic_id = ' . $data['topic_id'];
1626              $db->sql_query($sql);
1627          }
1628          // unglobalise
1629          else if ($row['topic_type'] == POST_GLOBAL && $topic_type != POST_GLOBAL)
1630          {
1631              // Increment topic/post count
1632              $make_global = true;
1633              $sql_data[FORUMS_TABLE]['stat'] = array();
1634  
1635              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts = forum_posts + ' . ($row['topic_replies_real'] + 1);
1636              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_real = forum_topics_real + 1' . (($row['topic_approved']) ? ', forum_topics = forum_topics + 1' : '');
1637  
1638              // Update forum_ids for all posts
1639              $sql = 'UPDATE ' . POSTS_TABLE . '
1640                  SET forum_id = ' . $data['forum_id'] . '
1641                  WHERE topic_id = ' . $data['topic_id'];
1642              $db->sql_query($sql);
1643          }
1644      }
1645  
1646      // Update the topics table
1647      if (isset($sql_data[TOPICS_TABLE]['sql']))
1648      {
1649          $sql = 'UPDATE ' . TOPICS_TABLE . '
1650              SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
1651              WHERE topic_id = ' . $data['topic_id'];
1652          $db->sql_query($sql);
1653      }
1654  
1655      // Update the posts table
1656      if (isset($sql_data[POSTS_TABLE]['sql']))
1657      {
1658          $sql = 'UPDATE ' . POSTS_TABLE . '
1659              SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
1660              WHERE post_id = ' . $data['post_id'];
1661          $db->sql_query($sql);
1662      }
1663  
1664      // Update Poll Tables
1665      if (isset($poll['poll_options']) && !empty($poll['poll_options']))
1666      {
1667          $cur_poll_options = array();
1668  
1669          if ($poll['poll_start'] && $mode == 'edit')
1670          {
1671              $sql = 'SELECT * FROM ' . POLL_OPTIONS_TABLE . '
1672                  WHERE topic_id = ' . $data['topic_id'] . '
1673                  ORDER BY poll_option_id';
1674              $result = $db->sql_query($sql);
1675  
1676              $cur_poll_options = array();
1677              while ($row = $db->sql_fetchrow($result))
1678              {
1679                  $cur_poll_options[] = $row;
1680              }
1681              $db->sql_freeresult($result);
1682          }
1683  
1684          $sql_insert_ary = array();
1685          for ($i = 0, $size = sizeof($poll['poll_options']); $i < $size; $i++)
1686          {
1687              if (trim($poll['poll_options'][$i]))
1688              {
1689                  if (empty($cur_poll_options[$i]))
1690                  {
1691                      $sql_insert_ary[] = array(
1692                          'poll_option_id'    => (int) $i,
1693                          'topic_id'            => (int) $data['topic_id'],
1694                          'poll_option_text'    => (string) $poll['poll_options'][$i]
1695                      );
1696                  }
1697                  else if ($poll['poll_options'][$i] != $cur_poll_options[$i])
1698                  {
1699                      $sql = "UPDATE " . POLL_OPTIONS_TABLE . "
1700                          SET poll_option_text = '" . $db->sql_escape($poll['poll_options'][$i]) . "'
1701                          WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . "
1702                              AND topic_id = " . $data['topic_id'];
1703                      $db->sql_query($sql);
1704                  }
1705              }
1706          }
1707  
1708          $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
1709  
1710          if (sizeof($poll['poll_options']) < sizeof($cur_poll_options))
1711          {
1712              $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
1713                  WHERE poll_option_id >= ' . sizeof($poll['poll_options']) . '
1714                      AND topic_id = ' . $data['topic_id'];
1715              $db->sql_query($sql);
1716          }
1717      }
1718  
1719      // Submit Attachments
1720      if (!empty($data['attachment_data']) && $data['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
1721      {
1722          $space_taken = $files_added = 0;
1723          $orphan_rows = array();
1724  
1725          foreach ($data['attachment_data'] as $pos => $attach_row)
1726          {
1727              $orphan_rows[(int) $attach_row['attach_id']] = array();
1728          }
1729  
1730          if (sizeof($orphan_rows))
1731          {
1732              $sql = 'SELECT attach_id, filesize, physical_filename
1733                  FROM ' . ATTACHMENTS_TABLE . '
1734                  WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
1735                      AND is_orphan = 1
1736                      AND poster_id = ' . $user->data['user_id'];
1737              $result = $db->sql_query($sql);
1738  
1739              $orphan_rows = array();
1740              while ($row = $db->sql_fetchrow($result))
1741              {
1742                  $orphan_rows[$row['attach_id']] = $row;
1743              }
1744              $db->sql_freeresult($result);
1745          }
1746  
1747          foreach ($data['attachment_data'] as $pos => $attach_row)
1748          {
1749              if ($attach_row['is_orphan'] && !in_array($attach_row['attach_id'], array_keys($orphan_rows)))
1750              {
1751                  continue;
1752              }
1753  
1754              if (!$attach_row['is_orphan'])
1755              {
1756                  // update entry in db if attachment already stored in db and filespace
1757                  $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
1758                      SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
1759                      WHERE attach_id = " . (int) $attach_row['attach_id'] . '
1760                          AND is_orphan = 0';
1761                  $db->sql_query($sql);
1762              }
1763              else
1764              {
1765                  // insert attachment into db
1766                  if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
1767                  {
1768                      continue;
1769                  }
1770  
1771                  $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
1772                  $files_added++;
1773  
1774                  $attach_sql = array(
1775                      'post_msg_id'        => $data['post_id'],
1776                      'topic_id'            => $data['topic_id'],
1777                      'is_orphan'            => 0,
1778                      'poster_id'            => $poster_id,
1779                      'attach_comment'    => $attach_row['attach_comment'],
1780                  );
1781  
1782                  $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
1783                      WHERE attach_id = ' . $attach_row['attach_id'] . '
1784                          AND is_orphan = 1
1785                          AND poster_id = ' . $user->data['user_id'];
1786                  $db->sql_query($sql);
1787              }
1788          }
1789  
1790          if ($space_taken && $files_added)
1791          {
1792              set_config('upload_dir_size', $config['upload_dir_size'] + $space_taken, true);
1793              set_config('num_files', $config['num_files'] + $files_added, true);
1794          }
1795      }
1796  
1797      $db->sql_transaction('commit');
1798  
1799      if ($post_mode == 'post' || $post_mode == 'reply' || $post_mode == 'edit_last_post')
1800      {
1801          if ($topic_type != POST_GLOBAL)
1802          {
1803              $update_sql = update_post_information('forum', $data['forum_id'], true);
1804              if (sizeof($update_sql))
1805              {
1806                  $sql_data[FORUMS_TABLE]['stat'][] = implode(', ', $update_sql[$data['forum_id']]);
1807              }
1808          }
1809  
1810          $update_sql = update_post_information('topic', $data['topic_id'], true);
1811          if (sizeof($update_sql))
1812          {
1813              $sql_data[TOPICS_TABLE]['stat'][] = implode(', ', $update_sql[$data['topic_id']]);
1814          }
1815      }
1816  
1817      if ($make_global)
1818      {
1819          $update_sql = update_post_information('forum', $data['forum_id'], true);
1820          if (sizeof($update_sql))
1821          {
1822              $sql_data[FORUMS_TABLE]['stat'][] = implode(', ', $update_sql[$data['forum_id']]);
1823          }
1824      }
1825  
1826      if ($post_mode == 'edit_topic')
1827      {
1828          $update_sql = update_post_information('topic', $data['topic_id'], true);
1829          if (sizeof($update_sql))
1830          {
1831              $sql_data[TOPICS_TABLE]['stat'][] = implode(', ', $update_sql[$data['topic_id']]);
1832          }
1833      }
1834  
1835      // Update total post count, do not consider moderated posts/topics
1836      if ($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id']))
1837      {
1838          if ($post_mode == 'post')
1839          {
1840              set_config('num_topics', $config['num_topics'] + 1, true);
1841              set_config('num_posts', $config['num_posts'] + 1, true);
1842          }
1843  
1844          if ($post_mode == 'reply')
1845          {
1846              set_config('num_posts', $config['num_posts'] + 1, true);
1847          }
1848      }
1849  
1850      // Update forum stats
1851      $db->sql_transaction('begin');
1852  
1853      $where_sql = array(POSTS_TABLE => 'post_id = ' . $data['post_id'], TOPICS_TABLE => 'topic_id = ' . $data['topic_id'], FORUMS_TABLE => 'forum_id = ' . $data['forum_id'], USERS_TABLE => 'user_id = ' . $user->data['user_id']);
1854  
1855      foreach ($sql_data as $table => $update_ary)
1856      {
1857          if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
1858          {
1859              $db->sql_query("UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table]);
1860          }
1861      }
1862  
1863      // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
1864      if ($make_global)
1865      {
1866          $sql = 'DELETE FROM ' . TOPICS_TABLE . '
1867              WHERE topic_moved_id = ' . $data['topic_id'];
1868          $db->sql_query($sql);
1869      }
1870  
1871      // Index message contents
1872      if ($update_message && $data['enable_indexing'])
1873      {
1874          // Select the search method and do some additional checks to ensure it can actually be utilised
1875          $search_type = basename($config['search_type']);
1876  
1877          if (!file_exists($phpbb_root_path . 'includes/search/' . $search_type . '.' . $phpEx))
1878          {
1879              trigger_error('NO_SUCH_SEARCH_MODULE');
1880          }
1881  
1882          require_once("{$phpbb_root_path}includes/search/$search_type.$phpEx");
1883  
1884          $error = false;
1885          $search = new $search_type($error);
1886  
1887          if ($error)
1888          {
1889              trigger_error($error);
1890          }
1891  
1892          $search->index($mode, $data['post_id'], $data['message'], $subject, $poster_id, ($topic_type == POST_GLOBAL) ? 0 : $data['forum_id']);
1893      }
1894  
1895      $db->sql_transaction('commit');
1896  
1897      // Delete draft if post was loaded...
1898      $draft_id = request_var('draft_loaded', 0);
1899      if ($draft_id)
1900      {
1901          $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
1902              WHERE draft_id = $draft_id
1903                  AND user_id = {$user->data['user_id']}";
1904          $db->sql_query($sql);
1905      }
1906  
1907      // Topic Notification, do not change if moderator is changing other users posts...
1908      if ($user->data['user_id'] == $poster_id)
1909      {
1910          if (!$data['notify_set'] && $data['notify'])
1911          {
1912              $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
1913                  VALUES (' . $user->data['user_id'] . ', ' . $data['topic_id'] . ')';
1914              $db->sql_query($sql);
1915          }
1916          else if ($data['notify_set'] && !$data['notify'])
1917          {
1918              $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
1919                  WHERE user_id = ' . $user->data['user_id'] . '
1920                      AND topic_id = ' . $data['topic_id'];
1921              $db->sql_query($sql);
1922          }
1923      }
1924  
1925      if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
1926      {
1927          // Mark this topic as posted to
1928          markread('post', $data['forum_id'], $data['topic_id'], $data['post_time']);
1929      }
1930  
1931      // Mark this topic as read
1932      // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
1933      markread('topic', $data['forum_id'], $data['topic_id'], time());
1934  
1935      //
1936      if ($config['load_db_lastread'] && $user->data['is_registered'])
1937      {
1938          $sql = 'SELECT mark_time
1939              FROM ' . FORUMS_TRACK_TABLE . '
1940              WHERE user_id = ' . $user->data['user_id'] . '
1941                  AND forum_id = ' . $data['forum_id'];
1942          $result = $db->sql_query($sql);
1943          $f_mark_time = (int) $db->sql_fetchfield('mark_time');
1944          $db->sql_freeresult($result);
1945      }
1946      else if ($config['load_anon_lastread'] || $user->data['is_registered'])
1947      {
1948          $f_mark_time = false;
1949      }
1950  
1951      if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
1952      {
1953          // Update forum info
1954          $sql = 'SELECT forum_last_post_time
1955              FROM ' . FORUMS_TABLE . '
1956              WHERE forum_id = ' . $data['forum_id'];
1957          $result = $db->sql_query($sql);
1958          $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
1959          $db->sql_freeresult($result);
1960  
1961          update_forum_tracking_info($data['forum_id'], $forum_last_post_time, $f_mark_time, false);
1962      }
1963  
1964      // Send Notifications
1965      if ($mode != 'edit' && $mode != 'delete' && ($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id'])))
1966      {
1967          user_notification($mode, $subject, $data['topic_title'], $data['forum_name'], $data['forum_id'], $data['topic_id'], $data['post_id']);
1968      }
1969  
1970      if ($mode == 'post')
1971      {
1972          $url = ($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id'])) ? append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $data['forum_id'] . '&amp;t=' . $data['topic_id']) : append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $data['forum_id']);
1973      }
1974      else
1975      {
1976          $url = ($auth->acl_get('f_noapprove', $data['forum_id']) || $auth->acl_get('m_approve', $data['forum_id'])) ?  append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f={$data['forum_id']}&amp;t={$data['topic_id']}&amp;p={$data['post_id']}") . "#p{$data['post_id']}" : append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f={$data['forum_id']}&amp;t={$data['topic_id']}");
1977      }
1978  
1979      return $url;
1980  }
1981  
1982  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6