phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file PoAEventHandler.php

Documentation is available at PoAEventHandler.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  * @package phpPoA2
  25.  */
  26.  
  27. /**
  28.  * Class to handle events inside the library.
  29.  */
  30. class PoAEventHandler {
  31.  
  32.     private $debug true;
  33.     private $local_site;
  34.     private $log;
  35.  
  36.     /**
  37.      * Main constructor.
  38.      */
  39.     public function __construct($site$log null$debug false{
  40.         $this->local_site $site;
  41.         $this->log $log;
  42.         $this->debug $debug;
  43.     }
  44.  
  45.     /**
  46.      * Set the logger.
  47.      * @param log The logger to use.
  48.      */
  49.     public function setLogger($log{
  50.         $this->log $log;
  51.     }
  52.  
  53.     /**
  54.      * Set debugging mode.
  55.      * @param debug A boolean value telling whether to make debugging active or not.
  56.      */
  57.     public function setDebug($debug{
  58.         $this->debug $debug;
  59.     }
  60.  
  61.     /**
  62.      * Finish execution and print an error message with the current backtrace.
  63.      */
  64.     public function abort($code$message{
  65.         if ($code (E_ERROR E_USER_ERROR)) {
  66.             header("Content-type: text/html; charset=utf-8\r\n");
  67.             echo "<html><body>\n";
  68.             echo "<h1>".msg("fatal-error")."</h1>\n";
  69.             echo "<p>".msg("error-desc")."</p>\n";
  70.             echo "<h4>".msg("error-message").":</h4>\n<div style=\"background: #ffcccc; padding: 10px; margin: 10px\"><tt>".$message."</tt></div>\n";
  71.             echo "<h4>".msg("session-id").":</h4>\n<div style=\"background: #cccccc; padding: 10px; margin: 10px\"><tt>#".$_COOKIE[$this->local_site.'_session']."</tt></div>\n";
  72.             echo "<h4>".msg("backtrace").":</h4>\n<div style=\"background: #cccccc; padding: 10px; margin: 10px\"><pre style=\"overflow: auto\">";
  73.             debug_print_backtrace();
  74.             echo "</tt></pre>\n";
  75.             echo "</body></html>";
  76.             die($code);
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Override __autoload standard function to allow loading classes dinamically.
  82.      */
  83.     function autoloadHandler($class_name{
  84.         // look for the class file in the path
  85.         $include_path get_include_path();
  86.         $include_path_tokens explode(':'$include_path);
  87.         foreach($include_path_tokens as $prefix){
  88.             $path $prefix '/' $class_name '.php';
  89.             if (file_exists($path)){
  90.                 include_once $path;
  91.                 if (class_exists($class_namefalse||
  92.                     interface_exists($class_namefalse)) {
  93.                     return true;
  94.                 }
  95.             }
  96.         }
  97.         trigger_error(msg('class-not-found'array($class_name))E_USER_WARNING);
  98.         return false;
  99.     }
  100.  
  101.     /**
  102.      * A generic exception handler that logs the exception message.
  103.      * @param exception The catched exception.
  104.      */
  105.     public function exceptionHandler($exception{
  106.         trigger_error($exceptionE_USER_ERROR);
  107.     }
  108.  
  109.     /**
  110.      * A generic error handler that logs the error message.
  111.      * @param code The error code.
  112.      * @param message The error message.
  113.      * @param file The file that triggered the error.
  114.      * @param line The line of the file where the error was triggered.
  115.      */
  116.     public function errorHandler($code$message$file$line{
  117.         // detect disabled error_reporting for the @ error control operator
  118.         if (!error_reporting()) return;
  119.  
  120.         // save original message
  121.         $original $message;
  122.  
  123.         // add the script:line that raised the error
  124.         if ($this->debug && isset($file&& isset($line)) {
  125.             $message "[".$this->local_site."] [".basename($file).":".$line."] ".$message;
  126.         }
  127.  
  128.         // add client IP
  129.         $ip $_SERVER['REMOTE_ADDR'];
  130.         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  131.             $ip trim(array_pop(explode(","$_SERVER['HTTP_X_FORWARDED_FOR'])));
  132.         }
  133.         $message "[client ".$ip."] ".$message;
  134.  
  135.         // add a description of the code
  136.         switch ($code{
  137.             case E_ERROR:
  138.             case E_USER_ERROR:
  139.             case E_PARSE:
  140.             case E_CORE_ERROR:
  141.             case E_COMPILE_ERROR:
  142.                 $message "[error] ".$message;
  143.                 break;
  144.             case E_WARNING:
  145.             case E_CORE_WARNING:
  146.             case E_USER_WARNING:
  147.             case E_COMPILE_WARNING:
  148.                 $message "[warning] ".$message;
  149.                 break;
  150.             default:
  151.                 $message "[info] ".$message;
  152.         }
  153.  
  154.         // finally add the session reference
  155.         $message "[#".$_COOKIE[$this->local_site."_session"]."] ".$message;
  156.  
  157.         // log the message
  158.         $this->log->write($message$code);
  159.  
  160.         // check if the error was critical and therefore we have to exit
  161.         $this->abort($code$original);
  162.  
  163.         // continue execution
  164.         return;
  165.     }
  166.  
  167. }
  168.  
  169. ?>

Documentation generated on Wed, 13 Oct 2010 15:06:24 +0200 by phpDocumentor 1.4.3