Skip to content

Commit

Permalink
Fixed some errors with PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Jan 20, 2022
1 parent d55c2d3 commit ace20bc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 38 deletions.
95 changes: 65 additions & 30 deletions public_html/lib-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -8060,34 +8060,18 @@ function COM_handleGeeklogError($errCode, $errStr)
}

/**
* Handle errors.
* This function will handle all PHP errors thrown at it, without exposing
* paths, and hopefully, providing much more information to Root Users than
* the default white error page.
* This function will call out to CUSTOM_handleError if it exists, but, be
* advised, only override this function with a very, very stable function. I'd
* suggest one that outputs some static, basic HTML.
* The PHP feature that allows us to do so is documented here:
* http://uk2.php.net/manual/en/function.set-error-handler.php
* Convert a combination of error numbers to a string
*
* @param int $errNo Error Number.
* @param string $errStr Error Message.
* @param string $errFile The file the error was raised in.
* @param int $errLine The line of the file that the error was raised at.
* @param array $errContext An array that points to the active symbol table at the point the error occurred.
* @param int $type Geeklog Error Type - 1 = PHP, 2 = Geeklog Specific
* @param int $errNo
* @return string
*/
function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errContext = array(), $type = 1)
function COM_convertErrorNumberToString($errNo)
{
global $_CONF, $_USER, $LANG01;

// Handle @ operator
if (error_reporting() == 0) {
return;
}
$errNo = (int) $errNo;
$temp = [];

// Table of error code and error type
$errorTypes = array(
$errorTypes = [
0 => 'E_SYNTAX', // Since Geeklog 2.2.0 - Handles syntax errors. Used when Developer Mode is on and PHP is set to show all errors
1 => 'E_ERROR',
2 => 'E_WARNING',
Expand All @@ -8104,7 +8088,51 @@ function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errConte
4096 => 'E_RECOVERABLE_ERROR', // Since PHP-5.2.0
8192 => 'E_DEPRECATED', // Since PHP-5.3.0
16384 => 'E_USER_DEPRECATED', // Since PHP-5.3.0
);
];

if ($errNo === 0) {
return $errorTypes[0];
}

foreach ($errorTypes as $key => $value) {
if ($key > 0) {
if (($errNo & $key) === $key) {
$temp[] = $value;
}
}
}

return implode(', ', $temp);
}

/**
* Handle errors.
* This function will handle all PHP errors thrown at it, without exposing
* paths, and hopefully, providing much more information to Root Users than
* the default white error page.
* This function will call out to CUSTOM_handleError if it exists, but, be
* advised, only override this function with a very, very stable function. I'd
* suggest one that outputs some static, basic HTML.
* The PHP feature that allows us to do so is documented here:
* http://uk2.php.net/manual/en/function.set-error-handler.php
*
* @param int $errNo Error Number.
* @param string $errStr Error Message.
* @param string $errFile The file the error was raised in.
* @param int $errLine The line of the file that the error was raised at.
* @param array $errContext An array that points to the active symbol table at the point the error occurred.
* @param int $type Geeklog Error Type - 1 = PHP, 2 = Geeklog Specific
*/
function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errContext = array(), $type = 1)
{
global $_CONF, $_USER, $LANG01;

// Handle @ operator
if (error_reporting() == 0) {
return;
}

$hasPHP8 = version_compare(PHP_VERSION, '8.0.0', '>=');

/*
* If we have a root user, then output detailed error message:
Expand Down Expand Up @@ -8150,15 +8178,17 @@ function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errConte
}

if ($type == 1) {
$output .= "<p>$errorTypes[$errNo]($errNo) - $errStr @ $errFile line $errLine</p>";
$output .= '<p>' . COM_convertErrorNumberToString($errNo) . "($errNo) - $errStr @ $errFile line $errLine</p>";
} else {
$output .= $errStr;
}

if ($type == 1) {
if (!function_exists('SEC_inGroup') || !SEC_inGroup('Root')) {
if ('force' != '' . $_CONF['rootdebug']) {
$errContext = COM_rootDebugClean($errContext);
if (!$hasPHP8) {
$errContext = COM_rootDebugClean($errContext);
}
} else {
$output .= <<<HTML
<h2 style="color: red;">Root Debug is set to "force", this means that passwords and session cookies are exposed in this message!!!</h2>
Expand All @@ -8169,7 +8199,8 @@ function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errConte

if (@ini_get('xdebug.default_enable') == 1) {
ob_start();
if ($type == 1) {
// Since PHP 8.0.0, $errContext argument was dropped
if (!$hasPHP8 && ($type == 1)) {
var_dump($errContext);
}
$output .= ob_get_clean() . PHP_EOL . '</body></html>';
Expand Down Expand Up @@ -8231,7 +8262,7 @@ function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errConte
; }

// Since PHP 8.0.0, $errContext argument was dropped
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
if (!$hasPHP8) {
$output .= '<pre>';
ob_start();
if ($type == 1) {
Expand All @@ -8256,15 +8287,19 @@ function COM_handleError($errNo, $errStr, $errFile = '', $errLine = 0, $errConte
require_once $_CONF['path_system'] . 'lib-custom.php';
}
if (function_exists('CUSTOM_handleError')) {
CUSTOM_handleError($errNo, $errStr, $errFile, $errLine, $errContext);
if ($hasPHP8) {
CUSTOM_handleError($errNo, $errStr, $errFile, $errLine);
} else {
CUSTOM_handleError($errNo, $errStr, $errFile, $errLine, $errContext);
}
exit;
}
}
}

// if we do not throw the error back to an admin, still log it in the error.log
if ($type == 1) {
COM_errorLog("$errorTypes[$errNo]($errNo) - $errStr @ $errFile line $errLine", 1);
COM_errorLog(COM_convertErrorNumberToString($errNo) . "($errNo) - $errStr @ $errFile line $errLine", 1);
} else {
COM_errorLog("$errNo - $errStr", 1);
}
Expand Down
17 changes: 12 additions & 5 deletions system/classes/Database/DbMysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace Geeklog\Database;

use Exception;
use InvalidArgumentException;
use Mysqli;
use mysqli_result;
Expand Down Expand Up @@ -506,12 +507,18 @@ public function dbQuery($sql, $ignore_errors = 0)
$sql = $this->fixCreateSQL($sql);

// Run query
if ($ignore_errors) {
$result = @$this->_db->query($sql);
} else {
$result = @$this->_db->query($sql);
try {
if ($ignore_errors) {
$result = @$this->_db->query($sql);
} else {
$result = @$this->_db->query($sql);

if ($result === false) {
if ($result === false) {
trigger_error($this->dbError($sql), E_USER_ERROR);
}
}
} catch (Exception $exception) {
if (!$ignore_errors) {
trigger_error($this->dbError($sql), E_USER_ERROR);
}
}
Expand Down
6 changes: 3 additions & 3 deletions system/lib-database.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@

if ($_DB_dbms === 'mysql') {
if (class_exists('MySQLi')) {
$_DB = new Geeklog\Database\DbMysqli($_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_table_prefix, 'COM_errorLog', $_DB_charset);
$_DB = new Geeklog\Database\DbMysqli($_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_table_prefix, '\COM_errorLog', $_DB_charset);
} else {
$_DB = new Geeklog\Database\DbMysql($_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_table_prefix, 'COM_errorLog', $_DB_charset);
$_DB = new Geeklog\Database\DbMysql($_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_table_prefix, '\COM_errorLog', $_DB_charset);
}
} elseif ($_DB_dbms === 'pgsql') {
$_DB = new Geeklog\Database\DbPgsql($_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_table_prefix, 'COM_errorLog', $_DB_charset);
$_DB = new Geeklog\Database\DbPgsql($_DB_host, $_DB_name, $_DB_user, $_DB_pass, $_DB_table_prefix, '\COM_errorLog', $_DB_charset);
} else {
throw new InvalidArgumentException(sprintf('Unknown database driver "%s" was specified', $_DB_dbms));
}
Expand Down

0 comments on commit ace20bc

Please sign in to comment.