]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/activity.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[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)
213     {
214         $this->element = $element;
215
216         if ($element->tagName == 'author') {
217
218             $this->type  = self::PERSON; // XXX: is this fair?
219             $this->title = $this->_childContent($element, self::NAME);
220             $this->id    = $this->_childContent($element, self::URI);
221
222             if (empty($this->id)) {
223                 $email = $this->_childContent($element, self::EMAIL);
224                 if (!empty($email)) {
225                     // XXX: acct: ?
226                     $this->id = 'mailto:'.$email;
227                 }
228             }
229
230         } else {
231
232             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
233                                                Activity::SPEC);
234
235             if (empty($this->type)) {
236                 $this->type = ActivityObject::NOTE;
237             }
238
239             $this->id      = $this->_childContent($element, self::ID);
240             $this->title   = $this->_childContent($element, self::TITLE);
241             $this->summary = $this->_childContent($element, self::SUMMARY);
242             $this->content = $this->_childContent($element, self::CONTENT);
243
244             $this->source  = $this->_getSource($element);
245
246             $this->link = ActivityUtils::getPermalink($element);
247
248             // XXX: grab PoCo stuff
249         }
250
251         // Some per-type attributes...
252         if ($this->type == self::PERSON || $this->type == self::GROUP) {
253             $this->displayName = $this->title;
254
255             // @fixme we may have multiple avatars with different resolutions specified
256             $this->avatar = ActivityUtils::getLink($element, 'avatar');
257         }
258     }
259
260     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
261     {
262         return ActivityUtils::childContent($element, $tag, $namespace);
263     }
264
265     // Try to get a unique id for the source feed
266
267     private function _getSource($element)
268     {
269         $sourceEl = ActivityUtils::child($element, 'source');
270
271         if (empty($sourceEl)) {
272             return null;
273         } else {
274             $href = ActivityUtils::getLink($sourceEl, 'self');
275             if (!empty($href)) {
276                 return $href;
277             } else {
278                 return ActivityUtils::childContent($sourceEl, 'id');
279             }
280         }
281     }
282 }
283
284 /**
285  * Utility class to hold a bunch of constant defining default verb types
286  *
287  * @category  OStatus
288  * @package   StatusNet
289  * @author    Evan Prodromou <evan@status.net>
290  * @copyright 2010 StatusNet, Inc.
291  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
292  * @link      http://status.net/
293  */
294
295 class ActivityVerb
296 {
297     const POST     = 'http://activitystrea.ms/schema/1.0/post';
298     const SHARE    = 'http://activitystrea.ms/schema/1.0/share';
299     const SAVE     = 'http://activitystrea.ms/schema/1.0/save';
300     const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
301     const PLAY     = 'http://activitystrea.ms/schema/1.0/play';
302     const FOLLOW   = 'http://activitystrea.ms/schema/1.0/follow';
303     const FRIEND   = 'http://activitystrea.ms/schema/1.0/make-friend';
304     const JOIN     = 'http://activitystrea.ms/schema/1.0/join';
305     const TAG      = 'http://activitystrea.ms/schema/1.0/tag';
306
307     // Custom OStatus verbs for the flipside until they're standardized
308     const DELETE     = 'http://ostatus.org/schema/1.0/unfollow';
309     const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
310     const UNFOLLOW   = 'http://ostatus.org/schema/1.0/unfollow';
311     const LEAVE      = 'http://ostatus.org/schema/1.0/leave';
312 }
313
314 class ActivityContext
315 {
316     public $replyToID;
317     public $replyToUrl;
318     public $location;
319     public $attention = array();
320     public $conversation;
321
322     const THR     = 'http://purl.org/syndication/thread/1.0';
323     const GEORSS  = 'http://www.georss.org/georss';
324     const OSTATUS = 'http://ostatus.org/schema/1.0';
325
326     const INREPLYTO = 'in-reply-to';
327     const REF       = 'ref';
328     const HREF      = 'href';
329
330     const POINT     = 'point';
331
332     const ATTENTION    = 'ostatus:attention';
333     const CONVERSATION = 'ostatus:conversation';
334
335     function __construct($element)
336     {
337         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
338
339         if (!empty($replyToEl)) {
340             $this->replyToID  = $replyToEl->getAttribute(self::REF);
341             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
342         }
343
344         $this->location = $this->getLocation($element);
345
346         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
347
348         // Multiple attention links allowed
349
350         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
351
352         for ($i = 0; $i < $links->length; $i++) {
353
354             $link = $links->item($i);
355
356             $linkRel = $link->getAttribute(ActivityUtils::REL);
357
358             if ($linkRel == self::ATTENTION) {
359                 $this->attention[] = $link->getAttribute(self::HREF);
360             }
361         }
362     }
363
364     /**
365      * Parse location given as a GeoRSS-simple point, if provided.
366      * http://www.georss.org/simple
367      *
368      * @param feed item $entry
369      * @return mixed Location or false
370      */
371     function getLocation($dom)
372     {
373         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
374
375         for ($i = 0; $i < $points->length; $i++) {
376             $point = $points->item($i)->textContent;
377             $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
378             $point = preg_replace('/\s+/', ' ', $point);
379             $point = trim($point);
380             $coords = explode(' ', $point);
381             if (count($coords) == 2) {
382                 list($lat, $lon) = $coords;
383                 if (is_numeric($lat) && is_numeric($lon)) {
384                     common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
385                     return Location::fromLatLon($lat, $lon);
386                 }
387             }
388             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
389         }
390
391         return null;
392     }
393 }
394
395 /**
396  * An activity in the ActivityStrea.ms world
397  *
398  * An activity is kind of like a sentence: someone did something
399  * to something else.
400  *
401  * 'someone' is the 'actor'; 'did something' is the verb;
402  * 'something else' is the object.
403  *
404  * @category  OStatus
405  * @package   StatusNet
406  * @author    Evan Prodromou <evan@status.net>
407  * @copyright 2010 StatusNet, Inc.
408  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
409  * @link      http://status.net/
410  */
411
412 class Activity
413 {
414     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
415     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
416
417     const VERB       = 'verb';
418     const OBJECT     = 'object';
419     const ACTOR      = 'actor';
420     const SUBJECT    = 'subject';
421     const OBJECTTYPE = 'object-type';
422     const CONTEXT    = 'context';
423     const TARGET     = 'target';
424
425     const ATOM = 'http://www.w3.org/2005/Atom';
426
427     const AUTHOR    = 'author';
428     const PUBLISHED = 'published';
429     const UPDATED   = 'updated';
430
431     public $actor;   // an ActivityObject
432     public $verb;    // a string (the URL)
433     public $object;  // an ActivityObject
434     public $target;  // an ActivityObject
435     public $context; // an ActivityObject
436     public $time;    // Time of the activity
437     public $link;    // an ActivityObject
438     public $entry;   // the source entry
439     public $feed;    // the source feed
440
441     /**
442      * Turns a regular old Atom <entry> into a magical activity
443      *
444      * @param DOMElement $entry Atom entry to poke at
445      * @param DOMElement $feed  Atom feed, for context
446      */
447
448     function __construct($entry, $feed = null)
449     {
450         $this->entry = $entry;
451         $this->feed  = $feed;
452
453         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
454
455         if (!empty($pubEl)) {
456             $this->time = strtotime($pubEl->textContent);
457         } else {
458             // XXX technically an error; being liberal. Good idea...?
459             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
460             if (!empty($updateEl)) {
461                 $this->time = strtotime($updateEl->textContent);
462             } else {
463                 $this->time = null;
464             }
465         }
466
467         $this->link = ActivityUtils::getPermalink($entry);
468
469         $verbEl = $this->_child($entry, self::VERB);
470
471         if (!empty($verbEl)) {
472             $this->verb = trim($verbEl->textContent);
473         } else {
474             $this->verb = ActivityVerb::POST;
475             // XXX: do other implied stuff here
476         }
477
478         $objectEl = $this->_child($entry, self::OBJECT);
479
480         if (!empty($objectEl)) {
481             $this->object = new ActivityObject($objectEl);
482         } else {
483             $this->object = new ActivityObject($entry);
484         }
485
486         $actorEl = $this->_child($entry, self::ACTOR);
487
488         if (!empty($actorEl)) {
489
490             $this->actor = new ActivityObject($actorEl);
491
492         } else if (!empty($feed) &&
493                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
494
495             $this->actor = new ActivityObject($subjectEl);
496
497         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
498
499             $this->actor = new ActivityObject($authorEl);
500
501         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
502                                                               self::ATOM)) {
503
504             $this->actor = new ActivityObject($authorEl);
505         }
506
507         $contextEl = $this->_child($entry, self::CONTEXT);
508
509         if (!empty($contextEl)) {
510             $this->context = new ActivityContext($contextEl);
511         } else {
512             $this->context = new ActivityContext($entry);
513         }
514
515         $targetEl = $this->_child($entry, self::TARGET);
516
517         if (!empty($targetEl)) {
518             $this->target = new ActivityObject($targetEl);
519         }
520     }
521
522     /**
523      * Returns an Atom <entry> based on this activity
524      *
525      * @return DOMElement Atom entry
526      */
527
528     function toAtomEntry()
529     {
530         return null;
531     }
532
533     private function _child($element, $tag, $namespace=self::SPEC)
534     {
535         return ActivityUtils::child($element, $tag, $namespace);
536     }
537 }