<?php
// TODO
// lisence
// filter by date
// if rss.php then use switches
// host out of eps
// explicit from db
# 

$mimetype = "audio/ogg";
$format = "ogg";


require "/home/hpr/php/include.php";
$query = "SELECT hosts.host, eps.id, eps.series, eps.title, eps.notes, eps.date, hosts.email, eps.hostid, eps.explicit, eps.version, eps.valid, eps.duration, assets.size
FROM eps, hosts, assets
WHERE eps.valid=1
AND eps.hostid = hosts.hostid
AND eps.id = assets.episode_id
AND eps.duration > 0
AND eps.date >= UTC_DATE()";

if (isset($_GET['format'])) {
    $this_format = $_GET['format'];
    if ( $this_format === "mp3" ) {
        $format = "mp3";
    }
    if ( $this_format === "ogg" ) {
        $format = "ogg";
    }
    if ( $this_format === "spx" ) {
        $format = "spx";
    }
}

$query = "${query}\nAND assets.filename REGEXP \"^hpr[0-9]+.$format\"";
$query = "${query}\nAND assets.extension = '$format'";

$html = 1;
if (isset($_GET['html'])) {
    $html = $_GET['html'];
    if ( $html === "0" ) {
      $html = 0;
    }
}

$full = "1";
if (isset($_GET['full'])) {
    $full = $_GET['full'];
    if ( $full === "1" ) {
        $feed = "total";
    }
    if ( $full === "0" ) {
        $feed = "tenday";
    }
}

if (isset($_GET['hostid'])) {
    $hostid = $_GET['hostid'];
    $hostid = intval($hostid);
    if (is_int(intval($hostid))) {
        $query = "$query AND hosts.hostid = '$hostid'";
    }
}
if (isset($_GET['series'])) {
    $series = $_GET['series'];
    $series = intval($series);
    if (is_int(intval($series))) {
        $query = "$query AND eps.series = '$series'";
    }
}

$query = "$query ORDER BY id DESC ";

if (isset($_GET['limit'])) {
    $this_limit = $_GET['limit'];
    $this_limit = intval($this_limit);
    if (is_int(intval($this_limit))) {
        $limit = $this_limit;
    }
}

header("Content-type: application/xml");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

date_default_timezone_set('UTC');

print '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";

?>
<rss version="2.0" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" >
<?php print "<!--\n${query}\n-->\n"; ?>
<channel>
  <title>Hacker Public Radio - Future Feed</title>
  <link><?php print "${baseurl}"; ?>about.html</link>
  <itunes:subtitle>A daily show hosted the community on topics that are of interest to hackers and hobbyists.</itunes:subtitle>
  <description>Hacker Public Radio is an podcast that releases shows every weekday Monday through Friday. Our shows are produced by the community (you) and can be on any topic that are of interest to hackers and hobbyists. This is the shows that are in the future feed.</description>
  <language>en-us</language>
  <itunes:category text="Technology">
    <itunes:category text="Tech News"/>
  </itunes:category>
  <itunes:category text="Education">
    <itunes:category text="Training"/>
  </itunes:category>
  <itunes:image href="<?php print "${baseurl}"; ?>images/hpr_feed_itunes.png"/>
  <itunes:explicit>yes</itunes:explicit>
  <itunes:author>Hacker Public Radio</itunes:author>
  <itunes:keywords>Community Radio, Tech Interviews, Linux, Open, Hobby, Software Freedom</itunes:keywords>
  <copyright>Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) License</copyright>
  <managingEditor>feedback@NOSPAM-hackerpublicradio.org (HPR Feedback)</managingEditor> 
<!--   <author>feedback@NOSPAM-hackerpublicradio.org (HPR Feedback)</author> -->
  <itunes:owner>    
    <itunes:name>HPR Volunteer</itunes:name>
    <itunes:email>admin@hackerpublicradio.org</itunes:email>
  </itunes:owner>
  <webMaster>admin@hackerpublicradio.org (HPR Volunteer)</webMaster> 
  <generator>kate</generator> 
  <docs>http://www.rssboard.org/rss-specification</docs>
  <ttl>43200</ttl>
  <skipDays>
    <day>Saturday</day>
    <day>Sunday</day>
  </skipDays>
  <image>
    <url><?php print "${baseurl}"; ?>images/hpr_feed_small.png</url>
    <title>Hacker Public Radio</title>
    <link><?php print "${baseurl}"; ?>about.html</link>
    <description>The Hacker Public Radio Old Microphone Logo</description>
    <height>164</height>
    <width>144</width>
  </image>
  <googleplay:author>HPR Volunteer</googleplay:author>
  <googleplay:description>Hacker Public Radio is an podcast that releases shows every weekday Monday through Friday. Our shows are produced by the community (you) and can be on any topic that are of interest to hackers and hobbyists.</googleplay:description>
  <googleplay:email>admin@hackerpublicradio.org</googleplay:email>
  <googleplay:image href="<?php print "${baseurl}"; ?>images/hpr_feed_itunes.png"/>
  <googleplay:category text="Technology"/>
<?php
print "  <atom:link href=\"${baseurl}". str_replace('&', '&amp;', $_SERVER["REQUEST_URI"]) ."\" rel=\"self\" type=\"application/rss+xml\" />\n";
print "  <pubDate>".date(DATE_RFC1123, strtotime(date('Y-m-d')))."</pubDate>\n";

//Set $r to SQL query for execution agains the table

if ($result = mysqli_query($connection, $query)) {
  while ($row = mysqli_fetch_array($result)) {
    $id = fixid(stripslashes($row['id']));
    $version = $row['version'];
    if ( $version === "0" ) {
        $version = "";
    }
    else {
        $version = ".${version}";
    }
    $episode_explicit = $row['explicit'];
    if ($episode_explicit == 0) {
        $episode_explicit = "Clean";
    }
    else{
        $episode_explicit = "Explicit";
    } 
    $url="${baseurl}eps/hpr${id}${version}";
    if (isset($_GET['hostidinfilename'])) {
        $hostidinfilename = $_GET['hostidinfilename'];
        if ( $hostidinfilename === "1" ) {
          $url = $url . "_host-" . htmlspecialchars(stripslashes(strip_tags($row["hostid"])));
        }
    }
    if ( isset($_GET['seriesidinfilename']) ) {
        $seriesidinfilename = $_GET['seriesidinfilename'];
        if ( $seriesidinfilename === "1" ) {
          $url = $url . "_series-" . htmlspecialchars(stripslashes(strip_tags($row["series"])));
        }
    }
    $url = $url . "." . $format;
  
    $itunes_summary = htmlspecialchars(stripslashes(strip_tags(substr($row["notes"],0,4000))));
    $show_notes = $row["notes"];
    $warning = "<p>This show has been flagged as $episode_explicit by the host.</p>\n";
    $show_notes = $show_notes . utf8_encode ( $warning );
    echo "  <item>\n";
    if ( $episode_explicit === "Clean" ) {
        echo "    <itunes:explicit>no</itunes:explicit>\n";
        echo "    <googleplay:explicit>No</googleplay:explicit>\n";
    }
    else {
        echo "    <itunes:explicit>yes</itunes:explicit>\n";
        echo "    <googleplay:explicit>Yes</googleplay:explicit>\n";
    }
    
    echo "    <title>HPR$id: " . htmlspecialchars(stripslashes(strip_tags($row["title"]))) . "</title>\n";
    //    echo "<description> " . ($row["title"]) . "</description>\n";
    $email_padded = formatemail($row['email']);
    echo "    <author>". $email_padded . " (" . htmlspecialchars(stripslashes(strip_tags($row["host"]))) .")</author>\n";
    echo "    <googleplay:author>". $email_padded . " (" . htmlspecialchars(stripslashes(strip_tags($row["host"]))) .")</googleplay:author>\n";
    echo "    <itunes:author>". $email_padded . " (" . htmlspecialchars(stripslashes(strip_tags($row["host"]))) .")</itunes:author>\n";
    echo "    <googleplay:image href=\"${baseurl}images/hpr_feed_itunes.png\"/>\n";
    echo "    <link>${baseurl}eps/hpr".$id."/index.html</link>\n";
    echo "    <description><![CDATA[" . $show_notes . "]]>\n</description>\n";
//       echo "    <googleplay:description><![CDATA[" . $show_notes . "]]>\n</googleplay:description>\n";
    echo "    <itunes:summary><![CDATA[" . $itunes_summary . "]]>\n</itunes:summary>\n";
    echo "    <pubDate>" .date(DATE_RFC1123, strtotime($row['date'])) . "</pubDate>\n";
    $length = $row['size'];    
    echo "    <enclosure url=\"$url\" length=\"$length\" type=\"$mimetype\"/>\n";
    echo "    <guid>" . $url . "</guid>\n";
    echo "  </item>\n";
  }
}
//Display non-connection errors
//Close sql connection
mysqli_close($connection);
echo "  </channel>
</rss>
";
?>
