HTTP::getResponse()   F
last analyzed

Complexity

Conditions 32
Paths > 20000

Size

Total Lines 138
Code Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 32
eloc 79
nc 2229120
nop 1
dl 0
loc 138
rs 2
c 0
b 0
f 0

How to fix   Long Method    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 MIT; https://www.oscommerce.com/license/mit.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
            $verify_ssl = (defined('OSCOM_HTTP_VERIFY_SSL') && (OSCOM_HTTP_VERIFY_SSL === 'True')) ? true : false;
92
93
            if (isset($parameters['verify_ssl']) && is_bool($parameters['verify_ssl'])) {
94
                $verify_ssl = $parameters['verify_ssl'];
95
            }
96
97
            if ($verify_ssl === true) {
98
                $curl_options[CURLOPT_SSL_VERIFYPEER] = true;
99
                $curl_options[CURLOPT_SSL_VERIFYHOST] = 2;
100
            } else {
101
                $curl_options[CURLOPT_SSL_VERIFYPEER] = false;
102
                $curl_options[CURLOPT_SSL_VERIFYHOST] = false;
103
            }
104
105
            if (!isset($parameters['cafile'])) {
106
                $parameters['cafile'] = OSCOM::getConfig('dir_root', 'Shop') . 'includes/cacert.pem';
107
            }
108
109
            if (is_file($parameters['cafile'])) {
110
                $curl_options[CURLOPT_CAINFO] = $parameters['cafile'];
111
            }
112
113
            if (isset($parameters['certificate'])) {
114
                $curl_options[CURLOPT_SSLCERT] = $parameters['certificate'];
115
            }
116
        }
117
118
        if ($parameters['method'] == 'post') {
119
            if (!isset($parameters['parameters'])) {
120
                $parameters['parameters'] = '';
121
            }
122
123
            $curl_options[CURLOPT_POST] = true;
124
            $curl_options[CURLOPT_POSTFIELDS] = $parameters['parameters'];
125
        }
126
127
        $proxy = defined('OSCOM_HTTP_PROXY') ? OSCOM_HTTP_PROXY : '';
128
129
        if (isset($parameters['proxy'])) {
130
            $proxy = $parameters['proxy'];
131
        }
132
133
        if (!empty($proxy)) {
134
            $curl_options[CURLOPT_HTTPPROXYTUNNEL] = true;
135
            $curl_options[CURLOPT_PROXY] = $proxy;
136
        }
137
138
        curl_setopt_array($curl, $curl_options);
139
        $result = curl_exec($curl);
140
141
        if ($result === false) {
142
            trigger_error(curl_error($curl));
143
144
            curl_close($curl);
145
146
            return false;
147
        }
148
149
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
150
151
        $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
152
        $headers = trim(substr($result, 0, $header_size));
153
        $body = substr($result, $header_size);
154
155
        curl_close($curl);
156
157
        if (($http_code == 301) || ($http_code == 302)) {
158
            if (!isset($parameters['redir_counter']) || ($parameters['redir_counter'] < 6)) {
159
                if (!isset($parameters['redir_counter'])) {
160
                    $parameters['redir_counter'] = 0;
161
                }
162
163
                $matches = [];
164
                preg_match('/(Location:|URI:)(.*?)\n/i', $headers, $matches);
165
166
                $redir_url = trim(array_pop($matches));
167
168
                $parameters['redir_counter']++;
169
170
                $redir_params = [
171
                    'url' => $redir_url,
172
                    'method' => $parameters['method'],
173
                    'redir_counter', $parameters['redir_counter']
174
                ];
175
176
                $body = static::getResponse($redir_params);
177
            }
178
        }
179
180
        return $body;
181
    }
182
183
    public static function getIpAddress($to_int = false)
184
    {
185
        $ips = [];
186
187
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
188
            foreach (array_reverse(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])) as $x_ip) {
189
                $ips[] = trim($x_ip);
190
            }
191
        }
192
193
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
194
            $ips[] = trim($_SERVER['HTTP_CLIENT_IP']);
195
        }
196
197
        if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
198
            $ips[] = trim($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']);
199
        }
200
201
        if (isset($_SERVER['HTTP_PROXY_USER'])) {
202
            $ips[] = trim($_SERVER['HTTP_PROXY_USER']);
203
        }
204
205
        if (isset($_SERVER['REMOTE_ADDR'])) {
206
            $ips[] = trim($_SERVER['REMOTE_ADDR']);
207
        }
208
209
        $ip = '0.0.0.0';
210
211
        foreach ($ips as $req_ip) {
212
            if (Is::ip_address($req_ip)) {
213
                $ip = $req_ip;
214
215
                break;
216
            }
217
        }
218
219
        if ($to_int === true) {
220
            $ip = sprintf('%u', ip2long($ip));
221
        }
222
223
        return $ip;
224
    }
225
}
226