<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["php"]))
{
  // Moeten slashes weg?
  if(get_magic_quotes_gpc() || ini_get("magic_quotes_sybase"))
    $_POST["php"] = stripslashes($_POST["php"]);

  // Highlighten + splitsen per lijn
  $zoek		= array("\r", "<br />");
  $vervang	= array("\n", "");
  $code		= explode("\n", str_replace($zoek, $vervang, highlight_string(trim($_POST["php"]), TRUE)));

  // Onnodige lijnen verwijderen
  $verwijder = array("</span>", "</font>", "</code>");
  while(empty($code[(sizeof($code) - 1)]) || in_array($code[(sizeof($code) - 1)], $verwijder))
    unset($code[(sizeof($code) - 1)]);

  // Output code starten
  $html  = "<!-- Code gemaakt door PHP2HTML v2.2 -->\n";
  $html .= "<div style=\"display: block; overflow: scroll; width: " . (ctype_digit($_POST["breedte"]) ? $_POST["breedte"] . "px" : "100%") . "; padding-right: 10px;\">\n";

  // Standaard tekst kleur ophalen + eerste en laatste lijn verwijderen
  preg_match('/<code><(span|font) (style="color: |color=")(#[A-Z0-9]{6})">/', $code[0], $kleur);
  $txt_kleur = $kleur[3];
  unset($kleur, $code[0]);
  
  // Lijn per lijn de code proper maken
  for($i = 1; $i <= sizeof($code); $i++)
  {	  
	// Code proper maken
	$code[$i] = preg_replace('/<(span|font) (style="color: |color=")(#[A-Z0-9]{6})">((&nbsp;)*)/', '\\4<span style="color: \\3;">', $code[$i]);
	$code[$i] = preg_replace('/(&nbsp;)*<\/(span|font)>/', '</span>\\1', $code[$i]);

	// De tag werd niet op dezelfde lijn gesloten
	if(sizeof(explode("<span ", $code[$i])) > sizeof(explode("</span>", $code[$i])) && isset($code[($i + 1)]))
	{
	  // Huidige kleur ophalen
	  preg_match_all('/<span style="color: (#[A-Z0-9]{6});">/', $code[$i], $kleur, PREG_SET_ORDER);
	  
	  // Volgende lijn bepalen
	  $t = 1;
	  while(empty($code[($i + $t)])) $t++;
	  
	  // Open tag voor kleur toevoegen
	  $code[($i + $t)] = "<span style=\"color: " . $kleur[(sizeof($kleur) - 1)][1] . "\">" . $code[($i + $t)];
	  
	  $code[$i] .= "</span>"; // Huidige lijn sluiten
	}
	elseif(sizeof(explode("<span ", $code[$i])) > sizeof(explode("</span>", $code[$i])))
	  $code[$i] .= "</span>"; // Span op huidige lijn sluiten, er is geen volgende lijn
	
	// Overbodige tags en spaties wegdoen
	$code[$i] = preg_replace('/<span style="color: #[A-Z0-9]{6};"><\/span>/', '', $code[$i]);
	$code[$i] = preg_replace('/^(&nbsp;)+$/', '', $code[$i]);
  }

  // Met lijnnummering & wordwrap
  if(!empty($_POST["lijnnr"][0]) && !empty($_POST["wordwrap"][0]))
  {
	$html .= "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n"; // Tabel openen

	foreach($code as $key => $lijn)
	{	  
	  $html .= "<tr>\n";
	  $html .= "<td valign=\"top\" align=\"right\" style=\"color: #000000; background-color: #CCCCCC; padding: 0px 2px 0px 10px;\"><code>" . $key . "</code></td>\n";
	  $html .= "<td valign=\"top\" align=\"left\" style=\"color: " . $txt_kleur . "; padding-left: 5px;\">" . (!empty($lijn) ? "<code>" . preg_replace("/([ ]{2,})/e", "str_replace(' ', '&nbsp;', '\\1');", preg_replace("/^([ ]+)/e", "str_replace(' ', '&nbsp;', '\\1');", str_replace("&nbsp;", " ", $lijn))) . "</code>" : "") . "</td>\n";
	  $html .= "</tr>\n";
	}
	
	$html .= "</table>\n"; // Tabel sluiten
  }
  // Met lijnnummering & zonder wordwrap
  elseif(!empty($_POST["lijnnr"][0]) && empty($_POST["wordwrap"][0]))
  {
	$nr = $bron = "";
	foreach($code as $key => $lijn)
	{
	  $nr .= "<br />" . $key;
	  $bron .= "<br />\n" . $lijn;
	}
	
	$html .= "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
	$html .= "<tr>\n";
	$html .= "<td valign=\"top\" align=\"right\" style=\"color: #000000; background-color: #CCCCCC; padding: 0px 2px 0px 10px;\"><code>" . substr($nr, 6) . "</code></td>\n";
	$html .= "<td valign=\"top\" align=\"left\" style=\"color: " . $txt_kleur . "; padding-left: 5px;\"><code>" . substr($bron, 7)  . "</code></td>\n";
	$html .= "</tr>\n";
	$html .= "</table>\n";
  }
  // Zonder lijnnummering
  else
  {
	$html .= "<code><span style=\"color: " . $txt_kleur . ";\">\n";
	foreach($code as $key => $lijn) $html .= $lijn . "<br />" . ((isset($code[($key + 1)]) && empty($code[($key + 1)])) ? "" : "\n");
	$html .= "</span></code>";
  }

  // HTML i.p.v XHTML -> "<br />" vervangen
  if($_POST["type"] == "HTML")
	$html = str_replace("<br />", "<br>", $html);

  // Einde code output
  $html .= "</div>\n";
  $html .= "<!-- Einde PHP2HTML code -->";  

  // Header instellen en bestand versturen
  header("Content-disposition: attachment; filename=php2html.html");
  header("Content-Type: text/html");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: " . strlen($html));
  header("Pragma: no-cache");
  header("Expires: 0");
  echo $html;
}
else
{
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>PHP2HTML v2.2</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
<!--
body
{
  margin: 10px;
  font-size: 12px;
  font-family: Arial, Helvetica, sans-serif;
  color: #333333;
  background-color: #CCCCCC;
}
table.pagina
{
  width: 600px;
  border: 1px solid #000000;
  background-color: #FFFFFF;
}
form
{
  margin: 0px;
  padding: 0px;
}
span.titel
{
  font-size: 25px;
  font-weight: bold;
}
a:link, a:active, a:visited
{
  color: #333333;
  text-decoration: underline;
}
a:hover
{
  color: #000000;
  text-decoration: overline;
}

-->
</style>

</head>
<body>

<form action="<?php echo $_SERVER["REQUEST_URI"]; ?>" method="post" name="php2html">
<table class="pagina" align="center" cellspacing="0" cellpadding="10">
  <tr>
    <td style="border-bottom: 1px solid #000000;" colspan="2" align="center"><span class="titel">PHP2HTML</span></td>
  </tr>
  <tr>
    <td style="padding-bottom: 0px;" align="center" colspan="2"><textarea name="php" cols="60" rows="20"></textarea></td>
  </tr>
  <tr>
    <td align="right" width="40%" style="padding-bottom: 0px;">Nummer de lijnen:</td>
	<td style="padding-bottom: 0px;"><input name="lijnnr[0]" type="checkbox" value="ja" checked="checked" /></td>
  </tr>
  <tr>
    <td align="right" valign="top" width="40%" style="padding-top: 5px; padding-bottom: 0px;">Output:</td>
	<td style="padding-top: 2px; padding-bottom: 2px;"><input name="type" type="radio" value="XHTML" checked="checked" style="position: relative; top: 2px;" /> XHTML 1.0<br /><input name="type" type="radio" value="HTML" style="position: relative; top: 2px;" /> HTML 4.01</td>
  </tr>
  <tr>
    <td align="right" valign="top" width="40%" style="padding-top: 5px; padding-bottom: 0px;">Wordwrap:</td>
	<td style="padding-top: 5px; padding-bottom: 0px;"><input name="wordwrap[0]" type="checkbox" value="ja" /></td>
  </tr>
  <tr>
    <td align="right" valign="top" width="40%" style="padding-top: 5px;">Maximum breedte:</td>
	<td style="padding-top: 5px;"><input name="breedte" type="text" value="Optioneel" size="7" maxlength="9" onclick="if(this.value='Optioneel') this.value = '';" style="font-size: 11px; padding: 0px;" />px</td>
  </tr>
  <tr>
    <td style="border-top: 1px solid #000000;" align="center" colspan="2"><input type="submit" value="Converteer" />&nbsp;&nbsp;&nbsp;<input type="reset" value="Wis alles" /></td>
  </tr>
</table>
</form>

<div align="center" style="margin-top: 3px;">&copy; <?php echo date("Y"); ?>-<?php echo (date(Y) + 1); ?> <a href="http://www.mvaprojects.be" target="_blank">MVAPROJE&copy;TS</a></div>

</body>
</html>

<?php
}
?> 