]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
b1e9071eda73ab238ec668cc2b49a16759bd6e73
[quix0rs-gnu-social.git] / lib / activityobject.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  * A noun-ish thing in the activity universe
37  *
38  * The activity streams spec talks about activity objects, while also having
39  * a tag activity:object, which is in fact an activity object. Aaaaaah!
40  *
41  * This is just a thing in the activity universe. Can be the subject, object,
42  * or indirect object (target!) of an activity verb. Rotten name, and I'm
43  * propagating it. *sigh*
44  *
45  * @category  OStatus
46  * @package   StatusNet
47  * @author    Evan Prodromou <evan@status.net>
48  * @copyright 2010 StatusNet, Inc.
49  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50  * @link      http://status.net/
51  */
52
53 class ActivityObject
54 {
55     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
56     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
57     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
58     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
59     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
60     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
61     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
62     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
63     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
64     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
65     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
66     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
67     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
68     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
69     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
70     // ^^^^^^^^^^ tea!
71
72     // Atom elements we snarf
73
74     const TITLE   = 'title';
75     const SUMMARY = 'summary';
76     const ID      = 'id';
77     const SOURCE  = 'source';
78
79     const NAME  = 'name';
80     const URI   = 'uri';
81     const EMAIL = 'email';
82
83     public $element;
84     public $type;
85     public $id;
86     public $title;
87     public $summary;
88     public $content;
89     public $link;
90     public $source;
91     public $avatarLinks = array();
92     public $geopoint;
93     public $poco;
94     public $displayName;
95
96     /**
97      * Constructor
98      *
99      * This probably needs to be refactored
100      * to generate a local class (ActivityPerson, ActivityFile, ...)
101      * based on the object type.
102      *
103      * @param DOMElement $element DOM thing to turn into an Activity thing
104      */
105
106     function __construct($element = null)
107     {
108         if (empty($element)) {
109             return;
110         }
111
112         $this->element = $element;
113
114         $this->geopoint = $this->_childContent(
115             $element,
116             ActivityContext::POINT,
117             ActivityContext::GEORSS
118         );
119
120         if ($element->tagName == 'author') {
121             $this->_fromAuthor($element);
122         } else if ($element->tagName == 'item') {
123             $this->_fromRssItem($element);
124         } else {
125             $this->_fromAtomEntry($element);
126         }
127
128         // Some per-type attributes...
129         if ($this->type == self::PERSON || $this->type == self::GROUP) {
130             $this->displayName = $this->title;
131
132             $photos = ActivityUtils::getLinks($element, 'photo');
133             if (count($photos)) {
134                 foreach ($photos as $link) {
135                     $this->avatarLinks[] = new AvatarLink($link);
136                 }
137             } else {
138                 $avatars = ActivityUtils::getLinks($element, 'avatar');
139                 foreach ($avatars as $link) {
140                     $this->avatarLinks[] = new AvatarLink($link);
141                 }
142             }
143
144             $this->poco = new PoCo($element);
145         }
146     }
147
148     private function _fromAuthor($element)
149     {
150         $this->type  = self::PERSON; // XXX: is this fair?
151         $this->title = $this->_childContent($element, self::NAME);
152         $this->id    = $this->_childContent($element, self::URI);
153
154         if (empty($this->id)) {
155             $email = $this->_childContent($element, self::EMAIL);
156             if (!empty($email)) {
157                 // XXX: acct: ?
158                 $this->id = 'mailto:'.$email;
159             }
160         }
161     }
162
163     private function _fromAtomEntry($element)
164     {
165         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
166                                            Activity::SPEC);
167
168         if (empty($this->type)) {
169             $this->type = ActivityObject::NOTE;
170         }
171
172         $this->id      = $this->_childContent($element, self::ID);
173         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
174         $this->content = ActivityUtils::getContent($element);
175
176         // We don't like HTML in our titles, although it's technically allowed
177
178         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
179
180         $this->title = html_entity_decode(strip_tags($title));
181
182         $this->source  = $this->_getSource($element);
183
184         $this->link = ActivityUtils::getPermalink($element);
185     }
186
187     // @fixme rationalize with Activity::_fromRssItem()
188
189     private function _fromRssItem($item)
190     {
191         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
192
193         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
194
195         if (!empty($contentEl)) {
196             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
197         } else {
198             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
199             if (!empty($descriptionEl)) {
200                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
201             }
202         }
203
204         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
205
206         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
207
208         if (!empty($guidEl)) {
209             $this->id = $guidEl->textContent;
210
211             if ($guidEl->hasAttribute('isPermaLink')) {
212                 // overwrites <link>
213                 $this->link = $this->id;
214             }
215         }
216     }
217
218     public static function fromRssAuthor($el)
219     {
220         $text = $el->textContent;
221
222         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
223             $email = $match[1];
224             $name = $match[2];
225         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
226             $name = $match[1];
227             $email = $match[2];
228         } else if (preg_match('/.*@.*/', $text)) {
229             $email = $text;
230             $name = null;
231         } else {
232             $name = $text;
233             $email = null;
234         }
235
236         // Not really enough info
237
238         $obj = new ActivityObject();
239
240         $obj->element = $el;
241
242         $obj->type  = ActivityObject::PERSON;
243         $obj->title = $name;
244
245         if (!empty($email)) {
246             $obj->id = 'mailto:'.$email;
247         }
248
249         return $obj;
250     }
251
252     public static function fromDcCreator($el)
253     {
254         // Not really enough info
255
256         $text = $el->textContent;
257
258         $obj = new ActivityObject();
259
260         $obj->element = $el;
261
262         $obj->title = $text;
263         $obj->type  = ActivityObject::PERSON;
264
265         return $obj;
266     }
267
268     public static function fromRssChannel($el)
269     {
270         $obj = new ActivityObject();
271
272         $obj->element = $el;
273
274         $obj->type = ActivityObject::PERSON; // @fixme guess better
275
276         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
277         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
278         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
279
280         if (empty($obj->id)) {
281             $obj->id = $obj->link;
282         }
283
284         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
285
286         if (!empty($desc)) {
287             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
288         }
289
290         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
291
292         if (!empty($imageEl)) {
293             $obj->avatarLinks[] = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
294         }
295
296         return $obj;
297     }
298
299     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
300     {
301         return ActivityUtils::childContent($element, $tag, $namespace);
302     }
303
304     // Try to get a unique id for the source feed
305
306     private function _getSource($element)
307     {
308         $sourceEl = ActivityUtils::child($element, 'source');
309
310         if (empty($sourceEl)) {
311             return null;
312         } else {
313             $href = ActivityUtils::getLink($sourceEl, 'self');
314             if (!empty($href)) {
315                 return $href;
316             } else {
317                 return ActivityUtils::childContent($sourceEl, 'id');
318             }
319         }
320     }
321
322     static function fromNotice(Notice $notice)
323     {
324         $object = new ActivityObject();
325
326         $object->type    = ActivityObject::NOTE;
327
328         $object->id      = $notice->uri;
329         $object->title   = $notice->content;
330         $object->content = $notice->rendered;
331         $object->link    = $notice->bestUrl();
332
333         return $object;
334     }
335
336     static function fromProfile(Profile $profile)
337     {
338         $object = new ActivityObject();
339
340         $object->type   = ActivityObject::PERSON;
341         $object->id     = $profile->getUri();
342         $object->title  = $profile->getBestName();
343         $object->link   = $profile->profileurl;
344
345         $orig = $profile->getOriginalAvatar();
346
347         if (!empty($orig)) {
348             $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
349         }
350
351         $sizes = array(
352             AVATAR_PROFILE_SIZE,
353             AVATAR_STREAM_SIZE,
354             AVATAR_MINI_SIZE
355         );
356
357         foreach ($sizes as $size) {
358
359             $alink  = null;
360             $avatar = $profile->getAvatar($size);
361
362             if (!empty($avatar)) {
363                 $alink = AvatarLink::fromAvatar($avatar);
364             } else {
365                 $alink = new AvatarLink();
366                 $alink->type   = 'image/png';
367                 $alink->height = $size;
368                 $alink->width  = $size;
369                 $alink->url    = Avatar::defaultImage($size);
370             }
371
372             $object->avatarLinks[] = $alink;
373         }
374
375         if (isset($profile->lat) && isset($profile->lon)) {
376             $object->geopoint = (float)$profile->lat
377                 . ' ' . (float)$profile->lon;
378         }
379
380         $object->poco = PoCo::fromProfile($profile);
381
382         return $object;
383     }
384
385     static function fromGroup($group)
386     {
387         $object = new ActivityObject();
388
389         $object->type   = ActivityObject::GROUP;
390         $object->id     = $group->getUri();
391         $object->title  = $group->getBestName();
392         $object->link   = $group->getUri();
393
394         $object->avatarLinks[] = AvatarLink::fromFilename(
395             $group->homepage_logo,
396             AVATAR_PROFILE_SIZE
397         );
398
399         $object->avatarLinks[] = AvatarLink::fromFilename(
400             $group->stream_logo,
401             AVATAR_STREAM_SIZE
402         );
403
404         $object->avatarLinks[] = AvatarLink::fromFilename(
405             $group->mini_logo,
406             AVATAR_MINI_SIZE
407         );
408
409         $object->poco = PoCo::fromGroup($group);
410
411         return $object;
412     }
413
414     function asString($tag='activity:object')
415     {
416         $xs = new XMLStringer(true);
417
418         $xs->elementStart($tag);
419
420         $xs->element('activity:object-type', null, $this->type);
421
422         $xs->element(self::ID, null, $this->id);
423
424         if (!empty($this->title)) {
425             $xs->element(
426                 self::TITLE,
427                 null,
428                 common_xml_safe_str($this->title)
429             );
430         }
431
432         if (!empty($this->summary)) {
433             $xs->element(
434                 self::SUMMARY,
435                 null,
436                 common_xml_safe_str($this->summary)
437             );
438         }
439
440         if (!empty($this->content)) {
441             // XXX: assuming HTML content here
442             $xs->element(
443                 ActivityUtils::CONTENT,
444                 array('type' => 'html'),
445                 common_xml_safe_str($this->content)
446             );
447         }
448
449         if (!empty($this->link)) {
450             $xs->element(
451                 'link',
452                 array(
453                     'rel' => 'alternate',
454                     'type' => 'text/html',
455                     'href' => $this->link
456                 ),
457                 null
458             );
459         }
460
461         if ($this->type == ActivityObject::PERSON
462             || $this->type == ActivityObject::GROUP) {
463
464             foreach ($this->avatarLinks as $avatar) {
465                 $xs->element(
466                     'link', array(
467                         'rel'  => 'avatar',
468                         'type'         => $avatar->type,
469                         'media:width'  => $avatar->width,
470                         'media:height' => $avatar->height,
471                         'href' => $avatar->url
472                     ),
473                     null
474                 );
475             }
476         }
477
478         if (!empty($this->geopoint)) {
479             $xs->element(
480                 'georss:point',
481                 null,
482                 $this->geopoint
483             );
484         }
485
486         if (!empty($this->poco)) {
487             $xs->raw($this->poco->asString());
488         }
489
490         $xs->elementEnd($tag);
491
492         return $xs->getString();
493     }
494 }