]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Style for invite forms and width fix for Onboard popup.
[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 _LIST     = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
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 $owner;
97     public $link;
98     public $source;
99     public $avatarLinks = array();
100     public $geopoint;
101     public $poco;
102     public $displayName;
103
104     // @todo move this stuff to it's own PHOTO activity object
105     const MEDIA_DESCRIPTION = 'description';
106
107     public $thumbnail;
108     public $largerImage;
109     public $description;
110     public $extra = array();
111
112     /**
113      * Constructor
114      *
115      * This probably needs to be refactored
116      * to generate a local class (ActivityPerson, ActivityFile, ...)
117      * based on the object type.
118      *
119      * @param DOMElement $element DOM thing to turn into an Activity thing
120      */
121     function __construct($element = null)
122     {
123         if (empty($element)) {
124             return;
125         }
126
127         $this->element = $element;
128
129         $this->geopoint = $this->_childContent(
130             $element,
131             ActivityContext::POINT,
132             ActivityContext::GEORSS
133         );
134
135         if ($element->tagName == 'author') {
136             $this->_fromAuthor($element);
137         } else if ($element->tagName == 'item') {
138             $this->_fromRssItem($element);
139         } else {
140             $this->_fromAtomEntry($element);
141         }
142
143         // Some per-type attributes...
144         if ($this->type == self::PERSON || $this->type == self::GROUP) {
145             $this->displayName = $this->title;
146
147             $photos = ActivityUtils::getLinks($element, 'photo');
148             if (count($photos)) {
149                 foreach ($photos as $link) {
150                     $this->avatarLinks[] = new AvatarLink($link);
151                 }
152             } else {
153                 $avatars = ActivityUtils::getLinks($element, 'avatar');
154                 foreach ($avatars as $link) {
155                     $this->avatarLinks[] = new AvatarLink($link);
156                 }
157             }
158
159             $this->poco = new PoCo($element);
160         }
161
162         if ($this->type == self::PHOTO) {
163
164             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
165             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
166
167             $this->description = ActivityUtils::childContent(
168                 $element,
169                 ActivityObject::MEDIA_DESCRIPTION,
170                 Activity::MEDIA
171             );
172         }
173         if ($this->type == self::_LIST) {
174             $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
175             $this->owner = new ActivityObject($owner);
176         }
177     }
178
179     private function _fromAuthor($element)
180     {
181         $this->type = $this->_childContent($element,
182                                            Activity::OBJECTTYPE,
183                                            Activity::SPEC);
184
185         if (empty($this->type)) {
186             $this->type = self::PERSON; // XXX: is this fair?
187         }
188
189         // start with <atom:title>
190
191         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
192
193         if (!empty($title)) {
194             $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
195         }
196
197         // fall back to <atom:name>
198
199         if (empty($this->title)) {
200             $this->title = $this->_childContent($element, self::NAME);
201         }
202
203         // start with <atom:id>
204
205         $this->id = $this->_childContent($element, self::ID);
206
207         // fall back to <atom:uri>
208
209         if (empty($this->id)) {
210             $this->id = $this->_childContent($element, self::URI);
211         }
212
213         // fall further back to <atom:email>
214
215         if (empty($this->id)) {
216             $email = $this->_childContent($element, self::EMAIL);
217             if (!empty($email)) {
218                 // XXX: acct: ?
219                 $this->id = 'mailto:'.$email;
220             }
221         }
222
223         $this->link = ActivityUtils::getPermalink($element);
224
225         // fall finally back to <link rel=alternate>
226
227         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
228             $this->id = $this->link;
229         }
230     }
231
232     private function _fromAtomEntry($element)
233     {
234         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
235                                            Activity::SPEC);
236
237         if (empty($this->type)) {
238             $this->type = ActivityObject::NOTE;
239         }
240
241         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
242         $this->content = ActivityUtils::getContent($element);
243
244         // We don't like HTML in our titles, although it's technically allowed
245
246         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
247
248         $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
249
250         $this->source  = $this->_getSource($element);
251
252         $this->link = ActivityUtils::getPermalink($element);
253
254         $this->id = $this->_childContent($element, self::ID);
255
256         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
257             $this->id = $this->link;
258         }
259     }
260
261     // @todo FIXME: rationalize with Activity::_fromRssItem()
262     private function _fromRssItem($item)
263     {
264         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
265
266         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
267
268         if (!empty($contentEl)) {
269             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
270         } else {
271             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
272             if (!empty($descriptionEl)) {
273                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
274             }
275         }
276
277         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
278
279         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
280
281         if (!empty($guidEl)) {
282             $this->id = $guidEl->textContent;
283
284             if ($guidEl->hasAttribute('isPermaLink')) {
285                 // overwrites <link>
286                 $this->link = $this->id;
287             }
288         }
289     }
290
291     public static function fromRssAuthor($el)
292     {
293         $text = $el->textContent;
294
295         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
296             $email = $match[1];
297             $name = $match[2];
298         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
299             $name = $match[1];
300             $email = $match[2];
301         } else if (preg_match('/.*@.*/', $text)) {
302             $email = $text;
303             $name = null;
304         } else {
305             $name = $text;
306             $email = null;
307         }
308
309         // Not really enough info
310
311         $obj = new ActivityObject();
312
313         $obj->element = $el;
314
315         $obj->type  = ActivityObject::PERSON;
316         $obj->title = $name;
317
318         if (!empty($email)) {
319             $obj->id = 'mailto:'.$email;
320         }
321
322         return $obj;
323     }
324
325     public static function fromDcCreator($el)
326     {
327         // Not really enough info
328
329         $text = $el->textContent;
330
331         $obj = new ActivityObject();
332
333         $obj->element = $el;
334
335         $obj->title = $text;
336         $obj->type  = ActivityObject::PERSON;
337
338         return $obj;
339     }
340
341     public static function fromRssChannel($el)
342     {
343         $obj = new ActivityObject();
344
345         $obj->element = $el;
346
347         $obj->type = ActivityObject::PERSON; // @fixme guess better
348
349         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
350         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
351         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
352
353         if (empty($obj->id)) {
354             $obj->id = $obj->link;
355         }
356
357         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
358
359         if (!empty($desc)) {
360             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
361         }
362
363         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
364
365         if (!empty($imageEl)) {
366             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
367             $al = new AvatarLink();
368             $al->url = $url;
369             $obj->avatarLinks[] = $al;
370         }
371
372         return $obj;
373     }
374
375     public static function fromPosterousAuthor($el)
376     {
377         $obj = new ActivityObject();
378
379         $obj->type = ActivityObject::PERSON; // @fixme any others...?
380
381         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
382
383         if (!empty($userImage)) {
384             $al = new AvatarLink();
385             $al->url = $userImage;
386             $obj->avatarLinks[] = $al;
387         }
388
389         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
390         $obj->id   = $obj->link;
391
392         $obj->poco = new PoCo();
393
394         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
395         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
396
397         $obj->title = $obj->poco->displayName;
398
399         return $obj;
400     }
401
402     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
403     {
404         return ActivityUtils::childContent($element, $tag, $namespace);
405     }
406
407     // Try to get a unique id for the source feed
408
409     private function _getSource($element)
410     {
411         $sourceEl = ActivityUtils::child($element, 'source');
412
413         if (empty($sourceEl)) {
414             return null;
415         } else {
416             $href = ActivityUtils::getLink($sourceEl, 'self');
417             if (!empty($href)) {
418                 return $href;
419             } else {
420                 return ActivityUtils::childContent($sourceEl, 'id');
421             }
422         }
423     }
424
425     static function fromNotice(Notice $notice)
426     {
427         $object = new ActivityObject();
428
429         if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
430
431             $object->type    = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
432
433             $object->id      = $notice->uri;
434             $object->title   = $notice->content;
435             $object->content = $notice->rendered;
436             $object->link    = $notice->bestUrl();
437
438             Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
439         }
440
441         return $object;
442     }
443
444     static function fromProfile(Profile $profile)
445     {
446         $object = new ActivityObject();
447
448         if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
449             $object->type   = ActivityObject::PERSON;
450             $object->id     = $profile->getUri();
451             $object->title  = $profile->getBestName();
452             $object->link   = $profile->profileurl;
453
454             $orig = $profile->getOriginalAvatar();
455
456             if (!empty($orig)) {
457                 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
458             }
459
460             $sizes = array(
461                 AVATAR_PROFILE_SIZE,
462                 AVATAR_STREAM_SIZE,
463                 AVATAR_MINI_SIZE
464             );
465
466             foreach ($sizes as $size) {
467                 $alink  = null;
468                 $avatar = $profile->getAvatar($size);
469
470                 if (!empty($avatar)) {
471                     $alink = AvatarLink::fromAvatar($avatar);
472                 } else {
473                     $alink = new AvatarLink();
474                     $alink->type   = 'image/png';
475                     $alink->height = $size;
476                     $alink->width  = $size;
477                     $alink->url    = Avatar::defaultImage($size);
478
479                     if ($size == AVATAR_PROFILE_SIZE) {
480                         // Hack for Twitter import: we don't have a 96x96 image,
481                         // but we do have a 73x73 image. For now, fake it with that.
482                         $avatar = $profile->getAvatar(73);
483                         if ($avatar) {
484                             $alink = AvatarLink::fromAvatar($avatar);
485                             $alink->height= $size;
486                             $alink->width = $size;
487                         }
488                     }
489                 }
490
491                 $object->avatarLinks[] = $alink;
492             }
493
494             if (isset($profile->lat) && isset($profile->lon)) {
495                 $object->geopoint = (float)$profile->lat
496                     . ' ' . (float)$profile->lon;
497             }
498
499             $object->poco = PoCo::fromProfile($profile);
500
501             Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
502         }
503
504         return $object;
505     }
506
507     static function fromGroup($group)
508     {
509         $object = new ActivityObject();
510
511         if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
512
513             $object->type   = ActivityObject::GROUP;
514             $object->id     = $group->getUri();
515             $object->title  = $group->getBestName();
516             $object->link   = $group->getUri();
517
518             $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
519                                                               AVATAR_PROFILE_SIZE);
520
521             $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
522                                                               AVATAR_STREAM_SIZE);
523
524             $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
525                                                               AVATAR_MINI_SIZE);
526
527             $object->poco = PoCo::fromGroup($group);
528             Event::handle('EndActivityObjectFromGroup', array($group, &$object));
529         }
530
531         return $object;
532     }
533
534     static function fromPeopletag($ptag)
535     {
536         $object = new ActivityObject();
537         if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
538             $object->type    = ActivityObject::_LIST;
539
540             $object->id      = $ptag->getUri();
541             $object->title   = $ptag->tag;
542             $object->summary = $ptag->description;
543             $object->link    = $ptag->homeUrl();
544             $object->owner   = Profile::staticGet('id', $ptag->tagger);
545             $object->poco    = PoCo::fromProfile($object->owner);
546             Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
547         }
548         return $object;
549     }
550
551     function outputTo($xo, $tag='activity:object')
552     {
553         if (!empty($tag)) {
554             $xo->elementStart($tag);
555         }
556
557         if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
558             $xo->element('activity:object-type', null, $this->type);
559
560             // <author> uses URI
561
562             if ($tag == 'author') {
563                 $xo->element(self::URI, null, $this->id);
564             } else {
565                 $xo->element(self::ID, null, $this->id);
566             }
567
568             if (!empty($this->title)) {
569                 $name = common_xml_safe_str($this->title);
570                 if ($tag == 'author') {
571                     // XXX: Backward compatibility hack -- atom:name should contain
572                     // full name here, instead of nickname, i.e.: $name. Change
573                     // this in the next version.
574                     $xo->element(self::NAME, null, $this->poco->preferredUsername);
575                 } else {
576                     $xo->element(self::TITLE, null, $name);
577                 }
578             }
579
580             if (!empty($this->summary)) {
581                 $xo->element(
582                     self::SUMMARY,
583                     null,
584                     common_xml_safe_str($this->summary)
585                 );
586             }
587
588             if (!empty($this->content)) {
589                 // XXX: assuming HTML content here
590                 $xo->element(
591                     ActivityUtils::CONTENT,
592                     array('type' => 'html'),
593                     common_xml_safe_str($this->content)
594                 );
595             }
596
597             if (!empty($this->link)) {
598                 $xo->element(
599                     'link',
600                     array(
601                         'rel' => 'alternate',
602                         'type' => 'text/html',
603                         'href' => $this->link
604                     ),
605                     null
606                 );
607             }
608
609             if(!empty($this->owner)) {
610                 $owner = $this->owner->asActivityNoun(self::AUTHOR);
611                 $xo->raw($owner);
612             }
613
614             if ($this->type == ActivityObject::PERSON
615                 || $this->type == ActivityObject::GROUP) {
616
617                 foreach ($this->avatarLinks as $avatar) {
618                     $xo->element(
619                         'link', array(
620                             'rel'  => 'avatar',
621                             'type'         => $avatar->type,
622                             'media:width'  => $avatar->width,
623                             'media:height' => $avatar->height,
624                             'href' => $avatar->url
625                         ),
626                         null
627                     );
628                 }
629             }
630
631             if (!empty($this->geopoint)) {
632                 $xo->element(
633                     'georss:point',
634                     null,
635                     $this->geopoint
636                 );
637             }
638
639             if (!empty($this->poco)) {
640                 $this->poco->outputTo($xo);
641             }
642
643             // @fixme there's no way here to make a tree; elements can only contain plaintext
644             // @fixme these may collide with JSON extensions
645             foreach ($this->extra as $el) {
646                 list($extraTag, $attrs, $content) = $el;
647                 $xo->element($extraTag, $attrs, $content);
648             }
649
650             Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
651         }
652
653         if (!empty($tag)) {
654             $xo->elementEnd($tag);
655         }
656
657         return;
658     }
659
660     function asString($tag='activity:object')
661     {
662         $xs = new XMLStringer(true);
663
664         $this->outputTo($xs, $tag);
665
666         return $xs->getString();
667     }
668
669     /*
670      * Returns an array based on this Activity Object suitable for
671      * encoding as JSON.
672      *
673      * @return array $object the activity object array
674      */
675
676     function asArray()
677     {
678         $object = array();
679
680         if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
681             // XXX: attachedObjects are added by Activity
682
683             // displayName
684             $object['displayName'] = $this->title;
685
686             // TODO: downstreamDuplicates
687
688             // embedCode (used for video)
689
690             // id
691             //
692             // XXX: Should we use URL here? or a crazy tag URI?
693             $object['id'] = $this->id;
694
695             if ($this->type == ActivityObject::PERSON
696                 || $this->type == ActivityObject::GROUP) {
697
698                 // XXX: Not sure what the best avatar is to use for the
699                 // author's "image". For now, I'm using the large size.
700
701                 $avatarLarge      = null;
702                 $avatarMediaLinks = array();
703
704                 foreach ($this->avatarLinks as $a) {
705
706                     // Make a MediaLink for every other Avatar
707                     $avatar = new ActivityStreamsMediaLink(
708                         $a->url,
709                         $a->width,
710                         $a->height,
711                         $a->type,
712                         'avatar'
713                     );
714
715                     // Find the big avatar to use as the "image"
716                     if ($a->height == AVATAR_PROFILE_SIZE) {
717                         $imgLink = $avatar;
718                     }
719
720                     $avatarMediaLinks[] = $avatar->asArray();
721                 }
722
723                 $object['avatarLinks'] = $avatarMediaLinks; // extension
724
725                 // image
726                 $object['image']  = $imgLink->asArray();
727             }
728
729             // objectType
730             //
731             // We can probably use the whole schema URL here but probably the
732             // relative simple name is easier to parse
733             // @fixme this breaks extension URIs
734             $object['type'] = substr($this->type, strrpos($this->type, '/') + 1);
735
736             // summary
737             $object['summary'] = $this->summary;
738
739             // TODO: upstreamDuplicates
740
741             // url (XXX: need to put the right thing here...)
742             $object['url'] = $this->id;
743
744             /* Extensions */
745             // @fixme these may collide with XML extensions
746             // @fixme multiple tags of same name will overwrite each other
747             // @fixme text content from XML extensions will be lost
748             foreach ($this->extra as $e) {
749                 list($objectName, $props, $txt) = $e;
750                 $object[$objectName] = $props;
751             }
752
753             // GeoJSON
754
755             if (!empty($this->geopoint)) {
756
757                 list($lat, $long) = explode(' ', $this->geopoint);
758
759                 $object['geopoint'] = array(
760                     'type'        => 'Point',
761                     'coordinates' => array($lat, $long)
762                 );
763             }
764
765             if (!empty($this->poco)) {
766                 $object['contact'] = $this->poco->asArray();
767             }
768             Event::handle('EndActivityObjectOutputJson', array($this, &$object));
769         }
770         return array_filter($object);
771     }
772 }