Cheetah
All Classes Namespaces Files Functions Variables Pages
ChWsbDNSBlacklists.php
Go to the documentation of this file.
1 <?php
2 
8 define('CH_WSB_DNSBL_NEGATIVE', 0); // negative
9 define('CH_WSB_DNSBL_POSITIVE', 1); // positive match
10 define('CH_WSB_DNSBL_FAILURE', 2); // generic failure, not enabled or configured
11 
12 // Types of queries for dnsbl_lookup_ip() and dnsbl_lookup_domain()
13 define('CH_WSB_DNSBL_ANYPOSTV_RETFIRST', 0); // Any positive from chain, stop and return first
14 define('CH_WSB_DNSBL_ANYPOSTV_RETEVERY', 1); // Any positive, check all and return every positive
15 define('CH_WSB_DNSBL_ALLPOSTV_RETEVERY', 2); // All must check positive, return every positive
16 
17 define('CH_WSB_DNSBL_MATCH_ANY', "any");
18 
19 define('CH_WSB_DNSBL_CHAIN_SPAMMERS', "spammers");
20 define('CH_WSB_DNSBL_CHAIN_WHITELIST', "whitelist");
21 define('CH_WSB_DNSBL_CHAIN_URIDNS', "uridns");
22 
40 {
41  private $aChains = array ();
42 
46  public function __construct()
47  {
48  $this->initChains();
49  }
50 
51  public function dnsbl_lookup_ip($mixedChain, $sIp, $querymode = CH_WSB_DNSBL_ANYPOSTV_RETFIRST)
52  {
53  $lookupkey = $this->ipreverse($sIp);
54  if (false === $lookupkey)
55  return CH_WSB_DNSBL_FAILURE; // unable to prepare lookup string from address
56 
57  if (is_array($mixedChain))
58  $aChain = $mixedChain;
59  else
60  $aChain = &$this->aChains[$mixedChain];
61  return $this->dnsbl_lookup($aChain, $lookupkey, $querymode);
62  }
63 
64  public function dnsbl_lookup_uri($sUri, $mixedChain = CH_WSB_DNSBL_CHAIN_URIDNS, $querymode = CH_WSB_DNSBL_ANYPOSTV_RETFIRST)
65  {
66  if (!$sUri)
67  return CH_WSB_DNSBL_FAILURE;
68 
69  if (is_array($mixedChain))
70  $aChain = $mixedChain;
71  else
72  $aChain = &$this->aChains[$mixedChain];
73  return $this->dnsbl_lookup($aChain, $sUri, $querymode);
74  }
75 
76  public function onPositiveDetection ($sIP, $sExtraData = '', $sType = 'dnsbl')
77  {
78  $iIP = sprintf("%u", ip2long($sIP));
80  $sExtraData = process_db_input($sExtraData);
81  return $GLOBALS['MySQL']->query("INSERT INTO `sys_antispam_block_log` SET `ip` = '$iIP', `member_id` = '$iMemberId', `type` = '$sType', `extra` = '$sExtraData', `added` = " . time());
82  }
83 
84  public function clearCache ()
85  {
86  $GLOBALS['MySQL']->cleanCache('sys_dnsbl_'.CH_WSB_DNSBL_CHAIN_SPAMMERS);
87  $GLOBALS['MySQL']->cleanCache('sys_dnsbl_'.CH_WSB_DNSBL_CHAIN_WHITELIST);
88  }
89 
90  /*************** private function ***************/
91 
92  private function dnsbl_lookup(&$zones, $key, $querymode)
93  {
94  $numpositive = 0;
95  $numservers = count ($zones);
96  $servers = $zones;
97 
98  if (!$servers)
99  return CH_WSB_DNSBL_FAILURE; // no servers defined
100 
101  if (($querymode!=CH_WSB_DNSBL_ANYPOSTV_RETFIRST) && ($querymode!=CH_WSB_DNSBL_ANYPOSTV_RETEVERY)
102  && ($querymode!=CH_WSB_DNSBL_ALLPOSTV_RETEVERY))
103  return CH_WSB_DNSBL_FAILURE; // invalid querymode
104 
105  foreach ($servers as $r) {
106  $resultaddr = gethostbyname ($key . "." . $r['zonedomain']);
107 
108  if ($resultaddr && $resultaddr != $key . "." . $r['zonedomain']) {
109  // we got some result from the DNS query, not NXDOMAIN. should we consider 'positive'?
110  $postvresp = $r['postvresp']; // check positive match criteria
111  if (
112  CH_WSB_DNSBL_MATCH_ANY == $postvresp ||
113  (preg_match("/^\d+\.\d+\.\d+\.\d+$/", $postvresp) && $resultaddr == $postvresp) ||
114  (is_numeric($postvresp) && (ip2long($resultaddr) & $postvresp))
115  ) {
116  $numpositive++;
117  if ($querymode == CH_WSB_DNSBL_ANYPOSTV_RETFIRST)
118  return CH_WSB_DNSBL_POSITIVE; // found one positive, returning single
119  }
120  }
121  }
122  // all servers were queried
123  if ($numpositive == $numservers)
124  return CH_WSB_DNSBL_POSITIVE;
125  else if (($querymode == CH_WSB_DNSBL_ANYPOSTV_RETEVERY) && ($numpositive > 0))
126  return CH_WSB_DNSBL_POSITIVE;
127  else
128  return CH_WSB_DNSBL_NEGATIVE;
129  }
130 
131  private function ipreverse ($sIp)
132  {
133  if (!preg_match ('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/', $sIp, $m))
134  return false;
135 
136  return "{$m[4]}.{$m[3]}.{$m[2]}.{$m[1]}";
137  }
138 
139  private function initChains()
140  {
141  if (!isset($GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_SPAMMERS]))
142  $GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_SPAMMERS] = $GLOBALS['MySQL']->fromCache('sys_dnsbl_'.CH_WSB_DNSBL_CHAIN_SPAMMERS, 'getAll', "SELECT `zonedomain`, `postvresp` FROM `sys_dnsbl_rules` WHERE `chain` = '".CH_WSB_DNSBL_CHAIN_SPAMMERS."' AND `active` = 1");
143 
144  if (!isset($GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_WHITELIST]))
145  $GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_WHITELIST] = $GLOBALS['MySQL']->fromCache('sys_dnsbl_'.CH_WSB_DNSBL_CHAIN_WHITELIST, 'getAll', "SELECT `zonedomain`, `postvresp` FROM `sys_dnsbl_rules` WHERE `chain` = '".CH_WSB_DNSBL_CHAIN_WHITELIST."' AND `active` = 1");
146 
147  if (!isset($GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_URIDNS]))
148  $GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_URIDNS] = $GLOBALS['MySQL']->fromCache('sys_dnsbl_'.CH_WSB_DNSBL_CHAIN_URIDNS, 'getAll', "SELECT `zonedomain`, `postvresp` FROM `sys_dnsbl_rules` WHERE `chain` = '".CH_WSB_DNSBL_CHAIN_URIDNS."' AND `active` = 1");
149 
150  $this->aChains[CH_WSB_DNSBL_CHAIN_SPAMMERS] = &$GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_SPAMMERS];
151  $this->aChains[CH_WSB_DNSBL_CHAIN_WHITELIST] = &$GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_WHITELIST];
152  $this->aChains[CH_WSB_DNSBL_CHAIN_URIDNS] = &$GLOBALS['ch_dol_dnsbl_'.CH_WSB_DNSBL_CHAIN_URIDNS];
153 
154  }
155 
156 }
CH_WSB_DNSBL_CHAIN_WHITELIST
const CH_WSB_DNSBL_CHAIN_WHITELIST
Definition: ChWsbDNSBlacklists.php:20
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
CH_WSB_DNSBL_POSITIVE
const CH_WSB_DNSBL_POSITIVE
Definition: ChWsbDNSBlacklists.php:9
ChWsbDNSBlacklists\__construct
__construct()
Definition: ChWsbDNSBlacklists.php:46
$iMemberId
$iMemberId
Definition: profile.php:91
ChWsbDNSBlacklists\clearCache
clearCache()
Definition: ChWsbDNSBlacklists.php:84
CH_WSB_DNSBL_ANYPOSTV_RETEVERY
const CH_WSB_DNSBL_ANYPOSTV_RETEVERY
Definition: ChWsbDNSBlacklists.php:14
php
ChWsbDNSBlacklists\onPositiveDetection
onPositiveDetection($sIP, $sExtraData='', $sType='dnsbl')
Definition: ChWsbDNSBlacklists.php:76
$sType
$sType
Definition: actions.inc.php:11
CH_WSB_DNSBL_CHAIN_SPAMMERS
const CH_WSB_DNSBL_CHAIN_SPAMMERS
Definition: ChWsbDNSBlacklists.php:19
getLoggedId
getLoggedId()
Definition: profiles.inc.php:32
CH_WSB_DNSBL_ANYPOSTV_RETFIRST
const CH_WSB_DNSBL_ANYPOSTV_RETFIRST
Definition: ChWsbDNSBlacklists.php:13
CH_WSB_DNSBL_ALLPOSTV_RETEVERY
const CH_WSB_DNSBL_ALLPOSTV_RETEVERY
Definition: ChWsbDNSBlacklists.php:15
CH_WSB_DNSBL_NEGATIVE
const CH_WSB_DNSBL_NEGATIVE
Definition: ChWsbDNSBlacklists.php:8
ChWsbDNSBlacklists
Definition: ChWsbDNSBlacklists.php:40
ChWsbDNSBlacklists\dnsbl_lookup_ip
dnsbl_lookup_ip($mixedChain, $sIp, $querymode=CH_WSB_DNSBL_ANYPOSTV_RETFIRST)
Definition: ChWsbDNSBlacklists.php:51
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
CH_WSB_DNSBL_MATCH_ANY
const CH_WSB_DNSBL_MATCH_ANY
Definition: ChWsbDNSBlacklists.php:17
CH_WSB_DNSBL_CHAIN_URIDNS
const CH_WSB_DNSBL_CHAIN_URIDNS
Definition: ChWsbDNSBlacklists.php:21
ChWsbDNSBlacklists\dnsbl_lookup_uri
dnsbl_lookup_uri($sUri, $mixedChain=CH_WSB_DNSBL_CHAIN_URIDNS, $querymode=CH_WSB_DNSBL_ANYPOSTV_RETFIRST)
Definition: ChWsbDNSBlacklists.php:64
CH_WSB_DNSBL_FAILURE
const CH_WSB_DNSBL_FAILURE
Definition: ChWsbDNSBlacklists.php:10
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
$GLOBALS
$GLOBALS['iAdminPage']
Definition: advanced_settings.php:10