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