BxAntispamGridDNSBL.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. bx_import('BxTemplGrid');
  12. class BxAntispamGridDNSBL extends BxTemplGrid
  13. {
  14. public function __construct ($aOptions, $oTemplate = false)
  15. {
  16. parent::__construct ($aOptions, $oTemplate);
  17. }
  18. /**
  19. * add js file for AJAX form submission
  20. */
  21. protected function _addJsCss()
  22. {
  23. parent::_addJsCss();
  24. $this->_oTemplate->addJs('jquery.form.min.js');
  25. bx_import('BxTemplFormView');
  26. $oForm = new BxTemplFormView(array());
  27. $oForm->addCssJs();
  28. }
  29. /**
  30. * 'help' action handler
  31. */
  32. public function performActionHelp()
  33. {
  34. bx_import('BxTemplFunctions');
  35. $s = BxTemplFunctions::getInstance()->popupBox(
  36. 'bx_antispam_poppup_help',
  37. _t('_bx_antispam_popup_help'),
  38. _t('_bx_antispam_help_dnsbl')
  39. );
  40. $this->_echoResultJson(array('popup' => array('html' => $s)), true);
  41. }
  42. /**
  43. * 'log' action handler
  44. */
  45. public function performActionLog()
  46. {
  47. bx_import('BxDolModule');
  48. $oModule = BxDolModule::getInstance('bx_antispam');
  49. $sGrid = $oModule->serviceBlockLog();
  50. if (!$sGrid) {
  51. $this->_echoResultJson(array('msg' => _t('_sys_txt_error_occured')), true);
  52. exit;
  53. }
  54. bx_import('BxTemplFunctions');
  55. $s = BxTemplFunctions::getInstance()->popupBox(
  56. 'bx_antispam_popup_block_log',
  57. _t('_bx_antispam_popup_block_log'),
  58. $sGrid . '<button class="bx-btn" style="float:none;" onclick ="$(\'.bx-popup-applied:visible\').dolPopupHide();">' . _t('_sys_close') . '</button>'
  59. );
  60. $this->_echoResultJson(array('popup' => array('html' => $s)), true);
  61. }
  62. /**
  63. * 'recheck' action handler
  64. */
  65. public function performActionRecheckItem()
  66. {
  67. bx_import('BxDolModule');
  68. $oModule = BxDolModule::getInstance('bx_antispam');
  69. $o = bx_instance('BxAntispamDNSBlacklists', array(), $oModule->_aModule);
  70. $aChain = $o->getRule((int)bx_get('id'));
  71. $iRet = BX_DOL_DNSBL_FAILURE;
  72. if ($aChain) {
  73. if (bx_get('chain') == 'dnsbl') {
  74. $iRet = $o->dnsbl_lookup_ip($aChain, bx_process_input(bx_get('test')));
  75. } elseif (bx_get('chain') == 'uridnsbl') {
  76. $sUrl = preg_replace('/^\w+:\/\//', '', bx_process_input(bx_get('test')));
  77. $sUrl = preg_replace('/^www\./', '', $sUrl);
  78. $oBxDolDNSURIBlacklists = bx_instance('BxAntispamDNSURIBlacklists', array(), $oModule->_aModule);
  79. $aUrls = $oBxDolDNSURIBlacklists->validateUrls(array($sUrl));
  80. if ($aUrls)
  81. $iRet = $o->dnsbl_lookup_uri($aUrls[0], $aChain);
  82. }
  83. }
  84. $s = '';
  85. switch ($iRet) {
  86. case BX_DOL_DNSBL_POSITIVE:
  87. $s = 'LISTED';
  88. break;
  89. case BX_DOL_DNSBL_NEGATIVE:
  90. $s = 'NOT LISTED';
  91. break;
  92. default:
  93. case BX_DOL_DNSBL_FAILURE:
  94. $s = 'FAIL';
  95. }
  96. echo $s;
  97. exit;
  98. }
  99. /**
  100. * 'recheck' action handler
  101. */
  102. public function performActionRecheck()
  103. {
  104. bx_import('BxDolModule');
  105. $oModule = BxDolModule::getInstance('bx_antispam');
  106. $oDNSBlacklists = bx_instance('BxAntispamDNSBlacklists', array(), $oModule->_aModule);
  107. $aForm = array(
  108. 'form_attrs' => array(
  109. 'id' => 'bx_antispam_form_dnsbl_recheck',
  110. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=bx_antispam_grid_dnsbl&a=recheck',
  111. 'onsubmit' => "return bx_antispam_recheck($('#bx_antispam_ip_url').val(), $('[name=dnsbl_uridnsbl]:checked').val());",
  112. 'method' => 'post',
  113. ),
  114. 'params' => array (
  115. 'db' => array(
  116. 'submit_name' => 'do_submit',
  117. ),
  118. ),
  119. 'inputs' => array(
  120. 'ip_url' => array(
  121. 'type' => 'text',
  122. 'name' => 'ip_url',
  123. 'value' => getVisitorIP(),
  124. 'caption' => _t('_bx_antispam_field_ip_url'),
  125. 'attrs' => array('id' => 'bx_antispam_ip_url'),
  126. ),
  127. 'dnsbl_uridnsbl' => array(
  128. 'type' => 'radio_set',
  129. 'name' => 'dnsbl_uridnsbl',
  130. 'caption' => _t('_bx_antispam_field_dnsbl_uridnsbl'),
  131. 'values' => array ('dnsbl' => _t('_bx_antispam_dnsbl'), 'uridnsbl' => _t('_bx_antispam_uri_dnsbl')),
  132. 'value' => 'dnsbl',
  133. ),
  134. 'submit' => array(
  135. 'type' => 'input_set',
  136. 0 => array (
  137. 'type' => 'submit',
  138. 'name' => 'do_submit',
  139. 'value' => _t('_sys_submit'),
  140. ),
  141. 1 => array (
  142. 'type' => 'reset',
  143. 'name' => 'close',
  144. 'value' => _t('_sys_close'),
  145. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  146. ),
  147. ),
  148. ),
  149. );
  150. bx_import('BxTemplFormView');
  151. $oForm = new BxTemplFormView($aForm);
  152. $s = $oModule->_oTemplate->parseHtmlByName('recheck.html', array (
  153. 'form' => $oForm->getCode(),
  154. 'url_recheck_item' => BX_DOL_URL_ROOT . 'grid.php?o=bx_antispam_grid_dnsbl&a=recheck_item',
  155. 'bx_repeat:items' => $oDNSBlacklists->getRules(array(BX_DOL_DNSBL_CHAIN_SPAMMERS, BX_DOL_DNSBL_CHAIN_WHITELIST, BX_DOL_DNSBL_CHAIN_URIDNS)),
  156. ));
  157. bx_import('BxTemplFunctions');
  158. $s = BxTemplFunctions::getInstance()->popupBox($oForm->getId() . '_form', _t('_bx_antispam_popup_dnsbl_recheck'), $s);
  159. $this->_echoResultJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))), true);
  160. }
  161. /**
  162. * 'recheck' action handler
  163. */
  164. public function performActionAdd()
  165. {
  166. bx_import('BxTemplFormView');
  167. $aForm = array(
  168. 'form_attrs' => array(
  169. 'id' => 'bx_antispam_form_dnsbl_add',
  170. 'action' => BX_DOL_URL_ROOT . 'grid.php?o=bx_antispam_grid_dnsbl&a=add',
  171. 'method' => 'post',
  172. ),
  173. 'params' => array (
  174. 'db' => array(
  175. 'table' => 'bx_antispam_dnsbl_rules',
  176. 'key' => 'id',
  177. 'submit_name' => 'do_submit',
  178. ),
  179. ),
  180. 'inputs' => array(
  181. 'country' => array(
  182. 'type' => 'select',
  183. 'name' => 'country',
  184. 'caption' => _t('_bx_antispam_field_country'),
  185. 'values' => BxDolForm::getDataItems('Country'),
  186. ),
  187. 'chain' => array(
  188. 'type' => 'radio_set',
  189. 'name' => 'chain',
  190. 'caption' => _t('_bx_antispam_field_action'),
  191. 'values' => array ('spammers' => _t('_bx_antispam_chain_spammers'), 'whitelist' => _t('_bx_antispam_chain_whitelist')),
  192. 'value' => 'spammers',
  193. ),
  194. 'submit' => array(
  195. 'type' => 'input_set',
  196. 0 => array (
  197. 'type' => 'submit',
  198. 'name' => 'do_submit',
  199. 'value' => _t('_sys_submit'),
  200. ),
  201. 1 => array (
  202. 'type' => 'reset',
  203. 'name' => 'close',
  204. 'value' => _t('_sys_close'),
  205. 'attrs' => array('class' => 'bx-def-margin-sec-left', 'onclick' => '$(\'.bx-popup-applied:visible\').dolPopupHide();'),
  206. ),
  207. ),
  208. ),
  209. );
  210. $oForm = new BxTemplFormView($aForm);
  211. $oForm->initChecker();
  212. if ($oForm->isSubmittedAndValid()) { // if form is submitted and all fields are valid
  213. $aCustomValues = array(
  214. 'chain' => $oForm->getCleanValue('chain'),
  215. 'zonedomain' => sprintf("%s.countries.nerd.dk.", strtolower($oForm->getCleanValue('country'))),
  216. 'postvresp' => '127.0.0.2',
  217. 'url' => 'http://countries.nerd.dk/',
  218. 'comment' => '_bx_antispam_rule_note_country',
  219. 'added' => time(),
  220. 'active' => 1,
  221. );
  222. $iRecentId = $oForm->insert ($aCustomValues, true); // insert new record
  223. if ($iRecentId)
  224. $aRes = array('grid' => $this->getCode(false), 'blink' => $iRecentId); // if record is successfully added, reload grid and highlight added row
  225. else
  226. $aRes = array('msg' => _t('_sys_txt_error_occured')); // if record adding failed, display error message
  227. $this->_echoResultJson($aRes, true);
  228. } else { // if form is not submitted or some fields are invalid, display popup with form
  229. bx_import('BxTemplFunctions');
  230. $s = BxTemplFunctions::getInstance()->popupBox($oForm->getId() . '_form', _t('_bx_antispam_popup_dnsbl_add'), $oForm->getCode() . '
  231. <script>
  232. $(document).ready(function () {
  233. $("#' . $oForm->getId() . '").ajaxForm({
  234. dataType: "json",
  235. beforeSubmit: function (formData, jqForm, options) {
  236. bx_loading($("#' . $oForm->getId() . '"), true);
  237. },
  238. success: function (data) {
  239. $(".bx-popup-applied:visible").dolPopupHide();
  240. glGrids.' . $this->_sObject . '.processJson(data, "add");
  241. }
  242. });
  243. });
  244. </script>');
  245. $this->_echoResultJson(array('popup' => array('html' => $s, 'options' => array('closeOnOuterClick' => false))), true);
  246. }
  247. }
  248. protected function _getCellChain ($mixedValue, $sKey, $aField, $aRow)
  249. {
  250. return parent::_getCellDefault (_t('_bx_antispam_chain_' . $mixedValue), $sKey, $aField, $aRow);
  251. }
  252. protected function _getCellZonedomain ($mixedValue, $sKey, $aField, $aRow)
  253. {
  254. $s = $mixedValue;
  255. if ($aRow['url'])
  256. $s = '<a target="_blank" href="' . $aRow['url'] . '">' . $mixedValue . '</a>';
  257. return parent::_getCellDefault ($s, $sKey, $aField, $aRow);
  258. }
  259. protected function _getCellComment ($mixedValue, $sKey, $aField, $aRow)
  260. {
  261. $sCountry = '';
  262. $aMatches = array();
  263. if (preg_match('/^(\w{2})\.countries\.nerd\.dk\.$/', $aRow['zonedomain'], $aMatches) && isset($aMatches[1])) {
  264. $sCountry = $aMatches[1];
  265. }
  266. return parent::_getCellDefault (_t($mixedValue, $sCountry), $sKey, $aField, $aRow);
  267. }
  268. protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow)
  269. {
  270. if (preg_match('/^(\w{2})\.countries\.nerd\.dk\.$/', $aRow['zonedomain'], $aMatches)) {
  271. $aRow['active'] = 1;
  272. return parent::_getCellActions ($mixedValue, $sKey, $aField, $aRow);
  273. }
  274. return parent::_getCellDefault ('', $sKey, $aField, $aRow);
  275. }
  276. }
  277. /** @} */