fix xml problems master
authorChristian Weiske <cweiske@cweiske.de>
Fri, 13 Mar 2020 20:32:52 +0000 (21:32 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 13 Mar 2020 20:32:52 +0000 (21:32 +0100)
README.rst
www/add-article.php
www/feed.php [new file with mode: 0644]
www/functions.php [new file with mode: 0644]
www/index.php

index 143ea49b2b92f34dad465f402de59b2d4d990f55..47d5a342852a17f8dc350f7ef0241b923063e075 100644 (file)
@@ -1,6 +1,6 @@
-*******************
-PubSubHubbub tester
-*******************
+*************
+WebSub 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.
index 6e9a03850491ff129354adbb8e3cb9d5911f98f2..3b46ef5e8a3f560cc58de1b95a228368f40becba 100644 (file)
@@ -6,7 +6,7 @@ $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`;
+$content = htmlspecialchars(`/usr/games/fortune`);
 
 file_put_contents(
     $file,
@@ -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 (file)
index 0000000..e3e1ac0
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+require_once __DIR__ . '/../data/config.php';
+require_once __DIR__ . '/functions.php';
+header('Content-type: application/atom+xml');
+header('Link: <' . $hub . '>; rel="hub"');
+header('Link: <' . $self . 'feed.php>; rel="self"', false);
+
+$articles = loadArticles();
+reset($articles);
+$lastUpdate = key($articles);
+?>
+<?xml version="1.0" encoding="utf-8" ?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+ <title>WebSub tester</title>
+ <author>
+  <name>Someone</name>
+  <email>someone@example.org</email>
+ </author>
+ <link href="<?php echo $self; ?>"/>
+ <link rel="self" href="<?php echo $self; ?>feed.php"/>
+ <link rel="hub" href="<?php echo $hub; ?>"/>
+ <link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-nc-sa/3.0/" />
+ <id><?php echo $self; ?>feed.php</id>
+ <updated><?php echo date('c', $lastUpdate); ?></updated>
+
+ <?php foreach ($articles as $article) { ?>
+ <entry>
+  <id><?php echo $article->file; ?></id>
+  <title><?php echo htmlspecialchars($article->title); ?></title>
+  <published><?php echo date('c', $article->time); ?></published>
+  <updated><?php echo date('c', $article->time); ?></updated>
+  <summary><?php echo substr($article->content, 0, 100); ?>&#8230;</summary>
+  <content type="xhtml">
+   <div xmlns="http://www.w3.org/1999/xhtml">
+    <?php echo $article->content; ?>
+   </div>
+  </content>
+  <link rel="alternate" type="text/html" href="<?php echo $self . $article->file; ?>" />
+ </entry>
+ <?php } ?>
+</feed>
diff --git a/www/functions.php b/www/functions.php
new file mode 100644 (file)
index 0000000..962012e
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+function loadArticles()
+{
+    $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 = array_slice($articles, 0, 10);
+    //FIXME: delete old ones
+    return $articles;
+}
+?>
index 0a205f80943041fc1f3540dd170fd3c00269f390..9be78bc1289bca6816b25953ec3b5683c287eb02 100644 (file)
@@ -1,30 +1,25 @@
 <?php
 require_once __DIR__ . '/../data/config.php';
+require_once __DIR__ . '/functions.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);
+$articles = loadArticles();
 ?>
 <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
-  <title>PubSubHubbub tester</title>
+  <title>WebSub tester</title>
+  <link rel="alternate" type="application/atom+xml" title="WebSub tester" href="feed.php" />
  </head>
  <body class="h-feed">
   <p>
    <a href="add-article.php">create new article</a>
+   | <a href="feed.php">atom feed</a>
+  </p>
+  <p>
+   This is a tool to test your <a href="https://www.w3.org/TR/websub/">WebSub</a>
+   subscriber.
+   With one click, a new post gets published and the hub gets notified.
   </p>
   <h1>Articles</h1>
   <?php foreach ($articles as $article) { ?>