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('STATUSNET')) {
39 const VALUE = 'value';
40 const PRIMARY = 'primary';
46 function __construct($type, $value, $primary = false)
49 $this->value = $value;
50 $this->primary = $primary;
55 $xs = new XMLStringer(true);
56 $xs->elementStart('poco:urls');
57 $xs->element('poco:type', null, $this->type);
58 $xs->element('poco:value', null, $this->value);
59 if (!empty($this->primary)) {
60 $xs->element('poco:primary', null, 'true');
62 $xs->elementEnd('poco:urls');
63 return $xs->getString();
69 const ADDRESS = 'address';
70 const FORMATTED = 'formatted';
74 // @todo Other address fields
78 if (!empty($this->formatted)) {
79 $xs = new XMLStringer(true);
80 $xs->elementStart('poco:address');
81 $xs->element('poco:formatted', null, $this->formatted);
82 $xs->elementEnd('poco:address');
83 return $xs->getString();
92 const NS = 'http://portablecontacts.net/spec/1.0';
94 const USERNAME = 'preferredUsername';
95 const DISPLAYNAME = 'displayName';
98 public $preferredUsername;
102 public $urls = array();
104 function __construct($element = null)
106 if (empty($element)) {
110 $this->preferredUsername = ActivityUtils::childContent(
116 $this->displayName = ActivityUtils::childContent(
122 $this->note = ActivityUtils::childContent(
128 $this->address = $this->_getAddress($element);
129 $this->urls = $this->_getURLs($element);
132 private function _getURLs($element)
134 $urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
137 foreach ($urlEls as $urlEl) {
139 $type = ActivityUtils::childContent(
145 $value = ActivityUtils::childContent(
151 $primary = ActivityUtils::childContent(
159 if (isset($primary) && $primary == 'true') {
163 // @todo check to make sure a primary hasn't already been added
165 array_push($urls, new PoCoURL($type, $value, $isPrimary));
170 private function _getAddress($element)
172 $addressEl = ActivityUtils::child(
174 PoCoAddress::ADDRESS,
178 if (!empty($addressEl)) {
179 $formatted = ActivityUtils::childContent(
181 PoCoAddress::FORMATTED,
185 if (!empty($formatted)) {
186 $address = new PoCoAddress();
187 $address->formatted = $formatted;
195 function fromProfile($profile)
197 if (empty($profile)) {
203 $poco->preferredUsername = $profile->nickname;
204 $poco->displayName = $profile->getBestName();
206 $poco->note = $profile->bio;
208 $paddy = new PoCoAddress();
209 $paddy->formatted = $profile->location;
210 $poco->address = $paddy;
212 if (!empty($profile->homepage)) {
226 function getPrimaryURL()
228 foreach ($this->urls as $url) {
237 $xs = new XMLStringer(true);
239 'poco:preferredUsername',
241 $this->preferredUsername
250 if (!empty($this->note)) {
251 $xs->element('poco:note', null, $this->note);
254 if (!empty($this->address)) {
255 $xs->raw($this->address->asString());
258 foreach ($this->urls as $url) {
259 $xs->raw($url->asString());
262 return $xs->getString();
267 * Utilities for turning DOMish things into Activityish things
269 * Some common functions that I didn't have the bandwidth to try to factor
270 * into some kind of reasonable superclass, so just dumped here. Might
271 * be useful to have an ActivityObject parent class or something.
275 * @author Evan Prodromou <evan@status.net>
276 * @copyright 2010 StatusNet, Inc.
277 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
278 * @link http://status.net/
283 const ATOM = 'http://www.w3.org/2005/Atom';
290 const CONTENT = 'content';
294 * Get the permalink for an Activity object
296 * @param DOMElement $element A DOM element
298 * @return string related link, if any
301 static function getPermalink($element)
303 return self::getLink($element, 'alternate', 'text/html');
307 * Get the permalink for an Activity object
309 * @param DOMElement $element A DOM element
311 * @return string related link, if any
314 static function getLink(DOMNode $element, $rel, $type=null)
316 $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
318 foreach ($links as $link) {
320 $linkRel = $link->getAttribute(self::REL);
321 $linkType = $link->getAttribute(self::TYPE);
323 if ($linkRel == $rel &&
324 (is_null($type) || $linkType == $type)) {
325 return $link->getAttribute(self::HREF);
333 * Gets the first child element with the given tag
335 * @param DOMElement $element element to pick at
336 * @param string $tag tag to look for
337 * @param string $namespace Namespace to look under
339 * @return DOMElement found element or null
342 static function child(DOMNode $element, $tag, $namespace=self::ATOM)
344 $els = $element->childNodes;
345 if (empty($els) || $els->length == 0) {
348 for ($i = 0; $i < $els->length; $i++) {
349 $el = $els->item($i);
350 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
358 * Grab the text content of a DOM element child of the current element
360 * @param DOMElement $element Element whose children we examine
361 * @param string $tag Tag to look up
362 * @param string $namespace Namespace to use, defaults to Atom
364 * @return string content of the child
367 static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
369 $el = self::child($element, $tag, $namespace);
374 return $el->textContent;
379 * Get the content of an atom:entry-like object
381 * @param DOMElement $element The element to examine.
383 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
385 * @todo handle remote content
386 * @todo handle embedded XML mime types
387 * @todo handle base64-encoded non-XML and non-text mime types
390 static function getContent($element)
392 $contentEl = ActivityUtils::child($element, self::CONTENT);
394 if (!empty($contentEl)) {
396 $src = $contentEl->getAttribute(self::SRC);
399 throw new ClientException(_("Can't handle remote content yet."));
402 $type = $contentEl->getAttribute(self::TYPE);
404 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
406 if ($type == 'text') {
407 return $contentEl->textContent;
408 } else if ($type == 'html') {
409 $text = $contentEl->textContent;
410 return htmlspecialchars_decode($text, ENT_QUOTES);
411 } else if ($type == 'xhtml') {
412 $divEl = ActivityUtils::child($contentEl, 'div');
416 $doc = $divEl->ownerDocument;
418 $children = $divEl->childNodes;
420 for ($i = 0; $i < $children->length; $i++) {
421 $child = $children->item($i);
422 $text .= $doc->saveXML($child);
425 } else if (in_array(array('text/xml', 'application/xml'), $type) ||
426 preg_match('#(+|/)xml$#', $type)) {
427 throw new ClientException(_("Can't handle embedded XML content yet."));
428 } else if (strncasecmp($type, 'text/', 5)) {
429 return $contentEl->textContent;
431 throw new ClientException(_("Can't handle embedded Base64 content yet."));
438 * A noun-ish thing in the activity universe
440 * The activity streams spec talks about activity objects, while also having
441 * a tag activity:object, which is in fact an activity object. Aaaaaah!
443 * This is just a thing in the activity universe. Can be the subject, object,
444 * or indirect object (target!) of an activity verb. Rotten name, and I'm
445 * propagating it. *sigh*
449 * @author Evan Prodromou <evan@status.net>
450 * @copyright 2010 StatusNet, Inc.
451 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
452 * @link http://status.net/
457 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
458 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
459 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
460 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
461 const FILE = 'http://activitystrea.ms/schema/1.0/file';
462 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
463 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
464 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
465 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
466 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
467 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
468 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
469 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
470 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
471 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
474 // Atom elements we snarf
476 const TITLE = 'title';
477 const SUMMARY = 'summary';
479 const SOURCE = 'source';
483 const EMAIL = 'email';
501 * This probably needs to be refactored
502 * to generate a local class (ActivityPerson, ActivityFile, ...)
503 * based on the object type.
505 * @param DOMElement $element DOM thing to turn into an Activity thing
508 function __construct($element = null)
510 if (empty($element)) {
514 $this->element = $element;
516 $this->geopoint = $this->_childContent(
518 ActivityContext::POINT,
519 ActivityContext::GEORSS
522 if ($element->tagName == 'author') {
524 $this->type = self::PERSON; // XXX: is this fair?
525 $this->title = $this->_childContent($element, self::NAME);
526 $this->id = $this->_childContent($element, self::URI);
528 if (empty($this->id)) {
529 $email = $this->_childContent($element, self::EMAIL);
530 if (!empty($email)) {
532 $this->id = 'mailto:'.$email;
538 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
541 if (empty($this->type)) {
542 $this->type = ActivityObject::NOTE;
545 $this->id = $this->_childContent($element, self::ID);
546 $this->title = $this->_childContent($element, self::TITLE);
547 $this->summary = $this->_childContent($element, self::SUMMARY);
549 $this->source = $this->_getSource($element);
551 $this->content = ActivityUtils::getContent($element);
553 $this->link = ActivityUtils::getPermalink($element);
557 // Some per-type attributes...
558 if ($this->type == self::PERSON || $this->type == self::GROUP) {
559 $this->displayName = $this->title;
561 // @fixme we may have multiple avatars with different resolutions specified
562 $this->avatar = ActivityUtils::getLink($element, 'avatar');
564 $this->poco = new PoCo($element);
568 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
570 return ActivityUtils::childContent($element, $tag, $namespace);
573 // Try to get a unique id for the source feed
575 private function _getSource($element)
577 $sourceEl = ActivityUtils::child($element, 'source');
579 if (empty($sourceEl)) {
582 $href = ActivityUtils::getLink($sourceEl, 'self');
586 return ActivityUtils::childContent($sourceEl, 'id');
591 static function fromNotice($notice)
593 $object = new ActivityObject();
595 $object->type = ActivityObject::NOTE;
597 $object->id = $notice->uri;
598 $object->title = $notice->content;
599 $object->content = $notice->rendered;
600 $object->link = $notice->bestUrl();
605 static function fromProfile($profile)
607 $object = new ActivityObject();
609 $object->type = ActivityObject::PERSON;
610 $object->id = $profile->getUri();
611 $object->title = $profile->getBestName();
612 $object->link = $profile->profileurl;
613 $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
615 if (isset($profile->lat) && isset($profile->lon)) {
616 $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
619 $object->poco = PoCo::fromProfile($profile);
624 function asString($tag='activity:object')
626 $xs = new XMLStringer(true);
628 $xs->elementStart($tag);
630 $xs->element('activity:object-type', null, $this->type);
632 $xs->element(self::ID, null, $this->id);
634 if (!empty($this->title)) {
635 $xs->element(self::TITLE, null, $this->title);
638 if (!empty($this->summary)) {
639 $xs->element(self::SUMMARY, null, $this->summary);
642 if (!empty($this->content)) {
643 // XXX: assuming HTML content here
644 $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
647 if (!empty($this->link)) {
651 'rel' => 'alternate',
652 'type' => 'text/html',
653 'href' => $this->link
659 if ($this->type == ActivityObject::PERSON
660 || $this->type == ActivityObject::GROUP) {
663 'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
665 'href' => empty($this->avatar)
666 ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
667 : $this->avatar->displayUrl()
673 if (!empty($this->geopoint)) {
681 if (!empty($this->poco)) {
682 $xs->raw($this->poco->asString());
685 $xs->elementEnd($tag);
687 return $xs->getString();
692 * Utility class to hold a bunch of constant defining default verb types
696 * @author Evan Prodromou <evan@status.net>
697 * @copyright 2010 StatusNet, Inc.
698 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
699 * @link http://status.net/
704 const POST = 'http://activitystrea.ms/schema/1.0/post';
705 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
706 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
707 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
708 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
709 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
710 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
711 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
712 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
714 // Custom OStatus verbs for the flipside until they're standardized
715 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
716 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
717 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
718 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
720 // For simple profile-update pings; no content to share.
721 const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
724 class ActivityContext
729 public $attention = array();
730 public $conversation;
732 const THR = 'http://purl.org/syndication/thread/1.0';
733 const GEORSS = 'http://www.georss.org/georss';
734 const OSTATUS = 'http://ostatus.org/schema/1.0';
736 const INREPLYTO = 'in-reply-to';
740 const POINT = 'point';
742 const ATTENTION = 'ostatus:attention';
743 const CONVERSATION = 'ostatus:conversation';
745 function __construct($element)
747 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
749 if (!empty($replyToEl)) {
750 $this->replyToID = $replyToEl->getAttribute(self::REF);
751 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
754 $this->location = $this->getLocation($element);
756 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
758 // Multiple attention links allowed
760 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
762 for ($i = 0; $i < $links->length; $i++) {
764 $link = $links->item($i);
766 $linkRel = $link->getAttribute(ActivityUtils::REL);
768 if ($linkRel == self::ATTENTION) {
769 $this->attention[] = $link->getAttribute(self::HREF);
775 * Parse location given as a GeoRSS-simple point, if provided.
776 * http://www.georss.org/simple
778 * @param feed item $entry
779 * @return mixed Location or false
781 function getLocation($dom)
783 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
785 for ($i = 0; $i < $points->length; $i++) {
786 $point = $points->item($i)->textContent;
787 return self::locationFromPoint($point);
793 // XXX: Move to ActivityUtils or Location?
794 static function locationFromPoint($point)
796 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
797 $point = preg_replace('/\s+/', ' ', $point);
798 $point = trim($point);
799 $coords = explode(' ', $point);
800 if (count($coords) == 2) {
801 list($lat, $lon) = $coords;
802 if (is_numeric($lat) && is_numeric($lon)) {
803 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
804 return Location::fromLatLon($lat, $lon);
807 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
813 * An activity in the ActivityStrea.ms world
815 * An activity is kind of like a sentence: someone did something
818 * 'someone' is the 'actor'; 'did something' is the verb;
819 * 'something else' is the object.
823 * @author Evan Prodromou <evan@status.net>
824 * @copyright 2010 StatusNet, Inc.
825 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
826 * @link http://status.net/
831 const SPEC = 'http://activitystrea.ms/spec/1.0/';
832 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
835 const OBJECT = 'object';
836 const ACTOR = 'actor';
837 const SUBJECT = 'subject';
838 const OBJECTTYPE = 'object-type';
839 const CONTEXT = 'context';
840 const TARGET = 'target';
842 const ATOM = 'http://www.w3.org/2005/Atom';
844 const AUTHOR = 'author';
845 const PUBLISHED = 'published';
846 const UPDATED = 'updated';
848 public $actor; // an ActivityObject
849 public $verb; // a string (the URL)
850 public $object; // an ActivityObject
851 public $target; // an ActivityObject
852 public $context; // an ActivityObject
853 public $time; // Time of the activity
854 public $link; // an ActivityObject
855 public $entry; // the source entry
856 public $feed; // the source feed
858 public $summary; // summary of activity
859 public $content; // HTML content of activity
860 public $id; // ID of the activity
861 public $title; // title of the activity
864 * Turns a regular old Atom <entry> into a magical activity
866 * @param DOMElement $entry Atom entry to poke at
867 * @param DOMElement $feed Atom feed, for context
870 function __construct($entry = null, $feed = null)
872 if (is_null($entry)) {
876 $this->entry = $entry;
879 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
881 if (!empty($pubEl)) {
882 $this->time = strtotime($pubEl->textContent);
884 // XXX technically an error; being liberal. Good idea...?
885 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
886 if (!empty($updateEl)) {
887 $this->time = strtotime($updateEl->textContent);
893 $this->link = ActivityUtils::getPermalink($entry);
895 $verbEl = $this->_child($entry, self::VERB);
897 if (!empty($verbEl)) {
898 $this->verb = trim($verbEl->textContent);
900 $this->verb = ActivityVerb::POST;
901 // XXX: do other implied stuff here
904 $objectEl = $this->_child($entry, self::OBJECT);
906 if (!empty($objectEl)) {
907 $this->object = new ActivityObject($objectEl);
909 $this->object = new ActivityObject($entry);
912 $actorEl = $this->_child($entry, self::ACTOR);
914 if (!empty($actorEl)) {
916 $this->actor = new ActivityObject($actorEl);
918 } else if (!empty($feed) &&
919 $subjectEl = $this->_child($feed, self::SUBJECT)) {
921 $this->actor = new ActivityObject($subjectEl);
923 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
925 $this->actor = new ActivityObject($authorEl);
927 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
930 $this->actor = new ActivityObject($authorEl);
933 $contextEl = $this->_child($entry, self::CONTEXT);
935 if (!empty($contextEl)) {
936 $this->context = new ActivityContext($contextEl);
938 $this->context = new ActivityContext($entry);
941 $targetEl = $this->_child($entry, self::TARGET);
943 if (!empty($targetEl)) {
944 $this->target = new ActivityObject($targetEl);
947 $this->summary = ActivityUtils::childContent($entry, 'summary');
948 $this->id = ActivityUtils::childContent($entry, 'id');
949 $this->content = ActivityUtils::getContent($entry);
953 * Returns an Atom <entry> based on this activity
955 * @return DOMElement Atom entry
958 function toAtomEntry()
963 function asString($namespace=false)
965 $xs = new XMLStringer(true);
968 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
969 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
970 'xmlns:georss' => 'http://www.georss.org/georss',
971 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
972 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0');
977 $xs->elementStart('entry', $attrs);
979 $xs->element('id', null, $this->id);
980 $xs->element('title', null, $this->title);
981 $xs->element('published', null, common_date_iso8601($this->time));
982 $xs->element('content', array('type' => 'html'), $this->content);
984 if (!empty($this->summary)) {
985 $xs->element('summary', null, $this->summary);
988 if (!empty($this->link)) {
989 $xs->element('link', array('rel' => 'alternate',
990 'type' => 'text/html'),
996 $xs->elementStart('author');
997 $xs->element('uri', array(), $this->actor->id);
998 if ($this->actor->title) {
999 $xs->element('name', array(), $this->actor->title);
1001 $xs->elementEnd('author');
1002 $xs->raw($this->actor->asString('activity:actor'));
1004 $xs->element('activity:verb', null, $this->verb);
1006 if ($this->object) {
1007 $xs->raw($this->object->asString());
1010 if ($this->target) {
1011 $xs->raw($this->target->asString('activity:target'));
1014 $xs->elementEnd('entry');
1016 return $xs->getString();
1019 private function _child($element, $tag, $namespace=self::SPEC)
1021 return ActivityUtils::child($element, $tag, $namespace);