Fix: redirects fail when app is installed in subfolder

This commit is contained in:
Bartek Fabiszewski 2017-09-07 18:09:36 +02:00
parent 146df4eec3
commit 7df61bd060
6 changed files with 130 additions and 13 deletions

View File

@ -17,4 +17,7 @@
<logging> <logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/> <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
</logging> </logging>
<php>
<server name='HTTP_HOST' value='http://127.0.0.1' />
</php>
</phpunit> </phpunit>

103
.tests/tests/UtilsTest.php Normal file
View 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);
}
}
?>

View File

@ -17,8 +17,10 @@
* along with this program; if not, see <http://www.gnu.org/licenses/>. * 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/user.php");
require_once(ROOT_DIR . "/helpers/utils.php");
if (!defined('BASE_URL')) { define('BASE_URL', uUtils::getBaseUrl()); }
/** /**
* Authentication * Authentication
@ -143,7 +145,7 @@
* @param string $path URL path * @param string $path URL path
* @return void * @return void
*/ */
public function logOutWithRedirect($path = NULL) { public function logOutWithRedirect($path = "") {
$this->sessionEnd(); $this->sessionEnd();
$this->exitWithRedirect($path); $this->exitWithRedirect($path);
} }
@ -174,14 +176,8 @@
* @param string $path Redirect URL path * @param string $path Redirect URL path
* @return void * @return void
*/ */
public function exitWithRedirect($path = NULL) { public function exitWithRedirect($path = "") {
$ssl = (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "" || $_SERVER['HTTPS'] == "off") ? "http" : "https"; header("Location: " . BASE_URL . $path);
$url = $_SERVER['HTTP_HOST'];
if (is_null($path)) {
$path = dirname($_SERVER['SCRIPT_NAME']) . "/";
}
$url = str_replace("//", "/", $url . $path);
header("Location: $ssl://$url");
exit(); exit();
} }
} }

View File

@ -108,6 +108,21 @@
exit; 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 . "/");
}
} }
?> ?>

View File

@ -39,7 +39,7 @@
<div id="login"> <div id="login">
<div id="title"><?= $lang["title"] ?></div> <div id="title"><?= $lang["title"] ?></div>
<div id="subtitle"><?= $lang["private"] ?></div> <div id="subtitle"><?= $lang["private"] ?></div>
<form action="/" method="post"> <form action="<?= BASE_URL ?>" method="post">
<?= $lang["username"] ?>:<br> <?= $lang["username"] ?>:<br>
<input type="text" name="user"><br> <input type="text" name="user"><br>
<?= $lang["password"] ?>:<br> <?= $lang["password"] ?>:<br>
@ -48,7 +48,7 @@
<input type="submit" value="<?= $lang["login"] ?>"> <input type="submit" value="<?= $lang["login"] ?>">
<input type="hidden" name="action" value="auth"> <input type="hidden" name="action" value="auth">
<?php if (!uConfig::$require_authentication): ?> <?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; ?> <?php endif; ?>
</form> </form>
<div id="error"><?= (($auth_error) ? $lang["authfail"] : "") ?></div> <div id="error"><?= (($auth_error) ? $lang["authfail"] : "") ?></div>

View File

@ -20,6 +20,6 @@
include_once(dirname(__DIR__) . "/helpers/auth.php"); include_once(dirname(__DIR__) . "/helpers/auth.php");
$auth = new uAuth(); $auth = new uAuth();
$auth->logOutWithRedirect(dirname(dirname($_SERVER['SCRIPT_NAME'])) . "/"); $auth->logOutWithRedirect();
?> ?>