BxAntispamStopForumSpam.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  12. * Spam detection based on email and ip powered by StopForumSpam service - http://www.stopforumspam.com/
  13. */
  14. class BxAntispamStopForumSpam extends BxDol
  15. {
  16. protected $_aKeys = array (
  17. 'ip' => 1,
  18. 'email' => 1,
  19. 'username' => 1,
  20. );
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. /**
  26. * Check if user is spammer
  27. * @param $aValues - array with keys: ip, email, username
  28. * @param $sDesc - desctiption, for example: join
  29. * @return true - on positive detection, false - on error or no spammer detection
  30. */
  31. public function isSpammer ($aValues, $sDesc)
  32. {
  33. if (!getParam('bx_antispam_stopforumspam_enable'))
  34. return false;
  35. if (!$aValues || !is_array($aValues))
  36. return false;
  37. $aRequestParams = array ('f' => 'json');
  38. foreach ($this->_aKeys as $k => $b)
  39. if (isset($aValues[$k]))
  40. $aRequestParams[$k] = rawurlencode($aValues[$k]);
  41. $s = bx_file_get_contents('http://www.stopforumspam.com/api', $aRequestParams);
  42. if (!$s)
  43. return false;
  44. $aResult = json_decode($s, true);
  45. if (null === $aResult || !$aResult['success'])
  46. return false;
  47. foreach ($this->_aKeys as $k => $b) {
  48. if (isset($aResult[$k]) && isset($aResult[$k]['appears']) && $aResult[$k]['appears']) {
  49. $this->onPositiveDetection($sDesc);
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * Submit spammer
  57. * @param @aValues - array with keys: ip, email, username
  58. * @return false - on error, or true - on success
  59. */
  60. public function submitSpammer ($aValues, $sEvidences = false)
  61. {
  62. if (!getParam('bx_antispam_stopforumspam_enable'))
  63. return false;
  64. $sKey = getParam('bx_antispam_stopforumspam_api_key');
  65. if (!$sKey)
  66. return false;
  67. $sData = 'api_key=' . $sKey . '&evidence=' . ($sEvidences ? rawurlencode($sEvidences) : 'spammer');
  68. foreach ($this->_aKeys as $k => $b)
  69. if (isset($aValues[$k]))
  70. $sData .= '&' . ('ip' == $k ? 'ip_addr' : $k) . '=' . rawurlencode($aValues[$k]);
  71. $fp = fsockopen("www.stopforumspam.com", 80);
  72. fputs($fp, "POST /add.php HTTP/1.1\n" );
  73. fputs($fp, "Host: www.stopforumspam.com\n" );
  74. fputs($fp, "Content-type: application/x-www-form-urlencoded\n" );
  75. fputs($fp, "Content-length: " . strlen($sData) . "\n" );
  76. fputs($fp, "Connection: close\n\n" );
  77. fputs($fp, $sData);
  78. fclose($fp);
  79. return true;
  80. }
  81. public function onPositiveDetection ($sExtraData = '')
  82. {
  83. $o = bx_instance('DNSBlacklists', array(), 'bx_antispam');
  84. $o->onPositiveDetection (getVisitorIP(false), $sExtraData, 'stopforumspam');
  85. }
  86. }
  87. /** @} */