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();
480 * @fixme missing avatar, bio info, etc
482 static function fromProfile($profile)
484 $object = new ActivityObject();
486 $object->type = ActivityObject::PERSON;
487 $object->id = $profile->getUri();
488 $object->title = $profile->getBestName();
489 $object->link = $profile->profileurl;
490 $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
492 if (isset($profile->lat) && isset($profile->lon)) {
493 $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
496 $object->poco = new PoCo($profile);
502 * @fixme missing avatar, bio info, etc
504 function asString($tag='activity:object')
506 $xs = new XMLStringer(true);
508 $xs->elementStart($tag);
510 $xs->element('activity:object-type', null, $this->type);
512 $xs->element(self::ID, null, $this->id);
514 if (!empty($this->title)) {
515 $xs->element(self::TITLE, null, $this->title);
518 if (!empty($this->summary)) {
519 $xs->element(self::SUMMARY, null, $this->summary);
522 if (!empty($this->content)) {
523 // XXX: assuming HTML content here
524 $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
527 if (!empty($this->link)) {
528 $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
532 if ($this->type == ActivityObject::PERSON) {
535 'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
537 'href' => empty($this->avatar)
538 ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
539 : $this->avatar->displayUrl()
545 if (!empty($this->geopoint)) {
553 if (!empty($this->poco)) {
554 $xs->raw($this->poco->asString());
557 $xs->elementEnd($tag);
559 return $xs->getString();
564 * Utility class to hold a bunch of constant defining default verb types
568 * @author Evan Prodromou <evan@status.net>
569 * @copyright 2010 StatusNet, Inc.
570 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
571 * @link http://status.net/
576 const POST = 'http://activitystrea.ms/schema/1.0/post';
577 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
578 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
579 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
580 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
581 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
582 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
583 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
584 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
586 // Custom OStatus verbs for the flipside until they're standardized
587 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
588 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
589 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
590 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
593 class ActivityContext
598 public $attention = array();
599 public $conversation;
601 const THR = 'http://purl.org/syndication/thread/1.0';
602 const GEORSS = 'http://www.georss.org/georss';
603 const OSTATUS = 'http://ostatus.org/schema/1.0';
605 const INREPLYTO = 'in-reply-to';
609 const POINT = 'point';
611 const ATTENTION = 'ostatus:attention';
612 const CONVERSATION = 'ostatus:conversation';
614 function __construct($element)
616 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
618 if (!empty($replyToEl)) {
619 $this->replyToID = $replyToEl->getAttribute(self::REF);
620 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
623 $this->location = $this->getLocation($element);
625 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
627 // Multiple attention links allowed
629 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
631 for ($i = 0; $i < $links->length; $i++) {
633 $link = $links->item($i);
635 $linkRel = $link->getAttribute(ActivityUtils::REL);
637 if ($linkRel == self::ATTENTION) {
638 $this->attention[] = $link->getAttribute(self::HREF);
644 * Parse location given as a GeoRSS-simple point, if provided.
645 * http://www.georss.org/simple
647 * @param feed item $entry
648 * @return mixed Location or false
650 function getLocation($dom)
652 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
654 for ($i = 0; $i < $points->length; $i++) {
655 $point = $points->item($i)->textContent;
656 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
657 $point = preg_replace('/\s+/', ' ', $point);
658 $point = trim($point);
659 $coords = explode(' ', $point);
660 if (count($coords) == 2) {
661 list($lat, $lon) = $coords;
662 if (is_numeric($lat) && is_numeric($lon)) {
663 common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
664 return Location::fromLatLon($lat, $lon);
667 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
675 * An activity in the ActivityStrea.ms world
677 * An activity is kind of like a sentence: someone did something
680 * 'someone' is the 'actor'; 'did something' is the verb;
681 * 'something else' is the object.
685 * @author Evan Prodromou <evan@status.net>
686 * @copyright 2010 StatusNet, Inc.
687 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
688 * @link http://status.net/
693 const SPEC = 'http://activitystrea.ms/spec/1.0/';
694 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
697 const OBJECT = 'object';
698 const ACTOR = 'actor';
699 const SUBJECT = 'subject';
700 const OBJECTTYPE = 'object-type';
701 const CONTEXT = 'context';
702 const TARGET = 'target';
704 const ATOM = 'http://www.w3.org/2005/Atom';
706 const AUTHOR = 'author';
707 const PUBLISHED = 'published';
708 const UPDATED = 'updated';
710 public $actor; // an ActivityObject
711 public $verb; // a string (the URL)
712 public $object; // an ActivityObject
713 public $target; // an ActivityObject
714 public $context; // an ActivityObject
715 public $time; // Time of the activity
716 public $link; // an ActivityObject
717 public $entry; // the source entry
718 public $feed; // the source feed
720 public $summary; // summary of activity
721 public $content; // HTML content of activity
722 public $id; // ID of the activity
723 public $title; // title of the activity
726 * Turns a regular old Atom <entry> into a magical activity
728 * @param DOMElement $entry Atom entry to poke at
729 * @param DOMElement $feed Atom feed, for context
732 function __construct($entry = null, $feed = null)
734 if (is_null($entry)) {
738 $this->entry = $entry;
741 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
743 if (!empty($pubEl)) {
744 $this->time = strtotime($pubEl->textContent);
746 // XXX technically an error; being liberal. Good idea...?
747 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
748 if (!empty($updateEl)) {
749 $this->time = strtotime($updateEl->textContent);
755 $this->link = ActivityUtils::getPermalink($entry);
757 $verbEl = $this->_child($entry, self::VERB);
759 if (!empty($verbEl)) {
760 $this->verb = trim($verbEl->textContent);
762 $this->verb = ActivityVerb::POST;
763 // XXX: do other implied stuff here
766 $objectEl = $this->_child($entry, self::OBJECT);
768 if (!empty($objectEl)) {
769 $this->object = new ActivityObject($objectEl);
771 $this->object = new ActivityObject($entry);
774 $actorEl = $this->_child($entry, self::ACTOR);
776 if (!empty($actorEl)) {
778 $this->actor = new ActivityObject($actorEl);
780 } else if (!empty($feed) &&
781 $subjectEl = $this->_child($feed, self::SUBJECT)) {
783 $this->actor = new ActivityObject($subjectEl);
785 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
787 $this->actor = new ActivityObject($authorEl);
789 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
792 $this->actor = new ActivityObject($authorEl);
795 $contextEl = $this->_child($entry, self::CONTEXT);
797 if (!empty($contextEl)) {
798 $this->context = new ActivityContext($contextEl);
800 $this->context = new ActivityContext($entry);
803 $targetEl = $this->_child($entry, self::TARGET);
805 if (!empty($targetEl)) {
806 $this->target = new ActivityObject($targetEl);
809 $this->summary = ActivityUtils::childContent($entry, 'summary');
810 $this->id = ActivityUtils::childContent($entry, 'id');
811 $this->content = ActivityUtils::getContent($entry);
815 * Returns an Atom <entry> based on this activity
817 * @return DOMElement Atom entry
820 function toAtomEntry()
825 function asString($namespace=false)
827 $xs = new XMLStringer(true);
830 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
831 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
832 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0');
837 $xs->elementStart('entry', $attrs);
839 $xs->element('id', null, $this->id);
840 $xs->element('title', null, $this->title);
841 $xs->element('published', null, common_date_iso8601($this->time));
842 $xs->element('content', array('type' => 'html'), $this->content);
844 if (!empty($this->summary)) {
845 $xs->element('summary', null, $this->summary);
848 if (!empty($this->link)) {
849 $xs->element('link', array('rel' => 'alternate',
850 'type' => 'text/html'),
857 $xs->raw($this->actor->asString('activity:actor'));
858 $xs->element('activity:verb', null, $this->verb);
859 $xs->raw($this->object->asString());
861 $xs->elementEnd('entry');
863 return $xs->getString();
866 private function _child($element, $tag, $namespace=self::SPEC)
868 return ActivityUtils::child($element, $tag, $namespace);