diff options
| author | 2015-11-04 20:26:30 +0100 | |
|---|---|---|
| committer | 2015-11-04 20:26:30 +0100 | |
| commit | 74639cb1535feddb808a4262e058a67d4a3aef6b (patch) | |
| tree | 78c5567578751752a3623574cbd768e602a46754 /lib/Minz/Request.php | |
| parent | 4c99fd689f0778cbf6a987471ebf72b3787739c2 (diff) | |
| parent | 4525e547faa8781e37f86125110f28248eb67fd3 (diff) | |
Merge branch 'dev'
Diffstat (limited to 'lib/Minz/Request.php')
| -rw-r--r-- | lib/Minz/Request.php | 72 |
1 files changed, 43 insertions, 29 deletions
diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php index 6db2e9c7a..effb9943c 100644 --- a/lib/Minz/Request.php +++ b/lib/Minz/Request.php @@ -85,44 +85,58 @@ class Minz_Request { } /** - * Retourn le nom de domaine du site + * Try to guess the base URL from $_SERVER information + * + * @return the base url (e.g. http://example.com/) */ - public static function getDomainName() { - return $_SERVER['HTTP_HOST']; - } + public static function guessBaseUrl() { + $url = 'http'; - /** - * Détermine la base de l'url - * @return la base de l'url - */ - public static function getBaseUrl() { - $conf = Minz_Configuration::get('system'); - $defaultBaseUrl = $conf->base_url; - if (!empty($defaultBaseUrl)) { - return $defaultBaseUrl; - } elseif (isset($_SERVER['REQUEST_URI'])) { - return dirname($_SERVER['REQUEST_URI']) . '/'; + if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { + $https = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https'; } else { - return '/'; + $https = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'; } - } - /** - * Récupère l'URI de la requête - * @return l'URI - */ - public static function getURI() { - if (isset($_SERVER['REQUEST_URI'])) { - $base_url = self::getBaseUrl(); - $uri = $_SERVER['REQUEST_URI']; + if (!empty($_SERVER['HTTP_HOST'])) { + $host = $_SERVER['HTTP_HOST']; + } elseif (!empty($_SERVER['SERVER_NAME'])) { + $host = $_SERVER['SERVER_NAME']; + } else { + $host = 'localhost'; + } - $len_base_url = strlen($base_url); - $real_uri = substr($uri, $len_base_url); + if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { + $port = intval($_SERVER['HTTP_X_FORWARDED_PORT']); + } elseif (!empty($_SERVER['SERVER_PORT'])) { + $port = intval($_SERVER['SERVER_PORT']); } else { - $real_uri = ''; + $port = $https ? 443 : 80; } - return $real_uri; + if ($https) { + $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port); + } else { + $url .= '://' . $host . ($port == 80 ? '' : ':' . $port); + } + if (isset($_SERVER['REQUEST_URI'])) { + $path = $_SERVER['REQUEST_URI']; + $url .= substr($path, -1) === '/' ? substr($path, 0, -1) : dirname($path); + } + + return filter_var($url, FILTER_SANITIZE_URL); + } + + /** + * Return the base_url from configuration and add a suffix if given. + * + * @param $base_url_suffix a string to add at base_url (default: empty string) + * @return the base_url with a suffix. + */ + public static function getBaseUrl($base_url_suffix = '') { + $conf = Minz_Configuration::get('system'); + $url = rtrim($conf->base_url, '/\\') . $base_url_suffix; + return filter_var($url, FILTER_SANITIZE_URL); } /** |
