Skip to content
Snippets Groups Projects
FeedController.php 1.95 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * @copyright  2020 Podlibre
     * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
     * @link       https://castopod.org/
     */
    
    
    namespace App\Controllers;
    
    
    use App\Models\PodcastModel;
    use CodeIgniter\Controller;
    
    use CodeIgniter\Exceptions\PageNotFoundException;
    use CodeIgniter\HTTP\ResponseInterface;
    use Exception;
    use Opawg\UserAgentsPhp\UserAgentsRSS;
    
    class FeedController extends Controller
    
        public function index(string $podcastName): ResponseInterface
    
            $podcast = (new PodcastModel())->where('name', $podcastName)
                ->first();
            if (! $podcast) {
    
                $service = UserAgentsRSS::find($_SERVER['HTTP_USER_AGENT']);
            } catch (Exception $exception) {
    
                // If things go wrong the show must go on and the user must be able to download the file
    
            $serviceSlug = null;
            if ($service) {
                $serviceSlug = $service['slug'];
            }
    
    
                "podcast#{$podcast->id}_feed" . ($service ? "_{$serviceSlug}" : '');
    
            if (! ($found = cache($cacheName))) {
    
                $found = get_rss_feed($podcast, $serviceSlug);
    
    
                // 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(
    
                cache()
                    ->save(
                        $cacheName,
                        $found,
                        $secondsToNextUnpublishedEpisode
    
            return $this->response->setXML($found);