Plugin now checks notify handlers before registering subscriptions

This commit is contained in:
Zach Copley 2009-12-07 09:10:04 +00:00
parent 4e07d9eeec
commit 61804bb7bb
4 changed files with 113 additions and 102 deletions

View File

@ -1,5 +1,4 @@
<?php
/**
* This test class pretends to be an RSS aggregator. It logs notifications
* from the cloud.
@ -78,8 +77,7 @@ class LoggingAggregatorAction extends Action
}
header('Content-Type: text/xml');
echo "<notifyResult success='true' msg='Thanks for the update.' challenge='" .
$this->challenge . "' />\n";
echo $this->challenge;
} else {
@ -92,7 +90,6 @@ class LoggingAggregatorAction extends Action
header('Content-Type: text/xml');
echo '<notifyResult success=\'true\' msg=\'Thanks for the update.\' />' . "\n";
}
$this->ip = $_SERVER['REMOTE_ADDR'];

View File

@ -33,59 +33,77 @@ if (!defined('STATUSNET')) {
class RSSCloudNotifier {
function postUpdate($endpoint, $feed) {
common_debug("CloudNotifier->notify: $feed");
$params = 'url=' . urlencode($feed);
$result = $this->httpPost($endpoint, $params);
// XXX: Make all this use CurlClient (lib/curlclient.php)
if ($result) {
common_debug('RSSCloud plugin - success notifying cloud endpoint!');
} else {
common_debug('RSSClous plugin - failure notifying cloud endpoint!');
}
return $result;
}
function userAgent()
function challenge($endpoint, $feed)
{
return 'rssCloudPlugin/' . RSSCLOUDPLUGIN_VERSION .
' StatusNet/' . STATUSNET_VERSION;
$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;
}
private function httpPost($url, $params) {
// Check response is betweet 200 and 299 and body contains challenge data
$options = array(CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_USERAGENT => $this->userAgent(),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FAILONERROR => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5);
$status = $response->getStatus();
$body = $response->getBody();
$ch = curl_init();
curl_setopt_array($ch, $options);
if ($status >= 200 && $status < 300) {
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
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;
}
}
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);
common_debug('body = ' . var_export($response->getBody(), true));
return false;
}
}
}

View File

@ -112,6 +112,8 @@ class RSSCloudPlugin extends Plugin
function onStartApiRss($action)
{
// XXX: Add RSS 1.0 user feeds
if (get_class($action) == 'ApiTimelineUserAction') {
$attrs = array('domain' => $this->domain,

View File

@ -147,7 +147,6 @@ class RSSCloudRequestNotifyAction extends Action
return true;
}
function getFeeds()
{
$feeds = array();
@ -169,30 +168,33 @@ class RSSCloudRequestNotifyAction extends Action
if (isset($this->domain)) {
//get
// 'domain' param set, so we have to use GET and send a challenge
$this->url = 'http://' . $this->domain . ':' . $this->port . '/' . $this->path;
$endpoint = 'http://' . $this->domain . ':' . $this->port . '/' . $this->path;
common_debug('domain set need to send challenge');
common_log(LOG_INFO, 'Testing notification handler with challenge: ' .
$endpoint);
return $notifier->challenge($endpoint, $feed);
} else {
//post
$endpoint = 'http://' . $this->ip . ':' . $this->port . '/' . $this->path;
$this->url = 'http://' . $this->ip . ':' . $this->port . '/' . $this->path;
//return $notifier->postUpdate($endpoint, $feed);
common_log(LOG_INFO, 'Testing notification handler: ' .
$endpoint);
return $notifier->postUpdate($endpoint, $feed);
}
return true;
}
function userFromFeed($feed)
{
// We only do profile feeds
// XXX: Add cloud element to RSS 1.0 feeds
$path = common_path('api/statuses/user_timeline/');
$valid = '%^' . $path . '(?<nickname>.*)\.rss$%';
@ -210,19 +212,11 @@ class RSSCloudRequestNotifyAction extends Action
{
$user = $this->userFromFeed($feed);
common_debug('user = ' . $user->id);
$sub = RSSCloudSubscription::getSubscription($user->id, $this->url);
if ($sub) {
common_debug("already subscribed to that!");
common_debug("Already subscribed to that!");
} else {
common_debug('No feed for user ' . $user->id . ' notify: ' . $this->url);
}
common_debug('RSSPlugin - saveSubscription');
// turn debugging high
DB_DataObject::debugLevel(5);
$sub = new RSSCloudSubscription();
@ -232,13 +226,15 @@ class RSSCloudRequestNotifyAction extends Action
// auto timestamp doesn't seem to work for me
$sub->modified = common_sql_now();
// $sub->modified = common_sql_now();
if (!$sub->insert()) {
common_log_db_error($sub, 'INSERT', __FILE__);
return false;
}
DB_DataObject::debugLevel();
}
return true;
}
@ -253,5 +249,3 @@ class RSSCloudRequestNotifyAction extends Action
}