PHPClientAddr::isHttpXForwardedFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace PHPTools\PHPClientAddr;
3
4
/**
5
 * Clas eque permite obtener la Ip y HostName del cliente
6
 */
7
class PHPClientAddr
8
{
9
    /**
10
     * Arreglod e expreciones regulres para validar una IP privada.
11
     * @var array
12
     */
13
    private $privateIp = array(
14
        '/^0\./','/^127\.0\.0\.1/',
15
        '/^192\.168\..*/','/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
16
        '/^10\..*/'
17
    );
18
19
    /**
20
     * Ip del cliente.
21
     * @var string
22
     */
23
    public $ip;
24
25
    /**
26
     * Hostname del Cliente.
27
     * @var string
28
     */
29
    public $hostname;
30
31
    /**
32
     * Arreglo de parametros del servidor.
33
     * @var array
34
     */
35
    private $server;
36
37
    /**
38
     * Arreglo de parametros del entorno.
39
     * @var array
40
     */
41
    private $env;
42
43
    /**
44
     * Thr constructor.
45
     */
46
    public function __construct()
47
    {
48
        $this->server = $_SERVER;
49
        $this->env = $_ENV;
50
        $this->ip = $this->getRemodeAddr();
51
        $this->getIpForwarded();
52
        $this->hostname = $this->getRemoteHostname();
53
    }
54
55
    /**
56
     * Permite obtener el hostname de la IP
57
     * @return string
58
     */
59
    private function getRemoteHostname()
60
    {
61
        $hostname = NULL;
62
63
        if(!is_null($this->ip)) {
64
            $hostname = gethostbyaddr($this->ip);
65
        }
66
        return $hostname;
67
    }
68
69
    /**
70
     * Permite obtener la IP proveniente de un servidor proxy
71
     * @return void
72
     */
73
    private function getIpForwarded()
74
    {
75
        if(!!$this->isHttpXForwardedFor()) {
76
            $entries = $this->getHttpXForwardedForEntities();
77
            $this->ip = $this->getXForwardedIp($entries);
78
        }
79
    }
80
81
    /**
82
     * Permite saber si la peticion proviene de un servidor proxy.
83
     * @return boolean
84
     */
85
    private function isHttpXForwardedFor()
86
    {
87
        return !!isset($this->server['HTTP_X_FORWARDED_FOR'])&&$this->server['HTTP_X_FORWARDED_FOR']!='';
88
    }
89
90
    /**
91
     * Permite obtener todas las entidades enviadas por un servidor proxy.
92
     * @return array
93
     */
94
    private function getHttpXForwardedForEntities()
95
    {
96
        $entries = preg_split('[, ]', $this->server['HTTP_X_FORWARDED_FOR']);
97
        reset($entries);
98
        return $entries;
99
    }
100
101
    /**
102
     * Permite obtener la IP real proveniente de un servidor proxy.
103
     * @param  array $entries Arreglo de entidades enviadas por un servidor proxy
104
     * @return string
105
     */
106
    private function getXForwardedIp($entries)
107
    {
108
        $ip = $this->ip;
109
        while (list(, $entry) = each($entries)) {
110
            $entry = trim($entry);
111
            if ( preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $entry, $ip_list)) {
112
                $found_ip = preg_replace( $this->privateIp, $ip, $ip_list[1]);
113
114
                if ($ip != $found_ip) {
115
                    $ip = $found_ip;
116
                    break;
117
                }
118
            }
119
        }
120
        return $ip;
121
    }
122
123
    /**
124
     * Permite obtener la IP real del cliente.
125
     * @return string
126
     */
127
    private function getRemodeAddr()
128
    {
129
        $ip = NULL;
130
        if(PHP_SAPI=='cli') {
131
            $ip = gethostbyname(gethostname());
132
        } elseif($this->hasServerRemoteAddr()) {
133
            $ip = $this->server['REMOTE_ADDR'];
134
        } elseif($this->hasEnvRemoteAddr()) {
135
            $ip = $this->env['REMOTE_ADDR'];
136
        }
137
        return $ip;
138
    }
139
140
    /**
141
     * Check if the remote Ip is in the glocal variable $_SERVER
142
     * @return boolean
143
     */
144
    private function hasServerRemoteAddr()
145
    {
146
        return !!isset($this->server['REMOTE_ADDR'])
147
            &&!empty($this->server['REMOTE_ADDR'])
148
        ;
149
    }
150
151
    /**
152
     * Check if the remote Ip is in the glocal variable $_ENV
153
     * @return boolean
154
     */
155
    private function hasEnvRemoteAddr()
156
    {
157
        return !!isset($this->env['REMOTE_ADDR'])
158
            &&!empty($this->env['REMOTE_ADDR'])
159
        ;
160
    }
161
}
162