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