Classes

File core/error/ExceptionHandler.class.php

File core/error/ExceptionHandler.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 
<?php
/**
 * @package     Core
 * @subpackage  Error
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2015 11 10
 * @since       PHPBoost 3.0 - 2009 12 08
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class ExceptionHandler
{
    /**
     * @var Exception
     */
    private $exception;

    /**
     * The user function needs to accept two parameters: the error code, and a string
     * describing the error.
     * @param Exception $exception contains the level of the error raised, as an integer
     * @return bool always true because we don't want the php default error handler to process the
     * error again
     */
    public function handle($exception)
    {
        $this->exception = $exception;
        $this->clean_output_buffer();
        $this->log();
        $this->display();
        $this->destroy_app();
    }

    private function clean_output_buffer()
    {
        AppContext::get_response()->clean_output();
    }

    private function log()
    {
        ErrorHandler::add_error_in_log($this->exception->getMessage(), Debug::get_stacktrace_as_string(0, $this->exception), E_USER_ERROR);
    }

    private function display()
    {
        if (Debug::is_debug_mode_enabled())
        {
            $this->raw_display();
        }
        else
        {
            $controller = $this->prepare_controller();
            $this->send_response($controller);
        }
    }

    private function destroy_app()
    {
        Environment::destroy();
        exit;
    }

    private function raw_display()
    {
        if (Debug::is_debug_mode_enabled())
        {
            Debug::fatal($this->exception);
        }
        else
        {
            echo ErrorHandler::FATAL_MESSAGE;
        }
    }

    private function prepare_controller()
    {
        $title = LangLoader::get_message('error', 'status-messages-common');

        if ($this->exception !== null && Debug::is_debug_mode_enabled())
        {
            $message = TextHelper::htmlspecialchars($this->exception->getMessage()) . '<br /><br /><i>' .
            $this->exception->getFile() . ':' . $this->exception->getLine() .
            '</i><div class="spacer"></div>' .
            Debug::get_stacktrace_as_string(0, $this->exception);
            $title .= ' ' . $this->exception->getCode();
        }
        else
        {
            $message = TextHelper::htmlspecialchars(LangLoader::get_message('process.error', 'status-messages-common'));
        }

        $controller = new UserErrorController($title, $message, UserErrorController::FATAL);
        return $controller;
    }

    private function send_response(Controller $controller)
    {
        try
        {
            $this->integrated_display($controller);
        }
        catch (Exception $exception)
        {
            $this->clean_output_buffer();
            $this->raw_display();
        }
    }

    private function integrated_display(Controller $controller)
    {
        $request = AppContext::get_request();
        $response = $controller->execute($request);
        $response->send();
    }
}
?>