merge-requests/5/head
Terence Eden 2024-02-13 13:30:36 +00:00
rodzic 911ba84220
commit a4360594df
1 zmienionych plików z 53 dodań i 36 usunięć

Wyświetl plik

@ -16,6 +16,7 @@
// Set up the Actor's information // Set up the Actor's information
$username = rawurlencode("example"); // Encoded as it is often used as part of a URl $username = rawurlencode("example"); // Encoded as it is often used as part of a URl
$realName = "E. Xample. Jr."; $realName = "E. Xample. Jr.";
$summary = "Some text about the user.";
$server = $_SERVER['SERVER_NAME']; // Domain name this is hosted on $server = $_SERVER['SERVER_NAME']; // Domain name this is hosted on
// Generate locally or from https://cryptotools.net/rsagen // Generate locally or from https://cryptotools.net/rsagen
@ -28,15 +29,15 @@
// Get all headers and requests sent to this server // Get all headers and requests sent to this server
$headers = print_r( getallheaders(), true ); $headers = print_r( getallheaders(), true );
$postData = print_r( $_POST, true ); $postData = print_r( $_POST, true );
$getData = print_r( $_GET, true ); $getData = print_r( $_GET, true );
$filesData = print_r( $_FILES, true ); $filesData = print_r( $_FILES, true );
$body = json_decode( file_get_contents( "php://input"), true ); $body = json_decode( file_get_contents( "php://input" ), true );
$bodyData = print_r( $input, true ); $bodyData = print_r( $input, true );
$requestData = print_r( $_REQUEST, true ); $requestData = print_r( $_REQUEST, true );
$serverData = print_r( $_SERVER, true ); $serverData = print_r( $_SERVER, true );
// Get the type of request // Get the type of request - used in the log filename
if ( isset( $body["type"] ) ) { if ( isset( $body["type"] ) ) {
$type = " " . $body["type"]; $type = " " . $body["type"];
} else { } else {
@ -48,10 +49,12 @@
// Filename for the log // Filename for the log
$filename = "{$timestamp}{$type}.txt"; $filename = "{$timestamp}{$type}.txt";
// Save headers and request data to the timestamped file // Save headers and request data to the timestamped file in the logs directory
file_put_contents( $filename, if( ! is_dir( "logs" ) ) { mkdir( "logs"); }
file_put_contents( "logs/{$filename}",
"Headers: \n$headers \n\n" . "Headers: \n$headers \n\n" .
"Body Data: \n$bodyData \n\n" . "Body Data: \n$bodyData \n\n" .
"POST Data: \n$postData \n\n" . "POST Data: \n$postData \n\n" .
"GET Data: \n$getData \n\n" . "GET Data: \n$getData \n\n" .
"Files Data: \n$filesData \n\n" . "Files Data: \n$filesData \n\n" .
@ -64,10 +67,10 @@
$path = $_GET["path"]; $path = $_GET["path"];
switch ($path) { switch ($path) {
case "": case "":
echo "Silence"; die();
case ".well-known/webfinger": case ".well-known/webfinger":
webfinger(); webfinger();
case "{$username}": case rawurldecode( $username ):
username(); username();
case "following": case "following":
following(); following();
@ -97,7 +100,7 @@
) )
) )
); );
header("Content-Type: application/json"); header( "Content-Type: application/json" );
echo json_encode( $webfinger ); echo json_encode( $webfinger );
die(); die();
} }
@ -118,7 +121,7 @@
"inbox" => "https://{$server}/inbox", "inbox" => "https://{$server}/inbox",
"preferredUsername" => rawurldecode($username), "preferredUsername" => rawurldecode($username),
"name" => "{$realName}", "name" => "{$realName}",
"summary" => "A single file ActivityPub server.", "summary" => "{$summary}",
"url" => "https://{$server}", "url" => "https://{$server}",
"manuallyApprovesFollowers" => true, "manuallyApprovesFollowers" => true,
"discoverable" => true, "discoverable" => true,
@ -134,7 +137,7 @@
"publicKeyPem" => $key_public "publicKeyPem" => $key_public
] ]
); );
header("Content-Type: application/activity+json"); header( "Content-Type: application/activity+json" );
echo json_encode( $user ); echo json_encode( $user );
die(); die();
} }
@ -150,7 +153,7 @@
"totalItems" => 0, "totalItems" => 0,
"items" => [] "items" => []
); );
header("Content-Type: application/activity+json"); header( "Content-Type: application/activity+json" );
echo json_encode( $following ); echo json_encode( $following );
die(); die();
} }
@ -165,7 +168,7 @@
"totalItems" => 0, "totalItems" => 0,
"items" => [] "items" => []
); );
header("Content-Type: application/activity+json"); header( "Content-Type: application/activity+json" );
echo json_encode( $followers ); echo json_encode( $followers );
die(); die();
} }
@ -184,8 +187,8 @@
// Get the parameters // Get the parameters
$inbox_id = $inbox_message["id"]; $inbox_id = $inbox_message["id"];
$inbox_actor = $inbox_message["actor"]; $inbox_actor = $inbox_message["actor"];
$inbox_url = parse_url($inbox_actor, PHP_URL_SCHEME) . "://" . parse_url($inbox_actor, PHP_URL_HOST); $inbox_url = parse_url( $inbox_actor, PHP_URL_SCHEME ) . "://" . parse_url( $inbox_actor, PHP_URL_HOST );
$inbox_host = parse_url($inbox_actor, PHP_URL_HOST); $inbox_host = parse_url( $inbox_actor, PHP_URL_HOST );
// Does this account have any followers? // Does this account have any followers?
if( file_exists( "followers.json" ) ) { if( file_exists( "followers.json" ) ) {
@ -224,20 +227,25 @@
// The Accept is sent to the server of the user who requested the follow // The Accept is sent to the server of the user who requested the follow
// TODO: The path doesn't *always* end with/inbox // TODO: The path doesn't *always* end with/inbox
$host = $inbox_host; $host = $inbox_host;
$path = parse_url($inbox_actor, PHP_URL_PATH) . "/inbox"; $path = parse_url( $inbox_actor, PHP_URL_PATH ) . "/inbox";
// Set up signing // Set up signing
$keyId = "https://{$server}/{$username}#main-key"; $keyId = "https://{$server}/{$username}#main-key";
// Generate signing variables // Generate signing variables
$hash = hash('sha256', $message_json, true); $hash = hash( 'sha256', $message_json, true );
$digest = base64_encode($hash); $digest = base64_encode( $hash );
$date = date('D, d M Y H:i:s \G\M\T'); $date = date( 'D, d M Y H:i:s \G\M\T' );
$signer = openssl_get_privatekey($key_private); $signer = openssl_get_privatekey( $key_private );
$stringToSign = "(request-target): post $path\nhost: $host\ndate: $date\ndigest: SHA-256=$digest"; $stringToSign = "(request-target): post $path\nhost: $host\ndate: $date\ndigest: SHA-256=$digest";
openssl_sign($stringToSign, $signature, $signer, OPENSSL_ALGO_SHA256); openssl_sign(
$signature_b64 = base64_encode($signature); $stringToSign,
$signature,
$signer,
OPENSSL_ALGO_SHA256
);
$signature_b64 = base64_encode( $signature );
$header = 'keyId="' . $keyId . '",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="' . $signature_b64 . '"'; $header = 'keyId="' . $keyId . '",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="' . $signature_b64 . '"';
@ -273,7 +281,7 @@
function uuid() { function uuid() {
// Date sortable UUID // Date sortable UUID
return sprintf('%08x-%04x-%04x-%04x-%012x', return sprintf( '%08x-%04x-%04x-%04x-%012x',
time(), time(),
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
@ -317,13 +325,15 @@ HTML;
// Get the posted content // Get the posted content
$content = $_POST["content"]; $content = $_POST["content"];
// Current time // Current time - ISO8601
$timestamp = date("c"); $timestamp = date( "c" );
// Outgoing Message ID // Outgoing Message ID
$guid = uuid(); $guid = uuid();
// Construct the Note // Construct the Note
// contentMap is used to prevent unnecessary "translate this post" pop ups
// hardcoded to English
$note = [ $note = [
"@context" => array( "@context" => array(
"https://www.w3.org/ns/activitystreams" "https://www.w3.org/ns/activitystreams"
@ -354,10 +364,12 @@ HTML;
$message_json = json_encode($message); $message_json = json_encode($message);
// Create the context for the permalink // Create the context for the permalink
$note = [ "@context" => "https://www.w3.org/ns/activitystreams", ...$note]; $note = [ "@context" => "https://www.w3.org/ns/activitystreams", ...$note ];
// Save the permalink // Save the permalink
$note_json = json_encode( $note ); $note_json = json_encode( $note );
// Check for posts/ directory and create it
if( ! is_dir( "posts" ) ) { mkdir( "posts"); }
file_put_contents( "posts/{$guid}.json", print_r( $note_json, true ) ); file_put_contents( "posts/{$guid}.json", print_r( $note_json, true ) );
// Read existing users and get their hosts // Read existing users and get their hosts
@ -380,12 +392,17 @@ HTML;
$hash = hash( "sha256", $message_json, true ); $hash = hash( "sha256", $message_json, true );
$digest = base64_encode( $hash ); $digest = base64_encode( $hash );
$date = date('D, d M Y H:i:s \G\M\T'); $date = date( 'D, d M Y H:i:s \G\M\T' );
$signer = openssl_get_privatekey( $key_private ); $signer = openssl_get_privatekey( $key_private );
$stringToSign = "(request-target): post $path\nhost: $host\ndate: $date\ndigest: SHA-256=$digest"; $stringToSign = "(request-target): post $path\nhost: $host\ndate: $date\ndigest: SHA-256=$digest";
openssl_sign( $stringToSign, $signature, $signer, OPENSSL_ALGO_SHA256 ); openssl_sign(
$signature_b64 = base64_encode($signature); $stringToSign,
$signature,
$signer,
OPENSSL_ALGO_SHA256
);
$signature_b64 = base64_encode( $signature );
$header = 'keyId="' . $keyId . '",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="' . $signature_b64 . '"'; $header = 'keyId="' . $keyId . '",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="' . $signature_b64 . '"';
@ -430,6 +447,6 @@ HTML;
die(); die();
} }
die(); die();
die(); die();
die(); die();