]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Merge request from chimo adding getvaliddaemons to stopdaemons.php
[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 $source;
106     public $avatarLinks = array();
107     public $geopoint;
108     public $poco;
109     public $displayName;
110
111     // @todo move this stuff to it's own PHOTO activity object
112     const MEDIA_DESCRIPTION = 'description';
113
114     public $thumbnail;
115     public $largerImage;
116     public $description;
117     public $extra = array();
118
119     public $stream;
120
121     /**
122      * Constructor
123      *
124      * This probably needs to be refactored
125      * to generate a local class (ActivityPerson, ActivityFile, ...)
126      * based on the object type.
127      *
128      * @param DOMElement $element DOM thing to turn into an Activity thing
129      */
130     function __construct($element = null)
131     {
132         if (empty($element)) {
133             return;
134         }
135
136         $this->element = $element;
137
138         $this->geopoint = $this->_childContent(
139             $element,
140             ActivityContext::POINT,
141             ActivityContext::GEORSS
142         );
143
144         if ($element->tagName == 'author') {
145             $this->_fromAuthor($element);
146         } else if ($element->tagName == 'item') {
147             $this->_fromRssItem($element);
148         } else {
149             $this->_fromAtomEntry($element);
150         }
151
152         // Some per-type attributes...
153         if ($this->type == self::PERSON || $this->type == self::GROUP) {
154             $this->displayName = $this->title;
155
156             $photos = ActivityUtils::getLinks($element, 'photo');
157             if (count($photos)) {
158                 foreach ($photos as $link) {
159                     $this->avatarLinks[] = new AvatarLink($link);
160                 }
161             } else {
162                 $avatars = ActivityUtils::getLinks($element, 'avatar');
163                 foreach ($avatars as $link) {
164                     $this->avatarLinks[] = new AvatarLink($link);
165                 }
166             }
167
168             $this->poco = new PoCo($element);
169         }
170
171         if ($this->type == self::PHOTO) {
172
173             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
174             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
175
176             $this->description = ActivityUtils::childContent(
177                 $element,
178                 ActivityObject::MEDIA_DESCRIPTION,
179                 Activity::MEDIA
180             );
181         }
182         if ($this->type == self::_LIST) {
183             $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
184             $this->owner = new ActivityObject($owner);
185         }
186     }
187
188     private function _fromAuthor($element)
189     {
190         $this->type = $this->_childContent($element,
191                                            Activity::OBJECTTYPE,
192                                            Activity::SPEC);
193
194         if (empty($this->type)) {
195             $this->type = self::PERSON; // XXX: is this fair?
196         }
197
198         // start with <atom:title>
199
200         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
201
202         if (!empty($title)) {
203             $this->title = common_strip_html($title);
204         }
205
206         // fall back to <atom:name>
207
208         if (empty($this->title)) {
209             $this->title = $this->_childContent($element, self::NAME);
210         }
211
212         // start with <atom:id>
213
214         $this->id = $this->_childContent($element, self::ID);
215
216         // fall back to <atom:uri>
217
218         if (empty($this->id)) {
219             $this->id = $this->_childContent($element, self::URI);
220         }
221
222         // fall further back to <atom:email>
223
224         if (empty($this->id)) {
225             $email = $this->_childContent($element, self::EMAIL);
226             if (!empty($email)) {
227                 // XXX: acct: ?
228                 $this->id = 'mailto:'.$email;
229             }
230         }
231
232         $this->link = ActivityUtils::getPermalink($element);
233
234         // fall finally back to <link rel=alternate>
235
236         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
237             $this->id = $this->link;
238         }
239     }
240
241     private function _fromAtomEntry($element)
242     {
243         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
244                                            Activity::SPEC);
245
246         if (empty($this->type)) {
247             $this->type = ActivityObject::NOTE;
248         }
249
250         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
251         $this->content = ActivityUtils::getContent($element);
252
253         // We don't like HTML in our titles, although it's technically allowed
254         $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
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 fromGroup(User_group $group)
432     {
433         $object = new ActivityObject();
434
435         if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
436
437             $object->type   = ActivityObject::GROUP;
438             $object->id     = $group->getUri();
439             $object->title  = $group->getBestName();
440             $object->link   = $group->getUri();
441
442             $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
443                                                               AVATAR_PROFILE_SIZE);
444
445             $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
446                                                               AVATAR_STREAM_SIZE);
447
448             $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
449                                                               AVATAR_MINI_SIZE);
450
451             $object->poco = PoCo::fromGroup($group);
452             Event::handle('EndActivityObjectFromGroup', array($group, &$object));
453         }
454
455         return $object;
456     }
457
458     static function fromPeopletag($ptag)
459     {
460         $object = new ActivityObject();
461         if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
462             $object->type    = ActivityObject::_LIST;
463
464             $object->id      = $ptag->getUri();
465             $object->title   = $ptag->tag;
466             $object->summary = $ptag->description;
467             $object->link    = $ptag->homeUrl();
468             $object->owner   = Profile::getKV('id', $ptag->tagger);
469             $object->poco    = PoCo::fromProfile($object->owner);
470             Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
471         }
472         return $object;
473     }
474
475     static function fromFile(File $file)
476     {
477         $object = new ActivityObject();
478
479         if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
480
481             $object->type = self::mimeTypeToObjectType($file->mimetype);
482             $object->id   = TagURI::mint(sprintf("file:%d", $file->id));
483             $object->link = common_local_url('attachment', array('attachment' => $file->id));
484
485             if ($file->title) {
486                 $object->title = $file->title;
487             }
488
489             if ($file->date) {
490                 $object->date = $file->date;
491             }
492
493             try {
494                 $thumbnail = $file->getThumbnail();
495                 $object->thumbnail = $thumbnail;
496             } catch (UseFileAsThumbnailException $e) {
497                 $object->thumbnail = null;
498             } catch (UnsupportedMediaException $e) {
499                 $object->thumbnail = null;
500             }
501
502             switch (self::canonicalType($object->type)) {
503             case 'image':
504                 $object->largerImage = $file->url;
505                 break;
506             case 'video':
507             case 'audio':
508                 $object->stream = $file->url;
509                 break;
510             }
511
512             Event::handle('EndActivityObjectFromFile', array($file, &$object));
513         }
514
515         return $object;
516     }
517
518     static function fromNoticeSource(Notice_source $source)
519     {
520         $object = new ActivityObject();
521         $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
522                            'activity', 'feed', 'mirror', 'twitter', 'facebook');
523
524         if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
525             $object->type = ActivityObject::APPLICATION;
526
527             if (in_array($source->code, $wellKnown)) {
528                 // We use one ID for all well-known StatusNet sources
529                 $object->id = "tag:status.net,2009:notice-source:".$source->code;
530             } else if ($source->url) {
531                 // They registered with an URL
532                 $object->id = $source->url;
533             } else {
534                 // Locally-registered, no URL
535                 $object->id = TagURI::mint("notice-source:".$source->code);
536             }
537
538             if ($source->url) {
539                 $object->link = $source->url;
540             }
541
542             if ($source->name) {
543                 $object->title = $source->name;
544             } else {
545                 $object->title = $source->code;
546             }
547
548             if ($source->created) {
549                 $object->date = $source->created;
550             }
551             
552             $object->extra[] = array('status_net', array('source_code' => $source->code));
553
554             Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
555         }
556
557         return $object;
558     }
559
560     static function fromMessage(Message $message)
561     {
562         $object = new ActivityObject();
563
564         if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
565
566             $object->type    = ActivityObject::NOTE;
567             $object->id      = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
568             $object->content = $message->rendered;
569             $object->date    = $message->created;
570
571             if ($message->url) {
572                 $object->link = $message->url;
573             } else {
574                 $object->link = common_local_url('showmessage', array('message' => $message->id));
575             }
576
577             $object->extra[] = array('status_net', array('message_id' => $message->id));
578             
579             Event::handle('EndActivityObjectFromMessage', array($message, &$object));
580         }
581
582         return $object;
583     }
584
585     function outputTo($xo, $tag='activity:object')
586     {
587         if (!empty($tag)) {
588             $xo->elementStart($tag);
589         }
590
591         if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
592             $xo->element('activity:object-type', null, $this->type);
593
594             // <author> uses URI
595
596             if ($tag == 'author') {
597                 $xo->element(self::URI, null, $this->id);
598             } else {
599                 $xo->element(self::ID, null, $this->id);
600             }
601
602             if (!empty($this->title)) {
603                 $name = common_xml_safe_str($this->title);
604                 if ($tag == 'author') {
605                     // XXX: Backward compatibility hack -- atom:name should contain
606                     // full name here, instead of nickname, i.e.: $name. Change
607                     // this in the next version.
608                     $xo->element(self::NAME, null, $this->poco->preferredUsername);
609                 } else {
610                     $xo->element(self::TITLE, null, $name);
611                 }
612             }
613
614             if (!empty($this->summary)) {
615                 $xo->element(
616                     self::SUMMARY,
617                     null,
618                     common_xml_safe_str($this->summary)
619                 );
620             }
621
622             if (!empty($this->content)) {
623                 // XXX: assuming HTML content here
624                 $xo->element(
625                     ActivityUtils::CONTENT,
626                     array('type' => 'html'),
627                     common_xml_safe_str($this->content)
628                 );
629             }
630
631             if (!empty($this->link)) {
632                 $xo->element(
633                     'link',
634                     array(
635                         'rel' => 'alternate',
636                         'type' => 'text/html',
637                         'href' => $this->link
638                     ),
639                     null
640                 );
641             }
642
643             if(!empty($this->owner)) {
644                 $owner = $this->owner->asActivityNoun(self::AUTHOR);
645                 $xo->raw($owner);
646             }
647
648             if ($this->type == ActivityObject::PERSON
649                 || $this->type == ActivityObject::GROUP) {
650
651                 foreach ($this->avatarLinks as $alink) {
652                     $xo->element('link',
653                             array(
654                                 'rel'          => 'avatar',
655                                 'type'         => $alink->type,
656                                 'media:width'  => $alink->width,
657                                 'media:height' => $alink->height,
658                                 'href'         => $alink->url,
659                                 ),
660                             null);
661                 }
662             }
663
664             if (!empty($this->geopoint)) {
665                 $xo->element(
666                     'georss:point',
667                     null,
668                     $this->geopoint
669                 );
670             }
671
672             if (!empty($this->poco)) {
673                 $this->poco->outputTo($xo);
674             }
675
676             // @fixme there's no way here to make a tree; elements can only contain plaintext
677             // @fixme these may collide with JSON extensions
678             foreach ($this->extra as $el) {
679                 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
680                 $xo->element($extraTag, $attrs, $content);
681             }
682
683             Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
684         }
685
686         if (!empty($tag)) {
687             $xo->elementEnd($tag);
688         }
689
690         return;
691     }
692
693     function asString($tag='activity:object')
694     {
695         $xs = new XMLStringer(true);
696
697         $this->outputTo($xs, $tag);
698
699         return $xs->getString();
700     }
701
702     /*
703      * Returns an array based on this Activity Object suitable for
704      * encoding as JSON.
705      *
706      * @return array $object the activity object array
707      */
708
709     function asArray()
710     {
711         $object = array();
712
713         if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
714             // XXX: attachments are added by Activity
715
716             // author (Add object for author? Could be useful for repeats.)
717
718             // content (Add rendered version of the notice?)
719
720             // downstreamDuplicates
721
722             // id
723
724             if ($this->id) {
725                 $object['id'] = $this->id;
726             } else if ($this->link) {
727                 $object['id'] = $this->link;
728             }
729
730             if ($this->type == ActivityObject::PERSON
731                 || $this->type == ActivityObject::GROUP) {
732
733                 // displayName
734                 $object['displayName'] = $this->title;
735
736                 // XXX: Not sure what the best avatar is to use for the
737                 // author's "image". For now, I'm using the large size.
738
739                 $imgLink          = null;
740                 $avatarMediaLinks = array();
741
742                 foreach ($this->avatarLinks as $a) {
743
744                     // Make a MediaLink for every other Avatar
745                     $avatar = new ActivityStreamsMediaLink(
746                         $a->url,
747                         $a->width,
748                         $a->height,
749                         $a->type,
750                         'avatar'
751                     );
752
753                     // Find the big avatar to use as the "image"
754                     if ($a->height == AVATAR_PROFILE_SIZE) {
755                         $imgLink = $avatar;
756                     }
757
758                     $avatarMediaLinks[] = $avatar->asArray();
759                 }
760
761                 if (!array_key_exists('status_net', $object)) {
762                     $object['status_net'] = array();
763                 }
764
765                 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
766
767                 // image
768                 if (!empty($imgLink)) {
769                     $object['image']  = $imgLink->asArray();
770                 }
771             }
772
773             // objectType
774             //
775             // We can probably use the whole schema URL here but probably the
776             // relative simple name is easier to parse
777
778             $object['objectType'] = self::canonicalType($this->type);
779
780             // summary
781             $object['summary'] = $this->summary;
782
783             // content, usually rendered HTML
784             $object['content'] = $this->content;
785
786             // published (probably don't need. Might be useful for repeats.)
787
788             // updated (probably don't need this)
789
790             // TODO: upstreamDuplicates
791
792             if ($this->link) {
793                 $object['url'] = $this->link;
794             }
795
796             /* Extensions */
797             // @fixme these may collide with XML extensions
798             // @fixme multiple tags of same name will overwrite each other
799             // @fixme text content from XML extensions will be lost
800
801             foreach ($this->extra as $e) {
802                 list($objectName, $props, $txt) = array_pad($e, 3, null);
803                 if (!empty($objectName)) {
804                     $parts = explode(":", $objectName);
805                     if (count($parts) == 2 && $parts[0] == "statusnet") {
806                         if (!array_key_exists('status_net', $object)) {
807                             $object['status_net'] = array();
808                         }
809                         $object['status_net'][$parts[1]] = $props;
810                     } else {
811                         $object[$objectName] = $props;
812                     }
813                 }
814             }
815
816             if (!empty($this->geopoint)) {
817
818                 list($lat, $lon) = explode(' ', $this->geopoint);
819
820                 if (!empty($lat) && !empty($lon)) {
821                     $object['location'] = array(
822                         'objectType' => 'place',
823                         'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
824                         'lat' => $lat,
825                         'lon' => $lon
826                     );
827
828                     $loc = Location::fromLatLon((float)$lat, (float)$lon);
829
830                     if ($loc) {
831                         $name = $loc->getName();
832
833                         if ($name) {
834                             $object['location']['displayName'] = $name;
835                         }
836                         $url = $loc->getURL();
837
838                         if ($url) {
839                             $object['location']['url'] = $url;
840                         }
841                     }
842                 }
843             }
844
845             if (!empty($this->poco)) {
846                 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
847             }
848
849             if (!empty($this->thumbnail)) {
850                 if (is_string($this->thumbnail)) {
851                     $object['image'] = array('url' => $this->thumbnail);
852                 } else {
853                     $object['image'] = array('url' => $this->thumbnail->url);
854                     if ($this->thumbnail->width) {
855                         $object['image']['width'] = $this->thumbnail->width;
856                     }
857                     if ($this->thumbnail->height) {
858                         $object['image']['height'] = $this->thumbnail->height;
859                     }
860                 }
861             }
862
863             switch (self::canonicalType($this->type)) {
864             case 'image':
865                 if (!empty($this->largerImage)) {
866                     $object['fullImage'] = array('url' => $this->largerImage);
867                 }
868                 break;
869             case 'audio':
870             case 'video':
871                 if (!empty($this->stream)) {
872                     $object['stream'] = array('url' => $this->stream);
873                 }
874                 break;
875             }
876
877             Event::handle('EndActivityObjectOutputJson', array($this, &$object));
878         }
879         return array_filter($object);
880     }
881
882     public function getIdentifiers() {
883         $ids = array();
884         foreach(array('id', 'link', 'url') as $id) {
885             if (isset($this->$id)) {
886                 $ids[] = $this->$id;
887             }
888         }
889         return array_unique($ids);
890     }
891
892     static function canonicalType($type) {
893         return ActivityUtils::resolveUri($type, true);
894     }
895
896     static function mimeTypeToObjectType($mimeType) {
897         $ot = null;
898
899         // Default
900
901         if (empty($mimeType)) {
902             return self::FILE;
903         }
904
905         $parts = explode('/', $mimeType);
906
907         switch ($parts[0]) {
908         case 'image':
909             $ot = self::IMAGE;
910             break;
911         case 'audio':
912             $ot = self::AUDIO;
913             break;
914         case 'video':
915             $ot = self::VIDEO;
916             break;
917         default:
918             $ot = self::FILE;
919         }
920
921         return $ot;
922     }
923 }