| 1 | 1488 | DarkViper | <?php
 | 
      
        | 2 |  |  | /**
 | 
      
        | 3 |  |  |  * @category        WebsiteBaker
 | 
      
        | 4 |  |  |  * @package         WebsiteBaker_core
 | 
      
        | 5 |  |  |  * @author          Werner v.d.Decken
 | 
      
        | 6 |  |  |  * @copyright       WebsiteBaker.org e.V.
 | 
      
        | 7 |  |  |  * @link            http://websitebaker2.org
 | 
      
        | 8 |  |  |  * @license         http://www.gnu.org/licenses/gpl.html
 | 
      
        | 9 | 1808 | Luisehahne |  * @version         $Id$
 | 
      
        | 10 |  |  |  * @filesource		$HeadURL$
 | 
      
        | 11 | 1488 | DarkViper |  *
 | 
      
        | 12 |  |  |  * Global exception-handler
 | 
      
        | 13 |  |  |  * This module will activate a global exception handler to catch all thrown exceptions
 | 
      
        | 14 |  |  |  *
 | 
      
        | 15 |  |  |  */
 | 
      
        | 16 |  |  | /**
 | 
      
        | 17 |  |  |  * define several default exceptions directly to prevent from extra loading requests
 | 
      
        | 18 |  |  |  */
 | 
      
        | 19 |  |  | /**
 | 
      
        | 20 | 1808 | Luisehahne |  *
 | 
      
        | 21 | 1670 | darkviper |  */
 | 
      
        | 22 |  |  | 	class AppException extends Exception{
 | 
      
        | 23 |  |  | 		public function __toString() {
 | 
      
        | 24 |  |  | 			$file = str_replace(dirname(dirname(__FILE__)), '', $this->getFile());
 | 
      
        | 25 |  |  | 			if(DEBUG) {
 | 
      
        | 26 |  |  | 				$trace = $this->getTrace();
 | 
      
        | 27 |  |  | 				$result = 'Exception: "'.$this->getMessage().'" @ ';
 | 
      
        | 28 |  |  | 				if($trace[0]['class'] != '') {
 | 
      
        | 29 |  |  | 				  $result .= $trace[0]['class'].'->';
 | 
      
        | 30 |  |  | 				}
 | 
      
        | 31 |  |  | 				$result .= $trace[0]['function'].'(); in'.$file.'<br />'."\n";
 | 
      
        | 32 |  |  | 				if(mysql_errno()) {
 | 
      
        | 33 |  |  | 					$result .= mysql_errno().': '.mysql_error().'<br />'."\n";
 | 
      
        | 34 |  |  | 				}
 | 
      
        | 35 | 1690 | darkviper | 				$result .= '<pre>'."\n";
 | 
      
        | 36 |  |  | 				$result .= print_r($trace, true)."\n";
 | 
      
        | 37 |  |  | 				$result .= '</pre>'."\n";
 | 
      
        | 38 | 1670 | darkviper | 			}else {
 | 
      
        | 39 | 1893 | Luisehahne | 				$result = 'Exception: "'.$this->getMessage().'" >> Exception detected in: ['.$file.']<br />'."\n";
 | 
      
        | 40 | 1670 | darkviper | 			}
 | 
      
        | 41 |  |  | 			return $result;
 | 
      
        | 42 |  |  | 		}
 | 
      
        | 43 |  |  | 	}
 | 
      
        | 44 |  |  | /**
 | 
      
        | 45 | 1488 | DarkViper |  * define Exception to show error after accessing a forbidden file
 | 
      
        | 46 |  |  |  */
 | 
      
        | 47 |  |  | 	class IllegalFileException extends LogicException {
 | 
      
        | 48 |  |  | 		public function __toString() {
 | 
      
        | 49 | 1489 | DarkViper | 			$file = str_replace(dirname(dirname(__FILE__)), '', $this->getFile());
 | 
      
        | 50 | 1499 | DarkViper | 			$out  = '<div style="color: #ff0000; text-align: center;"><br />';
 | 
      
        | 51 |  |  | 			$out .= '<br /><br /><h1>Illegale file access</h1>';
 | 
      
        | 52 | 1488 | DarkViper | 			$out .= '<h2>'.$file.'</h2></div>';
 | 
      
        | 53 |  |  | 			return $out;
 | 
      
        | 54 |  |  | 		}
 | 
      
        | 55 |  |  | 	} // end of class
 | 
      
        | 56 | 1808 | Luisehahne | /**
 | 
      
        | 57 |  |  |  * define Exception to show error message
 | 
      
        | 58 |  |  |  */
 | 
      
        | 59 |  |  | 	class ErrorMsgException extends Exception {
 | 
      
        | 60 |  |  | 		public function __toString() {
 | 
      
        | 61 |  |  |             $out  = $this->getMessage();
 | 
      
        | 62 |  |  | 			return $out;
 | 
      
        | 63 |  |  | 		}
 | 
      
        | 64 |  |  | 	} // end of class
 | 
      
        | 65 | 1488 | DarkViper | 
 | 
      
        | 66 | 1680 | darkviper | /* -- several security exceptions ----------------------------------------------------- */
 | 
      
        | 67 |  |  | 	class SecurityException extends RuntimeException { 	}
 | 
      
        | 68 |  |  | 
 | 
      
        | 69 |  |  | 	class SecDirectoryTraversalException extends SecurityException {
 | 
      
        | 70 |  |  | 		public function __toString() {
 | 
      
        | 71 | 1690 | darkviper | 			$out  = 'possible directory traversal attack<br />'."\n";
 | 
      
        | 72 |  |  | 			$out .= '\''.$e->getMessage().'\'<br />'."\n";
 | 
      
        | 73 |  |  | 			return $out;
 | 
      
        | 74 | 1680 | darkviper | 		}
 | 
      
        | 75 |  |  | 	}
 | 
      
        | 76 |  |  | /* ------------------------------------------------------------------------------------ */
 | 
      
        | 77 | 1488 | DarkViper | /**
 | 
      
        | 78 |  |  |  *
 | 
      
        | 79 |  |  |  * @param Exception $e
 | 
      
        | 80 |  |  |  */
 | 
      
        | 81 |  |  | 	function globalExceptionHandler($e) {
 | 
      
        | 82 |  |  | 		// hide server internals from filename where the exception was thrown
 | 
      
        | 83 |  |  | 		$file = str_replace(dirname(dirname(__FILE__)), '', $e->getFile());
 | 
      
        | 84 |  |  | 		// select some exceptions for special handling
 | 
      
        | 85 | 1680 | darkviper | 		if ($e instanceof SecurityException) {
 | 
      
        | 86 |  |  | 			$out = 'Exception: "'.(string)$e.'" @ ';
 | 
      
        | 87 |  |  | 		    $trace = $e->getTrace();
 | 
      
        | 88 |  |  | 			if($trace[0]['class'] != '') {
 | 
      
        | 89 |  |  | 				$out .= $trace[0]['class'].'->';
 | 
      
        | 90 |  |  | 			}
 | 
      
        | 91 |  |  | 			$out .= $trace[0]['function'].'();<br />';
 | 
      
        | 92 |  |  | 			$out .= 'in "'.$file.'"'."\n";
 | 
      
        | 93 |  |  | 			echo $out;
 | 
      
        | 94 | 1690 | darkviper | 		}elseif ($e instanceof AppException) {
 | 
      
        | 95 |  |  | 			echo (string)$e;
 | 
      
        | 96 | 1680 | darkviper | 		}elseif ($e instanceof IllegalFileException) {
 | 
      
        | 97 | 1488 | DarkViper | 			$sResponse  = $_SERVER['SERVER_PROTOCOL'].' 403 Forbidden';
 | 
      
        | 98 |  |  | 			header($sResponse);
 | 
      
        | 99 |  |  | 			echo $e;
 | 
      
        | 100 | 1808 | Luisehahne | 		}elseif($e instanceof ErrorMsgException) {
 | 
      
        | 101 |  |  | 			echo (string)$e;
 | 
      
        | 102 | 1647 | darkviper | 		}elseif($e instanceof RuntimeException) {
 | 
      
        | 103 | 1680 | darkviper | 			$out  = 'There was a serious runtime error:'."\n";
 | 
      
        | 104 | 1647 | darkviper | 			$out .= $e->getMessage()."\n";
 | 
      
        | 105 |  |  | 			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
 | 
      
        | 106 |  |  | 			echo $out;
 | 
      
        | 107 | 1488 | DarkViper | 		}else {
 | 
      
        | 108 |  |  | 		// default exception handling
 | 
      
        | 109 |  |  | 			$out  = 'There was an unknown exception:'."\n";
 | 
      
        | 110 |  |  | 			$out .= $e->getMessage()."\n";
 | 
      
        | 111 |  |  | 			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
 | 
      
        | 112 |  |  | 			echo $out;
 | 
      
        | 113 |  |  | 		}
 | 
      
        | 114 |  |  | 	}
 | 
      
        | 115 |  |  | /**
 | 
      
        | 116 |  |  |  * now activate the new defined handler
 | 
      
        | 117 |  |  |  */
 | 
      
        | 118 |  |  | 	set_exception_handler('globalExceptionHandler');
 |