-
Notifications
You must be signed in to change notification settings - Fork 15
/
websub.php
64 lines (53 loc) · 1.94 KB
/
websub.php
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
<?php
/*
* This file is part of FEED ON FEEDS - http://feedonfeeds.com/
*
* websub.php - WebSub endpoint
*
* Copyright (C) 2018 j. shagam
* [email protected] - http://beesbuzz.biz/
*
* Distributed under the GPL - see LICENSE
*
*/
$fof_no_login = true;
require_once 'fof-main.php';
list($pre, $feed_id, $secret) = explode('/', $_SERVER['PATH_INFO']);
if (!$feed_id) {
// No feed ID was specified
http_response_code(400);
die("Malformed request");
}
$feed = fof_db_get_feed_by_id($feed_id);
if (!$feed || !$feed['feed_websub_hub']) {
// The feed doesn't exist, or doesn't have a known hub
http_response_code(404);
fof_log("Got push to unknown feed: id=$feed_id", 'warning');
die("No such feed $feed_id");
}
if (!$feed['feed_websub_hub'] || $secret != $feed['feed_websub_secret']) {
// A bad actor was trying to push an update
http_response_code(403);
fof_log("Hub attempted bad push: id=$feed_id secret=$secret");
die("Bad push: id=$feed_id secret=$secret");
}
// Note: while the WebSub protocol calls these hub.mode, hub.topic, etc.
// PHP "helpfully" rewrites those to hub_mode, hub_topic etc. due to bad
// magic variable legacy which no longer applies.
if (isset($_GET['hub_mode']) && $_GET['hub_mode'] == 'subscribe') {
// We are responding to a subscription verification
$topic = $_GET['hub_topic'];
$challenge = $_GET['hub_challenge'];
$lease_time = $_GET['hub_lease_seconds'];
fof_log("Got subscription verification request: id=$feed_id topic=$topic lease_time=$lease_time");
// Set the lease to renew when they're down to 10% of their lifetime
fof_db_feed_update_websub($feed_id, $feed['feed_websub_hub'], $secret, time() + $lease_time*9/10);
// Respond with the challenge and exit
echo $challenge;
exit();
}
// We're responding to a push response.
fof_log("Got a WebSub push notification for feed $feed_id");
fof_update_feed($feed_id);
?>
Updated feed <?=$feed_id?>.