source: trunk/FACT++/www/showlog.php@ 15252

Last change on this file since 15252 was 15189, checked in by tbretz, 12 years ago
Updated to include 'to top' and 'to bottom' links
File size: 10.4 KB
Line 
1<?php
2
3require_once("smartfact/config.php");
4
5function login()
6{
7 global $ldaphost;
8 global $baseDN;
9 global $groupDN;
10
11 $username = $_SERVER['PHP_AUTH_USER'];
12 $password = $_SERVER['PHP_AUTH_PW'];
13
14 $con = @ldap_connect($ldaphost);
15 if (!$con)
16 return "ldap_connect failed to ".$ldaphost;
17
18 //------------------ Look for user common name
19 $attributes = array('cn', 'mail');
20 $dn = 'ou=People,'.$baseDN;
21 $filter = '(uid='.$username.')';
22
23 $sr = @ldap_search($con, $dn, $filter, $attributes);
24 if (!$sr)
25 return "ldap_search failed for dn=".$dn.": ".ldap_error($con);
26
27 $srData = @ldap_get_entries($con, $sr);
28 if ($srData["count"]==0)
29 return "No results returned by ldap_get_entries for dn=".$dn.".";
30
31 $email =$srData[0]['mail'][0];
32 $userCommonName=$srData[0]['cn'][0];
33 $userDN =$srData[0]['dn'];
34
35 //------------------ Authenticate user
36 if (!@ldap_bind($con, $userDN, $password))
37 return "ldap_bind failed: ".ldap_error($con);
38
39 //------------------ Check if the user is in FACT ldap group
40 $attributes= array("member");
41 $filter= '(objectClass=*)';
42
43 // Get all members of the group.
44 $sr = @ldap_read($con, $groupDN, $filter, $attributes);
45 if (!$sr)
46 return "ldap_read failed for dn=".$groupDN.": ".ldap_error($con);
47
48 // retrieve the corresponding data
49 $srData = @ldap_get_entries($con, $sr);
50 if ($srData["count"]==0)
51 return "No results returned by ldap_get_entries for dn=".$dn.".";
52
53 @ldap_unbind($con);
54
55 $found = false;
56 foreach ($srData[0]['member'] as $member)
57 if (strpos($member, "cn=".$userCommonName.",")===0)
58 return "";
59
60 return "Sorry, your credentials don't match!";
61}
62
63/*
64function ascii2entities($string)
65{
66 for ($i=128; $i<256; $i++)
67 {
68 $entity = htmlentities(chr($i), ENT_QUOTES, 'cp1252');
69 $temp = substr($entity, 0, 1);
70 $temp .= substr($entity, -1, 1);
71 $string = str_replace(chr($i), $temp!='&;'?'':$entity, $string);
72 }
73 return $string;
74}
75*/
76
77function ansi_decode($matches)
78{
79 static $colors =
80 array(
81 'black',
82 'maroon',
83 'green',
84 'olive',
85 'navy',
86 'purple',
87 'teal',
88 'silver',
89 'gray',
90 'red',
91 'lime',
92 'yellow',
93 'blue',
94 'fuchsia',
95 'aqua',
96 'white'
97 );
98
99 // Default styles.
100 static $styles =
101 array(
102 'background' => null, // Default is defined by the stylesheet.
103 'blink' => false,
104 'bold' => false,
105 'color' => null, // Default is defined by the stylesheet.
106 //'inverse' => false, // Cannot be expressed in terms of CSS!
107 'italic' => false, // Not supported by DarkOwl's ANSI.
108 'line-through' => false, // Not supported by DarkOwl's ANSI.
109 'underline' => false,
110 );
111
112 static $css = '';
113
114 // Copy the previous styles.
115 $newstyles = $styles;
116 // Extract the codes from the escape sequences.
117 preg_match_all('/\d+/', $matches[0], $matches);
118
119 // Walk through the codes.
120 foreach ($matches[0] as $code)
121 {
122 switch ($code)
123 {
124 case '0':
125 // Reset all styles.
126 $newstyles['background'] = null;
127 $newstyles['blink'] = false;
128 $newstyles['bold'] = false;
129 $newstyles['color'] = null;
130 // $newstyles['inverse'] = false;
131 $newstyles['italic'] = false;
132 $newstyles['line-through'] = false;
133 $newstyles['underline'] = false;
134 break;
135
136 case '1':
137 // Set the bold style.
138 $newstyles['bold'] = true;
139 break;
140
141 case '3':
142 // Set the italic style.
143 $newstyles['italic'] = true;
144 break;
145
146 case '4':
147 case '21': // Actually double underline, but CSS doesn't support that yet.
148 // Set the underline style.
149 $newstyles['underline'] = true;
150 break;
151
152 case '5':
153 case '6': // Actually rapid blinking, but CSS doesn't support that.
154 // Set the blink style.
155 $newstyles['blink'] = true;
156 break;
157
158// case '7':
159// // Set the inverse style.
160// $newstyles['inverse'] = true;
161// break;
162
163 case '9':
164 // Set the line-through style.
165 $newstyles['line-through'] = true;
166 break;
167
168 case '2': // Previously incorrectly interpreted by Pueblo/UE as cancel bold, now still supported for backward compatibility.
169 case '22':
170 // Reset the bold style.
171 $newstyles['bold'] = false;
172 break;
173
174 case '23':
175 // Reset the italic style.
176 $newstyles['italic'] = false;
177 break;
178
179 case '24':
180 // Reset the underline style.
181 $newstyles['underline'] = false;
182 break;
183
184 case '25':
185 // Reset the blink style.
186 $newstyles['blink'] = false;
187 break;
188
189// case '27':
190// // Reset the inverse style.
191// $newstyles['inverse'] = false;
192// break;
193
194 case '29':
195 // Reset the line-through style.
196 $newstyles['line-through'] = false;
197 break;
198
199 case '30': case '31': case '32': case '33': case '34': case '35': case '36': case '37':
200 // Set the foreground color.
201 $newstyles['color'] = $code - 30;
202 break;
203
204 case '39':
205 // Reset the foreground color.
206 $newstyles['color'] = null;
207 break;
208
209 case '40': case '41': case '42': case '43': case '44': case '45': case '46': case '47':
210 // Set the background color.
211 $newstyles['background'] = $code - 40;
212 break;
213
214 case '49':
215 // Reset the background color.
216 $newstyles['background'] = null;
217 break;
218
219 default:
220 // Unsupported code; simply ignore.
221 break;
222 }
223 }
224
225 // Styles are effectively unchanged; return nothing.
226 if ($newstyles === $styles)
227 return '';
228
229 // Copy the new styles.
230 $styles = $newstyles;
231 // If there's a previous CSS in effect, close the <span>.
232 $html = $css ? '</span>' : '';
233 // Generate CSS.
234 $css = '';
235
236 // background-color property.
237 if (!is_null($styles['background']))
238 $css .= ($css ? ';' : '') . "background-color:{$colors[$styles['background']]}";
239
240 // text-decoration property.
241 if ($styles['blink'] || $styles['line-through'] || $styles['underline'])
242 {
243 $css .= ($css ? ';' : '') . 'text-decoration:';
244
245 if ($styles['blink'])
246 $css .= 'blink';
247
248 if ($styles['line-through'])
249 $css .= 'line-through';
250
251 if ($styles['underline'])
252 $css .= 'underline';
253 }
254
255 // font-weight property.
256 if ($styles['bold'] && is_null($styles['color']))
257 $css .= ($css ? ';' : '') . 'font-weight:bold';
258
259 // color property.
260 if (!is_null($styles['color']))
261 $css .= ($css ? ';' : '') . "color:{$colors[$styles['color'] | $styles['bold'] << 3]}";
262
263 // font-style property.
264 if ($styles['italic'])
265 $css .= ($css ? ';' : '') . 'font-style:italic';
266
267 // Generate and return the HTML.
268 if ($css)
269 $html .= "<span style=\"$css\">";
270
271 return $html;
272}
273
274function ansi2html($str)
275{
276 // Replace database strings
277 $str = preg_replace("/\ (([[:word:].-]+)(:[^ ]+)?(@))?([[:word:].-]+)(:([[:digit:]]+))?(\/([[:word:].-]+))/", " $2$4$5$8", $str);
278
279 // Replace special characters to their corresponding HTML entities
280 //$str = ascii2entities($str);
281 $str = htmlentities($str, ENT_NOQUOTES);
282
283 // Replace ANSI codes.
284 $str = preg_replace_callback('/(?:\e\[\d+(?:;\d+)*m)+/', 'ansi_decode', "$str\033[0m");
285
286 // Strip ASCII bell.
287 // $str = str_replace("\007", '', $str);
288
289 // Replace \n
290 // $str = str_replace("\n", "<br/>\n", $str);
291
292 // Return the parsed string.
293 return $str;
294}
295
296if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
297{
298 header('WWW-Authenticate: Basic realm="SmartFACT++"');
299 header('HTTP/1.0 401 Unauthorized');
300 return;
301}
302
303$rc = login();
304if ($rc!="")
305 return header('HTTP/1.0 401 '.$rc);
306
307$refresh = isset($_GET['refresh']) ? $_GET['refresh'] : -1;
308if ($refresh>0 && $refresh<60)
309 $refresh = 60;
310
311unset($_GET['refresh']);
312
313$name = empty($_GET['log']) ? "dimserver.log" : $_GET['log'].".log";
314$dir = empty($_GET['dir']) ? "FACT++" : $_GET['dir'];
315
316if (!strpos($name, "/")===false || !strpos($dir, "/")===false)
317 return header('HTTP/1.0 403 Access forbidden.');
318
319$filename = "/users/fact/".$dir."/".$name;
320
321$size = filesize($filename);
322if ($size>30000000) // 30MB
323 return header('HTTP/1.0 403 File too large.');
324
325// FIXME: Reading the file line by line avoids any danger that
326// something yields a problem if files grow too large
327$file = file($filename);
328if ($file===false)
329 return header('HTTP/1.0 403 Access forbidden.');
330
331$max = 10000;
332$pos = 500;
333$n = count($file);
334if ($n>$max)
335{
336 $file[$pos] = "\n<b>[...]</b>\n\n";
337 array_splice($file, $pos+1, $n-$max);
338}
339
340?>
341<!DOCTYPE HTML>
342<html>
343<head>
344<?php
345if ($refresh>0)
346 print("<meta http-equiv='refresh' content='".$refresh."'>\n");
347?>
348<meta charset="UTF-8">
349<title><?php print($dir." - ".$name);?></title>
350<script>
351function scroll(top)
352{
353 document.getElementById(top?'top':'bottom').scrollIntoView(top);
354}
355</script>
356</head>
357<body onload="setTimeout(function(){scroll(false);},1);">
358<span onclick="scroll(true);" style="cursor:pointer;padding:0 5px 4px 7px;position:fixed;top:0;right:0;text-decoration:underline;color:navy;background-color:#f0f0f0;">go to top &uarr;</span>
359<span onclick="scroll(false);" style="cursor:pointer;padding:0 5px 4px 7px;padding-top:0px;position:fixed;bottom:0;right:0;text-decoration:underline;color:navy;background-color:#f0f0f0;">go to bottom &darr;</span>
360<H2 id="top"><?php printf("%s - %s (%dkB)", $dir, $name, $size/1000);?></H2>
361
362<pre style="font-size:small;font-family:'Lucida Console',Monaco,monospace">
363<?php
364foreach ($file as $line)
365 print(ansi2html(substr($line, 0, -1))."\n");
366?>
367
368</pre>
369<span id="bottom"></span>
370</body>
371</html>
Note: See TracBrowser for help on using the repository browser.