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