1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Download via HTTP.
|
6 |
|
|
*
|
7 |
|
|
* Support caching, HTTP Basic Authentication, detection of RSS/Atom feeds,
|
8 |
|
|
* redirects.
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* PCRE for finding the link tags in html.
|
13 |
|
|
*/
|
14 |
|
|
define('HTTP_REQUEST_PCRE_LINK_TAG', '/<link((?:[\x09\x0A\x0B\x0C\x0D\x20]+[^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?)*)[\x09\x0A\x0B\x0C\x0D\x20]*(>(.*)<\/link>|(\/)?>)/si');
|
15 |
|
|
|
16 |
|
|
/**
|
17 |
|
|
* PCRE for matching all the attributes in a tag.
|
18 |
|
|
*/
|
19 |
|
|
define('HTTP_REQUEST_PCRE_TAG_ATTRIBUTES', '/[\x09\x0A\x0B\x0C\x0D\x20]+([^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3D\x3E]*)(?:[\x09\x0A\x0B\x0C\x0D\x20]*=[\x09\x0A\x0B\x0C\x0D\x20]*(?:"([^"]*)"|\'([^\']*)\'|([^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?/');
|
20 |
|
|
|
21 |
|
|
/**
|
22 |
|
|
* For cUrl specific errors.
|
23 |
|
|
*/
|
24 |
|
|
class HRCurlException extends Exception {}
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* Discovers RSS or atom feeds at the given URL.
|
28 |
|
|
*
|
29 |
|
|
* If document in given URL is an HTML document, function attempts to discover
|
30 |
|
|
* RSS or Atom feeds.
|
31 |
|
|
*
|
32 |
|
|
* @param string $url
|
33 |
|
|
* The url of the feed to retrieve.
|
34 |
|
|
* @param array $settings
|
35 |
|
|
* An optional array of settings. Valid options are: accept_invalid_cert.
|
36 |
|
|
*
|
37 |
|
|
* @return bool|string
|
38 |
|
|
* The discovered feed, or FALSE if the URL is not reachable or there was an
|
39 |
|
|
* error.
|
40 |
|
|
*/
|
41 |
41cc1b08
|
Assos Assos
|
function http_request_get_common_syndication($url, $settings = array()) {
|
42 |
85ad3d82
|
Assos Assos
|
|
43 |
|
|
$accept_invalid_cert = isset($settings['accept_invalid_cert']) ? $settings['accept_invalid_cert'] : FALSE;
|
44 |
|
|
$download = http_request_get($url, NULL, NULL, $accept_invalid_cert);
|
45 |
|
|
|
46 |
|
|
// Cannot get the feed, return.
|
47 |
|
|
// http_request_get() always returns 200 even if its 304.
|
48 |
|
|
if ($download->code != 200) {
|
49 |
|
|
return FALSE;
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
// Drop the data into a seperate variable so all manipulations of the html
|
53 |
|
|
// will not effect the actual object that exists in the static cache.
|
54 |
|
|
// @see http_request_get.
|
55 |
|
|
$downloaded_string = $download->data;
|
56 |
|
|
// If this happens to be a feed then just return the url.
|
57 |
2c8c2b87
|
Assos Assos
|
if (isset($download->headers['content-type']) && http_request_is_feed($download->headers['content-type'], $downloaded_string)) {
|
58 |
85ad3d82
|
Assos Assos
|
return $url;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
$discovered_feeds = http_request_find_feeds($downloaded_string);
|
62 |
|
|
foreach ($discovered_feeds as $feed_url) {
|
63 |
|
|
$absolute = http_request_create_absolute_url($feed_url, $url);
|
64 |
|
|
if (!empty($absolute)) {
|
65 |
|
|
// @TODO: something more intelligent?
|
66 |
|
|
return $absolute;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Get the content from the given URL.
|
73 |
|
|
*
|
74 |
|
|
* @param string $url
|
75 |
|
|
* A valid URL (not only web URLs).
|
76 |
|
|
* @param string $username
|
77 |
|
|
* If the URL uses authentication, supply the username.
|
78 |
|
|
* @param string $password
|
79 |
|
|
* If the URL uses authentication, supply the password.
|
80 |
|
|
* @param bool $accept_invalid_cert
|
81 |
|
|
* Whether to accept invalid certificates.
|
82 |
|
|
* @param integer $timeout
|
83 |
|
|
* Timeout in seconds to wait for an HTTP get request to finish.
|
84 |
|
|
*
|
85 |
|
|
* @return stdClass
|
86 |
|
|
* An object that describes the data downloaded from $url.
|
87 |
|
|
*/
|
88 |
|
|
function http_request_get($url, $username = NULL, $password = NULL, $accept_invalid_cert = FALSE, $timeout = NULL) {
|
89 |
|
|
// Intra-pagedownload cache, avoid to download the same content twice within
|
90 |
|
|
// one page download (it's possible, compatible and parse calls).
|
91 |
|
|
static $download_cache = array();
|
92 |
|
|
if (isset($download_cache[$url])) {
|
93 |
|
|
return $download_cache[$url];
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
// Determine request timeout.
|
97 |
|
|
$request_timeout = !empty($timeout) ? $timeout : variable_get('http_request_timeout', 30);
|
98 |
|
|
|
99 |
|
|
if (!$username && valid_url($url, TRUE)) {
|
100 |
|
|
// Handle password protected feeds.
|
101 |
|
|
$url_parts = parse_url($url);
|
102 |
|
|
if (!empty($url_parts['user'])) {
|
103 |
41cc1b08
|
Assos Assos
|
$password = urldecode($url_parts['pass']);
|
104 |
|
|
$username = urldecode($url_parts['user']);
|
105 |
85ad3d82
|
Assos Assos
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
$curl = http_request_use_curl();
|
109 |
|
|
|
110 |
|
|
// Only download and parse data if really needs refresh.
|
111 |
|
|
// Based on "Last-Modified" and "If-Modified-Since".
|
112 |
|
|
$headers = array();
|
113 |
41cc1b08
|
Assos Assos
|
if ($cache = http_request_get_cache($url)) {
|
114 |
85ad3d82
|
Assos Assos
|
$last_result = $cache->data;
|
115 |
|
|
$last_headers = array_change_key_case($last_result->headers);
|
116 |
|
|
|
117 |
|
|
if (!empty($last_headers['etag'])) {
|
118 |
|
|
if ($curl) {
|
119 |
|
|
$headers[] = 'If-None-Match: ' . $last_headers['etag'];
|
120 |
|
|
}
|
121 |
|
|
else {
|
122 |
|
|
$headers['If-None-Match'] = $last_headers['etag'];
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
if (!empty($last_headers['last-modified'])) {
|
126 |
|
|
if ($curl) {
|
127 |
|
|
$headers[] = 'If-Modified-Since: ' . $last_headers['last-modified'];
|
128 |
|
|
}
|
129 |
|
|
else {
|
130 |
|
|
$headers['If-Modified-Since'] = $last_headers['last-modified'];
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
if (!empty($username) && !$curl) {
|
134 |
|
|
$headers['Authorization'] = 'Basic ' . base64_encode("$username:$password");
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
// Support the 'feed' and 'webcal' schemes by converting them into 'http'.
|
139 |
|
|
$url = strtr($url, array('feed://' => 'http://', 'webcal://' => 'http://'));
|
140 |
|
|
|
141 |
|
|
if ($curl) {
|
142 |
|
|
$headers[] = 'User-Agent: Drupal (+http://drupal.org/)';
|
143 |
|
|
$result = new stdClass();
|
144 |
|
|
$result->headers = array();
|
145 |
|
|
|
146 |
|
|
// Parse the URL and make sure we can handle the schema.
|
147 |
|
|
// cURL can only support either http:// or https://.
|
148 |
|
|
// CURLOPT_PROTOCOLS is only supported with cURL 7.19.4
|
149 |
|
|
$uri = parse_url($url);
|
150 |
|
|
if (!isset($uri['scheme'])) {
|
151 |
|
|
$result->error = 'missing schema';
|
152 |
|
|
$result->code = -1002;
|
153 |
|
|
}
|
154 |
|
|
else {
|
155 |
|
|
switch ($uri['scheme']) {
|
156 |
|
|
case 'http':
|
157 |
|
|
case 'https':
|
158 |
|
|
// Valid scheme.
|
159 |
|
|
break;
|
160 |
|
|
|
161 |
|
|
default:
|
162 |
|
|
$result->error = 'invalid schema ' . $uri['scheme'];
|
163 |
|
|
$result->code = -1003;
|
164 |
|
|
break;
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
// If the scheme was valid, continue to request the feed using cURL.
|
169 |
|
|
if (empty($result->error)) {
|
170 |
|
|
$download = curl_init($url);
|
171 |
|
|
curl_setopt($download, CURLOPT_FOLLOWLOCATION, TRUE);
|
172 |
|
|
if (!empty($username)) {
|
173 |
|
|
curl_setopt($download, CURLOPT_USERPWD, "{$username}:{$password}");
|
174 |
|
|
curl_setopt($download, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
175 |
|
|
}
|
176 |
|
|
curl_setopt($download, CURLOPT_HTTPHEADER, $headers);
|
177 |
|
|
curl_setopt($download, CURLOPT_HEADER, TRUE);
|
178 |
|
|
curl_setopt($download, CURLOPT_RETURNTRANSFER, TRUE);
|
179 |
|
|
curl_setopt($download, CURLOPT_ENCODING, '');
|
180 |
|
|
curl_setopt($download, CURLOPT_TIMEOUT, $request_timeout);
|
181 |
41cc1b08
|
Assos Assos
|
|
182 |
|
|
$proxy_server = variable_get('proxy_server');
|
183 |
|
|
|
184 |
|
|
if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
|
185 |
|
|
curl_setopt($download, CURLOPT_PROXY, $proxy_server);
|
186 |
|
|
curl_setopt($download, CURLOPT_PROXYPORT, variable_get('proxy_port', 8080));
|
187 |
|
|
|
188 |
|
|
// Proxy user/password.
|
189 |
|
|
if ($proxy_username = variable_get('proxy_username')) {
|
190 |
|
|
$username_password = $proxy_username . ':' . variable_get('proxy_password', '');
|
191 |
|
|
|
192 |
|
|
curl_setopt($download, CURLOPT_PROXYUSERPWD, $username_password);
|
193 |
|
|
curl_setopt($download, CURLOPT_PROXYAUTH, variable_get('proxy_auth_method', CURLAUTH_BASIC));
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
85ad3d82
|
Assos Assos
|
if ($accept_invalid_cert) {
|
198 |
|
|
curl_setopt($download, CURLOPT_SSL_VERIFYPEER, 0);
|
199 |
|
|
}
|
200 |
|
|
$header = '';
|
201 |
|
|
$data = curl_exec($download);
|
202 |
|
|
if (curl_error($download)) {
|
203 |
|
|
throw new HRCurlException(
|
204 |
|
|
t('cURL error (@code) @error for @url', array(
|
205 |
|
|
'@code' => curl_errno($download),
|
206 |
|
|
'@error' => curl_error($download),
|
207 |
|
|
'@url' => $url,
|
208 |
|
|
)), curl_errno($download)
|
209 |
|
|
);
|
210 |
|
|
}
|
211 |
|
|
|
212 |
41cc1b08
|
Assos Assos
|
// When using a proxy, remove extra data from the header which is not
|
213 |
|
|
// considered by CURLINFO_HEADER_SIZE (possibly cURL bug).
|
214 |
|
|
// This data is only added when to HTTP header when working with a proxy.
|
215 |
|
|
// Example string added: <HTTP/1.0 200 Connection established\r\n\r\n>
|
216 |
|
|
// This was fixed in libcurl version 7.30.0 (0x71e00) (April 12, 2013),
|
217 |
|
|
// so this workaround only removes the proxy-added headers if we are using
|
218 |
|
|
// an older version of libcurl.
|
219 |
|
|
$curl_ver = curl_version();
|
220 |
|
|
|
221 |
|
|
if ($proxy_server && $curl_ver['version_number'] < 0x71e00 && _drupal_http_use_proxy($uri['host'])) {
|
222 |
|
|
$http_header_break = "\r\n\r\n";
|
223 |
|
|
$response = explode($http_header_break, $data);
|
224 |
|
|
if (count($response) > 2) {
|
225 |
|
|
$data = substr($data, strlen($response[0] . $http_header_break), strlen($data));
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
|
229 |
85ad3d82
|
Assos Assos
|
$header_size = curl_getinfo($download, CURLINFO_HEADER_SIZE);
|
230 |
|
|
$header = substr($data, 0, $header_size - 1);
|
231 |
|
|
$result->data = substr($data, $header_size);
|
232 |
|
|
$headers = preg_split("/(\r\n){2}/", $header);
|
233 |
|
|
$header_lines = preg_split("/\r\n|\n|\r/", end($headers));
|
234 |
|
|
// Skip HTTP response status.
|
235 |
|
|
array_shift($header_lines);
|
236 |
|
|
|
237 |
|
|
while ($line = trim(array_shift($header_lines))) {
|
238 |
|
|
list($header, $value) = explode(':', $line, 2);
|
239 |
|
|
// Normalize the headers.
|
240 |
|
|
$header = strtolower($header);
|
241 |
|
|
|
242 |
|
|
if (isset($result->headers[$header]) && $header == 'set-cookie') {
|
243 |
|
|
// RFC 2109: the Set-Cookie response header comprises the token Set-
|
244 |
|
|
// Cookie:, followed by a comma-separated list of one or more cookies.
|
245 |
|
|
$result->headers[$header] .= ',' . trim($value);
|
246 |
|
|
}
|
247 |
|
|
else {
|
248 |
|
|
$result->headers[$header] = trim($value);
|
249 |
|
|
}
|
250 |
|
|
}
|
251 |
|
|
$result->code = curl_getinfo($download, CURLINFO_HTTP_CODE);
|
252 |
|
|
|
253 |
|
|
curl_close($download);
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
else {
|
257 |
|
|
$result = drupal_http_request($url, array('headers' => $headers, 'timeout' => $request_timeout));
|
258 |
|
|
$result->headers = isset($result->headers) ? $result->headers : array();
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
$result->code = isset($result->code) ? $result->code : 200;
|
262 |
|
|
|
263 |
|
|
// In case of 304 Not Modified try to return cached data.
|
264 |
|
|
if ($result->code == 304) {
|
265 |
|
|
|
266 |
|
|
if (isset($last_result)) {
|
267 |
|
|
$last_result->from_cache = TRUE;
|
268 |
|
|
return $last_result;
|
269 |
|
|
}
|
270 |
|
|
else {
|
271 |
|
|
// It's a tragedy, this file must exist and contain good data.
|
272 |
|
|
// In this case, clear cache and repeat.
|
273 |
41cc1b08
|
Assos Assos
|
http_request_clear_cache($url);
|
274 |
85ad3d82
|
Assos Assos
|
return http_request_get($url, $username, $password, $accept_invalid_cert, $request_timeout);
|
275 |
|
|
}
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
// Set caches.
|
279 |
41cc1b08
|
Assos Assos
|
http_request_set_cache($url, $result);
|
280 |
85ad3d82
|
Assos Assos
|
$download_cache[$url] = $result;
|
281 |
|
|
|
282 |
|
|
return $result;
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/**
|
286 |
|
|
* Decides if it's possible to use cURL or not.
|
287 |
|
|
*
|
288 |
|
|
* @return bool
|
289 |
41cc1b08
|
Assos Assos
|
* TRUE if cURL may be used, FALSE otherwise.
|
290 |
85ad3d82
|
Assos Assos
|
*/
|
291 |
|
|
function http_request_use_curl() {
|
292 |
|
|
// Allow site administrators to choose to not use cURL.
|
293 |
|
|
if (variable_get('feeds_never_use_curl', FALSE)) {
|
294 |
|
|
return FALSE;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
41cc1b08
|
Assos Assos
|
// Check that the PHP cURL extension has been enabled.
|
298 |
|
|
if (!extension_loaded('curl')) {
|
299 |
|
|
return FALSE;
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
// cURL below PHP 5.6.0 must not have open_basedir or safe_mode enabled.
|
303 |
|
|
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
|
304 |
|
|
return !ini_get('safe_mode') && !ini_get('open_basedir');
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
// cURL in PHP 5.6.0 and above re-enables CURLOPT_FOLLOWLOCATION with
|
308 |
|
|
// open_basedir so there is no need to check for this.
|
309 |
|
|
return TRUE;
|
310 |
85ad3d82
|
Assos Assos
|
}
|
311 |
|
|
|
312 |
|
|
/**
|
313 |
|
|
* Clear cache for a specific URL.
|
314 |
41cc1b08
|
Assos Assos
|
*
|
315 |
|
|
* @param string $url
|
316 |
|
|
* The URL to clear.
|
317 |
85ad3d82
|
Assos Assos
|
*/
|
318 |
|
|
function http_request_clear_cache($url) {
|
319 |
41cc1b08
|
Assos Assos
|
cache_clear_all(hash('sha256', $url), 'cache_feeds_http');
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
/**
|
323 |
|
|
* Gets the cache for a specific URL.
|
324 |
|
|
*
|
325 |
|
|
* @param string $url
|
326 |
|
|
* The URL to find the cached item.
|
327 |
|
|
*
|
328 |
|
|
* @return object|false
|
329 |
|
|
* The cache or FALSE on failure.
|
330 |
|
|
*/
|
331 |
|
|
function http_request_get_cache($url) {
|
332 |
|
|
return cache_get(hash('sha256', $url), 'cache_feeds_http');
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
/**
|
336 |
|
|
* Sets the cache for a specific URL.
|
337 |
|
|
*
|
338 |
|
|
* @param string $url
|
339 |
|
|
* The URL to cache.
|
340 |
|
|
* @param stdClass $result
|
341 |
|
|
* The result of the HTTP request.
|
342 |
|
|
*/
|
343 |
|
|
function http_request_set_cache($url, stdClass $result) {
|
344 |
|
|
cache_set(hash('sha256', $url), $result, 'cache_feeds_http');
|
345 |
85ad3d82
|
Assos Assos
|
}
|
346 |
|
|
|
347 |
|
|
/**
|
348 |
|
|
* Returns if the provided $content_type is a feed.
|
349 |
|
|
*
|
350 |
|
|
* @param string $content_type
|
351 |
|
|
* The Content-Type header.
|
352 |
|
|
*
|
353 |
|
|
* @param string $data
|
354 |
|
|
* The actual data from the http request.
|
355 |
|
|
*
|
356 |
|
|
* @return bool
|
357 |
|
|
* Returns TRUE if this is a parsable feed.
|
358 |
|
|
*/
|
359 |
|
|
function http_request_is_feed($content_type, $data) {
|
360 |
|
|
$pos = strpos($content_type, ';');
|
361 |
|
|
if ($pos !== FALSE) {
|
362 |
|
|
$content_type = substr($content_type, 0, $pos);
|
363 |
|
|
}
|
364 |
|
|
$content_type = strtolower($content_type);
|
365 |
|
|
if (strpos($content_type, 'xml') !== FALSE) {
|
366 |
|
|
return TRUE;
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
// @TODO: Sometimes the content-type can be text/html but still be a valid
|
370 |
|
|
// feed.
|
371 |
|
|
return FALSE;
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
/**
|
375 |
|
|
* Finds potential feed tags in the HTML document.
|
376 |
|
|
*
|
377 |
|
|
* @param string $html
|
378 |
|
|
* The html string to search.
|
379 |
|
|
*
|
380 |
|
|
* @return array
|
381 |
|
|
* An array of href to feeds.
|
382 |
|
|
*/
|
383 |
|
|
function http_request_find_feeds($html) {
|
384 |
|
|
$matches = array();
|
385 |
|
|
preg_match_all(HTTP_REQUEST_PCRE_LINK_TAG, $html, $matches);
|
386 |
|
|
$links = $matches[1];
|
387 |
|
|
$valid_links = array();
|
388 |
|
|
|
389 |
|
|
// Build up all the links information.
|
390 |
|
|
foreach ($links as $link_tag) {
|
391 |
|
|
$attributes = array();
|
392 |
|
|
$candidate = array();
|
393 |
|
|
|
394 |
|
|
preg_match_all(HTTP_REQUEST_PCRE_TAG_ATTRIBUTES, $link_tag, $attributes, PREG_SET_ORDER);
|
395 |
|
|
foreach ($attributes as $attribute) {
|
396 |
|
|
// Find the key value pairs, attribute[1] is key and attribute[2] is the
|
397 |
41cc1b08
|
Assos Assos
|
// value. However, if the link tag used single quotes, the value might
|
398 |
|
|
// be in attribute[3] instead.
|
399 |
|
|
if (empty($attribute[2])) {
|
400 |
|
|
$attribute[2] = $attribute[3];
|
401 |
|
|
}
|
402 |
85ad3d82
|
Assos Assos
|
if (!empty($attribute[1]) && !empty($attribute[2])) {
|
403 |
|
|
$candidate[drupal_strtolower($attribute[1])] = drupal_strtolower(decode_entities($attribute[2]));
|
404 |
|
|
}
|
405 |
|
|
}
|
406 |
|
|
|
407 |
|
|
// Examine candidate to see if it s a feed.
|
408 |
|
|
// @TODO: could/should use http_request_is_feed ??
|
409 |
|
|
if (isset($candidate['rel']) && $candidate['rel'] == 'alternate') {
|
410 |
|
|
if (isset($candidate['href']) && isset($candidate['type']) && strpos($candidate['type'], 'xml') !== FALSE) {
|
411 |
|
|
// All tests pass, its a valid candidate.
|
412 |
|
|
$valid_links[] = $candidate['href'];
|
413 |
|
|
}
|
414 |
|
|
}
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
return $valid_links;
|
418 |
|
|
}
|
419 |
|
|
|
420 |
|
|
/**
|
421 |
|
|
* Create an absolute url.
|
422 |
|
|
*
|
423 |
|
|
* @param string $url
|
424 |
|
|
* The href to transform.
|
425 |
|
|
* @param string $base_url
|
426 |
|
|
* The url to be used as the base for a relative $url.
|
427 |
|
|
*
|
428 |
|
|
* @return string
|
429 |
|
|
* An absolute url
|
430 |
|
|
*/
|
431 |
|
|
function http_request_create_absolute_url($url, $base_url) {
|
432 |
|
|
$url = trim($url);
|
433 |
|
|
if (valid_url($url, TRUE)) {
|
434 |
|
|
// Valid absolute url already.
|
435 |
|
|
return $url;
|
436 |
|
|
}
|
437 |
|
|
|
438 |
|
|
// Turn relative url into absolute.
|
439 |
|
|
if (valid_url($url, FALSE)) {
|
440 |
|
|
// Produces variables $scheme, $host, $user, $pass, $path, $query and
|
441 |
|
|
// $fragment.
|
442 |
|
|
$parsed_url = parse_url($base_url);
|
443 |
|
|
|
444 |
|
|
$path = dirname($parsed_url['path']);
|
445 |
|
|
|
446 |
|
|
// Adding to the existing path.
|
447 |
|
|
if ($url{0} == '/') {
|
448 |
|
|
$cparts = array_filter(explode("/", $url));
|
449 |
|
|
}
|
450 |
|
|
else {
|
451 |
|
|
// Backtracking from the existing path.
|
452 |
|
|
$cparts = array_merge(array_filter(explode("/", $path)), array_filter(explode("/", $url)));
|
453 |
|
|
foreach ($cparts as $i => $part) {
|
454 |
|
|
if ($part == '.') {
|
455 |
|
|
$cparts[$i] = NULL;
|
456 |
|
|
}
|
457 |
|
|
if ($part == '..') {
|
458 |
|
|
$cparts[$i - 1] = NULL;
|
459 |
|
|
$cparts[$i] = NULL;
|
460 |
|
|
}
|
461 |
|
|
}
|
462 |
|
|
$cparts = array_filter($cparts);
|
463 |
|
|
}
|
464 |
|
|
$path = implode("/", $cparts);
|
465 |
|
|
|
466 |
|
|
// Build the prefix to the path.
|
467 |
|
|
$absolute_url = '';
|
468 |
|
|
if (isset($parsed_url['scheme'])) {
|
469 |
|
|
$absolute_url = $parsed_url['scheme'] . '://';
|
470 |
|
|
}
|
471 |
|
|
|
472 |
|
|
if (isset($parsed_url['user'])) {
|
473 |
|
|
$absolute_url .= $parsed_url['user'];
|
474 |
|
|
if (isset($pass)) {
|
475 |
|
|
$absolute_url .= ':' . $parsed_url['pass'];
|
476 |
|
|
}
|
477 |
|
|
$absolute_url .= '@';
|
478 |
|
|
}
|
479 |
|
|
if (isset($parsed_url['host'])) {
|
480 |
|
|
$absolute_url .= $parsed_url['host'] . '/';
|
481 |
|
|
}
|
482 |
|
|
|
483 |
|
|
$absolute_url .= $path;
|
484 |
|
|
|
485 |
|
|
if (valid_url($absolute_url, TRUE)) {
|
486 |
|
|
return $absolute_url;
|
487 |
|
|
}
|
488 |
|
|
}
|
489 |
|
|
return FALSE;
|
490 |
|
|
} |