Better user agent. Better follower profile

merge-requests/5/head
Terence Eden 2024-03-04 13:16:39 +00:00
rodzic ad781e6fab
commit c8920dbde0
1 zmienionych plików z 18 dodań i 42 usunięć

Wyświetl plik

@ -42,6 +42,9 @@
// Internal data
$server = $_SERVER["SERVER_NAME"]; // Do not change this!
// Some requests require a User-Agent string.
define("USERAGENT", "activitypub-single-php-file/0.0");
// Set up where logs and messages go.
// You can change these directories to something more suitable if you like.
$data = "data";
@ -301,48 +304,15 @@
// Get the parameters
$follower_id = $inbox_message["id"]; // E.g. https://mastodon.social/(unique id)
$follower_actor = $inbox_message["actor"]; // E.g. https://mastodon.social/users/Edent
$follower_host = parse_url( $follower_actor, PHP_URL_HOST ); // E.g. mastodon.social
$follower_path = parse_url( $follower_actor, PHP_URL_PATH ); // E.g. /users/Edent
// Get the actor's profile as JSON
// Is the actor an https URl?
if(
( filter_var( $follower_actor, FILTER_VALIDATE_URL) == true) &&
( parse_url( $follower_actor, PHP_URL_SCHEME ) == "https" )
) {
// Request the JSON representation of the the user
$ch = curl_init( $follower_actor );
// Generate signed headers for this request
$headers = generate_signed_headers( null, $follower_host, $follower_path, "GET" );
// Set cURL options
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_USERAGENT, "activitypub-single-php-file/0.0" );
// Execute the cURL session
$inbox_actor_json = curl_exec( $ch );
// Check for errors
if ( curl_errno( $ch ) ) {
// TODO: Handle cURL error
die();
}
// Close cURL session
curl_close( $ch );
$follower_actor_details = getDataFromURl( $follower_actor );
// Save the actor's data in `/data/followers/`
$follower_filename = urlencode( $follower_actor );
file_put_contents( $directories["followers"] . "/{$follower_filename}.json", $inbox_actor_json );
} else {
die();
}
file_put_contents( $directories["followers"] . "/{$follower_filename}.json", json_encode( $follower_actor_details ) );
// Get the new follower's Inbox
$follower_actor_details = json_decode( $inbox_actor_json, true );
$follower_inbox = $follower_actor_details["inbox"];
// Response Message ID
@ -1029,7 +999,7 @@ HTML;
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $message ) );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_USERAGENT, "activitypub-single-php-file/0.0" );
curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
curl_exec( $ch );
// Check for errors
@ -1091,7 +1061,7 @@ HTML;
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $message ) );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_USERAGENT, "activitypub-single-php-file/0.0" );
curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
// Add the handle to the multi-handle
curl_multi_add_handle( $mh, $ch );
@ -1217,6 +1187,12 @@ HTML;
// GET a request to a URl and returns structured data
function getDataFromURl ( $url ) {
// Check this is a valid https address
if(
( 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 );
@ -1228,7 +1204,7 @@ HTML;
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_USERAGENT, "activitypub-single-php-file/0.0" );
curl_setopt( $ch, CURLOPT_USERAGENT, USERAGENT );
// Execute the cURL session
$urlJSON = curl_exec( $ch );