Skip to content
Snippets Groups Projects
rss_helper.php 19.9 KiB
Newer Older
/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

use CodeIgniter\I18n\Time;
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
    function get_rss_feed(Podcast $podcast, ?string $serviceSlug = null): string
        $itunesNamespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
            '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='{$itunesNamespace}' xmlns:podcast='{$podcastNamespace}' xmlns:content='http://purl.org/rss/1.0/modules/content/'></rss>",
        $atomLink->addAttribute('href', $podcast->feed_url);
        $atomLink->addAttribute('rel', 'self');
        $atomLink->addAttribute('type', 'application/rss+xml');
        if ($podcast->new_feed_url !== null) {
            $channel->addChild(
                'new-feed-url',
                $podcast->new_feed_url,
        // the last build date corresponds to the creation of the feed.xml cache
        $channel->addChild(
            'lastBuildDate',
            (new Time('now'))->format(DATE_RFC1123),
        $channel->addChild(
            'generator',
            'Castopod Host - https://castopod.org/',
        $channel->addChild('docs', 'https://cyber.harvard.edu/rss/rss.html');
        $channel->addChild('title', $podcast->title);
        $channel->addChildWithCDATA('description', $podcast->description_html);
        $itunesImage = $channel->addChild('image', null, $itunesNamespace);
        // FIXME: This should be downsized to 1400x1400
        $itunesImage->addAttribute('href', $podcast->image->url);
        $channel->addChild('language', $podcast->language_code);
        if ($podcast->location !== null) {
                htmlspecialchars($podcast->location->name),
            if ($podcast->location->geo !== null) {
                $locationElement->addAttribute('geo', $podcast->location->geo);
            if ($podcast->location->osm !== null) {
                $locationElement->addAttribute('osm', $podcast->location->osm);
        if ($podcast->payment_pointer !== null) {
            $valueElement->addAttribute('type', 'webmonetization');
            $valueElement->addAttribute('method', '');
            $valueElement->addAttribute('suggested', '');
            $recipientElement = $valueElement->addChild(
                'valueRecipient',
                null,
            $recipientElement->addAttribute('name', $podcast->owner_name);
            $recipientElement->addAttribute('type', 'ILP');
            $recipientElement->addAttribute(
                'address',
                $podcast->payment_pointer,
            $recipientElement->addAttribute('split', '100');
        }
        $channel
            ->addChild(
                'locked',
                $podcast->is_locked ? 'yes' : 'no',
        if ($podcast->imported_feed_url !== null) {
            $channel->addChild(
                'previousUrl',
                $podcast->imported_feed_url,
        foreach ($podcast->podcasting_platforms as $podcastingPlatform) {
            $podcastingPlatformElement = $channel->addChild(
                'id',
            $podcastingPlatformElement->addAttribute(
                'platform',
                $podcastingPlatform->slug,
            );
            if ($podcastingPlatform->link_content !== null) {
                $podcastingPlatformElement->addAttribute(
                    'id',
                    $podcastingPlatform->link_content,
                );
            }
            if ($podcastingPlatform->link_url !== null) {
                $podcastingPlatformElement->addAttribute(
                    'url',
                    htmlspecialchars($podcastingPlatform->link_url),
                );
            }
        }

        foreach ($podcast->social_platforms as $socialPlatform) {
            $socialPlatformElement = $channel->addChild(
                'social',
                $socialPlatform->link_content,
            );
            $socialPlatformElement->addAttribute(
                'platform',
                $socialPlatform->slug,
            );
            if ($socialPlatform->link_url !== null) {
                $socialPlatformElement->addAttribute(
                    'url',
                    htmlspecialchars($socialPlatform->link_url),
                );
            }
        foreach ($podcast->funding_platforms as $fundingPlatform) {
            $fundingPlatformElement = $channel->addChild(
                'funding',
                $fundingPlatform->link_content,
            $fundingPlatformElement->addAttribute(
                'platform',
                $fundingPlatform->slug,
            if ($fundingPlatform->link_url !== null) {
                $fundingPlatformElement->addAttribute(
                    'url',
                    htmlspecialchars($fundingPlatform->link_url),
                );
            }
        foreach ($podcast->persons as $podcastPerson) {
            $podcastPersonElement = $channel->addChild(
                htmlspecialchars($podcastPerson->full_name),
                $podcastPerson->role !== null &&
                $podcastPerson->role !== null
                            "PersonsTaxonomy.persons.{$podcastPerson->group}.roles.{$podcastPerson->role}.label",
            if ($podcastPerson->group !== null) {
                            "PersonsTaxonomy.persons.{$podcastPerson->group}.label",
                $podcastPerson->image->large_url,
            if ($podcastPerson->information_url !== null) {
                    $podcastPerson->information_url,
        // 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',
        );

        $channel->addChild(
            'author',
            $podcast->publisher ? $podcast->publisher : $podcast->owner_name,
        $owner = $channel->addChild('owner', null, $itunesNamespace);
        $owner->addChild('name', $podcast->owner_name, $itunesNamespace);
        $owner->addChild('email', $podcast->owner_email, $itunesNamespace);
        $channel->addChild('type', $podcast->type, $itunesNamespace);
        $podcast->copyright &&
            $channel->addChild('copyright', $podcast->copyright);
        $podcast->is_blocked &&
            $channel->addChild('block', 'Yes', $itunesNamespace);
            $channel->addChild('complete', 'Yes', $itunesNamespace);

        $image = $channel->addChild('image');
        $image->addChild('url', $podcast->image->feed_url);
        $image->addChild('title', $podcast->title);
        $image->addChild('link', $podcast->link);
        if ($podcast->custom_rss !== null) {
        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 .
                        ? ''
                        : '?_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) {
                    htmlspecialchars($episode->location->name),
                if ($episode->location->geo !== null) {
                    );
                }
            }
            $item->addChildWithCDATA(
                'description',
                $episode->getDescriptionHtml($serviceSlug),
            );
            $item->addChild(
                'duration',
                $episode->audio_file_duration,
                'href',
                $episode->image->feed_url,
            );

            $episode->parental_advisory &&
                $item->addChild(
                    'explicit',
                    $episode->parental_advisory === 'explicit'
                        ? 'true'
                        : 'false',
                $item->addChild('episode', $episode->number, $itunesNamespace);
            $episode->season_number &&
                $item->addChild(
                    'season',
                    $episode->season_number,
            $item->addChild('episodeType', $episode->type, $itunesNamespace);

            if ($episode->transcript_file_url) {
                $transcriptElement = $item->addChild(
                    'transcript',
                    null,
                );
                $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,
                );
                $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,
                );
                $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),
                    !empty($episodePerson->role) &&
                    !empty($episodePerson->group)
                ) {
                    $episodePersonElement->addAttribute(
                        'role',
                        htmlspecialchars(
                            lang(
                                "PersonsTaxonomy.persons.{$episodePerson->group}.roles.{$episodePerson->role}.label",
                                [],
                                'en',
                            ),
                        ),
                    );
                }
                if (!empty($episodePerson->person_group)) {
                    $episodePersonElement->addAttribute(
                        'group',
                        htmlspecialchars(
                            lang(
                                "PersonsTaxonomy.persons.{$episodePerson->group}.label",
                    $episodePerson->image->large_url,
                if (!empty($episodePerson->information_url)) {
                        $episodePerson->information_url,
                $item->addChild('block', 'Yes', $itunesNamespace);

            if (!empty($episode->custom_rss)) {
                array_to_rss(
                    [
                        'elements' => $episode->custom_rss,
                    ],
                    $item,
                );
            }
        }

        return $rss->asXML();
    }
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
    {
        $itunesNamespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
        $itunesCategory = $node->addChild('category', '', $itunesNamespace);
        $itunesCategory->addAttribute(
            'text',
            $category->parent !== null
                ? $category->parent->apple_category
                : $category->apple_category,
            $itunesCategoryChild = $itunesCategory->addChild(
                'text',
                $category->apple_category,
            );
            $node->addChild('category', $category->parent->apple_category);
        }
        $node->addChild('category', $category->apple_category);
if (!function_exists('rss_to_array')) {
    /**
     * Converts XML to array
     *
     * FIXME: param should be SimpleRSSElement
     *
     * @return array<string, mixed>
    function rss_to_array(SimpleXMLElement $rssNode): array
    {
        $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) {
        $textcontent = trim((string) $rssNode);
        if (strlen($textcontent) > 0) {
            $arrayNode['content'] = $textcontent;
            foreach ($rssNode->children($currentNameSpace) as $childXmlNode) {
                $arrayNode['elements'][] = rss_to_array($childXmlNode);
            }
        }

        return $arrayNode;
if (!function_exists('array_to_rss')) {
    /**
     * Inserts array (converted to XML node) in XML node
     *
     * @param array<string, mixed> $arrayNode
     * @param SimpleRSSElement $xmlNode The XML parent node where this arrayNode should be attached
     */
    function array_to_rss(
        array $arrayNode,
        SimpleRSSElement &$xmlNode
    ): SimpleRSSElement {
        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,
                        );
                    }