|
9 | 9 |
|
10 | 10 | namespace nguyenanhung\MyRequests; |
11 | 11 |
|
12 | | -use Exception; |
13 | | -use IPv4\SubnetCalculator; |
14 | | -use IPLib\Factory; |
15 | | -use Curl\Curl; |
| 12 | +use nguyenanhung\Libraries\IP\IP as BaseIP; |
16 | 13 |
|
17 | 14 | /** |
18 | 15 | * Class Ip |
|
21 | 18 | * @author 713uk13m <[email protected]> |
22 | 19 | * @copyright 713uk13m <[email protected]> |
23 | 20 | */ |
24 | | -class Ip implements ProjectInterface |
| 21 | +class Ip extends BaseIP |
25 | 22 | { |
26 | | - use Version; |
27 | | - |
28 | | - /** @var bool Cấu hình class có nhận IP theo HA Proxy hay không */ |
29 | | - protected $haProxyStatus; |
30 | | - |
31 | | - /** |
32 | | - * Ip constructor. |
33 | | - * |
34 | | - * @author : 713uk13m <[email protected]> |
35 | | - * @copyright: 713uk13m <[email protected]> |
36 | | - */ |
37 | | - public function __construct() |
38 | | - { |
39 | | - } |
40 | | - |
41 | | - /** |
42 | | - * Function getIpAddress |
43 | | - * |
44 | | - * @param bool $convertToInteger |
45 | | - * |
46 | | - * @return bool|int|string |
47 | | - * @author : 713uk13m <[email protected]> |
48 | | - * @copyright: 713uk13m <[email protected]> |
49 | | - * @time : 08/08/2020 43:50 |
50 | | - */ |
51 | | - public function getIpAddress(bool $convertToInteger = FALSE) |
52 | | - { |
53 | | - if ($this->haProxyStatus === TRUE) { |
54 | | - $ip = $this->getIpByHaProxy($convertToInteger); |
55 | | - } else { |
56 | | - $ip = $this->getRawIpAddress($convertToInteger); |
57 | | - } |
58 | | - |
59 | | - return $ip; |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * Function setHaProxy |
64 | | - * |
65 | | - * @param bool $haProxyStatus |
66 | | - * |
67 | | - * @return $this |
68 | | - * @author : 713uk13m <[email protected]> |
69 | | - * @copyright: 713uk13m <[email protected]> |
70 | | - * @time : 08/08/2020 44:11 |
71 | | - */ |
72 | | - public function setHaProxy(bool $haProxyStatus = FALSE): self |
73 | | - { |
74 | | - $this->haProxyStatus = $haProxyStatus; |
75 | | - |
76 | | - return $this; |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Function getIpByHaProxy |
81 | | - * |
82 | | - * @param bool $convertToInteger |
83 | | - * |
84 | | - * @return false|int|string |
85 | | - * @author : 713uk13m <[email protected]> |
86 | | - * @copyright: 713uk13m <[email protected]> |
87 | | - * @time : 09/07/2021 58:38 |
88 | | - */ |
89 | | - public function getIpByHaProxy(bool $convertToInteger = FALSE) |
90 | | - { |
91 | | - $key = 'HTTP_X_FORWARDED_FOR'; |
92 | | - if (array_key_exists($key, $_SERVER) === TRUE) { |
93 | | - foreach (explode(',', $_SERVER[$key]) as $ip) { |
94 | | - $ip = trim($ip); |
95 | | - if ($this->ipValidate($ip)) { |
96 | | - if ($convertToInteger === TRUE) { |
97 | | - return ip2long($ip); |
98 | | - } |
99 | | - |
100 | | - return $ip; |
101 | | - } |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - return FALSE; |
106 | | - } |
107 | | - |
108 | | - /** |
109 | | - * Function getRawIpAddress |
110 | | - * |
111 | | - * @param bool $convertToInteger |
112 | | - * |
113 | | - * @return false|int|string |
114 | | - * @author : 713uk13m <[email protected]> |
115 | | - * @copyright: 713uk13m <[email protected]> |
116 | | - * @time : 09/07/2021 58:45 |
117 | | - */ |
118 | | - public function getRawIpAddress(bool $convertToInteger = FALSE) |
119 | | - { |
120 | | - $ip_keys = array( |
121 | | - 0 => 'HTTP_CF_CONNECTING_IP', |
122 | | - 1 => 'HTTP_X_FORWARDED_FOR', |
123 | | - 2 => 'HTTP_X_FORWARDED', |
124 | | - 3 => 'HTTP_X_IPADDRESS', |
125 | | - 4 => 'HTTP_X_CLUSTER_CLIENT_IP', |
126 | | - 5 => 'HTTP_FORWARDED_FOR', |
127 | | - 6 => 'HTTP_FORWARDED', |
128 | | - 7 => 'HTTP_CLIENT_IP', |
129 | | - 8 => 'HTTP_IP', |
130 | | - 9 => 'REMOTE_ADDR' |
131 | | - ); |
132 | | - foreach ($ip_keys as $key) { |
133 | | - if (array_key_exists($key, $_SERVER) === TRUE) { |
134 | | - foreach (explode(',', $_SERVER[$key]) as $ip) { |
135 | | - $ip = trim($ip); |
136 | | - if ($this->ipValidate($ip)) { |
137 | | - if ($convertToInteger === TRUE) { |
138 | | - return ip2long($ip); |
139 | | - } |
140 | | - |
141 | | - return $ip; |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - } |
146 | | - |
147 | | - return FALSE; |
148 | | - } |
149 | | - |
150 | | - /** |
151 | | - * Function ipInRange |
152 | | - * |
153 | | - * @param string $ip_address |
154 | | - * @param string $network_range |
155 | | - * |
156 | | - * @return bool|string|null |
157 | | - * @author : 713uk13m <[email protected]> |
158 | | - * @copyright: 713uk13m <[email protected]> |
159 | | - * @time : 09/20/2021 07:43 |
160 | | - */ |
161 | | - public function ipInRange(string $ip_address = '', string $network_range = '') |
162 | | - { |
163 | | - $ip_address = trim($ip_address); |
164 | | - $network_range = trim($network_range); |
165 | | - if (empty($ip_address) || empty($network_range)) { |
166 | | - return NULL; |
167 | | - } |
168 | | - try { |
169 | | - $address = Factory::parseAddressString($ip_address); |
170 | | - $range = Factory::parseRangeString($network_range); |
171 | | - |
172 | | - return $address->matches($range); |
173 | | - } |
174 | | - catch (Exception $e) { |
175 | | - $result = 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage(); |
176 | | - if (function_exists('log_message')) { |
177 | | - log_message('error', 'Error Message: ' . $e->getMessage()); |
178 | | - log_message('error', 'Error Trace As String: ' . $e->getTraceAsString()); |
179 | | - } |
180 | | - |
181 | | - return $result; |
182 | | - } |
183 | | - } |
184 | | - |
185 | | - /** |
186 | | - * Function ipCalculator |
187 | | - * |
188 | | - * @param $ip |
189 | | - * @param $network_size |
190 | | - * |
191 | | - * @return array|string |
192 | | - * @author : 713uk13m <[email protected]> |
193 | | - * @copyright: 713uk13m <[email protected]> |
194 | | - * @time : 08/08/2020 47:09 |
195 | | - */ |
196 | | - public function ipCalculator($ip, $network_size) |
197 | | - { |
198 | | - try { |
199 | | - $result = new SubnetCalculator($ip, $network_size); |
200 | | - |
201 | | - return $result->getSubnetArrayReport(); |
202 | | - } |
203 | | - catch (Exception $e) { |
204 | | - $message = 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage(); |
205 | | - if (function_exists('log_message')) { |
206 | | - log_message('error', 'Error Message: ' . $e->getMessage()); |
207 | | - log_message('error', 'Error Trace As String: ' . $e->getTraceAsString()); |
208 | | - } |
209 | | - |
210 | | - return $message; |
211 | | - } |
212 | | - } |
213 | | - |
214 | | - /** |
215 | | - * Function ipValidate |
216 | | - * |
217 | | - * @param $ip |
218 | | - * |
219 | | - * @return bool |
220 | | - * @author : 713uk13m <[email protected]> |
221 | | - * @copyright: 713uk13m <[email protected]> |
222 | | - * @time : 08/08/2020 47:25 |
223 | | - */ |
224 | | - public function ipValidate($ip): bool |
225 | | - { |
226 | | - return !(filter_var($ip, FILTER_VALIDATE_IP) === false); |
227 | | - } |
228 | | - |
229 | | - /** |
230 | | - * Function ipValidateV4 |
231 | | - * |
232 | | - * @param $ip |
233 | | - * |
234 | | - * @return bool |
235 | | - * @author : 713uk13m <[email protected]> |
236 | | - * @copyright: 713uk13m <[email protected]> |
237 | | - * @time : 08/08/2020 47:31 |
238 | | - */ |
239 | | - public function ipValidateV4($ip): bool |
240 | | - { |
241 | | - return !(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false); |
242 | | - } |
243 | | - |
244 | | - /** |
245 | | - * Function ipValidateV6 |
246 | | - * |
247 | | - * @param $ip |
248 | | - * |
249 | | - * @return bool |
250 | | - * @author : 713uk13m <[email protected]> |
251 | | - * @copyright: 713uk13m <[email protected]> |
252 | | - * @time : 08/08/2020 47:40 |
253 | | - */ |
254 | | - public function ipValidateV6($ip): bool |
255 | | - { |
256 | | - return !(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false); |
257 | | - } |
258 | | - |
259 | | - /** |
260 | | - * Function ip2longV6 |
261 | | - * |
262 | | - * @param $ip |
263 | | - * |
264 | | - * @return string |
265 | | - * @author : 713uk13m <[email protected]> |
266 | | - * @copyright: 713uk13m <[email protected]> |
267 | | - * @time : 08/08/2020 47:45 |
268 | | - */ |
269 | | - public function ip2longV6($ip): string |
270 | | - { |
271 | | - if (substr_count($ip, '::')) { |
272 | | - $ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip); |
273 | | - } |
274 | | - $ip = explode(':', $ip); |
275 | | - $r_ip = ''; |
276 | | - foreach ($ip as $v) { |
277 | | - $r_ip .= str_pad(base_convert($v, 16, 2), 16, 0, STR_PAD_LEFT); |
278 | | - } |
279 | | - |
280 | | - return base_convert($r_ip, 2, 10); |
281 | | - } |
282 | | - |
283 | | - /** |
284 | | - * Function ipInfo |
285 | | - * |
286 | | - * @param string $ip |
287 | | - * |
288 | | - * @return string|null |
289 | | - * @author : 713uk13m <[email protected]> |
290 | | - * @copyright: 713uk13m <[email protected]> |
291 | | - * @time : 09/20/2021 14:49 |
292 | | - */ |
293 | | - public function ipInfo(string $ip = ''): ?string |
294 | | - { |
295 | | - if (empty($ip)) { |
296 | | - $ip = $this->getIpAddress(); |
297 | | - } |
298 | | - try { |
299 | | - $curl = new Curl(); |
300 | | - $curl->get('http://ip-api.com/json/' . $ip); |
301 | | - |
302 | | - if ($curl->error) { |
303 | | - return "cURL Error: " . $curl->errorMessage; |
304 | | - } |
305 | | - |
306 | | - return $curl->rawResponse; |
307 | | - } |
308 | | - catch (Exception $e) { |
309 | | - if (function_exists('log_message')) { |
310 | | - log_message('error', 'Error Message: ' . $e->getMessage()); |
311 | | - log_message('error', 'Error Trace As String: ' . $e->getTraceAsString()); |
312 | | - } |
313 | | - |
314 | | - return 'Error File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Code: ' . $e->getCode() . ' - Message: ' . $e->getMessage(); |
315 | | - |
316 | | - } |
317 | | - } |
318 | 23 | } |
0 commit comments