]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Some better context for notices as arrays
[quix0rs-gnu-social.git] / lib / activityobject.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * An activity
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * A noun-ish thing in the activity universe
37  *
38  * The activity streams spec talks about activity objects, while also having
39  * a tag activity:object, which is in fact an activity object. Aaaaaah!
40  *
41  * This is just a thing in the activity universe. Can be the subject, object,
42  * or indirect object (target!) of an activity verb. Rotten name, and I'm
43  * propagating it. *sigh*
44  *
45  * @category  OStatus
46  * @package   StatusNet
47  * @author    Evan Prodromou <evan@status.net>
48  * @copyright 2010 StatusNet, Inc.
49  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50  * @link      http://status.net/
51  */
52 class ActivityObject
53 {
54     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
55     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
57     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
58     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
59     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
60     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
61     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
62     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
63     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
64     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
65     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
66     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
67     const _LIST     = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
68     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
69     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
70     // ^^^^^^^^^^ tea!
71     const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
72     const SERVICE   = 'http://activitystrea.ms/schema/1.0/service';
73     const IMAGE     = 'http://activitystrea.ms/schema/1.0/image';
74     const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
75
76     // Atom elements we snarf
77
78     const TITLE   = 'title';
79     const SUMMARY = 'summary';
80     const ID      = 'id';
81     const SOURCE  = 'source';
82
83     const NAME  = 'name';
84     const URI   = 'uri';
85     const EMAIL = 'email';
86
87     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
88     const AUTHOR      = 'author';
89     const USERIMAGE   = 'userImage';
90     const PROFILEURL  = 'profileUrl';
91     const NICKNAME    = 'nickName';
92     const DISPLAYNAME = 'displayName';
93
94     public $element;
95     public $type;
96     public $id;
97     public $title;
98     public $summary;
99     public $content;
100     public $owner;
101     public $link;
102     public $source;
103     public $avatarLinks = array();
104     public $geopoint;
105     public $poco;
106     public $displayName;
107
108     // @todo move this stuff to it's own PHOTO activity object
109     const MEDIA_DESCRIPTION = 'description';
110
111     public $thumbnail;
112     public $largerImage;
113     public $description;
114     public $extra = array();
115
116     public $stream;
117
118     /**
119      * Constructor
120      *
121      * This probably needs to be refactored
122      * to generate a local class (ActivityPerson, ActivityFile, ...)
123      * based on the object type.
124      *
125      * @param DOMElement $element DOM thing to turn into an Activity thing
126      */
127     function __construct($element = null)
128     {
129         if (empty($element)) {
130             return;
131         }
132
133         $this->element = $element;
134
135         $this->geopoint = $this->_childContent(
136             $element,
137             ActivityContext::POINT,
138             ActivityContext::GEORSS
139         );
140
141         if ($element->tagName == 'author') {
142             $this->_fromAuthor($element);
143         } else if ($element->tagName == 'item') {
144             $this->_fromRssItem($element);
145         } else {
146             $this->_fromAtomEntry($element);
147         }
148
149         // Some per-type attributes...
150         if ($this->type == self::PERSON || $this->type == self::GROUP) {
151             $this->displayName = $this->title;
152
153             $photos = ActivityUtils::getLinks($element, 'photo');
154             if (count($photos)) {
155                 foreach ($photos as $link) {
156                     $this->avatarLinks[] = new AvatarLink($link);
157                 }
158             } else {
159                 $avatars = ActivityUtils::getLinks($element, 'avatar');
160                 foreach ($avatars as $link) {
161                     $this->avatarLinks[] = new AvatarLink($link);
162                 }
163             }
164
165             $this->poco = new PoCo($element);
166         }
167
168         if ($this->type == self::PHOTO) {
169
170             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
171             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
172
173             $this->description = ActivityUtils::childContent(
174                 $element,
175                 ActivityObject::MEDIA_DESCRIPTION,
176                 Activity::MEDIA
177             );
178         }
179         if ($this->type == self::_LIST) {
180             $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
181             $this->owner = new ActivityObject($owner);
182         }
183     }
184
185     private function _fromAuthor($element)
186     {
187         $this->type = $this->_childContent($element,
188                                            Activity::OBJECTTYPE,
189                                            Activity::SPEC);
190
191         if (empty($this->type)) {
192             $this->type = self::PERSON; // XXX: is this fair?
193         }
194
195         // start with <atom:title>
196
197         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
198
199         if (!empty($title)) {
200             $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
201         }
202
203         // fall back to <atom:name>
204
205         if (empty($this->title)) {
206             $this->title = $this->_childContent($element, self::NAME);
207         }
208
209         // start with <atom:id>
210
211         $this->id = $this->_childContent($element, self::ID);
212
213         // fall back to <atom:uri>
214
215         if (empty($this->id)) {
216             $this->id = $this->_childContent($element, self::URI);
217         }
218
219         // fall further back to <atom:email>
220
221         if (empty($this->id)) {
222             $email = $this->_childContent($element, self::EMAIL);
223             if (!empty($email)) {
224                 // XXX: acct: ?
225                 $this->id = 'mailto:'.$email;
226             }
227         }
228
229         $this->link = ActivityUtils::getPermalink($element);
230
231         // fall finally back to <link rel=alternate>
232
233         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
234             $this->id = $this->link;
235         }
236     }
237
238     private function _fromAtomEntry($element)
239     {
240         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
241                                            Activity::SPEC);
242
243         if (empty($this->type)) {
244             $this->type = ActivityObject::NOTE;
245         }
246
247         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
248         $this->content = ActivityUtils::getContent($element);
249
250         // We don't like HTML in our titles, although it's technically allowed
251
252         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
253
254         $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
255
256         $this->source  = $this->_getSource($element);
257
258         $this->link = ActivityUtils::getPermalink($element);
259
260         $this->id = $this->_childContent($element, self::ID);
261
262         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
263             $this->id = $this->link;
264         }
265     }
266
267     // @todo FIXME: rationalize with Activity::_fromRssItem()
268     private function _fromRssItem($item)
269     {
270         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
271
272         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
273
274         if (!empty($contentEl)) {
275             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
276         } else {
277             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
278             if (!empty($descriptionEl)) {
279                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
280             }
281         }
282
283         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
284
285         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
286
287         if (!empty($guidEl)) {
288             $this->id = $guidEl->textContent;
289
290             if ($guidEl->hasAttribute('isPermaLink')) {
291                 // overwrites <link>
292                 $this->link = $this->id;
293             }
294         }
295     }
296
297     public static function fromRssAuthor($el)
298     {
299         $text = $el->textContent;
300
301         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
302             $email = $match[1];
303             $name = $match[2];
304         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
305             $name = $match[1];
306             $email = $match[2];
307         } else if (preg_match('/.*@.*/', $text)) {
308             $email = $text;
309             $name = null;
310         } else {
311             $name = $text;
312             $email = null;
313         }
314
315         // Not really enough info
316
317         $obj = new ActivityObject();
318
319         $obj->element = $el;
320
321         $obj->type  = ActivityObject::PERSON;
322         $obj->title = $name;
323
324         if (!empty($email)) {
325             $obj->id = 'mailto:'.$email;
326         }
327
328         return $obj;
329     }
330
331     public static function fromDcCreator($el)
332     {
333         // Not really enough info
334
335         $text = $el->textContent;
336
337         $obj = new ActivityObject();
338
339         $obj->element = $el;
340
341         $obj->title = $text;
342         $obj->type  = ActivityObject::PERSON;
343
344         return $obj;
345     }
346
347     public static function fromRssChannel($el)
348     {
349         $obj = new ActivityObject();
350
351         $obj->element = $el;
352
353         $obj->type = ActivityObject::PERSON; // @fixme guess better
354
355         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
356         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
357         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
358
359         if (empty($obj->id)) {
360             $obj->id = $obj->link;
361         }
362
363         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
364
365         if (!empty($desc)) {
366             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
367         }
368
369         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
370
371         if (!empty($imageEl)) {
372             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
373             $al = new AvatarLink();
374             $al->url = $url;
375             $obj->avatarLinks[] = $al;
376         }
377
378         return $obj;
379     }
380
381     public static function fromPosterousAuthor($el)
382     {
383         $obj = new ActivityObject();
384
385         $obj->type = ActivityObject::PERSON; // @fixme any others...?
386
387         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
388
389         if (!empty($userImage)) {
390             $al = new AvatarLink();
391             $al->url = $userImage;
392             $obj->avatarLinks[] = $al;
393         }
394
395         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
396         $obj->id   = $obj->link;
397
398         $obj->poco = new PoCo();
399
400         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
401         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
402
403         $obj->title = $obj->poco->displayName;
404
405         return $obj;
406     }
407
408     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
409     {
410         return ActivityUtils::childContent($element, $tag, $namespace);
411     }
412
413     // Try to get a unique id for the source feed
414
415     private function _getSource($element)
416     {
417         $sourceEl = ActivityUtils::child($element, 'source');
418
419         if (empty($sourceEl)) {
420             return null;
421         } else {
422             $href = ActivityUtils::getLink($sourceEl, 'self');
423             if (!empty($href)) {
424                 return $href;
425             } else {
426                 return ActivityUtils::childContent($sourceEl, 'id');
427             }
428         }
429     }
430
431     static function fromNotice(Notice $notice)
432     {
433         $object = new ActivityObject();
434
435         if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
436
437             $object->type    = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
438
439             $object->id      = $notice->uri;
440             $object->content = $notice->rendered;
441             $object->link    = $notice->bestUrl();
442
443             Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
444         }
445
446         return $object;
447     }
448
449     static function fromProfile(Profile $profile)
450     {
451         $object = new ActivityObject();
452
453         if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
454             $object->type   = ActivityObject::PERSON;
455             $object->id     = $profile->getUri();
456             $object->title  = $profile->getBestName();
457             $object->link   = $profile->profileurl;
458
459             $orig = $profile->getOriginalAvatar();
460
461             if (!empty($orig)) {
462                 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
463             }
464
465             $sizes = array(
466                 AVATAR_PROFILE_SIZE,
467                 AVATAR_STREAM_SIZE,
468                 AVATAR_MINI_SIZE
469             );
470
471             foreach ($sizes as $size) {
472                 $alink  = null;
473                 $avatar = $profile->getAvatar($size);
474
475                 if (!empty($avatar)) {
476                     $alink = AvatarLink::fromAvatar($avatar);
477                 } else {
478                     $alink = new AvatarLink();
479                     $alink->type   = 'image/png';
480                     $alink->height = $size;
481                     $alink->width  = $size;
482                     $alink->url    = Avatar::defaultImage($size);
483
484                     if ($size == AVATAR_PROFILE_SIZE) {
485                         // Hack for Twitter import: we don't have a 96x96 image,
486                         // but we do have a 73x73 image. For now, fake it with that.
487                         $avatar = $profile->getAvatar(73);
488                         if ($avatar) {
489                             $alink = AvatarLink::fromAvatar($avatar);
490                             $alink->height= $size;
491                             $alink->width = $size;
492                         }
493                     }
494                 }
495
496                 $object->avatarLinks[] = $alink;
497             }
498
499             if (isset($profile->lat) && isset($profile->lon)) {
500                 $object->geopoint = (float)$profile->lat
501                     . ' ' . (float)$profile->lon;
502             }
503
504             $object->poco = PoCo::fromProfile($profile);
505
506             if ($profile->getUser()) {
507                 $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $profile->nickname))));
508             }
509
510             Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
511         }
512
513         return $object;
514     }
515
516     static function fromGroup($group)
517     {
518         $object = new ActivityObject();
519
520         if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
521
522             $object->type   = ActivityObject::GROUP;
523             $object->id     = $group->getUri();
524             $object->title  = $group->getBestName();
525             $object->link   = $group->getUri();
526
527             $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
528                                                               AVATAR_PROFILE_SIZE);
529
530             $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
531                                                               AVATAR_STREAM_SIZE);
532
533             $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
534                                                               AVATAR_MINI_SIZE);
535
536             $object->poco = PoCo::fromGroup($group);
537             Event::handle('EndActivityObjectFromGroup', array($group, &$object));
538         }
539
540         return $object;
541     }
542
543     static function fromPeopletag($ptag)
544     {
545         $object = new ActivityObject();
546         if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
547             $object->type    = ActivityObject::_LIST;
548
549             $object->id      = $ptag->getUri();
550             $object->title   = $ptag->tag;
551             $object->summary = $ptag->description;
552             $object->link    = $ptag->homeUrl();
553             $object->owner   = Profile::staticGet('id', $ptag->tagger);
554             $object->poco    = PoCo::fromProfile($object->owner);
555             Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
556         }
557         return $object;
558     }
559
560     static function fromFile(File $file)
561     {
562         $object = new ActivityObject();
563
564         if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
565
566             $object->type = self::mimeTypeToObjectType($file->mimetype);
567             $object->id   = TagURI::mint(sprintf("file:%d", $file->id));
568             $object->link = common_local_url('attachment', array('attachment' => $file->id));
569
570             if ($file->title) {
571                 $object->title = $file->title;
572             }
573
574             if ($file->date) {
575                 $object->date = $file->date;
576             }
577
578             $thumbnail = $file->getThumbnail();
579
580             if (!empty($thumbnail)) {
581                 $object->thumbnail = $thumbnail;
582             }
583
584             switch (ActivityObject::canonicalType($object->type)) {
585             case 'image':
586                 $object->largerImage = $file->url;
587                 break;
588             case 'video':
589             case 'audio':
590                 $object->stream = $file->url;
591                 break;
592             }
593
594             Event::handle('EndActivityObjectFromFile', array($file, &$object));
595         }
596
597         return $object;
598     }
599
600     function outputTo($xo, $tag='activity:object')
601     {
602         if (!empty($tag)) {
603             $xo->elementStart($tag);
604         }
605
606         if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
607             $xo->element('activity:object-type', null, $this->type);
608
609             // <author> uses URI
610
611             if ($tag == 'author') {
612                 $xo->element(self::URI, null, $this->id);
613             } else {
614                 $xo->element(self::ID, null, $this->id);
615             }
616
617             if (!empty($this->title)) {
618                 $name = common_xml_safe_str($this->title);
619                 if ($tag == 'author') {
620                     // XXX: Backward compatibility hack -- atom:name should contain
621                     // full name here, instead of nickname, i.e.: $name. Change
622                     // this in the next version.
623                     $xo->element(self::NAME, null, $this->poco->preferredUsername);
624                 } else {
625                     $xo->element(self::TITLE, null, $name);
626                 }
627             }
628
629             if (!empty($this->summary)) {
630                 $xo->element(
631                     self::SUMMARY,
632                     null,
633                     common_xml_safe_str($this->summary)
634                 );
635             }
636
637             if (!empty($this->content)) {
638                 // XXX: assuming HTML content here
639                 $xo->element(
640                     ActivityUtils::CONTENT,
641                     array('type' => 'html'),
642                     common_xml_safe_str($this->content)
643                 );
644             }
645
646             if (!empty($this->link)) {
647                 $xo->element(
648                     'link',
649                     array(
650                         'rel' => 'alternate',
651                         'type' => 'text/html',
652                         'href' => $this->link
653                     ),
654                     null
655                 );
656             }
657
658             if(!empty($this->owner)) {
659                 $owner = $this->owner->asActivityNoun(self::AUTHOR);
660                 $xo->raw($owner);
661             }
662
663             if ($this->type == ActivityObject::PERSON
664                 || $this->type == ActivityObject::GROUP) {
665
666                 foreach ($this->avatarLinks as $avatar) {
667                     $xo->element(
668                         'link', array(
669                             'rel'  => 'avatar',
670                             'type'         => $avatar->type,
671                             'media:width'  => $avatar->width,
672                             'media:height' => $avatar->height,
673                             'href' => $avatar->url
674                         ),
675                         null
676                     );
677                 }
678             }
679
680             if (!empty($this->geopoint)) {
681                 $xo->element(
682                     'georss:point',
683                     null,
684                     $this->geopoint
685                 );
686             }
687
688             if (!empty($this->poco)) {
689                 $this->poco->outputTo($xo);
690             }
691
692             // @fixme there's no way here to make a tree; elements can only contain plaintext
693             // @fixme these may collide with JSON extensions
694             foreach ($this->extra as $el) {
695                 list($extraTag, $attrs, $content) = $el;
696                 $xo->element($extraTag, $attrs, $content);
697             }
698
699             Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
700         }
701
702         if (!empty($tag)) {
703             $xo->elementEnd($tag);
704         }
705
706         return;
707     }
708
709     function asString($tag='activity:object')
710     {
711         $xs = new XMLStringer(true);
712
713         $this->outputTo($xs, $tag);
714
715         return $xs->getString();
716     }
717
718     /*
719      * Returns an array based on this Activity Object suitable for
720      * encoding as JSON.
721      *
722      * @return array $object the activity object array
723      */
724
725     function asArray()
726     {
727         $object = array();
728
729         if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
730             // XXX: attachments are added by Activity
731
732             // author (Add object for author? Could be useful for repeats.)
733
734             // content (Add rendered version of the notice?)
735
736             // displayName
737
738             if ($this->title) {
739                 $object['displayName'] = $this->title;
740             }
741
742             // downstreamDuplicates
743
744             // id
745
746             if ($this->id) {
747                 $object['id'] = $this->id;
748             } else if ($this->link) {
749                 $object['id'] = $this->link;
750             }
751
752             if ($this->type == ActivityObject::PERSON
753                 || $this->type == ActivityObject::GROUP) {
754
755                 // XXX: Not sure what the best avatar is to use for the
756                 // author's "image". For now, I'm using the large size.
757
758                 $imgLink          = null;
759                 $avatarMediaLinks = array();
760
761                 foreach ($this->avatarLinks as $a) {
762
763                     // Make a MediaLink for every other Avatar
764                     $avatar = new ActivityStreamsMediaLink(
765                         $a->url,
766                         $a->width,
767                         $a->height,
768                         $a->type,
769                         'avatar'
770                     );
771
772                     // Find the big avatar to use as the "image"
773                     if ($a->height == AVATAR_PROFILE_SIZE) {
774                         $imgLink = $avatar;
775                     }
776
777                     $avatarMediaLinks[] = $avatar->asArray();
778                 }
779
780                 if (!array_key_exists('status_net', $object)) {
781                     $object['status_net'] = array();
782                 }
783
784                 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
785
786                 // image
787                 if (!empty($imgLink)) {
788                     $object['image']  = $imgLink->asArray();
789                 }
790             }
791
792             // objectType
793             //
794             // We can probably use the whole schema URL here but probably the
795             // relative simple name is easier to parse
796
797             $object['objectType'] = ActivityObject::canonicalType($this->type);
798
799             // summary
800             $object['summary'] = $this->summary;
801
802             // summary
803             $object['content'] = $this->content;
804
805             // published (probably don't need. Might be useful for repeats.)
806
807             // updated (probably don't need this)
808
809             // TODO: upstreamDuplicates
810
811             if ($this->link) {
812                 $object['url'] = $this->link;
813             }
814
815             /* Extensions */
816             // @fixme these may collide with XML extensions
817             // @fixme multiple tags of same name will overwrite each other
818             // @fixme text content from XML extensions will be lost
819
820             foreach ($this->extra as $e) {
821                 list($objectName, $props, $txt) = $e;
822                 if (!empty($objectName)) {
823                     $parts = explode(":", $objectName);
824                     if (count($parts) == 2 && $parts[0] == "statusnet") {
825                         if (!array_key_exists('status_net', $object)) {
826                             $object['status_net'] = array();
827                         }
828                         $object['status_net'][$parts[1]] = $props;
829                     } else {
830                         $object[$objectName] = $props;
831                     }
832                 }
833             }
834
835             if (!empty($this->geopoint)) {
836
837                 list($lat, $lon) = explode(' ', $this->geopoint);
838
839                 $object['location'] = array(
840                     'objectType' => 'place',
841                     'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
842                     'lat' => $lat,
843                     'lon' => $lon
844                 );
845
846                 $loc = Location::fromLatLon($lat, $lon);
847
848                 if ($loc) {
849                     $name = $loc->getName();
850
851                     if ($name) {
852                         $object['location']['displayName'] = $name;
853                     }
854                     $url = $loc->getURL();
855
856                     if ($url) {
857                         $object['location']['url'] = $url;
858                     }
859                 }
860             }
861
862             if (!empty($this->poco)) {
863                 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
864             }
865
866             if (!empty($this->thumbnail)) {
867                 if (is_string($this->thumbnail)) {
868                     $object['image'] = array('url' => $this->thumbnail);
869                 } else {
870                     $object['image'] = array('url' => $this->thumbnail->url);
871                     if ($this->thumbnail->width) {
872                         $object['image']['width'] = $this->thumbnail->width;
873                     }
874                     if ($this->thumbnail->height) {
875                         $object['image']['height'] = $this->thumbnail->height;
876                     }
877                 }
878             }
879
880             switch (ActivityObject::canonicalType($this->type)) {
881             case 'image':
882                 if (!empty($this->largerImage)) {
883                     $object['fullImage'] = array('url' => $this->largerImage);
884                 }
885                 break;
886             case 'audio':
887             case 'video':
888                 if (!empty($this->stream)) {
889                     $object['stream'] = array('url' => $this->stream);
890                 }
891                 break;
892             }
893
894             Event::handle('EndActivityObjectOutputJson', array($this, &$object));
895         }
896         return array_filter($object);
897     }
898
899     static function canonicalType($type) {
900         $ns = 'http://activitystrea.ms/schema/1.0/';
901         if (substr($type, 0, mb_strlen($ns)) == $ns) {
902             return substr($type, mb_strlen($ns));
903         } else {
904             return $type;
905         }
906     }
907
908     static function mimeTypeToObjectType($mimeType) {
909         $ot = null;
910
911         // Default
912
913         if (empty($mimeType)) {
914             return self::FILE;
915         }
916
917         $parts = explode('/', $mimeType);
918
919         switch ($parts[0]) {
920         case 'image':
921             $ot = self::IMAGE;
922             break;
923         case 'audio':
924             $ot = self::AUDIO;
925             break;
926         case 'video':
927             $ot = self::VIDEO;
928             break;
929         default:
930             $ot = self::FILE;
931         }
932
933         return $ot;
934     }
935 }