From ac19684fed897e49dc20cea4614e908308c06946 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 16 Apr 2015 18:20:41 +0200 Subject: [PATCH] atom feed support --- README.rst | 4 +-- www/add-article.php | 62 ++++++++++++++++++++++++--------------------- www/feed.php | 41 ++++++++++++++++++++++++++++++ www/functions.php | 22 ++++++++++++++++ www/index.php | 18 +++---------- 5 files changed, 102 insertions(+), 45 deletions(-) create mode 100644 www/feed.php create mode 100644 www/functions.php diff --git a/README.rst b/README.rst index 143ea49..8b73957 100644 --- a/README.rst +++ b/README.rst @@ -2,5 +2,5 @@ PubSubHubbub tester ******************* -Simulates a blog with an h-feed. -Sends notifications to the hub when a new article has been created. +Simulates a blog with an atom and an h-feed. +Sends a publish notification to the hub when a new article has been created. diff --git a/www/add-article.php b/www/add-article.php index 6e9a038..5c925cf 100644 --- a/www/add-article.php +++ b/www/add-article.php @@ -31,37 +31,41 @@ HTM //echo "saved as " . $file . "\n"; //hub-notification -$params = array( - 'hub.mode' => 'publish', - 'hub.url' => $self, -); -$enc = array(); -foreach ($params as $key => $val) { - $enc[] = urlencode($key) . '=' . urlencode($val); -} -$postMsg = implode('&', $enc); +$arUrls = array($self, $self . 'feed.php'); +foreach ($arUrls as $url) { + $params = array( + 'hub.mode' => 'publish', + 'hub.url' => $url, + ); + $enc = array(); + foreach ($params as $key => $val) { + $enc[] = urlencode($key) . '=' . urlencode($val); + } + $postMsg = implode('&', $enc); -$ctx = stream_context_create( - array( - 'http' => array( - 'method' => 'POST', - 'header' => array( - 'Content-type: application/x-www-form-urlencoded', - ), - 'content' => $postMsg, - 'ignore_errors' => true, + $ctx = stream_context_create( + array( + 'http' => array( + 'method' => 'POST', + 'header' => array( + 'Content-type: application/x-www-form-urlencoded', + ), + 'content' => $postMsg, + 'ignore_errors' => true, + ) ) - ) -); + ); -$res = file_get_contents($hub, false, $ctx); -list($http, $code, $rest) = explode(' ', $http_response_header[0]); -if (intval($code / 100) === 2) { - echo "notified hub\n"; - header('Location: /'); - exit(); + $res = file_get_contents($hub, false, $ctx); + list($http, $code, $rest) = explode(' ', $http_response_header[0]); + if (intval($code / 100) !== 2) { + echo "Error notifying hub: HTTP status was not 2xx; got $code\n"; + echo $res . "\n"; + echo 'URL: ' . $url . "\n"; + } else { + //echo "notified hub: $url\n"; + } } - -echo "Error notifying hub: HTTP status was not 2xx; got $code\n"; -echo $res . "\n"; +header('Location: /'); +exit(); ?> diff --git a/www/feed.php b/www/feed.php new file mode 100644 index 0000000..04bead9 --- /dev/null +++ b/www/feed.php @@ -0,0 +1,41 @@ +; rel="hub"'); +header('Link: <' . $self . 'feed.php>; rel="self"', false); + +$articles = loadArticles(); +reset($articles); +$lastUpdate = key($articles); +?> + + + PubSubHubbub tester + + Someone + someone@example.org + + + + + + feed.php + + + + + file; ?> + <?php echo htmlspecialchars($article->title); ?> + time); ?> + time); ?> + content, 0, 100); ?>… + +
+ content; ?> +
+
+ +
+ +
diff --git a/www/functions.php b/www/functions.php new file mode 100644 index 0000000..962012e --- /dev/null +++ b/www/functions.php @@ -0,0 +1,22 @@ + 'articles/' . basename($file), + 'title' => basename($file, '.htm'), + 'content' => (string) $xml->body->div, + 'time' => $timestamp, + ); + } + krsort($articles); + $articles = array_slice($articles, 0, 10); + //FIXME: delete old ones + return $articles; +} +?> diff --git a/www/index.php b/www/index.php index 0a205f8..6a98ad0 100644 --- a/www/index.php +++ b/www/index.php @@ -1,30 +1,20 @@ ; rel="hub"'); header('Link: <' . $self . '>; rel="self"', false); -$files = glob(__DIR__ . '/articles/*.htm'); -$articles = array(); -foreach ($files as $file) { - $content = file_get_contents($file); - $xml = simplexml_load_string($content); - $timestamp = strtotime(basename($file, '.htm')); - $articles[$timestamp] = (object) array( - 'file' => 'articles/' . basename($file), - 'title' => basename($file, '.htm'), - 'content' => (string) $xml->body->div, - 'time' => $timestamp, - ); -} -krsort($articles); +$articles = loadArticles(); ?> PubSubHubbub tester +

create new article + | atom feed

Articles

-- 2.30.2