Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 4dd419a

Browse files
committed
Code refactoring by @MASNathan
2 parents f5493f1 + e5275f2 commit 4dd419a

File tree

1,357 files changed

+120
-118337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,357 files changed

+120
-118337
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ docs/*.pyc
99
.git*/
1010
.idea
1111
.DS_STORE
12+
vendor

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"autoload": {
3838
"psr-4": {
39-
"Josantonius\\Ip\\": "src/Ip/"
39+
"Josantonius\\Ip\\": "src/"
4040
}
4141
},
4242
"extra": {

src/Ip.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* PHP class to get user IP.
4+
*
5+
* @author Tobias Sette Class - http://github.com/gnumoksha
6+
* @author Josantonius - hello@josantonius.com
7+
* @copyright Copyright (c) 2017
8+
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
9+
* @link https://github.com/Josantonius/PHP-Ip
10+
* @since 1.0.0
11+
*/
12+
13+
namespace Josantonius\Ip;
14+
15+
/**
16+
* IP handler.
17+
*
18+
* @since 1.0.0
19+
*/
20+
class Ip
21+
{
22+
/**
23+
* Gets the key value from globals
24+
*
25+
* @since 1.1.3
26+
*
27+
* @param string $key
28+
*
29+
* @return string|null
30+
*/
31+
protected static function getGlobalValue($key)
32+
{
33+
if (isset($_SERVER[$key]) && static::validate($_SERVER[$key])) {
34+
return $_SERVER[$key];
35+
}
36+
37+
if (isset($_ENV[$key]) && static::validate($_ENV[$key])) {
38+
return $_ENV[$key];
39+
}
40+
41+
if (@getenv($key) && static::validate(getenv($key))) {
42+
return getenv($key);
43+
}
44+
45+
return null;
46+
}
47+
48+
/**
49+
* Get user's ip
50+
*
51+
* @since 1.0.0
52+
*
53+
* @return string|false → user IP
54+
*/
55+
public static function get()
56+
{
57+
$possibleKeys = [
58+
'HTTP_X_FORWARDED_FOR',
59+
'HTTP_X_FORWARDED',
60+
'HTTP_X_CLUSTER_CLIENT_IP',
61+
'HTTP_FORWARDED_FOR',
62+
'HTTP_FORWARDED',
63+
'HTTP_VIA',
64+
'HTTP_X_COMING_FROM',
65+
'HTTP_COMING_FROM',
66+
'HTTP_X_REAL_IP',
67+
'REMOTE_ADDR',
68+
];
69+
70+
foreach ($possibleKeys as $key) {
71+
if ($ip = static::getGlobalValue($key)) {
72+
return $ip;
73+
}
74+
}
75+
76+
return false;
77+
}
78+
79+
/**
80+
* Validate ip.
81+
*
82+
* @since 1.1.3
83+
*
84+
* @param string $ip
85+
*
86+
* @return boolean
87+
*/
88+
public static function validate($ip)
89+
{
90+
return filter_var($ip, FILTER_VALIDATE_IP) ? true : false;
91+
}
92+
}

src/Ip/Ip.php

Lines changed: 0 additions & 233 deletions
This file was deleted.

src/bootstrap.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
<?php
1+
<?php
22
/**
33
* PHP class to get user IP.
4-
*
5-
* @author Tobias Sette Class - http://github.com/gnumoksha
4+
*
5+
* @author Tobias Sette Class - http://github.com/gnumoksha
66
* @author Josantonius - hello@josantonius.com
77
* @copyright Copyright (c) 2017
88
* @license https://opensource.org/licenses/MIT - The MIT License (MIT)
99
* @link https://github.com/Josantonius/PHP-Ip
1010
* @since 1.1.4
1111
*/
1212

13-
function includeIfExists($file) {
14-
13+
function includeIfExists($file)
14+
{
1515
if (file_exists($file)) {
16-
16+
1717
return include $file;
1818
}
1919
}
2020

21-
if ((!$loader = includeIfExists(__DIR__ . '/../vendor/autoload.php')) &&
22-
(!$loader = includeIfExists(__DIR__ . '/../../../autoload.php'))) {
23-
21+
if ((!$loader = includeIfExists(__DIR__ . '/../vendor/autoload.php')) &&
22+
(!$loader = includeIfExists(__DIR__ . '/../../../autoload.php'))
23+
) {
24+
2425
die(PHP_EOL . 'You must set up the project dependencies, ' .
25-
'run the following commands:' . PHP_EOL .
26+
'run the following commands:' . PHP_EOL .
2627
PHP_EOL . 'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
2728
PHP_EOL . 'php composer.phar install' . PHP_EOL . PHP_EOL);
2829
}

0 commit comments

Comments
 (0)