[ Index ]

PHP Cross Reference of MyBB 1.8.41

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


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