+ return $data;
+ }
+
+ /**
+ * @brief Returns a tag array for a given item array
+ *
+ * @param array $item
+ *
+ * @return array of tags
+ */
+ private static function createTagList($item)
+ {
+ $tags = [];
+
+ $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION);
+ foreach ($terms as $term) {
+ $contact = Contact::getDetailsByURL($term['url']);
+ if (!empty($contact['addr'])) {
+ $mention = '@' . $contact['addr'];
+ } else {
+ $mention = '@' . $term['url'];
+ }
+
+ $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
+ }
+ return $tags;
+ }
+
+ /**
+ * @brief Fetches the "context" value for a givem item array from the "conversation" table
+ *
+ * @param array $item
+ *
+ * @return string with context url
+ */
+ private static function fetchContextURLForItem($item)
+ {
+ $conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
+ if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
+ $context_uri = $conversation['conversation-href'];
+ } elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
+ $context_uri = $conversation['conversation-uri'];
+ } else {
+ $context_uri = str_replace('/objects/', '/context/', $item['parent-uri']);
+ }
+ return $context_uri;
+ }
+
+ /**
+ * @brief Creates a note/article object array
+ *
+ * @param array $item
+ *
+ * @return object array
+ */
+ private static function createNote($item)
+ {
+ if (!empty($item['title'])) {
+ $type = 'Article';
+ } else {
+ $type = 'Note';
+ }
+
+ if ($item['deleted']) {
+ $type = 'Tombstone';
+ }
+
+ $data = [];
+ $data['id'] = $item['uri'];
+ $data['type'] = $type;
+
+ if ($item['deleted']) {
+ return $data;
+ }
+
+ $data['summary'] = null; // Ignore by now
+
+ if ($item['uri'] != $item['thr-parent']) {
+ $data['inReplyTo'] = $item['thr-parent'];
+ } else {
+ $data['inReplyTo'] = null;
+ }
+
+ $data['diaspora:guid'] = $item['guid'];
+ $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
+
+ if ($item["created"] != $item["edited"]) {
+ $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
+ }
+
+ $data['url'] = $item['plink'];
+ $data['attributedTo'] = $item['author-link'];
+ $data['actor'] = $item['author-link'];
+ $data['sensitive'] = false; // - Query NSFW
+ $data['context'] = self::fetchContextURLForItem($item);
+
+ if (!empty($item['title'])) {
+ $data['name'] = BBCode::convert($item['title'], false, 7);
+ }
+
+ $data['content'] = BBCode::convert($item['body'], false, 7);
+ $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
+
+ if (!empty($item['signed_text']) && ($item['uri'] != $item['thr-parent'])) {
+ $data['diaspora:comment'] = $item['signed_text'];
+ }
+
+ $data['attachment'] = []; // @ToDo
+ $data['tag'] = self::createTagList($item);
+ $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
+
+ return $data;
+ }
+
+ /**
+ * @brief Transmits a profile deletion to a given inbox
+ *
+ * @param integer $uid User ID
+ * @param string $inbox Target inbox
+ */
+ public static function transmitProfileDeletion($uid, $inbox)
+ {
+ $owner = User::getOwnerDataById($uid);
+ $profile = APContact::getByURL($owner['url']);
+
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Delete',
+ 'actor' => $owner['url'],
+ 'object' => self::profile($uid),
+ 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+ 'to' => [self::PUBLIC_COLLECTION],
+ 'cc' => []];
+
+ $signed = LDSignature::sign($data, $owner);
+
+ logger('Deliver profile deletion for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
+ HTTPSignature::transmit($signed, $inbox, $uid);
+ }
+
+ /**
+ * @brief Transmits a profile change to a given inbox
+ *
+ * @param integer $uid User ID
+ * @param string $inbox Target inbox
+ */
+ public static function transmitProfileUpdate($uid, $inbox)
+ {
+ $owner = User::getOwnerDataById($uid);
+ $profile = APContact::getByURL($owner['url']);
+
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Update',
+ 'actor' => $owner['url'],
+ 'object' => self::profile($uid),
+ 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
+ 'to' => [$profile['followers']],
+ 'cc' => []];
+
+ $signed = LDSignature::sign($data, $owner);
+
+ logger('Deliver profile update for user ' . $uid . ' to ' . $inbox .' via ActivityPub', LOGGER_DEBUG);
+ HTTPSignature::transmit($signed, $inbox, $uid);
+ }
+
+ /**
+ * @brief Transmits a given activity to a target
+ *
+ * @param array $activity
+ * @param string $target Target profile
+ * @param integer $uid User ID
+ */
+ public static function transmitActivity($activity, $target, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $owner = User::getOwnerDataById($uid);
+
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => $activity,
+ 'actor' => $owner['url'],
+ 'object' => $profile['url'],
+ 'to' => $profile['url']];
+
+ logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * @brief Transmit a message that the contact request had been accepted
+ *
+ * @param string $target Target profile
+ * @param $id
+ * @param integer $uid User ID
+ */
+ public static function transmitContactAccept($target, $id, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $owner = User::getOwnerDataById($uid);
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Accept',
+ 'actor' => $owner['url'],
+ 'object' => ['id' => $id, 'type' => 'Follow',
+ 'actor' => $profile['url'],
+ 'object' => $owner['url']],
+ 'to' => $profile['url']];
+
+ logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * @brief
+ *
+ * @param string $target Target profile
+ * @param $id
+ * @param integer $uid User ID
+ */
+ public static function transmitContactReject($target, $id, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $owner = User::getOwnerDataById($uid);
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
+ 'type' => 'Reject',
+ 'actor' => $owner['url'],
+ 'object' => ['id' => $id, 'type' => 'Follow',
+ 'actor' => $profile['url'],
+ 'object' => $owner['url']],
+ 'to' => $profile['url']];
+
+ logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * @brief
+ *
+ * @param string $target Target profile
+ * @param integer $uid User ID
+ */
+ public static function transmitContactUndo($target, $uid)
+ {
+ $profile = APContact::getByURL($target);
+
+ $id = System::baseUrl() . '/activity/' . System::createGUID();
+
+ $owner = User::getOwnerDataById($uid);
+ $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
+ 'id' => $id,
+ 'type' => 'Undo',
+ 'actor' => $owner['url'],
+ 'object' => ['id' => $id, 'type' => 'Follow',
+ 'actor' => $owner['url'],
+ 'object' => $profile['url']],
+ 'to' => $profile['url']];
+
+ logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
+
+ $signed = LDSignature::sign($data, $owner);
+ HTTPSignature::transmit($signed, $profile['inbox'], $uid);
+ }
+
+ /**
+ * Fetches ActivityPub content from the given url
+ *
+ * @param string $url content url
+ * @return array
+ */
+ public static function fetchContent($url)
+ {
+ $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
+ if (!$ret['success'] || empty($ret['body'])) {
+ return false;
+ }
+
+ return json_decode($ret['body'], true);
+ }
+
+ /**
+ * Fetches a profile from the given url into an array that is compatible to Probe::uri
+ *
+ * @param string $url profile url
+ * @return array
+ */
+ public static function probeProfile($url)
+ {
+ $apcontact = APContact::getByURL($url, true);
+ if (empty($apcontact)) {
+ return false;
+ }
+
+ $profile = ['network' => Protocol::ACTIVITYPUB];
+ $profile['nick'] = $apcontact['nick'];
+ $profile['name'] = $apcontact['name'];
+ $profile['guid'] = $apcontact['uuid'];
+ $profile['url'] = $apcontact['url'];
+ $profile['addr'] = $apcontact['addr'];
+ $profile['alias'] = $apcontact['alias'];
+ $profile['photo'] = $apcontact['photo'];
+ // $profile['community']
+ // $profile['keywords']
+ // $profile['location']
+ $profile['about'] = $apcontact['about'];
+ $profile['batch'] = $apcontact['sharedinbox'];
+ $profile['notify'] = $apcontact['inbox'];
+ $profile['poll'] = $apcontact['outbox'];
+ $profile['pubkey'] = $apcontact['pubkey'];
+ $profile['baseurl'] = $apcontact['baseurl'];
+
+ // Remove all "null" fields
+ foreach ($profile as $field => $content) {
+ if (is_null($content)) {
+ unset($profile[$field]);
+ }
+ }
+
+ return $profile;
+ }
+
+ /**
+ * @brief
+ *
+ * @param $body
+ * @param $header
+ * @param integer $uid User ID
+ */
+ public static function processInbox($body, $header, $uid)
+ {
+ $http_signer = HTTPSignature::getSigner($body, $header);
+ if (empty($http_signer)) {
+ logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
+ return;
+ } else {
+ logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
+ }
+
+ $activity = json_decode($body, true);
+
+ $actor = JsonLD::fetchElement($activity, 'actor', 'id');
+ logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
+
+ if (empty($activity)) {
+ logger('Invalid body.', LOGGER_DEBUG);
+ return;
+ }
+
+ if (LDSignature::isSigned($activity)) {
+ $ld_signer = LDSignature::getSigner($activity);
+ if (empty($ld_signer)) {
+ logger('Invalid JSON-LD signature from ' . $actor, LOGGER_DEBUG);
+ }
+ if (!empty($ld_signer && ($actor == $http_signer))) {
+ logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
+ $trust_source = true;
+ } elseif (!empty($ld_signer)) {
+ logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
+ $trust_source = true;
+ } elseif ($actor == $http_signer) {
+ logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
+ $trust_source = true;
+ } else {
+ logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
+ $trust_source = false;
+ }
+ } elseif ($actor == $http_signer) {
+ logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
+ $trust_source = true;
+ } else {
+ logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
+ $trust_source = false;
+ }
+
+ self::processActivity($activity, $body, $uid, $trust_source);
+ }
+
+ /**
+ * @brief
+ *
+ * @param $url
+ * @param integer $uid User ID
+ */
+ public static function fetchOutbox($url, $uid)
+ {
+ $data = self::fetchContent($url);
+ if (empty($data)) {
+ return;
+ }
+
+ if (!empty($data['orderedItems'])) {
+ $items = $data['orderedItems'];
+ } elseif (!empty($data['first']['orderedItems'])) {
+ $items = $data['first']['orderedItems'];
+ } elseif (!empty($data['first'])) {
+ self::fetchOutbox($data['first'], $uid);
+ return;
+ } else {
+ $items = [];
+ }
+
+ foreach ($items as $activity) {
+ self::processActivity($activity, '', $uid, true);
+ }
+ }
+
+ /**
+ * @brief
+ *
+ * @param array $activity
+ * @param integer $uid User ID
+ * @param $trust_source
+ *
+ * @return
+ */
+ private static function prepareObjectData($activity, $uid, &$trust_source)
+ {
+ $actor = JsonLD::fetchElement($activity, 'actor', 'id');
+ if (empty($actor)) {
+ logger('Empty actor', LOGGER_DEBUG);
+ return [];
+ }
+
+ // Fetch all receivers from to, cc, bto and bcc
+ $receivers = self::getReceivers($activity, $actor);
+
+ // When it is a delivery to a personal inbox we add that user to the receivers
+ if (!empty($uid)) {
+ $owner = User::getOwnerDataById($uid);
+ $additional = ['uid:' . $uid => $uid];
+ $receivers = array_merge($receivers, $additional);
+ }
+
+ logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
+
+ $object_id = JsonLD::fetchElement($activity, 'object', 'id');
+ if (empty($object_id)) {
+ logger('No object found', LOGGER_DEBUG);
+ return [];
+ }
+
+ // Fetch the content only on activities where this matters
+ if (in_array($activity['type'], ['Create', 'Announce'])) {
+ $object_data = self::fetchObject($object_id, $activity['object'], $trust_source);
+ if (empty($object_data)) {
+ logger("Object data couldn't be processed", LOGGER_DEBUG);
+ return [];
+ }
+ // We had been able to retrieve the object data - so we can trust the source
+ $trust_source = true;
+ } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
+ // Create a mostly empty array out of the activity data (instead of the object).
+ // This way we later don't have to check for the existence of ech individual array element.
+ $object_data = self::processObject($activity);
+ $object_data['name'] = $activity['type'];
+ $object_data['author'] = $activity['actor'];
+ $object_data['object'] = $object_id;
+ $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type
+ } else {
+ $object_data = [];
+ $object_data['id'] = $activity['id'];
+ $object_data['object'] = $activity['object'];
+ $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
+ }
+
+ $object_data = self::addActivityFields($object_data, $activity);
+
+ $object_data['type'] = $activity['type'];
+ $object_data['owner'] = $actor;
+ $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
+
+ logger('Processing ' . $object_data['type'] . ' ' . $object_data['object_type'] . ' ' . $object_data['id'], LOGGER_DEBUG);
+
+ return $object_data;
+ }
+
+ /**
+ * @brief
+ *
+ * @param array $activity
+ * @param $body
+ * @param integer $uid User ID
+ * @param $trust_source
+ */
+ private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
+ {
+ if (empty($activity['type'])) {
+ logger('Empty type', LOGGER_DEBUG);
+ return;
+ }
+
+ if (empty($activity['object'])) {
+ logger('Empty object', LOGGER_DEBUG);
+ return;
+ }
+
+ if (empty($activity['actor'])) {
+ logger('Empty actor', LOGGER_DEBUG);
+ return;
+
+ }
+
+ // $trust_source is called by reference and is set to true if the content was retrieved successfully
+ $object_data = self::prepareObjectData($activity, $uid, $trust_source);
+ if (empty($object_data)) {
+ logger('No object data found', LOGGER_DEBUG);
+ return;
+ }
+
+ if (!$trust_source) {
+ logger('No trust for activity type "' . $activity['type'] . '", so we quit now.', LOGGER_DEBUG);
+ }
+
+ switch ($activity['type']) {
+ case 'Create':
+ case 'Announce':
+ self::createItem($object_data, $body);
+ break;
+
+ case 'Like':
+ self::likeItem($object_data, $body);
+ break;
+
+ case 'Dislike':
+ self::dislikeItem($object_data, $body);
+ break;
+
+ case 'Update':
+ if (in_array($object_data['object_type'], self::CONTENT_TYPES)) {
+ /// @todo
+ } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
+ self::updatePerson($object_data, $body);
+ }
+ break;
+
+ case 'Delete':
+ if ($object_data['object_type'] == 'Tombstone') {
+ self::deleteItem($object_data, $body);
+ } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) {
+ self::deletePerson($object_data, $body);
+ }
+ break;