3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\App;
25 * Determine all arguments of the current call, including
26 * - The whole querystring (except the pagename/q parameter)
28 * - The arguments (C-Style based)
29 * - The count of arguments
33 const DEFAULT_MODULE = 'home';
36 * @var string The complete query string
40 * @var string The current Friendica command
44 * @var string The name of the current module
48 * @var array The arguments of the current execution
52 * @var int The count of arguments
56 * @var string The used HTTP method
60 public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0, string $method = Router::GET)
62 $this->queryString = $queryString;
63 $this->command = $command;
64 $this->moduleName = $moduleName;
67 $this->method = $method;
71 * @return string The whole query string of this call with url-encoded query parameters
73 public function getQueryString()
75 return $this->queryString;
79 * @return string The whole command of this call
81 public function getCommand(): string
83 return $this->command;
87 * @return string The module name based on the arguments
88 * @deprecated 2022.12 - With the new (sub-)routes, it's not trustworthy anymore, use the ModuleClass instead
89 * @see Router::getModuleClass()
91 public function getModuleName(): string
93 return $this->moduleName;
97 * @return array All arguments of this call
99 public function getArgv(): array
105 * @return string The used HTTP method
107 public function getMethod(): string
109 return $this->method;
113 * @return int The count of arguments of this call
115 public function getArgc(): int
120 public function setArgv(array $argv)
123 $this->argc = count($argv);
126 public function setArgc(int $argc)
132 * Returns the value of a argv key
133 * @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method
135 * @param int $position the position of the argument
136 * @param mixed $default the default value if not found
138 * @return mixed returns the value of the argument
140 public function get(int $position, $default = '')
142 return $this->has($position) ? $this->argv[$position] : $default;
146 * @param int $position
148 * @return bool if the argument position exists
150 public function has(int $position): bool
152 return array_key_exists($position, $this->argv);
156 * Determine the arguments of the current call
158 * @param array $server The $_SERVER variable
159 * @param array $get The $_GET variable
161 * @return Arguments The determined arguments
163 public function determine(array $server, array $get): Arguments
165 // removing leading / - maybe a nginx problem
166 $server['QUERY_STRING'] = ltrim($server['QUERY_STRING'] ?? '', '/');
168 $queryParameters = [];
169 parse_str($server['QUERY_STRING'], $queryParameters);
171 if (!empty($get['pagename'])) {
172 $command = trim($get['pagename'], '/\\');
173 } elseif (!empty($queryParameters['pagename'])) {
174 $command = trim($queryParameters['pagename'], '/\\');
175 } elseif (!empty($get['q'])) {
176 // Legacy page name parameter, now conflicts with the search query parameter
177 $command = trim($get['q'], '/\\');
182 // Remove generated and one-time use parameters
183 unset($queryParameters['pagename']);
184 unset($queryParameters['zrl']);
185 unset($queryParameters['owt']);
188 * Break the URL path into C style argc/argv style arguments for our
189 * modules. Given "http://example.com/module/arg1/arg2", $this->argc
190 * will be 3 (integer) and $this->argv will contain:
196 $argv = explode('/', $command);
201 $argc = count($argv);
203 $queryString = $command . ($queryParameters ? '?' . http_build_query($queryParameters) : '');
206 $module = str_replace('.', '_', $argv[0]);
207 $module = str_replace('-', '_', $module);
209 $module = self::DEFAULT_MODULE;
212 // Compatibility with the Firefox App
213 if (($module == "users") && ($command == "users/sign_in")) {
217 $httpMethod = in_array($server['REQUEST_METHOD'] ?? '', Router::ALLOWED_METHODS) ? $server['REQUEST_METHOD'] : Router::GET;
219 return new Arguments($queryString, $command, $module, $argv, $argc, $httpMethod);