3 namespace Friendica\App;
6 * Determine all arguments of the current call, including
7 * - The whole querystring (except the pagename/q parameter)
9 * - The arguments (C-Style based)
10 * - The count of arguments
15 * @var string The complete query string
19 * @var string The current Friendica command
23 * @var array The arguments of the current execution
27 * @var int The count of arguments
31 public function __construct(string $queryString = '', string $command = '', array $argv = [Module::DEFAULT], int $argc = 1)
33 $this->queryString = $queryString;
34 $this->command = $command;
40 * @return string The whole query string of this call
42 public function getQueryString()
44 return $this->queryString;
48 * @return string The whole command of this call
50 public function getCommand()
52 return $this->command;
56 * @return array All arguments of this call
58 public function getArgv()
64 * @return int The count of arguments of this call
66 public function getArgc()
72 * Returns the value of a argv key
73 * @todo there are a lot of $a->argv usages in combination with defaults() which can be replaced with this method
75 * @param int $position the position of the argument
76 * @param mixed $default the default value if not found
78 * @return mixed returns the value of the argument
80 public function get(int $position, $default = '')
82 return $this->has($position) ? $this->argv[$position] : $default;
86 * @param int $position
88 * @return bool if the argument position exists
90 public function has(int $position)
92 return array_key_exists($position, $this->argv);
96 * Determine the arguments of the current call
98 * @param array $server The $_SERVER variable
99 * @param array $get The $_GET variable
101 * @return Arguments The determined arguments
103 public function determine(array $server, array $get)
107 if (!empty($server['QUERY_STRING']) && strpos($server['QUERY_STRING'], 'pagename=') === 0) {
108 $queryString = substr($server['QUERY_STRING'], 9);
109 } elseif (!empty($server['QUERY_STRING']) && strpos($server['QUERY_STRING'], 'q=') === 0) {
110 $queryString = substr($server['QUERY_STRING'], 2);
113 // eventually strip ZRL
114 $queryString = $this->stripZRLs($queryString);
116 // eventually strip OWT
117 $queryString = $this->stripQueryParam($queryString, 'owt');
119 // removing trailing / - maybe a nginx problem
120 $queryString = ltrim($queryString, '/');
122 if (!empty($get['pagename'])) {
123 $command = trim($get['pagename'], '/\\');
124 } elseif (!empty($get['q'])) {
125 $command = trim($get['q'], '/\\');
127 $command = Module::DEFAULT;
132 if (!empty($command)) {
133 $queryString = str_replace(
140 // unix style "homedir"
141 if (substr($command, 0, 1) === '~') {
142 $command = 'profile/' . substr($command, 1);
145 // Diaspora style profile url
146 if (substr($command, 0, 2) === 'u/') {
147 $command = 'profile/' . substr($command, 2);
151 * Break the URL path into C style argc/argv style arguments for our
152 * modules. Given "http://example.com/module/arg1/arg2", $this->argc
153 * will be 3 (integer) and $this->argv will contain:
159 * There will always be one argument. If provided a naked domain
160 * URL, $this->argv[0] is set to "home".
163 $argv = explode('/', $command);
164 $argc = count($argv);
167 return new Arguments($queryString, $command, $argv, $argc);
171 * Strip zrl parameter from a string.
173 * @param string $queryString The input string.
175 * @return string The zrl.
177 public function stripZRLs(string $queryString)
179 return preg_replace('/[?&]zrl=(.*?)(&|$)/ism', '$2', $queryString);
183 * Strip query parameter from a string.
185 * @param string $queryString The input string.
186 * @param string $param
188 * @return string The query parameter.
190 public function stripQueryParam(string $queryString, string $param)
192 return preg_replace('/[?&]' . $param . '=(.*?)(&|$)/ism', '$2', $queryString);