Changeset 516


Ignore:
Timestamp:
Apr 9, 2013, 11:36:09 PM (11 years ago)
Author:
chronos
Message:
  • Upraveno: Obsluha chybových stavů přepracována na aplikační modul.
Location:
trunk
Files:
2 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Common/Global.php

    r513 r516  
    1212include_once(dirname(__FILE__).'/Version.php');
    1313include_once(dirname(__FILE__).'/Update.php');
     14include_once(dirname(__FILE__).'/VarDumper.php');
    1415include_once(dirname(__FILE__).'/Module.php');
    1516include_once(dirname(__FILE__).'/AppModule.php');
    1617include_once(dirname(__FILE__).'/Database.php');
    17 include_once(dirname(__FILE__).'/Error.php');
    1818include_once(dirname(__FILE__).'/Code.php');
    1919include_once(dirname(__FILE__).'/Mail.php');
     
    2828 
    2929// Application modules
     30include_once(dirname(__FILE__).'/../Modules/Error/Error.php');
    3031include_once(dirname(__FILE__).'/../Modules/File/File.php');
    3132include_once(dirname(__FILE__).'/../Modules/Meteostation/Meteostation.php');
     
    174175
    175176  $System = new System();
    176   $System->Config = $Config;
     177  $System->Config = &$Config;
    177178  $System->Database->Connect($Config['Database']['Host'], $Config['Database']['User'], $Config['Database']['Password'], $Config['Database']['Database']);
    178179  $System->Database->Prefix = $Config['Database']['Prefix'];
     
    190191  // Init old modules
    191192  $System->AddModule(new Log());
    192   $System->AddModule(new ErrorHandler());
    193   $System->Modules['ErrorHandler']->ShowErrors = $Config['Web']['ShowPHPError'];
    194   $System->Modules['ErrorHandler']->Init();
    195193  $System->AddModule(new User());
    196194  if(isset($_SERVER['REMOTE_ADDR'])) $System->Modules['User']->Check();
     
    200198  $System->Modules['Finance']->DirectoryId = $Config['Finance']['DirectoryId'];
    201199  $System->Modules['Finance']->LoadMonthParameters(0);
     200  $System->Modules['Log']->Database->LastQuery = 'ssd';
    202201  RegisterFormClasses($System->FormManager);
    203202 
     203 
    204204  // Register new modules
     205  $System->ModuleManager->RegisterModule(new ModuleError($System));
    205206  $System->ModuleManager->RegisterModule(new ModuleFile($System));
    206207  $System->ModuleManager->RegisterModule(new ModuleMeteostation($System));
  • trunk/Common/Version.php

    r515 r516  
    11<?php
    22
    3 $Revision = 515; // Subversion revision
     3$Revision = 516; // Subversion revision
    44$DatabaseRevision = 515;
    55$ReleaseTime = '2013-04-08';
  • trunk/Modules/Error/Error.php

    r511 r516  
    11<?php
    22
    3 class ErrorHandler extends Module
     3class ModuleError extends AppModule
    44{
    55  var $Encoding;
     
    77  var $UserErrors;
    88   
    9   function __construct()
     9  function __construct($System)
    1010  {
     11    parent::__construct($System);
     12    $this->Name = 'Error';
     13    $this->Version = '1.0';
     14    $this->Creator = 'Chronos';
     15    $this->License = 'GNU/GPLv3';
     16    $this->Description = 'Error logging and reporting';
     17    $this->Dependencies = array('Log');
     18
    1119    $this->Encoding = 'utf-8';
    1220    $this->ShowError = false;
     
    1422  }
    1523 
    16   function Init()
     24  function Install()
    1725  {
    18     set_error_handler(array($this, 'Handle'));
     26    parent::Install();
     27  }
     28
     29  function UnInstall()
     30  {
     31    parent::UnInstall();
     32  } 
     33 
     34  function Start()
     35  {
     36    parent::Start();
     37        $this->ShowError = $this->System->Config['Web']['ShowPHPError'];
     38    set_error_handler(array($this, 'ErrorHandler'));
     39    set_exception_handler(array($this, 'ExceptionHandler'));
    1940  }
    2041 
    21   function Handle($Number, $Message, $FileName, $LineNumber, $Variables)
     42  function Stop()
    2243  {
    23     $Date = date('Y-m-d H:i:s');                // časové razítko položky
     44        restore_error_handler();
     45        restore_exception_handler();
     46        parent::Stop();
     47  }
     48 
     49  function ErrorHandler($Number, $Message, $FileName, $LineNumber, $Variables)
     50  {
    2451    $ErrorType = array
    2552    (
     
    3966    if(($this->UserErrors & $Number))
    4067    {
    41       $Error = '# '.$Date."\n";
    4268      $Backtrace = debug_backtrace();
    4369      $Backtrace[0]['function'] = $Message;
     
    4571      $Backtrace[0]['file'] = $FileName;
    4672      $Backtrace[0]['line'] = $LineNumber;
    47       //$First = array_shift($Backtrace);
     73      $this->Report($Backtrace);
     74      if((E_ERROR | E_PARSE) & $Number) die();
     75    }
     76  }
     77 
     78  function ExceptionHandler(Exception $Exception)
     79  {
     80    $Backtrace = $Exception->getTrace();   
     81    array_unshift($Backtrace, array(
     82      'function' => $Exception->getMessage(),
     83      'file' => $Exception->getFile(),
     84      'line' => $Exception->getLine(),
     85    ));
     86    $this->Report($Backtrace);
     87    die();
     88  }
     89 
     90  function Report($Backtrace)
     91  {
     92    $Date = date('Y-m-d H:i:s');               
     93    $Error = '# '.$Date."\n";
     94    foreach($Backtrace as $Item)
     95    {
     96      if(!array_key_exists('line', $Item)) $Item['line'] = '';
     97      if(!array_key_exists('file', $Item)) $Item['file'] = '';
    4898   
    49       //array_unshift($Backtrace, $First);
    50       //array_shift($Backtrace);
    51       foreach($Backtrace as $Item)
    52       {
    53         $Error .= ' '.$Item['file'].'('.$Item['line'].")\t".$Item['function'];
    54         $Arguments = '';
    55         if(array_key_exists('args', $Item) and is_array($Item['args']))
     99      $Error .= ' '.$Item['file'].'('.$Item['line'].")\t".$Item['function'];
     100      $Arguments = '';
     101      if(array_key_exists('args', $Item) and is_array($Item['args']))
    56102        foreach($Item['args'] as $Item)
    57103        {
    58           if(is_object($Item)) ; 
     104          if(is_object($Item)) ;
    59105          else if(is_array($Item)) $Arguments .= "'".serialize($Item)."',";
    60106          else $Arguments .= "'".$Item."',";
    61107        }
    62108        if(strlen($Arguments) > 0) $Error .= '('.substr($Arguments, 0, -1).')';
    63         $Error .= "\n";     
    64       }
    65       $Error .= "\n";
     109        $Error .= "\n";
     110    }
     111    $Error .= "\n";
    66112     
    67       $this->System->Modules['Log']->NewRecord('Error', 'Log', $Error);   
     113    $this->System->Modules['Log']->NewRecord('Error', 'Log', $Error);
    68114   
    69       //if($Config['Web']['ErrorLogFile'] != '')
    70         // error_log($Error, 3, $Config['Web']['ErrorLogFile']);
    71             // Pošli mi zprávu (pokud je to kritická chyba)
    72          //mail($Config['Web']['AdminEmail'], $Config['Web']['Title'].' - Chybové hlášení', $Error);
    73           // Show error message
    74             if($this->ShowError == true)
    75           {
    76               echo('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>'."\n".
    77         '<meta http-equiv="Content-Language" content="cs">'."\n".
    78         '<meta http-equiv="Content-Type" content="text/html; charset='.$this->Encoding.'"></head><body>'."\n".
    79         'Došlo k vnitřní chybě!<br/> O chybě byl uvědoměn správce webu a chybu brzy odstraní.<br/><br/>');
    80             echo('<pre>'.$Error.'</pre><br/>');                 // V případě ladění chybu i zobraz
    81           echo('</body></html>');
    82             }
    83       if((E_ERROR | E_PARSE) & $Number) die();
    84     }
     115    //if($Config['Web']['ErrorLogFile'] != '')
     116    // error_log($Error, 3, $Config['Web']['ErrorLogFile']);
     117    // Pošli mi zprávu (pokud je to kritická chyba)
     118    //mail($Config['Web']['AdminEmail'], $Config['Web']['Title'].' - Chybové hlášení', $Error);
     119    // Show error message
     120    if($this->ShowError == true)
     121    {
     122      echo('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>'."\n".
     123          '<meta http-equiv="Content-Language" content="cs">'."\n".
     124          '<meta http-equiv="Content-Type" content="text/html; charset='.$this->Encoding.'"></head><body>'."\n".
     125          'Došlo k vnitřní chybě!<br/> O chybě byl uvědoměn správce webu a chybu brzy odstraní.<br/><br/>');
     126      echo('<pre>'.$Error.'</pre><br/>');                       // V případě ladění chybu i zobraz
     127      echo('</body></html>');
     128    }   
    85129  }
    86130}
Note: See TracChangeset for help on using the changeset viewer.