]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Arguments.php
[frio] Add Mute Author Server button to post actions
[friendica.git] / src / App / Arguments.php
index e65309f6b0ab18dd38bc4ffebd0dcaa266139683..6f527d2f4def937fa4a7619690455422f98253cb 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\App;
 
@@ -11,6 +30,8 @@ namespace Friendica\App;
  */
 class Arguments
 {
+       const DEFAULT_MODULE = 'home';
+
        /**
         * @var string The complete query string
         */
@@ -19,6 +40,10 @@ class Arguments
         * @var string The current Friendica command
         */
        private $command;
+       /**
+        * @var string The name of the current module
+        */
+       private $moduleName;
        /**
         * @var array The arguments of the current execution
         */
@@ -27,17 +52,23 @@ class Arguments
         * @var int The count of arguments
         */
        private $argc;
+       /**
+        * @var string The used HTTP method
+        */
+       private $method;
 
-       public function __construct(string $queryString = '', string $command = '', array $argv = [Module::DEFAULT], int $argc = 1)
+       public function __construct(string $queryString = '', string $command = '', string $moduleName = '', array $argv = [], int $argc = 0, string $method = Router::GET)
        {
                $this->queryString = $queryString;
                $this->command     = $command;
+               $this->moduleName  = $moduleName;
                $this->argv        = $argv;
                $this->argc        = $argc;
+               $this->method      = $method;
        }
 
        /**
-        * @return string The whole query string of this call
+        * @return string The whole query string of this call with url-encoded query parameters
         */
        public function getQueryString()
        {
@@ -47,27 +78,56 @@ class Arguments
        /**
         * @return string The whole command of this call
         */
-       public function getCommand()
+       public function getCommand(): string
        {
                return $this->command;
        }
 
+       /**
+        * @return string The module name based on the arguments
+        * @deprecated 2022.12 - With the new (sub-)routes, it's not trustworthy anymore, use the ModuleClass instead
+        * @see Router::getModuleClass()
+        */
+       public function getModuleName(): string
+       {
+               return $this->moduleName;
+       }
+
        /**
         * @return array All arguments of this call
         */
-       public function getArgv()
+       public function getArgv(): array
        {
                return $this->argv;
        }
 
+       /**
+        * @return string The used HTTP method
+        */
+       public function getMethod(): string
+       {
+               return $this->method;
+       }
+
        /**
         * @return int The count of arguments of this call
         */
-       public function getArgc()
+       public function getArgc(): int
        {
                return $this->argc;
        }
 
+       public function setArgv(array $argv)
+       {
+               $this->argv = $argv;
+               $this->argc = count($argv);
+       }
+
+       public function setArgc(int $argc)
+       {
+               $this->argc = $argc;
+       }
+
        /**
         * Returns the value of a argv key
         * @todo there are a lot of $a->argv usages in combination with ?? which can be replaced with this method
@@ -87,7 +147,7 @@ class Arguments
         *
         * @return bool if the argument position exists
         */
-       public function has(int $position)
+       public function has(int $position): bool
        {
                return array_key_exists($position, $this->argv);
        }
@@ -100,52 +160,29 @@ class Arguments
         *
         * @return Arguments The determined arguments
         */
-       public function determine(array $server, array $get)
+       public function determine(array $server, array $get): Arguments
        {
-               $queryString = '';
-
-               if (!empty($server['QUERY_STRING']) && strpos($server['QUERY_STRING'], 'pagename=') === 0) {
-                       $queryString = substr($server['QUERY_STRING'], 9);
-               } elseif (!empty($server['QUERY_STRING']) && strpos($server['QUERY_STRING'], 'q=') === 0) {
-                       $queryString = substr($server['QUERY_STRING'], 2);
-               }
-
-               // eventually strip ZRL
-               $queryString = $this->stripZRLs($queryString);
+               // removing leading / - maybe a nginx problem
+               $server['QUERY_STRING'] = ltrim($server['QUERY_STRING'] ?? '', '/');
 
-               // eventually strip OWT
-               $queryString = $this->stripQueryParam($queryString, 'owt');
-
-               // removing trailing / - maybe a nginx problem
-               $queryString = ltrim($queryString, '/');
+               $queryParameters = [];
+               parse_str($server['QUERY_STRING'], $queryParameters);
 
                if (!empty($get['pagename'])) {
                        $command = trim($get['pagename'], '/\\');
+               } elseif (!empty($queryParameters['pagename'])) {
+                       $command = trim($queryParameters['pagename'], '/\\');
                } elseif (!empty($get['q'])) {
+                       // Legacy page name parameter, now conflicts with the search query parameter
                        $command = trim($get['q'], '/\\');
                } else {
-                       $command = Module::DEFAULT;
-               }
-
-
-               // fix query_string
-               if (!empty($command)) {
-                       $queryString = str_replace(
-                               $command . '&',
-                               $command . '?',
-                               $queryString
-                       );
+                       $command = '';
                }
 
-               // unix style "homedir"
-               if (substr($command, 0, 1) === '~') {
-                       $command = 'profile/' . substr($command, 1);
-               }
-
-               // Diaspora style profile url
-               if (substr($command, 0, 2) === 'u/') {
-                       $command = 'profile/' . substr($command, 2);
-               }
+               // Remove generated and one-time use parameters
+               unset($queryParameters['pagename']);
+               unset($queryParameters['zrl']);
+               unset($queryParameters['owt']);
 
                /*
                 * Break the URL path into C style argc/argv style arguments for our
@@ -154,41 +191,31 @@ class Arguments
                 *   [0] => 'module'
                 *   [1] => 'arg1'
                 *   [2] => 'arg2'
-                *
-                *
-                * There will always be one argument. If provided a naked domain
-                * URL, $this->argv[0] is set to "home".
                 */
+               if ($command) {
+                       $argv = explode('/', $command);
+               } else {
+                       $argv = [];
+               }
 
-               $argv = explode('/', $command);
                $argc = count($argv);
 
+               $queryString = $command . ($queryParameters ? '?' . http_build_query($queryParameters) : '');
 
-               return new Arguments($queryString, $command, $argv, $argc);
-       }
+               if ($argc > 0) {
+                       $module = str_replace('.', '_', $argv[0]);
+                       $module = str_replace('-', '_', $module);
+               } else {
+                       $module = self::DEFAULT_MODULE;
+               }
 
-       /**
-        * Strip zrl parameter from a string.
-        *
-        * @param string $queryString The input string.
-        *
-        * @return string The zrl.
-        */
-       public function stripZRLs(string $queryString)
-       {
-               return preg_replace('/[?&]zrl=(.*?)(&|$)/ism', '$2', $queryString);
-       }
+               // Compatibility with the Firefox App
+               if (($module == "users") && ($command == "users/sign_in")) {
+                       $module = "login";
+               }
 
-       /**
-        * Strip query parameter from a string.
-        *
-        * @param string $queryString The input string.
-        * @param string $param
-        *
-        * @return string The query parameter.
-        */
-       public function stripQueryParam(string $queryString, string $param)
-       {
-               return preg_replace('/[?&]' . $param . '=(.*?)(&|$)/ism', '$2', $queryString);
+               $httpMethod = in_array($server['REQUEST_METHOD'] ?? '', Router::ALLOWED_METHODS) ? $server['REQUEST_METHOD'] : Router::GET;
+
+               return new Arguments($queryString, $command, $module, $argv, $argc, $httpMethod);
        }
-}
\ No newline at end of file
+}