summaryrefslogtreecommitdiff
path: root/plugins/OStatus
diff options
context:
space:
mode:
authorBrion Vibber <brion@status.net>2010-08-10 15:01:29 -0700
committerBrion Vibber <brion@status.net>2010-08-10 15:01:29 -0700
commit819d33210d298de74b64dc7ead79e9d9b223b12e (patch)
tree902d42087e633b96e12bef699f6c80e7342c9312 /plugins/OStatus
parent8f071b2818e8321ea910df612016175f65093402 (diff)
parent08fc6053ec55e911b842fd05dafc5e0c99c4e992 (diff)
Merge branch '0.9.x' into tinymce
Diffstat (limited to 'plugins/OStatus')
-rw-r--r--plugins/OStatus/OStatusPlugin.php61
-rw-r--r--plugins/OStatus/README39
-rw-r--r--plugins/OStatus/actions/groupsalmon.php3
-rw-r--r--plugins/OStatus/actions/hostmeta.php3
-rw-r--r--plugins/OStatus/actions/ostatusgroup.php2
-rw-r--r--plugins/OStatus/actions/ostatussub.php14
-rw-r--r--plugins/OStatus/actions/usersalmon.php11
-rw-r--r--plugins/OStatus/actions/userxrd.php10
-rw-r--r--plugins/OStatus/classes/FeedSub.php62
-rw-r--r--plugins/OStatus/classes/HubSub.php51
-rw-r--r--plugins/OStatus/classes/Magicsig.php44
-rw-r--r--plugins/OStatus/classes/Ostatus_profile.php281
-rw-r--r--plugins/OStatus/extlib/Math/BigInteger.php8
-rw-r--r--plugins/OStatus/lib/discovery.php2
-rw-r--r--plugins/OStatus/lib/discoveryhints.php3
-rw-r--r--plugins/OStatus/lib/feeddiscovery.php12
-rw-r--r--plugins/OStatus/lib/hubprepqueuehandler.php87
-rw-r--r--plugins/OStatus/lib/linkheader.php2
-rw-r--r--plugins/OStatus/lib/magicenvelope.php57
-rw-r--r--plugins/OStatus/lib/ostatusqueuehandler.php31
-rw-r--r--plugins/OStatus/lib/salmon.php4
-rw-r--r--plugins/OStatus/lib/xrd.php103
-rw-r--r--plugins/OStatus/lib/xrdaction.php5
-rw-r--r--plugins/OStatus/locale/OStatus.pot (renamed from plugins/OStatus/locale/OStatus.po)319
-rw-r--r--plugins/OStatus/locale/fr/LC_MESSAGES/OStatus.po106
-rw-r--r--plugins/OStatus/scripts/fixup-shadow.php39
-rw-r--r--plugins/OStatus/scripts/resub-feed.php74
-rw-r--r--plugins/OStatus/scripts/update-profile.php147
-rw-r--r--plugins/OStatus/tests/FeedDiscoveryTest.php2
-rw-r--r--plugins/OStatus/tests/remote-tests.php555
30 files changed, 1659 insertions, 478 deletions
diff --git a/plugins/OStatus/OStatusPlugin.php b/plugins/OStatus/OStatusPlugin.php
index 58f373e45..6fef20d6f 100644
--- a/plugins/OStatus/OStatusPlugin.php
+++ b/plugins/OStatus/OStatusPlugin.php
@@ -28,6 +28,15 @@ set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/ext
class FeedSubException extends Exception
{
+ function __construct($msg=null)
+ {
+ $type = get_class($this);
+ if ($msg) {
+ parent::__construct("$type: $msg");
+ } else {
+ parent::__construct($type);
+ }
+ }
}
class OStatusPlugin extends Plugin
@@ -87,6 +96,8 @@ class OStatusPlugin extends Plugin
// Outgoing from our internal PuSH hub
$qm->connect('hubconf', 'HubConfQueueHandler');
+ $qm->connect('hubprep', 'HubPrepQueueHandler');
+
$qm->connect('hubout', 'HubOutQueueHandler');
// Outgoing Salmon replies (when we don't need a return value)
@@ -102,7 +113,10 @@ class OStatusPlugin extends Plugin
*/
function onStartEnqueueNotice($notice, &$transports)
{
- $transports[] = 'ostatus';
+ if ($notice->isLocal()) {
+ // put our transport first, in case there's any conflict (like OMB)
+ array_unshift($transports, 'ostatus');
+ }
return true;
}
@@ -153,6 +167,9 @@ class OStatusPlugin extends Plugin
// Also, we'll add in the salmon link
$salmon = common_local_url($salmonAction, array('id' => $id));
+ $feed->addLink($salmon, array('rel' => Salmon::REL_SALMON));
+
+ // XXX: these are deprecated
$feed->addLink($salmon, array('rel' => Salmon::NS_REPLIES));
$feed->addLink($salmon, array('rel' => Salmon::NS_MENTIONS));
}
@@ -256,7 +273,7 @@ class OStatusPlugin extends Plugin
$matches = array();
// Webfinger matches: @user@example.com
- if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
+ if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+@(?:\w+\-?\w+\.)*\w+(?:\w+\-\w+)*\.\w+)!',
$text,
$wmatches,
PREG_OFFSET_CAPTURE)) {
@@ -451,6 +468,7 @@ class OStatusPlugin extends Plugin
return false;
}
}
+ return true;
}
/**
@@ -471,6 +489,24 @@ class OStatusPlugin extends Plugin
}
/**
+ * Tell the FeedSub infrastructure whether we have any active OStatus
+ * usage for the feed; if not it'll be able to garbage-collect the
+ * feed subscription.
+ *
+ * @param FeedSub $feedsub
+ * @param integer $count in/out
+ * @return mixed hook return code
+ */
+ function onFeedSubSubscriberCount($feedsub, &$count)
+ {
+ $oprofile = Ostatus_profile::staticGet('feeduri', $feedsub->uri);
+ if ($oprofile) {
+ $count += $oprofile->subscriberCount();
+ }
+ return true;
+ }
+
+ /**
* When about to subscribe to a remote user, start a server-to-server
* PuSH subscription if needed. If we can't establish that, abort.
*
@@ -948,22 +984,15 @@ class OStatusPlugin extends Plugin
return false;
}
- /**
- * Utility function to check if the given URL is a canonical user profile
- * page, and if so return the ID number.
- *
- * @param string $url
- * @return mixed int or false
- */
- public static function localProfileFromUrl($url)
+ public function onStartProfileGetAtomFeed($profile, &$feed)
{
- $template = common_local_url('userbyid', array('id' => '31337'));
- $template = preg_quote($template, '/');
- $template = str_replace('31337', '(\d+)', $template);
- if (preg_match("/$template/", $url, $matches)) {
- return intval($matches[1]);
+ $oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
+
+ if (empty($oprofile)) {
+ return true;
}
+
+ $feed = $oprofile->feeduri;
return false;
}
-
}
diff --git a/plugins/OStatus/README b/plugins/OStatus/README
index 3a98b7b25..ea5dfc055 100644
--- a/plugins/OStatus/README
+++ b/plugins/OStatus/README
@@ -1,18 +1,42 @@
-Plugin to support importing updates from external RSS and Atom feeds into your timeline.
+Plugin to support importing and exporting notices through Atom and RSS feeds.
+The OStatus plugin concentrates on user-to-user cases for federating StatusNet
+and similar social networking / microblogging / blogging sites, but includes
+low-level feed subscription systems which are used by some other plugins.
+
+Uses PubSubHubbub for push feed updates; currently non-PuSH feeds cannot be
+subscribed unless an external PuSH hub proxy is used.
-Uses PubSubHubbub for push feed updates; currently non-PuSH feeds cannot be subscribed.
Configuration options available:
$config['ostatus']['hub']
(default internal hub)
- Set to URL of an external PuSH hub to use it instead of our internal hub.
+ Set to URL of an external PuSH hub to use it instead of our internal hub
+ for sending outgoing updates in user and group feeds.
$config['ostatus']['hub_retries']
(default 0)
Number of times to retry a PuSH send to consumers if using internal hub
+Settings controlling incoming feed subscription:
+
+$config['feedsub']['fallback_hub']
+ To subscribe to feeds that don't have a hub, an external PuSH proxy hub
+ such as Superfeedr may be used. Any feed without a hub of its own will
+ be subscribed through the specified hub URL instead. If the external hub
+ has usage charges, be aware that there is no restriction placed to how
+ many feeds may be subscribed!
+
+ $config['feedsub']['fallback_hub'] = 'https://superfeedr.com/hubbub';
+
+$config['feedsub']['hub_user']
+$config['feedsub']['hub_password']
+ If using the fallback hub mode, these settings may be used to provide
+ HTTP authentication credentials for contacting the hub. Default hubs
+ specified from feeds are assumed to not require
+
+
For testing, shouldn't be used in production:
$config['ostatus']['skip_signatures']
@@ -23,12 +47,11 @@ $config['feedsub']['nohub']
(default require hub)
Allow low-level feed subscription setup for feeds without hubs.
Not actually usable at this stage, OStatus will check for hubs too
- and we have no polling backend.
+ and we have no polling backend. (The fallback hub option can be used
+ with a 3rd-party service to provide such polling.)
Todo:
-* fully functional l10n
-* redo non-OStatus feed support
-** rssCloud support?
-** possibly a polling daemon to support non-PuSH feeds?
+* better support for feeds that aren't natively oriented at social networking
* make use of tags/categories from feeds
+* better repeat handling
diff --git a/plugins/OStatus/actions/groupsalmon.php b/plugins/OStatus/actions/groupsalmon.php
index 29377b5fa..d60725a71 100644
--- a/plugins/OStatus/actions/groupsalmon.php
+++ b/plugins/OStatus/actions/groupsalmon.php
@@ -60,7 +60,8 @@ class GroupsalmonAction extends SalmonAction
function handlePost()
{
- switch ($this->act->object->type) {
+ // @fixme process all objects?
+ switch ($this->act->objects[0]->type) {
case ActivityObject::ARTICLE:
case ActivityObject::BLOGENTRY:
case ActivityObject::NOTE:
diff --git a/plugins/OStatus/actions/hostmeta.php b/plugins/OStatus/actions/hostmeta.php
index 6d35ada6c..8ca07f916 100644
--- a/plugins/OStatus/actions/hostmeta.php
+++ b/plugins/OStatus/actions/hostmeta.php
@@ -36,13 +36,12 @@ class HostMetaAction extends Action
$url.= '?uri={uri}';
$xrd = new XRD();
-
- $xrd = new XRD();
$xrd->host = $domain;
$xrd->links[] = array('rel' => Discovery::LRDD_REL,
'template' => $url,
'title' => array('Resource Descriptor'));
+ header('Content-type: application/xrd+xml');
print $xrd->toXML();
}
}
diff --git a/plugins/OStatus/actions/ostatusgroup.php b/plugins/OStatus/actions/ostatusgroup.php
index f325ba053..1b368de63 100644
--- a/plugins/OStatus/actions/ostatusgroup.php
+++ b/plugins/OStatus/actions/ostatusgroup.php
@@ -104,7 +104,7 @@ class OStatusGroupAction extends OStatusSubAction
}
$this->showEntity($group,
- $group->getProfileUrl(),
+ $group->homeUrl(),
$group->homepage_logo,
$group->description);
return $ok;
diff --git a/plugins/OStatus/actions/ostatussub.php b/plugins/OStatus/actions/ostatussub.php
index 994af6e95..28714514f 100644
--- a/plugins/OStatus/actions/ostatussub.php
+++ b/plugins/OStatus/actions/ostatussub.php
@@ -446,4 +446,18 @@ class OStatusSubAction extends Action
{
return common_local_url('ostatussub');
}
+
+ /**
+ * Disable the send-notice form at the top of the page.
+ * This is really just a hack for the broken CSS in the Cloudy theme,
+ * I think; copying from other non-notice-navigation pages that do this
+ * as well. There will be plenty of others also broken.
+ *
+ * @fixme fix the cloudy theme
+ * @fixme do this in a more general way
+ */
+ function showNoticeForm() {
+ // nop
+ }
+
}
diff --git a/plugins/OStatus/actions/usersalmon.php b/plugins/OStatus/actions/usersalmon.php
index 15e8c1869..6c360c49f 100644
--- a/plugins/OStatus/actions/usersalmon.php
+++ b/plugins/OStatus/actions/usersalmon.php
@@ -55,9 +55,10 @@ class UsersalmonAction extends SalmonAction
*/
function handlePost()
{
- common_log(LOG_INFO, "Received post of '{$this->act->object->id}' from '{$this->act->actor->id}'");
+ common_log(LOG_INFO, "Received post of '{$this->act->objects[0]->id}' from '{$this->act->actor->id}'");
- switch ($this->act->object->type) {
+ // @fixme: process all activity objects?
+ switch ($this->act->objects[0]->type) {
case ActivityObject::ARTICLE:
case ActivityObject::BLOGENTRY:
case ActivityObject::NOTE:
@@ -91,7 +92,7 @@ class UsersalmonAction extends SalmonAction
throw new ClientException("Not to anyone in reply to anything!");
}
- $existing = Notice::staticGet('uri', $this->act->object->id);
+ $existing = Notice::staticGet('uri', $this->act->objects[0]->id);
if (!empty($existing)) {
common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
@@ -142,7 +143,7 @@ class UsersalmonAction extends SalmonAction
function handleFavorite()
{
- $notice = $this->getNotice($this->act->object);
+ $notice = $this->getNotice($this->act->objects[0]);
$profile = $this->ensureProfile()->localProfile();
$old = Fave::pkeyGet(array('user_id' => $profile->id,
@@ -163,7 +164,7 @@ class UsersalmonAction extends SalmonAction
*/
function handleUnfavorite()
{
- $notice = $this->getNotice($this->act->object);
+ $notice = $this->getNotice($this->act->objects[0]);
$profile = $this->ensureProfile()->localProfile();
$fave = Fave::pkeyGet(array('user_id' => $profile->id,
diff --git a/plugins/OStatus/actions/userxrd.php b/plugins/OStatus/actions/userxrd.php
index eb80a5ad4..6a6886eb8 100644
--- a/plugins/OStatus/actions/userxrd.php
+++ b/plugins/OStatus/actions/userxrd.php
@@ -35,9 +35,13 @@ class UserxrdAction extends XrdAction
$this->uri = Discovery::normalize($this->uri);
if (Discovery::isWebfinger($this->uri)) {
- list($nick, $domain) = explode('@', substr(urldecode($this->uri), 5));
- $nick = common_canonical_nickname($nick);
- $this->user = User::staticGet('nickname', $nick);
+ $parts = explode('@', substr(urldecode($this->uri), 5));
+ if (count($parts) == 2) {
+ list($nick, $domain) = $parts;
+ // @fixme confirm the domain too
+ $nick = common_canonical_nickname($nick);
+ $this->user = User::staticGet('nickname', $nick);
+ }
} else {
$this->user = User::staticGet('uri', $this->uri);
}
diff --git a/plugins/OStatus/classes/FeedSub.php b/plugins/OStatus/classes/FeedSub.php
index b10509dae..dd1968db1 100644
--- a/plugins/OStatus/classes/FeedSub.php
+++ b/plugins/OStatus/classes/FeedSub.php
@@ -207,8 +207,8 @@ class FeedSub extends Memcached_DataObject
$discover = new FeedDiscovery();
$discover->discoverFromFeedURL($feeduri);
- $huburi = $discover->getAtomLink('hub');
- if (!$huburi) {
+ $huburi = $discover->getHubLink();
+ if (!$huburi && !common_config('feedsub', 'fallback_hub')) {
throw new FeedSubNoHubException();
}
@@ -241,8 +241,12 @@ class FeedSub extends Memcached_DataObject
common_log(LOG_WARNING, "Attempting to (re)start PuSH subscription to $this->uri in unexpected state $this->sub_state");
}
if (empty($this->huburi)) {
- if (common_config('feedsub', 'nohub')) {
+ if (common_config('feedsub', 'fallback_hub')) {
+ // No native hub on this feed?
+ // Use our fallback hub, which handles polling on our behalf.
+ } else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs.
+ // We'll never actually get updates in this mode.
return true;
} else {
throw new ServerException("Attempting to start PuSH subscription for feed with no hub");
@@ -255,6 +259,9 @@ class FeedSub extends Memcached_DataObject
/**
* Send a PuSH unsubscription request to the hub for this feed.
* The hub will later send us a confirmation POST to /main/push/callback.
+ * Warning: this will cancel the subscription even if someone else in
+ * the system is using it. Most callers will want garbageCollect() instead,
+ * which confirms there's no uses left.
*
* @return bool true on success, false on failure
* @throws ServerException if feed state is not valid
@@ -264,8 +271,12 @@ class FeedSub extends Memcached_DataObject
common_log(LOG_WARNING, "Attempting to (re)end PuSH subscription to $this->uri in unexpected state $this->sub_state");
}
if (empty($this->huburi)) {
- if (common_config('feedsub', 'nohub')) {
+ if (common_config('feedsub', 'fallback_hub')) {
+ // No native hub on this feed?
+ // Use our fallback hub, which handles polling on our behalf.
+ } else if (common_config('feedsub', 'nohub')) {
// Fake it! We're just testing remote feeds w/o hubs.
+ // We'll never actually get updates in this mode.
return true;
} else {
throw new ServerException("Attempting to end PuSH subscription for feed with no hub");
@@ -275,6 +286,33 @@ class FeedSub extends Memcached_DataObject
return $this->doSubscribe('unsubscribe');
}
+ /**
+ * Check if there are any active local uses of this feed, and if not then
+ * make sure it's inactive, unsubscribing if necessary.
+ *
+ * @return boolean true if the subscription is now inactive, false if still active.
+ */
+ public function garbageCollect()
+ {
+ if ($this->sub_state == '' || $this->sub_state == 'inactive') {
+ // No active PuSH subscription, we can just leave it be.
+ return true;
+ } else {
+ // PuSH subscription is either active or in an indeterminate state.
+ // Check if we're out of subscribers, and if so send an unsubscribe.
+ $count = 0;
+ Event::handle('FeedSubSubscriberCount', array($this, &$count));
+
+ if ($count) {
+ common_log(LOG_INFO, __METHOD__ . ': ok, ' . $count . ' user(s) left for ' . $this->uri);
+ return false;
+ } else {
+ common_log(LOG_INFO, __METHOD__ . ': unsubscribing, no users left for ' . $this->uri);
+ return $this->unsubscribe();
+ }
+ }
+ }
+
protected function doSubscribe($mode)
{
$orig = clone($this);
@@ -296,7 +334,21 @@ class FeedSub extends Memcached_DataObject
'hub.secret' => $this->secret,
'hub.topic' => $this->uri);
$client = new HTTPClient();
- $response = $client->post($this->huburi, $headers, $post);
+ if ($this->huburi) {
+ $hub = $this->huburi;
+ } else {
+ if (common_config('feedsub', 'fallback_hub')) {
+ $hub = common_config('feedsub', 'fallback_hub');
+ if (common_config('feedsub', 'hub_user')) {
+ $u = common_config('feedsub', 'hub_user');
+ $p = common_config('feedsub', 'hub_pass');
+ $client->setAuth($u, $p);
+ }
+ } else {
+ throw new FeedSubException('WTF?');
+ }
+ }
+ $response = $client->post($hub, $headers, $post);
$status = $response->getStatus();
if ($status == 202) {
common_log(LOG_INFO, __METHOD__ . ': sub req ok, awaiting verification callback');
diff --git a/plugins/OStatus/classes/HubSub.php b/plugins/OStatus/classes/HubSub.php
index cdace3c1f..7db528a4e 100644
--- a/plugins/OStatus/classes/HubSub.php
+++ b/plugins/OStatus/classes/HubSub.php
@@ -260,6 +260,37 @@ class HubSub extends Memcached_DataObject
$retries = intval(common_config('ostatus', 'hub_retries'));
}
+ if (common_config('ostatus', 'local_push_bypass')) {
+ // If target is a local site, bypass the web server and drop the
+ // item directly into the target's input queue.
+ $url = parse_url($this->callback);
+ $wildcard = common_config('ostatus', 'local_wildcard');
+ $site = Status_network::getFromHostname($url['host'], $wildcard);
+
+ if ($site) {
+ if ($this->secret) {
+ $hmac = 'sha1=' . hash_hmac('sha1', $atom, $this->secret);
+ } else {
+ $hmac = '';
+ }
+
+ // Hack: at the moment we stick the subscription ID in the callback
+ // URL so we don't have to look inside the Atom to route the subscription.
+ // For now this means we need to extract that from the target URL
+ // so we can include it in the data.
+ $parts = explode('/', $url['path']);
+ $subId = intval(array_pop($parts));
+
+ $data = array('feedsub_id' => $subId,
+ 'post' => $atom,
+ 'hmac' => $hmac);
+ common_log(LOG_DEBUG, "Cross-site PuSH bypass enqueueing straight to $site->nickname feed $subId");
+ $qm = QueueManager::get();
+ $qm->enqueue($data, 'pushin', $site->nickname);
+ return;
+ }
+ }
+
// We dare not clone() as when the clone is discarded it'll
// destroy the result data for the parent query.
// @fixme use clone() again when it's safe to copy an
@@ -274,6 +305,26 @@ class HubSub extends Memcached_DataObject
}
/**
+ * Queue up a large batch of pushes to multiple subscribers
+ * for this same topic update.
+ *
+ * If queues are disabled, this will run immediately.
+ *
+ * @param string $atom well-formed Atom feed
+ * @param array $pushCallbacks list of callback URLs
+ */
+ function bulkDistribute($atom, $pushCallbacks)
+ {
+ $data = array('atom' => $atom,
+ 'topic' => $this->topic,
+ 'pushCallbacks' => $pushCallbacks);
+ common_log(LOG_INFO, "Queuing PuSH batch: $this->topic to " .
+ count($pushCallbacks) . " sites");
+ $qm = QueueManager::get();
+ $qm->enqueue($data, 'hubprep');
+ }
+
+ /**
* Send a 'fat ping' to the subscriber's callback endpoint
* containing the given Atom feed chunk.
*
diff --git a/plugins/OStatus/classes/Magicsig.php b/plugins/OStatus/classes/Magicsig.php
index 5705ecc11..f8c56a05f 100644
--- a/plugins/OStatus/classes/Magicsig.php
+++ b/plugins/OStatus/classes/Magicsig.php
@@ -52,7 +52,15 @@ class Magicsig extends Memcached_DataObject
{
$obj = parent::staticGet(__CLASS__, $k, $v);
if (!empty($obj)) {
- return Magicsig::fromString($obj->keypair);
+ $obj = Magicsig::fromString($obj->keypair);
+
+ // Double check keys: Crypt_RSA did not
+ // consistently generate good keypairs.
+ // We've also moved to 1024 bit keys.
+ if (strlen($obj->publicKey->modulus->toBits()) != 1024) {
+ $obj->delete();
+ return false;
+ }
}
return $obj;
@@ -121,11 +129,11 @@ class Magicsig extends Memcached_DataObject
public function toString($full_pair = true)
{
- $mod = base64_url_encode($this->publicKey->modulus->toBytes());
- $exp = base64_url_encode($this->publicKey->exponent->toBytes());
+ $mod = Magicsig::base64_url_encode($this->publicKey->modulus->toBytes());
+ $exp = Magicsig::base64_url_encode($this->publicKey->exponent->toBytes());
$private_exp = '';
if ($full_pair && $this->privateKey->exponent->toBytes()) {
- $private_exp = '.' . base64_url_encode($this->privateKey->exponent->toBytes());
+ $private_exp = '.' . Magicsig::base64_url_encode($this->privateKey->exponent->toBytes());
}
return 'RSA.' . $mod . '.' . $exp . $private_exp;
@@ -166,9 +174,9 @@ class Magicsig extends Memcached_DataObject
$rsa = new Crypt_RSA();
$rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
$rsa->setHash('sha256');
- $rsa->modulus = new Math_BigInteger(base64_url_decode($mod), 256);
+ $rsa->modulus = new Math_BigInteger(Magicsig::base64_url_decode($mod), 256);
$rsa->k = strlen($rsa->modulus->toBytes());
- $rsa->exponent = new Math_BigInteger(base64_url_decode($exp), 256);
+ $rsa->exponent = new Math_BigInteger(Magicsig::base64_url_decode($exp), 256);
if ($type == 'private') {
$this->privateKey = $rsa;
@@ -195,23 +203,25 @@ class Magicsig extends Memcached_DataObject
public function sign($bytes)
{
$sig = $this->privateKey->sign($bytes);
- return base64_url_encode($sig);
+ return Magicsig::base64_url_encode($sig);
}
public function verify($signed_bytes, $signature)
{
- $signature = base64_url_decode($signature);
+ $signature = Magicsig::base64_url_decode($signature);
return $this->publicKey->verify($signed_bytes, $signature);
}
-
-}
-function base64_url_encode($input)
-{
- return strtr(base64_encode($input), '+/', '-_');
-}
-function base64_url_decode($input)
-{
- return base64_decode(strtr($input, '-_', '+/'));
+ public static function base64_url_encode($input)
+ {
+ return strtr(base64_encode($input), '+/', '-_');
+ }
+
+ public static function base64_url_decode($input)
+ {
+ return base64_decode(strtr($input, '-_', '+/'));
+ }
}
+
+
diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php
index e48ed6ee8..8f8eb773f 100644
--- a/plugins/OStatus/classes/Ostatus_profile.php
+++ b/plugins/OStatus/classes/Ostatus_profile.php
@@ -215,22 +215,13 @@ class Ostatus_profile extends Memcached_DataObject
}
/**
- * Send a PuSH unsubscription request to the hub for this feed.
- * The hub will later send us a confirmation POST to /main/push/callback.
+ * Check if this remote profile has any active local subscriptions, and
+ * if not drop the PuSH subscription feed.
*
* @return bool true on success, false on failure
- * @throws ServerException if feed state is not valid
*/
public function unsubscribe() {
- $feedsub = FeedSub::staticGet('uri', $this->feeduri);
- if (!$feedsub || $feedsub->sub_state == '' || $feedsub->sub_state == 'inactive') {
- // No active PuSH subscription, we can just leave it be.
- return true;
- } else {
- // PuSH subscription is either active or in an indeterminate state.
- // Send an unsubscribe.
- return $feedsub->unsubscribe();
- }
+ $this->garbageCollect();
}
/**
@@ -241,19 +232,35 @@ class Ostatus_profile extends Memcached_DataObject
*/
public function garbageCollect()
{
+ $feedsub = FeedSub::staticGet('uri', $this->feeduri);
+ return $feedsub->garbageCollect();
+ }
+
+ /**
+ * Check if this remote profile has any active local subscriptions, so the
+ * PuSH subscription layer can decide if it can drop the feed.
+ *
+ * This gets called via the FeedSubSubscriberCount event when running
+ * FeedSub::garbageCollect().
+ *
+ * @return int
+ */
+ public function subscriberCount()
+ {
if ($this->isGroup()) {
$members = $this->localGroup()->getMembers(0, 1);
$count = $members->N;
} else {
$count = $this->localProfile()->subscriberCount();
}
- if ($count == 0) {
- common_log(LOG_INFO, "Unsubscribing from now-unused remote feed $this->feeduri");
- $this->unsubscribe();
- return true;
- } else {
- return false;
- }
+ common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count");
+
+ // Other plugins may be piggybacking on OStatus without having
+ // an active group or user-to-user subscription we know about.
+ Event::handle('Ostatus_profileSubscriberCount', array($this, &$count));
+ common_log(LOG_INFO, __METHOD__ . " SUB COUNT AFTER: $count");
+
+ return $count;
}
/**
@@ -442,6 +449,18 @@ class Ostatus_profile extends Memcached_DataObject
{
$activity = new Activity($entry, $feed);
+ // @todo process all activity objects
+ switch ($activity->objects[0]->type) {
+ case ActivityObject::ARTICLE:
+ case ActivityObject::BLOGENTRY:
+ case ActivityObject::NOTE:
+ case ActivityObject::STATUS:
+ case ActivityObject::COMMENT:
+ break;
+ default:
+ throw new ClientException("Can't handle that kind of post.");
+ }
+
if ($activity->verb == ActivityVerb::POST) {
$this->processPost($activity, $source);
} else {
@@ -474,8 +493,14 @@ class Ostatus_profile extends Memcached_DataObject
// OK here! assume the default
} else if ($actor->id == $this->uri || $actor->link == $this->uri) {
$this->updateFromActivityObject($actor);
- } else {
+ } else if ($actor->id) {
+ // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner.
+ // This isn't what we expect from mainline OStatus person feeds!
+ // Group feeds go down another path, with different validation.
throw new Exception("Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}");
+ } else {
+ // Plain <author> without ActivityStreams actor info.
+ // We'll just ignore this info for now and save the update under the feed's identity.
}
$oprofile = $this;
@@ -483,7 +508,7 @@ class Ostatus_profile extends Memcached_DataObject
// It's not always an ActivityObject::NOTE, but... let's just say it is.
- $note = $activity->object;
+ $note = $activity->objects[0];
// The id URI will be used as a unique identifier for for the notice,
// protecting against duplicate saves. It isn't required to be a URL;
@@ -538,14 +563,22 @@ class Ostatus_profile extends Memcached_DataObject
}
$shortSummary = common_shorten_links($summary);
if (Notice::contentTooLong($shortSummary)) {
- $url = common_shorten_url(common_local_url('attachment',
- array('attachment' => $attachment->id)));
+ $url = common_shorten_url($sourceUrl);
$shortSummary = substr($shortSummary,
0,
Notice::maxContent() - (mb_strlen($url) + 2));
- $shortSummary .= '… ' . $url;
- $content = $shortSummary;
- $rendered = common_render_text($content);
+ $content = $shortSummary . ' ' . $url;
+
+ // We mark up the attachment link specially for the HTML output
+ // so we can fold-out the full version inline.
+ $attachUrl = common_local_url('attachment',
+ array('attachment' => $attachment->id));
+ $rendered = common_render_text($shortSummary) .
+ '<a href="' . htmlspecialchars($attachUrl) .'"'.
+ ' class="attachment more"' .
+ ' title="'. htmlspecialchars(_m('Show more')) . '">' .
+ '&#8230;' .
+ '</a>';
}
}
@@ -648,7 +681,7 @@ class Ostatus_profile extends Memcached_DataObject
common_log(LOG_DEBUG, "Original reply recipients: " . implode(', ', $attention_uris));
$groups = array();
$replies = array();
- foreach ($attention_uris as $recipient) {
+ foreach (array_unique($attention_uris) as $recipient) {
// Is the recipient a local user?
$user = User::staticGet('uri', $recipient);
if ($user) {
@@ -700,9 +733,14 @@ class Ostatus_profile extends Memcached_DataObject
}
/**
+ * Look up and if necessary create an Ostatus_profile for the remote entity
+ * with the given profile page URL. This should never return null -- you
+ * will either get an object or an exception will be thrown.
+ *
* @param string $profile_url
* @return Ostatus_profile
- * @throws FeedSubException
+ * @throws Exception on various error conditions
+ * @throws OStatusShadowException if this reference would obscure a local user/group
*/
public static function ensureProfileURL($profile_url, $hints=array())
@@ -723,7 +761,7 @@ class Ostatus_profile extends Memcached_DataObject
$response = $client->get($profile_url);
if (!$response->isOk()) {
- return null;
+ throw new Exception("Could not reach profile page: " . $profile_url);
}
// Check if we have a non-canonical URL
@@ -777,11 +815,20 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($feedurl)) {
$hints['feedurl'] = $feedurl;
-
return self::ensureFeedURL($feedurl, $hints);
}
+
+ throw new Exception("Could not find a feed URL for profile page " . $finalUrl);
}
+ /**
+ * Look up the Ostatus_profile, if present, for a remote entity with the
+ * given profile page URL. Will return null for both unknown and invalid
+ * remote profiles.
+ *
+ * @return mixed Ostatus_profile or null
+ * @throws OStatusShadowException for local profiles
+ */
static function getFromProfileURL($profile_url)
{
$profile = Profile::staticGet('profileurl', $profile_url);
@@ -803,7 +850,7 @@ class Ostatus_profile extends Memcached_DataObject
$user = User::staticGet('id', $profile->id);
if (!empty($user)) {
- throw new Exception("'$profile_url' is the profile for local user '{$user->nickname}'.");
+ throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
}
// Continue discovery; it's a remote profile
@@ -813,6 +860,14 @@ class Ostatus_profile extends Memcached_DataObject
return null;
}
+ /**
+ * Look up and if necessary create an Ostatus_profile for remote entity
+ * with the given update feed. This should never return null -- you will
+ * either get an object or an exception will be thrown.
+ *
+ * @return Ostatus_profile
+ * @throws Exception
+ */
public static function ensureFeedURL($feed_url, $hints=array())
{
$discover = new FeedDiscovery();
@@ -820,12 +875,12 @@ class Ostatus_profile extends Memcached_DataObject
$feeduri = $discover->discoverFromFeedURL($feed_url);
$hints['feedurl'] = $feeduri;
- $huburi = $discover->getAtomLink('hub');
+ $huburi = $discover->getHubLink();
$hints['hub'] = $huburi;
$salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
$hints['salmon'] = $salmonuri;
- if (!$huburi) {
+ if (!$huburi && !common_config('feedsub', 'fallback_hub')) {
// We can only deal with folks with a PuSH hub
throw new FeedSubNoHubException();
}
@@ -841,6 +896,18 @@ class Ostatus_profile extends Memcached_DataObject
}
}
+ /**
+ * Look up and, if necessary, create an Ostatus_profile for the remote
+ * profile with the given Atom feed - actually loaded from the feed.
+ * This should never return null -- you will either get an object or
+ * an exception will be thrown.
+ *
+ * @param DOMElement $feedEl root element of a loaded Atom feed
+ * @param array $hints additional discovery information passed from higher levels
+ * @fixme should this be marked public?
+ * @return Ostatus_profile
+ * @throws Exception
+ */
public static function ensureAtomFeed($feedEl, $hints)
{
// Try to get a profile from the feed activity:subject
@@ -891,6 +958,18 @@ class Ostatus_profile extends Memcached_DataObject
throw new FeedSubException("Can't find enough profile information to make a feed.");
}
+ /**
+ * Look up and, if necessary, create an Ostatus_profile for the remote
+ * profile with the given RSS feed - actually loaded from the feed.
+ * This should never return null -- you will either get an object or
+ * an exception will be thrown.
+ *
+ * @param DOMElement $feedEl root element of a loaded RSS feed
+ * @param array $hints additional discovery information passed from higher levels
+ * @fixme should this be marked public?
+ * @return Ostatus_profile
+ * @throws Exception
+ */
public static function ensureRssChannel($feedEl, $hints)
{
// Special-case for Posterous. They have some nice metadata in their
@@ -935,7 +1014,7 @@ class Ostatus_profile extends Memcached_DataObject
return;
}
if (!common_valid_http_url($url)) {
- throw new ServerException(_m("Invalid avatar URL %s"), $url);
+ throw new ServerException(sprintf(_m("Invalid avatar URL %s"), $url));
}
if ($this->isGroup()) {
@@ -1054,11 +1133,14 @@ class Ostatus_profile extends Memcached_DataObject
/**
* Fetch, or build if necessary, an Ostatus_profile for the actor
* in a given Activity Streams activity.
+ * This should never return null -- you will either get an object or
+ * an exception will be thrown.
*
* @param Activity $activity
* @param string $feeduri if we already know the canonical feed URI!
* @param string $salmonuri if we already know the salmon return channel URI
* @return Ostatus_profile
+ * @throws Exception
*/
public static function ensureActorProfile($activity, $hints=array())
@@ -1066,6 +1148,18 @@ class Ostatus_profile extends Memcached_DataObject
return self::ensureActivityObjectProfile($activity->actor, $hints);
}
+ /**
+ * Fetch, or build if necessary, an Ostatus_profile for the profile
+ * in a given Activity Streams object (can be subject, actor, or object).
+ * This should never return null -- you will either get an object or
+ * an exception will be thrown.
+ *
+ * @param ActivityObject $object
+ * @param array $hints additional discovery information passed from higher levels
+ * @return Ostatus_profile
+ * @throws Exception
+ */
+
public static function ensureActivityObjectProfile($object, $hints=array())
{
$profile = self::getActivityObjectProfile($object);
@@ -1080,35 +1174,45 @@ class Ostatus_profile extends Memcached_DataObject
/**
* @param Activity $activity
* @return mixed matching Ostatus_profile or false if none known
+ * @throws ServerException if feed info invalid
*/
public static function getActorProfile($activity)
{
return self::getActivityObjectProfile($activity->actor);
}
+ /**
+ * @param ActivityObject $activity
+ * @return mixed matching Ostatus_profile or false if none known
+ * @throws ServerException if feed info invalid
+ */
protected static function getActivityObjectProfile($object)
{
$uri = self::getActivityObjectProfileURI($object);
return Ostatus_profile::staticGet('uri', $uri);
}
- protected static function getActorProfileURI($activity)
- {
- return self::getActivityObjectProfileURI($activity->actor);
- }
-
/**
- * @param Activity $activity
+ * Get the identifier URI for the remote entity described
+ * by this ActivityObject. This URI is *not* guaranteed to be
+ * a resolvable HTTP/HTTPS URL.
+ *
+ * @param ActivityObject $object
* @return string
- * @throws ServerException
+ * @throws ServerException if feed info invalid
*/
protected static function getActivityObjectProfileURI($object)
{
- $opts = array('allowed_schemes' => array('http', 'https'));
- if ($object->id && Validate::uri($object->id, $opts)) {
- return $object->id;
+ if ($object->id) {
+ if (ActivityUtils::validateUri($object->id)) {
+ return $object->id;
+ }
}
- if ($object->link && Validate::uri($object->link, $opts)) {
+
+ // If the id is missing or invalid (we've seen feeds mistakenly listing
+ // things like local usernames in that field) then we'll use the profile
+ // page link, if valid.
+ if ($object->link && common_valid_http_url($object->link)) {
return $object->link;
}
throw new ServerException("No author ID URI found");
@@ -1121,6 +1225,8 @@ class Ostatus_profile extends Memcached_DataObject
/**
* Create local ostatus_profile and profile/user_group entries for
* the provided remote user or group.
+ * This should never return null -- you will either get an object or
+ * an exception will be thrown.
*
* @param ActivityObject $object
* @param array $hints
@@ -1137,7 +1243,8 @@ class Ostatus_profile extends Memcached_DataObject
throw new Exception("No profile URI");
}
- if (OStatusPlugin::localProfileFromUrl($homeuri)) {
+ $user = User::staticGet('uri', $homeuri);
+ if ($user) {
throw new Exception("Local user can't be referenced as remote.");
}
@@ -1169,10 +1276,10 @@ class Ostatus_profile extends Memcached_DataObject
$discover = new FeedDiscovery();
$discover->discoverFromFeedURL($hints['feedurl']);
}
- $huburi = $discover->getAtomLink('hub');
+ $huburi = $discover->getHubLink();
}
- if (!$huburi) {
+ if (!$huburi && !common_config('feedsub', 'fallback_hub')) {
// We can only deal with folks with a PuSH hub
throw new FeedSubNoHubException();
}
@@ -1209,15 +1316,23 @@ class Ostatus_profile extends Memcached_DataObject
$ok = $oprofile->insert();
- if ($ok) {
- $avatar = self::getActivityObjectAvatar($object, $hints);
- if ($avatar) {
+ if (!$ok) {
+ throw new ServerException("Can't save OStatus profile");
+ }
+
+ $avatar = self::getActivityObjectAvatar($object, $hints);
+
+ if ($avatar) {
+ try {
$oprofile->updateAvatar($avatar);
+ } catch (Exception $ex) {
+ // Profile is saved, but Avatar is messed up. We're
+ // just going to continue.
+ common_log(LOG_WARNING, "Exception saving OStatus profile avatar: ". $ex->getMessage());
}
- return $oprofile;
- } else {
- throw new ServerException("Can't save OStatus profile");
}
+
+ return $oprofile;
}
/**
@@ -1236,7 +1351,11 @@ class Ostatus_profile extends Memcached_DataObject
}
$avatar = self::getActivityObjectAvatar($object, $hints);
if ($avatar) {
- $this->updateAvatar($avatar);
+ try {
+ $this->updateAvatar($avatar);
+ } catch (Exception $ex) {
+ common_log(LOG_WARNING, "Exception saving OStatus profile avatar: " . $ex->getMessage());
+ }
}
}
@@ -1437,9 +1556,15 @@ class Ostatus_profile extends Memcached_DataObject
}
/**
+ * Look up, and if necessary create, an Ostatus_profile for the remote
+ * entity with the given webfinger address.
+ * This should never return null -- you will either get an object or
+ * an exception will be thrown.
+ *
* @param string $addr webfinger address
* @return Ostatus_profile
* @throws Exception on error conditions
+ * @throws OStatusShadowException if this reference would obscure a local user/group
*/
public static function ensureWebfinger($addr)
{
@@ -1518,9 +1643,18 @@ class Ostatus_profile extends Memcached_DataObject
$oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
return $oprofile;
+ } catch (OStatusShadowException $e) {
+ // We've ended up with a remote reference to a local user or group.
+ // @fixme ideally we should be able to say who it was so we can
+ // go back and refer to it the regular way
+ throw $e;
} catch (Exception $e) {
common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
// keep looking
+ //
+ // @fixme this means an error discovering from profile page
+ // may give us a corrupt entry using the webfinger URI, which
+ // will obscure the correct page-keyed profile later on.
}
}
@@ -1577,10 +1711,22 @@ class Ostatus_profile extends Memcached_DataObject
throw new Exception("Couldn't find a valid profile for '$addr'");
}
+ /**
+ * Store the full-length scrubbed HTML of a remote notice to an attachment
+ * file on our server. We'll link to this at the end of the cropped version.
+ *
+ * @param string $title plaintext for HTML page's title
+ * @param string $rendered HTML fragment for HTML page's body
+ * @return File
+ */
function saveHTMLFile($title, $rendered)
{
- $final = sprintf("<!DOCTYPE html>\n<html><head><title>%s</title></head>".
- '<body><div>%s</div></body></html>',
+ $final = sprintf("<!DOCTYPE html>\n" .
+ '<html><head>' .
+ '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' .
+ '<title>%s</title>' .
+ '</head>' .
+ '<body>%s</body></html>',
htmlspecialchars($title),
$rendered);
@@ -1610,3 +1756,24 @@ class Ostatus_profile extends Memcached_DataObject
return $file;
}
}
+
+/**
+ * Exception indicating we've got a remote reference to a local user,
+ * not a remote user!
+ *
+ * If we can ue a local profile after all, it's available as $e->profile.
+ */
+class OStatusShadowException extends Exception
+{
+ public $profile;
+
+ /**
+ * @param Profile $profile
+ * @param string $message
+ */
+ function __construct($profile, $message) {
+ $this->profile = $profile;
+ parent::__construct($message);
+ }
+}
+
diff --git a/plugins/OStatus/extlib/Math/BigInteger.php b/plugins/OStatus/extlib/Math/BigInteger.php
index 9733351d4..4373805f9 100644
--- a/plugins/OStatus/extlib/Math/BigInteger.php
+++ b/plugins/OStatus/extlib/Math/BigInteger.php
@@ -67,7 +67,7 @@
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVI Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt
- * @version $Id: BigInteger.php,v 1.31 2010/03/01 17:28:19 terrafrost Exp $
+ * @version $Id: BigInteger.php,v 1.33 2010/03/22 22:32:03 terrafrost Exp $
* @link http://pear.php.net/package/Math_BigInteger
*/
@@ -294,7 +294,7 @@ class Math_BigInteger {
$this->value = array();
}
- if ($x === 0) {
+ if (empty($x)) {
return;
}
@@ -718,7 +718,7 @@ class Math_BigInteger {
*
* Will be called, automatically, when serialize() is called on a Math_BigInteger object.
*
- * @see __wakeup
+ * @see __wakeup()
* @access public
*/
function __sleep()
@@ -740,7 +740,7 @@ class Math_BigInteger {
*
* Will be called, automatically, when unserialize() is called on a Math_BigInteger object.
*
- * @see __sleep
+ * @see __sleep()
* @access public
*/
function __wakeup()
diff --git a/plugins/OStatus/lib/discovery.php b/plugins/OStatus/lib/discovery.php
index 44fad62fb..7187c1f3e 100644
--- a/plugins/OStatus/lib/discovery.php
+++ b/plugins/OStatus/lib/discovery.php
@@ -195,7 +195,7 @@ class Discovery_LRDD_Link_Header implements Discovery_LRDD
// return false;
}
- return Discovery_LRDD_Link_Header::parseHeader($link_header);
+ return array(Discovery_LRDD_Link_Header::parseHeader($link_header));
}
protected static function parseHeader($header)
diff --git a/plugins/OStatus/lib/discoveryhints.php b/plugins/OStatus/lib/discoveryhints.php
index 80cfbbf15..34c9be277 100644
--- a/plugins/OStatus/lib/discoveryhints.php
+++ b/plugins/OStatus/lib/discoveryhints.php
@@ -30,6 +30,7 @@ class DiscoveryHints {
case Discovery::PROFILEPAGE:
$hints['profileurl'] = $link['href'];
break;
+ case Salmon::NS_MENTIONS:
case Salmon::NS_REPLIES:
$hints['salmon'] = $link['href'];
break;
@@ -83,7 +84,7 @@ class DiscoveryHints {
$hints['fullname'] = implode(' ', $hcard['n']);
}
- if (array_key_exists('photo', $hcard)) {
+ if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
$hints['avatar'] = $hcard['photo'][0];
}
diff --git a/plugins/OStatus/lib/feeddiscovery.php b/plugins/OStatus/lib/feeddiscovery.php
index 4809f9d35..a55399d7c 100644
--- a/plugins/OStatus/lib/feeddiscovery.php
+++ b/plugins/OStatus/lib/feeddiscovery.php
@@ -88,6 +88,16 @@ class FeedDiscovery
}
/**
+ * Get the referenced PuSH hub link from an Atom feed.
+ *
+ * @return mixed string or false
+ */
+ public function getHubLink()
+ {
+ return $this->getAtomLink('hub');
+ }
+
+ /**
* @param string $url
* @param bool $htmlOk pass false here if you don't want to follow web pages.
* @return string with validated URL
@@ -104,7 +114,7 @@ class FeedDiscovery
$response = $client->get($url);
} catch (HTTP_Request2_Exception $e) {
common_log(LOG_ERR, __METHOD__ . " Failure for $url - " . $e->getMessage());
- throw new FeedSubBadURLException($e);
+ throw new FeedSubBadURLException($e->getMessage());
}
if ($htmlOk) {
diff --git a/plugins/OStatus/lib/hubprepqueuehandler.php b/plugins/OStatus/lib/hubprepqueuehandler.php
new file mode 100644
index 000000000..0d585938f
--- /dev/null
+++ b/plugins/OStatus/lib/hubprepqueuehandler.php
@@ -0,0 +1,87 @@
+<?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/>.
+ */
+
+/**
+ * When we have a large batch of PuSH consumers, we break the data set
+ * into smaller chunks. Enqueue final destinations...
+ *
+ * @package Hub
+ * @author Brion Vibber <brion@status.net>
+ */
+class HubPrepQueueHandler extends QueueHandler
+{
+ // Enqueue this many low-level distributions before re-queueing the rest
+ // of the batch to be processed later. Helps to keep latency down for other
+ // things happening during a particularly long OStatus delivery session.
+ //
+ // [Could probably ditch this if we had working message delivery priorities
+ // for queueing, but this isn't supported in ActiveMQ 5.3.]
+ const ROLLING_BATCH = 20;
+
+ function transport()
+ {
+ return 'hubprep';
+ }
+
+ function handle($data)
+ {
+ $topic = $data['topic'];
+ $atom = $data['atom'];
+ $pushCallbacks = $data['pushCallbacks'];
+
+ assert(is_string($atom));
+ assert(is_string($topic));
+ assert(is_array($pushCallbacks));
+
+ // Set up distribution for the first n subscribing sites...
+ // If we encounter an uncatchable error, queue handling should
+ // automatically re-run the batch, which could lead to some dupe
+ // distributions.
+ //
+ // Worst case is if one of these hubprep entries dies too many
+ // times and gets dropped; the rest of the batch won't get processed.
+ try {
+ $n = 0;
+ while (count($pushCallbacks) && $n < self::ROLLING_BATCH) {
+ $n++;
+ $callback = array_shift($pushCallbacks);
+ $sub = HubSub::staticGet($topic, $callback);
+ if (!$sub) {
+ common_log(LOG_ERR, "Skipping PuSH delivery for deleted(?) consumer $callback on $topic");
+ continue;
+ }
+
+ $sub->distribute($atom);
+ }
+ } catch (Exception $e) {
+ common_log(LOG_ERR, "Exception during PuSH batch out: " .
+ $e->getMessage() .
+ " prepping $topic to $callback");
+ }
+
+ // And re-queue the rest of the batch!
+ if (count($pushCallbacks) > 0) {
+ $sub = new HubSub();
+ $sub->topic = $topic;
+ $sub->bulkDistribute($atom, $pushCallbacks);
+ }
+
+ return true;
+ }
+}
diff --git a/plugins/OStatus/lib/linkheader.php b/plugins/OStatus/lib/linkheader.php
index afcd66d26..cd78d31ce 100644
--- a/plugins/OStatus/lib/linkheader.php
+++ b/plugins/OStatus/lib/linkheader.php
@@ -11,7 +11,7 @@ class LinkHeader
preg_match('/^<[^>]+>/', $str, $uri_reference);
//if (empty($uri_reference)) return;
- $this->uri = trim($uri_reference[0], '<>');
+ $this->href = trim($uri_reference[0], '<>');
$this->rel = array();
$this->type = null;
diff --git a/plugins/OStatus/lib/magicenvelope.php b/plugins/OStatus/lib/magicenvelope.php
index 9266cab5c..967e5f6d1 100644
--- a/plugins/OStatus/lib/magicenvelope.php
+++ b/plugins/OStatus/lib/magicenvelope.php
@@ -59,12 +59,21 @@ class MagicEnvelope
}
if ($xrd->links) {
if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) {
- list($type, $keypair) = explode(',', $link['href']);
- if (empty($keypair)) {
+ $keypair = false;
+ $parts = explode(',', $link['href']);
+ if (count($parts) == 2) {
+ $keypair = $parts[1];
+ } else {
// Backwards compatibility check for separator bug in 0.9.0
- list($type, $keypair) = explode(';', $link['href']);
+ $parts = explode(';', $link['href']);
+ if (count($parts) == 2) {
+ $keypair = $parts[1];
+ }
+ }
+
+ if ($keypair) {
+ return $keypair;
}
- return $keypair;
}
}
throw new Exception('Unable to locate signer public key');
@@ -74,7 +83,7 @@ class MagicEnvelope
public function signMessage($text, $mimetype, $keypair)
{
$signature_alg = Magicsig::fromString($keypair);
- $armored_text = base64_url_encode($text);
+ $armored_text = Magicsig::base64_url_encode($text);
return array(
'data' => $armored_text,
@@ -88,31 +97,25 @@ class MagicEnvelope
}
public function toXML($env) {
- $dom = new DOMDocument();
-
- $envelope = $dom->createElementNS(MagicEnvelope::NS, 'me:env');
- $envelope->setAttribute('xmlns:me', MagicEnvelope::NS);
- $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
- $data->setAttribute('type', $env['data_type']);
- $envelope->appendChild($data);
- $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
- $envelope->appendChild($enc);
- $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
- $envelope->appendChild($alg);
- $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
- $envelope->appendChild($sig);
-
- $dom->appendChild($envelope);
+ $xs = new XMLStringer();
+ $xs->startXML();
+ $xs->elementStart('me:env', array('xmlns:me' => MagicEnvelope::NS));
+ $xs->element('me:data', array('type' => $env['data_type']), $env['data']);
+ $xs->element('me:encoding', null, $env['encoding']);
+ $xs->element('me:alg', null, $env['alg']);
+ $xs->element('me:sig', null, $env['sig']);
+ $xs->elementEnd('me:env');
-
- return $dom->saveXML();
+ $string = $xs->getString();
+ common_debug($string);
+ return $string;
}
public function unfold($env)
{
$dom = new DOMDocument();
- $dom->loadXML(base64_url_decode($env['data']));
+ $dom->loadXML(Magicsig::base64_url_decode($env['data']));
if ($dom->documentElement->tagName != 'entry') {
return false;
@@ -169,7 +172,7 @@ class MagicEnvelope
return false;
}
- $text = base64_url_decode($env['data']);
+ $text = Magicsig::base64_url_decode($env['data']);
$signer_uri = $this->getAuthor($text);
try {
@@ -207,13 +210,13 @@ class MagicEnvelope
}
$data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
-
+ $sig_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0);
return array(
- 'data' => trim($data_element->nodeValue),
+ 'data' => preg_replace('/\s/', '', $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,
+ 'sig' => preg_replace('/\s/', '', $sig_element->nodeValue),
);
}
diff --git a/plugins/OStatus/lib/ostatusqueuehandler.php b/plugins/OStatus/lib/ostatusqueuehandler.php
index d1e58f1d6..8905d2e21 100644
--- a/plugins/OStatus/lib/ostatusqueuehandler.php
+++ b/plugins/OStatus/lib/ostatusqueuehandler.php
@@ -25,6 +25,18 @@
*/
class OStatusQueueHandler extends QueueHandler
{
+ // If we have more than this many subscribing sites on a single feed,
+ // break up the PuSH distribution into smaller batches which will be
+ // rolled into the queue progressively. This reduces disruption to
+ // other, shorter activities being enqueued while we work.
+ const MAX_UNBATCHED = 50;
+
+ // Each batch (a 'hubprep' entry) will have this many items.
+ // Selected to provide a balance between queue packet size
+ // and number of batches that will end up getting processed.
+ // For 20,000 target sites, 1000 should work acceptably.
+ const BATCH_SIZE = 1000;
+
function transport()
{
return 'ostatus';
@@ -147,14 +159,31 @@ class OStatusQueueHandler extends QueueHandler
/**
* Queue up direct feed update pushes to subscribers on our internal hub.
+ * If there are a large number of subscriber sites, intermediate bulk
+ * distribution triggers may be queued.
+ *
* @param string $atom update feed, containing only new/changed items
* @param HubSub $sub open query of subscribers
*/
function pushFeedInternal($atom, $sub)
{
common_log(LOG_INFO, "Preparing $sub->N PuSH distribution(s) for $sub->topic");
+ $n = 0;
+ $batch = array();
while ($sub->fetch()) {
- $sub->distribute($atom);
+ $n++;
+ if ($n < self::MAX_UNBATCHED) {
+ $sub->distribute($atom);
+ } else {
+ $batch[] = $sub->callback;
+ if (count($batch) >= self::BATCH_SIZE) {
+ $sub->bulkDistribute($atom, $batch);
+ $batch = array();
+ }
+ }
+ }
+ if (count($batch) >= 0) {
+ $sub->bulkDistribute($atom, $batch);
}
}
diff --git a/plugins/OStatus/lib/salmon.php b/plugins/OStatus/lib/salmon.php
index 3d3341bc6..ef7719a40 100644
--- a/plugins/OStatus/lib/salmon.php
+++ b/plugins/OStatus/lib/salmon.php
@@ -28,9 +28,11 @@
*/
class Salmon
{
+ const REL_SALMON = 'salmon';
+ const REL_MENTIONED = 'mentioned';
+ // XXX: these are deprecated
const NS_REPLIES = "http://salmon-protocol.org/ns/salmon-replies";
-
const NS_MENTIONS = "http://salmon-protocol.org/ns/salmon-mention";
/**
diff --git a/plugins/OStatus/lib/xrd.php b/plugins/OStatus/lib/xrd.php
index aa13ef024..a10b9f427 100644
--- a/plugins/OStatus/lib/xrd.php
+++ b/plugins/OStatus/lib/xrd.php
@@ -53,7 +53,14 @@ class XRD
$xrd = new XRD();
$dom = new DOMDocument();
- if (!$dom->loadXML($xml)) {
+
+ // Don't spew XML warnings to output
+ $old = error_reporting();
+ error_reporting($old & ~E_WARNING);
+ $ok = $dom->loadXML($xml);
+ error_reporting($old);
+
+ if (!$ok) {
throw new Exception("Invalid XML");
}
$xrd_element = $dom->getElementsByTagName('XRD')->item(0);
@@ -99,44 +106,43 @@ class XRD
public function toXML()
{
- $dom = new DOMDocument('1.0', 'UTF-8');
- $dom->formatOutput = true;
-
- $xrd_dom = $dom->createElementNS(XRD::XRD_NS, 'XRD');
- $dom->appendChild($xrd_dom);
+ $xs = new XMLStringer();
+
+ $xs->startXML();
+ $xs->elementStart('XRD', array('xmlns' => XRD::XRD_NS));
if ($this->host) {
- $host_dom = $dom->createElement('hm:Host', $this->host);
- $xrd_dom->setAttributeNS(XRD::XML_NS, 'xmlns:hm', XRD::HOST_META_NS);
- $xrd_dom->appendChild($host_dom);
+ $xs->element('hm:Host', array('xmlns:hm' => XRD::HOST_META_NS), $this->host);
+ }
+
+ if ($this->expires) {
+ $xs->element('Expires', null, $this->expires);
+ }
+
+ if ($this->subject) {
+ $xs->element('Subject', null, $this->subject);
+ }
+
+ foreach ($this->alias as $alias) {
+ $xs->element('Alias', null, $alias);
+ }
+
+ foreach ($this->links as $link) {
+ $titles = array();
+ if (isset($link['title'])) {
+ $titles = $link['title'];
+ unset($link['title']);
+ }
+ $xs->elementStart('Link', $link);
+ foreach ($titles as $title) {
+ $xs->element('Title', null, $title);
+ }
+ $xs->elementEnd('Link');
}
- if ($this->expires) {
- $expires_dom = $dom->createElement('Expires', $this->expires);
- $xrd_dom->appendChild($expires_dom);
- }
-
- if ($this->subject) {
- $subject_dom = $dom->createElement('Subject', $this->subject);
- $xrd_dom->appendChild($subject_dom);
- }
-
- foreach ($this->alias as $alias) {
- $alias_dom = $dom->createElement('Alias', $alias);
- $xrd_dom->appendChild($alias_dom);
- }
-
- foreach ($this->types as $type) {
- $type_dom = $dom->createElement('Type', $type);
- $xrd_dom->appendChild($type_dom);
- }
-
- foreach ($this->links as $link) {
- $link_dom = $this->saveLink($dom, $link);
- $xrd_dom->appendChild($link_dom);
- }
-
- return $dom->saveXML();
+ $xs->elementEnd('XRD');
+
+ return $xs->getString();
}
function parseType($element)
@@ -162,32 +168,5 @@ class XRD
return $link;
}
-
- function saveLink($doc, $link)
- {
- $link_element = $doc->createElement('Link');
- if (!empty($link['rel'])) {
- $link_element->setAttribute('rel', $link['rel']);
- }
- if (!empty($link['type'])) {
- $link_element->setAttribute('type', $link['type']);
- }
- if (!empty($link['href'])) {
- $link_element->setAttribute('href', $link['href']);
- }
- if (!empty($link['template'])) {
- $link_element->setAttribute('template', $link['template']);
- }
-
- if (!empty($link['title']) && is_array($link['title'])) {
- foreach($link['title'] as $title) {
- $title = $doc->createElement('Title', $title);
- $link_element->appendChild($title);
- }
- }
-
-
- return $link_element;
- }
}
diff --git a/plugins/OStatus/lib/xrdaction.php b/plugins/OStatus/lib/xrdaction.php
index f1a56e0a8..d8cf648d6 100644
--- a/plugins/OStatus/lib/xrdaction.php
+++ b/plugins/OStatus/lib/xrdaction.php
@@ -76,6 +76,9 @@ class XrdAction extends Action
$salmon_url = common_local_url('usersalmon',
array('id' => $this->user->id));
+ $xrd->links[] = array('rel' => Salmon::REL_SALMON,
+ 'href' => $salmon_url);
+ // XXX : Deprecated - to be removed.
$xrd->links[] = array('rel' => Salmon::NS_REPLIES,
'href' => $salmon_url);
@@ -98,7 +101,7 @@ class XrdAction extends Action
$xrd->links[] = array('rel' => 'http://ostatus.org/schema/1.0/subscribe',
'template' => $url );
- header('Content-type: text/xml');
+ header('Content-type: application/xrd+xml');
print $xrd->toXML();
}
diff --git a/plugins/OStatus/locale/OStatus.po b/plugins/OStatus/locale/OStatus.pot
index 7e33a0eed..97d593ead 100644
--- a/plugins/OStatus/locale/OStatus.po
+++ b/plugins/OStatus/locale/OStatus.pot
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2010-03-01 14:58-0800\n"
+"POT-Creation-Date: 2010-04-29 23:39+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -16,297 +16,316 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
-#: actions/groupsalmon.php:51
-msgid "Can't accept remote posts for a remote group."
-msgstr ""
-
-#: actions/groupsalmon.php:123
-msgid "Can't read profile to set up group membership."
+#: OStatusPlugin.php:210 OStatusPlugin.php:913 actions/ostatusinit.php:99
+msgid "Subscribe"
msgstr ""
-#: actions/groupsalmon.php:126 actions/groupsalmon.php:169
-msgid "Groups can't join groups."
+#: OStatusPlugin.php:228 OStatusPlugin.php:635 actions/ostatussub.php:105
+#: actions/ostatusinit.php:96
+msgid "Join"
msgstr ""
-#: actions/groupsalmon.php:153
+#: OStatusPlugin.php:451
#, php-format
-msgid "Could not join remote user %1$s to group %2$s."
+msgid "Sent from %s via OStatus"
msgstr ""
-#: actions/groupsalmon.php:166
-msgid "Can't read profile to cancel group membership."
+#: OStatusPlugin.php:503
+msgid "Could not set up remote subscription."
msgstr ""
-#: actions/groupsalmon.php:182
-#, php-format
-msgid "Could not remove remote user %1$s from group %2$s."
+#: OStatusPlugin.php:619
+msgid "Could not set up remote group membership."
msgstr ""
-#: actions/ostatusinit.php:40
-msgid "You can use the local subscription!"
+#: OStatusPlugin.php:636
+#, php-format
+msgid "%s has joined group %s."
msgstr ""
-#: actions/ostatusinit.php:61
-msgid "There was a problem with your session token. Try again, please."
+#: OStatusPlugin.php:644
+msgid "Failed joining remote group."
msgstr ""
-#: actions/ostatusinit.php:79 actions/ostatussub.php:439
-msgid "Subscribe to user"
+#: OStatusPlugin.php:684
+msgid "Leave"
msgstr ""
-#: actions/ostatusinit.php:97
+#: OStatusPlugin.php:685
#, php-format
-msgid "Subscribe to %s"
+msgid "%s has left group %s."
msgstr ""
-#: actions/ostatusinit.php:102
-msgid "User nickname"
+#: OStatusPlugin.php:844
+msgid "Remote"
msgstr ""
-#: actions/ostatusinit.php:103
-msgid "Nickname of the user you want to follow"
+#: OStatusPlugin.php:883
+msgid "Profile update"
msgstr ""
-#: actions/ostatusinit.php:106
-msgid "Profile Account"
+#: OStatusPlugin.php:884
+#, php-format
+msgid "%s has updated their profile page."
msgstr ""
-#: actions/ostatusinit.php:107
-msgid "Your account id (i.e. user@identi.ca)"
+#: OStatusPlugin.php:928
+msgid ""
+"Follow people across social networks that implement <a href=\"http://ostatus."
+"org/\">OStatus</a>."
msgstr ""
-#: actions/ostatusinit.php:110 actions/ostatussub.php:115
-#: OStatusPlugin.php:205
-msgid "Subscribe"
+#: classes/Ostatus_profile.php:566
+msgid "Show more"
msgstr ""
-#: actions/ostatusinit.php:128
-msgid "Must provide a remote profile."
+#: classes/Ostatus_profile.php:1004
+#, php-format
+msgid "Invalid avatar URL %s"
msgstr ""
-#: actions/ostatusinit.php:138
-msgid "Couldn't look up OStatus account profile."
+#: classes/Ostatus_profile.php:1014
+#, php-format
+msgid "Tried to update avatar for unsaved remote profile %s"
msgstr ""
-#: actions/ostatusinit.php:153
-msgid "Couldn't confirm remote profile address."
+#: classes/Ostatus_profile.php:1022
+#, php-format
+msgid "Unable to fetch avatar from %s"
msgstr ""
-#: actions/ostatusinit.php:171
-msgid "OStatus Connect"
+#: lib/salmonaction.php:41
+msgid "This method requires a POST."
msgstr ""
-#: actions/ostatussub.php:68
-msgid "Address or profile URL"
+#: lib/salmonaction.php:45
+msgid "Salmon requires application/magic-envelope+xml"
msgstr ""
-#: actions/ostatussub.php:70
-msgid "Enter the profile URL of a PubSubHubbub-enabled feed"
+#: lib/salmonaction.php:55
+msgid "Salmon signature verification failed."
msgstr ""
-#: actions/ostatussub.php:74
-msgid "Continue"
+#: lib/salmonaction.php:67
+msgid "Salmon post must be an Atom entry."
msgstr ""
-#: actions/ostatussub.php:112 OStatusPlugin.php:503
-msgid "Join"
+#: lib/salmonaction.php:115
+msgid "Unrecognized activity type."
msgstr ""
-#: actions/ostatussub.php:113
-msgid "Join this group"
+#: lib/salmonaction.php:123
+msgid "This target doesn't understand posts."
msgstr ""
-#: actions/ostatussub.php:116
-msgid "Subscribe to this user"
+#: lib/salmonaction.php:128
+msgid "This target doesn't understand follows."
msgstr ""
-#: actions/ostatussub.php:137
-msgid "You are already subscribed to this user."
+#: lib/salmonaction.php:133
+msgid "This target doesn't understand unfollows."
msgstr ""
-#: actions/ostatussub.php:165
-msgid "You are already a member of this group."
+#: lib/salmonaction.php:138
+msgid "This target doesn't understand favorites."
msgstr ""
-#: actions/ostatussub.php:286
-msgid "Empty remote profile URL!"
+#: lib/salmonaction.php:143
+msgid "This target doesn't understand unfavorites."
msgstr ""
-#: actions/ostatussub.php:297
-msgid "Invalid address format."
+#: lib/salmonaction.php:148
+msgid "This target doesn't understand share events."
msgstr ""
-#: actions/ostatussub.php:302
-msgid "Invalid URL or could not reach server."
+#: lib/salmonaction.php:153
+msgid "This target doesn't understand joins."
msgstr ""
-#: actions/ostatussub.php:304
-msgid "Cannot read feed; server returned error."
+#: lib/salmonaction.php:158
+msgid "This target doesn't understand leave events."
msgstr ""
-#: actions/ostatussub.php:306
-msgid "Cannot read feed; server returned an empty page."
+#: tests/gettext-speedtest.php:57
+msgid "Feeds"
msgstr ""
-#: actions/ostatussub.php:308
-msgid "Bad HTML, could not find feed link."
+#: actions/ostatusgroup.php:75
+msgid "Join group"
msgstr ""
-#: actions/ostatussub.php:310
-msgid "Could not find a feed linked from this URL."
+#: actions/ostatusgroup.php:77
+msgid "OStatus group's address, like http://example.net/group/nickname"
msgstr ""
-#: actions/ostatussub.php:312
-msgid "Not a recognized feed type."
+#: actions/ostatusgroup.php:81 actions/ostatussub.php:71
+msgid "Continue"
msgstr ""
-#: actions/ostatussub.php:315
-#, php-format
-msgid "Bad feed URL: %s %s"
+#: actions/ostatusgroup.php:100
+msgid "You are already a member of this group."
msgstr ""
#. TRANS: OStatus remote group subscription dialog error.
-#: actions/ostatussub.php:336
+#: actions/ostatusgroup.php:135
msgid "Already a member!"
msgstr ""
#. TRANS: OStatus remote group subscription dialog error.
-#: actions/ostatussub.php:346
+#: actions/ostatusgroup.php:146
msgid "Remote group join failed!"
msgstr ""
#. TRANS: OStatus remote group subscription dialog error.
-#: actions/ostatussub.php:350
+#: actions/ostatusgroup.php:150
msgid "Remote group join aborted!"
msgstr ""
-#. TRANS: OStatus remote subscription dialog error.
-#: actions/ostatussub.php:356
-msgid "Already subscribed!"
+#. TRANS: Page title for OStatus remote group join form
+#: actions/ostatusgroup.php:163
+msgid "Confirm joining remote group"
msgstr ""
-#. TRANS: OStatus remote subscription dialog error.
-#: actions/ostatussub.php:361
-msgid "Remote subscription failed!"
+#: actions/ostatusgroup.php:174
+msgid ""
+"You can subscribe to groups from other supported sites. Paste the group's "
+"profile URI below:"
msgstr ""
-#. TRANS: Page title for OStatus remote subscription form
-#: actions/ostatussub.php:459
-msgid "Authorize subscription"
+#: actions/groupsalmon.php:51
+msgid "Can't accept remote posts for a remote group."
msgstr ""
-#: actions/ostatussub.php:470
-msgid ""
-"You can subscribe to users from other supported sites. Paste their address "
-"or profile URI below:"
+#: actions/groupsalmon.php:124
+msgid "Can't read profile to set up group membership."
msgstr ""
-#: classes/Ostatus_profile.php:789
-#, php-format
-msgid "Tried to update avatar for unsaved remote profile %s"
+#: actions/groupsalmon.php:127 actions/groupsalmon.php:170
+msgid "Groups can't join groups."
msgstr ""
-#: classes/Ostatus_profile.php:797
+#: actions/groupsalmon.php:154
#, php-format
-msgid "Unable to fetch avatar from %s"
+msgid "Could not join remote user %1$s to group %2$s."
msgstr ""
-#: lib/salmonaction.php:41
-msgid "This method requires a POST."
+#: actions/groupsalmon.php:167
+msgid "Can't read profile to cancel group membership."
msgstr ""
-#: lib/salmonaction.php:45
-msgid "Salmon requires application/magic-envelope+xml"
+#: actions/groupsalmon.php:183
+#, php-format
+msgid "Could not remove remote user %1$s from group %2$s."
msgstr ""
-#: lib/salmonaction.php:55
-msgid "Salmon signature verification failed."
+#: actions/ostatussub.php:65
+msgid "Subscribe to"
msgstr ""
-#: lib/salmonaction.php:67
-msgid "Salmon post must be an Atom entry."
+#: actions/ostatussub.php:67
+msgid ""
+"OStatus user's address, like nickname@example.com or http://example.net/"
+"nickname"
msgstr ""
-#: lib/salmonaction.php:115
-msgid "Unrecognized activity type."
+#: actions/ostatussub.php:106
+msgid "Join this group"
msgstr ""
-#: lib/salmonaction.php:123
-msgid "This target doesn't understand posts."
+#. TRANS: Page title for OStatus remote subscription form
+#: actions/ostatussub.php:108 actions/ostatussub.php:400
+msgid "Confirm"
msgstr ""
-#: lib/salmonaction.php:128
-msgid "This target doesn't understand follows."
+#: actions/ostatussub.php:109
+msgid "Subscribe to this user"
msgstr ""
-#: lib/salmonaction.php:133
-msgid "This target doesn't understand unfollows."
+#: actions/ostatussub.php:130
+msgid "You are already subscribed to this user."
msgstr ""
-#: lib/salmonaction.php:138
-msgid "This target doesn't understand favorites."
+#: actions/ostatussub.php:247 actions/ostatussub.php:253
+#: actions/ostatussub.php:272
+msgid ""
+"Sorry, we could not reach that address. Please make sure that the OStatus "
+"address is like nickname@example.com or http://example.net/nickname"
msgstr ""
-#: lib/salmonaction.php:143
-msgid "This target doesn't understand unfavorites."
+#: actions/ostatussub.php:256 actions/ostatussub.php:259
+#: actions/ostatussub.php:262 actions/ostatussub.php:265
+#: actions/ostatussub.php:268
+msgid ""
+"Sorry, we could not reach that feed. Please try that OStatus address again "
+"later."
msgstr ""
-#: lib/salmonaction.php:148
-msgid "This target doesn't understand share events."
+#. TRANS: OStatus remote subscription dialog error.
+#: actions/ostatussub.php:301
+msgid "Already subscribed!"
msgstr ""
-#: lib/salmonaction.php:153
-msgid "This target doesn't understand joins."
+#. TRANS: OStatus remote subscription dialog error.
+#: actions/ostatussub.php:306
+msgid "Remote subscription failed!"
msgstr ""
-#: lib/salmonaction.php:158
-msgid "This target doesn't understand leave events."
+#: actions/ostatussub.php:380 actions/ostatusinit.php:81
+msgid "Subscribe to user"
msgstr ""
-#: OStatusPlugin.php:319
-#, php-format
-msgid "Sent from %s via OStatus"
+#: actions/ostatussub.php:411
+msgid ""
+"You can subscribe to users from other supported sites. Paste their address "
+"or profile URI below:"
msgstr ""
-#: OStatusPlugin.php:371
-msgid "Could not set up remote subscription."
+#: actions/ostatusinit.php:41
+msgid "You can use the local subscription!"
msgstr ""
-#: OStatusPlugin.php:487
-msgid "Could not set up remote group membership."
+#: actions/ostatusinit.php:63
+msgid "There was a problem with your session token. Try again, please."
msgstr ""
-#: OStatusPlugin.php:504
+#: actions/ostatusinit.php:95
#, php-format
-msgid "%s has joined group %s."
+msgid "Join group %s"
msgstr ""
-#: OStatusPlugin.php:512
-msgid "Failed joining remote group."
+#: actions/ostatusinit.php:98
+#, php-format
+msgid "Subscribe to %s"
msgstr ""
-#: OStatusPlugin.php:553
-msgid "Leave"
+#: actions/ostatusinit.php:111
+msgid "User nickname"
msgstr ""
-#: OStatusPlugin.php:554
-#, php-format
-msgid "%s has left group %s."
+#: actions/ostatusinit.php:112
+msgid "Nickname of the user you want to follow"
msgstr ""
-#: OStatusPlugin.php:685
-msgid "Subscribe to remote user"
+#: actions/ostatusinit.php:116
+msgid "Profile Account"
msgstr ""
-#: OStatusPlugin.php:726
-msgid "Profile update"
+#: actions/ostatusinit.php:117
+msgid "Your account id (i.e. user@identi.ca)"
msgstr ""
-#: OStatusPlugin.php:727
-#, php-format
-msgid "%s has updated their profile page."
+#: actions/ostatusinit.php:138
+msgid "Must provide a remote profile."
msgstr ""
-#: tests/gettext-speedtest.php:57
-msgid "Feeds"
+#: actions/ostatusinit.php:149
+msgid "Couldn't look up OStatus account profile."
+msgstr ""
+
+#: actions/ostatusinit.php:161
+msgid "Couldn't confirm remote profile address."
+msgstr ""
+
+#: actions/ostatusinit.php:202
+msgid "OStatus Connect"
msgstr ""
diff --git a/plugins/OStatus/locale/fr/LC_MESSAGES/OStatus.po b/plugins/OStatus/locale/fr/LC_MESSAGES/OStatus.po
deleted file mode 100644
index f17dfa50a..000000000
--- a/plugins/OStatus/locale/fr/LC_MESSAGES/OStatus.po
+++ /dev/null
@@ -1,106 +0,0 @@
-# SOME DESCRIPTIVE TITLE.
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
-# This file is distributed under the same license as the PACKAGE package.
-# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
-#
-#, fuzzy
-msgid ""
-msgstr ""
-"Project-Id-Version: PACKAGE VERSION\n"
-"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2009-12-07 14:14-0800\n"
-"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
-"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
-"Language-Team: LANGUAGE <LL@li.org>\n"
-"MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=UTF-8\n"
-"Content-Transfer-Encoding: 8bit\n"
-
-#: FeedSubPlugin.php:77
-msgid "Feeds"
-msgstr "Flux"
-
-#: FeedSubPlugin.php:78
-msgid "Feed subscription options"
-msgstr "Préférences pour abonnement flux"
-
-#: feedmunger.php:215
-#, php-format
-msgid "New post: \"%1$s\" %2$s"
-msgstr "Nouveau: \"%1$s\" %2$s"
-
-#: actions/feedsubsettings.php:41
-msgid "Feed subscriptions"
-msgstr "Abonnements aux fluxes"
-
-#: actions/feedsubsettings.php:52
-msgid ""
-"You can subscribe to feeds from other sites; updates will appear in your "
-"personal timeline."
-msgstr ""
-"Abonner aux fluxes RSS ou Atom des autres sites web; les temps se trouverair"
-"en votre flux personnel."
-
-#: actions/feedsubsettings.php:96
-msgid "Subscribe"
-msgstr "Abonner"
-
-#: actions/feedsubsettings.php:98
-msgid "Continue"
-msgstr "Prochaine"
-
-#: actions/feedsubsettings.php:151
-msgid "Empty feed URL!"
-msgstr ""
-
-#: actions/feedsubsettings.php:161
-msgid "Invalid URL or could not reach server."
-msgstr ""
-
-#: actions/feedsubsettings.php:164
-msgid "Cannot read feed; server returned error."
-msgstr ""
-
-#: actions/feedsubsettings.php:167
-msgid "Cannot read feed; server returned an empty page."
-msgstr ""
-
-#: actions/feedsubsettings.php:170
-msgid "Bad HTML, could not find feed link."
-msgstr ""
-
-#: actions/feedsubsettings.php:173
-msgid "Could not find a feed linked from this URL."
-msgstr ""
-
-#: actions/feedsubsettings.php:176
-msgid "Not a recognized feed type."
-msgstr ""
-
-#: actions/feedsubsettings.php:180
-msgid "Bad feed URL."
-msgstr ""
-
-#: actions/feedsubsettings.php:188
-msgid "Feed is not PuSH-enabled; cannot subscribe."
-msgstr ""
-
-#: actions/feedsubsettings.php:208
-msgid "Feed subscription failed! Bad response from hub."
-msgstr ""
-
-#: actions/feedsubsettings.php:218
-msgid "Already subscribed!"
-msgstr ""
-
-#: actions/feedsubsettings.php:220
-msgid "Feed subscribed!"
-msgstr ""
-
-#: actions/feedsubsettings.php:222
-msgid "Feed subscription failed!"
-msgstr ""
-
-#: actions/feedsubsettings.php:231
-msgid "Previewing feed:"
-msgstr ""
diff --git a/plugins/OStatus/scripts/fixup-shadow.php b/plugins/OStatus/scripts/fixup-shadow.php
index ec014c787..6522ca240 100644
--- a/plugins/OStatus/scripts/fixup-shadow.php
+++ b/plugins/OStatus/scripts/fixup-shadow.php
@@ -50,20 +50,47 @@ $encGroup = str_replace($marker, '%', $encGroup);
$sql = "SELECT * FROM ostatus_profile WHERE uri LIKE '%s' OR uri LIKE '%s'";
$oprofile->query(sprintf($sql, $encProfile, $encGroup));
-echo "Found $oprofile->N bogus ostatus_profile entries for local users and groups:\n";
+$count = $oprofile->N;
+echo "Found $count bogus ostatus_profile entries shadowing local users and groups:\n";
while ($oprofile->fetch()) {
- echo "$oprofile->uri";
-
+ $uri = $oprofile->uri;
+ if (preg_match('!/group/(\d+)/id!', $oprofile->uri, $matches)) {
+ $id = intval($matches[1]);
+ $group = Local_group::staticGet('group_id', $id);
+ if ($group) {
+ $nick = $group->nickname;
+ } else {
+ $nick = '<deleted>';
+ }
+ echo "group $id ($nick) hidden by $uri";
+ } else if (preg_match('!/user/(\d+)!', $uri, $matches)) {
+ $id = intval($matches[1]);
+ $user = User::staticGet('id', $id);
+ if ($user) {
+ $nick = $user->nickname;
+ } else {
+ $nick = '<deleted>';
+ }
+ echo "user $id ($nick) hidden by $uri";
+ } else {
+ echo "$uri matched query, but we don't recognize it.\n";
+ continue;
+ }
+
if ($dry) {
- echo " (unchanged)\n";
+ echo " - skipping\n";
} else {
- echo " removing bogus ostatus_profile entry...";
+ echo " - removing bogus ostatus_profile entry...";
$evil = clone($oprofile);
$evil->delete();
echo " ok\n";
}
}
-echo "done.\n";
+if ($count && $dry) {
+ echo "NO CHANGES MADE -- To delete the bogus entries, run again without --dry-run option.\n";
+} else {
+ echo "done.\n";
+}
diff --git a/plugins/OStatus/scripts/resub-feed.php b/plugins/OStatus/scripts/resub-feed.php
new file mode 100644
index 000000000..121d12109
--- /dev/null
+++ b/plugins/OStatus/scripts/resub-feed.php
@@ -0,0 +1,74 @@
+#!/usr/bin/env php
+<?php
+/*
+ * StatusNet - a 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/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
+
+$helptext = <<<END_OF_HELP
+resub-feed.php [options] http://example.com/atom-feed-url
+Reinitialize the PuSH subscription for the given feed. This may help get
+things restarted if we and the hub have gotten our states out of sync.
+
+
+END_OF_HELP;
+
+require_once INSTALLDIR.'/scripts/commandline.inc';
+
+if (empty($args[0]) || !Validate::uri($args[0])) {
+ print "$helptext";
+ exit(1);
+}
+
+$feedurl = $args[0];
+
+
+$sub = FeedSub::staticGet('topic', $feedurl);
+if (!$sub) {
+ print "Feed $feedurl is not subscribed.\n";
+ exit(1);
+}
+
+print "Old state:\n";
+showSub($sub);
+
+print "\n";
+print "Pinging hub $sub->huburi with new subscription for $sub->uri\n";
+$ok = $sub->subscribe();
+
+if ($ok) {
+ print "ok\n";
+} else {
+ print "Could not confirm.\n";
+}
+
+$sub2 = FeedSub::staticGet('topic', $feedurl);
+
+print "\n";
+print "New state:\n";
+showSub($sub2);
+
+function showSub($sub)
+{
+ print " Subscription state: $sub->sub_state\n";
+ print " Verify token: $sub->verify_token\n";
+ print " Signature secret: $sub->secret\n";
+ print " Sub start date: $sub->sub_start\n";
+ print " Record created: $sub->created\n";
+ print " Record modified: $sub->modified\n";
+}
diff --git a/plugins/OStatus/scripts/update-profile.php b/plugins/OStatus/scripts/update-profile.php
new file mode 100644
index 000000000..64afa0f35
--- /dev/null
+++ b/plugins/OStatus/scripts/update-profile.php
@@ -0,0 +1,147 @@
+#!/usr/bin/env php
+<?php
+/*
+ * StatusNet - a 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/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
+
+$helptext = <<<END_OF_HELP
+update-profile.php [options] http://example.com/profile/url
+
+Rerun profile and feed info discovery for the given OStatus remote profile,
+and reinitialize its PuSH subscription for the given feed. This may help get
+things restarted if the hub or feed URLs have changed for the profile.
+
+
+END_OF_HELP;
+
+require_once INSTALLDIR.'/scripts/commandline.inc';
+
+if (empty($args[0]) || !Validate::uri($args[0])) {
+ print "$helptext";
+ exit(1);
+}
+
+$uri = $args[0];
+
+
+$oprofile = Ostatus_profile::staticGet('uri', $uri);
+
+if (!$oprofile) {
+ print "No OStatus remote profile known for URI $uri\n";
+ exit(1);
+}
+
+print "Old profile state for $oprofile->uri\n";
+showProfile($oprofile);
+
+print "\n";
+print "Re-running feed discovery for profile URL $oprofile->uri\n";
+// @fixme will bork where the URI isn't the profile URL for now
+$discover = new FeedDiscovery();
+$feedurl = $discover->discoverFromURL($oprofile->uri);
+$huburi = $discover->getHubLink();
+$salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
+
+print " Feed URL: $feedurl\n";
+print " Hub URL: $huburi\n";
+print " Salmon URL: $salmonuri\n";
+
+if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
+ print "\n";
+ print "Updating...\n";
+ // @fixme update keys :P
+ #$orig = clone($oprofile);
+ #$oprofile->feeduri = $feedurl;
+ #$oprofile->salmonuri = $salmonuri;
+ #$ok = $oprofile->update($orig);
+ $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
+ 'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
+ 'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
+ 'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
+
+ if (!$ok) {
+ print "Failed to update profile record...\n";
+ exit(1);
+ }
+
+ $oprofile->decache();
+} else {
+ print "\n";
+ print "Ok, ostatus_profile record unchanged.\n\n";
+}
+
+$sub = FeedSub::ensureFeed($feedurl);
+
+if ($huburi != $sub->huburi) {
+ print "\n";
+ print "Updating hub record for feed; was $sub->huburi\n";
+ $orig = clone($sub);
+ $sub->huburi = $huburi;
+ $ok = $sub->update($orig);
+
+ if (!$ok) {
+ print "Failed to update sub record...\n";
+ exit(1);
+ }
+} else {
+ print "\n";
+ print "Feed record ok, not changing.\n\n";
+}
+
+print "\n";
+print "Pinging hub $sub->huburi with new subscription for $sub->uri\n";
+$ok = $sub->subscribe();
+
+if ($ok) {
+ print "ok\n";
+} else {
+ print "Could not confirm.\n";
+}
+
+$o2 = Ostatus_profile::staticGet('uri', $uri);
+
+print "\n";
+print "New profile state:\n";
+showProfile($o2);
+
+print "\n";
+print "New feed state:\n";
+$sub2 = FeedSub::ensureFeed($feedurl);
+showSub($sub2);
+
+function showProfile($oprofile)
+{
+ print " Feed URL: $oprofile->feeduri\n";
+ print " Salmon URL: $oprofile->salmonuri\n";
+ print " Avatar URL: $oprofile->avatar\n";
+ print " Profile ID: $oprofile->profile_id\n";
+ print " Group ID: $oprofile->group_id\n";
+ print " Record created: $oprofile->created\n";
+ print " Record modified: $oprofile->modified\n";
+}
+
+function showSub($sub)
+{
+ print " Subscription state: $sub->sub_state\n";
+ print " Verify token: $sub->verify_token\n";
+ print " Signature secret: $sub->secret\n";
+ print " Sub start date: $sub->sub_start\n";
+ print " Record created: $sub->created\n";
+ print " Record modified: $sub->modified\n";
+}
diff --git a/plugins/OStatus/tests/FeedDiscoveryTest.php b/plugins/OStatus/tests/FeedDiscoveryTest.php
index 1c5249701..0e6354a86 100644
--- a/plugins/OStatus/tests/FeedDiscoveryTest.php
+++ b/plugins/OStatus/tests/FeedDiscoveryTest.php
@@ -10,7 +10,7 @@ define('STATUSNET', true);
define('LACONICA', true);
require_once INSTALLDIR . '/lib/common.php';
-require_once INSTALLDIR . '/plugins/FeedSub/feedsub.php';
+require_once INSTALLDIR . '/plugins/OStatus/lib/feeddiscovery.php';
class FeedDiscoveryTest extends PHPUnit_Framework_TestCase
{
diff --git a/plugins/OStatus/tests/remote-tests.php b/plugins/OStatus/tests/remote-tests.php
new file mode 100644
index 000000000..24b4b1660
--- /dev/null
+++ b/plugins/OStatus/tests/remote-tests.php
@@ -0,0 +1,555 @@
+<?php
+
+if (php_sapi_name() != 'cli') {
+ die('not for web');
+}
+
+define('INSTALLDIR', dirname(dirname(dirname(dirname(__FILE__)))));
+set_include_path(INSTALLDIR . '/extlib' . PATH_SEPARATOR . get_include_path());
+
+require_once 'PEAR.php';
+require_once 'Net/URL2.php';
+require_once 'HTTP/Request2.php';
+
+
+// ostatus test script, client-side :)
+
+class TestBase
+{
+ function log($str)
+ {
+ $args = func_get_args();
+ array_shift($args);
+
+ $msg = vsprintf($str, $args);
+ print $msg . "\n";
+ }
+
+ function assertEqual($a, $b)
+ {
+ if ($a != $b) {
+ throw new Exception("Failed to assert equality: expected $a, got $b");
+ }
+ return true;
+ }
+
+ function assertNotEqual($a, $b)
+ {
+ if ($a == $b) {
+ throw new Exception("Failed to assert inequality: expected not $a, got $b");
+ }
+ return true;
+ }
+
+ function assertTrue($a)
+ {
+ if (!$a) {
+ throw new Exception("Failed to assert true: got false");
+ }
+ }
+
+ function assertFalse($a)
+ {
+ if ($a) {
+ throw new Exception("Failed to assert false: got true");
+ }
+ }
+}
+
+class OStatusTester extends TestBase
+{
+ /**
+ * @param string $a base URL of test site A (eg http://localhost/mublog)
+ * @param string $b base URL of test site B (eg http://localhost/mublog2)
+ */
+ function __construct($a, $b) {
+ $this->a = $a;
+ $this->b = $b;
+
+ $base = 'test' . mt_rand(1, 1000000);
+ $this->pub = new SNTestClient($this->a, 'pub' . $base, 'pw-' . mt_rand(1, 1000000));
+ $this->sub = new SNTestClient($this->b, 'sub' . $base, 'pw-' . mt_rand(1, 1000000));
+ }
+
+ function run()
+ {
+ $this->setup();
+
+ $methods = get_class_methods($this);
+ foreach ($methods as $method) {
+ if (strtolower(substr($method, 0, 4)) == 'test') {
+ print "\n";
+ print "== $method ==\n";
+ call_user_func(array($this, $method));
+ }
+ }
+
+ print "\n";
+ $this->log("DONE!");
+ }
+
+ function setup()
+ {
+ $this->pub->register();
+ $this->pub->assertRegistered();
+
+ $this->sub->register();
+ $this->sub->assertRegistered();
+ }
+
+ function testLocalPost()
+ {
+ $post = $this->pub->post("Local post, no subscribers yet.");
+ $this->assertNotEqual('', $post);
+
+ $post = $this->sub->post("Local post, no subscriptions yet.");
+ $this->assertNotEqual('', $post);
+ }
+
+ /**
+ * pub posts: @b/sub
+ */
+ function testMentionUrl()
+ {
+ $bits = parse_url($this->b);
+ $base = $bits['host'];
+ if (isset($bits['path'])) {
+ $base .= $bits['path'];
+ }
+ $name = $this->sub->username;
+
+ $post = $this->pub->post("@$base/$name should have this in home and replies");
+ $this->sub->assertReceived($post);
+ }
+
+ function testSubscribe()
+ {
+ $this->assertFalse($this->sub->hasSubscription($this->pub->getProfileUri()));
+ $this->assertFalse($this->pub->hasSubscriber($this->sub->getProfileUri()));
+ $this->sub->subscribe($this->pub->getProfileLink());
+ $this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
+ $this->assertTrue($this->pub->hasSubscriber($this->sub->getProfileUri()));
+ }
+
+ function testPush()
+ {
+ $this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
+ $this->assertTrue($this->pub->hasSubscriber($this->sub->getProfileUri()));
+
+ $name = $this->sub->username;
+ $post = $this->pub->post("Regular post, which $name should get via PuSH");
+ $this->sub->assertReceived($post);
+ }
+
+ function testMentionSubscribee()
+ {
+ $this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
+ $this->assertFalse($this->pub->hasSubscription($this->sub->getProfileUri()));
+
+ $name = $this->pub->username;
+ $post = $this->sub->post("Just a quick note back to my remote subscribee @$name");
+ $this->pub->assertReceived($post);
+ }
+
+ function testUnsubscribe()
+ {
+ $this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
+ $this->assertTrue($this->pub->hasSubscriber($this->sub->getProfileUri()));
+ $this->sub->unsubscribe($this->pub->getProfileLink());
+ $this->assertFalse($this->sub->hasSubscription($this->pub->getProfileUri()));
+ $this->assertFalse($this->pub->hasSubscriber($this->sub->getProfileUri()));
+ }
+
+}
+
+class SNTestClient extends TestBase
+{
+ function __construct($base, $username, $password)
+ {
+ $this->basepath = $base;
+ $this->username = $username;
+ $this->password = $password;
+
+ $this->fullname = ucfirst($username) . ' Smith';
+ $this->homepage = 'http://example.org/' . $username;
+ $this->bio = 'Stub account for OStatus tests.';
+ $this->location = 'Montreal, QC';
+ }
+
+ /**
+ * Make a low-level web hit to this site, with authentication.
+ * @param string $path URL fragment for something under the base path
+ * @param array $params POST parameters to send
+ * @param boolean $auth whether to include auth data
+ * @return string
+ * @throws Exception on low-level error conditions
+ */
+ protected function hit($path, $params=array(), $auth=false, $cookies=array())
+ {
+ $url = $this->basepath . '/' . $path;
+
+ $http = new HTTP_Request2($url, 'POST');
+ if ($auth) {
+ $http->setAuth($this->username, $this->password, HTTP_Request2::AUTH_BASIC);
+ }
+ foreach ($cookies as $name => $val) {
+ $http->addCookie($name, $val);
+ }
+ $http->addPostParameter($params);
+ $response = $http->send();
+
+ $code = $response->getStatus();
+ if ($code < '200' || $code >= '400') {
+ throw new Exception("Failed API hit to $url: $code\n" . $response->getBody());
+ }
+
+ return $response;
+ }
+
+ /**
+ * Make a hit to a web form, without authentication but with a session.
+ * @param string $path URL fragment relative to site base
+ * @param string $form id of web form to pull initial parameters from
+ * @param array $params POST parameters, will be merged with defaults in form
+ */
+ protected function web($path, $form, $params=array())
+ {
+ $url = $this->basepath . '/' . $path;
+ $http = new HTTP_Request2($url, 'GET');
+ $response = $http->send();
+
+ $dom = $this->checkWeb($url, 'GET', $response);
+ $cookies = array();
+ foreach ($response->getCookies() as $cookie) {
+ // @fixme check for expirations etc
+ $cookies[$cookie['name']] = $cookie['value'];
+ }
+
+ $form = $dom->getElementById($form);
+ if (!$form) {
+ throw new Exception("Form $form not found on $url");
+ }
+ $inputs = $form->getElementsByTagName('input');
+ foreach ($inputs as $item) {
+ $type = $item->getAttribute('type');
+ if ($type != 'check') {
+ $name = $item->getAttribute('name');
+ $val = $item->getAttribute('value');
+ if ($name && $val && !isset($params[$name])) {
+ $params[$name] = $val;
+ }
+ }
+ }
+
+ $response = $this->hit($path, $params, false, $cookies);
+ $dom = $this->checkWeb($url, 'POST', $response);
+
+ return $dom;
+ }
+
+ protected function checkWeb($url, $method, $response)
+ {
+ $dom = new DOMDocument();
+ if (!$dom->loadHTML($response->getBody())) {
+ throw new Exception("Invalid HTML from $method to $url");
+ }
+
+ $xpath = new DOMXPath($dom);
+ $error = $xpath->query('//p[@class="error"]');
+ if ($error && $error->length) {
+ throw new Exception("Error on $method to $url: " .
+ $error->item(0)->textContent);
+ }
+
+ return $dom;
+ }
+
+ protected function parseXml($path, $body)
+ {
+ $dom = new DOMDocument();
+ if ($dom->loadXML($body)) {
+ return $dom;
+ } else {
+ throw new Exception("Bogus XML data from $path:\n$body");
+ }
+ }
+
+ /**
+ * Make a hit to a REST-y XML page on the site, without authentication.
+ * @param string $path URL fragment for something relative to base
+ * @param array $params POST parameters to send
+ * @return DOMDocument
+ * @throws Exception on low-level error conditions
+ */
+ protected function xml($path, $params=array())
+ {
+ $response = $this->hit($path, $params, true);
+ $body = $response->getBody();
+ return $this->parseXml($path, $body);
+ }
+
+ protected function parseJson($path, $body)
+ {
+ $data = json_decode($body, true);
+ if ($data !== null) {
+ if (!empty($data['error'])) {
+ throw new Exception("JSON API returned error: " . $data['error']);
+ }
+ return $data;
+ } else {
+ throw new Exception("Bogus JSON data from $path:\n$body");
+ }
+ }
+
+ /**
+ * Make an API hit to this site, with authentication.
+ * @param string $path URL fragment for something under 'api' folder
+ * @param string $style one of 'json', 'xml', or 'atom'
+ * @param array $params POST parameters to send
+ * @return mixed associative array for JSON, DOMDocument for XML/Atom
+ * @throws Exception on low-level error conditions
+ */
+ protected function api($path, $style, $params=array())
+ {
+ $response = $this->hit("api/$path.$style", $params, true);
+ $body = $response->getBody();
+ if ($style == 'json') {
+ return $this->parseJson($path, $body);
+ } else if ($style == 'xml' || $style == 'atom') {
+ return $this->parseXml($path, $body);
+ } else {
+ throw new Exception("API needs to be JSON, XML, or Atom");
+ }
+ }
+
+ /**
+ * Register the account.
+ *
+ * Unfortunately there's not an API method for registering, so we fake it.
+ */
+ function register()
+ {
+ $this->log("Registering user %s on %s",
+ $this->username,
+ $this->basepath);
+ $ret = $this->web('main/register', 'form_register',
+ array('nickname' => $this->username,
+ 'password' => $this->password,
+ 'confirm' => $this->password,
+ 'fullname' => $this->fullname,
+ 'homepage' => $this->homepage,
+ 'bio' => $this->bio,
+ 'license' => 1,
+ 'submit' => 'Register'));
+ }
+
+ /**
+ * @return string canonical URI/URL to profile page
+ */
+ function getProfileUri()
+ {
+ $data = $this->api('account/verify_credentials', 'json');
+ $id = $data['id'];
+ return $this->basepath . '/user/' . $id;
+ }
+
+ /**
+ * @return string human-friendly URL to profile page
+ */
+ function getProfileLink()
+ {
+ return $this->basepath . '/' . $this->username;
+ }
+
+ /**
+ * Check that the account has been registered and can be used.
+ * On failure, throws a test failure exception.
+ */
+ function assertRegistered()
+ {
+ $this->log("Confirming %s is registered on %s",
+ $this->username,
+ $this->basepath);
+ $data = $this->api('account/verify_credentials', 'json');
+ $this->assertEqual($this->username, $data['screen_name']);
+ $this->assertEqual($this->fullname, $data['name']);
+ $this->assertEqual($this->homepage, $data['url']);
+ $this->assertEqual($this->bio, $data['description']);
+ $this->log(" looks good!");
+ }
+
+ /**
+ * Post a given message from this account
+ * @param string $message
+ * @return string URL/URI of notice
+ * @todo reply, location options
+ */
+ function post($message)
+ {
+ $this->log("Posting notice as %s on %s: %s",
+ $this->username,
+ $this->basepath,
+ $message);
+ $data = $this->api('statuses/update', 'json',
+ array('status' => $message));
+
+ $url = $this->basepath . '/notice/' . $data['id'];
+ return $url;
+ }
+
+ /**
+ * Check that this account has received the notice.
+ * @param string $notice_uri URI for the notice to check for
+ */
+ function assertReceived($notice_uri)
+ {
+ $timeout = 5;
+ $tries = 6;
+ while ($tries) {
+ $ok = $this->checkReceived($notice_uri);
+ if ($ok) {
+ return true;
+ }
+ $tries--;
+ if ($tries) {
+ $this->log(" didn't see it yet, waiting $timeout seconds");
+ sleep($timeout);
+ }
+ }
+ throw new Exception(" message $notice_uri not received by $this->username");
+ }
+
+ /**
+ * Pull the user's home timeline to check if a notice with the given
+ * source URL has been received recently.
+ * If we don't see it, we'll try a couple more times up to 10 seconds.
+ *
+ * @param string $notice_uri
+ */
+ function checkReceived($notice_uri)
+ {
+ $this->log("Checking if %s on %s received notice %s",
+ $this->username,
+ $this->basepath,
+ $notice_uri);
+ $params = array();
+ $dom = $this->api('statuses/home_timeline', 'atom', $params);
+
+ $xml = simplexml_import_dom($dom);
+ if (!$xml->entry) {
+ return false;
+ }
+ if (is_array($xml->entry)) {
+ $entries = $xml->entry;
+ } else {
+ $entries = array($xml->entry);
+ }
+ foreach ($entries as $entry) {
+ if ($entry->id == $notice_uri) {
+ $this->log(" found it $notice_uri");
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * @param string $profile user page link or webfinger
+ */
+ function subscribe($profile)
+ {
+ // This uses the command interface, since there's not currently
+ // a friendly Twit-API way to do a fresh remote subscription and
+ // the web form's a pain to use.
+ $this->post('follow ' . $profile);
+ }
+
+ /**
+ * @param string $profile user page link or webfinger
+ */
+ function unsubscribe($profile)
+ {
+ // This uses the command interface, since there's not currently
+ // a friendly Twit-API way to do a fresh remote subscription and
+ // the web form's a pain to use.
+ $this->post('leave ' . $profile);
+ }
+
+ /**
+ * Check that this account is subscribed to the given profile.
+ * @param string $profile_uri URI for the profile to check for
+ * @return boolean
+ */
+ function hasSubscription($profile_uri)
+ {
+ $this->log("Checking if $this->username has a subscription to $profile_uri");
+
+ $me = $this->getProfileUri();
+ return $this->checkSubscription($me, $profile_uri);
+ }
+
+ /**
+ * Check that this account is subscribed to by the given profile.
+ * @param string $profile_uri URI for the profile to check for
+ * @return boolean
+ */
+ function hasSubscriber($profile_uri)
+ {
+ $this->log("Checking if $this->username is subscribed to by $profile_uri");
+
+ $me = $this->getProfileUri();
+ return $this->checkSubscription($profile_uri, $me);
+ }
+
+ protected function checkSubscription($subscriber, $subscribed)
+ {
+ // Using FOAF as the API methods for checking the social graph
+ // currently are unfriendly to remote profiles
+ $ns_foaf = 'http://xmlns.com/foaf/0.1/';
+ $ns_sioc = 'http://rdfs.org/sioc/ns#';
+ $ns_rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
+
+ $dom = $this->xml($this->username . '/foaf');
+ $agents = $dom->getElementsByTagNameNS($ns_foaf, 'Agent');
+ foreach ($agents as $agent) {
+ $agent_uri = $agent->getAttributeNS($ns_rdf, 'about');
+ if ($agent_uri == $subscriber) {
+ $follows = $agent->getElementsByTagNameNS($ns_sioc, 'follows');
+ foreach ($follows as $follow) {
+ $target = $follow->getAttributeNS($ns_rdf, 'resource');
+ if ($target == ($subscribed . '#acct')) {
+ $this->log(" confirmed $subscriber subscribed to $subscribed");
+ return true;
+ }
+ }
+ $this->log(" we found $subscriber but they don't follow $subscribed");
+ return false;
+ }
+ }
+ $this->log(" can't find $subscriber in {$this->username}'s social graph.");
+ return false;
+ }
+
+}
+
+$args = array_slice($_SERVER['argv'], 1);
+if (count($args) < 2) {
+ print <<<END_HELP
+remote-tests.php <url1> <url2>
+ url1: base URL of a StatusNet instance
+ url2: base URL of another StatusNet instance
+
+This will register user accounts on the two given StatusNet instances
+and run some tests to confirm that OStatus subscription and posting
+between the two sites works correctly.
+
+END_HELP;
+exit(1);
+}
+
+$a = $args[0];
+$b = $args[1];
+
+$tester = new OStatusTester($a, $b);
+$tester->run();
+