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: class.order.php 1487 2011-08-10 13:20:15Z DarkViper $
10
 * @filesource		$HeadURL: http://svn.websitebaker2.org/branches/2.8.x/wb/framework/class.order.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
/* -- several security exceptions ----------------------------------------------------- */
58
	class SecurityException extends RuntimeException { 	}
59

    
60
	class SecDirectoryTraversalException extends SecurityException {
61
		public function __toString() {
62
			$out  = 'possible directory traversal attack<br />'."\n";
63
			$out .= '\''.$e->getMessage().'\'<br />'."\n";
64
			return $out;
65
		}
66
	}
67
/* ------------------------------------------------------------------------------------ */
68
/**
69
 *
70
 * @param Exception $e
71
 */
72
	function globalExceptionHandler($e) {
73
		// hide server internals from filename where the exception was thrown
74
		$file = str_replace(dirname(dirname(__FILE__)), '', $e->getFile());
75
		// select some exceptions for special handling
76
		if ($e instanceof SecurityException) {
77
			$out = 'Exception: "'.(string)$e.'" @ ';
78
		    $trace = $e->getTrace();
79
			if($trace[0]['class'] != '') {
80
				$out .= $trace[0]['class'].'->';
81
			}
82
			$out .= $trace[0]['function'].'();<br />';
83
			$out .= 'in "'.$file.'"'."\n";
84
			echo $out;
85
		}elseif ($e instanceof AppException) {
86
			echo (string)$e;
87
		}elseif ($e instanceof IllegalFileException) {
88
			$sResponse  = $_SERVER['SERVER_PROTOCOL'].' 403 Forbidden';
89
			header($sResponse);
90
			echo $e;
91
		}elseif($e instanceof RuntimeException) {
92
			$out  = 'There was a serious runtime error:'."\n";
93
			$out .= $e->getMessage()."\n";
94
			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
95
			echo $out;
96
		}else {
97
		// default exception handling
98
			$out  = 'There was an unknown exception:'."\n";
99
			$out .= $e->getMessage()."\n";
100
			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
101
			echo $out;
102
		}
103
	}
104
/**
105
 * now activate the new defined handler
106
 */
107
	set_exception_handler('globalExceptionHandler');
(21-21/25)