PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/xoops_trust_path/modules/d3blog/include/blacklist.class.php

http://xoopscube-modules.googlecode.com/
PHP | 185 lines | 150 code | 27 blank | 8 comment | 32 complexity | e5187e3f922472ccc967ba89aeb3498e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @version $Id: blacklist.class.php 398 2008-03-26 02:20:50Z hodaka $
  4. */
  5. class Blacklist
  6. {
  7. var $_blacklists = array();
  8. var $_ips;
  9. var $_domains;
  10. var $_doubleCcTldFile = 'http://spamcheck.freeapp.net/two-level-tlds';
  11. function setBlacklists($blacklists)
  12. {
  13. if (is_array($blacklists)) {
  14. $this->_blacklists = $blacklists;
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }
  20. function isListed($host)
  21. {
  22. foreach ($this->_blacklists as $blacklist) {
  23. $built_host = $host. '.'. $blacklist;
  24. $ret = gethostbyname($built_host);
  25. if($ret != $built_host) {
  26. // if( strstr($ret, '127.0.0.') !== false ) {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. function reverseIP($ip)
  33. {
  34. return implode('.', array_reverse(explode('.', $ip)));
  35. }
  36. function checkIP($ip)
  37. {
  38. $oct = explode('.', $ip);
  39. if (count($oct) != 4) {
  40. return false;
  41. }
  42. for ($i = 0; $i < 4; $i++) {
  43. if (!preg_match("/^[0-9]+$/", $oct[$i])) {
  44. return false;
  45. }
  46. if ($oct[$i] < 0 || $oct[$i] > 255) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. function getIP()
  53. {
  54. $ip = 'unknown';
  55. $ip_array = array();
  56. if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')) {
  57. $ip_array += explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
  58. } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] != '') {
  59. $ip_array[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
  60. }
  61. if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] != '') {
  62. $ip_array[] = $_SERVER['REMOTE_ADDR'];
  63. }
  64. foreach ( $ip_array as $ip_s ) {
  65. if( !empty($ip_s) && !$this->_isPrivateNet($ip_s)){
  66. $ip = $ip_s;
  67. break;
  68. }
  69. }
  70. return $ip;
  71. }
  72. function _isPrivateNet($ip)
  73. {
  74. $private_ips = array(
  75. '127.0.0.0/8',
  76. '10.0.0.0/8',
  77. '172.16.0.0/12',
  78. '192.168.0.0/16'
  79. );
  80. foreach ($private_ips as $private) {
  81. list($net, $mask) = split('/', $private);
  82. if($this->_isIpInNet($ip, $net, $mask)){
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. function _isIpInNet($ip, $net, $mask)
  89. {
  90. $long_net = ip2long($net);
  91. $long_ip = ip2long($ip);
  92. $binary_net = str_pad( decbin($long_net), 32, '0', 'STR_PAD_LEFT');
  93. $firstpart = substr($binary_net, 0, $mask);
  94. $binary_ip = str_pad( decbin($long_ip), 32, '0', 'STR_PAD_LEFT');
  95. $firstip = substr($binary_ip, 0, $mask);
  96. return (strcmp($firstpart, $firstip) == 0);
  97. }
  98. function buildLookupDomain($urls) {
  99. foreach($urls as $url) {
  100. // extract the hostname from the given URI
  101. $parsed_url = parse_url($url);
  102. $host = $parsed_url['host'];
  103. // check if the "hostname" is an ip
  104. if($this->checkIP($host)) {
  105. $this->_ips[] = $this->reverseIP($host);
  106. } else {
  107. $this->_domains[] = $this->_stripDomainPrefixes($host);
  108. }
  109. }
  110. $this->_ips = array_unique($this->_ips);
  111. $this->_domains = array_unique($this->_domains);
  112. }
  113. function _stripDomainPrefixes($host) {
  114. static $doubleLevelTlds = array();
  115. if (empty($doubleLevelTlds)) {
  116. $doubleLevelTlds = $this->_getDoubleCcTld();
  117. }
  118. $host_elements = explode('.', $host);
  119. while (count($host_elements) > 3) {
  120. array_shift($host_elements);
  121. }
  122. $host_3_elements = implode('.', $host_elements);
  123. $host_elements = explode('.', $host);
  124. while (count($host_elements) > 2) {
  125. array_shift($host_elements);
  126. }
  127. $host_2_elements = implode('.', $host_elements);
  128. // check if is in "CC-2-level-TLD"
  129. return (array_key_exists($host_2_elements, $doubleLevelTlds))? $host_3_elements : $host_2_elements;
  130. }
  131. function _getDoubleCcTld()
  132. {
  133. $duration = 2592000; // 30 days
  134. $cache_path = XOOPS_TRUST_PATH . '/cache';
  135. $cache_file = $cache_path.'/two_level_tld_'.substr(md5($this->_doubleCcTldFile), 0, 12);
  136. $cache_file_mtime = file_exists($cache_file) ? filemtime($cache_file) : 0 ;
  137. if(!file_exists($cache_file) || $cache_file_mtime < time() - $duration) {
  138. $snoopy = new Snoopy;
  139. if($snoopy->fetch($this->_doubleCcTldFile)) {
  140. $contents = $snoopy->results;
  141. } else {
  142. // $this->setErrors('Could not get doubleCcTld data: '.$snoopy-error);
  143. return array();
  144. }
  145. $fp = fopen($cache_file, 'wb');
  146. if(!$fp) return array();
  147. fwrite($fp, $contents) ;
  148. fclose($fp) ;
  149. } else {
  150. $fp = fopen($cache_file, 'rb');
  151. $contents = fread($fp, filesize($cache_file));
  152. fclose($fp);
  153. }
  154. $data = explode("\n", $contents);
  155. return array_flip($data);
  156. }
  157. }
  158. ?>