aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2015-07-23 11:42:47 +0200
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2015-07-23 11:42:47 +0200
commitd2caf4349c4c5957ddc453795ffe8ea10237231c (patch)
tree5708d4ddd2768e8df3e7ba2c5cb71ab5131b75ff /lib
parentc748cfa0355bdc3e4c51c924b4462ee8c519de22 (diff)
parent1e65fd687e030a24773e88a2e7fb173e8439a99a (diff)
Merge pull request #915 from marienfressinaud/906-fix-getBaseUrl
Fix getBaseUrl behaviour
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Request.php45
-rw-r--r--lib/Minz/Url.php3
2 files changed, 31 insertions, 17 deletions
diff --git a/lib/Minz/Request.php b/lib/Minz/Request.php
index b9eda82a5..bf01bc26f 100644
--- a/lib/Minz/Request.php
+++ b/lib/Minz/Request.php
@@ -85,26 +85,37 @@ class Minz_Request {
}
/**
- * Détermine la base de l'url
- * @return la base de l'url
+ * Try to guess the base URL from $_SERVER information
+ *
+ * @return the base url (e.g. http://example.com/)
*/
- public static function getBaseUrl($baseUrlSuffix = '') {
- $conf = Minz_Configuration::get('system');
- $url = $conf->base_url;
- if ($url == '' || !preg_match('%^https?://%i', $url)) {
- $url = 'http';
- $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
- $port = empty($_SERVER['SERVER_PORT']) ? 80 : $_SERVER['SERVER_PORT'];
- if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
- $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port);
- } else {
- $url .= '://' . $host . ($port == 80 ? '' : ':' . $port);
- }
- $url .= isset($_SERVER['REQUEST_URI']) ? dirname($_SERVER['REQUEST_URI']) : '';
+ public static function guessBaseUrl() {
+ $url = 'http';
+ $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
+ $port = empty($_SERVER['SERVER_PORT']) ? 80 : $_SERVER['SERVER_PORT'];
+ if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
+ $url .= 's://' . $host . ($port == 443 ? '' : ':' . $port);
} else {
- $url = rtrim($url, '/\\') . $baseUrlSuffix;
+ $url .= '://' . $host . ($port == 80 ? '' : ':' . $port);
}
- return filter_var($url . '/', FILTER_SANITIZE_URL);
+ 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);
}
/**
diff --git a/lib/Minz/Url.php b/lib/Minz/Url.php
index a2809257d..4279b045b 100644
--- a/lib/Minz/Url.php
+++ b/lib/Minz/Url.php
@@ -25,6 +25,9 @@ class Minz_Url {
if ($absolute) {
$url_string = Minz_Request::getBaseUrl(PUBLIC_TO_INDEX_PATH);
+ if ($url_string === PUBLIC_TO_INDEX_PATH) {
+ $url_string = Minz_Request::guessBaseUrl();
+ }
} else {
$url_string = $isArray ? '.' : PUBLIC_RELATIVE;
}