[ Index ]

PHP Cross Reference of phpBB-3.3.0-deutsch

title

Body

[close]

/includes/mcp/ -> mcp_main.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  * mcp_main
  24  * Handling mcp actions
  25  */
  26  class mcp_main
  27  {
  28      var $p_master;
  29      var $u_action;
  30  
  31  	function __construct($p_master)
  32      {
  33          $this->p_master = $p_master;
  34      }
  35  
  36  	function main($id, $mode)
  37      {
  38          global $auth, $user, $action;
  39          global $phpbb_root_path, $phpEx, $request;
  40          global $phpbb_dispatcher;
  41  
  42          $quickmod = ($mode == 'quickmod') ? true : false;
  43  
  44          /**
  45          * Event to perform additional actions before an MCP action is executed.
  46          *
  47          * @event core.mcp_main_before
  48          * @var    string    action                The action that is about to be performed
  49          * @var    string    mode                The mode in which the MCP is accessed, e.g. front, forum_view, topic_view, post_details, quickmod
  50          * @var    boolean    quickmod            Whether or not the action is performed via QuickMod
  51          * @since 3.2.8-RC1
  52          */
  53          $vars = [
  54              'action',
  55              'mode',
  56              'quickmod',
  57          ];
  58          extract($phpbb_dispatcher->trigger_event('core.mcp_main_before', compact($vars)));
  59  
  60          switch ($action)
  61          {
  62              case 'lock':
  63              case 'unlock':
  64                  $topic_ids = (!$quickmod) ? $request->variable('topic_id_list', array(0)) : array($request->variable('t', 0));
  65  
  66                  if (!count($topic_ids))
  67                  {
  68                      trigger_error('NO_TOPIC_SELECTED');
  69                  }
  70  
  71                  lock_unlock($action, $topic_ids);
  72              break;
  73  
  74              case 'lock_post':
  75              case 'unlock_post':
  76  
  77                  $post_ids = (!$quickmod) ? $request->variable('post_id_list', array(0)) : array($request->variable('p', 0));
  78  
  79                  if (!count($post_ids))
  80                  {
  81                      trigger_error('NO_POST_SELECTED');
  82                  }
  83  
  84                  lock_unlock($action, $post_ids);
  85              break;
  86  
  87              case 'make_announce':
  88              case 'make_sticky':
  89              case 'make_global':
  90              case 'make_normal':
  91  
  92                  $topic_ids = (!$quickmod) ? $request->variable('topic_id_list', array(0)) : array($request->variable('t', 0));
  93  
  94                  if (!count($topic_ids))
  95                  {
  96                      trigger_error('NO_TOPIC_SELECTED');
  97                  }
  98  
  99                  change_topic_type($action, $topic_ids);
 100              break;
 101  
 102              case 'move':
 103                  $user->add_lang('viewtopic');
 104  
 105                  $topic_ids = (!$quickmod) ? $request->variable('topic_id_list', array(0)) : array($request->variable('t', 0));
 106  
 107                  if (!count($topic_ids))
 108                  {
 109                      trigger_error('NO_TOPIC_SELECTED');
 110                  }
 111  
 112                  mcp_move_topic($topic_ids);
 113              break;
 114  
 115              case 'fork':
 116                  $user->add_lang('viewtopic');
 117  
 118                  $topic_ids = (!$quickmod) ? $request->variable('topic_id_list', array(0)) : array($request->variable('t', 0));
 119  
 120                  if (!count($topic_ids))
 121                  {
 122                      trigger_error('NO_TOPIC_SELECTED');
 123                  }
 124  
 125                  mcp_fork_topic($topic_ids);
 126              break;
 127  
 128              case 'delete_topic':
 129                  $user->add_lang('viewtopic');
 130  
 131                  // f parameter is not reliable for permission usage, however we just use it to decide
 132                  // which permission we will check later on. So if it is manipulated, we will still catch it later on.
 133                  $forum_id = $request->variable('f', 0);
 134                  $topic_ids = (!$quickmod) ? $request->variable('topic_id_list', array(0)) : array($request->variable('t', 0));
 135                  $soft_delete = (($request->is_set_post('confirm') && !$request->is_set_post('delete_permanent')) || !$auth->acl_get('m_delete', $forum_id)) ? true : false;
 136  
 137                  if (!count($topic_ids))
 138                  {
 139                      trigger_error('NO_TOPIC_SELECTED');
 140                  }
 141  
 142                  mcp_delete_topic($topic_ids, $soft_delete, $request->variable('delete_reason', '', true));
 143              break;
 144  
 145              case 'delete_post':
 146                  $user->add_lang('posting');
 147  
 148                  // f parameter is not reliable for permission usage, however we just use it to decide
 149                  // which permission we will check later on. So if it is manipulated, we will still catch it later on.
 150                  $forum_id = $request->variable('f', 0);
 151                  $post_ids = (!$quickmod) ? $request->variable('post_id_list', array(0)) : array($request->variable('p', 0));
 152                  $soft_delete = (($request->is_set_post('confirm') && !$request->is_set_post('delete_permanent')) || !$auth->acl_get('m_delete', $forum_id)) ? true : false;
 153  
 154                  if (!count($post_ids))
 155                  {
 156                      trigger_error('NO_POST_SELECTED');
 157                  }
 158  
 159                  mcp_delete_post($post_ids, $soft_delete, $request->variable('delete_reason', '', true));
 160              break;
 161  
 162              case 'restore_topic':
 163                  $user->add_lang('posting');
 164  
 165                  $topic_ids = (!$quickmod) ? $request->variable('topic_id_list', array(0)) : array($request->variable('t', 0));
 166  
 167                  if (!count($topic_ids))
 168                  {
 169                      trigger_error('NO_TOPIC_SELECTED');
 170                  }
 171  
 172                  mcp_restore_topic($topic_ids);
 173              break;
 174  
 175              default:
 176                  /**
 177                  * This event allows you to handle custom quickmod options
 178                  *
 179                  * @event core.modify_quickmod_actions
 180                  * @var    string    action        Topic quick moderation action name
 181                  * @var    bool    quickmod    Flag indicating whether MCP is in quick moderation mode
 182                  * @since 3.1.0-a4
 183                  * @changed 3.1.0-RC4 Added variables: action, quickmod
 184                  */
 185                  $vars = array('action', 'quickmod');
 186                  extract($phpbb_dispatcher->trigger_event('core.modify_quickmod_actions', compact($vars)));
 187              break;
 188          }
 189  
 190          switch ($mode)
 191          {
 192              case 'front':
 193                  if (!function_exists('mcp_front_view'))
 194                  {
 195                      include($phpbb_root_path . 'includes/mcp/mcp_front.' . $phpEx);
 196                  }
 197  
 198                  $user->add_lang('acp/common');
 199  
 200                  mcp_front_view($id, $mode, $action);
 201  
 202                  $this->tpl_name = 'mcp_front';
 203                  $this->page_title = 'MCP_MAIN';
 204              break;
 205  
 206              case 'forum_view':
 207                  if (!function_exists('mcp_forum_view'))
 208                  {
 209                      include($phpbb_root_path . 'includes/mcp/mcp_forum.' . $phpEx);
 210                  }
 211  
 212                  $user->add_lang('viewforum');
 213  
 214                  $forum_id = $request->variable('f', 0);
 215  
 216                  $forum_info = phpbb_get_forum_data($forum_id, 'm_', true);
 217  
 218                  if (!count($forum_info))
 219                  {
 220                      $this->main('main', 'front');
 221                      return;
 222                  }
 223  
 224                  $forum_info = $forum_info[$forum_id];
 225  
 226                  mcp_forum_view($id, $mode, $action, $forum_info);
 227  
 228                  $this->tpl_name = 'mcp_forum';
 229                  $this->page_title = 'MCP_MAIN_FORUM_VIEW';
 230              break;
 231  
 232              case 'topic_view':
 233                  if (!function_exists('mcp_topic_view'))
 234                  {
 235                      include($phpbb_root_path . 'includes/mcp/mcp_topic.' . $phpEx);
 236                  }
 237  
 238                  mcp_topic_view($id, $mode, $action);
 239  
 240                  $this->tpl_name = 'mcp_topic';
 241                  $this->page_title = 'MCP_MAIN_TOPIC_VIEW';
 242              break;
 243  
 244              case 'post_details':
 245                  if (!function_exists('mcp_post_details'))
 246                  {
 247                      include($phpbb_root_path . 'includes/mcp/mcp_post.' . $phpEx);
 248                  }
 249  
 250                  mcp_post_details($id, $mode, $action);
 251  
 252                  $this->tpl_name = ($action == 'whois') ? 'mcp_whois' : 'mcp_post';
 253                  $this->page_title = 'MCP_MAIN_POST_DETAILS';
 254              break;
 255  
 256              default:
 257                  if ($quickmod)
 258                  {
 259                      switch ($action)
 260                      {
 261                          case 'lock':
 262                          case 'unlock':
 263                          case 'make_announce':
 264                          case 'make_sticky':
 265                          case 'make_global':
 266                          case 'make_normal':
 267                          case 'make_onindex':
 268                          case 'move':
 269                          case 'fork':
 270                          case 'delete_topic':
 271                              trigger_error('TOPIC_NOT_EXIST');
 272                          break;
 273  
 274                          case 'lock_post':
 275                          case 'unlock_post':
 276                          case 'delete_post':
 277                              trigger_error('POST_NOT_EXIST');
 278                          break;
 279                      }
 280                  }
 281  
 282                  trigger_error('NO_MODE', E_USER_ERROR);
 283              break;
 284          }
 285      }
 286  }
 287  
 288  /**
 289  * Lock/Unlock Topic/Post
 290  */
 291  function lock_unlock($action, $ids)
 292  {
 293      global $user, $db, $request, $phpbb_log, $phpbb_dispatcher;
 294  
 295      if ($action == 'lock' || $action == 'unlock')
 296      {
 297          $table = TOPICS_TABLE;
 298          $sql_id = 'topic_id';
 299          $set_id = 'topic_status';
 300          $l_prefix = 'TOPIC';
 301      }
 302      else
 303      {
 304          $table = POSTS_TABLE;
 305          $sql_id = 'post_id';
 306          $set_id = 'post_edit_locked';
 307          $l_prefix = 'POST';
 308      }
 309  
 310      $orig_ids = $ids;
 311  
 312      if (!phpbb_check_ids($ids, $table, $sql_id, array('m_lock')))
 313      {
 314          // Make sure that for f_user_lock only the lock action is triggered.
 315          if ($action != 'lock')
 316          {
 317              return;
 318          }
 319  
 320          $ids = $orig_ids;
 321  
 322          if (!phpbb_check_ids($ids, $table, $sql_id, array('f_user_lock')))
 323          {
 324              return;
 325          }
 326      }
 327      unset($orig_ids);
 328  
 329      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
 330      $redirect = reapply_sid($redirect);
 331  
 332      $s_hidden_fields = build_hidden_fields(array(
 333          $sql_id . '_list'    => $ids,
 334          'action'            => $action,
 335          'redirect'            => $redirect)
 336      );
 337  
 338      if (confirm_box(true))
 339      {
 340          $sql = "UPDATE $table
 341              SET $set_id = " . (($action == 'lock' || $action == 'lock_post') ? ITEM_LOCKED : ITEM_UNLOCKED) . '
 342              WHERE ' . $db->sql_in_set($sql_id, $ids);
 343          $db->sql_query($sql);
 344  
 345          $data = ($action == 'lock' || $action == 'unlock') ? phpbb_get_topic_data($ids) : phpbb_get_post_data($ids);
 346  
 347          foreach ($data as $id => $row)
 348          {
 349              $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_' . strtoupper($action), false, array(
 350                  'forum_id' => $row['forum_id'],
 351                  'topic_id' => $row['topic_id'],
 352                  'post_id'  => isset($row['post_id']) ? $row['post_id'] : 0,
 353                  $row['topic_title']
 354              ));
 355          }
 356  
 357          /**
 358           * Perform additional actions after locking/unlocking posts/topics
 359           *
 360           * @event core.mcp_lock_unlock_after
 361           * @var    string    action                Variable containing the action we perform on the posts/topics ('lock', 'unlock', 'lock_post' or 'unlock_post')
 362           * @var    array    ids                    Array containing the post/topic IDs that have been locked/unlocked
 363           * @var    array    data                Array containing posts/topics data
 364           * @since 3.1.7-RC1
 365           */
 366          $vars = array(
 367              'action',
 368              'ids',
 369              'data',
 370          );
 371          extract($phpbb_dispatcher->trigger_event('core.mcp_lock_unlock_after', compact($vars)));
 372  
 373          $success_msg = $l_prefix . ((count($ids) == 1) ? '' : 'S') . '_' . (($action == 'lock' || $action == 'lock_post') ? 'LOCKED' : 'UNLOCKED') . '_SUCCESS';
 374  
 375          meta_refresh(2, $redirect);
 376          $message = $user->lang[$success_msg];
 377  
 378          if (!$request->is_ajax())
 379          {
 380              $message .= '<br /><br />' . $user->lang('RETURN_PAGE', '<a href="' . $redirect . '">', '</a>');
 381          }
 382          trigger_error($message);
 383      }
 384      else
 385      {
 386          confirm_box(false, strtoupper($action) . '_' . $l_prefix . ((count($ids) == 1) ? '' : 'S'), $s_hidden_fields);
 387      }
 388  
 389      redirect($redirect);
 390  }
 391  
 392  /**
 393  * Change Topic Type
 394  */
 395  function change_topic_type($action, $topic_ids)
 396  {
 397      global $user, $db, $request, $phpbb_log, $phpbb_dispatcher;
 398  
 399      switch ($action)
 400      {
 401          case 'make_announce':
 402              $new_topic_type = POST_ANNOUNCE;
 403              $check_acl = 'f_announce';
 404              $l_new_type = (count($topic_ids) == 1) ? 'MCP_MAKE_ANNOUNCEMENT' : 'MCP_MAKE_ANNOUNCEMENTS';
 405          break;
 406  
 407          case 'make_global':
 408              $new_topic_type = POST_GLOBAL;
 409              $check_acl = 'f_announce_global';
 410              $l_new_type = (count($topic_ids) == 1) ? 'MCP_MAKE_GLOBAL' : 'MCP_MAKE_GLOBALS';
 411          break;
 412  
 413          case 'make_sticky':
 414              $new_topic_type = POST_STICKY;
 415              $check_acl = 'f_sticky';
 416              $l_new_type = (count($topic_ids) == 1) ? 'MCP_MAKE_STICKY' : 'MCP_MAKE_STICKIES';
 417          break;
 418  
 419          default:
 420              $new_topic_type = POST_NORMAL;
 421              $check_acl = false;
 422              $l_new_type = (count($topic_ids) == 1) ? 'MCP_MAKE_NORMAL' : 'MCP_MAKE_NORMALS';
 423          break;
 424      }
 425  
 426      $forum_id = phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id', $check_acl, true);
 427  
 428      if ($forum_id === false)
 429      {
 430          return;
 431      }
 432  
 433      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
 434      $redirect = reapply_sid($redirect);
 435  
 436      $s_hidden_fields = array(
 437          'topic_id_list'    => $topic_ids,
 438          'f'                => $forum_id,
 439          'action'        => $action,
 440          'redirect'        => $redirect,
 441      );
 442  
 443      if (confirm_box(true))
 444      {
 445  
 446          /**
 447           * Perform additional actions before changing topic(s) type
 448           *
 449           * @event core.mcp_change_topic_type_before
 450           * @var    int        new_topic_type        The candidated topic type.
 451           * @var    int        forum_id            The forum ID for the topic ID(s).
 452           * @var    array    topic_ids            Array containing the topic ID(s) that will be changed
 453           * @since 3.2.6-RC1
 454           */
 455          $vars = array(
 456              'new_topic_type',
 457              'forum_id',
 458              'topic_ids',
 459          );
 460          extract($phpbb_dispatcher->trigger_event('core.mcp_change_topic_type_before', compact($vars)));
 461  
 462          $db->sql_transaction('begin');
 463  
 464          $sql = 'UPDATE ' . TOPICS_TABLE . "
 465              SET topic_type = $new_topic_type
 466              WHERE " . $db->sql_in_set('topic_id', $topic_ids);
 467          $db->sql_query($sql);
 468  
 469          if (($new_topic_type == POST_GLOBAL) && count($topic_ids))
 470          {
 471              // Delete topic shadows for global announcements
 472              $sql = 'DELETE FROM ' . TOPICS_TABLE . '
 473                  WHERE ' . $db->sql_in_set('topic_moved_id', $topic_ids);
 474              $db->sql_query($sql);
 475          }
 476  
 477          $db->sql_transaction('commit');
 478  
 479          $success_msg = (count($topic_ids) == 1) ? 'TOPIC_TYPE_CHANGED' : 'TOPICS_TYPE_CHANGED';
 480  
 481          if (count($topic_ids))
 482          {
 483              $data = phpbb_get_topic_data($topic_ids);
 484  
 485              foreach ($data as $topic_id => $row)
 486              {
 487                  $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_TOPIC_TYPE_CHANGED', false, array(
 488                      'forum_id' => $forum_id,
 489                      'topic_id' => $topic_id,
 490                      $row['topic_title']
 491                  ));
 492              }
 493          }
 494  
 495          /**
 496           * Perform additional actions after changing topic types
 497           *
 498           * @event core.mcp_change_topic_type_after
 499           * @var    int        new_topic_type        The newly changed topic type.
 500           * @var    int        forum_id            The forum ID where the newly changed topic type belongs to.
 501           * @var    array    topic_ids            Array containing the topic IDs that have been changed
 502           * @since 3.2.6-RC1
 503           */
 504          $vars = array(
 505              'new_topic_type',
 506              'forum_id',
 507              'topic_ids',
 508          );
 509          extract($phpbb_dispatcher->trigger_event('core.mcp_change_topic_type_after', compact($vars)));
 510  
 511          meta_refresh(2, $redirect);
 512          $message = $user->lang[$success_msg];
 513  
 514          if (!$request->is_ajax())
 515          {
 516              $message .= '<br /><br />' . $user->lang('RETURN_PAGE', '<a href="' . $redirect . '">', '</a>');
 517          }
 518          trigger_error($message);
 519      }
 520      else
 521      {
 522          confirm_box(false, $l_new_type, build_hidden_fields($s_hidden_fields));
 523      }
 524  
 525      redirect($redirect);
 526  }
 527  
 528  /**
 529  * Move Topic
 530  */
 531  function mcp_move_topic($topic_ids)
 532  {
 533      global $auth, $user, $db, $template, $phpbb_log, $request, $phpbb_dispatcher;
 534      global $phpEx, $phpbb_root_path;
 535  
 536      // Here we limit the operation to one forum only
 537      $forum_id = phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_move'), true);
 538  
 539      if ($forum_id === false)
 540      {
 541          return;
 542      }
 543  
 544      $to_forum_id = $request->variable('to_forum_id', 0);
 545      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
 546      $additional_msg = $success_msg = '';
 547  
 548      $s_hidden_fields = build_hidden_fields(array(
 549          'topic_id_list'    => $topic_ids,
 550          'f'                => $forum_id,
 551          'action'        => 'move',
 552          'redirect'        => $redirect)
 553      );
 554  
 555      if ($to_forum_id)
 556      {
 557          $forum_data = phpbb_get_forum_data($to_forum_id, 'f_post');
 558  
 559          if (!count($forum_data))
 560          {
 561              $additional_msg = $user->lang['FORUM_NOT_EXIST'];
 562          }
 563          else
 564          {
 565              $forum_data = $forum_data[$to_forum_id];
 566  
 567              if ($forum_data['forum_type'] != FORUM_POST)
 568              {
 569                  $additional_msg = $user->lang['FORUM_NOT_POSTABLE'];
 570              }
 571              else if (!$auth->acl_get('f_post', $to_forum_id) || (!$auth->acl_get('m_approve', $to_forum_id) && !$auth->acl_get('f_noapprove', $to_forum_id)))
 572              {
 573                  $additional_msg = $user->lang['USER_CANNOT_POST'];
 574              }
 575              else if ($forum_id == $to_forum_id)
 576              {
 577                  $additional_msg = $user->lang['CANNOT_MOVE_SAME_FORUM'];
 578              }
 579          }
 580      }
 581      else if (isset($_POST['confirm']))
 582      {
 583          $additional_msg = $user->lang['FORUM_NOT_EXIST'];
 584      }
 585  
 586      if (!$to_forum_id || $additional_msg)
 587      {
 588          $request->overwrite('confirm', null, \phpbb\request\request_interface::POST);
 589          $request->overwrite('confirm_key', null);
 590      }
 591  
 592      if (confirm_box(true))
 593      {
 594          $topic_data = phpbb_get_topic_data($topic_ids);
 595          $leave_shadow = (isset($_POST['move_leave_shadow'])) ? true : false;
 596  
 597          $forum_sync_data = array();
 598  
 599          $forum_sync_data[$forum_id] = current($topic_data);
 600          $forum_sync_data[$to_forum_id] = $forum_data;
 601  
 602          $topics_moved = $topics_moved_unapproved = $topics_moved_softdeleted = 0;
 603          $posts_moved = $posts_moved_unapproved = $posts_moved_softdeleted = 0;
 604  
 605          foreach ($topic_data as $topic_id => $topic_info)
 606          {
 607              if ($topic_info['topic_visibility'] == ITEM_APPROVED)
 608              {
 609                  $topics_moved++;
 610              }
 611              else if ($topic_info['topic_visibility'] == ITEM_UNAPPROVED || $topic_info['topic_visibility'] == ITEM_REAPPROVE)
 612              {
 613                  $topics_moved_unapproved++;
 614              }
 615              else if ($topic_info['topic_visibility'] == ITEM_DELETED)
 616              {
 617                  $topics_moved_softdeleted++;
 618              }
 619  
 620              $posts_moved += $topic_info['topic_posts_approved'];
 621              $posts_moved_unapproved += $topic_info['topic_posts_unapproved'];
 622              $posts_moved_softdeleted += $topic_info['topic_posts_softdeleted'];
 623          }
 624  
 625          $db->sql_transaction('begin');
 626  
 627          // Move topics, but do not resync yet
 628          move_topics($topic_ids, $to_forum_id, false);
 629  
 630          if ($request->is_set_post('move_lock_topics') && $auth->acl_get('m_lock', $to_forum_id))
 631          {
 632              $sql = 'UPDATE ' . TOPICS_TABLE . '
 633                  SET topic_status = ' . ITEM_LOCKED . '
 634                  WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
 635              $db->sql_query($sql);
 636          }
 637  
 638          $shadow_topics = 0;
 639          $forum_ids = array($to_forum_id);
 640          foreach ($topic_data as $topic_id => $row)
 641          {
 642              // Get the list of forums to resync
 643              $forum_ids[] = $row['forum_id'];
 644  
 645              // We add the $to_forum_id twice, because 'forum_id' is updated
 646              // when the topic is moved again later.
 647              $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_MOVE', false, array(
 648                  'forum_id'        => (int) $to_forum_id,
 649                  'topic_id'        => (int) $topic_id,
 650                  $row['forum_name'],
 651                  $forum_data['forum_name'],
 652                  (int) $row['forum_id'],
 653                  (int) $forum_data['forum_id'],
 654              ));
 655  
 656              // Leave a redirection if required and only if the topic is visible to users
 657              if ($leave_shadow && $row['topic_visibility'] == ITEM_APPROVED && $row['topic_type'] != POST_GLOBAL)
 658              {
 659                  $shadow = array(
 660                      'forum_id'                =>    (int) $row['forum_id'],
 661                      'icon_id'                =>    (int) $row['icon_id'],
 662                      'topic_attachment'        =>    (int) $row['topic_attachment'],
 663                      'topic_visibility'        =>    ITEM_APPROVED, // a shadow topic is always approved
 664                      'topic_reported'        =>    0, // a shadow topic is never reported
 665                      'topic_title'            =>    (string) $row['topic_title'],
 666                      'topic_poster'            =>    (int) $row['topic_poster'],
 667                      'topic_time'            =>    (int) $row['topic_time'],
 668                      'topic_time_limit'        =>    (int) $row['topic_time_limit'],
 669                      'topic_views'            =>    (int) $row['topic_views'],
 670                      'topic_posts_approved'    =>    (int) $row['topic_posts_approved'],
 671                      'topic_posts_unapproved'=>    (int) $row['topic_posts_unapproved'],
 672                      'topic_posts_softdeleted'=>    (int) $row['topic_posts_softdeleted'],
 673                      'topic_status'            =>    ITEM_MOVED,
 674                      'topic_type'            =>    POST_NORMAL,
 675                      'topic_first_post_id'    =>    (int) $row['topic_first_post_id'],
 676                      'topic_first_poster_colour'=>(string) $row['topic_first_poster_colour'],
 677                      'topic_first_poster_name'=>    (string) $row['topic_first_poster_name'],
 678                      'topic_last_post_id'    =>    (int) $row['topic_last_post_id'],
 679                      'topic_last_poster_id'    =>    (int) $row['topic_last_poster_id'],
 680                      'topic_last_poster_colour'=>(string) $row['topic_last_poster_colour'],
 681                      'topic_last_poster_name'=>    (string) $row['topic_last_poster_name'],
 682                      'topic_last_post_subject'=>    (string) $row['topic_last_post_subject'],
 683                      'topic_last_post_time'    =>    (int) $row['topic_last_post_time'],
 684                      'topic_last_view_time'    =>    (int) $row['topic_last_view_time'],
 685                      'topic_moved_id'        =>    (int) $row['topic_id'],
 686                      'topic_bumped'            =>    (int) $row['topic_bumped'],
 687                      'topic_bumper'            =>    (int) $row['topic_bumper'],
 688                      'poll_title'            =>    (string) $row['poll_title'],
 689                      'poll_start'            =>    (int) $row['poll_start'],
 690                      'poll_length'            =>    (int) $row['poll_length'],
 691                      'poll_max_options'        =>    (int) $row['poll_max_options'],
 692                      'poll_last_vote'        =>    (int) $row['poll_last_vote']
 693                  );
 694  
 695                  /**
 696                  * Perform actions before shadow topic is created.
 697                  *
 698                  * @event core.mcp_main_modify_shadow_sql
 699                  * @var    array    shadow    SQL array to be used by $db->sql_build_array
 700                  * @var    array    row        Topic data
 701                  * @since 3.1.11-RC1
 702                  * @changed 3.1.11-RC1 Added variable: row
 703                  */
 704                  $vars = array(
 705                      'shadow',
 706                      'row',
 707                  );
 708                  extract($phpbb_dispatcher->trigger_event('core.mcp_main_modify_shadow_sql', compact($vars)));
 709  
 710                  $db->sql_query('INSERT INTO ' . TOPICS_TABLE . $db->sql_build_array('INSERT', $shadow));
 711  
 712                  // Shadow topics only count on new "topics" and not posts... a shadow topic alone has 0 posts
 713                  $shadow_topics++;
 714              }
 715          }
 716          unset($topic_data);
 717  
 718          $sync_sql = array();
 719          if ($posts_moved)
 720          {
 721              $sync_sql[$to_forum_id][] = 'forum_posts_approved = forum_posts_approved + ' . (int) $posts_moved;
 722              $sync_sql[$forum_id][] = 'forum_posts_approved = forum_posts_approved - ' . (int) $posts_moved;
 723          }
 724          if ($posts_moved_unapproved)
 725          {
 726              $sync_sql[$to_forum_id][] = 'forum_posts_unapproved = forum_posts_unapproved + ' . (int) $posts_moved_unapproved;
 727              $sync_sql[$forum_id][] = 'forum_posts_unapproved = forum_posts_unapproved - ' . (int) $posts_moved_unapproved;
 728          }
 729          if ($posts_moved_softdeleted)
 730          {
 731              $sync_sql[$to_forum_id][] = 'forum_posts_softdeleted = forum_posts_softdeleted + ' . (int) $posts_moved_softdeleted;
 732              $sync_sql[$forum_id][] = 'forum_posts_softdeleted = forum_posts_softdeleted - ' . (int) $posts_moved_softdeleted;
 733          }
 734  
 735          if ($topics_moved)
 736          {
 737              $sync_sql[$to_forum_id][] = 'forum_topics_approved = forum_topics_approved + ' . (int) $topics_moved;
 738              if ($topics_moved - $shadow_topics > 0)
 739              {
 740                  $sync_sql[$forum_id][] = 'forum_topics_approved = forum_topics_approved - ' . (int) ($topics_moved - $shadow_topics);
 741              }
 742          }
 743          if ($topics_moved_unapproved)
 744          {
 745              $sync_sql[$to_forum_id][] = 'forum_topics_unapproved = forum_topics_unapproved + ' . (int) $topics_moved_unapproved;
 746              $sync_sql[$forum_id][] = 'forum_topics_unapproved = forum_topics_unapproved - ' . (int) $topics_moved_unapproved;
 747          }
 748          if ($topics_moved_softdeleted)
 749          {
 750              $sync_sql[$to_forum_id][] = 'forum_topics_softdeleted = forum_topics_softdeleted + ' . (int) $topics_moved_softdeleted;
 751              $sync_sql[$forum_id][] = 'forum_topics_softdeleted = forum_topics_softdeleted - ' . (int) $topics_moved_softdeleted;
 752          }
 753  
 754          $success_msg = (count($topic_ids) == 1) ? 'TOPIC_MOVED_SUCCESS' : 'TOPICS_MOVED_SUCCESS';
 755  
 756          foreach ($sync_sql as $forum_id_key => $array)
 757          {
 758              $sql = 'UPDATE ' . FORUMS_TABLE . '
 759                  SET ' . implode(', ', $array) . '
 760                  WHERE forum_id = ' . $forum_id_key;
 761              $db->sql_query($sql);
 762          }
 763  
 764          $db->sql_transaction('commit');
 765  
 766          sync('forum', 'forum_id', array($forum_id, $to_forum_id));
 767      }
 768      else
 769      {
 770          $template->assign_vars(array(
 771              'S_FORUM_SELECT'        => make_forum_select($to_forum_id, $forum_id, false, true, true, true),
 772              'S_CAN_LEAVE_SHADOW'    => true,
 773              'S_CAN_LOCK_TOPIC'        => ($auth->acl_get('m_lock', $to_forum_id)) ? true : false,
 774              'ADDITIONAL_MSG'        => $additional_msg)
 775          );
 776  
 777          confirm_box(false, 'MOVE_TOPIC' . ((count($topic_ids) == 1) ? '' : 'S'), $s_hidden_fields, 'mcp_move.html');
 778      }
 779  
 780      $redirect = $request->variable('redirect', "index.$phpEx");
 781      $redirect = reapply_sid($redirect);
 782  
 783      if (!$success_msg)
 784      {
 785          redirect($redirect);
 786      }
 787      else
 788      {
 789          meta_refresh(3, $redirect);
 790  
 791          $message = $user->lang[$success_msg];
 792          $message .= '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>');
 793          $message .= '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$forum_id") . '">', '</a>');
 794          $message .= '<br /><br />' . sprintf($user->lang['RETURN_NEW_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", "f=$to_forum_id") . '">', '</a>');
 795  
 796          trigger_error($message);
 797      }
 798  }
 799  
 800  /**
 801  * Restore Topics
 802  */
 803  function mcp_restore_topic($topic_ids)
 804  {
 805      global $user, $phpEx, $phpbb_root_path, $request, $phpbb_container, $phpbb_log;
 806  
 807      if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_approve')))
 808      {
 809          return;
 810      }
 811  
 812      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
 813      $forum_id = $request->variable('f', 0);
 814  
 815      $s_hidden_fields = build_hidden_fields(array(
 816          'topic_id_list'    => $topic_ids,
 817          'f'                => $forum_id,
 818          'action'        => 'restore_topic',
 819          'redirect'        => $redirect,
 820      ));
 821      $success_msg = '';
 822  
 823      if (confirm_box(true))
 824      {
 825          $success_msg = (count($topic_ids) == 1) ? 'TOPIC_RESTORED_SUCCESS' : 'TOPICS_RESTORED_SUCCESS';
 826  
 827          $data = phpbb_get_topic_data($topic_ids);
 828  
 829          /* @var $phpbb_content_visibility \phpbb\content_visibility */
 830          $phpbb_content_visibility = $phpbb_container->get('content.visibility');
 831          foreach ($data as $topic_id => $row)
 832          {
 833              $return = $phpbb_content_visibility->set_topic_visibility(ITEM_APPROVED, $topic_id, $row['forum_id'], $user->data['user_id'], time(), '');
 834              if (!empty($return))
 835              {
 836                  $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_RESTORE_TOPIC', false, array(
 837                      'forum_id' => $row['forum_id'],
 838                      'topic_id' => $topic_id,
 839                      $row['topic_title'],
 840                      $row['topic_first_poster_name']
 841                  ));
 842              }
 843          }
 844      }
 845      else
 846      {
 847          confirm_box(false, (count($topic_ids) == 1) ? 'RESTORE_TOPIC' : 'RESTORE_TOPICS', $s_hidden_fields);
 848      }
 849  
 850      $topic_id = $request->variable('t', 0);
 851      if (!$request->is_set('quickmod', \phpbb\request\request_interface::REQUEST))
 852      {
 853          $redirect = $request->variable('redirect', "index.$phpEx");
 854          $redirect = reapply_sid($redirect);
 855          $redirect_message = 'PAGE';
 856      }
 857      else if ($topic_id)
 858      {
 859          $redirect = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $topic_id);
 860          $redirect_message = 'TOPIC';
 861      }
 862      else
 863      {
 864          $redirect = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
 865          $redirect_message = 'FORUM';
 866      }
 867  
 868      if (!$success_msg)
 869      {
 870          redirect($redirect);
 871      }
 872      else
 873      {
 874          meta_refresh(3, $redirect);
 875          trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_' . $redirect_message], '<a href="' . $redirect . '">', '</a>'));
 876      }
 877  }
 878  
 879  /**
 880  * Delete Topics
 881  */
 882  function mcp_delete_topic($topic_ids, $is_soft = false, $soft_delete_reason = '', $action = 'delete_topic')
 883  {
 884      global $auth, $user, $db, $phpEx, $phpbb_root_path, $request, $phpbb_container, $phpbb_log;
 885  
 886      $check_permission = ($is_soft) ? 'm_softdelete' : 'm_delete';
 887      if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array($check_permission)))
 888      {
 889          return;
 890      }
 891  
 892      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
 893      $forum_id = $request->variable('f', 0);
 894  
 895      $s_hidden_fields = array(
 896          'topic_id_list'    => $topic_ids,
 897          'f'                => $forum_id,
 898          'action'        => $action,
 899          'redirect'        => $redirect,
 900      );
 901      $success_msg = '';
 902  
 903      if (confirm_box(true))
 904      {
 905          $success_msg = (count($topic_ids) == 1) ? 'TOPIC_DELETED_SUCCESS' : 'TOPICS_DELETED_SUCCESS';
 906  
 907          $data = phpbb_get_topic_data($topic_ids);
 908  
 909          foreach ($data as $topic_id => $row)
 910          {
 911              if ($row['topic_moved_id'])
 912              {
 913                  $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_DELETE_SHADOW_TOPIC', false, array(
 914                      'forum_id' => $row['forum_id'],
 915                      'topic_id' => $topic_id,
 916                      $row['topic_title']
 917                  ));
 918              }
 919              else
 920              {
 921                  // Only soft delete non-shadow topics
 922                  if ($is_soft)
 923                  {
 924                      /* @var $phpbb_content_visibility \phpbb\content_visibility */
 925                      $phpbb_content_visibility = $phpbb_container->get('content.visibility');
 926                      $return = $phpbb_content_visibility->set_topic_visibility(ITEM_DELETED, $topic_id, $row['forum_id'], $user->data['user_id'], time(), $soft_delete_reason);
 927                      if (!empty($return))
 928                      {
 929                          $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_SOFTDELETE_TOPIC', false, array(
 930                              'forum_id' => $row['forum_id'],
 931                              'topic_id' => $topic_id,
 932                              $row['topic_title'],
 933                              $row['topic_first_poster_name'],
 934                              $soft_delete_reason
 935                          ));
 936                      }
 937                  }
 938                  else
 939                  {
 940                      $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_DELETE_TOPIC', false, array(
 941                          'forum_id' => $row['forum_id'],
 942                          'topic_id' => $topic_id,
 943                          $row['topic_title'],
 944                          $row['topic_first_poster_name'],
 945                          $soft_delete_reason
 946                      ));
 947                  }
 948              }
 949          }
 950  
 951          if (!$is_soft)
 952          {
 953              delete_topics('topic_id', $topic_ids);
 954          }
 955      }
 956      else
 957      {
 958          global $template;
 959  
 960          $user->add_lang('posting');
 961  
 962          // If there are only shadow topics, we neither need a reason nor softdelete
 963          $sql = 'SELECT topic_id
 964              FROM ' . TOPICS_TABLE . '
 965              WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
 966                  AND topic_moved_id = 0';
 967          $result = $db->sql_query_limit($sql, 1);
 968          $only_shadow = !$db->sql_fetchfield('topic_id');
 969          $db->sql_freeresult($result);
 970  
 971          $only_softdeleted = false;
 972          if (!$only_shadow && $auth->acl_get('m_delete', $forum_id) && $auth->acl_get('m_softdelete', $forum_id))
 973          {
 974              // If there are only soft deleted topics, we display a message why the option is not available
 975              $sql = 'SELECT topic_id
 976                  FROM ' . TOPICS_TABLE . '
 977                  WHERE ' . $db->sql_in_set('topic_id', $topic_ids) . '
 978                      AND topic_visibility <> ' . ITEM_DELETED;
 979              $result = $db->sql_query_limit($sql, 1);
 980              $only_softdeleted = !$db->sql_fetchfield('topic_id');
 981              $db->sql_freeresult($result);
 982          }
 983  
 984          $template->assign_vars(array(
 985              'S_SHADOW_TOPICS'                    => $only_shadow,
 986              'S_SOFTDELETED'                        => $only_softdeleted,
 987              'S_TOPIC_MODE'                        => true,
 988              'S_ALLOWED_DELETE'                    => $auth->acl_get('m_delete', $forum_id),
 989              'S_ALLOWED_SOFTDELETE'                => $auth->acl_get('m_softdelete', $forum_id),
 990              'DELETE_TOPIC_PERMANENTLY_EXPLAIN'    => $user->lang('DELETE_TOPIC_PERMANENTLY', count($topic_ids)),
 991          ));
 992  
 993          $count = count($topic_ids);
 994          $l_confirm = $count === 1 ? 'DELETE_TOPIC' : 'DELETE_TOPICS';
 995          if ($only_softdeleted)
 996          {
 997              $l_confirm = array($l_confirm . '_PERMANENTLY', $count);
 998              $s_hidden_fields['delete_permanent'] = '1';
 999          }
1000          else if ($only_shadow || !$auth->acl_get('m_softdelete', $forum_id))
1001          {
1002              $s_hidden_fields['delete_permanent'] = '1';
1003          }
1004  
1005          confirm_box(false, $l_confirm, build_hidden_fields($s_hidden_fields), 'confirm_delete_body.html');
1006      }
1007  
1008      $topic_id = $request->variable('t', 0);
1009      if (!$request->is_set('quickmod', \phpbb\request\request_interface::REQUEST))
1010      {
1011          $redirect = $request->variable('redirect', "index.$phpEx");
1012          $redirect = reapply_sid($redirect);
1013          $redirect_message = 'PAGE';
1014      }
1015      else if ($is_soft && $topic_id)
1016      {
1017          $redirect = append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $topic_id);
1018          $redirect_message = 'TOPIC';
1019      }
1020      else
1021      {
1022          $redirect = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
1023          $redirect_message = 'FORUM';
1024      }
1025  
1026      if (!$success_msg)
1027      {
1028          redirect($redirect);
1029      }
1030      else
1031      {
1032          meta_refresh(3, $redirect);
1033          trigger_error($user->lang[$success_msg] . '<br /><br />' . sprintf($user->lang['RETURN_' . $redirect_message], '<a href="' . $redirect . '">', '</a>'));
1034      }
1035  }
1036  
1037  /**
1038  * Delete Posts
1039  */
1040  function mcp_delete_post($post_ids, $is_soft = false, $soft_delete_reason = '', $action = 'delete_post')
1041  {
1042      global $auth, $user, $db, $phpEx, $phpbb_root_path, $request, $phpbb_container, $phpbb_log;
1043  
1044      $check_permission = ($is_soft) ? 'm_softdelete' : 'm_delete';
1045      if (!phpbb_check_ids($post_ids, POSTS_TABLE, 'post_id', array($check_permission)))
1046      {
1047          return;
1048      }
1049  
1050      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
1051      $forum_id = $request->variable('f', 0);
1052  
1053      $s_hidden_fields = array(
1054          'post_id_list'    => $post_ids,
1055          'f'                => $forum_id,
1056          'action'        => $action,
1057          'redirect'        => $redirect,
1058      );
1059      $success_msg = '';
1060  
1061      if (confirm_box(true) && $is_soft)
1062      {
1063          $post_info = phpbb_get_post_data($post_ids);
1064  
1065          $topic_info = $approve_log = array();
1066  
1067          // Group the posts by topic_id
1068          foreach ($post_info as $post_id => $post_data)
1069          {
1070              if ($post_data['post_visibility'] != ITEM_APPROVED)
1071              {
1072                  continue;
1073              }
1074              $topic_id = (int) $post_data['topic_id'];
1075  
1076              $topic_info[$topic_id]['posts'][] = (int) $post_id;
1077              $topic_info[$topic_id]['forum_id'] = (int) $post_data['forum_id'];
1078  
1079              if ($post_id == $post_data['topic_first_post_id'])
1080              {
1081                  $topic_info[$topic_id]['first_post'] = true;
1082              }
1083  
1084              if ($post_id == $post_data['topic_last_post_id'])
1085              {
1086                  $topic_info[$topic_id]['last_post'] = true;
1087              }
1088  
1089              $approve_log[] = array(
1090                  'forum_id'        => $post_data['forum_id'],
1091                  'topic_id'        => $post_data['topic_id'],
1092                  'post_id'        => $post_id,
1093                  'post_subject'    => $post_data['post_subject'],
1094                  'poster_id'        => $post_data['poster_id'],
1095                  'post_username'    => $post_data['post_username'],
1096                  'username'        => $post_data['username'],
1097              );
1098          }
1099  
1100          /* @var $phpbb_content_visibility \phpbb\content_visibility */
1101          $phpbb_content_visibility = $phpbb_container->get('content.visibility');
1102          foreach ($topic_info as $topic_id => $topic_data)
1103          {
1104              $phpbb_content_visibility->set_post_visibility(ITEM_DELETED, $topic_data['posts'], $topic_id, $topic_data['forum_id'], $user->data['user_id'], time(), $soft_delete_reason, isset($topic_data['first_post']), isset($topic_data['last_post']));
1105          }
1106          $affected_topics = count($topic_info);
1107          // None of the topics is really deleted, so a redirect won't hurt much.
1108          $deleted_topics = 0;
1109  
1110          $success_msg = (count($post_info) == 1) ? $user->lang['POST_DELETED_SUCCESS'] : $user->lang['POSTS_DELETED_SUCCESS'];
1111  
1112          foreach ($approve_log as $row)
1113          {
1114              $post_username = ($row['poster_id'] == ANONYMOUS && !empty($row['post_username'])) ? $row['post_username'] : $row['username'];
1115              $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_SOFTDELETE_POST', false, array(
1116                  'forum_id' => $row['forum_id'],
1117                  'topic_id' => $row['topic_id'],
1118                  'post_id'  => $row['post_id'],
1119                  $row['post_subject'],
1120                  $post_username,
1121                  $soft_delete_reason
1122              ));
1123          }
1124  
1125          $topic_id = $request->variable('t', 0);
1126  
1127          // Return links
1128          $return_link = array();
1129          if ($affected_topics == 1 && $topic_id)
1130          {
1131              $return_link[] = sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id") . '">', '</a>');
1132          }
1133          $return_link[] = sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
1134  
1135      }
1136      else if (confirm_box(true))
1137      {
1138          if (!function_exists('delete_posts'))
1139          {
1140              include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
1141          }
1142  
1143          // Count the number of topics that are affected
1144          // I did not use COUNT(DISTINCT ...) because I remember having problems
1145          // with it on older versions of MySQL -- Ashe
1146  
1147          $sql = 'SELECT DISTINCT topic_id
1148              FROM ' . POSTS_TABLE . '
1149              WHERE ' . $db->sql_in_set('post_id', $post_ids);
1150          $result = $db->sql_query($sql);
1151  
1152          $topic_id_list = array();
1153          while ($row = $db->sql_fetchrow($result))
1154          {
1155              $topic_id_list[] = $row['topic_id'];
1156          }
1157          $affected_topics = count($topic_id_list);
1158          $db->sql_freeresult($result);
1159  
1160          $post_data = phpbb_get_post_data($post_ids);
1161  
1162          foreach ($post_data as $id => $row)
1163          {
1164              $post_username = ($row['poster_id'] == ANONYMOUS && !empty($row['post_username'])) ? $row['post_username'] : $row['username'];
1165              $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_DELETE_POST', false, array(
1166                  'forum_id' => $row['forum_id'],
1167                  'topic_id' => $row['topic_id'],
1168                  'post_id'  => $row['post_id'],
1169                  $row['post_subject'],
1170                  $post_username,
1171                  $soft_delete_reason
1172              ));
1173          }
1174  
1175          // Now delete the posts, topics and forums are automatically resync'ed
1176          delete_posts('post_id', $post_ids);
1177  
1178          $sql = 'SELECT COUNT(topic_id) AS topics_left
1179              FROM ' . TOPICS_TABLE . '
1180              WHERE ' . $db->sql_in_set('topic_id', $topic_id_list);
1181          $result = $db->sql_query_limit($sql, 1);
1182  
1183          $deleted_topics = ($row = $db->sql_fetchrow($result)) ? ($affected_topics - $row['topics_left']) : $affected_topics;
1184          $db->sql_freeresult($result);
1185  
1186          $topic_id = $request->variable('t', 0);
1187  
1188          // Return links
1189          $return_link = array();
1190          if ($affected_topics == 1 && !$deleted_topics && $topic_id)
1191          {
1192              $return_link[] = sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$forum_id&amp;t=$topic_id") . '">', '</a>');
1193          }
1194          $return_link[] = sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id) . '">', '</a>');
1195  
1196          if (count($post_ids) == 1)
1197          {
1198              if ($deleted_topics)
1199              {
1200                  // We deleted the only post of a topic, which in turn has
1201                  // been removed from the database
1202                  $success_msg = $user->lang['TOPIC_DELETED_SUCCESS'];
1203              }
1204              else
1205              {
1206                  $success_msg = $user->lang['POST_DELETED_SUCCESS'];
1207              }
1208          }
1209          else
1210          {
1211              if ($deleted_topics)
1212              {
1213                  // Some of topics disappeared
1214                  $success_msg = $user->lang['POSTS_DELETED_SUCCESS'] . '<br /><br />' . $user->lang['EMPTY_TOPICS_REMOVED_WARNING'];
1215              }
1216              else
1217              {
1218                  $success_msg = $user->lang['POSTS_DELETED_SUCCESS'];
1219              }
1220          }
1221      }
1222      else
1223      {
1224          global $template;
1225  
1226          $user->add_lang('posting');
1227  
1228          $only_softdeleted = false;
1229          if ($auth->acl_get('m_delete', $forum_id) && $auth->acl_get('m_softdelete', $forum_id))
1230          {
1231              // If there are only soft deleted posts, we display a message why the option is not available
1232              $sql = 'SELECT post_id
1233                  FROM ' . POSTS_TABLE . '
1234                  WHERE ' . $db->sql_in_set('post_id', $post_ids) . '
1235                      AND post_visibility <> ' . ITEM_DELETED;
1236              $result = $db->sql_query_limit($sql, 1);
1237              $only_softdeleted = !$db->sql_fetchfield('post_id');
1238              $db->sql_freeresult($result);
1239          }
1240  
1241          $template->assign_vars(array(
1242              'S_SOFTDELETED'                        => $only_softdeleted,
1243              'S_ALLOWED_DELETE'                    => $auth->acl_get('m_delete', $forum_id),
1244              'S_ALLOWED_SOFTDELETE'                => $auth->acl_get('m_softdelete', $forum_id),
1245              'DELETE_POST_PERMANENTLY_EXPLAIN'    => $user->lang('DELETE_POST_PERMANENTLY', count($post_ids)),
1246          ));
1247  
1248          $count = count($post_ids);
1249          $l_confirm = $count === 1 ? 'DELETE_POST' : 'DELETE_POSTS';
1250          if ($only_softdeleted)
1251          {
1252              $l_confirm = array($l_confirm . '_PERMANENTLY', $count);
1253              $s_hidden_fields['delete_permanent'] = '1';
1254          }
1255          else if (!$auth->acl_get('m_softdelete', $forum_id))
1256          {
1257              $s_hidden_fields['delete_permanent'] = '1';
1258          }
1259  
1260          confirm_box(false, $l_confirm, build_hidden_fields($s_hidden_fields), 'confirm_delete_body.html');
1261      }
1262  
1263      $redirect = $request->variable('redirect', "index.$phpEx");
1264      $redirect = reapply_sid($redirect);
1265  
1266      if (!$success_msg)
1267      {
1268          redirect($redirect);
1269      }
1270      else
1271      {
1272          if ($affected_topics != 1 || $deleted_topics || !$topic_id)
1273          {
1274              $redirect = append_sid("{$phpbb_root_path}mcp.$phpEx", "f=$forum_id&i=main&mode=forum_view", false);
1275          }
1276  
1277          meta_refresh(3, $redirect);
1278          trigger_error($success_msg . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>') . '<br /><br />' . implode('<br /><br />', $return_link));
1279      }
1280  }
1281  
1282  /**
1283  * Fork Topic
1284  */
1285  function mcp_fork_topic($topic_ids)
1286  {
1287      global $auth, $user, $db, $template, $config;
1288      global $phpEx, $phpbb_root_path, $phpbb_log, $request, $phpbb_dispatcher;
1289  
1290      if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_')))
1291      {
1292          return;
1293      }
1294  
1295      $to_forum_id = $request->variable('to_forum_id', 0);
1296      $forum_id = $request->variable('f', 0);
1297      $redirect = $request->variable('redirect', build_url(array('action', 'quickmod')));
1298      $additional_msg = $success_msg = '';
1299      $counter = array();
1300  
1301      $s_hidden_fields = build_hidden_fields(array(
1302          'topic_id_list'    => $topic_ids,
1303          'f'                => $forum_id,
1304          'action'        => 'fork',
1305          'redirect'        => $redirect)
1306      );
1307  
1308      if ($to_forum_id)
1309      {
1310          $forum_data = phpbb_get_forum_data($to_forum_id, 'f_post');
1311  
1312          if (!count($topic_ids))
1313          {
1314              $additional_msg = $user->lang['NO_TOPIC_SELECTED'];
1315          }
1316          else if (!count($forum_data))
1317          {
1318              $additional_msg = $user->lang['FORUM_NOT_EXIST'];
1319          }
1320          else
1321          {
1322              $forum_data = $forum_data[$to_forum_id];
1323  
1324              if ($forum_data['forum_type'] != FORUM_POST)
1325              {
1326                  $additional_msg = $user->lang['FORUM_NOT_POSTABLE'];
1327              }
1328              else if (!$auth->acl_get('f_post', $to_forum_id))
1329              {
1330                  $additional_msg = $user->lang['USER_CANNOT_POST'];
1331              }
1332          }
1333      }
1334      else if (isset($_POST['confirm']))
1335      {
1336          $additional_msg = $user->lang['FORUM_NOT_EXIST'];
1337      }
1338  
1339      if ($additional_msg)
1340      {
1341          $request->overwrite('confirm', null, \phpbb\request\request_interface::POST);
1342          $request->overwrite('confirm_key', null);
1343      }
1344  
1345      if (confirm_box(true))
1346      {
1347          $topic_data = phpbb_get_topic_data($topic_ids, 'f_post');
1348  
1349          $total_topics = $total_topics_unapproved = $total_topics_softdeleted = 0;
1350          $total_posts = $total_posts_unapproved = $total_posts_softdeleted = 0;
1351          $new_topic_id_list = array();
1352  
1353          foreach ($topic_data as $topic_id => $topic_row)
1354          {
1355              if (!isset($search_type) && $topic_row['enable_indexing'])
1356              {
1357                  // Select the search method and do some additional checks to ensure it can actually be utilised
1358                  $search_type = $config['search_type'];
1359  
1360                  if (!class_exists($search_type))
1361                  {
1362                      trigger_error('NO_SUCH_SEARCH_MODULE');
1363                  }
1364  
1365                  $error = false;
1366                  $search = new $search_type($error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher);
1367                  $search_mode = 'post';
1368  
1369                  if ($error)
1370                  {
1371                      trigger_error($error);
1372                  }
1373              }
1374              else if (!isset($search_type) && !$topic_row['enable_indexing'])
1375              {
1376                  $search_type = false;
1377              }
1378  
1379              $sql_ary = array(
1380                  'forum_id'                    => (int) $to_forum_id,
1381                  'icon_id'                    => (int) $topic_row['icon_id'],
1382                  'topic_attachment'            => (int) $topic_row['topic_attachment'],
1383                  'topic_visibility'            => (int) $topic_row['topic_visibility'],
1384                  'topic_reported'            => 0,
1385                  'topic_title'                => (string) $topic_row['topic_title'],
1386                  'topic_poster'                => (int) $topic_row['topic_poster'],
1387                  'topic_time'                => (int) $topic_row['topic_time'],
1388                  'topic_posts_approved'        => (int) $topic_row['topic_posts_approved'],
1389                  'topic_posts_unapproved'    => (int) $topic_row['topic_posts_unapproved'],
1390                  'topic_posts_softdeleted'    => (int) $topic_row['topic_posts_softdeleted'],
1391                  'topic_status'                => (int) $topic_row['topic_status'],
1392                  'topic_type'                => (int) $topic_row['topic_type'],
1393                  'topic_first_poster_name'    => (string) $topic_row['topic_first_poster_name'],
1394                  'topic_last_poster_id'        => (int) $topic_row['topic_last_poster_id'],
1395                  'topic_last_poster_name'    => (string) $topic_row['topic_last_poster_name'],
1396                  'topic_last_post_time'        => (int) $topic_row['topic_last_post_time'],
1397                  'topic_last_view_time'        => (int) $topic_row['topic_last_view_time'],
1398                  'topic_bumped'                => (int) $topic_row['topic_bumped'],
1399                  'topic_bumper'                => (int) $topic_row['topic_bumper'],
1400                  'poll_title'                => (string) $topic_row['poll_title'],
1401                  'poll_start'                => (int) $topic_row['poll_start'],
1402                  'poll_length'                => (int) $topic_row['poll_length'],
1403                  'poll_max_options'            => (int) $topic_row['poll_max_options'],
1404                  'poll_vote_change'            => (int) $topic_row['poll_vote_change'],
1405              );
1406  
1407              /**
1408              * Perform actions before forked topic is created.
1409              *
1410              * @event core.mcp_main_modify_fork_sql
1411              * @var    array    sql_ary        SQL array to be used by $db->sql_build_array
1412              * @var    array    topic_row    Topic data
1413              * @since 3.1.11-RC1
1414              * @changed 3.1.11-RC1 Added variable: topic_row
1415              */
1416              $vars = array(
1417                  'sql_ary',
1418                  'topic_row',
1419              );
1420              extract($phpbb_dispatcher->trigger_event('core.mcp_main_modify_fork_sql', compact($vars)));
1421  
1422              $db->sql_query('INSERT INTO ' . TOPICS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
1423              $new_topic_id = $db->sql_nextid();
1424              $new_topic_id_list[$topic_id] = $new_topic_id;
1425  
1426              switch ($topic_row['topic_visibility'])
1427              {
1428                  case ITEM_APPROVED:
1429                      $total_topics++;
1430                  break;
1431                  case ITEM_UNAPPROVED:
1432                  case ITEM_REAPPROVE:
1433                      $total_topics_unapproved++;
1434                  break;
1435                  case ITEM_DELETED:
1436                      $total_topics_softdeleted++;
1437                  break;
1438              }
1439  
1440              if ($topic_row['poll_start'])
1441              {
1442                  $sql = 'SELECT *
1443                      FROM ' . POLL_OPTIONS_TABLE . "
1444                      WHERE topic_id = $topic_id";
1445                  $result = $db->sql_query($sql);
1446  
1447                  while ($row = $db->sql_fetchrow($result))
1448                  {
1449                      $sql_ary = array(
1450                          'poll_option_id'    => (int) $row['poll_option_id'],
1451                          'topic_id'            => (int) $new_topic_id,
1452                          'poll_option_text'    => (string) $row['poll_option_text'],
1453                          'poll_option_total'    => 0
1454                      );
1455  
1456                      $db->sql_query('INSERT INTO ' . POLL_OPTIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
1457                  }
1458                  $db->sql_freeresult($result);
1459              }
1460  
1461              $sql = 'SELECT *
1462                  FROM ' . POSTS_TABLE . "
1463                  WHERE topic_id = $topic_id
1464                  ORDER BY post_time ASC, post_id ASC";
1465              $result = $db->sql_query($sql);
1466  
1467              $post_rows = array();
1468              while ($row = $db->sql_fetchrow($result))
1469              {
1470                  $post_rows[] = $row;
1471              }
1472              $db->sql_freeresult($result);
1473  
1474              if (!count($post_rows))
1475              {
1476                  continue;
1477              }
1478  
1479              foreach ($post_rows as $row)
1480              {
1481                  $sql_ary = array(
1482                      'topic_id'            => (int) $new_topic_id,
1483                      'forum_id'            => (int) $to_forum_id,
1484                      'poster_id'            => (int) $row['poster_id'],
1485                      'icon_id'            => (int) $row['icon_id'],
1486                      'poster_ip'            => (string) $row['poster_ip'],
1487                      'post_time'            => (int) $row['post_time'],
1488                      'post_visibility'    => (int) $row['post_visibility'],
1489                      'post_reported'        => 0,
1490                      'enable_bbcode'        => (int) $row['enable_bbcode'],
1491                      'enable_smilies'    => (int) $row['enable_smilies'],
1492                      'enable_magic_url'    => (int) $row['enable_magic_url'],
1493                      'enable_sig'        => (int) $row['enable_sig'],
1494                      'post_username'        => (string) $row['post_username'],
1495                      'post_subject'        => (string) $row['post_subject'],
1496                      'post_text'            => (string) $row['post_text'],
1497                      'post_edit_reason'    => (string) $row['post_edit_reason'],
1498                      'post_edit_user'    => (int) $row['post_edit_user'],
1499                      'post_checksum'        => (string) $row['post_checksum'],
1500                      'post_attachment'    => (int) $row['post_attachment'],
1501                      'bbcode_bitfield'    => $row['bbcode_bitfield'],
1502                      'bbcode_uid'        => (string) $row['bbcode_uid'],
1503                      'post_edit_time'    => (int) $row['post_edit_time'],
1504                      'post_edit_count'    => (int) $row['post_edit_count'],
1505                      'post_edit_locked'    => (int) $row['post_edit_locked'],
1506                      'post_postcount'    => $row['post_postcount'],
1507                  );
1508                  // Adjust post count only if the post can be incremented to the user counter
1509                  if ($row['post_postcount'])
1510                  {
1511                      if (isset($counter[$row['poster_id']]))
1512                      {
1513                          ++$counter[$row['poster_id']];
1514                      }
1515                      else
1516                      {
1517                          $counter[$row['poster_id']] = 1;
1518                      }
1519                  }
1520                  $db->sql_query('INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
1521                  $new_post_id = $db->sql_nextid();
1522  
1523                  /**
1524                  * Perform actions after forked topic is created.
1525                  *
1526                  * @event core.mcp_main_fork_sql_after
1527                  * @var    int        new_topic_id    The newly created topic ID
1528                  * @var    int        to_forum_id        The forum ID where the forked topic has been moved to
1529                  * @var    int        new_post_id        The newly created post ID
1530                  * @var    array    row                Post data
1531                  * @since 3.2.4-RC1
1532                  */
1533                  $vars = array(
1534                      'new_topic_id',
1535                      'to_forum_id',
1536                      'new_post_id',
1537                      'row',
1538                  );
1539                  extract($phpbb_dispatcher->trigger_event('core.mcp_main_fork_sql_after', compact($vars)));
1540  
1541                  switch ($row['post_visibility'])
1542                  {
1543                      case ITEM_APPROVED:
1544                          $total_posts++;
1545                      break;
1546                      case ITEM_UNAPPROVED:
1547                      case ITEM_REAPPROVE:
1548                          $total_posts_unapproved++;
1549                      break;
1550                      case ITEM_DELETED:
1551                          $total_posts_softdeleted++;
1552                      break;
1553                  }
1554  
1555                  // Copy whether the topic is dotted
1556                  markread('post', $to_forum_id, $new_topic_id, 0, $row['poster_id']);
1557  
1558                  if (!empty($search_type))
1559                  {
1560                      $search->index($search_mode, $new_post_id, $sql_ary['post_text'], $sql_ary['post_subject'], $sql_ary['poster_id'], ($topic_row['topic_type'] == POST_GLOBAL) ? 0 : $to_forum_id);
1561                      $search_mode = 'reply'; // After one we index replies
1562                  }
1563  
1564                  // Copy Attachments
1565                  if ($row['post_attachment'])
1566                  {
1567                      $sql = 'SELECT * FROM ' . ATTACHMENTS_TABLE . "
1568                          WHERE post_msg_id = {$row['post_id']}
1569                              AND topic_id = $topic_id
1570                              AND in_message = 0";
1571                      $result = $db->sql_query($sql);
1572  
1573                      $sql_ary = array();
1574                      while ($attach_row = $db->sql_fetchrow($result))
1575                      {
1576                          $sql_ary[] = array(
1577                              'post_msg_id'        => (int) $new_post_id,
1578                              'topic_id'            => (int) $new_topic_id,
1579                              'in_message'        => 0,
1580                              'is_orphan'            => (int) $attach_row['is_orphan'],
1581                              'poster_id'            => (int) $attach_row['poster_id'],
1582                              'physical_filename'    => (string) utf8_basename($attach_row['physical_filename']),
1583                              'real_filename'        => (string) utf8_basename($attach_row['real_filename']),
1584                              'download_count'    => (int) $attach_row['download_count'],
1585                              'attach_comment'    => (string) $attach_row['attach_comment'],
1586                              'extension'            => (string) $attach_row['extension'],
1587                              'mimetype'            => (string) $attach_row['mimetype'],
1588                              'filesize'            => (int) $attach_row['filesize'],
1589                              'filetime'            => (int) $attach_row['filetime'],
1590                              'thumbnail'            => (int) $attach_row['thumbnail']
1591                          );
1592                      }
1593                      $db->sql_freeresult($result);
1594  
1595                      if (count($sql_ary))
1596                      {
1597                          $db->sql_multi_insert(ATTACHMENTS_TABLE, $sql_ary);
1598                      }
1599                  }
1600              }
1601  
1602              // Copy topic subscriptions to new topic
1603              $sql = 'SELECT user_id, notify_status
1604                  FROM ' . TOPICS_WATCH_TABLE . '
1605                  WHERE topic_id = ' . $topic_id;
1606              $result = $db->sql_query($sql);
1607  
1608              $sql_ary = array();
1609              while ($row = $db->sql_fetchrow($result))
1610              {
1611                  $sql_ary[] = array(
1612                      'topic_id'        => (int) $new_topic_id,
1613                      'user_id'        => (int) $row['user_id'],
1614                      'notify_status'    => (int) $row['notify_status'],
1615                  );
1616              }
1617              $db->sql_freeresult($result);
1618  
1619              if (count($sql_ary))
1620              {
1621                  $db->sql_multi_insert(TOPICS_WATCH_TABLE, $sql_ary);
1622              }
1623  
1624              // Copy bookmarks to new topic
1625              $sql = 'SELECT user_id
1626                  FROM ' . BOOKMARKS_TABLE . '
1627                  WHERE topic_id = ' . $topic_id;
1628              $result = $db->sql_query($sql);
1629  
1630              $sql_ary = array();
1631              while ($row = $db->sql_fetchrow($result))
1632              {
1633                  $sql_ary[] = array(
1634                      'topic_id'        => (int) $new_topic_id,
1635                      'user_id'        => (int) $row['user_id'],
1636                  );
1637              }
1638              $db->sql_freeresult($result);
1639  
1640              if (count($sql_ary))
1641              {
1642                  $db->sql_multi_insert(BOOKMARKS_TABLE, $sql_ary);
1643              }
1644          }
1645  
1646          // Sync new topics, parent forums and board stats
1647          $sql = 'UPDATE ' . FORUMS_TABLE . '
1648              SET forum_posts_approved = forum_posts_approved + ' . $total_posts . ',
1649                  forum_posts_unapproved = forum_posts_unapproved + ' . $total_posts_unapproved . ',
1650                  forum_posts_softdeleted = forum_posts_softdeleted + ' . $total_posts_softdeleted . ',
1651                  forum_topics_approved = forum_topics_approved + ' . $total_topics . ',
1652                  forum_topics_unapproved = forum_topics_unapproved + ' . $total_topics_unapproved . ',
1653                  forum_topics_softdeleted = forum_topics_softdeleted + ' . $total_topics_softdeleted . '
1654              WHERE forum_id = ' . $to_forum_id;
1655          $db->sql_query($sql);
1656  
1657          if (!empty($counter))
1658          {
1659              // Do only one query per user and not a query per post.
1660              foreach ($counter as $user_id => $count)
1661              {
1662                  $sql = 'UPDATE ' . USERS_TABLE . '
1663                      SET user_posts = user_posts + ' . (int) $count . '
1664                      WHERE user_id = ' . (int) $user_id;
1665                  $db->sql_query($sql);
1666              }
1667          }
1668  
1669          sync('topic', 'topic_id', $new_topic_id_list);
1670          sync('forum', 'forum_id', $to_forum_id);
1671  
1672          $config->increment('num_topics', count($new_topic_id_list), false);
1673          $config->increment('num_posts', $total_posts, false);
1674  
1675          foreach ($new_topic_id_list as $topic_id => $new_topic_id)
1676          {
1677              $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_FORK', false, array(
1678                  'forum_id' => $to_forum_id,
1679                  'topic_id' => $new_topic_id,
1680                  $topic_row['forum_name']
1681              ));
1682          }
1683  
1684          $success_msg = (count($topic_ids) == 1) ? 'TOPIC_FORKED_SUCCESS' : 'TOPICS_FORKED_SUCCESS';
1685      }
1686      else
1687      {
1688          $template->assign_vars(array(
1689              'S_FORUM_SELECT'        => make_forum_select($to_forum_id, false, false, true, true, true),
1690              'S_CAN_LEAVE_SHADOW'    => false,
1691              'ADDITIONAL_MSG'        => $additional_msg)
1692          );
1693  
1694          confirm_box(false, 'FORK_TOPIC' . ((count($topic_ids) == 1) ? '' : 'S'), $s_hidden_fields, 'mcp_move.html');
1695      }
1696  
1697      $redirect = $request->variable('redirect', "index.$phpEx");
1698      $redirect = reapply_sid($redirect);
1699  
1700      if (!$success_msg)
1701      {
1702          redirect($redirect);
1703      }
1704      else
1705      {
1706          $redirect_url = append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id);
1707          meta_refresh(3, $redirect_url);
1708          $return_link = sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>');
1709  
1710          if ($forum_id != $to_forum_id)
1711          {
1712              $return_link .= '<br /><br />' . sprintf($user->lang['RETURN_NEW_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $to_forum_id) . '">', '</a>');
1713          }
1714  
1715          trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
1716      }
1717  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1