2020-06-26 14:34:52 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-07-10 12:20:25 +00:00
|
|
|
/**
|
2022-12-09 15:04:42 +00:00
|
|
|
* @copyright 2022 Ad Aures
|
2020-07-10 12:20:25 +00:00
|
|
|
* @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;
|
|
|
|
|
2023-06-13 16:05:02 +00:00
|
|
|
use App\Entities\Podcast;
|
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;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
2023-06-13 16:05:02 +00:00
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
use Exception;
|
2023-04-14 11:11:53 +00:00
|
|
|
use Modules\PremiumPodcasts\Entities\Subscription;
|
2022-09-28 15:02:09 +00:00
|
|
|
use Modules\PremiumPodcasts\Models\SubscriptionModel;
|
2023-12-21 15:21:45 +00:00
|
|
|
use Opawg\UserAgentsV2Php\UserAgentsRSS;
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
class FeedController extends Controller
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2023-06-13 16:05:02 +00:00
|
|
|
/**
|
|
|
|
* Instance of the main Request object.
|
|
|
|
*
|
|
|
|
* @var IncomingRequest
|
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
2021-07-26 13:10:46 +00:00
|
|
|
public function index(string $podcastHandle): ResponseInterface
|
2020-06-26 14:34:52 +00:00
|
|
|
{
|
2023-04-13 11:45:03 +00:00
|
|
|
helper(['rss', 'premium_podcasts', 'misc']);
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-07-26 13:10:46 +00:00
|
|
|
$podcast = (new PodcastModel())->where('handle', $podcastHandle)
|
2021-05-19 16:35:13 +00:00
|
|
|
->first();
|
2023-06-13 16:05:02 +00:00
|
|
|
if (! $podcast instanceof Podcast) {
|
2021-05-06 14:00:48 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2020-10-21 16:04:18 +00:00
|
|
|
}
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
$service = null;
|
2020-10-21 16:04:18 +00:00
|
|
|
try {
|
2023-08-29 12:58:20 +00:00
|
|
|
$service = UserAgentsRSS::find(service('superglobals')->server('HTTP_USER_AGENT'));
|
2021-05-06 14:00:48 +00:00
|
|
|
} catch (Exception $exception) {
|
2020-10-21 16:04:18 +00:00
|
|
|
// If things go wrong the show must go on and the user must be able to download the file
|
2021-06-08 09:52:11 +00:00
|
|
|
log_message('critical', $exception->getMessage());
|
2020-10-21 16:04:18 +00:00
|
|
|
}
|
2020-10-22 17:41:59 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
$serviceSlug = '';
|
2021-05-12 14:00:25 +00:00
|
|
|
if ($service) {
|
|
|
|
$serviceSlug = $service['slug'];
|
|
|
|
}
|
|
|
|
|
2022-09-28 15:02:09 +00:00
|
|
|
$subscription = null;
|
|
|
|
$token = $this->request->getGet('token');
|
|
|
|
if ($token) {
|
|
|
|
$subscription = (new SubscriptionModel())->validateSubscription($podcastHandle, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
"podcast#{$podcast->id}",
|
|
|
|
'feed',
|
|
|
|
$service ? $serviceSlug : null,
|
2023-04-14 11:11:53 +00:00
|
|
|
$subscription instanceof Subscription ? 'unlocked' : null,
|
2022-09-28 15:02:09 +00:00
|
|
|
]),
|
|
|
|
);
|
2020-10-22 17:41:59 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache($cacheName))) {
|
2022-09-28 15:02:09 +00:00
|
|
|
$found = get_rss_feed($podcast, $serviceSlug, $subscription, $token);
|
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
|
|
|
);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2024-04-26 09:26:22 +00:00
|
|
|
->save($cacheName, $found, $secondsToNextUnpublishedEpisode ?: DECADE);
|
2020-10-21 16:04:18 +00:00
|
|
|
}
|
2021-05-12 14:00:25 +00:00
|
|
|
|
2020-10-21 16:04:18 +00:00
|
|
|
return $this->response->setXML($found);
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
}
|