Newer
Older

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

Yassine Doghri
committed
use App\Libraries\SimpleRSSElement;
use CodeIgniter\I18n\Time;

Yassine Doghri
committed
use App\Entities\Podcast;
use App\Entities\Category;
if (!function_exists('get_rss_feed')) {
/**
* Generates the rss feed for a given podcast entity
*
* @param string $serviceSlug The name of the service that fetches the RSS feed for future reference when the audio file is eventually downloaded

Yassine Doghri
committed
* @return string rss feed as xml
*/
function get_rss_feed(Podcast $podcast, ?string $serviceSlug = null): string

Yassine Doghri
committed
{
$episodes = $podcast->episodes;
$itunes_namespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
$podcast_namespace =
'https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md';
$rss = new SimpleRSSElement(
"<?xml version='1.0' encoding='utf-8'?><rss version='2.0' xmlns:itunes='{$itunes_namespace}' xmlns:podcast='{$podcast_namespace}' xmlns:content='http://purl.org/rss/1.0/modules/content/'></rss>",

Yassine Doghri
committed
$channel = $rss->addChild('channel');

Yassine Doghri
committed
$atom_link = $channel->addChild(
'atom:link',
null,

Yassine Doghri
committed
'http://www.w3.org/2005/Atom',
);

Yassine Doghri
committed
$atom_link->addAttribute('href', $podcast->feed_url);
$atom_link->addAttribute('rel', 'self');
$atom_link->addAttribute('type', 'application/rss+xml');
if ($podcast->new_feed_url !== null) {

Yassine Doghri
committed
$channel->addChild(
'new-feed-url',
$podcast->new_feed_url,
$itunes_namespace,
);
}

Yassine Doghri
committed
// the last build date corresponds to the creation of the feed.xml cache
$channel->addChild(
'lastBuildDate',
(new Time('now'))->format(DATE_RFC1123),

Yassine Doghri
committed
$channel->addChild(
'generator',
'Castopod Host - https://castopod.org/',

Yassine Doghri
committed
$channel->addChild('docs', 'https://cyber.harvard.edu/rss/rss.html');

Yassine Doghri
committed
$channel->addChild('title', $podcast->title);
$channel->addChildWithCDATA('description', $podcast->description_html);

Yassine Doghri
committed
$itunes_image = $channel->addChild('image', null, $itunes_namespace);

Benjamin Bellamy
committed
// FIXME: This should be downsized to 1400x1400
$itunes_image->addAttribute('href', $podcast->image->url);

Yassine Doghri
committed
$channel->addChild('language', $podcast->language_code);
if ($podcast->location !== null) {

Yassine Doghri
committed
$locationElement = $channel->addChild(
htmlspecialchars($podcast->location->name),
$podcast_namespace,
if ($podcast->location->geo !== null) {
$locationElement->addAttribute('geo', $podcast->location->geo);
if ($podcast->location->osm_id !== null) {
$locationElement->addAttribute(
'osm',
$podcast->location->osm_id,
);
if ($podcast->payment_pointer !== null) {

Yassine Doghri
committed
$valueElement = $channel->addChild(
'value',
$podcast_namespace,

Yassine Doghri
committed
$valueElement->addAttribute('type', 'webmonetization');
$valueElement->addAttribute('method', '');
$valueElement->addAttribute('suggested', '');
$recipientElement = $valueElement->addChild(
'valueRecipient',
null,
$podcast_namespace,

Yassine Doghri
committed
$recipientElement->addAttribute('name', $podcast->owner_name);
$recipientElement->addAttribute('type', 'ILP');
$recipientElement->addAttribute(
'address',
$podcast->payment_pointer,
$recipientElement->addAttribute('split', '100');

Yassine Doghri
committed
}
$channel
->addChild(
'locked',
$podcast->is_locked ? 'yes' : 'no',
$podcast_namespace,
)
->addAttribute('owner', $podcast->owner_email);
if ($podcast->imported_feed_url !== null) {

Yassine Doghri
committed
$channel->addChild(
'previousUrl',
$podcast->imported_feed_url,
$podcast_namespace,
foreach ($podcast->podcasting_platforms as $podcastingPlatform) {

Yassine Doghri
committed
$podcastingPlatformElement = $channel->addChild(
'id',
$podcast_namespace,

Yassine Doghri
committed
$podcastingPlatformElement->addAttribute(
'platform',
$podcastingPlatform->slug,
);
if ($podcastingPlatform->link_content !== null) {

Yassine Doghri
committed
$podcastingPlatformElement->addAttribute(
'id',
$podcastingPlatform->link_content,
);
}
if ($podcastingPlatform->link_url !== null) {

Yassine Doghri
committed
$podcastingPlatformElement->addAttribute(
'url',
htmlspecialchars($podcastingPlatform->link_url),
);
}
}
foreach ($podcast->social_platforms as $socialPlatform) {

Yassine Doghri
committed
$socialPlatformElement = $channel->addChild(
'social',
$socialPlatform->link_content,
$podcast_namespace,
);
$socialPlatformElement->addAttribute(
'platform',
$socialPlatform->slug,
);
if ($socialPlatform->link_url !== null) {

Yassine Doghri
committed
$socialPlatformElement->addAttribute(
'url',
htmlspecialchars($socialPlatform->link_url),
);
}
foreach ($podcast->funding_platforms as $fundingPlatform) {

Yassine Doghri
committed
$fundingPlatformElement = $channel->addChild(
'funding',
$fundingPlatform->link_content,
$podcast_namespace,

Yassine Doghri
committed
$fundingPlatformElement->addAttribute(
'platform',
$fundingPlatform->slug,
if ($fundingPlatform->link_url !== null) {

Yassine Doghri
committed
$fundingPlatformElement->addAttribute(
'url',
htmlspecialchars($fundingPlatform->link_url),
);
}

Yassine Doghri
committed
foreach ($podcast->persons as $podcastPerson) {
$podcastPersonElement = $channel->addChild(
htmlspecialchars($podcastPerson->full_name),
$podcast_namespace,
$podcastPerson->role !== null &&
$podcastPerson->role !== null

Yassine Doghri
committed
$podcastPersonElement->addAttribute(
'role',
htmlspecialchars(
lang(
"PersonsTaxonomy.persons.{$podcastPerson->group}.roles.{$podcastPerson->role}.label",
'en',
),
),
if ($podcastPerson->group !== null) {

Yassine Doghri
committed
$podcastPersonElement->addAttribute(
'group',
htmlspecialchars(
lang(
"PersonsTaxonomy.persons.{$podcastPerson->group}.label",
'en',
),
),

Yassine Doghri
committed
$podcastPersonElement->addAttribute(
$podcastPerson->image->large_url,
if ($podcastPerson->information_url !== null) {

Yassine Doghri
committed
$podcastPersonElement->addAttribute(
$podcastPerson->information_url,
);
}
}

Yassine Doghri
committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// set main category first, then other categories as apple
add_category_tag($channel, $podcast->category);
foreach ($podcast->other_categories as $other_category) {
add_category_tag($channel, $other_category);
}
$channel->addChild(
'explicit',
$podcast->parental_advisory === 'explicit' ? 'true' : 'false',
$itunes_namespace,
);
$channel->addChild(
'author',
$podcast->publisher ? $podcast->publisher : $podcast->owner_name,
$itunes_namespace,
);
$channel->addChild('link', $podcast->link);
$owner = $channel->addChild('owner', null, $itunes_namespace);
$owner->addChild('name', $podcast->owner_name, $itunes_namespace);
$owner->addChild('email', $podcast->owner_email, $itunes_namespace);
$channel->addChild('type', $podcast->type, $itunes_namespace);
$podcast->copyright &&
$channel->addChild('copyright', $podcast->copyright);
$podcast->is_blocked &&
$channel->addChild('block', 'Yes', $itunes_namespace);
$podcast->is_completed &&
$channel->addChild('complete', 'Yes', $itunes_namespace);
$image = $channel->addChild('image');
$image->addChild('url', $podcast->image->feed_url);
$image->addChild('title', $podcast->title);
$image->addChild('link', $podcast->link);

Benjamin Bellamy
committed
if ($podcast->custom_rss !== null) {

Benjamin Bellamy
committed
array_to_rss(
[

Yassine Doghri
committed
'elements' => $podcast->custom_rss,

Benjamin Bellamy
committed
],

Yassine Doghri
committed
$channel,

Benjamin Bellamy
committed
);
}

Yassine Doghri
committed
foreach ($episodes as $episode) {
$item = $channel->addChild('item');
$item->addChild('title', $episode->title);
$enclosure = $item->addChild('enclosure');
$enclosure->addAttribute(
'url',
$episode->audio_file_analytics_url .
($serviceSlug === ''

Yassine Doghri
committed
? ''
: '?_from=' . urlencode($serviceSlug)),
);
$enclosure->addAttribute('length', $episode->audio_file_size);
$enclosure->addAttribute('type', $episode->audio_file_mimetype);
$item->addChild('guid', $episode->guid);
$item->addChild(
'pubDate',
$episode->published_at->format(DATE_RFC1123),
);
if ($episode->location !== null) {

Yassine Doghri
committed
$locationElement = $item->addChild(
'location',
htmlspecialchars($episode->location->name),

Yassine Doghri
committed
$podcast_namespace,
);
if ($episode->location->geo !== null) {

Yassine Doghri
committed
$locationElement->addAttribute(
'geo',
$episode->location->geo,

Yassine Doghri
committed
);
}
if ($episode->location->osm_id !== null) {

Yassine Doghri
committed
$locationElement->addAttribute(
'osm',
$episode->location->osm_id,

Yassine Doghri
committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
);
}
}
$item->addChildWithCDATA(
'description',
$episode->getDescriptionHtml($serviceSlug),
);
$item->addChild(
'duration',
$episode->audio_file_duration,
$itunes_namespace,
);
$item->addChild('link', $episode->link);
$episode_itunes_image = $item->addChild(
'image',
null,
$itunes_namespace,
);
$episode_itunes_image->addAttribute(
'href',
$episode->image->feed_url,
);
$episode->parental_advisory &&
$item->addChild(
'explicit',
$episode->parental_advisory === 'explicit'
? 'true'
: 'false',
$itunes_namespace,
);
$episode->number &&
$item->addChild('episode', $episode->number, $itunes_namespace);
$episode->season_number &&
$item->addChild(
'season',
$episode->season_number,
$itunes_namespace,
);
$item->addChild('episodeType', $episode->type, $itunes_namespace);
if ($episode->transcript_file_url) {
$transcriptElement = $item->addChild(
'transcript',
null,
$podcast_namespace,
);
$transcriptElement->addAttribute(
'url',
$episode->transcript_file_url,
);
$transcriptElement->addAttribute(
'type',
Mimes::guessTypeFromExtension(
pathinfo(
$episode->transcript_file_url,
PATHINFO_EXTENSION,
),
),
);
$transcriptElement->addAttribute(
'language',
$podcast->language_code,
);
}
if ($episode->chapters_file_url) {
$chaptersElement = $item->addChild(
'chapters',
null,
$podcast_namespace,
);
$chaptersElement->addAttribute(
'url',
$episode->chapters_file_url,
);
$chaptersElement->addAttribute(
'type',
'application/json+chapters',
);
}
foreach ($episode->soundbites as $soundbite) {
$soundbiteElement = $item->addChild(
'soundbite',
empty($soundbite->label) ? null : $soundbite->label,
$podcast_namespace,
);
$soundbiteElement->addAttribute(
'start_time',
$soundbite->start_time,
);
$soundbiteElement->addAttribute(
'duration',
$soundbite->duration,
);
}
foreach ($episode->persons as $episodePerson) {
$episodePersonElement = $item->addChild(
'person',
htmlspecialchars($episodePerson->full_name),

Yassine Doghri
committed
$podcast_namespace,
);
if (
!empty($episodePerson->role) &&
!empty($episodePerson->group)

Yassine Doghri
committed
) {
$episodePersonElement->addAttribute(
'role',
htmlspecialchars(
lang(
"PersonsTaxonomy.persons.{$episodePerson->group}.roles.{$episodePerson->role}.label",

Yassine Doghri
committed
[],
'en',
),
),
);
}
if (!empty($episodePerson->person_group)) {
$episodePersonElement->addAttribute(
'group',
htmlspecialchars(
lang(
"PersonsTaxonomy.persons.{$episodePerson->group}.label",

Yassine Doghri
committed
[],
'en',
),
),
);
}
$episodePersonElement->addAttribute(
'img',
$episodePerson->image->large_url,

Yassine Doghri
committed
);
if (!empty($episodePerson->information_url)) {

Yassine Doghri
committed
$episodePersonElement->addAttribute(
'href',
$episodePerson->information_url,

Yassine Doghri
committed
);
}
}
$episode->is_blocked &&
$item->addChild('block', 'Yes', $itunes_namespace);
if (!empty($episode->custom_rss)) {
array_to_rss(
[
'elements' => $episode->custom_rss,
],
$item,
);
}
}
return $rss->asXML();
}

Yassine Doghri
committed
if (!function_exists('add_category_tag')) {
/**
* Adds <itunes:category> and <category> tags to node for a given category
*/
function add_category_tag(SimpleXMLElement $node, Category $category): void
{
$itunes_namespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
$itunes_category = $node->addChild('category', '', $itunes_namespace);

Yassine Doghri
committed
$itunes_category->addAttribute(
'text',
$category->parent !== null
? $category->parent->apple_category
: $category->apple_category,

Yassine Doghri
committed
if ($category->parent !== null) {
$itunes_category_child = $itunes_category->addChild(
'category',

Yassine Doghri
committed
$itunes_namespace,
);
$itunes_category_child->addAttribute(
'text',
$category->apple_category,
);
$node->addChild('category', $category->parent->apple_category);
}
$node->addChild('category', $category->apple_category);
}
}

Benjamin Bellamy
committed

Yassine Doghri
committed
if (!function_exists('rss_to_array')) {
/**
* Converts XML to array
*
* FIXME: param should be SimpleRSSElement
*
* @return array<string, mixed>

Yassine Doghri
committed
*/
function rss_to_array(SimpleXMLElement $rssNode): array

Yassine Doghri
committed
{
$nameSpaces = [
'',
'http://www.itunes.com/dtds/podcast-1.0.dtd',
'https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md',
];
$arrayNode = [];
$arrayNode['name'] = $rssNode->getName();
$arrayNode['namespace'] = $rssNode->getNamespaces(false);
foreach ($rssNode->attributes() as $key => $value) {

Benjamin Bellamy
committed
$arrayNode['attributes'][$key] = (string) $value;
}
$textcontent = trim((string) $rssNode);

Yassine Doghri
committed
if (strlen($textcontent) > 0) {
$arrayNode['content'] = $textcontent;

Benjamin Bellamy
committed
}

Yassine Doghri
committed
foreach ($nameSpaces as $currentNameSpace) {
foreach ($rssNode->children($currentNameSpace) as $childXmlNode) {

Yassine Doghri
committed
$arrayNode['elements'][] = rss_to_array($childXmlNode);
}
}
return $arrayNode;

Benjamin Bellamy
committed
}
}

Yassine Doghri
committed
if (!function_exists('array_to_rss')) {
/**
* Inserts array (converted to XML node) in XML node
*
* @param array<string, mixed> $arrayNode

Yassine Doghri
committed
* @param SimpleRSSElement $xmlNode The XML parent node where this arrayNode should be attached
*/
function array_to_rss(
array $arrayNode,
SimpleRSSElement &$xmlNode
): SimpleRSSElement {

Yassine Doghri
committed
if (array_key_exists('elements', $arrayNode)) {
foreach ($arrayNode['elements'] as $childArrayNode) {
$childXmlNode = $xmlNode->addChild(
$childArrayNode['name'],
$childArrayNode['content'] ?? null,
empty($childArrayNode['namespace'])
? null
: current($childArrayNode['namespace']),
);
if (array_key_exists('attributes', $childArrayNode)) {
foreach (
$childArrayNode['attributes']
as $attributeKey => $attributeValue
) {
$childXmlNode->addAttribute(
$attributeKey,
$attributeValue,
);
}

Benjamin Bellamy
committed
}

Yassine Doghri
committed
array_to_rss($childArrayNode, $childXmlNode);

Benjamin Bellamy
committed
}
}

Yassine Doghri
committed
return $xmlNode;