From b0f379dfff8738defba307341887ee5938625f55 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 1 Apr 2011 09:36:29 +0400 Subject: [PATCH] add experimental support for pubsubhubbub in published feed, bump config version (refs #251) --- config.php-dist | 6 ++- functions.php | 19 ++++++-- lib/pubsubhubbub/README.txt | 21 +++++++++ lib/pubsubhubbub/publisher.php | 86 ++++++++++++++++++++++++++++++++++ modules/backend-rpc.php | 15 +++++- sanity_check.php | 2 +- sanity_config.php | 6 +-- 7 files changed, 144 insertions(+), 11 deletions(-) create mode 100644 lib/pubsubhubbub/README.txt create mode 100644 lib/pubsubhubbub/publisher.php diff --git a/config.php-dist b/config.php-dist index 531ce20b3..87429d136 100644 --- a/config.php-dist +++ b/config.php-dist @@ -192,7 +192,11 @@ // Please set this to true if you have read everything above and // finished setting configuration options. - define('CONFIG_VERSION', 21); + define('PUBSUBHUBBUB_HUB', ''); + // URL to a PubSubHubbub-compatible hub server. If defined, Published + // articles generated feeds would automatically become PUSH-enabled. + + define('CONFIG_VERSION', 22); // Expected config version. Please update this option in config.php // if necessary (after migrating all new options from this file). diff --git a/functions.php b/functions.php index d33b30454..1e843dd14 100644 --- a/functions.php +++ b/functions.php @@ -110,6 +110,7 @@ require_once "lib/magpierss/rss_fetch.inc"; require_once 'lib/magpierss/rss_utils.inc'; require_once 'lib/htmlpurifier/library/HTMLPurifier.auto.php'; + require_once 'lib/pubsubhubbub/publisher.php'; $config = HTMLPurifier_Config::createDefault(); @@ -3583,13 +3584,19 @@ $feed_site_url = $qfh_ret[2]; $last_error = $qfh_ret[3]; -// if (!$feed_site_url) $feed_site_url = "http://localhost/"; + if (!$feed_site_url) $feed_site_url = get_self_url_prefix(); print " - - - $feed_title + + "; + + if (PUBSUBHUBBUB_HUB && $feed == -2) { + print ""; + } + + print "$feed_title $feed_site_url Feed generated by Tiny Tiny RSS"; @@ -4100,8 +4107,10 @@ $reply .= ""; + if ($is_cat) $cat_q = "&is_cat=$is_cat"; + $rss_link = htmlspecialchars(get_self_url_prefix() . - "/backend.php?op=rss&id=$feed_id&is_cat=$is_cat&view_mode=$view_mode$search_q"); + "/backend.php?op=rss&id=$feed_id$cat_q$search_q"); $reply .= ""; diff --git a/lib/pubsubhubbub/README.txt b/lib/pubsubhubbub/README.txt new file mode 100644 index 000000000..3d27c4044 --- /dev/null +++ b/lib/pubsubhubbub/README.txt @@ -0,0 +1,21 @@ +This PHP library for PubSubHubbub was written by Josh Fraser (joshfraser.com) and is released under the Apache 2.0 License + +Usage: +// specify which hub you want to use. in this case we'll use the demo hub on app engine. +$hub_url = "http://pubsubhubbub.appspot.com/"; + +// create a new pubsubhubbub publisher +$p = new Publisher($hub_url); + +// specify the feed that has been updated +$topic_url = "http://www.onlineaspect.com"; + +// notify the hub that the specified topic_url (ATOM feed) has been updated +// alternatively, publish_update() also accepts an array of topic urls +if ($p->publish_update($topic_url)) { +    echo "$topic_url was successfully published to $hub_url"; +} else { +    echo "Ooops..."; +    print_r($p->last_response()); +} + \ No newline at end of file diff --git a/lib/pubsubhubbub/publisher.php b/lib/pubsubhubbub/publisher.php new file mode 100644 index 000000000..f176a9b8a --- /dev/null +++ b/lib/pubsubhubbub/publisher.php @@ -0,0 +1,86 @@ +hub_url = $hub_url; + } + + // accepts either a single url or an array of urls + public function publish_update($topic_urls, $http_function = false) { + if (!isset($topic_urls)) + throw new Exception('Please specify a topic url'); + + // check that we're working with an array + if (!is_array($topic_urls)) { + $topic_urls = array($topic_urls); + } + + // set the mode to publish + $post_string = "hub.mode=publish"; + // loop through each topic url + foreach ($topic_urls as $topic_url) { + + // lightweight check that we're actually working w/ a valid url + if (!preg_match("|^https?://|i",$topic_url)) + throw new Exception('The specified topic url does not appear to be valid: '.$topic_url); + + // append the topic url parameters + $post_string .= "&hub.url=".urlencode($topic_url); + } + + // make the http post request and return true/false + // easy to over-write to use your own http function + if ($http_function) + return $http_function($this->hub_url,$post_string); + else + return $this->http_post($this->hub_url,$post_string); + } + + // returns any error message from the latest request + public function last_response() { + return $this->last_response; + } + + // default http function that uses curl to post to the hub endpoint + private function http_post($url, $post_string) { + + // add any additional curl options here + $options = array(CURLOPT_URL => $url, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $post_string, + CURLOPT_USERAGENT => "PubSubHubbub-Publisher-PHP/1.0"); + + $ch = curl_init(); + curl_setopt_array($ch, $options); + + $response = curl_exec($ch); + $this->last_response = $response; + $info = curl_getinfo($ch); + + curl_close($ch); + + // all good + if ($info['http_code'] == 204) + return true; + return false; + } +} + +?> \ No newline at end of file diff --git a/modules/backend-rpc.php b/modules/backend-rpc.php index 1810d585e..abb04ab46 100644 --- a/modules/backend-rpc.php +++ b/modules/backend-rpc.php @@ -211,7 +211,20 @@ published = $pub WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); - print json_encode(array("message" => "UPDATE_COUNTERS")); + $pubsub_result = false; + + if (PUBSUBHUBBUB_HUB) { + $rss_link = get_self_url_prefix() . + "/backend.php?op=rss&id=-2&key=" . + get_feed_access_key($link, -2, false); + + $p = new Publisher(PUBSUBHUBBUB_HUB); + + $pubsub_result = $p->publish_update($rss_link); + } + + print json_encode(array("message" => "UPDATE_COUNTERS", + "pubsub_result" => $pubsub_result)); return; } diff --git a/sanity_check.php b/sanity_check.php index 3b9de3478..4cb74259d 100644 --- a/sanity_check.php +++ b/sanity_check.php @@ -1,7 +1,7 @@ +