2012-12-26 20:39:54 +01:00
|
|
|
<?php
|
|
|
|
|
2019-09-18 14:09:30 +02:00
|
|
|
use Engelsystem\Http\Exceptions\HttpForbidden;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\News;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-04-05 16:54:45 +02:00
|
|
|
use Illuminate\Support\Collection as SupportCollection;
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2017-07-18 21:38:53 +02:00
|
|
|
* Publically available page to feed the news to feed readers
|
2017-01-03 03:22:48 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_atom()
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2019-09-18 14:09:30 +02:00
|
|
|
$user = auth()->apiUser('key');
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2019-09-18 14:09:30 +02:00
|
|
|
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']);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-11-12 14:41:23 +01:00
|
|
|
if (!auth()->can('atom')) {
|
2019-09-18 14:09:30 +02:00
|
|
|
throw new HttpForbidden('Not allowed', ['content-type' => 'text/text']);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
$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());
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
header('Content-Type: application/atom+xml; charset=utf-8');
|
2017-01-03 14:12:17 +01:00
|
|
|
header('Content-Length: ' . strlen($output));
|
2017-01-02 03:57:23 +01:00
|
|
|
raw_output($output);
|
2016-08-22 19:08:10 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2020-04-05 16:54:45 +02:00
|
|
|
* @param News[]|Collection|SupportCollection $news_entries
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function make_atom_entries_from_news($news_entries)
|
|
|
|
{
|
2017-08-31 17:30:54 +02:00
|
|
|
$request = app('request');
|
2019-11-10 21:30:26 +01:00
|
|
|
$updatedAt = isset($news_entries[0]) ? $news_entries[0]->updated_at->format('Y-m-d\TH:i:sP') : '0000:00:00T00:00:00+00:00';
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$html = '<?xml version="1.0" encoding="utf-8"?>
|
2013-09-10 14:27:31 +02:00
|
|
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
2018-10-05 15:35:14 +02:00
|
|
|
<title>' . config('app_name') . '</title>
|
2017-08-29 16:21:25 +02:00
|
|
|
<id>' . $request->getHttpHost()
|
2017-01-02 15:43:36 +01:00
|
|
|
. htmlspecialchars(preg_replace(
|
2017-01-21 19:47:44 +01:00
|
|
|
'#[&?]key=[a-f\d]{32}#',
|
2017-01-02 15:43:36 +01:00
|
|
|
'',
|
2017-08-29 16:21:25 +02:00
|
|
|
$request->getRequestUri()
|
2017-01-02 15:43:36 +01:00
|
|
|
))
|
|
|
|
. '</id>
|
2019-11-10 21:30:26 +01:00
|
|
|
<updated>' . $updatedAt . '</updated>' . "\n";
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($news_entries as $news_entry) {
|
|
|
|
$html .= make_atom_entry_from_news($news_entry);
|
|
|
|
}
|
2017-01-03 14:12:17 +01:00
|
|
|
$html .= '</feed>';
|
2017-01-02 03:57:23 +01:00
|
|
|
return $html;
|
2016-08-22 19:08:10 +02:00
|
|
|
}
|
|
|
|
|
2017-12-25 23:12:52 +01:00
|
|
|
/**
|
2019-11-10 21:30:26 +01:00
|
|
|
* @param News $news
|
2017-12-25 23:12:52 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-11-10 21:30:26 +01:00
|
|
|
function make_atom_entry_from_news(News $news)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-08-29 16:21:25 +02:00
|
|
|
return '
|
|
|
|
<entry>
|
2019-11-10 21:30:26 +01:00
|
|
|
<title>' . htmlspecialchars($news->title) . '</title>
|
2020-04-05 16:54:45 +02:00
|
|
|
<link href="' . page_link_to('news/' . $news->id) . '"/>
|
2017-12-25 23:12:52 +01:00
|
|
|
<id>' . preg_replace(
|
|
|
|
'#^https?://#',
|
|
|
|
'',
|
2020-04-05 16:54:45 +02:00
|
|
|
page_link_to('news/' . $news->id)
|
2017-12-25 23:12:52 +01:00
|
|
|
) . '</id>
|
2019-11-10 21:30:26 +01:00
|
|
|
<updated>' . $news->updated_at->format('Y-m-d\TH:i:sP') . '</updated>
|
|
|
|
<summary type="html">' . htmlspecialchars($news->text) . '</summary>
|
2017-08-29 16:21:25 +02:00
|
|
|
</entry>' . "\n";
|
2013-09-10 14:27:31 +02:00
|
|
|
}
|