4 // Allows viewing of files with word wrap.
7 if (get_magic_quotes_gpc()) return stripslashes($x);
11 $file = mq($_GET['file']);
12 $title = mq($_GET['title']);
13 $numbers = mq($_GET['numbers']);
14 $search = mq($_GET['search']);
18 if ($title == '') $title = $file;
20 $files = explode("\n", file_get_contents('viewtext.txt'));
23 foreach($files as $idx => $line) {
25 $files[$idx] = "<li><a href=\"?file=$line\">$line</a></li>\n";
30 if (!in_array($file, $files)) {
31 echo "Thought you could access some random file, didn't you. Not!";
35 $text = htmlspecialchars(file_get_contents($file));
37 // Do search, if requested
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
50 $lines = explode("\n", $text);
52 $digits = strlen($cnt);
53 $format = "%0" . $digits . 'd';
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;
62 $text = implode("\n", $lines);
66 $text = str_replace("\n", "<br>\n", $text);
68 // Make spaces non-breaking
69 $text = str_replace(" ", " ", $text);
71 // And change the non-duplicated ones back to spaces
72 $text = preg_replace("/([^;]) ([^&])/", "$1 $2", $text);
74 // Once more to get the remaining after single chars
75 $text = preg_replace("/([^;]) ([^&])/", "$1 $2", $text);
78 function searchBody($match) {
80 return '<a name="' . $i . '" href="#' . ++
$i . '"><b>' . $match[0] . '</b></a>';
86 <title
><?
echo htmlspecialchars($title); ?
></title
>
92 <div style
="font-family: courier">
97 echo "You may view the following files:<p>\n<ul>\n";
98 foreach ($files as $line) echo $line;
100 echo '<p>Add "?search=foo" to search for foo' . "\n";
101 echo '<br>Add "?numbers=yes" to add line numbers' . "\n";
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.