]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
ebe686a0772b396f5b5964dcae02404509f6c77a
[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 class ActivityObject
53 {
54     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
55     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
57     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
58     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
59     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
60     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
61     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
62     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
63     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
64     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
65     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
66     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
67     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
68     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
69     // ^^^^^^^^^^ tea!
70
71     // Atom elements we snarf
72
73     const TITLE   = 'title';
74     const SUMMARY = 'summary';
75     const ID      = 'id';
76     const SOURCE  = 'source';
77
78     const NAME  = 'name';
79     const URI   = 'uri';
80     const EMAIL = 'email';
81
82     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
83     const AUTHOR      = 'author';
84     const USERIMAGE   = 'userImage';
85     const PROFILEURL  = 'profileUrl';
86     const NICKNAME    = 'nickName';
87     const DISPLAYNAME = 'displayName';
88
89     public $element;
90     public $type;
91     public $id;
92     public $title;
93     public $summary;
94     public $content;
95     public $link;
96     public $source;
97     public $avatarLinks = array();
98     public $geopoint;
99     public $poco;
100     public $displayName;
101
102     // @todo move this stuff to it's own PHOTO activity object
103     const MEDIA_DESCRIPTION = 'description';
104
105     public $thumbnail;
106     public $largerImage;
107     public $description;
108
109         public $extra; // For extra stuff
110
111     /**
112      * Constructor
113      *
114      * This probably needs to be refactored
115      * to generate a local class (ActivityPerson, ActivityFile, ...)
116      * based on the object type.
117      *
118      * @param DOMElement $element DOM thing to turn into an Activity thing
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     private function _fromAuthor($element)
175     {
176         $this->type  = self::PERSON; // XXX: is this fair?
177         $this->title = $this->_childContent($element, self::NAME);
178
179         $this->id = $this->_childContent($element, self::URI);
180
181         if (empty($this->id)) {
182             $email = $this->_childContent($element, self::EMAIL);
183             if (!empty($email)) {
184                 // XXX: acct: ?
185                 $this->id = 'mailto:'.$email;
186             }
187         }
188     }
189
190     private function _fromAtomEntry($element)
191     {
192         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
193                                            Activity::SPEC);
194
195         if (empty($this->type)) {
196             $this->type = ActivityObject::NOTE;
197         }
198
199         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
200         $this->content = ActivityUtils::getContent($element);
201
202         // We don't like HTML in our titles, although it's technically allowed
203
204         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
205
206         $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
207
208         $this->source  = $this->_getSource($element);
209
210         $this->link = ActivityUtils::getPermalink($element);
211
212         $this->id = $this->_childContent($element, self::ID);
213
214         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
215             $this->id = $this->link;
216         }
217     }
218
219     // @todo FIXME: rationalize with Activity::_fromRssItem()
220     private function _fromRssItem($item)
221     {
222         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
223
224         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
225
226         if (!empty($contentEl)) {
227             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
228         } else {
229             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
230             if (!empty($descriptionEl)) {
231                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
232             }
233         }
234
235         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
236
237         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
238
239         if (!empty($guidEl)) {
240             $this->id = $guidEl->textContent;
241
242             if ($guidEl->hasAttribute('isPermaLink')) {
243                 // overwrites <link>
244                 $this->link = $this->id;
245             }
246         }
247     }
248
249     public static function fromRssAuthor($el)
250     {
251         $text = $el->textContent;
252
253         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
254             $email = $match[1];
255             $name = $match[2];
256         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
257             $name = $match[1];
258             $email = $match[2];
259         } else if (preg_match('/.*@.*/', $text)) {
260             $email = $text;
261             $name = null;
262         } else {
263             $name = $text;
264             $email = null;
265         }
266
267         // Not really enough info
268
269         $obj = new ActivityObject();
270
271         $obj->element = $el;
272
273         $obj->type  = ActivityObject::PERSON;
274         $obj->title = $name;
275
276         if (!empty($email)) {
277             $obj->id = 'mailto:'.$email;
278         }
279
280         return $obj;
281     }
282
283     public static function fromDcCreator($el)
284     {
285         // Not really enough info
286
287         $text = $el->textContent;
288
289         $obj = new ActivityObject();
290
291         $obj->element = $el;
292
293         $obj->title = $text;
294         $obj->type  = ActivityObject::PERSON;
295
296         return $obj;
297     }
298
299     public static function fromRssChannel($el)
300     {
301         $obj = new ActivityObject();
302
303         $obj->element = $el;
304
305         $obj->type = ActivityObject::PERSON; // @fixme guess better
306
307         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
308         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
309         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
310
311         if (empty($obj->id)) {
312             $obj->id = $obj->link;
313         }
314
315         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
316
317         if (!empty($desc)) {
318             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
319         }
320
321         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
322
323         if (!empty($imageEl)) {
324             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
325             $al = new AvatarLink();
326             $al->url = $url;
327             $obj->avatarLinks[] = $al;
328         }
329
330         return $obj;
331     }
332
333     public static function fromPosterousAuthor($el)
334     {
335         $obj = new ActivityObject();
336
337         $obj->type = ActivityObject::PERSON; // @fixme any others...?
338
339         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
340
341         if (!empty($userImage)) {
342             $al = new AvatarLink();
343             $al->url = $userImage;
344             $obj->avatarLinks[] = $al;
345         }
346
347         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
348         $obj->id   = $obj->link;
349
350         $obj->poco = new PoCo();
351
352         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
353         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
354
355         $obj->title = $obj->poco->displayName;
356
357         return $obj;
358     }
359
360     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
361     {
362         return ActivityUtils::childContent($element, $tag, $namespace);
363     }
364
365     // Try to get a unique id for the source feed
366
367     private function _getSource($element)
368     {
369         $sourceEl = ActivityUtils::child($element, 'source');
370
371         if (empty($sourceEl)) {
372             return null;
373         } else {
374             $href = ActivityUtils::getLink($sourceEl, 'self');
375             if (!empty($href)) {
376                 return $href;
377             } else {
378                 return ActivityUtils::childContent($sourceEl, 'id');
379             }
380         }
381     }
382
383     static function fromNotice(Notice $notice)
384     {
385         $object = new ActivityObject();
386                 
387                 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
388
389                         $object->type    = ActivityObject::NOTE;
390
391                         $object->id      = $notice->uri;
392                         $object->title   = $notice->content;
393                         $object->content = $notice->rendered;
394                         $object->link    = $notice->bestUrl();
395
396                         Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
397                 }
398
399         return $object;
400     }
401
402     static function fromProfile(Profile $profile)
403     {
404         $object = new ActivityObject();
405
406                 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
407
408                         $object->type   = ActivityObject::PERSON;
409                         $object->id     = $profile->getUri();
410                         $object->title  = $profile->getBestName();
411                         $object->link   = $profile->profileurl;
412
413                         $orig = $profile->getOriginalAvatar();
414
415                         if (!empty($orig)) {
416                                 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
417                         }
418
419                         $sizes = array(
420                 AVATAR_PROFILE_SIZE,
421                 AVATAR_STREAM_SIZE,
422                 AVATAR_MINI_SIZE
423             );
424
425                         foreach ($sizes as $size) {
426                                 $alink  = null;
427                                 $avatar = $profile->getAvatar($size);
428
429                                 if (!empty($avatar)) {
430                                         $alink = AvatarLink::fromAvatar($avatar);
431                                 } else {
432                                         $alink = new AvatarLink();
433                                         $alink->type   = 'image/png';
434                                         $alink->height = $size;
435                                         $alink->width  = $size;
436                                         $alink->url    = Avatar::defaultImage($size);
437
438                                         if ($size == AVATAR_PROFILE_SIZE) {
439                                                 // Hack for Twitter import: we don't have a 96x96 image,
440                                                 // but we do have a 73x73 image. For now, fake it with that.
441                                                 $avatar = $profile->getAvatar(73);
442                                                 if ($avatar) {
443                                                         $alink = AvatarLink::fromAvatar($avatar);
444                                                         $alink->height= $size;
445                                                         $alink->width = $size;
446                                                 }
447                                         }
448                                 }
449
450                                 $object->avatarLinks[] = $alink;
451                         }
452
453                         if (isset($profile->lat) && isset($profile->lon)) {
454                                 $object->geopoint = (float)$profile->lat
455                                         . ' ' . (float)$profile->lon;
456                         }
457
458                         $object->poco = PoCo::fromProfile($profile);
459
460                         Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
461                 }
462
463         return $object;
464     }
465
466     static function fromGroup($group)
467     {
468         $object = new ActivityObject();
469
470                 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
471
472                         $object->type   = ActivityObject::GROUP;
473                         $object->id     = $group->getUri();
474                         $object->title  = $group->getBestName();
475                         $object->link   = $group->getUri();
476
477                         $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
478                                                                                                                           AVATAR_PROFILE_SIZE);
479
480                         $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
481                                                                                                                           AVATAR_STREAM_SIZE);
482
483                         $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
484                                                                                                                           AVATAR_MINI_SIZE);
485
486                         $object->poco = PoCo::fromGroup($group);
487
488                         Event::handle('EndActivityObjectFromGroup', array($group, &$object));
489                 }
490
491         return $object;
492     }
493         
494         function outputTo($xo, $tag='activity:object')
495         {
496                 if (!empty($tag)) {
497                         $xo->elementStart($tag);
498                 }
499
500         $xo->element('activity:object-type', null, $this->type);
501
502         $xo->element(self::ID, null, $this->id);
503
504         if (!empty($this->title)) {
505             $xo->element(
506                 self::TITLE,
507                 null,
508                 common_xml_safe_str($this->title)
509             );
510         }
511
512         if (!empty($this->summary)) {
513             $xo->element(
514                 self::SUMMARY,
515                 null,
516                 common_xml_safe_str($this->summary)
517             );
518         }
519
520         if (!empty($this->content)) {
521             // XXX: assuming HTML content here
522             $xo->element(
523                 ActivityUtils::CONTENT,
524                 array('type' => 'html'),
525                 common_xml_safe_str($this->content)
526             );
527         }
528
529         if (!empty($this->link)) {
530             $xo->element(
531                 'link',
532                 array(
533                     'rel' => 'alternate',
534                     'type' => 'text/html',
535                     'href' => $this->link
536                 ),
537                 null
538             );
539         }
540
541         if ($this->type == ActivityObject::PERSON
542             || $this->type == ActivityObject::GROUP) {
543
544             foreach ($this->avatarLinks as $avatar) {
545                 $xo->element(
546                     'link', array(
547                         'rel'  => 'avatar',
548                         'type'         => $avatar->type,
549                         'media:width'  => $avatar->width,
550                         'media:height' => $avatar->height,
551                         'href' => $avatar->url
552                     ),
553                     null
554                 );
555             }
556         }
557
558         if (!empty($this->geopoint)) {
559             $xo->element(
560                 'georss:point',
561                 null,
562                 $this->geopoint
563             );
564         }
565
566         if (!empty($this->poco)) {
567             $xo->raw($this->poco->asString());
568         }
569
570         foreach ($this->extra as $el) {
571             list($extraTag, $attrs, $content) = $el;
572             $xo->element($extraTag, $attrs, $content);
573         }
574
575                 if (!empty($tag)) {
576                         $xo->elementEnd($tag);
577                 }
578
579         return;
580         }
581
582     function asString($tag='activity:object')
583     {
584         $xs = new XMLStringer(true);
585
586                 $this->outputTo($xs, $tag);
587
588         return $xs->getString();
589     }
590 }