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