<?php

global $realroot;
$realroot = "/dvodist";

function head ($title) {
  echo "<head>\n";
  echo "  <title> $title </title>\n";
  /* JavaScript code */
  echo "  <script type=\"text/javascript\">\n";
  echo "  function changeCell(cellId, newContent){\n";
  echo "    document.getElementById(cellId).innerHTML=newContent;\n";
  echo "  }\n";
  echo "  </script>\n";
  echo "</head>\n\n";
}

function menu ($source, $title, $root, $sheet, $append, $project) {

  echo "<html>\n";
  head($title);
  echo "<link rel=\"STYLESHEET\" HREF=\"$sheet\">\n";
  echo "<body>\n";

  if (! $project) { $project = "none"; }

  // echo "<p style=\"font-size:50%\">host: $WWWHOST, db: $DBHOST</p>\n";
  echo "<table class=page cellspacing=10px><tr><td valign=top>\n";
  echo "<table class=menu cellspacing=0px>\n";

  menulines ($source, $root, $append, $project);

  echo '</table></td><td class="body" valign=top>', "\n\n";
}

function menulines ($source, $root, $append, $project) {

  $file = fopen ($source, "r");

  // loop over the lines in the file and generate lines in the menu
  while ($line = fgetcsv ($file, 1024, "|")) {
    if (ereg ('^[:blank:]*#', $line[0])) continue;
    if (count($line) < 2) continue;

    if (count($line) == 2) {
      $value = trim($line[0]);
      if ($value == "input") {
        $value = trim($line[1]);
        menulines ($value, $root, $append, $project);
      }
      continue;
    }
    if (count($line) != 5) continue;

    $type = trim($line[2]);
    $name = trim($line[3]);
    $base = trim($line[4]);

    if (preg_match ('|\$PROJECT|', $name)) {
      $name = preg_replace ('|\$PROJECT|', $project, $name);
    }

    // append additional elements if needed
    if ($append) {
      $link = "$base" . "?" . $append;
    } else {
      $link = "$base";
    }
	
    // prepend the rootdir if needed (reference to /)
    if ($link[0] == '/') {
      $link = $root . $link;
    } 

    global $realroot;

    // hi-lite the current page (use select-style, not style)
    $thisname = $_SERVER[SCRIPT_NAME]; 
    $page = $realroot . $base;
    if ($page == $thisname) {
      $style = trim($line[1]);
    } else { 
      $style = trim($line[0]);
    }       

    switch ($type) { 
    case 'plain':
      echo "<tr><td class=\"$style\"> $name </td></tr>\n";  
      break;
    case 'picture':
      // prepend the rootdir if needed (reference to /)
      if ($name[0] == '/') {
	$name = $root . $name;
      } 
      echo "<tr><td class=\"$style\"> <img src=\"$name\"> </td></tr>\n";     
      break;
    case 'piclink':
      // prepend the rootdir if needed (reference to /)
      if ($name[0] == '/') {
	$name = $root . $name;
      } 
      echo "<tr><td class=\"$style\"> <a class=\"$style\" href=\"$link\"> <img class=\"$style\" src=\"$name\"> </a></td></tr>\n";     
      break;
    default:
      echo "<tr><td class=\"$style\"> <a class=\"$style\" href=\"$link\"> $name </a></td></tr>\n";     
      break;
    } 
  }
  fclose ($file);
}

function menu_end () { 
  echo "</td></tr></table>\n";
  echo "</body></html>\n\n";
  exit ();
}

function dirlist ($source) {
  $file = fopen ($source, "r");
  echo "<table class=\"dirlist\">\n";

  // loop over the lines in the file and generate lines in the menu
  while ($line = fgetcsv ($file, 1024, ":")) {
    if (ereg ('^[:blank:]*#', $line[0])) continue;
    if (count($line) < 2) continue;

    // allows nested entries..
    // if (count($line) == 2) {
    //   $value = trim($line[0]);
    //   if ($value == "input") {
    //     $value = trim($line[1]);
    //     menulines ($value, $root, $append, $project);
    //   }
    //   continue;
    // }

    if (count($line) != 4) continue;

    $name  = trim($line[0]);
    $type  = trim($line[1]);
    $style = trim($line[2]);
    $info  = trim($line[3]);

    switch ($type) { 
    case 'plain':
      echo "<tr class=\"$style\"><td>$name</td><td>$info</td></tr>\n";  
      break;
    case 'dir':
      echo "<tr class=\"$style\"><td><a class=\"$style\" href=\"$name\">$name</a></td><td>$info</td></tr>\n";
      break;
    default:
      echo "<tr class=\"$style\"><td><a class=\"$style\" href=\"$name\">$name</a></td><td>$info</td></tr>\n";
      break;
    } 
  }
  fclose ($file);
  echo "</table>\n";
}

function verbatim ($source) {
  $file = fopen ($source, "r");

  $addbreak = 0;

  // loop over the lines in the file and generate lines in the menu
  while ($line = fgets ($file, 1024)) {
    if (strlen($line) < 2) {
      if (!$addbreak) {
	echo "<br>\n";
      }
      $addbreak = 1;
      continue;
    }
    $Nc = strlen($line);
    echo "$line\n";
    $addbreak = 0;
  } 
  fclose ($file);
}

//////////////////////////////////////////////////////////////////////////
// Return the value associated to the key (GET or POST method)
// If convertKeyToHtmlkey is true, the key is converted to a 
// so-called HTML, i.e. the key where '.' are replaced by '_'
// (e.g. the conversion of 'f.o.o.b_a_r' is 'f_o_o_b_a_r'.
//
function get_value_from_key ($key, $convertKeyToHtmlkey) {
  if ($convertKeyToHtmlkey) {
    $htmlkey = preg_replace ('|\.|', '_', $key);
  } else {
    $htmlkey = $key;
  }
  if ($_SERVER[REQUEST_METHOD] == 'GET') { 
    return $_GET[$htmlkey]; 
  } else {
    return $_POST[$htmlkey]; 
  }
}
