Newer
Older

Yassine Doghri
committed

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers;

Yassine Doghri
committed
use App\Models\EpisodeModel;
use App\Models\PodcastModel;
use CodeIgniter\Controller;
class Feed extends Controller
{

Yassine Doghri
committed
public function index($podcastName)

Yassine Doghri
committed
helper('rss');
$podcast = (new PodcastModel())->where('name', $podcastName)->first();
if (!$podcast) {
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}

Yassine Doghri
committed

Benjamin Bellamy
committed
$serviceSlug = '';
try {
$service = \Opawg\UserAgentsPhp\UserAgentsRSS::find(
$_SERVER['HTTP_USER_AGENT']
);

Benjamin Bellamy
committed
if ($service) {
$serviceSlug = $service['slug'];
}
} 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);
}

Yassine Doghri
committed

Benjamin Bellamy
committed
"podcast{$podcast->id}_feed" . ($service ? "_{$serviceSlug}" : '');

Yassine Doghri
committed
if (!($found = cache($cacheName))) {

Benjamin Bellamy
committed
$found = get_rss_feed($podcast, $serviceSlug);

Yassine Doghri
committed
// 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(
$podcast->id
);
cache()->save(
$cacheName,
$found,
$secondsToNextUnpublishedEpisode
? $secondsToNextUnpublishedEpisode
: DECADE
);
}
return $this->response->setXML($found);