DEFINE("IMGPATH", "");
DEFINE("FILEPATH", "comp/" . USER . "/");
DEFINE("COMPILATIONS_PATH", "comp/" . USER . "/");
// DEFINE("ADL_PATH_ABS","/Users/basj/ADL_ou/");
// passthru ('PATH=$PATH:'.ADL_PATH_ABS);// or exit('error:Cannot set PATH directive using exec');
session_start();
require "inc/Session.inc.php";
require "inc/Bestand.inc.php";
require "inc/Actie.inc.php";
require "inc/Operatie.inc.php";
require "inc/connectToDataBase.inc.php";
$ses = readSession($_SESSION["adlsessie"]) or exit('error:Cannot find session, ensure cookies are enabled');
//  if($ses->get_ip()!=$_SERVER['REMOTE_ADDR']){
//    exit('error:Cannot find a session for your IP, ensure cookies are enabled');
//  }
$file = readBestand($ses->get_file()) or exit('error:Cannot find the file to compile. Upload a file first');
if (!isset($_REQUEST['op'])) {
    exit('error:Variable \'op\' not given');
} else {
    $op = $_REQUEST['op'];
}
$opr = readOperatie($op) or exit('error:Operation op=\'' . $op . '\' unknown');
$target = escapeshellcmd(COMPILATIONS_PATH . $file->getId() . '_' . $op . '/');
$source = escapeshellcmd(FILEPATH . $file->getId() . '.adl');
$compileurl = '' . sprintf($opr->get_outputURL(), $target, $source, $file->getId(), USER);
//  function ok($i){
//    return '<A HREF="'.COMPILATIONS_PATH.$i.'/"><IMG SRC="'.IMGPATH.'ok.png" /></A>';
//  }
//  foreach($file->get_compilations() as $c){
//    if($op==$c['operatie']){
//      // check the progress of the compilation
}
$buttons = "";
if (isset($_REQUEST['new'])) {
    $new = true;
} else {
    $new = false;
}
if (isset($_REQUEST['edit']) || $new) {
    $edit = true;
} else {
    $edit = false;
}
$del = isset($_REQUEST['del']);
if (isset($_REQUEST['Bestand'])) {
    if (!$del || !delBestand($_REQUEST['Bestand'])) {
        $Bestand = readBestand($_REQUEST['Bestand']);
    } else {
        $Bestand = false;
    }
    // delete was a succes!
} else {
    if ($new) {
        $Bestand = new Bestand();
    } else {
        $Bestand = false;
    }
}
if ($Bestand) {
    writeHead("<TITLE>Bestand - Meterkast - ADL Prototype</TITLE>" . ($edit ? '<SCRIPT type="text/javascript" src="js/edit.js"></SCRIPT>' : '<SCRIPT type="text/javascript" src="js/navigate.js"></SCRIPT>') . "\n");
    if ($edit) {
        echo '<FORM name="editForm" action="' . $_SERVER['PHP_SELF'] . '" method="POST" class="Edit">';
echo SESSIELADEN;
?>
',WIDTH, 350,ABOVE);"
     onmouseout="return nd();">
     <?php 
echo '<IMG SRC="' . IMGPATH . 'info.png" />';
?>
</a>
  </H3>
  <p>Scriptversie <select name="sessie"><?php 
$myscripts = array();
if ($usr = readGebruiker($ses->get_gebruiker())) {
    //ignore sessions/files which cannot be found
    foreach ($usr->get_sessies() as $sesid) {
        if ($sesi = readSession($sesid)) {
            if ($sesf = readBestand($sesi->get_file())) {
                $myscripts += array($sesf->getId() => '<option value="' . $sesid . '">' . $sesf->get_path() . ' (' . $sesf->getId() . ')</option>');
            }
        }
    }
    if (ksort($myscripts)) {
        echo print_r(array_reverse($myscripts, true));
    }
}
?>
</select></p><br>
  <P><input type="submit" name="adlsessie" value="Script laden" /></P>
  </td>
  </tr></table>
  </FORM>
</BODY>