]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
e5cea727b77b343bcaf394e304f91d66a90a9da3
[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         $this->id    = $this->_childContent($element, self::URI);
160
161         if (empty($this->id)) {
162             $email = $this->_childContent($element, self::EMAIL);
163             if (!empty($email)) {
164                 // XXX: acct: ?
165                 $this->id = 'mailto:'.$email;
166             }
167         }
168     }
169
170     private function _fromAtomEntry($element)
171     {
172         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
173                                            Activity::SPEC);
174
175         if (empty($this->type)) {
176             $this->type = ActivityObject::NOTE;
177         }
178
179         $this->id      = $this->_childContent($element, self::ID);
180         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
181         $this->content = ActivityUtils::getContent($element);
182
183         // We don't like HTML in our titles, although it's technically allowed
184
185         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
186
187         $this->title = html_entity_decode(strip_tags($title));
188
189         $this->source  = $this->_getSource($element);
190
191         $this->link = ActivityUtils::getPermalink($element);
192     }
193
194     // @fixme rationalize with Activity::_fromRssItem()
195
196     private function _fromRssItem($item)
197     {
198         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
199
200         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
201
202         if (!empty($contentEl)) {
203             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
204         } else {
205             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
206             if (!empty($descriptionEl)) {
207                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
208             }
209         }
210
211         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
212
213         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
214
215         if (!empty($guidEl)) {
216             $this->id = $guidEl->textContent;
217
218             if ($guidEl->hasAttribute('isPermaLink')) {
219                 // overwrites <link>
220                 $this->link = $this->id;
221             }
222         }
223     }
224
225     public static function fromRssAuthor($el)
226     {
227         $text = $el->textContent;
228
229         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
230             $email = $match[1];
231             $name = $match[2];
232         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
233             $name = $match[1];
234             $email = $match[2];
235         } else if (preg_match('/.*@.*/', $text)) {
236             $email = $text;
237             $name = null;
238         } else {
239             $name = $text;
240             $email = null;
241         }
242
243         // Not really enough info
244
245         $obj = new ActivityObject();
246
247         $obj->element = $el;
248
249         $obj->type  = ActivityObject::PERSON;
250         $obj->title = $name;
251
252         if (!empty($email)) {
253             $obj->id = 'mailto:'.$email;
254         }
255
256         return $obj;
257     }
258
259     public static function fromDcCreator($el)
260     {
261         // Not really enough info
262
263         $text = $el->textContent;
264
265         $obj = new ActivityObject();
266
267         $obj->element = $el;
268
269         $obj->title = $text;
270         $obj->type  = ActivityObject::PERSON;
271
272         return $obj;
273     }
274
275     public static function fromRssChannel($el)
276     {
277         $obj = new ActivityObject();
278
279         $obj->element = $el;
280
281         $obj->type = ActivityObject::PERSON; // @fixme guess better
282
283         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
284         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
285         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
286
287         if (empty($obj->id)) {
288             $obj->id = $obj->link;
289         }
290
291         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
292
293         if (!empty($desc)) {
294             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
295         }
296
297         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
298
299         if (!empty($imageEl)) {
300             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
301             $al = new AvatarLink();
302             $al->url = $url;
303             $obj->avatarLinks[] = $al;
304         }
305
306         return $obj;
307     }
308
309     public static function fromPosterousAuthor($el)
310     {
311         $obj = new ActivityObject();
312
313         $obj->type = ActivityObject::PERSON; // @fixme any others...?
314
315         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
316
317         if (!empty($userImage)) {
318             $al = new AvatarLink();
319             $al->url = $userImage;
320             $obj->avatarLinks[] = $al;
321         }
322
323         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
324         $obj->id   = $obj->link;
325
326         $obj->poco = new PoCo();
327
328         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
329         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
330
331         $obj->title = $obj->poco->displayName;
332
333         return $obj;
334     }
335
336     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
337     {
338         return ActivityUtils::childContent($element, $tag, $namespace);
339     }
340
341     // Try to get a unique id for the source feed
342
343     private function _getSource($element)
344     {
345         $sourceEl = ActivityUtils::child($element, 'source');
346
347         if (empty($sourceEl)) {
348             return null;
349         } else {
350             $href = ActivityUtils::getLink($sourceEl, 'self');
351             if (!empty($href)) {
352                 return $href;
353             } else {
354                 return ActivityUtils::childContent($sourceEl, 'id');
355             }
356         }
357     }
358
359     static function fromNotice(Notice $notice)
360     {
361         $object = new ActivityObject();
362
363         $object->type    = ActivityObject::NOTE;
364
365         $object->id      = $notice->uri;
366         $object->title   = $notice->content;
367         $object->content = $notice->rendered;
368         $object->link    = $notice->bestUrl();
369
370         return $object;
371     }
372
373     static function fromProfile(Profile $profile)
374     {
375         $object = new ActivityObject();
376
377         $object->type   = ActivityObject::PERSON;
378         $object->id     = $profile->getUri();
379         $object->title  = $profile->getBestName();
380         $object->link   = $profile->profileurl;
381
382         $orig = $profile->getOriginalAvatar();
383
384         if (!empty($orig)) {
385             $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
386         }
387
388         $sizes = array(
389             AVATAR_PROFILE_SIZE,
390             AVATAR_STREAM_SIZE,
391             AVATAR_MINI_SIZE
392         );
393
394         foreach ($sizes as $size) {
395
396             $alink  = null;
397             $avatar = $profile->getAvatar($size);
398
399             if (!empty($avatar)) {
400                 $alink = AvatarLink::fromAvatar($avatar);
401             } else {
402                 $alink = new AvatarLink();
403                 $alink->type   = 'image/png';
404                 $alink->height = $size;
405                 $alink->width  = $size;
406                 $alink->url    = Avatar::defaultImage($size);
407             }
408
409             $object->avatarLinks[] = $alink;
410         }
411
412         if (isset($profile->lat) && isset($profile->lon)) {
413             $object->geopoint = (float)$profile->lat
414                 . ' ' . (float)$profile->lon;
415         }
416
417         $object->poco = PoCo::fromProfile($profile);
418
419         return $object;
420     }
421
422     static function fromGroup($group)
423     {
424         $object = new ActivityObject();
425
426         $object->type   = ActivityObject::GROUP;
427         $object->id     = $group->getUri();
428         $object->title  = $group->getBestName();
429         $object->link   = $group->getUri();
430
431         $object->avatarLinks[] = AvatarLink::fromFilename(
432             $group->homepage_logo,
433             AVATAR_PROFILE_SIZE
434         );
435
436         $object->avatarLinks[] = AvatarLink::fromFilename(
437             $group->stream_logo,
438             AVATAR_STREAM_SIZE
439         );
440
441         $object->avatarLinks[] = AvatarLink::fromFilename(
442             $group->mini_logo,
443             AVATAR_MINI_SIZE
444         );
445
446         $object->poco = PoCo::fromGroup($group);
447
448         return $object;
449     }
450
451     function asString($tag='activity:object')
452     {
453         $xs = new XMLStringer(true);
454
455         $xs->elementStart($tag);
456
457         $xs->element('activity:object-type', null, $this->type);
458
459         $xs->element(self::ID, null, $this->id);
460
461         if (!empty($this->title)) {
462             $xs->element(
463                 self::TITLE,
464                 null,
465                 common_xml_safe_str($this->title)
466             );
467         }
468
469         if (!empty($this->summary)) {
470             $xs->element(
471                 self::SUMMARY,
472                 null,
473                 common_xml_safe_str($this->summary)
474             );
475         }
476
477         if (!empty($this->content)) {
478             // XXX: assuming HTML content here
479             $xs->element(
480                 ActivityUtils::CONTENT,
481                 array('type' => 'html'),
482                 common_xml_safe_str($this->content)
483             );
484         }
485
486         if (!empty($this->link)) {
487             $xs->element(
488                 'link',
489                 array(
490                     'rel' => 'alternate',
491                     'type' => 'text/html',
492                     'href' => $this->link
493                 ),
494                 null
495             );
496         }
497
498         if ($this->type == ActivityObject::PERSON
499             || $this->type == ActivityObject::GROUP) {
500
501             foreach ($this->avatarLinks as $avatar) {
502                 $xs->element(
503                     'link', array(
504                         'rel'  => 'avatar',
505                         'type'         => $avatar->type,
506                         'media:width'  => $avatar->width,
507                         'media:height' => $avatar->height,
508                         'href' => $avatar->url
509                     ),
510                     null
511                 );
512             }
513         }
514
515         if (!empty($this->geopoint)) {
516             $xs->element(
517                 'georss:point',
518                 null,
519                 $this->geopoint
520             );
521         }
522
523         if (!empty($this->poco)) {
524             $xs->raw($this->poco->asString());
525         }
526
527         $xs->elementEnd($tag);
528
529         return $xs->getString();
530     }
531 }