Fix: redirects fail when app is installed in subfolder
This commit is contained in:
parent
146df4eec3
commit
7df61bd060
@ -17,4 +17,7 @@
|
||||
<logging>
|
||||
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
|
||||
</logging>
|
||||
<php>
|
||||
<server name='HTTP_HOST' value='http://127.0.0.1' />
|
||||
</php>
|
||||
</phpunit>
|
||||
|
103
.tests/tests/UtilsTest.php
Normal file
103
.tests/tests/UtilsTest.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once(__DIR__ . "/../../helpers/utils.php");
|
||||
|
||||
class UtilsTest extends TestCase {
|
||||
|
||||
public function testGetUploadMaxSize() {
|
||||
$iniGetBytes = new ReflectionMethod('uUtils', 'iniGetBytes');
|
||||
$iniGetBytes->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);
|
||||
}
|
||||
}
|
||||
?>
|
@ -17,8 +17,10 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
@ -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 . "/");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -39,7 +39,7 @@
|
||||
<div id="login">
|
||||
<div id="title"><?= $lang["title"] ?></div>
|
||||
<div id="subtitle"><?= $lang["private"] ?></div>
|
||||
<form action="/" method="post">
|
||||
<form action="<?= BASE_URL ?>" method="post">
|
||||
<?= $lang["username"] ?>:<br>
|
||||
<input type="text" name="user"><br>
|
||||
<?= $lang["password"] ?>:<br>
|
||||
@ -48,7 +48,7 @@
|
||||
<input type="submit" value="<?= $lang["login"] ?>">
|
||||
<input type="hidden" name="action" value="auth">
|
||||
<?php if (!uConfig::$require_authentication): ?>
|
||||
<div id="cancel"><a href="/"><?= $lang["cancel"] ?></a></div>
|
||||
<div id="cancel"><a href="<?= BASE_URL ?>"><?= $lang["cancel"] ?></a></div>
|
||||
<?php endif; ?>
|
||||
</form>
|
||||
<div id="error"><?= (($auth_error) ? $lang["authfail"] : "") ?></div>
|
||||
|
@ -20,6 +20,6 @@
|
||||
include_once(dirname(__DIR__) . "/helpers/auth.php");
|
||||
|
||||
$auth = new uAuth();
|
||||
$auth->logOutWithRedirect(dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/");
|
||||
$auth->logOutWithRedirect();
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user