add log class
[phinde.git] / www / push-subscription.php
1 <?php
2 namespace phinde;
3 /**
4  * Handles PuSH subscription responses
5  */
6 header('HTTP/1.0 500 Internal Server Error');
7 require 'www-header.php';
8
9 //PHP converts dots to underscore, so hub.mode becomes hub_mode
10 if (!isset($_GET['hub_mode'])) {
11     header('HTTP/1.0 400 Bad Request');
12     echo "Parameter missing: hub.mode\n";
13     exit(1);
14 }
15 $hubMode = $_GET['hub_mode'];
16
17 if (!isset($_GET['hub_topic'])) {
18     header('HTTP/1.0 400 Bad Request');
19     echo "Parameter missing: hub.topic\n";
20     exit(1);
21 }
22 if (!isValidUrl($_GET['hub_topic'])) {
23     header('HTTP/1.0 400 Bad Request');
24     echo "Invalid parameter value for hub.topic: Invalid URL\n";
25     exit(1);
26 }
27 $hubTopic = $_GET['hub_topic'];
28
29 $subDb = new Subscriptions();
30
31 if ($hubMode == 'denied') {
32     //TODO: Inspect Location header to retry subscription
33     //TODO: remove subscription
34     return;
35 } else if ($hubMode == 'subscribe') {
36     //FIXME
37     $pos = array_search($hubTopic, $GLOBALS['phinde']['subscriptions']);
38     if ($pos === false) {
39         //we do not want to subscribe
40         header('HTTP/1.0 404 Not Found');
41         echo "We are not interested in this hub.topic\n";
42         exit(1);
43     }
44     if (!isset($_GET['hub_challenge'])) {
45         header('HTTP/1.0 400 Bad Request');
46         echo "Parameter missing: hub.challenge\n";
47         exit(1);
48     }
49     $hubChallenge = $_GET['hub_challenge'];
50
51     if (!isset($_GET['hub_lease_seconds'])) {
52         header('HTTP/1.0 400 Bad Request');
53         echo "Parameter missing: hub.lease_seconds\n";
54         exit(1);
55     }
56     $hubLeaseSeconds = $_GET['hub_lease_seconds'];
57
58     //FIXME: store in database
59
60     header('HTTP/1.0 200 OK');
61     header('Content-type: text/plain');
62     echo $hubChallenge;
63     exit(0);
64
65 } else if ($hubMode == 'unsubscribe') {
66     $sub = $subDb->get($hubTopic);
67     if ($sub === false) {
68         //we do not know this subscription
69         header('HTTP/1.0 404 Not Found');
70         echo "We are not subscribed to this hub.topic\n";
71         exit(1);
72     }
73     $pos = array_search($hubTopic, $GLOBALS['phinde']['subscriptions']);
74     if ($pos !== false) {
75         //we do not want to unsubscribe
76         header('HTTP/1.0 404 Not Found');
77         echo "We do not want to unsubscribe from this hub.topic\n";
78         exit(1);
79     }
80     $sub->remove($hubTopic);
81     header('HTTP/1.0 200 OK');
82     header('Content-type: text/plain');
83     echo "Unsubscribed.\n";
84     exit(0);
85 } else {
86     header('HTTP/1.0 400 Bad Request');
87     echo "Invalid parameter value for hub.mode\n";
88     exit(1);
89 }
90
91
92 function isValidUrl($url)
93 {
94     if (filter_var($url, FILTER_VALIDATE_URL) === false) {
95         return false;
96     }
97     if (substr($url, 0, 7) == 'http://'
98         || substr($url, 0, 8) == 'https://'
99     ) {
100         return true;
101     }
102     return false;
103 }
104 ?>