better http errors

This commit is contained in:
codepoet80 committed 2021-06-07 19:48:29 -04:00
1 parent 9aee75da94
commit 7682706f8a
7 files changed
+87 -28

No files matched your search

+39
View File
@@ -203,6 +203,45 @@ function gracefuldeath_html($error) {
echo "ERROR: " . $error;
}
function gracefuldeath_httpcode($code) {
$explanation = "An error has occured on the server and the request could not be processed";
if (!isset($code))
$code = 404;
switch($code) {
case 400:
$code = "400 Bad Request";
$explanation = "The request was missing a required parameter and could not be processed";
break;
case 404:
$code = "404 Not Found";
$explanation = "The requested resource was not found or never existed";
break;
case 410:
$code = "410 Gone";
$explanation = "The requested content does not exist, it may have expired, been deleted, or you may have an out-of-date link";
break;
case 417:
$code = "417 Expectation Failed";
$explanation = "The user specified in the request was not found or never existed";
break;
}
header($_SERVER["SERVER_PROTOCOL"] . $code);
echo "<html><head>";
echo "<meta name='viewport' content='width=device-width, initial-scale=1'>";
echo "<title>Share Service - Error " . $code . "</title>";
echo "<link rel='stylesheet' href='style-columns.css'>";
include("web-meta.php");
echo "</head>";
echo "<body style='margin-left: 20px'><table border=0><tr>";
echo "<td><img src='images/icon-128.png'></td>";
echo "<td><h1>HTTP error: " . $code . "</h1></td>";
echo "</tr></table>";
echo "<p>" . $explanation . "<p>";
echo "<a href='index.php'>Go Home</a>";
echo "</body></html>";
die;
}
function gracefuldeath_later($message) {
global $error_message;
$error_message = $message;
+9 -5
View File
@@ -5,7 +5,7 @@ include("common.php");
$sharehandle = $_SERVER['QUERY_STRING'];
if (!isset($sharehandle) || $sharehandle == "")
gracefuldeath_html("content request not specified!");
gracefuldeath_httpcode(400);
//handle Facebook
if (strpos($sharehandle, "&fbclid")) {
@@ -20,16 +20,17 @@ if (count($shareparts) > 1) {
$contentid = $shareparts[1];
if (!is_dir("data/" . $username)) {
gracefuldeath_html("user does not exist!");
gracefuldeath_httpcode(417);
}
//Make sure the file exists and can be loaded
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_html');
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_httpcode');
$found = false;
foreach ($jsondata['shares'] as $share => $value) {
//print_r($value);
if ($contentid == $value['guid'])
{
$found = true;
$client = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($client, "hpwos") || strpos($client, "webos") || strpos($client, "android")) {
echo '<img src="i.php?'. $_SERVER['QUERY_STRING'] . '">';
@@ -46,8 +47,11 @@ if (count($shareparts) > 1) {
}
}
}
if (!$found) {
gracefuldeath_httpcode(410);
}
} else {
gracefuldeath_html("content request malformed!");
gracefuldeath_httpcode(400);
}
?>
+10 -6
View File
@@ -3,7 +3,7 @@
// This endpoint supports returning the binary data of an image share that can be the source of an HTML img element
include("common.php");
if (isset($_GET["size"]))
if (isset($_GET["size"])) //TODO: Accepted but not used at this time
$imgSize = $_GET["size"];
if (isset($_GET['img']) && $_GET['img'] != "") {
$sharehandle = $_GET['img'];
@@ -12,8 +12,7 @@ if (isset($_GET['img']) && $_GET['img'] != "") {
$sharehandle = $_SERVER['QUERY_STRING'];
}
if (!isset($sharehandle)) { //Deal with no usable request
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
die;
gracefuldeath_httpcode(400);
}
$sharehandle = base64url_decode($sharehandle);
@@ -23,22 +22,27 @@ if (count($shareparts) > 1) {
$contentid = $shareparts[1];
if (!is_dir("data/" . $username)) {
gracefuldeath_html("user does not exist!");
gracefuldeath_httpcode(417);
}
//Make sure the file exists and can be loaded
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_html');
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_httpcode');
$found = false;
foreach ($jsondata['shares'] as $share => $value) {
//print_r($value);
if ($contentid == $value['guid'])
{
$found = true;
header('Content-Type '. $value['contenttype']);
$fp = fopen($value['content'], 'rb');
fpassthru($fp);
}
}
if (!$found) {
gracefuldeath_httpcode(410);
}
} else {
gracefuldeath_html("content request malformed!");
gracefuldeath_httpcode(400);
}
?>
+1 -1
View File
@@ -20,7 +20,7 @@
//Show appropriate instructions for platform
$client = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($client, "hpwos") || strpos($client, "webos")) {
echo "Welcome webOS User! This sharing service was made for you in mind, but the web front-end doesn't work on your current device. Instead, you can <a href='http://appcatalog.webosarchive.com/showMuseumDetails.php?search=share+space&app=1005788'>download the native webOS App called Share Space</a>, that works on Touchpad, Pre and all the other webOS phones!";
echo "Welcome webOS User! This sharing service was made with you in mind, but this web front-end doesn't work on your current device. Instead, you can <a href='http://appcatalog.webosarchive.com/showMuseumDetails.php?search=share+space&app=1005788'>download the native webOS App called Share Space</a>, that works on Touchpad, Pre and all the other webOS phones!";
} else {
echo "<span style='font-size: smaller; color: dimgray;'>The Share Service is a file sharing web app for retro devices -- but it works with modern devices too! You can create an account where you can share pictures or text with others, or they can share with you!</span>";
}
+9 -5
View File
@@ -15,8 +15,7 @@ if (isset($_GET['itemid']) && $_GET['itemid'] != "") {
$itemid = $_SERVER['QUERY_STRING'];
}
if (!isset($itemid)) { //Deal with no usable request
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
die;
gracefuldeath_httpcode(400);
}
$sharehandle = base64url_decode($itemid);
@@ -26,14 +25,16 @@ if (count($shareparts) > 1) {
$contentid = $shareparts[1];
if (!is_dir("data/" . $username)) {
gracefuldeath_html("user does not exist!");
gracefuldeath_httpcode(417);
}
//Make sure the file exists and can be loaded
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_html');
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_httpcode');
$found = false;
foreach ($jsondata['shares'] as $share => $value) {
if ($contentid == $value['guid'])
{
$found = true;
//Prepare the cache name
$cacheID = "thumb-" . $value['guid'];
$path = "data/" . $username;
@@ -54,9 +55,12 @@ if (count($shareparts) > 1) {
exit;
}
}
if (!$found) {
gracefuldeath_httpcode(410);
}
} else {
gracefuldeath_html("content request malformed!");
gracefuldeath_httpcode(400);
}
//Function to resize common image formats
+9 -4
View File
@@ -5,7 +5,7 @@ include("common.php");
$sharehandle = $_SERVER['QUERY_STRING'];
if (!isset($sharehandle) || $sharehandle == "")
graceful_death("content request not specified!");
gracefuldeath_httpcode(400);
if (strpos($sharehandle, "&fbclid")) {
$sharehandle = str_replace("%3D", "=", $sharehandle);
@@ -20,15 +20,17 @@ if (count($shareparts) > 1) {
$contentid = $shareparts[1];
if (!is_dir("data/" . $username)) {
gracefuldeath_html("user does not exist!");
gracefuldeath_httpcode(417);
}
//Make sure the file exists and can be loaded
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_html');
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_httpcode');
$found = false;
foreach ($jsondata['shares'] as $share => $value) {
//print_r($value);
if ($contentid == $value['guid'])
{
$found = true;
if ($value['contenttype'] == "application/json") {
header('Content-Type: application/json');
print_r(json_encode($value['content'], JSON_PRETTY_PRINT));
@@ -39,9 +41,12 @@ if (count($shareparts) > 1) {
}
}
}
if (!$found) {
gracefuldeath_httpcode(410);
}
} else {
gracefuldeath_html("content request malformed!");
gracefuldeath_httpcode(400);
}
+10 -7
View File
@@ -1,7 +1,6 @@
<?php
// ithumb Endpoint
// tthumb Endpoint
// This endpoint creates (if needed) and returns an image version of a text share as binary data that can be the source of an HTML img element
// It needs work.
include ("common.php");
//Handle more specific queries
@@ -16,8 +15,7 @@ if (isset($_GET['itemid']) && $_GET['itemid'] != "") {
$itemid = $_SERVER['QUERY_STRING'];
}
if (!isset($itemid)) { //Deal with no usable request
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
die;
gracefuldeath_httpcode(400);
}
$sharehandle = base64url_decode($itemid);
@@ -27,14 +25,16 @@ if (count($shareparts) > 1) {
$contentid = $shareparts[1];
if (!is_dir("data/" . $username)) {
gracefuldeath_html("user does not exist!");
gracefuldeath_httpcode(417);
}
//Make sure the file exists and can be loaded
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_html');
$jsondata = get_share_data($username, $config['readonlykey'], 'gracefuldeath_httpcode');
$found = false;
foreach ($jsondata['shares'] as $share => $value) {
if ($contentid == $value['guid'])
{
$found = true;
if ($value['contenttype'] == "application/json") {
$usetext = json_encode($value['content']);
}
@@ -64,9 +64,12 @@ if (count($shareparts) > 1) {
}
}
}
if (!$found) {
gracefuldeath_httpcode(410);
}
} else {
gracefuldeath_html("content request malformed!");
gracefuldeath_httpcode(400);
}
//Function to make an image square out of text