[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/ -> class_stopforumspamchecker.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  /**
  12   * Registration checker to check registrations against the StopForumSpam.com database.
  13   */
  14  class StopForumSpamChecker
  15  {
  16      /**
  17       * The base URL format to the stop forum spam API.
  18       *
  19       * @var string
  20       */
  21      const STOP_FORUM_SPAM_API_URL_FORMAT = 'https://api.stopforumspam.org/api?username=%s&email=%s&ip=%s&f=json&confidence';
  22      /**
  23       * @var pluginSystem
  24       */
  25      private $plugins = null;
  26      /**
  27       * The minimum weighting before a user is considered to be a spammer.
  28       *
  29       * @var double
  30       */
  31      private $min_weighting_before_spam = null;
  32      /**
  33       * Whether to check usernames against StopForumSPam. If set to false, the username weighting won't be used.
  34       *
  35       * @var bool
  36       */
  37      private $check_usernames = false;
  38      /**
  39       * Whether to check email addresses against StopForumSPam. If set to false, the username weighting won't be used.
  40       *
  41       * @var bool
  42       */
  43      private $check_emails = true;
  44      /**
  45       * Whether to check IP addresses against StopForumSPam. If set to false, the username weighting won't be used.
  46       *
  47       * @var bool
  48       */
  49      private $check_ips = true;
  50      /**
  51       * Whether to log whenever a user is found to be a spammer.
  52       *
  53       * @var bool
  54       */
  55      private $log_blocks;
  56  
  57      /**
  58       * Create a new instance of the StopForumSpam.com checker.
  59       *
  60       * @param pluginSystem $plugins                   An instance of the plugin system.
  61       * @param double       $min_weighting_before_spam The minimum confidence rating before a user is considered definitely spam.
  62       * @param bool         $check_usernames           Whether to check usernames against StopForumSpam.
  63       * @param bool         $check_emails              Whether to check email address against StopForumSpam.
  64       * @param bool         $check_ips                 Whether to check IP addresses against StopForumSpam.
  65       */
  66  	public function __construct(&$plugins, $min_weighting_before_spam = 50.00, $check_usernames = false, $check_emails = true, $check_ips = true, $log_blocks = true)
  67      {
  68          $this->plugins                   = $plugins;
  69          $this->min_weighting_before_spam = (double)$min_weighting_before_spam;
  70          $this->check_usernames           = (bool)$check_usernames;
  71          $this->check_emails              = (bool)$check_emails;
  72          $this->check_ips                 = (bool)$check_ips;
  73          $this->log_blocks                = (bool)$log_blocks;
  74      }
  75  
  76      /**
  77       * Check a user against the 3rd party service to determine whether they are a spammer.
  78       *
  79       * @param string $username   The username of the user to check.
  80       * @param string $email      The email address of the user to check.
  81       * @param string $ip_address The IP address sof the user to check.
  82       * @return bool Whether the user is considered a spammer or not.
  83       * @throws Exception Thrown when there's an error fetching from the StopForumSpam API or when the data cannot be decoded.
  84       */
  85  	public function is_user_a_spammer($username = '', $email = '', $ip_address = '')
  86      {
  87          $is_spammer = false;
  88          $checknum = $confidence = 0;
  89  
  90          if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  91          {
  92              throw new Exception("stopforumspam_invalid_email");
  93          }
  94  
  95          if(!filter_var($ip_address, FILTER_VALIDATE_IP))
  96          {
  97              throw new Exception('stopforumspam_invalid_ip_address');
  98          }
  99  
 100          $is_internal_ip = !filter_var(
 101              $ip_address,
 102              FILTER_VALIDATE_IP,
 103              FILTER_FLAG_NO_PRIV_RANGE |  FILTER_FLAG_NO_RES_RANGE
 104          );
 105  
 106          if($is_internal_ip)
 107          {
 108              return false;
 109          }
 110  
 111          $username_encoded = urlencode($username);
 112          $email_encoded    = urlencode($email);
 113  
 114          $check_url = sprintf(self::STOP_FORUM_SPAM_API_URL_FORMAT, $username_encoded, $email_encoded, $ip_address);
 115  
 116          $result = fetch_remote_file($check_url);
 117  
 118          if($result !== false)
 119          {
 120              $result_json = @json_decode($result);
 121  
 122              if($result_json != null && !isset($result_json->error))
 123              {
 124                  if($this->check_usernames && $result_json->username->appears)
 125                  {
 126                      $checknum++;
 127                      $confidence += $result_json->username->confidence;
 128                  }
 129  
 130                  if($this->check_emails && $result_json->email->appears)
 131                  {
 132                      $checknum++;
 133                      $confidence += $result_json->email->confidence;
 134                  }
 135  
 136                  if($this->check_ips && $result_json->ip->appears)
 137                  {
 138                      $checknum++;
 139                      $confidence += $result_json->ip->confidence;
 140                  }
 141                  
 142                  if($checknum > 0 && $confidence)
 143                  {
 144                      $confidence = $confidence / $checknum;
 145                  }
 146  
 147                  if($confidence > $this->min_weighting_before_spam)
 148                  {
 149                      $is_spammer = true;
 150                  }
 151              }
 152              else
 153              {
 154                  throw new Exception('stopforumspam_error_decoding');
 155              }
 156          }
 157          else
 158          {
 159              throw new Exception('stopforumspam_error_retrieving');
 160          }
 161  
 162          if($this->plugins)
 163          {
 164              $params = array(
 165                  'username'   => &$username,
 166                  'email'      => &$email,
 167                  'ip_address' => &$ip_address,
 168                  'is_spammer' => &$is_spammer,
 169                  'confidence' => &$confidence,
 170              );
 171  
 172              $this->plugins->run_hooks('stopforumspam_check_spammer_pre_return', $params);
 173          }
 174  
 175          if($this->log_blocks && $is_spammer)
 176          {
 177              log_spam_block(
 178                  $username, $email, $ip_address, array(
 179                      'confidence' => (double)$confidence,
 180                  )
 181              );
 182          }
 183  
 184          return $is_spammer;
 185      }
 186  
 187      /**
 188       * @param array $sfsSettingsEnabled
 189       *
 190       * @return string
 191       */
 192  	public function getErrorText($sfsSettingsEnabled)
 193      {
 194          global $mybb, $lang;
 195  
 196          foreach($sfsSettingsEnabled as $setting)
 197          {
 198              if($setting == 'stopforumspam_check_usernames' && $mybb->settings[$setting])
 199              {
 200                  $settingsenabled[] = $lang->sfs_error_username;
 201                  continue;
 202              }
 203  
 204              if($setting == 'stopforumspam_check_emails' && $mybb->settings[$setting])
 205              {
 206                  $settingsenabled[] = $lang->sfs_error_email;
 207                  continue;
 208              }
 209  
 210              if($setting == 'stopforumspam_check_ips' && $mybb->settings[$setting])
 211              {
 212                  $settingsenabled[] = $lang->sfs_error_ip;
 213                  continue;
 214              }
 215          }
 216  
 217          if(sizeof($settingsenabled) > 1)
 218          {
 219              $lastsetting = $settingsenabled[sizeof($settingsenabled)-1];
 220              unset($settingsenabled[sizeof($settingsenabled)-1]);
 221  
 222              $stopforumspamerror = implode($lang->comma, $settingsenabled) . " {$lang->sfs_error_or} " . $lastsetting;
 223          }
 224          else
 225          {
 226              $stopforumspamerror = $settingsenabled[0];
 227          }
 228          return $stopforumspamerror;
 229      }
 230  }


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