enforce some stricter type checking for loggers

This commit is contained in:
Andrew Dolgov 2021-02-25 17:10:03 +03:00
parent dcf0135285
commit 34c74400a4
7 changed files with 19 additions and 18 deletions

View File

@ -614,7 +614,7 @@ class Handler_Public extends Handler {
function dbupdate() {
startup_gettext();
if (!Config::get(Config::SINGLE_USER_MODE) && $_SESSION["access_level"] < 10) {
if (!Config::get(Config::SINGLE_USER_MODE) && ($_SESSION["access_level"] ?? 0) < 10) {
$_SESSION["login_error_msg"] = __("Your access level is insufficient to run this script.");
$this->_render_login_form();
exit;

View File

@ -63,6 +63,9 @@ class Logger {
default:
$this->adapter = false;
}
if ($this->adapter && !implements_interface($this->adapter, "Logger_Adapter"))
user_error("Adapter for LOG_DESTINATION: " . Config::LOG_DESTINATION . " does not implement required interface.", E_USER_ERROR);
}
private static function get_instance() : Logger {

View File

@ -0,0 +1,4 @@
<?php
interface Logger_Adapter {
function log_error(int $errno, string $errstr, string $file, int $line, $context);
}

View File

@ -1,17 +1,15 @@
<?php
class Logger_SQL {
class Logger_SQL implements Logger_Adapter {
private $pdo;
function log_error($errno, $errstr, $file, $line, $context) {
function log_error(int $errno, string $errstr, string $file, int $line, $context) {
// separate PDO connection object is used for logging
if (!$this->pdo) $this->pdo = Db::instance()->pdo_connect();
if ($this->pdo && get_schema_version() > 117) {
$owner_uid = $_SESSION["uid"] ?? null;
// limit context length, DOMDocument dumps entire XML in here sometimes, which may be huge
$context = mb_substr($context, 0, 8192);
@ -37,7 +35,7 @@ class Logger_SQL {
$sth = $this->pdo->prepare("INSERT INTO ttrss_error_log
(errno, errstr, filename, lineno, context, owner_uid, created_at) VALUES
(?, ?, ?, ?, ?, ?, NOW())");
$sth->execute([$errno, $errstr, $file, $line, $context, $owner_uid]);
$sth->execute([$errno, $errstr, $file, $line, $context, $_SESSION["uid"] ?? null]);
return $sth->rowCount();
}

View File

@ -1,7 +1,7 @@
<?php
class Logger_Stdout {
class Logger_Stdout implements Logger_Adapter {
function log_error($errno, $errstr, $file, $line, $context) {
function log_error(int $errno, string $errstr, string $file, int $line, $context) {
switch ($errno) {
case E_ERROR:

View File

@ -1,7 +1,7 @@
<?php
class Logger_Syslog {
class Logger_Syslog implements Logger_Adapter {
function log_error($errno, $errstr, $file, $line, $context) {
function log_error(int $errno, string $errstr, string $file, int $line, $context) {
switch ($errno) {
case E_ERROR:

View File

@ -40,13 +40,13 @@ function format_backtrace($trace) {
}
function ttrss_error_handler($errno, $errstr, $file, $line) {
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
/*if (version_compare(PHP_VERSION, '8.0.0', '<')) {
if (error_reporting() == 0 || !$errno) return false;
} else {
if (!(error_reporting() & $errno)) return false;
}
if (error_reporting() == 0 || !$errno) return false;
if (error_reporting() == 0 || !$errno) return false;*/
$file = substr(str_replace(dirname(__DIR__), "", $file), 1);
@ -54,12 +54,10 @@ function ttrss_error_handler($errno, $errstr, $file, $line) {
$errstr = truncate_middle($errstr, 16384, " (...) ");
if (class_exists("Logger"))
return Logger::log_error($errno, $errstr, $file, $line, $context);
return Logger::log_error((int)$errno, $errstr, $file, (int)$line, $context);
}
function ttrss_fatal_handler() {
global $last_query;
$error = error_get_last();
if ($error !== NULL) {
@ -74,10 +72,8 @@ function ttrss_fatal_handler() {
$file = substr(str_replace(dirname(__DIR__), "", $file), 1);
if ($last_query) $errstr .= " [Last query: $last_query]";
if (class_exists("Logger"))
return Logger::log_error($errno, $errstr, $file, $line, $context);
return Logger::log_error((int)$errno, $errstr, $file, (int)$line, $context);
}
return false;