diff --git a/index.php b/index.php index 0ca1193..4632878 100644 --- a/index.php +++ b/index.php @@ -43,7 +43,7 @@ $server = $_SERVER["SERVER_NAME"]; // Do not change this! // Set up where logs and messages go. - // If you want to, you can change these directories to something more suitable for you. + // You can change these directories to something more suitable if you like. $data = "data"; $directories = array( "inbox" => "{$data}/inbox", @@ -53,6 +53,7 @@ "posts" => "posts", "images" => "images", ); + // Create the directories if they don't already exist. foreach ( $directories as $directory ) { if( !is_dir( $directory ) ) { mkdir( $data ); mkdir( $directory ); } } @@ -71,7 +72,8 @@ $bodyData = print_r( $body, true ); $requestData = print_r( $_REQUEST, true ); $serverData = print_r( $_SERVER, true ); - !empty( $_GET["path"] ) ? $path = $_GET["path"] : $path = "/"; + // If the root has been requested, manually set the path to `/` + !empty( $_GET["path"] ) ? $path = $_GET["path"] : $path = "/"; // Get the type of request - used in the log filename if ( isset( $body["type"] ) ) { @@ -123,21 +125,21 @@ case "outbox": outbox(); // Optional. Dynamic. case "write": - write(); // User interface for writing posts + write(); // User interface for writing posts. case "send": - send(); // API for posting content to the Fediverse + send(); // API for posting content to the Fediverse. 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 + follow_user(); // API for following a user. case "read": - view( "read" ); // User interface for reading posts + view( "read" );// User interface for reading posts. case ".well-known/nodeinfo": - wk_nodeinfo(); // Optional. Static. + wk_nodeinfo(); // Optional. Static. case "nodeinfo/2.1": - nodeinfo(); // Optional. Static. + nodeinfo(); // Optional. Static. case "/": - view( "home" ); // Optional. Can be dynamic + view( "home" );// Optional. Can be dynamic default: die(); } @@ -221,7 +223,7 @@ // Create a list of all the followers $items = array(); foreach ( $following_files as $following_file ) { - $following = json_decode( file_get_contents( $following_file ),true ); + $following = json_decode( file_get_contents( $following_file ), true ); $items[] = $following["id"]; } @@ -238,8 +240,8 @@ } function followers() { global $server, $directories; - // The number of followers is self-reported - // You can set this to any number you like + // The number of followers is self-reported. + // You can set this to any number you like. // Get all the files $follower_files = glob( $directories["followers"] . "/*.json"); @@ -322,13 +324,13 @@ $inbox_actor_json = curl_exec( $ch ); // Check for errors - if (curl_errno($ch)) { + if ( curl_errno( $ch ) ) { // TODO: Handle cURL error die(); } // Close cURL session - curl_close($ch); + curl_close( $ch ); // Save the actor's data in `/data/followers/` $follower_filename = urlencode( $follower_actor ); @@ -362,24 +364,7 @@ ]; // The Accept is POSTed to the inbox on the server of the user who requested the follow - $follower_inbox_path = parse_url( $follower_inbox, PHP_URL_PATH ); - // Generate the signed headers - $headers = generate_signed_headers( $message, $follower_host, $follower_inbox_path, "POST" ); - - // POST the message and header to the requester's inbox - $ch = curl_init( $follower_inbox ); - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); - curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" ); - curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $message ) ); - curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); - curl_exec( $ch ); - - // Check for errors - if( curl_errno( $ch ) ) { - $timestamp = ( new DateTime() )->format( DATE_RFC3339_EXTENDED ); - file_put_contents( $directories["logs"] . "/{$timestamp}.Error.txt", curl_error( $ch ) ); - } - curl_close( $ch ); + sentMessageToSingle( $follower_inbox, $message ); die(); } @@ -405,7 +390,7 @@ global $server, $username, $key_private; // Location of the Public Key - $keyId = "https://{$server}/{$username}#main-key"; + $keyId = "https://{$server}/{$username}#main-key"; // Get the Private Key $signer = openssl_get_privatekey( $key_private ); @@ -413,7 +398,7 @@ // Timestamp this message was sent $date = date( "D, d M Y H:i:s \G\M\T" ); - // There are subtly different signing requirements for POST and GET + // There are subtly different signing requirements for POST and GET. if ( "POST" == $method ) { // Encode the message object to JSON $message_json = json_encode( $message ); @@ -497,64 +482,64 @@ } // Counters for followers, following, and posts - $follower_files = glob( $directories["followers"] . "/*.json"); + $follower_files = glob( $directories["followers"] . "/*.json" ); $totalFollowers = count( $follower_files ); - $following_files = glob( $directories["following"] . "/*.json"); + $following_files = glob( $directories["following"] . "/*.json" ); $totalFollowing = count( $following_files ); // Show the HTML page echo <<< HTML - - - - - - - - - - - {$h1} {$realName} - - - -
-
- -
-

{$realName}

-

-
-

{$summary}

-

Following: {$totalFollowing} | Followers: {$totalFollowers}

-
-

This software is licenced under AGPL 3.0.

-

This site is a basic ActivityPub server designed to be a lightweight educational tool.

-
-
-