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