1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-27 12:56:08 +01:00
MadelineProto/libpy2php/strict_mode.php

139 lines
4.5 KiB
PHP
Raw Normal View History

2016-06-23 23:51:08 +02:00
<?php
/**
* This will initialize strict mode. It is safe to be called multiple times per process
* eg in the event that a 3rd party lib overrides an error or exception handler.
*
* It is called in this file; the parent php file(s) should use require_once and do
* not need to make any call.
*/
2016-07-14 15:15:50 +02:00
function init_strict_mode()
{
2016-06-23 23:51:08 +02:00
// these are safe to call multiple times per process without dups.
2016-07-14 15:15:50 +02:00
error_reporting(E_ALL | E_STRICT);
2016-06-23 23:51:08 +02:00
restore_strict_error_handler();
restore_strict_exception_handler();
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
// register_shutdown_function should only be called once per process to avoid dups.
static $called = false;
2016-07-14 15:15:50 +02:00
if (!$called) {
register_shutdown_function('shutdown_handler');
2016-06-23 23:51:08 +02:00
$called = true;
}
}
/**
* This function restores the error handler if it should get overridden
* eg by a 3rd party lib. Any error handlers that were registered after
* ours are removed.
*/
2016-07-14 15:15:50 +02:00
function restore_strict_error_handler()
{
$e_handler_name = function () {
2016-06-23 23:51:08 +02:00
$name = set_error_handler('restore_strict_error_handler'); // will never be used.
restore_error_handler();
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $name;
};
2016-07-14 15:15:50 +02:00
while (!in_array($e_handler_name(), ['_global_error_handler', null])) {
2016-06-23 23:51:08 +02:00
restore_error_handler();
}
2016-07-14 15:15:50 +02:00
if (!$e_handler_name()) {
set_error_handler('_global_error_handler');
2016-06-23 23:51:08 +02:00
}
}
/**
* This function restores the exception handler if it should get overridden
* eg by a 3rd party lib. Any error handlers that were registered after
* ours are removed.
*/
2016-07-14 15:15:50 +02:00
function restore_strict_exception_handler()
{
$exc_handler_name = function () {
2016-06-23 23:51:08 +02:00
$name = set_exception_handler('restore_strict_exception_handler'); // will never be used.
restore_exception_handler();
2016-07-14 15:15:50 +02:00
2016-06-23 23:51:08 +02:00
return $name;
};
2016-07-14 15:15:50 +02:00
while (!in_array($exc_handler_name(), ['_global_exception_handler', null])) {
2016-06-23 23:51:08 +02:00
restore_exception_handler();
}
2016-07-14 15:15:50 +02:00
if (!$exc_handler_name()) {
set_exception_handler('_global_exception_handler');
2016-06-23 23:51:08 +02:00
}
}
/***
* This error handler callback will be called for every type of PHP notice/warning/error.
2016-07-14 15:15:50 +02:00
*
2016-06-23 23:51:08 +02:00
* We aspire to write solid code. everything is an exception, even minor warnings.
*
* However, we allow the @operator in the code to override.
*/
2016-07-14 15:15:50 +02:00
function _global_error_handler($errno, $errstr, $errfile, $errline)
{
2016-06-23 23:51:08 +02:00
/* from php.net
* error_reporting() settings will have no effect and your error handler will
* be called regardless - however you are still able to read the current value of
* error_reporting and act appropriately. Of particular note is that this value will
* be 0 if the statement that caused the error was prepended by the @ error-control operator.
*/
2016-07-14 15:15:50 +02:00
if (!error_reporting()) {
2016-06-23 23:51:08 +02:00
return;
}
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
/***
* This exception handler callback will be called for any exceptions that the application code does not catch.
*/
2016-07-14 15:15:50 +02:00
function _global_exception_handler($e)
{
$msg = sprintf("\nUncaught Exception. code: %s, message: %s\n%s : %s\n\nStack Trace:\n%s\n", $e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
while (($e = $e->getPrevious())) {
$msg .= sprintf("\nPrevious Exception. code: %s, message: %s\n%s : %s\n\nStack Trace:\n%s\n", $e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
2016-06-23 23:51:08 +02:00
}
echo $msg;
// error_log( $msg );
2016-07-14 15:15:50 +02:00
strict_mode_mail_admin('Uncaught exception!', $msg);
2016-06-23 23:51:08 +02:00
echo "\n\nNow exiting. Please report this problem to the software author\n\n";
exit(1);
}
/**
2016-07-14 15:15:50 +02:00
* This shutdown handler callback prints a message and sends email on any PHP fatal error.
2016-06-23 23:51:08 +02:00
*/
2016-07-14 15:15:50 +02:00
function shutdown_handler()
{
$error = error_get_last();
2016-06-23 23:51:08 +02:00
2016-07-14 15:15:50 +02:00
$ignore = E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE | E_STRICT | E_DEPRECATED | E_USER_DEPRECATED;
if ($error && ($error['type'] & $ignore) == 0) {
2016-06-23 23:51:08 +02:00
// error keys: type, file, line, message
2016-07-14 15:15:50 +02:00
$msg = "Ouch! Encountered PHP Fatal Error. Shutting down.\n".print_r($error, true);
echo $msg;
strict_mode_mail_admin('PHP Fatal Error!', $msg);
}
2016-06-23 23:51:08 +02:00
}
/**
2016-07-14 15:15:50 +02:00
* email admin if defined.
2016-06-23 23:51:08 +02:00
*/
2016-07-14 15:15:50 +02:00
function strict_mode_mail_admin($subject, $msg)
{
$subject = sprintf('[%s] [%s] %s [pid: %s]', gethostname(), basename($_SERVER['PHP_SELF']), $subject, getmypid());
if (defined('ALERTS_MAIL_TO')) {
mail(ALERTS_MAIL_TO, $subject, $msg);
} else {
2016-06-23 23:51:08 +02:00
echo "\nWARNING: ALERTS_MAIL_TO not defined in environment. alert not sent with subject: $subject\n";
}
}