route : the baseURL is now relative by default. It allow the route module to work across a proxy

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2074 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-04-27 13:44:05 +00:00
parent 586a7fa075
commit 1578a21a6b

View File

@@ -30,7 +30,7 @@ class route
/** Return the baseURL of the site /** Return the baseURL of the site
Always finish with a slash Always finish with a slash
@param string|null $module The module name (if thereis one) */ @param string|null $module The module name (if thereis one) */
function baseURL ($module = FALSE) function baseURL ($module = FALSE, $absolute=false)
{ {
if ($this->module === NULL) if ($this->module === NULL)
$this->module = $module; $this->module = $module;
@@ -47,12 +47,15 @@ class route
if (isset ($_SERVER["SCRIPT_NAME"])) if (isset ($_SERVER["SCRIPT_NAME"]))
$this->baseURL = dirname ($_SERVER["SCRIPT_NAME"]); $this->baseURL = dirname ($_SERVER["SCRIPT_NAME"]);
if (isset ($_SERVER["SERVER_NAME"])) if ($absolute === true)
$this->baseURL = "//".$_SERVER["SERVER_NAME"].$port.$this->baseURL; {
if (isset ($_SERVER["HTTPS"])) if (isset ($_SERVER["SERVER_NAME"]))
$this->baseURL = "https:".$this->baseURL; $this->baseURL = "//".$_SERVER["SERVER_NAME"].$port.$this->baseURL;
else if (isset ($_SERVER["HTTPS"]))
$this->baseURL = "http:".$this->baseURL; $this->baseURL = "https:".$this->baseURL;
else
$this->baseURL = "http:".$this->baseURL;
}
if (substr ($this->baseURL, -1) !== "/") if (substr ($this->baseURL, -1) !== "/")
$this->baseURL .= "/"; $this->baseURL .= "/";
$this->baseURLmodule = $this->baseURL; $this->baseURLmodule = $this->baseURL;
@@ -84,22 +87,26 @@ class route
} }
/** Return the complete URL used to see this page */ /** Return the complete URL used to see this page */
function requestURL () function requestURL ($absolute = false)
{ {
if (isset ($_SERVER["HTTPS"])) $url = "";
$url = "https:"; if ($absolute === true)
else {
$url = "http:"; if (isset ($_SERVER["HTTPS"]))
if (!isset ($_SERVER["SERVER_PORT"])) $url = "https:";
$_SERVER["SERVER_PORT"] = "80"; else
$port = ":".$_SERVER["SERVER_PORT"]; $url = "http:";
if (!isset ($_SERVER["HTTPS"]) && $_SERVER["SERVER_PORT"] === "80") if (!isset ($_SERVER["SERVER_PORT"]))
$port = ""; $_SERVER["SERVER_PORT"] = "80";
if (isset ($_SERVER["HTTPS"]) && $_SERVER["SERVER_PORT"] === "443") $port = ":".$_SERVER["SERVER_PORT"];
$port = ""; if (!isset ($_SERVER["HTTPS"]) && $_SERVER["SERVER_PORT"] === "80")
$port = "";
if (isset ($_SERVER["HTTPS"]) && $_SERVER["SERVER_PORT"] === "443")
$port = "";
if (isset ($_SERVER["SERVER_NAME"])) if (isset ($_SERVER["SERVER_NAME"]))
$url .= "//".$_SERVER["SERVER_NAME"].$port; $url .= "//".$_SERVER["SERVER_NAME"].$port;
}
if (isset ($_SERVER["REQUEST_URI"])) if (isset ($_SERVER["REQUEST_URI"]))
$url .= $_SERVER["REQUEST_URI"]; $url .= $_SERVER["REQUEST_URI"];
return $url; return $url;
@@ -128,11 +135,6 @@ class route
// Absolute : return to project base // Absolute : return to project base
$destURL = $baseURL.substr ($destURL, 1); $destURL = $baseURL.substr ($destURL, 1);
} }
if (substr ($destURL, 0, 4) !== "http")
{
// Relative to parameters : continue
$destURL = $requestURL.$destURL;
}
// Else http : keep the complete URL // Else http : keep the complete URL
if ($destURL === "") if ($destURL === "")
@@ -222,6 +224,7 @@ class route
return; return;
} }
file_put_contents ("/tmp/csrf.log", date ("Y-m-d H:i:s")." GET route->map ($route, ...)\n", FILE_APPEND);
return $this->map ($route, $function); return $this->map ($route, $function);
} }
@@ -243,6 +246,7 @@ class route
return; return;
} }
file_put_contents ("/tmp/csrf.log", date ("Y-m-d H:i:s")." POST route->map ($route, ...)\n", FILE_APPEND);
return $this->map ($route, $function); return $this->map ($route, $function);
} }
@@ -264,6 +268,7 @@ class route
return; return;
} }
file_put_contents ("/tmp/csrf.log", date ("Y-m-d H:i:s")." PUT route->map ($route, ...)\n", FILE_APPEND);
return $this->map ($route, $function); return $this->map ($route, $function);
} }
@@ -285,6 +290,7 @@ class route
return; return;
} }
file_put_contents ("/tmp/csrf.log", date ("Y-m-d H:i:s")." DELETE route->map ($route, ...)\n", FILE_APPEND);
return $this->map ($route, $function); return $this->map ($route, $function);
} }
@@ -301,6 +307,7 @@ class route
{ {
if ($this->debug) if ($this->debug)
echo "==> FOUND EQUAL !\n"; echo "==> FOUND EQUAL !\n";
file_put_contents ("/tmp/csrf.log", date ("Y-m-d H:i:s")." map : BACKTRACE = ".print_r(debug_backtrace(), TRUE)."\n", FILE_APPEND);
try try
{ {
$function (); $function ();
@@ -329,6 +336,7 @@ class route
{ {
if ($this->debug) if ($this->debug)
echo "==> FOUND REGEX !\n"; echo "==> FOUND REGEX !\n";
file_put_contents ("/tmp/csrf.log", date ("Y-m-d H:i:s")." map : BACKTRACE = ".print_r(debug_backtrace(), TRUE)."\n", FILE_APPEND);
$params = array (); $params = array ();
$reflect = new ReflectionFunction ($function); $reflect = new ReflectionFunction ($function);
foreach ($reflect->getParameters() as $key=>$val) foreach ($reflect->getParameters() as $key=>$val)