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