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

date_default_timezone_set('UTC');

if (isset($_GET['id'])) {
  $id = $_GET['id'];
  $result = mysqli_query($connection, 'SELECT MAX(id) FROM eps;');
  if (!isset($result)) {
    problem( "45f606ad99fe4fca7430b7b5bba1c681" );
    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 ) ){
    problem( "ea860134910fecd136229e45262709d7" );
    exit;
  }
  $query = "SELECT id FROM eps WHERE id = '$id'";
  $result = @mysqli_query($connection, $query);
  if($result === FALSE) {
    problem( "dc5b8dae7ea2a7e70ac0b7ea65ce2d12" );
  }
  else {
    $db = mysqli_fetch_array($result, MYSQLI_ASSOC);
    if ( empty($db["id"]) ) {
      problem( "2b6462ff2389405a796066dfc73ccf55" );
    }
  }
}
else {
  problem( "ae1f3471af22d32d3bf2efc9130a00ae" );
  exit;
}

$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}, ";
    }
  }
}
else {
  http_response_code(404);
  die(); 
}



$synopsis = "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'];
  $synopsis = "${synopsis} It is part of the series \"$series_title\"";
}
$synopsis = "${synopsis} ${host_notes} and is about " . round($duration/60) . " minutes long. It carries ";
if ($explicit == 0) {
    $synopsis = "${synopsis} a clean flag. ";
    $explicit = "Clean";
}
else{
    $synopsis = "${synopsis} an explicit flag. ";
    $explicit = "Explicit";
} 
if ( !empty( $summary ) ) {
  $synopsis = "${synopsis}. The summary is. $summary";
}
if (strcmp($license, "CC-BY-SA" ) !== 0) {
    $synopsis = "${synopsis}. Todays show is licensed under a $license_long_name license.";
} 

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

// --------------------------------------------
// Display the results

$arr = array(
  'genre' => "Podcast",
  'album' => "Hacker Public Radio",
  'track' => "$id",
  'year' => date("Y", $date),
  'date' => date("Y-m-d", $date),
  'artist' => "${host}",
  'hostid' => "${hostid}",
  'title' => "${title}",
  'duration' => "${duration}",
  'license' => "${license}",
  'explicit' => "${explicit}",
  'summary' => "${summary}",
  'comment' => "https://hackerpublicradio.org ${explicit}; $summary",
  'synopsis' => "${synopsis}"
);

header('Content-Type: application/json');
header("Content-disposition: inline; filename=hpr_stats.json");

echo json_encode($arr);

mysqli_close($connection);
