X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FHttpUtils.php;h=c84ba6f05746d404b195607715556a67ccce8eba;hb=caa69b585381cc1c22df3dbb9c943855b8f13a70;hp=e2c1cb470f8a9c776358046839b10d3cdbdd3fd5;hpb=92ba7b573f2833bd35c7eb2fc7fdbeb1a0ac7b44;p=github%2Fshaarli%2FShaarli.git diff --git a/application/HttpUtils.php b/application/HttpUtils.php old mode 100755 new mode 100644 index e2c1cb47..c84ba6f0 --- a/application/HttpUtils.php +++ b/application/HttpUtils.php @@ -27,7 +27,9 @@ function get_http_response($url, $timeout = 30, $maxBytes = 4194304) { $urlObj = new Url($url); - if (! filter_var($url, FILTER_VALIDATE_URL) || ! $urlObj->isHttp()) { + $cleanUrl = $urlObj->idnToAscii(); + + if (! filter_var($cleanUrl, FILTER_VALIDATE_URL) || ! $urlObj->isHttp()) { return array(array(0 => 'Invalid HTTP Url'), false); } @@ -35,22 +37,27 @@ function get_http_response($url, $timeout = 30, $maxBytes = 4194304) 'http' => array( 'method' => 'GET', 'timeout' => $timeout, - 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)' - .' Gecko/20100101 Firefox/23.0', - 'request_fulluri' => true, + 'user_agent' => 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:45.0)' + .' Gecko/20100101 Firefox/45.0', + 'accept_language' => substr(setlocale(LC_COLLATE, 0), 0, 2) . ',en-US;q=0.7,en;q=0.3', ) ); - $context = stream_context_create($options); stream_context_set_default($options); + list($headers, $finalUrl) = get_redirected_headers($cleanUrl); + if (! $headers || strpos($headers[0], '200 OK') === false) { + $options['http']['request_fulluri'] = true; + stream_context_set_default($options); + list($headers, $finalUrl) = get_redirected_headers($cleanUrl); + } - list($headers, $finalUrl) = get_redirected_headers($urlObj->cleanup()); if (! $headers || strpos($headers[0], '200 OK') === false) { return array($headers, false); } try { // TODO: catch Exception in calling code (thumbnailer) + $context = stream_context_create($options); $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes); } catch (Exception $exc) { return array(array(0 => 'HTTP Error'), $exc->getMessage()); @@ -60,16 +67,19 @@ function get_http_response($url, $timeout = 30, $maxBytes = 4194304) } /** - * Retrieve HTTP headers, following n redirections (temporary and permanent). + * Retrieve HTTP headers, following n redirections (temporary and permanent ones). * - * @param string $url initial URL to reach. - * @param int $redirectionLimit max redirection follow.. + * @param string $url initial URL to reach. + * @param int $redirectionLimit max redirection follow. * - * @return array + * @return array HTTP headers, or false if it failed. */ function get_redirected_headers($url, $redirectionLimit = 3) { $headers = get_headers($url, 1); + if (!empty($headers['location']) && empty($headers['Location'])) { + $headers['Location'] = $headers['location']; + } // Headers found, redirection found, and limit not reached. if ($redirectionLimit-- > 0 @@ -79,6 +89,7 @@ function get_redirected_headers($url, $redirectionLimit = 3) $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; if ($redirection != $url) { + $redirection = getAbsoluteUrl($url, $redirection); return get_redirected_headers($redirection, $redirectionLimit); } } @@ -86,6 +97,35 @@ function get_redirected_headers($url, $redirectionLimit = 3) return array($headers, $url); } +/** + * Get an absolute URL from a complete one, and another absolute/relative URL. + * + * @param string $originalUrl The original complete URL. + * @param string $newUrl The new one, absolute or relative. + * + * @return string Final URL: + * - $newUrl if it was already an absolute URL. + * - if it was relative, absolute URL from $originalUrl path. + */ +function getAbsoluteUrl($originalUrl, $newUrl) +{ + $newScheme = parse_url($newUrl, PHP_URL_SCHEME); + // Already an absolute URL. + if (!empty($newScheme)) { + return $newUrl; + } + + $parts = parse_url($originalUrl); + $final = $parts['scheme'] .'://'. $parts['host']; + $final .= (!empty($parts['port'])) ? $parts['port'] : ''; + $final .= '/'; + if ($newUrl[0] != '/') { + $final .= substr(ltrim($parts['path'], '/'), 0, strrpos($parts['path'], '/')); + } + $final .= ltrim($newUrl, '/'); + return $final; +} + /** * Returns the server's base URL: scheme://domain.tld[:port] * @@ -106,11 +146,21 @@ function server_url($server) // Shaarli is served behind a proxy if (isset($server['HTTP_X_FORWARDED_PROTO'])) { // Keep forwarded scheme - $scheme = $server['HTTP_X_FORWARDED_PROTO']; + if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { + $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']); + $scheme = trim($schemes[0]); + } else { + $scheme = $server['HTTP_X_FORWARDED_PROTO']; + } if (isset($server['HTTP_X_FORWARDED_PORT'])) { // Keep forwarded port - $port = ':'.$server['HTTP_X_FORWARDED_PORT']; + if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) { + $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']); + $port = ':' . trim($ports[0]); + } else { + $port = ':' . $server['HTTP_X_FORWARDED_PORT']; + } } return $scheme.'://'.$server['SERVER_NAME'].$port;