BxAntispamIP.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. define('BX_ANTISPAM_IP_TABLE_DISABLED', 0);
  12. define('BX_ANTISPAM_IP_TABLE_ALL_ALLOWED_EXCEPT_LISTED', 1);
  13. define('BX_ANTISPAM_IP_TABLE_ALL_BLOCKED_EXCEPT_LISTED', 2);
  14. /**
  15. * Blocking/whitelisting by IP using local database
  16. */
  17. class BxAntispamIP extends BxDol
  18. {
  19. protected $_aIpTableConfigMap;
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->_aIpTableConfigMap = array (
  24. 0 => _t('_bx_antispam_ip_table_disabled'),
  25. 1 => _t('_bx_antispam_ip_table_all_allowed_except_listed'),
  26. 2 => _t('_bx_antispam_ip_table_all_blocked_except_listed'),
  27. );
  28. }
  29. /**
  30. * Check if IP is directly whitelisted by IP address or by IP address range
  31. *
  32. * @param $sCurIP IP to check, or empty for current IP
  33. * @return true - if IP is whitelisted or under cron execution; false - if IP is not whitelisted, or feature is not enabled
  34. */
  35. public function isIpWhitelisted ($sCurIP = '')
  36. {
  37. if (defined('BX_DOL_CRON_EXECUTE'))
  38. return true;
  39. $iIPGlobalType = (int)getParam('bx_antispam_ip_list_type');
  40. if (BX_ANTISPAM_IP_TABLE_DISABLED == $iIPGlobalType)
  41. return false;
  42. if (!$sCurIP)
  43. $sCurIP = getVisitorIP();
  44. return $this->_isIpListed('allow', $sCurIP);
  45. }
  46. /**
  47. * Check if IP is directly bloked by IP address or by IP address range
  48. *
  49. * @param $sCurIP IP to check, or empty for current IP
  50. * @return true - if IP is blocked; false - if IP is not blocked, or feature is not enabled, or it is run under cron
  51. */
  52. public function isIpBlocked ($sCurIP = '')
  53. {
  54. if (defined('BX_DOL_CRON_EXECUTE'))
  55. return false;
  56. $iIPGlobalType = (int)getParam('bx_antispam_ip_list_type');
  57. if (BX_ANTISPAM_IP_TABLE_DISABLED == $iIPGlobalType)
  58. return false;
  59. if (!$sCurIP)
  60. $sCurIP = getVisitorIP();
  61. if ($this->isIpWhitelisted($sCurIP))
  62. return false;
  63. if ($this->_isIpListed('deny', $sCurIP))
  64. return true;
  65. return BX_ANTISPAM_IP_TABLE_ALL_BLOCKED_EXCEPT_LISTED == $iIPGlobalType ? true : false;
  66. }
  67. /**
  68. * Add IP to blocklist
  69. *
  70. * @param $sIP IP as string or long integer
  71. * @param $iExpirationInSec set expiration in seconds
  72. * @param $sComment comment about blocking
  73. * @return false on error, not false on success
  74. */
  75. public function blockIp($mixedIP, $iExpirationInSec = 86400, $sComment = '')
  76. {
  77. if (preg_match('/^[0-9]+$/', $mixedIP))
  78. $iIP = $mixedIP;
  79. else
  80. $iIP = sprintf("%u", ip2long($mixedIP));
  81. $iExpirationInSec = time() + (int)$iExpirationInSec;
  82. $oDb = BxDolDb::getInstance();
  83. $sQuery = $oDb->prepare("SELECT ID FROM `bx_antispam_ip_table` WHERE `From` = ? AND `To` = ? LIMIT 1", $iIP, $iIP);
  84. if (!$oDb->getOne($sQuery)) {
  85. $sQuery = $oDb->prepare("INSERT INTO `bx_antispam_ip_table` SET `From` = ?, `To` = ?, `Type` = 'deny', `LastDT` = ?, `Desc` = ?", $iIP, $iIP, $iExpirationInSec, $sComment);
  86. return $oDb->res($sQuery);
  87. }
  88. return false;
  89. }
  90. /**
  91. * Get IP table direcitve by ID
  92. *
  93. * @param $sId
  94. * @return array with IP table directive data
  95. */
  96. public function getIpTableDirective($iId)
  97. {
  98. $oDb = BxDolDb::getInstance();
  99. $sQuery = $oDb->prepare("SELECT * FROM `bx_antispam_ip_table` WHERE `ID` = ? LIMIT 1", $iId);
  100. return $oDb->getRow($sQuery);
  101. }
  102. public function getIpTableConfigValues ()
  103. {
  104. return $this->_aIpTableConfigMap;
  105. }
  106. public function getIpTableConfigTitle ($i)
  107. {
  108. return $this->_aIpTableConfigMap[$i];
  109. }
  110. /**
  111. * Clean up expired entries
  112. * @return number of affected rows
  113. */
  114. public function pruning ()
  115. {
  116. $oDb = BxDolDb::getInstance();
  117. $sQuery = $oDb->prepare("DELETE FROM `bx_antispam_ip_table` WHERE `LastDT` <= ?", time());
  118. $iAffectedRows = $oDb->query($sQuery);
  119. if ($iAffectedRows) {
  120. $oDb->query("OPTIMIZE TABLE `bx_antispam_ip_table`");
  121. return $iAffectedRows;
  122. }
  123. return 0;
  124. }
  125. protected function _isIpListed($sType, $sIp)
  126. {
  127. $iIp = sprintf("%u", ip2long($sIp));
  128. $oDb = BxDolDb::getInstance();
  129. $sQuery = $oDb->prepare("SELECT `ID` FROM `bx_antispam_ip_table` WHERE `Type` = ? AND `LastDT` > ? AND `From` <= ? AND `To` >= ? LIMIT 1", $sType, time(), $iIp, $iIp);
  130. return $oDb->getOne($sQuery) ? true : false;
  131. }
  132. }
  133. /** @} */