From 185070df160e4d92c4673b6ad667637029173b8f Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 27 Sep 2018 20:45:03 -0400 Subject: [PATCH] Dereference objects wherever possible --- includes/server/deliver.php | 1 + includes/server/followers.php | 3 +++ includes/server/inbox.php | 2 ++ includes/server/objects.php | 42 +++++++++++++++++++------------- includes/server/outbox.php | 5 ++++ includes/util.php | 45 ++++++++++++++++++++++++----------- 6 files changed, 68 insertions(+), 30 deletions(-) diff --git a/includes/server/deliver.php b/includes/server/deliver.php index 36f200b..ae14112 100644 --- a/includes/server/deliver.php +++ b/includes/server/deliver.php @@ -18,6 +18,7 @@ function deliver_activity( $activity ) { } $recipients = array_unique( $recipients ); if ( array_key_exists( 'actor', $activity ) ) { + $actor = \util\dereference_object( $activity['actor'] ); $recipients = remove_actor_inbox_from_recipients( $activity['actor'], $recipients ); } $activity = \activities\strip_private_fields( $activity ); diff --git a/includes/server/followers.php b/includes/server/followers.php index 958a444..0187fdb 100644 --- a/includes/server/followers.php +++ b/includes/server/followers.php @@ -3,6 +3,7 @@ namespace followers; require_once plugin_dir_path( __FILE__ ) . 'actors.php'; require_once plugin_dir_path( __FILE__ ) . 'objects.php'; +require_once plugin_dir_path( __FILE__ ) . '../util.php'; function add_follower( $actor_slug, $follower ) { global $wpdb; @@ -14,6 +15,7 @@ function add_follower( $actor_slug, $follower ) { array( 'status' => 404 ) ); } + $follower = \util\dereference_object( $follower ); if ( !array_key_exists( 'id', $follower ) ) { return new \WP_Error( 'invalid_object', @@ -45,6 +47,7 @@ function remove_follower( $actor_slug, $follower ) { array( 'status' => 404 ) ); } + $follower = \util\dereference_object( $follower ); if ( !array_key_exists( 'id', $follower ) ) { return new \WP_Error( 'invalid_object', diff --git a/includes/server/inbox.php b/includes/server/inbox.php index 04e838d..adfd827 100644 --- a/includes/server/inbox.php +++ b/includes/server/inbox.php @@ -20,8 +20,10 @@ require_once plugin_dir_path( __FILE__ ) . 'activities/accept.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/reject.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/announce.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/undo.php'; +require_once plugin_dir_path( __FILE__ ) . '../util.php'; function handle_activity( $actor_slug, $activity ) { + $activity = \util\dereference_object( $activity ); if ( !array_key_exists( 'type', $activity ) ) { return new \WP_Error( 'invalid_activity', diff --git a/includes/server/objects.php b/includes/server/objects.php index ff1ede3..7bde197 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -1,11 +1,17 @@ 404 ) - ); - } - $body_array = json_decode( $body, true ); - return $body_array; + return get_object_from_url( $object, $depth ); } else { return new \WP_Error( 'invalid_object', @@ -31,6 +31,23 @@ function dereference_object( $object ) { } } +function get_object_from_url( $url, $depth ) { + $response = wp_remote_get( $url ); + if ( is_wp_error( $response ) ) { + return $response; + } + $body = wp_remote_retrieve_body( $response ); + if ( empty( $body ) ) { + return new \WP_Error( + 'not_found', + __( 'The object did not dereference to a valid object', 'pterotype' ), + array( 'status' => 404 ) + ); + } + $body_array = json_decode( $body, true ); + return dereference_object_helper( $body_array, $depth + 1 ); +} + function is_same_object( $object1, $object2 ) { return get_id( $object1 ) === get_id( $object2 ); }