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