<?php
//This PHP program outputs a colored, formatted, version of it's own source as HTML.
//The resulting HTML code is not the source, but the rendered version in the browser is.
//It relies on javascript for some styling, with just a touch of css to be trouble.
//Due to some issues with the way I wrote it, this will only work properly in *nix systems.

//Setup my functions
function thisisjavascript(){
?>
<script type="text/javascript">
     function highlightme(){
           size=0;
           while(typeof(document.getElementById(size))!="undefined"){
                 dgebis=document.getElementById(size);
                 check=dgebis.innerHTML;
                 color="#000000";
                 if(check.search('//')!=-1 && check.search("'//")==-1)
                       color="#00ff00";
                 if(check.search('{')!=-1)
                       color="#0000ff";
                 if(check.search('}')!=-1)
                       color="#0000ff";
                 if(check.search("&lt;script")!=-1 || check.search("&lt;/script")!=-1)
                       color="#ff33ff";
                 if(check.search('return')!=-1)
                       if(check.search("'return")==-1)
                             color="#aaaa33";
           
                 dgebis.style.color=color;
                 size++;
           }
     }
     </script>
<?php
}

function starthtml($titletext){
?>
<html>
<head>
     <title><?php echo $titletext;?></title>
     <?php thisisjavascript(); ?>
     <style type="text/css">
     body{font-family:arial;font-size:80%;}
     </style>
</head>

<body onload="highlightme()">
<?php
}

function endhtml(){
?>

</body>
</html>
<?php
}

function getfile(){
     $temp=explode('/',$_SERVER['SCRIPT_NAME']);
     $tlen=sizeof($temp)-1;
     return $temp[$tlen];
}

function formatforoutput($incomingsource){
     return str_replace("?","&#63;",str_replace("\n",'<br />'."\n",str_replace(' ','&nbsp;',str_replace("\t","\t     ",str_replace('>','&gt;',str_replace('<','&lt;',str_replace('&','&amp;',$incomingsource)))))));
}

function addspantags($incomingsource){
     $srcarray=explode("\n", $incomingsource);
     $srcsize=sizeof($srcarray);
     $newsource='';
     for($i=0;$i<$srcsize;$i++){
           $srcarray[$i]='<span id="'.$i.'">'.$srcarray[$i].'</span>';
     }
     return implode("\n", $srcarray);
}

//This is where the program actually executes
$iam=getfile();
starthtml($iam);
$mysourceis=addspantags(formatforoutput(file_get_contents($iam)));
echo $mysourceis;
endhtml();
?>