Make all exception classes extend Box_Exception

This makes all of them translatable, I also created a new option for Box_Exception to hide values from the stack trace for improved security.
This commit is contained in:
Belle Aerni committed 2023-01-26 15:28:03 -08:00
1 parent 226c4766d4
commit 5b232ce4dc
4 files changed
+58 -30

No files matched your search

+19 -12
View File
@@ -1,4 +1,5 @@
<?php
/**
* FOSSBilling
*
@@ -18,18 +19,20 @@ class Box_Exception extends Exception
* Creates a new translated exception.
*
* @param string error message
* @param array translation variables
* @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, hide them from the stack trace.
*/
public function __construct($message, array $variables = NULL, $code = 0)
public function __construct(string $message, array|null $variables = null, int $code = 0, bool $protected = false)
{
$config = include PATH_ROOT.'/config.php';
$debug = (isset($config['debug'])) ? $config['debug'] : false;
$logStack = (isset($config['log_stacktrace'])) ? $config['log_stacktrace'] : true;
$stackLength = (isset($config['stacktrace_length'])) ? $config['stacktrace_length'] : 25;
$config = include PATH_ROOT . '/config.php';
$debug = $config['debug'] ?? false;
$logStack = $config['log_stacktrace'] ?? true;
$stackLength = $config['stacktrace_length'] ?? 25;
if($debug && $logStack){
if ($debug && $logStack) {
error_log('An exception has been thrown. Stacktrace:');
error_log( $this->stackTrace($stackLength) );
error_log($this->stackTrace($stackLength, $protected));
}
// Translate the exception
@@ -39,12 +42,14 @@ class Box_Exception extends Exception
parent::__construct($message, $code);
}
/**
* Big thank you to jhurliman and jambroseclarke on Stack Overflow for this backtrace formatter.
* We have slightly modified it for our purposes
* https://stackoverflow.com/a/32365961
*/
private function stackTrace($Length = 25) {
private function stackTrace($Length = 25, $protected = false)
{
$stack = debug_backtrace($Length);
$output = '';
@@ -53,11 +58,13 @@ class Box_Exception extends Exception
$entry = $stack[$i];
$func = $entry['function'] . '(';
if(isset($entry['args'])){
if (isset($entry['args'])) {
$argsLen = count($entry['args']);
for ($j = 0; $j < $argsLen; $j++) {
$my_entry = $entry['args'][$j];
if (is_string($my_entry)) {
if ($protected) {
$func .= "***";
} else if (is_string($my_entry)) {
$func .= $my_entry;
}
if ($j < $argsLen - 1) $func .= ', ';
@@ -67,7 +74,7 @@ class Box_Exception extends Exception
$entry_file = 'NO_FILE';
if (array_key_exists('file', $entry)) {
$entry_file = str_replace(PATH_ROOT, '' , $entry['file']);
$entry_file = str_replace(PATH_ROOT, '', $entry['file']);
}
$entry_line = 'NO_LINE';
if (array_key_exists('line', $entry)) {
+11 -13
View File
@@ -12,20 +12,18 @@
* with this source code in the file LICENSE
*/
class Payment_Exception extends Exception
class Payment_Exception extends Box_Exception
{
/**
* Creates a new translated exception.
*
* @param string error message
* @param array translation variables
*/
public function __construct($message, array $variables = NULL, $code = 0)
/**
* Creates a new translated exception, using the Box_Exception class.
*
* @param string error message
* @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, disable stacktracing abilities.
*/
public function __construct(string $message, array $variables = NULL, int $code = 0)
{
// Set the message
$message = __trans($message, $variables);
// Pass the message to the parent
parent::__construct($message, $code);
parent:: __construct($message, $variables, $code, true);
}
}
+13 -2
View File
@@ -1,5 +1,16 @@
<?php
class Registrar_Exception extends Exception
class Registrar_Exception extends Box_Exception
{
/**
* Creates a new translated exception, using the Box_Exception class.
*
* @param string error message
* @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, disable stacktracing abilities.
*/
public function __construct(string $message, array $variables = NULL, int $code = 0)
{
parent::__construct($message, $variables, $code, true);
}
}
+15 -3
View File
@@ -1,4 +1,5 @@
<?php
/**
* FOSSBilling
*
@@ -12,7 +13,18 @@
* with this source code in the file LICENSE
*/
class Server_Exception extends Exception
class Server_Exception extends Box_Exception
{
}
/**
* Creates a new translated exception, using the Box_Exception class.
*
* @param string error message
* @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, disable stacktracing abilities.
*/
public function __construct(string $message, array $variables = NULL, int $code = 0)
{
parent::__construct($message, $variables, $code, true);
}
}