<?php

include('global.php');

function TimeToHumanTime($Value)
{
  return(floor($Value / 3600 / 24).' days, '.date('H:i:s', $Value - 3600));
}

echo('<table width="100%"><tr><td width="10%" valign="top" style="">');
echo('<strong>Restart history:</strong><br>');

if(array_key_exists('Id', $_GET)) $Id = addslashes($_GET['Id']);
  else $Id = 0;
if(array_key_exists('Page', $_GET)) $Page = addslashes($_GET['Page']);
  else $Page = 0;

$DbResult = $Database->query('SELECT COUNT(*) FROM debug');
$DbRow = $DbResult->fetch_array();
$Total = $DbRow[0];

$DbResult = $Database->query('SELECT Id, Time FROM debug WHERE 1 ORDER BY Time DESC LIMIT '.($Page * $ItemPerPage).','.$ItemPerPage);
while($DbRow = $DbResult->fetch_array())
{
  if($DbRow['Id'] == $Id) echo('<strong>');
  echo('<a href="?Id='.$DbRow['Id'].'&amp;Page='.$Page.'&amp;Show=Backtrace">'.str_replace(" ", "&nbsp;", $DbRow['Time']).'</a>');
  if($DbRow['Id'] == $Id) echo('</strong>');
  echo('<br>');
}
echo(PagesList('?Page=', $Page, $Total, $ItemPerPage, 2));

echo('</td><td valign="top" width="90%">');

if($Id > 0)
{
  $DbResult = $Database->query('SELECT * FROM debug WHERE Id="'.$Id.'" ORDER BY Time');
  if($DbResult->num_rows > 0)
  {
    $DbRow = $DbResult->fetch_array();
    echo('<strong>Time:</strong> '.$DbRow['Time'].'<br>');
    echo('<strong>MaNGOS version:</strong> '.$DbRow['MangosVersion'].'<br>'); 
    echo('<strong>Database version:</strong> '.$DbRow['DbVersion'].'<br>'); 
    echo('<strong>Uptime:</strong> '.TimeToHumanTime($DbRow['Uptime']).'<br>'); 
    echo('<strong>MaxPlayerCount:</strong> '.$DbRow['MaxPlayerCount'].'<br><br>'); 
    echo('<a href="?Id='.$Id.'&amp;Page='.$Page.'&amp;Show=Backtrace">Backtrace</a>'.
      '  <a href="?Id='.$Id.'&amp;Page='.$Page.'&amp;Show=Log">Mangos log</a>'.
      '  <a href="?Id='.$Id.'&amp;Page='.$Page.'&amp;Show=Error">Console error log</a>'.
      '  <a href="?Id='.$Id.'&amp;Page='.$Page.'&amp;Show=DbErrors">Database error log</a>'.
      '  <a href="?Id='.$Id.'&amp;Page='.$Page.'&amp;Show=Configuration">Mangos configuration</a>'.
      '<hr>');
    if(array_key_exists('Show', $_GET))
    {
      $Show = addslashes($_GET['Show']);     
      switch($Show)
      {
        case 'Backtrace':
          $Content = htmlspecialchars($DbRow['Backtrace']);
          for($I = 1; $I < 8; $I++)
          {
            $Content = str_replace('Thread '.$I.' ', '<hr><strong id="'.$I.'">Thread '.$I.'</strong>', $Content);
            $Content = str_replace($I.' Thread ', '<a href="#'.$I.'"">'.$I.' Thread</a>', $Content);
          }
          echo('<strong>Backtrace:</strong> <br><pre>'.$Content.'</pre>');
          break;
        case 'Log': 
          echo('<strong>Console standard output log:</strong> <br><pre>'.htmlspecialchars($DbRow['Log']).'</pre>');
          break;
        case 'Error': 
          echo('<strong>Console error log:</strong> <br><pre>'.htmlspecialchars($DbRow['ErrorLog']).'</pre>');
          break;
        case 'DbErrors': 
          echo('<strong>Database error log:</strong> <br><pre>'.htmlspecialchars($DbRow['DbErrors']).'</pre>');
          break;
        case 'Configuration': 
          echo('<strong>Mangos configuration:</strong> <br><pre>'.htmlspecialchars($DbRow['Configuration']).'</pre>');
          break;
      }
    }
  }
}
  
echo('</td></tr></table>');

?>