<?php
// http://www.podcast411.com/howto_1.html
// http://www.apple.com/itunes/whatson/podcasts/specs.html
// https://phobos.apple.com/WebObjects/MZFinance.woa/wa/pingPodcast?id=301712190

require('_lib/_standalone.php');
require('_lang/' . WHICHLANG . '/mainlayout.php');
//ini_set('display_errors', 1);

// The problem, however with the application/rss+xml content type is that Firefox and IE prompt you to download the XML file instead of displaying it in the browser like it would a text/xml document.
header('Content-Type: text/xml;charset=UTF-8');
//text/xml
//application/rss+xml
//application/xml


// I tried DATE_FORMAT(publishDate, '%a, %d %b %Y %H:%i:%s') and it's the correct format, but it's in the wrong timezone

// same query as in htdocs/grfm/index.php
$query = "SELECT id, textTypeId, header, text, UNIX_TIMESTAMP(publishDate) AS publishTimestamp FROM gr". WHICHLANG .".texts WHERE groupingId = 4887" .
" ORDER BY publishDate DESC LIMIT 20 ";

ConnectMainDb();
$result = QueryMainDb($query);
CloseMainDb();

$durations = array(
'15:48', // episode 1
'14:26', // episode 2
'20:17', // episode 3
'16:48', // episode 4
'19:03', // episode 5
'18:01', // episode 6
'19:28', // episode 7
'22:15', // episode 8
'19:13', // episode 9
'21:57', // episode 10
'27:06', // episode 11
'25:24', // episode 12
'22:35', // episode 13
'37:15', // episode 14
'25:37', // episode 15
'25:37', // episode 16
'28:15', // episode 17
'20:33' // episode 18
);

$lastBuildDate = '';
$items = '';
$episode = $result->rowCount();
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
	$text = $row['text'];
	$text = str_replace('{', '', str_replace('}', '', $text));
	$text = preg_replace('{<a .+</a>}U', '', $text);
	// $text = preg_replace('{<b>.+</b>}U', '', $text); Who coded this? Why? It's obviously a bad idea.
	$text = strip_tags($text);
	$text = preg_replace('{\n?&lt;bild&gt;.*&lt;/bild&gt;\n?}U', '', $text); // also strip our own custom tag for images inside the text
	$text = preg_replace('{\n?&lt;video&gt;\n?}U', '', $text); // also strip our own custom tag for videos inside the text
	$text = trim($text);

	$date = gmdate("D, d M Y H:i:s", $row['publishTimestamp']).' GMT';

	if (!$lastBuildDate)
		$lastBuildDate = $date;

	$path = 'media/radio/gr_radio_avsnitt'.$episode.'.mp3';

	$items .= '<item>';
//	$items .= '	<title>'.htmlspecialchars($row['header'], ENT_NOQUOTES).'</title>';
	$items .= '	<title>GRFM - Avsnitt #'.$episode.'</title>';
	$items .= '	<description>'.utf8_encode(htmlspecialchars($text,ENT_QUOTES)).'</description>';
	$items .= '	<link>'.$config['url'].TextType($langTextTypeUrl, $row['textTypeId']).$row['id'].'/'.SeoFriendlyUrlEncode($row['header']).'/</link>';
	$items .= '	<guid>'.$config['url'].TextType($langTextTypeUrl, $row['textTypeId']).$row['id'].'/'.SeoFriendlyUrlEncode($row['header']).'/</guid>';
	$items .= '	<pubDate>'.$date.'</pubDate>';
	$items .= '	<enclosure url="http://www.gamereactor.se/'.$path.'" length="'.filesize($config['localPath'].$path).'" type="audio/mpeg"/>';
	if (isset($durations[$episode-1]))
		$items .= '	<itunes:duration>'.$durations[$episode-1].'</itunes:duration>';
	$items .= '</item>';

	$episode--;
}
$result->closeCursor();
unset($result);





// http://www.rssboard.org/rss-specification
//managingEditor  Email address for person responsible for editorial content.
//webMaster       Email address for person responsible for technical issues relating to channel.
//lastBuildDate   The last time the content of the channel changed.
//<lastBuildDate>Tue, 01 May 2007 04:00:00 GMT</lastBuildDate>

echo '<', '?xml version="1.0" encoding="UTF-8"?', '>
';
?>
<rss
    xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
    version="2.0">
<channel>
	<title>GRFM - Gamereactor Radio</title>
	<description><?=utf8_encode('Gamereactors podcast om TV-spel - recensioner, nyheter och reportage från Nordens största speltidning')?></description>
	<link>http://www.gamereactor.se/</link>
	<category domain="">Video Games</category>
	<copyright>Gamereactor 2008</copyright>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<language>sv-se</language>
	<lastBuildDate><?=$lastBuildDate?></lastBuildDate>
	<pubDate><?=$lastBuildDate?></pubDate>
	<managingEditor>jonas.elfving@gamereactor.se (Jonas Elfving)</managingEditor>
	<webMaster>emil.hall@gamereactor.se (Emil Hall)</webMaster>
	<skipHours>
		<hour>20</hour>
		<hour>21</hour>
		<hour>22</hour>
		<hour>23</hour>
		<hour>0</hour>
		<hour>1</hour>
		<hour>2</hour>
		<hour>3</hour>
		<hour>4</hour>
		<hour>5</hour>
		<hour>6</hour>
		<hour>7</hour>
	</skipHours>
	<skipDays>
		<day>Saturday</day>
		<day>Sunday</day>
	</skipDays>
	<ttl>180</ttl>
	<itunes:subtitle>GRFM - Gamereactor Radio</itunes:subtitle>
	<itunes:summary><?=utf8_encode('Gamereactors podcast om TV-spel - recensioner, nyheter och reportage från Nordens största speltidning. Sänds varje till varannan vecka.')?></itunes:summary>
	<itunes:owner>
		<itunes:name>Jonas Elfving</itunes:name>
		<itunes:email>jonas.elfving@gamereactor.se</itunes:email>
	</itunes:owner>
	<itunes:author>Gamereactor.se</itunes:author>
	<itunes:category text="Games &amp; Hobbies">
		<itunes:category text="Video Games"/>
	</itunes:category>
	<itunes:keywords>Gamereactor, grfm, gamereactor radio, tv-spel, datorspel, spelradio, xbox 360, wii, playstation 3, ps3, nintendo, sony</itunes:keywords>
	<itunes:image href="http://www.gamereactor.se/rss/grfm.jpg"/>
	<itunes:explicit>no</itunes:explicit>
	<itunes:block>no</itunes:block>
	<image>
		<url>http://www.gamereactor.se/rss/grfm.jpg</url>
		<title>GRFM - Gamereactor Radio</title>
		<link>http://www.gamereactor.se/</link>
		<width>144</width>
		<height>144</height>
	</image>
<?
	echo $items;

?></channel>
</rss>