Ticket #212: 01212_basic_error_handler.diff

File 01212_basic_error_handler.diff, 3.1 kB (added by rho, 9 months ago)
  • a/lib/setup.php

    old new  
    176176if (!isset($CFG->debug)) { 
    177177    $CFG->debug = 7; 
    178178} 
    179 error_reporting($CFG->debug); 
     179 
     180// always log errors 
     181@ini_set('log_errors', '1'); 
     182@ini_set('error_log', $CFG->dataroot . 'errors.log'); 
     183// hide error of screen, handled by error handler function 
     184@ini_set('display_errors', '0'); 
     185// handle errors 
     186set_error_handler('elgg_error_handler'); 
    180187 
    181188/// File permissions on created directories in the $CFG->dataroot 
    182189 
     
    192199 
    193200if (!is_writable($CFG->dataroot)) { 
    194201    die("Your current dataroot directory, <strong>$CFG->dataroot</strong> is not writable by the webserver!"); 
    195 } 
    196  
    197 if ($CFG->debug > 0) { 
    198     @ini_set('log_errors', '1'); 
    199     @ini_set('error_log', $CFG->dataroot . 'errors.log'); 
    200 } else { 
    201     // hide errors on production systems 
    202     @ini_set('display_errors', '0'); 
    203 } 
    204  
    205 if ($CFG->debug > 7) { 
    206     @ini_set('display_errors', '1'); 
    207202} 
    208203 
    209204/// Set up session handling 
     
    514509    } 
    515510} 
    516511 
     512/** 
     513 * Basic error handler 
     514 */ 
     515function elgg_error_handler($errno, $errmsg, $errfile, $errline, $errcontext) { 
     516    global $CFG; 
     517 
     518    $date = date('Y-m-d H:i:s'); 
     519    $fatal = "Error: $errmsg (# $errno)"; 
     520    $file = "Error in line $errline of file $errfile"; 
     521    $script = "Script: {$_SERVER['PHP_SELF']}"; 
     522 
     523    switch ($errno) { 
     524        case E_USER_NOTICE: 
     525        case E_NOTICE: 
     526            if ($CFG->debug == 2047) { 
     527                $msg = "$date\n$fatal\n$file\n$script\n"; 
     528                error_log($msg); 
     529            } 
     530            break; 
     531        case E_USER_WARNING: 
     532        case E_WARNING: 
     533        case E_CORE_WARNING: 
     534        case E_COMPILE_WARNING: 
     535            //log errors if debug enabled 
     536            if ($CFG->debug >= 7) { 
     537                $msg = "$date\nWarning - $fatal\n$file\n$script\n"; 
     538                error_log($msg); 
     539            } 
     540            break; 
     541        case E_USER_ERROR: 
     542        case E_ERROR: 
     543        case E_PARSE: 
     544        case E_CORE_ERROR: 
     545        case E_COMPILE_ERROR: 
     546            $msg = "<em>$date</em>\n"; 
     547            $msg .= "<p><code>$fatal</code></p>\n"; 
     548            $msg .= "<p><code>$file</code></p>\n"; 
     549            $msg .= "<p><code>$script</code></p>\n"; 
     550 
     551            if ($CFG->debug > 0) { 
     552                echo "<p><em>Disable debug mode if you do not want to display errors on screen browser</em></p>\n"; 
     553                echo $msg; 
     554            } else { 
     555                echo "<h2>Our apologies, the system can't complete your request</h2>\n"; 
     556                echo "<p>The error has been reported to administrators</p>\n"; 
     557                echo "<p><em>{$CFG->sitename} team</em><br/>"; 
     558                echo "<a href=\"{$CFG->wwwroot}\">{$CFG->wwwroot}</a></p>"; 
     559 
     560            } 
     561            error_log(strip_tags($msg)); 
     562            // halt 
     563            die;  
     564            break; 
     565        default: 
     566            break; 
     567    } 
     568} 
     569 
    517570?>