]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
move check for bad IDs from activityobject to activity and make simpler
[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
87     public $actor;   // an ActivityObject
88     public $verb;    // a string (the URL)
89     public $objects = array();  // an array of ActivityObjects
90     public $target;  // an ActivityObject
91     public $context; // an ActivityObject
92     public $time;    // Time of the activity
93     public $link;    // an ActivityObject
94     public $entry;   // the source entry
95     public $feed;    // the source feed
96
97     public $summary; // summary of activity
98     public $content; // HTML content of activity
99     public $id;      // ID of the activity
100     public $title;   // title of the activity
101     public $categories = array(); // list of AtomCategory objects
102     public $enclosures = array(); // list of enclosure URL references
103
104     /**
105      * Turns a regular old Atom <entry> into a magical activity
106      *
107      * @param DOMElement $entry Atom entry to poke at
108      * @param DOMElement $feed  Atom feed, for context
109      */
110
111     function __construct($entry = null, $feed = null)
112     {
113         if (is_null($entry)) {
114             return;
115         }
116
117         // Insist on a feed's root DOMElement; don't allow a DOMDocument
118         if ($feed instanceof DOMDocument) {
119             throw new ClientException(
120                 _("Expecting a root feed element but got a whole XML document.")
121             );
122         }
123
124         $this->entry = $entry;
125         $this->feed  = $feed;
126
127         if ($entry->namespaceURI == Activity::ATOM &&
128             $entry->localName == 'entry') {
129             $this->_fromAtomEntry($entry, $feed);
130         } else if ($entry->namespaceURI == Activity::RSS &&
131                    $entry->localName == 'item') {
132             $this->_fromRssItem($entry, $feed);
133         } else {
134             throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
135         }
136     }
137
138     function _fromAtomEntry($entry, $feed)
139     {
140         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
141
142         if (!empty($pubEl)) {
143             $this->time = strtotime($pubEl->textContent);
144         } else {
145             // XXX technically an error; being liberal. Good idea...?
146             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
147             if (!empty($updateEl)) {
148                 $this->time = strtotime($updateEl->textContent);
149             } else {
150                 $this->time = null;
151             }
152         }
153
154         $this->link = ActivityUtils::getPermalink($entry);
155
156         $verbEl = $this->_child($entry, self::VERB);
157
158         if (!empty($verbEl)) {
159             $this->verb = trim($verbEl->textContent);
160         } else {
161             $this->verb = ActivityVerb::POST;
162             // XXX: do other implied stuff here
163         }
164
165         $objectEls = $entry->getElementsByTagNameNS(self::SPEC, self::OBJECT);
166
167         if ($objectEls->length > 0) {
168             for ($i = 0; $i < $objectEls->length; $i++) {
169                 $objectEl = $objectEls->item($i);
170                 $this->objects[] = new ActivityObject($objectEl);
171             }
172         } else {
173             $this->objects[] = new ActivityObject($entry);
174         }
175
176         $actorEl = $this->_child($entry, self::ACTOR);
177
178         if (!empty($actorEl)) {
179
180             $this->actor = new ActivityObject($actorEl);
181
182             // Cliqset has bad actor IDs (just nickname of user). We
183             // work around it by getting the author data and using its
184             // id instead
185
186             if (!preg_match('/^\w+:/', $this->actor->id)) {
187                 $authorEl = ActivityUtils::child($entry, 'author');
188                 if (!empty($authorEl)) {
189                     $authorObj = new ActivityObject($authorEl);
190                     $this->actor->id = $authorObj->id;
191                 }
192             }
193         } else if (!empty($feed) &&
194                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
195
196             $this->actor = new ActivityObject($subjectEl);
197
198         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
199
200             $this->actor = new ActivityObject($authorEl);
201
202         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
203                                                               self::ATOM)) {
204
205             $this->actor = new ActivityObject($authorEl);
206         }
207
208         $contextEl = $this->_child($entry, self::CONTEXT);
209
210         if (!empty($contextEl)) {
211             $this->context = new ActivityContext($contextEl);
212         } else {
213             $this->context = new ActivityContext($entry);
214         }
215
216         $targetEl = $this->_child($entry, self::TARGET);
217
218         if (!empty($targetEl)) {
219             $this->target = new ActivityObject($targetEl);
220         }
221
222         $this->summary = ActivityUtils::childContent($entry, 'summary');
223         $this->id      = ActivityUtils::childContent($entry, 'id');
224         $this->content = ActivityUtils::getContent($entry);
225
226         $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
227         if ($catEls) {
228             for ($i = 0; $i < $catEls->length; $i++) {
229                 $catEl = $catEls->item($i);
230                 $this->categories[] = new AtomCategory($catEl);
231             }
232         }
233
234         foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
235             $this->enclosures[] = $link->getAttribute('href');
236         }
237     }
238
239     function _fromRssItem($item, $channel)
240     {
241         $verbEl = $this->_child($item, self::VERB);
242
243         if (!empty($verbEl)) {
244             $this->verb = trim($verbEl->textContent);
245         } else {
246             $this->verb = ActivityVerb::POST;
247             // XXX: do other implied stuff here
248         }
249
250         $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
251
252         if (!empty($pubDateEl)) {
253             $this->time = strtotime($pubDateEl->textContent);
254         }
255
256         if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) {
257             $this->actor = ActivityObject::fromRssAuthor($authorEl);
258         } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) {
259             $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
260         } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) {
261             // Special case for Posterous.com
262             $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
263         } else if (!empty($channel)) {
264             $this->actor = ActivityObject::fromRssChannel($channel);
265         } else {
266             // No actor!
267         }
268
269         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
270
271         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, self::CONTENTNS);
272
273         if (!empty($contentEl)) {
274             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
275         } else {
276             $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
277             if (!empty($descriptionEl)) {
278                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
279             }
280         }
281
282         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
283
284         // @fixme enclosures
285         // @fixme thumbnails... maybe
286
287         $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
288
289         if (!empty($guidEl)) {
290             $this->id = $guidEl->textContent;
291
292             if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
293                 // overwrites <link>
294                 $this->link = $this->id;
295             }
296         }
297
298         $this->objects[] = new ActivityObject($item);
299         $this->context   = new ActivityContext($item);
300     }
301
302     /**
303      * Returns an Atom <entry> based on this activity
304      *
305      * @return DOMElement Atom entry
306      */
307
308     function toAtomEntry()
309     {
310         return null;
311     }
312
313     function asString($namespace=false)
314     {
315         $xs = new XMLStringer(true);
316
317         if ($namespace) {
318             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
319                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
320                            'xmlns:georss' => 'http://www.georss.org/georss',
321                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
322                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
323                            'xmlns:media' => 'http://purl.org/syndication/atommedia');
324         } else {
325             $attrs = array();
326         }
327
328         $xs->elementStart('entry', $attrs);
329
330         $xs->element('id', null, $this->id);
331         $xs->element('title', null, $this->title);
332         $xs->element('published', null, common_date_iso8601($this->time));
333         $xs->element('content', array('type' => 'html'), $this->content);
334
335         if (!empty($this->summary)) {
336             $xs->element('summary', null, $this->summary);
337         }
338
339         if (!empty($this->link)) {
340             $xs->element('link', array('rel' => 'alternate',
341                                        'type' => 'text/html'),
342                          $this->link);
343         }
344
345         // XXX: add context
346
347         $xs->elementStart('author');
348         $xs->element('uri', array(), $this->actor->id);
349         if ($this->actor->title) {
350             $xs->element('name', array(), $this->actor->title);
351         }
352         $xs->elementEnd('author');
353         $xs->raw($this->actor->asString('activity:actor'));
354
355         $xs->element('activity:verb', null, $this->verb);
356
357         if (!empty($this->objects)) {
358             foreach($this->objects as $object) {
359                 $xs->raw($object->asString());
360             }
361         }
362
363         if ($this->target) {
364             $xs->raw($this->target->asString('activity:target'));
365         }
366
367         foreach ($this->categories as $cat) {
368             $xs->raw($cat->asString());
369         }
370
371         $xs->elementEnd('entry');
372
373         return $xs->getString();
374     }
375
376     private function _child($element, $tag, $namespace=self::SPEC)
377     {
378         return ActivityUtils::child($element, $tag, $namespace);
379     }
380 }
381