summaryrefslogtreecommitdiff
path: root/plugins/OStatus/lib
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/OStatus/lib')
-rw-r--r--plugins/OStatus/lib/activity.php716
-rw-r--r--plugins/OStatus/lib/hubconfqueuehandler.php (renamed from plugins/OStatus/lib/hubverifyqueuehandler.php)4
-rw-r--r--plugins/OStatus/lib/huboutqueuehandler.php18
-rw-r--r--plugins/OStatus/lib/magicenvelope.php172
-rw-r--r--plugins/OStatus/lib/ostatusqueuehandler.php (renamed from plugins/OStatus/lib/hubdistribqueuehandler.php)88
-rw-r--r--plugins/OStatus/lib/pushinqueuehandler.php49
-rw-r--r--plugins/OStatus/lib/salmon.php34
-rw-r--r--plugins/OStatus/lib/salmonaction.php114
-rw-r--r--plugins/OStatus/lib/salmonqueuehandler.php44
-rw-r--r--plugins/OStatus/lib/webfinger.php18
10 files changed, 427 insertions, 830 deletions
diff --git a/plugins/OStatus/lib/activity.php b/plugins/OStatus/lib/activity.php
deleted file mode 100644
index f25a843c3..000000000
--- a/plugins/OStatus/lib/activity.php
+++ /dev/null
@@ -1,716 +0,0 @@
-<?php
-/**
- * StatusNet, the distributed open-source microblogging tool
- *
- * An activity
- *
- * PHP version 5
- *
- * LICENCE: This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * @category OStatus
- * @package StatusNet
- * @author Evan Prodromou <evan@status.net>
- * @copyright 2010 StatusNet, Inc.
- * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
- * @link http://status.net/
- */
-
-if (!defined('STATUSNET')) {
- exit(1);
-}
-
-/**
- * Utilities for turning DOMish things into Activityish things
- *
- * Some common functions that I didn't have the bandwidth to try to factor
- * into some kind of reasonable superclass, so just dumped here. Might
- * be useful to have an ActivityObject parent class or something.
- *
- * @category OStatus
- * @package StatusNet
- * @author Evan Prodromou <evan@status.net>
- * @copyright 2010 StatusNet, Inc.
- * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
- * @link http://status.net/
- */
-
-class ActivityUtils
-{
- const ATOM = 'http://www.w3.org/2005/Atom';
-
- const LINK = 'link';
- const REL = 'rel';
- const TYPE = 'type';
- const HREF = 'href';
-
- const CONTENT = 'content';
- const SRC = 'src';
-
- /**
- * Get the permalink for an Activity object
- *
- * @param DOMElement $element A DOM element
- *
- * @return string related link, if any
- */
-
- static function getPermalink($element)
- {
- return self::getLink($element, 'alternate', 'text/html');
- }
-
- /**
- * Get the permalink for an Activity object
- *
- * @param DOMElement $element A DOM element
- *
- * @return string related link, if any
- */
-
- static function getLink($element, $rel, $type=null)
- {
- $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
-
- foreach ($links as $link) {
-
- $linkRel = $link->getAttribute(self::REL);
- $linkType = $link->getAttribute(self::TYPE);
-
- if ($linkRel == $rel &&
- (is_null($type) || $linkType == $type)) {
- return $link->getAttribute(self::HREF);
- }
- }
-
- return null;
- }
-
- /**
- * Gets the first child element with the given tag
- *
- * @param DOMElement $element element to pick at
- * @param string $tag tag to look for
- * @param string $namespace Namespace to look under
- *
- * @return DOMElement found element or null
- */
-
- static function child($element, $tag, $namespace=self::ATOM)
- {
- $els = $element->childNodes;
- if (empty($els) || $els->length == 0) {
- return null;
- } else {
- for ($i = 0; $i < $els->length; $i++) {
- $el = $els->item($i);
- if ($el->localName == $tag && $el->namespaceURI == $namespace) {
- return $el;
- }
- }
- }
- }
-
- /**
- * Grab the text content of a DOM element child of the current element
- *
- * @param DOMElement $element Element whose children we examine
- * @param string $tag Tag to look up
- * @param string $namespace Namespace to use, defaults to Atom
- *
- * @return string content of the child
- */
-
- static function childContent($element, $tag, $namespace=self::ATOM)
- {
- $el = self::child($element, $tag, $namespace);
-
- if (empty($el)) {
- return null;
- } else {
- return $el->textContent;
- }
- }
-
- /**
- * Get the content of an atom:entry-like object
- *
- * @param DOMElement $element The element to examine.
- *
- * @return string unencoded HTML content of the element, like "This -&lt; is <b>HTML</b>."
- *
- * @todo handle remote content
- * @todo handle embedded XML mime types
- * @todo handle base64-encoded non-XML and non-text mime types
- */
-
- static function getContent($element)
- {
- $contentEl = ActivityUtils::child($element, self::CONTENT);
-
- if (!empty($contentEl)) {
-
- $src = $contentEl->getAttribute(self::SRC);
-
- if (!empty($src)) {
- throw new ClientException(_("Can't handle remote content yet."));
- }
-
- $type = $contentEl->getAttribute(self::TYPE);
-
- // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
-
- if ($type == 'text') {
- return $contentEl->textContent;
- } else if ($type == 'html') {
- $text = $contentEl->textContent;
- return htmlspecialchars_decode($text, ENT_QUOTES);
- } else if ($type == 'xhtml') {
- $divEl = ActivityUtils::child($contentEl, 'div');
- if (empty($divEl)) {
- return null;
- }
- $doc = $divEl->ownerDocument;
- $text = '';
- $children = $divEl->childNodes;
-
- for ($i = 0; $i < $children->length; $i++) {
- $child = $children->item($i);
- $text .= $doc->saveXML($child);
- }
- return trim($text);
- } else if (in_array(array('text/xml', 'application/xml'), $type) ||
- preg_match('#(+|/)xml$#', $type)) {
- throw new ClientException(_("Can't handle embedded XML content yet."));
- } else if (strncasecmp($type, 'text/', 5)) {
- return $contentEl->textContent;
- } else {
- throw new ClientException(_("Can't handle embedded Base64 content yet."));
- }
- }
- }
-}
-
-/**
- * A noun-ish thing in the activity universe
- *
- * The activity streams spec talks about activity objects, while also having
- * a tag activity:object, which is in fact an activity object. Aaaaaah!
- *
- * This is just a thing in the activity universe. Can be the subject, object,
- * or indirect object (target!) of an activity verb. Rotten name, and I'm
- * propagating it. *sigh*
- *
- * @category OStatus
- * @package StatusNet
- * @author Evan Prodromou <evan@status.net>
- * @copyright 2010 StatusNet, Inc.
- * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
- * @link http://status.net/
- */
-
-class ActivityObject
-{
- const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
- const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
- const NOTE = 'http://activitystrea.ms/schema/1.0/note';
- const STATUS = 'http://activitystrea.ms/schema/1.0/status';
- const FILE = 'http://activitystrea.ms/schema/1.0/file';
- const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
- const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
- const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
- const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
- const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
- const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
- const PERSON = 'http://activitystrea.ms/schema/1.0/person';
- const GROUP = 'http://activitystrea.ms/schema/1.0/group';
- const PLACE = 'http://activitystrea.ms/schema/1.0/place';
- const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
- // ^^^^^^^^^^ tea!
-
- // Atom elements we snarf
-
- const TITLE = 'title';
- const SUMMARY = 'summary';
- const ID = 'id';
- const SOURCE = 'source';
-
- const NAME = 'name';
- const URI = 'uri';
- const EMAIL = 'email';
-
- public $element;
- public $type;
- public $id;
- public $title;
- public $summary;
- public $content;
- public $link;
- public $source;
- public $avatar;
-
- /**
- * Constructor
- *
- * This probably needs to be refactored
- * to generate a local class (ActivityPerson, ActivityFile, ...)
- * based on the object type.
- *
- * @param DOMElement $element DOM thing to turn into an Activity thing
- */
-
- function __construct($element = null)
- {
- if (empty($element)) {
- return;
- }
-
- $this->element = $element;
-
- if ($element->tagName == 'author') {
-
- $this->type = self::PERSON; // XXX: is this fair?
- $this->title = $this->_childContent($element, self::NAME);
- $this->id = $this->_childContent($element, self::URI);
-
- if (empty($this->id)) {
- $email = $this->_childContent($element, self::EMAIL);
- if (!empty($email)) {
- // XXX: acct: ?
- $this->id = 'mailto:'.$email;
- }
- }
-
- } else {
-
- $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
- Activity::SPEC);
-
- if (empty($this->type)) {
- $this->type = ActivityObject::NOTE;
- }
-
- $this->id = $this->_childContent($element, self::ID);
- $this->title = $this->_childContent($element, self::TITLE);
- $this->summary = $this->_childContent($element, self::SUMMARY);
-
- $this->source = $this->_getSource($element);
-
- $this->content = ActivityUtils::getContent($element);
-
- $this->link = ActivityUtils::getPermalink($element);
-
- // XXX: grab PoCo stuff
- }
-
- // Some per-type attributes...
- if ($this->type == self::PERSON || $this->type == self::GROUP) {
- $this->displayName = $this->title;
-
- // @fixme we may have multiple avatars with different resolutions specified
- $this->avatar = ActivityUtils::getLink($element, 'avatar');
- }
- }
-
- private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
- {
- return ActivityUtils::childContent($element, $tag, $namespace);
- }
-
- // Try to get a unique id for the source feed
-
- private function _getSource($element)
- {
- $sourceEl = ActivityUtils::child($element, 'source');
-
- if (empty($sourceEl)) {
- return null;
- } else {
- $href = ActivityUtils::getLink($sourceEl, 'self');
- if (!empty($href)) {
- return $href;
- } else {
- return ActivityUtils::childContent($sourceEl, 'id');
- }
- }
- }
-
- static function fromNotice($notice)
- {
- $object = new ActivityObject();
-
- $object->type = ActivityObject::NOTE;
-
- $object->id = $notice->uri;
- $object->title = $notice->content;
- $object->content = $notice->rendered;
- $object->link = $notice->bestUrl();
-
- return $object;
- }
-
- static function fromProfile($profile)
- {
- $object = new ActivityObject();
-
- $object->type = ActivityObject::PERSON;
- $object->id = $profile->getUri();
- $object->title = $profile->getBestName();
- $object->link = $profile->profileurl;
-
- return $object;
- }
-
- function asString($tag='activity:object')
- {
- $xs = new XMLStringer(true);
-
- $xs->elementStart($tag);
-
- $xs->element('activity:object-type', null, $this->type);
-
- $xs->element(self::ID, null, $this->id);
-
- if (!empty($this->title)) {
- $xs->element(self::TITLE, null, $this->title);
- }
-
- if (!empty($this->summary)) {
- $xs->element(self::SUMMARY, null, $this->summary);
- }
-
- if (!empty($this->content)) {
- // XXX: assuming HTML content here
- $xs->element(self::CONTENT, array('type' => 'html'), $this->content);
- }
-
- if (!empty($this->link)) {
- $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
- $this->content);
- }
-
- $xs->elementEnd($tag);
-
- return $xs->getString();
- }
-}
-
-/**
- * Utility class to hold a bunch of constant defining default verb types
- *
- * @category OStatus
- * @package StatusNet
- * @author Evan Prodromou <evan@status.net>
- * @copyright 2010 StatusNet, Inc.
- * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
- * @link http://status.net/
- */
-
-class ActivityVerb
-{
- const POST = 'http://activitystrea.ms/schema/1.0/post';
- const SHARE = 'http://activitystrea.ms/schema/1.0/share';
- const SAVE = 'http://activitystrea.ms/schema/1.0/save';
- const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
- const PLAY = 'http://activitystrea.ms/schema/1.0/play';
- const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
- const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
- const JOIN = 'http://activitystrea.ms/schema/1.0/join';
- const TAG = 'http://activitystrea.ms/schema/1.0/tag';
-
- // Custom OStatus verbs for the flipside until they're standardized
- const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
- const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
- const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
- const LEAVE = 'http://ostatus.org/schema/1.0/leave';
-}
-
-class ActivityContext
-{
- public $replyToID;
- public $replyToUrl;
- public $location;
- public $attention = array();
- public $conversation;
-
- const THR = 'http://purl.org/syndication/thread/1.0';
- const GEORSS = 'http://www.georss.org/georss';
- const OSTATUS = 'http://ostatus.org/schema/1.0';
-
- const INREPLYTO = 'in-reply-to';
- const REF = 'ref';
- const HREF = 'href';
-
- const POINT = 'point';
-
- const ATTENTION = 'ostatus:attention';
- const CONVERSATION = 'ostatus:conversation';
-
- function __construct($element)
- {
- $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
-
- if (!empty($replyToEl)) {
- $this->replyToID = $replyToEl->getAttribute(self::REF);
- $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
- }
-
- $this->location = $this->getLocation($element);
-
- $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
-
- // Multiple attention links allowed
-
- $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
-
- for ($i = 0; $i < $links->length; $i++) {
-
- $link = $links->item($i);
-
- $linkRel = $link->getAttribute(ActivityUtils::REL);
-
- if ($linkRel == self::ATTENTION) {
- $this->attention[] = $link->getAttribute(self::HREF);
- }
- }
- }
-
- /**
- * Parse location given as a GeoRSS-simple point, if provided.
- * http://www.georss.org/simple
- *
- * @param feed item $entry
- * @return mixed Location or false
- */
- function getLocation($dom)
- {
- $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
-
- for ($i = 0; $i < $points->length; $i++) {
- $point = $points->item($i)->textContent;
- $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
- $point = preg_replace('/\s+/', ' ', $point);
- $point = trim($point);
- $coords = explode(' ', $point);
- if (count($coords) == 2) {
- list($lat, $lon) = $coords;
- if (is_numeric($lat) && is_numeric($lon)) {
- common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
- return Location::fromLatLon($lat, $lon);
- }
- }
- common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
- }
-
- return null;
- }
-}
-
-/**
- * An activity in the ActivityStrea.ms world
- *
- * An activity is kind of like a sentence: someone did something
- * to something else.
- *
- * 'someone' is the 'actor'; 'did something' is the verb;
- * 'something else' is the object.
- *
- * @category OStatus
- * @package StatusNet
- * @author Evan Prodromou <evan@status.net>
- * @copyright 2010 StatusNet, Inc.
- * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
- * @link http://status.net/
- */
-
-class Activity
-{
- const SPEC = 'http://activitystrea.ms/spec/1.0/';
- const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
-
- const VERB = 'verb';
- const OBJECT = 'object';
- const ACTOR = 'actor';
- const SUBJECT = 'subject';
- const OBJECTTYPE = 'object-type';
- const CONTEXT = 'context';
- const TARGET = 'target';
-
- const ATOM = 'http://www.w3.org/2005/Atom';
-
- const AUTHOR = 'author';
- const PUBLISHED = 'published';
- const UPDATED = 'updated';
-
- public $actor; // an ActivityObject
- public $verb; // a string (the URL)
- public $object; // an ActivityObject
- public $target; // an ActivityObject
- public $context; // an ActivityObject
- public $time; // Time of the activity
- public $link; // an ActivityObject
- public $entry; // the source entry
- public $feed; // the source feed
-
- public $summary; // summary of activity
- public $content; // HTML content of activity
- public $id; // ID of the activity
- public $title; // title of the activity
-
- /**
- * Turns a regular old Atom <entry> into a magical activity
- *
- * @param DOMElement $entry Atom entry to poke at
- * @param DOMElement $feed Atom feed, for context
- */
-
- function __construct($entry = null, $feed = null)
- {
- if (is_null($entry)) {
- return;
- }
-
- $this->entry = $entry;
- $this->feed = $feed;
-
- $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
-
- if (!empty($pubEl)) {
- $this->time = strtotime($pubEl->textContent);
- } else {
- // XXX technically an error; being liberal. Good idea...?
- $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
- if (!empty($updateEl)) {
- $this->time = strtotime($updateEl->textContent);
- } else {
- $this->time = null;
- }
- }
-
- $this->link = ActivityUtils::getPermalink($entry);
-
- $verbEl = $this->_child($entry, self::VERB);
-
- if (!empty($verbEl)) {
- $this->verb = trim($verbEl->textContent);
- } else {
- $this->verb = ActivityVerb::POST;
- // XXX: do other implied stuff here
- }
-
- $objectEl = $this->_child($entry, self::OBJECT);
-
- if (!empty($objectEl)) {
- $this->object = new ActivityObject($objectEl);
- } else {
- $this->object = new ActivityObject($entry);
- }
-
- $actorEl = $this->_child($entry, self::ACTOR);
-
- if (!empty($actorEl)) {
-
- $this->actor = new ActivityObject($actorEl);
-
- } else if (!empty($feed) &&
- $subjectEl = $this->_child($feed, self::SUBJECT)) {
-
- $this->actor = new ActivityObject($subjectEl);
-
- } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
-
- $this->actor = new ActivityObject($authorEl);
-
- } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
- self::ATOM)) {
-
- $this->actor = new ActivityObject($authorEl);
- }
-
- $contextEl = $this->_child($entry, self::CONTEXT);
-
- if (!empty($contextEl)) {
- $this->context = new ActivityContext($contextEl);
- } else {
- $this->context = new ActivityContext($entry);
- }
-
- $targetEl = $this->_child($entry, self::TARGET);
-
- if (!empty($targetEl)) {
- $this->target = new ActivityObject($targetEl);
- }
-
- $this->summary = ActivityUtils::childContent($entry, 'summary');
- $this->id = ActivityUtils::childContent($entry, 'id');
- $this->content = ActivityUtils::getContent($entry);
- }
-
- /**
- * Returns an Atom <entry> based on this activity
- *
- * @return DOMElement Atom entry
- */
-
- function toAtomEntry()
- {
- return null;
- }
-
- function asString($namespace=false)
- {
- $xs = new XMLStringer(true);
-
- if ($namespace) {
- $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
- 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
- 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0');
- } else {
- $attrs = array();
- }
-
- $xs->elementStart('entry', $attrs);
-
- $xs->element('id', null, $this->id);
- $xs->element('title', null, $this->title);
- $xs->element('published', null, common_date_iso8601($this->time));
- $xs->element('content', array('type' => 'html'), $this->content);
-
- if (!empty($this->summary)) {
- $xs->element('summary', null, $this->summary);
- }
-
- if (!empty($this->link)) {
- $xs->element('link', array('rel' => 'alternate',
- 'type' => 'text/html'),
- $this->link);
- }
-
- // XXX: add context
- // XXX: add target
-
- $xs->raw($this->actor->asString());
- $xs->element('activity:verb', null, $this->verb);
- $xs->raw($this->object->asString());
-
- $xs->elementEnd('entry');
-
- return $xs->getString();
- }
-
- private function _child($element, $tag, $namespace=self::SPEC)
- {
- return ActivityUtils::child($element, $tag, $namespace);
- }
-} \ No newline at end of file
diff --git a/plugins/OStatus/lib/hubverifyqueuehandler.php b/plugins/OStatus/lib/hubconfqueuehandler.php
index 7ce9e1431..c8e0b72fe 100644
--- a/plugins/OStatus/lib/hubverifyqueuehandler.php
+++ b/plugins/OStatus/lib/hubconfqueuehandler.php
@@ -22,11 +22,11 @@
* @package Hub
* @author Brion Vibber <brion@status.net>
*/
-class HubVerifyQueueHandler extends QueueHandler
+class HubConfQueueHandler extends QueueHandler
{
function transport()
{
- return 'hubverify';
+ return 'hubconf';
}
function handle($data)
diff --git a/plugins/OStatus/lib/huboutqueuehandler.php b/plugins/OStatus/lib/huboutqueuehandler.php
index 0791c7e5d..3ad94646e 100644
--- a/plugins/OStatus/lib/huboutqueuehandler.php
+++ b/plugins/OStatus/lib/huboutqueuehandler.php
@@ -33,6 +33,7 @@ class HubOutQueueHandler extends QueueHandler
{
$sub = $data['sub'];
$atom = $data['atom'];
+ $retries = $data['retries'];
assert($sub instanceof HubSub);
assert(is_string($atom));
@@ -40,13 +41,20 @@ class HubOutQueueHandler extends QueueHandler
try {
$sub->push($atom);
} catch (Exception $e) {
- common_log(LOG_ERR, "Failed PuSH to $sub->callback for $sub->topic: " .
- $e->getMessage());
- // @fixme Reschedule a later delivery?
- return true;
+ $retries--;
+ $msg = "Failed PuSH to $sub->callback for $sub->topic: " .
+ $e->getMessage();
+ if ($retries > 0) {
+ common_log(LOG_ERR, "$msg; scheduling for $retries more tries");
+
+ // @fixme when we have infrastructure to schedule a retry
+ // after a delay, use it.
+ $sub->distribute($atom, $retries);
+ } else {
+ common_log(LOG_ERR, "$msg; discarding");
+ }
}
return true;
}
}
-
diff --git a/plugins/OStatus/lib/magicenvelope.php b/plugins/OStatus/lib/magicenvelope.php
new file mode 100644
index 000000000..81f4609c5
--- /dev/null
+++ b/plugins/OStatus/lib/magicenvelope.php
@@ -0,0 +1,172 @@
+<?php
+/**
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * A sample module to show best practices for StatusNet plugins
+ *
+ * PHP version 5
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @package StatusNet
+ * @author James Walker <james@status.net>
+ * @copyright 2010 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link http://status.net/
+ */
+
+class MagicEnvelope
+{
+ const ENCODING = 'base64url';
+
+ const NS = 'http://salmon-protocol.org/ns/magic-env';
+
+ private function normalizeUser($user_id)
+ {
+ if (substr($user_id, 0, 5) == 'http:' ||
+ substr($user_id, 0, 6) == 'https:' ||
+ substr($user_id, 0, 5) == 'acct:') {
+ return $user_id;
+ }
+
+ if (strpos($user_id, '@') !== FALSE) {
+ return 'acct:' . $user_id;
+ }
+
+ return 'http://' . $user_id;
+ }
+
+ public function getKeyPair($signer_uri)
+ {
+ return 'RSA.79_L2gq-TD72Nsb5yGS0r9stLLpJZF5AHXyxzWmQmlqKl276LEJEs8CppcerLcR90MbYQUwt-SX9slx40Yq3vA==.AQAB.AR-jo5KMfSISmDAT2iMs2_vNFgWRjl5rbJVvA0SpGIEWyPdCGxlPtCbTexp8-0ZEIe8a4SyjatBECH5hxgMTpw==';
+ }
+
+
+ public function signMessage($text, $mimetype, $signer_uri)
+ {
+ $signer_uri = $this->normalizeUser($signer_uri);
+
+ if (!$this->checkAuthor($text, $signer_uri)) {
+ return false;
+ }
+
+ $signature_alg = Magicsig::fromString($this->getKeyPair($signer_uri));
+ $armored_text = base64_encode($text);
+
+ return array(
+ 'data' => $armored_text,
+ 'encoding' => MagicEnvelope::ENCODING,
+ 'data_type' => $mimetype,
+ 'sig' => $signature_alg->sign($armored_text),
+ 'alg' => $signature_alg->getName()
+ );
+
+
+ }
+
+ public function unfold($env)
+ {
+ $dom = new DOMDocument();
+ $dom->loadXML(base64_decode($env['data']));
+
+ if ($dom->documentElement->tagName != 'entry') {
+ return false;
+ }
+
+ $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
+ $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
+ $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
+ $data->setAttribute('type', $env['data_type']);
+ $prov->appendChild($data);
+ $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
+ $prov->appendChild($enc);
+ $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
+ $prov->appendChild($alg);
+ $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
+ $prov->appendChild($sig);
+
+ $dom->documentElement->appendChild($prov);
+
+ return $dom->saveXML();
+ }
+
+ public function getAuthor($text) {
+ $doc = new DOMDocument();
+ if (!$doc->loadXML($text)) {
+ return FALSE;
+ }
+
+ if ($doc->documentElement->tagName == 'entry') {
+ $authors = $doc->documentElement->getElementsByTagName('author');
+ foreach ($authors as $author) {
+ $uris = $author->getElementsByTagName('uri');
+ foreach ($uris as $uri) {
+ return $this->normalizeUser($uri->nodeValue);
+ }
+ }
+ }
+ }
+
+ public function checkAuthor($text, $signer_uri)
+ {
+ return ($this->getAuthor($text) == $signer_uri);
+ }
+
+ public function verify($env)
+ {
+ if ($env['alg'] != 'RSA-SHA256') {
+ return false;
+ }
+
+ if ($env['encoding'] != MagicEnvelope::ENCODING) {
+ return false;
+ }
+
+ $text = base64_decode($env['data']);
+ $signer_uri = $this->getAuthor($text);
+
+ $verifier = Magicsig::fromString($this->getKeyPair($signer_uri));
+
+ return $verifier->verify($env['data'], $env['sig']);
+ }
+
+ public function parse($text)
+ {
+ $dom = DOMDocument::loadXML($text);
+ return $this->fromDom($dom);
+ }
+
+ public function fromDom($dom)
+ {
+ if ($dom->documentElement->tagName == 'entry') {
+ $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
+ } else if ($dom->documentElement->tagName == 'me:env') {
+ $env_element = $dom->documentElement;
+ } else {
+ return false;
+ }
+
+ $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
+
+ return array(
+ 'data' => trim($data_element->nodeValue),
+ 'data_type' => $data_element->getAttribute('type'),
+ 'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
+ 'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
+ 'sig' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0)->nodeValue,
+ );
+ }
+
+}
diff --git a/plugins/OStatus/lib/hubdistribqueuehandler.php b/plugins/OStatus/lib/ostatusqueuehandler.php
index 245a57f72..0da85600f 100644
--- a/plugins/OStatus/lib/hubdistribqueuehandler.php
+++ b/plugins/OStatus/lib/ostatusqueuehandler.php
@@ -18,46 +18,77 @@
*/
/**
- * Send a PuSH subscription verification from our internal hub.
- * Queue up final distribution for
- * @package Hub
+ * Prepare PuSH and Salmon distributions for an outgoing message.
+ *
+ * @package OStatusPlugin
* @author Brion Vibber <brion@status.net>
*/
-class HubDistribQueueHandler extends QueueHandler
+class OStatusQueueHandler extends QueueHandler
{
function transport()
{
- return 'hubdistrib';
+ return 'ostatus';
}
function handle($notice)
{
assert($notice instanceof Notice);
- $this->pushUser($notice);
+ $this->notice = $notice;
+ $this->user = User::staticGet($notice->profile_id);
+
+ $this->pushUser();
+
foreach ($notice->getGroups() as $group) {
- $this->pushGroup($notice, $group->group_id);
+ $oprofile = Ostatus_profile::staticGet('group_id', $group->id);
+ if ($oprofile) {
+ $this->pingReply($oprofile);
+ } else {
+ $this->pushGroup($group->id);
+ }
+ }
+
+ foreach ($notice->getReplies() as $profile_id) {
+ $oprofile = Ostatus_profile::staticGet('profile_id', $profile_id);
+ if ($oprofile) {
+ $this->pingReply($oprofile);
+ }
}
+
return true;
}
-
- function pushUser($notice)
+
+ function pushUser()
{
- // See if there's any PuSH subscriptions, including OStatus clients.
- // @fixme handle group subscriptions as well
- // http://identi.ca/api/statuses/user_timeline/1.atom
- $feed = common_local_url('ApiTimelineUser',
- array('id' => $notice->profile_id,
- 'format' => 'atom'));
- $this->pushFeed($feed, array($this, 'userFeedForNotice'), $notice);
+ if ($this->user) {
+ // For local posts, ping the PuSH hub to update their feed.
+ // http://identi.ca/api/statuses/user_timeline/1.atom
+ $feed = common_local_url('ApiTimelineUser',
+ array('id' => $this->user->id,
+ 'format' => 'atom'));
+ $this->pushFeed($feed, array($this, 'userFeedForNotice'));
+ }
}
- function pushGroup($notice, $group_id)
+ function pushGroup($group_id)
{
+ // For a local group, ping the PuSH hub to update its feed.
+ // Updates may come from either a local or a remote user.
$feed = common_local_url('ApiTimelineGroup',
array('id' => $group_id,
'format' => 'atom'));
- $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id, $notice);
+ $this->pushFeed($feed, array($this, 'groupFeedForNotice'), $group_id);
+ }
+
+ function pingReply($oprofile)
+ {
+ if ($this->user) {
+ // For local posts, send a Salmon ping to the mentioned
+ // remote user or group.
+ // @fixme as an optimization we can skip this if the
+ // remote profile is subscribed to the author.
+ $oprofile->notifyDeferred($this->notice);
+ }
}
/**
@@ -122,31 +153,26 @@ class HubDistribQueueHandler extends QueueHandler
function pushFeedInternal($atom, $sub)
{
common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
- $qm = QueueManager::get();
while ($sub->fetch()) {
- common_log(LOG_INFO, "Prepping PuSH distribution to $sub->callback for $sub->topic");
- $data = array('sub' => clone($sub),
- 'atom' => $atom);
- $qm->enqueue($data, 'hubout');
+ $sub->distribute($atom);
}
}
/**
* Build a single-item version of the sending user's Atom feed.
- * @param Notice $notice
* @return string
*/
- function userFeedForNotice($notice)
+ function userFeedForNotice()
{
// @fixme this feels VERY hacky...
// should probably be a cleaner way to do it
ob_start();
$api = new ApiTimelineUserAction();
- $api->prepare(array('id' => $notice->profile_id,
+ $api->prepare(array('id' => $this->notice->profile_id,
'format' => 'atom',
- 'max_id' => $notice->id,
- 'since_id' => $notice->id - 1));
+ 'max_id' => $this->notice->id,
+ 'since_id' => $this->notice->id - 1));
$api->showTimeline();
$feed = ob_get_clean();
@@ -158,7 +184,7 @@ class HubDistribQueueHandler extends QueueHandler
return $feed;
}
- function groupFeedForNotice($group_id, $notice)
+ function groupFeedForNotice($group_id)
{
// @fixme this feels VERY hacky...
// should probably be a cleaner way to do it
@@ -167,8 +193,8 @@ class HubDistribQueueHandler extends QueueHandler
$api = new ApiTimelineGroupAction();
$args = array('id' => $group_id,
'format' => 'atom',
- 'max_id' => $notice->id,
- 'since_id' => $notice->id - 1);
+ 'max_id' => $this->notice->id,
+ 'since_id' => $this->notice->id - 1);
$api->prepare($args);
$api->handle($args);
$feed = ob_get_clean();
diff --git a/plugins/OStatus/lib/pushinqueuehandler.php b/plugins/OStatus/lib/pushinqueuehandler.php
new file mode 100644
index 000000000..a90f52df2
--- /dev/null
+++ b/plugins/OStatus/lib/pushinqueuehandler.php
@@ -0,0 +1,49 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Process a feed distribution POST from a PuSH hub.
+ * @package FeedSub
+ * @author Brion Vibber <brion@status.net>
+ */
+
+class PushInQueueHandler extends QueueHandler
+{
+ function transport()
+ {
+ return 'pushin';
+ }
+
+ function handle($data)
+ {
+ assert(is_array($data));
+
+ $feedsub_id = $data['feedsub_id'];
+ $post = $data['post'];
+ $hmac = $data['hmac'];
+
+ $feedsub = FeedSub::staticGet('id', $feedsub_id);
+ if ($feedsub) {
+ $feedsub->receive($post, $hmac);
+ } else {
+ common_log(LOG_ERR, "Discarding POST to unknown feed subscription id $feedsub_id");
+ }
+ return true;
+ }
+}
diff --git a/plugins/OStatus/lib/salmon.php b/plugins/OStatus/lib/salmon.php
index df17a7006..b5f178cc6 100644
--- a/plugins/OStatus/lib/salmon.php
+++ b/plugins/OStatus/lib/salmon.php
@@ -28,13 +28,26 @@
*/
class Salmon
{
+ /**
+ * Sign and post the given Atom entry as a Salmon message.
+ *
+ * @fixme pass through the actor for signing?
+ *
+ * @param string $endpoint_uri
+ * @param string $xml
+ * @return boolean success
+ */
public function post($endpoint_uri, $xml)
{
if (empty($endpoint_uri)) {
- return FALSE;
+ return false;
+ }
+
+ if (!common_config('ostatus', 'skip_signatures')) {
+ $xml = $this->createMagicEnv($xml);
}
- $headers = array('Content-type: application/atom+xml');
+ $headers = array('Content-Type: application/atom+xml');
try {
$client = new HTTPClient();
@@ -49,19 +62,28 @@ class Salmon
$response->getStatus() . ': ' . $response->getBody());
return false;
}
-
+ return true;
}
- public function createMagicEnv($text, $userid)
+ public function createMagicEnv($text)
{
+ $magic_env = new MagicEnvelope();
+
+ // TODO: Should probably be getting the signer uri as an argument?
+ $signer_uri = $magic_env->getAuthor($text);
+ $env = $magic_env->signMessage($text, 'application/atom+xml', $signer_uri);
+ return $magic_env->unfold($env);
}
- public function verifyMagicEnv($env)
+ public function verifyMagicEnv($dom)
{
+ $magic_env = new MagicEnvelope();
+
+ $env = $magic_env->fromDom($dom);
-
+ return $magic_env->verify($env);
}
}
diff --git a/plugins/OStatus/lib/salmonaction.php b/plugins/OStatus/lib/salmonaction.php
index 4e5ed7fe6..a03169101 100644
--- a/plugins/OStatus/lib/salmonaction.php
+++ b/plugins/OStatus/lib/salmonaction.php
@@ -38,11 +38,11 @@ class SalmonAction extends Action
parent::prepare($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
- $this->clientError(_('This method requires a POST.'));
+ $this->clientError(_m('This method requires a POST.'));
}
- if ($_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
- $this->clientError(_('Salmon requires application/atom+xml'));
+ if (empty($_SERVER['CONTENT_TYPE']) || $_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
+ $this->clientError(_m('Salmon requires application/atom+xml'));
}
$xml = file_get_contents('php://input');
@@ -54,7 +54,15 @@ class SalmonAction extends Action
common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
$this->clientError(_m('Salmon post must be an Atom entry.'));
}
- // XXX: check the signature
+
+ // Check the signature
+ $salmon = new Salmon;
+ if (!common_config('ostatus', 'skip_signatures')) {
+ if (!$salmon->verifyMagicEnv($dom)) {
+ common_log(LOG_DEBUG, "Salmon signature verification failed.");
+ $this->clientError(_m('Salmon signature verification failed.'));
+ }
+ }
$this->act = new Activity($dom->documentElement);
return true;
@@ -68,8 +76,7 @@ class SalmonAction extends Action
{
StatusNet::setApi(true); // Send smaller error pages
- // TODO : Insert new $xml -> notice code
-
+ common_log(LOG_DEBUG, "Got a " . $this->act->verb);
if (Event::handle('StartHandleSalmon', array($this->activity))) {
switch ($this->act->verb)
{
@@ -95,8 +102,14 @@ class SalmonAction extends Action
case ActivityVerb::JOIN:
$this->handleJoin();
break;
+ case ActivityVerb::LEAVE:
+ $this->handleLeave();
+ break;
+ case ActivityVerb::UPDATE_PROFILE:
+ $this->handleUpdateProfile();
+ break;
default:
- throw new ClientException(_("Unimplemented."));
+ throw new ClientException(_m("Unrecognized activity type."));
}
Event::handle('EndHandleSalmon', array($this->activity));
}
@@ -104,48 +117,57 @@ class SalmonAction extends Action
function handlePost()
{
- throw new ClientException(_("Unimplemented!"));
+ throw new ClientException(_m("This target doesn't understand posts."));
}
function handleFollow()
{
- throw new ClientException(_("Unimplemented!"));
+ throw new ClientException(_m("This target doesn't understand follows."));
}
function handleUnfollow()
{
- throw new ClientException(_("Unimplemented!"));
+ throw new ClientException(_m("This target doesn't understand unfollows."));
}
function handleFavorite()
{
- throw new ClientException(_("Unimplemented!"));
+ throw new ClientException(_m("This target doesn't understand favorites."));
}
- /**
- * Remote user doesn't like one of our posts after all!
- * Confirm the post is ours, and delete a local favorite event.
- */
-
function handleUnfavorite()
{
- throw new ClientException(_("Unimplemented!"));
+ throw new ClientException(_m("This target doesn't understand unfavorites."));
}
- /**
- * Hmmmm
- */
function handleShare()
{
- throw new ClientException(_("Unimplemented!"));
+ throw new ClientException(_m("This target doesn't understand share events."));
+ }
+
+ function handleJoin()
+ {
+ throw new ClientException(_m("This target doesn't understand joins."));
+ }
+
+ function handleLeave()
+ {
+ throw new ClientException(_m("This target doesn't understand leave events."));
}
/**
- * Hmmmm
+ * Remote user sent us an update to their profile.
+ * If we already know them, accept the updates.
*/
- function handleJoin()
+ function handleUpdateProfile()
{
- throw new ClientException(_("Unimplemented!"));
+ $oprofile = Ostatus_profile::getActorProfile($this->act);
+ if ($oprofile) {
+ common_log(LOG_INFO, "Got a profile-update ping from $oprofile->uri");
+ $oprofile->updateFromActivityObject($this->act->actor);
+ } else {
+ common_log(LOG_INFO, "Ignoring profile-update ping from unknown " . $this->act->actor->id);
+ }
}
/**
@@ -156,54 +178,16 @@ class SalmonAction extends Action
$actor = $this->act->actor;
if (empty($actor->id)) {
common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
+ common_log(LOG_ERR, "activity with no actor: " . var_export($this->act, true));
throw new Exception("Received a salmon slap from unidentified actor.");
}
- return Ostatus_profile::ensureActorProfile($this->act);
+ return Ostatus_profile::ensureActivityObjectProfile($actor);
}
function saveNotice()
{
$oprofile = $this->ensureProfile();
-
- // Get (safe!) HTML and text versions of the content
-
- require_once(INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php');
-
- $html = $this->act->object->content;
-
- $rendered = HTMLPurifier::purify($html);
- $content = html_entity_decode(strip_tags($rendered));
-
- $options = array('is_local' => Notice::REMOTE_OMB,
- 'uri' => $this->act->object->id,
- 'url' => $this->act->object->link,
- 'rendered' => $rendered);
-
- if (!empty($this->act->context->location)) {
- $options['lat'] = $location->lat;
- $options['lon'] = $location->lon;
- if ($location->location_id) {
- $options['location_ns'] = $location->location_ns;
- $options['location_id'] = $location->location_id;
- }
- }
-
- if (!empty($this->act->context->replyToID)) {
- $orig = Notice::staticGet('uri',
- $this->act->context->replyToID);
- if (!empty($orig)) {
- $options['reply_to'] = $orig->id;
- }
- }
-
- if (!empty($this->act->time)) {
- $options['created'] = common_sql_time($this->act->time);
- }
-
- return Notice::saveNew($oprofile->profile_id,
- $content,
- 'ostatus+salmon',
- $options);
+ return $oprofile->processPost($this->act, 'salmon');
}
}
diff --git a/plugins/OStatus/lib/salmonqueuehandler.php b/plugins/OStatus/lib/salmonqueuehandler.php
new file mode 100644
index 000000000..aa97018dc
--- /dev/null
+++ b/plugins/OStatus/lib/salmonqueuehandler.php
@@ -0,0 +1,44 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * Send a Salmon notification in the background.
+ * @package OStatusPlugin
+ * @author Brion Vibber <brion@status.net>
+ */
+class SalmonQueueHandler extends QueueHandler
+{
+ function transport()
+ {
+ return 'salmon';
+ }
+
+ function handle($data)
+ {
+ assert(is_array($data));
+ assert(is_string($data['salmonuri']));
+ assert(is_string($data['entry']));
+
+ $salmon = new Salmon();
+ $salmon->post($data['salmonuri'], $data['entry']);
+
+ // @fixme detect failure and attempt to resend
+ return true;
+ }
+}
diff --git a/plugins/OStatus/lib/webfinger.php b/plugins/OStatus/lib/webfinger.php
index 417d54904..8a5037629 100644
--- a/plugins/OStatus/lib/webfinger.php
+++ b/plugins/OStatus/lib/webfinger.php
@@ -32,11 +32,16 @@ define('WEBFINGER_SERVICE_REL_VALUE', 'lrdd');
/**
* Implement the webfinger protocol.
*/
+
class Webfinger
{
+ const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
+ const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
+
/**
* Perform a webfinger lookup given an account.
- */
+ */
+
public function lookup($id)
{
$id = $this->normalize($id);
@@ -46,7 +51,7 @@ class Webfinger
if (!$links) {
return false;
}
-
+
$services = array();
foreach ($links as $link) {
if ($link['template']) {
@@ -64,7 +69,7 @@ class Webfinger
function normalize($id)
{
if (substr($id, 0, 7) == 'acct://') {
- return substr($id, 7);
+ return substr($id, 7);
} else if (substr($id, 0, 5) == 'acct:') {
return substr($id, 5);
}
@@ -86,7 +91,7 @@ class Webfinger
if ($result->host != $domain) {
return false;
}
-
+
$links = array();
foreach ($result->links as $link) {
if ($link['rel'] == WEBFINGER_SERVICE_REL_VALUE) {
@@ -103,6 +108,10 @@ class Webfinger
$content = $this->fetchURL($url);
+ if (!$content) {
+ return false;
+ }
+
return XRD::parse($content);
}
@@ -140,4 +149,3 @@ class Webfinger
}
}
-