billstclair / trubanc

Trubanc is an anonymous, digitally-signed vault and trading system.

This URL has Read+Write access

trubanc / viewtext.php
100644 136 lines (115 sloc) 3.837 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
 
  // Simple file viewer
  // Allows viewing of files with word wrap.
 
function mq($x) {
  if (get_magic_quotes_gpc()) return stripslashes($x);
  else return $x;
}
 
$file = mq($_GET['file']);
$title = mq($_GET['title']);
$numbers = mq($_GET['numbers']);
$search = mq($_GET['search']);
 
$file = trim($file);
 
if ($title == '') $title = $file;
 
$files = explode("\n", file_get_contents('viewtext.txt'));
 
foreach($files as $idx => $line) {
  if ($line != '') {
    $parts = explode('|', $line);
    $name = $parts[0];
    $label = '';
    if (count($parts) > 1) $label = " - " . $parts[1];
    if ($file != '') $files[$idx] = $name;
    else {
      $files[$idx] = "<input type='radio' name='file' value='$name'/> <a href=\"?file=$name\">$name</a>$label<br/>";
      if (count($files) > ($idx+2) && $files[$idx+1] == '') {
        $files[$idx] .= "<br/>";
      }
    }
  }
}
 
if ($file == '') {
  if ($title == '') $title = "Text Viewer";
} else {
  if (!in_array($file, $files)) {
    echo "Thought you could access some random file, didn't you. Not!";
    return;
  }
 
  $text = htmlspecialchars(file_get_contents($file));
 
  // Do search, if requested
  if ($search != '') {
    $i = 1;
    // Escape special chars in the search string
    $search = preg_replace("=([/\\\\^$.[\\]|()?*+{}])=", '\\\\$1', $search);
    // Now replace instances of the search string
    $text = preg_replace_callback('/' . $search . '/i', "searchBody", $text);
    // Make the last match loop around
    $text = str_replace('href="#' . $i . '">', 'href="#1">', $text);
  }
 
  // Add line numbers, if requested
  if ($numbers != '') {
    $lines = explode("\n", $text);
    $cnt = count($lines);
    $digits = strlen($cnt);
    $format = "%0" . $digits . 'd';
    $i = 1;
    foreach ($lines as $idx => $line) {
      if ($i < $cnt || $line != '') {
        $lines[$idx] = '<a name="line' . $i . '" href="#line' . $i . '">' .
          sprintf($format, $i) . '</a> ' . $line;
      }
      $i++;
    }
    $text = implode("\n", $lines);
  }
 
  // Add line breaks
  $text = str_replace("\n", "<br>\n", $text);
 
  // Make spaces non-breaking
  $text = str_replace(" ", "&nbsp;", $text);
 
  // And change the non-duplicated ones back to spaces
  $text = preg_replace("/([^;])&nbsp;([^&])/", "$1 $2", $text);
 
  // Once more to get the remaining &nbsp; after single chars
  $text = preg_replace("/([^;])&nbsp;([^&])/", "$1 $2", $text);
}
 
function searchBody($match) {
  global $i;
  return '<a name="' . $i . '" href="#' . ++$i . '"><b>' . $match[0] . '</b></a>';
}
 
?>
<html>
<head>
<title><?php echo htmlspecialchars($title); ?></title>
</head>
<body>
<?php
if ($file != '') {
?>
<div style="font-family: courier">
<?php echo $text; ?>
</div>
<?php
} else {
  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";
  echo "<form action='#1' method='get'>\n";
  foreach ($files as $line) echo $line;
  echo "<p>Search selected file for: <input type='text' name='search'> <input type='submit' name='Go' value='Go'/>\n";
  echo "<br>View selected file with line numbers? <input type='submit' name='numbers' value='Yes'>\n";
  echo "</form>\n";
 }
?>
</body>
</html>
 
<?php
 
// Copyright 2008 Bill St. Clair
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions
// and limitations under the License.
 
?>