]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/activity.php
OStatus: fix regressions from merge
[quix0rs-gnu-social.git] / plugins / OStatus / 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  OStatus
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Utilities for turning DOMish things into Activityish things
36  *
37  * Some common functions that I didn't have the bandwidth to try to factor
38  * into some kind of reasonable superclass, so just dumped here. Might
39  * be useful to have an ActivityObject parent class or something.
40  *
41  * @category  OStatus
42  * @package   StatusNet
43  * @author    Evan Prodromou <evan@status.net>
44  * @copyright 2010 StatusNet, Inc.
45  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
46  * @link      http://status.net/
47  */
48
49 class ActivityUtils
50 {
51     const ATOM = 'http://www.w3.org/2005/Atom';
52
53     const LINK = 'link';
54     const REL  = 'rel';
55     const TYPE = 'type';
56     const HREF = 'href';
57
58     /**
59      * Get the permalink for an Activity object
60      *
61      * @param DOMElement $element A DOM element
62      *
63      * @return string related link, if any
64      */
65
66     static function getPermalink($element)
67     {
68         return self::getLink($element, 'alternate', 'text/html');
69     }
70
71     /**
72      * Get the permalink for an Activity object
73      *
74      * @param DOMElement $element A DOM element
75      *
76      * @return string related link, if any
77      */
78
79     static function getLink($element, $rel, $type=null)
80     {
81         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
82
83         foreach ($links as $link) {
84
85             $linkRel = $link->getAttribute(self::REL);
86             $linkType = $link->getAttribute(self::TYPE);
87
88             if ($linkRel == $rel &&
89                 (is_null($type) || $linkType == $type)) {
90                 return $link->getAttribute(self::HREF);
91             }
92         }
93
94         return null;
95     }
96
97     /**
98      * Gets the first child element with the given tag
99      *
100      * @param DOMElement $element   element to pick at
101      * @param string     $tag       tag to look for
102      * @param string     $namespace Namespace to look under
103      *
104      * @return DOMElement found element or null
105      */
106
107     static function child($element, $tag, $namespace=self::ATOM)
108     {
109         $els = $element->childNodes;
110         if (empty($els) || $els->length == 0) {
111             return null;
112         } else {
113             for ($i = 0; $i < $els->length; $i++) {
114                 $el = $els->item($i);
115                 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
116                     return $el;
117                 }
118             }
119         }
120     }
121
122     /**
123      * Grab the text content of a DOM element child of the current element
124      *
125      * @param DOMElement $element   Element whose children we examine
126      * @param string     $tag       Tag to look up
127      * @param string     $namespace Namespace to use, defaults to Atom
128      *
129      * @return string content of the child
130      */
131
132     static function childContent($element, $tag, $namespace=self::ATOM)
133     {
134         $el = self::child($element, $tag, $namespace);
135
136         if (empty($el)) {
137             return null;
138         } else {
139             return $el->textContent;
140         }
141     }
142 }
143
144 /**
145  * A noun-ish thing in the activity universe
146  *
147  * The activity streams spec talks about activity objects, while also having
148  * a tag activity:object, which is in fact an activity object. Aaaaaah!
149  *
150  * This is just a thing in the activity universe. Can be the subject, object,
151  * or indirect object (target!) of an activity verb. Rotten name, and I'm
152  * propagating it. *sigh*
153  *
154  * @category  OStatus
155  * @package   StatusNet
156  * @author    Evan Prodromou <evan@status.net>
157  * @copyright 2010 StatusNet, Inc.
158  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
159  * @link      http://status.net/
160  */
161
162 class ActivityObject
163 {
164     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
165     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
166     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
167     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
168     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
169     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
170     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
171     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
172     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
173     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
174     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
175     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
176     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
177     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
178     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
179     // ^^^^^^^^^^ tea!
180
181     // Atom elements we snarf
182
183     const TITLE   = 'title';
184     const SUMMARY = 'summary';
185     const CONTENT = 'content';
186     const ID      = 'id';
187     const SOURCE  = 'source';
188
189     const NAME  = 'name';
190     const URI   = 'uri';
191     const EMAIL = 'email';
192
193     public $element;
194     public $type;
195     public $id;
196     public $title;
197     public $summary;
198     public $content;
199     public $link;
200     public $source;
201
202    /**
203      * Constructor
204      *
205      * This probably needs to be refactored
206      * to generate a local class (ActivityPerson, ActivityFile, ...)
207      * based on the object type.
208      *
209      * @param DOMElement $element DOM thing to turn into an Activity thing
210      */
211
212     function __construct($element = null)
213     {
214         if (empty($element)) {
215             return;
216         }
217
218         $this->element = $element;
219
220         if ($element->tagName == 'author') {
221
222             $this->type  = self::PERSON; // XXX: is this fair?
223             $this->title = $this->_childContent($element, self::NAME);
224             $this->id    = $this->_childContent($element, self::URI);
225
226             if (empty($this->id)) {
227                 $email = $this->_childContent($element, self::EMAIL);
228                 if (!empty($email)) {
229                     // XXX: acct: ?
230                     $this->id = 'mailto:'.$email;
231                 }
232             }
233
234         } else {
235
236             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
237                                                Activity::SPEC);
238
239             if (empty($this->type)) {
240                 $this->type = ActivityObject::NOTE;
241             }
242
243             $this->id      = $this->_childContent($element, self::ID);
244             $this->title   = $this->_childContent($element, self::TITLE);
245             $this->summary = $this->_childContent($element, self::SUMMARY);
246             $this->content = $this->_childContent($element, self::CONTENT);
247
248             $this->source  = $this->_getSource($element);
249
250             $this->link = ActivityUtils::getPermalink($element);
251
252             // XXX: grab PoCo stuff
253         }
254
255         // Some per-type attributes...
256         if ($this->type == self::PERSON || $this->type == self::GROUP) {
257             $this->displayName = $this->title;
258
259             // @fixme we may have multiple avatars with different resolutions specified
260             $this->avatar = ActivityUtils::getLink($element, 'avatar');
261         }
262     }
263
264     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
265     {
266         return ActivityUtils::childContent($element, $tag, $namespace);
267     }
268
269     // Try to get a unique id for the source feed
270
271     private function _getSource($element)
272     {
273         $sourceEl = ActivityUtils::child($element, 'source');
274
275         if (empty($sourceEl)) {
276             return null;
277         } else {
278             $href = ActivityUtils::getLink($sourceEl, 'self');
279             if (!empty($href)) {
280                 return $href;
281             } else {
282                 return ActivityUtils::childContent($sourceEl, 'id');
283             }
284         }
285     }
286
287     static function fromNotice($notice)
288     {
289         $object = new ActivityObject();
290
291         $object->type    = ActivityObject::NOTE;
292
293         $object->id      = $notice->uri;
294         $object->title   = $notice->content;
295         $object->content = $notice->rendered;
296         $object->link    = $notice->bestUrl();
297
298         return $object;
299     }
300
301     function asString($tag='activity:object')
302     {
303         $xs = new XMLStringer(true);
304
305         $xs->elementStart($tag);
306
307         $xs->element('activity:object-type', null, $this->type);
308
309         $xs->element(self::ID, null, $this->id);
310
311         if (!empty($this->title)) {
312             $xs->element(self::TITLE, null, $this->title);
313         }
314
315         if (!empty($this->summary)) {
316             $xs->element(self::SUMMARY, null, $this->summary);
317         }
318
319         if (!empty($this->content)) {
320             // XXX: assuming HTML content here
321             $xs->element(self::CONTENT, array('type' => 'html'), $this->content);
322         }
323
324         if (!empty($this->link)) {
325             $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
326                          $this->content);
327         }
328
329         $xs->elementEnd($tag);
330
331         return $xs->getString();
332     }
333 }
334
335 /**
336  * Utility class to hold a bunch of constant defining default verb types
337  *
338  * @category  OStatus
339  * @package   StatusNet
340  * @author    Evan Prodromou <evan@status.net>
341  * @copyright 2010 StatusNet, Inc.
342  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
343  * @link      http://status.net/
344  */
345
346 class ActivityVerb
347 {
348     const POST     = 'http://activitystrea.ms/schema/1.0/post';
349     const SHARE    = 'http://activitystrea.ms/schema/1.0/share';
350     const SAVE     = 'http://activitystrea.ms/schema/1.0/save';
351     const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
352     const PLAY     = 'http://activitystrea.ms/schema/1.0/play';
353     const FOLLOW   = 'http://activitystrea.ms/schema/1.0/follow';
354     const FRIEND   = 'http://activitystrea.ms/schema/1.0/make-friend';
355     const JOIN     = 'http://activitystrea.ms/schema/1.0/join';
356     const TAG      = 'http://activitystrea.ms/schema/1.0/tag';
357
358     // Custom OStatus verbs for the flipside until they're standardized
359     const DELETE     = 'http://ostatus.org/schema/1.0/unfollow';
360     const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
361     const UNFOLLOW   = 'http://ostatus.org/schema/1.0/unfollow';
362     const LEAVE      = 'http://ostatus.org/schema/1.0/leave';
363 }
364
365 class ActivityContext
366 {
367     public $replyToID;
368     public $replyToUrl;
369     public $location;
370     public $attention = array();
371     public $conversation;
372
373     const THR     = 'http://purl.org/syndication/thread/1.0';
374     const GEORSS  = 'http://www.georss.org/georss';
375     const OSTATUS = 'http://ostatus.org/schema/1.0';
376
377     const INREPLYTO = 'in-reply-to';
378     const REF       = 'ref';
379     const HREF      = 'href';
380
381     const POINT     = 'point';
382
383     const ATTENTION    = 'ostatus:attention';
384     const CONVERSATION = 'ostatus:conversation';
385
386     function __construct($element)
387     {
388         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
389
390         if (!empty($replyToEl)) {
391             $this->replyToID  = $replyToEl->getAttribute(self::REF);
392             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
393         }
394
395         $this->location = $this->getLocation($element);
396
397         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
398
399         // Multiple attention links allowed
400
401         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
402
403         for ($i = 0; $i < $links->length; $i++) {
404
405             $link = $links->item($i);
406
407             $linkRel = $link->getAttribute(ActivityUtils::REL);
408
409             if ($linkRel == self::ATTENTION) {
410                 $this->attention[] = $link->getAttribute(self::HREF);
411             }
412         }
413     }
414
415     /**
416      * Parse location given as a GeoRSS-simple point, if provided.
417      * http://www.georss.org/simple
418      *
419      * @param feed item $entry
420      * @return mixed Location or false
421      */
422     function getLocation($dom)
423     {
424         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
425
426         for ($i = 0; $i < $points->length; $i++) {
427             $point = $points->item($i)->textContent;
428             $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
429             $point = preg_replace('/\s+/', ' ', $point);
430             $point = trim($point);
431             $coords = explode(' ', $point);
432             if (count($coords) == 2) {
433                 list($lat, $lon) = $coords;
434                 if (is_numeric($lat) && is_numeric($lon)) {
435                     common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
436                     return Location::fromLatLon($lat, $lon);
437                 }
438             }
439             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
440         }
441
442         return null;
443     }
444 }
445
446 /**
447  * An activity in the ActivityStrea.ms world
448  *
449  * An activity is kind of like a sentence: someone did something
450  * to something else.
451  *
452  * 'someone' is the 'actor'; 'did something' is the verb;
453  * 'something else' is the object.
454  *
455  * @category  OStatus
456  * @package   StatusNet
457  * @author    Evan Prodromou <evan@status.net>
458  * @copyright 2010 StatusNet, Inc.
459  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
460  * @link      http://status.net/
461  */
462
463 class Activity
464 {
465     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
466     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
467
468     const VERB       = 'verb';
469     const OBJECT     = 'object';
470     const ACTOR      = 'actor';
471     const SUBJECT    = 'subject';
472     const OBJECTTYPE = 'object-type';
473     const CONTEXT    = 'context';
474     const TARGET     = 'target';
475
476     const ATOM = 'http://www.w3.org/2005/Atom';
477
478     const AUTHOR    = 'author';
479     const PUBLISHED = 'published';
480     const UPDATED   = 'updated';
481
482     public $actor;   // an ActivityObject
483     public $verb;    // a string (the URL)
484     public $object;  // an ActivityObject
485     public $target;  // an ActivityObject
486     public $context; // an ActivityObject
487     public $time;    // Time of the activity
488     public $link;    // an ActivityObject
489     public $entry;   // the source entry
490     public $feed;    // the source feed
491
492     /**
493      * Turns a regular old Atom <entry> into a magical activity
494      *
495      * @param DOMElement $entry Atom entry to poke at
496      * @param DOMElement $feed  Atom feed, for context
497      */
498
499     function __construct($entry, $feed = null)
500     {
501         $this->entry = $entry;
502         $this->feed  = $feed;
503
504         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
505
506         if (!empty($pubEl)) {
507             $this->time = strtotime($pubEl->textContent);
508         } else {
509             // XXX technically an error; being liberal. Good idea...?
510             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
511             if (!empty($updateEl)) {
512                 $this->time = strtotime($updateEl->textContent);
513             } else {
514                 $this->time = null;
515             }
516         }
517
518         $this->link = ActivityUtils::getPermalink($entry);
519
520         $verbEl = $this->_child($entry, self::VERB);
521
522         if (!empty($verbEl)) {
523             $this->verb = trim($verbEl->textContent);
524         } else {
525             $this->verb = ActivityVerb::POST;
526             // XXX: do other implied stuff here
527         }
528
529         $objectEl = $this->_child($entry, self::OBJECT);
530
531         if (!empty($objectEl)) {
532             $this->object = new ActivityObject($objectEl);
533         } else {
534             $this->object = new ActivityObject($entry);
535         }
536
537         $actorEl = $this->_child($entry, self::ACTOR);
538
539         if (!empty($actorEl)) {
540
541             $this->actor = new ActivityObject($actorEl);
542
543         } else if (!empty($feed) &&
544                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
545
546             $this->actor = new ActivityObject($subjectEl);
547
548         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
549
550             $this->actor = new ActivityObject($authorEl);
551
552         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
553                                                               self::ATOM)) {
554
555             $this->actor = new ActivityObject($authorEl);
556         }
557
558         $contextEl = $this->_child($entry, self::CONTEXT);
559
560         if (!empty($contextEl)) {
561             $this->context = new ActivityContext($contextEl);
562         } else {
563             $this->context = new ActivityContext($entry);
564         }
565
566         $targetEl = $this->_child($entry, self::TARGET);
567
568         if (!empty($targetEl)) {
569             $this->target = new ActivityObject($targetEl);
570         }
571     }
572
573     /**
574      * Returns an Atom <entry> based on this activity
575      *
576      * @return DOMElement Atom entry
577      */
578
579     function toAtomEntry()
580     {
581         return null;
582     }
583
584     private function _child($element, $tag, $namespace=self::SPEC)
585     {
586         return ActivityUtils::child($element, $tag, $namespace);
587     }
588 }