<?php
/**
 * Custom source viewer with line numbering.
 * Use with .src or .phps files.
 *
 * mod_rewrite rule (for .src extension):
 *   RewriteRule ^(.+\.src)$ path/to/source.php?file=$1 [E=source:true]
 * The [E=source:true] will allow _any_ redirected file to be sourced,
 * regardless of extension.
 *
 * @author Matt Sparks
 * @version 20050601
 */
$file=$_GET["file"];
$path=$_SERVER["DOCUMENT_ROOT"]."/";
$nicename=basename($file);

$dlink=($_SERVER["REDIRECT_URL"]) ? "?download=1" "?".$_SERVER["QUERY_STRING"]."&download=1"

//die(print_r($_SERVER,1));

// Unless [E=source:true] is specified in a rewrite rule, strictly
// require the sourced file to end in .phps or .src.
if (!ereg("\.(src|phps)$",$file) && $_SERVER["REDIRECT_source"]!=="true") {
    die(
"$file: File must end in .src or .phps");
}
if (!
file_exists($path.$file)) die("$file: does not exist");

// Check if the file is to be downloaded
if (strpos($_SERVER["REQUEST_URI"],"download=1")) {
    
header("Content-Type: text/plain");
    
header("Content-Disposition: attachment; filename=$nicename");
    echo 
file_get_contents($path.$file);
    exit();
}

$hl=trim(highlight_file($path.$file,true));
$s=split("<br />",$hl);
?>
<!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" xml:lang="en">
<head>
 <title>
  <?=$nicename?>
 </title>
<style type="text/css">
<!--
* {
    padding: 0;
    margin: 0;
}

body {
    font-size: 11px;
    font-family: monospace;
    line-height: 15px;
}

#gutter {
    float: left;
    clear: left;
    background-color: #CCCCCC;
    border-right: 1px solid black;
    color: #666666;
    text-align: right;
    padding-right: 2px;
    width: 30px;
}

#code {
    margin-left: 40px;
    display: block;
}

#header {
    text-align: right;
    font-size: 14px;
    padding: 5px;
    border-bottom: 1px solid black;
}
-->
</style>
</head>
<body>
<div id="header">
 <p><?=$nicename?> (<a href="<?=$dlink?>">download</a>)</p>
</div>

<div id="gutter">
 <p>
<?php
$lines
=count($s);
for(
$i=1;$i<=count($s);$i++) {
    echo 
"  $i<br />\n";
}
?>
 </p>
</div>

<div id="code">
<p>
<?=join("<br />\n",$s);?>
</p>
</div>
</body>
</html>