<?php
# request.php > request_confirm.php > upload.php > upload_confirm.php 
require "/home/hpr/php/include.php";

if ( $_SERVER['REQUEST_METHOD'] !== 'GET' and empty($_SERVER["REMOTE_ADDR"]) and count($_GET) !== 1 ) {
  call412("9a77e4ab24410cbf68a3a05ba97221e4");
}

if ( isset( $_GET['key'] ) and strlen( $_GET['key'] ) ===  45  and strlen( htmlspecialchars( stripslashes( strip_tags( $_GET['key'] ) ) ) ) ===  45 and ctype_xdigit( $_GET['key'] ) ) {
  $key = htmlspecialchars( stripslashes( strip_tags( $_GET['key'] ) ) );
}
else {
  if ( isset( $_GET['delete'] ) and strlen( $_GET['delete'] ) ===  45  and strlen( htmlspecialchars( stripslashes( strip_tags( $_GET['delete'] ) ) ) ) ===  45 and ctype_xdigit( $_GET['delete'] ) ) {
    $key = htmlspecialchars( stripslashes( strip_tags( $_GET['delete'] ) ) );
  }
  else {
    call412("e8ac90fc2a8996e5fb7a83e73e636e80");
  }
}

$ip = $_SERVER["REMOTE_ADDR"];

$query = "select * FROM reservations WHERE reservations.ip = '$ip' AND reservations.key = '$key' ";
$result = mysqli_query($connection, $query);
if($result === FALSE) {
  call412("91432866e3c9c36a9c4884345d578761");
}
$db = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ( empty($db) or empty( $db['timestamp'] ) or empty( $db['key'] ) or empty( $db['ep_num'] ) or empty( $db['ep_date'] ) or empty( $db['email'] ) ) {
  call412("b019bd29c1fe5b756e4b620a5428f730");
}
else {
  $db_timestamp = $db['timestamp'];
  $db_key = $db['key'];
  $email_padded = formatemail($db['email']);
  $email = unformatemail($db['email']);
  $ep_num = $db['ep_num'];
  $ep_date = $db['ep_date'];
  $db_ip = $db['ip'];
}

// User selects delete
if ( isset( $_GET['delete'] ) ) {
  $query = "DELETE FROM reservations WHERE reservations.ip = '$ip' AND reservations.key = '$key' ";
  $result = mysqli_query($connection, $query);
  if($result === FALSE) {
    call412("35a7f4e80ecba8284049e9d6261ae523");
  }
  else {
    header( "Location: https://" . $_SERVER['SERVER_NAME'] . "/calendar.php" ) ;
    $dir_structure = "/home/hpr/upload/" . strtotime($db_timestamp) . "_${ep_num}_${ep_date}_${db_key}/";
    rrmdir("$dir_structure");
    exit;
  }
}

// Set the show to verified so that we can remove the temporary lock
$status = strtotime("now") . ".EMAIL_LINK_CLICKED." . date('Y-m-d\TH:i:s\Z');
$query = "UPDATE reservations SET `verified` = '1', `status` = 'EMAIL_LINK_CLICKED' WHERE `ip` = '$db_ip' AND `timestamp` = '$db_timestamp' AND `key` = '$db_key'";
$result = mysqli_query($connection, $query );
if(!isset($result)) {
  naughty("066c518314f0d3b1ad3e4af60fcf36ce");
}

// Display host information
/*
*/

$ip = $_SERVER["REMOTE_ADDR"];
# TODO need to add the email for new hosts
$query = "SELECT * FROM hosts WHERE hosts.email = '$email' OR hosts.email = '$email_padded'";
$result = mysqli_query($connection, $query);
if($result === FALSE) {
  call412("5cb513b590ab5859bf7603b79402a5cb");
}
$db = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ( empty($db) or empty( $db['hostid'] ) or empty( $db['host'] ) or empty( $db['email'] ) ) {
  $hostid = "0";
  $host = "";
  $profile = "";
  $license = "CC-BY-SA";
  $local_image = 0;
  $status = "Please fill in some information about yourself.";
}
else {
  $hostid = $db['hostid'];
  $host = $db['host'];
  $profile = $db['profile'];
  $license = $db['license'];
  $local_image = $db['local_image'];
  $email_padded = formatemail($db['email']);
  $email = unformatemail($db['email']);
  $status = "Please confirm or change the following information about yourself.";
}

$image_url = "./images/hosts/${hostid}.png";
if ( !file_exists( $image_url )) {
  $image_url = 'https://secure.gravatar.com/avatar/' . md5($email) . '.png&d=404';
}

// Main

$body="give";
//$body="index_full";
include 'header.html'; 

?>

<main id="maincontent">
  <hr />
  <?php 
  if ( $ep_num == 9999 ) {
    echo "<h1>Uploading to the Reserve Queue.</h1>\n";
  }
  else {
    echo "<h1>Uploading hpr${ep_num} for release on ${ep_date}</h1>\n";
  }
  ?>
  <p>
  You can fill in information about yourself and your show below. Please see the <a href="request_a_slot.php">help</a> page for more information.
  </p>
  <h2><?php echo $status ?></h2>
  <form method="POST" action="upload_confirm.php" enctype="multipart/form-data">
  <table>
    <tr>
      <td>Host Email (<a href="<?php echo "${baseurl}about.html#email"; ?>" target="_blank">?</a>):</td>
      <td><?php echo $email;?></td>
    </tr>
    <tr>
      <td style="vertical-align:top;">Image: (<a href="<?php echo "${baseurl}about.html#image"; ?>" target="_blank">?</a>)</td>
      <td>
      <?php 
      echo "<img src=\"$image_url\" alt=\"host image\" /><br /><small><em>";
      if ( $local_image === 1 ) {
        echo "Image previously uploaded";
      }
      else {
        echo "Image sourced from <a href=\"https://secure.gravatar.com\">gravatar</a>";
      }
      ?>
      </em></small><br />
      Upload your photo/avatar here: <input type="file" name="host_photo" id="photo">      
      </td>
    </tr>
    <tr>
      <td><strong>Name/Handle: (<a href="<?php echo "${baseurl}about.html#Name_Handle"; ?>" target="_blank">?</a>)</strong></td>
      <td><input required type="text" name="host_name" size="40" maxlength="40" placeholder="Enter the name you wish to be know as on HPR" value="<?php echo $host?>"></td>
    </tr>
    <tr>
      <td><strong>Your Default license: (<a href="<?php echo "${baseurl}about.html#Default_license"; ?>" target="_blank">?</a>)</strong></td>
      <td>
      <select name="host_license">
        <option value="CC-0" <?php if($license == "CC-0") echo "selected"; ?>>CC-Zero/Public Domain (CC-0)
        <option value="CC-BY" <?php if($license == "CC-BY") echo "selected"; ?>>Attribution (CC-BY)
        <option value="CC-BY-SA" <?php if($license == "CC-BY-SA") echo "selected"; ?>>Attribution-ShareAlike (CC-BY-SA)
        <option value="CC-BY-ND" <?php if($license == "CC-BY-ND") echo "selected"; ?>>Attribution-NoDerivs (CC-BY-ND)
        <option value="CC-BY-NC" <?php if($license == "CC-BY-NC") echo "selected"; ?>>Attribution-NonCommercial (CC-BY-NC)
        <option value="CC-BY-NC-SA" <?php if($license == "CC-BY-NC-SA") echo "selected"; ?>>Attribution-NonCommercial-ShareAlike (CC-BY-NC-SA)
        <option value="CC-BY-NC-ND" <?php if($license == "CC-BY-NC-ND") echo "selected"; ?>>Attribution-NonCommercial-NoDerivs (CC-BY-NC-ND)
      </select>
      </td>
    </tr>
    <tr>
      <td style="vertical-align:top;">Profile: (<a href="<?php echo "${baseurl}about.html#Profile"; ?>" target="_blank">?</a>)</td>
<!--  TODO https://xing.github.io/wysihtml5/      -->
      <td><textarea name="host_profile" maxlength="2000" rows="10" cols="50" placeholder="Enter some text about yourself with links to your blog or other online presence."><?php echo htmlspecialchars($profile) ?></textarea></td>
    </tr>
  </table>
  <h2>Please fill in some information about this episode</h2>
  <table>
    <tr>
      <td><strong>Title(*) (<a href="<?php echo "${baseurl}about.html#Title"; ?>" target="_blank">?</a>):</strong></td>
      <td><input required type="text" name="title" size="50" maxlength="100" placeholder="Enter a short descriptive title for your show."></td>
    </tr>
    <tr>
      <td><strong>Summary(*) (<a href="<?php echo "${baseurl}about.html#Short_Summary"; ?>" target="_blank">?</a>):</strong></td>
      <td><input required type="text" name="summary" size="70" maxlength="100" placeholder="This is a short 100 character summary of what your show is about."></td>
    </tr>
    <tr>
      <td style="vertical-align:top;"><strong>Theme: (<a href="<?php echo "${baseurl}about.html#Theme"; ?>" target="_blank">?</a>)</strong></td>
      <td>
        Please <strong>do not</strong> add a theme to your show: <small>See <a href="https://lists.hackerpublicradio.com/pipermail/hpr/2021-November/004328.html">Policy Decision</a> for more information.</small><br />
      </td>
    </tr>
    <tr>
      <td><strong>Explicit: (<a href="<?php echo "${baseurl}about.html#Explicit"; ?>" target="_blank">?</a>)</strong></td>
      <td>
        Is the show <em>Explicit</em> ?:
        <input required type="radio" name="explicit" value="Yes">Yes 
        <input type="radio" name="explicit" value="Clean">No. 
        <small>If in doubt please select <strong>Yes</strong>. See <a href="https://www.apple.com/uk/itunes/podcasts/specs.html#submitting">iTunes</a> for more information.</small>
      </td>
    </tr>
    <tr>
      <td><strong>License: (<a href="<?php echo "${baseurl}about.html#License"; ?>" target="_blank">?</a>)</strong></td>
      <td>
        <select name="license">
            <option value="CC-BY-SA" selected>CC-BY-SA</option>
            <option value="CC-BY-NC-SA" >CC-BY-NC-SA</option>
            <option value="CC-BY-NC-ND" >CC-BY-NC-ND</option>
            <option value="CC-0" >CC-0</option>
            <option value="CC-BY-NC" >CC-BY-NC</option>
            <option value="CC-BY" >CC-BY</option>
            <option value="Other" >Other</option>
        </select>
        <small>See <a href="https://creativecommons.org/licenses/">https://creativecommons.org/licenses/</a> for more information.</small>
      </td>
    </tr>
    <tr>
      <td><strong>Notes(*) (<a href="<?php echo "${baseurl}about.html#show_notes"; ?>" target="_blank">?</a>):</strong></td>
      <td><textarea required name="notes" maxlength="40000" rows="20" cols="70" placeholder="Please add your show notes here." ></textarea></td>
    </tr>
    <tr>
      <td>Format: (<a href="<?php echo "${baseurl}about.html#shownotes_format"; ?>" target="_blank">?</a>)</td>
      <td>
        <small>What format if any did you just use in the show notes above ?</small><br />
        <select name="shownotes_format">
            <option value="plain_text" selected>Plain text</option>
            <option value="html5">HTML5 (Preferred)</option>
            <option value="Markdown_GitHub">Markdown (GitHub flavoured)</option>
            <option value="Markdown_Pandoc">Markdown (Pandoc flavoured)</option>
            <option value="restructured_text">RestructuredText</option>
            <option value="txt2tags">txt2tags</option>
        </select>
      </td>
    </tr>
    <tr>
      <td>Series: (<a href="<?php echo "${baseurl}about.html#series"; ?>" target="_blank">?</a>)</td>
      <td>
        <?php
        $result = mysqli_query($connection, "SELECT id, name FROM miniseries WHERE private=0 ORDER BY name ASC");
        echo "<select name=\"series\"><option value=\"0\" selected>none</option>";
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            printf("<option value=\"{$row['id']}\">{$row['name']}</option>");
        }
        mysqli_free_result($result);
        ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Tags: (<a href="<?php echo "${baseurl}about.html#tags"; ?>" target="_blank">?</a>)</td>
      <td><input required type="text" name="tags" size="70" maxlength="100" placeholder="Add a list of comma separated tags." /><br />
      Add a list of <strong>comma separated</strong> tags.<br />
      If you can provide tags for your show that would be appreciated; you are certainly the best judge of what makes good tags, but if not then a volunteer will add them later.
      </td>
    </tr>
    <tr>
      <td style="vertical-align:top;"><strong>Upload Method: (<a href="<?php echo "${baseurl}about.html#upload_method"; ?>" target="_blank">?</a>)</strong></td>
      <td>
        <em>You have the choice of the following upload options:</em> <br />
        <p>
        <strong>1. Upload now</strong> via this browser: (<a href="<?php echo "${baseurl}about.html#browser"; ?>" target="_blank">?</a>)<br />
        <input type="file" multiple="multiple" name="media_files[]" title="select if using the http upload option" />
        </p>
        <p>
        <strong>2. Provide a url</strong> we we can directly download it from: (<a href="<?php echo "${baseurl}about.html#url"; ?>" target="_blank">?</a>)<br />
        This must be accessable using a curl or wget command.<br />
        Please avoid services that require login or downloading via a browser.<br />
        <input type="url" size="30"  name="url" placeholder="https://example.com/hpr9999.flac">
        </p>
        <p>
        <strong>3. Upload via an alternative method</strong>:
        If you wish to send a show using another method then please discuss it with the HPR Volunteer at admin@hackerpublicradio.org.
        </p>
        <p>
        <strong>4. Reserve a slot</strong>:
        Leave upload option 1 and 2 empty if you have received prior approval for a reservation from the Community via the <a href="/mailman/listinfo/hpr_hackerpublicradio.org">HPR Mailing List</a> to either:
        </p>
        <ul>
          <li>Reserve the show now and upload media later.</li>
          <li>Send physical media by the postal service or deliver in person.<br />
          <em>Make sure you plan in enough time to deliver your media.</em></li>
        </ul>
      </td>
    </tr>
  </table>
  <?php echo "<input type=\"hidden\" name=\"hostid\" value=\"$hostid\">"; ?>
  <?php echo "<input type=\"hidden\" name=\"key\" value=\"$key\">"; ?>
  <br />
  <input type="submit" name="submit_edit" value="Submit"> This will take a <strong>long time</strong>, leave the browser running. You will get an email once the upload is complete.
  <?php 
  if ( $ep_num == 9999 ) {
    echo "<p>\n<em><a href=\"https://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . "?delete=$key\">Delete this reservation for the Reserve Queue</a>.</em>\n</p>\n"; 
  }
  else {
    echo "<p>\n<em><a href=\"https://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . "?delete=$key\">Delete this reservation for hpr${ep_num} on ${ep_date}</a>.</em>\n</p>\n"; 
  }
  
  
  
  ?>
  
  </form>
</main>

<?php
include 'footer.html'; 
?>