BxAntispamIP.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Antispam Antispam
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Blocking/whitelisting by IP using local database
  13. */
  14. class BxAntispamIP extends BxDol
  15. {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. /**
  21. * Check if IP is directly whitelisted by IP address or by IP address range
  22. *
  23. * @param $sCurIP IP to check, or empty for current IP
  24. * @return true - if IP is whitelisted or under cron execution; false - if IP is not whitelisted, or feature is not enabled
  25. */
  26. public function isIpWhitelisted ($sCurIP = '')
  27. {
  28. if (defined('BX_DOL_CRON_EXECUTE'))
  29. return true;
  30. $iIPGlobalType = (int)getParam('bx_antispam_ip_list_type');
  31. if ($iIPGlobalType != 1 && $iIPGlobalType != 2) // 0 - disabled
  32. return false;
  33. if (!$sCurIP)
  34. $sCurIP = getVisitorIP();
  35. $iCurIP = sprintf("%u", ip2long($sCurIP));
  36. $iCurrTime = time();
  37. return db_value("SELECT `ID` FROM `sys_ip_list` WHERE `Type` = 'allow' AND `LastDT` > $iCurrTime AND `From` <= '$iCurIP' AND `To` >= '$iCurIP' LIMIT 1") ? true : false;
  38. }
  39. /**
  40. * Check if IP is directly bloked by IP address or by IP address range
  41. *
  42. * @param $sCurIP IP to check, or empty for current IP
  43. * @return true - if IP is blocked; false - if IP is not blocked, or feature is not enabled, or it is run under cron
  44. */
  45. public function isIpBlocked ($sCurIP = '')
  46. {
  47. if (defined('BX_DOL_CRON_EXECUTE'))
  48. return false;
  49. $iIPGlobalType = (int)getParam('bx_antispam_ip_list_type');
  50. if ($iIPGlobalType != 1 && $iIPGlobalType != 2) // 0 - disabled
  51. return false;
  52. if (!$sCurIP)
  53. $sCurIP = getVisitorIP();
  54. $iCurIP = sprintf("%u", ip2long($sCurIP));
  55. $iCurrTime = time();
  56. if ($this->isIpWhitelisted($sCurIP))
  57. return false;
  58. $isBlocked = db_value("SELECT `ID` FROM `sys_ip_list` WHERE `Type` = 'deny' AND `LastDT` > $iCurrTime AND `From` <= '$iCurIP' AND `To` >= '$iCurIP' LIMIT 1");
  59. if ($isBlocked)
  60. return true;
  61. // 1 - all allowed except listed
  62. // 2 - all blocked except listed
  63. return $iIPGlobalType == 2 ? true : false;
  64. }
  65. /**
  66. * Add IP to blocklist
  67. *
  68. * @param $sIP IP as string or long integer
  69. * @param $iExpirationInSec set expiration in seconds
  70. * @param $sComment comment about blocking
  71. * @return false on error, not false on success
  72. */
  73. public function blockIp($mixedIP, $iExpirationInSec = 86400, $sComment = '')
  74. {
  75. if (preg_match('/^[0-9]+$/', $mixedIP))
  76. $iIP = $mixedIP;
  77. else
  78. $iIP = sprintf("%u", ip2long($sIP));
  79. $iExpirationInSec = time() + (int)$iExpirationInSec;
  80. $oDb = BxDolDb::getInstance();
  81. $sQuery = $oDb->prepare("SELECT ID FROM `sys_ip_list` WHERE `From` = ? AND `To` = ? LIMIT 1", $iIP, $iIP);
  82. if (!$oDb->getOne($sQuery)) {
  83. $sQuery = $oDb->prepare("INSERT INTO `sys_ip_list` SET `From` = ?, `To` = ?, `Type` = 'deny', `LastDT` = ?, `Desc` = ?", $iIP, $iIP, $iExpirationInSec, $sComment);
  84. return $oDb->res($sQuery);
  85. }
  86. return false;
  87. }
  88. }
  89. /** @} */