]> git.mxchange.org Git - friendica.git/blob - src/App/Arguments.php
Merge pull request #7752 from kPherox/develop
[friendica.git] / src / App / Arguments.php
1 <?php
2
3 namespace Friendica\App;
4
5 /**
6  * Determine all arguments of the current call, including
7  * - The whole querystring (except the pagename/q parameter)
8  * - The command
9  * - The arguments (C-Style based)
10  * - The count of arguments
11  */
12 class Arguments
13 {
14         /**
15          * @var string The complete query string
16          */
17         private $queryString;
18         /**
19          * @var string The current Friendica command
20          */
21         private $command;
22         /**
23          * @var array The arguments of the current execution
24          */
25         private $argv;
26         /**
27          * @var int The count of arguments
28          */
29         private $argc;
30
31         public function __construct(string $queryString = '', string $command = '', array $argv = [Module::DEFAULT], int $argc = 1)
32         {
33                 $this->queryString = $queryString;
34                 $this->command     = $command;
35                 $this->argv        = $argv;
36                 $this->argc        = $argc;
37         }
38
39         /**
40          * @return string The whole query string of this call
41          */
42         public function getQueryString()
43         {
44                 return $this->queryString;
45         }
46
47         /**
48          * @return string The whole command of this call
49          */
50         public function getCommand()
51         {
52                 return $this->command;
53         }
54
55         /**
56          * @return array All arguments of this call
57          */
58         public function getArgv()
59         {
60                 return $this->argv;
61         }
62
63         /**
64          * @return int The count of arguments of this call
65          */
66         public function getArgc()
67         {
68                 return $this->argc;
69         }
70
71         /**
72          * Returns the value of a argv key
73          * @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method
74          *
75          * @param int   $position the position of the argument
76          * @param mixed $default  the default value if not found
77          *
78          * @return mixed returns the value of the argument
79          */
80         public function get(int $position, $default = '')
81         {
82                 return $this->has($position) ? $this->argv[$position] : $default;
83         }
84
85         /**
86          * @param int $position
87          *
88          * @return bool if the argument position exists
89          */
90         public function has(int $position)
91         {
92                 return array_key_exists($position, $this->argv);
93         }
94
95         /**
96          * Determine the arguments of the current call
97          *
98          * @param array $server The $_SERVER variable
99          * @param array $get    The $_GET variable
100          *
101          * @return Arguments The determined arguments
102          */
103         public function determine(array $server, array $get)
104         {
105                 $queryString = '';
106
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);
111                 }
112
113                 // eventually strip ZRL
114                 $queryString = $this->stripZRLs($queryString);
115
116                 // eventually strip OWT
117                 $queryString = $this->stripQueryParam($queryString, 'owt');
118
119                 // removing trailing / - maybe a nginx problem
120                 $queryString = ltrim($queryString, '/');
121
122                 if (!empty($get['pagename'])) {
123                         $command = trim($get['pagename'], '/\\');
124                 } elseif (!empty($get['q'])) {
125                         $command = trim($get['q'], '/\\');
126                 } else {
127                         $command = Module::DEFAULT;
128                 }
129
130
131                 // fix query_string
132                 if (!empty($command)) {
133                         $queryString = str_replace(
134                                 $command . '&',
135                                 $command . '?',
136                                 $queryString
137                         );
138                 }
139
140                 // unix style "homedir"
141                 if (substr($command, 0, 1) === '~') {
142                         $command = 'profile/' . substr($command, 1);
143                 }
144
145                 // Diaspora style profile url
146                 if (substr($command, 0, 2) === 'u/') {
147                         $command = 'profile/' . substr($command, 2);
148                 }
149
150                 /*
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:
154                  *   [0] => 'module'
155                  *   [1] => 'arg1'
156                  *   [2] => 'arg2'
157                  *
158                  *
159                  * There will always be one argument. If provided a naked domain
160                  * URL, $this->argv[0] is set to "home".
161                  */
162
163                 $argv = explode('/', $command);
164                 $argc = count($argv);
165
166
167                 return new Arguments($queryString, $command, $argv, $argc);
168         }
169
170         /**
171          * Strip zrl parameter from a string.
172          *
173          * @param string $queryString The input string.
174          *
175          * @return string The zrl.
176          */
177         public function stripZRLs(string $queryString)
178         {
179                 return preg_replace('/[?&]zrl=(.*?)(&|$)/ism', '$2', $queryString);
180         }
181
182         /**
183          * Strip query parameter from a string.
184          *
185          * @param string $queryString The input string.
186          * @param string $param
187          *
188          * @return string The query parameter.
189          */
190         public function stripQueryParam(string $queryString, string $param)
191         {
192                 return preg_replace('/[?&]' . $param . '=(.*?)(&|$)/ism', '$2', $queryString);
193         }
194 }