Location: PHPKode > projects > TEMS -Time and Expense Management System > tems/tesbean/ErrorHandler.php
<?php

/* * *******************************************************************************
 * TES is a Time and Expense Management program developed by
 * Initechs, LLC. Copyright (C) 2009 - 2010 Initechs LLC.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY INITECHS, INITECHS DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 *
 * You can contact Initechs headquarters at 1841 Piedmont Road, Suite 301,
 * Marietta, GA, USA. or at email address hide@address.com
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display od the "Initechs" logo.
 * If the display of the logo is not reasonably feasible for technical reasons,
 * the Appropriate Legal Notices must display the words "Powered by Initechs".

 * ****************************************************************************** */

error_reporting(E_ALL ^ (E_NOTICE | E_DEPRECATED));  // Sets which PHP errors are reported

// error_reporting(E_ERROR | E_WARNING | E_PARSE);  // Sets which PHP errors are reported

set_error_handler('ierrorHandler', E_ALL);

function ierrorHandler($errno, $errMessage, $errFile, $errLine, $errContext) {
    $errorType = array(
        E_ERROR => 'ERROR',
        E_WARNING => 'WARNING',
        E_PARSE => 'PARSING ERROR',
        E_NOTICE => 'NOTICE',
        E_CORE_ERROR => 'CORE ERROR',
        E_CORE_WARNING => 'CORE WARNING',
        E_COMPILE_ERROR => 'COMPILE ERROR',
        E_COMPILE_WARNING => 'COMPILE WARNING',
        E_USER_ERROR => 'USER ERROR',
        E_USER_WARNING => 'USER WARNING',
        E_USER_NOTICE => 'USER NOTICE',
        E_STRICT => 'STRICT NOTICE',
        E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',
        E_DEPRECATED => 'DEPRECATED',
        E_USER_DEPRECATED => 'USER DEPRECATED'
    );

    if (!(error_reporting() & $errno)) { // This error code is not included in error_reporting
        return;
    }

//    ob_clean();
//    var_dump(get_defined_vars());

    $errReport = "";
    $errReport .= ierrorOnScreenReport($errorType[$errno], $errno, $errMessage, $errFile, $errLine, $errContext);

    ierrorRecordInFile($errorType[$errno], $errno, $errMessage, $errFile, $errLine, $errContext);

    switch ($errno) {
        case E_ERROR:
        case E_CORE_ERROR:
        case E_COMPILE_ERROR:
        case E_USER_ERROR:
        case E_PARSE:
             echo $errReport;
             exit(1);
             break;

        case E_WARNING:
        case E_CORE_WARNING:
        case E_COMPILE_WARNING:
        case E_USER_WARNING:
             $_SESSION['error']['program_error'] = "$errReport";
             break;

        case E_NOTICE:
        case E_USER_NOTICE:
             $_SESSION['error']['program_error'] = "$errReport";
             break;

        default:
            echo $errReport;
            exit(1);
            break;
    }

    return true; // Don't execute PHP internal error handler
}

function ierrorOnScreenReport($errType, $errno, $errMessage, $errFile, $errLine, $errContext) {

    $errReport = "";
    $errReport .= "\n<table>";
    $errReport .= "\n<tr> <td colspan=2>Oops, an unexpected error has occurred.
                    Contact at <a href='mailto:hide@address.com'>hide@address.com</a>.
                    Sorry for the inconvenience!</td> </tr>";
    $errReport .= "\n<tr> <td colspan=2><h3>Technical Information:</h3></td> </tr>";
    $errReport .= "\n<tr> <td>Error Type:</td><td>$errType (Error Type Code = $errno)</td> </tr>";
    $errReport .= "\n<tr> <td>Error:</td><td>$errMessage</td> </tr>";
    $errReport .= "\n<tr> <td>File:</td><td>$errFile</td> </tr>";
    $errReport .= "\n<tr> <td>Line Number:</td><td>$errLine</td> </tr>";
    $errReport .= "\n<tr> <td>Error Context:</td><td>$errContext</td> </tr>";
    $errReport .= "\n</table>";

    return $errReport;
}

function ierrorRecordInFile($errType, $errno, $errMessage, $errFile, $errLine, $errContext) {
    global $basedir;

    $message = "\n" . date("Y-m-d h:m:s") ." $errType (Error Type Code = $errno)\tFile: $errFile (Line $errLine), Message: $errMessage";
    error_log($message, 3, "$basedir/log/logs.txt");
}

?>
Return current item: TEMS -Time and Expense Management System