Example #1
0
/**
 * Custom exception handler with backtrace
 * 
 * @param exception $exception Thrown exception
 * 
 * @return void
 */
function exceptionHandler($exception)
{
    global $dPconfig;
    $time = date("Y-m-d H:i:s");
    // User information
    $user_id = null;
    $user_view = "";
    if (class_exists("CAppUI", false) && CAppUI::$user) {
        $user = CAppUI::$user;
        if ($user->_id) {
            $user_id = $user->_id;
            $user_view = $user->_view;
        }
    }
    // Server IP
    $server_ip = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : null;
    $file = mbRelativePath($exception->getFile());
    $line = $exception->getLine();
    $type = "exception";
    $text = $exception->getMessage();
    // Stacktrace
    $contexts = $exception->getTrace();
    foreach ($contexts as &$ctx) {
        unset($ctx['args']);
    }
    // Might noy be ready at the time error is thrown
    $session = isset($_SESSION) ? $_SESSION : array();
    unset($session['AppUI']);
    unset($session['dPcompteRendu']['templateManager']);
    $_all_params = array("GET" => $_GET, "POST" => $_POST, "SESSION" => $session);
    filterInput($_all_params);
    // CApp might not be ready yet as of early error handling
    $request_uid = null;
    if (class_exists("CApp", false)) {
        $request_uid = CApp::getRequestUID();
        CApp::$performance[CError::$_categories["exception"]]++;
    }
    $build_output = ini_get("display_errors");
    $save_to_file = false;
    $data = array("stacktrace" => $contexts, "param_GET" => $_all_params["GET"], "param_POST" => $_all_params["POST"], "session_data" => $_all_params["SESSION"]);
    if (@$dPconfig["error_logs_in_db"] && class_exists("CErrorLog")) {
        try {
            CErrorLog::insert($user_id, $server_ip, $time, $request_uid, $type, $text, $file, $line, $data);
        } catch (Exception $e) {
            $build_output = true;
            $save_to_file = true;
        }
    } else {
        $build_output = true;
        $save_to_file = true;
    }
    if ($build_output) {
        $hash = md5(serialize($contexts));
        $html_class = "big-warning";
        $log = "\n\n<div class='{$html_class}' title='{$hash}'>";
        if ($user_id) {
            $log .= "\n<strong>User: </strong>{$user_view} ({$user_id})";
        }
        $file = CError::openInIDE($file, $line);
        $log .= <<<HTML
  <strong>Time: </strong>{$time}
  <strong>Type: </strong>{$type}
  <strong>Text: </strong>{$text}
  <strong>File: </strong>{$file}
  <strong>Line: </strong>{$line}
HTML;
        foreach ($_all_params as $_type => $_params) {
            $log .= print_infos($_all_params[$_type], $_type);
        }
        foreach ($contexts as $context) {
            $function = isset($context["class"]) ? $context["class"] . ":" : "";
            $function .= $context["function"] . "()";
            $log .= "\n<strong>Function: </strong> {$function}";
            if (isset($context["file"])) {
                $log .= "\n<strong>File: </strong>" . CError::openInIDE($context["file"], isset($context["line"]) ? $context["line"] : null);
            }
            if (isset($context["line"])) {
                $log .= "\n<strong>Line: </strong>" . $context["line"];
            }
            $log .= "<br />";
        }
        $log .= "</div>";
        if ($save_to_file) {
            file_put_contents(LOG_PATH, $log, FILE_APPEND);
        }
        if (ini_get("display_errors")) {
            echo $log;
        }
    }
}
 protected function _get_template_compile_dir($base, $source)
 {
     $realpath = realpath($this->template_dir . $source);
     $path = mbRelativePath($realpath);
     $path = explode("/", $path);
     // Remove "templates" subdir
     CMbArray::removeValue("templates", $path);
     $subdir = dirname(implode("/", $path));
     return "{$base}{$subdir}/";
 }