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')) {
38 const VALUE = 'value';
39 const PRIMARY = 'primary';
45 function __construct($type, $value, $primary = false)
48 $this->value = $value;
49 $this->primary = $primary;
54 $xs = new XMLStringer(true);
55 $xs->elementStart('poco:urls');
56 $xs->element('poco:type', null, $this->type);
57 $xs->element('poco:value', null, $this->value);
59 $xs->element('poco:primary', null, 'true');
61 $xs->elementEnd('poco:urls');
62 return $xs->getString();
68 const ADDRESS = 'address';
69 const FORMATTED = 'formatted';
73 function __construct($formatted)
75 if (empty($formatted)) {
78 $this->formatted = $formatted;
83 $xs = new XMLStringer(true);
84 $xs->elementStart('poco:address');
85 $xs->element('poco:formatted', null, $this->formatted);
86 $xs->elementEnd('poco:address');
87 return $xs->getString();
93 const NS = 'http://portablecontacts.net/spec/1.0';
95 const USERNAME = 'preferredUsername';
99 public $preferredUsername;
102 public $urls = array();
104 function __construct($profile)
106 $this->preferredUsername = $profile->nickname;
108 $this->note = $profile->bio;
109 $this->address = new PoCoAddress($profile->location);
111 if (!empty($profile->homepage)) {
125 $xs = new XMLStringer(true);
127 'poco:preferredUsername',
129 $this->preferredUsername
132 if (!empty($this->note)) {
133 $xs->element('poco:note', null, $this->note);
136 if (!empty($this->address)) {
137 $xs->raw($this->address->asString());
140 foreach ($this->urls as $url) {
141 $xs->raw($url->asString());
144 return $xs->getString();
149 * Utilities for turning DOMish things into Activityish things
151 * Some common functions that I didn't have the bandwidth to try to factor
152 * into some kind of reasonable superclass, so just dumped here. Might
153 * be useful to have an ActivityObject parent class or something.
157 * @author Evan Prodromou <evan@status.net>
158 * @copyright 2010 StatusNet, Inc.
159 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
160 * @link http://status.net/
165 const ATOM = 'http://www.w3.org/2005/Atom';
172 const CONTENT = 'content';
176 * Get the permalink for an Activity object
178 * @param DOMElement $element A DOM element
180 * @return string related link, if any
183 static function getPermalink($element)
185 return self::getLink($element, 'alternate', 'text/html');
189 * Get the permalink for an Activity object
191 * @param DOMElement $element A DOM element
193 * @return string related link, if any
196 static function getLink($element, $rel, $type=null)
198 $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
200 foreach ($links as $link) {
202 $linkRel = $link->getAttribute(self::REL);
203 $linkType = $link->getAttribute(self::TYPE);
205 if ($linkRel == $rel &&
206 (is_null($type) || $linkType == $type)) {
207 return $link->getAttribute(self::HREF);
215 * Gets the first child element with the given tag
217 * @param DOMElement $element element to pick at
218 * @param string $tag tag to look for
219 * @param string $namespace Namespace to look under
221 * @return DOMElement found element or null
224 static function child($element, $tag, $namespace=self::ATOM)
226 $els = $element->childNodes;
227 if (empty($els) || $els->length == 0) {
230 for ($i = 0; $i < $els->length; $i++) {
231 $el = $els->item($i);
232 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
240 * Grab the text content of a DOM element child of the current element
242 * @param DOMElement $element Element whose children we examine
243 * @param string $tag Tag to look up
244 * @param string $namespace Namespace to use, defaults to Atom
246 * @return string content of the child
249 static function childContent($element, $tag, $namespace=self::ATOM)
251 $el = self::child($element, $tag, $namespace);
256 return $el->textContent;
261 * Get the content of an atom:entry-like object
263 * @param DOMElement $element The element to examine.
265 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
267 * @todo handle remote content
268 * @todo handle embedded XML mime types
269 * @todo handle base64-encoded non-XML and non-text mime types
272 static function getContent($element)
274 $contentEl = ActivityUtils::child($element, self::CONTENT);
276 if (!empty($contentEl)) {
278 $src = $contentEl->getAttribute(self::SRC);
281 throw new ClientException(_("Can't handle remote content yet."));
284 $type = $contentEl->getAttribute(self::TYPE);
286 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
288 if ($type == 'text') {
289 return $contentEl->textContent;
290 } else if ($type == 'html') {
291 $text = $contentEl->textContent;
292 return htmlspecialchars_decode($text, ENT_QUOTES);
293 } else if ($type == 'xhtml') {
294 $divEl = ActivityUtils::child($contentEl, 'div');
298 $doc = $divEl->ownerDocument;
300 $children = $divEl->childNodes;
302 for ($i = 0; $i < $children->length; $i++) {
303 $child = $children->item($i);
304 $text .= $doc->saveXML($child);
307 } else if (in_array(array('text/xml', 'application/xml'), $type) ||
308 preg_match('#(+|/)xml$#', $type)) {
309 throw new ClientException(_("Can't handle embedded XML content yet."));
310 } else if (strncasecmp($type, 'text/', 5)) {
311 return $contentEl->textContent;
313 throw new ClientException(_("Can't handle embedded Base64 content yet."));
320 * A noun-ish thing in the activity universe
322 * The activity streams spec talks about activity objects, while also having
323 * a tag activity:object, which is in fact an activity object. Aaaaaah!
325 * This is just a thing in the activity universe. Can be the subject, object,
326 * or indirect object (target!) of an activity verb. Rotten name, and I'm
327 * propagating it. *sigh*
331 * @author Evan Prodromou <evan@status.net>
332 * @copyright 2010 StatusNet, Inc.
333 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
334 * @link http://status.net/
339 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
340 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
341 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
342 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
343 const FILE = 'http://activitystrea.ms/schema/1.0/file';
344 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
345 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
346 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
347 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
348 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
349 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
350 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
351 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
352 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
353 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
356 // Atom elements we snarf
358 const TITLE = 'title';
359 const SUMMARY = 'summary';
361 const SOURCE = 'source';
365 const EMAIL = 'email';
381 * This probably needs to be refactored
382 * to generate a local class (ActivityPerson, ActivityFile, ...)
383 * based on the object type.
385 * @param DOMElement $element DOM thing to turn into an Activity thing
388 function __construct($element = null)
390 if (empty($element)) {
394 $this->element = $element;
396 if ($element->tagName == 'author') {
398 $this->type = self::PERSON; // XXX: is this fair?
399 $this->title = $this->_childContent($element, self::NAME);
400 $this->id = $this->_childContent($element, self::URI);
402 if (empty($this->id)) {
403 $email = $this->_childContent($element, self::EMAIL);
404 if (!empty($email)) {
406 $this->id = 'mailto:'.$email;
412 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
415 if (empty($this->type)) {
416 $this->type = ActivityObject::NOTE;
419 $this->id = $this->_childContent($element, self::ID);
420 $this->title = $this->_childContent($element, self::TITLE);
421 $this->summary = $this->_childContent($element, self::SUMMARY);
423 $this->source = $this->_getSource($element);
425 $this->content = ActivityUtils::getContent($element);
427 $this->link = ActivityUtils::getPermalink($element);
429 // XXX: grab PoCo stuff
432 // Some per-type attributes...
433 if ($this->type == self::PERSON || $this->type == self::GROUP) {
434 $this->displayName = $this->title;
436 // @fixme we may have multiple avatars with different resolutions specified
437 $this->avatar = ActivityUtils::getLink($element, 'avatar');
438 $this->nickname = ActivityUtils::childContent($element, PoCo::USERNAME, PoCo::NS);
442 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
444 return ActivityUtils::childContent($element, $tag, $namespace);
447 // Try to get a unique id for the source feed
449 private function _getSource($element)
451 $sourceEl = ActivityUtils::child($element, 'source');
453 if (empty($sourceEl)) {
456 $href = ActivityUtils::getLink($sourceEl, 'self');
460 return ActivityUtils::childContent($sourceEl, 'id');
465 static function fromNotice($notice)
467 $object = new ActivityObject();
469 $object->type = ActivityObject::NOTE;
471 $object->id = $notice->uri;
472 $object->title = $notice->content;
473 $object->content = $notice->rendered;
474 $object->link = $notice->bestUrl();
479 static function fromProfile($profile)
481 $object = new ActivityObject();
483 $object->type = ActivityObject::PERSON;
484 $object->id = $profile->getUri();
485 $object->title = $profile->getBestName();
486 $object->link = $profile->profileurl;
487 $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
489 if (isset($profile->lat) && isset($profile->lon)) {
490 $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
493 $object->poco = new PoCo($profile);
498 function asString($tag='activity:object')
500 $xs = new XMLStringer(true);
502 $xs->elementStart($tag);
504 $xs->element('activity:object-type', null, $this->type);
506 $xs->element(self::ID, null, $this->id);
508 if (!empty($this->title)) {
509 $xs->element(self::TITLE, null, $this->title);
512 if (!empty($this->summary)) {
513 $xs->element(self::SUMMARY, null, $this->summary);
516 if (!empty($this->content)) {
517 // XXX: assuming HTML content here
518 $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
521 if (!empty($this->link)) {
522 $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
526 if ($this->type == ActivityObject::PERSON) {
529 'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
531 'href' => empty($this->avatar)
532 ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
533 : $this->avatar->displayUrl()
539 if (!empty($this->geopoint)) {
547 if (!empty($this->poco)) {
548 $xs->raw($this->poco->asString());
551 $xs->elementEnd($tag);
553 return $xs->getString();
558 * Utility class to hold a bunch of constant defining default verb types
562 * @author Evan Prodromou <evan@status.net>
563 * @copyright 2010 StatusNet, Inc.
564 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
565 * @link http://status.net/
570 const POST = 'http://activitystrea.ms/schema/1.0/post';
571 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
572 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
573 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
574 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
575 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
576 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
577 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
578 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
580 // Custom OStatus verbs for the flipside until they're standardized
581 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
582 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
583 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
584 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
587 class ActivityContext
592 public $attention = array();
593 public $conversation;
595 const THR = 'http://purl.org/syndication/thread/1.0';
596 const GEORSS = 'http://www.georss.org/georss';
597 const OSTATUS = 'http://ostatus.org/schema/1.0';
599 const INREPLYTO = 'in-reply-to';
603 const POINT = 'point';
605 const ATTENTION = 'ostatus:attention';
606 const CONVERSATION = 'ostatus:conversation';
608 function __construct($element)
610 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
612 if (!empty($replyToEl)) {
613 $this->replyToID = $replyToEl->getAttribute(self::REF);
614 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
617 $this->location = $this->getLocation($element);
619 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
621 // Multiple attention links allowed
623 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
625 for ($i = 0; $i < $links->length; $i++) {
627 $link = $links->item($i);
629 $linkRel = $link->getAttribute(ActivityUtils::REL);
631 if ($linkRel == self::ATTENTION) {
632 $this->attention[] = $link->getAttribute(self::HREF);
638 * Parse location given as a GeoRSS-simple point, if provided.
639 * http://www.georss.org/simple
641 * @param feed item $entry
642 * @return mixed Location or false
644 function getLocation($dom)
646 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
648 for ($i = 0; $i < $points->length; $i++) {
649 $point = $points->item($i)->textContent;
650 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
651 $point = preg_replace('/\s+/', ' ', $point);
652 $point = trim($point);
653 $coords = explode(' ', $point);
654 if (count($coords) == 2) {
655 list($lat, $lon) = $coords;
656 if (is_numeric($lat) && is_numeric($lon)) {
657 common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
658 return Location::fromLatLon($lat, $lon);
661 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
669 * An activity in the ActivityStrea.ms world
671 * An activity is kind of like a sentence: someone did something
674 * 'someone' is the 'actor'; 'did something' is the verb;
675 * 'something else' is the object.
679 * @author Evan Prodromou <evan@status.net>
680 * @copyright 2010 StatusNet, Inc.
681 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
682 * @link http://status.net/
687 const SPEC = 'http://activitystrea.ms/spec/1.0/';
688 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
691 const OBJECT = 'object';
692 const ACTOR = 'actor';
693 const SUBJECT = 'subject';
694 const OBJECTTYPE = 'object-type';
695 const CONTEXT = 'context';
696 const TARGET = 'target';
698 const ATOM = 'http://www.w3.org/2005/Atom';
700 const AUTHOR = 'author';
701 const PUBLISHED = 'published';
702 const UPDATED = 'updated';
704 public $actor; // an ActivityObject
705 public $verb; // a string (the URL)
706 public $object; // an ActivityObject
707 public $target; // an ActivityObject
708 public $context; // an ActivityObject
709 public $time; // Time of the activity
710 public $link; // an ActivityObject
711 public $entry; // the source entry
712 public $feed; // the source feed
714 public $summary; // summary of activity
715 public $content; // HTML content of activity
716 public $id; // ID of the activity
717 public $title; // title of the activity
720 * Turns a regular old Atom <entry> into a magical activity
722 * @param DOMElement $entry Atom entry to poke at
723 * @param DOMElement $feed Atom feed, for context
726 function __construct($entry = null, $feed = null)
728 if (is_null($entry)) {
732 $this->entry = $entry;
735 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
737 if (!empty($pubEl)) {
738 $this->time = strtotime($pubEl->textContent);
740 // XXX technically an error; being liberal. Good idea...?
741 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
742 if (!empty($updateEl)) {
743 $this->time = strtotime($updateEl->textContent);
749 $this->link = ActivityUtils::getPermalink($entry);
751 $verbEl = $this->_child($entry, self::VERB);
753 if (!empty($verbEl)) {
754 $this->verb = trim($verbEl->textContent);
756 $this->verb = ActivityVerb::POST;
757 // XXX: do other implied stuff here
760 $objectEl = $this->_child($entry, self::OBJECT);
762 if (!empty($objectEl)) {
763 $this->object = new ActivityObject($objectEl);
765 $this->object = new ActivityObject($entry);
768 $actorEl = $this->_child($entry, self::ACTOR);
770 if (!empty($actorEl)) {
772 $this->actor = new ActivityObject($actorEl);
774 } else if (!empty($feed) &&
775 $subjectEl = $this->_child($feed, self::SUBJECT)) {
777 $this->actor = new ActivityObject($subjectEl);
779 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
781 $this->actor = new ActivityObject($authorEl);
783 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
786 $this->actor = new ActivityObject($authorEl);
789 $contextEl = $this->_child($entry, self::CONTEXT);
791 if (!empty($contextEl)) {
792 $this->context = new ActivityContext($contextEl);
794 $this->context = new ActivityContext($entry);
797 $targetEl = $this->_child($entry, self::TARGET);
799 if (!empty($targetEl)) {
800 $this->target = new ActivityObject($targetEl);
803 $this->summary = ActivityUtils::childContent($entry, 'summary');
804 $this->id = ActivityUtils::childContent($entry, 'id');
805 $this->content = ActivityUtils::getContent($entry);
809 * Returns an Atom <entry> based on this activity
811 * @return DOMElement Atom entry
814 function toAtomEntry()
819 function asString($namespace=false)
821 $xs = new XMLStringer(true);
824 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
825 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
826 'xmlns:georss' => 'http://www.georss.org/georss',
827 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
828 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0');
833 $xs->elementStart('entry', $attrs);
835 $xs->element('id', null, $this->id);
836 $xs->element('title', null, $this->title);
837 $xs->element('published', null, common_date_iso8601($this->time));
838 $xs->element('content', array('type' => 'html'), $this->content);
840 if (!empty($this->summary)) {
841 $xs->element('summary', null, $this->summary);
844 if (!empty($this->link)) {
845 $xs->element('link', array('rel' => 'alternate',
846 'type' => 'text/html'),
853 $xs->raw($this->actor->asString('activity:actor'));
854 $xs->element('activity:verb', null, $this->verb);
855 $xs->raw($this->object->asString());
857 $xs->elementEnd('entry');
859 return $xs->getString();
862 private function _child($element, $tag, $namespace=self::SPEC)
864 return ActivityUtils::child($element, $tag, $namespace);