diff --git a/.tests/phpunit.xml b/.tests/phpunit.xml index acdbf15..81a05b1 100644 --- a/.tests/phpunit.xml +++ b/.tests/phpunit.xml @@ -17,4 +17,7 @@ + + + diff --git a/.tests/tests/UtilsTest.php b/.tests/tests/UtilsTest.php new file mode 100644 index 0000000..38eefd8 --- /dev/null +++ b/.tests/tests/UtilsTest.php @@ -0,0 +1,103 @@ +setAccessible(true); + + ini_set("memory_limit", "1G"); + $result = $iniGetBytes->invoke(null, "memory_limit"); + $this->assertEquals(1024 * 1024 * 1024, $result); + + ini_set("memory_limit", 100 . "M"); + $result = $iniGetBytes->invoke(null, "memory_limit"); + $this->assertEquals(100 * 1024 * 1024, $result); + + ini_set("memory_limit", 100 * 1024 . "K"); + $result = $iniGetBytes->invoke(null, "memory_limit"); + $this->assertEquals(100 * 1024 * 1024, $result); + + ini_set("memory_limit", 100 * 1024 * 1024); + $result = $iniGetBytes->invoke(null, "memory_limit"); + $this->assertEquals(100 * 1024 * 1024, $result); + + } + + public function testGetBaseUrlMain() { + if (!defined("ROOT_DIR")) { + define("ROOT_DIR", "/var/www/html/ulogger"); + } + + $_SERVER["HTTPS"] = ""; + $_SERVER["HTTP_HOST"] = "www.example.com"; + $_SERVER["SCRIPT_FILENAME"] = ROOT_DIR . "/index.php"; + $_SERVER["PHP_SELF"] = "/index.php"; + $result = uUtils::getBaseUrl(); + $expected = "http://www.example.com/"; + $this->assertEquals($expected, $result); + } + + public function testGetBaseUrlScript() { + if (!defined("ROOT_DIR")) { + define("ROOT_DIR", "/var/www/html"); + } + + $_SERVER["HTTPS"] = ""; + $_SERVER["HTTP_HOST"] = "www.example.com"; + $_SERVER["SCRIPT_FILENAME"] = ROOT_DIR . "/utils/test.php"; + $_SERVER["PHP_SELF"] = "/utils/test.php"; + $result = uUtils::getBaseUrl(); + $expected = "http://www.example.com/"; + $this->assertEquals($expected, $result); + } + + public function testGetBaseUrlSubfolder() { + if (!defined("ROOT_DIR")) { + define("ROOT_DIR", "/var/www/html"); + } + + $_SERVER["HTTPS"] = ""; + $_SERVER["HTTP_HOST"] = "www.example.com"; + $_SERVER["SCRIPT_FILENAME"] = ROOT_DIR . "/index.php"; + $_SERVER["PHP_SELF"] = "/ulogger/index.php"; + $result = uUtils::getBaseUrl(); + $expected = "http://www.example.com/ulogger/"; + $this->assertEquals($expected, $result); + } + + public function testGetBaseUrlHttps() { + if (!defined("ROOT_DIR")) { + define("ROOT_DIR", "/var/www/html"); + } + + $_SERVER["HTTPS"] = "on"; + $_SERVER["HTTP_HOST"] = "www.example.com"; + $_SERVER["SCRIPT_FILENAME"] = ROOT_DIR . "/index.php"; + $_SERVER["PHP_SELF"] = "/index.php"; + $result = uUtils::getBaseUrl(); + $expected = "https://www.example.com/"; + $this->assertEquals($expected, $result); + } + + public function testGetBaseUrlHttp() { + if (!defined("ROOT_DIR")) { + define("ROOT_DIR", "/var/www/html"); + } + + $_SERVER["HTTPS"] = "off"; + $_SERVER["HTTP_HOST"] = "www.example.com"; + $_SERVER["SCRIPT_FILENAME"] = ROOT_DIR . "/index.php"; + $_SERVER["PHP_SELF"] = "/index.php"; + $result = uUtils::getBaseUrl(); + $expected = "http://www.example.com/"; + $this->assertEquals($expected, $result); + + unset($_SERVER["HTTPS"]); + $this->assertEquals($expected, $result); + } +} +?> diff --git a/helpers/auth.php b/helpers/auth.php index e8b49c5..2f6aea8 100644 --- a/helpers/auth.php +++ b/helpers/auth.php @@ -17,8 +17,10 @@ * along with this program; if not, see . */ - define('ROOT_DIR', dirname(__DIR__)); + if (!defined('ROOT_DIR')) { define('ROOT_DIR', dirname(__DIR__)); } require_once(ROOT_DIR . "/helpers/user.php"); + require_once(ROOT_DIR . "/helpers/utils.php"); + if (!defined('BASE_URL')) { define('BASE_URL', uUtils::getBaseUrl()); } /** * Authentication @@ -143,7 +145,7 @@ * @param string $path URL path * @return void */ - public function logOutWithRedirect($path = NULL) { + public function logOutWithRedirect($path = "") { $this->sessionEnd(); $this->exitWithRedirect($path); } @@ -174,14 +176,8 @@ * @param string $path Redirect URL path * @return void */ - public function exitWithRedirect($path = NULL) { - $ssl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off") ? "http" : "https"; - $url = $_SERVER['HTTP_HOST']; - if (is_null($path)) { - $path = dirname($_SERVER['SCRIPT_NAME']) . "/"; - } - $url = str_replace("//", "/", $url . $path); - header("Location: $ssl://$url"); + public function exitWithRedirect($path = "") { + header("Location: " . BASE_URL . $path); exit(); } } \ No newline at end of file diff --git a/helpers/utils.php b/helpers/utils.php index 3b28cec..82aabb4 100644 --- a/helpers/utils.php +++ b/helpers/utils.php @@ -108,6 +108,21 @@ exit; } + public static function getBaseUrl() { + $proto = (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] == "" || $_SERVER["HTTPS"] == "off") ? "http://" : "https://"; + $host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : ""; + if (realpath($_SERVER["SCRIPT_FILENAME"])) { + $scriptPath = substr(dirname(realpath($_SERVER["SCRIPT_FILENAME"])), strlen(ROOT_DIR)); + } else { + // for phpunit + $scriptPath = substr(dirname($_SERVER["SCRIPT_FILENAME"]), strlen(ROOT_DIR)); + } + $self = dirname($_SERVER["PHP_SELF"]); + $path = str_replace("\\", "/", substr($self, 0, strlen($self) - strlen($scriptPath))); + + return $proto . str_replace("//", "/", $host . $path . "/"); + } + } ?> \ No newline at end of file diff --git a/login.php b/login.php index b56faca..6f2156a 100644 --- a/login.php +++ b/login.php @@ -39,7 +39,7 @@
-
+ :

:
@@ -48,7 +48,7 @@ "> -
+
diff --git a/utils/logout.php b/utils/logout.php index 534fdc9..c758fc3 100755 --- a/utils/logout.php +++ b/utils/logout.php @@ -20,6 +20,6 @@ include_once(dirname(__DIR__) . "/helpers/auth.php"); $auth = new uAuth(); -$auth->logOutWithRedirect(dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/"); +$auth->logOutWithRedirect(); ?> \ No newline at end of file