Project

General

Profile

1
<?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
 * @version         $Id: globalExceptionHandler.php 1808 2012-11-07 11:05:52Z Luisehahne $
10
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/globalExceptionHandler.php $
11
 *
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
 *
21
 */
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
				$result .= '<pre>'."\n";
36
				$result .= print_r($trace, true)."\n";
37
				$result .= '</pre>'."\n";
38
			}else {
39
				$result = 'Exception: "'.$this->getMessage().'" in ['.$file.']<br />'."\n";
40
			}
41
			return $result;
42
		}
43
	}
44
/**
45
 * define Exception to show error after accessing a forbidden file
46
 */
47
	class IllegalFileException extends LogicException {
48
		public function __toString() {
49
			$file = str_replace(dirname(dirname(__FILE__)), '', $this->getFile());
50
			$out  = '<div style="color: #ff0000; text-align: center;"><br />';
51
			$out .= '<br /><br /><h1>Illegale file access</h1>';
52
			$out .= '<h2>'.$file.'</h2></div>';
53
			return $out;
54
		}
55
	} // end of class
56
/**
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

    
66
/* -- several security exceptions ----------------------------------------------------- */
67
	class SecurityException extends RuntimeException { 	}
68

    
69
	class SecDirectoryTraversalException extends SecurityException {
70
		public function __toString() {
71
			$out  = 'possible directory traversal attack<br />'."\n";
72
			$out .= '\''.$e->getMessage().'\'<br />'."\n";
73
			return $out;
74
		}
75
	}
76
/* ------------------------------------------------------------------------------------ */
77
/**
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
		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
		}elseif ($e instanceof AppException) {
95
			echo (string)$e;
96
		}elseif ($e instanceof IllegalFileException) {
97
			$sResponse  = $_SERVER['SERVER_PROTOCOL'].' 403 Forbidden';
98
			header($sResponse);
99
			echo $e;
100
		}elseif($e instanceof ErrorMsgException) {
101
			echo (string)$e;
102
		}elseif($e instanceof RuntimeException) {
103
			$out  = 'There was a serious runtime error:'."\n";
104
			$out .= $e->getMessage()."\n";
105
			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
106
			echo $out;
107
		}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');
(21-21/25)