Merge branch 'branch-2.10'

This commit is contained in:
Shish
2024-02-08 00:41:38 +00:00
42 changed files with 132 additions and 1259 deletions

View File

@@ -21,7 +21,7 @@ class CliApp extends \Symfony\Component\Console\Application
$definition->addOption(new InputOption(
'--user',
'-u',
InputOption::VALUE_NONE,
InputOption::VALUE_REQUIRED,
'Log in as the given user'
));
@@ -36,9 +36,10 @@ class CliApp extends \Symfony\Component\Console\Application
$output ??= new ConsoleOutput();
if ($input->hasParameterOption(['--user', '-u'])) {
$user = User::by_name($input->getOption('user'));
$name = $input->getParameterOption(['--user', '-u']);
$user = User::by_name($name);
if (is_null($user)) {
die("Unknown user");
die("Unknown user '$name'\n");
} else {
send_event(new UserLoginEvent($user));
}

View File

@@ -306,18 +306,14 @@ function get_base_href(): string
if (defined("BASE_HREF") && !empty(BASE_HREF)) {
return BASE_HREF;
}
$possible_vars = ['SCRIPT_NAME', 'PHP_SELF', 'PATH_INFO', 'ORIG_PATH_INFO'];
$ok_var = null;
foreach ($possible_vars as $var) {
if (isset($_SERVER[$var]) && substr($_SERVER[$var], -4) === '.php') {
$ok_var = $_SERVER[$var];
break;
}
if(str_ends_with($_SERVER['PHP_SELF'], 'index.php')) {
$self = $_SERVER['PHP_SELF'];
}
assert(!empty($ok_var));
$dir = dirname($ok_var);
elseif(isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['DOCUMENT_ROOT'])) {
$self = substr($_SERVER['SCRIPT_FILENAME'], strlen(rtrim($_SERVER['DOCUMENT_ROOT'], "/")));
}
$dir = dirname($self);
$dir = str_replace("\\", "/", $dir);
$dir = str_replace("//", "/", $dir);
$dir = rtrim($dir, "/");
return $dir;
}

View File

@@ -254,4 +254,42 @@ class PolyfillsTest extends TestCase
deltree($dir);
$this->assertFalse(file_exists($dir));
}
private function _tbh(array $vars, string $result): void
{
// update $_SERVER with $vars, call get_base_href() and check result, then reset $_SERVER to original value
$old_server = $_SERVER;
$_SERVER = array_merge($_SERVER, $vars);
$this->assertEquals($result, get_base_href());
$_SERVER = $old_server;
}
public function test_get_base_href(): void
{
// PHP_SELF should point to "the currently executing script
// relative to the document root"
$this->_tbh(["PHP_SELF" => "/index.php"], "");
$this->_tbh(["PHP_SELF" => "/mydir/index.php"], "/mydir");
// SCRIPT_FILENAME should point to "the absolute pathname of
// the currently executing script" and DOCUMENT_ROOT should
// point to "the document root directory under which the
// current script is executing"
$this->_tbh([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/mydir/index.php",
"DOCUMENT_ROOT" => "/var/www/html",
], "/mydir");
$this->_tbh([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/mydir/index.php",
"DOCUMENT_ROOT" => "/var/www/html/",
], "/mydir");
$this->_tbh([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/index.php",
"DOCUMENT_ROOT" => "/var/www/html",
], "");
}
}

View File

@@ -162,4 +162,27 @@ class UtilTest extends TestCase
path_to_tags("/category:/tag/baz.jpg")
);
}
public function test_get_query(): void
{
// niceurls
$_SERVER["REQUEST_URI"] = "/test/tasty/cake";
$this->assertEquals("/tasty/cake", _get_query());
// no niceurls
$_SERVER["REQUEST_URI"] = "/test/index.php?q=/tasty/cake";
$this->assertEquals("/tasty/cake", _get_query());
// leave url encoding alone
$_SERVER["REQUEST_URI"] = "/test/index.php?q=/tasty/cake%20pie";
$this->assertEquals("/tasty/cake%20pie", _get_query());
// if just viewing index.php
$_SERVER["REQUEST_URI"] = "/test/index.php";
$this->assertEquals("/", _get_query());
// niceurl root
$_SERVER["REQUEST_URI"] = "/test/";
$this->assertEquals("/", _get_query());
}
}

View File

@@ -715,19 +715,33 @@ function _get_user(): User
function _get_query(): string
{
// if query is explicitly set, use it
$q = @$_POST["q"] ?: @$_GET["q"];
if(!empty($q)) {
return $q;
// if q is set in POST, use that
if(isset($_POST["q"])) {
return $_POST["q"];
}
// if q is set in GET, use that
// (we need to manually parse the query string because PHP's $_GET
// does an extra round of URL decoding, which we don't want)
$parts = parse_url($_SERVER['REQUEST_URI']);
$qs = [];
foreach(explode('&', $parts['query'] ?? "") as $z) {
$qps = explode('=', $z, 2);
if(count($qps) == 2) {
$qs[$qps[0]] = $qps[1];
}
}
if(isset($qs["q"])) {
return $qs["q"];
}
// if we're just looking at index.php, use the default query
elseif (str_contains($_SERVER['REQUEST_URI'], "index.php")) {
if(str_ends_with($parts["path"], "index.php")) {
return "/";
}
// otherwise, use the request URI
else {
return explode("?", $_SERVER['REQUEST_URI'])[0];
}
// otherwise, use the request URI minus the base path
return substr($parts["path"], strlen(get_base_href()));
}