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.

84 lines
2.4 KiB
PHTML

<?php
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\News;
use Illuminate\Database\Eloquent\Collection;
/**
* Publically available page to feed the news to feed readers
*/
8 years ago
function user_atom()
{
$request = request();
$user = auth()->apiUser('key');
8 years ago
if (
!$request->has('key')
|| !preg_match('/^[\da-f]{32}$/', $request->input('key'))
|| empty($user)
) {
throw new HttpForbidden('Missing or invalid key', ['content-type' => 'text/text']);
8 years ago
}
8 years ago
if (!auth()->can('atom')) {
throw new HttpForbidden('Not allowed', ['content-type' => 'text/text']);
8 years ago
}
8 years ago
$news = $request->has('meetings') ? News::whereIsMeeting((bool)$request->get('meetings', false)) : News::query();
$news
->limit((int)config('display_news'))
->orderByDesc('updated_at');
$output = make_atom_entries_from_news($news->get());
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 News[]|Collection $news_entries
* @return string
*/
8 years ago
function make_atom_entries_from_news($news_entries)
{
$request = app('request');
$updatedAt = isset($news_entries[0]) ? $news_entries[0]->updated_at->format('Y-m-d\TH:i:sP') : '0000:00:00T00:00:00+00:00';
8 years ago
$html = '<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>' . config('app_name') . '</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>' . $updatedAt . '</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 News $news
* @return string
*/
function make_atom_entry_from_news(News $news)
8 years ago
{
return '
<entry>
<title>' . htmlspecialchars($news->title) . '</title>
<link href="' . page_link_to('news_comments', ['nid' => $news->id]) . '"/>
<id>' . preg_replace(
'#^https?://#',
'',
page_link_to('news_comments', ['nid' => $news->id])
) . '</id>
<updated>' . $news->updated_at->format('Y-m-d\TH:i:sP') . '</updated>
<summary type="html">' . htmlspecialchars($news->text) . '</summary>
</entry>' . "\n";
}