<?php 
include("./config.inc.php");
include(INCLUDEPATH . 'mgw.class.php');
include(INCLUDEPATH . 'userfunc.inc.php');
include(INCLUDEPATH . 'log.inc.php');

session_name($appconf["session_name"]);
session_start();

DEFINE("DEBUG", false);

if (!DEBUG and (!isset($_SESSION["MGW"]->userid) or !$_SESSION["MGW"]->userid)) die ("unauthorized");

if (!isset($_SESSION["MGW"]->settings["enable_logging"]) or !$_SESSION["MGW"]->settings["enable_logging"]) die("logging not enabled.");

$conn = connect_database();
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

/* get log id */
(isset($_GET["logid"]))?($id = (int)$_GET["logid"]):($id = 0);

/* get output mode */
(isset($_GET["output"]))?($output = addslashes($_GET["output"])):($output = "html");

if ($output=="file") {
    /* get log data */
    $sql = "SELECT stamp,level,module,line,file,message FROM mgw_log WHERE id=".(int)$id." ORDER BY sort";
    $res = $conn->Execute($sql);
    $logArr = array();
    while($row = $res->FetchRow()){
        if (!isset($logStamp)) $logStamp = $row["stamp"];
        
        $logArr[] = $row;
    }
    
    /* we'll be outputting plain text */
    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=log.txt");
    foreach ($logArr as $line) {
        echo(mgw_log_str_part($line,1));
    }
} else {
    /* get mode */
    (!isset($_GET["mode"]))?($mode = 0):($mode = (int)$_GET["mode"]);
    
    /* whole session, or just this page? */    
    (!isset($_GET["amount"]))?($amount = 0):($amount = (int)$_GET["amount"]);
    $header_txt = "MOREGROUPWARE LOG ";
    if ($amount==0) {
        $sql = "SELECT stamp,level,module,line,file,message FROM mgw_log WHERE id=".(int)$id." ORDER BY sort";
        $res = $conn->Execute($sql);
        $logArr = array();
        $row = $res->FetchRow();
        $logStamp = $row["stamp"];
        
        $header_txt .= "(log id: ".$id.")<br />Logged on ".date("H:i, d M, Y", $logStamp)."<br />\n";
    } else {
        $sql = "SELECT session,stamp FROM mgw_log WHERE id=".(int)$id." ORDER BY stamp";
        $res = $conn->Execute($sql);
        $row = $res->FetchRow();
        $mysession = $row["session"];
        
        $header_txt .= "(session id: ".$mysession.")<br />Logged on ".date("d M, Y", $row["stamp"])."<br />\n";
    }

    echo '
    <?xml version="1.0"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>MGW LOG</title>
    <style type="text/css">
    <!--
    body {
    	font-family : "Courier New", Courier, monospace;
    	font-size : 12px;
        text-decoration : none;
        color : black;
    }
    a {
    	font-family : "Courier New", Courier, monospace;
    	font-size : 11px;
        font-weight: normal;
        text-decoration : none;
    }
    hr {
        height: 1px;
    }
    p {
        font-weight: bold;
    }
    -->
    </style>
    </head>
    <body>';

    if (DEBUG) echo("<p aling=\"center\"><a href=\"http://validator.w3.org/check?uri=http://".$_SERVER["HTTP_HOST"]."".$_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"]."\" target=\"_blank\">Validate</a></p>"); 
    ?>
    <hr />
    <p align="center">
    <?php echo($header_txt); ?>
    <?php if ($mode==1) { ?>
    [<a href="<?php echo($_SERVER['PHP_SELF']."?logid=".$id."&amp;mode=0&amp;amount=".$amount); ?>">short</a>]
    <?php } else { ?>
    [<a href="<?php echo($_SERVER['PHP_SELF']."?logid=".$id."&amp;mode=1&amp;amount=".$amount); ?>">long</a>]
    <?php } ?>
    <?php if ($amount==0) { ?>
        [<a href="<?php echo($_SERVER['PHP_SELF']."?logid=".$id."&amp;mode=".$mode."&amp;amount=1"); ?>">entire session</a>]<br /><br />
    <?php } else { ?>
        [<a href="<?php echo($_SERVER['PHP_SELF']."?logid=".$id."&amp;mode=".$mode."&amp;amount=0"); ?>">last page only</a>]<br /><br />
    <?php } ?>
    </p>
    <hr />
    <?php
    if ($amount==0) {
        $sql = "SELECT stamp,level,module,line,file,message FROM mgw_log WHERE id=".(int)$id." ORDER BY sort";
        $res = $conn->Execute($sql);
        $logArr = array();
        while($row = $res->FetchRow()){
            if (!isset($logStamp)) $logStamp = $row["stamp"];
            
            $logArr[] = $row;
        }
        echo("<pre>"); 
        foreach ($logArr as $line) {
            echo(mgw_log_str_part($line,$mode));
        }
        echo("\n");
        echo("</pre>"); 
    } else {
        echo("<br />");
        $sql = "SELECT id,stamp,level,module,line,file,message FROM mgw_log WHERE session=".$conn->QMagic($mysession)." ORDER BY stamp,id,sort";
        $res = $conn->Execute($sql);
        $lastId = "";
        while($row = $res->FetchRow()){
            if (trim($row["id"])!=$lastId) {
                if ($lastId!="") echo("</pre>"); 
                echo("<b>log id ".$row["id"]." - ".date("H:i:s", $row["stamp"])."</b>");
                echo("<pre>"); 
                $lastId = $row["id"];
            }
            echo(mgw_log_str_part($row,$mode));
        }
    }
    ?>
    </body>
    </html>
    <?php
}

exit;

?>
