]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
OStatus: do PuSH subscription setup from subscribe/join event hooks, so resubscribing...
[quix0rs-gnu-social.git] / lib / activity.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 class PoCoURL
36 {
37     const TYPE      = 'type';
38     const VALUE     = 'value';
39     const PRIMARY   = 'primary';
40
41     public $type;
42     public $value;
43     public $primary;
44
45     function __construct($type, $value, $primary = false)
46     {
47         $this->type    = $type;
48         $this->value   = $value;
49         $this->primary = $primary;
50     }
51
52     function asString()
53     {
54         $xs = new XMLStringer(true);
55         $xs->elementStart('poco:urls');
56         $xs->element('poco:type', null, $this->type);
57         $xs->element('poco:value', null, $this->value);
58         if ($this->primary) {
59             $xs->element('poco:primary', null, 'true');
60         }
61         $xs->elementEnd('poco:urls');
62         return $xs->getString();
63     }
64 }
65
66 class PoCoAddress
67 {
68     const ADDRESS   = 'address';
69     const FORMATTED = 'formatted';
70
71     public $formatted;
72
73     function __construct($formatted)
74     {
75         if (empty($formatted)) {
76             return null;
77         }
78         $this->formatted = $formatted;
79     }
80
81     function asString()
82     {
83         $xs = new XMLStringer(true);
84         $xs->elementStart('poco:address');
85         $xs->element('poco:formatted', null, $this->formatted);
86         $xs->elementEnd('poco:address');
87         return $xs->getString();
88     }
89 }
90
91 class PoCo
92 {
93     const NS = 'http://portablecontacts.net/spec/1.0';
94
95     const USERNAME  = 'preferredUsername';
96     const NOTE      = 'note';
97     const URLS      = 'urls';
98
99     public $preferredUsername;
100     public $note;
101     public $address;
102     public $urls = array();
103
104     function __construct($profile)
105     {
106         $this->preferredUsername = $profile->nickname;
107
108         $this->note    = $profile->bio;
109         $this->address = new PoCoAddress($profile->location);
110
111         if (!empty($profile->homepage)) {
112             array_push(
113                 $this->urls,
114                 new PoCoURL(
115                     'homepage',
116                     $profile->homepage,
117                     true
118                 )
119             );
120         }
121     }
122
123     function asString()
124     {
125         $xs = new XMLStringer(true);
126         $xs->element(
127             'poco:preferredUsername',
128             null,
129             $this->preferredUsername
130         );
131
132         if (!empty($this->note)) {
133             $xs->element('poco:note', null, $this->note);
134         }
135
136         if (!empty($this->address)) {
137             $xs->raw($this->address->asString());
138         }
139
140         foreach ($this->urls as $url) {
141             $xs->raw($url->asString());
142         }
143
144         return $xs->getString();
145     }
146 }
147
148 /**
149  * Utilities for turning DOMish things into Activityish things
150  *
151  * Some common functions that I didn't have the bandwidth to try to factor
152  * into some kind of reasonable superclass, so just dumped here. Might
153  * be useful to have an ActivityObject parent class or something.
154  *
155  * @category  OStatus
156  * @package   StatusNet
157  * @author    Evan Prodromou <evan@status.net>
158  * @copyright 2010 StatusNet, Inc.
159  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
160  * @link      http://status.net/
161  */
162
163 class ActivityUtils
164 {
165     const ATOM = 'http://www.w3.org/2005/Atom';
166
167     const LINK = 'link';
168     const REL  = 'rel';
169     const TYPE = 'type';
170     const HREF = 'href';
171
172     const CONTENT = 'content';
173     const SRC     = 'src';
174
175     /**
176      * Get the permalink for an Activity object
177      *
178      * @param DOMElement $element A DOM element
179      *
180      * @return string related link, if any
181      */
182
183     static function getPermalink($element)
184     {
185         return self::getLink($element, 'alternate', 'text/html');
186     }
187
188     /**
189      * Get the permalink for an Activity object
190      *
191      * @param DOMElement $element A DOM element
192      *
193      * @return string related link, if any
194      */
195
196     static function getLink($element, $rel, $type=null)
197     {
198         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
199
200         foreach ($links as $link) {
201
202             $linkRel = $link->getAttribute(self::REL);
203             $linkType = $link->getAttribute(self::TYPE);
204
205             if ($linkRel == $rel &&
206                 (is_null($type) || $linkType == $type)) {
207                 return $link->getAttribute(self::HREF);
208             }
209         }
210
211         return null;
212     }
213
214     /**
215      * Gets the first child element with the given tag
216      *
217      * @param DOMElement $element   element to pick at
218      * @param string     $tag       tag to look for
219      * @param string     $namespace Namespace to look under
220      *
221      * @return DOMElement found element or null
222      */
223
224     static function child($element, $tag, $namespace=self::ATOM)
225     {
226         $els = $element->childNodes;
227         if (empty($els) || $els->length == 0) {
228             return null;
229         } else {
230             for ($i = 0; $i < $els->length; $i++) {
231                 $el = $els->item($i);
232                 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
233                     return $el;
234                 }
235             }
236         }
237     }
238
239     /**
240      * Grab the text content of a DOM element child of the current element
241      *
242      * @param DOMElement $element   Element whose children we examine
243      * @param string     $tag       Tag to look up
244      * @param string     $namespace Namespace to use, defaults to Atom
245      *
246      * @return string content of the child
247      */
248
249     static function childContent($element, $tag, $namespace=self::ATOM)
250     {
251         $el = self::child($element, $tag, $namespace);
252
253         if (empty($el)) {
254             return null;
255         } else {
256             return $el->textContent;
257         }
258     }
259
260     /**
261      * Get the content of an atom:entry-like object
262      *
263      * @param DOMElement $element The element to examine.
264      *
265      * @return string unencoded HTML content of the element, like "This -&lt; is <b>HTML</b>."
266      *
267      * @todo handle remote content
268      * @todo handle embedded XML mime types
269      * @todo handle base64-encoded non-XML and non-text mime types
270      */
271
272     static function getContent($element)
273     {
274         $contentEl = ActivityUtils::child($element, self::CONTENT);
275
276         if (!empty($contentEl)) {
277
278             $src  = $contentEl->getAttribute(self::SRC);
279
280             if (!empty($src)) {
281                 throw new ClientException(_("Can't handle remote content yet."));
282             }
283
284             $type = $contentEl->getAttribute(self::TYPE);
285
286             // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
287
288             if ($type == 'text') {
289                 return $contentEl->textContent;
290             } else if ($type == 'html') {
291                 $text = $contentEl->textContent;
292                 return htmlspecialchars_decode($text, ENT_QUOTES);
293             } else if ($type == 'xhtml') {
294                 $divEl = ActivityUtils::child($contentEl, 'div');
295                 if (empty($divEl)) {
296                     return null;
297                 }
298                 $doc = $divEl->ownerDocument;
299                 $text = '';
300                 $children = $divEl->childNodes;
301
302                 for ($i = 0; $i < $children->length; $i++) {
303                     $child = $children->item($i);
304                     $text .= $doc->saveXML($child);
305                 }
306                 return trim($text);
307             } else if (in_array(array('text/xml', 'application/xml'), $type) ||
308                        preg_match('#(+|/)xml$#', $type)) {
309                 throw new ClientException(_("Can't handle embedded XML content yet."));
310             } else if (strncasecmp($type, 'text/', 5)) {
311                 return $contentEl->textContent;
312             } else {
313                 throw new ClientException(_("Can't handle embedded Base64 content yet."));
314             }
315         }
316     }
317 }
318
319 /**
320  * A noun-ish thing in the activity universe
321  *
322  * The activity streams spec talks about activity objects, while also having
323  * a tag activity:object, which is in fact an activity object. Aaaaaah!
324  *
325  * This is just a thing in the activity universe. Can be the subject, object,
326  * or indirect object (target!) of an activity verb. Rotten name, and I'm
327  * propagating it. *sigh*
328  *
329  * @category  OStatus
330  * @package   StatusNet
331  * @author    Evan Prodromou <evan@status.net>
332  * @copyright 2010 StatusNet, Inc.
333  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
334  * @link      http://status.net/
335  */
336
337 class ActivityObject
338 {
339     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
340     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
341     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
342     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
343     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
344     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
345     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
346     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
347     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
348     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
349     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
350     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
351     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
352     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
353     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
354     // ^^^^^^^^^^ tea!
355
356     // Atom elements we snarf
357
358     const TITLE   = 'title';
359     const SUMMARY = 'summary';
360     const ID      = 'id';
361     const SOURCE  = 'source';
362
363     const NAME  = 'name';
364     const URI   = 'uri';
365     const EMAIL = 'email';
366
367     public $element;
368     public $type;
369     public $id;
370     public $title;
371     public $summary;
372     public $content;
373     public $link;
374     public $source;
375     public $avatar;
376     public $geopoint;
377
378     /**
379      * Constructor
380      *
381      * This probably needs to be refactored
382      * to generate a local class (ActivityPerson, ActivityFile, ...)
383      * based on the object type.
384      *
385      * @param DOMElement $element DOM thing to turn into an Activity thing
386      */
387
388     function __construct($element = null)
389     {
390         if (empty($element)) {
391             return;
392         }
393
394         $this->element = $element;
395
396         if ($element->tagName == 'author') {
397
398             $this->type  = self::PERSON; // XXX: is this fair?
399             $this->title = $this->_childContent($element, self::NAME);
400             $this->id    = $this->_childContent($element, self::URI);
401
402             if (empty($this->id)) {
403                 $email = $this->_childContent($element, self::EMAIL);
404                 if (!empty($email)) {
405                     // XXX: acct: ?
406                     $this->id = 'mailto:'.$email;
407                 }
408             }
409
410         } else {
411
412             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
413                                                Activity::SPEC);
414
415             if (empty($this->type)) {
416                 $this->type = ActivityObject::NOTE;
417             }
418
419             $this->id      = $this->_childContent($element, self::ID);
420             $this->title   = $this->_childContent($element, self::TITLE);
421             $this->summary = $this->_childContent($element, self::SUMMARY);
422
423             $this->source  = $this->_getSource($element);
424
425             $this->content = ActivityUtils::getContent($element);
426
427             $this->link = ActivityUtils::getPermalink($element);
428
429             // XXX: grab PoCo stuff
430         }
431
432         // Some per-type attributes...
433         if ($this->type == self::PERSON || $this->type == self::GROUP) {
434             $this->displayName = $this->title;
435
436             // @fixme we may have multiple avatars with different resolutions specified
437             $this->avatar   = ActivityUtils::getLink($element, 'avatar');
438             $this->nickname = ActivityUtils::childContent($element, PoCo::USERNAME, PoCo::NS);
439         }
440     }
441
442     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
443     {
444         return ActivityUtils::childContent($element, $tag, $namespace);
445     }
446
447     // Try to get a unique id for the source feed
448
449     private function _getSource($element)
450     {
451         $sourceEl = ActivityUtils::child($element, 'source');
452
453         if (empty($sourceEl)) {
454             return null;
455         } else {
456             $href = ActivityUtils::getLink($sourceEl, 'self');
457             if (!empty($href)) {
458                 return $href;
459             } else {
460                 return ActivityUtils::childContent($sourceEl, 'id');
461             }
462         }
463     }
464
465     static function fromNotice($notice)
466     {
467         $object = new ActivityObject();
468
469         $object->type    = ActivityObject::NOTE;
470
471         $object->id      = $notice->uri;
472         $object->title   = $notice->content;
473         $object->content = $notice->rendered;
474         $object->link    = $notice->bestUrl();
475
476         return $object;
477     }
478
479     static function fromProfile($profile)
480     {
481         $object = new ActivityObject();
482
483         $object->type   = ActivityObject::PERSON;
484         $object->id     = $profile->getUri();
485         $object->title  = $profile->getBestName();
486         $object->link   = $profile->profileurl;
487         $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
488
489         if (isset($profile->lat) && isset($profile->lon)) {
490             $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
491         }
492
493         $object->poco = new PoCo($profile);
494
495         return $object;
496     }
497
498     function asString($tag='activity:object')
499     {
500         $xs = new XMLStringer(true);
501
502         $xs->elementStart($tag);
503
504         $xs->element('activity:object-type', null, $this->type);
505
506         $xs->element(self::ID, null, $this->id);
507
508         if (!empty($this->title)) {
509             $xs->element(self::TITLE, null, $this->title);
510         }
511
512         if (!empty($this->summary)) {
513             $xs->element(self::SUMMARY, null, $this->summary);
514         }
515
516         if (!empty($this->content)) {
517             // XXX: assuming HTML content here
518             $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
519         }
520
521         if (!empty($this->link)) {
522             $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
523                          $this->link);
524         }
525
526         if ($this->type == ActivityObject::PERSON) {
527             $xs->element(
528                 'link', array(
529                     'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
530                     'rel'  => 'avatar',
531                     'href' => empty($this->avatar)
532                     ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
533                     : $this->avatar->displayUrl()
534                 ),
535                 ''
536             );
537         }
538
539         if (!empty($this->geopoint)) {
540             $xs->element(
541                 'georss:point',
542                 null,
543                 $this->geopoint
544             );
545         }
546
547         if (!empty($this->poco)) {
548             $xs->raw($this->poco->asString());
549         }
550
551         $xs->elementEnd($tag);
552
553         return $xs->getString();
554     }
555 }
556
557 /**
558  * Utility class to hold a bunch of constant defining default verb types
559  *
560  * @category  OStatus
561  * @package   StatusNet
562  * @author    Evan Prodromou <evan@status.net>
563  * @copyright 2010 StatusNet, Inc.
564  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
565  * @link      http://status.net/
566  */
567
568 class ActivityVerb
569 {
570     const POST     = 'http://activitystrea.ms/schema/1.0/post';
571     const SHARE    = 'http://activitystrea.ms/schema/1.0/share';
572     const SAVE     = 'http://activitystrea.ms/schema/1.0/save';
573     const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
574     const PLAY     = 'http://activitystrea.ms/schema/1.0/play';
575     const FOLLOW   = 'http://activitystrea.ms/schema/1.0/follow';
576     const FRIEND   = 'http://activitystrea.ms/schema/1.0/make-friend';
577     const JOIN     = 'http://activitystrea.ms/schema/1.0/join';
578     const TAG      = 'http://activitystrea.ms/schema/1.0/tag';
579
580     // Custom OStatus verbs for the flipside until they're standardized
581     const DELETE     = 'http://ostatus.org/schema/1.0/unfollow';
582     const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
583     const UNFOLLOW   = 'http://ostatus.org/schema/1.0/unfollow';
584     const LEAVE      = 'http://ostatus.org/schema/1.0/leave';
585 }
586
587 class ActivityContext
588 {
589     public $replyToID;
590     public $replyToUrl;
591     public $location;
592     public $attention = array();
593     public $conversation;
594
595     const THR     = 'http://purl.org/syndication/thread/1.0';
596     const GEORSS  = 'http://www.georss.org/georss';
597     const OSTATUS = 'http://ostatus.org/schema/1.0';
598
599     const INREPLYTO = 'in-reply-to';
600     const REF       = 'ref';
601     const HREF      = 'href';
602
603     const POINT     = 'point';
604
605     const ATTENTION    = 'ostatus:attention';
606     const CONVERSATION = 'ostatus:conversation';
607
608     function __construct($element)
609     {
610         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
611
612         if (!empty($replyToEl)) {
613             $this->replyToID  = $replyToEl->getAttribute(self::REF);
614             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
615         }
616
617         $this->location = $this->getLocation($element);
618
619         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
620
621         // Multiple attention links allowed
622
623         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
624
625         for ($i = 0; $i < $links->length; $i++) {
626
627             $link = $links->item($i);
628
629             $linkRel = $link->getAttribute(ActivityUtils::REL);
630
631             if ($linkRel == self::ATTENTION) {
632                 $this->attention[] = $link->getAttribute(self::HREF);
633             }
634         }
635     }
636
637     /**
638      * Parse location given as a GeoRSS-simple point, if provided.
639      * http://www.georss.org/simple
640      *
641      * @param feed item $entry
642      * @return mixed Location or false
643      */
644     function getLocation($dom)
645     {
646         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
647
648         for ($i = 0; $i < $points->length; $i++) {
649             $point = $points->item($i)->textContent;
650             $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
651             $point = preg_replace('/\s+/', ' ', $point);
652             $point = trim($point);
653             $coords = explode(' ', $point);
654             if (count($coords) == 2) {
655                 list($lat, $lon) = $coords;
656                 if (is_numeric($lat) && is_numeric($lon)) {
657                     common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
658                     return Location::fromLatLon($lat, $lon);
659                 }
660             }
661             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
662         }
663
664         return null;
665     }
666 }
667
668 /**
669  * An activity in the ActivityStrea.ms world
670  *
671  * An activity is kind of like a sentence: someone did something
672  * to something else.
673  *
674  * 'someone' is the 'actor'; 'did something' is the verb;
675  * 'something else' is the object.
676  *
677  * @category  OStatus
678  * @package   StatusNet
679  * @author    Evan Prodromou <evan@status.net>
680  * @copyright 2010 StatusNet, Inc.
681  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
682  * @link      http://status.net/
683  */
684
685 class Activity
686 {
687     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
688     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
689
690     const VERB       = 'verb';
691     const OBJECT     = 'object';
692     const ACTOR      = 'actor';
693     const SUBJECT    = 'subject';
694     const OBJECTTYPE = 'object-type';
695     const CONTEXT    = 'context';
696     const TARGET     = 'target';
697
698     const ATOM = 'http://www.w3.org/2005/Atom';
699
700     const AUTHOR    = 'author';
701     const PUBLISHED = 'published';
702     const UPDATED   = 'updated';
703
704     public $actor;   // an ActivityObject
705     public $verb;    // a string (the URL)
706     public $object;  // an ActivityObject
707     public $target;  // an ActivityObject
708     public $context; // an ActivityObject
709     public $time;    // Time of the activity
710     public $link;    // an ActivityObject
711     public $entry;   // the source entry
712     public $feed;    // the source feed
713
714     public $summary; // summary of activity
715     public $content; // HTML content of activity
716     public $id;      // ID of the activity
717     public $title;   // title of the activity
718
719     /**
720      * Turns a regular old Atom <entry> into a magical activity
721      *
722      * @param DOMElement $entry Atom entry to poke at
723      * @param DOMElement $feed  Atom feed, for context
724      */
725
726     function __construct($entry = null, $feed = null)
727     {
728         if (is_null($entry)) {
729             return;
730         }
731
732         $this->entry = $entry;
733         $this->feed  = $feed;
734
735         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
736
737         if (!empty($pubEl)) {
738             $this->time = strtotime($pubEl->textContent);
739         } else {
740             // XXX technically an error; being liberal. Good idea...?
741             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
742             if (!empty($updateEl)) {
743                 $this->time = strtotime($updateEl->textContent);
744             } else {
745                 $this->time = null;
746             }
747         }
748
749         $this->link = ActivityUtils::getPermalink($entry);
750
751         $verbEl = $this->_child($entry, self::VERB);
752
753         if (!empty($verbEl)) {
754             $this->verb = trim($verbEl->textContent);
755         } else {
756             $this->verb = ActivityVerb::POST;
757             // XXX: do other implied stuff here
758         }
759
760         $objectEl = $this->_child($entry, self::OBJECT);
761
762         if (!empty($objectEl)) {
763             $this->object = new ActivityObject($objectEl);
764         } else {
765             $this->object = new ActivityObject($entry);
766         }
767
768         $actorEl = $this->_child($entry, self::ACTOR);
769
770         if (!empty($actorEl)) {
771
772             $this->actor = new ActivityObject($actorEl);
773
774         } else if (!empty($feed) &&
775                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
776
777             $this->actor = new ActivityObject($subjectEl);
778
779         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
780
781             $this->actor = new ActivityObject($authorEl);
782
783         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
784                                                               self::ATOM)) {
785
786             $this->actor = new ActivityObject($authorEl);
787         }
788
789         $contextEl = $this->_child($entry, self::CONTEXT);
790
791         if (!empty($contextEl)) {
792             $this->context = new ActivityContext($contextEl);
793         } else {
794             $this->context = new ActivityContext($entry);
795         }
796
797         $targetEl = $this->_child($entry, self::TARGET);
798
799         if (!empty($targetEl)) {
800             $this->target = new ActivityObject($targetEl);
801         }
802
803         $this->summary = ActivityUtils::childContent($entry, 'summary');
804         $this->id      = ActivityUtils::childContent($entry, 'id');
805         $this->content = ActivityUtils::getContent($entry);
806     }
807
808     /**
809      * Returns an Atom <entry> based on this activity
810      *
811      * @return DOMElement Atom entry
812      */
813
814     function toAtomEntry()
815     {
816         return null;
817     }
818
819     function asString($namespace=false)
820     {
821         $xs = new XMLStringer(true);
822
823         if ($namespace) {
824             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
825                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
826                            'xmlns:georss' => 'http://www.georss.org/georss',
827                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
828                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0');
829         } else {
830             $attrs = array();
831         }
832
833         $xs->elementStart('entry', $attrs);
834
835         $xs->element('id', null, $this->id);
836         $xs->element('title', null, $this->title);
837         $xs->element('published', null, common_date_iso8601($this->time));
838         $xs->element('content', array('type' => 'html'), $this->content);
839
840         if (!empty($this->summary)) {
841             $xs->element('summary', null, $this->summary);
842         }
843
844         if (!empty($this->link)) {
845             $xs->element('link', array('rel' => 'alternate',
846                                        'type' => 'text/html'),
847                          $this->link);
848         }
849
850         // XXX: add context
851         // XXX: add target
852
853         $xs->raw($this->actor->asString('activity:actor'));
854         $xs->element('activity:verb', null, $this->verb);
855         $xs->raw($this->object->asString());
856
857         $xs->elementEnd('entry');
858
859         return $xs->getString();
860     }
861
862     private function _child($element, $tag, $namespace=self::SPEC)
863     {
864         return ActivityUtils::child($element, $tag, $namespace);
865     }
866 }