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