]> git.mxchange.org Git - friendica.git/blob - src/App/Arguments.php
Split and delete `ModuleController`
[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         const DEFAULT_MODULE = 'home';
34
35         /**
36          * @var string The complete query string
37          */
38         private $queryString;
39         /**
40          * @var string The current Friendica command
41          */
42         private $command;
43         /**
44          * @var string The name of the current module
45          */
46         private $moduleName;
47         /**
48          * @var array The arguments of the current execution
49          */
50         private $argv;
51         /**
52          * @var int The count of arguments
53          */
54         private $argc;
55
56         public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0)
57         {
58                 $this->queryString = $queryString;
59                 $this->command     = $command;
60                 $this->moduleName  = $moduleName;
61                 $this->argv        = $argv;
62                 $this->argc        = $argc;
63         }
64
65         /**
66          * @return string The whole query string of this call with url-encoded query parameters
67          */
68         public function getQueryString()
69         {
70                 return $this->queryString;
71         }
72
73         /**
74          * @return string The whole command of this call
75          */
76         public function getCommand()
77         {
78                 return $this->command;
79         }
80
81         /**
82          * @return string The module name based on the arguments
83          */
84         public function getModuleName(): string
85         {
86                 return $this->moduleName;
87         }
88
89         /**
90          * @return array All arguments of this call
91          */
92         public function getArgv()
93         {
94                 return $this->argv;
95         }
96
97         /**
98          * @return int The count of arguments of this call
99          */
100         public function getArgc()
101         {
102                 return $this->argc;
103         }
104
105         public function setArgv(array $argv)
106         {
107                 $this->argv = $argv;
108                 $this->argc = count($argv);
109         }
110
111         public function setArgc(int $argc)
112         {
113                 $this->argc = $argc;
114         }
115
116         /**
117          * Returns the value of a argv key
118          * @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method
119          *
120          * @param int   $position the position of the argument
121          * @param mixed $default  the default value if not found
122          *
123          * @return mixed returns the value of the argument
124          */
125         public function get(int $position, $default = '')
126         {
127                 return $this->has($position) ? $this->argv[$position] : $default;
128         }
129
130         /**
131          * @param int $position
132          *
133          * @return bool if the argument position exists
134          */
135         public function has(int $position)
136         {
137                 return array_key_exists($position, $this->argv);
138         }
139
140         /**
141          * Determine the arguments of the current call
142          *
143          * @param array $server The $_SERVER variable
144          * @param array $get    The $_GET variable
145          *
146          * @return Arguments The determined arguments
147          */
148         public function determine(array $server, array $get)
149         {
150                 // removing leading / - maybe a nginx problem
151                 $server['QUERY_STRING'] = ltrim($server['QUERY_STRING'] ?? '', '/');
152
153                 $queryParameters = [];
154                 parse_str($server['QUERY_STRING'], $queryParameters);
155
156                 if (!empty($get['pagename'])) {
157                         $command = trim($get['pagename'], '/\\');
158                 } elseif (!empty($queryParameters['pagename'])) {
159                         $command = trim($queryParameters['pagename'], '/\\');
160                 } elseif (!empty($get['q'])) {
161                         // Legacy page name parameter, now conflicts with the search query parameter
162                         $command = trim($get['q'], '/\\');
163                 } else {
164                         $command = '';
165                 }
166
167                 // Remove generated and one-time use parameters
168                 unset($queryParameters['pagename']);
169                 unset($queryParameters['zrl']);
170                 unset($queryParameters['owt']);
171
172                 /*
173                  * Break the URL path into C style argc/argv style arguments for our
174                  * modules. Given "http://example.com/module/arg1/arg2", $this->argc
175                  * will be 3 (integer) and $this->argv will contain:
176                  *   [0] => 'module'
177                  *   [1] => 'arg1'
178                  *   [2] => 'arg2'
179                  */
180                 if ($command) {
181                         $argv = explode('/', $command);
182                 } else {
183                         $argv = [];
184                 }
185
186                 $argc = count($argv);
187
188                 $queryString = $command . ($queryParameters ? '?' . http_build_query($queryParameters) : '');
189
190                 if ($argc > 0) {
191                         $module = str_replace('.', '_', $argv[0]);
192                         $module = str_replace('-', '_', $module);
193                 } else {
194                         $module = self::DEFAULT_MODULE;
195                 }
196
197                 // Compatibility with the Firefox App
198                 if (($module == "users") && ($command == "users/sign_in")) {
199                         $module = "login";
200                 }
201
202                 return new Arguments($queryString, $command, $module, $argv, $argc);
203         }
204 }