summaryrefslogtreecommitdiff
path: root/plugins/OStatus/lib
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-02-21 11:12:56 -0800
committerBrion Vibber <brion@pobox.com>2010-02-21 11:12:56 -0800
commit85528ccb1f5840a8c73f6c939f12d98bb3680cde (patch)
treebe0f09c02a2bf003c23d6be98919783347b9786d /plugins/OStatus/lib
parent86f2f530ef60fdb601720885d493cf5b2a446e6b (diff)
parent3e7a2a4014dd93637f5a666e238dde13e397523c (diff)
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
Diffstat (limited to 'plugins/OStatus/lib')
-rw-r--r--plugins/OStatus/lib/activity.php278
-rw-r--r--plugins/OStatus/lib/feeddiscovery.php50
-rw-r--r--plugins/OStatus/lib/feedmunger.php350
-rw-r--r--plugins/OStatus/lib/hubverifyqueuehandler.php3
-rw-r--r--plugins/OStatus/lib/salmon.php3
-rw-r--r--plugins/OStatus/lib/salmonaction.php209
6 files changed, 526 insertions, 367 deletions
diff --git a/plugins/OStatus/lib/activity.php b/plugins/OStatus/lib/activity.php
index f137946ab..f25a843c3 100644
--- a/plugins/OStatus/lib/activity.php
+++ b/plugins/OStatus/lib/activity.php
@@ -55,6 +55,9 @@ class ActivityUtils
const TYPE = 'type';
const HREF = 'href';
+ const CONTENT = 'content';
+ const SRC = 'src';
+
/**
* Get the permalink for an Activity object
*
@@ -139,6 +142,64 @@ class ActivityUtils
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."));
+ }
+ }
+ }
}
/**
@@ -182,7 +243,6 @@ class ActivityObject
const TITLE = 'title';
const SUMMARY = 'summary';
- const CONTENT = 'content';
const ID = 'id';
const SOURCE = 'source';
@@ -198,6 +258,7 @@ class ActivityObject
public $content;
public $link;
public $source;
+ public $avatar;
/**
* Constructor
@@ -209,8 +270,12 @@ class ActivityObject
* @param DOMElement $element DOM thing to turn into an Activity thing
*/
- function __construct($element)
+ function __construct($element = null)
{
+ if (empty($element)) {
+ return;
+ }
+
$this->element = $element;
if ($element->tagName == 'author') {
@@ -239,10 +304,11 @@ class ActivityObject
$this->id = $this->_childContent($element, self::ID);
$this->title = $this->_childContent($element, self::TITLE);
$this->summary = $this->_childContent($element, self::SUMMARY);
- $this->content = $this->_childContent($element, self::CONTENT);
$this->source = $this->_getSource($element);
+ $this->content = ActivityUtils::getContent($element);
+
$this->link = ActivityUtils::getPermalink($element);
// XXX: grab PoCo stuff
@@ -279,6 +345,65 @@ class ActivityObject
}
}
}
+
+ 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();
+ }
}
/**
@@ -303,6 +428,93 @@ class ActivityVerb
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;
+ }
}
/**
@@ -351,6 +563,11 @@ class Activity
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
*
@@ -358,8 +575,12 @@ class Activity
* @param DOMElement $feed Atom feed, for context
*/
- function __construct($entry, $feed = null)
+ function __construct($entry = null, $feed = null)
{
+ if (is_null($entry)) {
+ return;
+ }
+
$this->entry = $entry;
$this->feed = $feed;
@@ -420,7 +641,9 @@ class Activity
$contextEl = $this->_child($entry, self::CONTEXT);
if (!empty($contextEl)) {
- $this->context = new ActivityObject($contextEl);
+ $this->context = new ActivityContext($contextEl);
+ } else {
+ $this->context = new ActivityContext($entry);
}
$targetEl = $this->_child($entry, self::TARGET);
@@ -428,6 +651,10 @@ class Activity
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);
}
/**
@@ -441,6 +668,47 @@ class Activity
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);
diff --git a/plugins/OStatus/lib/feeddiscovery.php b/plugins/OStatus/lib/feeddiscovery.php
index 39985fc90..7afb71bdc 100644
--- a/plugins/OStatus/lib/feeddiscovery.php
+++ b/plugins/OStatus/lib/feeddiscovery.php
@@ -48,6 +48,14 @@ class FeedSubNoFeedException extends FeedSubException
{
}
+class FeedSubBadXmlException extends FeedSubException
+{
+}
+
+class FeedSubNoHubException extends FeedSubException
+{
+}
+
/**
* Given a web page or feed URL, discover the final location of the feed
* and return its current contents.
@@ -57,21 +65,25 @@ class FeedSubNoFeedException extends FeedSubException
* if ($feed->discoverFromURL($url)) {
* print $feed->uri;
* print $feed->type;
- * processFeed($feed->body);
+ * processFeed($feed->feed); // DOMDocument
* }
*/
class FeedDiscovery
{
public $uri;
public $type;
- public $body;
+ public $feed;
+ /** Post-initialize query helper... */
+ public function getLink($rel, $type=null)
+ {
+ // @fixme check for non-Atom links in RSS2 feeds as well
+ return self::getAtomLink($rel, $type);
+ }
- public function feedMunger()
+ public function getAtomLink($rel, $type=null)
{
- require_once 'XML/Feed/Parser.php';
- $feed = new XML_Feed_Parser($this->body, false, false, true); // @fixme
- return new FeedMunger($feed, $this->uri);
+ return ActivityUtils::getLink($this->feed->documentElement, $rel, $type);
}
/**
@@ -90,6 +102,7 @@ class FeedDiscovery
$client = new HTTPClient();
$response = $client->get($url);
} catch (HTTP_Request2_Exception $e) {
+ common_log(LOG_ERR, __METHOD__ . " Failure for $url - " . $e->getMessage());
throw new FeedSubBadURLException($e);
}
@@ -107,7 +120,12 @@ class FeedDiscovery
return $this->initFromResponse($response);
}
-
+
+ function discoverFromFeedURL($url)
+ {
+ return $this->discoverFromURL($url, false);
+ }
+
function initFromResponse($response)
{
if (!$response->isOk()) {
@@ -122,16 +140,26 @@ class FeedDiscovery
$type = $response->getHeader('Content-Type');
if (preg_match('!^(text/xml|application/xml|application/(rss|atom)\+xml)!i', $type)) {
- $this->uri = $sourceurl;
- $this->type = $type;
- $this->body = $body;
- return true;
+ return $this->init($sourceurl, $type, $body);
} else {
common_log(LOG_WARNING, "Unrecognized feed type $type for $sourceurl");
throw new FeedSubUnrecognizedTypeException($type);
}
}
+ function init($sourceurl, $type, $body)
+ {
+ $feed = new DOMDocument();
+ if ($feed->loadXML($body)) {
+ $this->uri = $sourceurl;
+ $this->type = $type;
+ $this->feed = $feed;
+ return $this->uri;
+ } else {
+ throw new FeedSubBadXmlException($url);
+ }
+ }
+
/**
* @param string $url source URL, used to resolve relative links
* @param string $body HTML body text
diff --git a/plugins/OStatus/lib/feedmunger.php b/plugins/OStatus/lib/feedmunger.php
deleted file mode 100644
index e8c46de90..000000000
--- a/plugins/OStatus/lib/feedmunger.php
+++ /dev/null
@@ -1,350 +0,0 @@
-<?php
-/*
- * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2009, 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/>.
- */
-
-/**
- * @package FeedSubPlugin
- * @maintainer Brion Vibber <brion@status.net>
- */
-
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
-
-class FeedSubPreviewNotice extends Notice
-{
- protected $fetched = true;
-
- function __construct($profile)
- {
- $this->profile = $profile;
- $this->profile_id = 0;
- }
-
- function getProfile()
- {
- return $this->profile;
- }
-
- function find()
- {
- return true;
- }
-
- function fetch()
- {
- $got = $this->fetched;
- $this->fetched = false;
- return $got;
- }
-}
-
-class FeedSubPreviewProfile extends Profile
-{
- function getAvatar($width, $height=null)
- {
- return new FeedSubPreviewAvatar($width, $height, $this->avatar);
- }
-}
-
-class FeedSubPreviewAvatar extends Avatar
-{
- function __construct($width, $height, $remote)
- {
- $this->remoteImage = $remote;
- }
-
- function displayUrl() {
- return $this->remoteImage;
- }
-}
-
-class FeedMunger
-{
- /**
- * @param XML_Feed_Parser $feed
- */
- function __construct($feed, $url=null)
- {
- $this->feed = $feed;
- $this->url = $url;
- }
-
- function ostatusProfile()
- {
- $profile = new Ostatus_profile();
- $profile->feeduri = $this->url;
- $profile->homeuri = $this->feed->link;
- $profile->huburi = $this->getHubLink();
- $salmon = $this->getSalmonLink();
- if ($salmon) {
- $profile->salmonuri = $salmon;
- }
- return $profile;
- }
-
- function getAtomLink($item, $attribs=array())
- {
- // XML_Feed_Parser gets confused by multiple <link> elements.
- $dom = $item->model;
-
- // Note that RSS feeds would embed an <atom:link> so this should work for both.
- /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
- // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
- $links = $dom->getElementsByTagNameNS('http://www.w3.org/2005/Atom', 'link');
- for ($i = 0; $i < $links->length; $i++) {
- $node = $links->item($i);
- if ($node->hasAttributes()) {
- $href = $node->attributes->getNamedItem('href');
- if ($href) {
- $matches = 0;
- foreach ($attribs as $name => $val) {
- $attrib = $node->attributes->getNamedItem($name);
- if ($attrib && $attrib->value == $val) {
- $matches++;
- }
- }
- if ($matches == count($attribs)) {
- return $href->value;
- }
- }
- }
- }
- return false;
- }
-
- function getRssLink($item)
- {
- // XML_Feed_Parser gets confused by multiple <link> elements.
- $dom = $item->model;
-
- // Note that RSS feeds would embed an <atom:link> so this should work for both.
- /// http://code.google.com/p/pubsubhubbub/wiki/RssFeeds
- // <link rel='hub' href='http://pubsubhubbub.appspot.com/'/>
- $links = $dom->getElementsByTagName('link');
- for ($i = 0; $i < $links->length; $i++) {
- $node = $links->item($i);
- if (!$node->hasAttributes()) {
- return $node->textContent;
- }
- }
- return false;
- }
-
- function getAltLink($item)
- {
- // Check for an atom link...
- $link = $this->getAtomLink($item, array('rel' => 'alternate', 'type' => 'text/html'));
- if (!$link) {
- $link = $this->getRssLink($item);
- }
- return $link;
- }
-
- function getHubLink()
- {
- return $this->getAtomLink($this->feed, array('rel' => 'hub'));
- }
-
- function getSalmonLink()
- {
- return $this->getAtomLink($this->feed, array('rel' => 'salmon'));
- }
-
- function getSelfLink()
- {
- return $this->getAtomLink($this->feed, array('rel' => 'self'));
- }
-
- /**
- * Get an appropriate avatar image source URL, if available.
- * @return mixed string or false
- */
- function getAvatar()
- {
- $logo = $this->feed->logo;
- if ($logo) {
- return $logo;
- }
- $icon = $this->feed->icon;
- if ($icon) {
- return $icon;
- }
- return common_path('plugins/OStatus/images/48px-Feed-icon.svg.png');
- }
-
- function profile($preview=false)
- {
- if ($preview) {
- $profile = new FeedSubPreviewProfile();
- } else {
- $profile = new Profile();
- }
-
- // @todo validate/normalize nick?
- $profile->nickname = $this->feed->title;
- $profile->fullname = $this->feed->title;
- $profile->homepage = $this->getAltLink($this->feed);
- $profile->bio = $this->feed->description;
- $profile->profileurl = $this->getAltLink($this->feed);
-
- if ($preview) {
- $profile->avatar = $this->getAvatar();
- }
-
- // @todo tags from categories
- // @todo lat/lon/location?
-
- return $profile;
- }
-
- function notice($index=1, $preview=false)
- {
- $entry = $this->feed->getEntryByOffset($index);
- if (!$entry) {
- return null;
- }
-
- if ($preview) {
- $notice = new FeedSubPreviewNotice($this->profile(true));
- $notice->id = -1;
- } else {
- $notice = new Notice();
- $notice->profile_id = $this->profileIdForEntry($index);
- }
-
- $link = $this->getAltLink($entry);
- if (empty($link)) {
- if (preg_match('!^https?://!', $entry->id)) {
- $link = $entry->id;
- common_log(LOG_DEBUG, "No link on entry, using URL from id: $link");
- }
- }
- $notice->uri = $link;
- $notice->url = $link;
- $notice->content = $this->noticeFromEntry($entry);
- $notice->rendered = common_render_content($notice->content, $notice); // @fixme this is failing on group posts
- $notice->created = common_sql_date($entry->updated); // @fixme
- $notice->is_local = Notice::GATEWAY;
- $notice->source = 'feed';
-
- $location = $this->getLocation($entry);
- if ($location) {
- if ($location->location_id) {
- $notice->location_ns = $location->location_ns;
- $notice->location_id = $location->location_id;
- }
- $notice->lat = $location->lat;
- $notice->lon = $location->lon;
- }
-
- return $notice;
- }
-
- function profileIdForEntry($index=1)
- {
- // hack hack hack
- // should get profile for this entry's author...
- $feeduri = $this->getSelfLink();
- $remote = Ostatus_profile::staticGet('feeduri', $feeduri);
- if ($remote) {
- return $remote->profile_id;
- } else {
- throw new Exception("Can't find feed profile for $feeduri");
- }
- }
-
- /**
- * 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($entry)
- {
- $dom = $entry->model;
- $points = $dom->getElementsByTagNameNS('http://www.georss.org/georss', 'point');
-
- for ($i = 0; $i < $points->length; $i++) {
- $point = $points->item(0)->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 false;
- }
-
- /**
- * @param XML_Feed_Type $entry
- * @return string notice text, within post size limit
- */
- function noticeFromEntry($entry)
- {
- $max = Notice::maxContent();
- $ellipsis = "\xe2\x80\xa6"; // U+2026 HORIZONTAL ELLIPSIS
- $title = $entry->title;
- $link = $entry->link;
-
- // @todo We can get <category> entries like this:
- // $cats = $entry->getCategory('category', array(0, true));
- // but it feels like an awful hack. If it's accessible cleanly,
- // try adding #hashtags from the categories/tags on a post.
-
- $title = $entry->title;
- $link = $this->getAltLink($entry);
- if ($link) {
- // Blog post or such...
- // @todo Should we force a language here?
- $format = _m('New post: "%1$s" %2$s');
- $out = sprintf($format, $title, $link);
-
- // Trim link if needed...
- if (mb_strlen($out) > $max) {
- $link = common_shorten_url($link);
- $out = sprintf($format, $title, $link);
- }
-
- // Trim title if needed...
- if (mb_strlen($out) > $max) {
- $used = mb_strlen($out) - mb_strlen($title);
- $available = $max - $used - mb_strlen($ellipsis);
- $title = mb_substr($title, 0, $available) . $ellipsis;
- $out = sprintf($format, $title, $link);
- }
- } else {
- // No link? Consider a bare status update.
- if (mb_strlen($title) > $max) {
- $available = $max - mb_strlen($ellipsis);
- $out = mb_substr($title, 0, $available) . $ellipsis;
- } else {
- $out = $title;
- }
- }
-
- return $out;
- }
-}
diff --git a/plugins/OStatus/lib/hubverifyqueuehandler.php b/plugins/OStatus/lib/hubverifyqueuehandler.php
index 125d13a77..7ce9e1431 100644
--- a/plugins/OStatus/lib/hubverifyqueuehandler.php
+++ b/plugins/OStatus/lib/hubverifyqueuehandler.php
@@ -33,13 +33,14 @@ class HubVerifyQueueHandler extends QueueHandler
{
$sub = $data['sub'];
$mode = $data['mode'];
+ $token = $data['token'];
assert($sub instanceof HubSub);
assert($mode === 'subscribe' || $mode === 'unsubscribe');
common_log(LOG_INFO, __METHOD__ . ": $mode $sub->callback $sub->topic");
try {
- $sub->verify($mode);
+ $sub->verify($mode, $token);
} catch (Exception $e) {
common_log(LOG_ERR, "Failed PuSH $mode verify to $sub->callback for $sub->topic: " .
$e->getMessage());
diff --git a/plugins/OStatus/lib/salmon.php b/plugins/OStatus/lib/salmon.php
index 8c77222a6..df17a7006 100644
--- a/plugins/OStatus/lib/salmon.php
+++ b/plugins/OStatus/lib/salmon.php
@@ -41,9 +41,12 @@ class Salmon
$client->setBody($xml);
$response = $client->post($endpoint_uri, $headers);
} catch (HTTP_Request2_Exception $e) {
+ common_log(LOG_ERR, "Salmon post to $endpoint_uri failed: " . $e->getMessage());
return false;
}
if ($response->getStatus() != 200) {
+ common_log(LOG_ERR, "Salmon at $endpoint_uri returned status " .
+ $response->getStatus() . ': ' . $response->getBody());
return false;
}
diff --git a/plugins/OStatus/lib/salmonaction.php b/plugins/OStatus/lib/salmonaction.php
new file mode 100644
index 000000000..4e5ed7fe6
--- /dev/null
+++ b/plugins/OStatus/lib/salmonaction.php
@@ -0,0 +1,209 @@
+<?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/>.
+ */
+
+/**
+ * @package OStatusPlugin
+ * @author James Walker <james@status.net>
+ */
+
+if (!defined('STATUSNET')) {
+ exit(1);
+}
+
+class SalmonAction extends Action
+{
+ var $xml = null;
+ var $activity = null;
+
+ function prepare($args)
+ {
+ StatusNet::setApi(true); // Send smaller error pages
+
+ parent::prepare($args);
+
+ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+ $this->clientError(_('This method requires a POST.'));
+ }
+
+ if ($_SERVER['CONTENT_TYPE'] != 'application/atom+xml') {
+ $this->clientError(_('Salmon requires application/atom+xml'));
+ }
+
+ $xml = file_get_contents('php://input');
+
+ $dom = DOMDocument::loadXML($xml);
+
+ if ($dom->documentElement->namespaceURI != Activity::ATOM ||
+ $dom->documentElement->localName != 'entry') {
+ common_log(LOG_DEBUG, "Got invalid Salmon post: $xml");
+ $this->clientError(_m('Salmon post must be an Atom entry.'));
+ }
+ // XXX: check the signature
+
+ $this->act = new Activity($dom->documentElement);
+ return true;
+ }
+
+ /**
+ * Check the posted activity type and break out to appropriate processing.
+ */
+
+ function handle($args)
+ {
+ StatusNet::setApi(true); // Send smaller error pages
+
+ // TODO : Insert new $xml -> notice code
+
+ if (Event::handle('StartHandleSalmon', array($this->activity))) {
+ switch ($this->act->verb)
+ {
+ case ActivityVerb::POST:
+ $this->handlePost();
+ break;
+ case ActivityVerb::SHARE:
+ $this->handleShare();
+ break;
+ case ActivityVerb::FAVORITE:
+ $this->handleFavorite();
+ break;
+ case ActivityVerb::UNFAVORITE:
+ $this->handleUnfavorite();
+ break;
+ case ActivityVerb::FOLLOW:
+ case ActivityVerb::FRIEND:
+ $this->handleFollow();
+ break;
+ case ActivityVerb::UNFOLLOW:
+ $this->handleUnfollow();
+ break;
+ case ActivityVerb::JOIN:
+ $this->handleJoin();
+ break;
+ default:
+ throw new ClientException(_("Unimplemented."));
+ }
+ Event::handle('EndHandleSalmon', array($this->activity));
+ }
+ }
+
+ function handlePost()
+ {
+ throw new ClientException(_("Unimplemented!"));
+ }
+
+ function handleFollow()
+ {
+ throw new ClientException(_("Unimplemented!"));
+ }
+
+ function handleUnfollow()
+ {
+ throw new ClientException(_("Unimplemented!"));
+ }
+
+ function handleFavorite()
+ {
+ throw new ClientException(_("Unimplemented!"));
+ }
+
+ /**
+ * 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!"));
+ }
+
+ /**
+ * Hmmmm
+ */
+ function handleShare()
+ {
+ throw new ClientException(_("Unimplemented!"));
+ }
+
+ /**
+ * Hmmmm
+ */
+ function handleJoin()
+ {
+ throw new ClientException(_("Unimplemented!"));
+ }
+
+ /**
+ * @return Ostatus_profile
+ */
+ function ensureProfile()
+ {
+ $actor = $this->act->actor;
+ if (empty($actor->id)) {
+ common_log(LOG_ERR, "broken actor: " . var_export($actor, true));
+ throw new Exception("Received a salmon slap from unidentified actor.");
+ }
+
+ return Ostatus_profile::ensureActorProfile($this->act);
+ }
+
+ 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);
+ }
+}