BxAntispamModule.php 9.2 KB

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