aboutsummaryrefslogtreecommitdiff
path: root/p/api/pshb.php
blob: bcb8341b1ac0ec73d89f5c340e92e91db217ac0f (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
105
106
107
108
109
110
111
112
113
114
115
116
<?php
require('../../constants.php');
require(LIB_PATH . '/lib_rss.php');	//Includes class autoloader

define('MAX_PAYLOAD', 3145728);

header('Content-Type: text/plain; charset=UTF-8');

function logMe($text) {
	file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
}

$ORIGINAL_INPUT = file_get_contents('php://input', false, null, -1, MAX_PAYLOAD);

logMe(print_r(array('_GET' => $_GET, '_POST' => $_POST, 'INPUT' => $ORIGINAL_INPUT), true));

$secret = isset($_GET['s']) ? substr($_GET['s'], 0, 128) : '';
if (!ctype_xdigit($secret)) {
	header('HTTP/1.1 422 Unprocessable Entity');
	die('Invalid feed secret format!');
}
chdir(PSHB_PATH);
$canonical64 = @file_get_contents('secrets/' . $secret . '.txt');
if ($canonical64 === false) {
	header('HTTP/1.1 404 Not Found');
	logMe('Feed secret not found!: ' . $secret);
	die('Feed secret not found!');
}
$canonical64 = trim($canonical64);
if (!preg_match('/^[A-Za-z0-9_-]+$/D', $canonical64)) {
	header('HTTP/1.1 500 Internal Server Error');
	logMe('Invalid secret reference!: ' . $canonical64);
	die('Invalid secret reference!');
}
$secret2 = @file_get_contents('feeds/' . $canonical64 . '/secret.txt');
if ($secret2 === false) {
	header('HTTP/1.1 404 Not Found');
	//@unlink('secrets/' . $secret . '.txt');
	logMe('Feed reverse secret not found!: ' . $canonical64);
	die('Feed reverse secret not found!');
}
if ($secret !== $secret2) {
	header('HTTP/1.1 500 Internal Server Error');
	logMe('Invalid secret cross-check!: ' . $secret);
	die('Invalid secret cross-check!');
}
chdir('feeds/' . $canonical64);
$users = glob('*/*.txt', GLOB_NOSORT);
if (empty($users)) {
	header('HTTP/1.1 410 Gone');
	logMe('Nobody is subscribed to this feed anymore!: ' . $canonical64);
	die('Nobody is subscribed to this feed anymore!');
}

if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] === 'subscribe') {
	//TODO: hub_lease_seconds
	exit(isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '');
}

Minz_Configuration::register('system', DATA_PATH . '/config.php', DATA_PATH . '/config.default.php');
$system_conf = Minz_Configuration::get('system');
$system_conf->auth_type = 'none';	// avoid necessity to be logged in (not saved!)
Minz_Translate::init('en');
Minz_Request::_param('ajax', true);
$feedController = new FreshRSS_feed_Controller();

$simplePie = customSimplePie();
$simplePie->set_raw_data($ORIGINAL_INPUT);
$simplePie->init();
unset($ORIGINAL_INPUT);

$links = $simplePie->get_links('self');
$self = isset($links[0]) ? $links[0] : null;

if ($self !== base64url_decode($canonical64)) {
	header('HTTP/1.1 422 Unprocessable Entity');
	logMe('Self URL does not match registered canonical URL!: ' . $self);
	die('Self URL does not match registered canonical URL!');
}
Minz_Request::_param('url', $self);

$nb = 0;
foreach ($users as $userLine) {
	$userLine = strtr($userLine, '\\', '/');
	$userInfos = explode('/', $userLine);
	$feedUrl = isset($userInfos[0]) ? base64url_decode($userInfos[0]) : '';
	$username = isset($userInfos[1]) ? basename($userInfos[1], '.txt') : '';
	if (!file_exists(USERS_PATH . '/' . $username . '/config.php')) {
		break;
	}

	try {
		Minz_Session::_param('currentUser', $username);
		Minz_Configuration::register('user',
		                             join_path(USERS_PATH, $username, 'config.php'),
		                             join_path(USERS_PATH, '_', 'config.default.php'));
		FreshRSS_Context::init();
		if ($feedController->actualizeAction($simplePie) > 0) {
			$nb++;
		}
	} catch (Exception $e) {
		logMe($e->getMessage());
	}
}

$simplePie->__destruct();
unset($simplePie);

if ($nb === 0) {
	header('HTTP/1.1 410 Gone');
	logMe('Nobody is subscribed to this feed anymore after all!: ' . $self);
	die('Nobody is subscribed to this feed anymore after all!');
}

logMe($self . ' done: ' . $nb);
exit('Done: ' . $nb . "\n");