<?php

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";

  echo "<!-- Start eXTReMe Pro Tracker Code V3/7\n";
  echo "Login:    emagnier\n";
  echo "Help:     https://www.extremetracking.com\n";
  echo "Installation instructions:\n";
  echo "Copy and paste this code between the two body tags into the\n";
  echo "*source* of your pages or in your footer/template. Best is\n";
  echo "to use a normal text editor such as notepad. -->\n";
  echo "<script type=\"text/javascript\"><!--\n";
  echo "EXd=document;location.protocol==\"https:\"?EXp=\"https\":EXp=\"http\";EXs=\"src\";\n";
  echo "EXd.write(\"<img \"+EXs+\"=\"+EXp+\"://nht-2.extreme-dm.com/n2.g?login=emagnier\",\n";
  echo "\"&url=\"+escape(EXd.URL)+\"&jv=\"+navigator.javaEnabled()+\"&\",\n";
  echo "\"d=\"+screen.width+\"x\"+screen.height+\"&c=\"+screen.colorDepth+\"\",\n";
  echo "\"&l=\"+escape(EXd.referrer)+\" width=0 height=0>\");//-->\n";
  echo "</script><noscript><img height=\"1\" width=\"1\" alt=\"\"\n";
  echo "src=\"http://nht-2.extreme-dm.com/n2.g?login=emagnier&amp;url=nojs\">\n";
  echo "</noscript>\n";
  echo "<!-- End eXTReMe Pro Tracker Code -->\n";

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

  // echo "<p style=\"font-size:50%\">host: $root</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";
}

// generate the menu based on the text file
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;

    // allow load of other menu files
    if (count($line) == 2) {
      $value = trim($line[0]);
      if ($value == "input") {
        $value = trim($line[1]);
        menulines ($value, $root, $append, $project);
      }
      continue;
    }

    // blithly skip lines with invalid entries
    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;
    } 

    // hi-lite the current page (use select-style, not style)
    $thisname = $_SERVER[SCRIPT_NAME]; 
    $page = $root . $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 root 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 root 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) {
  if (!file_exists($source)) return;

  $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) && (count($line) != 5)) continue;

    // support both the old and new style tables:
    // old: name | type | style | info
    // new: name | link | type | style | info

    if (count($line) == 4) {
      $name  = trim($line[0]);
      $type  = trim($line[1]);
      $style = trim($line[2]);
      $info  = trim($line[3]);
      $link  = $name;
    }
    if (count($line) == 5) {
      $name  = trim($line[0]);
      $link  = trim($line[1]);
      $type  = trim($line[2]);
      $style = trim($line[3]);
      $info  = trim($line[4]);
    }

    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 parse_dirlist ($input) {
  echo "<table class=\"dirlist\">\n";

  $inlist = explode ("\n", $input);
  $Ninlist = count($inlist);
  for ($i = 0; $i < $Ninlist; $i++) {
    $inline = trim ($inlist[$i], "@ \t");
    $line = explode (":", $inline);
    if (ereg ('^[:blank:]*#', $line[0])) continue;
    if (count($line) < 2) continue;

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

    // support both the old and new style tables:
    // old: name | type | style | info
    // new: name | link | type | style | info

    if (count($line) == 4) {
      $name  = trim($line[0]);
      $type  = trim($line[1]);
      $style = trim($line[2]);
      $info  = trim($line[3]);
      $link  = $name;
    }
    if (count($line) == 5) {
      $name  = trim($line[0]);
      $link  = trim($line[1]);
      $type  = trim($line[2]);
      $style = trim($line[3]);
      $info  = trim($line[4]);
    }

    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=\"$link\">$name</a></td><td>$info</td></tr>\n";
      break;
    default:
      echo "<tr class=\"$style\"><td><a class=\"$style\" href=\"$link\">$name</a></td><td>$info</td></tr>\n";
      break;
    } 
  }
  echo "</table>\n";
}

function verbatim ($source) {
  if (!file_exists($source)) return;

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

  $addbreak = 0;

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

// lines which start with @ are processed by 'dirlist', all others are processed by Markdown
function do_markdown ($source, $root) {

  if (!file_exists($source)) return;

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

  // loop over the lines in the file and generate lines in the menu
  $to_markdwn = "";
  $to_dirlist = "";
  while ($line = fgets ($file, 1024)) {
    // replace @WWWHOST@ with $root
    $line = str_replace ("@WWWHOST@", $root, $line);

    // echo "line: $line";
    if (!strncmp($line, "@", 1)) {
      if ($to_markdwn) {
	// echo "<b> start markdown</b><br>\n";
	$my_html = Markdown($to_markdwn);
	echo "$my_html";
	$to_markdwn = "";
      }
      $to_dirlist .= $line;
    } else {
      if ($to_dirlist) {
	// echo "<b> start dirlist</b><br>\n";
	parse_dirlist($to_dirlist);
	$to_dirlist = "";
      }
      $to_markdwn .= $line;
    }
  }
  // echo "end of lines<br>\n";
  // echo "to_markdwn: $to_markdwn\n";
  // echo "<br>\n";
  // echo "to_dirlist: $to_dirlist\n";
  // echo "<br>\n";

  if ($to_markdwn) {
    $my_html = Markdown($to_markdwn);
    echo "$my_html";
    $to_markdwn = "";
  }
  if ($to_dirlist) {
    parse_dirlist($to_dirlist);
    $to_dirlist = "";
  }
  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]; 
  }
}
