]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/HttpUtils.php
Merge pull request #522 from ArthurHoaro/hotfix/readershaare
[github/shaarli/Shaarli.git] / application / HttpUtils.php
CommitLineData
451314eb
V
1<?php
2/**
3 * GET an HTTP URL to retrieve its content
4 *
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)
8 *
9 * @return array HTTP response headers, downloaded content
10 *
11 * Output format:
12 * [0] = associative array containing HTTP response headers
13 * [1] = URL content (downloaded data)
14 *
15 * Example:
1557cefb 16 * list($headers, $data) = get_http_response('http://sebauvage.net/');
451314eb
V
17 * if (strpos($headers[0], '200 OK') !== false) {
18 * echo 'Data type: '.htmlspecialchars($headers['Content-Type']);
19 * } else {
20 * echo 'There was an error: '.htmlspecialchars($headers[0]);
21 * }
22 *
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
26 */
1557cefb 27function get_http_response($url, $timeout = 30, $maxBytes = 4194304)
451314eb 28{
1557cefb
A
29 $urlObj = new Url($url);
30 if (! filter_var($url, FILTER_VALIDATE_URL) || ! $urlObj->isHttp()) {
31 return array(array(0 => 'Invalid HTTP Url'), false);
32 }
33
451314eb
V
34 $options = array(
35 'http' => array(
36 'method' => 'GET',
37 'timeout' => $timeout,
38 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)'
1557cefb
A
39 .' Gecko/20100101 Firefox/23.0',
40 'request_fulluri' => true,
451314eb
V
41 )
42 );
43
44 $context = stream_context_create($options);
1557cefb
A
45 stream_context_set_default($options);
46
47 list($headers, $finalUrl) = get_redirected_headers($urlObj->cleanup());
48 if (! $headers || strpos($headers[0], '200 OK') === false) {
49 return array($headers, false);
50 }
451314eb
V
51
52 try {
53 // TODO: catch Exception in calling code (thumbnailer)
1557cefb 54 $content = file_get_contents($finalUrl, false, $context, -1, $maxBytes);
451314eb
V
55 } catch (Exception $exc) {
56 return array(array(0 => 'HTTP Error'), $exc->getMessage());
57 }
58
1557cefb
A
59 return array($headers, $content);
60}
61
62/**
63 * Retrieve HTTP headers, following n redirections (temporary and permanent).
64 *
65 * @param string $url initial URL to reach.
66 * @param int $redirectionLimit max redirection follow..
67 *
68 * @return array
69 */
70function get_redirected_headers($url, $redirectionLimit = 3)
71{
72 $headers = get_headers($url, 1);
73
74 // Headers found, redirection found, and limit not reached.
75 if ($redirectionLimit-- > 0
76 && !empty($headers)
77 && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false)
78 && !empty($headers['Location'])) {
79
80 $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
81 if ($redirection != $url) {
82 return get_redirected_headers($redirection, $redirectionLimit);
83 }
451314eb
V
84 }
85
1557cefb 86 return array($headers, $url);
451314eb 87}
482d67bd
V
88
89/**
90 * Returns the server's base URL: scheme://domain.tld[:port]
91 *
92 * @param array $server the $_SERVER array
93 *
94 * @return string the server's base URL
95 *
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
100 */
101function server_url($server)
102{
103 $scheme = 'http';
104 $port = '';
105
106 // Shaarli is served behind a proxy
107 if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
108 // Keep forwarded scheme
85244fa0
A
109 if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
110 $schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']);
111 $scheme = trim($schemes[0]);
112 } else {
113 $scheme = $server['HTTP_X_FORWARDED_PROTO'];
114 }
482d67bd
V
115
116 if (isset($server['HTTP_X_FORWARDED_PORT'])) {
117 // Keep forwarded port
85244fa0
A
118 if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
119 $ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
120 $port = ':' . trim($ports[0]);
121 } else {
122 $port = ':' . $server['HTTP_X_FORWARDED_PORT'];
123 }
482d67bd
V
124 }
125
126 return $scheme.'://'.$server['SERVER_NAME'].$port;
127 }
128
129 // SSL detection
130 if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on')
131 || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) {
132 $scheme = 'https';
133 }
134
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'];
139 }
140
141 return $scheme.'://'.$server['SERVER_NAME'].$port;
142}
143
144/**
145 * Returns the absolute URL of the current script, without the query
146 *
147 * If the resource is "index.php", then it is removed (for better-looking URLs)
148 *
149 * @param array $server the $_SERVER array
150 *
151 * @return string the absolute URL of the current script, without the query
152 */
153function index_url($server)
154{
155 $scriptname = $server['SCRIPT_NAME'];
156 if (endswith($scriptname, 'index.php')) {
157 $scriptname = substr($scriptname, 0, -9);
158 }
159 return server_url($server) . $scriptname;
160}
161
162/**
163 * Returns the absolute URL of the current script, with the query
164 *
165 * If the resource is "index.php", then it is removed (for better-looking URLs)
166 *
167 * @param array $server the $_SERVER array
168 *
169 * @return string the absolute URL of the current script, with the query
170 */
171function page_url($server)
172{
173 if (! empty($server['QUERY_STRING'])) {
174 return index_url($server).'?'.$server['QUERY_STRING'];
175 }
176 return index_url($server);
177}