BxAntispamGridDNSBL.php 11 KB

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