<?php
require "/home/hpr/php/include.php";

if (isset($_GET['id'])) {
  $id = $_GET['id'];
  $result = mysqli_query($connection, 'SELECT MAX(id) FROM eps;');
  if (!isset($result)) {
      die('Could not query:' . mysqli_error());
  }
  $maxhost_array = mysqli_fetch_row( $result );
  $maxhost = $maxhost_array[0]; 
  $num_get_args=0;
  foreach($_GET as $k => $v) { 
    ++$num_get_args; 
  }
  if ( (strval(intval($id)) != strval($id)) OR ( intval($id) <= 0 ) OR ( intval($id) > $maxhost ) OR ( $num_get_args > 1 ) ){
      exit;
  }
  $query = "SELECT id FROM eps WHERE id = '$id'";
  $result = @mysqli_query($connection, $query);
  if($result === FALSE) {
    call412( "a9564ebc3289b7a14551baf8ad5ec60a" );
  }
  else {
    $db = mysqli_fetch_array($result, MYSQLI_ASSOC);
    if ( empty($db["id"]) ) {
      call412( "a9564ebc3289b7a14551baf8ad5ec60a" );
    }
  }
}
else {
  call412( "a9564ebc3289b7a14551baf8ad5ec60a" );
  exit;
}
Header('Content-type: text/tab-separated-values');
header("Content-disposition: inline; filename=say.txt");

$ep_retrieve = "SELECT
	UNIX_TIMESTAMP(eps.date) AS timestamp,
	eps.title,
	eps.duration,
	eps.summary,
	hosts.host,
	eps.hostid,
	eps.series,
	eps.license,
	licenses.long_name,
	eps.explicit
FROM
	eps
	LEFT JOIN hosts ON eps.hostid = hosts.hostid
	LEFT JOIN licenses ON licenses.short_name = eps.license 	
WHERE
	hosts.valid = '1'
	AND eps.id = '$id'";
if ($result = mysqli_query($connection, $ep_retrieve)) {
  while ($row = mysqli_fetch_array($result)) {
    $date = $row['timestamp'];
    $title = $row['title'];
    $duration = $row['duration'];
    $summary = $row['summary'];
    $host = $row['host'];
    $hostid = $row['hostid'];
    $series = $row['series'];
    $license = $row['license'];
    $license_long_name = $row['long_name'];
    $explicit = $row['explicit'];

    $id = fixid($id); 

    $host_retrieve = "SELECT host, espeak_name FROM hosts WHERE hostid = '$hostid'";
    $result1 = mysqli_query($connection, $host_retrieve);
    $row1 = mysqli_fetch_array($result1); 
    $espeak_name = $row1['espeak_name'];
    
    $epcountquery = "SELECT count( id ) AS total FROM eps WHERE hostid = '$hostid'";
    $result2 = mysqli_query($connection, $epcountquery);
    $row2 = mysqli_fetch_array($result2); 
    $total = $row2['total'];
    if ( $total == 1 ) {
      $host_notes = ". It is the first show by new host ${espeak_name}, ";
    }
    else if ($total % 10 == 0)   {
      $host_notes = ". It is the ${total}th show of ${espeak_name}, ";
    }
    else {
      $host_notes = ". It is hosted by ${espeak_name}, ";
    }
  }
}
$HPR_summary = "This is Hacker Public Radio episode $id for " . date("l", $date) . " the " . date("jS", $date) . " of " . date("F Y", $date) . ". Todays show is entitled. ${title}.";
if ($series > "0"){
  $series_query = mysqli_query($connection, "SELECT name, description FROM miniseries WHERE id = '$series'");
  $series_result = mysqli_fetch_array($series_query);
  $series_title = $series_result['name'];
  $desc = $series_result['description'];
  $HPR_summary = "${HPR_summary} It is part of the series \"$series_title\"";
}
$HPR_summary = "${HPR_summary} ${host_notes} and is about " . round($duration/60) . " minutes long. It carries ";
if ($explicit == 0) {
    $HPR_summary = "${HPR_summary} a clean flag. ";
    $explicit = "Clean";
}
else{
    $HPR_summary = "${HPR_summary} an explicit flag. ";
    $explicit = "Explicit";
} 
if ( !empty( $summary ) ) {
  $HPR_summary = "${HPR_summary}. The summary is. $summary";
}
if (strcmp($license, "CC-BY-SA" ) !== 0) {
    $HPR_summary = "${HPR_summary}. Todays show is licensed under a $license_long_name license.";
} 

$HPR_summary = str_replace($host,$espeak_name,$HPR_summary);

echo "HPR_summary: ${HPR_summary}\n";
echo "HPR_album: Hacker Public Radio\n";
echo "HPR_artist: ${host}\n";
echo "HPR_hostid: ${hostid}\n";
echo "HPR_comment: https://hackerpublicradio.org ${explicit}; $summary\n";
echo "HPR_genre: Podcast\n";
echo "HPR_license: ${license}\n";
echo "HPR_title: ${title}\n";
echo "HPR_track: $id\n";
echo "HPR_year: " . date("Y", $date) . "\n";
echo "HPR_duration: ${duration}\n";
echo "HPR_explicit: ${explicit}\n";
?>
