source: projects/documentation/index.php @ 32

Last change on this file since 32 was 32, checked in by sven, 11 years ago

A minor fix in index.php: Making up/down directory for users
workable (by removing unneccessary redirect)

File size: 5.7 KB
Line 
1<?php
2/**
3 * The Paper Tape Project -- Documentation
4 *
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".
10 * There's a small menu nested as PHP array in this script.
11 *
12 * Use this small script as in the public domain.
13 * 22.12.2008, 12.02.2009 -- Sven Koeppel
14 **/
15
16  // the web site root path, taken relatively to the
17  // global project path
18  $doc = 'documentation';
19
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
25  // show default page if no one is given:
26  $default_page = $doc.'/start.htm';
27  #if(empty($filename)) { header("Location: $_SERVER[PHP_SELF]/$default_page"); exit; }
28  if(empty($filename)) $filename = $default_page;
29
30  // check if filename is correct
31  $jail_dir = realpath('../'); # don't display files higher than parental dir
32  $file_path = realpath('../'.$filename);
33  $extension = substr(strrchr($file_path, '.'),1);
34  $good_extensions = array('c', 'cpp', 'pl', 'htm', 'txt');
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) ) {
40      // user want's display file higher than parental dir
41      // or file is not a nice file (like *.htm) or file is not
42      // readable, then Redirect to file! Apache will handle error ;-)
43      header("Location: $_SERVER[SCRIPT_NAME]/../../$filename");
44      echo "Won't make file $filename beautiful\nRefering to that file...\n";
45      exit;
46  }
47
48  // since PHP cannot initialize global variables on an intelligent way:
49  $exec_action = $extension=='htm' ? 'display_file' : 'syntax_highlight_file';
50
51  function display_file($filename) {
52      // display HTML file.
53      // parse file until <body> starts...
54      $handle = fopen("../$filename", 'r');
55      if(!$handle) { print "Error at opening $filename\n"; exit; }
56      while(!feof($handle)) {
57          if(strpos(fgets($handle), "<body") !== false)
58              break;
59      }
60      fpassthru($handle);
61  }
62
63  function syntax_highlight_file($filename) {
64      // syntax highlight some file
65      global $extension; // yes, this is bad.
66      echo "<h2>Contents of <code>$filename</code></h2>";
67      if($extension == 'txt') {
68          echo "<pre>";
69          readfile("../$filename");
70          echo "</pre>";
71      } else {
72          include_once('geshi.php');
73          $geshi = new GeSHi();
74          $geshi->load_from_file("../$filename");
75          $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
76          echo $geshi->parse_code();
77      }
78  }
79?>
80<html>
81<head>
82  <title>The Punched Paper Project / <?=$filename; ?></title>
83  <link rel="stylesheet" href="<?=$_SERVER['SCRIPT_NAME'].'/'.$doc; ?>/style.css" type="text/css">
84  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
85</head>
86<body>
87<div id="header">
88  <h1>The Punched Paper Project</h1>
89  <p class="subtitle">/<?=$filename; ?></p>
90  <a href="http://dev.technikum29.de/" class="powered-by"><img src="<?=$_SERVER['SCRIPT_NAME'].'/'.$doc; ?>/src/powered-by-icon.png" alt="technikum29 development zone"></a>
91</div>
92<div id="navigation">
93<?php
94
95 $nav = array(
96        #all URLs are taken relative to one subdir (e.g. documentation/).
97        'Start' => $default_page,
98        'The Paper Tape Project' => array(
99                'Start' => $doc.'/paper-tape-project.htm',
100                'Abstract' => array(
101                        'Subproject overview' => 'paper-tape/README.txt',
102                        'To Do' => 'paper-tape/TODO.txt',
103                        'Using Windows' => 'paper-tape/WINDOWS.txt',
104                ),
105                'Data' => 'paper-tape/daten/README.htm',
106                'Perl tools' => 'paper-tape/perl-tools/README.htm',
107                'Labeling and Fonts' => array(
108                        'Generating Labels' => 'paper-tape/schriften/README.htm',
109                        'Font Files' => 'paper-tape/schriften/font_files.htm'
110                ),
111                'Devices' => array(
112                        'Facit Tape Punch' => 'paper-tape/driver/FACIT-MANUAL.txt',
113                        'Ghielmetti Tape Reader' => 'paper-tape/reader/GHIELMETTI-MANUAL.txt'
114                ),
115                'Driver framework' => array(
116                        'Legacy reader documentation' => 'paper-tape/reader/README.txt',
117                        'Legacy puncher documentation' => 'paper-tape/puncher/README.txt',
118                        'Legacy old puncher documentation' => 'paper-tape/userspace-driver/README.txt'
119                ),
120                'Visualisation' => 'paper-tape/visualisator/README.htm',
121                'Web Frontend' => array(
122                        'Abstract' => 'paper-tape/web-frontend/README.txt',
123                        'Generating Paper Tapes online!' => 'paper-tape/web-frontend/'
124                )
125        ),
126        'The Punch Card Project' => array(
127                'Start' => $doc.'/punch-card.htm'
128        ),
129        #'About' => $doc.'/about.htm'
130 );
131
132 function print_nav($nav) {
133    global $filename;
134    print "<ul>"; foreach($nav as $name => $url) {
135        echo "\t<li>";
136        if(is_array($url)) {
137           print "<em>$name</em>\n"; print_nav($url);
138        } else
139           print $url == $filename ? "<strong>$name</strong>" : "<a href=\"$_SERVER[SCRIPT_NAME]/$url\">$name</a>";
140        echo "</li>\n";
141    }
142    print "</ul>\n";
143 }
144 print_nav($nav);
145?>
146</div>
147<div id="content">
148<?php
149  call_user_func($exec_action, $filename);#, $exec_array);
150?>
151</div>
152<div id="footer">
153  Further reading:
154  <a href="http://dev.technikum29.de">dev.technikum29.de</a> |
155  <a href="http://dev.technikum29.de/websvn/listing.php?repname=paper-tape-project&path=%2F&sc=0">Paper-Tape-Project WebSVN</a> |
156  <a href="http://koeppel.homeunix.org">koeppel.homeunix.org</a>
157</div>
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License