push tester
authorChristian Weiske <cweiske@cweiske.de>
Thu, 26 Mar 2015 16:41:50 +0000 (17:41 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 26 Mar 2015 16:41:50 +0000 (17:41 +0100)
.gitignore [new file with mode: 0644]
README.rst [new file with mode: 0644]
data/config.php.dist [new file with mode: 0644]
www/add-article.php [new file with mode: 0644]
www/articles/.keep [new file with mode: 0644]
www/index.php [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..e989522
--- /dev/null
@@ -0,0 +1,2 @@
+data/config.php
+www/articles/*.htm
diff --git a/README.rst b/README.rst
new file mode 100644 (file)
index 0000000..143ea49
--- /dev/null
@@ -0,0 +1,6 @@
+*******************
+PubSubHubbub tester
+*******************
+
+Simulates a blog with an h-feed.
+Sends notifications to the hub when a new article has been created.
diff --git a/data/config.php.dist b/data/config.php.dist
new file mode 100644 (file)
index 0000000..97a2101
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+$hub = 'http://phubb.bogo/hub.php';
+$self = 'http://push-tester.bogo/';
+?>
diff --git a/www/add-article.php b/www/add-article.php
new file mode 100644 (file)
index 0000000..25db71e
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+header('Content-type: text/plain');
+require_once __DIR__ . '/../data/config.php';
+
+$time = time();
+$timestr = date('Y-m-d', $time) . 'T' . date('H:i:s', $time);
+$file = __DIR__ . '/articles/' . $timestr . '.htm';
+$title = $timestr;
+$content = `/usr/games/fortune`;
+
+file_put_contents(
+    $file,
+    <<<HTM
+<html>
+ <head>
+  <title>$title</title>
+ </head>
+ <body class="h-entry">
+  <h1>$title</h1>
+  <div class="e-content">$content</div>
+ </body>
+</html>
+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);
+
+$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";
+    exit();
+}
+
+echo "Error notifying hub: HTTP status was not 2xx; got $code\n";
+echo $res . "\n";
+?>
diff --git a/www/articles/.keep b/www/articles/.keep
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/www/index.php b/www/index.php
new file mode 100644 (file)
index 0000000..b4d0b2f
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+require_once __DIR__ . '/../data/config.php';
+header('Link: <' . $hub . '>; 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);
+?>
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+  <title>PubSubHubbub tester</title>
+ </head>
+ <body class="h-feed">
+  <p>
+   <a href="add-article.php">create new article</a>
+  </p>
+  <h1>Articles</h1>
+  <?php foreach ($articles as $article) { ?>
+   <article class="h-entry">
+    <h2><?php echo htmlspecialchars($article->title); ?></h2>
+    <div class="e-content">
+     <?php echo $article->content; ?>
+    </div>
+    <p>
+     <a href="<?php echo $article->file; ?>" class="u-url">permalink</a>
+      <time class="dt-published"><?php echo date('c', $article->time); ?></time>
+    </p>
+   </article>
+  <?php } ?>
+ </body>
+</html>