blob: e8ef11d1486daff8a81bafab21d20fcb8b8b701d (
plain) (
tree)
|
|
<?php
header('Content-Type: application/xml; charset=utf-8');
$type = Request::any('type', 'news', 'string');
$locationId = Request::any('location', 0, 'int');
if ($locationId === 0) {
$locationId = Location::getFromIp($_SERVER['REMOTE_ADDR']);
}
$locations = Location::getLocationRootChain($locationId);
$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 = $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>";
}
|