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 if ($title == '') $title = "Text Viewer";
24 foreach($files as $idx => $line) {
26 $files[$idx] = "<li><a href=\"?file=$line\">$line</a></li>\n";
31 if (!in_array($file, $files)) {
32 echo "Thought you could access some random file, didn't you. Not!";
36 $text = htmlspecialchars(file_get_contents($file));
38 // Do search, if requested
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
51 $lines = explode("\n", $text);
53 $digits = strlen($cnt);
54 $format = "%0" . $digits . 'd';
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;
63 $text = implode("\n", $lines);
67 $text = str_replace("\n", "<br>\n", $text);
69 // Make spaces non-breaking
70 $text = str_replace(" ", " ", $text);
72 // And change the non-duplicated ones back to spaces
73 $text = preg_replace("/([^;]) ([^&])/", "$1 $2", $text);
75 // Once more to get the remaining after single chars
76 $text = preg_replace("/([^;]) ([^&])/", "$1 $2", $text);
79 function searchBody($match) {
81 return '<a name="' . $i . '" href="#' . ++
$i . '"><b>' . $match[0] . '</b></a>';
87 <title
><?
echo htmlspecialchars($title); ?
></title
>
93 <div style
="font-family: courier">
98 echo "You may view the following files:<p>\n<ul>\n";
99 foreach ($files as $line) echo $line;
101 echo '<p>Add "?search=foo" to search for foo' . "\n";
102 echo '<br>Add "?numbers=yes" to add line numbers' . "\n";
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.