2020-06-26 14:34:52 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-06-26 14:34:52 +00:00
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2020-10-22 17:41:59 +00:00
|
|
|
use App\Models\EpisodeModel;
|
2020-06-26 14:34:52 +00:00
|
|
|
use App\Models\PodcastModel;
|
|
|
|
use CodeIgniter\Controller;
|
|
|
|
|
|
|
|
class Feed extends Controller
|
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
public function index($podcastName)
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2020-08-04 11:25:22 +00:00
|
|
|
helper('rss');
|
|
|
|
|
|
|
|
$podcast = (new PodcastModel())->where('name', $podcastName)->first();
|
2020-10-21 16:04:18 +00:00
|
|
|
if (!$podcast) {
|
|
|
|
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2020-11-30 11:45:41 +00:00
|
|
|
$serviceSlug = '';
|
2020-10-21 16:04:18 +00:00
|
|
|
try {
|
|
|
|
$service = \Opawg\UserAgentsPhp\UserAgentsRSS::find(
|
2021-04-20 13:43:38 +00:00
|
|
|
$_SERVER['HTTP_USER_AGENT'],
|
2020-10-21 16:04:18 +00:00
|
|
|
);
|
2020-11-30 11:45:41 +00:00
|
|
|
if ($service) {
|
|
|
|
$serviceSlug = $service['slug'];
|
|
|
|
}
|
2020-10-21 16:04:18 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
// If things go wrong the show must go on and the user must be able to download the file
|
|
|
|
log_message('critical', $e);
|
|
|
|
}
|
2020-10-22 17:41:59 +00:00
|
|
|
|
2020-10-21 16:04:18 +00:00
|
|
|
$cacheName =
|
2021-04-20 13:43:38 +00:00
|
|
|
"podcast#{$podcast->id}_feed" . ($service ? "_{$serviceSlug}" : '');
|
2020-10-22 17:41:59 +00:00
|
|
|
|
2020-10-21 16:04:18 +00:00
|
|
|
if (!($found = cache($cacheName))) {
|
2020-11-30 11:45:41 +00:00
|
|
|
$found = get_rss_feed($podcast, $serviceSlug);
|
2020-10-22 17:41:59 +00:00
|
|
|
|
|
|
|
// The page cache is set to expire after next episode publication or a decade by default so it is deleted manually upon podcast update
|
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
2021-04-20 13:43:38 +00:00
|
|
|
$podcast->id,
|
2020-10-22 17:41:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
cache()->save(
|
|
|
|
$cacheName,
|
|
|
|
$found,
|
|
|
|
$secondsToNextUnpublishedEpisode
|
|
|
|
? $secondsToNextUnpublishedEpisode
|
2021-04-20 13:43:38 +00:00
|
|
|
: DECADE,
|
2020-10-22 17:41:59 +00:00
|
|
|
);
|
2020-10-21 16:04:18 +00:00
|
|
|
}
|
|
|
|
return $this->response->setXML($found);
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
}
|