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(
157 array_push($urls, new PoCoURL($type, $value, $primary));
162 private function _getAddress($element)
164 $addressEl = ActivityUtils::child(
166 PoCoAddress::ADDRESS,
170 $formatted = ActivityUtils::childContent(
172 PoCoAddress::FORMATTED,
176 if (!empty($formatted)) {
177 $address = new PoCoAddress();
178 $address->formatted = $formatted;
185 function fromProfile($profile)
187 if (empty($profile)) {
193 $poco->preferredUsername = $profile->nickname;
194 $poco->displayName = $profile->getBestName();
196 $poco->note = $profile->bio;
198 $paddy = new PoCoAddress();
199 $paddy->formatted = $profile->location;
200 $poco->address = $paddy;
202 if (!empty($profile->homepage)) {
218 $xs = new XMLStringer(true);
220 'poco:preferredUsername',
222 $this->preferredUsername
231 if (!empty($this->note)) {
232 $xs->element('poco:note', null, $this->note);
235 if (!empty($this->address)) {
236 $xs->raw($this->address->asString());
239 foreach ($this->urls as $url) {
240 $xs->raw($url->asString());
243 return $xs->getString();
248 * Utilities for turning DOMish things into Activityish things
250 * Some common functions that I didn't have the bandwidth to try to factor
251 * into some kind of reasonable superclass, so just dumped here. Might
252 * be useful to have an ActivityObject parent class or something.
256 * @author Evan Prodromou <evan@status.net>
257 * @copyright 2010 StatusNet, Inc.
258 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
259 * @link http://status.net/
264 const ATOM = 'http://www.w3.org/2005/Atom';
271 const CONTENT = 'content';
275 * Get the permalink for an Activity object
277 * @param DOMElement $element A DOM element
279 * @return string related link, if any
282 static function getPermalink($element)
284 return self::getLink($element, 'alternate', 'text/html');
288 * Get the permalink for an Activity object
290 * @param DOMElement $element A DOM element
292 * @return string related link, if any
295 static function getLink($element, $rel, $type=null)
297 $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
299 foreach ($links as $link) {
301 $linkRel = $link->getAttribute(self::REL);
302 $linkType = $link->getAttribute(self::TYPE);
304 if ($linkRel == $rel &&
305 (is_null($type) || $linkType == $type)) {
306 return $link->getAttribute(self::HREF);
314 * Gets the first child element with the given tag
316 * @param DOMElement $element element to pick at
317 * @param string $tag tag to look for
318 * @param string $namespace Namespace to look under
320 * @return DOMElement found element or null
323 static function child($element, $tag, $namespace=self::ATOM)
325 $els = $element->childNodes;
326 if (empty($els) || $els->length == 0) {
329 for ($i = 0; $i < $els->length; $i++) {
330 $el = $els->item($i);
331 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
339 * Grab the text content of a DOM element child of the current element
341 * @param DOMElement $element Element whose children we examine
342 * @param string $tag Tag to look up
343 * @param string $namespace Namespace to use, defaults to Atom
345 * @return string content of the child
348 static function childContent($element, $tag, $namespace=self::ATOM)
350 $el = self::child($element, $tag, $namespace);
355 return $el->textContent;
360 * Get the content of an atom:entry-like object
362 * @param DOMElement $element The element to examine.
364 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
366 * @todo handle remote content
367 * @todo handle embedded XML mime types
368 * @todo handle base64-encoded non-XML and non-text mime types
371 static function getContent($element)
373 $contentEl = ActivityUtils::child($element, self::CONTENT);
375 if (!empty($contentEl)) {
377 $src = $contentEl->getAttribute(self::SRC);
380 throw new ClientException(_("Can't handle remote content yet."));
383 $type = $contentEl->getAttribute(self::TYPE);
385 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
387 if ($type == 'text') {
388 return $contentEl->textContent;
389 } else if ($type == 'html') {
390 $text = $contentEl->textContent;
391 return htmlspecialchars_decode($text, ENT_QUOTES);
392 } else if ($type == 'xhtml') {
393 $divEl = ActivityUtils::child($contentEl, 'div');
397 $doc = $divEl->ownerDocument;
399 $children = $divEl->childNodes;
401 for ($i = 0; $i < $children->length; $i++) {
402 $child = $children->item($i);
403 $text .= $doc->saveXML($child);
406 } else if (in_array(array('text/xml', 'application/xml'), $type) ||
407 preg_match('#(+|/)xml$#', $type)) {
408 throw new ClientException(_("Can't handle embedded XML content yet."));
409 } else if (strncasecmp($type, 'text/', 5)) {
410 return $contentEl->textContent;
412 throw new ClientException(_("Can't handle embedded Base64 content yet."));
419 * A noun-ish thing in the activity universe
421 * The activity streams spec talks about activity objects, while also having
422 * a tag activity:object, which is in fact an activity object. Aaaaaah!
424 * This is just a thing in the activity universe. Can be the subject, object,
425 * or indirect object (target!) of an activity verb. Rotten name, and I'm
426 * propagating it. *sigh*
430 * @author Evan Prodromou <evan@status.net>
431 * @copyright 2010 StatusNet, Inc.
432 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
433 * @link http://status.net/
438 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
439 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
440 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
441 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
442 const FILE = 'http://activitystrea.ms/schema/1.0/file';
443 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
444 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
445 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
446 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
447 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
448 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
449 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
450 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
451 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
452 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
455 // Atom elements we snarf
457 const TITLE = 'title';
458 const SUMMARY = 'summary';
460 const SOURCE = 'source';
464 const EMAIL = 'email';
482 * This probably needs to be refactored
483 * to generate a local class (ActivityPerson, ActivityFile, ...)
484 * based on the object type.
486 * @param DOMElement $element DOM thing to turn into an Activity thing
489 function __construct($element = null)
491 if (empty($element)) {
495 $this->element = $element;
497 if ($element->tagName == 'author') {
499 $this->type = self::PERSON; // XXX: is this fair?
500 $this->title = $this->_childContent($element, self::NAME);
501 $this->id = $this->_childContent($element, self::URI);
503 if (empty($this->id)) {
504 $email = $this->_childContent($element, self::EMAIL);
505 if (!empty($email)) {
507 $this->id = 'mailto:'.$email;
513 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
516 if (empty($this->type)) {
517 $this->type = ActivityObject::NOTE;
520 $this->id = $this->_childContent($element, self::ID);
521 $this->title = $this->_childContent($element, self::TITLE);
522 $this->summary = $this->_childContent($element, self::SUMMARY);
524 $this->source = $this->_getSource($element);
526 $this->content = ActivityUtils::getContent($element);
528 $this->link = ActivityUtils::getPermalink($element);
532 // Some per-type attributes...
533 if ($this->type == self::PERSON || $this->type == self::GROUP) {
534 $this->displayName = $this->title;
536 // @fixme we may have multiple avatars with different resolutions specified
537 $this->avatar = ActivityUtils::getLink($element, 'avatar');
539 $this->poco = new PoCo($element);
543 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
545 return ActivityUtils::childContent($element, $tag, $namespace);
548 // Try to get a unique id for the source feed
550 private function _getSource($element)
552 $sourceEl = ActivityUtils::child($element, 'source');
554 if (empty($sourceEl)) {
557 $href = ActivityUtils::getLink($sourceEl, 'self');
561 return ActivityUtils::childContent($sourceEl, 'id');
566 static function fromNotice($notice)
568 $object = new ActivityObject();
570 $object->type = ActivityObject::NOTE;
572 $object->id = $notice->uri;
573 $object->title = $notice->content;
574 $object->content = $notice->rendered;
575 $object->link = $notice->bestUrl();
580 static function fromProfile($profile)
582 $object = new ActivityObject();
584 $object->type = ActivityObject::PERSON;
585 $object->id = $profile->getUri();
586 $object->title = $profile->getBestName();
587 $object->link = $profile->profileurl;
588 $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
590 if (isset($profile->lat) && isset($profile->lon)) {
591 $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
594 $object->poco = PoCo::fromProfile($profile);
599 function asString($tag='activity:object')
601 $xs = new XMLStringer(true);
603 $xs->elementStart($tag);
605 $xs->element('activity:object-type', null, $this->type);
607 $xs->element(self::ID, null, $this->id);
609 if (!empty($this->title)) {
610 $xs->element(self::TITLE, null, $this->title);
613 if (!empty($this->summary)) {
614 $xs->element(self::SUMMARY, null, $this->summary);
617 if (!empty($this->content)) {
618 // XXX: assuming HTML content here
619 $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
622 if (!empty($this->link)) {
626 'rel' => 'alternate',
627 'type' => 'text/html',
628 'href' => $this->link
634 if ($this->type == ActivityObject::PERSON
635 || $this->type == ActivityObject::GROUP) {
638 'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
640 'href' => empty($this->avatar)
641 ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
642 : $this->avatar->displayUrl()
648 if (!empty($this->geopoint)) {
656 if (!empty($this->poco)) {
657 $xs->raw($this->poco->asString());
660 $xs->elementEnd($tag);
662 return $xs->getString();
667 * Utility class to hold a bunch of constant defining default verb types
671 * @author Evan Prodromou <evan@status.net>
672 * @copyright 2010 StatusNet, Inc.
673 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
674 * @link http://status.net/
679 const POST = 'http://activitystrea.ms/schema/1.0/post';
680 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
681 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
682 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
683 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
684 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
685 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
686 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
687 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
689 // Custom OStatus verbs for the flipside until they're standardized
690 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
691 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
692 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
693 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
696 class ActivityContext
701 public $attention = array();
702 public $conversation;
704 const THR = 'http://purl.org/syndication/thread/1.0';
705 const GEORSS = 'http://www.georss.org/georss';
706 const OSTATUS = 'http://ostatus.org/schema/1.0';
708 const INREPLYTO = 'in-reply-to';
712 const POINT = 'point';
714 const ATTENTION = 'ostatus:attention';
715 const CONVERSATION = 'ostatus:conversation';
717 function __construct($element)
719 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
721 if (!empty($replyToEl)) {
722 $this->replyToID = $replyToEl->getAttribute(self::REF);
723 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
726 $this->location = $this->getLocation($element);
728 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
730 // Multiple attention links allowed
732 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
734 for ($i = 0; $i < $links->length; $i++) {
736 $link = $links->item($i);
738 $linkRel = $link->getAttribute(ActivityUtils::REL);
740 if ($linkRel == self::ATTENTION) {
741 $this->attention[] = $link->getAttribute(self::HREF);
747 * Parse location given as a GeoRSS-simple point, if provided.
748 * http://www.georss.org/simple
750 * @param feed item $entry
751 * @return mixed Location or false
753 function getLocation($dom)
755 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
757 for ($i = 0; $i < $points->length; $i++) {
758 $point = $points->item($i)->textContent;
759 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
760 $point = preg_replace('/\s+/', ' ', $point);
761 $point = trim($point);
762 $coords = explode(' ', $point);
763 if (count($coords) == 2) {
764 list($lat, $lon) = $coords;
765 if (is_numeric($lat) && is_numeric($lon)) {
766 common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
767 return Location::fromLatLon($lat, $lon);
770 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
778 * An activity in the ActivityStrea.ms world
780 * An activity is kind of like a sentence: someone did something
783 * 'someone' is the 'actor'; 'did something' is the verb;
784 * 'something else' is the object.
788 * @author Evan Prodromou <evan@status.net>
789 * @copyright 2010 StatusNet, Inc.
790 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
791 * @link http://status.net/
796 const SPEC = 'http://activitystrea.ms/spec/1.0/';
797 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
800 const OBJECT = 'object';
801 const ACTOR = 'actor';
802 const SUBJECT = 'subject';
803 const OBJECTTYPE = 'object-type';
804 const CONTEXT = 'context';
805 const TARGET = 'target';
807 const ATOM = 'http://www.w3.org/2005/Atom';
809 const AUTHOR = 'author';
810 const PUBLISHED = 'published';
811 const UPDATED = 'updated';
813 public $actor; // an ActivityObject
814 public $verb; // a string (the URL)
815 public $object; // an ActivityObject
816 public $target; // an ActivityObject
817 public $context; // an ActivityObject
818 public $time; // Time of the activity
819 public $link; // an ActivityObject
820 public $entry; // the source entry
821 public $feed; // the source feed
823 public $summary; // summary of activity
824 public $content; // HTML content of activity
825 public $id; // ID of the activity
826 public $title; // title of the activity
829 * Turns a regular old Atom <entry> into a magical activity
831 * @param DOMElement $entry Atom entry to poke at
832 * @param DOMElement $feed Atom feed, for context
835 function __construct($entry = null, $feed = null)
837 if (is_null($entry)) {
841 $this->entry = $entry;
844 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
846 if (!empty($pubEl)) {
847 $this->time = strtotime($pubEl->textContent);
849 // XXX technically an error; being liberal. Good idea...?
850 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
851 if (!empty($updateEl)) {
852 $this->time = strtotime($updateEl->textContent);
858 $this->link = ActivityUtils::getPermalink($entry);
860 $verbEl = $this->_child($entry, self::VERB);
862 if (!empty($verbEl)) {
863 $this->verb = trim($verbEl->textContent);
865 $this->verb = ActivityVerb::POST;
866 // XXX: do other implied stuff here
869 $objectEl = $this->_child($entry, self::OBJECT);
871 if (!empty($objectEl)) {
872 $this->object = new ActivityObject($objectEl);
874 $this->object = new ActivityObject($entry);
877 $actorEl = $this->_child($entry, self::ACTOR);
879 if (!empty($actorEl)) {
881 $this->actor = new ActivityObject($actorEl);
883 } else if (!empty($feed) &&
884 $subjectEl = $this->_child($feed, self::SUBJECT)) {
886 $this->actor = new ActivityObject($subjectEl);
888 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
890 $this->actor = new ActivityObject($authorEl);
892 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
895 $this->actor = new ActivityObject($authorEl);
898 $contextEl = $this->_child($entry, self::CONTEXT);
900 if (!empty($contextEl)) {
901 $this->context = new ActivityContext($contextEl);
903 $this->context = new ActivityContext($entry);
906 $targetEl = $this->_child($entry, self::TARGET);
908 if (!empty($targetEl)) {
909 $this->target = new ActivityObject($targetEl);
912 $this->summary = ActivityUtils::childContent($entry, 'summary');
913 $this->id = ActivityUtils::childContent($entry, 'id');
914 $this->content = ActivityUtils::getContent($entry);
918 * Returns an Atom <entry> based on this activity
920 * @return DOMElement Atom entry
923 function toAtomEntry()
928 function asString($namespace=false)
930 $xs = new XMLStringer(true);
933 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
934 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
935 'xmlns:georss' => 'http://www.georss.org/georss',
936 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
937 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0');
942 $xs->elementStart('entry', $attrs);
944 $xs->element('id', null, $this->id);
945 $xs->element('title', null, $this->title);
946 $xs->element('published', null, common_date_iso8601($this->time));
947 $xs->element('content', array('type' => 'html'), $this->content);
949 if (!empty($this->summary)) {
950 $xs->element('summary', null, $this->summary);
953 if (!empty($this->link)) {
954 $xs->element('link', array('rel' => 'alternate',
955 'type' => 'text/html'),
961 $xs->elementStart('author');
962 $xs->element('uri', array(), $this->actor->id);
963 if ($this->actor->title) {
964 $xs->element('name', array(), $this->actor->title);
966 $xs->elementEnd('author');
967 $xs->raw($this->actor->asString('activity:actor'));
969 $xs->element('activity:verb', null, $this->verb);
972 $xs->raw($this->object->asString());
976 $xs->raw($this->target->asString('activity:target'));
979 $xs->elementEnd('entry');
981 return $xs->getString();
984 private function _child($element, $tag, $namespace=self::SPEC)
986 return ActivityUtils::child($element, $tag, $namespace);