]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #6225 from annando/http-error
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 2 Dec 2018 21:15:04 +0000 (16:15 -0500)
committerGitHub <noreply@github.com>
Sun, 2 Dec 2018 21:15:04 +0000 (16:15 -0500)
Using the standard function to return http errors

mod/fetch.php
mod/pubsub.php
src/Core/System.php

index 9336c140475ef0b0b8edc102c9e5f07aa6aa59e7..5dcedb1aafdca58b6b8d0a01b58ed8611fe88155 100644 (file)
@@ -18,8 +18,7 @@ function fetch_init(App $a)
 {
 
        if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
-               header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
-               killme();
+               System::httpExit(404);
        }
 
        $guid = $a->argv[2];
@@ -45,15 +44,13 @@ function fetch_init(App $a)
                        }
                }
 
-               header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
-               killme();
+               System::httpExit(404);
        }
 
        // Fetch some data from the author (We could combine both queries - but I think this is more readable)
        $user = User::getOwnerDataById($item["uid"]);
        if (!$user) {
-               header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
-               killme();
+               System::httpExit(404);
        }
 
        $status = Diaspora::buildStatus($item, $user);
index f0a8d463c4517999d29773112e2aff9b45a4df13..cd2f21dd67904907cc73646688442e1e14c4498f 100644 (file)
@@ -7,6 +7,7 @@ use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Protocol\OStatus;
 use Friendica\Util\Strings;
+use Friendica\Core\System;
 
 require_once 'include/items.php';
 
@@ -16,7 +17,7 @@ function hub_return($valid, $body)
                header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
                echo $body;
        } else {
-               header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
+               System::httpExit(404);
        }
        killme();
 }
@@ -25,8 +26,7 @@ function hub_return($valid, $body)
 
 function hub_post_return()
 {
-       header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
-       killme();
+       System::httpExit(200);
 }
 
 function pubsub_init(App $a)
index d24581e996202a6b54be24bfc626abcfb4fec305..eadbf98d2648b6ae4fb89c8650f982b3196cf1d4 100644 (file)
@@ -126,9 +126,33 @@ class System extends BaseObject
        {
                $err = '';
                if ($val >= 400) {
-                       $err = 'Error';
-                       if (!isset($description["title"])) {
-                               $description["title"] = $err." ".$val;
+                       if (!empty($description['title'])) {
+                               $err = $description['title'];
+                       } else {
+                               $title = [
+                                       '400' => L10n::t('Error 400 - Bad Request'),
+                                       '401' => L10n::t('Error 401 - Unauthorized'),
+                                       '403' => L10n::t('Error 403 - Forbidden'),
+                                       '404' => L10n::t('Error 404 - Not Found'),
+                                       '500' => L10n::t('Error 500 - Internal Server Error'),
+                                       '503' => L10n::t('Error 503 - Service Unavailable'),
+                                       ];
+                               $err = defaults($title, $val, 'Error ' . $val);
+                               $description['title'] = $err;
+                       }
+                       if (empty($description['description'])) {
+                               // 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.'),
+                                       ];
+                               if (!empty($explanation[$val])) {
+                                       $description['description'] = $explanation[$val];
+                               }
                        }
                }