[23] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * The Paper Tape Project -- Documentation |
---|
| 4 | * |
---|
[31] | 5 | * This is a very small "template" like system that quickly wraps a design |
---|
| 6 | * around almost every file (html,txt,source codes,...) in the Paper-Tape-Project |
---|
| 7 | * repository. |
---|
| 8 | * It should handle links, etc. well and works with PATH_INFO; typical calls are |
---|
| 9 | * linke "path/to/documentation/index.php/path/to/file.htm". |
---|
[23] | 10 | * There's a small menu nested as PHP array in this script. |
---|
| 11 | * |
---|
| 12 | * Use this small script as in the public domain. |
---|
[31] | 13 | * 22.12.2008, 12.02.2009 -- Sven Koeppel |
---|
[23] | 14 | **/ |
---|
| 15 | |
---|
[30] | 16 | // the web site root path, taken relatively to the |
---|
| 17 | // global project path |
---|
| 18 | $doc = 'documentation'; |
---|
| 19 | |
---|
[23] | 20 | // where to get the file to display: Can be simply |
---|
| 21 | // $filename = $_SERVER['QUERY_STRING']; |
---|
| 22 | // or path info (advantage that links will work): |
---|
| 23 | $filename = substr($_SERVER['PATH_INFO'], 1); # strip trailing slash: /a/b => a/b |
---|
| 24 | |
---|
[32] | 25 | // show default page if no one is given: |
---|
[30] | 26 | $default_page = $doc.'/start.htm'; |
---|
[32] | 27 | #if(empty($filename)) { header("Location: $_SERVER[PHP_SELF]/$default_page"); exit; } |
---|
| 28 | if(empty($filename)) $filename = $default_page; |
---|
[23] | 29 | |
---|
| 30 | // check if filename is correct |
---|
| 31 | $jail_dir = realpath('../'); # don't display files higher than parental dir |
---|
| 32 | $file_path = realpath('../'.$filename); |
---|
[30] | 33 | $extension = substr(strrchr($file_path, '.'),1); |
---|
[39] | 34 | $good_extensions = array('c', 'cc', 'h', 'cpp', 'pl', 'htm', 'html', 'txt'); |
---|
[30] | 35 | |
---|
| 36 | #var_dump($extension, $filename, $file_path, $jail_dir, $good_extensions, in_array($extension, $good_extensions)); exit(); |
---|
| 37 | if(0 !== strncmp($file_path, $jail_dir, strlen($jail_dir)) |
---|
| 38 | || !in_array($extension, $good_extensions) |
---|
| 39 | || !is_readable('../'.$filename) ) { |
---|
[23] | 40 | // user want's display file higher than parental dir |
---|
[30] | 41 | // or file is not a nice file (like *.htm) or file is not |
---|
| 42 | // readable, then Redirect to file! Apache will handle error ;-) |
---|
[23] | 43 | header("Location: $_SERVER[SCRIPT_NAME]/../../$filename"); |
---|
| 44 | echo "Won't make file $filename beautiful\nRefering to that file...\n"; |
---|
| 45 | exit; |
---|
| 46 | } |
---|
| 47 | |
---|
[33] | 48 | // do a little bit lightweight caching: |
---|
| 49 | // don't calculate etag since that's too expensive |
---|
| 50 | $last_modified_time = max( |
---|
| 51 | filemtime("../$filename"), filemtime($_SERVER['SCRIPT_FILENAME']) |
---|
| 52 | ); |
---|
| 53 | #$etag = md5_file("../$filename"); |
---|
| 54 | |
---|
| 55 | header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); |
---|
| 56 | #header("Etag: $etag"); |
---|
| 57 | |
---|
| 58 | if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) { |
---|
| 59 | #|| trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { |
---|
| 60 | header("HTTP/1.1 304 Not Modified"); |
---|
| 61 | exit; |
---|
| 62 | } |
---|
| 63 | |
---|
[30] | 64 | // since PHP cannot initialize global variables on an intelligent way: |
---|
| 65 | $exec_action = $extension=='htm' ? 'display_file' : 'syntax_highlight_file'; |
---|
| 66 | |
---|
| 67 | function display_file($filename) { |
---|
| 68 | // display HTML file. |
---|
| 69 | // parse file until <body> starts... |
---|
| 70 | $handle = fopen("../$filename", 'r'); |
---|
| 71 | if(!$handle) { print "Error at opening $filename\n"; exit; } |
---|
| 72 | while(!feof($handle)) { |
---|
| 73 | if(strpos(fgets($handle), "<body") !== false) |
---|
| 74 | break; |
---|
| 75 | } |
---|
| 76 | fpassthru($handle); |
---|
[23] | 77 | } |
---|
[30] | 78 | |
---|
| 79 | function syntax_highlight_file($filename) { |
---|
| 80 | // syntax highlight some file |
---|
[31] | 81 | global $extension; // yes, this is bad. |
---|
[30] | 82 | echo "<h2>Contents of <code>$filename</code></h2>"; |
---|
[31] | 83 | if($extension == 'txt') { |
---|
| 84 | echo "<pre>"; |
---|
| 85 | readfile("../$filename"); |
---|
| 86 | echo "</pre>"; |
---|
| 87 | } else { |
---|
| 88 | include_once('geshi.php'); |
---|
| 89 | $geshi = new GeSHi(); |
---|
| 90 | $geshi->load_from_file("../$filename"); |
---|
| 91 | $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); |
---|
| 92 | echo $geshi->parse_code(); |
---|
| 93 | } |
---|
[30] | 94 | } |
---|
[23] | 95 | ?> |
---|
| 96 | <html> |
---|
| 97 | <head> |
---|
[30] | 98 | <title>The Punched Paper Project / <?=$filename; ?></title> |
---|
| 99 | <link rel="stylesheet" href="<?=$_SERVER['SCRIPT_NAME'].'/'.$doc; ?>/style.css" type="text/css"> |
---|
[23] | 100 | <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> |
---|
| 101 | </head> |
---|
| 102 | <body> |
---|
[30] | 103 | <div id="header"> |
---|
[37] | 104 | <h1><a href="<?=$_SERVER['SCRIPT_NAME']; ?>">The Punched Paper Project</a></h1> |
---|
| 105 | <p class="subtitle"><a href="<?=$_SERVER['SCRIPT_NAME'].'/../../'.$filename; ?>" title="Read the original file">/<?=$filename; ?></a></p> |
---|
| 106 | <a href="http://dev.technikum29.de/" class="powered-by"><img src="<?=$_SERVER['SCRIPT_NAME'].'/'.$doc; ?>/src/powered-by-icon.png" alt="a project from the technikum29 development zone"></a> |
---|
[30] | 107 | </div> |
---|
[23] | 108 | <div id="navigation"> |
---|
| 109 | <?php |
---|
| 110 | |
---|
| 111 | $nav = array( |
---|
[30] | 112 | #all URLs are taken relative to one subdir (e.g. documentation/). |
---|
[37] | 113 | 'Home' => $default_page, |
---|
[30] | 114 | 'The Paper Tape Project' => array( |
---|
| 115 | 'Start' => $doc.'/paper-tape-project.htm', |
---|
[31] | 116 | 'Abstract' => array( |
---|
| 117 | 'Subproject overview' => 'paper-tape/README.txt', |
---|
| 118 | 'To Do' => 'paper-tape/TODO.txt', |
---|
| 119 | 'Using Windows' => 'paper-tape/WINDOWS.txt', |
---|
| 120 | ), |
---|
[30] | 121 | 'Data' => 'paper-tape/daten/README.htm', |
---|
| 122 | 'Perl tools' => 'paper-tape/perl-tools/README.htm', |
---|
| 123 | 'Labeling and Fonts' => array( |
---|
| 124 | 'Generating Labels' => 'paper-tape/schriften/README.htm', |
---|
[31] | 125 | 'Font Files' => 'paper-tape/schriften/font_files.htm' |
---|
[30] | 126 | ), |
---|
[31] | 127 | 'Devices' => array( |
---|
| 128 | 'Facit Tape Punch' => 'paper-tape/driver/FACIT-MANUAL.txt', |
---|
| 129 | 'Ghielmetti Tape Reader' => 'paper-tape/reader/GHIELMETTI-MANUAL.txt' |
---|
| 130 | ), |
---|
| 131 | 'Driver framework' => array( |
---|
| 132 | 'Legacy reader documentation' => 'paper-tape/reader/README.txt', |
---|
| 133 | 'Legacy puncher documentation' => 'paper-tape/puncher/README.txt', |
---|
| 134 | 'Legacy old puncher documentation' => 'paper-tape/userspace-driver/README.txt' |
---|
| 135 | ), |
---|
[30] | 136 | 'Visualisation' => 'paper-tape/visualisator/README.htm', |
---|
[31] | 137 | 'Web Frontend' => array( |
---|
| 138 | 'Abstract' => 'paper-tape/web-frontend/README.txt', |
---|
| 139 | 'Generating Paper Tapes online!' => 'paper-tape/web-frontend/' |
---|
| 140 | ) |
---|
[30] | 141 | ), |
---|
| 142 | 'The Punch Card Project' => array( |
---|
[39] | 143 | 'Start' => 'punch-card/README.htm', |
---|
[34] | 144 | 'Device Drivers' => array( |
---|
[35] | 145 | 'Overview' => 'punch-card/driver/README.htm', |
---|
| 146 | 'Nixdorf 0377.01' => 'punch-card/driver/nixdorf-0377.01/README.htm' |
---|
[34] | 147 | ) |
---|
[30] | 148 | ), |
---|
[31] | 149 | #'About' => $doc.'/about.htm' |
---|
[23] | 150 | ); |
---|
| 151 | |
---|
| 152 | function print_nav($nav) { |
---|
| 153 | global $filename; |
---|
| 154 | print "<ul>"; foreach($nav as $name => $url) { |
---|
[37] | 155 | echo "<li>"; |
---|
[23] | 156 | if(is_array($url)) { |
---|
[37] | 157 | print "<em>$name</em>"; print_nav($url); |
---|
[23] | 158 | } else |
---|
[37] | 159 | print $url == $filename ? "<strong title=\"You're currently on that page\">$name</strong>" : "<a href=\"$_SERVER[SCRIPT_NAME]/$url\">$name</a>"; |
---|
| 160 | echo "</li>"; |
---|
[23] | 161 | } |
---|
[37] | 162 | print "</ul>"; |
---|
[23] | 163 | } |
---|
| 164 | print_nav($nav); |
---|
| 165 | ?> |
---|
| 166 | </div> |
---|
| 167 | <div id="content"> |
---|
| 168 | <?php |
---|
[30] | 169 | call_user_func($exec_action, $filename);#, $exec_array); |
---|
[23] | 170 | ?> |
---|
| 171 | </div> |
---|
[30] | 172 | <div id="footer"> |
---|
[37] | 173 | This page was last modified at <?=date('r', $last_modified_time); ?> |
---|
[33] | 174 | <br/> |
---|
[30] | 175 | Further reading: |
---|
[23] | 176 | <a href="http://dev.technikum29.de">dev.technikum29.de</a> | |
---|
| 177 | <a href="http://dev.technikum29.de/websvn/listing.php?repname=paper-tape-project&path=%2F&sc=0">Paper-Tape-Project WebSVN</a> | |
---|
| 178 | <a href="http://koeppel.homeunix.org">koeppel.homeunix.org</a> |
---|
[32] | 179 | </div> |
---|