]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Pull <atom:author> info as well as <activity:actor> when we have an old-style Activit...
[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     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
84     const AUTHOR      = 'author';
85     const USERIMAGE   = 'userImage';
86     const PROFILEURL  = 'profileUrl';
87     const NICKNAME    = 'nickName';
88     const DISPLAYNAME = 'displayName';
89
90     public $element;
91     public $type;
92     public $id;
93     public $title;
94     public $summary;
95     public $content;
96     public $link;
97     public $source;
98     public $avatarLinks = array();
99     public $geopoint;
100     public $poco;
101     public $displayName;
102
103     /**
104      * Constructor
105      *
106      * This probably needs to be refactored
107      * to generate a local class (ActivityPerson, ActivityFile, ...)
108      * based on the object type.
109      *
110      * @param DOMElement $element DOM thing to turn into an Activity thing
111      */
112
113     function __construct($element = null)
114     {
115         if (empty($element)) {
116             return;
117         }
118
119         $this->element = $element;
120
121         $this->geopoint = $this->_childContent(
122             $element,
123             ActivityContext::POINT,
124             ActivityContext::GEORSS
125         );
126
127         if ($element->tagName == 'author') {
128             $this->_fromAuthor($element);
129         } else if ($element->tagName == 'item') {
130             $this->_fromRssItem($element);
131         } else {
132             $this->_fromAtomEntry($element);
133         }
134
135         // Some per-type attributes...
136         if ($this->type == self::PERSON || $this->type == self::GROUP) {
137             $this->displayName = $this->title;
138
139             $photos = ActivityUtils::getLinks($element, 'photo');
140             if (count($photos)) {
141                 foreach ($photos as $link) {
142                     $this->avatarLinks[] = new AvatarLink($link);
143                 }
144             } else {
145                 $avatars = ActivityUtils::getLinks($element, 'avatar');
146                 foreach ($avatars as $link) {
147                     $this->avatarLinks[] = new AvatarLink($link);
148                 }
149             }
150
151             $this->poco = new PoCo($element);
152         }
153     }
154
155     private function _fromAuthor($element)
156     {
157         $this->type  = self::PERSON; // XXX: is this fair?
158         $this->title = $this->_childContent($element, self::NAME);
159
160         $id = $this->_childContent($element, self::URI);
161         if (ActivityUtils::validateUri($id)) {
162             $this->id = $id;
163         }
164
165         if (empty($this->id)) {
166             $email = $this->_childContent($element, self::EMAIL);
167             if (!empty($email)) {
168                 // XXX: acct: ?
169                 $this->id = 'mailto:'.$email;
170             }
171         }
172     }
173
174     private function _fromAtomEntry($element)
175     {
176         if ($element->localName == 'actor') {
177             // Old-fashioned <activity:actor>...
178             // First pull anything from <author>, then we'll add on top.
179             $author = ActivityUtils::child($element->parentNode, 'author');
180             if ($author) {
181                 $this->_fromAuthor($author);
182             }
183         }
184
185         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
186                                            Activity::SPEC);
187
188         if (empty($this->type)) {
189             $this->type = ActivityObject::NOTE;
190         }
191
192         $id = $this->_childContent($element, self::ID);
193         if (ActivityUtils::validateUri($id)) {
194             $this->id = $id;
195         }
196
197         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
198         $this->content = ActivityUtils::getContent($element);
199
200         // We don't like HTML in our titles, although it's technically allowed
201
202         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
203
204         $this->title = html_entity_decode(strip_tags($title));
205
206         $this->source  = $this->_getSource($element);
207
208         $this->link = ActivityUtils::getPermalink($element);
209     }
210
211     // @fixme rationalize with Activity::_fromRssItem()
212
213     private function _fromRssItem($item)
214     {
215         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
216
217         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
218
219         if (!empty($contentEl)) {
220             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
221         } else {
222             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
223             if (!empty($descriptionEl)) {
224                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
225             }
226         }
227
228         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
229
230         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
231
232         if (!empty($guidEl)) {
233             $this->id = $guidEl->textContent;
234
235             if ($guidEl->hasAttribute('isPermaLink')) {
236                 // overwrites <link>
237                 $this->link = $this->id;
238             }
239         }
240     }
241
242     public static function fromRssAuthor($el)
243     {
244         $text = $el->textContent;
245
246         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
247             $email = $match[1];
248             $name = $match[2];
249         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
250             $name = $match[1];
251             $email = $match[2];
252         } else if (preg_match('/.*@.*/', $text)) {
253             $email = $text;
254             $name = null;
255         } else {
256             $name = $text;
257             $email = null;
258         }
259
260         // Not really enough info
261
262         $obj = new ActivityObject();
263
264         $obj->element = $el;
265
266         $obj->type  = ActivityObject::PERSON;
267         $obj->title = $name;
268
269         if (!empty($email)) {
270             $obj->id = 'mailto:'.$email;
271         }
272
273         return $obj;
274     }
275
276     public static function fromDcCreator($el)
277     {
278         // Not really enough info
279
280         $text = $el->textContent;
281
282         $obj = new ActivityObject();
283
284         $obj->element = $el;
285
286         $obj->title = $text;
287         $obj->type  = ActivityObject::PERSON;
288
289         return $obj;
290     }
291
292     public static function fromRssChannel($el)
293     {
294         $obj = new ActivityObject();
295
296         $obj->element = $el;
297
298         $obj->type = ActivityObject::PERSON; // @fixme guess better
299
300         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
301         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
302         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
303
304         if (empty($obj->id)) {
305             $obj->id = $obj->link;
306         }
307
308         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
309
310         if (!empty($desc)) {
311             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
312         }
313
314         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
315
316         if (!empty($imageEl)) {
317             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
318             $al = new AvatarLink();
319             $al->url = $url;
320             $obj->avatarLinks[] = $al;
321         }
322
323         return $obj;
324     }
325
326     public static function fromPosterousAuthor($el)
327     {
328         $obj = new ActivityObject();
329
330         $obj->type = ActivityObject::PERSON; // @fixme any others...?
331
332         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
333
334         if (!empty($userImage)) {
335             $al = new AvatarLink();
336             $al->url = $userImage;
337             $obj->avatarLinks[] = $al;
338         }
339
340         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
341         $obj->id   = $obj->link;
342
343         $obj->poco = new PoCo();
344
345         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
346         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
347
348         $obj->title = $obj->poco->displayName;
349
350         return $obj;
351     }
352
353     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
354     {
355         return ActivityUtils::childContent($element, $tag, $namespace);
356     }
357
358     // Try to get a unique id for the source feed
359
360     private function _getSource($element)
361     {
362         $sourceEl = ActivityUtils::child($element, 'source');
363
364         if (empty($sourceEl)) {
365             return null;
366         } else {
367             $href = ActivityUtils::getLink($sourceEl, 'self');
368             if (!empty($href)) {
369                 return $href;
370             } else {
371                 return ActivityUtils::childContent($sourceEl, 'id');
372             }
373         }
374     }
375
376     static function fromNotice(Notice $notice)
377     {
378         $object = new ActivityObject();
379
380         $object->type    = ActivityObject::NOTE;
381
382         $object->id      = $notice->uri;
383         $object->title   = $notice->content;
384         $object->content = $notice->rendered;
385         $object->link    = $notice->bestUrl();
386
387         return $object;
388     }
389
390     static function fromProfile(Profile $profile)
391     {
392         $object = new ActivityObject();
393
394         $object->type   = ActivityObject::PERSON;
395         $object->id     = $profile->getUri();
396         $object->title  = $profile->getBestName();
397         $object->link   = $profile->profileurl;
398
399         $orig = $profile->getOriginalAvatar();
400
401         if (!empty($orig)) {
402             $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
403         }
404
405         $sizes = array(
406             AVATAR_PROFILE_SIZE,
407             AVATAR_STREAM_SIZE,
408             AVATAR_MINI_SIZE
409         );
410
411         foreach ($sizes as $size) {
412
413             $alink  = null;
414             $avatar = $profile->getAvatar($size);
415
416             if (!empty($avatar)) {
417                 $alink = AvatarLink::fromAvatar($avatar);
418             } else {
419                 $alink = new AvatarLink();
420                 $alink->type   = 'image/png';
421                 $alink->height = $size;
422                 $alink->width  = $size;
423                 $alink->url    = Avatar::defaultImage($size);
424             }
425
426             $object->avatarLinks[] = $alink;
427         }
428
429         if (isset($profile->lat) && isset($profile->lon)) {
430             $object->geopoint = (float)$profile->lat
431                 . ' ' . (float)$profile->lon;
432         }
433
434         $object->poco = PoCo::fromProfile($profile);
435
436         return $object;
437     }
438
439     static function fromGroup($group)
440     {
441         $object = new ActivityObject();
442
443         $object->type   = ActivityObject::GROUP;
444         $object->id     = $group->getUri();
445         $object->title  = $group->getBestName();
446         $object->link   = $group->getUri();
447
448         $object->avatarLinks[] = AvatarLink::fromFilename(
449             $group->homepage_logo,
450             AVATAR_PROFILE_SIZE
451         );
452
453         $object->avatarLinks[] = AvatarLink::fromFilename(
454             $group->stream_logo,
455             AVATAR_STREAM_SIZE
456         );
457
458         $object->avatarLinks[] = AvatarLink::fromFilename(
459             $group->mini_logo,
460             AVATAR_MINI_SIZE
461         );
462
463         $object->poco = PoCo::fromGroup($group);
464
465         return $object;
466     }
467
468     function asString($tag='activity:object')
469     {
470         $xs = new XMLStringer(true);
471
472         $xs->elementStart($tag);
473
474         $xs->element('activity:object-type', null, $this->type);
475
476         $xs->element(self::ID, null, $this->id);
477
478         if (!empty($this->title)) {
479             $xs->element(
480                 self::TITLE,
481                 null,
482                 common_xml_safe_str($this->title)
483             );
484         }
485
486         if (!empty($this->summary)) {
487             $xs->element(
488                 self::SUMMARY,
489                 null,
490                 common_xml_safe_str($this->summary)
491             );
492         }
493
494         if (!empty($this->content)) {
495             // XXX: assuming HTML content here
496             $xs->element(
497                 ActivityUtils::CONTENT,
498                 array('type' => 'html'),
499                 common_xml_safe_str($this->content)
500             );
501         }
502
503         if (!empty($this->link)) {
504             $xs->element(
505                 'link',
506                 array(
507                     'rel' => 'alternate',
508                     'type' => 'text/html',
509                     'href' => $this->link
510                 ),
511                 null
512             );
513         }
514
515         if ($this->type == ActivityObject::PERSON
516             || $this->type == ActivityObject::GROUP) {
517
518             foreach ($this->avatarLinks as $avatar) {
519                 $xs->element(
520                     'link', array(
521                         'rel'  => 'avatar',
522                         'type'         => $avatar->type,
523                         'media:width'  => $avatar->width,
524                         'media:height' => $avatar->height,
525                         'href' => $avatar->url
526                     ),
527                     null
528                 );
529             }
530         }
531
532         if (!empty($this->geopoint)) {
533             $xs->element(
534                 'georss:point',
535                 null,
536                 $this->geopoint
537             );
538         }
539
540         if (!empty($this->poco)) {
541             $xs->raw($this->poco->asString());
542         }
543
544         $xs->elementEnd($tag);
545
546         return $xs->getString();
547     }
548 }