<?php

require_once 'HTML/Template/ITX.php';

$contact_dir = "/var/smei/new/TelemProcess/log/contacts";
$extract     = "/home/mpc/Projects/SMEI/Code/web/TelemProcess/extract_boards/extract_boards";

function make_filename($year, $month, $day)
{
  return sprintf("%04u_%02u_%02u.txt", 0+$year, 0+$month, 0+$day);
}

if (count($_POST) > 0) {
  // Process the submitted form

  $year  = 0 + $_POST['year'];
  $month = 0 + $_POST['month'];
  $day   = 0 + $_POST['day'];
  $ret   = 'f';
  $code  = '0';
  $fname = sprintf("%s/%s",
		   $contact_dir,
		   make_filename($year, $month, $day));
  
  // Validate the data
  
  if (file_exists($fname)) {
    // Oops.  File exists already.  No overwriting from the web form.
    $code = 1;
  } elseif (count($_FILES) == 0) {
    // No file actually uploaded.
    $code = 2;
  } elseif (!is_uploaded_file($_FILES['file']['tmp_name'])) {
    // Upload failed
    $code = 3;
  } elseif ((mktime($day, $month, $year) == false) || ($day == 0) || ($month == 0) || ($year == 0)) {
    // Date isn't valid
    $code = 9;
  } else {
    // All is fine.  Convert the uploaded file

    $src = $_FILES['file']['tmp_name'];
    system("$extract '$src' '$fname'", $sr);

    switch($sr) {
    case 0:
      $ret = 't';

      $ar = $contact_dir . '/xls/' . make_filename($year, $month, $day) . '.xls';
      if (!move_uploaded_file($_FILES['file']['tmp_name'], $ar)) {
	$ret = 'f';
	$code = 10;
	break;
      };

      break;

    case 1:
      $code = 4;
      break;

    case 2:
      $code = 5;
      break;
      
    case 3:
      $code = 6;
      break;
      
    case 4:
      $code = 7;
      break;
      
    default:
      $code = 8;
      break;
    }
  }
  
  // Redirect to produce a ticket
  header("Location: newsupports.php?r={$ret}&c={$code}&y={$year}&m={$month}&d={$day}");
  
} elseif (count($_GET) > 0) {

  // Process the submission ticket
  $template = new HTML_Template_ITX('./tmpl');
  $template->loadTemplateFile('page.tpl', true, true);
  
  $template->addBlockFile('PAGE', 'PAGE', 'newsupports_tickets.tpl');
  
  $template->setVariable('TITLE', 'Support Submission Results');
  
  // Validate the fine inputs
  $ret   = $_GET['r'];
  $code  = 0 + $_GET['c'];
  $year  = 0 + $_GET['y'];
  $month = 0 + $_GET['m'];
  $day   = 0 + $_GET['d'];
  
  // Do the processing
  if ($ret == 'f') {
    // Failed
    $template->setCurrentBlock('FAIL');

    $template->setVariable('CODE', $code);
    $template->setVariable('YEAR', $year);
    $template->setVariable('MONTH', $month);
    $template->setVariable('DAY', $day);

    switch($code) {

    case 1:
      $template->setVariable('REASON', 'The file for this date already exists.  Please contact the administrator.');
      break;

    case 2:
      $template->setVariable('REASON', 'No file was uploaded.');
      break;

    case 3:
      $template->setVariable('REASON', 'The file was not uploaded correctly.  Please contact the administrator.');
      break;

    case 4:
      $template->setVariable('REASON', 'The conversion script did not find the input file.  Please contact the administrator.');
      break;

    case 5:
      $template->setVariable('REASON', 'The uploaded file was not recognised as an excel spreadsheet.');
      break;

    case 6:
      $template->setVariable('REASON', 'The output file could not be created.  Please contact the administrator.');
      break;

    case 7:
      $template->setVariable('REASON', 'The conversion did not create any output.  Is this a valid file format ?');
      break;

    case 8:
      $template->setVariable('REASON', 'The conversion script had an unknown error.  Please contact the administrator.');
      break;

    case 9:
      $template->setVariable('REASON', 'The specified date is not valid.');
      break;

    case 10:
      $template->setVariable('REASON', 'The uploaded file could not be moved to the archive directory.');
      break;

    case 11:
      $template->setVariable('REASON', 'The uploaded file was not found in the log directory.  Very odd!');
      break;

    default:
      $template->setVariable('REASON', 'This error code was not recognised.  Trying to hack, were you ?');
      break;
    }

    $template->parseCurrentBlock();
    
  } else {
    // Success
    $template->setCurrentBlock('OK');
    $template->setVariable('FILE', make_filename($year, $month, $day));
    $template->parseCurrentBlock();

    $fname = sprintf("%s/%s",
		     $contact_dir,
		     make_filename($year, $month, $day));
    $fhandle = fopen($fname, 'r');
    if ($fhandle === false) {
      // Redirect to produce a ticket
      header("Location: newsupports.php?r=f&c=11&y={$year}&m={$month}&d={$day}");
      exit;
    } else {
      while (!feof($fhandle)) {
        $i = fscanf($fhandle, "%s %s %s %s\n");
        if ($i) {
	  list ($tstart, $tstop, $rev, $site) = $i;

	  // Clean it up a little
	  switch($site) {
	  case 'FBKS':
	    $site = 'Fairbanks';
	    break;
	  case 'SVAL':
	    $site = 'Svalbard';
	    break;
	  }

	  $tstart = gmdate('Y-z H:i:s', $tstart);
	  $tstop  = gmdate('Y-z H:i:s', $tstop);
	
	  $template->setCurrentBlock('DATA');
	  $template->setVariable('REV', $rev);
	  $template->setVariable('AOS', $tstart);
	  $template->setVariable('LOS', $tstop);
	  $template->setVariable('SITE', $site);
	  $template->parseCurrentBlock();
	
	  $i=NULL;
        }
      }
      fclose($fhandle);
    }
  }
  
  $template->show();
  
} else {
  // Process the plain form
  $template = new HTML_Template_ITX('./tmpl');
  $template->loadTemplateFile('page.tpl', true, true);
  
  $template->addBlockFile('PAGE', 'PAGE', 'newsupports.tpl');

  $template->setVariable('TITLE', 'New Support Submission Form');
  
  $template->setCurrentBlock('PAGE');
  
  $date = getdate();
  
  $template->setVariable('YEAR', $date['year']);
  $template->setVariable('MONTH', $date['mon']);
  $template->setVariable('DAY', '?');
  
  $template->parseCurrentBlock();

  $template->show();

}

?>
