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