aboutsummaryrefslogtreecommitdiff
path: root/www/push-subscription.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2016-03-31 20:46:01 +0200
committerChristian Weiske <cweiske@cweiske.de>2016-03-31 20:46:01 +0200
commit087e616b4eed27573d5a00b725eca02c1584fa4f (patch)
treee42ee972875966a4f6082857f6026ef77ab86a35 /www/push-subscription.php
parenta23efd3bfd8de4bdf8085ea0e39e7abd4f84f516 (diff)
downloadphinde-087e616b4eed27573d5a00b725eca02c1584fa4f.tar.gz
phinde-087e616b4eed27573d5a00b725eca02c1584fa4f.zip
wip pubsubhubbub
Diffstat (limited to 'www/push-subscription.php')
-rw-r--r--www/push-subscription.php104
1 files changed, 104 insertions, 0 deletions
diff --git a/www/push-subscription.php b/www/push-subscription.php
new file mode 100644
index 0000000..014f15d
--- /dev/null
+++ b/www/push-subscription.php
@@ -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