summaryrefslogtreecommitdiffstats
path: root/modules-available/news/api.inc.php
blob: 3b56c70d2ea8e66d7562da1b72c3a2f62783c05e (plain) (blame)
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

header('Content-Type: application/xml; charset=utf-8');

$type = Request::any('type', 'news', 'string');

if (Module::isAvailable('locations')) {
	$locationId = Request::any('location', 0, 'int');
	if ($locationId === 0) {
		$locationId = Location::getFromIp($_SERVER['REMOTE_ADDR']);
	}
	$locations = Location::getLocationRootChain($locationId);
	$locations[] = 0;
} else {
	$locations = [0];
}

// Fetch news from DB
$res = Database::simpleQuery('SELECT title, locationid, content, dateline FROM vmchooser_pages
	WHERE type = :type AND (locationid IS NULL OR locationid IN (:lids))
	  AND expires > UNIX_TIMESTAMP() ORDER BY dateline DESC', [
			'type' => $type,
			'lids' => $locations,
	]);

// Get one for each location. As we sort by dateline and check expiry in the query, we only
// need one per location and then pick the first one that is set, as the locations are ordered
// by closest to furthest
$locations = array_flip($locations);
foreach ($res as $row) {
	$lid = (int)$row['locationid'];
	if (is_array($locations[$lid]))
		continue; // Already have one
	$locations[$lid] = $row;
}
// Pick first one
foreach ($locations as $row) {
	if (is_array($row))
		break;
}

if (is_array($row)) {
	$title = htmlspecialchars($row['title']);
	$content = htmlspecialchars($row['content']);
	echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<news>
	<headline>
		$title
	</headline>
	<info>
		$content
	</info>
	<date>
		{$row['dateline']}
	</date>
</news>
EOF;

} else {
	// no news in DB, output a 'null' news xml
	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
	echo "<news>null</news>";
}