<?php

include_once('global_function.php');

function EmptyErrorHandler($Number, $Message, $Filename, $LineNumber, $Variables)
{
}

function CustomErrorHandler($Number, $Message, $Filename, $LineNumber, $Variables)
{
  global $Config;
  
  set_error_handler('EmptyErrorHandler');

  $Date = date('Y-m-d H:i:s');    // časové razítko položky
  $ErrorType = array
  (
    1 => 'Error',
    2 => 'Warning',
    4 => 'Parsing Error',
    8 => 'Notice',
    16 => 'Core Error',
    32 => 'Core Warning',
    64 => 'Compile Error',
    128 => 'Compile Warning',
    256 => 'User Error',
    512 => 'User Warning',
    1024 => 'User Notice'
  );
  $UserErrors = E_ALL;  //E_ERROR | E_WARNING | E_PARSE;
  
  if(($UserErrors & $Number))
  {
    $Error = '# '.$Date.' : '.$Message.' on line '.$LineNumber."\n";
    $Backtrace = debug_backtrace();
    $Backtrace[0]['function'] = '';
    $Backtrace[0]['args'] = '';
    //$First = array_shift($Backtrace);
    //print_r($First);
    
    //array_unshift($Backtrace, $First);
    //array_shift($Backtrace);
    //print_r($Backtrace);
    foreach($Backtrace as $Item)
    {
      //print_r($Item);
      if(!array_key_exists('line', $Item)) $Item['line'] = '';
      if(!array_key_exists('file', $Item)) $Item['file'] = '';
      if(!array_key_exists('function', $Item)) $Item['function'] = '';
      $Error .= ' '.$Item['file'].'('.$Item['line'].")\t".$Item['function'];
      $Arguments = '';
      if(array_key_exists('args', $Item) and is_array($Item['args']))
        foreach($Item['args'] as $Item)
        {
          if(is_array($Item)) $Arguments .= "'".serialize($Item)."',";
          else $Arguments .= "'".serialize($Item)."',";
        }
      if(strlen($Arguments) > 0) $Error .= '('.substr($Arguments, 0, -1).')';
      $Error .= "\n";
     
    }
    $Error .= "\n";
    //if($Config['Web']['ErrorLogFile'] != '')
      //error_log($Error, 3, $Config['Web']['ErrorLogFile']);
    // Pošli mi zprávu (pokud je to kritická chyba)
      //mail($Config['Web']['AdminEmail'], $Config['Web']['Title'].' - Chybové hlášení', $Error);
    // Show error message
    if($Config['Web']['ShowPHPError'] == true)
    {
      echo('<?xml version="1.0" encoding="utf-8"?>'."\n".
      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'.
      '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">'.
      '<head>'.
      '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />'.
      '</head><body>'.    
      'Došlo k vnitřní chybě!<br /> O chybě byl uvědoměn správce webu a chybu brzy odstraní.<br /><br />');
      echo('<pre>'.$Error.'</pre><br />');      // V případě ladění chybu i zobraz
      echo('</body></html>');
    }
    WriteLog($Error, LOG_TYPE_ERROR);
    if((E_ERROR | E_PARSE) & $Number) die(); 
  }
  set_error_handler('CustomErrorHandler');
}

set_error_handler('CustomErrorHandler');

?>
