1
0

BxAntispamModule.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Antispam Antispam
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxAntispamModule extends BxDolModule
  12. {
  13. public function __construct(&$aModule)
  14. {
  15. parent::__construct($aModule);
  16. }
  17. public function serviceIpTable ()
  18. {
  19. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  20. $s = _t('_bx_antispam_ip_table_status', mb_strtolower($o->getIpTableConfigTitle((int)getParam('bx_antispam_ip_list_type'))));
  21. $s .= $this->_grid('bx_antispam_grid_ip_table');
  22. return $s;
  23. }
  24. public function serviceDnsblList ()
  25. {
  26. $s = _t('_bx_antispam_dnsbl_status',
  27. BxTemplFunctions::getInstance()->statusOnOff((bool)$this->_oConfig->getAntispamOption('dnsbl_enable'), true),
  28. BxTemplFunctions::getInstance()->statusOnOff((bool)$this->_oConfig->getAntispamOption('uridnsbl_enable'), true),
  29. mb_strtolower(_t('_bx_antispam_dnsbl_behaviour_login_' . $this->_oConfig->getAntispamOption('dnsbl_behaviour_login'))),
  30. mb_strtolower(_t('_bx_antispam_dnsbl_behaviour_join_' . $this->_oConfig->getAntispamOption('dnsbl_behaviour_join')))
  31. );
  32. $s .= $this->_grid('bx_antispam_grid_dnsbl');
  33. return $s;
  34. }
  35. public function serviceBlockLog ()
  36. {
  37. return $this->_grid('bx_antispam_grid_block_log');
  38. }
  39. /**
  40. * Check text for spam.
  41. * First it check if IP is whitelisted(or under cron execution or user is admin) - for whitelisted IPs check for spam isn't performed,
  42. * then it checks URLs found in text for DNSURI black lists (@see BxAntispamDNSURIBlacklists),
  43. * then it checks text in Akismet service (@see BxAntispamAkismet).
  44. * It can send report if spam is found or tries to inform caller to block the content (depending on configuration).
  45. *
  46. * @param $sContent content to check for spam
  47. * @param $sIp IP address of content poster
  48. * @param $isStripSlashes slashes parameter:
  49. * BX_SLASHES_AUTO - automatically detect magic_quotes_gpc setting
  50. * BX_SLASHES_NO_ACTION - do not perform any action with slashes
  51. * @return true if spam detected and content shouln't be recorded, false if content should be processed as usual.
  52. */
  53. public function serviceIsSpam ($sContent, $sIp = '', $isStripSlashes = BX_SLASHES_AUTO)
  54. {
  55. if (defined('BX_DOL_CRON_EXECUTE') || isAdmin())
  56. return false;
  57. if ($this->serviceIsIpWhitelisted($sIp))
  58. return false;
  59. if (get_magic_quotes_gpc() && $isStripSlashes == BX_SLASHES_AUTO)
  60. $sContent = stripslashes($sContent);
  61. $bRet = false;
  62. if ('on' == $this->_oConfig->getAntispamOption('uridnsbl_enable')) {
  63. $oDNSURIBlacklists = bx_instance('BxAntispamDNSURIBlacklists', array(), $this->_aModule);
  64. if ($oDNSURIBlacklists->isSpam($sContent)) {
  65. $oDNSURIBlacklists->onPositiveDetection($sContent);
  66. $bRet = true;
  67. }
  68. }
  69. if (!$bRet && 'on' == $this->_oConfig->getAntispamOption('akismet_enable')) {
  70. $oAkismet = bx_instance('BxAntispamAkismet', array(), $this->_aModule);
  71. if ($oAkismet->isSpam($sContent)) {
  72. $oAkismet->onPositiveDetection($sContent);
  73. $bRet = true;
  74. }
  75. }
  76. if ($bRet && 'on' == $this->_oConfig->getAntispamOption('antispam_report')) {
  77. $oProfile = BxDolProfile::getInstance();
  78. $aPlus = array(
  79. 'SpammerUrl' => $oProfile->getUrl(),
  80. 'SpammerNickName' => $oProfile->getDisplayName(),
  81. 'Page' => htmlspecialchars_adv($_SERVER['PHP_SELF']),
  82. 'Get' => print_r($_GET, true),
  83. 'Post' => print_r($_POST, true),
  84. 'SpamContent' => htmlspecialchars_adv($sContent),
  85. );
  86. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate('bx_antispam_spam_report', $aPlus);
  87. if (!$aTemplate)
  88. trigger_error('Email template or translation missing: bx_antispam_spam_report', E_USER_ERROR);
  89. sendMail(getParam('site_email'), $aTemplate['Subject'], $aTemplate['Body']);
  90. }
  91. if ($bRet && 'on' == $this->_oConfig->getAntispamOption('antispam_block'))
  92. return true;
  93. return false;
  94. }
  95. /**
  96. * Perform complex check if user is allowed to login.
  97. * First it checks if IP is directly blocked (@see serviceIsIpBlocked),
  98. * then it checks in DNS black lists (@see serviceIsIpDnsBlacklisted).
  99. *
  100. * @param $sIp IP to check, or empty for current IP
  101. * @return empty string - if join should be allowed, error message - if join should be blocked
  102. */
  103. public function serviceCheckLogin ($sIp = '')
  104. {
  105. $bLoginBlock = ('block' == $this->_oConfig->getAntispamOption('dnsbl_behaviour_login'));
  106. $sErrorMsg = '';
  107. $sNote = $bLoginBlock ? 'login block' : 'login log';
  108. if (!$sIp)
  109. $sIp = getVisitorIP();
  110. if (!$sErrorMsg && $this->serviceIsIpBlocked($sIp))
  111. $sErrorMsg = $this->getErrorMessageIpBlocked();
  112. if (!$sErrorMsg && 'on' == $this->_oConfig->getAntispamOption('dnsbl_enable') && $this->serviceIsIpDnsBlacklisted($sIp, $sNote) && $bLoginBlock)
  113. $sErrorMsg = $this->getErrorMessageSpam();
  114. return $sErrorMsg;
  115. }
  116. /**
  117. * Perform complex check if user is allowed to join.
  118. * First it checks if IP is directly blocked (@see serviceIsIpBlocked),
  119. * then it checks in DNS black lists (@see serviceIsIpDnsBlacklisted),
  120. * then it checks in StopForumSpam service (@see BxAntispamStopForumSpam).
  121. *
  122. * @param $sCurIP IP to check, or empty for current IP
  123. * @return empty string - if join should be allowed, error message - if join should be blocked
  124. */
  125. public function serviceCheckJoin ($sEmail, &$bApproval, $sIp = '')
  126. {
  127. $bJoinBlock = ('block' == $this->_oConfig->getAntispamOption('dnsbl_behaviour_join'));
  128. $bApproval = false;
  129. $sErrorMsg = '';
  130. $sNote = $bJoinBlock ? 'join block' : 'join approval';
  131. if (!$sIp)
  132. $sIp = getVisitorIP();
  133. if (!$sErrorMsg && $this->serviceIsIpBlocked($sIp))
  134. $sErrorMsg = $this->getErrorMessageIpBlocked();
  135. if (!$sErrorMsg && 'on' == $this->_oConfig->getAntispamOption('dnsbl_enable') && $this->serviceIsIpDnsBlacklisted($sIp, $sNote)) {
  136. if ('approval' == $this->_oConfig->getAntispamOption('dnsbl_behaviour_join'))
  137. $bApproval = true;
  138. else
  139. $sErrorMsg = $this->getErrorMessageSpam();
  140. }
  141. if (!$sErrorMsg) {
  142. $oStopForumSpam = bx_instance('BxAntispamStopForumSpam', array(), $this->_aModule);
  143. if ($oStopForumSpam->isSpammer(array('email' => $sEmail, 'ip' => $sIp), $sNote))
  144. $sErrorMsg = $this->getErrorMessageSpam();
  145. }
  146. return $sErrorMsg;
  147. }
  148. /**
  149. * Check if IP is blacklisted in some DNS chain (@see BxAntispamDNSBlacklists).
  150. *
  151. * @param $sCurIP IP to check, or empty for current IP
  152. * @param $sNote [optional] place where checking is performed, for example 'join', 'login'
  153. * @return true if IP blacklisted and not whiteloisted, or false if under cron execution or if IP isn't blacklisted
  154. */
  155. public function serviceIsIpDnsBlacklisted($sCurIP = '', $sNote = '')
  156. {
  157. if (defined('BX_DOL_CRON_EXECUTE'))
  158. return false;
  159. if (!$sCurIP)
  160. $sCurIP = getVisitorIP();
  161. if ($this->serviceIsIpWhitelisted($sCurIP))
  162. return false;
  163. $o = bx_instance('BxAntispamDNSBlacklists', array(), $this->_aModule);
  164. 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)) {
  165. $o->onPositiveDetection ($sCurIP, $sNote);
  166. return true;
  167. }
  168. return false;
  169. }
  170. /**
  171. * @see BxAntispamIP::isIpWhitelisted
  172. */
  173. public function serviceIsIpWhitelisted($sIp = '')
  174. {
  175. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  176. return $o->isIpWhitelisted($sIp);
  177. }
  178. /**
  179. * @see BxAntispamIP::isIpBlocked
  180. */
  181. public function serviceIsIpBlocked($sIp = '')
  182. {
  183. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  184. return $o->isIpBlocked($sIp);
  185. }
  186. /**
  187. * @see BxAntispamIP::blockIp
  188. */
  189. public function serviceBlockIp($mixedIP, $iExpirationInSec = 86400, $sComment = '')
  190. {
  191. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  192. return $o->blockIp($mixedIP, $iExpirationInSec, $sComment);
  193. }
  194. /**
  195. * @see BxAntispamIP::pruning
  196. */
  197. public function servicePruning()
  198. {
  199. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  200. return $o->pruning();
  201. }
  202. /**
  203. * @see BxAntispamIP::pruning
  204. */
  205. public function serviceConfigValues($s)
  206. {
  207. switch ($s) {
  208. case 'ip_table':
  209. $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
  210. return $o->getIpTableConfigValues();
  211. case 'dnsbl_login':
  212. $o = bx_instance('BxAntispamDNSBlacklists', array(), $this->_aModule);
  213. return $o->getDNSBLConfigValues();
  214. case 'dnsbl_join';
  215. $o = bx_instance('BxAntispamDNSURIBlacklists', array(), $this->_aModule);
  216. return $o->getURIDNSBLConfigValues();
  217. }
  218. }
  219. protected function getErrorMessageIpBlocked ()
  220. {
  221. bx_import('BxDolLanguages');
  222. return _t('_bx_antispam_ip_blocked', $this->getErrorMessageSubmitFalsePositiveReport());
  223. }
  224. protected function getErrorMessageSpam ()
  225. {
  226. bx_import('BxDolLanguages');
  227. return _t('_bx_antispam_spam_detected', $this->getErrorMessageSubmitFalsePositiveReport());
  228. }
  229. protected function getErrorMessageSubmitFalsePositiveReport ()
  230. {
  231. if (BxDolRequest::serviceExists('bx_contact', 'get_contact_page_url') && ($sUrl = BxDolService::call('bx_contact', 'get_contact_page_url')))
  232. return _t('_bx_antispam_submit_false_positive_report', $sUrl);
  233. return '';
  234. }
  235. protected function _grid ($sObjectGrid)
  236. {
  237. $oGrid = BxDolGrid::getObjectInstance($sObjectGrid);
  238. return $oGrid ? $oGrid->getCode() : '';
  239. }
  240. }
  241. /** @} */