Add colors to status page to make it easy to spot problems
[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_once 'www-header.php';
8
9 //PHP converts dots to underscore, so hub.topic becomes hub_topic
10 if (!isset($_GET['hub_topic'])) {
11     err('Parameter missing: hub.topic', '400 Bad Request');
12 }
13 if (!isValidUrl($_GET['hub_topic'])) {
14     err(
15         'Invalid parameter value for hub.topic: Invalid URL',
16         '400 Bad Request'
17     );
18 }
19 $hubTopic = $_GET['hub_topic'];
20
21 $subDb = new Subscriptions();
22 $sub   = $subDb->get($hubTopic);
23 if ($sub === false) {
24     //we do not have this topic in our database
25     err('We know nothing about this hub.topic', '404 Not Found');
26 }
27
28 //capability key verification so third parties can't forge requests
29 // see https://www.w3.org/TR/capability-urls/
30 if (!isset($_GET['capkey'])) {
31     err('Parameter missing: capkey', '400 Bad Request');
32 }
33 if ($sub->sub_capkey !== $_GET['capkey']) {
34     err('Invalid parameter value for capkey', '400 Bad Request');
35 }
36
37 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
38     $queue = new Queue();
39     $queue->addToProcessList($hubTopic, ['index', 'crawl']);
40     $subDb->pinged($sub->sub_id);
41     header('HTTP/1.0 200 OK');
42     echo "URL queued.\n";
43     exit();
44 }
45
46 if (!isset($_GET['hub_mode'])) {
47     err('Parameter missing: hub.mode', '400 Bad Request');
48 }
49 $hubMode = $_GET['hub_mode'];
50
51 if ($hubMode == 'subscribe') {
52     if (!isset($_GET['hub_challenge'])) {
53         err('Parameter missing: hub.challenge', '400 Bad Request');
54     }
55     $hubChallenge = $_GET['hub_challenge'];
56
57     if (!isset($_GET['hub_lease_seconds'])) {
58         err('Parameter missing: hub.lease_seconds', '400 Bad Request');
59     }
60     if (!is_numeric($_GET['hub_lease_seconds'])) {
61         err('Invalid value for hub.lease_seconds', '400 Bad Request');
62     }
63     $hubLeaseSeconds = intval($_GET['hub_lease_seconds']);
64
65     $subDb->subscribed($sub->sub_id, $hubLeaseSeconds);
66
67     header('HTTP/1.0 200 OK');
68     header('Content-type: text/plain');
69     echo $hubChallenge;
70     exit();
71
72 } else if ($hubMode == 'unsubscribe') {
73     if ($sub->sub_status != 'unsubscribing') {
74         //we do not want to unsubscribe
75         err(
76             'We do not want to unsubscribe from this hub.topic',
77             '404 Not Found'
78         );
79     }
80     if (!isset($_GET['hub_challenge'])) {
81         err('Parameter missing: hub.challenge', '400 Bad Request');
82     }
83     $hubChallenge = $_GET['hub_challenge'];
84
85     $subDb->unsubscribed($sub->sub_id);
86
87     header('HTTP/1.0 200 OK');
88     header('Content-type: text/plain');
89     echo $hubChallenge;
90     exit();
91
92 } else if ($hubMode == 'denied') {
93     //TODO: Inspect Location header to retry subscription (still valid?)
94     $reason = '';
95     if (isset($_GET['hub_reason'])) {
96         $reason = $_GET['hub_reason'];
97     }
98     $subDb->denied($sub->sub_id, $reason);
99     exit();
100
101 } else {
102     err('Invalid parameter value for hub.mode', '400 Bad Request');
103 }
104
105
106 function isValidUrl($url)
107 {
108     if (filter_var($url, FILTER_VALIDATE_URL) === false) {
109         return false;
110     }
111     if (substr($url, 0, 7) == 'http://'
112         || substr($url, 0, 8) == 'https://'
113     ) {
114         return true;
115     }
116     return false;
117 }
118
119 function err($msg, $statusline)
120 {
121     header('HTTP/1.0 ' . $statusline);
122     echo $msg . "\n";
123     exit(1);
124 }
125 ?>