wip pubsubhubbub
authorChristian Weiske <cweiske@cweiske.de>
Thu, 31 Mar 2016 18:46:01 +0000 (20:46 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 31 Mar 2016 18:46:01 +0000 (20:46 +0200)
data/config.php.dist
src/phinde/Subscriptions.php [new file with mode: 0644]
www/push-subscription.php [new file with mode: 0644]

index 4c6a14fe8962151708a7929e7740e8bf209501c7..7eb8ccfe71a2c070c1cf5acc51ad11bd395b3413 100644 (file)
@@ -4,6 +4,14 @@ $GLOBALS['phinde'] = array(
     'domains' => array(
         'www.example.org',
         'test.example.org'
-    )
+    ),
+    //list of URL beginnings that should be ignored
+    'blacklist' => array(
+        'http://bad.example.org/'
+    ),
+    //list of URLs that should be subscribed to with PubSubHubbub
+    'subscriptions' => array(
+        'http://www.example.org/feed',
+    ),
 );
 ?>
\ No newline at end of file
diff --git a/src/phinde/Subscriptions.php b/src/phinde/Subscriptions.php
new file mode 100644 (file)
index 0000000..9db4b16
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+namespace phinde;
+
+class Subscriptions
+{
+    public function get($url)
+    {
+        //FIXME
+        return false;
+    }
+}
+?>
diff --git a/www/push-subscription.php b/www/push-subscription.php
new file mode 100644 (file)
index 0000000..014f15d
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+namespace phinde;
+/**
+ * Handles PuSH subscription responses
+ */
+header('HTTP/1.0 500 Internal Server Error');
+require 'www-header.php';
+
+//PHP converts dots to underscore, so hub.mode becomes hub_mode
+if (!isset($_GET['hub_mode'])) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Parameter missing: hub.mode\n";
+    exit(1);
+}
+$hubMode = $_GET['hub_mode'];
+
+if (!isset($_GET['hub_topic'])) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Parameter missing: hub.topic\n";
+    exit(1);
+}
+if (!isValidUrl($_GET['hub_topic'])) {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Invalid parameter value for hub.topic: Invalid URL\n";
+    exit(1);
+}
+$hubTopic = $_GET['hub_topic'];
+
+$subDb = new Subscriptions();
+
+if ($hubMode == 'denied') {
+    //TODO: Inspect Location header to retry subscription
+    //TODO: remove subscription
+    return;
+} else if ($hubMode == 'subscribe') {
+    //FIXME
+    $pos = array_search($hubTopic, $GLOBALS['phinde']['subscriptions']);
+    if ($pos === false) {
+        //we do not want to subscribe
+        header('HTTP/1.0 404 Not Found');
+        echo "We are not interested in this hub.topic\n";
+        exit(1);
+    }
+    if (!isset($_GET['hub_challenge'])) {
+        header('HTTP/1.0 400 Bad Request');
+        echo "Parameter missing: hub.challenge\n";
+        exit(1);
+    }
+    $hubChallenge = $_GET['hub_challenge'];
+
+    if (!isset($_GET['hub_lease_seconds'])) {
+        header('HTTP/1.0 400 Bad Request');
+        echo "Parameter missing: hub.lease_seconds\n";
+        exit(1);
+    }
+    $hubLeaseSeconds = $_GET['hub_lease_seconds'];
+
+    //FIXME: store in database
+
+    header('HTTP/1.0 200 OK');
+    header('Content-type: text/plain');
+    echo $hubChallenge;
+    exit(0);
+
+} else if ($hubMode == 'unsubscribe') {
+    $sub = $subDb->get($hubTopic);
+    if ($sub === false) {
+        //we do not know this subscription
+        header('HTTP/1.0 404 Not Found');
+        echo "We are not subscribed to this hub.topic\n";
+        exit(1);
+    }
+    $pos = array_search($hubTopic, $GLOBALS['phinde']['subscriptions']);
+    if ($pos !== false) {
+        //we do not want to unsubscribe
+        header('HTTP/1.0 404 Not Found');
+        echo "We do not want to unsubscribe from this hub.topic\n";
+        exit(1);
+    }
+    $sub->remove($hubTopic);
+    header('HTTP/1.0 200 OK');
+    header('Content-type: text/plain');
+    echo "Unsubscribed.\n";
+    exit(0);
+} else {
+    header('HTTP/1.0 400 Bad Request');
+    echo "Invalid parameter value for hub.mode\n";
+    exit(1);
+}
+
+
+function isValidUrl($url)
+{
+    if (filter_var($url, FILTER_VALIDATE_URL) === false) {
+        return false;
+    }
+    if (substr($url, 0, 7) == 'http://'
+        || substr($url, 0, 8) == 'https://'
+    ) {
+        return true;
+    }
+    return false;
+}
+?>
\ No newline at end of file