]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Special/HTTPException.php
Add OPTIONS endpoint
[friendica.git] / src / Module / Special / HTTPException.php
index 6446ec38cb3bc764cbba57504204e787dd3a1486..34bb67538555dd37e8103d59ba9a76263924c528 100644 (file)
@@ -1,12 +1,30 @@
 <?php
-
+/**
+ * @copyright Copyright (C) 2010-2022, 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\Module\Special;
 
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\DI;
 
 /**
  * This special module displays HTTPException when they are thrown in modules.
@@ -25,34 +43,20 @@ class HTTPException
         */
        private static function getVars(\Friendica\Network\HTTPException $e)
        {
-               $message = $e->getMessage();
-
-               $titles = [
-                       200 => 'OK',
-                       400 => L10n::t('Bad Request'),
-                       401 => L10n::t('Unauthorized'),
-                       403 => L10n::t('Forbidden'),
-                       404 => L10n::t('Not Found'),
-                       500 => L10n::t('Internal Server Error'),
-                       503 => L10n::t('Service Unavailable'),
+               // Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
+               $vars = [
+                       '$title' => $e->getDescription() ?: 'Error ' . $e->getCode(),
+                       '$message' => $e->getMessage() ?: $e->getExplanation(),
+                       '$back' => DI::l10n()->t('Go back'),
+                       '$stack_trace' => DI::l10n()->t('Stack trace:'),
                ];
-               $title = defaults($titles, $e->getCode(), 'Error ' . $e->getCode());
-
-               if (empty($message)) {
-                       // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
-                       $explanation = [
-                               400 => L10n::t('The server cannot or will not process the request due to an apparent client error.'),
-                               401 => L10n::t('Authentication is required and has failed or has not yet been provided.'),
-                               403 => L10n::t('The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account.'),
-                               404 => L10n::t('The requested resource could not be found but may be available in the future.'),
-                               500 => L10n::t('An unexpected condition was encountered and no more specific message is suitable.'),
-                               503 => L10n::t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
-                       ];
 
-                       $message = defaults($explanation, $e->getCode(), '');
+               if (DI::app()->isSiteAdmin()) {
+                       $vars['$thrown'] = DI::l10n()->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
+                       $vars['$trace'] = $e->getTraceAsString();
                }
 
-               return ['$title' => $title, '$message' => $message, '$back' => L10n::t('Go back')];
+               return $vars;
        }
 
        /**
@@ -61,7 +65,7 @@ class HTTPException
         * @param \Friendica\Network\HTTPException $e
         * @throws \Exception
         */
-       public static function rawContent(\Friendica\Network\HTTPException $e)
+       public function rawContent(\Friendica\Network\HTTPException $e)
        {
                $content = '';
 
@@ -70,7 +74,7 @@ class HTTPException
                        $content = Renderer::replaceMacros($tpl, self::getVars($e));
                }
 
-               System::httpExit($e->getCode(), $e->httpdesc, $content);
+               System::httpExit($e->getCode(), $e->getDescription(), $content);
        }
 
        /**
@@ -80,9 +84,13 @@ class HTTPException
         * @return string
         * @throws \Exception
         */
-       public static function content(\Friendica\Network\HTTPException $e)
+       public function content(\Friendica\Network\HTTPException $e): string
        {
-               header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
+               header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->getDescription());
+
+               if ($e->getCode() >= 400) {
+                       Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->getDescription(), 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
+               }
 
                $tpl = Renderer::getMarkupTemplate('exception.tpl');