You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.3 KiB
PHTML

<?php
use Engelsystem\Database\DB;
/**
* Publically available page to feed the news to feed readers
*/
8 years ago
function user_atom()
{
global $user;
$request = request();
8 years ago
if (!$request->has('key') || !preg_match('/^[\da-f]{32}$/', $request->input('key'))) {
engelsystem_error('Missing key.');
8 years ago
}
$key = $request->input('key');
8 years ago
8 years ago
$user = User_by_api_key($key);
if (empty($user)) {
engelsystem_error('Key invalid.');
8 years ago
}
8 years ago
if (!in_array('atom', privileges_for_user($user['UID']))) {
engelsystem_error('No privilege for atom.');
8 years ago
}
8 years ago
$news = DB::select('
SELECT *
8 years ago
FROM `News`
' . (!$request->has('meetings') ? '' : 'WHERE `Treffen` = 1 ') . '
ORDER BY `ID`
DESC LIMIT ' . (int)config('display_news')
);
8 years ago
8 years ago
$output = make_atom_entries_from_news($news);
8 years ago
8 years ago
header('Content-Type: application/atom+xml; charset=utf-8');
header('Content-Length: ' . strlen($output));
8 years ago
raw_output($output);
}
/**
* @param array[] $news_entries
* @return string
*/
8 years ago
function make_atom_entries_from_news($news_entries)
{
$request = app('request');
8 years ago
$html = '<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Engelsystem</title>
<id>' . $request->getHttpHost()
8 years ago
. htmlspecialchars(preg_replace(
'#[&?]key=[a-f\d]{32}#',
8 years ago
'',
$request->getRequestUri()
8 years ago
))
. '</id>
<updated>' . date('Y-m-d\TH:i:sP', $news_entries[0]['Datum']) . '</updated>' . "\n";
8 years ago
foreach ($news_entries as $news_entry) {
$html .= make_atom_entry_from_news($news_entry);
}
$html .= '</feed>';
8 years ago
return $html;
}
/**
* @param array $news_entry
* @return string
*/
8 years ago
function make_atom_entry_from_news($news_entry)
{
return '
<entry>
<title>' . htmlspecialchars($news_entry['Betreff']) . '</title>
<link href="' . page_link_to('news_comments', ['nid' => $news_entry['ID']]) . '"/>
<id>' . preg_replace(
'#^https?://#',
'',
page_link_to('news_comments', ['nid' => $news_entry['ID']])
) . '</id>
<updated>' . date('Y-m-d\TH:i:sP', $news_entry['Datum']) . '</updated>
<summary>' . htmlspecialchars($news_entry['Text']) . '</summary>
</entry>' . "\n";
}