A bunch of little errors, including a fix to the balancehash computation
[Trubanc.git] / viewtext.php
blob8cc2c16e5c748a697e2fcfbf53f72e2e0502ccf8
1 <?php
3 // Simple file viewer
4 // Allows viewing of files with word wrap.
6 function mq($x) {
7 if (get_magic_quotes_gpc()) return stripslashes($x);
8 else return $x;
11 $file = mq($_GET['file']);
12 $title = mq($_GET['title']);
13 $numbers = mq($_GET['numbers']);
14 $search = mq($_GET['search']);
16 $file = trim($file);
18 if ($title == '') $title = $file;
20 $files = explode("\n", file_get_contents('viewtext.txt'));
22 foreach($files as $idx => $line) {
23 if ($line != '') {
24 $parts = explode('|', $line);
25 $name = $parts[0];
26 $label = '';
27 if (count($parts) > 1) $label = " - " . $parts[1];
28 if ($file != '') $files[$idx] = $name;
29 else {
30 $files[$idx] = "<input type='radio' name='file' value='$name'/> <a href=\"?file=$name\">$name</a>$label<br/>";
31 if (count($files) > ($idx+2) && $files[$idx+1] == '') {
32 $files[$idx] .= "<br/>";
38 if ($file == '') {
39 if ($title == '') $title = "Text Viewer";
40 } else {
41 if (!in_array($file, $files)) {
42 echo "Thought you could access some random file, didn't you. Not!";
43 return;
46 $text = htmlspecialchars(file_get_contents($file));
48 // Do search, if requested
49 if ($search != '') {
50 $i = 1;
51 // Escape special chars in the search string
52 $search = preg_replace("=([/\\\\^$.[\\]|()?*+{}])=", '\\\\$1', $search);
53 // Now replace instances of the search string
54 $text = preg_replace_callback('/' . $search . '/i', "searchBody", $text);
55 // Make the last match loop around
56 $text = str_replace('href="#' . $i . '">', 'href="#1">', $text);
59 // Add line numbers, if requested
60 if ($numbers != '') {
61 $lines = explode("\n", $text);
62 $cnt = count($lines);
63 $digits = strlen($cnt);
64 $format = "%0" . $digits . 'd';
65 $i = 1;
66 foreach ($lines as $idx => $line) {
67 if ($i < $cnt || $line != '') {
68 $lines[$idx] = '<a name="line' . $i . '" href="#line' . $i . '">' .
69 sprintf($format, $i) . '</a> ' . $line;
71 $i++;
73 $text = implode("\n", $lines);
76 // Add line breaks
77 $text = str_replace("\n", "<br>\n", $text);
79 // Make spaces non-breaking
80 $text = str_replace(" ", "&nbsp;", $text);
82 // And change the non-duplicated ones back to spaces
83 $text = preg_replace("/([^;])&nbsp;([^&])/", "$1 $2", $text);
85 // Once more to get the remaining &nbsp; after single chars
86 $text = preg_replace("/([^;])&nbsp;([^&])/", "$1 $2", $text);
89 function searchBody($match) {
90 global $i;
91 return '<a name="' . $i . '" href="#' . ++$i . '"><b>' . $match[0] . '</b></a>';
95 <html>
96 <head>
97 <title><?php echo htmlspecialchars($title); ?></title>
98 </head>
99 <body>
100 <?php
101 if ($file != '') {
103 <div style="font-family: courier">
104 <?php echo $text; ?>
105 </div>
106 <?php
107 } else {
108 echo "You may view the following files.<br>Click on a file name or click a radio button and click one of the buttons below the list.<p>\n";
109 echo "<form action='#1' method='get'>\n";
110 foreach ($files as $line) echo $line;
111 echo "<p>Search selected file for: <input type='text' name='search'> <input type='submit' name='Go' value='Go'/>\n";
112 echo "<br>View selected file with line numbers? <input type='submit' name='numbers' value='Yes'>\n";
113 echo "</form>\n";
116 </body>
117 </html>
119 <?php
121 // Copyright 2008 Bill St. Clair
123 // Licensed under the Apache License, Version 2.0 (the "License");
124 // you may not use this file except in compliance with the License.
125 // You may obtain a copy of the License at
127 // http://www.apache.org/licenses/LICENSE-2.0
129 // Unless required by applicable law or agreed to in writing, software
130 // distributed under the License is distributed on an "AS IS" BASIS,
131 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132 // See the License for the specific language governing permissions
133 // and limitations under the License.