<?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( "d7f47a123af3ea10628ce44d2146b40e" );
    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( "5d45cb79dd9426e7e4ab5595ac647a62" );
    exit;
  }
  $query = "SELECT id FROM eps WHERE id = '$id'";
  $result = @mysqli_query($connection, $query);
  if($result === FALSE) {
    problem( "b26e5cea3779b66c76bf1c3c266e1a51" );
  }
  else {
    $db = mysqli_fetch_array($result, MYSQLI_ASSOC);
    if ( empty($db["id"]) ) {
      problem( "a5d5eab30290ba4460ceb337add11821" );
    }
  }
}
else {
  problem( "39122c23e90fcf5cb9ca9e03a5c5a3c5" );
  exit;
}

$ep_retrieve = "SELECT
  UNIX_TIMESTAMP(eps.date) AS timestamp,
  eps.title,
  eps.duration,
  eps.summary,
  eps.tags,
  hosts.host,
  hosts.email,
  hosts.license as host_license,
  hosts.profile,
  eps.hostid,
  eps.series,
  eps.notes,
  miniseries.name as series_name, 
  eps.license as show_license,
  eps.explicit
FROM
  eps
  LEFT JOIN hosts ON eps.hostid = hosts.hostid
  LEFT JOIN miniseries ON miniseries.id = eps.series
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'];
    $host_email = $row['email'];
    $host_license = $row['host_license'];
    $host_profile = $row['profile'];
    $series = $row['series'];
    $series_name = $row['series_name'];
    $show_license = $row['show_license'];
    $explicit = $row['explicit'];
    $tags = $row['tags'];
    $notes = $row['notes'];
  }
}
else {
  http_response_code(404);
  die(); 
}

if ($explicit == 0) {
  $explicit = "Clean";
}
else{
  $explicit = "Explicit";
} 

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

$arr = array(
  'host' => array ( 
    'Host_ID' => "${hostid}",
    'Host_Name' => "${host}",
    'Host_Email' => "${host_email}",
    'Host_License' => "${host_license}",
    'Host_Profile' => "${host_profile}"
  ),
  'episode' => array ( 
    'Title' => "${title}",
    'Summary' => "${summary}",
    'Explicit' => "${explicit}",
    'Show_License' => "${show_license}",
    'Series' => "${series}",
    'Series_Name' => "${series_name}",
    'Tags' => "${tags}",
    'Show_Notes' => "${notes}"
  ),
  'metadata' => array ( 
    'Episode_Number' => "${id}",
    'Episode_Date' => date("Y-m-d", $date)
  )
);

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

echo json_encode($arr);

mysqli_close($connection);
