web add text share and thumbnail

This commit is contained in:
CodePoet80 committed 2021-05-20 09:42:21 -04:00
1 parent 94d45c8b11
commit 8dd294d78d
8 files changed
+136 -151

No files matched your search

+2
View File
@@ -25,6 +25,8 @@ if (!isset($_GET["itemid"])) {
$updatedsharedata = delete_share_item($itemid, $auth['username'], $auth['credential'], 'gracefuldeath_json');
if (isset($updatedsharedata)) {
die ("{\"success\":\"share item ". $itemid ." deleted from " . $itemid . " share space\"}");
} else {
gracefuldeath_json("delete failed.");
}
exit();
+54 -2
View File
@@ -82,11 +82,63 @@ function create_new_user($username, $sharephrase, $password, $errorhandler = 'de
return;
}
function add_share_text($postdata, $username, $credential, $reqtype, $errorhandler = 'default_error_handler') {
global $supported_content_types;
//Make sure the file exists and can be loaded
$sharedata = get_share_data($username, $credential, $errorhandler);
//Make sure this share content is valid and allowed
$allowedtype = $sharedata['sharetype'];
if (!in_array($reqtype, $supported_content_types)) {
print_r($supported_content_types);
$errorhandler("shared content-type, " . $reqtype . ", not supported by this service"); //TODO: could list
}
if ($reqtype == "text/plain") {
if ($allowedtype != "all" && $allowedtype != "string" && $allowedtype != "text/plain")
$errorhandler("shared content-type not allowed. this user or service instance only allows ". $allowedtype);
}
else if ($reqtype == "application/json") {
if ($allowedtype != "all" && $allowedtype != "string" && $allowedtype != "application/json")
$errorhandler("shared content-type not allowed. this user or service instance only allows ". $allowedtype);
else {
//Make sure we're not getting junk
if (!is_JSON($postdata)) {
$errorhandler("posted json could not be parsed: it may be too long or malformed");
} else {
$postdata = json_decode($postdata);
}
}
}
else {
$errorhandler("shared content-type not allowed on this endpoint");
}
//Get and update share file
$newid = uniq_alphaid();
$updatedsharedata = add_share_item($postdata, $sharedata, $username, $credential, $reqtype, $newid, $errorhandler);
if (isset($updatedsharedata)) {
$file = "data/" . strtolower($username) . "/sharelog.json";
$written = file_put_contents($file, json_encode($updatedsharedata, JSON_PRETTY_PRINT));
//Output the results
if (!$written) {
$errorhandler("failed to write to file " . $file);
} else {
return $newid;
}
} else {
$errorhandler("failed to build new share data");
}
}
function add_share_item($newshareitem, $oldsharedata, $username, $credential, $contenttype, $newid, $errorhandler = 'default_error_handler'){
global $config;
if (!isset($newshareitem) || !isset($oldsharedata) || !isset($credential) || !isset($contenttype) || !isset($newid)) {
$errorhandler("functional call missing required parameters!");
$errorhandler("add_share_item function call missing required parameters!");
return;
}
if (($oldsharedata['sharephrase'] != $credential && base64_decode($oldsharedata['password']) != $credential) || $credential == $config['readonlykey']) {
@@ -139,7 +191,7 @@ function remove_share_item($itemid, $oldsharedata, $username, $credential, $erro
global $config;
if (!isset($itemid) || !isset($oldsharedata) || !isset($credential)) {
$errorhandler("functional call missing required parameters!");
$errorhandler("remove_share_item function call missing required parameters!");
return;
}
if (base64_decode($oldsharedata['password']) != $credential || $credential == $config['readonlykey']) {
+6 -11
View File
@@ -2,27 +2,22 @@
include ("common.php");
//Handle more specific queries
$img = null;
$itemid = null;
$imgSize = 128;
if (isset($_GET["size"]))
$imgSize = $_GET["size"];
if (isset($_GET['img']) && $_GET['img'] != "") {
$img = $_GET['img'];
if (isset($_GET['itemid']) && $_GET['itemid'] != "") {
$itemid = $_GET['itemid'];
} else { //Accept a blanket query
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != "")
$img = $_SERVER['QUERY_STRING'];
$itemid = $_SERVER['QUERY_STRING'];
}
if (!isset($img)) { //Deal with no usable request
if (!isset($itemid)) { //Deal with no usable request
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
die;
}
$sharehandle = $_SERVER['QUERY_STRING'];
if (!isset($sharehandle) || $sharehandle == "")
graceful_death("content request not specified!");
$sharehandle = base64url_decode($sharehandle);
$sharehandle = base64url_decode($itemid);
$shareparts = explode("|", $sharehandle);
if (count($shareparts) > 1) {
+9 -52
View File
@@ -9,64 +9,21 @@ $auth = get_authorization();
//Make sure we can get the input
$postraw = file_get_contents('php://input', false, null, 0, $config['maxtextlength']); //TODO: better error for cropped text
if (!isset($postraw) || $postraw == "")
gracefuldeath_json("no data posted to share");
//Make sure the file exists and can be loaded
$jsondata = get_share_data($auth['username'], $auth['credential'], 'gracefuldeath_json');
//Make sure this share content is valid and allowed
$allowedtype = $jsondata['sharetype'];
if (!isset($postraw) || $postraw == "") {
gracefuldeath_json("no contentData in post payload");
}
$request_headers = get_request_headers();
if (isset($request_headers["content-type"]) && $request_headers["content-type"] != "") {
$reqtype = explode(";", $request_headers["content-type"]);
$reqtype = $reqtype[0];
if (!in_array($reqtype, $supported_content_types))
gracefuldeath_json("shared content-type, " . $reqtype . ", not supported by this service"); //TODO: could list
if ($reqtype == "text/plain") {
if ($allowedtype != "all" && $allowedtype != "string" && $allowedtype != "text/plain")
gracefuldeath_json("shared content-type not allowed. this user or service instance only allows ". $allowedtype);
else
$postdata = $postraw;
}
else if ($reqtype == "application/json") {
if ($allowedtype != "all" && $allowedtype != "string" && $allowedtype != "application/json")
gracefuldeath_json("shared content-type not allowed. this user or service instance only allows ". $allowedtype);
else {
//Make sure we're not getting junk
if (!is_JSON($postraw)) {
gracefuldeath_json("posted json could not be parsed: it may be too long or malformed");
} else {
$postdata = json_decode($postraw);
}
}
}
else {
gracefuldeath_json("shared content-type not allowed on this endpoint");
}
} else {
if (!isset($request_headers["content-type"]) || $request_headers["content-type"] == "")
gracefuldeath_json("content-type not specified");
}
//Get and update share file
$newid = uniq_alphaid();
$sharedata = get_share_data($auth['username'], $auth['credential'], 'gracefuldeath_json');
$updatedsharedata = add_share_item($postdata, $sharedata, $auth['username'], $auth['credential'], $reqtype, $newid, 'gracefuldeath_json');
if (isset($updatedsharedata)) {
$file = "data/" . strtolower($auth['username']) . "/sharelog.json";
$written = file_put_contents($file, json_encode($updatedsharedata, JSON_PRETTY_PRINT));
$reqtype = explode(";", $request_headers["content-type"]);
$reqtype = $reqtype[0];
//Output the results
if (!$written) {
gracefuldeath_json("failed to write to file " . $file);
} else {
echo "{\"success\":\"" . make_url_from_contentid($newid, $auth['username'], "t") . "\"}";
}
$newshareid = add_share_text($postraw, $auth['username'], $auth['credential'], $reqtype, 'gracefuldeath_json');
if (isset($newshareid)) {
die ("{\"success\":\"" . make_url_from_contentid($newshareid, $auth['username'], "t") . "\"}");
} else {
gracefuldeath_json("failed to build new share data");
gracefuldeath_json("share text failed");
}
exit();
?>
+1 -1
View File
@@ -1,7 +1,7 @@
{
"sharephrase": "Share Phrase",
"password": "Hashed password",
"sharetype": "any",
"sharetype": "all",
"sharelength": 10,
"shares": [{
"guid": "999",
+41 -74
View File
@@ -1,31 +1,23 @@
<?php
include ("common.php");
//TODO: Try this: https://code-boxx.com/convert-text-image-php/
//Handle more specific queries
$img = null;
$itemid = null;
$imgSize = 128;
if (isset($_GET["size"]))
$imgSize = $_GET["size"];
if (isset($_GET['img']) && $_GET['img'] != "") {
$img = $_GET['img'];
if (isset($_GET['itemid']) && $_GET['itemid'] != "") {
$itemid = $_GET['itemid'];
} else { //Accept a blanket query
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != "")
$img = $_SERVER['QUERY_STRING'];
$itemid = $_SERVER['QUERY_STRING'];
}
if (!isset($img)) { //Deal with no usable request
if (!isset($itemid)) { //Deal with no usable request
header($_SERVER["SERVER_PROTOCOL"] . " 400 Bad Request");
die;
}
$sharehandle = $_SERVER['QUERY_STRING'];
if (!isset($sharehandle) || $sharehandle == "")
graceful_death("content request not specified!");
$sharehandle = base64url_decode($sharehandle);
$sharehandle = base64url_decode($itemid);
$shareparts = explode("|", $sharehandle);
if (count($shareparts) > 1) {
@@ -41,24 +33,33 @@ if (count($shareparts) > 1) {
foreach ($jsondata['shares'] as $share => $value) {
if ($contentid == $value['guid'])
{
//Prepare the cache name
$cacheID = "thumb-" . $value['guid'];
$path = "data/" . $username;
//Fetch and cache the file if its not already cached
$path = $path . "/" . $cacheID . ".png";
if (!file_exists($path)) {
resize_img($imgSize, $path, $value['content']);
if ($value['contenttype'] == "application/json") {
$usetext = json_encode($value['content']);
}
elseif($value['contenttype'] == "text/plain") {
$usetext = $value['content'];
}
if (isset($usetext)) {
//echo $usetext;
//Prepare the cache name
$cacheID = "thumb-" . $value['guid'];
$path = "data/" . $username;
//Fetch and cache the file if its not already cached
$path = $path . "/" . $cacheID . ".png";
if (!file_exists($path)) {
make_text_thumb($imgSize, $path, $usetext);
}
//Send the right headers
$info = getimagesize($path);
header("Content-Type: " . $info['mime']);
header("Content-Length: " . filesize($path));
//Dump the file and stop the script
$fp = fopen($path, 'r');
fpassthru($fp);
exit;
//Send the right headers
$info = getimagesize($path);
header("Content-Type: image/png");
header("Content-Length: " . filesize($path));
//Dump the file and stop the script
$fp = fopen($path, 'r');
fpassthru($fp);
exit;
}
}
}
@@ -66,50 +67,16 @@ if (count($shareparts) > 1) {
gracefuldeath_html("content request malformed!");
}
//Function to resize common image formats
// Found on https://stackoverflow.com/questions/13596794/resize-images-with-php-support-png-jpg
function resize_img($newWidth, $targetFile, $originalFile) {
//Function to make an image square out of text
//TODO: Text does not wrap
function make_text_thumb($newWidth, $targetFile, $text) {
$info = getimagesize($originalFile);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create_func = 'imagecreatefromjpeg';
$image_save_func = 'imagejpeg';
$new_image_ext = 'jpg';
break;
case 'image/png':
$image_create_func = 'imagecreatefrompng';
$image_save_func = 'imagepng';
$new_image_ext = 'png';
break;
case 'image/gif':
$image_create_func = 'imagecreatefromgif';
$image_save_func = 'imagegif';
$new_image_ext = 'gif';
break;
default:
throw new Exception('Unknown image type.');
}
$img = $image_create_func($originalFile);
list($width, $height) = getimagesize($originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagesavealpha($tmp, true);
$trans_colour = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
imagefill($tmp, 0, 0, $trans_colour);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
$image_save_func($tmp, $targetFile);
$tmpimg = imagecreate($newWidth, $newWidth);
$white = imagecolorallocate($tmpimg, 255, 255, 255);
$black = imagecolorallocate($tmpimg, 0, 0, 0);
//$text = wordwrap($text, 15, "\r", true);
imagefilledrectangle($tmpimg, 0, 0, $newWidth, $newWidth, $white);
imagestring($tmpimg, 3, 0, 0, $text, $black);
imagepng($tmpimg, $targetFile);
}
?>
+9 -6
View File
@@ -37,14 +37,18 @@ foreach($sharedata->shares as $thisshare)
switch ($thisshare['contenttype']) {
case "text/plain":
$textLink = make_url_from_contentid($thisshare['guid'], $_GET["username"], "t");
echo "<td class='shareDescriptor'><b>Text</b></td>";
echo "<td class='shareContent'>" . trim(substr($thisshare['content'], 0, 100)) . "<br>";
$imageLoad = make_url_from_contentid($thisshare['guid'], $_GET["username"], "tthumb");
echo "<td class='shareDescriptor'><img src='" . $imageLoad . "' style='border:1px solid black; height: 64px' vertical-align:middle></td>";
//echo "<td class='shareContent'>" . trim(substr($thisshare['content'], 0, 100)) . "<br>";
echo "<td class='shareContent'>Shared on: " . $thisshare['timestamp'] . " UTC <br>";
echo "<div class='shareLinks'><b>Public View Link:</b> <a href='" . $textLink . "' target='_blank'>" . $textLink . "</a></div>";
break;
case "application/json":
$textLink = make_url_from_contentid($thisshare['guid'], $_GET["username"], "t");
echo "<td class='shareDescriptor'><b>JSON</b></td>";
echo "<td class='shareContent'>" . trim(substr(json_encode($thisshare['content']), 0, 100)) . "<br>";
$imageLoad = make_url_from_contentid($thisshare['guid'], $_GET["username"], "tthumb");
echo "<td class='shareDescriptor'><img src='" . $imageLoad . "' style='border:1px solid black; height: 64px' vertical-align:middle></td>";
//echo "<td class='shareContent'>" . trim(substr(json_encode($thisshare['content']), 0, 100)) . "<br>";
echo "<td class='shareContent'>Shared on: " . $thisshare['timestamp'] . " UTC <br>";
echo "<div class='shareLinks'><b>Public View Link:</b> <a href='" . $textLink . "' target='_blank'>" . $textLink . "</a></div>";
break;
default:
@@ -52,11 +56,10 @@ foreach($sharedata->shares as $thisshare)
$imageLoad = make_url_from_contentid($thisshare['guid'], $_GET["username"], "ithumb");
$imageDownload = make_url_from_contentid($thisshare['guid'], $_GET["username"], "download");
echo "<td class='shareDescriptor'><img src='" . $imageLoad . "' style='height: 64px' vertical-align:middle></td>";
echo "<td class='shareContent'><div class='shareLinks'><b>Public View Link:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b> <a href='" . $imagePreview . "' target='_blank'>" . $imagePreview . "</a><br>";
echo "<td class='shareContent'>Shared on: " . $thisshare['timestamp'] . " UTC <br><div class='shareLinks'><b>Public View Link:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b> <a href='" . $imagePreview . "' target='_blank'>" . $imagePreview . "</a><br>";
echo "<b>Public Download Link:</b> <a href='" . $imageDownload . "'>" . $imageDownload . "</a></div>";
break;
}
echo $sharedata->accesslevel;
if ($sharedata->accesslevel == 'admin') {
echo "<div class='shareDelete'><a href='web-delete-item.php?username=" . $_GET["username"] . "&itemid=" . $thisshare['guid'] . "'>Delete</a></div>";
}
+14 -5
View File
@@ -9,8 +9,17 @@
if(isset($_POST['txtContent']) && $_POST['txtContent'] != "")
{
echo "Saving content: " . $_POST['txtContent'];
}
if (isset($_POST['optContentType'])) {
$newshareid = add_share_text($_POST['txtContent'], $auth['username'], $auth['credential'], $_POST['optContentType'], 'gracefuldeath_later');
if (isset($newshareid)) {
gracefuldeath_later("SUCCESS! This is not actually an error!");
}
} else {
gracefuldeath_later("Content-Type not specified");
}
}
?>
<html>
@@ -81,9 +90,9 @@
<tr><td>Share Phrase: </td><td><input type="text" id="txtSharephrase" name="txtSharephrase" value="<?php echo $_POST['txtSharephrase']?>"></td></tr>
<tr><td>Type: </td>
<td>
<select>
<option value="text">Text</option>
<option value="json">JSON</option>
<select id="optContentType" name="optContentType">
<option value="text/plain">Text</option>
<option value="application/json">JSON</option>
</select>
</td>
</tr>