[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/includes/ -> functions_posting.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  /**
  15  * @ignore
  16  */
  17  if (!defined('IN_PHPBB'))
  18  {
  19      exit;
  20  }
  21  
  22  /**
  23  * Fill smiley templates (or just the variables) with smilies, either in a window or inline
  24  */
  25  function generate_smilies($mode, $forum_id)
  26  {
  27      global $db, $user, $config, $template, $phpbb_dispatcher, $request;
  28      global $phpEx, $phpbb_root_path, $phpbb_container, $phpbb_path_helper;
  29  
  30      /* @var $pagination \phpbb\pagination */
  31      $pagination = $phpbb_container->get('pagination');
  32      $base_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies&amp;f=' . $forum_id);
  33      $start = $request->variable('start', 0);
  34  
  35      if ($mode == 'window')
  36      {
  37          if ($forum_id)
  38          {
  39              $sql = 'SELECT forum_style
  40                  FROM ' . FORUMS_TABLE . "
  41                  WHERE forum_id = $forum_id";
  42              $result = $db->sql_query_limit($sql, 1);
  43              $row = $db->sql_fetchrow($result);
  44              $db->sql_freeresult($result);
  45  
  46              $user->setup('posting', (int) $row['forum_style']);
  47          }
  48          else
  49          {
  50              $user->setup('posting');
  51          }
  52  
  53          page_header($user->lang['SMILIES']);
  54  
  55          $sql = 'SELECT COUNT(smiley_id) AS item_count
  56              FROM ' . SMILIES_TABLE . '
  57              GROUP BY smiley_url';
  58          $result = $db->sql_query($sql, 3600);
  59  
  60          $smiley_count = 0;
  61          while ($row = $db->sql_fetchrow($result))
  62          {
  63              ++$smiley_count;
  64          }
  65          $db->sql_freeresult($result);
  66  
  67          $template->set_filenames(array(
  68              'body' => 'posting_smilies.html')
  69          );
  70  
  71          $start = $pagination->validate_start($start, $config['smilies_per_page'], $smiley_count);
  72          $pagination->generate_template_pagination($base_url, 'pagination', 'start', $smiley_count, $config['smilies_per_page'], $start);
  73      }
  74  
  75      $display_link = false;
  76      if ($mode == 'inline')
  77      {
  78          $sql = 'SELECT smiley_id
  79              FROM ' . SMILIES_TABLE . '
  80              WHERE display_on_posting = 0';
  81          $result = $db->sql_query_limit($sql, 1, 0, 3600);
  82  
  83          if ($row = $db->sql_fetchrow($result))
  84          {
  85              $display_link = true;
  86          }
  87          $db->sql_freeresult($result);
  88      }
  89  
  90      if ($mode == 'window')
  91      {
  92          $sql = 'SELECT smiley_url, MIN(emotion) as emotion, MIN(code) AS code, smiley_width, smiley_height, MIN(smiley_order) AS min_smiley_order
  93              FROM ' . SMILIES_TABLE . '
  94              GROUP BY smiley_url, smiley_width, smiley_height
  95              ORDER BY min_smiley_order';
  96          $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $start, 3600);
  97      }
  98      else
  99      {
 100          $sql = 'SELECT *
 101              FROM ' . SMILIES_TABLE . '
 102              WHERE display_on_posting = 1
 103              ORDER BY smiley_order';
 104          $result = $db->sql_query($sql, 3600);
 105      }
 106  
 107      $smilies = array();
 108      while ($row = $db->sql_fetchrow($result))
 109      {
 110          if (empty($smilies[$row['smiley_url']]))
 111          {
 112              $smilies[$row['smiley_url']] = $row;
 113          }
 114      }
 115      $db->sql_freeresult($result);
 116  
 117      if (count($smilies))
 118      {
 119          $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_path_helper->get_web_root_path();
 120  
 121          foreach ($smilies as $row)
 122          {
 123              /**
 124              * Modify smiley root path before populating smiley list
 125              *
 126              * @event core.generate_smilies_before
 127              * @var string  root_path root_path for smilies
 128              * @since 3.1.11-RC1
 129              */
 130              $vars = array('root_path');
 131              extract($phpbb_dispatcher->trigger_event('core.generate_smilies_before', compact($vars)));
 132              $template->assign_block_vars('smiley', array(
 133                  'SMILEY_CODE'    => $row['code'],
 134                  'A_SMILEY_CODE'    => addslashes($row['code']),
 135                  'SMILEY_IMG'    => $root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
 136                  'SMILEY_WIDTH'    => $row['smiley_width'],
 137                  'SMILEY_HEIGHT'    => $row['smiley_height'],
 138                  'SMILEY_DESC'    => $row['emotion'])
 139              );
 140          }
 141      }
 142  
 143      /**
 144      * This event is called after the smilies are populated
 145      *
 146      * @event core.generate_smilies_after
 147      * @var    string    mode            Mode of the smilies: window|inline
 148      * @var    int        forum_id        The forum ID we are currently in
 149      * @var    bool    display_link    Shall we display the "more smilies" link?
 150      * @since 3.1.0-a1
 151      */
 152      $vars = array('mode', 'forum_id', 'display_link');
 153      extract($phpbb_dispatcher->trigger_event('core.generate_smilies_after', compact($vars)));
 154  
 155      if ($mode == 'inline' && $display_link)
 156      {
 157          $template->assign_vars(array(
 158              'S_SHOW_SMILEY_LINK'     => true,
 159              'U_MORE_SMILIES'         => $base_url,
 160          ));
 161      }
 162  
 163      if ($mode == 'window')
 164      {
 165          page_footer();
 166      }
 167  }
 168  
 169  /**
 170  * Update last post information
 171  * Should be used instead of sync() if only the last post information are out of sync... faster
 172  *
 173  * @param    string    $type                Can be forum|topic
 174  * @param    mixed    $ids                topic/forum ids
 175  * @param    bool    $return_update_sql    true: SQL query shall be returned, false: execute SQL
 176  */
 177  function update_post_information($type, $ids, $return_update_sql = false)
 178  {
 179      global $db;
 180  
 181      if (empty($ids))
 182      {
 183          return;
 184      }
 185      if (!is_array($ids))
 186      {
 187          $ids = array($ids);
 188      }
 189  
 190      $update_sql = $empty_forums = $not_empty_forums = array();
 191  
 192      if ($type != 'topic')
 193      {
 194          $topic_join = ', ' . TOPICS_TABLE . ' t';
 195          $topic_condition = 'AND t.topic_id = p.topic_id AND t.topic_visibility = ' . ITEM_APPROVED;
 196      }
 197      else
 198      {
 199          $topic_join = '';
 200          $topic_condition = '';
 201      }
 202  
 203      if (count($ids) == 1)
 204      {
 205          $sql = 'SELECT p.post_id as last_post_id
 206              FROM ' . POSTS_TABLE . " p $topic_join
 207              WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
 208                  $topic_condition
 209                  AND p.post_visibility = " . ITEM_APPROVED . "
 210              ORDER BY p.post_id DESC";
 211          $result = $db->sql_query_limit($sql, 1);
 212      }
 213      else
 214      {
 215          $sql = 'SELECT p.' . $type . '_id, MAX(p.post_id) as last_post_id
 216              FROM ' . POSTS_TABLE . " p $topic_join
 217              WHERE " . $db->sql_in_set('p.' . $type . '_id', $ids) . "
 218                  $topic_condition
 219                  AND p.post_visibility = " . ITEM_APPROVED . "
 220              GROUP BY p.{$type}_id";
 221          $result = $db->sql_query($sql);
 222      }
 223  
 224      $last_post_ids = array();
 225      while ($row = $db->sql_fetchrow($result))
 226      {
 227          if (count($ids) == 1)
 228          {
 229              $row[$type . '_id'] = $ids[0];
 230          }
 231  
 232          if ($type == 'forum')
 233          {
 234              $not_empty_forums[] = $row['forum_id'];
 235  
 236              if (empty($row['last_post_id']))
 237              {
 238                  $empty_forums[] = $row['forum_id'];
 239              }
 240          }
 241  
 242          $last_post_ids[] = $row['last_post_id'];
 243      }
 244      $db->sql_freeresult($result);
 245  
 246      if ($type == 'forum')
 247      {
 248          $empty_forums = array_merge($empty_forums, array_diff($ids, $not_empty_forums));
 249  
 250          foreach ($empty_forums as $void => $forum_id)
 251          {
 252              $update_sql[$forum_id][] = 'forum_last_post_id = 0';
 253              $update_sql[$forum_id][] = "forum_last_post_subject = ''";
 254              $update_sql[$forum_id][] = 'forum_last_post_time = 0';
 255              $update_sql[$forum_id][] = 'forum_last_poster_id = 0';
 256              $update_sql[$forum_id][] = "forum_last_poster_name = ''";
 257              $update_sql[$forum_id][] = "forum_last_poster_colour = ''";
 258          }
 259      }
 260  
 261      if (count($last_post_ids))
 262      {
 263          $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
 264              FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
 265              WHERE p.poster_id = u.user_id
 266                  AND ' . $db->sql_in_set('p.post_id', $last_post_ids);
 267          $result = $db->sql_query($sql);
 268  
 269          while ($row = $db->sql_fetchrow($result))
 270          {
 271              $update_sql[$row["{$type}_id"]][] = $type . '_last_post_id = ' . (int) $row['post_id'];
 272              $update_sql[$row["{$type}_id"]][] = "{$type}_last_post_subject = '" . $db->sql_escape($row['post_subject']) . "'";
 273              $update_sql[$row["{$type}_id"]][] = $type . '_last_post_time = ' . (int) $row['post_time'];
 274              $update_sql[$row["{$type}_id"]][] = $type . '_last_poster_id = ' . (int) $row['poster_id'];
 275              $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_colour = '" . $db->sql_escape($row['user_colour']) . "'";
 276              $update_sql[$row["{$type}_id"]][] = "{$type}_last_poster_name = '" . (($row['poster_id'] == ANONYMOUS) ? $db->sql_escape($row['post_username']) : $db->sql_escape($row['username'])) . "'";
 277          }
 278          $db->sql_freeresult($result);
 279      }
 280      unset($empty_forums, $ids, $last_post_ids);
 281  
 282      if ($return_update_sql || !count($update_sql))
 283      {
 284          return $update_sql;
 285      }
 286  
 287      $table = ($type == 'forum') ? FORUMS_TABLE : TOPICS_TABLE;
 288  
 289      foreach ($update_sql as $update_id => $update_sql_ary)
 290      {
 291          $sql = "UPDATE $table
 292              SET " . implode(', ', $update_sql_ary) . "
 293              WHERE {$type}_id = $update_id";
 294          $db->sql_query($sql);
 295      }
 296  
 297      return;
 298  }
 299  
 300  /**
 301  * Generate Topic Icons for display
 302  */
 303  function posting_gen_topic_icons($mode, $icon_id)
 304  {
 305      global $phpbb_root_path, $config, $template, $cache;
 306  
 307      // Grab icons
 308      $icons = $cache->obtain_icons();
 309  
 310      if (!$icon_id)
 311      {
 312          $template->assign_var('S_NO_ICON_CHECKED', ' checked="checked"');
 313      }
 314  
 315      if (count($icons))
 316      {
 317          $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
 318  
 319          foreach ($icons as $id => $data)
 320          {
 321              if ($data['display'])
 322              {
 323                  $template->assign_block_vars('topic_icon', array(
 324                      'ICON_ID'        => $id,
 325                      'ICON_IMG'        => $root_path . $config['icons_path'] . '/' . $data['img'],
 326                      'ICON_WIDTH'    => $data['width'],
 327                      'ICON_HEIGHT'    => $data['height'],
 328                      'ICON_ALT'        => $data['alt'],
 329  
 330                      'S_CHECKED'            => ($id == $icon_id) ? true : false,
 331                      'S_ICON_CHECKED'    => ($id == $icon_id) ? ' checked="checked"' : '')
 332                  );
 333              }
 334          }
 335  
 336          return true;
 337      }
 338  
 339      return false;
 340  }
 341  
 342  /**
 343  * Build topic types able to be selected
 344  */
 345  function posting_gen_topic_types($forum_id, $cur_topic_type = POST_NORMAL)
 346  {
 347      global $auth, $user, $template;
 348  
 349      $toggle = false;
 350  
 351      $topic_types = array(
 352          'sticky'            => array('const' => POST_STICKY, 'lang' => 'POST_STICKY'),
 353          'announce'            => array('const' => POST_ANNOUNCE, 'lang' => 'POST_ANNOUNCEMENT'),
 354          'announce_global'    => array('const' => POST_GLOBAL, 'lang' => 'POST_GLOBAL')
 355      );
 356  
 357      $topic_type_array = array();
 358  
 359      foreach ($topic_types as $auth_key => $topic_value)
 360      {
 361          if ($auth->acl_get('f_' . $auth_key, $forum_id))
 362          {
 363              $toggle = true;
 364  
 365              $topic_type_array[] = array(
 366                  'VALUE'            => $topic_value['const'],
 367                  'S_CHECKED'        => ($cur_topic_type == $topic_value['const']) ? ' checked="checked"' : '',
 368                  'L_TOPIC_TYPE'    => $user->lang[$topic_value['lang']]
 369              );
 370          }
 371      }
 372  
 373      if ($toggle)
 374      {
 375          $topic_type_array = array_merge(array(0 => array(
 376              'VALUE'            => POST_NORMAL,
 377              'S_CHECKED'        => ($cur_topic_type == POST_NORMAL) ? ' checked="checked"' : '',
 378              'L_TOPIC_TYPE'    => $user->lang['POST_NORMAL'])),
 379  
 380              $topic_type_array
 381          );
 382  
 383          foreach ($topic_type_array as $array)
 384          {
 385              $template->assign_block_vars('topic_type', $array);
 386          }
 387  
 388          $template->assign_vars(array(
 389              'S_TOPIC_TYPE_STICKY'    => ($auth->acl_get('f_sticky', $forum_id)),
 390              'S_TOPIC_TYPE_ANNOUNCE'    => ($auth->acl_gets('f_announce', 'f_announce_global', $forum_id)),
 391          ));
 392      }
 393  
 394      return $toggle;
 395  }
 396  
 397  //
 398  // Attachment related functions
 399  //
 400  
 401  /**
 402  * Upload Attachment - filedata is generated here
 403  * Uses upload class
 404  *
 405  * @deprecated 3.2.0-a1 (To be removed: 3.4.0)
 406  *
 407  * @param string            $form_name        The form name of the file upload input
 408  * @param int            $forum_id        The id of the forum
 409  * @param bool            $local            Whether the file is local or not
 410  * @param string            $local_storage    The path to the local file
 411  * @param bool            $is_message        Whether it is a PM or not
 412  * @param array            $local_filedata    A filespec object created for the local file
 413  *
 414  * @return array File data array
 415  */
 416  function upload_attachment($form_name, $forum_id, $local = false, $local_storage = '', $is_message = false, $local_filedata = false)
 417  {
 418      global $phpbb_container;
 419  
 420      /** @var \phpbb\attachment\manager $attachment_manager */
 421      $attachment_manager = $phpbb_container->get('attachment.manager');
 422      $file = $attachment_manager->upload($form_name, $forum_id, $local, $local_storage, $is_message, $local_filedata);
 423      unset($attachment_manager);
 424  
 425      return $file;
 426  }
 427  
 428  /**
 429  * Calculate the needed size for Thumbnail
 430  */
 431  function get_img_size_format($width, $height)
 432  {
 433      global $config;
 434  
 435      // Maximum Width the Image can take
 436      $max_width = ($config['img_max_thumb_width']) ? $config['img_max_thumb_width'] : 400;
 437  
 438      if ($width > $height)
 439      {
 440          return array(
 441              round($width * ($max_width / $width)),
 442              round($height * ($max_width / $width))
 443          );
 444      }
 445      else
 446      {
 447          return array(
 448              round($width * ($max_width / $height)),
 449              round($height * ($max_width / $height))
 450          );
 451      }
 452  }
 453  
 454  /**
 455  * Return supported image types
 456  */
 457  function get_supported_image_types($type = false)
 458  {
 459      if (@extension_loaded('gd'))
 460      {
 461          $format = imagetypes();
 462          $new_type = 0;
 463  
 464          if ($type !== false)
 465          {
 466              // Type is one of the IMAGETYPE constants - it is fetched from getimagesize()
 467              switch ($type)
 468              {
 469                  // GIF
 470                  case IMAGETYPE_GIF:
 471                      $new_type = ($format & IMG_GIF) ? IMG_GIF : false;
 472                  break;
 473  
 474                  // JPG, JPC, JP2
 475                  case IMAGETYPE_JPEG:
 476                  case IMAGETYPE_JPC:
 477                  case IMAGETYPE_JPEG2000:
 478                  case IMAGETYPE_JP2:
 479                  case IMAGETYPE_JPX:
 480                  case IMAGETYPE_JB2:
 481                      $new_type = ($format & IMG_JPG) ? IMG_JPG : false;
 482                  break;
 483  
 484                  // PNG
 485                  case IMAGETYPE_PNG:
 486                      $new_type = ($format & IMG_PNG) ? IMG_PNG : false;
 487                  break;
 488  
 489                  // WBMP
 490                  case IMAGETYPE_WBMP:
 491                      $new_type = ($format & IMG_WBMP) ? IMG_WBMP : false;
 492                  break;
 493              }
 494          }
 495          else
 496          {
 497              $new_type = array();
 498              $go_through_types = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP);
 499  
 500              foreach ($go_through_types as $check_type)
 501              {
 502                  if ($format & $check_type)
 503                  {
 504                      $new_type[] = $check_type;
 505                  }
 506              }
 507          }
 508  
 509          return array(
 510              'gd'        => ($new_type) ? true : false,
 511              'format'    => $new_type,
 512              'version'    => (function_exists('imagecreatetruecolor')) ? 2 : 1
 513          );
 514      }
 515  
 516      return array('gd' => false);
 517  }
 518  
 519  /**
 520  * Create Thumbnail
 521  */
 522  function create_thumbnail($source, $destination, $mimetype)
 523  {
 524      global $config, $phpbb_filesystem, $phpbb_dispatcher;
 525  
 526      $min_filesize = (int) $config['img_min_thumb_filesize'];
 527      $img_filesize = (file_exists($source)) ? @filesize($source) : false;
 528  
 529      if (!$img_filesize || $img_filesize <= $min_filesize)
 530      {
 531          return false;
 532      }
 533  
 534      $dimension = @getimagesize($source);
 535  
 536      if ($dimension === false)
 537      {
 538          return false;
 539      }
 540  
 541      list($width, $height, $type, ) = $dimension;
 542  
 543      if (empty($width) || empty($height))
 544      {
 545          return false;
 546      }
 547  
 548      list($new_width, $new_height) = get_img_size_format($width, $height);
 549  
 550      // Do not create a thumbnail if the resulting width/height is bigger than the original one
 551      if ($new_width >= $width && $new_height >= $height)
 552      {
 553          return false;
 554      }
 555  
 556      $thumbnail_created = false;
 557  
 558      /**
 559       * Create thumbnail event to replace GD thumbnail creation with for example ImageMagick
 560       *
 561       * @event core.thumbnail_create_before
 562       * @var    string    source                Image source path
 563       * @var    string    destination            Thumbnail destination path
 564       * @var    string    mimetype            Image mime type
 565       * @var    float    new_width            Calculated thumbnail width
 566       * @var    float    new_height            Calculated thumbnail height
 567       * @var    bool    thumbnail_created    Set to true to skip default GD thumbnail creation
 568       * @since 3.2.4
 569       */
 570      $vars = array(
 571          'source',
 572          'destination',
 573          'mimetype',
 574          'new_width',
 575          'new_height',
 576          'thumbnail_created',
 577      );
 578      extract($phpbb_dispatcher->trigger_event('core.thumbnail_create_before', compact($vars)));
 579  
 580      if (!$thumbnail_created)
 581      {
 582          $type = get_supported_image_types($type);
 583  
 584          if ($type['gd'])
 585          {
 586              // If the type is not supported, we are not able to create a thumbnail
 587              if ($type['format'] === false)
 588              {
 589                  return false;
 590              }
 591  
 592              switch ($type['format'])
 593              {
 594                  case IMG_GIF:
 595                      $image = @imagecreatefromgif($source);
 596                  break;
 597  
 598                  case IMG_JPG:
 599                      @ini_set('gd.jpeg_ignore_warning', 1);
 600                      $image = @imagecreatefromjpeg($source);
 601                  break;
 602  
 603                  case IMG_PNG:
 604                      $image = @imagecreatefrompng($source);
 605                  break;
 606  
 607                  case IMG_WBMP:
 608                      $image = @imagecreatefromwbmp($source);
 609                  break;
 610              }
 611  
 612              if (empty($image))
 613              {
 614                  return false;
 615              }
 616  
 617              if ($type['version'] == 1)
 618              {
 619                  $new_image = imagecreate($new_width, $new_height);
 620  
 621                  if ($new_image === false)
 622                  {
 623                      return false;
 624                  }
 625  
 626                  imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 627              }
 628              else
 629              {
 630                  $new_image = imagecreatetruecolor($new_width, $new_height);
 631  
 632                  if ($new_image === false)
 633                  {
 634                      return false;
 635                  }
 636  
 637                  // Preserve alpha transparency (png for example)
 638                  @imagealphablending($new_image, false);
 639                  @imagesavealpha($new_image, true);
 640  
 641                  imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
 642              }
 643  
 644              // If we are in safe mode create the destination file prior to using the gd functions to circumvent a PHP bug
 645              if (@ini_get('safe_mode') || @strtolower(ini_get('safe_mode')) == 'on')
 646              {
 647                  @touch($destination);
 648              }
 649  
 650              switch ($type['format'])
 651              {
 652                  case IMG_GIF:
 653                      imagegif($new_image, $destination);
 654                  break;
 655  
 656                  case IMG_JPG:
 657                      imagejpeg($new_image, $destination, 90);
 658                  break;
 659  
 660                  case IMG_PNG:
 661                      imagepng($new_image, $destination);
 662                  break;
 663  
 664                  case IMG_WBMP:
 665                      imagewbmp($new_image, $destination);
 666                  break;
 667              }
 668  
 669              imagedestroy($new_image);
 670          }
 671          else
 672          {
 673              return false;
 674          }
 675      }
 676  
 677      if (!file_exists($destination))
 678      {
 679          return false;
 680      }
 681  
 682      try
 683      {
 684          $phpbb_filesystem->phpbb_chmod($destination, CHMOD_READ | CHMOD_WRITE);
 685      }
 686      catch (\phpbb\filesystem\exception\filesystem_exception $e)
 687      {
 688          // Do nothing
 689      }
 690  
 691      return true;
 692  }
 693  
 694  /**
 695  * Assign Inline attachments (build option fields)
 696  */
 697  function posting_gen_inline_attachments(&$attachment_data)
 698  {
 699      global $template;
 700  
 701      if (count($attachment_data))
 702      {
 703          $s_inline_attachment_options = '';
 704  
 705          foreach ($attachment_data as $i => $attachment)
 706          {
 707              $s_inline_attachment_options .= '<option value="' . $i . '">' . utf8_basename($attachment['real_filename']) . '</option>';
 708          }
 709  
 710          $template->assign_var('S_INLINE_ATTACHMENT_OPTIONS', $s_inline_attachment_options);
 711  
 712          return true;
 713      }
 714  
 715      return false;
 716  }
 717  
 718  /**
 719  * Generate inline attachment entry
 720  */
 721  function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_attach_box = true)
 722  {
 723      global $template, $config, $phpbb_root_path, $phpEx, $user, $phpbb_dispatcher;
 724  
 725      // Some default template variables
 726      $template->assign_vars(array(
 727          'S_SHOW_ATTACH_BOX'    => $show_attach_box,
 728          'S_HAS_ATTACHMENTS'    => count($attachment_data),
 729          'FILESIZE'            => $config['max_filesize'],
 730          'FILE_COMMENT'        => (isset($filename_data['filecomment'])) ? $filename_data['filecomment'] : '',
 731      ));
 732  
 733      if (count($attachment_data))
 734      {
 735          // We display the posted attachments within the desired order.
 736          ($config['display_order']) ? krsort($attachment_data) : ksort($attachment_data);
 737  
 738          $attachrow_template_vars = [];
 739  
 740          foreach ($attachment_data as $count => $attach_row)
 741          {
 742              $hidden = '';
 743              $attach_row['real_filename'] = utf8_basename($attach_row['real_filename']);
 744  
 745              foreach ($attach_row as $key => $value)
 746              {
 747                  $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
 748              }
 749  
 750              $download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'mode=view&amp;id=' . (int) $attach_row['attach_id'], true, ($attach_row['is_orphan']) ? $user->session_id : false);
 751  
 752              $attachrow_template_vars[(int) $attach_row['attach_id']] = array(
 753                  'FILENAME'            => utf8_basename($attach_row['real_filename']),
 754                  'A_FILENAME'        => addslashes(utf8_basename($attach_row['real_filename'])),
 755                  'FILE_COMMENT'        => $attach_row['attach_comment'],
 756                  'ATTACH_ID'            => $attach_row['attach_id'],
 757                  'S_IS_ORPHAN'        => $attach_row['is_orphan'],
 758                  'ASSOC_INDEX'        => $count,
 759                  'FILESIZE'            => get_formatted_filesize($attach_row['filesize']),
 760  
 761                  'U_VIEW_ATTACHMENT'    => $download_link,
 762                  'S_HIDDEN'            => $hidden,
 763              );
 764          }
 765  
 766          /**
 767          * Modify inline attachments template vars
 768          *
 769          * @event core.modify_inline_attachments_template_vars
 770          * @var    array    attachment_data                Array containing attachments data
 771          * @var    array    attachrow_template_vars        Array containing attachments template vars
 772          * @since 3.2.2-RC1
 773          */
 774          $vars = array('attachment_data', 'attachrow_template_vars');
 775          extract($phpbb_dispatcher->trigger_event('core.modify_inline_attachments_template_vars', compact($vars)));
 776  
 777          $template->assign_block_vars_array('attach_row', $attachrow_template_vars);
 778      }
 779  
 780      return count($attachment_data);
 781  }
 782  
 783  //
 784  // General Post functions
 785  //
 786  
 787  /**
 788  * Load Drafts
 789  */
 790  function load_drafts($topic_id = 0, $forum_id = 0, $id = 0, $pm_action = '', $msg_id = 0)
 791  {
 792      global $user, $db, $template, $auth;
 793      global $phpbb_root_path, $phpbb_dispatcher, $phpEx;
 794  
 795      $topic_ids = $forum_ids = $draft_rows = array();
 796  
 797      // Load those drafts not connected to forums/topics
 798      // If forum_id == 0 AND topic_id == 0 then this is a PM draft
 799      if (!$topic_id && !$forum_id)
 800      {
 801          $sql_and = ' AND d.forum_id = 0 AND d.topic_id = 0';
 802      }
 803      else
 804      {
 805          $sql_and = '';
 806          $sql_and .= ($forum_id) ? ' AND d.forum_id = ' . (int) $forum_id : '';
 807          $sql_and .= ($topic_id) ? ' AND d.topic_id = ' . (int) $topic_id : '';
 808      }
 809  
 810      $sql = 'SELECT d.*, f.forum_id, f.forum_name
 811          FROM ' . DRAFTS_TABLE . ' d
 812          LEFT JOIN ' . FORUMS_TABLE . ' f ON (f.forum_id = d.forum_id)
 813              WHERE d.user_id = ' . $user->data['user_id'] . "
 814              $sql_and
 815          ORDER BY d.save_time DESC";
 816      $result = $db->sql_query($sql);
 817  
 818      while ($row = $db->sql_fetchrow($result))
 819      {
 820          if ($row['topic_id'])
 821          {
 822              $topic_ids[] = (int) $row['topic_id'];
 823          }
 824          $draft_rows[] = $row;
 825      }
 826      $db->sql_freeresult($result);
 827  
 828      if (!count($draft_rows))
 829      {
 830          return;
 831      }
 832  
 833      $topic_rows = array();
 834      if (count($topic_ids))
 835      {
 836          $sql = 'SELECT topic_id, forum_id, topic_title, topic_poster
 837              FROM ' . TOPICS_TABLE . '
 838              WHERE ' . $db->sql_in_set('topic_id', array_unique($topic_ids));
 839          $result = $db->sql_query($sql);
 840  
 841          while ($row = $db->sql_fetchrow($result))
 842          {
 843              $topic_rows[$row['topic_id']] = $row;
 844          }
 845          $db->sql_freeresult($result);
 846      }
 847  
 848      /**
 849      * Drafts found and their topics
 850      * Edit $draft_rows in order to add or remove drafts loaded
 851      *
 852      * @event core.load_drafts_draft_list_result
 853      * @var    array    draft_rows            The drafts query result. Includes its forum id and everything about the draft
 854      * @var    array    topic_ids            The list of topics got from the topics table
 855      * @var    array    topic_rows            The topics that draft_rows references
 856      * @since 3.1.0-RC3
 857      */
 858      $vars = array('draft_rows', 'topic_ids', 'topic_rows');
 859      extract($phpbb_dispatcher->trigger_event('core.load_drafts_draft_list_result', compact($vars)));
 860  
 861      unset($topic_ids);
 862  
 863      $template->assign_var('S_SHOW_DRAFTS', true);
 864  
 865      foreach ($draft_rows as $draft)
 866      {
 867          $link_topic = $link_forum = $link_pm = false;
 868          $view_url = $title = '';
 869  
 870          if (isset($topic_rows[$draft['topic_id']])
 871              && (
 872                  ($topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_get('f_read', $topic_rows[$draft['topic_id']]['forum_id']))
 873                  ||
 874                  (!$topic_rows[$draft['topic_id']]['forum_id'] && $auth->acl_getf_global('f_read'))
 875              ))
 876          {
 877              $topic_forum_id = ($topic_rows[$draft['topic_id']]['forum_id']) ? $topic_rows[$draft['topic_id']]['forum_id'] : $forum_id;
 878  
 879              $link_topic = true;
 880              $view_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id']);
 881              $title = $topic_rows[$draft['topic_id']]['topic_title'];
 882  
 883              $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $topic_forum_id . '&amp;t=' . $draft['topic_id'] . '&amp;mode=reply&amp;d=' . $draft['draft_id']);
 884          }
 885          else if ($draft['forum_id'] && $auth->acl_get('f_read', $draft['forum_id']))
 886          {
 887              $link_forum = true;
 888              $view_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $draft['forum_id']);
 889              $title = $draft['forum_name'];
 890  
 891              $insert_url = append_sid("{$phpbb_root_path}posting.$phpEx", 'f=' . $draft['forum_id'] . '&amp;mode=post&amp;d=' . $draft['draft_id']);
 892          }
 893          else
 894          {
 895              // Either display as PM draft if forum_id and topic_id are empty or if access to the forums has been denied afterwards...
 896              $link_pm = true;
 897              $insert_url = append_sid("{$phpbb_root_path}ucp.$phpEx", "i=$id&amp;mode=compose&amp;d={$draft['draft_id']}" . (($pm_action) ? "&amp;action=$pm_action" : '') . (($msg_id) ? "&amp;p=$msg_id" : ''));
 898          }
 899  
 900          $template->assign_block_vars('draftrow', array(
 901              'DRAFT_ID'        => $draft['draft_id'],
 902              'DATE'            => $user->format_date($draft['save_time']),
 903              'DRAFT_SUBJECT'    => $draft['draft_subject'],
 904  
 905              'TITLE'            => $title,
 906              'U_VIEW'        => $view_url,
 907              'U_INSERT'        => $insert_url,
 908  
 909              'S_LINK_PM'        => $link_pm,
 910              'S_LINK_TOPIC'    => $link_topic,
 911              'S_LINK_FORUM'    => $link_forum)
 912          );
 913      }
 914  }
 915  
 916  /**
 917  * Topic Review
 918  */
 919  function topic_review($topic_id, $forum_id, $mode = 'topic_review', $cur_post_id = 0, $show_quote_button = true)
 920  {
 921      global $user, $auth, $db, $template;
 922      global $config, $phpbb_root_path, $phpEx, $phpbb_container, $phpbb_dispatcher;
 923  
 924      /* @var $phpbb_content_visibility \phpbb\content_visibility */
 925      $phpbb_content_visibility = $phpbb_container->get('content.visibility');
 926      $sql_sort = ($mode == 'post_review') ? 'ASC' : 'DESC';
 927  
 928      // Go ahead and pull all data for this topic
 929      $sql = 'SELECT p.post_id
 930          FROM ' . POSTS_TABLE . ' p' . "
 931          WHERE p.topic_id = $topic_id
 932              AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id, 'p.') . '
 933              ' . (($mode == 'post_review') ? " AND p.post_id > $cur_post_id" : '') . '
 934              ' . (($mode == 'post_review_edit') ? " AND p.post_id = $cur_post_id" : '') . '
 935          ORDER BY p.post_time ' . $sql_sort . ', p.post_id ' . $sql_sort;
 936      $result = $db->sql_query_limit($sql, $config['posts_per_page']);
 937  
 938      $post_list = array();
 939  
 940      while ($row = $db->sql_fetchrow($result))
 941      {
 942          $post_list[] = $row['post_id'];
 943      }
 944  
 945      $db->sql_freeresult($result);
 946  
 947      if (!count($post_list))
 948      {
 949          return false;
 950      }
 951  
 952      // Handle 'post_review_edit' like 'post_review' from now on
 953      if ($mode == 'post_review_edit')
 954      {
 955          $mode = 'post_review';
 956      }
 957  
 958      $sql_ary = array(
 959          'SELECT'    => 'u.username, u.user_id, u.user_colour, p.*, z.friend, z.foe, uu.username as post_delete_username, uu.user_colour as post_delete_user_colour',
 960  
 961          'FROM'        => array(
 962              USERS_TABLE        => 'u',
 963              POSTS_TABLE        => 'p',
 964          ),
 965  
 966          'LEFT_JOIN'    => array(
 967              array(
 968                  'FROM'    => array(ZEBRA_TABLE => 'z'),
 969                  'ON'    => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id',
 970              ),
 971              array(
 972                  'FROM'    => array(USERS_TABLE => 'uu'),
 973                  'ON'    => 'uu.user_id = p.post_delete_user',
 974              ),
 975          ),
 976  
 977          'WHERE'        => $db->sql_in_set('p.post_id', $post_list) . '
 978              AND u.user_id = p.poster_id',
 979      );
 980  
 981      /**
 982      * Event to modify the SQL query for topic reviews
 983      *
 984      * @event core.topic_review_modify_sql_ary
 985      * @var    int        topic_id            The topic ID that is being reviewed
 986      * @var    int        forum_id            The topic's forum ID
 987      * @var    string    mode                The topic review mode
 988      * @var    int        cur_post_id            Post offset ID
 989      * @var    bool    show_quote_button    Flag indicating if the quote button should be displayed
 990      * @var    array    post_list            Array with the post IDs
 991      * @var    array    sql_ary                Array with the SQL query
 992      * @since 3.2.8-RC1
 993      */
 994      $vars = array(
 995          'topic_id',
 996          'forum_id',
 997          'mode',
 998          'cur_post_id',
 999          'show_quote_button',
1000          'post_list',
1001          'sql_ary',
1002      );
1003      extract($phpbb_dispatcher->trigger_event('core.topic_review_modify_sql_ary', compact($vars)));
1004  
1005      $sql = $db->sql_build_query('SELECT', $sql_ary);
1006      $result = $db->sql_query($sql);
1007  
1008      $rowset = array();
1009      $has_attachments = false;
1010      while ($row = $db->sql_fetchrow($result))
1011      {
1012          $rowset[$row['post_id']] = $row;
1013  
1014          if ($row['post_attachment'])
1015          {
1016              $has_attachments = true;
1017          }
1018      }
1019      $db->sql_freeresult($result);
1020  
1021      // Grab extensions
1022      $attachments = array();
1023      if ($has_attachments && $auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id))
1024      {
1025          // Get attachments...
1026          $sql = 'SELECT *
1027              FROM ' . ATTACHMENTS_TABLE . '
1028              WHERE ' . $db->sql_in_set('post_msg_id', $post_list) . '
1029                  AND in_message = 0
1030              ORDER BY filetime DESC, post_msg_id ASC';
1031          $result = $db->sql_query($sql);
1032  
1033          while ($row = $db->sql_fetchrow($result))
1034          {
1035              $attachments[$row['post_msg_id']][] = $row;
1036          }
1037          $db->sql_freeresult($result);
1038      }
1039  
1040      /**
1041      * Event to modify the posts list for topic reviews
1042      *
1043      * @event core.topic_review_modify_post_list
1044      * @var    array    attachments            Array with the post attachments data
1045      * @var    int        cur_post_id            Post offset ID
1046      * @var    int        forum_id            The topic's forum ID
1047      * @var    string    mode                The topic review mode
1048      * @var    array    post_list            Array with the post IDs
1049      * @var    array    rowset                Array with the posts data
1050      * @var    bool    show_quote_button    Flag indicating if the quote button should be displayed
1051      * @var    int        topic_id            The topic ID that is being reviewed
1052      * @since 3.1.9-RC1
1053      */
1054      $vars = array(
1055          'attachments',
1056          'cur_post_id',
1057          'forum_id',
1058          'mode',
1059          'post_list',
1060          'rowset',
1061          'show_quote_button',
1062          'topic_id',
1063      );
1064      extract($phpbb_dispatcher->trigger_event('core.topic_review_modify_post_list', compact($vars)));
1065  
1066      for ($i = 0, $end = count($post_list); $i < $end; ++$i)
1067      {
1068          // A non-existing rowset only happens if there was no user present for the entered poster_id
1069          // This could be a broken posts table.
1070          if (!isset($rowset[$post_list[$i]]))
1071          {
1072              continue;
1073          }
1074  
1075          $row = $rowset[$post_list[$i]];
1076  
1077          $poster_id        = $row['user_id'];
1078          $post_subject    = $row['post_subject'];
1079  
1080          $decoded_message = false;
1081  
1082          if ($show_quote_button && $auth->acl_get('f_reply', $forum_id))
1083          {
1084              $decoded_message = censor_text($row['post_text']);
1085              decode_message($decoded_message, $row['bbcode_uid']);
1086  
1087              $decoded_message = bbcode_nl2br($decoded_message);
1088          }
1089  
1090          $parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0);
1091          $parse_flags |= ($row['enable_smilies'] ? OPTION_FLAG_SMILIES : 0);
1092          $message = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
1093  
1094          if (!empty($attachments[$row['post_id']]))
1095          {
1096              $update_count = array();
1097              parse_attachments($forum_id, $message, $attachments[$row['post_id']], $update_count);
1098          }
1099  
1100          $post_subject = censor_text($post_subject);
1101  
1102          $post_anchor = ($mode == 'post_review') ? 'ppr' . $row['post_id'] : 'pr' . $row['post_id'];
1103          $u_show_post = append_sid($phpbb_root_path . 'viewtopic.' . $phpEx, "f=$forum_id&amp;t=$topic_id&amp;p={$row['post_id']}&amp;view=show#p{$row['post_id']}");
1104  
1105          $l_deleted_message = '';
1106          if ($row['post_visibility'] == ITEM_DELETED)
1107          {
1108              $display_postername = get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']);
1109  
1110              // User having deleted the post also being the post author?
1111              if (!$row['post_delete_user'] || $row['post_delete_user'] == $poster_id)
1112              {
1113                  $display_username = $display_postername;
1114              }
1115              else
1116              {
1117                  $display_username = get_username_string('full', $row['post_delete_user'], $row['post_delete_username'], $row['post_delete_user_colour']);
1118              }
1119  
1120              if ($row['post_delete_reason'])
1121              {
1122                  $l_deleted_message = $user->lang('POST_DELETED_BY_REASON', $display_postername, $display_username, $user->format_date($row['post_delete_time'], false, true), $row['post_delete_reason']);
1123              }
1124              else
1125              {
1126                  $l_deleted_message = $user->lang('POST_DELETED_BY', $display_postername, $display_username, $user->format_date($row['post_delete_time'], false, true));
1127              }
1128          }
1129  
1130          $post_row = array(
1131              'POST_AUTHOR_FULL'        => get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1132              'POST_AUTHOR_COLOUR'    => get_username_string('colour', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1133              'POST_AUTHOR'            => get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1134              'U_POST_AUTHOR'            => get_username_string('profile', $poster_id, $row['username'], $row['user_colour'], $row['post_username']),
1135  
1136              'S_HAS_ATTACHMENTS'    => (!empty($attachments[$row['post_id']])) ? true : false,
1137              'S_FRIEND'            => ($row['friend']) ? true : false,
1138              'S_IGNORE_POST'        => ($row['foe']) ? true : false,
1139              'L_IGNORE_POST'        => ($row['foe']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), "<a href=\"{$u_show_post}\" onclick=\"phpbb.toggleDisplay('{$post_anchor}', 1); return false;\">", '</a>') : '',
1140              'S_POST_DELETED'    => ($row['post_visibility'] == ITEM_DELETED) ? true : false,
1141              'L_DELETE_POST'        => $l_deleted_message,
1142  
1143              'POST_SUBJECT'        => $post_subject,
1144              'MINI_POST_IMG'        => $user->img('icon_post_target', $user->lang['POST']),
1145              'POST_DATE'            => $user->format_date($row['post_time']),
1146              'MESSAGE'            => $message,
1147              'DECODED_MESSAGE'    => $decoded_message,
1148              'POST_ID'            => $row['post_id'],
1149              'POST_TIME'            => $row['post_time'],
1150              'USER_ID'            => $row['user_id'],
1151              'U_MINI_POST'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . '#p' . $row['post_id'],
1152              '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) : '',
1153              'POSTER_QUOTE'        => ($show_quote_button && $auth->acl_get('f_reply', $forum_id)) ? addslashes(get_username_string('username', $poster_id, $row['username'], $row['user_colour'], $row['post_username'])) : '',
1154          );
1155  
1156          $current_row_number = $i;
1157  
1158          /**
1159          * Event to modify the template data block for topic reviews
1160          *
1161          * @event core.topic_review_modify_row
1162          * @var    string    mode                The review mode
1163          * @var    int        topic_id            The topic that is being reviewed
1164          * @var    int        forum_id            The topic's forum
1165          * @var    int        cur_post_id            Post offset id
1166          * @var    int        current_row_number    Number of the current row being iterated
1167          * @var    array    post_row            Template block array of the current post
1168          * @var    array    row                    Array with original post and user data
1169          * @since 3.1.4-RC1
1170          */
1171          $vars = array(
1172              'mode',
1173              'topic_id',
1174              'forum_id',
1175              'cur_post_id',
1176              'current_row_number',
1177              'post_row',
1178              'row',
1179          );
1180          extract($phpbb_dispatcher->trigger_event('core.topic_review_modify_row', compact($vars)));
1181  
1182          $template->assign_block_vars($mode . '_row', $post_row);
1183  
1184          // Display not already displayed Attachments for this post, we already parsed them. ;)
1185          if (!empty($attachments[$row['post_id']]))
1186          {
1187              foreach ($attachments[$row['post_id']] as $attachment)
1188              {
1189                  $template->assign_block_vars($mode . '_row.attachment', array(
1190                      'DISPLAY_ATTACHMENT'    => $attachment)
1191                  );
1192              }
1193          }
1194  
1195          unset($rowset[$post_list[$i]]);
1196      }
1197  
1198      if ($mode == 'topic_review')
1199      {
1200          $template->assign_var('QUOTE_IMG', $user->img('icon_post_quote', $user->lang['REPLY_WITH_QUOTE']));
1201      }
1202  
1203      return true;
1204  }
1205  
1206  //
1207  // Post handling functions
1208  //
1209  
1210  /**
1211  * Delete Post
1212  */
1213  function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $softdelete_reason = '')
1214  {
1215      global $db, $user, $phpbb_container, $phpbb_dispatcher;
1216      global $config, $phpEx, $phpbb_root_path;
1217  
1218      // Specify our post mode
1219      $post_mode = 'delete';
1220      if (($data['topic_first_post_id'] === $data['topic_last_post_id']) && ($data['topic_posts_approved'] + $data['topic_posts_unapproved'] + $data['topic_posts_softdeleted'] == 1))
1221      {
1222          $post_mode = 'delete_topic';
1223      }
1224      else if ($data['topic_first_post_id'] == $post_id)
1225      {
1226          $post_mode = 'delete_first_post';
1227      }
1228      else if ($data['topic_last_post_id'] == $post_id)
1229      {
1230          $post_mode = 'delete_last_post';
1231      }
1232      $sql_data = array();
1233      $next_post_id = false;
1234  
1235      include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1236  
1237      $db->sql_transaction('begin');
1238  
1239      // we must make sure to update forums that contain the shadow'd topic
1240      if ($post_mode == 'delete_topic')
1241      {
1242          $shadow_forum_ids = array();
1243  
1244          $sql = 'SELECT forum_id
1245              FROM ' . TOPICS_TABLE . '
1246              WHERE ' . $db->sql_in_set('topic_moved_id', $topic_id);
1247          $result = $db->sql_query($sql);
1248          while ($row = $db->sql_fetchrow($result))
1249          {
1250              if (!isset($shadow_forum_ids[(int) $row['forum_id']]))
1251              {
1252                  $shadow_forum_ids[(int) $row['forum_id']] = 1;
1253              }
1254              else
1255              {
1256                  $shadow_forum_ids[(int) $row['forum_id']]++;
1257              }
1258          }
1259          $db->sql_freeresult($result);
1260      }
1261  
1262      /* @var $phpbb_content_visibility \phpbb\content_visibility */
1263      $phpbb_content_visibility = $phpbb_container->get('content.visibility');
1264  
1265      // (Soft) delete the post
1266      if ($is_soft && ($post_mode != 'delete_topic'))
1267      {
1268          $phpbb_content_visibility->set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason, ($data['topic_first_post_id'] == $post_id), ($data['topic_last_post_id'] == $post_id));
1269      }
1270      else if (!$is_soft)
1271      {
1272          if (!delete_posts('post_id', array($post_id), false, false, false))
1273          {
1274              // Try to delete topic, we may had an previous error causing inconsistency
1275              if ($post_mode == 'delete_topic')
1276              {
1277                  delete_topics('topic_id', array($topic_id), false);
1278              }
1279              trigger_error('ALREADY_DELETED');
1280          }
1281      }
1282  
1283      $db->sql_transaction('commit');
1284  
1285      // Collect the necessary information for updating the tables
1286      $sql_data[FORUMS_TABLE] = $sql_data[TOPICS_TABLE] = '';
1287      switch ($post_mode)
1288      {
1289          case 'delete_topic':
1290  
1291              foreach ($shadow_forum_ids as $updated_forum => $topic_count)
1292              {
1293                  // counting is fun! we only have to do count($forum_ids) number of queries,
1294                  // even if the topic is moved back to where its shadow lives (we count how many times it is in a forum)
1295                  $sql = 'UPDATE ' . FORUMS_TABLE . '
1296                      SET forum_topics_approved = forum_topics_approved - ' . $topic_count . '
1297                      WHERE forum_id = ' . $updated_forum;
1298                  $db->sql_query($sql);
1299                  update_post_information('forum', $updated_forum);
1300              }
1301  
1302              if ($is_soft)
1303              {
1304                  $phpbb_content_visibility->set_topic_visibility(ITEM_DELETED, $topic_id, $forum_id, $user->data['user_id'], time(), $softdelete_reason);
1305              }
1306              else
1307              {
1308                  delete_topics('topic_id', array($topic_id), false);
1309  
1310                  $phpbb_content_visibility->remove_topic_from_statistic($data, $sql_data);
1311                  $config->increment('num_posts', -1, false);
1312  
1313                  $update_sql = update_post_information('forum', $forum_id, true);
1314                  if (count($update_sql))
1315                  {
1316                      $sql_data[FORUMS_TABLE] .= ($sql_data[FORUMS_TABLE]) ? ', ' : '';
1317                      $sql_data[FORUMS_TABLE] .= implode(', ', $update_sql[$forum_id]);
1318                  }
1319              }
1320  
1321          break;
1322  
1323          case 'delete_first_post':
1324              $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
1325                  FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1326                  WHERE p.topic_id = $topic_id
1327                      AND p.poster_id = u.user_id
1328                      AND p.post_visibility = " . ITEM_APPROVED . '
1329                  ORDER BY p.post_time ASC, p.post_id ASC';
1330              $result = $db->sql_query_limit($sql, 1);
1331              $row = $db->sql_fetchrow($result);
1332              $db->sql_freeresult($result);
1333  
1334              if (!$row)
1335              {
1336                  // No approved post, so the first is a not-approved post (unapproved or soft deleted)
1337                  $sql = 'SELECT p.post_id, p.poster_id, p.post_time, p.post_username, u.username, u.user_colour
1338                      FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
1339                      WHERE p.topic_id = $topic_id
1340                          AND p.poster_id = u.user_id
1341                      ORDER BY p.post_time ASC, p.post_id ASC";
1342                  $result = $db->sql_query_limit($sql, 1);
1343                  $row = $db->sql_fetchrow($result);
1344                  $db->sql_freeresult($result);
1345              }
1346  
1347              $next_post_id = (int) $row['post_id'];
1348  
1349              $sql_data[TOPICS_TABLE] = $db->sql_build_array('UPDATE', array(
1350                  'topic_poster'                => (int) $row['poster_id'],
1351                  'topic_first_post_id'        => (int) $row['post_id'],
1352                  'topic_first_poster_colour'    => $row['user_colour'],
1353                  'topic_first_poster_name'    => ($row['poster_id'] == ANONYMOUS) ? $row['post_username'] : $row['username'],
1354                  'topic_time'                => (int) $row['post_time'],
1355              ));
1356          break;
1357  
1358          case 'delete_last_post':
1359              if (!$is_soft)
1360              {
1361                  // Update last post information when hard deleting. Soft delete already did that by itself.
1362                  $update_sql = update_post_information('forum', $forum_id, true);
1363                  if (count($update_sql))
1364                  {
1365                      $sql_data[FORUMS_TABLE] = (($sql_data[FORUMS_TABLE]) ? $sql_data[FORUMS_TABLE] . ', ' : '') . implode(', ', $update_sql[$forum_id]);
1366                  }
1367  
1368                  $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_bumped = 0, topic_bumper = 0';
1369  
1370                  $update_sql = update_post_information('topic', $topic_id, true);
1371                  if (!empty($update_sql))
1372                  {
1373                      $sql_data[TOPICS_TABLE] .= ', ' . implode(', ', $update_sql[$topic_id]);
1374                      $next_post_id = (int) str_replace('topic_last_post_id = ', '', $update_sql[$topic_id][0]);
1375                  }
1376              }
1377  
1378              if (!$next_post_id)
1379              {
1380                  $sql = 'SELECT MAX(post_id) as last_post_id
1381                      FROM ' . POSTS_TABLE . "
1382                      WHERE topic_id = $topic_id
1383                          AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id);
1384                  $result = $db->sql_query($sql);
1385                  $next_post_id = (int) $db->sql_fetchfield('last_post_id');
1386                  $db->sql_freeresult($result);
1387              }
1388          break;
1389  
1390          case 'delete':
1391              $sql = 'SELECT post_id
1392                  FROM ' . POSTS_TABLE . "
1393                  WHERE topic_id = $topic_id
1394                      AND " . $phpbb_content_visibility->get_visibility_sql('post', $forum_id) . '
1395                      AND post_time > ' . $data['post_time'] . '
1396                  ORDER BY post_time ASC, post_id ASC';
1397              $result = $db->sql_query_limit($sql, 1);
1398              $next_post_id = (int) $db->sql_fetchfield('post_id');
1399              $db->sql_freeresult($result);
1400          break;
1401      }
1402  
1403      if (($post_mode == 'delete') || ($post_mode == 'delete_last_post') || ($post_mode == 'delete_first_post'))
1404      {
1405          if (!$is_soft)
1406          {
1407              $phpbb_content_visibility->remove_post_from_statistic($data, $sql_data);
1408          }
1409  
1410          $sql = 'SELECT 1 AS has_attachments
1411              FROM ' . ATTACHMENTS_TABLE . '
1412              WHERE topic_id = ' . $topic_id;
1413          $result = $db->sql_query_limit($sql, 1);
1414          $has_attachments = (int) $db->sql_fetchfield('has_attachments');
1415          $db->sql_freeresult($result);
1416  
1417          if (!$has_attachments)
1418          {
1419              $sql_data[TOPICS_TABLE] = (($sql_data[TOPICS_TABLE]) ? $sql_data[TOPICS_TABLE] . ', ' : '') . 'topic_attachment = 0';
1420          }
1421      }
1422  
1423      $db->sql_transaction('begin');
1424  
1425      $where_sql = array(
1426          FORUMS_TABLE    => "forum_id = $forum_id",
1427          TOPICS_TABLE    => "topic_id = $topic_id",
1428          USERS_TABLE        => 'user_id = ' . $data['poster_id'],
1429      );
1430  
1431      foreach ($sql_data as $table => $update_sql)
1432      {
1433          if ($update_sql)
1434          {
1435              $db->sql_query("UPDATE $table SET $update_sql WHERE " . $where_sql[$table]);
1436          }
1437      }
1438  
1439      // Adjust posted info for this user by looking for a post by him/her within this topic...
1440      if ($post_mode != 'delete_topic' && $config['load_db_track'] && $data['poster_id'] != ANONYMOUS)
1441      {
1442          $sql = 'SELECT poster_id
1443              FROM ' . POSTS_TABLE . '
1444              WHERE topic_id = ' . $topic_id . '
1445                  AND poster_id = ' . $data['poster_id'];
1446          $result = $db->sql_query_limit($sql, 1);
1447          $poster_id = (int) $db->sql_fetchfield('poster_id');
1448          $db->sql_freeresult($result);
1449  
1450          // The user is not having any more posts within this topic
1451          if (!$poster_id)
1452          {
1453              $sql = 'DELETE FROM ' . TOPICS_POSTED_TABLE . '
1454                  WHERE topic_id = ' . $topic_id . '
1455                      AND user_id = ' . $data['poster_id'];
1456              $db->sql_query($sql);
1457          }
1458      }
1459  
1460      $db->sql_transaction('commit');
1461  
1462      if ($data['post_reported'] && ($post_mode != 'delete_topic'))
1463      {
1464          sync('topic_reported', 'topic_id', array($topic_id));
1465      }
1466  
1467      /**
1468      * This event is used for performing actions directly after a post or topic
1469      * has been deleted.
1470      *
1471      * @event core.delete_post_after
1472      * @var    int        forum_id            Post forum ID
1473      * @var    int        topic_id            Post topic ID
1474      * @var    int        post_id                Post ID
1475      * @var    array    data                Post data
1476      * @var    bool    is_soft                Soft delete flag
1477      * @var    string    softdelete_reason    Soft delete reason
1478      * @var    string    post_mode            delete_topic, delete_first_post, delete_last_post or delete
1479      * @var    mixed    next_post_id        Next post ID in the topic (post ID or false)
1480      *
1481      * @since 3.1.11-RC1
1482      */
1483      $vars = array(
1484          'forum_id',
1485          'topic_id',
1486          'post_id',
1487          'data',
1488          'is_soft',
1489          'softdelete_reason',
1490          'post_mode',
1491          'next_post_id',
1492      );
1493      extract($phpbb_dispatcher->trigger_event('core.delete_post_after', compact($vars)));
1494  
1495      return $next_post_id;
1496  }
1497  
1498  /**
1499  * Submit Post
1500  * @todo Split up and create lightweight, simple API for this.
1501  */
1502  function submit_post($mode, $subject, $username, $topic_type, &$poll_ary, &$data_ary, $update_message = true, $update_search_index = true)
1503  {
1504      global $db, $auth, $user, $config, $phpEx, $phpbb_root_path, $phpbb_container, $phpbb_dispatcher, $phpbb_log, $request;
1505  
1506      $poll = $poll_ary;
1507      $data = $data_ary;
1508      /**
1509      * Modify the data for post submitting
1510      *
1511      * @event core.modify_submit_post_data
1512      * @var    string    mode                Variable containing posting mode value
1513      * @var    string    subject                Variable containing post subject value
1514      * @var    string    username            Variable containing post author name
1515      * @var    int        topic_type            Variable containing topic type value
1516      * @var    array    poll                Array with the poll data for the post
1517      * @var    array    data                Array with the data for the post
1518      * @var    bool    update_message        Flag indicating if the post will be updated
1519      * @var    bool    update_search_index    Flag indicating if the search index will be updated
1520      * @since 3.1.0-a4
1521      */
1522      $vars = array(
1523          'mode',
1524          'subject',
1525          'username',
1526          'topic_type',
1527          'poll',
1528          'data',
1529          'update_message',
1530          'update_search_index',
1531      );
1532      extract($phpbb_dispatcher->trigger_event('core.modify_submit_post_data', compact($vars)));
1533      $poll_ary = $poll;
1534      $data_ary = $data;
1535      unset($poll);
1536      unset($data);
1537  
1538      // We do not handle erasing posts here
1539      if ($mode == 'delete')
1540      {
1541          return false;
1542      }
1543  
1544      if (!empty($data_ary['post_time']))
1545      {
1546          $current_time = $data_ary['post_time'];
1547      }
1548      else
1549      {
1550          $current_time = time();
1551      }
1552  
1553      if ($mode == 'post')
1554      {
1555          $post_mode = 'post';
1556          $update_message = true;
1557      }
1558      else if ($mode != 'edit')
1559      {
1560          $post_mode = 'reply';
1561          $update_message = true;
1562      }
1563      else if ($mode == 'edit')
1564      {
1565          $post_mode = ($data_ary['topic_posts_approved'] + $data_ary['topic_posts_unapproved'] + $data_ary['topic_posts_softdeleted'] == 1) ? 'edit_topic' : (($data_ary['topic_first_post_id'] == $data_ary['post_id']) ? 'edit_first_post' : (($data_ary['topic_last_post_id'] == $data_ary['post_id']) ? 'edit_last_post' : 'edit'));
1566      }
1567  
1568      // First of all make sure the subject and topic title are having the correct length.
1569      // To achieve this without cutting off between special chars we convert to an array and then count the elements.
1570      $subject = truncate_string($subject, 120);
1571      $data_ary['topic_title'] = truncate_string($data_ary['topic_title'], 120);
1572  
1573      // Collect some basic information about which tables and which rows to update/insert
1574      $sql_data = $topic_row = array();
1575      $poster_id = ($mode == 'edit') ? $data_ary['poster_id'] : (int) $user->data['user_id'];
1576  
1577      // Retrieve some additional information if not present
1578      if ($mode == 'edit' && (!isset($data_ary['post_visibility']) || !isset($data_ary['topic_visibility']) || $data_ary['post_visibility'] === false || $data_ary['topic_visibility'] === false))
1579      {
1580          $sql = 'SELECT p.post_visibility, t.topic_type, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted, t.topic_visibility
1581              FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
1582              WHERE t.topic_id = p.topic_id
1583                  AND p.post_id = ' . $data_ary['post_id'];
1584          $result = $db->sql_query($sql);
1585          $topic_row = $db->sql_fetchrow($result);
1586          $db->sql_freeresult($result);
1587  
1588          $data_ary['topic_visibility'] = $topic_row['topic_visibility'];
1589          $data_ary['post_visibility'] = $topic_row['post_visibility'];
1590      }
1591  
1592      // This variable indicates if the user is able to post or put into the queue
1593      $post_visibility = ITEM_APPROVED;
1594  
1595      // Check the permissions for post approval.
1596      // Moderators must go through post approval like ordinary users.
1597      if (!$auth->acl_get('f_noapprove', $data_ary['forum_id']))
1598      {
1599          // Post not approved, but in queue
1600          $post_visibility = ITEM_UNAPPROVED;
1601          switch ($post_mode)
1602          {
1603              case 'edit_first_post':
1604              case 'edit':
1605              case 'edit_last_post':
1606              case 'edit_topic':
1607                  $post_visibility = ITEM_REAPPROVE;
1608              break;
1609          }
1610      }
1611      else if (isset($data_ary['post_visibility']) && $data_ary['post_visibility'] !== false)
1612      {
1613          $post_visibility = $data_ary['post_visibility'];
1614      }
1615  
1616      // MODs/Extensions are able to force any visibility on posts
1617      if (isset($data_ary['force_approved_state']))
1618      {
1619          $post_visibility = (in_array((int) $data_ary['force_approved_state'], array(ITEM_APPROVED, ITEM_UNAPPROVED, ITEM_DELETED, ITEM_REAPPROVE))) ? (int) $data_ary['force_approved_state'] : $post_visibility;
1620      }
1621      if (isset($data_ary['force_visibility']))
1622      {
1623          $post_visibility = (in_array((int) $data_ary['force_visibility'], array(ITEM_APPROVED, ITEM_UNAPPROVED, ITEM_DELETED, ITEM_REAPPROVE))) ? (int) $data_ary['force_visibility'] : $post_visibility;
1624      }
1625  
1626      // Start the transaction here
1627      $db->sql_transaction('begin');
1628  
1629      // Collect Information
1630      switch ($post_mode)
1631      {
1632          case 'post':
1633          case 'reply':
1634              $sql_data[POSTS_TABLE]['sql'] = array(
1635                  'forum_id'            => $data_ary['forum_id'],
1636                  'poster_id'            => (int) $user->data['user_id'],
1637                  'icon_id'            => $data_ary['icon_id'],
1638                  'poster_ip'            => $user->ip,
1639                  'post_time'            => $current_time,
1640                  'post_visibility'    => $post_visibility,
1641                  'enable_bbcode'        => $data_ary['enable_bbcode'],
1642                  'enable_smilies'    => $data_ary['enable_smilies'],
1643                  'enable_magic_url'    => $data_ary['enable_urls'],
1644                  'enable_sig'        => $data_ary['enable_sig'],
1645                  'post_username'        => (!$user->data['is_registered']) ? $username : '',
1646                  'post_subject'        => $subject,
1647                  'post_text'            => $data_ary['message'],
1648                  'post_checksum'        => $data_ary['message_md5'],
1649                  'post_attachment'    => (!empty($data_ary['attachment_data'])) ? 1 : 0,
1650                  'bbcode_bitfield'    => $data_ary['bbcode_bitfield'],
1651                  'bbcode_uid'        => $data_ary['bbcode_uid'],
1652                  'post_postcount'    => ($auth->acl_get('f_postcount', $data_ary['forum_id'])) ? 1 : 0,
1653                  'post_edit_locked'    => $data_ary['post_edit_locked']
1654              );
1655          break;
1656  
1657          case 'edit_first_post':
1658          case 'edit':
1659  
1660          case 'edit_last_post':
1661          case 'edit_topic':
1662  
1663              // If edit reason is given always display edit info
1664  
1665              // If editing last post then display no edit info
1666              // If m_edit permission then display no edit info
1667              // If normal edit display edit info
1668  
1669              // Display edit info if edit reason given or user is editing his post, which is not the last within the topic.
1670              if ($data_ary['post_edit_reason'] || (!$auth->acl_get('m_edit', $data_ary['forum_id']) && ($post_mode == 'edit' || $post_mode == 'edit_first_post')))
1671              {
1672                  $data_ary['post_edit_reason']        = truncate_string($data_ary['post_edit_reason'], 255, 255, false);
1673  
1674                  $sql_data[POSTS_TABLE]['sql']    = array(
1675                      'post_edit_time'    => $current_time,
1676                      'post_edit_reason'    => $data_ary['post_edit_reason'],
1677                      'post_edit_user'    => (int) $data_ary['post_edit_user'],
1678                  );
1679  
1680                  $sql_data[POSTS_TABLE]['stat'][] = 'post_edit_count = post_edit_count + 1';
1681              }
1682              else if (!$data_ary['post_edit_reason'] && $mode == 'edit' && $auth->acl_get('m_edit', $data_ary['forum_id']))
1683              {
1684                  $sql_data[POSTS_TABLE]['sql'] = array(
1685                      'post_edit_reason'    => '',
1686                  );
1687              }
1688  
1689              // If the person editing this post is different to the one having posted then we will add a log entry stating the edit
1690              // Could be simplified by only adding to the log if the edit is not tracked - but this may confuse admins/mods
1691              if ($user->data['user_id'] != $poster_id)
1692              {
1693                  $log_subject = ($subject) ? $subject : $data_ary['topic_title'];
1694                  $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_POST_EDITED', false, array(
1695                      'forum_id' => $data_ary['forum_id'],
1696                      'topic_id' => $data_ary['topic_id'],
1697                      'post_id'  => $data_ary['post_id'],
1698                      $log_subject,
1699                      (!empty($username)) ? $username : $user->lang['GUEST'],
1700                      $data_ary['post_edit_reason']
1701                  ));
1702              }
1703  
1704              if (!isset($sql_data[POSTS_TABLE]['sql']))
1705              {
1706                  $sql_data[POSTS_TABLE]['sql'] = array();
1707              }
1708  
1709              $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1710                  'forum_id'            => $data_ary['forum_id'],
1711                  'poster_id'            => $data_ary['poster_id'],
1712                  'icon_id'            => $data_ary['icon_id'],
1713                  // We will change the visibility later
1714                  //'post_visibility'    => $post_visibility,
1715                  'enable_bbcode'        => $data_ary['enable_bbcode'],
1716                  'enable_smilies'    => $data_ary['enable_smilies'],
1717                  'enable_magic_url'    => $data_ary['enable_urls'],
1718                  'enable_sig'        => $data_ary['enable_sig'],
1719                  'post_username'        => ($username && $data_ary['poster_id'] == ANONYMOUS) ? $username : '',
1720                  'post_subject'        => $subject,
1721                  'post_checksum'        => $data_ary['message_md5'],
1722                  'post_attachment'    => (!empty($data_ary['attachment_data'])) ? 1 : 0,
1723                  'bbcode_bitfield'    => $data_ary['bbcode_bitfield'],
1724                  'bbcode_uid'        => $data_ary['bbcode_uid'],
1725                  'post_edit_locked'    => $data_ary['post_edit_locked'])
1726              );
1727  
1728              if ($update_message)
1729              {
1730                  $sql_data[POSTS_TABLE]['sql']['post_text'] = $data_ary['message'];
1731              }
1732  
1733          break;
1734      }
1735  
1736      // And the topic ladies and gentlemen
1737      switch ($post_mode)
1738      {
1739          case 'post':
1740              $sql_data[TOPICS_TABLE]['sql'] = array(
1741                  'topic_poster'                => (int) $user->data['user_id'],
1742                  'topic_time'                => $current_time,
1743                  'topic_last_view_time'        => $current_time,
1744                  'forum_id'                    => $data_ary['forum_id'],
1745                  'icon_id'                    => $data_ary['icon_id'],
1746                  'topic_posts_approved'        => ($post_visibility == ITEM_APPROVED) ? 1 : 0,
1747                  'topic_posts_softdeleted'    => ($post_visibility == ITEM_DELETED) ? 1 : 0,
1748                  'topic_posts_unapproved'    => ($post_visibility == ITEM_UNAPPROVED) ? 1 : 0,
1749                  'topic_visibility'            => $post_visibility,
1750                  'topic_delete_user'            => ($post_visibility != ITEM_APPROVED) ? (int) $user->data['user_id'] : 0,
1751                  'topic_title'                => $subject,
1752                  'topic_first_poster_name'    => (!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : ''),
1753                  'topic_first_poster_colour'    => $user->data['user_colour'],
1754                  'topic_type'                => $topic_type,
1755                  'topic_time_limit'            => $topic_type != POST_NORMAL ? ($data_ary['topic_time_limit'] * 86400) : 0,
1756                  'topic_attachment'            => (!empty($data_ary['attachment_data'])) ? 1 : 0,
1757                  'topic_status'                => (isset($data_ary['topic_status'])) ? $data_ary['topic_status'] : ITEM_UNLOCKED,
1758              );
1759  
1760              if (isset($poll_ary['poll_options']) && !empty($poll_ary['poll_options']))
1761              {
1762                  $poll_start = ($poll_ary['poll_start']) ? $poll_ary['poll_start'] : $current_time;
1763                  $poll_length = $poll_ary['poll_length'] * 86400;
1764                  if ($poll_length < 0)
1765                  {
1766                      $poll_start = $poll_start + $poll_length;
1767                      if ($poll_start < 0)
1768                      {
1769                          $poll_start = 0;
1770                      }
1771                      $poll_length = 1;
1772                  }
1773  
1774                  $sql_data[TOPICS_TABLE]['sql'] = array_merge($sql_data[TOPICS_TABLE]['sql'], array(
1775                      'poll_title'        => $poll_ary['poll_title'],
1776                      'poll_start'        => $poll_start,
1777                      'poll_max_options'    => $poll_ary['poll_max_options'],
1778                      'poll_length'        => $poll_length,
1779                      'poll_vote_change'    => $poll_ary['poll_vote_change'])
1780                  );
1781              }
1782  
1783              $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data_ary['forum_id']) && $post_visibility == ITEM_APPROVED) ? ', user_posts = user_posts + 1' : '');
1784  
1785              if ($post_visibility == ITEM_APPROVED)
1786              {
1787                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_approved = forum_topics_approved + 1';
1788                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_approved = forum_posts_approved + 1';
1789              }
1790              else if ($post_visibility == ITEM_UNAPPROVED)
1791              {
1792                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_unapproved = forum_topics_unapproved + 1';
1793                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_unapproved = forum_posts_unapproved + 1';
1794              }
1795              else if ($post_visibility == ITEM_DELETED)
1796              {
1797                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_topics_softdeleted = forum_topics_softdeleted + 1';
1798                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_softdeleted = forum_posts_softdeleted + 1';
1799              }
1800          break;
1801  
1802          case 'reply':
1803              $sql_data[TOPICS_TABLE]['stat'][] = 'topic_last_view_time = ' . $current_time . ',
1804                  topic_bumped = 0,
1805                  topic_bumper = 0' .
1806                  (($post_visibility == ITEM_APPROVED) ? ', topic_posts_approved = topic_posts_approved + 1' : '') .
1807                  (($post_visibility == ITEM_UNAPPROVED) ? ', topic_posts_unapproved = topic_posts_unapproved + 1' : '') .
1808                  (($post_visibility == ITEM_DELETED) ? ', topic_posts_softdeleted = topic_posts_softdeleted + 1' : '') .
1809                  ((!empty($data_ary['attachment_data']) || (isset($data_ary['topic_attachment']) && $data_ary['topic_attachment'])) ? ', topic_attachment = 1' : '');
1810  
1811              $sql_data[USERS_TABLE]['stat'][] = "user_lastpost_time = $current_time" . (($auth->acl_get('f_postcount', $data_ary['forum_id']) && $post_visibility == ITEM_APPROVED) ? ', user_posts = user_posts + 1' : '');
1812  
1813              if ($post_visibility == ITEM_APPROVED)
1814              {
1815                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_approved = forum_posts_approved + 1';
1816              }
1817              else if ($post_visibility == ITEM_UNAPPROVED)
1818              {
1819                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_unapproved = forum_posts_unapproved + 1';
1820              }
1821              else if ($post_visibility == ITEM_DELETED)
1822              {
1823                  $sql_data[FORUMS_TABLE]['stat'][] = 'forum_posts_softdeleted = forum_posts_softdeleted + 1';
1824              }
1825          break;
1826  
1827          case 'edit_topic':
1828          case 'edit_first_post':
1829              if (isset($poll_ary['poll_options']))
1830              {
1831                  $poll_start = ($poll_ary['poll_start'] || empty($poll_ary['poll_options'])) ? $poll_ary['poll_start'] : $current_time;
1832                  $poll_length = $poll_ary['poll_length'] * 86400;
1833                  if ($poll_length < 0)
1834                  {
1835                      $poll_start = $poll_start + $poll_length;
1836                      if ($poll_start < 0)
1837                      {
1838                          $poll_start = 0;
1839                      }
1840                      $poll_length = 1;
1841                  }
1842              }
1843  
1844              $sql_data[TOPICS_TABLE]['sql'] = array(
1845                  'forum_id'                    => $data_ary['forum_id'],
1846                  'icon_id'                    => $data_ary['icon_id'],
1847                  'topic_title'                => $subject,
1848                  'topic_first_poster_name'    => $username,
1849                  'topic_type'                => $topic_type,
1850                  'topic_time_limit'            => $topic_type != POST_NORMAL ? ($data_ary['topic_time_limit'] * 86400) : 0,
1851                  'poll_title'                => (isset($poll_ary['poll_options'])) ? $poll_ary['poll_title'] : '',
1852                  'poll_start'                => (isset($poll_ary['poll_options'])) ? $poll_start : 0,
1853                  'poll_max_options'            => (isset($poll_ary['poll_options'])) ? $poll_ary['poll_max_options'] : 1,
1854                  'poll_length'                => (isset($poll_ary['poll_options'])) ? $poll_length : 0,
1855                  'poll_vote_change'            => (isset($poll_ary['poll_vote_change'])) ? $poll_ary['poll_vote_change'] : 0,
1856                  'topic_last_view_time'        => $current_time,
1857  
1858                  'topic_attachment'            => (!empty($data_ary['attachment_data'])) ? 1 : (isset($data_ary['topic_attachment']) ? $data_ary['topic_attachment'] : 0),
1859              );
1860  
1861          break;
1862      }
1863  
1864      $poll = $poll_ary;
1865      $data = $data_ary;
1866      /**
1867      * Modify sql query data for post submitting
1868      *
1869      * @event core.submit_post_modify_sql_data
1870      * @var    array    data                Array with the data for the post
1871      * @var    array    poll                Array with the poll data for the post
1872      * @var    string    post_mode            Variable containing posting mode value
1873      * @var    bool    sql_data            Array with the data for the posting SQL query
1874      * @var    string    subject                Variable containing post subject value
1875      * @var    int        topic_type            Variable containing topic type value
1876      * @var    string    username            Variable containing post author name
1877      * @since 3.1.3-RC1
1878      */
1879      $vars = array(
1880          'data',
1881          'poll',
1882          'post_mode',
1883          'sql_data',
1884          'subject',
1885          'topic_type',
1886          'username',
1887      );
1888      extract($phpbb_dispatcher->trigger_event('core.submit_post_modify_sql_data', compact($vars)));
1889      $poll_ary = $poll;
1890      $data_ary = $data;
1891      unset($poll);
1892      unset($data);
1893  
1894      // Submit new topic
1895      if ($post_mode == 'post')
1896      {
1897          $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' .
1898              $db->sql_build_array('INSERT', $sql_data[TOPICS_TABLE]['sql']);
1899          $db->sql_query($sql);
1900  
1901          $data_ary['topic_id'] = $db->sql_nextid();
1902  
1903          $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1904              'topic_id' => $data_ary['topic_id'])
1905          );
1906          unset($sql_data[TOPICS_TABLE]['sql']);
1907      }
1908  
1909      // Submit new post
1910      if ($post_mode == 'post' || $post_mode == 'reply')
1911      {
1912          if ($post_mode == 'reply')
1913          {
1914              $sql_data[POSTS_TABLE]['sql'] = array_merge($sql_data[POSTS_TABLE]['sql'], array(
1915                  'topic_id' => $data_ary['topic_id'],
1916              ));
1917          }
1918  
1919          $sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data[POSTS_TABLE]['sql']);
1920          $db->sql_query($sql);
1921          $data_ary['post_id'] = $db->sql_nextid();
1922  
1923          if ($post_mode == 'post' || $post_visibility == ITEM_APPROVED)
1924          {
1925              $sql_data[TOPICS_TABLE]['sql'] = array(
1926                  'topic_last_post_id'        => $data_ary['post_id'],
1927                  'topic_last_post_time'        => $current_time,
1928                  'topic_last_poster_id'        => $sql_data[POSTS_TABLE]['sql']['poster_id'],
1929                  'topic_last_poster_name'    => ($user->data['user_id'] == ANONYMOUS) ? $sql_data[POSTS_TABLE]['sql']['post_username'] : $user->data['username'],
1930                  'topic_last_poster_colour'    => $user->data['user_colour'],
1931                  'topic_last_post_subject'    => (string) $subject,
1932              );
1933          }
1934  
1935          if ($post_mode == 'post')
1936          {
1937              $sql_data[TOPICS_TABLE]['sql']['topic_first_post_id'] = $data_ary['post_id'];
1938          }
1939  
1940          // Update total post count and forum information
1941          if ($post_visibility == ITEM_APPROVED)
1942          {
1943              if ($post_mode == 'post')
1944              {
1945                  $config->increment('num_topics', 1, false);
1946              }
1947              $config->increment('num_posts', 1, false);
1948  
1949              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_id = ' . $data_ary['post_id'];
1950              $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
1951              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_post_time = ' . $current_time;
1952              $sql_data[FORUMS_TABLE]['stat'][] = 'forum_last_poster_id = ' . (int) $user->data['user_id'];
1953              $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape((!$user->data['is_registered'] && $username) ? $username : (($user->data['user_id'] != ANONYMOUS) ? $user->data['username'] : '')) . "'";
1954              $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_colour = '" . $db->sql_escape($user->data['user_colour']) . "'";
1955          }
1956  
1957          unset($sql_data[POSTS_TABLE]['sql']);
1958      }
1959  
1960      // Update the topics table
1961      if (isset($sql_data[TOPICS_TABLE]['sql']))
1962      {
1963          $sql = 'UPDATE ' . TOPICS_TABLE . '
1964              SET ' . $db->sql_build_array('UPDATE', $sql_data[TOPICS_TABLE]['sql']) . '
1965              WHERE topic_id = ' . $data_ary['topic_id'];
1966          $db->sql_query($sql);
1967  
1968          unset($sql_data[TOPICS_TABLE]['sql']);
1969      }
1970  
1971      // Update the posts table
1972      if (isset($sql_data[POSTS_TABLE]['sql']))
1973      {
1974          $sql = 'UPDATE ' . POSTS_TABLE . '
1975              SET ' . $db->sql_build_array('UPDATE', $sql_data[POSTS_TABLE]['sql']) . '
1976              WHERE post_id = ' . $data_ary['post_id'];
1977          $db->sql_query($sql);
1978  
1979          unset($sql_data[POSTS_TABLE]['sql']);
1980      }
1981  
1982      // Update Poll Tables
1983      if (isset($poll_ary['poll_options']))
1984      {
1985          $cur_poll_options = array();
1986  
1987          if ($mode == 'edit')
1988          {
1989              $sql = 'SELECT *
1990                  FROM ' . POLL_OPTIONS_TABLE . '
1991                  WHERE topic_id = ' . $data_ary['topic_id'] . '
1992                  ORDER BY poll_option_id';
1993              $result = $db->sql_query($sql);
1994  
1995              $cur_poll_options = array();
1996              while ($row = $db->sql_fetchrow($result))
1997              {
1998                  $cur_poll_options[] = $row;
1999              }
2000              $db->sql_freeresult($result);
2001          }
2002  
2003          $sql_insert_ary = array();
2004  
2005          for ($i = 0, $size = count($poll_ary['poll_options']); $i < $size; $i++)
2006          {
2007              if (strlen(trim($poll_ary['poll_options'][$i])))
2008              {
2009                  if (empty($cur_poll_options[$i]))
2010                  {
2011                      // If we add options we need to put them to the end to be able to preserve votes...
2012                      $sql_insert_ary[] = array(
2013                          'poll_option_id'    => (int) count($cur_poll_options) + 1 + count($sql_insert_ary),
2014                          'topic_id'            => (int) $data_ary['topic_id'],
2015                          'poll_option_text'    => (string) $poll_ary['poll_options'][$i]
2016                      );
2017                  }
2018                  else if ($poll_ary['poll_options'][$i] != $cur_poll_options[$i])
2019                  {
2020                      $sql = 'UPDATE ' . POLL_OPTIONS_TABLE . "
2021                          SET poll_option_text = '" . $db->sql_escape($poll_ary['poll_options'][$i]) . "'
2022                          WHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . '
2023                              AND topic_id = ' . $data_ary['topic_id'];
2024                      $db->sql_query($sql);
2025                  }
2026              }
2027          }
2028  
2029          $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
2030  
2031          if (count($poll_ary['poll_options']) < count($cur_poll_options))
2032          {
2033              $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
2034                  WHERE poll_option_id > ' . count($poll_ary['poll_options']) . '
2035                      AND topic_id = ' . $data_ary['topic_id'];
2036              $db->sql_query($sql);
2037          }
2038  
2039          // If edited, we would need to reset votes (since options can be re-ordered above, you can't be sure if the change is for changing the text or adding an option
2040          if ($mode == 'edit' && count($poll_ary['poll_options']) != count($cur_poll_options))
2041          {
2042              $db->sql_query('DELETE FROM ' . POLL_VOTES_TABLE . ' WHERE topic_id = ' . $data_ary['topic_id']);
2043              $db->sql_query('UPDATE ' . POLL_OPTIONS_TABLE . ' SET poll_option_total = 0 WHERE topic_id = ' . $data_ary['topic_id']);
2044          }
2045      }
2046  
2047      // Submit Attachments
2048      if (!empty($data_ary['attachment_data']) && $data_ary['post_id'] && in_array($mode, array('post', 'reply', 'quote', 'edit')))
2049      {
2050          $space_taken = $files_added = 0;
2051          $orphan_rows = array();
2052  
2053          foreach ($data_ary['attachment_data'] as $pos => $attach_row)
2054          {
2055              $orphan_rows[(int) $attach_row['attach_id']] = array();
2056          }
2057  
2058          if (count($orphan_rows))
2059          {
2060              $sql = 'SELECT attach_id, filesize, physical_filename
2061                  FROM ' . ATTACHMENTS_TABLE . '
2062                  WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan_rows)) . '
2063                      AND is_orphan = 1
2064                      AND poster_id = ' . $user->data['user_id'];
2065              $result = $db->sql_query($sql);
2066  
2067              $orphan_rows = array();
2068              while ($row = $db->sql_fetchrow($result))
2069              {
2070                  $orphan_rows[$row['attach_id']] = $row;
2071              }
2072              $db->sql_freeresult($result);
2073          }
2074  
2075          foreach ($data_ary['attachment_data'] as $pos => $attach_row)
2076          {
2077              if ($attach_row['is_orphan'] && !isset($orphan_rows[$attach_row['attach_id']]))
2078              {
2079                  continue;
2080              }
2081  
2082              if (preg_match('/[\x{10000}-\x{10FFFF}]/u', $attach_row['attach_comment']))
2083              {
2084                  trigger_error('ATTACH_COMMENT_NO_EMOJIS');
2085              }
2086  
2087              if (!$attach_row['is_orphan'])
2088              {
2089                  // update entry in db if attachment already stored in db and filespace
2090                  $sql = 'UPDATE ' . ATTACHMENTS_TABLE . "
2091                      SET attach_comment = '" . $db->sql_escape($attach_row['attach_comment']) . "'
2092                      WHERE attach_id = " . (int) $attach_row['attach_id'] . '
2093                          AND is_orphan = 0';
2094                  $db->sql_query($sql);
2095              }
2096              else
2097              {
2098                  // insert attachment into db
2099                  if (!@file_exists($phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($orphan_rows[$attach_row['attach_id']]['physical_filename'])))
2100                  {
2101                      continue;
2102                  }
2103  
2104                  $space_taken += $orphan_rows[$attach_row['attach_id']]['filesize'];
2105                  $files_added++;
2106  
2107                  $attach_sql = array(
2108                      'post_msg_id'        => $data_ary['post_id'],
2109                      'topic_id'            => $data_ary['topic_id'],
2110                      'is_orphan'            => 0,
2111                      'poster_id'            => $poster_id,
2112                      'attach_comment'    => $attach_row['attach_comment'],
2113                  );
2114  
2115                  $sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $attach_sql) . '
2116                      WHERE attach_id = ' . $attach_row['attach_id'] . '
2117                          AND is_orphan = 1
2118                          AND poster_id = ' . $user->data['user_id'];
2119                  $db->sql_query($sql);
2120              }
2121          }
2122  
2123          if ($space_taken && $files_added)
2124          {
2125              $config->increment('upload_dir_size', $space_taken, false);
2126              $config->increment('num_files', $files_added, false);
2127          }
2128      }
2129  
2130      $first_post_has_topic_info = ($post_mode == 'edit_first_post' &&
2131              (($post_visibility == ITEM_DELETED && $data_ary['topic_posts_softdeleted'] == 1) ||
2132              ($post_visibility == ITEM_UNAPPROVED && $data_ary['topic_posts_unapproved'] == 1) ||
2133              ($post_visibility == ITEM_REAPPROVE && $data_ary['topic_posts_unapproved'] == 1) ||
2134              ($post_visibility == ITEM_APPROVED && $data_ary['topic_posts_approved'] == 1)));
2135      // Fix the post's and topic's visibility and first/last post information, when the post is edited
2136      if (($post_mode != 'post' && $post_mode != 'reply') && $data_ary['post_visibility'] != $post_visibility)
2137      {
2138          // If the post was not approved, it could also be the starter,
2139          // so we sync the starter after approving/restoring, to ensure that the stats are correct
2140          // Same applies for the last post
2141          $is_starter = ($post_mode == 'edit_first_post' || $post_mode == 'edit_topic' || $data_ary['post_visibility'] != ITEM_APPROVED);
2142          $is_latest = ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || $data_ary['post_visibility'] != ITEM_APPROVED);
2143  
2144          /* @var $phpbb_content_visibility \phpbb\content_visibility */
2145          $phpbb_content_visibility = $phpbb_container->get('content.visibility');
2146          $phpbb_content_visibility->set_post_visibility($post_visibility, $data_ary['post_id'], $data_ary['topic_id'], $data_ary['forum_id'], $user->data['user_id'], time(), '', $is_starter, $is_latest);
2147      }
2148      else if ($post_mode == 'edit_last_post' || $post_mode == 'edit_topic' || $first_post_has_topic_info)
2149      {
2150          if ($post_visibility == ITEM_APPROVED || $data_ary['topic_visibility'] == $post_visibility)
2151          {
2152              // only the subject can be changed from edit
2153              $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_post_subject = '" . $db->sql_escape($subject) . "'";
2154  
2155              // Maybe not only the subject, but also changing anonymous usernames. ;)
2156              if ($data_ary['poster_id'] == ANONYMOUS)
2157              {
2158                  $sql_data[TOPICS_TABLE]['stat'][] = "topic_last_poster_name = '" . $db->sql_escape($username) . "'";
2159              }
2160  
2161              if ($post_visibility == ITEM_APPROVED)
2162              {
2163                  // this does not _necessarily_ mean that we must update the info again,
2164                  // it just means that we might have to
2165                  $sql = 'SELECT forum_last_post_id, forum_last_post_subject
2166                      FROM ' . FORUMS_TABLE . '
2167                      WHERE forum_id = ' . (int) $data_ary['forum_id'];
2168                  $result = $db->sql_query($sql);
2169                  $row = $db->sql_fetchrow($result);
2170                  $db->sql_freeresult($result);
2171  
2172                  // this post is the latest post in the forum, better update
2173                  if ($row['forum_last_post_id'] == $data_ary['post_id'] && ($row['forum_last_post_subject'] !== $subject || $data_ary['poster_id'] == ANONYMOUS))
2174                  {
2175                      // the post's subject changed
2176                      if ($row['forum_last_post_subject'] !== $subject)
2177                      {
2178                          $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_post_subject = '" . $db->sql_escape($subject) . "'";
2179                      }
2180  
2181                      // Update the user name if poster is anonymous... just in case a moderator changed it
2182                      if ($data_ary['poster_id'] == ANONYMOUS)
2183                      {
2184                          $sql_data[FORUMS_TABLE]['stat'][] = "forum_last_poster_name = '" . $db->sql_escape($username) . "'";
2185                      }
2186                  }
2187              }
2188          }
2189      }
2190  
2191      // Update forum stats
2192      $where_sql = array(
2193          POSTS_TABLE        => 'post_id = ' . $data_ary['post_id'],
2194          TOPICS_TABLE    => 'topic_id = ' . $data_ary['topic_id'],
2195          FORUMS_TABLE    => 'forum_id = ' . $data_ary['forum_id'],
2196          USERS_TABLE        => 'user_id = ' . $poster_id
2197      );
2198  
2199      foreach ($sql_data as $table => $update_ary)
2200      {
2201          if (isset($update_ary['stat']) && implode('', $update_ary['stat']))
2202          {
2203              $sql = "UPDATE $table SET " . implode(', ', $update_ary['stat']) . ' WHERE ' . $where_sql[$table];
2204              $db->sql_query($sql);
2205          }
2206      }
2207  
2208      // Delete topic shadows (if any exist). We do not need a shadow topic for an global announcement
2209      if ($topic_type == POST_GLOBAL)
2210      {
2211          $sql = 'DELETE FROM ' . TOPICS_TABLE . '
2212              WHERE topic_moved_id = ' . $data_ary['topic_id'];
2213          $db->sql_query($sql);
2214      }
2215  
2216      // Committing the transaction before updating search index
2217      $db->sql_transaction('commit');
2218  
2219      // Delete draft if post was loaded...
2220      $draft_id = $request->variable('draft_loaded', 0);
2221      if ($draft_id)
2222      {
2223          $sql = 'DELETE FROM ' . DRAFTS_TABLE . "
2224              WHERE draft_id = $draft_id
2225                  AND user_id = {$user->data['user_id']}";
2226          $db->sql_query($sql);
2227      }
2228  
2229      // Index message contents
2230      if ($update_search_index && $data_ary['enable_indexing'])
2231      {
2232          // Select the search method and do some additional checks to ensure it can actually be utilised
2233          $search_type = $config['search_type'];
2234  
2235          if (!class_exists($search_type))
2236          {
2237              trigger_error('NO_SUCH_SEARCH_MODULE');
2238          }
2239  
2240          $error = false;
2241          $search = new $search_type($error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher);
2242  
2243          if ($error)
2244          {
2245              trigger_error($error);
2246          }
2247  
2248          $search->index($mode, $data_ary['post_id'], $data_ary['message'], $subject, $poster_id, $data_ary['forum_id']);
2249      }
2250  
2251      // Topic Notification, do not change if moderator is changing other users posts...
2252      if ($user->data['user_id'] == $poster_id)
2253      {
2254          if (!$data_ary['notify_set'] && $data_ary['notify'])
2255          {
2256              $sql = 'INSERT INTO ' . TOPICS_WATCH_TABLE . ' (user_id, topic_id)
2257                  VALUES (' . $user->data['user_id'] . ', ' . $data_ary['topic_id'] . ')';
2258              $db->sql_query($sql);
2259          }
2260          else if (($config['email_enable'] || $config['jab_enable']) && $data_ary['notify_set'] && !$data_ary['notify'])
2261          {
2262              $sql = 'DELETE FROM ' . TOPICS_WATCH_TABLE . '
2263                  WHERE user_id = ' . $user->data['user_id'] . '
2264                      AND topic_id = ' . $data_ary['topic_id'];
2265              $db->sql_query($sql);
2266          }
2267      }
2268  
2269      if ($mode == 'post' || $mode == 'reply' || $mode == 'quote')
2270      {
2271          // Mark this topic as posted to
2272          markread('post', $data_ary['forum_id'], $data_ary['topic_id']);
2273      }
2274  
2275      // Mark this topic as read
2276      // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
2277      markread('topic', $data_ary['forum_id'], $data_ary['topic_id'], time());
2278  
2279      //
2280      if ($config['load_db_lastread'] && $user->data['is_registered'])
2281      {
2282          $sql = 'SELECT mark_time
2283              FROM ' . FORUMS_TRACK_TABLE . '
2284              WHERE user_id = ' . $user->data['user_id'] . '
2285                  AND forum_id = ' . $data_ary['forum_id'];
2286          $result = $db->sql_query($sql);
2287          $f_mark_time = (int) $db->sql_fetchfield('mark_time');
2288          $db->sql_freeresult($result);
2289      }
2290      else if ($config['load_anon_lastread'] || $user->data['is_registered'])
2291      {
2292          $f_mark_time = false;
2293      }
2294  
2295      if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
2296      {
2297          // Update forum info
2298          $sql = 'SELECT forum_last_post_time
2299              FROM ' . FORUMS_TABLE . '
2300              WHERE forum_id = ' . $data_ary['forum_id'];
2301          $result = $db->sql_query($sql);
2302          $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
2303          $db->sql_freeresult($result);
2304  
2305          update_forum_tracking_info($data_ary['forum_id'], $forum_last_post_time, $f_mark_time, false);
2306      }
2307  
2308      // If a username was supplied or the poster is a guest, we will use the supplied username.
2309      // Doing it this way we can use "...post by guest-username..." in notifications when
2310      // "guest-username" is supplied or ommit the username if it is not.
2311      $username = ($username !== '' || !$user->data['is_registered']) ? $username : $user->data['username'];
2312  
2313      // Send Notifications
2314      $notification_data = array_merge($data_ary, array(
2315          'topic_title'        => (isset($data_ary['topic_title'])) ? $data_ary['topic_title'] : $subject,
2316          'post_username'        => $username,
2317          'poster_id'            => $poster_id,
2318          'post_text'            => $data_ary['message'],
2319          'post_time'            => $current_time,
2320          'post_subject'        => $subject,
2321      ));
2322  
2323      /**
2324      * This event allows you to modify the notification data upon submission
2325      *
2326      * @event core.modify_submit_notification_data
2327      * @var    array    notification_data    The notification data to be inserted in to the database
2328      * @var    array    data_ary            The data array with a lot of the post submission data
2329      * @var     string    mode                The posting mode
2330      * @var    int        poster_id            The poster id
2331      * @since 3.2.4-RC1
2332      */
2333      $vars = array('notification_data', 'data_ary', 'mode', 'poster_id');
2334      extract($phpbb_dispatcher->trigger_event('core.modify_submit_notification_data', compact($vars)));
2335  
2336      /* @var $phpbb_notifications \phpbb\notification\manager */
2337      $phpbb_notifications = $phpbb_container->get('notification_manager');
2338  
2339      if ($post_visibility == ITEM_APPROVED)
2340      {
2341          switch ($mode)
2342          {
2343              case 'post':
2344                  $phpbb_notifications->add_notifications(array(
2345                      'notification.type.quote',
2346                      'notification.type.topic',
2347                  ), $notification_data);
2348              break;
2349  
2350              case 'reply':
2351              case 'quote':
2352                  $phpbb_notifications->add_notifications(array(
2353                      'notification.type.quote',
2354                      'notification.type.bookmark',
2355                      'notification.type.post',
2356                  ), $notification_data);
2357              break;
2358  
2359              case 'edit_topic':
2360              case 'edit_first_post':
2361              case 'edit':
2362              case 'edit_last_post':
2363                  if ($user->data['user_id'] == $poster_id)
2364                  {
2365                      $phpbb_notifications->update_notifications(array(
2366                          'notification.type.quote',
2367                      ), $notification_data);
2368                  }
2369  
2370                  $phpbb_notifications->update_notifications(array(
2371                      'notification.type.bookmark',
2372                      'notification.type.topic',
2373                      'notification.type.post',
2374                  ), $notification_data);
2375              break;
2376          }
2377      }
2378      else if ($post_visibility == ITEM_UNAPPROVED)
2379      {
2380          switch ($mode)
2381          {
2382              case 'post':
2383                  $phpbb_notifications->add_notifications('notification.type.topic_in_queue', $notification_data);
2384              break;
2385  
2386              case 'reply':
2387              case 'quote':
2388                  $phpbb_notifications->add_notifications('notification.type.post_in_queue', $notification_data);
2389              break;
2390  
2391              case 'edit_topic':
2392              case 'edit_first_post':
2393              case 'edit':
2394              case 'edit_last_post':
2395                  // Nothing to do here
2396              break;
2397          }
2398      }
2399      else if ($post_visibility == ITEM_REAPPROVE)
2400      {
2401          switch ($mode)
2402          {
2403              case 'edit_topic':
2404              case 'edit_first_post':
2405                  $phpbb_notifications->add_notifications('notification.type.topic_in_queue', $notification_data);
2406  
2407                  // Delete the approve_post notification so we can notify the user again,
2408                  // when his post got reapproved
2409                  $phpbb_notifications->delete_notifications('notification.type.approve_post', $notification_data['post_id']);
2410              break;
2411  
2412              case 'edit':
2413              case 'edit_last_post':
2414                  $phpbb_notifications->add_notifications('notification.type.post_in_queue', $notification_data);
2415  
2416                  // Delete the approve_post notification so we can notify the user again,
2417                  // when his post got reapproved
2418                  $phpbb_notifications->delete_notifications('notification.type.approve_post', $notification_data['post_id']);
2419              break;
2420  
2421              case 'post':
2422              case 'reply':
2423              case 'quote':
2424                  // Nothing to do here
2425              break;
2426          }
2427      }
2428      else if ($post_visibility == ITEM_DELETED)
2429      {
2430          switch ($mode)
2431          {
2432              case 'post':
2433              case 'reply':
2434              case 'quote':
2435              case 'edit_topic':
2436              case 'edit_first_post':
2437              case 'edit':
2438              case 'edit_last_post':
2439                  // Nothing to do here
2440              break;
2441          }
2442      }
2443  
2444      $params = $add_anchor = '';
2445  
2446      if ($post_visibility == ITEM_APPROVED ||
2447          ($auth->acl_get('m_softdelete', $data_ary['forum_id']) && $post_visibility == ITEM_DELETED) ||
2448          ($auth->acl_get('m_approve', $data_ary['forum_id']) && in_array($post_visibility, array(ITEM_UNAPPROVED, ITEM_REAPPROVE))))
2449      {
2450          $params .= '&amp;t=' . $data_ary['topic_id'];
2451  
2452          if ($mode != 'post')
2453          {
2454              $params .= '&amp;p=' . $data_ary['post_id'];
2455              $add_anchor = '#p' . $data_ary['post_id'];
2456          }
2457      }
2458      else if ($mode != 'post' && $post_mode != 'edit_first_post' && $post_mode != 'edit_topic')
2459      {
2460          $params .= '&amp;t=' . $data_ary['topic_id'];
2461      }
2462  
2463      $url = (!$params) ? "{$phpbb_root_path}viewforum.$phpEx" : "{$phpbb_root_path}viewtopic.$phpEx";
2464      $url = append_sid($url, 'f=' . $data_ary['forum_id'] . $params) . $add_anchor;
2465  
2466      $poll = $poll_ary;
2467      $data = $data_ary;
2468      /**
2469      * This event is used for performing actions directly after a post or topic
2470      * has been submitted. When a new topic is posted, the topic ID is
2471      * available in the $data array.
2472      *
2473      * The only action that can be done by altering data made available to this
2474      * event is to modify the return URL ($url).
2475      *
2476      * @event core.submit_post_end
2477      * @var    string    mode                Variable containing posting mode value
2478      * @var    string    subject                Variable containing post subject value
2479      * @var    string    username            Variable containing post author name
2480      * @var    int        topic_type            Variable containing topic type value
2481      * @var    array    poll                Array with the poll data for the post
2482      * @var    array    data                Array with the data for the post
2483      * @var    int        post_visibility        Variable containing up to date post visibility
2484      * @var    bool    update_message        Flag indicating if the post will be updated
2485      * @var    bool    update_search_index    Flag indicating if the search index will be updated
2486      * @var    string    url                    The "Return to topic" URL
2487      *
2488      * @since 3.1.0-a3
2489      * @changed 3.1.0-RC3 Added vars mode, subject, username, topic_type,
2490      *        poll, update_message, update_search_index
2491      */
2492      $vars = array(
2493          'mode',
2494          'subject',
2495          'username',
2496          'topic_type',
2497          'poll',
2498          'data',
2499          'post_visibility',
2500          'update_message',
2501          'update_search_index',
2502          'url',
2503      );
2504      extract($phpbb_dispatcher->trigger_event('core.submit_post_end', compact($vars)));
2505      $data_ary = $data;
2506      $poll_ary = $poll;
2507      unset($data);
2508      unset($poll);
2509  
2510      return $url;
2511  }
2512  
2513  /**
2514  * Handle topic bumping
2515  * @param int $forum_id The ID of the forum the topic is being bumped belongs to
2516  * @param int $topic_id The ID of the topic is being bumping
2517  * @param array $post_data Passes some topic parameters:
2518  *                - 'topic_title'
2519  *                - 'topic_last_post_id'
2520  *                - 'topic_last_poster_id'
2521  *                - 'topic_last_post_subject'
2522  *                - 'topic_last_poster_name'
2523  *                - 'topic_last_poster_colour'
2524  * @param int $bump_time The time at which topic was bumped, usually it is a current time as obtained via time().
2525  * @return string An URL to the bumped topic, example: ./viewtopic.php?forum_id=1&amptopic_id=2&ampp=3#p3
2526  */
2527  function phpbb_bump_topic($forum_id, $topic_id, $post_data, $bump_time = false)
2528  {
2529      global $config, $db, $user, $phpEx, $phpbb_root_path, $phpbb_log;
2530  
2531      if ($bump_time === false)
2532      {
2533          $bump_time = time();
2534      }
2535  
2536      // Begin bumping
2537      $db->sql_transaction('begin');
2538  
2539      // Update the topic's last post post_time
2540      $sql = 'UPDATE ' . POSTS_TABLE . "
2541          SET post_time = $bump_time
2542          WHERE post_id = {$post_data['topic_last_post_id']}
2543              AND topic_id = $topic_id";
2544      $db->sql_query($sql);
2545  
2546      // Sync the topic's last post time, the rest of the topic's last post data isn't changed
2547      $sql = 'UPDATE ' . TOPICS_TABLE . "
2548          SET topic_last_post_time = $bump_time,
2549              topic_bumped = 1,
2550              topic_bumper = " . $user->data['user_id'] . "
2551          WHERE topic_id = $topic_id";
2552      $db->sql_query($sql);
2553  
2554      // Update the forum's last post info
2555      $sql = 'UPDATE ' . FORUMS_TABLE . "
2556          SET forum_last_post_id = " . $post_data['topic_last_post_id'] . ",
2557              forum_last_poster_id = " . $post_data['topic_last_poster_id'] . ",
2558              forum_last_post_subject = '" . $db->sql_escape($post_data['topic_last_post_subject']) . "',
2559              forum_last_post_time = $bump_time,
2560              forum_last_poster_name = '" . $db->sql_escape($post_data['topic_last_poster_name']) . "',
2561              forum_last_poster_colour = '" . $db->sql_escape($post_data['topic_last_poster_colour']) . "'
2562          WHERE forum_id = $forum_id";
2563      $db->sql_query($sql);
2564  
2565      // Update bumper's time of the last posting to prevent flood
2566      $sql = 'UPDATE ' . USERS_TABLE . "
2567          SET user_lastpost_time = $bump_time
2568          WHERE user_id = " . $user->data['user_id'];
2569      $db->sql_query($sql);
2570  
2571      $db->sql_transaction('commit');
2572  
2573      // Mark this topic as posted to
2574      markread('post', $forum_id, $topic_id, $bump_time);
2575  
2576      // Mark this topic as read
2577      markread('topic', $forum_id, $topic_id, $bump_time);
2578  
2579      // Update forum tracking info
2580      if ($config['load_db_lastread'] && $user->data['is_registered'])
2581      {
2582          $sql = 'SELECT mark_time
2583              FROM ' . FORUMS_TRACK_TABLE . '
2584              WHERE user_id = ' . $user->data['user_id'] . '
2585                  AND forum_id = ' . $forum_id;
2586          $result = $db->sql_query($sql);
2587          $f_mark_time = (int) $db->sql_fetchfield('mark_time');
2588          $db->sql_freeresult($result);
2589      }
2590      else if ($config['load_anon_lastread'] || $user->data['is_registered'])
2591      {
2592          $f_mark_time = false;
2593      }
2594  
2595      if (($config['load_db_lastread'] && $user->data['is_registered']) || $config['load_anon_lastread'] || $user->data['is_registered'])
2596      {
2597          // Update forum info
2598          $sql = 'SELECT forum_last_post_time
2599              FROM ' . FORUMS_TABLE . '
2600              WHERE forum_id = ' . $forum_id;
2601          $result = $db->sql_query($sql);
2602          $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
2603          $db->sql_freeresult($result);
2604  
2605          update_forum_tracking_info($forum_id, $forum_last_post_time, $f_mark_time, false);
2606      }
2607  
2608      $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_BUMP_TOPIC', false, array(
2609          'forum_id' => $forum_id,
2610          'topic_id' => $topic_id,
2611          $post_data['topic_title']
2612      ));
2613  
2614      $url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;p={$post_data['topic_last_post_id']}") . "#p{$post_data['topic_last_post_id']}";
2615  
2616      return $url;
2617  }
2618  
2619  /**
2620  * Show upload popup (progress bar)
2621  */
2622  function phpbb_upload_popup($forum_style = 0)
2623  {
2624      global $template, $user;
2625  
2626      ($forum_style) ? $user->setup('posting', $forum_style) : $user->setup('posting');
2627  
2628      page_header($user->lang['PROGRESS_BAR']);
2629  
2630      $template->set_filenames(array(
2631              'popup'    => 'posting_progress_bar.html')
2632      );
2633  
2634      $template->assign_vars(array(
2635              'PROGRESS_BAR'    => $user->img('upload_bar', $user->lang['UPLOAD_IN_PROGRESS']))
2636      );
2637  
2638      $template->display('popup');
2639  
2640      garbage_collection();
2641      exit_handler();
2642  }
2643  
2644  /**
2645  * Do the various checks required for removing posts as well as removing it
2646  *
2647  * @param int        $forum_id        The id of the forum
2648  * @param int        $topic_id        The id of the topic
2649  * @param int        $post_id        The id of the post
2650  * @param array        $post_data        Array with the post data
2651  * @param bool        $is_soft        The flag indicating whether it is the soft delete mode
2652  * @param string        $delete_reason    Description for the post deletion reason
2653  *
2654  * @return null
2655  */
2656  function phpbb_handle_post_delete($forum_id, $topic_id, $post_id, &$post_data, $is_soft = false, $delete_reason = '')
2657  {
2658      global $user, $auth, $config, $request;
2659      global $phpbb_root_path, $phpEx, $phpbb_log, $phpbb_dispatcher;
2660  
2661      $force_delete_allowed = $force_softdelete_allowed = false;
2662      $perm_check = ($is_soft) ? 'softdelete' : 'delete';
2663  
2664      /**
2665      * This event allows to modify the conditions for the post deletion
2666      *
2667      * @event core.handle_post_delete_conditions
2668      * @var    int        forum_id        The id of the forum
2669      * @var    int        topic_id        The id of the topic
2670      * @var    int        post_id            The id of the post
2671      * @var    array    post_data        Array with the post data
2672      * @var    bool    is_soft            The flag indicating whether it is the soft delete mode
2673      * @var    string    delete_reason    Description for the post deletion reason
2674      * @var    bool    force_delete_allowed        Allow the user to delete the post (all permissions and conditions are ignored)
2675      * @var    bool    force_softdelete_allowed    Allow the user to softdelete the post (all permissions and conditions are ignored)
2676      * @var    string    perm_check        The deletion mode softdelete|delete
2677      * @since 3.1.11-RC1
2678      */
2679      $vars = array(
2680          'forum_id',
2681          'topic_id',
2682          'post_id',
2683          'post_data',
2684          'is_soft',
2685          'delete_reason',
2686          'force_delete_allowed',
2687          'force_softdelete_allowed',
2688          'perm_check',
2689      );
2690      extract($phpbb_dispatcher->trigger_event('core.handle_post_delete_conditions', compact($vars)));
2691  
2692      // If moderator removing post or user itself removing post, present a confirmation screen
2693      if ($force_delete_allowed || ($is_soft && $force_softdelete_allowed) || $auth->acl_get("m_$perm_check", $forum_id) || ($post_data['poster_id'] == $user->data['user_id'] && $user->data['is_registered'] && $auth->acl_get("f_$perm_check", $forum_id) && $post_id == $post_data['topic_last_post_id'] && !$post_data['post_edit_locked'] && ($post_data['post_time'] > time() - ($config['delete_time'] * 60) || !$config['delete_time'])))
2694      {
2695          $s_hidden_fields = array(
2696              'p'        => $post_id,
2697              'f'        => $forum_id,
2698              'mode'    => ($is_soft) ? 'soft_delete' : 'delete',
2699          );
2700  
2701          if (confirm_box(true))
2702          {
2703              $data = array(
2704                  'topic_first_post_id'    => $post_data['topic_first_post_id'],
2705                  'topic_last_post_id'    => $post_data['topic_last_post_id'],
2706                  'topic_posts_approved'        => $post_data['topic_posts_approved'],
2707                  'topic_posts_unapproved'    => $post_data['topic_posts_unapproved'],
2708                  'topic_posts_softdeleted'    => $post_data['topic_posts_softdeleted'],
2709                  'topic_visibility'        => $post_data['topic_visibility'],
2710                  'topic_type'            => $post_data['topic_type'],
2711                  'post_visibility'        => $post_data['post_visibility'],
2712                  'post_reported'            => $post_data['post_reported'],
2713                  'post_time'                => $post_data['post_time'],
2714                  'poster_id'                => $post_data['poster_id'],
2715                  'post_postcount'        => $post_data['post_postcount'],
2716              );
2717  
2718              $next_post_id = delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $delete_reason);
2719              $post_username = ($post_data['poster_id'] == ANONYMOUS && !empty($post_data['post_username'])) ? $post_data['post_username'] : $post_data['username'];
2720  
2721              if ($next_post_id === false)
2722              {
2723                  $phpbb_log->add('mod', $user->data['user_id'], $user->ip, (($is_soft) ? 'LOG_SOFTDELETE_TOPIC' : 'LOG_DELETE_TOPIC'), false, array(
2724                      'forum_id' => $forum_id,
2725                      'topic_id' => $topic_id,
2726                      $post_data['topic_title'],
2727                      $post_username,
2728                      $delete_reason
2729                  ));
2730  
2731                  $meta_info = append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id");
2732                  $message = $user->lang['POST_DELETED'];
2733              }
2734              else
2735              {
2736                  $phpbb_log->add('mod', $user->data['user_id'], $user->ip, (($is_soft) ? 'LOG_SOFTDELETE_POST' : 'LOG_DELETE_POST'), false, array(
2737                      'forum_id' => $forum_id,
2738                      'topic_id' => $topic_id,
2739                      'post_id'  => $post_id,
2740                      $post_data['post_subject'],
2741                      $post_username,
2742                      $delete_reason
2743                  ));
2744  
2745                  $meta_info = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id&amp;p=$next_post_id") . "#p$next_post_id";
2746                  $message = $user->lang['POST_DELETED'];
2747  
2748                  if (!$request->is_ajax())
2749                  {
2750                      $message .= '<br /><br />' . $user->lang('RETURN_TOPIC', '<a href="' . $meta_info . '">', '</a>');
2751                  }
2752              }
2753  
2754              meta_refresh(3, $meta_info);
2755              if (!$request->is_ajax())
2756              {
2757                  $message .= '<br /><br />' . $user->lang('RETURN_FORUM', '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
2758              }
2759              trigger_error($message);
2760          }
2761          else
2762          {
2763              global $template;
2764  
2765              $can_delete = $force_delete_allowed || ($auth->acl_get('m_delete', $forum_id) || ($post_data['poster_id'] == $user->data['user_id'] && $user->data['is_registered'] && $auth->acl_get('f_delete', $forum_id)));
2766              $can_softdelete = $force_softdelete_allowed || ($auth->acl_get('m_softdelete', $forum_id) || ($post_data['poster_id'] == $user->data['user_id'] && $user->data['is_registered'] && $auth->acl_get('f_softdelete', $forum_id)));
2767  
2768              $template->assign_vars(array(
2769                  'S_SOFTDELETED'            => $post_data['post_visibility'] == ITEM_DELETED,
2770                  'S_CHECKED_PERMANENT'    => $request->is_set_post('delete_permanent') ? ' checked="checked"' : '',
2771                  'S_ALLOWED_DELETE'        => $can_delete,
2772                  'S_ALLOWED_SOFTDELETE'    => $can_softdelete,
2773              ));
2774  
2775              $l_confirm = 'DELETE_POST';
2776              if ($post_data['post_visibility'] == ITEM_DELETED)
2777              {
2778                  $l_confirm .= '_PERMANENTLY';
2779                  $s_hidden_fields['delete_permanent'] = '1';
2780              }
2781              else if (!$can_softdelete)
2782              {
2783                  $s_hidden_fields['delete_permanent'] = '1';
2784              }
2785  
2786              confirm_box(false, $l_confirm, build_hidden_fields($s_hidden_fields), 'confirm_delete_body.html');
2787          }
2788      }
2789  
2790      // If we are here the user is not able to delete - present the correct error message
2791      if ($post_data['poster_id'] != $user->data['user_id'] && $auth->acl_get('f_delete', $forum_id))
2792      {
2793          trigger_error('DELETE_OWN_POSTS');
2794      }
2795  
2796      if ($post_data['poster_id'] == $user->data['user_id'] && $auth->acl_get('f_delete', $forum_id) && $post_id != $post_data['topic_last_post_id'])
2797      {
2798          trigger_error('CANNOT_DELETE_REPLIED');
2799      }
2800  
2801      trigger_error('USER_CANNOT_DELETE');
2802  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1