diff options
author | VirtualTam <virtualtam@flibidi.net> | 2015-09-06 21:31:37 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2015-09-14 20:27:16 +0200 |
commit | 482d67bd523bf12f36508a0131d09fe299ee02f4 (patch) | |
tree | b4e7c6ddaa1d88cc49bc96c2524f12431d8fcce0 /application | |
parent | 7b114771d337af3bfd51d8fda1e8f2fd5b39535d (diff) | |
download | Shaarli-482d67bd523bf12f36508a0131d09fe299ee02f4.tar.gz Shaarli-482d67bd523bf12f36508a0131d09fe299ee02f4.tar.zst Shaarli-482d67bd523bf12f36508a0131d09fe299ee02f4.zip |
HTTP: move server URL functions to `HttpUtils.php`
Relates to #333
Modifications:
- refactor server URL utility functions
- do not access global `$_SERVER` variables
- add test coverage
- improve readability
- apply coding conventions
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'application')
-rw-r--r-- | application/HttpUtils.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/application/HttpUtils.php b/application/HttpUtils.php index 175333ae..499220c5 100644 --- a/application/HttpUtils.php +++ b/application/HttpUtils.php | |||
@@ -50,3 +50,83 @@ function get_http_url($url, $timeout = 30, $maxBytes = 4194304) | |||
50 | 50 | ||
51 | return array(get_headers($url, 1), $content); | 51 | return array(get_headers($url, 1), $content); |
52 | } | 52 | } |
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 | */ | ||
66 | function 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 | */ | ||
108 | function 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 | */ | ||
126 | function 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 | } | ||