diff --git a/README.md b/README.md
index 8154f91..c6b1275 100644
--- a/README.md
+++ b/README.md
@@ -19,6 +19,7 @@ There are no tests, no checks, no security features, no formal verifications, no
1. Check social media to see if the message appears.
1. To follow other users, visit `https://test.example.com/follow` type in the name of the user you want to follow and your password. Press the "Send Follow Request" button.
1. Check social media to see if the follow request came through.
+1. To read the messages that your server's inbox has received, visit `https://test.example.com/read`
## How this works
diff --git a/index.php b/index.php
index 2543dce..1c4efe6 100644
--- a/index.php
+++ b/index.php
@@ -118,9 +118,11 @@
case "outbox":
outbox(); // Optional. Dynamic.
case "follow":
- follow(); // User interface for following an external user
+ follow(); // User interface for following an external user
case "follow_user":
follow_user(); // API for following a user
+ case "read":
+ read(); // User interface for reading posts
case "/":
home(); // Optional. Can be dynamic
default:
@@ -460,7 +462,7 @@
// User Interface for Homepage:
// This creates a basic HTML page. This content appears when someone visits the root of your site.
function home() {
- global $username, $server, $realName, $summary;
+ global $username, $server, $realName, $summary, $directories;
echo <<< HTML
@@ -485,7 +487,7 @@ echo <<< HTML
HTML;
// Get all posts, most recent first
- $posts = array_reverse( glob("posts/*.json") );
+ $posts = array_reverse( glob( $directories["posts"] . "/*.json") );
// Loop through the posts
foreach ($posts as $post) {
@@ -785,10 +787,10 @@ HTML;
// The Outbox contains a date-ordered list (newest first) of all the user's posts
// This is optional.
function outbox() {
- global $server, $username;
+ global $server, $username, $directories;
// Get all posts
- $posts = array_reverse( glob("posts/*.json") );
+ $posts = array_reverse( glob( $directories["posts"] . "/*.json") );
// Number of posts
$totalItems = count( $posts );
// Create an ordered list
@@ -1079,6 +1081,114 @@ HTML;
return $verified;
}
+ // Displays the most recent 200 messages in the inbox
+ function read() {
+ global $server, $directories;
+
+ // Get all the files in the inbox
+ $inbox_files = array_reverse( glob( $directories["inbox"] . "/*.json") );
+ // Keep the most recent 200
+ $inbox_files = array_slice( $inbox_files, 0, 200 );
+
+ // Sometimes messages are received out of order.
+ // This sorts them by their published time or, if there is none, the received time.
+ $inbox_ordered = [];
+ foreach ( $inbox_files as $inbox_file ) {
+ // Get the contents of the JSON
+ $inbox_message = json_decode( file_get_contents( $inbox_file ), true );
+
+ // Use the timestamp of the message. If there is none, use the date in the filename
+ if ( isset( $inbox_message["published"] ) ) {
+ $published = $inbox_message["published"];
+ } else {
+ $published = end( explode( "/", explode( ".", $inbox_file)[0] ) ) ;
+ }
+ // Place in an array where the key is the timestamp
+ $inbox_ordered[$published] = $inbox_message;
+ }
+
+ // Sort with newest on top
+ krsort( $inbox_ordered );
+
+ // Show a basic HTML interface
+echo <<< HTML
+
+
+
+
+ Reader
+
+
+
+
+HTML;
+ // Print the items in a list
+ foreach ( $inbox_ordered as $published=>$inbox_message ) {
+
+ // Set up the common components
+ $object = $inbox_message["object"];
+ $actor = $inbox_message["actor"];
+ $actorName = end( explode("/", $actor ) );
+ $actorHTML = "@{$actorName}";
+ $timeHTML = "";
+
+ // HTML is *probably* sanitised by the sender. But let's not risk it, eh?
+ // Using the allow-list from https://docs.joinmastodon.org/spec/activitypub/#sanitization
+ $allowed_elements = ["p", "span", "br", "a", "del", "pre", "code", "em", "strong", "b", "i", "u", "ul", "ol", "li", "blockquote"];
+
+ // What type of message is this?
+ $type = $inbox_message["type"];
+
+ // Render the message according to type
+ if ( "Create" == $type ) {
+ // Get the HTML content and sanitise it.
+ $content = $object["content"];
+ $content = strip_tags($content, $allowed_elements);
+
+ // Add any images
+ if ( isset( $object["attachment"] ) ) {
+ foreach ( $object["attachment"] as $attachment ) {
+
+ // Only use things which have a MIME Type set
+ if ( isset( $attachment["mediaType"] ) ) {
+ $mediaType = explode( "/", $attachment["mediaType"])[0];
+
+ if ( "image" == $mediaType ) {
+ // Get the alt text
+ isset( $attachment["name"] ) ? $alt = $attachment["name"] : $alt = "";
+ $content .= "";
+ } else if ( "video" == $mediaType ) {
+ $content .= "";
+ }else if ( "audio" == $mediaType ) {
+ $content .= "";
+ }
+ }
+ }
+ }
+
+ echo "