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