2019-08-12 18:13:58 +02:00
|
|
|
<?php
|
2020-02-08 17:16:42 +01:00
|
|
|
/**
|
2023-01-01 15:36:24 +01:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-08 17:16:42 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-08-12 18:13:58 +02:00
|
|
|
|
|
|
|
namespace Friendica\App;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine all arguments of the current call, including
|
|
|
|
* - The whole querystring (except the pagename/q parameter)
|
|
|
|
* - The command
|
|
|
|
* - The arguments (C-Style based)
|
|
|
|
* - The count of arguments
|
|
|
|
*/
|
|
|
|
class Arguments
|
|
|
|
{
|
2021-11-19 22:47:49 +01:00
|
|
|
const DEFAULT_MODULE = 'home';
|
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
/**
|
|
|
|
* @var string The complete query string
|
|
|
|
*/
|
|
|
|
private $queryString;
|
|
|
|
/**
|
|
|
|
* @var string The current Friendica command
|
|
|
|
*/
|
|
|
|
private $command;
|
2021-11-19 22:47:49 +01:00
|
|
|
/**
|
|
|
|
* @var string The name of the current module
|
|
|
|
*/
|
|
|
|
private $moduleName;
|
2019-08-12 18:13:58 +02:00
|
|
|
/**
|
|
|
|
* @var array The arguments of the current execution
|
|
|
|
*/
|
|
|
|
private $argv;
|
|
|
|
/**
|
|
|
|
* @var int The count of arguments
|
|
|
|
*/
|
|
|
|
private $argc;
|
2022-01-02 22:21:41 +01:00
|
|
|
/**
|
|
|
|
* @var string The used HTTP method
|
|
|
|
*/
|
|
|
|
private $method;
|
2019-08-12 18:13:58 +02:00
|
|
|
|
2022-01-02 22:21:41 +01:00
|
|
|
public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0, string $method = Router::GET)
|
2019-08-12 18:13:58 +02:00
|
|
|
{
|
|
|
|
$this->queryString = $queryString;
|
|
|
|
$this->command = $command;
|
2021-11-19 22:47:49 +01:00
|
|
|
$this->moduleName = $moduleName;
|
2019-08-12 18:13:58 +02:00
|
|
|
$this->argv = $argv;
|
|
|
|
$this->argc = $argc;
|
2022-01-02 22:21:41 +01:00
|
|
|
$this->method = $method;
|
2019-08-12 18:13:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-09 06:24:44 +02:00
|
|
|
* @return string The whole query string of this call with url-encoded query parameters
|
2019-08-12 18:13:58 +02:00
|
|
|
*/
|
|
|
|
public function getQueryString()
|
|
|
|
{
|
|
|
|
return $this->queryString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string The whole command of this call
|
|
|
|
*/
|
2022-06-16 16:49:43 +02:00
|
|
|
public function getCommand(): string
|
2019-08-12 18:13:58 +02:00
|
|
|
{
|
|
|
|
return $this->command;
|
|
|
|
}
|
|
|
|
|
2021-11-19 22:47:49 +01:00
|
|
|
/**
|
|
|
|
* @return string The module name based on the arguments
|
2022-11-26 23:32:56 +01:00
|
|
|
* @deprecated 2022.12 - With the new (sub-)routes, it's not trustworthy anymore, use the ModuleClass instead
|
2022-11-25 23:43:07 +01:00
|
|
|
* @see Router::getModuleClass()
|
2021-11-19 22:47:49 +01:00
|
|
|
*/
|
|
|
|
public function getModuleName(): string
|
|
|
|
{
|
|
|
|
return $this->moduleName;
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
/**
|
|
|
|
* @return array All arguments of this call
|
|
|
|
*/
|
2022-06-16 16:49:43 +02:00
|
|
|
public function getArgv(): array
|
2019-08-12 18:13:58 +02:00
|
|
|
{
|
|
|
|
return $this->argv;
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:26:22 +01:00
|
|
|
/**
|
|
|
|
* @return string The used HTTP method
|
|
|
|
*/
|
2022-06-16 16:49:43 +02:00
|
|
|
public function getMethod(): string
|
2022-01-02 22:21:41 +01:00
|
|
|
{
|
|
|
|
return $this->method;
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
/**
|
|
|
|
* @return int The count of arguments of this call
|
|
|
|
*/
|
2022-06-16 16:49:43 +02:00
|
|
|
public function getArgc(): int
|
2019-08-12 18:13:58 +02:00
|
|
|
{
|
|
|
|
return $this->argc;
|
|
|
|
}
|
|
|
|
|
2021-07-25 16:27:13 +02:00
|
|
|
public function setArgv(array $argv)
|
|
|
|
{
|
|
|
|
$this->argv = $argv;
|
|
|
|
$this->argc = count($argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setArgc(int $argc)
|
|
|
|
{
|
|
|
|
$this->argc = $argc;
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:13:58 +02:00
|
|
|
/**
|
|
|
|
* Returns the value of a argv key
|
2019-10-16 14:35:14 +02:00
|
|
|
* @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method
|
2019-08-12 18:13:58 +02:00
|
|
|
*
|
|
|
|
* @param int $position the position of the argument
|
|
|
|
* @param mixed $default the default value if not found
|
|
|
|
*
|
|
|
|
* @return mixed returns the value of the argument
|
|
|
|
*/
|
|
|
|
public function get(int $position, $default = '')
|
|
|
|
{
|
|
|
|
return $this->has($position) ? $this->argv[$position] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $position
|
|
|
|
*
|
|
|
|
* @return bool if the argument position exists
|
|
|
|
*/
|
2022-06-16 16:49:43 +02:00
|
|
|
public function has(int $position): bool
|
2019-08-12 18:13:58 +02:00
|
|
|
{
|
|
|
|
return array_key_exists($position, $this->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the arguments of the current call
|
|
|
|
*
|
|
|
|
* @param array $server The $_SERVER variable
|
|
|
|
* @param array $get The $_GET variable
|
|
|
|
*
|
|
|
|
* @return Arguments The determined arguments
|
|
|
|
*/
|
2022-06-16 16:49:43 +02:00
|
|
|
public function determine(array $server, array $get): Arguments
|
2019-08-12 18:13:58 +02:00
|
|
|
{
|
2020-09-09 06:24:44 +02:00
|
|
|
// removing leading / - maybe a nginx problem
|
|
|
|
$server['QUERY_STRING'] = ltrim($server['QUERY_STRING'] ?? '', '/');
|
2019-08-12 18:13:58 +02:00
|
|
|
|
2020-09-09 06:24:44 +02:00
|
|
|
$queryParameters = [];
|
|
|
|
parse_str($server['QUERY_STRING'], $queryParameters);
|
2019-08-12 18:13:58 +02:00
|
|
|
|
|
|
|
if (!empty($get['pagename'])) {
|
|
|
|
$command = trim($get['pagename'], '/\\');
|
2020-09-09 06:24:44 +02:00
|
|
|
} elseif (!empty($queryParameters['pagename'])) {
|
|
|
|
$command = trim($queryParameters['pagename'], '/\\');
|
2019-08-12 18:13:58 +02:00
|
|
|
} elseif (!empty($get['q'])) {
|
2020-09-09 06:24:44 +02:00
|
|
|
// Legacy page name parameter, now conflicts with the search query parameter
|
2019-08-12 18:13:58 +02:00
|
|
|
$command = trim($get['q'], '/\\');
|
|
|
|
} else {
|
2020-09-09 06:24:44 +02:00
|
|
|
$command = '';
|
2019-08-12 18:13:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 06:24:44 +02:00
|
|
|
// Remove generated and one-time use parameters
|
|
|
|
unset($queryParameters['pagename']);
|
|
|
|
unset($queryParameters['zrl']);
|
|
|
|
unset($queryParameters['owt']);
|
2019-08-12 18:13:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Break the URL path into C style argc/argv style arguments for our
|
|
|
|
* modules. Given "http://example.com/module/arg1/arg2", $this->argc
|
|
|
|
* will be 3 (integer) and $this->argv will contain:
|
|
|
|
* [0] => 'module'
|
|
|
|
* [1] => 'arg1'
|
|
|
|
* [2] => 'arg2'
|
|
|
|
*/
|
2020-09-09 06:24:44 +02:00
|
|
|
if ($command) {
|
|
|
|
$argv = explode('/', $command);
|
|
|
|
} else {
|
|
|
|
$argv = [];
|
|
|
|
}
|
2019-08-12 18:13:58 +02:00
|
|
|
|
|
|
|
$argc = count($argv);
|
|
|
|
|
2020-09-09 06:24:44 +02:00
|
|
|
$queryString = $command . ($queryParameters ? '?' . http_build_query($queryParameters) : '');
|
2019-08-12 18:13:58 +02:00
|
|
|
|
2021-11-19 22:47:49 +01:00
|
|
|
if ($argc > 0) {
|
|
|
|
$module = str_replace('.', '_', $argv[0]);
|
|
|
|
$module = str_replace('-', '_', $module);
|
|
|
|
} else {
|
|
|
|
$module = self::DEFAULT_MODULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compatibility with the Firefox App
|
|
|
|
if (($module == "users") && ($command == "users/sign_in")) {
|
|
|
|
$module = "login";
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:21:41 +01:00
|
|
|
$httpMethod = in_array($server['REQUEST_METHOD'] ?? '', Router::ALLOWED_METHODS) ? $server['REQUEST_METHOD'] : Router::GET;
|
|
|
|
|
|
|
|
return new Arguments($queryString, $command, $module, $argv, $argc, $httpMethod);
|
2019-08-12 18:13:58 +02:00
|
|
|
}
|
2020-09-09 06:24:44 +02:00
|
|
|
}
|