2020-06-10 15:00:12 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-06-10 15:00:12 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use App\Entities\Episode;
|
|
|
|
use App\Entities\Podcast;
|
2021-07-12 18:40:22 +00:00
|
|
|
use App\Libraries\NoteObject;
|
|
|
|
use App\Libraries\PodcastEpisode;
|
2020-06-10 15:00:12 +00:00
|
|
|
use App\Models\EpisodeModel;
|
|
|
|
use App\Models\PodcastModel;
|
2022-01-04 15:40:27 +00:00
|
|
|
use App\Models\PostModel;
|
2021-07-12 18:40:22 +00:00
|
|
|
use CodeIgniter\Database\BaseBuilder;
|
2021-05-06 14:00:48 +00:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
2021-07-12 18:40:22 +00:00
|
|
|
use CodeIgniter\HTTP\Response;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2023-08-27 13:26:06 +00:00
|
|
|
use Config\Embed;
|
|
|
|
use Config\Images;
|
2021-05-19 16:35:13 +00:00
|
|
|
use Config\Services;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Analytics\AnalyticsTrait;
|
|
|
|
use Modules\Fediverse\Objects\OrderedCollectionObject;
|
|
|
|
use Modules\Fediverse\Objects\OrderedCollectionPage;
|
2021-04-02 17:20:02 +00:00
|
|
|
use SimpleXMLElement;
|
2020-06-10 15:00:12 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
class EpisodeController extends BaseController
|
2020-06-10 15:00:12 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
use AnalyticsTrait;
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected Podcast $podcast;
|
|
|
|
|
|
|
|
protected Episode $episode;
|
2020-06-10 15:00:12 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function _remap(string $method, string ...$params): mixed
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
if (count($params) < 2) {
|
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-04-14 11:11:53 +00:00
|
|
|
! ($podcast = (new PodcastModel())->getPodcastByHandle($params[0])) instanceof Podcast
|
2021-05-06 14:00:48 +00:00
|
|
|
) {
|
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$this->podcast = $podcast;
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
if (
|
2023-04-14 11:11:53 +00:00
|
|
|
! ($episode = (new EpisodeModel())->getEpisodeBySlug($params[0], $params[1])) instanceof Episode
|
2023-02-22 16:29:45 +00:00
|
|
|
) {
|
2021-06-09 12:40:22 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
2021-05-06 14:00:48 +00:00
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$this->episode = $episode;
|
|
|
|
|
|
|
|
unset($params[1]);
|
|
|
|
unset($params[0]);
|
|
|
|
|
|
|
|
return $this->{$method}(...$params);
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function index(): string
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
// Prevent analytics hit when authenticated
|
2022-10-15 11:22:08 +00:00
|
|
|
if (! auth()->loggedIn()) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$this->registerPodcastWebpageHit($this->episode->podcast_id);
|
|
|
|
}
|
2020-07-02 10:08:32 +00:00
|
|
|
|
2022-09-29 10:51:12 +00:00
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
|
|
|
"podcast#{$this->podcast->id}",
|
|
|
|
"episode#{$this->episode->id}",
|
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
|
|
|
is_unlocked($this->podcast->handle) ? 'unlocked' : null,
|
2022-10-15 11:22:08 +00:00
|
|
|
auth()
|
|
|
|
->loggedIn() ? 'authenticated' : null,
|
2022-09-29 10:51:12 +00:00
|
|
|
]),
|
|
|
|
);
|
2020-10-20 10:31:40 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($cachedView = cache($cacheName))) {
|
2020-09-04 09:09:26 +00:00
|
|
|
$data = [
|
2021-11-12 16:31:35 +00:00
|
|
|
'metatags' => get_episode_metatags($this->episode),
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
2020-09-04 09:09:26 +00:00
|
|
|
];
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->podcast->id,
|
2020-10-22 17:41:59 +00:00
|
|
|
);
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
if (auth()->loggedIn()) {
|
2021-04-02 17:20:02 +00:00
|
|
|
helper('form');
|
2022-01-21 09:08:14 +00:00
|
|
|
|
|
|
|
return view('episode/comments', $data);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2022-01-21 09:08:14 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
// The page cache is set to a decade so it is deleted manually upon podcast update
|
2021-10-13 15:43:40 +00:00
|
|
|
return view('episode/comments', $data, [
|
|
|
|
'cache' => $secondsToNextUnpublishedEpisode
|
2023-08-27 13:26:06 +00:00
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
2021-10-13 15:43:40 +00:00
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cachedView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activity(): string
|
|
|
|
{
|
|
|
|
// Prevent analytics hit when authenticated
|
2022-10-15 11:22:08 +00:00
|
|
|
if (! auth()->loggedIn()) {
|
2021-10-13 15:43:40 +00:00
|
|
|
$this->registerPodcastWebpageHit($this->episode->podcast_id);
|
|
|
|
}
|
|
|
|
|
2022-09-29 10:51:12 +00:00
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
|
|
|
"podcast#{$this->podcast->id}",
|
|
|
|
"episode#{$this->episode->id}",
|
|
|
|
'activity',
|
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
|
|
|
is_unlocked($this->podcast->handle) ? 'unlocked' : null,
|
2022-10-15 11:22:08 +00:00
|
|
|
auth()
|
|
|
|
->loggedIn() ? 'authenticated' : null,
|
2022-09-29 10:51:12 +00:00
|
|
|
]),
|
|
|
|
);
|
2021-10-13 15:43:40 +00:00
|
|
|
|
|
|
|
if (! ($cachedView = cache($cacheName))) {
|
|
|
|
$data = [
|
2021-11-12 16:31:35 +00:00
|
|
|
'metatags' => get_episode_metatags($this->episode),
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
2021-10-13 15:43:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
|
|
|
$this->podcast->id,
|
|
|
|
);
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
if (auth()->loggedIn()) {
|
2021-10-13 15:43:40 +00:00
|
|
|
helper('form');
|
2022-01-21 09:08:14 +00:00
|
|
|
|
|
|
|
return view('episode/activity', $data);
|
2021-10-13 15:43:40 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2021-10-13 15:43:40 +00:00
|
|
|
// The page cache is set to a decade so it is deleted manually upon podcast update
|
|
|
|
return view('episode/activity', $data, [
|
2021-05-14 17:59:35 +00:00
|
|
|
'cache' => $secondsToNextUnpublishedEpisode
|
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
2020-06-26 14:34:52 +00:00
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
return $cachedView;
|
2020-06-10 15:00:12 +00:00
|
|
|
}
|
2021-02-27 21:21:26 +00:00
|
|
|
|
2021-10-20 14:22:58 +00:00
|
|
|
public function embed(string $theme = 'light-transparent'): string
|
2021-05-19 16:35:13 +00:00
|
|
|
{
|
2021-08-09 10:28:16 +00:00
|
|
|
header('Content-Security-Policy: frame-ancestors http://*:* https://*:*');
|
2021-03-01 15:59:07 +01:00
|
|
|
|
2021-04-22 17:20:28 +00:00
|
|
|
// Prevent analytics hit when authenticated
|
2022-10-15 11:22:08 +00:00
|
|
|
if (! auth()->loggedIn()) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$this->registerPodcastWebpageHit($this->episode->podcast_id);
|
|
|
|
}
|
2021-02-27 21:21:26 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
$session = Services::session();
|
2023-09-17 10:07:59 +00:00
|
|
|
|
2023-08-29 12:58:20 +00:00
|
|
|
if (service('superglobals')->server('HTTP_REFERER') !== null) {
|
|
|
|
$session->set('embed_domain', parse_url(service('superglobals')->server('HTTP_REFERER'), PHP_URL_HOST));
|
2021-02-27 21:21:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 13:34:28 +00:00
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
|
|
|
"podcast#{$this->podcast->id}",
|
|
|
|
"episode#{$this->episode->id}",
|
|
|
|
'embed',
|
|
|
|
$theme,
|
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
|
|
|
is_unlocked($this->podcast->handle) ? 'unlocked' : null,
|
|
|
|
]),
|
|
|
|
);
|
2021-02-27 21:21:26 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($cachedView = cache($cacheName))) {
|
2021-08-09 10:28:16 +00:00
|
|
|
$themeData = EpisodeModel::$themes[$theme];
|
2021-02-27 21:21:26 +00:00
|
|
|
|
|
|
|
$data = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'episode' => $this->episode,
|
|
|
|
'theme' => $theme,
|
2021-08-09 10:28:16 +00:00
|
|
|
'themeData' => $themeData,
|
2021-02-27 21:21:26 +00:00
|
|
|
];
|
|
|
|
|
2021-04-14 15:58:40 +00:00
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->podcast->id,
|
2021-02-27 21:21:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// The page cache is set to a decade so it is deleted manually upon podcast update
|
2021-10-20 14:22:58 +00:00
|
|
|
return view('embed', $data, [
|
2021-02-27 21:21:26 +00:00
|
|
|
'cache' => $secondsToNextUnpublishedEpisode
|
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cachedView;
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function oembedJSON(): ResponseInterface
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
return $this->response->setJSON([
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'rich',
|
|
|
|
'version' => '1.0',
|
|
|
|
'title' => $this->episode->title,
|
2021-04-02 17:20:02 +00:00
|
|
|
'provider_name' => $this->podcast->title,
|
2023-06-12 14:47:38 +00:00
|
|
|
'provider_url' => $this->podcast->link,
|
|
|
|
'author_name' => $this->podcast->title,
|
|
|
|
'author_url' => $this->podcast->link,
|
|
|
|
'html' => '<iframe src="' .
|
2021-10-20 14:22:58 +00:00
|
|
|
$this->episode->embed_url .
|
2023-08-27 13:26:06 +00:00
|
|
|
'" width="100%" height="' . config(Embed::class)->height . '" frameborder="0" scrolling="no"></iframe>',
|
|
|
|
'width' => config(Embed::class)
|
2022-01-23 15:42:56 +00:00
|
|
|
->width,
|
2023-08-27 13:26:06 +00:00
|
|
|
'height' => config(Embed::class)
|
2022-01-23 15:42:56 +00:00
|
|
|
->height,
|
2023-06-12 14:47:38 +00:00
|
|
|
'thumbnail_url' => $this->episode->cover->og_url,
|
2023-08-27 13:26:06 +00:00
|
|
|
'thumbnail_width' => config(Images::class)
|
2022-01-23 15:42:56 +00:00
|
|
|
->podcastCoverSizes['og']['width'],
|
2023-08-27 13:26:06 +00:00
|
|
|
'thumbnail_height' => config(Images::class)
|
2022-01-23 15:42:56 +00:00
|
|
|
->podcastCoverSizes['og']['height'],
|
2021-04-02 17:20:02 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function oembedXML(): ResponseInterface
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-06-08 09:52:11 +00:00
|
|
|
$oembed = new SimpleXMLElement("<?xml version='1.0' encoding='utf-8' standalone='yes'?><oembed></oembed>");
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
$oembed->addChild('type', 'rich');
|
|
|
|
$oembed->addChild('version', '1.0');
|
|
|
|
$oembed->addChild('title', $this->episode->title);
|
|
|
|
$oembed->addChild('provider_name', $this->podcast->title);
|
|
|
|
$oembed->addChild('provider_url', $this->podcast->link);
|
|
|
|
$oembed->addChild('author_name', $this->podcast->title);
|
|
|
|
$oembed->addChild('author_url', $this->podcast->link);
|
2022-01-23 15:42:56 +00:00
|
|
|
$oembed->addChild('thumbnail', $this->episode->cover->og_url);
|
2023-08-27 13:26:06 +00:00
|
|
|
$oembed->addChild('thumbnail_width', (string) config(Images::class)->podcastCoverSizes['og']['width']);
|
|
|
|
$oembed->addChild('thumbnail_height', (string) config(Images::class)->podcastCoverSizes['og']['height']);
|
2021-04-02 17:20:02 +00:00
|
|
|
$oembed->addChild(
|
|
|
|
'html',
|
2022-03-25 14:37:14 +00:00
|
|
|
htmlspecialchars(
|
2021-04-02 17:20:02 +00:00
|
|
|
'<iframe src="' .
|
2021-10-20 14:22:58 +00:00
|
|
|
$this->episode->embed_url .
|
2023-08-27 13:26:06 +00:00
|
|
|
'" width="100%" height="' . config(
|
|
|
|
Embed::class
|
|
|
|
)->height . '" frameborder="0" scrolling="no"></iframe>',
|
2021-04-02 17:20:02 +00:00
|
|
|
),
|
|
|
|
);
|
2023-08-27 13:26:06 +00:00
|
|
|
$oembed->addChild('width', (string) config(Embed::class)->width);
|
|
|
|
$oembed->addChild('height', (string) config(Embed::class)->height);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-11-12 16:31:35 +00:00
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
return $this->response->setXML($oembed);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-07-12 18:40:22 +00:00
|
|
|
|
|
|
|
public function episodeObject(): Response
|
|
|
|
{
|
|
|
|
$podcastObject = new PodcastEpisode($this->episode);
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/json')
|
|
|
|
->setBody($podcastObject->toJSON());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function comments(): Response
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* get comments: aggregated replies from posts referring to the episode
|
|
|
|
*/
|
2022-01-04 15:40:27 +00:00
|
|
|
$episodeComments = model(PostModel::class)
|
2021-07-12 18:40:22 +00:00
|
|
|
->whereIn('in_reply_to_id', function (BaseBuilder $builder): BaseBuilder {
|
|
|
|
return $builder->select('id')
|
2023-08-26 13:03:01 +00:00
|
|
|
->from('fediverse_posts')
|
2021-07-12 18:40:22 +00:00
|
|
|
->where('episode_id', $this->episode->id);
|
|
|
|
})
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2021-07-12 18:40:22 +00:00
|
|
|
->orderBy('published_at', 'ASC');
|
|
|
|
|
|
|
|
$pageNumber = (int) $this->request->getGet('page');
|
|
|
|
|
|
|
|
if ($pageNumber < 1) {
|
|
|
|
$episodeComments->paginate(12);
|
|
|
|
$pager = $episodeComments->pager;
|
|
|
|
$collection = new OrderedCollectionObject(null, $pager);
|
|
|
|
} else {
|
|
|
|
$paginatedComments = $episodeComments->paginate(12, 'default', $pageNumber);
|
|
|
|
$pager = $episodeComments->pager;
|
|
|
|
|
|
|
|
$orderedItems = [];
|
|
|
|
if ($paginatedComments !== null) {
|
|
|
|
foreach ($paginatedComments as $comment) {
|
|
|
|
$orderedItems[] = (new NoteObject($comment))->toArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
$collection = new OrderedCollectionPage($pager, $orderedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
2021-07-24 15:33:34 +00:00
|
|
|
->setHeader('Access-Control-Allow-Origin', '*')
|
2021-07-12 18:40:22 +00:00
|
|
|
->setBody($collection->toJSON());
|
|
|
|
}
|
2020-06-10 15:00:12 +00:00
|
|
|
}
|