1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Http Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Http\Transport; |
10
|
|
|
|
11
|
|
|
use Composer\CaBundle\CaBundle; |
12
|
|
|
use Joomla\Http\Exception\InvalidResponseCodeException; |
13
|
|
|
use Joomla\Http\TransportInterface; |
14
|
|
|
use Joomla\Http\Response; |
15
|
|
|
use Joomla\Uri\Uri; |
16
|
|
|
use Joomla\Uri\UriInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* HTTP transport class for using PHP streams. |
20
|
|
|
* |
21
|
|
|
* @since 1.0 |
22
|
|
|
*/ |
23
|
|
|
class Stream implements TransportInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The client options. |
27
|
|
|
* |
28
|
|
|
* @var array|\ArrayAccess |
29
|
|
|
* @since 1.0 |
30
|
|
|
*/ |
31
|
|
|
protected $options; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param array|\ArrayAccess $options Client options array. |
37
|
|
|
* |
38
|
|
|
* @since 1.0 |
39
|
|
|
* @throws \RuntimeException |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function __construct($options = array()) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
// Verify that fopen() is available. |
44
|
|
|
if (!self::isSupported()) |
45
|
|
|
{ |
46
|
|
|
throw new \RuntimeException('Cannot use a stream transport when fopen() is not available.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Verify that URLs can be used with fopen(); |
50
|
|
|
if (!ini_get('allow_url_fopen')) |
51
|
|
|
{ |
52
|
|
|
throw new \RuntimeException('Cannot use a stream transport when "allow_url_fopen" is disabled.'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!is_array($options) && !($options instanceof \ArrayAccess)) |
56
|
|
|
{ |
57
|
|
|
throw new \InvalidArgumentException( |
58
|
|
|
'The options param must be an array or implement the ArrayAccess interface.' |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->options = $options; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Send a request to the server and return a Response object with the response. |
67
|
|
|
* |
68
|
|
|
* @param string $method The HTTP method for sending the request. |
69
|
|
|
* @param UriInterface $uri The URI to the resource to request. |
70
|
|
|
* @param mixed $data Either an associative array or a string to be sent with the request. |
71
|
|
|
* @param array $headers An array of request headers to send with the request. |
72
|
|
|
* @param integer $timeout Read timeout in seconds. |
73
|
|
|
* @param string $userAgent The optional user agent string to send with the request. |
74
|
|
|
* |
75
|
|
|
* @return Response |
76
|
|
|
* |
77
|
|
|
* @since 1.0 |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
*/ |
80
|
|
|
public function request($method, UriInterface $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null) |
81
|
|
|
{ |
82
|
|
|
// Create the stream context options array with the required method offset. |
83
|
|
|
$options = array('method' => strtoupper($method)); |
84
|
|
|
|
85
|
|
|
// If data exists let's encode it and make sure our Content-Type header is set. |
86
|
|
|
if (isset($data)) |
87
|
|
|
{ |
88
|
|
|
// If the data is a scalar value simply add it to the stream context options. |
89
|
|
|
if (is_scalar($data)) |
90
|
|
|
{ |
91
|
|
|
$options['content'] = $data; |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
// Otherwise we need to encode the value first. |
95
|
|
|
{ |
96
|
|
|
$options['content'] = http_build_query($data); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!isset($headers['Content-Type'])) |
100
|
|
|
{ |
101
|
|
|
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Add the relevant headers. |
105
|
|
|
$headers['Content-Length'] = strlen($options['content']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// If an explicit timeout is given user it. |
109
|
|
|
if (isset($timeout)) |
110
|
|
|
{ |
111
|
|
|
$options['timeout'] = (int) $timeout; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// If an explicit user agent is given use it. |
115
|
|
|
if (isset($userAgent)) |
116
|
|
|
{ |
117
|
|
|
$options['user_agent'] = $userAgent; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Ignore HTTP errors so that we can capture them. |
121
|
|
|
$options['ignore_errors'] = 1; |
122
|
|
|
|
123
|
|
|
// Follow redirects. |
124
|
|
|
$options['follow_location'] = isset($this->options['follow_location']) ? (int) $this->options['follow_location'] : 1; |
125
|
|
|
|
126
|
|
|
// Add the proxy configuration if enabled |
127
|
|
|
$proxyEnabled = isset($this->options['proxy.enabled']) ? (bool) $this->options['proxy.enabled'] : false; |
128
|
|
|
|
129
|
|
|
if ($proxyEnabled) |
130
|
|
|
{ |
131
|
|
|
$options['request_fulluri'] = true; |
132
|
|
|
|
133
|
|
|
if (isset($this->options['proxy.host']) && isset($this->options['proxy.port'])) |
134
|
|
|
{ |
135
|
|
|
$options['proxy'] = $this->options['proxy.host'] . ':' . (int) $this->options['proxy.port']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// If authentication details are provided, add those as well |
139
|
|
View Code Duplication |
if (isset($this->options['proxy.user']) && isset($this->options['proxy.password'])) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$headers['Proxy-Authorization'] = 'Basic ' . base64_encode($this->options['proxy.user'] . ':' . $this->options['proxy.password']); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Build the headers string for the request. |
146
|
|
|
$headerString = null; |
147
|
|
|
|
148
|
|
|
if (isset($headers)) |
149
|
|
|
{ |
150
|
|
|
foreach ($headers as $key => $value) |
151
|
|
|
{ |
152
|
|
|
$headerString .= $key . ': ' . $value . "\r\n"; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Add the headers string into the stream context options array. |
156
|
|
|
$options['header'] = trim($headerString, "\r\n"); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// Authentication, if needed |
160
|
|
|
if ($uri instanceof Uri && isset($this->options['userauth']) && isset($this->options['passwordauth'])) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$uri->setUser($this->options['userauth']); |
163
|
|
|
$uri->setPass($this->options['passwordauth']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// Set any custom transport options |
167
|
|
|
if (isset($this->options['transport.stream'])) |
168
|
|
|
{ |
169
|
|
|
foreach ($this->options['transport.stream'] as $key => $value) |
170
|
|
|
{ |
171
|
|
|
$options[$key] = $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Get the current context options. |
176
|
|
|
$contextOptions = stream_context_get_options(stream_context_get_default()); |
177
|
|
|
|
178
|
|
|
// Add our options to the currently defined options, if any. |
179
|
|
|
$contextOptions['http'] = isset($contextOptions['http']) ? array_merge($contextOptions['http'], $options) : $options; |
180
|
|
|
|
181
|
|
|
// Create the stream context for the request. |
182
|
|
|
$streamOptions = array( |
183
|
|
|
'http' => $options, |
184
|
|
|
'ssl' => array( |
185
|
|
|
'verify_peer' => true, |
186
|
|
|
'verify_depth' => 5, |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
// The cacert may be a file or path |
191
|
|
|
$certpath = isset($this->options['stream.certpath']) ? $this->options['stream.certpath'] : CaBundle::getSystemCaRootBundlePath(); |
192
|
|
|
|
193
|
|
|
if (is_dir($certpath)) |
194
|
|
|
{ |
195
|
|
|
$streamOptions['ssl']['capath'] = $certpath; |
196
|
|
|
} |
197
|
|
|
else |
198
|
|
|
{ |
199
|
|
|
$streamOptions['ssl']['cafile'] = $certpath; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$context = stream_context_create($streamOptions); |
203
|
|
|
|
204
|
|
|
// Capture PHP errors |
205
|
|
|
$php_errormsg = ''; |
206
|
|
|
$track_errors = ini_get('track_errors'); |
207
|
|
|
ini_set('track_errors', true); |
208
|
|
|
|
209
|
|
|
// Open the stream for reading. |
210
|
|
|
$stream = @fopen((string) $uri, 'r', false, $context); |
211
|
|
|
|
212
|
|
View Code Duplication |
if (!$stream) |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
if (!$php_errormsg) |
215
|
|
|
{ |
216
|
|
|
// Error but nothing from php? Create our own |
217
|
|
|
// @todo $err and $errno are undefined variables. |
218
|
|
|
$php_errormsg = sprintf('Could not connect to resource: %s', $uri, $err, $errno); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Restore error tracking to give control to the exception handler |
222
|
|
|
ini_set('track_errors', $track_errors); |
223
|
|
|
|
224
|
|
|
throw new \RuntimeException($php_errormsg); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// Restore error tracking to what it was before. |
228
|
|
|
ini_set('track_errors', $track_errors); |
229
|
|
|
|
230
|
|
|
// Get the metadata for the stream, including response headers. |
231
|
|
|
$metadata = stream_get_meta_data($stream); |
232
|
|
|
|
233
|
|
|
// Get the contents from the stream. |
234
|
|
|
$content = stream_get_contents($stream); |
235
|
|
|
|
236
|
|
|
// Close the stream. |
237
|
|
|
fclose($stream); |
238
|
|
|
|
239
|
|
|
if (isset($metadata['wrapper_data']['headers'])) |
240
|
|
|
{ |
241
|
|
|
$headers = $metadata['wrapper_data']['headers']; |
242
|
|
|
} |
243
|
|
|
elseif (isset($metadata['wrapper_data'])) |
244
|
|
|
{ |
245
|
|
|
$headers = $metadata['wrapper_data']; |
246
|
|
|
} |
247
|
|
|
else |
248
|
|
|
{ |
249
|
|
|
$headers = array(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this->getResponse($headers, $content); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Method to get a response object from a server response. |
257
|
|
|
* |
258
|
|
|
* @param array $headers The response headers as an array. |
259
|
|
|
* @param string $body The response body as a string. |
260
|
|
|
* |
261
|
|
|
* @return Response |
262
|
|
|
* |
263
|
|
|
* @since 1.0 |
264
|
|
|
* @throws InvalidResponseCodeException |
265
|
|
|
*/ |
266
|
|
|
protected function getResponse(array $headers, $body) |
267
|
|
|
{ |
268
|
|
|
// Create the response object. |
269
|
|
|
$return = new Response; |
270
|
|
|
|
271
|
|
|
// Set the body for the response. |
272
|
|
|
$return->body = $body; |
273
|
|
|
|
274
|
|
|
// Get the response code from the first offset of the response headers. |
275
|
|
|
preg_match('/[0-9]{3}/', array_shift($headers), $matches); |
276
|
|
|
$code = $matches[0]; |
277
|
|
|
|
278
|
|
|
if (is_numeric($code)) |
279
|
|
|
{ |
280
|
|
|
$return->code = (int) $code; |
281
|
|
|
} |
282
|
|
|
// No valid response code was detected. |
283
|
|
|
else |
284
|
|
|
{ |
285
|
|
|
throw new InvalidResponseCodeException('No HTTP response code found.'); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// Add the response headers to the response object. |
289
|
|
View Code Duplication |
foreach ($headers as $header) |
|
|
|
|
290
|
|
|
{ |
291
|
|
|
$pos = strpos($header, ':'); |
292
|
|
|
$return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1))); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $return; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Method to check if http transport stream available for use |
300
|
|
|
* |
301
|
|
|
* @return boolean True if available else false |
302
|
|
|
* |
303
|
|
|
* @since 1.0 |
304
|
|
|
*/ |
305
|
|
|
public static function isSupported() |
306
|
|
|
{ |
307
|
|
|
return function_exists('fopen') && is_callable('fopen') && ini_get('allow_url_fopen'); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.