aboutsummaryrefslogtreecommitdiff
path: root/www/push-subscription.php
blob: 014f15d8202272f7546a965efb44022a75d918a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
}
?>