]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Module.php
Merge pull request #10813 from tobiasd/20211003-lengthcounter
[friendica.git] / src / App / Module.php
index 50688710d1c5b435d4a25f428ca60de0f850c5a6..f15b1236e81042708e97a56e0834026da05e1656 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, 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,7 @@ use Friendica\Module\HTTPException\MethodNotAllowed;
 use Friendica\Module\HTTPException\PageNotFound;
 use Friendica\Network\HTTPException\MethodNotAllowedException;
 use Friendica\Network\HTTPException\NotFoundException;
+use Friendica\Util\Profiler;
 use Psr\Log\LoggerInterface;
 
 /**
@@ -43,7 +63,6 @@ class Module
                'outbox',
                'poco',
                'post',
-               'proxy',
                'pubsub',
                'pubsubhubbub',
                'receive',
@@ -215,10 +234,10 @@ class Module
         *
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function run(Core\L10n $l10n, App\BaseURL $baseUrl, LoggerInterface $logger, array $server, array $post)
+       public function run(Core\L10n $l10n, App\BaseURL $baseUrl, LoggerInterface $logger, Profiler $profiler, array $server, array $post)
        {
                if ($this->printNotAllowedAddon) {
-                       info($l10n->t("You must be logged in to use addons. "));
+                       notice($l10n->t("You must be logged in to use addons. "));
                }
 
                /* The URL provided does not resolve to a valid module.
@@ -245,17 +264,66 @@ class Module
                        $logger->debug('index.php: page not found.', ['request_uri' => $server['REQUEST_URI'], 'address' => $server['REMOTE_ADDR'], 'query' => $server['QUERY_STRING']]);
                }
 
+               // @see https://github.com/tootsuite/mastodon/blob/c3aef491d66aec743a3a53e934a494f653745b61/config/initializers/cors.rb
+               if (substr($_REQUEST['pagename'] ?? '', 0, 12) == '.well-known/') {
+                       header('Access-Control-Allow-Origin: *');
+                       header('Access-Control-Allow-Headers: *');
+                       header('Access-Control-Allow-Methods: ' . Router::GET);
+                       header('Access-Control-Allow-Credentials: false');
+               } elseif (substr($_REQUEST['pagename'] ?? '', 0, 8) == 'profile/') {
+                       header('Access-Control-Allow-Origin: *');
+                       header('Access-Control-Allow-Headers: *');
+                       header('Access-Control-Allow-Methods: ' . Router::GET);
+                       header('Access-Control-Allow-Credentials: false');
+               } elseif (substr($_REQUEST['pagename'] ?? '', 0, 4) == 'api/') {
+                       header('Access-Control-Allow-Origin: *');
+                       header('Access-Control-Allow-Headers: *');
+                       header('Access-Control-Allow-Methods: ' . implode(',', Router::ALLOWED_METHODS));
+                       header('Access-Control-Allow-Credentials: false');
+                       header('Access-Control-Expose-Headers: Link');
+               } elseif (substr($_REQUEST['pagename'] ?? '', 0, 11) == 'oauth/token') {
+                       header('Access-Control-Allow-Origin: *');
+                       header('Access-Control-Allow-Headers: *');
+                       header('Access-Control-Allow-Methods: ' . Router::POST);
+                       header('Access-Control-Allow-Credentials: false');
+               }
+
+               // @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
+               // @todo Check allowed methods per requested path
+               if ($server['REQUEST_METHOD'] === Router::OPTIONS) {
+                       header('HTTP/1.1 204 No Content');
+                       header('Allow: ' . implode(',', Router::ALLOWED_METHODS));
+                       exit();
+               }
+
                $placeholder = '';
 
+               $profiler->set(microtime(true), 'ready');
+               $timestamp = microtime(true);
+
                Core\Hook::callAll($this->module . '_mod_init', $placeholder);
 
                call_user_func([$this->module_class, 'init'], $this->module_parameters);
 
-               if ($server['REQUEST_METHOD'] === 'POST') {
+               $profiler->set(microtime(true) - $timestamp, 'init');
+
+               if ($server['REQUEST_METHOD'] === Router::DELETE) {
+                       call_user_func([$this->module_class, 'delete'], $this->module_parameters);
+               }
+
+               if ($server['REQUEST_METHOD'] === Router::PATCH) {
+                       call_user_func([$this->module_class, 'patch'], $this->module_parameters);
+               }
+
+               if ($server['REQUEST_METHOD'] === Router::POST) {
                        Core\Hook::callAll($this->module . '_mod_post', $post);
                        call_user_func([$this->module_class, 'post'], $this->module_parameters);
                }
 
+               if ($server['REQUEST_METHOD'] === Router::PUT) {
+                       call_user_func([$this->module_class, 'put'], $this->module_parameters);
+               }
+
                Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
                call_user_func([$this->module_class, 'afterpost'], $this->module_parameters);