From 206c46514a715403dc066e60c3e8f0a229a66ecc Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Sun, 18 Feb 2024 15:48:33 +0000 Subject: [PATCH] Add image attachements --- index.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index a2efe20..acbb641 100644 --- a/index.php +++ b/index.php @@ -7,7 +7,8 @@ * For educational purposes only. * The Server produces an Actor who can be followed. * The Actor can send messages to followers. - * Those messages can have linkable URls, hashtags, and mentions. + * The message can have linkable URls, hashtags, and mentions. + * An image and alt text can be attached to the message. * The Server saves logs about requests it receives and sends. * This code is NOT suitable for production use. * SPDX-License-Identifier: AGPL-3.0-or-later @@ -381,6 +382,10 @@ echo <<< HTML


+
+
+ +


@@ -406,6 +411,46 @@ HTML; // Process the content into HTML to get hashtags etc list( "HTML" => $content, "TagArray" => $tags ) = process_content( $content ); + // Is there an image attached? + if ( isset( $_FILES['image']['tmp_name'] ) && ("" != $_FILES['image']['tmp_name'] ) ) { + // Get information about the image + $image = $_FILES['image']['tmp_name']; + $image_info = getimagesize( $image ); + $image_ext = image_type_to_extension( $image_info[2] ); + $image_mime = $image_info["mime"]; + + // Files are stored according to their hash + // A hash of "abc123" is stored in "/images/abc123.jpg" + $sha1 = sha1_file( $image ); + $image_path = "images"; + $image_full_path = "{$image_path}/{$sha1}.{$image_ext}"; + + // Move media to the correct location + // Create a directory if it doesn't exist + if( ! is_dir( $image_path ) ) { + mkdir( $image_path ); + } + move_uploaded_file($image, $image_full_path ); + + // Get the alt text + if ( isset( $_POST["alt"] ) ) { + $alt = $_POST["alt"]; + } else { + $alt = ""; + } + + // Construct the attachment value for the post + $attachment = [ + "type" => "Image", + "mediaType" => "{$image_mime}", + "url" => "https://{$server}/{$image_full_path}", + "name" => $alt + ]; + + } else { + $attachment = []; + } + // Current time - ISO8601 $timestamp = date( "c" ); @@ -426,7 +471,8 @@ HTML; "content" => $content, "contentMap" => ["en" => $content], "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "tag" => $tags + "tag" => $tags, + "attachment" => $attachment ]; // Construct the Message