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