Project

General

Profile

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
 * @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 1670 darkviper
 *
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
			}else {
36
				$result = 'Exception: "'.$this->getMessage().'" in ['.$file.']<br />'."\n";
37
			}
38
			return $result;
39
		}
40
	}
41
/**
42 1488 DarkViper
 * define Exception to show error after accessing a forbidden file
43
 */
44
	class IllegalFileException extends LogicException {
45
		public function __toString() {
46 1489 DarkViper
			$file = str_replace(dirname(dirname(__FILE__)), '', $this->getFile());
47 1499 DarkViper
			$out  = '<div style="color: #ff0000; text-align: center;"><br />';
48
			$out .= '<br /><br /><h1>Illegale file access</h1>';
49 1488 DarkViper
			$out .= '<h2>'.$file.'</h2></div>';
50
			return $out;
51
		}
52
	} // end of class
53
54
/**
55
 *
56
 * @param Exception $e
57
 */
58
	function globalExceptionHandler($e) {
59
		// hide server internals from filename where the exception was thrown
60
		$file = str_replace(dirname(dirname(__FILE__)), '', $e->getFile());
61
		// select some exceptions for special handling
62
		if ($e instanceof IllegalFileException) {
63
			$sResponse  = $_SERVER['SERVER_PROTOCOL'].' 403 Forbidden';
64
			header($sResponse);
65
			echo $e;
66 1647 darkviper
		}elseif($e instanceof RuntimeException) {
67
			$out  ='There was a serious runtime error:'."\n";
68
			$out .= $e->getMessage()."\n";
69
			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
70
			echo $out;
71 1488 DarkViper
		}else {
72
		// default exception handling
73
			$out  = 'There was an unknown exception:'."\n";
74
			$out .= $e->getMessage()."\n";
75
			$out .= 'in line ('.$e->getLine().') of ('.$file.')'."\n";
76
			echo $out;
77
		}
78
	}
79
/**
80
 * now activate the new defined handler
81
 */
82
	set_exception_handler('globalExceptionHandler');