Completed
Push — master ( 20d8fe...40dd32 )
by Harald
09:20 queued 04:54
created

HTTP::getIpAddress()   D

Complexity

Conditions 11
Paths 192

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 21
nc 192
nop 1
dl 0
loc 42
rs 4.9629
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license GPL; https://www.oscommerce.com/gpllicense.txt
7
  */
8
9
namespace OSC\OM;
10
11
use OSC\OM\Is;
12
13
class HTTP
14
{
15
    protected static $request_type;
16
17
    public static function setRequestType()
18
    {
19
        static::$request_type = ((isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on')) || (isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] == 443))) ? 'SSL' : 'NONSSL';
20
    }
21
22
    public static function getRequestType()
23
    {
24
        return static::$request_type;
25
    }
26
27
    public static function redirect($url, $http_response_code = null)
28
    {
29
        if ((strstr($url, "\n") === false) && (strstr($url, "\r") === false)) {
30
            if ( strpos($url, '&amp;') !== false ) {
31
                $url = str_replace('&amp;', '&', $url);
32
            }
33
34
            header('Location: ' . $url, true, $http_response_code);
35
        }
36
37
        exit;
38
    }
39
40
    /**
41
     * @param array $parameters url, headers, parameters, method, verify_ssl, cafile, certificate, proxy
42
     */
43
44
    public static function getResponse(array $parameters)
45
    {
46
        $parameters['server'] = parse_url($parameters['url']);
47
48
        if (!isset($parameters['server']['port'])) {
49
            $parameters['server']['port'] = ($parameters['server']['scheme'] == 'https') ? 443 : 80;
50
        }
51
52
        if (!isset($parameters['server']['path'])) {
53
            $parameters['server']['path'] = '/';
54
        }
55
56
        if (isset($parameters['server']['user']) && isset($parameters['server']['pass'])) {
57
            $parameters['headers'][] = 'Authorization: Basic ' . base64_encode($parameters['server']['user'] . ':' . $parameters['server']['pass']);
58
        }
59
60
        unset($parameters['url']);
61
62
        if (!isset($parameters['headers']) || !is_array($parameters['headers'])) {
63
            $parameters['headers'] = [];
64
        }
65
66
        if (!isset($parameters['method'])) {
67
            if (isset($parameters['parameters'])) {
68
                $parameters['method'] = 'post';
69
            } else {
70
                $parameters['method'] = 'get';
71
            }
72
        }
73
74
        $curl = curl_init($parameters['server']['scheme'] . '://' . $parameters['server']['host'] . $parameters['server']['path'] . (isset($parameters['server']['query']) ? '?' . $parameters['server']['query'] : ''));
75
76
        $curl_options = [
77
            CURLOPT_PORT => $parameters['server']['port'],
78
            CURLOPT_HEADER => true,
79
            CURLOPT_RETURNTRANSFER => true,
80
            CURLOPT_FORBID_REUSE => true,
81
            CURLOPT_FRESH_CONNECT => true,
82
            CURLOPT_ENCODING => '', // disable gzip
83
            CURLOPT_FOLLOWLOCATION => false // does not work with open_basedir so a workaround is implemented below
84
        ];
85
86
        if (!empty($parameters['headers'])) {
87
            $curl_options[CURLOPT_HTTPHEADER] = $parameters['headers'];
88
        }
89
90
        if ($parameters['server']['scheme'] == 'https') {
91
            if (!isset($parameters['verify_ssl']) || ($parameters['verify_ssl'] === true)) {
92
                $curl_options[CURLOPT_SSL_VERIFYPEER] = true;
93
                $curl_options[CURLOPT_SSL_VERIFYHOST] = 2;
94
            } else {
95
                $curl_options[CURLOPT_SSL_VERIFYPEER] = false;
96
                $curl_options[CURLOPT_SSL_VERIFYHOST] = false;
97
            }
98
99
            if (!isset($parameters['cafile'])) {
100
                $parameters['cafile'] = OSCOM::getConfig('dir_root', 'Shop') . 'includes/cacert.pem';
101
            }
102
103
            if (is_file($parameters['cafile'])) {
104
                $curl_options[CURLOPT_CAINFO] = $parameters['cafile'];
105
            }
106
107
            if (isset($parameters['certificate'])) {
108
                $curl_options[CURLOPT_SSLCERT] = $parameters['certificate'];
109
            }
110
        }
111
112
        if ($parameters['method'] == 'post') {
113
            if (!isset($parameters['parameters'])) {
114
                $parameters['parameters'] = '';
115
            }
116
117
            $curl_options[CURLOPT_POST] = true;
118
            $curl_options[CURLOPT_POSTFIELDS] = $parameters['parameters'];
119
        }
120
121
        if (isset($parameters['proxy']) && !empty($parameters['proxy'])) {
122
            $curl_options[CURLOPT_HTTPPROXYTUNNEL] = true;
123
            $curl_options[CURLOPT_PROXY] = $parameters['proxy'];
124
        }
125
126
        curl_setopt_array($curl, $curl_options);
127
        $result = curl_exec($curl);
128
129
        if ($result === false) {
130
            trigger_error(curl_error($curl));
131
132
            curl_close($curl);
133
134
            return false;
135
        }
136
137
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
138
139
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
140
        $headers = trim(substr($result, 0, $header_size));
141
        $body = substr($result, $header_size);
142
143
        curl_close($curl);
144
145
        if (($http_code == 301) || ($http_code == 302)) {
146
            if (!isset($parameters['redir_counter']) || ($parameters['redir_counter'] < 6)) {
147
                if (!isset($parameters['redir_counter'])) {
148
                    $parameters['redir_counter'] = 0;
149
                }
150
151
                $matches = [];
152
                preg_match('/(Location:|URI:)(.*?)\n/i', $headers, $matches);
153
154
                $redir_url = trim(array_pop($matches));
155
156
                $parameters['redir_counter']++;
157
158
                $redir_params = [
159
                    'url' => $redir_url,
160
                    'method' => $parameters['method'],
161
                    'redir_counter', $parameters['redir_counter']
162
                ];
163
164
                $body = static::getResponse($redir_params);
165
            }
166
        }
167
168
        return $body;
169
    }
170
171
    public static function getIpAddress($to_int = false)
172
    {
173
        $ips = [];
174
175
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
176
            foreach (array_reverse(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])) as $x_ip) {
177
                $ips[] = trim($x_ip);
178
            }
179
        }
180
181
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
182
            $ips[] = trim($_SERVER['HTTP_CLIENT_IP']);
183
        }
184
185
        if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
186
            $ips[] = trim($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']);
187
        }
188
189
        if (isset($_SERVER['HTTP_PROXY_USER'])) {
190
            $ips[] = trim($_SERVER['HTTP_PROXY_USER']);
191
        }
192
193
        if (isset($_SERVER['REMOTE_ADDR'])) {
194
            $ips[] = trim($_SERVER['REMOTE_ADDR']);
195
        }
196
197
        $ip = '0.0.0.0';
198
199
        foreach ($ips as $req_ip) {
200
            if (Is::ip_address($req_ip)) {
201
                $ip = $req_ip;
202
203
                break;
204
            }
205
        }
206
207
        if ($to_int === true) {
208
            $ip = sprintf('%u', ip2long($ip));
209
        }
210
211
        return $ip;
212
    }
213
}
214