Source of “lister.php”
<?php	// Emacs settings: -*- mode: Fundamental; tab-width: 4; -*-

////////////////////////////////////////////////////////////////////////////
//                                                                        //
// Source code lister                                                     //
//                                                                        //
// Copyright 1998-2010, Andrew D. Birrell                                 //
//                                                                        //
// View a source file as HTML, with tabs converted to 4 spaces            //
//                                                                        //
// Views only files in the current directory and /var/www/html/support    //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

function putHtmlHeader($title) {
    // Put standard HTML boilerplate up to first "div".
    // Caller should html-escape $title
    ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css"><!--
body {
    margin: 0px;
    padding: 0px;
}
div.cr {
    background-color: #999999;
    color: #ffffff;
    border-top: 2px #cccccc ridge;
    border-bottom: 2px #cccccc ridge;
    padding: 2px;
    padding-left: 6px;
    font-size: 0.75em;
}
pre.source {
    margin: 0px;
    padding-top: 2px;
    padding-bottom: 3px;
    padding-left: 6px;
}
-->
</style>
<title><?php echo $title; ?></title>
</head>
<body>
<div class=cr>
<?php
}

function putHtmlTrailer() {
    // Put standard HTML trailing boilerplate
    ?>
</div>
</body>
</html>
<?php
}

function replaceAll($pattern, $replacement, $target) {
    // Iterate replacement while it's making a difference
    while (true) {
        $new = preg_replace($pattern, $replacement, $target);
        if ($new == $target) break;
        $target = $new;
    }
    return $new;
}

function addTab($str) {
    // Add spaces equivalent to a 4-space tab at end of $str
    return $str . str_repeat(" ", (4-((strlen($str)-1) % 4)));
}

if (!isset($_GET["file"])) {
    // Missing argument
    putHtmlHeader("Source Lister");
    echo "Usage: lister.php?file=filename\n";
    putHtmlTrailer();
} else {
    $file = $_GET["file"];
    if (get_magic_quotes_gpc() == 1) $file = stripslashes($file);
    $name = basename($file);
    if ($file != $name || is_dir($name)) {
        header("HTTP/1.0 403 Forbidden");
        putHtmlHeader("Forbidden");
        echo "The argument should be a simple file name, " .
            "not a path or directory name.\n";
        putHtmlTrailer();
    } else if ($name == "" ||
               !is_readable($name) &&
               !is_readable($name = "/var/www/html/support/$name")) {
        header("HTTP/1.0 404 Not Found");
        putHtmlHeader("Not Found");
        echo "File does not exist, or is not readable.\n";
        putHtmlTrailer();
    } else {
        header("Content-Type: text/html; charset=ISO-8859-1");
        putHtmlHeader("Source of &ldquo;" . htmlentities($file) .
                        "&rdquo;");
        ?>
Source of &ldquo;<?php echo htmlentities($file); ?>&rdquo;
</div>
<pre class=source>
<?php
        $str = file_get_contents($name);
        $str = preg_replace("#\r\n?#", "\n", $str);
        $str = replaceAll("#(\n[^\t\n]*)\t#e",
            "addTab('$1')", $str);
        echo htmlentities($str);
        ?>
</pre>
<div class=cr>
End of listing
<?php
        putHtmlTrailer();
    }
}
?>
End of listing