]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/HttpUtils.php
3 * GET an HTTP URL to retrieve its content
5 * @param string $url URL to get (http://...)
6 * @param int $timeout network timeout (in seconds)
7 * @param int $maxBytes maximum downloaded bytes (default: 4 MiB)
9 * @return array HTTP response headers, downloaded content
12 * [0] = associative array containing HTTP response headers
13 * [1] = URL content (downloaded data)
16 * list($headers, $data) = get_http_response('http://sebauvage.net/');
17 * if (strpos($headers[0], '200 OK') !== false) {
18 * echo 'Data type: '.htmlspecialchars($headers['Content-Type']);
20 * echo 'There was an error: '.htmlspecialchars($headers[0]);
23 * @see http://php.net/manual/en/function.file-get-contents.php
24 * @see http://php.net/manual/en/function.stream-context-create.php
25 * @see http://php.net/manual/en/function.get-headers.php
27 function get_http_response($url, $timeout = 30, $maxBytes = 4194304)
29 $urlObj = new Url($url);
30 if (! filter_var($url, FILTER_VALIDATE_URL
) || ! $urlObj->isHttp()) {
31 return array(array(0 => 'Invalid HTTP Url'), false);
37 'timeout' => $timeout,
38 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)'
39 .' Gecko/20100101 Firefox/23.0',
40 'request_fulluri' => true,
44 $context = stream_context_create($options);
45 stream_context_set_default($options);
47 list($headers, $finalUrl) = get_redirected_headers($urlObj->cleanup());
48 if (! $headers || strpos($headers[0], '200 OK') === false) {
49 return array($headers, false);
53 // TODO: catch Exception in calling code (thumbnailer)
54 $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes);
55 } catch (Exception
$exc) {
56 return array(array(0 => 'HTTP Error'), $exc->getMessage());
59 return array($headers, $content);
63 * Retrieve HTTP headers, following n redirections (temporary and permanent).
65 * @param string $url initial URL to reach.
66 * @param int $redirectionLimit max redirection follow..
70 function get_redirected_headers($url, $redirectionLimit = 3)
72 $headers = get_headers($url, 1);
74 // Headers found, redirection found, and limit not reached.
75 if ($redirectionLimit-- > 0
77 && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false)
78 && !empty($headers['Location'])) {
80 $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
81 if ($redirection != $url) {
82 return get_redirected_headers($redirection, $redirectionLimit);
86 return array($headers, $url);
90 * Returns the server's base URL: scheme://domain.tld[:port]
92 * @param array $server the $_SERVER array
94 * @return string the server's base URL
96 * @see http://www.ietf.org/rfc/rfc7239.txt
97 * @see http://www.ietf.org/rfc/rfc6648.txt
98 * @see http://stackoverflow.com/a/3561399
99 * @see http://stackoverflow.com/q/452375
101 function server_url($server)
106 // Shaarli is served behind a proxy
107 if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
108 // Keep forwarded scheme
109 if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
110 $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']);
111 $scheme = trim($schemes[0]);
113 $scheme = $server['HTTP_X_FORWARDED_PROTO'];
116 if (isset($server['HTTP_X_FORWARDED_PORT'])) {
117 // Keep forwarded port
118 if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
119 $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
120 $port = ':' . trim($ports[0]);
122 $port = ':' . $server['HTTP_X_FORWARDED_PORT'];
126 return $scheme.'://'.$server['SERVER_NAME'].$port;
130 if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on')
131 || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) {
135 // Do not append standard port values
136 if (($scheme == 'http' && $server['SERVER_PORT'] != '80')
137 || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) {
138 $port = ':'.$server['SERVER_PORT'];
141 return $scheme.'://'.$server['SERVER_NAME'].$port;
145 * Returns the absolute URL of the current script, without the query
147 * If the resource is "index.php", then it is removed (for better-looking URLs)
149 * @param array $server the $_SERVER array
151 * @return string the absolute URL of the current script, without the query
153 function index_url($server)
155 $scriptname = $server['SCRIPT_NAME'];
156 if (endswith($scriptname, 'index.php')) {
157 $scriptname = substr($scriptname, 0, -9);
159 return server_url($server) . $scriptname;
163 * Returns the absolute URL of the current script, with the query
165 * If the resource is "index.php", then it is removed (for better-looking URLs)
167 * @param array $server the $_SERVER array
169 * @return string the absolute URL of the current script, with the query
171 function page_url($server)
173 if (! empty($server['QUERY_STRING'])) {
174 return index_url($server).'?'.$server['QUERY_STRING'];
176 return index_url($server);