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