New article: Trubanc Server Working
[loomclient.git] / viewtext.php
blob66a254745b24c1994b5a7ea0546d14bd67236ed4
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 if ($file == '') {
23 if ($title == '') $title = "Text Viewer";
24 foreach($files as $idx => $line) {
25 if ($line != '') {
26 $files[$idx] = "<li><a href=\"?file=$line\">$line</a></li>\n";
29 } else {
31 if (!in_array($file, $files)) {
32 echo "Thought you could access some random file, didn't you. Not!";
33 return;
36 $text = htmlspecialchars(file_get_contents($file));
38 // Do search, if requested
39 if ($search != '') {
40 $i = 1;
41 // Escape special chars in the search string
42 $search = preg_replace("=([/\\\\^$.[\\]|()?*+{}])=", '\\\\$1', $search);
43 // Now replace instances of the search string
44 $text = preg_replace_callback('/' . $search . '/i', "searchBody", $text);
45 // Make the last match loop around
46 $text = str_replace('href="#' . $i . '">', 'href="#1">', $text);
49 // Add line numbers, if requested
50 if ($numbers != '') {
51 $lines = explode("\n", $text);
52 $cnt = count($lines);
53 $digits = strlen($cnt);
54 $format = "%0" . $digits . 'd';
55 $i = 1;
56 foreach ($lines as $idx => $line) {
57 if ($i < $cnt || $line != '') {
58 $lines[$idx] = '<a name="line' . $i . '" href="#line' . $i . '">' .
59 sprintf($format, $i) . '</a> ' . $line;
61 $i++;
63 $text = implode("\n", $lines);
66 // Add line breaks
67 $text = str_replace("\n", "<br>\n", $text);
69 // Make spaces non-breaking
70 $text = str_replace(" ", "&nbsp;", $text);
72 // And change the non-duplicated ones back to spaces
73 $text = preg_replace("/([^;])&nbsp;([^&])/", "$1 $2", $text);
75 // Once more to get the remaining &nbsp; after single chars
76 $text = preg_replace("/([^;])&nbsp;([^&])/", "$1 $2", $text);
79 function searchBody($match) {
80 global $i;
81 return '<a name="' . $i . '" href="#' . ++$i . '"><b>' . $match[0] . '</b></a>';
85 <html>
86 <head>
87 <title><? echo htmlspecialchars($title); ?></title>
88 </head>
89 <body>
91 if ($file != '') {
93 <div style="font-family: courier">
94 <? echo $text; ?>
95 </div>
97 } else {
98 echo "You may view the following files:<p>\n<ul>\n";
99 foreach ($files as $line) echo $line;
100 echo "</ul>\n";
101 echo '<p>Add "?search=foo" to search for foo' . "\n";
102 echo '<br>Add "?numbers=yes" to add line numbers' . "\n";
105 </body>
106 </html>
110 // Copyright 2008 Bill St. Clair
112 // Licensed under the Apache License, Version 2.0 (the "License");
113 // you may not use this file except in compliance with the License.
114 // You may obtain a copy of the License at
116 // http://www.apache.org/licenses/LICENSE-2.0
118 // Unless required by applicable law or agreed to in writing, software
119 // distributed under the License is distributed on an "AS IS" BASIS,
120 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121 // See the License for the specific language governing permissions
122 // and limitations under the License.