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