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://websitebaker.org
8
 * @license         http://www.gnu.org/licenses/gpl.html
9
 * @version         $Id: globalExceptionHandler.php 2 2017-07-02 15:14:29Z Manuela $
10
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/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
/**
18
 * define Exception to show error after accessing a forbidden file
19
 */
20
    class IllegalFileException extends LogicException {
21
        public function __toString() {
22
            $file = str_replace(dirname(dirname(__FILE__)), '', $this->getFile());
23
            $out  = '<div style="color: #ff0000; text-align: center;"><br />';
24
            $out .= '<br /><br /><h1>Illegale file access</h1>';
25
            $out .= '<h2>'.$file.'</h2></div>';
26
            return $out;
27
        }
28
    } // end of class
29
/**
30
 * define several default exceptions directly to prevent from extra loading requests
31
 */
32
/**
33
 *
34
 */
35
    class AppException extends Exception{
36
        public function __toString() {
37
            $file = str_replace(dirname(dirname(__FILE__)), '', $this->getFile());
38
            if (defined('DEBUG')&& DEBUG) {
39
                $trace = $this->getTrace();
40
                $result = 'Exception: "'.$this->getMessage().'" @ ';
41
                if($trace[0]['class'] != '') {
42
                  $result .= $trace[0]['class'].'->';
43
                }
44
                $result .= $trace[0]['function'].'(); in'.$file.'<br />'."\n";
45
                if($GLOBALS['database']->get_error()) {
46
                    $result .= $GLOBALS['database']->get_error().': '.$GLOBALS['database']->get_error().'<br />'."\n";
47
                }
48
                $result .= '<pre>'."\n";
49
                $result .= print_r($trace, true)."\n";
50
                $result .= '</pre>'."\n";
51
            }else {
52
                $result = 'Exception: "'.$this->getMessage().'" >> Exception detected in: ['.$file.']<br />'."\n";
53
            }
54
            return $result;
55
        }
56
    }
57

    
58
/**
59
 *
60
 * @param Exception $e
61
 */
62
    function globalExceptionHandler($e) {
63
        // hide server internals from filename where the exception was thrown
64
        $file = str_replace(dirname(dirname(__FILE__)), '', $e->getFile());
65
        // select some exceptions for special handling
66
        if ($e instanceof IllegalFileException) {
67
            $sResponse  = $_SERVER['SERVER_PROTOCOL'].' 403 Forbidden';
68
            header($sResponse);
69
            echo $e;
70
        } elseif ($e instanceof AppException) {
71
            echo (string)$e;
72
        } else {
73
        // default exception handling
74
            $out  = 'There was an uncatched exception'."\n";
75
            $out .= $e->getMessage()."\n";
76
            $out .= 'in line ('.$e->getLine().') of ('.$file.'):'."\n";
77
            echo nl2br($out);
78
        }
79
    }
80
/**
81
 * now activate the new defined handler
82
 */
83
    set_exception_handler('globalExceptionHandler');
(28-28/30)