From 83489b36c4f8299ea1219b7d45c4ea974daf4f66 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 2 Dec 2018 19:33:01 +0000 Subject: [PATCH 1/3] Using the standard function to return http errors --- mod/fetch.php | 9 +++------ mod/pubsub.php | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/mod/fetch.php b/mod/fetch.php index 9336c1404..d66f19302 100644 --- a/mod/fetch.php +++ b/mod/fetch.php @@ -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, ['title' => L10n::t('Not found.')]); } $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, ['title' => L10n::t('Not found.')]); } // 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, ['title' => L10n::t('Not found.')]); } $status = Diaspora::buildStatus($item, $user); diff --git a/mod/pubsub.php b/mod/pubsub.php index f0a8d463c..cb6738ada 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -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, ['title' => L10n::t('Not found.')]); } killme(); } From 649228cd3d2a1cb1139e29d8cdbbc7dba398c776 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 2 Dec 2018 20:10:53 +0000 Subject: [PATCH 2/3] More detailled HTTP error explanations --- mod/pubsub.php | 3 +-- src/Core/System.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/mod/pubsub.php b/mod/pubsub.php index cb6738ada..1245c9462 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -26,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) diff --git a/src/Core/System.php b/src/Core/System.php index d24581e99..eadbf98d2 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -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]; + } } } From 02a72ef04fb4ef8b8984b3dd05d6813c71d6abcc Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 2 Dec 2018 20:14:53 +0000 Subject: [PATCH 3/3] Simplify the call --- mod/fetch.php | 6 +++--- mod/pubsub.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mod/fetch.php b/mod/fetch.php index d66f19302..5dcedb1aa 100644 --- a/mod/fetch.php +++ b/mod/fetch.php @@ -18,7 +18,7 @@ function fetch_init(App $a) { if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) { - System::httpExit(404, ['title' => L10n::t('Not found.')]); + System::httpExit(404); } $guid = $a->argv[2]; @@ -44,13 +44,13 @@ function fetch_init(App $a) } } - System::httpExit(404, ['title' => L10n::t('Not found.')]); + 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) { - System::httpExit(404, ['title' => L10n::t('Not found.')]); + System::httpExit(404); } $status = Diaspora::buildStatus($item, $user); diff --git a/mod/pubsub.php b/mod/pubsub.php index 1245c9462..cd2f21dd6 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -17,7 +17,7 @@ function hub_return($valid, $body) header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK'); echo $body; } else { - System::httpExit(404, ['title' => L10n::t('Not found.')]); + System::httpExit(404); } killme(); }