-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIpLocation.php
More file actions
executable file
·141 lines (123 loc) · 2.84 KB
/
IpLocation.php
File metadata and controls
executable file
·141 lines (123 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/*
* Copyright (c) 2023. Ankio. All Rights Reserved.
*/
namespace library\ip;
use library\ip\IpParser\IpV6wry;
use library\ip\IpParser\QWry;
/**
*
*/
define("IP_DATABASE_ROOT_DIR", __DIR__);
/**
* Class IpLocation
* @package itbdw\Ip
*/
class IpLocation
{
/**
* @var
*/
private static $ipV4Path;
/**
* @var
*/
private static $ipV6Path;
/**
* @param $ip
* @param string $ipV4Path
* @param string $ipV6Path
* @return array
*/
public static function getLocation($ip, string $ipV4Path = '', string $ipV6Path = ''): array
{
$location = self::getLocationWithoutParse($ip, $ipV4Path, $ipV6Path);
if (isset($location['error'])) {
return $location;
}
return StringParser::parse($location);
}
/**
* @param $ip
* @param string $ipV4Path
* @param string $ipV6Path
* @return array
*/
public static function getLocationWithoutParse($ip, string $ipV4Path = '', string $ipV6Path = ''): array
{
//if ipV4Path 记录位置
if (strlen($ipV4Path)) {
self::setIpV4Path($ipV4Path);
}
//if ipV6Path 记录位置
if (strlen($ipV6Path)) {
self::setIpV6Path($ipV6Path);
}
if (self::isIpV4($ip)) {
$ins = new QWry();
$ins->setDBPath(self::getIpV4Path());
$location = $ins->getIp($ip);
} else if (self::isIpV6($ip)) {
$ins = new IpV6wry();
$ins->setDBPath(self::getIpV6Path());
$location = $ins->getIp($ip);
} else {
$location = [
'error' => 'IP Invalid'
];
}
return $location;
}
/**
* @param $path
*/
public static function setIpV4Path($path)
{
self::$ipV4Path = $path;
}
/**
* @param $path
*/
public static function setIpV6Path($path)
{
self::$ipV6Path = $path;
}
/**
* @param $ip
* @return bool
*/
private static function isIpV4($ip): bool
{
return false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
/**
* @return string
*/
private static function getIpV4Path(): string
{
return self::$ipV4Path ?: self::root('/db/qqwry.dat');
}
/**
* @param $filename
* @return string
*/
public static function root($filename): string
{
return IP_DATABASE_ROOT_DIR . $filename;
}
/**
* @param $ip
* @return bool
*/
private static function isIpV6($ip): bool
{
return false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
/**
* @return string
*/
private static function getIpV6Path(): string
{
return self::$ipV6Path ?: self::root('/db/ipv6wry.db');
}
}