]>
Commit | Line | Data |
---|---|---|
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 | 27 | function 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 | */ | |
70 | function 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 | */ | |
101 | function 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 | |
109 | $scheme = $server['HTTP_X_FORWARDED_PROTO']; | |
110 | ||
111 | if (isset($server['HTTP_X_FORWARDED_PORT'])) { | |
112 | // Keep forwarded port | |
113 | $port = ':'.$server['HTTP_X_FORWARDED_PORT']; | |
114 | } | |
115 | ||
116 | return $scheme.'://'.$server['SERVER_NAME'].$port; | |
117 | } | |
118 | ||
119 | // SSL detection | |
120 | if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on') | |
121 | || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) { | |
122 | $scheme = 'https'; | |
123 | } | |
124 | ||
125 | // Do not append standard port values | |
126 | if (($scheme == 'http' && $server['SERVER_PORT'] != '80') | |
127 | || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) { | |
128 | $port = ':'.$server['SERVER_PORT']; | |
129 | } | |
130 | ||
131 | return $scheme.'://'.$server['SERVER_NAME'].$port; | |
132 | } | |
133 | ||
134 | /** | |
135 | * Returns the absolute URL of the current script, without the query | |
136 | * | |
137 | * If the resource is "index.php", then it is removed (for better-looking URLs) | |
138 | * | |
139 | * @param array $server the $_SERVER array | |
140 | * | |
141 | * @return string the absolute URL of the current script, without the query | |
142 | */ | |
143 | function index_url($server) | |
144 | { | |
145 | $scriptname = $server['SCRIPT_NAME']; | |
146 | if (endswith($scriptname, 'index.php')) { | |
147 | $scriptname = substr($scriptname, 0, -9); | |
148 | } | |
149 | return server_url($server) . $scriptname; | |
150 | } | |
151 | ||
152 | /** | |
153 | * Returns the absolute URL of the current script, with the query | |
154 | * | |
155 | * If the resource is "index.php", then it is removed (for better-looking URLs) | |
156 | * | |
157 | * @param array $server the $_SERVER array | |
158 | * | |
159 | * @return string the absolute URL of the current script, with the query | |
160 | */ | |
161 | function page_url($server) | |
162 | { | |
163 | if (! empty($server['QUERY_STRING'])) { | |
164 | return index_url($server).'?'.$server['QUERY_STRING']; | |
165 | } | |
166 | return index_url($server); | |
167 | } |