backport database-logging-fix
Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
@@ -224,7 +224,7 @@ class Database {
|
||||
&& isset($sql['root_password'])
|
||||
&& (!isset($sql_root) || !is_array($sql_root))
|
||||
) {
|
||||
$sql_root = array(0 => array('caption' => 'Default', 'host' => $sql['host'], 'user' => $sql['root_user'], 'password' => $sql['root_password']));
|
||||
$sql_root = array(0 => array('caption' => 'Default', 'host' => $sql['host'], 'socket' => (isset($sql['socket']) ? $sql['socket'] : null), 'user' => $sql['root_user'], 'password' => $sql['root_password']));
|
||||
unset($sql['root_user']);
|
||||
unset($sql['root_password']);
|
||||
}
|
||||
@@ -235,11 +235,15 @@ class Database {
|
||||
$user = $sql_root[self::$_dbserver]['user'];
|
||||
$password = $sql_root[self::$_dbserver]['password'];
|
||||
$host = $sql_root[self::$_dbserver]['host'];
|
||||
$socket = isset($sql_root[self::$_dbserver]['socket']) ? $sql_root[self::$_dbserver]['socket'] : null;
|
||||
$port = isset($sql_root[self::$_dbserver]['port']) ? $sql_root[self::$_dbserver]['port'] : '3306';
|
||||
} else {
|
||||
$caption = 'localhost';
|
||||
$user = $sql["user"];
|
||||
$password = $sql["password"];
|
||||
$host = $sql["host"];
|
||||
$socket = isset($sql['socket']) ? $sql['socket'] : null;
|
||||
$port = isset($sql['port']) ? $sql['port'] : '3306';
|
||||
}
|
||||
|
||||
// save sql-access-data if needed
|
||||
@@ -248,6 +252,8 @@ class Database {
|
||||
'user' => $user,
|
||||
'passwd' => $password,
|
||||
'host' => $host,
|
||||
'port' => $port,
|
||||
'socket' => $socket,
|
||||
'db' => $sql["db"],
|
||||
'caption' => $caption
|
||||
);
|
||||
@@ -264,10 +270,11 @@ class Database {
|
||||
'charset' => 'utf8'
|
||||
);
|
||||
|
||||
if (!validateLocalHostname($host) && !validate_ip2($host, true, 'invalidip', true, true)) {
|
||||
$dbconf["dsn"]['unix_socket'] = makeCorrectFile($host);
|
||||
if ($socket != null) {
|
||||
$dbconf["dsn"]['unix_socket'] = makeCorrectFile($socket);
|
||||
} else {
|
||||
$dbconf["dsn"]['host'] = $host;
|
||||
$dbconf["dsn"]['port'] = $port;
|
||||
}
|
||||
|
||||
self::$_dbname = $sql["db"];
|
||||
@@ -305,47 +312,15 @@ class Database {
|
||||
private static function _showerror($error, $showerror = true) {
|
||||
global $userinfo, $theme, $linker;
|
||||
|
||||
/**
|
||||
* log to a file, so we can actually ask people for the error
|
||||
* (no one seems to find the stuff in the syslog)
|
||||
*/
|
||||
$sl_dir = makeCorrectDir(FROXLOR_INSTALL_DIR."/logs/");
|
||||
if (!file_exists($sl_dir)) {
|
||||
@mkdir($sl_dir, 0755);
|
||||
}
|
||||
$sl_file = makeCorrectFile($sl_dir."/sql-error.log");
|
||||
$sqllog = @fopen($sl_file, 'a');
|
||||
@fwrite($sqllog, date('d.m.Y H:i', time())." --- ".str_replace("\n", " ", $error->getMessage())."\n");
|
||||
@fwrite($sqllog, date('d.m.Y H:i', time())." --- DEBUG: \n".$error->getTraceAsString()."\n");
|
||||
@fclose($sqllog);
|
||||
|
||||
/**
|
||||
* log error for reporting
|
||||
*/
|
||||
$errid = substr(md5(microtime()), 5, 5);
|
||||
$err_file = makeCorrectFile($sl_dir."/".$errid."_sql-error.log");
|
||||
$errlog = @fopen($err_file, 'w');
|
||||
@fwrite($errlog, "|CODE ".$error->getCode()."\n");
|
||||
@fwrite($errlog, "|MSG ".$error->getMessage()."\n");
|
||||
@fwrite($errlog, "|FILE ".$error->getFile()."\n");
|
||||
@fwrite($errlog, "|LINE ".$error->getLine()."\n");
|
||||
@fwrite($errlog, "|TRACE\n".$error->getTraceAsString()."\n");
|
||||
@fclose($errlog);
|
||||
|
||||
if ($showerror) {
|
||||
|
||||
// include userdata.inc.php
|
||||
require FROXLOR_INSTALL_DIR."/lib/userdata.inc.php";
|
||||
|
||||
// fallback
|
||||
$theme = 'Sparkle';
|
||||
|
||||
// le format
|
||||
if (isset($sql['root_user'])
|
||||
&& isset($sql['root_password'])
|
||||
&& (!isset($sql_root) || !is_array($sql_root))
|
||||
) {
|
||||
$sql_root = array(0 => array('caption' => 'Default', 'host' => $sql['host'], 'user' => $sql['root_user'], 'password' => $sql['root_password']));
|
||||
$sql_root = array(0 => array('caption' => 'Default', 'host' => $sql['host'], 'socket' => (isset($sql['socket']) ? $sql['socket'] : null), 'user' => $sql['root_user'], 'password' => $sql['root_password']));
|
||||
}
|
||||
|
||||
// hide username/password in messages
|
||||
@@ -358,6 +333,42 @@ class Database {
|
||||
$error_trace = str_replace($sql['password'], 'DB_UNPRIV_PWD', $error_trace);
|
||||
$error_trace = str_replace($sql_root[0]['password'], 'DB_ROOT_PWD', $error_trace);
|
||||
|
||||
if ($error->getCode() == 2003) {
|
||||
$error_message = "Unable to connect to database. Either the mysql-server is not running or your user/password is wrong.";
|
||||
$error_trace = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* log to a file, so we can actually ask people for the error
|
||||
* (no one seems to find the stuff in the syslog)
|
||||
*/
|
||||
$sl_dir = makeCorrectDir(FROXLOR_INSTALL_DIR."/logs/");
|
||||
if (!file_exists($sl_dir)) {
|
||||
@mkdir($sl_dir, 0755);
|
||||
}
|
||||
openlog("froxlor", LOG_PID | LOG_PERROR, LOG_LOCAL0);
|
||||
syslog(LOG_WARNING, str_replace("\n", " ", $error_message));
|
||||
syslog(LOG_WARNING, str_replace("\n", " ", "--- DEBUG: ".$error_trace));
|
||||
closelog();
|
||||
|
||||
/**
|
||||
* log error for reporting
|
||||
*/
|
||||
$errid = substr(md5(microtime()), 5, 5);
|
||||
$err_file = makeCorrectFile($sl_dir."/".$errid."_sql-error.log");
|
||||
$errlog = @fopen($err_file, 'w');
|
||||
@fwrite($errlog, "|CODE ".$error->getCode()."\n");
|
||||
@fwrite($errlog, "|MSG ".$error_message."\n");
|
||||
@fwrite($errlog, "|FILE ".$error->getFile()."\n");
|
||||
@fwrite($errlog, "|LINE ".$error->getLine()."\n");
|
||||
@fwrite($errlog, "|TRACE\n".$error_trace."\n");
|
||||
@fclose($errlog);
|
||||
|
||||
if ($showerror) {
|
||||
|
||||
// fallback
|
||||
$theme = 'Sparkle';
|
||||
|
||||
// clean up sensitive data
|
||||
unset($sql);
|
||||
unset($sql_root);
|
||||
@@ -388,7 +399,8 @@ class Database {
|
||||
die($err_hint);
|
||||
}
|
||||
}
|
||||
die("We are sorry, but a MySQL - error occurred. The administrator may find more information in in the sql-error.log in the logs/ directory");
|
||||
die("We are sorry, but a MySQL - error occurred. The administrator may find more information in the syslog");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user