]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
0863cf8fa77b1e5498968b18c7f3cf5f43a90047
[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 fromGroup($group)
227     {
228         if (empty($group)) {
229             return null;
230         }
231
232         $poco = new PoCo();
233
234         $poco->preferredUsername = $group->nickname;
235         $poco->displayName       = $group->getBestName();
236
237         $poco->note = $group->description;
238
239         $paddy = new PoCoAddress();
240         $paddy->formatted = $group->location;
241         $poco->address = $paddy;
242
243         if (!empty($group->homepage)) {
244             array_push(
245                 $poco->urls,
246                 new PoCoURL(
247                     'homepage',
248                     $group->homepage,
249                     true
250                 )
251             );
252         }
253
254         return $poco;
255     }
256
257     function getPrimaryURL()
258     {
259         foreach ($this->urls as $url) {
260             if ($url->primary) {
261                 return $url;
262             }
263         }
264     }
265
266     function asString()
267     {
268         $xs = new XMLStringer(true);
269         $xs->element(
270             'poco:preferredUsername',
271             null,
272             $this->preferredUsername
273         );
274
275         $xs->element(
276             'poco:displayName',
277             null,
278             $this->displayName
279         );
280
281         if (!empty($this->note)) {
282             $xs->element('poco:note', null, $this->note);
283         }
284
285         if (!empty($this->address)) {
286             $xs->raw($this->address->asString());
287         }
288
289         foreach ($this->urls as $url) {
290             $xs->raw($url->asString());
291         }
292
293         return $xs->getString();
294     }
295 }
296
297 /**
298  * Utilities for turning DOMish things into Activityish things
299  *
300  * Some common functions that I didn't have the bandwidth to try to factor
301  * into some kind of reasonable superclass, so just dumped here. Might
302  * be useful to have an ActivityObject parent class or something.
303  *
304  * @category  OStatus
305  * @package   StatusNet
306  * @author    Evan Prodromou <evan@status.net>
307  * @copyright 2010 StatusNet, Inc.
308  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
309  * @link      http://status.net/
310  */
311
312 class ActivityUtils
313 {
314     const ATOM = 'http://www.w3.org/2005/Atom';
315
316     const LINK = 'link';
317     const REL  = 'rel';
318     const TYPE = 'type';
319     const HREF = 'href';
320
321     const CONTENT = 'content';
322     const SRC     = 'src';
323
324     /**
325      * Get the permalink for an Activity object
326      *
327      * @param DOMElement $element A DOM element
328      *
329      * @return string related link, if any
330      */
331
332     static function getPermalink($element)
333     {
334         return self::getLink($element, 'alternate', 'text/html');
335     }
336
337     /**
338      * Get the permalink for an Activity object
339      *
340      * @param DOMElement $element A DOM element
341      *
342      * @return string related link, if any
343      */
344
345     static function getLink(DOMNode $element, $rel, $type=null)
346     {
347         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
348
349         foreach ($links as $link) {
350
351             $linkRel = $link->getAttribute(self::REL);
352             $linkType = $link->getAttribute(self::TYPE);
353
354             if ($linkRel == $rel &&
355                 (is_null($type) || $linkType == $type)) {
356                 return $link->getAttribute(self::HREF);
357             }
358         }
359
360         return null;
361     }
362
363     /**
364      * Gets the first child element with the given tag
365      *
366      * @param DOMElement $element   element to pick at
367      * @param string     $tag       tag to look for
368      * @param string     $namespace Namespace to look under
369      *
370      * @return DOMElement found element or null
371      */
372
373     static function child(DOMNode $element, $tag, $namespace=self::ATOM)
374     {
375         $els = $element->childNodes;
376         if (empty($els) || $els->length == 0) {
377             return null;
378         } else {
379             for ($i = 0; $i < $els->length; $i++) {
380                 $el = $els->item($i);
381                 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
382                     return $el;
383                 }
384             }
385         }
386     }
387
388     /**
389      * Grab the text content of a DOM element child of the current element
390      *
391      * @param DOMElement $element   Element whose children we examine
392      * @param string     $tag       Tag to look up
393      * @param string     $namespace Namespace to use, defaults to Atom
394      *
395      * @return string content of the child
396      */
397
398     static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
399     {
400         $el = self::child($element, $tag, $namespace);
401
402         if (empty($el)) {
403             return null;
404         } else {
405             return $el->textContent;
406         }
407     }
408
409     /**
410      * Get the content of an atom:entry-like object
411      *
412      * @param DOMElement $element The element to examine.
413      *
414      * @return string unencoded HTML content of the element, like "This -&lt; is <b>HTML</b>."
415      *
416      * @todo handle remote content
417      * @todo handle embedded XML mime types
418      * @todo handle base64-encoded non-XML and non-text mime types
419      */
420
421     static function getContent($element)
422     {
423         $contentEl = ActivityUtils::child($element, self::CONTENT);
424
425         if (!empty($contentEl)) {
426
427             $src  = $contentEl->getAttribute(self::SRC);
428
429             if (!empty($src)) {
430                 throw new ClientException(_("Can't handle remote content yet."));
431             }
432
433             $type = $contentEl->getAttribute(self::TYPE);
434
435             // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
436
437             if ($type == 'text') {
438                 return $contentEl->textContent;
439             } else if ($type == 'html') {
440                 $text = $contentEl->textContent;
441                 return htmlspecialchars_decode($text, ENT_QUOTES);
442             } else if ($type == 'xhtml') {
443                 $divEl = ActivityUtils::child($contentEl, 'div');
444                 if (empty($divEl)) {
445                     return null;
446                 }
447                 $doc = $divEl->ownerDocument;
448                 $text = '';
449                 $children = $divEl->childNodes;
450
451                 for ($i = 0; $i < $children->length; $i++) {
452                     $child = $children->item($i);
453                     $text .= $doc->saveXML($child);
454                 }
455                 return trim($text);
456             } else if (in_array(array('text/xml', 'application/xml'), $type) ||
457                        preg_match('#(+|/)xml$#', $type)) {
458                 throw new ClientException(_("Can't handle embedded XML content yet."));
459             } else if (strncasecmp($type, 'text/', 5)) {
460                 return $contentEl->textContent;
461             } else {
462                 throw new ClientException(_("Can't handle embedded Base64 content yet."));
463             }
464         }
465     }
466 }
467
468 /**
469  * A noun-ish thing in the activity universe
470  *
471  * The activity streams spec talks about activity objects, while also having
472  * a tag activity:object, which is in fact an activity object. Aaaaaah!
473  *
474  * This is just a thing in the activity universe. Can be the subject, object,
475  * or indirect object (target!) of an activity verb. Rotten name, and I'm
476  * propagating it. *sigh*
477  *
478  * @category  OStatus
479  * @package   StatusNet
480  * @author    Evan Prodromou <evan@status.net>
481  * @copyright 2010 StatusNet, Inc.
482  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
483  * @link      http://status.net/
484  */
485
486 class ActivityObject
487 {
488     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
489     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
490     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
491     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
492     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
493     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
494     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
495     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
496     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
497     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
498     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
499     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
500     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
501     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
502     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
503     // ^^^^^^^^^^ tea!
504
505     // Atom elements we snarf
506
507     const TITLE   = 'title';
508     const SUMMARY = 'summary';
509     const ID      = 'id';
510     const SOURCE  = 'source';
511
512     const NAME  = 'name';
513     const URI   = 'uri';
514     const EMAIL = 'email';
515
516     public $element;
517     public $type;
518     public $id;
519     public $title;
520     public $summary;
521     public $content;
522     public $link;
523     public $source;
524     public $avatar;
525     public $geopoint;
526     public $poco;
527     public $displayName;
528
529     /**
530      * Constructor
531      *
532      * This probably needs to be refactored
533      * to generate a local class (ActivityPerson, ActivityFile, ...)
534      * based on the object type.
535      *
536      * @param DOMElement $element DOM thing to turn into an Activity thing
537      */
538
539     function __construct($element = null)
540     {
541         if (empty($element)) {
542             return;
543         }
544
545         $this->element = $element;
546
547         $this->geopoint = $this->_childContent(
548             $element,
549             ActivityContext::POINT,
550             ActivityContext::GEORSS
551         );
552
553         if ($element->tagName == 'author') {
554
555             $this->type  = self::PERSON; // XXX: is this fair?
556             $this->title = $this->_childContent($element, self::NAME);
557             $this->id    = $this->_childContent($element, self::URI);
558
559             if (empty($this->id)) {
560                 $email = $this->_childContent($element, self::EMAIL);
561                 if (!empty($email)) {
562                     // XXX: acct: ?
563                     $this->id = 'mailto:'.$email;
564                 }
565             }
566
567         } else {
568
569             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
570                                                Activity::SPEC);
571
572             if (empty($this->type)) {
573                 $this->type = ActivityObject::NOTE;
574             }
575
576             $this->id      = $this->_childContent($element, self::ID);
577             $this->title   = $this->_childContent($element, self::TITLE);
578             $this->summary = $this->_childContent($element, self::SUMMARY);
579
580             $this->source  = $this->_getSource($element);
581
582             $this->content = ActivityUtils::getContent($element);
583
584             $this->link = ActivityUtils::getPermalink($element);
585
586         }
587
588         // Some per-type attributes...
589         if ($this->type == self::PERSON || $this->type == self::GROUP) {
590             $this->displayName = $this->title;
591
592             // @fixme we may have multiple avatars with different resolutions specified
593             $this->avatar = ActivityUtils::getLink($element, 'avatar');
594
595             $this->poco = new PoCo($element);
596         }
597     }
598
599     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
600     {
601         return ActivityUtils::childContent($element, $tag, $namespace);
602     }
603
604     // Try to get a unique id for the source feed
605
606     private function _getSource($element)
607     {
608         $sourceEl = ActivityUtils::child($element, 'source');
609
610         if (empty($sourceEl)) {
611             return null;
612         } else {
613             $href = ActivityUtils::getLink($sourceEl, 'self');
614             if (!empty($href)) {
615                 return $href;
616             } else {
617                 return ActivityUtils::childContent($sourceEl, 'id');
618             }
619         }
620     }
621
622     static function fromNotice($notice)
623     {
624         $object = new ActivityObject();
625
626         $object->type    = ActivityObject::NOTE;
627
628         $object->id      = $notice->uri;
629         $object->title   = $notice->content;
630         $object->content = $notice->rendered;
631         $object->link    = $notice->bestUrl();
632
633         return $object;
634     }
635
636     static function fromProfile($profile)
637     {
638         $object = new ActivityObject();
639
640         $object->type   = ActivityObject::PERSON;
641         $object->id     = $profile->getUri();
642         $object->title  = $profile->getBestName();
643         $object->link   = $profile->profileurl;
644         $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
645
646         if (isset($profile->lat) && isset($profile->lon)) {
647             $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
648         }
649
650         $object->poco = PoCo::fromProfile($profile);
651
652         return $object;
653     }
654
655     static function fromGroup($group)
656     {
657         $object = new ActivityObject();
658
659         $object->type   = ActivityObject::GROUP;
660         $object->id     = $group->getUri();
661         $object->title  = $group->getBestName();
662         $object->link   = $group->getUri();
663         $object->avatar = $group->getAvatar();
664
665         $object->poco = PoCo::fromGroup($group);
666
667         return $object;
668     }
669
670     function asString($tag='activity:object')
671     {
672         $xs = new XMLStringer(true);
673
674         $xs->elementStart($tag);
675
676         $xs->element('activity:object-type', null, $this->type);
677
678         $xs->element(self::ID, null, $this->id);
679
680         if (!empty($this->title)) {
681             $xs->element(self::TITLE, null, $this->title);
682         }
683
684         if (!empty($this->summary)) {
685             $xs->element(self::SUMMARY, null, $this->summary);
686         }
687
688         if (!empty($this->content)) {
689             // XXX: assuming HTML content here
690             $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
691         }
692
693         if (!empty($this->link)) {
694             $xs->element(
695                 'link',
696                 array(
697                     'rel' => 'alternate',
698                     'type' => 'text/html',
699                     'href' => $this->link
700                 ),
701                 null
702             );
703         }
704
705         if ($this->type == ActivityObject::PERSON) {
706             $xs->element(
707                 'link', array(
708                     'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
709                     'rel'  => 'avatar',
710                     'href' => empty($this->avatar)
711                     ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
712                     : $this->avatar->displayUrl()
713                 ),
714                 null
715             );
716         }
717
718         // XXX: Gotta figure out mime-type! Gar.
719
720         if ($this->type == ActivityObject::GROUP) {
721             $xs->element(
722                 'link', array(
723                     'rel'  => 'avatar',
724                     'href' => $this->avatar
725                 ),
726                 null
727             );
728         }
729
730         if (!empty($this->geopoint)) {
731             $xs->element(
732                 'georss:point',
733                 null,
734                 $this->geopoint
735             );
736         }
737
738         if (!empty($this->poco)) {
739             $xs->raw($this->poco->asString());
740         }
741
742         $xs->elementEnd($tag);
743
744         return $xs->getString();
745     }
746 }
747
748 /**
749  * Utility class to hold a bunch of constant defining default verb types
750  *
751  * @category  OStatus
752  * @package   StatusNet
753  * @author    Evan Prodromou <evan@status.net>
754  * @copyright 2010 StatusNet, Inc.
755  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
756  * @link      http://status.net/
757  */
758
759 class ActivityVerb
760 {
761     const POST     = 'http://activitystrea.ms/schema/1.0/post';
762     const SHARE    = 'http://activitystrea.ms/schema/1.0/share';
763     const SAVE     = 'http://activitystrea.ms/schema/1.0/save';
764     const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
765     const PLAY     = 'http://activitystrea.ms/schema/1.0/play';
766     const FOLLOW   = 'http://activitystrea.ms/schema/1.0/follow';
767     const FRIEND   = 'http://activitystrea.ms/schema/1.0/make-friend';
768     const JOIN     = 'http://activitystrea.ms/schema/1.0/join';
769     const TAG      = 'http://activitystrea.ms/schema/1.0/tag';
770
771     // Custom OStatus verbs for the flipside until they're standardized
772     const DELETE     = 'http://ostatus.org/schema/1.0/unfollow';
773     const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
774     const UNFOLLOW   = 'http://ostatus.org/schema/1.0/unfollow';
775     const LEAVE      = 'http://ostatus.org/schema/1.0/leave';
776
777     // For simple profile-update pings; no content to share.
778     const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
779 }
780
781 class ActivityContext
782 {
783     public $replyToID;
784     public $replyToUrl;
785     public $location;
786     public $attention = array();
787     public $conversation;
788
789     const THR     = 'http://purl.org/syndication/thread/1.0';
790     const GEORSS  = 'http://www.georss.org/georss';
791     const OSTATUS = 'http://ostatus.org/schema/1.0';
792
793     const INREPLYTO = 'in-reply-to';
794     const REF       = 'ref';
795     const HREF      = 'href';
796
797     const POINT     = 'point';
798
799     const ATTENTION    = 'ostatus:attention';
800     const CONVERSATION = 'ostatus:conversation';
801
802     function __construct($element)
803     {
804         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
805
806         if (!empty($replyToEl)) {
807             $this->replyToID  = $replyToEl->getAttribute(self::REF);
808             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
809         }
810
811         $this->location = $this->getLocation($element);
812
813         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
814
815         // Multiple attention links allowed
816
817         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
818
819         for ($i = 0; $i < $links->length; $i++) {
820
821             $link = $links->item($i);
822
823             $linkRel = $link->getAttribute(ActivityUtils::REL);
824
825             if ($linkRel == self::ATTENTION) {
826                 $this->attention[] = $link->getAttribute(self::HREF);
827             }
828         }
829     }
830
831     /**
832      * Parse location given as a GeoRSS-simple point, if provided.
833      * http://www.georss.org/simple
834      *
835      * @param feed item $entry
836      * @return mixed Location or false
837      */
838     function getLocation($dom)
839     {
840         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
841
842         for ($i = 0; $i < $points->length; $i++) {
843             $point = $points->item($i)->textContent;
844             return self::locationFromPoint($point);
845         }
846
847         return null;
848     }
849
850     // XXX: Move to ActivityUtils or Location?
851     static function locationFromPoint($point)
852     {
853         $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
854         $point = preg_replace('/\s+/', ' ', $point);
855         $point = trim($point);
856         $coords = explode(' ', $point);
857         if (count($coords) == 2) {
858             list($lat, $lon) = $coords;
859             if (is_numeric($lat) && is_numeric($lon)) {
860                 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
861                 return Location::fromLatLon($lat, $lon);
862             }
863         }
864         common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
865         return null;
866     }
867 }
868
869 /**
870  * An activity in the ActivityStrea.ms world
871  *
872  * An activity is kind of like a sentence: someone did something
873  * to something else.
874  *
875  * 'someone' is the 'actor'; 'did something' is the verb;
876  * 'something else' is the object.
877  *
878  * @category  OStatus
879  * @package   StatusNet
880  * @author    Evan Prodromou <evan@status.net>
881  * @copyright 2010 StatusNet, Inc.
882  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
883  * @link      http://status.net/
884  */
885
886 class Activity
887 {
888     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
889     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
890
891     const VERB       = 'verb';
892     const OBJECT     = 'object';
893     const ACTOR      = 'actor';
894     const SUBJECT    = 'subject';
895     const OBJECTTYPE = 'object-type';
896     const CONTEXT    = 'context';
897     const TARGET     = 'target';
898
899     const ATOM = 'http://www.w3.org/2005/Atom';
900
901     const AUTHOR    = 'author';
902     const PUBLISHED = 'published';
903     const UPDATED   = 'updated';
904
905     public $actor;   // an ActivityObject
906     public $verb;    // a string (the URL)
907     public $object;  // an ActivityObject
908     public $target;  // an ActivityObject
909     public $context; // an ActivityObject
910     public $time;    // Time of the activity
911     public $link;    // an ActivityObject
912     public $entry;   // the source entry
913     public $feed;    // the source feed
914
915     public $summary; // summary of activity
916     public $content; // HTML content of activity
917     public $id;      // ID of the activity
918     public $title;   // title of the activity
919     public $categories = array(); // list of AtomCategory objects
920
921     /**
922      * Turns a regular old Atom <entry> into a magical activity
923      *
924      * @param DOMElement $entry Atom entry to poke at
925      * @param DOMElement $feed  Atom feed, for context
926      */
927
928     function __construct($entry = null, $feed = null)
929     {
930         if (is_null($entry)) {
931             return;
932         }
933
934         $this->entry = $entry;
935         $this->feed  = $feed;
936
937         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
938
939         if (!empty($pubEl)) {
940             $this->time = strtotime($pubEl->textContent);
941         } else {
942             // XXX technically an error; being liberal. Good idea...?
943             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
944             if (!empty($updateEl)) {
945                 $this->time = strtotime($updateEl->textContent);
946             } else {
947                 $this->time = null;
948             }
949         }
950
951         $this->link = ActivityUtils::getPermalink($entry);
952
953         $verbEl = $this->_child($entry, self::VERB);
954
955         if (!empty($verbEl)) {
956             $this->verb = trim($verbEl->textContent);
957         } else {
958             $this->verb = ActivityVerb::POST;
959             // XXX: do other implied stuff here
960         }
961
962         $objectEl = $this->_child($entry, self::OBJECT);
963
964         if (!empty($objectEl)) {
965             $this->object = new ActivityObject($objectEl);
966         } else {
967             $this->object = new ActivityObject($entry);
968         }
969
970         $actorEl = $this->_child($entry, self::ACTOR);
971
972         if (!empty($actorEl)) {
973
974             $this->actor = new ActivityObject($actorEl);
975
976         } else if (!empty($feed) &&
977                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
978
979             $this->actor = new ActivityObject($subjectEl);
980
981         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
982
983             $this->actor = new ActivityObject($authorEl);
984
985         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
986                                                               self::ATOM)) {
987
988             $this->actor = new ActivityObject($authorEl);
989         }
990
991         $contextEl = $this->_child($entry, self::CONTEXT);
992
993         if (!empty($contextEl)) {
994             $this->context = new ActivityContext($contextEl);
995         } else {
996             $this->context = new ActivityContext($entry);
997         }
998
999         $targetEl = $this->_child($entry, self::TARGET);
1000
1001         if (!empty($targetEl)) {
1002             $this->target = new ActivityObject($targetEl);
1003         }
1004
1005         $this->summary = ActivityUtils::childContent($entry, 'summary');
1006         $this->id      = ActivityUtils::childContent($entry, 'id');
1007         $this->content = ActivityUtils::getContent($entry);
1008
1009         $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
1010         if ($catEls) {
1011             for ($i = 0; $i < $catEls->length; $i++) {
1012                 $catEl = $catEls->item($i);
1013                 $this->categories[] = new AtomCategory($catEl);
1014             }
1015         }
1016     }
1017
1018     /**
1019      * Returns an Atom <entry> based on this activity
1020      *
1021      * @return DOMElement Atom entry
1022      */
1023
1024     function toAtomEntry()
1025     {
1026         return null;
1027     }
1028
1029     function asString($namespace=false)
1030     {
1031         $xs = new XMLStringer(true);
1032
1033         if ($namespace) {
1034             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1035                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
1036                            'xmlns:georss' => 'http://www.georss.org/georss',
1037                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
1038                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0');
1039         } else {
1040             $attrs = array();
1041         }
1042
1043         $xs->elementStart('entry', $attrs);
1044
1045         $xs->element('id', null, $this->id);
1046         $xs->element('title', null, $this->title);
1047         $xs->element('published', null, common_date_iso8601($this->time));
1048         $xs->element('content', array('type' => 'html'), $this->content);
1049
1050         if (!empty($this->summary)) {
1051             $xs->element('summary', null, $this->summary);
1052         }
1053
1054         if (!empty($this->link)) {
1055             $xs->element('link', array('rel' => 'alternate',
1056                                        'type' => 'text/html'),
1057                          $this->link);
1058         }
1059
1060         // XXX: add context
1061
1062         $xs->elementStart('author');
1063         $xs->element('uri', array(), $this->actor->id);
1064         if ($this->actor->title) {
1065             $xs->element('name', array(), $this->actor->title);
1066         }
1067         $xs->elementEnd('author');
1068         $xs->raw($this->actor->asString('activity:actor'));
1069
1070         $xs->element('activity:verb', null, $this->verb);
1071
1072         if ($this->object) {
1073             $xs->raw($this->object->asString());
1074         }
1075
1076         if ($this->target) {
1077             $xs->raw($this->target->asString('activity:target'));
1078         }
1079
1080         foreach ($this->categories as $cat) {
1081             $xs->raw($cat->asString());
1082         }
1083
1084         $xs->elementEnd('entry');
1085
1086         return $xs->getString();
1087     }
1088
1089     private function _child($element, $tag, $namespace=self::SPEC)
1090     {
1091         return ActivityUtils::child($element, $tag, $namespace);
1092     }
1093 }
1094
1095 class AtomCategory
1096 {
1097     public $term;
1098     public $scheme;
1099     public $label;
1100
1101     function __construct($element=null)
1102     {
1103         if ($element && $element->attributes) {
1104             $this->term = $this->extract($element, 'term');
1105             $this->scheme = $this->extract($element, 'scheme');
1106             $this->label = $this->extract($element, 'label');
1107         }
1108     }
1109
1110     protected function extract($element, $attrib)
1111     {
1112         $node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
1113         if ($node) {
1114             return trim($node->textContent);
1115         }
1116         $node = $element->attributes->getNamedItem($attrib);
1117         if ($node) {
1118             return trim($node->textContent);
1119         }
1120         return null;
1121     }
1122
1123     function asString()
1124     {
1125         $attribs = array();
1126         if ($this->term !== null) {
1127             $attribs['term'] = $this->term;
1128         }
1129         if ($this->scheme !== null) {
1130             $attribs['scheme'] = $this->scheme;
1131         }
1132         if ($this->label !== null) {
1133             $attribs['label'] = $this->label;
1134         }
1135         $xs = new XMLStringer();
1136         $xs->element('category', $attribs);
1137         return $xs->asString();
1138     }
1139 }