BxAntispamModule.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // TODO: add checking on join form
  12. bx_import('BxDolModule');
  13. class BxAntispamModule extends BxDolModule
  14. {
  15. function __construct(&$aModule)
  16. {
  17. parent::__construct($aModule);
  18. }
  19. /**
  20. * Check text for spam.
  21. * First it check if IP is whitelisted(or under cron execution or user is admin) - for whitelisted IPs check for spam isn't performed,
  22. * then it checks URLs found in text for DNSURI black lists (@see BxAntispamDNSURIBlacklists),
  23. * then it checks text in Akismet service (@see BxAntispamAkismet).
  24. * It can send report if spam is found or tries to inform caller to block the content (depending on configuration).
  25. *
  26. * @param $sContent content to check for spam
  27. * @param $sIp IP address of content poster
  28. * @param $isStripSlashes slashes parameter:
  29. * BX_SLASHES_AUTO - automatically detect magic_quotes_gpc setting
  30. * BX_SLASHES_NO_ACTION - do not perform any action with slashes
  31. * @return true if spam detected and content shouln't be recorded, false if content should be processed as usual.
  32. */
  33. function serviceIsSpam ($sContent, $sIp = '', $isStripSlashes = BX_SLASHES_AUTO)
  34. {
  35. if (defined('BX_DOL_CRON_EXECUTE') || isAdmin())
  36. return false;
  37. if ($this->serviceIsIpWhitelisted($sIp))
  38. return false;
  39. if (get_magic_quotes_gpc() && $isStripSlashes == BX_SLASHES_AUTO)
  40. $sContent = stripslashes($sContent);
  41. $bRet = false;
  42. if ('on' == $this->_oConfig->getAntispamOption('uridnsbl_enable')) {
  43. $oDNSURIBlacklists = bx_instance('BxAntispamDNSURIBlacklists', array(), $this->_aModule);
  44. if ($oDNSURIBlacklists->isSpam($sContent)) {
  45. $oDNSURIBlacklists->onPositiveDetection($sContent);
  46. $bRet = true;
  47. }
  48. }
  49. if (!$bRet && 'on' == $this->_oConfig->getAntispamOption('akismet_enable')) {
  50. $oAkismet = bx_instance('BxAntispamAkismet', array(), $this->_aModule);
  51. if ($oAkismet->isSpam($sContent)) {
  52. $oAkismet->onPositiveDetection($sContent);
  53. $bRet = true;
  54. }
  55. }
  56. bx_alert('bx_antispam', 'is_spam', bx_get_logged_profile_id(), 0, array('content' => $sContent, 'ip' => $sIp, 'result' => &$ret));
  57. if ($bRet && 'on' == $this->_oConfig->getAntispamOption('antispam_report')) {
  58. $iProfileId = getLoggedId();
  59. $aPlus = array(
  60. 'SpammerUrl' => getProfileLink($iProfileId),
  61. 'SpammerNickName' => getNickName($iProfileId),
  62. 'Page' => htmlspecialchars_adv($_SERVER['PHP_SELF']),
  63. 'Get' => print_r($_GET, true),
  64. 'SpamContent' => htmlspecialchars_adv($sContent),
  65. );
  66. bx_import('BxDolEmailTemplates');
  67. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('t_SpamReportAuto', $aPlus);
  68. if (!$aTemplate)
  69. trigger_error('Email template or translation missing: t_SpamReportAuto', E_USER_ERROR);
  70. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  71. }
  72. if ($bRet && 'on' == $this->_oConfig->getAntispamOption('antispam_block'))
  73. return true;
  74. return false;
  75. }
  76. /**
  77. * Perform complex check if user is allowed to login.
  78. * First it checks if IP is directly blocked (@see serviceIsIpBlocked),
  79. * then it checks in DNS black lists (@see serviceIsIpDnsBlacklisted).
  80. *
  81. * @param $sCurIP IP to check, or empty for current IP
  82. * @param $sType [optional] place where checking is performed, for example 'join', 'login'
  83. * @return empty string - if join should be allowed, error message - if join should be blocked
  84. */
  85. function serviceCheckLogin ($sIp = '')
  86. {
  87. $sErrorMsg = '';
  88. $sType = 'login';
  89. if (!$sIp)
  90. $sIp = getVisitorIP();
  91. if (!$sErrorMsg && $this->serviceIsIpBlocked($sIp))
  92. $sErrorMsg = $this->getErrorMessageIpBlocked();
  93. if (!$sErrorMsg && 'on' == $this->_oConfig->getAntispamOption('dnsbl_enable') && 'block' == $this->_oConfig->getAntispamOption('dnsbl_behaviour') && $this->serviceIsIpDnsBlacklisted($sIp, $sType))
  94. $sErrorMsg = $this->getErrorMessageSpam();
  95. bx_alert('bx_antispam', 'check_login', bx_get_logged_profile_id(), 0, array('ip' => $sIp, 'error_msg' => &$sErrorMsg));
  96. return $sErrorMsg;
  97. }
  98. /**
  99. * Perform complex check if user is allowed to join.
  100. * First it checks if IP is directly blocked (@see serviceIsIpBlocked),
  101. * then it checks in DNS black lists (@see serviceIsIpDnsBlacklisted),
  102. * then it checks in StopForumSpam service (@see BxAntispamStopForumSpam).
  103. *
  104. * TODO: if dnsbl_behaviour == approval, inform caller to set account to approval status on positive detection
  105. *
  106. * @param $sCurIP IP to check, or empty for current IP
  107. * @param $sType [optional] place where checking is performed, for example 'join', 'login'
  108. * @return empty string - if join should be allowed, error message - if join should be blocked
  109. */
  110. function serviceCheckJoin ($sEmail, $sIp = '')
  111. {
  112. $sErrorMsg = '';
  113. $sType = 'join';
  114. if (!$sIp)
  115. $sIp = getVisitorIP();
  116. if (!$sErrorMsg && $this->serviceIsIpBlocked($sIp))
  117. $sErrorMsg = $this->getErrorMessageIpBlocked();
  118. if (!$sErrorMsg && 'on' == $this->_oConfig->getAntispamOption('dnsbl_enable') && 'block' == $this->_oConfig->getAntispamOption('dnsbl_behaviour') && $this->serviceIsIpDnsBlacklisted($sIp, $sType))
  119. $sErrorMsg = $this->getErrorMessageSpam();
  120. if (!$sErrorMsg) {
  121. $oStopForumSpam = bx_instance('BxAntispamStopForumSpam', array(), $this->_aModule);
  122. if ($oStopForumSpam->isSpammer(array('email' => $sEmail, 'ip' => $sIp), $sType))
  123. $sErrorMsg = $this->getErrorMessageSpam();
  124. }
  125. bx_alert('bx_antispam', 'check_join', bx_get_logged_profile_id(), 0, array('email' => $sEmail, 'ip' => $sIp, 'error_msg' => &$sErrorMsg));
  126. return $sErrorMsg;
  127. }
  128. /**
  129. * Check if IP is blacklisted in some DNS chain (@see BxAntispamDNSBlacklists).
  130. *
  131. * @param $sCurIP IP to check, or empty for current IP
  132. * @param $sType [optional] place where checking is performed, for example 'join', 'login'
  133. * @return true if IP blacklisted and not whiteloisted, or false if under cron execution or if IP isn't blacklisted
  134. */
  135. function serviceIsIpDnsBlacklisted($sCurIP = '', $sType = '')
  136. {
  137. if (defined('BX_DOL_CRON_EXECUTE'))
  138. return false;
  139. if (!$sCurIP)
  140. $sCurIP = getVisitorIP();
  141. if ($this->serviceIsIpWhitelisted($sCurIP))
  142. return false;
  143. $o = bx_instance('BxAntispamDNSBlacklists', array(), $this->_aModule);
  144. if (BX_DOL_DNSBL_POSITIVE == $o->dnsbl_lookup_ip(BX_DOL_DNSBL_CHAIN_SPAMMERS, $sCurIP) && BX_DOL_DNSBL_POSITIVE != $o->dnsbl_lookup_ip(BX_DOL_DNSBL_CHAIN_WHITELIST, $sCurIP)) {
  145. $o->onPositiveDetection ($sCurIP, $sType);
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * BxAntispamIP::isIpWhitelisted
  152. */
  153. function serviceIsIpWhitelisted($sIp = '')
  154. {
  155. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  156. return $o->isIpWhitelisted($sIp);
  157. }
  158. /**
  159. * @see BxAntispamIP::isIpBlocked
  160. */
  161. function serviceIsIpBlocked($sIp = '')
  162. {
  163. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  164. return $o->isIpBlocked($sIp);
  165. }
  166. /**
  167. * @see BxAntispamIP::blockIp
  168. */
  169. function serviceBlockIp($mixedIP, $iExpirationInSec = 86400, $sComment = '')
  170. {
  171. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  172. return $o->blockIp($mixedIP, $iExpirationInSec, $sComment);
  173. }
  174. protected function getErrorMessageIpBlocked ()
  175. {
  176. bx_import('BxDolLanguages');
  177. return _t('_bx_antispam_ip_blocked', $this->getErrorMessageSubmitFalsePositiveReport());
  178. }
  179. protected function getErrorMessageSpam ()
  180. {
  181. bx_import('BxDolLanguages');
  182. return _t('_bx_antispam_spam_detected', $this->getErrorMessageSubmitFalsePositiveReport());
  183. }
  184. protected function getErrorMessageSubmitFalsePositiveReport ()
  185. {
  186. if (BxDolRequest::serviceExists('bx_contact', 'get_contact_page_url') && ($sUrl = BxDolService::call('bx_contact', 'get_contact_page_url')))
  187. return _t('_bx_antispam_submit_false_positive_report', $sUrl);
  188. return '';
  189. }
  190. }
  191. /** @} */