[ Index ]

PHP Cross Reference of MyBB 1.8.29

title

Body

[close]

/inc/ -> class_custommoderation.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  // Disallow direct access to this file for security reasons
  12  if(!defined("IN_MYBB"))
  13  {
  14      die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
  15  }
  16  
  17  /**
  18   * Used to execute a custom moderation tool
  19   *
  20   */
  21  
  22  class CustomModeration extends Moderation
  23  {
  24      /**
  25       * Get info on a tool
  26       *
  27       * @param int $tool_id Tool ID
  28       * @return array|bool Returns tool data (tid, type, name, description) in an array, otherwise boolean false.
  29       */
  30  	function tool_info($tool_id)
  31      {
  32          global $db;
  33  
  34          // Get tool info
  35          $query = $db->simple_select("modtools", "*", 'tid='.(int)$tool_id);
  36          $tool = $db->fetch_array($query);
  37          if(!$tool['tid'])
  38          {
  39              return false;
  40          }
  41          else
  42          {
  43              return $tool;
  44          }
  45      }
  46  
  47      /**
  48       * Execute Custom Moderation Tool
  49       *
  50       * @param int $tool_id Tool ID
  51       * @param int|array Thread ID(s)
  52       * @param int|array Post ID(s)
  53       * @return string 'forum' or 'default' indicating where to redirect
  54       */
  55  	function execute($tool_id, $tids=0, $pids=0)
  56      {
  57          global $db;
  58  
  59          // Get tool info
  60          $query = $db->simple_select("modtools", '*', 'tid='.(int)$tool_id);
  61          $tool = $db->fetch_array($query);
  62          if(!$tool['tid'])
  63          {
  64              return false;
  65          }
  66  
  67          // Format single tid and pid
  68          if(!is_array($tids))
  69          {
  70              $tids = array($tids);
  71          }
  72          if(!is_array($pids))
  73          {
  74              $pids = array($pids);
  75          }
  76  
  77          // Unserialize custom moderation
  78          $post_options = my_unserialize($tool['postoptions']);
  79          $thread_options = my_unserialize($tool['threadoptions']);
  80  
  81          // If the tool type is a post tool, then execute the post moderation
  82          $deleted_thread = 0;
  83          if($tool['type'] == 'p')
  84          {
  85              $deleted_thread = $this->execute_post_moderation($tids, $post_options, $pids);
  86          }
  87          // Always execute thead moderation
  88          $this->execute_thread_moderation($thread_options, $tids);
  89  
  90          // If the thread is deleted, indicate to the calling script to redirect to the forum, and not the nonexistant thread
  91          if($thread_options['deletethread'] == 1 || $deleted_thread === 1)
  92          {
  93              return 'forum';
  94          }
  95          return 'default';
  96      }
  97  
  98      /**
  99       * Execute Inline Post Moderation
 100       *
 101       * @param array|int $tid Thread IDs (in order of dateline ascending). Only the first one will be used
 102       * @param array $post_options Moderation information
 103       * @param array $pids Post IDs
 104       *
 105       * @return boolean true
 106       */
 107  	function execute_post_moderation($tid, $post_options=array(), $pids=array())
 108      {
 109          global $db, $mybb, $lang, $plugins;
 110  
 111          if(is_array($tid))
 112          {
 113              $tid = (int)$tid[0]; // There's only 1 thread when doing inline post moderation
 114              // The thread chosen is the first thread in the array of tids.
 115              // It is recommended that this be the tid of the oldest post
 116          }
 117  
 118          // Get the information about thread
 119          $thread = get_thread($tid);
 120          $author = get_user($thread['uid']);
 121  
 122          $args = array(
 123              'post_options' => &$post_options,
 124              'pids' => &$pids,
 125              'thread' => &$thread,
 126          );
 127  
 128          $plugins->run_hooks("class_custommoderation_execute_post_moderation_start", $args);
 129  
 130          // If deleting posts, only do that
 131          if($post_options['deleteposts'] == 1)
 132          {
 133              foreach($pids as $pid)
 134              {
 135                  $this->delete_post($pid);
 136              }
 137  
 138              $delete_tids = array();
 139              $imploded_pids = implode(",", array_map("intval", $pids));
 140              $query = $db->simple_select("threads", "tid", "firstpost IN ({$imploded_pids})");
 141              while($threadid = $db->fetch_field($query, "tid"))
 142              {
 143                  $delete_tids[] = $threadid;
 144              }
 145              if(!empty($delete_tids))
 146              {
 147                  foreach($delete_tids as $delete_tid)
 148                  {
 149                      $this->delete_thread($delete_tid);
 150                  }
 151                  // return true here so the code in execute() above knows to redirect to the forum
 152                  return true;
 153              }
 154          }
 155          else
 156          {
 157              if($post_options['mergeposts'] == 1) // Merge posts
 158              {
 159                  $this->merge_posts($pids);
 160              }
 161  
 162              if($post_options['approveposts'] == 'approve') // Approve posts
 163              {
 164                  $this->approve_posts($pids);
 165              }
 166              elseif($post_options['approveposts'] == 'unapprove') // Unapprove posts
 167              {
 168                  $this->unapprove_posts($pids);
 169              }
 170              elseif($post_options['approveposts'] == 'toggle') // Toggle post visibility
 171              {
 172                  $this->toggle_post_visibility($pids);
 173              }
 174  
 175              if($post_options['softdeleteposts'] == 'softdelete') // Soft delete posts
 176              {
 177                  $this->soft_delete_posts($pids);
 178              }
 179              elseif($post_options['softdeleteposts'] == 'restore') // Restore posts
 180              {
 181                  $this->restore_posts($pids);
 182              }
 183              elseif($post_options['softdeleteposts'] == 'toggle') // Toggle post visibility
 184              {
 185                  $this->toggle_post_softdelete($pids);
 186              }
 187  
 188              if($post_options['splitposts'] > 0 || $post_options['splitposts'] == -2) // Split posts
 189              {
 190                  $query = $db->simple_select("posts", "COUNT(*) AS totalposts", "tid='{$tid}'");
 191                  $count = $db->fetch_array($query);
 192  
 193                  if($count['totalposts'] == 1)
 194                  {
 195                      error($lang->error_cantsplitonepost);
 196                  }
 197  
 198                  if($count['totalposts'] == count($pids))
 199                  {
 200                      error($lang->error_cantsplitall);
 201                  }
 202  
 203                  if($post_options['splitposts'] == -2)
 204                  {
 205                      $post_options['splitposts'] = $thread['fid'];
 206                  }
 207                  if(empty($post_options['splitpostsnewsubject']))
 208                  {
 209                      // Enter in a subject if a predefined one does not exist.
 210                      $post_options['splitpostsnewsubject'] = "{$lang->split_thread_subject} {$thread['subject']}";
 211                  }
 212  
 213                  $find = array('{username}', '{author}', '{subject}');
 214                  $replace = array($mybb->user['username'], $author['username'], $thread['subject']);
 215  
 216                  $new_subject = str_ireplace($find, $replace, $post_options['splitpostsnewsubject']);
 217  
 218                  $args = array(
 219                      'post_options' => &$post_options,
 220                      'pids' => &$pids,
 221                      'thread' => &$thread,
 222                      'new_subject' => &$new_subject,
 223                  );
 224  
 225                  $plugins->run_hooks("class_custommoderation_splitposts", $args);
 226  
 227                  $new_thread_subject = $new_subject;
 228                  $new_tid = $this->split_posts($pids, $tid, $post_options['splitposts'], $new_thread_subject);
 229  
 230                  if($post_options['splitpostsclose'] == 'close') // Close new thread
 231                  {
 232                      $this->close_threads($new_tid);
 233                  }
 234                  if($post_options['splitpostsstick'] == 'stick') // Stick new thread
 235                  {
 236                      $this->stick_threads($new_tid);
 237                  }
 238                  if($post_options['splitpostsunapprove'] == 'unapprove') // Unapprove new thread
 239                  {
 240                      $this->unapprove_threads($new_tid, $thread['fid']);
 241                  }
 242                  if($post_options['splitthreadprefix'] != '0')
 243                  {
 244                      $this->apply_thread_prefix($new_tid, $post_options['splitthreadprefix']); // Add thread prefix to new thread
 245                  }
 246                  if(!empty($post_options['splitpostsaddreply'])) // Add reply to new thread
 247                  {
 248                      require_once  MYBB_ROOT."inc/datahandlers/post.php";
 249                      $posthandler = new PostDataHandler("insert");
 250  
 251                      $find = array('{username}', '{author}', '{subject}');
 252                      $replace = array($mybb->user['username'], $author['username'], $new_thread_subject);
 253  
 254                      if(empty($post_options['splitpostsreplysubject']))
 255                      {
 256                          $new_subject = 'RE: '.$new_thread_subject;
 257                      }
 258                      else
 259                      {
 260                          $new_subject = str_ireplace($find, $replace, $post_options['splitpostsreplysubject']);
 261                      }
 262  
 263                      $new_message = str_ireplace($find, $replace, $post_options['splitpostsaddreply']);
 264  
 265                      $args = array(
 266                          'post_options' => &$post_options,
 267                          'pids' => &$pids,
 268                          'thread' => &$thread,
 269                          'new_subject' => &$new_subject,
 270                          'new_message' => &$new_message,
 271                      );
 272  
 273                      $plugins->run_hooks("class_custommoderation_splitpostsaddreply", $args);
 274  
 275                      // Set the post data that came from the input to the $post array.
 276                      $post = array(
 277                          "tid" => $new_tid,
 278                          "fid" => $post_options['splitposts'],
 279                          "subject" => $new_subject,
 280                          "uid" => $mybb->user['uid'],
 281                          "username" => $mybb->user['username'],
 282                          "message" => $new_message,
 283                          "ipaddress" => my_inet_pton(get_ip()),
 284                      );
 285                      // Set up the post options from the input.
 286                      $post['options'] = array(
 287                          "signature" => 1,
 288                          "emailnotify" => 0,
 289                          "disablesmilies" => 0
 290                      );
 291  
 292                      $posthandler->set_data($post);
 293  
 294                      if($posthandler->validate_post($post))
 295                      {
 296                          $posthandler->insert_post($post);
 297                      }
 298                  }
 299              }
 300          }
 301  
 302          $args = array(
 303              'post_options' => &$post_options,
 304              'pids' => &$pids,
 305              'thread' => &$thread,
 306          );
 307  
 308          $plugins->run_hooks("class_custommoderation_execute_post_moderation_end", $args);
 309  
 310          return true;
 311      }
 312  
 313      /**
 314       * Execute Normal and Inline Thread Moderation
 315       *
 316       * @param array $thread_options Moderation information
 317       * @param array Thread IDs. Only the first one will be used, but it needs to be an array
 318       * @return boolean true
 319       */
 320  	function execute_thread_moderation($thread_options=array(), $tids=array())
 321      {
 322          global $db, $mybb, $plugins;
 323  
 324          $tid = (int)$tids[0]; // Take the first thread to get thread data from
 325          $query = $db->simple_select("threads", 'fid', "tid='$tid'");
 326          $thread = $db->fetch_array($query);
 327  
 328          $args = array(
 329              'thread_options' => &$thread_options,
 330              'tids' => &$tids,
 331              'thread' => &$thread,
 332          );
 333  
 334          $plugins->run_hooks("class_custommoderation_execute_thread_moderation_start", $args);
 335  
 336          // If deleting threads, only do that
 337          if($thread_options['deletethread'] == 1)
 338          {
 339              foreach($tids as $tid)
 340              {
 341                  $this->delete_thread($tid);
 342              }
 343          }
 344          else
 345          {
 346              if($thread_options['mergethreads'] == 1 && count($tids) > 1) // Merge Threads (ugly temp code until find better fix)
 347              {
 348                  $tid_list = implode(',', $tids);
 349                  $options = array('order_by' => 'dateline', 'order_dir' => 'DESC');
 350                  $query = $db->simple_select("threads", 'tid, subject', "tid IN ($tid_list)", $options); // Select threads from newest to oldest
 351                  $last_tid = 0;
 352                  while($tid = $db->fetch_array($query))
 353                  {
 354                      if($last_tid != 0)
 355                      {
 356                          $this->merge_threads($last_tid, $tid['tid'], $tid['subject']); // And keep merging them until we get down to one thread.
 357                      }
 358                      $last_tid = $tid['tid'];
 359                  }
 360              }
 361              if($thread_options['deletepoll'] == 1) // Delete poll
 362              {
 363                  foreach($tids as $tid)
 364                  {
 365                      $this->delete_poll($tid);
 366                  }
 367              }
 368              if($thread_options['removeredirects'] == 1) // Remove redirects
 369              {
 370                  foreach($tids as $tid)
 371                  {
 372                      $this->remove_redirects($tid);
 373                  }
 374              }
 375  
 376              if($thread_options['removesubscriptions'] == 1) // Remove thread subscriptions
 377              {
 378                  $this->remove_thread_subscriptions($tids, true);
 379              }
 380  
 381              if($thread_options['approvethread'] == 'approve') // Approve thread
 382              {
 383                  $this->approve_threads($tids, $thread['fid']);
 384              }
 385              elseif($thread_options['approvethread'] == 'unapprove') // Unapprove thread
 386              {
 387                  $this->unapprove_threads($tids, $thread['fid']);
 388              }
 389              elseif($thread_options['approvethread'] == 'toggle') // Toggle thread visibility
 390              {
 391                  $this->toggle_thread_visibility($tids, $thread['fid']);
 392              }
 393  
 394              if($thread_options['softdeletethread'] == 'softdelete') // Soft delete thread
 395              {
 396                  $this->soft_delete_threads($tids);
 397              }
 398              elseif($thread_options['softdeletethread'] == 'restore') // Restore thread
 399              {
 400                  $this->restore_threads($tids);
 401              }
 402              elseif($thread_options['softdeletethread'] == 'toggle') // Toggle thread visibility
 403              {
 404                  $this->toggle_thread_softdelete($tids);
 405              }
 406  
 407              if($thread_options['openthread'] == 'open') // Open thread
 408              {
 409                  $this->open_threads($tids);
 410              }
 411              elseif($thread_options['openthread'] == 'close') // Close thread
 412              {
 413                  $this->close_threads($tids);
 414              }
 415              elseif($thread_options['openthread'] == 'toggle') // Toggle thread visibility
 416              {
 417                  $this->toggle_thread_status($tids);
 418              }
 419  
 420              if($thread_options['stickthread'] == 'stick') // Stick thread
 421              {
 422                  $this->stick_threads($tids);
 423              }
 424              elseif($thread_options['stickthread'] == 'unstick') // Unstick thread
 425              {
 426                  $this->unstick_threads($tids);
 427              }
 428              elseif($thread_options['stickthread'] == 'toggle') // Toggle thread importance
 429              {
 430                  $this->toggle_thread_importance($tids);
 431              }
 432  
 433              if($thread_options['threadprefix'] != '-1')
 434              {
 435                  $this->apply_thread_prefix($tids, $thread_options['threadprefix']); // Update thread prefix
 436              }
 437  
 438              if(my_strtolower(trim($thread_options['newsubject'])) != '{subject}') // Update thread subjects
 439              {
 440                  $this->change_thread_subject($tids, $thread_options['newsubject']);
 441              }
 442              if(!empty($thread_options['addreply'])) // Add reply to thread
 443              {
 444                  $tid_list = implode(',', $tids);
 445                  $query = $db->query("
 446                      SELECT u.uid, u.username, t.fid, t.subject, t.tid, t.firstpost, t.closed FROM ".TABLE_PREFIX."threads t
 447                      LEFT JOIN ".TABLE_PREFIX."users u ON t.uid=u.uid
 448                      WHERE tid IN ($tid_list) AND closed NOT LIKE 'moved|%'
 449                  ");
 450                  require_once  MYBB_ROOT."inc/datahandlers/post.php";
 451  
 452                  // Loop threads adding a reply to each one
 453                  while($thread = $db->fetch_array($query))
 454                  {
 455                      $posthandler = new PostDataHandler("insert");
 456  
 457                      $find = array('{username}', '{author}', '{subject}');
 458                      $replace = array($mybb->user['username'], $thread['username'], $thread['subject']);
 459  
 460                      if(empty($thread_options['replysubject']))
 461                      {
 462                          $new_subject = 'RE: '.$thread['subject'];
 463                      }
 464                      else
 465                      {
 466                          $new_subject = str_ireplace($find, $replace, $thread_options['replysubject']);
 467                      }
 468  
 469                      $new_message = str_ireplace($find, $replace, $thread_options['addreply']);
 470  
 471                      $args = array(
 472                          'thread_options' => &$thread_options,
 473                          'tids' => &$tids,
 474                          'thread' => &$thread,
 475                          'new_subject' => &$new_subject,
 476                          'new_message' => &$new_message,
 477                      );
 478  
 479                      $plugins->run_hooks("class_custommoderation_addreply", $args);
 480  
 481                      // Set the post data that came from the input to the $post array.
 482                      $post = array(
 483                          "tid" => $thread['tid'],
 484                          "replyto" => $thread['firstpost'],
 485                          "fid" => $thread['fid'],
 486                          "subject" => $new_subject,
 487                          "uid" => $mybb->user['uid'],
 488                          "username" => $mybb->user['username'],
 489                          "message" => $new_message,
 490                          "ipaddress" => my_inet_pton(get_ip()),
 491                      );
 492  
 493                      // Set up the post options from the input.
 494                      $post['options'] = array(
 495                          "signature" => 1,
 496                          "emailnotify" => 0,
 497                          "disablesmilies" => 0
 498                      );
 499  
 500                      if($thread['closed'] == 1)
 501                      {
 502                          // Keep this thread closed
 503                          $post['modoptions']['closethread'] = 1;
 504                      }
 505  
 506                      $posthandler->set_data($post);
 507                      if($posthandler->validate_post($post))
 508                      {
 509                          $posthandler->insert_post($post);
 510                      }
 511                  }
 512              }
 513              if($thread_options['movethread'] > 0 && $thread_options['movethread'] != $thread['fid']) // Move thread
 514              {
 515                  if($thread_options['movethreadredirect'] == 1) // Move Thread with redirect
 516                  {
 517                      $time = TIME_NOW + ($thread_options['movethreadredirectexpire'] * 86400);
 518                      foreach($tids as $tid)
 519                      {
 520                          $this->move_thread($tid, $thread_options['movethread'], 'redirect', $time);
 521                      }
 522                  }
 523                  else // Normal move
 524                  {
 525                      $this->move_threads($tids, $thread_options['movethread']);
 526                  }
 527              }
 528              if($thread_options['copythread'] > 0 || $thread_options['copythread'] == -2) // Copy thread
 529              {
 530                  if($thread_options['copythread'] == -2)
 531                  {
 532                      $thread_options['copythread'] = $thread['fid'];
 533                  }
 534                  foreach($tids as $tid)
 535                  {
 536                      $new_tid = $this->move_thread($tid, $thread_options['copythread'], 'copy');
 537                  }
 538              }
 539              if(!empty($thread_options['recountrebuild']))
 540              {
 541                  require_once  MYBB_ROOT.'/inc/functions_rebuild.php';
 542  
 543                  foreach($tids as $tid)
 544                  {
 545                      rebuild_thread_counters($tid);
 546                  }
 547              }
 548          }
 549          
 550          // Do we have a PM subject and PM message?
 551          if(isset($thread_options['pm_subject']) && $thread_options['pm_subject'] != '' && isset($thread_options['pm_message']) && $thread_options['pm_message'] != '')
 552          {
 553              $tid_list = implode(',', $tids);
 554              
 555              // For each thread, we send a PM to the author
 556              $query = $db->query("
 557                  SELECT u.uid, u.username, t.subject FROM ".TABLE_PREFIX."threads t
 558                  LEFT JOIN ".TABLE_PREFIX."users u ON t.uid=u.uid
 559                  WHERE tid IN ($tid_list)
 560              ");
 561              while($thread = $db->fetch_array($query))
 562              {
 563                  $find = array('{username}', '{author}', '{subject}');
 564                  $replace = array($mybb->user['username'], $thread['username'], $thread['subject']);
 565  
 566                  $pm_subject = str_ireplace($find, $replace, $thread_options['pm_subject']);
 567                  $pm_message = str_ireplace($find, $replace, $thread_options['pm_message']);
 568  
 569                  $args = array(
 570                      'thread_options' => &$thread_options,
 571                      'tids' => &$tids,
 572                      'thread' => &$thread,
 573                      'pm_subject' => &$pm_subject,
 574                      'pm_message' => &$pm_message,
 575                  );
 576  
 577                  $plugins->run_hooks("class_custommoderation_pm", $args);
 578  
 579                  // Let's send our PM
 580                  $pm = array(
 581                      'subject' => $pm_subject,
 582                      'message' => $pm_message,
 583                      'touid' => $thread['uid']
 584                  );
 585                  send_pm($pm, $mybb->user['uid'], 1);
 586              }
 587          }
 588  
 589          $args = array(
 590              'thread_options' => &$thread_options,
 591              'tids' => &$tids,
 592              'thread' => &$thread,
 593          );
 594  
 595          $plugins->run_hooks("class_custommoderation_execute_thread_moderation_end", $args);
 596          
 597          return true;
 598      }
 599  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref