<?php
/* whisper-strip.php - remove whispering from OpenRPG logs. */
$chat_regex = "^\\[";
$whisper_regex = "\\(whispering\\):|<i>whispering to ";

/* Strip out extra backslashes. */
$log_file = ereg_replace("\\\\(.)", "\\1", $_REQUEST["log"]);

/* Sanity check log file name. */
if( !ereg("^logs(/|/On the Great River II/|/The Dark Trade/|/nwc/|/DMed/|/played/|/TSG/|/Craig's/)log-[0-9]+-[0-9]+-[0-9]+\\.html$", $log_file) 
    || !is_readable($log_file) ) {
    echo "<p>No such log</p>";
    exit;
}

$title = filename_to_title($log_file);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<head>
  <title>D. Vrabel's/PnP Stuff/Logs/<?php echo $title ?></title>
  <link href="../global.css" rel="stylesheet" type="text/css">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<div class="header">
<a href="..">Contents</a> / <a href=".">PnP Stuff</a> / <a href="logs.php">Logs</a> / <?php echo $title ?>
</div>

<p>
<?php
$show = false;
$log = file($log_file);
foreach( $log as $line ) {
    if( ereg($chat_regex, $line) ) {
        if( ereg("GAME START", $line) ) {
            $show = true;
        }
        if ($show) {
            if( ereg($whisper_regex, $line) ) {
                continue;
            }
            echo $line;
        }
        if( ereg("GAME END", $line) ) {
            $show = false;
        }
    }
}
?>
</p>

<div class="footer">
<table>
  <tr>
    <td class="left">
      <a href="..">Contents</a> / <a href=".">PnP Stuff</a> / <a href="logs.php">Logs</a> / <?php echo $title ?>
    <td class="center"><a href="">Top</a></td>
    <td class="right"></td>
  </tr>
</table>
</div>
 
</body>
</html>

<?php
function filename_to_title($fn)
{
    list(,$dir, $fn) = split("/", $fn);

    $old_style_regex = "log-[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{2}\.html$";
    $new_style_regex = "log-[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}\.html$";
    
    if( ereg( $old_style_regex, $fn ) ) {
        $year = (int)substr( $fn, 10, 2 ) + 2000;
        $month = (int)substr( $fn, 7, 2 );
        $day = (int)substr( $fn, 4, 2 );
    } else if( ereg( $new_style_regex, $fn ) ) {
        $year = (int)substr( $fn, 4, 4 );
        $month = (int)substr( $fn, 9, 2 );
        $day = (int)substr( $fn, 12, 2 );
    } else {
        return "";
    }
    return $dir . " - " . date( "d F Y", mktime( 0,0,0, $month, $day, $year ));
}
?>
