3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
31 if (!defined('GNUSOCIAL')) { exit(1); }
33 require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php');
36 * A noun-ish thing in the activity universe
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!
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*
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/
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';
71 const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
72 const SERVICE = 'http://activitystrea.ms/schema/1.0/service';
73 const IMAGE = 'http://activitystrea.ms/schema/1.0/image';
74 const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
75 const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
77 // Atom elements we snarf
79 const TITLE = 'title';
80 const SUMMARY = 'summary';
82 const SOURCE = 'source';
86 const EMAIL = 'email';
88 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
89 const AUTHOR = 'author';
90 const USERIMAGE = 'userImage';
91 const PROFILEURL = 'profileUrl';
92 const NICKNAME = 'nickName';
93 const DISPLAYNAME = 'displayName';
104 public $avatarLinks = array();
109 // @todo move this stuff to it's own PHOTO activity object
110 const MEDIA_DESCRIPTION = 'description';
115 public $extra = array();
122 * This probably needs to be refactored
123 * to generate a local class (ActivityPerson, ActivityFile, ...)
124 * based on the object type.
126 * @param DOMElement $element DOM thing to turn into an Activity thing
128 function __construct($element = null)
130 if (empty($element)) {
134 $this->element = $element;
136 $this->geopoint = $this->_childContent(
138 ActivityContext::POINT,
139 ActivityContext::GEORSS
142 if ($element->tagName == 'author') {
143 $this->_fromAuthor($element);
144 } else if ($element->tagName == 'item') {
145 $this->_fromRssItem($element);
147 $this->_fromAtomEntry($element);
150 // Some per-type attributes...
151 if ($this->type == self::PERSON || $this->type == self::GROUP) {
152 $this->displayName = $this->title;
154 $photos = ActivityUtils::getLinks($element, 'photo');
155 if (count($photos)) {
156 foreach ($photos as $link) {
157 $this->avatarLinks[] = new AvatarLink($link);
160 $avatars = ActivityUtils::getLinks($element, 'avatar');
161 foreach ($avatars as $link) {
162 $this->avatarLinks[] = new AvatarLink($link);
166 $this->poco = new PoCo($element);
169 if ($this->type == self::PHOTO) {
171 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
172 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
174 $this->description = ActivityUtils::childContent(
176 ActivityObject::MEDIA_DESCRIPTION,
180 if ($this->type == self::_LIST) {
181 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
182 $this->owner = new ActivityObject($owner);
186 private function _fromAuthor($element)
188 $this->type = $this->_childContent($element,
189 Activity::OBJECTTYPE,
192 if (empty($this->type)) {
193 $this->type = self::PERSON; // XXX: is this fair?
197 // Start with <poco::displayName>
199 $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
201 // try falling back to <atom:title>
203 if (empty($this->title)) {
204 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
206 if (!empty($title)) {
207 $this->title = common_strip_html($title);
211 // fall back to <atom:name> as a last resort
213 if (empty($this->title)) {
214 $this->title = $this->_childContent($element, self::NAME);
217 // start with <atom:id>
219 $this->id = $this->_childContent($element, self::ID);
221 // fall back to <atom:uri>
223 if (empty($this->id)) {
224 $this->id = $this->_childContent($element, self::URI);
227 // fall further back to <atom:email>
229 if (empty($this->id)) {
230 $email = $this->_childContent($element, self::EMAIL);
231 if (!empty($email)) {
233 $this->id = 'mailto:'.$email;
237 $this->link = ActivityUtils::getPermalink($element);
239 // fall finally back to <link rel=alternate>
241 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
242 $this->id = $this->link;
246 private function _fromAtomEntry($element)
248 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
251 if (empty($this->type)) {
252 $this->type = ActivityObject::NOTE;
255 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
256 $this->content = ActivityUtils::getContent($element);
258 // We don't like HTML in our titles, although it's technically allowed
259 $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
261 $this->source = $this->_getSource($element);
263 $this->link = ActivityUtils::getPermalink($element);
265 $this->id = $this->_childContent($element, self::ID);
267 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
268 $this->id = $this->link;
271 $els = $element->childNodes;
274 for ($i = 0; $i < $els->length; $i++) {
275 $link = $els->item($i);
276 if ($link->localName == ActivityUtils::LINK && $link->namespaceURI == ActivityUtils::ATOM) {
278 foreach ($link->attributes as $attrName=>$attrNode) {
279 $attrs[$attrName] = $attrNode->nodeValue;
281 $this->extra[] = [$link->localName,
288 // @todo FIXME: rationalize with Activity::_fromRssItem()
289 private function _fromRssItem($item)
291 if (empty($this->type)) {
292 $this->type = ActivityObject::NOTE;
295 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
297 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
299 if (!empty($contentEl)) {
300 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
302 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
303 if (!empty($descriptionEl)) {
304 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
308 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
310 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
312 if (!empty($guidEl)) {
313 $this->id = $guidEl->textContent;
315 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
317 $this->link = $this->id;
322 public static function fromRssAuthor($el)
324 $text = $el->textContent;
326 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
329 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
332 } else if (preg_match('/.*@.*/', $text)) {
340 // Not really enough info
342 $obj = new ActivityObject();
346 $obj->type = ActivityObject::PERSON;
349 if (!empty($email)) {
350 $obj->id = 'mailto:'.$email;
356 public static function fromDcCreator($el)
358 // Not really enough info
360 $text = $el->textContent;
362 $obj = new ActivityObject();
367 $obj->type = ActivityObject::PERSON;
372 public static function fromRssChannel($el)
374 $obj = new ActivityObject();
378 $obj->type = ActivityObject::PERSON; // @fixme guess better
380 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
381 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
382 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
384 if (empty($obj->id)) {
385 $obj->id = $obj->link;
388 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
391 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
394 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
396 if (!empty($imageEl)) {
397 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
398 $al = new AvatarLink();
400 $obj->avatarLinks[] = $al;
406 public static function fromPosterousAuthor($el)
408 $obj = new ActivityObject();
410 $obj->type = ActivityObject::PERSON; // @fixme any others...?
412 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
414 if (!empty($userImage)) {
415 $al = new AvatarLink();
416 $al->url = $userImage;
417 $obj->avatarLinks[] = $al;
420 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
421 $obj->id = $obj->link;
423 $obj->poco = new PoCo();
425 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
426 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
428 $obj->title = $obj->poco->displayName;
433 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
435 return ActivityUtils::childContent($element, $tag, $namespace);
438 // Try to get a unique id for the source feed
440 private function _getSource($element)
442 $sourceEl = ActivityUtils::child($element, 'source');
444 if (empty($sourceEl)) {
447 $href = ActivityUtils::getLink($sourceEl, 'self');
451 return ActivityUtils::childContent($sourceEl, 'id');
456 static function fromGroup(User_group $group)
458 $object = new ActivityObject();
460 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
462 $object->type = ActivityObject::GROUP;
463 $object->id = $group->getUri();
464 $object->title = $group->getBestName();
465 $object->link = $group->getUri();
467 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
468 AVATAR_PROFILE_SIZE);
470 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
473 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
476 $object->poco = PoCo::fromGroup($group);
477 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
483 static function fromPeopletag($ptag)
485 $object = new ActivityObject();
486 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
487 $object->type = ActivityObject::_LIST;
489 $object->id = $ptag->getUri();
490 $object->title = $ptag->tag;
491 $object->summary = $ptag->description;
492 $object->link = $ptag->homeUrl();
493 $object->owner = Profile::getKV('id', $ptag->tagger);
494 $object->poco = PoCo::fromProfile($object->owner);
495 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
500 static function fromFile(File $file)
502 $object = new ActivityObject();
504 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
506 $object->type = self::mimeTypeToObjectType($file->mimetype);
507 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
508 $object->link = $file->getAttachmentUrl();
511 $object->title = $file->title;
515 $object->date = $file->date;
519 $thumbnail = $file->getThumbnail();
520 $object->thumbnail = $thumbnail;
521 } catch (UseFileAsThumbnailException $e) {
522 $object->thumbnail = null;
523 } catch (UnsupportedMediaException $e) {
524 $object->thumbnail = null;
527 switch (self::canonicalType($object->type)) {
529 $object->largerImage = $file->getUrl();
533 $object->stream = $file->getUrl();
537 Event::handle('EndActivityObjectFromFile', array($file, &$object));
543 static function fromNoticeSource(Notice_source $source)
545 $object = new ActivityObject();
546 $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
547 'activity', 'feed', 'mirror', 'twitter', 'facebook');
549 if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
550 $object->type = ActivityObject::APPLICATION;
552 if (in_array($source->code, $wellKnown)) {
553 // We use one ID for all well-known StatusNet sources
554 $object->id = "tag:status.net,2009:notice-source:".$source->code;
555 } else if ($source->url) {
556 // They registered with an URL
557 $object->id = $source->url;
559 // Locally-registered, no URL
560 $object->id = TagURI::mint("notice-source:".$source->code);
564 $object->link = $source->url;
568 $object->title = $source->name;
570 $object->title = $source->code;
573 if ($source->created) {
574 $object->date = $source->created;
577 $object->extra[] = array('status_net', array('source_code' => $source->code));
579 Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
585 static function fromMessage(Message $message)
587 $object = new ActivityObject();
589 if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
591 $object->type = ActivityObject::NOTE;
592 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
593 $object->content = $message->rendered;
594 $object->date = $message->created;
597 $object->link = $message->url;
599 $object->link = common_local_url('showmessage', array('message' => $message->id));
602 $object->extra[] = array('status_net', array('message_id' => $message->id));
604 Event::handle('EndActivityObjectFromMessage', array($message, &$object));
610 function outputTo($xo, $tag='activity:object')
613 $xo->elementStart($tag);
616 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
617 $xo->element('activity:object-type', null, $this->type);
621 if ($tag == 'author') {
622 $xo->element(self::URI, null, $this->id);
624 $xo->element(self::ID, null, $this->id);
627 if (!empty($this->title)) {
628 $name = common_xml_safe_str($this->title);
629 if ($tag == 'author') {
630 // XXX: Backward compatibility hack -- atom:name should contain
631 // full name here, instead of nickname, i.e.: $name. Change
632 // this in the next version.
633 $xo->element(self::NAME, null, $this->poco->preferredUsername);
635 $xo->element(self::TITLE, null, $name);
639 if (!empty($this->summary)) {
643 common_xml_safe_str($this->summary)
647 if (!empty($this->content)) {
648 // XXX: assuming HTML content here
650 ActivityUtils::CONTENT,
651 array('type' => 'html'),
652 common_xml_safe_str($this->content)
656 if (!empty($this->link)) {
660 'rel' => 'alternate',
661 'type' => 'text/html',
662 'href' => $this->link
668 if(!empty($this->owner)) {
669 $owner = $this->owner->asActivityNoun(self::AUTHOR);
673 if ($this->type == ActivityObject::PERSON
674 || $this->type == ActivityObject::GROUP) {
676 foreach ($this->avatarLinks as $alink) {
680 'type' => $alink->type,
681 'media:width' => $alink->width,
682 'media:height' => $alink->height,
683 'href' => $alink->url,
689 if (!empty($this->geopoint)) {
697 if (!empty($this->poco)) {
698 $this->poco->outputTo($xo);
701 // @fixme there's no way here to make a tree; elements can only contain plaintext
702 // @fixme these may collide with JSON extensions
703 foreach ($this->extra as $el) {
704 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
705 $xo->element($extraTag, $attrs, $content);
708 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
712 $xo->elementEnd($tag);
718 function asString($tag='activity:object')
720 $xs = new XMLStringer(true);
722 $this->outputTo($xs, $tag);
724 return $xs->getString();
728 * Returns an array based on this Activity Object suitable for
731 * @return array $object the activity object array
738 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
739 // XXX: attachments are added by Activity
741 // author (Add object for author? Could be useful for repeats.)
743 // content (Add rendered version of the notice?)
745 // downstreamDuplicates
750 $object['id'] = $this->id;
751 } else if ($this->link) {
752 $object['id'] = $this->link;
755 if ($this->type == ActivityObject::PERSON
756 || $this->type == ActivityObject::GROUP) {
759 $object['displayName'] = $this->title;
761 // XXX: Not sure what the best avatar is to use for the
762 // author's "image". For now, I'm using the large size.
765 $avatarMediaLinks = array();
767 foreach ($this->avatarLinks as $a) {
769 // Make a MediaLink for every other Avatar
770 $avatar = new ActivityStreamsMediaLink(
778 // Find the big avatar to use as the "image"
779 if ($a->height == AVATAR_PROFILE_SIZE) {
783 $avatarMediaLinks[] = $avatar->asArray();
786 if (!array_key_exists('status_net', $object)) {
787 $object['status_net'] = array();
790 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
793 if (!empty($imgLink)) {
794 $object['image'] = $imgLink->asArray();
800 // We can probably use the whole schema URL here but probably the
801 // relative simple name is easier to parse
803 $object['objectType'] = self::canonicalType($this->type);
806 $object['summary'] = $this->summary;
808 // content, usually rendered HTML
809 $object['content'] = $this->content;
811 // published (probably don't need. Might be useful for repeats.)
813 // updated (probably don't need this)
815 // TODO: upstreamDuplicates
818 $object['url'] = $this->link;
822 // @fixme these may collide with XML extensions
823 // @fixme multiple tags of same name will overwrite each other
824 // @fixme text content from XML extensions will be lost
826 foreach ($this->extra as $e) {
827 list($objectName, $props, $txt) = array_pad($e, 3, null);
828 if (!empty($objectName)) {
829 $parts = explode(":", $objectName);
830 if (count($parts) == 2 && $parts[0] == "statusnet") {
831 if (!array_key_exists('status_net', $object)) {
832 $object['status_net'] = array();
834 $object['status_net'][$parts[1]] = $props;
836 $object[$objectName] = $props;
841 if (!empty($this->geopoint)) {
843 list($lat, $lon) = explode(' ', $this->geopoint);
845 if (!empty($lat) && !empty($lon)) {
846 $object['location'] = array(
847 'objectType' => 'place',
848 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
853 $loc = Location::fromLatLon((float)$lat, (float)$lon);
856 $name = $loc->getName();
859 $object['location']['displayName'] = $name;
861 $url = $loc->getURL();
864 $object['location']['url'] = $url;
870 if (!empty($this->poco)) {
871 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
874 if (!empty($this->thumbnail)) {
875 if (is_string($this->thumbnail)) {
876 $object['image'] = array('url' => $this->thumbnail);
878 $object['image'] = array('url' => $this->thumbnail->getUrl());
879 if ($this->thumbnail->width) {
880 $object['image']['width'] = $this->thumbnail->width;
882 if ($this->thumbnail->height) {
883 $object['image']['height'] = $this->thumbnail->height;
888 switch (self::canonicalType($this->type)) {
890 if (!empty($this->largerImage)) {
891 $object['fullImage'] = array('url' => $this->largerImage);
896 if (!empty($this->stream)) {
897 $object['stream'] = array('url' => $this->stream);
902 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
904 return array_filter($object);
907 public function getIdentifiers() {
909 foreach(array('id', 'link', 'url') as $id) {
910 if (isset($this->$id)) {
914 return array_unique($ids);
917 static function canonicalType($type) {
918 return ActivityUtils::resolveUri($type, true);
921 static function mimeTypeToObjectType($mimeType) {
926 if (empty($mimeType)) {
930 $parts = explode('/', $mimeType);