Files
cachet-docker/app/Http/Controllers/AtomController.php
Luc Didry 3cde840e2b Canonicalize URLs to be W3C compliant
The W3C feed validator (https://validator.w3.org/feed/) says that
http://cachet.example.org is not a valid value for a link, but that
http://cachet.example.org/ is.

The canonicalizeUrl method add a trailing slash if needed.
2015-06-12 23:03:42 +02:00

79 lines
2.2 KiB
PHP

<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Controllers;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use Roumen\Feed\Facades\Feed;
class AtomController extends AbstractController
{
/**
* Generates an Atom feed of all incidents.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup|null $group
*
* @return \Illuminate\Http\Response
*/
public function feedAction(ComponentGroup $group = null)
{
$feed = Feed::make();
$feed->title = Setting::get('app_name');
$feed->description = trans('cachet.feed');
$feed->link = $this->canonicalizeUrl(Setting::get('app_domain'));
$feed->setDateFormat('datetime');
if ($group->exists) {
$group->components->map(function ($component) use ($feed) {
$component->incidents()->visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($feed) {
$this->feedAddItem($feed, $incident);
});
});
} else {
Incident::visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($feed) {
$this->feedAddItem($feed, $incident);
});
}
return $feed->render('atom');
}
/**
* Adds an item to the feed.
*
* @param Roumen\Feed\Facades\Feed $feed
* @param \CachetHQ\Cachet\Models\Incident $incident
*/
private function feedAddItem(&$feed, $incident)
{
$feed->add(
$incident->name,
Setting::get('app_name'),
$this->canonicalizeUrl(Setting::get('app_domain')),
$incident->created_at->toAtomString(),
$incident->message
);
}
/**
* Add a / at the end of an URL to in order to be W3C compliant
*
* @param string $url
*/
private function canonicalizeUrl($url)
{
return preg_replace('/([^\/])$/', '$1/', $url);
}
}