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