]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
18e3e21ddb2bcd9749e276b23cf706fe45c76d8d
[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             $obj->avatarLinks[] = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
301         }
302
303         return $obj;
304     }
305
306     public static function fromPosterousAuthor($el)
307     {
308         $obj = new ActivityObject();
309
310         $obj->type = ActivityObject::PERSON; // @fixme any others...?
311
312         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
313
314         if (!empty($userImage)) {
315             $obj->avatarLinks[] = $userImage;
316         }
317
318         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
319         $obj->id   = $obj->link;
320
321         $obj->poco = new PoCo();
322
323         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
324         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
325
326         $obj->title = $obj->poco->displayName;
327
328         return $obj;
329     }
330
331     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
332     {
333         return ActivityUtils::childContent($element, $tag, $namespace);
334     }
335
336     // Try to get a unique id for the source feed
337
338     private function _getSource($element)
339     {
340         $sourceEl = ActivityUtils::child($element, 'source');
341
342         if (empty($sourceEl)) {
343             return null;
344         } else {
345             $href = ActivityUtils::getLink($sourceEl, 'self');
346             if (!empty($href)) {
347                 return $href;
348             } else {
349                 return ActivityUtils::childContent($sourceEl, 'id');
350             }
351         }
352     }
353
354     static function fromNotice(Notice $notice)
355     {
356         $object = new ActivityObject();
357
358         $object->type    = ActivityObject::NOTE;
359
360         $object->id      = $notice->uri;
361         $object->title   = $notice->content;
362         $object->content = $notice->rendered;
363         $object->link    = $notice->bestUrl();
364
365         return $object;
366     }
367
368     static function fromProfile(Profile $profile)
369     {
370         $object = new ActivityObject();
371
372         $object->type   = ActivityObject::PERSON;
373         $object->id     = $profile->getUri();
374         $object->title  = $profile->getBestName();
375         $object->link   = $profile->profileurl;
376
377         $orig = $profile->getOriginalAvatar();
378
379         if (!empty($orig)) {
380             $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
381         }
382
383         $sizes = array(
384             AVATAR_PROFILE_SIZE,
385             AVATAR_STREAM_SIZE,
386             AVATAR_MINI_SIZE
387         );
388
389         foreach ($sizes as $size) {
390
391             $alink  = null;
392             $avatar = $profile->getAvatar($size);
393
394             if (!empty($avatar)) {
395                 $alink = AvatarLink::fromAvatar($avatar);
396             } else {
397                 $alink = new AvatarLink();
398                 $alink->type   = 'image/png';
399                 $alink->height = $size;
400                 $alink->width  = $size;
401                 $alink->url    = Avatar::defaultImage($size);
402             }
403
404             $object->avatarLinks[] = $alink;
405         }
406
407         if (isset($profile->lat) && isset($profile->lon)) {
408             $object->geopoint = (float)$profile->lat
409                 . ' ' . (float)$profile->lon;
410         }
411
412         $object->poco = PoCo::fromProfile($profile);
413
414         return $object;
415     }
416
417     static function fromGroup($group)
418     {
419         $object = new ActivityObject();
420
421         $object->type   = ActivityObject::GROUP;
422         $object->id     = $group->getUri();
423         $object->title  = $group->getBestName();
424         $object->link   = $group->getUri();
425
426         $object->avatarLinks[] = AvatarLink::fromFilename(
427             $group->homepage_logo,
428             AVATAR_PROFILE_SIZE
429         );
430
431         $object->avatarLinks[] = AvatarLink::fromFilename(
432             $group->stream_logo,
433             AVATAR_STREAM_SIZE
434         );
435
436         $object->avatarLinks[] = AvatarLink::fromFilename(
437             $group->mini_logo,
438             AVATAR_MINI_SIZE
439         );
440
441         $object->poco = PoCo::fromGroup($group);
442
443         return $object;
444     }
445
446     function asString($tag='activity:object')
447     {
448         $xs = new XMLStringer(true);
449
450         $xs->elementStart($tag);
451
452         $xs->element('activity:object-type', null, $this->type);
453
454         $xs->element(self::ID, null, $this->id);
455
456         if (!empty($this->title)) {
457             $xs->element(
458                 self::TITLE,
459                 null,
460                 common_xml_safe_str($this->title)
461             );
462         }
463
464         if (!empty($this->summary)) {
465             $xs->element(
466                 self::SUMMARY,
467                 null,
468                 common_xml_safe_str($this->summary)
469             );
470         }
471
472         if (!empty($this->content)) {
473             // XXX: assuming HTML content here
474             $xs->element(
475                 ActivityUtils::CONTENT,
476                 array('type' => 'html'),
477                 common_xml_safe_str($this->content)
478             );
479         }
480
481         if (!empty($this->link)) {
482             $xs->element(
483                 'link',
484                 array(
485                     'rel' => 'alternate',
486                     'type' => 'text/html',
487                     'href' => $this->link
488                 ),
489                 null
490             );
491         }
492
493         if ($this->type == ActivityObject::PERSON
494             || $this->type == ActivityObject::GROUP) {
495
496             foreach ($this->avatarLinks as $avatar) {
497                 $xs->element(
498                     'link', array(
499                         'rel'  => 'avatar',
500                         'type'         => $avatar->type,
501                         'media:width'  => $avatar->width,
502                         'media:height' => $avatar->height,
503                         'href' => $avatar->url
504                     ),
505                     null
506                 );
507             }
508         }
509
510         if (!empty($this->geopoint)) {
511             $xs->element(
512                 'georss:point',
513                 null,
514                 $this->geopoint
515             );
516         }
517
518         if (!empty($this->poco)) {
519             $xs->raw($this->poco->asString());
520         }
521
522         $xs->elementEnd($tag);
523
524         return $xs->getString();
525     }
526 }