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