First commit

This commit is contained in:
2025-06-18 10:24:27 +08:00
commit ebc39cd5dd
3873 changed files with 412712 additions and 0 deletions

View File

View File

@@ -0,0 +1,39 @@
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: ip_getter_dnslist.php 1034 2019-12-19 12:00:00Z community $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class ip_getter_dnslist {
public static function get($s) {
if (empty($s['header']) || empty($s['list'])) {
return $_SERVER['REMOTE_ADDR'];
}
$ip = $_SERVER['REMOTE_ADDR'];
$rdns = gethostbyaddr($ip);
foreach($s['list'] as $host) {
if (preg_match('/'.$host.'$/i', $rdns)) {
if ($s['header'] != 'HTTP_X_FORWARDED_FOR') {
$ip = ip::validate_ip($_SERVER[$s['header']]) ? $_SERVER[$s['header']] : $ip;
} else {
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ",") > 0) {
$exp = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = ip::validate_ip(trim($exp[0])) ? $exp[0] : $ip;
} else {
$ip = ip::validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $ip;
}
}
}
}
return $ip;
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: ip_getter_header.php 873 2019-12-19 12:00:00Z community $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class ip_getter_header {
public static function get($s) {
if (empty($s['header'])) {
return $_SERVER['REMOTE_ADDR'];
}
$ip = $_SERVER['REMOTE_ADDR'];
if ($s['header'] != 'HTTP_X_FORWARDED_FOR') {
$ip = ip::validate_ip($_SERVER[$s['header']]) ? $_SERVER[$s['header']] : $ip;
} else {
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ",") > 0) {
$exp = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = ip::validate_ip(trim($exp[0])) ? $exp[0] : $ip;
} else {
$ip = ip::validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $ip;
}
}
return $ip;
}
}

View File

@@ -0,0 +1,39 @@
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: ip_getter_iplist.php 1022 2019-12-19 12:00:00Z community $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class ip_getter_iplist {
public static function get($s) {
if (empty($s['header']) || empty($s['list'])) {
return $_SERVER['REMOTE_ADDR'];
}
$ip = $_SERVER['REMOTE_ADDR'];
$rdns = gethostbyaddr($ip);
foreach($s['list'] as $host) {
if (ip::check_ip($ip, $host)) {
if ($s['header'] != 'HTTP_X_FORWARDED_FOR') {
$ip = ip::validate_ip($_SERVER[$s['header']]) ? $_SERVER[$s['header']] : $ip;
} else {
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ",") > 0) {
$exp = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = ip::validate_ip(trim($exp[0])) ? $exp[0] : $ip;
} else {
$ip = ip::validate_ip($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $ip;
}
}
}
}
return $ip;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: ip_tiny.php 1587 2019-12-03 12:00:00Z opensource $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class ip_tiny_init_exception extends Exception {}
class ip_tiny {
private static $instance = NULL;
private $fp = NULL;
private $offset = array();
private $index = NULL;
private $length = 0;
private function __construct() {
$ipdatafile = constant("DISCUZ_ROOT").'./data/ipdata/tinyipdata.dat';
if($this->fp === NULL && $this->fp = fopen($ipdatafile, 'rb')) {
$this->offset = unpack('Nlen', fread($this->fp, 4));
$this->index = fread($this->fp, $this->offset['len'] - 4);
}
if($this->fp === FALSE) {
throw new ip_tiny_init_exception();
}
$this->length = $this->offset['len'] - 1028;
}
function __destruct() {
if ($this->fp) {
@fclose($this->fp);
}
}
public static function getInstance() {
if (!self::$instance) {
try {
self::$instance = new ip_tiny();
} catch (Exception $e) {
return NULL;
}
}
return self::$instance;
}
public function convert($ip) {
$ipdot = explode('.', $ip);
$ip = pack('N', ip2long($ip));
$ipdot[0] = (int)$ipdot[0];
$ipdot[1] = (int)$ipdot[1];
$start = @unpack('Vlen', $this->index[$ipdot[0] * 4] . $this->index[$ipdot[0] * 4 + 1] . $this->index[$ipdot[0] * 4 + 2] . $this->index[$ipdot[0] * 4 + 3]);
for ($start = $start['len'] * 8 + 1024; $start < $this->length; $start += 8) {
if ($this->index[$start] . $this->index[$start + 1] . $this->index[$start + 2] . $this->index[$start + 3] >= $ip) {
$index_offset = @unpack('Vlen', $this->index[$start + 4] . $this->index[$start + 5] . $this->index[$start + 6] . "\x0");
$index_length = @unpack('Clen', $this->index[$start + 7]);
break;
}
}
@fseek($this->fp, $this->offset['len'] + $index_offset['len'] - 1024);
if($index_length['len']) {
return '- '.@fread($this->fp, $index_length['len']);
} else {
return '- Unknown';
}
}
}
?>

View File

@@ -0,0 +1,123 @@
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class ip_v6wry_init_exception extends Exception {}
class ip_v6wry {
private static $instance = null;
public $ipdb,$firstIndex,$indexCount,$offlen;
public function __construct() {
$ipdatafile = constant("DISCUZ_ROOT").'./data/ipdata/ipv6wry.dat';
$this->ipdb = fopen($ipdatafile,'rb');
if (!$this->ipdb) {
throw new ip_v6wry_init_exception();
}
$this->firstIndex = unpack('V',$this->reader(16,8))[1];
$this->indexCount = unpack('V',$this->reader(8,8))[1];
$this->offlen = ord($this->reader(6,1));
}
public function __destruct() {
if($this->ipdb) {
@fclose($this->ipdb);
}
}
public static function getInstance() {
if (!self::$instance) {
try {
self::$instance = new ip_v6wry();
} catch (Exception $e) {
return null;
}
}
return self::$instance;
}
public function getstring($offset) {
fseek($this->ipdb,$offset);
$flag = 1;
$return = '';
while($flag) {
$i = fread($this->ipdb,1);
if($i === "\0") {
$flag = 0;
} else {
$return .= $i;
}
}
return $return;
}
public function getareaaddr($offset) {
$byte = ord($this->reader($offset,1));
if($byte == 1 || $byte == 2) {
$p = unpack('V',str_pad($this->reader($offset + 1,$this->offlen),4,"\0"))[1];
return $this->getareaaddr($p);
} else {
return $this->getstring($offset);
}
}
public function getaddr($offset) {
$byte = ord($this->reader($offset,1));
if($byte == 1) {
return $this->getaddr(unpack('V',str_pad($this->reader($offset + 1,$this->offlen),4,"\0"))[1]);
} else {
$carea = $this->getareaaddr($offset);
if($byte == 2) {
$offset += 1 + $this->offlen;
} else {
$offset += strlen($carea) + 1;
}
$aarea = $this->getareaaddr($offset);
return [$carea,$aarea];
}
}
public function ipcomp($ip1,$ip2) {
$ip1a = unpack('v',substr($ip1,-2))[1];
$ip2a = unpack('v',substr($ip2,-2))[1];
if($ip1a == $ip2a) {
if(strlen($ip1)<=2) {
return 0;
} else {
return $this->ipcomp(substr($ip1,0,-2),substr($ip2,0,-2));
}
} elseif($ip1a > $ip2a) {
return 1;
} else {
return -1;
}
}
public function reader($offset,$length) {
fseek($this->ipdb,$offset);
return fread($this->ipdb,$length);
}
public function finder($ip,$l,$r) {
if($r-$l<=1) {
return $l;
}
$m = intval(($l + $r)/2);
$o = $this->firstIndex + $m * (8 + $this->offlen);
$new_ip = $this->reader($o,8);
if($this->ipcomp($new_ip,$ip)>0) {
return $this->finder($ip,$l,$m);
} else {
return $this->finder($ip,$m,$r);
}
}
public function getipaddr($ip) {
$ipbinary = inet_pton($ip);
if($ipbinary == false) {
return '- Unknown';
}
$iprev = strrev($ipbinary);
$i = $this->finder($iprev,0,$this->indexCount);
$o = $this->firstIndex + $i * (8 + $this->offlen);
$output = $this->getaddr(unpack('L',str_pad($this->reader($o + 8,$this->offlen),4,"\0"))[1]);
return $output;
}
public function convert($ip) {
return '- '.diconv(implode(' ',$this->getipaddr($ip)),'utf-8');
}
}
?>

171
source/class/ip/ip_wry.php Normal file
View File

@@ -0,0 +1,171 @@
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: ip_wry.php 3915 2019-12-03 12:00:00Z opensource $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class ip_wry_init_exception extends Exception {}
class ip_wry {
private static $instance = null;
private $fp = null;
private $ipbegin = null;
private $ipAllNum = null;
private function __construct() {
$ipdatafile = constant("DISCUZ_ROOT").'./data/ipdata/wry.dat';
$this->fp = fopen($ipdatafile, 'rb');
if (!$this->fp) {
throw new ip_wry_init_exception();
}
if(!($DataBegin = fread($this->fp, 4)) || !($DataEnd = fread($this->fp, 4)) ) throw new ip_wry_init_exception();
$this->ipbegin = implode('', unpack('L', $DataBegin));
if($this->ipbegin < 0) $this->ipbegin += pow(2, 32);
$ipend = implode('', unpack('L', $DataEnd));
if($ipend < 0) $ipend += pow(2, 32);
$this->ipAllNum = ($ipend - $this->ipbegin) / 7 + 1;
}
function __destruct() {
if ($this->fp) {
@fclose($this->fp);
}
}
public static function getInstance() {
if (!self::$instance) {
try {
self::$instance = new ip_wry();
} catch (Exception $e) {
return null;
}
}
return self::$instance;
}
public function convert($ip) {
$ip = explode('.', $ip);
$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
$BeginNum = $ip2num = $ip1num = 0;
$ipAddr1 = $ipAddr2 = '';
$EndNum = $this->ipAllNum;
while($ip1num > $ipNum || $ip2num < $ipNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);
fseek($this->fp, $this->ipbegin + 7 * $Middle);
$ipData1 = fread($this->fp, 4);
if(strlen($ipData1) < 4) {
return '- System Error';
}
$ip1num = implode('', unpack('L', $ipData1));
if($ip1num < 0) $ip1num += pow(2, 32);
if($ip1num > $ipNum) {
$EndNum = $Middle;
continue;
}
$DataSeek = fread($this->fp, 3);
if(strlen($DataSeek) < 3) {
return '- System Error';
}
$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
fseek($this->fp, $DataSeek);
$ipData2 = fread($this->fp, 4);
if(strlen($ipData2) < 4) {
return '- System Error';
}
$ip2num = implode('', unpack('L', $ipData2));
if($ip2num < 0) $ip2num += pow(2, 32);
if($ip2num < $ipNum) {
if($Middle == $BeginNum) {
return '- Unknown';
}
$BeginNum = $Middle;
}
}
$ipFlag = fread($this->fp, 1);
if($ipFlag == chr(1)) {
$ipSeek = fread($this->fp, 3);
if(strlen($ipSeek) < 3) {
return '- System Error';
}
$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
fseek($this->fp, $ipSeek);
$ipFlag = fread($this->fp, 1);
}
if($ipFlag == chr(2)) {
$AddrSeek = fread($this->fp, 3);
if(strlen($AddrSeek) < 3) {
return '- System Error';
}
$ipFlag = fread($this->fp, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($this->fp, 3);
if(strlen($AddrSeek2) < 3) {
return '- System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($this->fp, $AddrSeek2);
} else {
fseek($this->fp, -1, SEEK_CUR);
}
while(($char = fread($this->fp, 1)) != chr(0))
$ipAddr2 .= $char;
$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
fseek($this->fp, $AddrSeek);
while(($char = fread($this->fp, 1)) != chr(0))
$ipAddr1 .= $char;
} else {
fseek($this->fp, -1, SEEK_CUR);
while(($char = fread($this->fp, 1)) != chr(0))
$ipAddr1 .= $char;
$ipFlag = fread($this->fp, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($this->fp, 3);
if(strlen($AddrSeek2) < 3) {
return '- System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($this->fp, $AddrSeek2);
} else {
fseek($this->fp, -1, SEEK_CUR);
}
while(($char = fread($this->fp, 1)) != chr(0))
$ipAddr2 .= $char;
}
if(preg_match('/http/i', $ipAddr2)) {
$ipAddr2 = '';
}
$ipaddr = "$ipAddr1 $ipAddr2";
$ipaddr = preg_replace('/CZ88\.NET/is', '', $ipaddr);
$ipaddr = preg_replace('/^\s*/is', '', $ipaddr);
$ipaddr = preg_replace('/\s*$/is', '', $ipaddr);
if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
$ipaddr = '- Unknown';
}
return '- '.diconv($ipaddr, 'GBK');
}
}
?>