]> git.mxchange.org Git - friendica.git/blob - src/App/Arguments.php
Merge pull request #10953 from annando/bott-shrinked
[friendica.git] / src / App / Arguments.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\App;
23
24 /**
25  * Determine all arguments of the current call, including
26  * - The whole querystring (except the pagename/q parameter)
27  * - The command
28  * - The arguments (C-Style based)
29  * - The count of arguments
30  */
31 class Arguments
32 {
33         /**
34          * @var string The complete query string
35          */
36         private $queryString;
37         /**
38          * @var string The current Friendica command
39          */
40         private $command;
41         /**
42          * @var array The arguments of the current execution
43          */
44         private $argv;
45         /**
46          * @var int The count of arguments
47          */
48         private $argc;
49
50         public function __construct(string $queryString = '', string $command = '', array $argv = [], int $argc = 0)
51         {
52                 $this->queryString = $queryString;
53                 $this->command     = $command;
54                 $this->argv        = $argv;
55                 $this->argc        = $argc;
56         }
57
58         /**
59          * @return string The whole query string of this call with url-encoded query parameters
60          */
61         public function getQueryString()
62         {
63                 return $this->queryString;
64         }
65
66         /**
67          * @return string The whole command of this call
68          */
69         public function getCommand()
70         {
71                 return $this->command;
72         }
73
74         /**
75          * @return array All arguments of this call
76          */
77         public function getArgv()
78         {
79                 return $this->argv;
80         }
81
82         /**
83          * @return int The count of arguments of this call
84          */
85         public function getArgc()
86         {
87                 return $this->argc;
88         }
89
90         public function setArgv(array $argv)
91         {
92                 $this->argv = $argv;
93                 $this->argc = count($argv);
94         }
95
96         public function setArgc(int $argc)
97         {
98                 $this->argc = $argc;
99         }
100
101         /**
102          * Returns the value of a argv key
103          * @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method
104          *
105          * @param int   $position the position of the argument
106          * @param mixed $default  the default value if not found
107          *
108          * @return mixed returns the value of the argument
109          */
110         public function get(int $position, $default = '')
111         {
112                 return $this->has($position) ? $this->argv[$position] : $default;
113         }
114
115         /**
116          * @param int $position
117          *
118          * @return bool if the argument position exists
119          */
120         public function has(int $position)
121         {
122                 return array_key_exists($position, $this->argv);
123         }
124
125         /**
126          * Determine the arguments of the current call
127          *
128          * @param array $server The $_SERVER variable
129          * @param array $get    The $_GET variable
130          *
131          * @return Arguments The determined arguments
132          */
133         public function determine(array $server, array $get)
134         {
135                 // removing leading / - maybe a nginx problem
136                 $server['QUERY_STRING'] = ltrim($server['QUERY_STRING'] ?? '', '/');
137
138                 $queryParameters = [];
139                 parse_str($server['QUERY_STRING'], $queryParameters);
140
141                 if (!empty($get['pagename'])) {
142                         $command = trim($get['pagename'], '/\\');
143                 } elseif (!empty($queryParameters['pagename'])) {
144                         $command = trim($queryParameters['pagename'], '/\\');
145                 } elseif (!empty($get['q'])) {
146                         // Legacy page name parameter, now conflicts with the search query parameter
147                         $command = trim($get['q'], '/\\');
148                 } else {
149                         $command = '';
150                 }
151
152                 // Remove generated and one-time use parameters
153                 unset($queryParameters['pagename']);
154                 unset($queryParameters['zrl']);
155                 unset($queryParameters['owt']);
156
157                 /*
158                  * Break the URL path into C style argc/argv style arguments for our
159                  * modules. Given "http://example.com/module/arg1/arg2", $this->argc
160                  * will be 3 (integer) and $this->argv will contain:
161                  *   [0] => 'module'
162                  *   [1] => 'arg1'
163                  *   [2] => 'arg2'
164                  */
165                 if ($command) {
166                         $argv = explode('/', $command);
167                 } else {
168                         $argv = [];
169                 }
170
171                 $argc = count($argv);
172
173                 $queryString = $command . ($queryParameters ? '?' . http_build_query($queryParameters) : '');
174
175                 return new Arguments($queryString, $command, $argv, $argc);
176         }
177 }