X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FHttpUtils.php;h=e2c1cb470f8a9c776358046839b10d3cdbdd3fd5;hb=1557cefbd76257ceb830f65806831b490faf0acc;hp=499220c596c201c098bc2178417c09b7d89f8be1;hpb=c0a50f3663e207d5df007e0fa321219c1b32d6ea;p=github%2Fshaarli%2FShaarli.git diff --git a/application/HttpUtils.php b/application/HttpUtils.php old mode 100644 new mode 100755 index 499220c5..e2c1cb47 --- a/application/HttpUtils.php +++ b/application/HttpUtils.php @@ -13,7 +13,7 @@ * [1] = URL content (downloaded data) * * Example: - * list($headers, $data) = get_http_url('http://sebauvage.net/'); + * list($headers, $data) = get_http_response('http://sebauvage.net/'); * if (strpos($headers[0], '200 OK') !== false) { * echo 'Data type: '.htmlspecialchars($headers['Content-Type']); * } else { @@ -24,31 +24,66 @@ * @see http://php.net/manual/en/function.stream-context-create.php * @see http://php.net/manual/en/function.get-headers.php */ -function get_http_url($url, $timeout = 30, $maxBytes = 4194304) +function get_http_response($url, $timeout = 30, $maxBytes = 4194304) { + $urlObj = new Url($url); + if (! filter_var($url, FILTER_VALIDATE_URL) || ! $urlObj->isHttp()) { + return array(array(0 => 'Invalid HTTP Url'), false); + } + $options = array( 'http' => array( 'method' => 'GET', 'timeout' => $timeout, 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)' - .' Gecko/20100101 Firefox/23.0' + .' Gecko/20100101 Firefox/23.0', + 'request_fulluri' => true, ) ); $context = stream_context_create($options); + stream_context_set_default($options); + + 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) - $content = file_get_contents($url, false, $context, -1, $maxBytes); + $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes); } catch (Exception $exc) { return array(array(0 => 'HTTP Error'), $exc->getMessage()); } - if (!$content) { - return array(array(0 => 'HTTP Error'), ''); + return array($headers, $content); +} + +/** + * Retrieve HTTP headers, following n redirections (temporary and permanent). + * + * @param string $url initial URL to reach. + * @param int $redirectionLimit max redirection follow.. + * + * @return array + */ +function get_redirected_headers($url, $redirectionLimit = 3) +{ + $headers = get_headers($url, 1); + + // Headers found, redirection found, and limit not reached. + if ($redirectionLimit-- > 0 + && !empty($headers) + && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false) + && !empty($headers['Location'])) { + + $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location']; + if ($redirection != $url) { + return get_redirected_headers($redirection, $redirectionLimit); + } } - return array(get_headers($url, 1), $content); + return array($headers, $url); } /**