live error handlers updated.

main
Robert 3 weeks ago
parent 578dce4034
commit 92043c38c7
  1. 12
      src/Bootstrap.php
  2. 38
      src/Framework/ErrorHandler.php
  3. 6
      src/Framework/Middleware/ErrorMiddleware.php
  4. 92
      src/Framework/SiteHelper.php

@ -12,6 +12,7 @@ use IOcornerstone\Psr4AutoloaderClass;
use IOcornerstone\Framework\{ use IOcornerstone\Framework\{
Registry as Reg, Registry as Reg,
Console, Console,
Configure,
CliDefaults, CliDefaults,
ErrorHandler, ErrorHandler,
APICacheAge, APICacheAge,
@ -62,6 +63,8 @@ $myErrorHandler->register();
Reg::set('loader', $loader); Reg::set('loader', $loader);
Reg::set('di', new DI()); // Initialize our Dependency Injector Reg::set('di', new DI()); // Initialize our Dependency Injector
Reg::set('container', new AutowireContainer()); Reg::set('container', new AutowireContainer());
Reg::set('error_handler', $myErrorHandler);
Reg::set('debug', $debug);
Console::setupConsoleVars(); // Copy CLI Args into $_GET Console::setupConsoleVars(); // Copy CLI Args into $_GET
@ -73,10 +76,17 @@ function isLive(): bool
{ {
if (Configure::has('IOcornerstone', 'live')) { if (Configure::has('IOcornerstone', 'live')) {
$live = Configure::get('IOcornerstone', 'live'); $live = Configure::get('IOcornerstone', 'live');
}
if ($live === null) { if ($live === null) {
$live = true; $live = true;
} }
$debugger = ($live) ? false : true;
Reg::get('error_handler')->resetDebugger($debugger);
} else {
echo "Warning: LIVE not Set in Config!!!";
$live = Reg::get('debug') ? false : true;
}
return (bool) $live; return (bool) $live;
} }

@ -11,6 +11,7 @@ use Throwable;
final class ErrorHandler final class ErrorHandler
{ {
private bool $hasError = false; private bool $hasError = false;
private string $myErr = "An internal error occurred. Please try again later.";
public function __construct( public function __construct(
private bool $debug = false, private bool $debug = false,
@ -18,7 +19,18 @@ final class ErrorHandler
define('WORD_WRAP_CHRS', 80); // Letters before Line Wrap on Errors define('WORD_WRAP_CHRS', 80); // Letters before Line Wrap on Errors
} }
public function register(): void public function setMyErr(string $em): void
{
$this->myErr = $em;
}
public function resetDebugger(bool $debug)
{
$this->debug = $debug;
$this->setErrors();
}
private function setErrors(): void
{ {
if ($this->debug) { if ($this->debug) {
error_reporting(E_ALL); error_reporting(E_ALL);
@ -37,7 +49,11 @@ final class ErrorHandler
// } // }
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED); error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
} }
}
public function register(): void
{
$this->setErrors();
set_error_handler([$this, 'handleError']); set_error_handler([$this, 'handleError']);
set_exception_handler([$this, 'handleException']); set_exception_handler([$this, 'handleException']);
register_shutdown_function([$this, 'handleShutdown']); register_shutdown_function([$this, 'handleShutdown']);
@ -309,6 +325,22 @@ final class ErrorHandler
return $msg; return $msg;
} }
public function renderProdMessage(): string {
if (Console::isConsole()) {
return $this->myErr;
}
if ($this->isJsonRequest()) {
$this->setJsonHeaders();
return json_encode([
'error' => [
'message' => 'Internal Server Error'
]
]);
}
return "<h1 style=\"color: red;\">" . $this->myErr . "</h1>";
}
private function renderDebug(Throwable $e): void private function renderDebug(Throwable $e): void
{ {
echo "<h1>Uncaught " . $this->getErrorType($e) . "</h1>"; echo "<h1>Uncaught " . $this->getErrorType($e) . "</h1>";
@ -317,7 +349,7 @@ final class ErrorHandler
private function renderProductionConsole(): void private function renderProductionConsole(): void
{ {
echo "An internal error occurred. Please try again later."; echo $this->myErr;
} }
/** /**
@ -325,7 +357,7 @@ final class ErrorHandler
*/ */
private function renderProduction(): void private function renderProduction(): void
{ {
echo "<h1 style=\"color: red;\">An internal error occurred. Please try again later.</h1>"; echo "<h1 style=\"color: red;\">" . $this->myErr . "</h1>";
} }
private function setLoggerByLevel(Throwable $e): void private function setLoggerByLevel(Throwable $e): void

@ -12,6 +12,7 @@ use IOcornerstone\Framework\Http\{
Response, Response,
Stream Stream
}; };
use IOcornerstone\Framework\Registry as Reg;
final class ErrorMiddleware implements MiddlewareInterface final class ErrorMiddleware implements MiddlewareInterface
{ {
@ -44,6 +45,11 @@ final class ErrorMiddleware implements MiddlewareInterface
private function formatException(\Throwable $e): string private function formatException(\Throwable $e): string
{ {
if (isLive()) {
return Reg::get('error_handler')->renderProdMessage();
}
return sprintf( return sprintf(
"%s\n\n%s", "%s\n\n%s",
$e->getMessage(), $e->getMessage(),

@ -0,0 +1,92 @@
<?php
declare(strict_types = 1);
/**
* @author Robert Strutts
* @copyright (c) 2026, Robert Strutts
* @license MIT
*/
namespace IOcornerstone\Framework;
use IOcornerstone\Framework\Security;
/**
* Description of SiteHelper
* Checks if IP is allowed for LIVE DEBUGGING
*
* @author Robert Strutts
*/
final class SiteHelper
{
private static $local_site_domains = ['localhost'];
private static $Private_IPs_allowed = ['127.0.0.1', '::1'];
private static $Public_IPs_allowed = [];
public static function setLocalSiteDomains(string|array $domain_name): void {
if (is_array($domain_name)) {
foreach($domain_name as $domain) {
self::$local_site_domains[] = $domain;
}
} elseif (is_string($domain_name)) {
self::$local_site_domains[] = $domain_name;
}
}
public static function setAllowedPrivateIPs(string|array $IP_addresses): void {
if (is_array($IP_addresses)) {
foreach($IP_addresses as $IP) {
$s_ip = Security::getValidIp($IP);
if ($s_ip === false) {
continue;
}
self::$Private_IPs_allowed[] = $IP;
}
} elseif (is_string($IP_addresses)) {
$s_ip = Security::getValidIp($IP);
if ($s_ip === false) {
return;
}
self::$Private_IPs_allowed[] = $IP_addresses;
}
}
public static function setAllowedPublicIPs(string|array $IP_addresses): void {
if (is_array($IP_addresses)) {
foreach($IP_addresses as $IP) {
$s_ip = Security::getValidPublicIp($IP);
if ($s_ip === false) {
continue;
}
self::$Public_IPs_allowed[] = $s_ip;
}
} elseif (is_string($IP_addresses)) {
$s_ip = Security::getValidPublicIp($IP);
if ($s_ip === false) {
return;
}
self::$Public_IPs_allowed[] = $IP_addresses;
}
}
public static function isServerName_A_PrivateDomain(): bool {
$white_list = array_merge(self::$local_site_domains, self::$Private_IPs_allowed);
return (Security::isServerNameOnDomainList($white_list));
}
public static function remoteNotAllowedForceLive(): bool {
return (! self::is_allowed());
}
public static function is_allowed(): bool {
$remote_ip = Security::getClientIpAddress();
if (in_array($remote_ip, self::$Public_IPs_allowed)) {
return true;
}
if (self::isServerName_A_PrivateDomain() && in_array($remote_ip, self::$Private_IPs_allowed)) {
return true;
}
return false;
}
}
Loading…
Cancel
Save