summaryrefslogtreecommitdiff
path: root/plugins/RSSCloud
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/RSSCloud')
-rw-r--r--plugins/RSSCloud/LoggingAggregator.php140
-rw-r--r--plugins/RSSCloud/README54
-rw-r--r--plugins/RSSCloud/RSSCloudNotifier.php240
-rw-r--r--plugins/RSSCloud/RSSCloudPlugin.php295
-rwxr-xr-xplugins/RSSCloud/RSSCloudQueueHandler.php78
-rw-r--r--plugins/RSSCloud/RSSCloudRequestNotify.php347
-rw-r--r--plugins/RSSCloud/RSSCloudSubscription.php79
7 files changed, 1233 insertions, 0 deletions
diff --git a/plugins/RSSCloud/LoggingAggregator.php b/plugins/RSSCloud/LoggingAggregator.php
new file mode 100644
index 000000000..e37eed16a
--- /dev/null
+++ b/plugins/RSSCloud/LoggingAggregator.php
@@ -0,0 +1,140 @@
+<?php
+/**
+ * This test class pretends to be an RSS aggregator. It logs notifications
+ * from the cloud.
+ *
+ * PHP version 5
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
+ * @link http://status.net/
+ *
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('STATUSNET')) {
+ exit(1);
+}
+
+/**
+ * Dummy aggregator that acts as a proper notification handler. It
+ * doesn't do anything but respond correctly when notified via
+ * REST. Mostly, this is just and action I used to develop the plugin
+ * and easily test things end-to-end. I'm leaving it in here as it
+ * may be useful for developing the plugin further.
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ **/
+class LoggingAggregatorAction extends Action
+{
+
+ var $challenge = null;
+ var $url = null;
+
+ /**
+ * Initialization.
+ *
+ * @param array $args Web and URL arguments
+ *
+ * @return boolean false if user doesn't exist
+ */
+
+ function prepare($args)
+ {
+ parent::prepare($args);
+
+ $this->url = $this->arg('url');
+ $this->challenge = $this->arg('challenge');
+
+ common_debug("args = " . var_export($this->args, true));
+ common_debug('url = ' . $this->url . ' challenge = ' . $this->challenge);
+
+ return true;
+ }
+
+ /**
+ * Handle the request
+ *
+ * @param array $args $_REQUEST data (unused)
+ *
+ * @return void
+ */
+
+ function handle($args)
+ {
+ parent::handle($args);
+
+ if (empty($this->url)) {
+ $this->showError('Hey, you have to provide a url parameter.');
+ return;
+ }
+
+ if (!empty($this->challenge)) {
+
+ // must be a GET
+
+ if ($_SERVER['REQUEST_METHOD'] != 'GET') {
+ $this->showError('This resource requires an HTTP GET.');
+ return;
+ }
+
+ header('Content-Type: text/xml');
+ echo $this->challenge;
+
+ } else {
+
+ // must be a POST
+
+ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+ $this->showError('This resource requires an HTTP POST.');
+ return;
+ }
+
+ header('Content-Type: text/xml');
+ Echo "<notifyResult success='true' msg='Thanks for the update.' />\n";
+ }
+
+ $this->ip = $_SERVER['REMOTE_ADDR'];
+
+ common_log(LOG_INFO, 'RSSCloud Logging Aggregator - ' .
+ $this->ip . ' claims the feed at ' .
+ $this->url . ' has been updated.');
+ }
+
+ /**
+ * Show an XML error when things go badly
+ *
+ * @param string $msg the error message
+ *
+ * @return void
+ */
+
+ function showError($msg)
+ {
+ header('HTTP/1.1 400 Bad Request');
+ header('Content-Type: text/xml');
+ echo "<?xml version='1.0'?>\n";
+ echo "<notifyResult success='false' msg='$msg' />\n";
+ }
+
+} \ No newline at end of file
diff --git a/plugins/RSSCloud/README b/plugins/RSSCloud/README
new file mode 100644
index 000000000..1237e3e0e
--- /dev/null
+++ b/plugins/RSSCloud/README
@@ -0,0 +1,54 @@
+This plugin enables RSSCloud (http://rsscloud.org/) publishing and
+subscription handling for RSS 2.0 profile feeds (i.e:
+http://SITE/PATH/api/statuses/user_timeline/USERNAME.rss). When the
+plugin is enabled, StatusNet acts as both the publisher and hub ('writer' and
+'cloud' in RSSCloud parlance), but only for local StatusNet feeds. It's
+not possible to use it as a general purpose hub -- for instance you can't
+subscribe and get updates to a Wordpress feed from StatusNet using this
+plugin.
+
+To use the plugin, add the following to your config.php:
+
+ addPlugin('RSSCloud');
+
+Enabling the plugin will add a <cloud> element to your RSS 2.0 profile feeds
+that looks like this:
+
+ <cloud domain="SITE" port="80" path="/main/rsscloud/request_notify"
+ registerProcedure="" protocol="http-post"/>
+
+Aggregators may subscribe by sending a proper REST RSSCloud subscription
+request (the optional 'domain' parameter with challenge is supported).
+Subscribing aggregators will be notified ('pinged') when users they have
+subscribed to post new notices. Currently, REST is the only protocol
+supported for notifications.
+
+Deamon
+------
+
+There's also a daemon for offline processing of queued notices with
+RSSCloud destinations, which will start automatically if/when you run
+scripts/startdaemons.sh.
+
+Notes
+-----
+
+- Again, only RSS 2.0 profile feeds may be subscribed to, and they have
+ to be the ones with user names in them, like:
+ http://SITE/PATH/api/statuses/user_timeline/USERNAME.rss
+- Subscriptions are deleted after three notification failures in a row
+ (not sure this is optimal).
+- The plugin includes a dummy LoggingAggregator class that can be used
+ for end-to-end testing. You probably don't want to mess with it.
+
+TODO
+----
+
+- Figure out why the RSSCloudSubcription can't ->delete() or ->update()
+- Support pinging via XML-RPC and SOAP
+- Automatically delete subscriptions? Point of reference: Dave's hub
+ implementation auto-deletes them after 25 hours. WordPress never deletes them.
+- Support additional feed URL addresses for the same feed (e.g.: by numeric ID,
+ ?user_id=xxx, etc.)
+- Support additional feeds that make sense (e.g: replies)?
+- Possibly use "rssCloud" (like Dave) instead of "RSSCloud" everywhere
diff --git a/plugins/RSSCloud/RSSCloudNotifier.php b/plugins/RSSCloud/RSSCloudNotifier.php
new file mode 100644
index 000000000..d454691c8
--- /dev/null
+++ b/plugins/RSSCloud/RSSCloudNotifier.php
@@ -0,0 +1,240 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Class to ping an rssCloud endpoint when a feed has been updated
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+ exit(1);
+}
+
+/**
+ * Class for notifying cloud-enabled RSS aggregators that StatusNet
+ * feeds have been updated.
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ **/
+class RSSCloudNotifier
+{
+ const MAX_FAILURES = 3;
+
+ /**
+ * Send an HTTP GET to the notification handler with a
+ * challenge string to see if it repsonds correctly.
+ *
+ * @param string $endpoint URL of the notification handler
+ * @param string $feed the feed being subscribed to
+ *
+ * @return boolean success
+ */
+ function challenge($endpoint, $feed)
+ {
+ $code = common_confirmation_code(128);
+ $params = array('url' => $feed, 'challenge' => $code);
+ $url = $endpoint . '?' . http_build_query($params);
+
+ try {
+ $client = new HTTPClient();
+ $response = $client->get($url);
+ } catch (HTTP_Request2_Exception $e) {
+ common_log(LOG_INFO,
+ 'RSSCloud plugin - failure testing notify handler ' .
+ $endpoint . ' - ' . $e->getMessage());
+ return false;
+ }
+
+ // Check response is betweet 200 and 299 and body contains challenge data
+
+ $status = $response->getStatus();
+ $body = $response->getBody();
+
+ if ($status >= 200 && $status < 300) {
+
+ // NOTE: the spec says that the body must contain the string
+ // challenge. It doesn't say that the body must contain the
+ // challenge string ONLY, although that seems to be the way
+ // the other implementors have interpreted it.
+
+ if (strpos($body, $code) !== false) {
+ common_log(LOG_INFO, 'RSSCloud plugin - ' .
+ "success testing notify handler: $endpoint");
+ return true;
+ } else {
+ common_log(LOG_INFO, 'RSSCloud plugin - ' .
+ 'challenge/repsonse failed for notify handler ' .
+ $endpoint);
+ common_debug('body = ' . var_export($body, true));
+ return false;
+ }
+ } else {
+ common_log(LOG_INFO, 'RSSCloud plugin - ' .
+ "failure testing notify handler: $endpoint " .
+ ' - got HTTP ' . $status);
+ common_debug('body = ' . var_export($body, true));
+ return false;
+ }
+ }
+
+ /**
+ * HTTP POST a notification that a feed has been updated
+ * ('ping the cloud').
+ *
+ * @param String $endpoint URL of the notification handler
+ * @param String $feed the feed being subscribed to
+ *
+ * @return boolean success
+ */
+ function postUpdate($endpoint, $feed)
+ {
+
+ $headers = array();
+ $postdata = array('url' => $feed);
+
+ try {
+ $client = new HTTPClient();
+ $response = $client->post($endpoint, $headers, $postdata);
+ } catch (HTTP_Request2_Exception $e) {
+ common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
+ $endpoint . ' that feed ' . $feed .
+ ' has changed: ' . $e->getMessage());
+ return false;
+ }
+
+ $status = $response->getStatus();
+
+ if ($status >= 200 && $status < 300) {
+ common_log(LOG_INFO, 'RSSCloud plugin - success notifying ' .
+ $endpoint . ' that feed ' . $feed . ' has changed.');
+ return true;
+ } else {
+ common_log(LOG_INFO, 'RSSCloud plugin - failure notifying ' .
+ $endpoint . ' that feed ' . $feed .
+ ' has changed: got HTTP ' . $status);
+ return false;
+ }
+ }
+
+ /**
+ * Notify all subscribers to a profile feed that it has changed.
+ *
+ * @param Profile $profile the profile whose feed has been
+ * updated
+ *
+ * @return boolean success
+ */
+ function notify($profile)
+ {
+ $feed = common_path('api/statuses/user_timeline/') .
+ $profile->nickname . '.rss';
+
+ $cloudSub = new RSSCloudSubscription();
+
+ $cloudSub->subscribed = $profile->id;
+
+ if ($cloudSub->find()) {
+ while ($cloudSub->fetch()) {
+ $result = $this->postUpdate($cloudSub->url, $feed);
+ if ($result == false) {
+ $this->handleFailure($cloudSub);
+ }
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Handle problems posting cloud notifications. Increment the failure
+ * count, or delete the subscription if the maximum number of failures
+ * is exceeded.
+ *
+ * XXX: Redo with proper DB_DataObject methods once I figure out what
+ * what the problem is with pluginized DB_DataObjects. -Z
+ *
+ * @param RSSCloudSubscription $cloudSub the subscription in question
+ *
+ * @return boolean success
+ */
+ function handleFailure($cloudSub)
+ {
+ $failCnt = $cloudSub->failures + 1;
+
+ if ($failCnt == self::MAX_FAILURES) {
+
+ common_log(LOG_INFO,
+ 'Deleting RSSCloud subcription ' .
+ '(max failure count reached), profile: ' .
+ $cloudSub->subscribed .
+ ' handler: ' .
+ $cloudSub->url);
+
+ // XXX: WTF! ->delete() doesn't work. Clearly, there are some issues with
+ // the DB_DataObject, or my understanding of it. Have to drop into SQL.
+
+ // $result = $cloudSub->delete();
+
+ $qry = 'DELETE from rsscloud_subscription' .
+ ' WHERE subscribed = ' . $cloudSub->subscribed .
+ ' AND url = \'' . $cloudSub->url . '\'';
+
+ $result = $cloudSub->query($qry);
+
+ if (!$result) {
+ common_log_db_error($cloudSub, 'DELETE', __FILE__);
+ common_log(LOG_ERR, 'Could not delete RSSCloud subscription.');
+ }
+
+ } else {
+
+ common_debug('Updating failure count on RSSCloud subscription. ' .
+ $failCnt);
+
+ $failCnt = $cloudSub->failures + 1;
+
+ // XXX: ->update() not working either, gar!
+
+ $qry = 'UPDATE rsscloud_subscription' .
+ ' SET failures = ' . $failCnt .
+ ' WHERE subscribed = ' . $cloudSub->subscribed .
+ ' AND url = \'' . $cloudSub->url . '\'';
+
+ $result = $cloudSub->query($qry);
+
+ if (!$result) {
+ common_log_db_error($cloudsub, 'UPDATE', __FILE__);
+ common_log(LOG_ERR,
+ 'Could not update failure ' .
+ 'count on RSSCloud subscription');
+ }
+ }
+ }
+
+}
+
diff --git a/plugins/RSSCloud/RSSCloudPlugin.php b/plugins/RSSCloud/RSSCloudPlugin.php
new file mode 100644
index 000000000..2de162628
--- /dev/null
+++ b/plugins/RSSCloud/RSSCloudPlugin.php
@@ -0,0 +1,295 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Plugin to support RSSCloud
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+ exit(1);
+}
+
+define('RSSCLOUDPLUGIN_VERSION', '0.1');
+
+/**
+ * Plugin class for adding RSSCloud capabilities to StatusNet
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ **/
+
+class RSSCloudPlugin extends Plugin
+{
+ /**
+ * Our friend, the constructor
+ *
+ * @return void
+ */
+ function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Setup the info for the subscription handler. Allow overriding
+ * to point at another cloud hub (not currently used).
+ *
+ * @return void
+ */
+
+ function onInitializePlugin()
+ {
+ $this->domain = common_config('rsscloud', 'domain');
+ $this->port = common_config('rsscloud', 'port');
+ $this->path = common_config('rsscloud', 'path');
+ $this->funct = common_config('rsscloud', 'function');
+ $this->protocol = common_config('rsscloud', 'protocol');
+
+ // set defaults
+
+ $local_server = parse_url(common_path('main/rsscloud/request_notify'));
+
+ if (empty($this->domain)) {
+ $this->domain = $local_server['host'];
+ }
+
+ if (empty($this->port)) {
+ $this->port = '80';
+ }
+
+ if (empty($this->path)) {
+ $this->path = $local_server['path'];
+ }
+
+ if (empty($this->funct)) {
+ $this->funct = '';
+ }
+
+ if (empty($this->protocol)) {
+ $this->protocol = 'http-post';
+ }
+ }
+
+ /**
+ * Add RSSCloud-related paths to the router table
+ *
+ * Hook for RouterInitialized event.
+ *
+ * @param Mapper &$m URL parser and mapper
+ *
+ * @return boolean hook return
+ */
+
+ function onRouterInitialized(&$m)
+ {
+ $m->connect('/main/rsscloud/request_notify',
+ array('action' => 'RSSCloudRequestNotify'));
+
+ // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
+ // to be a cloud hub for some reason.
+ //$m->connect('/main/rsscloud/notify',
+ // array('action' => 'LoggingAggregator'));
+
+ return true;
+ }
+
+ /**
+ * Automatically load the actions and libraries used by
+ * the RSSCloud plugin
+ *
+ * @param Class $cls the class
+ *
+ * @return boolean hook return
+ *
+ */
+
+ function onAutoload($cls)
+ {
+ switch ($cls)
+ {
+ case 'RSSCloudSubscription':
+ include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php';
+ return false;
+ case 'RSSCloudNotifier':
+ include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php';
+ return false;
+ case 'RSSCloudRequestNotifyAction':
+ case 'LoggingAggregatorAction':
+ include_once INSTALLDIR . '/plugins/RSSCloud/' .
+ mb_substr($cls, 0, -6) . '.php';
+ return false;
+ default:
+ return true;
+ }
+ }
+
+ /**
+ * Add a <cloud> element to the RSS feed (after the rss <channel>
+ * element is started).
+ *
+ * @param Action $action the ApiAction
+ *
+ * @return void
+ */
+
+ function onStartApiRss($action)
+ {
+ if (get_class($action) == 'ApiTimelineUserAction') {
+
+ $attrs = array('domain' => $this->domain,
+ 'port' => $this->port,
+ 'path' => $this->path,
+ 'registerProcedure' => $this->funct,
+ 'protocol' => $this->protocol);
+
+ // Dipping into XMLWriter to avoid a full end element (</cloud>).
+
+ $action->xw->startElement('cloud');
+ foreach ($attrs as $name => $value) {
+ $action->xw->writeAttribute($name, $value);
+ }
+
+ $action->xw->endElement();
+ }
+ }
+
+ /**
+ * Add an RSSCloud queue item for each notice
+ *
+ * @param Notice $notice the notice
+ * @param array &$transports the list of transports (queues)
+ *
+ * @return boolean hook return
+ */
+
+ function onStartEnqueueNotice($notice, &$transports)
+ {
+ array_push($transports, 'rsscloud');
+ return true;
+ }
+
+ /**
+ * broadcast the message when not using queuehandler
+ *
+ * @param Notice &$notice the notice
+ * @param array $queue destination queue
+ *
+ * @return boolean hook return
+ */
+
+ function onUnqueueHandleNotice(&$notice, $queue)
+ {
+ if (($queue == 'rsscloud') && ($this->_isLocal($notice))) {
+
+ common_debug('broadcasting rssCloud bound notice ' . $notice->id);
+
+ $profile = $notice->getProfile();
+
+ $notifier = new RSSCloudNotifier();
+ $notifier->notify($profile);
+
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Determine whether the notice was locally created
+ *
+ * @param Notice $notice the notice in question
+ *
+ * @return boolean locality
+ */
+
+ function _isLocal($notice)
+ {
+ return ($notice->is_local == Notice::LOCAL_PUBLIC ||
+ $notice->is_local == Notice::LOCAL_NONPUBLIC);
+ }
+
+ /**
+ * Create the rsscloud_subscription table if it's not
+ * already in the DB
+ *
+ * @return boolean hook return
+ */
+
+ function onCheckSchema()
+ {
+ $schema = Schema::get();
+ $schema->ensureTable('rsscloud_subscription',
+ array(new ColumnDef('subscribed', 'integer',
+ null, false, 'PRI'),
+ new ColumnDef('url', 'varchar',
+ '255', false, 'PRI'),
+ new ColumnDef('failures', 'integer',
+ null, false, null, 0),
+ new ColumnDef('created', 'datetime',
+ null, false),
+ new ColumnDef('modified', 'timestamp',
+ null, false, null,
+ 'CURRENT_TIMESTAMP',
+ 'on update CURRENT_TIMESTAMP')
+ ));
+ return true;
+ }
+
+ /**
+ * Add RSSCloudQueueHandler to the list of valid daemons to
+ * start
+ *
+ * @param array $daemons the list of daemons to run
+ *
+ * @return boolean hook return
+ *
+ */
+
+ function onGetValidDaemons($daemons)
+ {
+ array_push($daemons, INSTALLDIR .
+ '/plugins/RSSCloud/RSSCloudQueueHandler.php');
+ return true;
+ }
+
+ function onPluginVersion(&$versions)
+ {
+ $versions[] = array('name' => 'RSSCloud',
+ 'version' => RSSCLOUDPLUGIN_VERSION,
+ 'author' => 'Zach Copley',
+ 'homepage' => 'http://status.net/wiki/Plugin:RSSCloud',
+ 'rawdescription' =>
+ _m('The RSSCloud plugin enables your StatusNet instance to publish ' .
+ 'real-time updates for profile RSS feeds using the ' .
+ '<a href="http://rsscloud.org/">RSSCloud protocol</a>".'));
+
+ return true;
+ }
+
+}
+
diff --git a/plugins/RSSCloud/RSSCloudQueueHandler.php b/plugins/RSSCloud/RSSCloudQueueHandler.php
new file mode 100755
index 000000000..693dd27c1
--- /dev/null
+++ b/plugins/RSSCloud/RSSCloudQueueHandler.php
@@ -0,0 +1,78 @@
+#!/usr/bin/env php
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
+
+$shortoptions = 'i::';
+$longoptions = array('id::');
+
+$helptext = <<<END_OF_ENJIT_HELP
+Daemon script for pushing new notices to RSSCloud subscribers.
+
+ -i --id Identity (default none)
+
+END_OF_ENJIT_HELP;
+
+require_once INSTALLDIR . '/scripts/commandline.inc';
+require_once INSTALLDIR . '/lib/queuehandler.php';
+require_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php';
+require_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php';
+
+class RSSCloudQueueHandler extends QueueHandler
+{
+ var $notifier = null;
+
+ function transport()
+ {
+ return 'rsscloud';
+ }
+
+ function start()
+ {
+ $this->log(LOG_INFO, "INITIALIZE");
+ $this->notifier = new RSSCloudNotifier();
+ return true;
+ }
+
+ function handle_notice($notice)
+ {
+ $profile = $notice->getProfile();
+ return $this->notifier->notify($profile);
+ }
+
+ function finish()
+ {
+ }
+
+}
+
+if (have_option('i')) {
+ $id = get_option_value('i');
+} else if (have_option('--id')) {
+ $id = get_option_value('--id');
+} else if (count($args) > 0) {
+ $id = $args[0];
+} else {
+ $id = null;
+}
+
+$handler = new RSSCloudQueueHandler($id);
+
+$handler->runOnce();
diff --git a/plugins/RSSCloud/RSSCloudRequestNotify.php b/plugins/RSSCloud/RSSCloudRequestNotify.php
new file mode 100644
index 000000000..d76c08d37
--- /dev/null
+++ b/plugins/RSSCloud/RSSCloudRequestNotify.php
@@ -0,0 +1,347 @@
+<?php
+/**
+ * Action to let RSSCloud aggregators request update notification when
+ * user profile feeds change.
+ *
+ * PHP version 5
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
+ * @link http://status.net/
+ *
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('STATUSNET')) {
+ exit(1);
+}
+
+/**
+ * Action class to handle RSSCloud notification (subscription) requests
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Zach Copley <zach@status.net>
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ **/
+
+class RSSCloudRequestNotifyAction extends Action
+{
+ /**
+ * Initialization.
+ *
+ * @param array $args Web and URL arguments
+ *
+ * @return boolean false if user doesn't exist
+ */
+
+ function prepare($args)
+ {
+ parent::prepare($args);
+
+ $this->ip = $_SERVER['REMOTE_ADDR'];
+ $this->port = $this->arg('port');
+ $this->path = $this->arg('path');
+
+ if ($this->path[0] != '/') {
+ $this->path = '/' . $this->path;
+ }
+
+ $this->protocol = $this->arg('protocol');
+ $this->procedure = $this->arg('notifyProcedure');
+ $this->domain = $this->arg('domain');
+
+ $this->feeds = $this->getFeeds();
+
+ return true;
+ }
+
+ /**
+ * Handle the request
+ *
+ * Checks for all the required parameters for a subscription,
+ * validates that the feed being subscribed to is real, and then
+ * saves the subsctiption.
+ *
+ * @param array $args $_REQUEST data (unused)
+ *
+ * @return void
+ */
+
+ function handle($args)
+ {
+ parent::handle($args);
+
+ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
+ $this->showResult(false, 'Request must be POST.');
+ return;
+ }
+
+ $missing = array();
+
+ if (empty($this->port)) {
+ $missing[] = 'port';
+ }
+
+ if (empty($this->path)) {
+ $missing[] = 'path';
+ }
+
+ if (empty($this->protocol)) {
+ $missing[] = 'protocol';
+ } else if (strtolower($this->protocol) != 'http-post') {
+ $msg = 'Only http-post notifications are supported at this time.';
+ $this->showResult(false, $msg);
+ return;
+ }
+
+ if (!isset($this->procedure)) {
+ $missing[] = 'notifyProcedure';
+ }
+
+ if (!empty($missing)) {
+ $msg = 'The following parameters were missing from the request body: ' .
+ implode(', ', $missing) . '.';
+ $this->showResult(false, $msg);
+ return;
+ }
+
+ if (empty($this->feeds)) {
+ $msg = 'You must provide at least one valid profile feed url ' .
+ '(url1, url2, url3 ... urlN).';
+ $this->showResult(false, $msg);
+ return;
+ }
+
+ // We have to validate everything before saving anything.
+ // We only return one success or failure no matter how
+ // many feeds the subscriber is trying to subscribe to
+
+ foreach ($this->feeds as $feed) {
+
+ if (!$this->validateFeed($feed)) {
+
+ $nh = $this->getNotifyUrl();
+ common_log(LOG_WARNING,
+ "RSSCloud plugin - $nh tried to subscribe to invalid feed: $feed");
+
+ $msg = 'Feed subscription failed - Not a valid feed.';
+ $this->showResult(false, $msg);
+ return;
+ }
+
+ if (!$this->testNotificationHandler($feed)) {
+ $msg = 'Feed subscription failed - ' .
+ 'notification handler doesn\'t respond correctly.';
+ $this->showResult(false, $msg);
+ return;
+ }
+
+ }
+
+ foreach ($this->feeds as $feed) {
+ $this->saveSubscription($feed);
+ }
+
+ // XXX: What to do about deleting stale subscriptions?
+ // 25 hours seems harsh. WordPress doesn't ever remove
+ // subscriptions.
+
+ $msg = 'Thanks for the subscription. ' .
+ 'When the feed(s) update(s) we\'ll notify you.';
+
+ $this->showResult(true, $msg);
+ }
+
+ /**
+ * Validate that the requested feed is one we serve
+ * up via RSSCloud.
+ *
+ * @param string $feed the feed in question
+ *
+ * @return void
+ */
+
+ function validateFeed($feed)
+ {
+ $user = $this->userFromFeed($feed);
+
+ if (empty($user)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Pull all of the urls (url1, url2, url3...urlN) that
+ * the subscriber wants to subscribe to.
+ *
+ * @return array $feeds the list of feeds
+ */
+
+ function getFeeds()
+ {
+ $feeds = array();
+
+ while (list($key, $feed) = each($this->args)) {
+ if (preg_match('/^url\d*$/', $key)) {
+ $feeds[] = $feed;
+ }
+ }
+
+ return $feeds;
+ }
+
+ /**
+ * Test that a notification handler is there and is reponding
+ * correctly. This is called before adding a subscription.
+ *
+ * @param string $feed the feed to verify
+ *
+ * @return boolean success result
+ */
+
+ function testNotificationHandler($feed)
+ {
+ $notifyUrl = $this->getNotifyUrl();
+
+ $notifier = new RSSCloudNotifier();
+
+ if (isset($this->domain)) {
+
+ // 'domain' param set, so we have to use GET and send a challenge
+
+ common_log(LOG_INFO,
+ 'RSSCloud plugin - Testing notification handler with challenge: ' .
+ $notifyUrl);
+ return $notifier->challenge($notifyUrl, $feed);
+
+ } else {
+ common_log(LOG_INFO, 'RSSCloud plugin - Testing notification handler: ' .
+ $notifyUrl);
+
+ return $notifier->postUpdate($notifyUrl, $feed);
+ }
+ }
+
+ /**
+ * Build the URL for the notification handler based on the
+ * parameters passed in with the subscription request.
+ *
+ * @return string notification handler url
+ */
+
+ function getNotifyUrl()
+ {
+ if (isset($this->domain)) {
+ return 'http://' . $this->domain . ':' . $this->port . $this->path;
+ } else {
+ return 'http://' . $this->ip . ':' . $this->port . $this->path;
+ }
+ }
+
+ /**
+ * Uses the nickname part of the subscribed feed URL to figure out
+ * whethere there's really a user with such a feed. Used to
+ * validate feeds before adding a subscription.
+ *
+ * @param string $feed the feed in question
+ *
+ * @return boolean success
+ */
+
+ function userFromFeed($feed)
+ {
+ // We only do profile feeds
+
+ $path = common_path('api/statuses/user_timeline/');
+ $valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
+
+ if (preg_match($valid, $feed, $matches)) {
+ $user = User::staticGet('nickname', $matches['nickname']);
+ if (!empty($user)) {
+ return $user;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Save an RSSCloud subscription
+ *
+ * @param string $feed a valid profile feed
+ *
+ * @return boolean success result
+ */
+
+ function saveSubscription($feed)
+ {
+ $user = $this->userFromFeed($feed);
+
+ $notifyUrl = $this->getNotifyUrl();
+
+ $sub = RSSCloudSubscription::getSubscription($user->id, $notifyUrl);
+
+ if ($sub) {
+ common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl refreshed subscription" .
+ " to user $user->nickname (id: $user->id).");
+ } else {
+
+ $sub = new RSSCloudSubscription();
+
+ $sub->subscribed = $user->id;
+ $sub->url = $notifyUrl;
+ $sub->created = common_sql_now();
+
+ if (!$sub->insert()) {
+ common_log_db_error($sub, 'INSERT', __FILE__);
+ return false;
+ }
+
+ common_log(LOG_INFO, "RSSCloud plugin - $notifyUrl subscribed" .
+ " to user $user->nickname (id: $user->id)");
+ }
+
+ return true;
+ }
+
+ /**
+ * Show an XML message indicating the subscription
+ * was successful or failed.
+ *
+ * @param boolean $success whether it was good or bad
+ * @param string $msg the message to output
+ *
+ * @return boolean success result
+ */
+
+ function showResult($success, $msg)
+ {
+ $this->startXML();
+ $this->elementStart('notifyResult',
+ array('success' => ($success) ? 'true' : 'false',
+ 'msg' => $msg));
+ $this->endXML();
+ }
+
+}
+
diff --git a/plugins/RSSCloud/RSSCloudSubscription.php b/plugins/RSSCloud/RSSCloudSubscription.php
new file mode 100644
index 000000000..396c604e7
--- /dev/null
+++ b/plugins/RSSCloud/RSSCloudSubscription.php
@@ -0,0 +1,79 @@
+<?php
+/*
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('STATUSNET') && !defined('LACONICA')) {
+ exit(1);
+}
+
+/**
+ * Table Definition for rsscloud_subscription
+ */
+
+require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
+
+class RSSCloudSubscription extends Memcached_DataObject {
+
+ var $__table='rsscloud_subscription'; // table name
+ var $subscribed; // int primary key user id
+ var $url; // string primary key
+ var $failures; // int
+ var $created; // datestamp()
+ var $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
+
+ function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('DataObjects_Grp',$k,$v); }
+
+ function table()
+ {
+
+ $db = $this->getDatabaseConnection();
+ $dbtype = $db->phptype;
+
+ $cols = array('subscribed' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
+ 'url' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
+ 'failures' => DB_DATAOBJECT_INT,
+ 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
+ 'modified' => ($dbtype == 'mysql' || $dbtype == 'mysqli') ?
+ DB_DATAOBJECT_MYSQLTIMESTAMP + DB_DATAOBJECT_NOTNULL :
+ DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME
+ );
+
+ return $cols;
+ }
+
+ function keys()
+ {
+ return array('subscribed' => 'N', 'url' => 'N');
+ }
+
+ static function getSubscription($subscribed, $url)
+ {
+ $sub = new RSSCloudSubscription();
+ $sub->whereAdd("subscribed = $subscribed");
+ $sub->whereAdd("url = '$url'");
+ $sub->limit(1);
+
+ if ($sub->find()) {
+ $sub->fetch();
+ return $sub;
+ }
+
+ return false;
+ }
+
+}