<commit_before><?php
/*
 * error.php:
 * Error handling apparatus for PHP programs.
 * 
 * Copyright (c) 2005 UK Citizens Online Democracy. All rights reserved.
 * Email: <EMAIL>; WWW: http://www.mysociety.org/
 *
 * $Id: error.php,v 1.8 2005-04-28 17:57:56 francis Exp $
 * 
 */

/* Make sure register globals is turned off */
if (ini_get("register_globals")) {
    print "Turn off register_globals in php.ini";
    exit;
}

/*
 * PHP error handling is, you will not be surprised to hear, completely
 * amateurish. It doesn't have exceptions and it doesn't have nonlocal jumps
 * either, so we can't emulate exceptions. (Exception: PHP 5 has exceptions,
 * but we're not using that.)
 * 
 * There is the notion of an "error handler", which is a function called when
 * an "error" occurs (generated either by PHP or raised by code via the
 * trigger_error function. Note that trigger_error and the error handler
 * functions are not a control flow mechanism -- the default behaviour is for
 * the error handler to do its stuff, and then return control to the calling
 * code. Observe that this is exactly like handling the error, except that it
 * lacks the key concepts of "error" and "handling".
 *
 * We adapt the error handling apparatus as follows:
 * 
 *   - Install a default error handler which calls an error logging handler,
 *     and an error display handler, and then aborts.
 *
 *   - The default error logging handler takes steps to dump the error in
 *     the proper server error log (PHP makes this quite hard, as you will
 *     see below); the default error display handler does nothing.
 * 
 *   - The user may override either handler using our own functions.
 */


/*
 * Implementation.
 */

/* err ERROR
 * Report the given ERROR and abort. */
function err($err) {
    /* We can't just call trigger_error, because that will always report this
     * function as the location where the error occured. So use debug_backtrace
     * to construct the relevant information. */
    $a = debug_backtrace(); /* now $a[1], if present, is the caller */
    $i = 0;
    if (array_key_exists(1, $a))
        $i = 1;
    err_global_handler(E_USER_ERROR, $err, $a[$i]['file'], $a[$i]['line'],
                        /* XXX We can't obtain the calling context AFAIK. */
                        null);
    exit(1); /* NOTREACHED */
}

/* err_log_webserver NUMBER STRING FILE LINE CONTEXT
 * Log the error with the given parameters, trying to arrange that it reach the
 * appropriate web server error log. */
function err_log_webserver($num, $str, $file, $line, $context) {
#print "<pre>";
#print_r($context);
#print "</pre>";

    /* Apache (and perhaps other webservers) logs errors preceded by a tag
     * giving the time and "severity" of the error. The time is in the format
     * "[%a %b %d %H:%M:%S %Y]", and the "severity" is one of "[error]",
     * "[warning]" or "[notice]". */

    $prefix = '';
    
    /* Time. */
/*
    $prefix .= strftime('[%a %b %d %H:%M:%S %Y]') . " ";
*/
    /* Severity. */
/*
    if ($num & (E_WARNING | E_CORE_WARNING | E_USER_WARNING))
        $prefix .= '[warning]';
    else if ($num & (E_NOTICE | E_USER_NOTICE))<commit_msg>All tests passed.  Pip pip!<commit_after><?php
/*
 * error.php:
 * Error handling apparatus for PHP programs.
 * 
 * Copyright (c) 2005 UK Citizens Online Democracy. All rights reserved.
 * Email: <EMAIL>; WWW: http://www.mysociety.org/
 *
 * $Id: error.php,v 1.9 2005-05-26 02:19:05 francis Exp $
 * 
 */

# Tip of the Day:  This code grabs a backtrace into a string, and 
# writes it to the Apache error log.  print_r can only do this
# in PHP 4.3.0 and above.
#       error_log(print_r(debug_backtrace(), TRUE));

/* Make sure register globals is turned off */
if (ini_get("register_globals")) {
    print "Turn off register_globals in php.ini";
    exit;
}

/*
 * PHP error handling is, you will not be surprised to hear, completely
 * amateurish. It doesn't have exceptions and it doesn't have nonlocal jumps
 * either, so we can't emulate exceptions. (Exception: PHP 5 has exceptions,
 * but we're not using that.)
 * 
 * There is the notion of an "error handler", which is a function called when
 * an "error" occurs (generated either by PHP or raised by code via the
 * trigger_error function. Note that trigger_error and the error handler
 * functions are not a control flow mechanism -- the default behaviour is for
 * the error handler to do its stuff, and then return control to the calling
 * code. Observe that this is exactly like handling the error, except that it
 * lacks the key concepts of "error" and "handling".
 *
 * We adapt the error handling apparatus as follows:
 * 
 *   - Install a default error handler which calls an error logging handler,
 *     and an error display handler, and then aborts.
 *
 *   - The default error logging handler takes steps to dump the error in
 *     the proper server error log (PHP makes this quite hard, as you will
 *     see below); the default error display handler does nothing.
 * 
 *   - The user may override either handler using our own functions.
 */


/*
 * Implementation.
 */

/* err ERROR
 * Report the given ERROR and abort. */
function err($err) {
    /* We can't just call trigger_error, because that will always report this
     * function as the location where the error occured. So use debug_backtrace
     * to construct the relevant information. */
    $a = debug_backtrace(); /* now $a[1], if present, is the caller */
    $i = 0;
    if (array_key_exists(1, $a))
        $i = 1;
    err_global_handler(E_USER_ERROR, $err, $a[$i]['file'], $a[$i]['line'],
                        /* XXX We can't obtain the calling context AFAIK. */
                        null);
    exit(1); /* NOTREACHED */
}

/* err_log_webserver NUMBER STRING FILE LINE CONTEXT
 * Log the error with the given parameters, trying to arrange that it reach the
 * appropriate web server error log. */
function err_log_webserver($num, $str, $file, $line, $context) {
#print "<pre>";
#print_r($context);
#error_log(print_r($context, TRUE));
#print "</pre>";

    /* Apache (and perhaps other webservers) logs errors preceded by a tag
     * giving the time and "severity" of the error. The time is in the format
     * "[%a %b %d %H:%M:%S %Y]", and the "severity" is one of "[error]",
     * "[warning]" or "[notice]". */

    $prefix = '';
    
    /* Time. */
/*
    $prefix .= strftime('[%a %b %d %H:%M:%S %Y]') . " ";
*/
    /* Severity. */
/*
    if ($num & (E_WARNING | E_CORE_WARNING | E_USER_WARNING))
        $prefix .= '[warning]';
    else if ($num & (E_NOTICE | E_USER_NOTICE))
{'fraction_non_alphanumeric': 0.11579743008314437, 'fraction_numerical': 0.008314436885865457, 'mean_word_length': 3.031687995124924, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587027', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1917, 'n_words': 845}