aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/HttpUtils.php80
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 */
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}