From df7836d9fa6c871d2a57200561a0284ef1d3f4a3 Mon Sep 17 00:00:00 2001 From: Terence Eden Date: Mon, 4 Mar 2024 15:43:21 +0000 Subject: [PATCH] Fix sending and reading of own images Fixes #12 Note, the homepage won't show older, incorrectly formatted sent images --- index.php | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/index.php b/index.php index c2e11f9..f7115a4 100644 --- a/index.php +++ b/index.php @@ -567,14 +567,16 @@ HTML; // Print the items in a list foreach ( $messages_ordered as $published=>$message ) { - // Set up the common components - $object = $message["object"] ?? []; + // Received messages have an `object` which contains their data. + // Sent messages *are* their data + if ( isset( $message["object"] ) ) { + $object = $message["object"]; + } else { + $object = $message; + } - if ( isset( $message["object"]["id"] ) ) { - $id = $message["object"]["id"]; - $publishedHTML = "{$published}"; - } else if ( isset( $message["id"] ) ) { - $id = $message["id"]; + if ( isset( $object["id"] ) ) { + $id = $object["id"]; $publishedHTML = "{$published}"; } else { $id = ""; @@ -584,19 +586,18 @@ HTML; $timeHTML = ""; // Get the actor who authored the message - if ( isset( $message["actor"] ) ) { - // Usually found in Inbox messages + if ( isset( $object["attributedTo"] ) ) { + $actor = $object["attributedTo"]; + } else if ( isset( $message["actor"] ) ) { + // Usually found in Like and Announce messages $actor = $message["actor"]; - } else if ( isset( $message["attributedTo"] ) ) { - // Usually found in sent messages - $actor = $message["attributedTo"]; } else { // Should never happen! $actor = "https://example.com/anonymous"; } // Assume that what comes after the final `/` in the URl is the name - $actorArray = explode( "/", $actor ); - $actorName = end( $actorArray ); + $actorArray = explode( "/", $actor ); + $actorName = end( $actorArray ); $actorServer = parse_url( $actor, PHP_URL_HOST ); $actorUsername = "@{$actorName}@{$actorServer}"; // Make i18n usernames readable and safe. @@ -669,8 +670,7 @@ HTML; // Add any images if ( isset( $object["attachment"] ) ) { - foreach ( $object["attachment"] as $attachment ) { - + foreach ( $object["attachment"] as $attachment ) { // Only use things which have a MIME Type set if ( isset( $attachment["mediaType"] ) ) { $mediaURl = $attachment["url"]; @@ -718,12 +718,14 @@ HTML; $messageHTML = "{$timeHTML} {$actorHTML} {$verb} {$replyTo}:
{$content}
"; } - } else if ( "Like" == $type ) { - $objectHTML = "{$object}"; - $messageHTML = "{$timeHTML} {$actorHTML} liked {$objectHTML}"; } else if ( "Follow" == $type ) { $messageHTML = "{$timeHTML} {$actorHTML} followed you"; + } else if ( "Like" == $type ) { + $object = $message["object"]; + $objectHTML = "{$object}"; + $messageHTML = "{$timeHTML} {$actorHTML} liked {$objectHTML}"; } else if ( "Announce" == $type ) { + $object = $message["object"]; $objectHTML = "{$object}"; $messageHTML = "{$timeHTML} {$actorHTML} boosted {$objectHTML}"; } @@ -910,12 +912,12 @@ HTML; } // Construct the attachment value for the post - $attachment = [ + $attachment = array( [ "type" => "Image", "mediaType" => "{$image_mime}", "url" => "https://{$server}/{$image_full_path}", "name" => $alt - ]; + ] ); } else { $attachment = []; } @@ -1192,7 +1194,7 @@ HTML; ( filter_var( $url, FILTER_VALIDATE_URL) != true) || ( parse_url( $url, PHP_URL_SCHEME ) != "https" ) ) { die(); } - + // Split the URL $url_host = parse_url( $url, PHP_URL_HOST ); $url_path = parse_url( $url, PHP_URL_PATH );