]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[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 /**
36  * An activity in the ActivityStrea.ms world
37  *
38  * An activity is kind of like a sentence: someone did something
39  * to something else.
40  *
41  * 'someone' is the 'actor'; 'did something' is the verb;
42  * 'something else' is the object.
43  *
44  * @category  OStatus
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
49  * @link      http://status.net/
50  */
51
52 class Activity
53 {
54     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
55     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
56     const MEDIA  = 'http://purl.org/syndication/atommedia';
57
58     const VERB       = 'verb';
59     const OBJECT     = 'object';
60     const ACTOR      = 'actor';
61     const SUBJECT    = 'subject';
62     const OBJECTTYPE = 'object-type';
63     const CONTEXT    = 'context';
64     const TARGET     = 'target';
65
66     const ATOM = 'http://www.w3.org/2005/Atom';
67
68     const AUTHOR    = 'author';
69     const PUBLISHED = 'published';
70     const UPDATED   = 'updated';
71
72     const RSS = null; // no namespace!
73
74     const PUBDATE     = 'pubDate';
75     const DESCRIPTION = 'description';
76     const GUID        = 'guid';
77     const SELF        = 'self';
78     const IMAGE       = 'image';
79     const URL         = 'url';
80
81     const DC = 'http://purl.org/dc/elements/1.1/';
82
83     const CREATOR = 'creator';
84
85     const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
86     const ENCODED = 'encoded';
87
88     public $actor;   // an ActivityObject
89     public $verb;    // a string (the URL)
90     public $objects = array();  // an array of ActivityObjects
91     public $target;  // an ActivityObject
92     public $context; // an ActivityObject
93     public $time;    // Time of the activity
94     public $link;    // an ActivityObject
95     public $entry;   // the source entry
96     public $feed;    // the source feed
97
98     public $summary; // summary of activity
99     public $content; // HTML content of activity
100     public $id;      // ID of the activity
101     public $title;   // title of the activity
102     public $categories = array(); // list of AtomCategory objects
103     public $enclosures = array(); // list of enclosure URL references
104
105     /**
106      * Turns a regular old Atom <entry> into a magical activity
107      *
108      * @param DOMElement $entry Atom entry to poke at
109      * @param DOMElement $feed  Atom feed, for context
110      */
111
112     function __construct($entry = null, $feed = null)
113     {
114         if (is_null($entry)) {
115             return;
116         }
117
118         // Insist on a feed's root DOMElement; don't allow a DOMDocument
119         if ($feed instanceof DOMDocument) {
120             throw new ClientException(
121                 // TRANS: Client exception thrown when a feed instance is a DOMDocument.
122                 _('Expecting a root feed element but got a whole XML document.')
123             );
124         }
125
126         $this->entry = $entry;
127         $this->feed  = $feed;
128
129         if ($entry->namespaceURI == Activity::ATOM &&
130             $entry->localName == 'entry') {
131             $this->_fromAtomEntry($entry, $feed);
132         } else if ($entry->namespaceURI == Activity::RSS &&
133                    $entry->localName == 'item') {
134             $this->_fromRssItem($entry, $feed);
135         } else {
136             throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
137         }
138     }
139
140     function _fromAtomEntry($entry, $feed)
141     {
142         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
143
144         if (!empty($pubEl)) {
145             $this->time = strtotime($pubEl->textContent);
146         } else {
147             // XXX technically an error; being liberal. Good idea...?
148             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
149             if (!empty($updateEl)) {
150                 $this->time = strtotime($updateEl->textContent);
151             } else {
152                 $this->time = null;
153             }
154         }
155
156         $this->link = ActivityUtils::getPermalink($entry);
157
158         $verbEl = $this->_child($entry, self::VERB);
159
160         if (!empty($verbEl)) {
161             $this->verb = trim($verbEl->textContent);
162         } else {
163             $this->verb = ActivityVerb::POST;
164             // XXX: do other implied stuff here
165         }
166
167         $objectEls = $entry->getElementsByTagNameNS(self::SPEC, self::OBJECT);
168
169         if ($objectEls->length > 0) {
170             for ($i = 0; $i < $objectEls->length; $i++) {
171                 $objectEl = $objectEls->item($i);
172                 $this->objects[] = new ActivityObject($objectEl);
173             }
174         } else {
175             $this->objects[] = new ActivityObject($entry);
176         }
177
178         $actorEl = $this->_child($entry, self::ACTOR);
179
180         if (!empty($actorEl)) {
181
182             $this->actor = new ActivityObject($actorEl);
183
184             // Cliqset has bad actor IDs (just nickname of user). We
185             // work around it by getting the author data and using its
186             // id instead
187
188             if (!preg_match('/^\w+:/', $this->actor->id)) {
189                 $authorEl = ActivityUtils::child($entry, 'author');
190                 if (!empty($authorEl)) {
191                     $authorObj = new ActivityObject($authorEl);
192                     $this->actor->id = $authorObj->id;
193                 }
194             }
195         } else if (!empty($feed) &&
196                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
197
198             $this->actor = new ActivityObject($subjectEl);
199
200         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
201
202             $this->actor = new ActivityObject($authorEl);
203
204         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
205                                                               self::ATOM)) {
206
207             $this->actor = new ActivityObject($authorEl);
208         }
209
210         $contextEl = $this->_child($entry, self::CONTEXT);
211
212         if (!empty($contextEl)) {
213             $this->context = new ActivityContext($contextEl);
214         } else {
215             $this->context = new ActivityContext($entry);
216         }
217
218         $targetEl = $this->_child($entry, self::TARGET);
219
220         if (!empty($targetEl)) {
221             $this->target = new ActivityObject($targetEl);
222         }
223
224         $this->summary = ActivityUtils::childContent($entry, 'summary');
225         $this->id      = ActivityUtils::childContent($entry, 'id');
226         $this->content = ActivityUtils::getContent($entry);
227
228         $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
229         if ($catEls) {
230             for ($i = 0; $i < $catEls->length; $i++) {
231                 $catEl = $catEls->item($i);
232                 $this->categories[] = new AtomCategory($catEl);
233             }
234         }
235
236         foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
237             $this->enclosures[] = $link->getAttribute('href');
238         }
239     }
240
241     function _fromRssItem($item, $channel)
242     {
243         $verbEl = $this->_child($item, self::VERB);
244
245         if (!empty($verbEl)) {
246             $this->verb = trim($verbEl->textContent);
247         } else {
248             $this->verb = ActivityVerb::POST;
249             // XXX: do other implied stuff here
250         }
251
252         $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
253
254         if (!empty($pubDateEl)) {
255             $this->time = strtotime($pubDateEl->textContent);
256         }
257
258         if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) {
259             $this->actor = ActivityObject::fromRssAuthor($authorEl);
260         } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) {
261             $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
262         } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) {
263             // Special case for Posterous.com
264             $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
265         } else if (!empty($channel)) {
266             $this->actor = ActivityObject::fromRssChannel($channel);
267         } else {
268             // No actor!
269         }
270
271         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
272
273         $contentEl = ActivityUtils::child($item, self::ENCODED, self::CONTENTNS);
274
275         if (!empty($contentEl)) {
276             // <content:encoded> XML node's text content is HTML; no further processing needed.
277             $this->content = $contentEl->textContent;
278         } else {
279             $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
280             if (!empty($descriptionEl)) {
281                 // Per spec, <description> must be plaintext.
282                 // In practice, often there's HTML... but these days good
283                 // feeds are using <content:encoded> which is explicitly
284                 // real HTML.
285                 // We'll treat this following spec, and do HTML escaping
286                 // to convert from plaintext to HTML.
287                 $this->content = htmlspecialchars($descriptionEl->textContent);
288             }
289         }
290
291         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
292
293         // @fixme enclosures
294         // @fixme thumbnails... maybe
295
296         $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
297
298         if (!empty($guidEl)) {
299             $this->id = $guidEl->textContent;
300
301             if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
302                 // overwrites <link>
303                 $this->link = $this->id;
304             }
305         }
306
307         $this->objects[] = new ActivityObject($item);
308         $this->context   = new ActivityContext($item);
309     }
310
311     /**
312      * Returns an Atom <entry> based on this activity
313      *
314      * @return DOMElement Atom entry
315      */
316
317     function toAtomEntry()
318     {
319         return null;
320     }
321
322     function asString($namespace=false)
323     {
324         $xs = new XMLStringer(true);
325
326         if ($namespace) {
327             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
328                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
329                            'xmlns:georss' => 'http://www.georss.org/georss',
330                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
331                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
332                            'xmlns:media' => 'http://purl.org/syndication/atommedia');
333         } else {
334             $attrs = array();
335         }
336
337         $xs->elementStart('entry', $attrs);
338
339         $xs->element('id', null, $this->id);
340         $xs->element('title', null, $this->title);
341         $xs->element('published', null, common_date_iso8601($this->time));
342         $xs->element('content', array('type' => 'html'), $this->content);
343
344         if (!empty($this->summary)) {
345             $xs->element('summary', null, $this->summary);
346         }
347
348         if (!empty($this->link)) {
349             $xs->element('link', array('rel' => 'alternate',
350                                        'type' => 'text/html'),
351                          $this->link);
352         }
353
354         // XXX: add context
355
356         $xs->elementStart('author');
357         $xs->element('uri', array(), $this->actor->id);
358         if ($this->actor->title) {
359             $xs->element('name', array(), $this->actor->title);
360         }
361         $xs->elementEnd('author');
362         $xs->raw($this->actor->asString('activity:actor'));
363
364         $xs->element('activity:verb', null, $this->verb);
365
366         if (!empty($this->objects)) {
367             foreach($this->objects as $object) {
368                 $xs->raw($object->asString());
369             }
370         }
371
372         if ($this->target) {
373             $xs->raw($this->target->asString('activity:target'));
374         }
375
376         foreach ($this->categories as $cat) {
377             $xs->raw($cat->asString());
378         }
379
380         $xs->elementEnd('entry');
381
382         return $xs->getString();
383     }
384
385     private function _child($element, $tag, $namespace=self::SPEC)
386     {
387         return ActivityUtils::child($element, $tag, $namespace);
388     }
389 }
390