PEMapModder

LegionPE SpamDetector.php

Apr 2nd, 2015
233
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace legionpe\session;
  4.  
  5. use legionpe\config\Settings;
  6. use legionpe\MysqlConnection;
  7.  
  8. class SpamDetector{
  9.     private $ses;
  10.     /** @var float|null */
  11.     private $lastMsgTime = null;
  12.     /** @var bool */
  13.     private $bypass;
  14.     /** @var int[] */
  15.     private $lengths = [10, 10, 10, 10];
  16.     /** @var string[] */
  17.     private $lastMsgs = ["", "", ""];
  18.     public function __construct(Session $session){
  19.         $this->ses = $session;
  20.         $this->bypass = ($session->getRank() & Settings::RANK_PERM_SPAM) > 0;
  21.     }
  22.     public function onMessage($msg){
  23.         if($this->onMsg($msg)){
  24.             $this->lastMsgTime = microtime(true);
  25.             return true;
  26.         }
  27.         return false;
  28.     }
  29.     private function onMsg($msg){
  30.         if($msg === "SPAM!! SPAM!! SPAM!! SPAM!! SPAM!! SPAM!! SPAM!! SPAM!!" and !$this->ses->isKicked()){
  31.             $this->ses->kick("You have been banned for 7 days! Reason: using the Ghost Hack mod");
  32.             $this->ses->getMain()->getMySQLi()->query("INSERT INTO ipbans (ip, msg, issuer, creation, length) VALUES (%s, %s, %s, from_unixtime(%d), %d);", MysqlConnection::RAW, $this->ses->getPlayer()->getAddress(), "Using Ghost Hack mod, detected by spamming", "LegionPE SpamDetective", time(), 3600 * 24 * 7);
  33.             $this->ses->getMain()->getServer()->broadcast("$this->ses ({$this->ses->getPlayer()->getAddress()}) said the Ghost Hack spam message and is IP-banned for 7 days!", "pocketmine.broadcast.admin");
  34.             return false;
  35.         }
  36.         if(strtoupper($msg) === $msg and strtolower($msg) !== $msg){
  37.             $this->ses->tell("Abusive caps are disallowed.");
  38.             return false;
  39.         }
  40.         if($this->lastMsgTime === null){
  41.             return true;
  42.         }
  43.         if($this->bypass){
  44.             return true;
  45.         }
  46.         if(microtime(true) - $this->lastMsgTime < 2.5){
  47.             $this->ses->tell("You don't realize that sending chat messages that quick is considered as spamming?");
  48.             return false;
  49.         }
  50.         $s = array_shift($this->lengths);
  51.         $cnt = 1;
  52.         $this->lengths[] = strlen($msg);
  53.         foreach($this->lengths as $l){
  54.             $cnt++;
  55.             $s += $l;
  56.         }
  57.         if($s <= $cnt + 1){
  58.             $this->ses->tell("Your recent messages are too short!");
  59.             return false;
  60.         }
  61.         $last = array_shift($this->lastMsgs);
  62.         $this->lastMsgs[] = $msg;
  63.         foreach($this->lastMsgs as $m){
  64.             if($m !== $last){
  65.                 return true;
  66.             }
  67.         }
  68.         $this->ses->tell("No spamming.");
  69.         return false;
  70.     }
  71. }
RAW Paste Data