<?php
header("Content-Type: text/xml");

function slugify($string) {
    $string = htmlentities($string);
    $string = strtolower($string);
    $string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
    $string = preg_replace("/([^a-zA-Z0-9]+)/", "-", html_entity_decode($string));
    $string = trim($string, "-");

    return $string;
}

require_once __DIR__ . '/libs/vendor/autoload.php';
require_once __DIR__ . '/libs/mp3file/mp3file.php';

$config = new EvisuaKit_Config_Ini(__DIR__ . '/application/config/config.ini', 'live');

$dsn = 'mysql:host=' . $config->db->host . ';dbname=' . $config->db->dbname;
$pdo = new PDO($dsn, $config->db->username, $config->db->password);

$podcasts = $pdo->query("
    SELECT *
    FROM `downloads`
    WHERE `typeId` = 7
    AND `deleted` = '0'
    AND `published` = '1'
    ORDER BY `downloadId` DESC
")->fetchAll();

$data = array();

foreach ($podcasts as $podcast) {
    $file = __DIR__ . '/filestorage/downloads/' . $podcast['path'];

    if (!file_exists($file)) {
        continue;
    }

    $mp3 = new mp3file($file);
    $md = $mp3->get_metadata();

    $data[] = array(
        'title' => $podcast['name'],
        'url' => 'http://www.fnatic.com/downloads/' . $podcast['downloadId'] . '/' . slugify($podcast['name']) . '.html',
        'pubDate' => date('D, j M Y H:i:s O', $podcast['postStamp']),
        'desc' => nl2br(strip_tags($podcast['desc'])),
        'file' => 'http://www.fnatic.com/filestorage/downloads/' . rawurlencode($podcast['path']),
        'size' => $md['Filesize'],
        'duration' => $md['Length mm:ss'],
    );
}
?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <atom:link href="http://www.fnatic.com/itunes.rss" rel="self" type="application/rss+xml" />
        <title>FNATIC Radio</title>
        <link>http://www.fnatic.com</link>
        <language>en-us</language>
        <copyright>℗ &amp; © 2013, FNATIC PTY LTD</copyright>
        <itunes:subtitle>
            A show about competitive gaming, e-sports and FNATIC.
        </itunes:subtitle>
        <itunes:author>FNATIC Team</itunes:author>
        <itunes:summary>
            The Fnatic Team is a professional video gaming team, consisting of players from across the globe who all make a living through competing in video game tournaments. Fnatic is considered to have world class squads in Counter-Strike, StarCraft II, League of Legends, Dota 2, ShootMania: Storm and Call of Duty.
        </itunes:summary>
        <description>
            The Fnatic Team is a professional video gaming team, consisting of players from across the globe who all make a living through competing in video game tournaments. Fnatic is considered to have world class squads in Counter-Strike, StarCraft II, League of Legends, Dota 2, ShootMania: Storm and Call of Duty.
        </description>
        <itunes:owner>
            <itunes:name>FNATIC Radio</itunes:name>
            <itunes:email>carn@fnatic.com</itunes:email>
        </itunes:owner>
        <itunes:image href="http://www.fnatic.com/itunes.jpg"/>
        <itunes:category text="Games &amp; Hobbies">
            <itunes:category text="Video Games"/>
        </itunes:category>
        <?php foreach ($data as $item): ?>
	    <item>
            <itunes:image href="http://www.fnatic.com/itunes.jpg"/>
            <title><?php echo $item['title'] ?></title>
            <link><?php echo $item['url'] ?></link>
            <guid><?php echo $item['url'] ?></guid>
            <category>Video Games</category>
            <pubDate><?php echo $item['pubDate'] ?></pubDate>
            <description><![CDATA[<?php echo $item['desc'] ?>]]></description>
            <itunes:summary><![CDATA[<?php echo $item['desc'] ?>]]></itunes:summary>
            <enclosure url="<?php echo $item['file'] ?>" length="<?php echo $item['size'] ?>" type="audio/mpeg"/>
            <itunes:duration><?php echo $item['duration'] ?></itunes:duration>
            <itunes:keywords>
                fnatic, esports, esport, eswc, counterstrike, cs, counter-strike, dreamhack, mlg, starcraft, lol, league of legends
            </itunes:keywords>
            <itunes:explicit>no</itunes:explicit>
	    </item>
	    <?php endforeach; ?>
    </channel>
</rss>
