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