Log database errors instead of showing them.

display_errors is off unless DISPLAY_ERRORS is set in .env.
This commit is contained in:
tim
2026-09-25 21:57:11 +02:00
parent bcba2e1ca3
commit f97bdecbc1
9 changed files with 40 additions and 16 deletions
+4
View File
@@ -17,3 +17,7 @@ GOOGLE_ANALYTICS_ID=
# Directory for uploaded images waiting to be saved. Keep it outside the # Directory for uploaded images waiting to be saved. Keep it outside the
# web root. The default is xxx-uploads in the system temporary directory. # web root. The default is xxx-uploads in the system temporary directory.
UPLOAD_DIR= UPLOAD_DIR=
# Show PHP errors in the page (1) or on stderr (stderr). Leave it empty in
# production: errors always go to the PHP error log.
DISPLAY_ERRORS=
+4
View File
@@ -39,4 +39,8 @@ function config($key, $default = NULL) {
return array_key_exists($key, $values) ? $values[$key] : $default; return array_key_exists($key, $values) ? $values[$key] : $default;
} }
// Always log PHP errors; show them only if DISPLAY_ERRORS is set (for development)
ini_set('log_errors', '1');
ini_set('display_errors', (string)config('DISPLAY_ERRORS', '0'));
?> ?>
+1 -1
View File
@@ -60,7 +60,7 @@ function display_img($id) {
} }
$query = "SELECT img, mime FROM $table WHERE 1 AND ID = ?".$hidden." LIMIT 0, 1"; $query = "SELECT img, mime FROM $table WHERE 1 AND ID = ?".$hidden." LIMIT 0, 1";
$result = $db->execute_query($query, array((int)$id)) or die("Query failed : ".$db->error); $result = $db->execute_query($query, array((int)$id)) or db_fail();
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
if ($row) { if ($row) {
+1 -1
View File
@@ -13,7 +13,7 @@ header("Content-Type: application/rss+xml");
// Set up a RSS "header" // Set up a RSS "header"
$query = "SELECT MAX(`date`) FROM $table;"; $query = "SELECT MAX(`date`) FROM $table;";
$result = $db->query($query) or die("Query failed : ".$db->error); $result = $db->query($query) or db_fail();
$most_recent = $result->fetch_row(); $most_recent = $result->fetch_row();
$now = date("D, d M Y H:i:s O"); $now = date("D, d M Y H:i:s O");
+2 -3
View File
@@ -127,13 +127,12 @@ function get_count() {
//echo $count_query." <br />"; //echo $count_query." <br />";
$row_count = NULL; $row_count = NULL;
echo $db->info;
if ($count_query) { if ($count_query) {
if ($result = $db->execute_query($count_query, $params)) { if ($result = $db->execute_query($count_query, $params)) {
$row_count = $result->fetch_row(); $row_count = $result->fetch_row();
$result->close(); $result->close();
} else { } else {
die("Error in query: $db->error"); db_fail();
} }
} }
return $row_count[0]; return $row_count[0];
@@ -186,7 +185,7 @@ function get_query($id = NULL) {
} }
$result->close(); $result->close();
} else { } else {
die("Error in query: $db->error"); db_fail();
} }
return $results; return $results;
} }
+19 -2
View File
@@ -13,8 +13,9 @@ function open_db($state) {
mysqli_report(MYSQLI_REPORT_OFF); mysqli_report(MYSQLI_REPORT_OFF);
$db = new mysqli($hostname, $view_user, $view_pass, $db_name); $db = new mysqli($hostname, $view_user, $view_pass, $db_name);
if ($db->connect_error) { if ($db->connect_error) {
die ("Unable to connect to database"); $detail = 'connect error (' . $db->connect_errno . ') ' . $db->connect_error;
die ('Connect Error (' . $db->connect_errno . ') ' . $db->connect_error); $db = NULL;
db_fail($detail);
} }
return TRUE; return TRUE;
} else } else
@@ -29,4 +30,20 @@ function open_db($state) {
return FALSE; return FALSE;
} }
} }
/*
* Stop the page after a database error. The details, with the file and
* line of the failed call, go to the PHP error log; the visitor only
* sees a short apology.
*/
function db_fail($detail = NULL) {
global $db;
if ($detail === NULL)
$detail = $db ? $db->error : 'no database connection';
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
error_log('xxx: database error at ' . basename($caller['file']) . ':' . $caller['line'] . ': ' . $detail);
if (!headers_sent())
header('HTTP/1.1 500 Internal Server Error');
die('<p>Sorry, something went wrong. Please try again later.</p>');
}
?> ?>
+1 -1
View File
@@ -27,7 +27,7 @@ if (isset($_SESSION['editor'])) {
$username = (string)($_POST['username'] ?? ''); $username = (string)($_POST['username'] ?? '');
$query = "SELECT * FROM $table WHERE username = ? LIMIT 1"; $query = "SELECT * FROM $table WHERE username = ? LIMIT 1";
$getid = $db->execute_query($query, array($username)) or die("Query failed: ".$db->error); $getid = $db->execute_query($query, array($username)) or db_fail();
$row = $getid->fetch_assoc(); $row = $getid->fetch_assoc();
$stored_password = ''; $stored_password = '';
+4 -4
View File
@@ -46,7 +46,7 @@ function save_data($id = NULL) {
$mime = $row['mime']; $mime = $row['mime'];
$getimg->close(); $getimg->close();
} else { } else {
die("Error in query: $db->error"); db_fail();
} }
} else { } else {
$image_file = save_uploaded_file(); $image_file = save_uploaded_file();
@@ -91,7 +91,7 @@ function save_data($id = NULL) {
} }
//echo $query; //echo $query;
$result = $db->execute_query($query, $params) or die("Query failed: ".$db->error); $result = $db->execute_query($query, $params) or db_fail();
if (is_numeric($id) && $move) { if (is_numeric($id) && $move) {
$queue_id = $id; $queue_id = $id;
@@ -100,7 +100,7 @@ function save_data($id = NULL) {
// Get the (new) ID if we don't already have it. // Get the (new) ID if we don't already have it.
if (!is_numeric($id) || $move) { if (!is_numeric($id) || $move) {
$query = "SELECT LAST_INSERT_ID();"; $query = "SELECT LAST_INSERT_ID();";
$getid = $db->query($query) or die("Query failed: ".$db->error); $getid = $db->query($query) or db_fail();
$row = $getid->fetch_row(); $row = $getid->fetch_row();
$id = $row[0]; $id = $row[0];
} }
@@ -140,7 +140,7 @@ function delete_story($id, $move = FALSE) {
$query = "DELETE FROM $table WHERE `id` = ? LIMIT 1"; $query = "DELETE FROM $table WHERE `id` = ? LIMIT 1";
//echo $query; //echo $query;
$result = $db->execute_query($query, array((int)$id)) or die("Query failed : ".$db->error); $result = $db->execute_query($query, array((int)$id)) or db_fail();
$message = ''; $message = '';
if (!$move) if (!$move)
+4 -4
View File
@@ -55,7 +55,7 @@ function check_persistent_login() {
open_db('open'); open_db('open');
$query = "SELECT * FROM $table WHERE username = ? AND session = ? LIMIT 1"; $query = "SELECT * FROM $table WHERE username = ? AND session = ? LIMIT 1";
$getid = $db->execute_query($query, array($username, $key_hash)) or die("Query failed: ".$db->error); $getid = $db->execute_query($query, array($username, $key_hash)) or db_fail();
$row = $getid->fetch_assoc(); $row = $getid->fetch_assoc();
open_db('close'); open_db('close');
@@ -79,7 +79,7 @@ function create_persistent_login($username) {
// Add the hash of the session key to the database // Add the hash of the session key to the database
$query = "INSERT INTO $table (username, session) VALUES (?, ?)"; $query = "INSERT INTO $table (username, session) VALUES (?, ?)";
$db->execute_query($query, array($username, persistent_key_hash($key))) or die("Query failed: ".$db->error); $db->execute_query($query, array($username, persistent_key_hash($key))) or db_fail();
open_db('close'); open_db('close');
// Create cookie with key // Create cookie with key
@@ -98,14 +98,14 @@ function delete_persistent_login() {
// Remove this session key from the database // Remove this session key from the database
if ($username !== '' && $key !== '') { if ($username !== '' && $key !== '') {
$query = "DELETE FROM $table WHERE username = ? AND session = ?"; $query = "DELETE FROM $table WHERE username = ? AND session = ?";
$db->execute_query($query, array($username, persistent_key_hash($key))) or die("Query failed: ".$db->error); $db->execute_query($query, array($username, persistent_key_hash($key))) or db_fail();
} }
// Reap old sessions, and keys stored before they were hashed // Reap old sessions, and keys stored before they were hashed
$query = "DELETE FROM $table " . $query = "DELETE FROM $table " .
" WHERE created < DATE_SUB(now(), INTERVAL 1 YEAR)" . " WHERE created < DATE_SUB(now(), INTERVAL 1 YEAR)" .
" OR CHAR_LENGTH(session) <> 64;"; " OR CHAR_LENGTH(session) <> 64;";
$db->query($query) or die("Query failed: ".$db->error); $db->query($query) or db_fail();
open_db('close'); open_db('close');