3 * @file src/Protocol/ActivityPub.php
5 namespace Friendica\Protocol;
7 use Friendica\Database\DBA;
8 use Friendica\Core\System;
9 use Friendica\BaseObject;
10 use Friendica\Util\Network;
11 use Friendica\Util\HTTPSignature;
12 use Friendica\Core\Protocol;
13 use Friendica\Model\Conversation;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Item;
16 use Friendica\Model\Profile;
17 use Friendica\Model\Term;
18 use Friendica\Model\User;
19 use Friendica\Util\DateTimeFormat;
20 use Friendica\Util\Crypto;
21 use Friendica\Content\Text\BBCode;
22 use Friendica\Content\Text\HTML;
23 use Friendica\Util\JsonLD;
24 use Friendica\Util\LDSignature;
25 use Friendica\Core\Config;
28 * @brief ActivityPub Protocol class
29 * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
30 * https://www.w3.org/TR/activitypub/
31 * https://www.w3.org/TR/activitystreams-core/
32 * https://www.w3.org/TR/activitystreams-vocabulary/
34 * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
35 * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
37 * Digest: https://tools.ietf.org/html/rfc5843
38 * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
40 * Mastodon implementation of supported activities:
41 * https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/activity.rb#L26
46 * - Activities: Update (Notes, Person), Delete (Person, Activities, Notes)
47 * - Object Types: Person, Tombstome
50 * - Activities: Announce, Update (Person)
51 * - Object Tyoes: Person
54 * - Queueing unsucessful deliveries
56 * - Polling the outboxes for missing content?
60 const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
61 const CONTEXT = ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
62 ['ostatus' => 'http://ostatus.org#', 'uuid' => 'http://schema.org/identifier',
63 'sensitive' => 'as:sensitive', 'Hashtag' => 'as:Hashtag',
64 'atomUri' => 'ostatus:atomUri', 'conversation' => 'ostatus:conversation',
65 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri']];
67 public static function isRequest()
69 return stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/activity+json') ||
70 stristr(defaults($_SERVER, 'HTTP_ACCEPT', ''), 'application/ld+json');
73 public static function getFollowers($owner, $page = null)
75 $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
76 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
77 $count = DBA::count('contact', $condition);
79 $data = ['@context' => self::CONTEXT];
80 $data['id'] = System::baseUrl() . '/followers/' . $owner['nickname'];
81 $data['type'] = 'OrderedCollection';
82 $data['totalItems'] = $count;
84 // When we hide our friends we will only show the pure number but don't allow more.
85 $profile = Profile::getProfileForUser($owner['uid']);
86 if (!empty($profile['hide-friends'])) {
91 $data['first'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
95 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
96 while ($contact = DBA::fetch($contacts)) {
97 $list[] = $contact['url'];
101 $data['next'] = System::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
104 $data['partOf'] = System::baseUrl() . '/followers/' . $owner['nickname'];
106 $data['orderedItems'] = $list;
112 public static function getFollowing($owner, $page = null)
114 $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::NATIVE_SUPPORT, 'uid' => $owner['uid'],
115 'self' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
116 $count = DBA::count('contact', $condition);
118 $data = ['@context' => self::CONTEXT];
119 $data['id'] = System::baseUrl() . '/following/' . $owner['nickname'];
120 $data['type'] = 'OrderedCollection';
121 $data['totalItems'] = $count;
123 // When we hide our friends we will only show the pure number but don't allow more.
124 $profile = Profile::getProfileForUser($owner['uid']);
125 if (!empty($profile['hide-friends'])) {
130 $data['first'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
134 $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
135 while ($contact = DBA::fetch($contacts)) {
136 $list[] = $contact['url'];
140 $data['next'] = System::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
143 $data['partOf'] = System::baseUrl() . '/following/' . $owner['nickname'];
145 $data['orderedItems'] = $list;
151 public static function getOutbox($owner, $page = null)
153 $public_contact = Contact::getIdForURL($owner['url'], 0, true);
155 $condition = ['uid' => $owner['uid'], 'contact-id' => $owner['id'], 'author-id' => $public_contact,
156 'wall' => true, 'private' => false, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
157 'deleted' => false, 'visible' => true];
158 $count = DBA::count('item', $condition);
160 $data = ['@context' => self::CONTEXT];
161 $data['id'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
162 $data['type'] = 'OrderedCollection';
163 $data['totalItems'] = $count;
166 $data['first'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=1';
170 $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
172 $items = Item::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
173 while ($item = Item::fetch($items)) {
174 $object = self::createObjectFromItemID($item['id']);
175 unset($object['@context']);
180 $data['next'] = System::baseUrl() . '/outbox/' . $owner['nickname'] . '?page=' . ($page + 1);
183 $data['partOf'] = System::baseUrl() . '/outbox/' . $owner['nickname'];
185 $data['orderedItems'] = $list;
192 * Return the ActivityPub profile of the given user
194 * @param integer $uid User ID
197 public static function profile($uid)
199 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
200 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
201 'account_removed' => false, 'verified' => true];
202 $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
203 $user = DBA::selectFirst('user', $fields, $condition);
204 if (!DBA::isResult($user)) {
208 $fields = ['locality', 'region', 'country-name'];
209 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
210 if (!DBA::isResult($profile)) {
214 $fields = ['name', 'url', 'location', 'about', 'avatar'];
215 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
216 if (!DBA::isResult($contact)) {
220 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
221 ['vcard' => 'http://www.w3.org/2006/vcard/ns#', 'uuid' => 'http://schema.org/identifier',
222 'sensitive' => 'as:sensitive', 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers']]];
224 $data['id'] = $contact['url'];
225 $data['uuid'] = $user['guid'];
226 $data['type'] = $accounttype[$user['account-type']];
227 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
228 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
229 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
230 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
231 $data['preferredUsername'] = $user['nickname'];
232 $data['name'] = $contact['name'];
233 $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
234 'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
235 $data['summary'] = $contact['about'];
236 $data['url'] = $contact['url'];
237 $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP]);
238 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
239 'owner' => $contact['url'],
240 'publicKeyPem' => $user['pubkey']];
241 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
242 $data['icon'] = ['type' => 'Image',
243 'url' => $contact['avatar']];
245 // tags: https://kitty.town/@inmysocks/100656097926961126.json
249 private static function fetchPermissionBlockFromConversation($item)
251 if (empty($item['thr-parent'])) {
255 $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
256 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
257 if (!DBA::isResult($conversation)) {
261 $activity = json_decode($conversation['source'], true);
263 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
264 $profile = ActivityPub::fetchprofile($actor);
266 $item_profile = ActivityPub::fetchprofile($item['author-link']);
267 $exclude[] = $item['author-link'];
269 if ($item['gravity'] == GRAVITY_PARENT) {
270 $exclude[] = $item['owner-link'];
275 $elements = ['to', 'cc', 'bto', 'bcc'];
276 foreach ($elements as $element) {
277 if (empty($activity[$element])) {
280 if (is_string($activity[$element])) {
281 $activity[$element] = [$activity[$element]];
283 foreach ($activity[$element] as $receiver) {
284 if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
285 $receiver = $item_profile['followers'];
287 if (!in_array($receiver, $exclude)) {
288 $permissions[$element][] = $receiver;
295 public static function createPermissionBlockForItem($item)
297 $data = ['to' => [], 'cc' => []];
299 $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
301 $actor_profile = ActivityPub::fetchprofile($item['author-link']);
303 $terms = Term::tagArrayFromItemId($item['id']);
305 $contacts[$item['author-link']] = $item['author-link'];
307 if (!$item['private']) {
308 $data['to'][] = self::PUBLIC;
309 if (!empty($actor_profile['followers'])) {
310 $data['cc'][] = $actor_profile['followers'];
313 foreach ($terms as $term) {
314 if ($term['type'] != TERM_MENTION) {
317 $profile = self::fetchprofile($term['url'], false);
318 if (!empty($profile) && empty($contacts[$profile['url']])) {
319 $data['cc'][] = $profile['url'];
320 $contacts[$profile['url']] = $profile['url'];
324 $receiver_list = Item::enumeratePermissions($item);
328 foreach ($terms as $term) {
329 if ($term['type'] != TERM_MENTION) {
332 $cid = Contact::getIdForURL($term['url'], $item['uid']);
333 if (!empty($cid) && in_array($cid, $receiver_list)) {
334 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid, 'network' => Protocol::ACTIVITYPUB]);
335 $data['to'][] = $contact['url'];
336 $contacts[$contact['url']] = $contact['url'];
340 foreach ($receiver_list as $receiver) {
341 $contact = DBA::selectFirst('contact', ['url'], ['id' => $receiver, 'network' => Protocol::ACTIVITYPUB]);
342 if (empty($contacts[$contact['url']])) {
343 $data['cc'][] = $contact['url'];
344 $contacts[$contact['url']] = $contact['url'];
349 // It is to decide whether we should include all profiles in a thread to the list of receivers
351 $parents = Item::select(['author-link', 'owner-link', 'gravity'], ['parent' => $item['parent']]);
352 while ($parent = Item::fetch($parents)) {
353 $profile = self::fetchprofile($parent['author-link'], false);
354 if (!empty($profile) && empty($contacts[$profile['url']])) {
355 $data['cc'][] = $profile['url'];
356 $contacts[$profile['url']] = $profile['url'];
359 if ($item['gravity'] != GRAVITY_PARENT) {
363 $profile = self::fetchprofile($parent['owner-link'], false);
364 if (!empty($profile) && empty($contacts[$profile['url']])) {
365 $data['cc'][] = $profile['url'];
366 $contacts[$profile['url']] = $profile['url'];
369 DBA::close($parents);
371 if (empty($data['to'])) {
372 $data['to'] = $data['cc'];
379 public static function fetchTargetInboxes($item, $uid)
381 $permissions = self::createPermissionBlockForItem($item);
382 if (empty($permissions)) {
388 if ($item['gravity'] == GRAVITY_ACTIVITY) {
389 $item_profile = ActivityPub::fetchprofile($item['author-link']);
391 $item_profile = ActivityPub::fetchprofile($item['owner-link']);
394 $elements = ['to', 'cc', 'bto', 'bcc'];
395 foreach ($elements as $element) {
396 if (empty($permissions[$element])) {
400 foreach ($permissions[$element] as $receiver) {
401 if ($receiver == $item_profile['followers']) {
402 $contacts = DBA::select('contact', ['notify', 'batch'], ['uid' => $uid,
403 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::ACTIVITYPUB]);
404 while ($contact = DBA::fetch($contacts)) {
405 $contact = defaults($contact, 'batch', $contact['notify']);
406 $inboxes[$contact] = $contact;
408 DBA::close($contacts);
410 $profile = self::fetchprofile($receiver);
411 if (!empty($profile)) {
412 $target = defaults($profile, 'sharedinbox', $profile['inbox']);
413 $inboxes[$target] = $target;
422 public static function getTypeOfItem($item)
424 if ($item['verb'] == ACTIVITY_POST) {
425 if ($item['created'] == $item['edited']) {
430 } elseif ($item['verb'] == ACTIVITY_LIKE) {
432 } elseif ($item['verb'] == ACTIVITY_DISLIKE) {
434 } elseif ($item['verb'] == ACTIVITY_ATTEND) {
436 } elseif ($item['verb'] == ACTIVITY_ATTENDNO) {
438 } elseif ($item['verb'] == ACTIVITY_ATTENDMAYBE) {
439 $type = 'TentativeAccept';
447 public static function createActivityFromItem($item_id, $object_mode = false)
449 $item = Item::selectFirst([], ['id' => $item_id]);
451 if (!DBA::isResult($item)) {
455 $condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
456 $conversation = DBA::selectFirst('conversation', ['source'], $condition);
457 if (DBA::isResult($conversation)) {
458 $data = json_decode($conversation['source']);
464 $type = self::getTypeOfItem($item);
467 $data = ['@context' => self::CONTEXT];
469 if ($item['deleted'] && ($item['gravity'] == GRAVITY_ACTIVITY)) {
471 } elseif ($item['deleted']) {
478 $data['id'] = $item['uri'] . '#' . $type;
479 $data['type'] = $type;
480 $data['actor'] = $item['author-link'];
482 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
484 if ($item["created"] != $item["edited"]) {
485 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
488 $data['context'] = self::createConversationURLFromItem($item);
490 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
492 if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) {
493 $data['object'] = self::CreateNote($item);
494 } elseif ($data['type'] == 'Undo') {
495 $data['object'] = self::createActivityFromItem($item_id, true);
497 $data['object'] = $item['thr-parent'];
500 $owner = User::getOwnerDataById($item['uid']);
503 return LDSignature::sign($data, $owner);
509 public static function createObjectFromItemID($item_id)
511 $item = Item::selectFirst([], ['id' => $item_id]);
513 if (!DBA::isResult($item)) {
517 $data = ['@context' => self::CONTEXT];
518 $data = array_merge($data, self::CreateNote($item));
523 private static function createTagList($item)
527 $terms = Term::tagArrayFromItemId($item['id']);
528 foreach ($terms as $term) {
529 if ($term['type'] == TERM_MENTION) {
530 $contact = Contact::getDetailsByURL($term['url']);
531 if (!empty($contact['addr'])) {
532 $mention = '@' . $contact['addr'];
534 $mention = '@' . $term['url'];
537 $tags[] = ['type' => 'Mention', 'href' => $term['url'], 'name' => $mention];
543 private static function createConversationURLFromItem($item)
545 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
546 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
547 $conversation_uri = $conversation['conversation-uri'];
549 $conversation_uri = $item['parent-uri'];
551 return $conversation_uri;
554 private static function CreateNote($item)
556 if (!empty($item['title'])) {
562 if ($item['deleted']) {
567 $data['id'] = $item['uri'];
568 $data['type'] = $type;
570 if ($item['deleted']) {
574 $data['summary'] = null; // Ignore by now
576 if ($item['uri'] != $item['thr-parent']) {
577 $data['inReplyTo'] = $item['thr-parent'];
579 $data['inReplyTo'] = null;
582 $data['uuid'] = $item['guid'];
583 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
585 if ($item["created"] != $item["edited"]) {
586 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
589 $data['url'] = $item['plink'];
590 $data['attributedTo'] = $item['author-link'];
591 $data['actor'] = $item['author-link'];
592 $data['sensitive'] = false; // - Query NSFW
593 $data['conversation'] = $data['context'] = self::createConversationURLFromItem($item);
595 if (!empty($item['title'])) {
596 $data['name'] = BBCode::convert($item['title'], false, 7);
599 $data['content'] = BBCode::convert($item['body'], false, 7);
600 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
601 $data['attachment'] = []; // @ToDo
602 $data['tag'] = self::createTagList($item);
603 $data = array_merge($data, ActivityPub::createPermissionBlockForItem($item));
605 //$data['emoji'] = []; // Ignore by now
609 public static function transmitActivity($activity, $target, $uid)
611 $profile = self::fetchprofile($target);
613 $owner = User::getOwnerDataById($uid);
615 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
616 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
618 'actor' => $owner['url'],
619 'object' => $profile['url'],
620 'to' => $profile['url']];
622 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
624 $signed = LDSignature::sign($data, $owner);
625 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
628 public static function transmitContactAccept($target, $id, $uid)
630 $profile = self::fetchprofile($target);
632 $owner = User::getOwnerDataById($uid);
633 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
634 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
636 'actor' => $owner['url'],
637 'object' => ['id' => $id, 'type' => 'Follow',
638 'actor' => $profile['url'],
639 'object' => $owner['url']],
640 'to' => $profile['url']];
642 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
644 $signed = LDSignature::sign($data, $owner);
645 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
648 public static function transmitContactReject($target, $id, $uid)
650 $profile = self::fetchprofile($target);
652 $owner = User::getOwnerDataById($uid);
653 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
654 'id' => System::baseUrl() . '/activity/' . System::createGUID(),
656 'actor' => $owner['url'],
657 'object' => ['id' => $id, 'type' => 'Follow',
658 'actor' => $profile['url'],
659 'object' => $owner['url']],
660 'to' => $profile['url']];
662 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
664 $signed = LDSignature::sign($data, $owner);
665 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
668 public static function transmitContactUndo($target, $uid)
670 $profile = self::fetchprofile($target);
672 $id = System::baseUrl() . '/activity/' . System::createGUID();
674 $owner = User::getOwnerDataById($uid);
675 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
678 'actor' => $owner['url'],
679 'object' => ['id' => $id, 'type' => 'Follow',
680 'actor' => $owner['url'],
681 'object' => $profile['url']],
682 'to' => $profile['url']];
684 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
686 $signed = LDSignature::sign($data, $owner);
687 return HTTPSignature::transmit($signed, $profile['inbox'], $uid);
691 * Fetches ActivityPub content from the given url
693 * @param string $url content url
696 public static function fetchContent($url)
698 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json, application/ld+json']);
699 if (!$ret['success'] || empty($ret['body'])) {
702 return json_decode($ret['body'], true);
706 * Resolves the profile url from the address by using webfinger
708 * @param string $addr profile address (user@domain.tld)
711 private static function addrToUrl($addr)
713 $addr_parts = explode('@', $addr);
714 if (count($addr_parts) != 2) {
718 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
720 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
721 if (!$ret['success'] || empty($ret['body'])) {
725 $data = json_decode($ret['body'], true);
727 if (empty($data['links'])) {
731 foreach ($data['links'] as $link) {
732 if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
736 if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
737 return $link['href'];
745 * Fetches a profile form a given url
747 * @param string $url profile url
748 * @param boolean $update true = always update, false = never update, null = update when not found
749 * @return array profile array
751 public static function fetchprofile($url, $update = null)
757 if (empty($update)) {
758 $apcontact = DBA::selectFirst('apcontact', [], ['url' => $url]);
759 if (DBA::isResult($apcontact)) {
763 $apcontact = DBA::selectFirst('apcontact', [], ['alias' => $url]);
764 if (DBA::isResult($apcontact)) {
768 $apcontact = DBA::selectFirst('apcontact', [], ['addr' => $url]);
769 if (DBA::isResult($apcontact)) {
773 if (!is_null($update)) {
778 if (empty(parse_url($url, PHP_URL_SCHEME))) {
779 $url = self::addrToUrl($url);
785 $data = self::fetchContent($url);
787 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
792 $apcontact['url'] = $data['id'];
793 $apcontact['uuid'] = defaults($data, 'uuid', null);
794 $apcontact['type'] = defaults($data, 'type', null);
795 $apcontact['following'] = defaults($data, 'following', null);
796 $apcontact['followers'] = defaults($data, 'followers', null);
797 $apcontact['inbox'] = defaults($data, 'inbox', null);
798 $apcontact['outbox'] = defaults($data, 'outbox', null);
799 $apcontact['sharedinbox'] = JsonLD::fetchElement($data, 'endpoints', 'sharedInbox');
800 $apcontact['nick'] = defaults($data, 'preferredUsername', null);
801 $apcontact['name'] = defaults($data, 'name', $apcontact['nick']);
802 $apcontact['about'] = defaults($data, 'summary', '');
803 $apcontact['photo'] = JsonLD::fetchElement($data, 'icon', 'url');
804 $apcontact['alias'] = JsonLD::fetchElement($data, 'url', 'href');
806 $parts = parse_url($apcontact['url']);
807 unset($parts['scheme']);
808 unset($parts['path']);
809 $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
811 $apcontact['pubkey'] = trim(JsonLD::fetchElement($data, 'publicKey', 'publicKeyPem'));
814 // manuallyApprovesFollowers
817 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
819 // Unhandled from Misskey
820 // sharedInbox, isCat
822 // Unhandled from Kroeg
823 // kroeg:blocks, updated
825 // Check if the address is resolvable
826 if (self::addrToUrl($apcontact['addr']) == $apcontact['url']) {
827 $parts = parse_url($apcontact['url']);
828 unset($parts['path']);
829 $apcontact['baseurl'] = Network::unparseURL($parts);
831 $apcontact['addr'] = null;
834 if ($apcontact['url'] == $apcontact['alias']) {
835 $apcontact['alias'] = null;
838 $apcontact['updated'] = DateTimeFormat::utcNow();
840 DBA::update('apcontact', $apcontact, ['url' => $url], true);
846 * Fetches a profile from the given url into an array that is compatible to Probe::uri
848 * @param string $url profile url
851 public static function probeProfile($url)
853 $apcontact = self::fetchprofile($url, true);
854 if (empty($apcontact)) {
858 $profile = ['network' => Protocol::ACTIVITYPUB];
859 $profile['nick'] = $apcontact['nick'];
860 $profile['name'] = $apcontact['name'];
861 $profile['guid'] = $apcontact['uuid'];
862 $profile['url'] = $apcontact['url'];
863 $profile['addr'] = $apcontact['addr'];
864 $profile['alias'] = $apcontact['alias'];
865 $profile['photo'] = $apcontact['photo'];
866 // $profile['community']
867 // $profile['keywords']
868 // $profile['location']
869 $profile['about'] = $apcontact['about'];
870 $profile['batch'] = $apcontact['sharedinbox'];
871 $profile['notify'] = $apcontact['inbox'];
872 $profile['poll'] = $apcontact['outbox'];
873 $profile['pubkey'] = $apcontact['pubkey'];
874 $profile['baseurl'] = $apcontact['baseurl'];
876 // Remove all "null" fields
877 foreach ($profile as $field => $content) {
878 if (is_null($content)) {
879 unset($profile[$field]);
886 public static function processInbox($body, $header, $uid)
888 $http_signer = HTTPSignature::getSigner($body, $header);
889 if (empty($http_signer)) {
890 logger('Invalid HTTP signature, message will be discarded.', LOGGER_DEBUG);
893 logger('HTTP signature is signed by ' . $http_signer, LOGGER_DEBUG);
896 $activity = json_decode($body, true);
898 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
899 logger('Message for user ' . $uid . ' is from actor ' . $actor, LOGGER_DEBUG);
901 if (empty($activity)) {
902 logger('Invalid body.', LOGGER_DEBUG);
906 if (LDSignature::isSigned($activity)) {
907 $ld_signer = LDSignature::getSigner($activity);
908 if (!empty($ld_signer && ($actor == $http_signer))) {
909 logger('The HTTP and the JSON-LD signature belong to ' . $ld_signer, LOGGER_DEBUG);
910 $trust_source = true;
911 } elseif (!empty($ld_signer)) {
912 logger('JSON-LD signature is signed by ' . $ld_signer, LOGGER_DEBUG);
913 $trust_source = true;
914 } elseif ($actor == $http_signer) {
915 logger('Bad JSON-LD signature, but HTTP signer fits the actor.', LOGGER_DEBUG);
916 $trust_source = true;
918 logger('Invalid JSON-LD signature and the HTTP signer is different.', LOGGER_DEBUG);
919 $trust_source = false;
921 } elseif ($actor == $http_signer) {
922 logger('Trusting post without JSON-LD signature, The actor fits the HTTP signer.', LOGGER_DEBUG);
923 $trust_source = true;
925 logger('No JSON-LD signature, different actor.', LOGGER_DEBUG);
926 $trust_source = false;
929 self::processActivity($activity, $body, $uid, $trust_source);
932 public static function fetchOutbox($url, $uid)
934 $data = self::fetchContent($url);
939 if (!empty($data['orderedItems'])) {
940 $items = $data['orderedItems'];
941 } elseif (!empty($data['first']['orderedItems'])) {
942 $items = $data['first']['orderedItems'];
943 } elseif (!empty($data['first'])) {
944 self::fetchOutbox($data['first'], $uid);
950 foreach ($items as $activity) {
951 self::processActivity($activity, '', $uid, true);
955 private static function prepareObjectData($activity, $uid, $trust_source)
957 $actor = JsonLD::fetchElement($activity, 'actor', 'id');
959 logger('Empty actor', LOGGER_DEBUG);
963 // Fetch all receivers from to, cc, bto and bcc
964 $receivers = self::getReceivers($activity, $actor);
966 // When it is a delivery to a personal inbox we add that user to the receivers
968 $owner = User::getOwnerDataById($uid);
969 $additional = ['uid:' . $uid => $uid];
970 $receivers = array_merge($receivers, $additional);
973 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
975 if (is_string($activity['object'])) {
976 $object_url = $activity['object'];
977 } elseif (!empty($activity['object']['id'])) {
978 $object_url = $activity['object']['id'];
980 logger('No object found', LOGGER_DEBUG);
984 // Fetch the content only on activities where this matters
985 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
986 $object_data = self::fetchObject($object_url, $activity['object'], $trust_source);
987 if (empty($object_data)) {
988 logger("Object data couldn't be processed", LOGGER_DEBUG);
991 } elseif ($activity['type'] == 'Accept') {
993 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
994 $object_data['object'] = JsonLD::fetchElement($activity, 'object', 'actor');
995 } elseif ($activity['type'] == 'Undo') {
997 $object_data['object_type'] = JsonLD::fetchElement($activity, 'object', 'type');
998 $object_data['object'] = JsonLD::fetchElement($activity, 'object', 'object');
999 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
1000 // Create a mostly empty array out of the activity data (instead of the object).
1001 // This way we later don't have to check for the existence of ech individual array element.
1002 $object_data = self::processCommonData($activity);
1003 $object_data['name'] = $activity['type'];
1004 $object_data['author'] = $activity['actor'];
1005 $object_data['object'] = $object_url;
1006 } elseif ($activity['type'] == 'Follow') {
1007 $object_data['id'] = $activity['id'];
1008 $object_data['object'] = $object_url;
1013 $object_data = self::addActivityFields($object_data, $activity);
1015 $object_data['type'] = $activity['type'];
1016 $object_data['owner'] = $actor;
1017 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
1019 return $object_data;
1022 private static function processActivity($activity, $body = '', $uid = null, $trust_source = false)
1024 if (empty($activity['type'])) {
1025 logger('Empty type', LOGGER_DEBUG);
1029 if (empty($activity['object'])) {
1030 logger('Empty object', LOGGER_DEBUG);
1034 if (empty($activity['actor'])) {
1035 logger('Empty actor', LOGGER_DEBUG);
1041 // title, atomUri, context_id, statusnetConversationId
1044 // context, location, signature;
1046 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
1048 $object_data = self::prepareObjectData($activity, $uid, $trust_source);
1049 if (empty($object_data)) {
1050 logger('No object data found', LOGGER_DEBUG);
1054 switch ($activity['type']) {
1057 self::createItem($object_data, $body);
1061 self::likeItem($object_data, $body);
1065 self::dislikeItem($object_data, $body);
1075 self::followUser($object_data);
1079 if ($object_data['object_type'] == 'Follow') {
1080 self::acceptFollowUser($object_data);
1085 if ($object_data['object_type'] == 'Follow') {
1086 self::undoFollowUser($object_data);
1091 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
1096 private static function getReceivers($activity, $actor)
1100 // When it is an answer, we inherite the receivers from the parent
1101 $replyto = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1102 if (!empty($replyto)) {
1103 $parents = Item::select(['uid'], ['uri' => $replyto]);
1104 while ($parent = Item::fetch($parents)) {
1105 $receivers['uid:' . $parent['uid']] = $parent['uid'];
1109 if (!empty($actor)) {
1110 $profile = self::fetchprofile($actor);
1111 $followers = defaults($profile, 'followers', '');
1113 logger('Actor: ' . $actor . ' - Followers: ' . $followers, LOGGER_DEBUG);
1115 logger('Empty actor', LOGGER_DEBUG);
1119 $elements = ['to', 'cc', 'bto', 'bcc'];
1120 foreach ($elements as $element) {
1121 if (empty($activity[$element])) {
1125 // The receiver can be an arror or a string
1126 if (is_string($activity[$element])) {
1127 $activity[$element] = [$activity[$element]];
1130 foreach ($activity[$element] as $receiver) {
1131 if ($receiver == self::PUBLIC) {
1132 $receivers['uid:0'] = 0;
1135 if (($receiver == self::PUBLIC) && !empty($actor)) {
1136 // This will most likely catch all OStatus connections to Mastodon
1137 $condition = ['alias' => [$actor, normalise_link($actor)], 'rel' => [Contact::SHARING, Contact::FRIEND]];
1138 $contacts = DBA::select('contact', ['uid'], $condition);
1139 while ($contact = DBA::fetch($contacts)) {
1140 if ($contact['uid'] != 0) {
1141 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1144 DBA::close($contacts);
1147 if (in_array($receiver, [$followers, self::PUBLIC]) && !empty($actor)) {
1148 $condition = ['nurl' => normalise_link($actor), 'rel' => [Contact::SHARING, Contact::FRIEND],
1149 'network' => Protocol::ACTIVITYPUB];
1150 $contacts = DBA::select('contact', ['uid'], $condition);
1151 while ($contact = DBA::fetch($contacts)) {
1152 if ($contact['uid'] != 0) {
1153 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1156 DBA::close($contacts);
1160 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
1161 $contact = DBA::selectFirst('contact', ['uid'], $condition);
1162 if (!DBA::isResult($contact)) {
1165 $receivers['uid:' . $contact['uid']] = $contact['uid'];
1171 private static function addActivityFields($object_data, $activity)
1173 if (!empty($activity['published']) && empty($object_data['published'])) {
1174 $object_data['published'] = $activity['published'];
1177 if (!empty($activity['updated']) && empty($object_data['updated'])) {
1178 $object_data['updated'] = $activity['updated'];
1181 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
1182 $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id');
1185 if (!empty($activity['instrument'])) {
1186 $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service');
1188 return $object_data;
1191 private static function fetchObject($object_url, $object = [], $trust_source = false)
1193 if (!$trust_source || is_string($object)) {
1194 $data = self::fetchContent($object_url);
1196 logger('Empty content for ' . $object_url . ', check if content is available locally.', LOGGER_DEBUG);
1197 $data = $object_url;
1199 logger('Fetched content for ' . $object_url, LOGGER_DEBUG);
1202 logger('Using original object for url ' . $object_url, LOGGER_DEBUG);
1206 if (is_string($data)) {
1207 $item = Item::selectFirst([], ['uri' => $data]);
1208 if (!DBA::isResult($item)) {
1209 logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
1212 logger('Using already stored item for url ' . $object_url, LOGGER_DEBUG);
1213 $data = self::CreateNote($item);
1216 if (empty($data['type'])) {
1217 logger('Empty type', LOGGER_DEBUG);
1220 $type = $data['type'];
1221 logger('Type ' . $type, LOGGER_DEBUG);
1224 if (in_array($type, ['Note', 'Article', 'Video'])) {
1225 $common = self::processCommonData($data);
1230 return array_merge($common, self::processNote($data));
1232 return array_merge($common, self::processArticle($data));
1234 return array_merge($common, self::processVideo($data));
1237 if (empty($data['object'])) {
1240 return self::fetchObject($data['object']);
1247 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
1252 private static function processCommonData(&$object)
1254 if (empty($object['id'])) {
1259 $object_data['type'] = $object['type'];
1260 $object_data['uri'] = $object['id'];
1262 if (!empty($object['inReplyTo'])) {
1263 $object_data['reply-to-uri'] = JsonLD::fetchElement($object, 'inReplyTo', 'id');
1265 $object_data['reply-to-uri'] = $object_data['uri'];
1268 $object_data['published'] = defaults($object, 'published', null);
1269 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
1271 if (empty($object_data['published']) && !empty($object_data['updated'])) {
1272 $object_data['published'] = $object_data['updated'];
1275 $object_data['uuid'] = defaults($object, 'uuid', null);
1276 $object_data['owner'] = $object_data['author'] = JsonLD::fetchElement($object, 'attributedTo', 'id');
1277 $object_data['context'] = defaults($object, 'context', null);
1278 $object_data['conversation'] = defaults($object, 'conversation', null);
1279 $object_data['sensitive'] = defaults($object, 'sensitive', null);
1280 $object_data['name'] = defaults($object, 'title', null);
1281 $object_data['name'] = defaults($object, 'name', $object_data['name']);
1282 $object_data['summary'] = defaults($object, 'summary', null);
1283 $object_data['content'] = defaults($object, 'content', null);
1284 $object_data['source'] = defaults($object, 'source', null);
1285 $object_data['location'] = JsonLD::fetchElement($object, 'location', 'name', 'type', 'Place');
1286 $object_data['attachments'] = defaults($object, 'attachment', null);
1287 $object_data['tags'] = defaults($object, 'tag', null);
1288 $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service');
1289 $object_data['alternate-url'] = JsonLD::fetchElement($object, 'url', 'href');
1290 $object_data['receiver'] = self::getReceivers($object, $object_data['owner']);
1293 // @context, type, actor, signature, mediaType, duration, replies, icon
1295 // Also missing: (Defined in the standard, but currently unused)
1296 // audience, preview, endTime, startTime, generator, image
1298 return $object_data;
1301 private static function processNote($object)
1306 // emoji, atomUri, inReplyToAtomUri
1309 // contentMap, announcement_count, announcements, context_id, likes, like_count
1310 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
1312 return $object_data;
1315 private static function processArticle($object)
1319 return $object_data;
1322 private static function processVideo($object)
1327 // category, licence, language, commentsEnabled
1330 // views, waitTranscoding, state, support, subtitleLanguage
1331 // likes, dislikes, shares, comments
1333 return $object_data;
1336 private static function convertMentions($body)
1338 $URLSearchString = "^\[\]";
1339 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
1344 private static function constructTagList($tags, $sensitive)
1351 foreach ($tags as $tag) {
1352 if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
1353 if (!empty($tag_text)) {
1357 if (empty($tag['href'])) {
1362 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
1366 /// @todo add nsfw for $sensitive
1371 private static function constructAttachList($attachments, $item)
1373 if (empty($attachments)) {
1377 foreach ($attachments as $attach) {
1378 $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
1379 if ($filetype == 'image') {
1380 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
1382 if (!empty($item["attach"])) {
1383 $item["attach"] .= ',';
1385 $item["attach"] = '';
1387 if (!isset($attach['length'])) {
1388 $attach['length'] = "0";
1390 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1397 private static function createItem($activity, $body)
1400 $item['verb'] = ACTIVITY_POST;
1401 $item['parent-uri'] = $activity['reply-to-uri'];
1403 if ($activity['reply-to-uri'] == $activity['uri']) {
1404 $item['gravity'] = GRAVITY_PARENT;
1405 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1407 $item['gravity'] = GRAVITY_COMMENT;
1408 $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1411 if (($activity['uri'] != $activity['reply-to-uri']) && !Item::exists(['uri' => $activity['reply-to-uri']])) {
1412 logger('Parent ' . $activity['reply-to-uri'] . ' not found. Try to refetch it.');
1413 self::fetchMissingActivity($activity['reply-to-uri'], $activity);
1416 self::postItem($activity, $item, $body);
1419 private static function likeItem($activity, $body)
1422 $item['verb'] = ACTIVITY_LIKE;
1423 $item['parent-uri'] = $activity['object'];
1424 $item['gravity'] = GRAVITY_ACTIVITY;
1425 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1427 self::postItem($activity, $item, $body);
1430 private static function dislikeItem($activity, $body)
1433 $item['verb'] = ACTIVITY_DISLIKE;
1434 $item['parent-uri'] = $activity['object'];
1435 $item['gravity'] = GRAVITY_ACTIVITY;
1436 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1438 self::postItem($activity, $item, $body);
1441 private static function postItem($activity, $item, $body)
1443 /// @todo What to do with $activity['context']?
1445 $item['network'] = Protocol::ACTIVITYPUB;
1446 $item['private'] = !in_array(0, $activity['receiver']);
1447 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1448 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1449 $item['uri'] = $activity['uri'];
1450 $item['created'] = $activity['published'];
1451 $item['edited'] = $activity['updated'];
1452 $item['guid'] = $activity['uuid'];
1453 $item['title'] = HTML::toBBCode($activity['name']);
1454 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1455 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1456 $item['location'] = $activity['location'];
1457 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1458 $item['app'] = $activity['service'];
1459 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1461 $item = self::constructAttachList($activity['attachments'], $item);
1463 $source = JsonLD::fetchElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1464 if (!empty($source)) {
1465 $item['body'] = $source;
1468 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1469 $item['source'] = $body;
1470 $item['conversation-uri'] = $activity['conversation'];
1472 foreach ($activity['receiver'] as $receiver) {
1473 $item['uid'] = $receiver;
1474 $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1476 if (($receiver != 0) && empty($item['contact-id'])) {
1477 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1480 $item_id = Item::insert($item);
1481 logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1485 private static function fetchMissingActivity($url, $child)
1487 if (Config::get('system', 'ostatus_full_threads')) {
1491 $object = ActivityPub::fetchContent($url);
1492 if (empty($object)) {
1493 logger('Activity ' . $url . ' was not fetchable, aborting.');
1498 $activity['@context'] = $object['@context'];
1499 unset($object['@context']);
1500 $activity['id'] = $object['id'];
1501 $activity['to'] = defaults($object, 'to', []);
1502 $activity['cc'] = defaults($object, 'cc', []);
1503 $activity['actor'] = $child['author'];
1504 $activity['object'] = $object;
1505 $activity['published'] = $object['published'];
1506 $activity['type'] = 'Create';
1508 self::processActivity($activity);
1509 logger('Activity ' . $url . ' had been fetched and processed.');
1512 private static function getUserOfObject($object)
1514 $self = DBA::selectFirst('contact', ['uid'], ['nurl' => normalise_link($object), 'self' => true]);
1515 if (!DBA::isResult($self)) {
1518 return $self['uid'];
1522 private static function followUser($activity)
1524 $uid = self::getUserOfObject($activity['object']);
1529 $owner = User::getOwnerDataById($uid);
1531 $cid = Contact::getIdForURL($activity['owner'], $uid);
1533 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1538 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1539 'author-link' => $activity['owner']];
1541 Contact::addRelationship($owner, $contact, $item);
1542 $cid = Contact::getIdForURL($activity['owner'], $uid);
1547 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1548 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1549 Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1552 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1553 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1556 private static function acceptFollowUser($activity)
1558 $uid = self::getUserOfObject($activity['object']);
1563 $owner = User::getOwnerDataById($uid);
1565 $cid = Contact::getIdForURL($activity['owner'], $uid);
1567 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1571 $fields = ['pending' => false];
1573 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1574 if ($contact['rel'] == Contact::FOLLOWER) {
1575 $fields['rel'] = Contact::FRIEND;
1578 $condition = ['id' => $cid];
1579 DBA::update('contact', $fields, $condition);
1580 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1583 private static function undoFollowUser($activity)
1585 $uid = self::getUserOfObject($activity['object']);
1590 $owner = User::getOwnerDataById($uid);
1592 $cid = Contact::getIdForURL($activity['owner'], $uid);
1594 logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1598 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1599 if (!DBA::isResult($contact)) {
1603 Contact::removeFollower($owner, $contact);
1604 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);