2018-09-10 00:48:14 +00:00
|
|
|
<?php
|
|
|
|
namespace deliver;
|
|
|
|
|
2018-09-14 01:52:13 +00:00
|
|
|
require_once plugin_dir_path( __FILE__ ) . 'activities.php';
|
|
|
|
|
2018-09-24 21:36:30 +00:00
|
|
|
// TODO look at inReplyTo, object, target, and tag objects
|
|
|
|
// and deliver to their audience as well. Recurse through these
|
|
|
|
// objects up to some limit
|
|
|
|
|
2018-09-10 00:48:14 +00:00
|
|
|
function deliver_activity( $activity ) {
|
|
|
|
$recipients = array();
|
2018-09-15 19:18:20 +00:00
|
|
|
foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $field ) {
|
2018-09-10 00:48:14 +00:00
|
|
|
$recipients = array_merge(
|
|
|
|
$recipients, retrieve_recipients_for_field( $field, $activity )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$recipients = array_unique( $recipients );
|
|
|
|
if ( array_key_exists( 'actor', $activity ) ) {
|
|
|
|
$recipients = remove_actor_inbox_from_recipients( $activity['actor'], $recipients );
|
|
|
|
}
|
2018-09-14 01:52:13 +00:00
|
|
|
$activity = \activities\strip_private_fields( $activity );
|
2018-09-10 00:48:14 +00:00
|
|
|
post_activity_to_inboxes( $activity, $recipients );
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_actor_inbox_from_recipients( $actor, $recipients ) {
|
|
|
|
if ( array_key_exists( 'inbox', $actor ) ) {
|
|
|
|
$key = array_search( $actor['inbox'], $recipients );
|
|
|
|
if ( $key ) {
|
2018-09-15 19:18:20 +00:00
|
|
|
unset( $recipients[$key] );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
}
|
|
|
|
|
|
|
|
function retrieve_recipients_for_field( $field, $activity ) {
|
|
|
|
if ( array_key_exists( $field, $activity ) ) {
|
2018-09-22 12:35:12 +00:00
|
|
|
$to_value = $activity[$field];
|
|
|
|
return get_recipient_urls( $to_value, 0, array() );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
2018-09-22 12:35:12 +00:00
|
|
|
return array();
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 12:35:12 +00:00
|
|
|
/*
|
|
|
|
$object is either an Actor, a Link, a Collection/OrderedCollection,
|
|
|
|
a single object id (url), or an array of object ids. If it's a url, it
|
|
|
|
should dereference to one of the above types
|
|
|
|
*/
|
|
|
|
function get_recipient_urls( $object, $depth, $acc ) {
|
2018-09-21 19:46:25 +00:00
|
|
|
if ( $depth === 30 ) {
|
2018-09-22 12:35:12 +00:00
|
|
|
return $acc;
|
2018-09-21 19:46:25 +00:00
|
|
|
}
|
2018-09-22 12:35:12 +00:00
|
|
|
if ( array_key_exists( 'type', $object ) ) {
|
|
|
|
// It's an Actor, Link, or Collection
|
|
|
|
switch ( $object['type'] ) {
|
|
|
|
case Collection:
|
|
|
|
case OrderedCollection:
|
|
|
|
$items = array();
|
|
|
|
if ( array_key_exists( 'items', $object ) ) {
|
|
|
|
$items = $object['items'];
|
|
|
|
} else if ( array_key_exists( 'orderedItems', $object ) ) {
|
|
|
|
$items = $object['orderedItems'];
|
|
|
|
}
|
|
|
|
$recipients = $acc;
|
2018-09-10 00:48:14 +00:00
|
|
|
foreach ( $items as $item ) {
|
2018-09-22 12:35:12 +00:00
|
|
|
$recipients = array_merge(
|
|
|
|
$recipients,
|
|
|
|
get_recipient_urls( $item, $depth + 1, array_merge( $recipients, $acc ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
case Link:
|
|
|
|
if ( array_key_exists( 'href', $object ) ) {
|
2018-09-24 21:36:30 +00:00
|
|
|
$response = wp_remote_get( $object['href'] );
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$link_target = wp_remote_retrieve_body( $response );
|
|
|
|
if ( empty( $link_target ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return get_recipient_urls( json_decode( $link_target, true ), $depth + 1, $acc );
|
2018-09-22 12:35:12 +00:00
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// An Actor
|
|
|
|
if ( array_key_exists( 'inbox', $object ) ) {
|
|
|
|
return array( $object['inbox'] );
|
|
|
|
} else {
|
|
|
|
return array();
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 12:35:12 +00:00
|
|
|
} else {
|
|
|
|
// Assume it's an array of object ids (urls) or a single url
|
|
|
|
if ( is_array( $object ) ) {
|
|
|
|
$recipients = $acc;
|
|
|
|
foreach( $object as $url ) {
|
|
|
|
$recipients = array_merge(
|
|
|
|
$recipients,
|
|
|
|
get_recipient_urls( $url, $depth + 1, array_merge( $recipients, $acc ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $recipients;
|
|
|
|
} else {
|
|
|
|
if ( filter_var( $object, FILTER_VALIDATE_URL ) ) {
|
2018-09-24 21:36:30 +00:00
|
|
|
$response = wp_remote_get( $object );
|
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$response_body = wp_remote_retrieve_body( $response );
|
|
|
|
if ( empty( $response_body ) ) {
|
|
|
|
return $array();
|
|
|
|
}
|
|
|
|
return get_recipient_urls( json_decode( $response_body, true ), $depth + 1, $acc );
|
2018-09-22 12:35:12 +00:00
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function post_activity_to_inboxes( $activity, $recipients ) {
|
|
|
|
foreach ( $inbox as $recipients ) {
|
2018-09-10 00:51:59 +00:00
|
|
|
$args = array(
|
|
|
|
'body' => $activity,
|
|
|
|
'headers' => array( 'Content-Type' => 'application/ld+json' )
|
|
|
|
);
|
|
|
|
// TODO do something with the result?
|
|
|
|
wp_remote_post( $inbox, $args );
|
2018-09-10 00:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|