<html>
<head>
<meta http-equiv="content-type"
content="text/html; charset=ISO-8859-1">
<title>Process Uploaded File</title>
</head>
<body>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2"
cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top; width: 10%;"><br>
</td>
<td style="vertical-align: top;"><h1>Thanks for your submission!</h1><h2>Summary:</h2>

<?php

$submitted = false;
try {
$subFolder = "./uploads/";
$completeLog = $subFolder."log.xml";
$team = $_POST['teamName'];
$tempFile1 = $_FILES['uploadFile1'] ['tmp_name'];
$tempFile2 = $_FILES['uploadFile2'] ['tmp_name'];
$tempFile3 = $_FILES['uploadFile3'] ['tmp_name'];
$tempFile4 = $_FILES['uploadFile4'] ['tmp_name'];
$tempFile5 = $_FILES['uploadFile5'] ['tmp_name'];
$destFolder = $subFolder.$team;
$ip = $_SERVER['REMOTE_ADDR'];
$count = 1;
$orgName1 = $_FILES['uploadFile1'] ['name'];
$orgName2 = $_FILES['uploadFile2'] ['name'];
$orgName3 = $_FILES['uploadFile3'] ['name'];
$orgName4 = $_FILES['uploadFile4'] ['name'];
$orgName5 = $_FILES['uploadFile5'] ['name'];

if (!file_exists($destFolder))
   mkdir($destFolder);

while (file_exists($destFolder."/".$count)) {
   $count++;
}
$destFolder = $destFolder."/".$count;
mkdir($destFolder);

$dest1 = $destFolder."/".$orgName1;
$dest2 = $destFolder."/".$orgName2;
$dest3 = $destFolder."/".$orgName3;
$dest4 = $destFolder."/".$orgName4;
$dest5 = $destFolder."/".$orgName5;
move_uploaded_file ($tempFile1, $dest1);
move_uploaded_file ($tempFile2, $dest2);
move_uploaded_file ($tempFile3, $dest3);
move_uploaded_file ($tempFile4, $dest4);
move_uploaded_file ($tempFile5, $dest5);

$submitted = true;

date_default_timezone_set("Asia/Tokyo");
$time = date("Y-m-d H:i:s");

$logFile = $destFolder."/log.xml";
$fh = fopen($logFile, 'w');

fwrite($fh, "<Submission>\n");
fwrite($fh, "  <Team>".$_POST['teamName']."</Team>\n");
fwrite($fh, "  <EMail>".$_POST['mailAdr']."</EMail>\n");
fwrite($fh, "  <IP>".$ip."</IP>\n");
fwrite($fh, "  <Time>".$time."</Time>\n");
fwrite($fh, "</Submission>\n");
fclose($fh);

$fh = fopen($completeLog, 'a');
fwrite($fh, "<Submission>\n");
fwrite($fh, "  <Team>".$_POST['teamName']."</Team>\n");
fwrite($fh, "  <EMail>".$_POST['mailAdr']."</EMail>\n");
fwrite($fh, "  <IP>".$ip."</IP>\n");
fwrite($fh, "  <Time>".$time."</Time>\n");
fwrite($fh, "</Submission>\n");
fclose($fh);

echo "<table style=\"text-align: left; width: 100%;\" border=\"1\"
cellpadding=\"2\" cellspacing=\"2\">";

echo "<tbody><tr><td style=\"vertical-align: top;\">Team name<br></td>";
echo "<td style=\"vertical-align: top;\">".$_POST['teamName']."<br>";

echo "</td></tr><tr><td style=\"vertical-align: top;\">E-Mail<br></td>";
echo "<td style=\"vertical-align: top;\">".$_POST['mailAdr']."<br>";

echo "</td></tr><tr><td style=\"vertical-align: top;\">Time<br></td>";
echo "<td style=\"vertical-align: top;\">".$time."<br>";

echo "</td></tr><tr><td style=\"vertical-align: top;\">Submission count of your
team<br></td>";
echo "<td style=\"vertical-align: top;\">".$count."<br>";
}
catch (Exception $e) {
   echo "We are sorry, some error occured. ";
  if  ($submitted)
    echo "However, it seemed your submission was stored on our server, in any case contact the organisators directly and submit your code on an USB-stick";
  else
    echo "Your submission was not stored on our server, please contact the organisators directly and submit your code on an USB-stick";
  echo "<br> Error: ".$e->getMessage()."<br>";
}

?>
</td>
</tr>
</tbody>
</table>
</td>
<td style="vertical-align: top; width: 30%;"><br>
</td>
</tr>
</tbody>
</table>
<br>
<br>
<br>
</body>
</html>
