6 * GET an HTTP URL to retrieve its content
7 * Uses the cURL library or a fallback method
9 * @param string $url URL to get (http://...)
10 * @param int $timeout network timeout (in seconds)
11 * @param int $maxBytes maximum downloaded bytes (default: 4 MiB)
12 * @param callable|string $curlHeaderFunction Optional callback called during the download of headers
13 * (CURLOPT_HEADERFUNCTION)
14 * @param callable|string $curlWriteFunction Optional callback called during the download (cURL CURLOPT_WRITEFUNCTION).
15 * Can be used to add download conditions on the
16 * headers (response code, content type, etc.).
18 * @return array HTTP response headers, downloaded content
21 * [0] = associative array containing HTTP response headers
22 * [1] = URL content (downloaded data)
25 * list($headers, $data) = get_http_response('http://sebauvage.net/');
26 * if (strpos($headers[0], '200 OK') !== false) {
27 * echo 'Data type: '.htmlspecialchars($headers['Content-Type']);
29 * echo 'There was an error: '.htmlspecialchars($headers[0]);
32 * @see https://secure.php.net/manual/en/ref.curl.php
33 * @see https://secure.php.net/manual/en/functions.anonymous.php
34 * @see https://secure.php.net/manual/en/function.preg-split.php
35 * @see https://secure.php.net/manual/en/function.explode.php
36 * @see http://stackoverflow.com/q/17641073
37 * @see http://stackoverflow.com/q/9183178
38 * @see http://stackoverflow.com/q/1462720
40 function get_http_response(
44 $curlHeaderFunction = null,
45 $curlWriteFunction = null
47 $urlObj = new Url($url);
48 $cleanUrl = $urlObj->idnToAscii();
50 if (!filter_var($cleanUrl, FILTER_VALIDATE_URL
) || !$urlObj->isHttp()) {
51 return array(array(0 => 'Invalid HTTP UrlUtils'), false);
55 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:45.0)'
56 . ' Gecko/20100101 Firefox/45.0';
58 substr(setlocale(LC_COLLATE
, 0), 0, 2) . ',en-US;q=0.7,en;q=0.3';
61 if (!function_exists('curl_init')) {
62 return get_http_response_fallback(
72 $ch = curl_init($cleanUrl);
74 return array(array(0 => 'curl_init() error'), false);
77 // General cURL settings
78 curl_setopt($ch, CURLOPT_AUTOREFERER
, true);
79 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
, true);
80 // Default header download if the $curlHeaderFunction is not defined
81 curl_setopt($ch, CURLOPT_HEADER
, !is_callable($curlHeaderFunction));
85 array('Accept-Language: ' . $acceptLanguage)
87 curl_setopt($ch, CURLOPT_MAXREDIRS
, $maxRedirs);
88 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
89 curl_setopt($ch, CURLOPT_TIMEOUT
, $timeout);
90 curl_setopt($ch, CURLOPT_USERAGENT
, $userAgent);
92 // Max download size management
93 curl_setopt($ch, CURLOPT_BUFFERSIZE
, 1024*16);
94 curl_setopt($ch, CURLOPT_NOPROGRESS
, false);
95 if (is_callable($curlHeaderFunction)) {
96 curl_setopt($ch, CURLOPT_HEADERFUNCTION
, $curlHeaderFunction);
98 if (is_callable($curlWriteFunction)) {
99 curl_setopt($ch, CURLOPT_WRITEFUNCTION
, $curlWriteFunction);
103 CURLOPT_PROGRESSFUNCTION
,
104 function ($arg0, $arg1, $arg2, $arg3, $arg4) use ($maxBytes) {
107 // Non-zero return stops downloading
108 return ($downloaded > $maxBytes) ? 1 : 0;
112 $response = curl_exec($ch);
113 $errorNo = curl_errno($ch);
114 $errorStr = curl_error($ch);
115 $headSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
118 if ($response === false) {
119 if ($errorNo == CURLE_COULDNT_RESOLVE_HOST
) {
121 * Workaround to match fallback method behaviour
122 * Removing this would require updating
123 * GetHttpUrlTest::testGetInvalidRemoteUrl()
125 return array(false, false);
127 return array(array(0 => 'curl_exec() error: ' . $errorStr), false);
130 // Formatting output like the fallback method
131 $rawHeaders = substr($response, 0, $headSize);
133 // Keep only headers from latest redirection
134 $rawHeadersArrayRedirs = explode("\r\n\r\n", trim($rawHeaders));
135 $rawHeadersLastRedir = end($rawHeadersArrayRedirs);
137 $content = substr($response, $headSize);
139 foreach (preg_split('~[\r\n]+~', $rawHeadersLastRedir) as $line) {
140 if (empty($line) || ctype_space($line)) {
143 $splitLine = explode(': ', $line, 2);
144 if (count($splitLine) > 1) {
145 $key = $splitLine[0];
146 $value = $splitLine[1];
147 if (array_key_exists($key, $headers)) {
148 if (!is_array($headers[$key])) {
149 $headers[$key] = array(0 => $headers[$key]);
151 $headers[$key][] = $value;
153 $headers[$key] = $value;
156 $headers[] = $splitLine[0];
160 return array($headers, $content);
164 * GET an HTTP URL to retrieve its content (fallback method)
166 * @param string $cleanUrl URL to get (http://... valid and in ASCII form)
167 * @param int $timeout network timeout (in seconds)
168 * @param int $maxBytes maximum downloaded bytes
169 * @param string $userAgent "User-Agent" header
170 * @param string $acceptLanguage "Accept-Language" header
171 * @param int $maxRedr maximum amount of redirections followed
173 * @return array HTTP response headers, downloaded content
176 * [0] = associative array containing HTTP response headers
177 * [1] = URL content (downloaded data)
179 * @see http://php.net/manual/en/function.file-get-contents.php
180 * @see http://php.net/manual/en/function.stream-context-create.php
181 * @see http://php.net/manual/en/function.get-headers.php
183 function get_http_response_fallback(
194 'timeout' => $timeout,
195 'user_agent' => $userAgent,
196 'header' => "Accept: */*\r\n"
197 . 'Accept-Language: ' . $acceptLanguage
201 stream_context_set_default($options);
202 list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr);
203 if (! $headers || strpos($headers[0], '200 OK') === false) {
204 $options['http']['request_fulluri'] = true;
205 stream_context_set_default($options);
206 list($headers, $finalUrl) = get_redirected_headers($cleanUrl, $maxRedr);
210 return array($headers, false);
214 // TODO: catch Exception in calling code (thumbnailer)
215 $context = stream_context_create($options);
216 $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes);
217 } catch (Exception
$exc) {
218 return array(array(0 => 'HTTP Error'), $exc->getMessage());
221 return array($headers, $content);
225 * Retrieve HTTP headers, following n redirections (temporary and permanent ones).
227 * @param string $url initial URL to reach.
228 * @param int $redirectionLimit max redirection follow.
230 * @return array HTTP headers, or false if it failed.
232 function get_redirected_headers($url, $redirectionLimit = 3)
234 $headers = get_headers($url, 1);
235 if (!empty($headers['location']) && empty($headers['Location'])) {
236 $headers['Location'] = $headers['location'];
239 // Headers found, redirection found, and limit not reached.
240 if ($redirectionLimit-- > 0
242 && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false)
243 && !empty($headers['Location'])) {
244 $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
245 if ($redirection != $url) {
246 $redirection = getAbsoluteUrl($url, $redirection);
247 return get_redirected_headers($redirection, $redirectionLimit);
251 return array($headers, $url);
255 * Get an absolute URL from a complete one, and another absolute/relative URL.
257 * @param string $originalUrl The original complete URL.
258 * @param string $newUrl The new one, absolute or relative.
260 * @return string Final URL:
261 * - $newUrl if it was already an absolute URL.
262 * - if it was relative, absolute URL from $originalUrl path.
264 function getAbsoluteUrl($originalUrl, $newUrl)
266 $newScheme = parse_url($newUrl, PHP_URL_SCHEME
);
267 // Already an absolute URL.
268 if (!empty($newScheme)) {
272 $parts = parse_url($originalUrl);
273 $final = $parts['scheme'] .'://'. $parts['host'];
274 $final .= (!empty($parts['port'])) ? $parts['port'] : '';
276 if ($newUrl[0] != '/') {
277 $final .= substr(ltrim($parts['path'], '/'), 0, strrpos($parts['path'], '/'));
279 $final .= ltrim($newUrl, '/');
284 * Returns the server's base URL: scheme://domain.tld[:port]
286 * @param array $server the $_SERVER array
288 * @return string the server's base URL
290 * @see http://www.ietf.org/rfc/rfc7239.txt
291 * @see http://www.ietf.org/rfc/rfc6648.txt
292 * @see http://stackoverflow.com/a/3561399
293 * @see http://stackoverflow.com/q/452375
295 function server_url($server)
300 // Shaarli is served behind a proxy
301 if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
302 // Keep forwarded scheme
303 if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
304 $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']);
305 $scheme = trim($schemes[0]);
307 $scheme = $server['HTTP_X_FORWARDED_PROTO'];
310 if (isset($server['HTTP_X_FORWARDED_PORT'])) {
311 // Keep forwarded port
312 if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
313 $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
314 $port = trim($ports[0]);
316 $port = $server['HTTP_X_FORWARDED_PORT'];
319 // This is a workaround for proxies that don't forward the scheme properly.
320 // Connecting over port 443 has to be in HTTPS.
321 // See https://github.com/shaarli/Shaarli/issues/1022
322 if ($port == '443') {
326 if (($scheme == 'http' && $port != '80')
327 || ($scheme == 'https' && $port != '443')
335 if (isset($server['HTTP_X_FORWARDED_HOST'])) {
336 // Keep forwarded host
337 if (strpos($server['HTTP_X_FORWARDED_HOST'], ',') !== false) {
338 $hosts = explode(',', $server['HTTP_X_FORWARDED_HOST']);
339 $host = trim($hosts[0]);
341 $host = $server['HTTP_X_FORWARDED_HOST'];
344 $host = $server['SERVER_NAME'];
347 return $scheme.'://'.$host.$port;
351 if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on')
352 || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) {
356 // Do not append standard port values
357 if (($scheme == 'http' && $server['SERVER_PORT'] != '80')
358 || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) {
359 $port = ':'.$server['SERVER_PORT'];
362 return $scheme.'://'.$server['SERVER_NAME'].$port;
366 * Returns the absolute URL of the current script, without the query
368 * If the resource is "index.php", then it is removed (for better-looking URLs)
370 * @param array $server the $_SERVER array
372 * @return string the absolute URL of the current script, without the query
374 function index_url($server)
376 if (defined('SHAARLI_ROOT_URL') && null !== SHAARLI_ROOT_URL
) {
377 return rtrim(SHAARLI_ROOT_URL
, '/') . '/';
380 $scriptname = !empty($server['SCRIPT_NAME']) ? $server['SCRIPT_NAME'] : '/';
381 if (endsWith($scriptname, 'index.php')) {
382 $scriptname = substr($scriptname, 0, -9);
384 return server_url($server) . $scriptname;
388 * Returns the absolute URL of the current script, with current route and query
390 * If the resource is "index.php", then it is removed (for better-looking URLs)
392 * @param array $server the $_SERVER array
394 * @return string the absolute URL of the current script, with the query
396 function page_url($server)
398 $scriptname = $server['SCRIPT_NAME'] ?? '';
399 if (endsWith($scriptname, 'index.php')) {
400 $scriptname = substr($scriptname, 0, -9);
403 $route = preg_replace('@^' . $scriptname . '@', '', $server['REQUEST_URI'] ?? '');
404 if (! empty($server['QUERY_STRING'])) {
405 return index_url($server) . $route . '?' . $server['QUERY_STRING'];
408 return index_url($server) . $route;
412 * Retrieve the initial IP forwarded by the reverse proxy.
414 * Inspired from: https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/RemoteAddress.php
416 * @param array $server $_SERVER array which contains HTTP headers.
417 * @param array $trustedIps List of trusted IP from the configuration.
419 * @return string|bool The forwarded IP, or false if none could be extracted.
421 function getIpAddressFromProxy($server, $trustedIps)
423 $forwardedIpHeader = 'HTTP_X_FORWARDED_FOR';
424 if (empty($server[$forwardedIpHeader])) {
428 $ips = preg_split('/\s*,\s*/', $server[$forwardedIpHeader]);
429 $ips = array_diff($ips, $trustedIps);
434 return array_pop($ips);
439 * Return an identifier based on the advertised client IP address(es)
441 * This aims at preventing session hijacking from users behind the same proxy
442 * by relying on HTTP headers.
445 * - https://secure.php.net/manual/en/reserved.variables.server.php
446 * - https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php
447 * - https://stackoverflow.com/questions/12233406/preventing-session-hijacking
448 * - https://stackoverflow.com/questions/21354859/trusting-x-forwarded-for-to-identify-a-visitor
450 * @param array $server The $_SERVER array
452 * @return string An identifier based on client IP address information
454 function client_ip_id($server)
456 $ip = $server['REMOTE_ADDR'];
458 if (isset($server['HTTP_X_FORWARDED_FOR'])) {
459 $ip = $ip . '_' . $server['HTTP_X_FORWARDED_FOR'];
461 if (isset($server['HTTP_CLIENT_IP'])) {
462 $ip = $ip . '_' . $server['HTTP_CLIENT_IP'];
469 * Returns true if Shaarli's currently browsed in HTTPS.
470 * Supports reverse proxies (if the headers are correctly set).
472 * @param array $server $_SERVER.
474 * @return bool true if HTTPS, false otherwise.
476 function is_https($server)
479 if (isset($server['HTTP_X_FORWARDED_PORT'])) {
480 // Keep forwarded port
481 if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
482 $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
483 $port = trim($ports[0]);
485 $port = $server['HTTP_X_FORWARDED_PORT'];
488 if ($port == '443') {
493 return ! empty($server['HTTPS']);
497 * Get cURL callback function for CURLOPT_WRITEFUNCTION
499 * @param string $charset to extract from the downloaded page (reference)
500 * @param string $curlGetInfo Optionally overrides curl_getinfo function
504 function get_curl_header_callback(
506 $curlGetInfo = 'curl_getinfo'
508 $isRedirected = false;
510 return function ($ch, $data) use ($curlGetInfo, &$charset, &$isRedirected) {
511 $responseCode = $curlGetInfo($ch, CURLINFO_RESPONSE_CODE
);
512 $chunkLength = strlen($data);
513 if (!empty($responseCode) && in_array($responseCode, [301, 302])) {
514 $isRedirected = true;
517 if (!empty($responseCode) && $responseCode !== 200) {
520 // After a redirection, the content type will keep the previous request value
521 // until it finds the next content-type header.
522 if (! $isRedirected || strpos(strtolower($data), 'content-type') !== false) {
523 $contentType = $curlGetInfo($ch, CURLINFO_CONTENT_TYPE
);
525 if (!empty($contentType) && strpos($contentType, 'text/html') === false) {
528 if (!empty($contentType) && empty($charset)) {
529 $charset = header_extract_charset($contentType);
537 * Get cURL callback function for CURLOPT_WRITEFUNCTION
539 * @param string $charset to extract from the downloaded page (reference)
540 * @param string $title to extract from the downloaded page (reference)
541 * @param string $description to extract from the downloaded page (reference)
542 * @param string $keywords to extract from the downloaded page (reference)
543 * @param bool $retrieveDescription Automatically tries to retrieve description and keywords from HTML content
544 * @param string $curlGetInfo Optionally overrides curl_getinfo function
548 function get_curl_download_callback(
559 * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download).
561 * While downloading the remote page, we check that the HTTP code is 200 and content type is 'html/text'
562 * Then we extract the title and the charset and stop the download when it's done.
564 * @param resource $ch cURL resource
565 * @param string $data chunk of data being downloaded
567 * @return int|bool length of $data or false if we need to stop the download
569 return function ($ch, $data) use (
570 $retrieveDescription,
578 $chunkLength = strlen($data);
581 if (empty($charset)) {
582 $charset = html_extract_charset($data);
585 $title = html_extract_title($data);
586 $foundChunk = ! empty($title) ? $currentChunk : $foundChunk;
589 $title = html_extract_tag('title', $data);
590 $foundChunk = ! empty($title) ? $currentChunk : $foundChunk;
592 if ($retrieveDescription && empty($description)) {
593 $description = html_extract_tag('description', $data);
594 $foundChunk = ! empty($description) ? $currentChunk : $foundChunk;
596 if ($retrieveDescription && empty($keywords)) {
597 $keywords = html_extract_tag('keywords', $data);
598 if (! empty($keywords)) {
599 $foundChunk = $currentChunk;
600 // Keywords use the format tag1, tag2 multiple words, tag
601 // So we format them to match Shaarli's separator and glue multiple words with '-'
602 $keywords = implode(' ', array_map(function($keyword) {
603 return implode('-', preg_split('/\s+/', trim($keyword)));
604 }, explode(',', $keywords)));
608 // We got everything we want, stop the download.
609 // If we already found either the title, description or keywords,
610 // it's highly unlikely that we'll found the other metas further than
611 // in the same chunk of data or the next one. So we also stop the download after that.
612 if ((!empty($responseCode) && !empty($contentType) && !empty($charset)) && $foundChunk !== null
613 && (! $retrieveDescription
614 || $foundChunk < $currentChunk
615 || (!empty($title) && !empty($description) && !empty($keywords))