Link to Trubanc 1.0b1 announcement
[loomclient.git] / viewtext.php
blob20474d9f8d9ae13f17077c6a7e0efda30f0afcbc
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><? echo htmlspecialchars($title); ?></title>
98 </head>
99 <body>
101 if ($file != '') {
103 <div style="font-family: courier">
104 <? echo $text; ?>
105 </div>
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>
121 /* ***** BEGIN LICENSE BLOCK *****
122 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/Apache 2.0
124 * The contents of this file are subject to the Mozilla Public License Version
125 * 1.1 (the "License"); you may not use this file except in compliance with
126 * the License. You may obtain a copy of the License at
127 * http://www.mozilla.org/MPL/
129 * Software distributed under the License is distributed on an "AS IS" basis,
130 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
131 * for the specific language governing rights and limitations under the
132 * License.
134 * The Original Code is LoomClient PHP library
136 * The Initial Developer of the Original Code is
137 * Bill St. Clair.
138 * Portions created by the Initial Developer are Copyright (C) 2008
139 * the Initial Developer. All Rights Reserved.
141 * Contributor(s):
142 * Bill St. Clair <bill@billstclair.com>
144 * Alternatively, the contents of this file may be used under the
145 * terms of the GNU General Public License Version 2 or later (the
146 * "GPL"), the GNU Lesser General Public License Version 2.1 or later
147 * (the "LGPL"), or The Apache License Version 2.0 (the "AL"), in
148 * which case the provisions of the GPL, LGPL, or AL are applicable
149 * instead of those above. If you wish to allow use of your version of
150 * this file only under the terms of the GPL, the LGPL, or the AL, and
151 * not to allow others to use your version of this file under the
152 * terms of the MPL, indicate your decision by deleting the provisions
153 * above and replace them with the notice and other provisions
154 * required by the GPL or the LGPL. If you do not delete the
155 * provisions above, a recipient may use your version of this file
156 * under the terms of any one of the MPL, the GPL the LGPL, or the AL.
157 ****** END LICENSE BLOCK ***** */