]> git.mxchange.org Git - friendica.git/blob - src/Protocol/ActivityPub.php
3047853b1108cfc6436aa443b57e0d4b92d6a829
[friendica.git] / src / Protocol / ActivityPub.php
1 <?php
2 /**
3  * @file src/Protocol/ActivityPub.php
4  */
5 namespace Friendica\Protocol;
6
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\User;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Util\Crypto;
19 use Friendica\Content\Text\BBCode;
20 use Friendica\Content\Text\HTML;
21 use Friendica\Network\Probe;
22
23 /**
24  * @brief ActivityPub Protocol class
25  * The ActivityPub Protocol is a message exchange protocol defined by the W3C.
26  * https://www.w3.org/TR/activitypub/
27  * https://www.w3.org/TR/activitystreams-core/
28  * https://www.w3.org/TR/activitystreams-vocabulary/
29  *
30  * https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/
31  * https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
32  *
33  * Digest: https://tools.ietf.org/html/rfc5843
34  * https://tools.ietf.org/html/draft-cavage-http-signatures-10#ref-15
35  *
36  * Part of the code for HTTP signing is taken from the Osada project.
37  * https://framagit.org/macgirvin/osada
38  *
39  * To-do:
40  *
41  * Receiver:
42  * - Activities: Dislike, Update, Delete
43  * - Object Types: Person, Tombstome
44  *
45  * Transmitter:
46  * - Activities: Like, Dislike, Update, Delete
47  * - Object Tyoes: Article, Announce, Person, Tombstone
48  *
49  * General:
50  * - Message distribution
51  * - Endpoints: Outbox, Object, Follower, Following
52  * - General cleanup
53  */
54 class ActivityPub
55 {
56         const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
57
58         public static function transmit($data, $target, $uid)
59         {
60                 $owner = User::getOwnerDataById($uid);
61
62                 if (!$owner) {
63                         return;
64                 }
65
66                 $content = json_encode($data);
67
68                 $host = parse_url($target, PHP_URL_HOST);
69                 $path = parse_url($target, PHP_URL_PATH);
70                 $date = date('r');
71
72                 $headers = ['Host: ' . $host, 'Date: ' . $date];
73
74                 $signed_data = "(request-target): post " . $path . "\nhost: " . $host . "\ndate: " . $date;
75
76                 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
77
78                 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",headers="(request-target) host date",signature="' . $signature . '"';
79                 $headers[] = 'Content-Type: application/activity+json';
80
81                 Network::post($target, $content, $headers);
82                 $return_code = BaseObject::getApp()->get_curl_code();
83
84                 logger('Transmit to ' . $target . ' returned ' . $return_code);
85         }
86
87         /**
88          * Return the ActivityPub profile of the given user
89          *
90          * @param integer $uid User ID
91          * @return array
92          */
93         public static function profile($uid)
94         {
95                 $accounttype = ['Person', 'Organization', 'Service', 'Group', 'Application'];
96
97                 $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
98                         'account_removed' => false, 'verified' => true];
99                 $fields = ['guid', 'nickname', 'pubkey', 'account-type'];
100                 $user = DBA::selectFirst('user', $fields, $condition);
101                 if (!DBA::isResult($user)) {
102                         return [];
103                 }
104
105                 $fields = ['locality', 'region', 'country-name'];
106                 $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
107                 if (!DBA::isResult($profile)) {
108                         return [];
109                 }
110
111                 $fields = ['name', 'url', 'location', 'about', 'avatar'];
112                 $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
113                 if (!DBA::isResult($contact)) {
114                         return [];
115                 }
116
117                 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
118                         ['uuid' => 'http://schema.org/identifier', 'sensitive' => 'as:sensitive',
119                         'vcard' => 'http://www.w3.org/2006/vcard/ns#']]];
120
121                 $data['id'] = $contact['url'];
122                 $data['uuid'] = $user['guid'];
123                 $data['type'] = $accounttype[$user['account-type']];
124                 $data['following'] = System::baseUrl() . '/following/' . $user['nickname'];
125                 $data['followers'] = System::baseUrl() . '/followers/' . $user['nickname'];
126                 $data['inbox'] = System::baseUrl() . '/inbox/' . $user['nickname'];
127                 $data['outbox'] = System::baseUrl() . '/outbox/' . $user['nickname'];
128                 $data['preferredUsername'] = $user['nickname'];
129                 $data['name'] = $contact['name'];
130                 $data['vcard:hasAddress'] = ['@type' => 'Home', 'vcard:country-name' => $profile['country-name'],
131                         'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
132                 $data['summary'] = $contact['about'];
133                 $data['url'] = $contact['url'];
134                 $data['manuallyApprovesFollowers'] = false; /// @todo
135                 $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
136                         'owner' => $contact['url'],
137                         'publicKeyPem' => $user['pubkey']];
138                 $data['endpoints'] = ['sharedInbox' => System::baseUrl() . '/inbox'];
139                 $data['icon'] = ['type' => 'Image',
140                         'url' => $contact['avatar']];
141
142                 // tags: https://kitty.town/@inmysocks/100656097926961126.json
143                 return $data;
144         }
145
146         public static function createActivityFromItem($item_id)
147         {
148                 $item = Item::selectFirst([], ['id' => $item_id]);
149
150                 $data = ['@context' => ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1',
151                         ['Emoji' => 'toot:Emoji', 'Hashtag' => 'as:Hashtag', 'atomUri' => 'ostatus:atomUri',
152                         'conversation' => 'ostatus:conversation', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri',
153                         'ostatus' => 'http://ostatus.org#', 'sensitive' => 'as:sensitive',
154                         'toot' => 'http://joinmastodon.org/ns#']]];
155
156                 $data['type'] = 'Create';
157                 $data['id'] = $item['uri'];
158                 $data['actor'] = $item['author-link'];
159                 $data['to'] = 'https://www.w3.org/ns/activitystreams#Public';
160                 $data['object'] = self::createNote($item);
161                 return $data;
162         }
163
164         public static function createNote($item)
165         {
166                 $data = [];
167                 $data['type'] = 'Note';
168                 $data['id'] = $item['uri'];
169
170                 if ($item['uri'] != $item['thr-parent']) {
171                         $data['inReplyTo'] = $item['thr-parent'];
172                 }
173
174                 $conversation = DBA::selectFirst('conversation', ['conversation-uri'], ['item-uri' => $item['parent-uri']]);
175                 if (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
176                         $conversation_uri = $conversation['conversation-uri'];
177                 } else {
178                         $conversation_uri = $item['parent-uri'];
179                 }
180
181                 $data['context'] = $data['conversation'] = $conversation_uri;
182                 $data['actor'] = $item['author-link'];
183                 $data['to'] = [];
184                 if (!$item['private']) {
185                         $data['to'][] = 'https://www.w3.org/ns/activitystreams#Public';
186                 }
187                 $data['published'] = DateTimeFormat::utc($item["created"]."+00:00", DateTimeFormat::ATOM);
188                 $data['updated'] = DateTimeFormat::utc($item["edited"]."+00:00", DateTimeFormat::ATOM);
189                 $data['attributedTo'] = $item['author-link'];
190                 $data['name'] = BBCode::convert($item['title'], false, 7);
191                 $data['content'] = BBCode::convert($item['body'], false, 7);
192                 $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];
193                 //$data['summary'] = ''; // Ignore by now
194                 //$data['sensitive'] = false; // - Query NSFW
195                 //$data['emoji'] = []; // Ignore by now
196                 //$data['tag'] = []; /// @ToDo
197                 //$data['attachment'] = []; // @ToDo
198                 return $data;
199         }
200
201         public static function transmitActivity($activity, $target, $uid)
202         {
203                 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
204
205                 $owner = User::getOwnerDataById($uid);
206
207                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
208                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
209                         'type' => $activity,
210                         'actor' => $owner['url'],
211                         'object' => $profile['url']];
212
213                 logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG);
214                 return self::transmit($data,  $profile['notify'], $uid);
215         }
216
217         public static function transmitContactAccept($target, $id, $uid)
218         {
219                 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
220
221                 $owner = User::getOwnerDataById($uid);
222                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
223                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
224                         'type' => 'Accept',
225                         'actor' => $owner['url'],
226                         'object' => ['id' => $id, 'type' => 'Follow',
227                                 'actor' => $profile['url'],
228                                 'object' => $owner['url']]];
229
230                 logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
231                 return self::transmit($data,  $profile['notify'], $uid);
232         }
233
234         public static function transmitContactReject($target, $id, $uid)
235         {
236                 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
237
238                 $owner = User::getOwnerDataById($uid);
239                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
240                         'id' => System::baseUrl() . '/activity/' . System::createGUID(),
241                         'type' => 'Reject',
242                         'actor' => $owner['url'],
243                         'object' => ['id' => $id, 'type' => 'Follow',
244                                 'actor' => $profile['url'],
245                                 'object' => $owner['url']]];
246
247                 logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
248                 return self::transmit($data,  $profile['notify'], $uid);
249         }
250
251         public static function transmitContactUndo($target, $uid)
252         {
253                 $profile = Probe::uri($target, Protocol::ACTIVITYPUB);
254
255                 $id = System::baseUrl() . '/activity/' . System::createGUID();
256
257                 $owner = User::getOwnerDataById($uid);
258                 $data = ['@context' => 'https://www.w3.org/ns/activitystreams',
259                         'id' => $id,
260                         'type' => 'Undo',
261                         'actor' => $owner['url'],
262                         'object' => ['id' => $id, 'type' => 'Follow',
263                                 'actor' => $owner['url'],
264                                 'object' => $profile['url']]];
265
266                 logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG);
267                 return self::transmit($data,  $profile['notify'], $uid);
268         }
269
270         /**
271          * Fetches ActivityPub content from the given url
272          *
273          * @param string $url content url
274          * @return array
275          */
276         public static function fetchContent($url)
277         {
278                 $ret = Network::curl($url, false, $redirects, ['accept_content' => 'application/activity+json']);
279
280                 if (!$ret['success'] || empty($ret['body'])) {
281                         return;
282                 }
283
284                 return json_decode($ret['body'], true);
285         }
286
287         /**
288          * Resolves the profile url from the address by using webfinger
289          *
290          * @param string $addr profile address (user@domain.tld)
291          * @return string url
292          */
293         private static function addrToUrl($addr)
294         {
295                 $addr_parts = explode('@', $addr);
296                 if (count($addr_parts) != 2) {
297                         return false;
298                 }
299
300                 $webfinger = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
301
302                 $ret = Network::curl($webfinger, false, $redirects, ['accept_content' => 'application/jrd+json,application/json']);
303                 if (!$ret['success'] || empty($ret['body'])) {
304                         return false;
305                 }
306
307                 $data = json_decode($ret['body'], true);
308
309                 if (empty($data['links'])) {
310                         return false;
311                 }
312
313                 foreach ($data['links'] as $link) {
314                         if (empty($link['href']) || empty($link['rel']) || empty($link['type'])) {
315                                 continue;
316                         }
317
318                         if (($link['rel'] == 'self') && ($link['type'] == 'application/activity+json')) {
319                                 return $link['href'];
320                         }
321                 }
322
323                 return false;
324         }
325
326         public static function verifySignature($content, $http_headers)
327         {
328                 $object = json_decode($content, true);
329
330                 if (empty($object)) {
331                         return false;
332                 }
333
334                 $actor = self::processElement($object, 'actor', 'id');
335
336                 $headers = [];
337                 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
338
339                 // First take every header
340                 foreach ($http_headers as $k => $v) {
341                         $field = str_replace('_', '-', strtolower($k));
342                         $headers[$field] = $v;
343                 }
344
345                 // Now add every http header
346                 foreach ($http_headers as $k => $v) {
347                         if (strpos($k, 'HTTP_') === 0) {
348                                 $field = str_replace('_', '-', strtolower(substr($k, 5)));
349                                 $headers[$field] = $v;
350                         }
351                 }
352
353                 $sig_block = ActivityPub::parseSigHeader($http_headers['HTTP_SIGNATURE']);
354
355                 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
356                         return false;
357                 }
358
359                 $signed_data = '';
360                 foreach ($sig_block['headers'] as $h) {
361                         if (array_key_exists($h, $headers)) {
362                                 $signed_data .= $h . ': ' . $headers[$h] . "\n";
363                         }
364                 }
365                 $signed_data = rtrim($signed_data, "\n");
366
367                 if (empty($signed_data)) {
368                         return false;
369                 }
370
371                 $algorithm = null;
372
373                 if ($sig_block['algorithm'] === 'rsa-sha256') {
374                         $algorithm = 'sha256';
375                 }
376
377                 if ($sig_block['algorithm'] === 'rsa-sha512') {
378                         $algorithm = 'sha512';
379                 }
380
381                 if (empty($algorithm)) {
382                         return false;
383                 }
384
385                 $key = self::fetchKey($sig_block['keyId'], $actor);
386
387                 if (empty($key)) {
388                         return false;
389                 }
390
391                 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) {
392                         return false;
393                 }
394
395                 // Check the digest if it was part of the signed data
396                 if (in_array('digest', $sig_block['headers'])) {
397                         $digest = explode('=', $headers['digest'], 2);
398                         if ($digest[0] === 'SHA-256') {
399                                 $hashalg = 'sha256';
400                         }
401                         if ($digest[0] === 'SHA-512') {
402                                 $hashalg = 'sha512';
403                         }
404
405                         /// @todo add all hashes from the rfc
406
407                         if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
408                                 return false;
409                         }
410                 }
411
412                 // Check the content-length if it was part of the signed data
413                 if (in_array('content-length', $sig_block['headers'])) {
414                         if (strlen($content) != $headers['content-length']) {
415                                 return false;
416                         }
417                 }
418
419                 return true;
420
421         }
422
423         private static function fetchKey($id, $actor)
424         {
425                 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
426
427                 $profile = Probe::uri($url, Protocol::ACTIVITYPUB);
428                 if (!empty($profile)) {
429                         return $profile['pubkey'];
430                 } elseif ($url != $actor) {
431                         $profile = Probe::uri($actor, Protocol::ACTIVITYPUB);
432                         if (!empty($profile)) {
433                                 return $profile['pubkey'];
434                         }
435                 }
436
437                 return false;
438         }
439
440         /**
441          * @brief
442          *
443          * @param string $header
444          * @return array associate array with
445          *   - \e string \b keyID
446          *   - \e string \b algorithm
447          *   - \e array  \b headers
448          *   - \e string \b signature
449          */
450         private static function parseSigHeader($header)
451         {
452                 $ret = [];
453                 $matches = [];
454
455                 if (preg_match('/keyId="(.*?)"/ism',$header,$matches)) {
456                         $ret['keyId'] = $matches[1];
457                 }
458
459                 if (preg_match('/algorithm="(.*?)"/ism',$header,$matches)) {
460                         $ret['algorithm'] = $matches[1];
461                 }
462
463                 if (preg_match('/headers="(.*?)"/ism',$header,$matches)) {
464                         $ret['headers'] = explode(' ', $matches[1]);
465                 }
466
467                 if (preg_match('/signature="(.*?)"/ism',$header,$matches)) {
468                         $ret['signature'] = base64_decode(preg_replace('/\s+/','',$matches[1]));
469                 }
470
471                 return $ret;
472         }
473
474         /**
475          * Fetches a profile from the given url
476          *
477          * @param string $url profile url
478          * @return array
479          */
480         public static function fetchProfile($url)
481         {
482                 if (empty(parse_url($url, PHP_URL_SCHEME))) {
483                         $url = self::addrToUrl($url);
484                         if (empty($url)) {
485                                 return false;
486                         }
487                 }
488
489                 $data = self::fetchContent($url);
490
491                 if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
492                         return false;
493                 }
494
495                 $profile = ['network' => Protocol::ACTIVITYPUB];
496                 $profile['nick'] = $data['preferredUsername'];
497                 $profile['name'] = defaults($data, 'name', $profile['nick']);
498                 $profile['guid'] = defaults($data, 'uuid', null);
499                 $profile['url'] = $data['id'];
500
501                 $parts = parse_url($profile['url']);
502                 unset($parts['scheme']);
503                 unset($parts['path']);
504                 $profile['addr'] = $profile['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts));
505                 $profile['alias'] = self::processElement($data, 'url', 'href');
506                 $profile['photo'] = self::processElement($data, 'icon', 'url');
507                 // $profile['community']
508                 // $profile['keywords']
509                 // $profile['location']
510                 $profile['about'] = defaults($data, 'summary', '');
511                 $profile['batch'] = self::processElement($data, 'endpoints', 'sharedInbox');
512                 $profile['notify'] = $data['inbox'];
513                 $profile['poll'] = $data['outbox'];
514                 $profile['pubkey'] = self::processElement($data, 'publicKey', 'publicKeyPem');
515
516                 // Check if the address is resolvable
517                 if (self::addrToUrl($profile['addr']) == $profile['url']) {
518                         $parts = parse_url($profile['url']);
519                         unset($parts['path']);
520                         $profile['baseurl'] = Network::unparseURL($parts);
521                 } else {
522                         unset($profile['addr']);
523                 }
524
525                 if ($profile['url'] == $profile['alias']) {
526                         unset($profile['alias']);
527                 }
528
529                 // Remove all "null" fields
530                 foreach ($profile as $field => $content) {
531                         if (is_null($content)) {
532                                 unset($profile[$field]);
533                         }
534                 }
535
536                 // To-Do
537                 // type, manuallyApprovesFollowers
538
539                 // Unhandled
540                 // @context, tag, attachment, image, nomadicLocations, signature, following, followers, featured, movedTo, liked
541
542                 // Unhandled from Misskey
543                 // sharedInbox, isCat
544
545                 // Unhandled from Kroeg
546                 // kroeg:blocks, updated
547
548                 return $profile;
549         }
550
551         public static function processInbox($body, $header, $uid)
552         {
553                 logger('Incoming message for user ' . $uid, LOGGER_DEBUG);
554
555                 if (!self::verifySignature($body, $header)) {
556                         logger('Invalid signature, message will be discarded.', LOGGER_DEBUG);
557                         return;
558                 }
559
560                 $activity = json_decode($body, true);
561
562                 if (!is_array($activity)) {
563                         logger('Invalid body.', LOGGER_DEBUG);
564                         return;
565                 }
566
567                 self::processActivity($activity, $body, $uid);
568         }
569
570         public static function fetchOutbox($url)
571         {
572                 $data = self::fetchContent($url);
573                 if (empty($data)) {
574                         return;
575                 }
576
577                 if (!empty($data['orderedItems'])) {
578                         $items = $data['orderedItems'];
579                 } elseif (!empty($data['first']['orderedItems'])) {
580                         $items = $data['first']['orderedItems'];
581                 } elseif (!empty($data['first'])) {
582                         self::fetchOutbox($data['first']);
583                         return;
584                 } else {
585                         $items = [];
586                 }
587
588                 foreach ($items as $activity) {
589                         self::processActivity($activity);
590                 }
591         }
592
593         private static function prepareObjectData($activity, $uid)
594         {
595                 $actor = self::processElement($activity, 'actor', 'id');
596                 if (empty($actor)) {
597                         logger('Empty actor', LOGGER_DEBUG);
598                         return [];
599                 }
600
601                 // Fetch all receivers from to, cc, bto and bcc
602                 $receivers = self::getReceivers($activity);
603
604                 // When it is a delivery to a personal inbox we add that user to the receivers
605                 if (!empty($uid)) {
606                         $owner = User::getOwnerDataById($uid);
607                         $additional = [$owner['url'] => $uid];
608                         $receivers = array_merge($receivers, $additional);
609                 }
610
611                 logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG);
612
613                 if (is_string($activity['object'])) {
614                         $object_url = $activity['object'];
615                 } elseif (!empty($activity['object']['id'])) {
616                         $object_url = $activity['object']['id'];
617                 } else {
618                         logger('No object found', LOGGER_DEBUG);
619                         return [];
620                 }
621
622                 // Fetch the content only on activities where this matters
623                 if (in_array($activity['type'], ['Create', 'Update', 'Announce'])) {
624                         $object_data = self::fetchObject($object_url, $activity['object']);
625                         if (empty($object_data)) {
626                                 logger("Object data couldn't be processed", LOGGER_DEBUG);
627                                 return [];
628                         }
629                 } elseif ($activity['type'] == 'Accept') {
630                         $object_data = [];
631                         $object_data['object_type'] = self::processElement($activity, 'object', 'type');
632                         $object_data['object'] = self::processElement($activity, 'object', 'actor');
633                 } elseif ($activity['type'] == 'Undo') {
634                         $object_data = [];
635                         $object_data['object_type'] = self::processElement($activity, 'object', 'type');
636                         $object_data['object'] = self::processElement($activity, 'object', 'object');
637                 } elseif (in_array($activity['type'], ['Like', 'Dislike'])) {
638                         // Create a mostly empty array out of the activity data (instead of the object).
639                         // This way we later don't have to check for the existence of ech individual array element.
640                         $object_data = self::processCommonData($activity);
641                         $object_data['name'] = $activity['type'];
642                         $object_data['author'] = $activity['actor'];
643                         $object_data['object'] = $object_url;
644                 } elseif ($activity['type'] == 'Follow') {
645                         $object_data['id'] = $activity['id'];
646                         $object_data['object'] = $object_url;
647                 }
648
649                 $object_data = self::addActivityFields($object_data, $activity);
650
651                 $object_data['type'] = $activity['type'];
652                 $object_data['owner'] = $actor;
653                 $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
654
655                 return $object_data;
656         }
657
658         private static function processActivity($activity, $body = '', $uid = null)
659         {
660                 if (empty($activity['type'])) {
661                         logger('Empty type', LOGGER_DEBUG);
662                         return;
663                 }
664
665                 if (empty($activity['object'])) {
666                         logger('Empty object', LOGGER_DEBUG);
667                         return;
668                 }
669
670                 if (empty($activity['actor'])) {
671                         logger('Empty actor', LOGGER_DEBUG);
672                         return;
673
674                 }
675
676                 // Non standard
677                 // title, atomUri, context_id, statusnetConversationId
678
679                 // To-Do?
680                 // context, location, signature;
681
682                 logger('Processing activity: ' . $activity['type'], LOGGER_DEBUG);
683
684                 $object_data = self::prepareObjectData($activity, $uid);
685                 if (empty($object_data)) {
686                         logger('No object data found', LOGGER_DEBUG);
687                         return;
688                 }
689
690                 switch ($activity['type']) {
691                         case 'Create':
692                         case 'Announce':
693                                 self::createItem($object_data, $body);
694                                 break;
695
696                         case 'Like':
697                                 self::likeItem($object_data, $body);
698                                 break;
699
700                         case 'Dislike':
701                                 break;
702
703                         case 'Update':
704                                 break;
705
706                         case 'Delete':
707                                 break;
708
709                         case 'Follow':
710                                 self::followUser($object_data);
711                                 break;
712
713                         case 'Accept':
714                                 if ($object_data['object_type'] == 'Follow') {
715                                         self::acceptFollowUser($object_data);
716                                 }
717                                 break;
718
719                         case 'Undo':
720                                 if ($object_data['object_type'] == 'Follow') {
721                                         self::undoFollowUser($object_data);
722                                 }
723                                 break;
724
725                         default:
726                                 logger('Unknown activity: ' . $activity['type'], LOGGER_DEBUG);
727                                 break;
728                 }
729         }
730
731         private static function getReceivers($activity)
732         {
733                 $receivers = [];
734
735                 $elements = ['to', 'cc', 'bto', 'bcc'];
736                 foreach ($elements as $element) {
737                         if (empty($activity[$element])) {
738                                 continue;
739                         }
740
741                         // The receiver can be an arror or a string
742                         if (is_string($activity[$element])) {
743                                 $activity[$element] = [$activity[$element]];
744                         }
745
746                         foreach ($activity[$element] as $receiver) {
747                                 if ($receiver == self::PUBLIC) {
748                                         $receivers[$receiver] = 0;
749                                 }
750
751                                 $condition = ['self' => true, 'nurl' => normalise_link($receiver)];
752                                 $contact = DBA::selectFirst('contact', ['uid'], $condition);
753                                 if (!DBA::isResult($contact)) {
754                                         continue;
755                                 }
756                                 $receivers[$receiver] = $contact['uid'];
757                         }
758                 }
759                 return $receivers;
760         }
761
762         private static function addActivityFields($object_data, $activity)
763         {
764                 if (!empty($activity['published']) && empty($object_data['published'])) {
765                         $object_data['published'] = $activity['published'];
766                 }
767
768                 if (!empty($activity['updated']) && empty($object_data['updated'])) {
769                         $object_data['updated'] = $activity['updated'];
770                 }
771
772                 if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
773                         $object_data['parent-uri'] = self::processElement($activity, 'inReplyTo', 'id');
774                 }
775
776                 if (!empty($activity['instrument'])) {
777                         $object_data['service'] = self::processElement($activity, 'instrument', 'name', 'type', 'Service');
778                 }
779                 return $object_data;
780         }
781
782         private static function fetchObject($object_url, $object = [])
783         {
784                 $data = self::fetchContent($object_url);
785                 if (empty($data)) {
786                         $data = $object;
787                         if (empty($data)) {
788                                 logger('Empty content', LOGGER_DEBUG);
789                                 return false;
790                         } elseif (is_string($data)) {
791                                 logger('No object array provided.', LOGGER_DEBUG);
792                                 $item = Item::selectFirst([], ['uri' => $data]);
793                                 if (!DBA::isResult($item)) {
794                                         logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG);
795                                         return false;
796                                 }
797                                 logger('Using already stored item', LOGGER_DEBUG);
798                                 $data = self::createNote($item);
799                         } else {
800                                 logger('Using provided object', LOGGER_DEBUG);
801                         }
802                 }
803
804                 if (empty($data['type'])) {
805                         logger('Empty type', LOGGER_DEBUG);
806                         return false;
807                 } else {
808                         $type = $data['type'];
809                         logger('Type ' . $type, LOGGER_DEBUG);
810                 }
811
812                 if (in_array($type, ['Note', 'Article', 'Video'])) {
813                         $common = self::processCommonData($data);
814                 }
815
816                 switch ($type) {
817                         case 'Note':
818                                 return array_merge($common, self::processNote($data));
819                         case 'Article':
820                                 return array_merge($common, self::processArticle($data));
821                         case 'Video':
822                                 return array_merge($common, self::processVideo($data));
823
824                         case 'Announce':
825                                 if (empty($data['object'])) {
826                                         return false;
827                                 }
828                                 return self::fetchObject($data['object']);
829
830                         case 'Person':
831                         case 'Tombstone':
832                                 break;
833
834                         default:
835                                 logger('Unknown object type: ' . $data['type'], LOGGER_DEBUG);
836                                 break;
837                 }
838         }
839
840         private static function processCommonData(&$object)
841         {
842                 if (empty($object['id'])) {
843                         return false;
844                 }
845
846                 $object_data = [];
847                 $object_data['type'] = $object['type'];
848                 $object_data['uri'] = $object['id'];
849
850                 if (!empty($object['inReplyTo'])) {
851                         $object_data['reply-to-uri'] = self::processElement($object, 'inReplyTo', 'id');
852                 } else {
853                         $object_data['reply-to-uri'] = $object_data['uri'];
854                 }
855
856                 $object_data['published'] = defaults($object, 'published', null);
857                 $object_data['updated'] = defaults($object, 'updated', $object_data['published']);
858
859                 if (empty($object_data['published']) && !empty($object_data['updated'])) {
860                         $object_data['published'] = $object_data['updated'];
861                 }
862
863                 $object_data['uuid'] = defaults($object, 'uuid', null);
864                 $object_data['owner'] = $object_data['author'] = self::processElement($object, 'attributedTo', 'id');
865                 $object_data['context'] = defaults($object, 'context', null);
866                 $object_data['conversation'] = defaults($object, 'conversation', null);
867                 $object_data['sensitive'] = defaults($object, 'sensitive', null);
868                 $object_data['name'] = defaults($object, 'title', null);
869                 $object_data['name'] = defaults($object, 'name', $object_data['name']);
870                 $object_data['summary'] = defaults($object, 'summary', null);
871                 $object_data['content'] = defaults($object, 'content', null);
872                 $object_data['source'] = defaults($object, 'source', null);
873                 $object_data['location'] = self::processElement($object, 'location', 'name', 'type', 'Place');
874                 $object_data['attachments'] = defaults($object, 'attachment', null);
875                 $object_data['tags'] = defaults($object, 'tag', null);
876                 $object_data['service'] = self::processElement($object, 'instrument', 'name', 'type', 'Service');
877                 $object_data['alternate-url'] = self::processElement($object, 'url', 'href');
878                 $object_data['receiver'] = self::getReceivers($object);
879
880                 // Unhandled
881                 // @context, type, actor, signature, mediaType, duration, replies, icon
882
883                 // Also missing: (Defined in the standard, but currently unused)
884                 // audience, preview, endTime, startTime, generator, image
885
886                 return $object_data;
887         }
888
889         private static function processNote($object)
890         {
891                 $object_data = [];
892
893                 // To-Do?
894                 // emoji, atomUri, inReplyToAtomUri
895
896                 // Unhandled
897                 // contentMap, announcement_count, announcements, context_id, likes, like_count
898                 // inReplyToStatusId, shares, quoteUrl, statusnetConversationId
899
900                 return $object_data;
901         }
902
903         private static function processArticle($object)
904         {
905                 $object_data = [];
906
907                 return $object_data;
908         }
909
910         private static function processVideo($object)
911         {
912                 $object_data = [];
913
914                 // To-Do?
915                 // category, licence, language, commentsEnabled
916
917                 // Unhandled
918                 // views, waitTranscoding, state, support, subtitleLanguage
919                 // likes, dislikes, shares, comments
920
921                 return $object_data;
922         }
923
924         private static function processElement($array, $element, $key, $type = null, $type_value = null)
925         {
926                 if (empty($array)) {
927                         return false;
928                 }
929
930                 if (empty($array[$element])) {
931                         return false;
932                 }
933
934                 if (is_string($array[$element])) {
935                         return $array[$element];
936                 }
937
938                 if (is_null($type_value)) {
939                         if (!empty($array[$element][$key])) {
940                                 return $array[$element][$key];
941                         }
942
943                         if (!empty($array[$element][0][$key])) {
944                                 return $array[$element][0][$key];
945                         }
946
947                         return false;
948                 }
949
950                 if (!empty($array[$element][$key]) && !empty($array[$element][$type]) && ($array[$element][$type] == $type_value)) {
951                         return $array[$element][$key];
952                 }
953
954                 /// @todo Add array search
955
956                 return false;
957         }
958
959         private static function convertMentions($body)
960         {
961                 $URLSearchString = "^\[\]";
962                 $body = preg_replace("/\[url\=([$URLSearchString]*)\]([#@!])(.*?)\[\/url\]/ism", '$2[url=$1]$3[/url]', $body);
963
964                 return $body;
965         }
966
967         private static function constructTagList($tags, $sensitive)
968         {
969                 if (empty($tags)) {
970                         return '';
971                 }
972
973                 $tag_text = '';
974                 foreach ($tags as $tag) {
975                         if (in_array($tag['type'], ['Mention', 'Hashtag'])) {
976                                 if (!empty($tag_text)) {
977                                         $tag_text .= ',';
978                                 }
979
980                                 $tag_text .= substr($tag['name'], 0, 1) . '[url=' . $tag['href'] . ']' . substr($tag['name'], 1) . '[/url]';
981                         }
982                 }
983
984                 /// @todo add nsfw for $sensitive
985
986                 return $tag_text;
987         }
988
989         private static function constructAttachList($attachments, $item)
990         {
991                 if (empty($attachments)) {
992                         return $item;
993                 }
994
995                 foreach ($attachments as $attach) {
996                         $filetype = strtolower(substr($attach['mediaType'], 0, strpos($attach['mediaType'], '/')));
997                         if ($filetype == 'image') {
998                                 $item['body'] .= "\n[img]".$attach['url'].'[/img]';
999                         } else {
1000                                 if (!empty($item["attach"])) {
1001                                         $item["attach"] .= ',';
1002                                 } else {
1003                                         $item["attach"] = '';
1004                                 }
1005                                 if (!isset($attach['length'])) {
1006                                         $attach['length'] = "0";
1007                                 }
1008                                 $item["attach"] .= '[attach]href="'.$attach['url'].'" length="'.$attach['length'].'" type="'.$attach['mediaType'].'" title="'.defaults($attach, 'name', '').'"[/attach]';
1009                         }
1010                 }
1011
1012                 return $item;
1013         }
1014
1015         private static function createItem($activity, $body)
1016         {
1017                 $item = [];
1018                 $item['verb'] = ACTIVITY_POST;
1019                 $item['parent-uri'] = $activity['reply-to-uri'];
1020
1021                 if ($activity['reply-to-uri'] == $activity['uri']) {
1022                         $item['gravity'] = GRAVITY_PARENT;
1023                         $item['object-type'] = ACTIVITY_OBJ_NOTE;
1024                 } else {
1025                         $item['gravity'] = GRAVITY_COMMENT;
1026                         $item['object-type'] = ACTIVITY_OBJ_COMMENT;
1027                 }
1028
1029                 self::postItem($activity, $item, $body);
1030         }
1031
1032         private static function likeItem($activity, $body)
1033         {
1034                 $item = [];
1035                 $item['verb'] = ACTIVITY_LIKE;
1036                 $item['parent-uri'] = $activity['object'];
1037                 $item['gravity'] = GRAVITY_ACTIVITY;
1038                 $item['object-type'] = ACTIVITY_OBJ_NOTE;
1039
1040                 self::postItem($activity, $item, $body);
1041         }
1042
1043         private static function postItem($activity, $item, $body)
1044         {
1045                 /// @todo What to do with $activity['context']?
1046
1047                 $item['network'] = Protocol::ACTIVITYPUB;
1048                 $item['private'] = !in_array(0, $activity['receiver']);
1049                 $item['author-id'] = Contact::getIdForURL($activity['author'], 0, true);
1050                 $item['owner-id'] = Contact::getIdForURL($activity['owner'], 0, true);
1051                 $item['uri'] = $activity['uri'];
1052                 $item['created'] = $activity['published'];
1053                 $item['edited'] = $activity['updated'];
1054                 $item['guid'] = $activity['uuid'];
1055                 $item['title'] = HTML::toBBCode($activity['name']);
1056                 $item['content-warning'] = HTML::toBBCode($activity['summary']);
1057                 $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
1058                 $item['location'] = $activity['location'];
1059                 $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
1060                 $item['app'] = $activity['service'];
1061                 $item['plink'] = defaults($activity, 'alternate-url', $item['uri']);
1062
1063                 $item = self::constructAttachList($activity['attachments'], $item);
1064
1065                 $source = self::processElement($activity, 'source', 'content', 'mediaType', 'text/bbcode');
1066                 if (!empty($source)) {
1067                         $item['body'] = $source;
1068                 }
1069
1070                 $item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
1071                 $item['source'] = $body;
1072                 $item['conversation-uri'] = $activity['conversation'];
1073
1074                 foreach ($activity['receiver'] as $receiver) {
1075                         $item['uid'] = $receiver;
1076                         $item['contact-id'] = Contact::getIdForURL($activity['author'], $receiver, true);
1077
1078                         if (($receiver != 0) && empty($item['contact-id'])) {
1079                                 $item['contact-id'] = Contact::getIdForURL($activity['author'], 0, true);
1080                         }
1081
1082                         $item_id = Item::insert($item);
1083                         logger('Storing for user ' . $item['uid'] . ': ' . $item_id);
1084                         if (!empty($item_id) && ($item['uid'] == 0)) {
1085                                 Item::distribute($item_id);
1086                         }
1087                 }
1088         }
1089
1090         private static function followUser($activity)
1091         {
1092                 if (empty($activity['receiver'][$activity['object']])) {
1093                         return;
1094                 }
1095
1096                 $uid = $activity['receiver'][$activity['object']];
1097                 $owner = User::getOwnerDataById($uid);
1098
1099                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1100                 if (!empty($cid)) {
1101                         $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1102                 } else {
1103                         $contact = false;
1104                 }
1105
1106                 $item = ['author-id' => Contact::getIdForURL($activity['owner']),
1107                         'author-link' => $activity['owner']];
1108
1109                 Contact::addRelationship($owner, $contact, $item);
1110                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1111                 if (empty($cid)) {
1112                         return;
1113                 }
1114
1115                 $contact = DBA::selectFirst('contact', ['network'], ['id' => $cid]);
1116                 if ($contact['network'] != Protocol::ACTIVITYPUB) {
1117                         Contact::updateFromProbe($cid, Protocol::ACTIVITYPUB);
1118                 }
1119
1120                 DBA::update('contact', ['hub-verify' => $activity['id']], ['id' => $cid]);
1121                 logger('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
1122         }
1123
1124         private static function acceptFollowUser($activity)
1125         {
1126                 if (empty($activity['receiver'][$activity['object']])) {
1127                         return;
1128                 }
1129
1130                 $uid = $activity['receiver'][$activity['object']];
1131                 $owner = User::getOwnerDataById($uid);
1132
1133                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1134                 if (empty($cid)) {
1135                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1136                         return;
1137                 }
1138
1139                 $fields = ['pending' => false];
1140
1141                 $contact = DBA::selectFirst('contact', ['rel'], ['id' => $cid]);
1142                 if ($contact['rel'] == Contact::FOLLOWER) {
1143                         $fields['rel'] = Contact::FRIEND;
1144                 }
1145
1146                 $condition = ['id' => $cid];
1147                 DBA::update('contact', $fields, $condition);
1148                 logger('Accept contact request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1149         }
1150
1151         private static function undoFollowUser($activity)
1152         {
1153                 if (empty($activity['receiver'][$activity['object']])) {
1154                         return;
1155                 }
1156
1157                 $uid = $activity['receiver'][$activity['object']];
1158                 $owner = User::getOwnerDataById($uid);
1159
1160                 $cid = Contact::getIdForURL($activity['owner'], $uid);
1161                 if (empty($cid)) {
1162                         logger('No contact found for ' . $activity['owner'], LOGGER_DEBUG);
1163                         return;
1164                 }
1165
1166                 $contact = DBA::selectFirst('contact', [], ['id' => $cid]);
1167                 if (!DBA::isResult($contact)) {
1168                         return;
1169                 }
1170
1171                 Contact::removeFollower($owner, $contact);
1172                 logger('Undo following request from contact ' . $cid . ' for user ' . $uid, LOGGER_DEBUG);
1173         }
1174 }