]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/HttpUtils.php
Git *wants* to rewrite this file in the exact same way...
[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:
16 * list($headers, $data) = get_http_url('http://sebauvage.net/');
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 */
27function get_http_url($url, $timeout = 30, $maxBytes = 4194304)
28{
29 $options = array(
30 'http' => array(
31 'method' => 'GET',
32 'timeout' => $timeout,
33 'user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0)'
34 .' Gecko/20100101 Firefox/23.0'
35 )
36 );
37
38 $context = stream_context_create($options);
39
40 try {
41 // TODO: catch Exception in calling code (thumbnailer)
42 $content = file_get_contents($url, false, $context, -1, $maxBytes);
43 } catch (Exception $exc) {
44 return array(array(0 => 'HTTP Error'), $exc->getMessage());
45 }
46
47 if (!$content) {
48 return array(array(0 => 'HTTP Error'), '');
49 }
50
51 return array(get_headers($url, 1), $content);
52}
482d67bd
V
53
54/**
55 * Returns the server's base URL: scheme://domain.tld[:port]
56 *
57 * @param array $server the $_SERVER array
58 *
59 * @return string the server's base URL
60 *
61 * @see http://www.ietf.org/rfc/rfc7239.txt
62 * @see http://www.ietf.org/rfc/rfc6648.txt
63 * @see http://stackoverflow.com/a/3561399
64 * @see http://stackoverflow.com/q/452375
65 */
66function server_url($server)
67{
68 $scheme = 'http';
69 $port = '';
70
71 // Shaarli is served behind a proxy
72 if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
73 // Keep forwarded scheme
74 $scheme = $server['HTTP_X_FORWARDED_PROTO'];
75
76 if (isset($server['HTTP_X_FORWARDED_PORT'])) {
77 // Keep forwarded port
78 $port = ':'.$server['HTTP_X_FORWARDED_PORT'];
79 }
80
81 return $scheme.'://'.$server['SERVER_NAME'].$port;
82 }
83
84 // SSL detection
85 if ((! empty($server['HTTPS']) && strtolower($server['HTTPS']) == 'on')
86 || (isset($server['SERVER_PORT']) && $server['SERVER_PORT'] == '443')) {
87 $scheme = 'https';
88 }
89
90 // Do not append standard port values
91 if (($scheme == 'http' && $server['SERVER_PORT'] != '80')
92 || ($scheme == 'https' && $server['SERVER_PORT'] != '443')) {
93 $port = ':'.$server['SERVER_PORT'];
94 }
95
96 return $scheme.'://'.$server['SERVER_NAME'].$port;
97}
98
99/**
100 * Returns the absolute URL of the current script, without the query
101 *
102 * If the resource is "index.php", then it is removed (for better-looking URLs)
103 *
104 * @param array $server the $_SERVER array
105 *
106 * @return string the absolute URL of the current script, without the query
107 */
108function index_url($server)
109{
110 $scriptname = $server['SCRIPT_NAME'];
111 if (endswith($scriptname, 'index.php')) {
112 $scriptname = substr($scriptname, 0, -9);
113 }
114 return server_url($server) . $scriptname;
115}
116
117/**
118 * Returns the absolute URL of the current script, with the query
119 *
120 * If the resource is "index.php", then it is removed (for better-looking URLs)
121 *
122 * @param array $server the $_SERVER array
123 *
124 * @return string the absolute URL of the current script, with the query
125 */
126function page_url($server)
127{
128 if (! empty($server['QUERY_STRING'])) {
129 return index_url($server).'?'.$server['QUERY_STRING'];
130 }
131 return index_url($server);
132}