<?php

class ModuleLog extends AppModule
{
	var $Excludes;
	
  function __construct($System)
  {
    parent::__construct($System);
    $this->Name = 'Log';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPL';
    $this->Description = 'Log various application events';
    $this->Dependencies = array('Error', 'News');
    
    $this->Excludes = array();
  }
  
  function Start()
  {
  	$this->System->RegisterPage('log.php', 'PageLog');
  	$this->System->ModuleManager->Modules['Error']->OnError[] = array($this, 'DoAddItem');
  	$this->System->ModuleManager->Modules['News']->RegisterRSS(array('Title' => T('Logs'), 
  	  'Channel' => 'log', 'Callback' => array('PageLog', 'ShowRSS'), 'Permission' => LICENCE_ADMIN));
  }  
  
  function DoAddItem($Message)
  {
  	$this->WriteLog($Message, LOG_TYPE_ERROR);
  }

  function WriteLog($Text, $Type)
  {
  	if(!isset($_SERVER['REMOTE_ADDR'])) $IP = 'Konzole';
  	  else $IP = addslashes($_SERVER['REMOTE_ADDR']);
  	
  	if(isset($this->System->User) and !is_null($this->System->User->Id)) $UserId = $this->System->User->Id;
  	  else $UserId = 'NULL';
  	$Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
  		'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.$IP.'", "'.$_SERVER['REQUEST_URI'].'")';
  	$this->System->Database->query($Query);
  }
}

// Log types
define('LOG_TYPE_TRANSLATION', 1);
define('LOG_TYPE_DOWNLOAD', 2);
define('LOG_TYPE_USER', 3);
define('LOG_TYPE_MODERATOR', 4);
define('LOG_TYPE_ERROR', 10);
define('LOG_TYPE_IMPORT', 11);
define('LOG_TYPE_EXPORT', 12);
define('LOG_TYPE_CZWOW', 13);
define('LOG_TYPE_ADMINISTRATION', 14);
define('LOG_TYPE_PAGE_NOT_FOUND', 15);

// TODO: Change global function to module class local method  
function WriteLog($Text, $Type)
{
	global $System, $User;

	if(!isset($_SERVER['REMOTE_ADDR'])) $IP = 'Konzole';
	  else $IP = addslashes($_SERVER['REMOTE_ADDR']);

	if(isset($User) and !is_null($User->Id)) $UserId = $User->Id;
	  else $UserId = 'NULL';
	$Query = 'INSERT INTO `Log` ( `User` , `Type` , `Text` , `Date` , `IP`, `URL` ) '.
	  'VALUES ('.$UserId.', '.$Type.', "'.addslashes($Text).'", NOW(), "'.$IP.'", "'.$_SERVER['REQUEST_URI'].'")';
	$System->Database->query($Query);
}

class PageLog extends Page
{
  function ShowRSS()
  {
  	$this->RawPage = true;
  	$Output = '';
    $Items = array();
    if(array_key_exists('type', $_GET)) $Where = ' WHERE `Type` = "'.($_GET['type'] * 1).'"'; 
      else $Where = '';
    $sql = 'SELECT *, UNIX_TIMESTAMP(`Date`) AS `TimeCreate`, (SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName`, `Date` FROM `Log`'.
      $Where.' ORDER BY `Date` DESC LIMIT 100';
    $DbResult = $this->System->Database->query($sql);
    while($Line = $DbResult->fetch_assoc()) 
    {
      $DbResult2 = $this->System->Database->query('SELECT * FROM `LogType` WHERE `Id`='.$Line['Type']);
      $LogType = $DbResult2->fetch_assoc();
      
      if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
      $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
    
      $Items[] = array
      (
        'Title' => $LogType['Name'].' ('.$Line['UserName'].', '.$Line['IP'].')',
        'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/log.php'),
        'Description' => $LogType['Name'].': '.$Line['Text'].' ('.$Line['UserName'].
          ', '.$Line['IP'].', '.HumanDate($Line['Date']).')',
        'Time' => $Line['TimeCreate'],
      );
    } 

    $Output .= GenerateRSS(array
    (
      'Title' => $this->System->Config['Web']['Title'].' - '.T('Logs'),
      'Link' => 'http://'.$this->System->Config['Web']['Host'].$this->System->Link('/'),
      'Description' => $this->System->Config['Web']['Description'],
      'WebmasterEmail' => $this->System->Config['Web']['AdminEmail'],
      'Items' => $Items,
    ));
    return($Output);
  }
  
  function Show()
  {
		if(array_key_exists('a', $_POST)) $Action = $_POST['a'];
		  else if(array_key_exists('a', $_GET)) $Action = $_GET['a'];
		  else $Action = '';		
		if($Action == 'delerrlog') $Output = $this->DeleteErrorLog();
		else $Output = $this->ShowList();
		return($Output);
  }
  
  function ShowList()
  {
    global $TranslationTree;
    
    $this->Title = T('System log');
    $Output = '';
    if(array_key_exists('type', $_GET)) $_SESSION['type'] = $_GET['type'] * 1; 
    else if(!array_key_exists('type', $_SESSION)) $_SESSION['type'] = '';

    if(array_key_exists('group', $_GET)) $_SESSION['group'] = $_GET['group'];

    if($_SESSION['type'] != '') $WhereType = ' `Type`='.$_SESSION['type']; 
      else $WhereType = '1=1';
    
    $RSSChannels = array(
    	array('Title' => 'Záznamy změn', 'Channel' => 'log&amp;type='.$_SESSION['type'])
    );
  
    if($this->System->User->Licence(LICENCE_MODERATOR))
    {
      $Output = '<strong>Filtr: </strong>'.
        '<span style="color:black"><a href="log.php?type=" title="Bez filtrování">Všechny</a></span> ';
      $DbResult = $this->System->Database->query('SELECT * FROM `LogType`');
      while($LogType = $DbResult->fetch_assoc())
      {
        $Output .= '<a href="log.php?type='.$LogType['Id'].'" style="color:'.$LogType['Color'].'" title="'.$LogType['Name'].'">'.$LogType['Name'].'</a> ';
      }
      // echo ' Formát: datum: text zprávy (uživatel, IP)<br /><br />';
     $Output .= '<br /><br />';

    if(array_key_exists('type', $_SESSION)) $Where = ' WHERE '.$WhereType; 
    else
    {
      if(array_key_exists('group', $_SESSION)) $Where = ' WHERE `Text` LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';
        else $Where = '';
    } 
    //if(($Where != '') and (array_key_exists('group', $_SESSION))) $Where .= ' AND text LIKE "%'.$TranslationTree[$_SESSION['group']]['Name'].'%"';

    $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` '.$Where);
    $DbRow = $DbResult->fetch_row();
    $PageList = GetPageList($DbRow[0]);    
  
    $Output .= $PageList['Output'];
  
    $TableColumns = array(
      array('Name' => 'Date', 'Title' => 'Čas'), 
      array('Name' => 'LogName', 'Title' => 'Typ'), 
  	  array('Name' => 'Text', 'Title' => 'Text'), 
      array('Name' => 'UserName', 'Title' => 'Uživatel'), 
      array('Name' => 'IP', 'Title' => 'Adresa'), 
      array('Name' => 'URL', 'Title' => 'URL'),
    );
    $Order = GetOrderTableHeader($TableColumns, 'date', 1);
    $Output .= '<table width="98%" class="BaseTable">'.
      $Order['Output'];
  
    $sql = 'SELECT *, `LogType`.`Color` AS `LogColor`, `LogType`.`Name` AS `LogName`, '.
      '(SELECT `User`.`Name` FROM `User` WHERE `User`.`ID` = `Log`.`User`) AS `UserName` FROM `Log` LEFT JOIN `LogType` ON `LogType`.`Id`=`Log`.`Type` '.$Where.$Order['SQL'].$PageList['SQLLimit'];
    $DbResult = $this->System->Database->query($sql);
    while($Line = $DbResult->fetch_assoc()) 
    {
  	  if($Line['Type'] == LOG_TYPE_ERROR) $Line['Text'] = htmlspecialchars($Line['Text']);
      $Line['Text'] = str_replace("\n", '<br>', $Line['Text']);
  	  $Output .= '<tr><td>'.$Line['Date'].'</td>'.
      '<td>'.$Line['LogName'].'</td>'.
      '<td><span style="color: '.$Line['LogColor'].'">'.$Line['Text'].'</span></td>'.
      '<td><a href="'.$this->System->Link('/user.php?user='.$Line['User']).'">'.$Line['UserName'].'</a></td>'.
      '<td>'.$Line['IP'].'</td>'.
  	  '<td>'.$Line['URL'].'</td></tr>';
    }
    $Output .= '</table>'.
      $PageList['Output'];
      if($this->System->User->Licence(LICENCE_ADMIN))
      {
        $Output .= '<div>Vymazat: <a href="'.$this->System->Link('/log.php?a=delerrlog&amp;type='.LOG_TYPE_ERROR).'">Chybové záznamy</a> '.
          '<a href="'.$this->System->Link('/log.php?a=delerrlog&amp;type='.LOG_TYPE_PAGE_NOT_FOUND).'">Neznámé stránky</a></div>';
      }     
    } else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
  
    return($Output);  
  }
  
  function DeleteErrorLog()
  {
  	if($this->System->User->Licence(LICENCE_ADMIN) and 
  	(($_GET['type'] == LOG_TYPE_ERROR) or ($_GET['type'] == LOG_TYPE_PAGE_NOT_FOUND)))
  	{
  		$DbResult = $this->System->Database->select('LogType', '*', 'Id='.$_GET['type']);
  		$LogType = $DbResult->fetch_assoc();
  	  $DbResult = $this->System->Database->query('SELECT COUNT(*) FROM `Log` WHERE `Type`='.$_GET['type']);
  	  $DbRow = $DbResult->fetch_row();
  	  $this->System->Database->query('DELETE FROM `Log` WHERE `Type`='.$_GET['type']);
  	  $this->System->ModuleManager->Modules['Log']->WriteLog('Vymazáno záznamů z '.$LogType['Description'].'.', LOG_TYPE_ADMINISTRATION);
  	  $Output = ShowMessage('Smazáno všech '.$DbRow[0].' záznamů z '.$LogType['Description'].'.');
  	  $Output .= $this->ShowList();
  	  return($Output);
  	} else $Output .= ShowMessage(T('Access denied'), MESSAGE_CRITICAL);
  }
}
