Skip to content

Commit 3fd0944

Browse files
Victor Teslacmpalm
authored andcommitted
php-lib-db: add log functions and replace die
die(), which is an alias for exit, outputs its message to stdout which will be sent to the client making debugging hard. Errors will now be logged to the php error log. References: GXL-53
1 parent 01d9c3c commit 3fd0944

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

exch/php/lib/db.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ function get_db_connection()
1212
$config = get_app_config();
1313
$db_config = $config['database'];
1414
if (!isset($db_config)) {
15-
die("cannot find [database] section in config file");
15+
_log_db("cannot find [database] section in config file");
1616
}
1717
if (!isset($db_config['host'])) {
18-
die("cannot find host under [database] in config file");
18+
_log_db("cannot find host under [database] in config file");
1919
}
2020
if (!isset($db_config['username'])) {
21-
die("cannot find username under [database] in config file");
21+
_log_db("cannot find username under [database] in config file");
2222
}
2323
if (!isset($db_config['password'])) {
24-
die("cannot find password under [database] in config file");
24+
_log_db("cannot find password under [database] in config file");
2525
}
2626
if (!isset($db_config['dbname'])) {
27-
die("cannot find dbname under [database] in config file");
27+
_log_db("cannot find dbname under [database] in config file");
2828
}
2929
$db_conn = mysqli_connect($db_config['host'], $db_config['username'], $db_config['password'], $db_config['dbname']);
30-
if ($db_conn->connect_errno) {
31-
die("Failed to connect to database server: " . $db_conn->connect_error);
30+
$e = mysqli_connect_errno();
31+
if ($e != 0) {
32+
_log_db("Failed to connect to database server: " . $e);
3233
}
3334
return $db_conn;
3435
}
@@ -40,7 +41,7 @@ function get_user_info_by_name($email_address)
4041
$sql_string = "SELECT maildir, '', username, id, domain_id, timezone FROM users WHERE username='" . $db_conn->real_escape_string($email_address) . "'";
4142
$results = $db_conn->query($sql_string);
4243
if (!$results) {
43-
die("fail to query database: " . $db_conn->error);
44+
_log_db("fail to query database: " . $db_conn->error);
4445
}
4546
if (1 != mysqli_num_rows($results)) {
4647
return NULL;
@@ -65,7 +66,7 @@ function get_user_info_by_name($email_address)
6566
$sql_string = "SELECT proptag, propval_str FROM users INNER JOIN user_properties AS up ON users.id=up.user_id WHERE users.username='" . $db_conn->real_escape_string($email_address) . "'";
6667
$results = $db_conn->query($sql_string);
6768
if (!$results)
68-
die("failed to query database: " . $db_conn->error);
69+
_log_db("failed to query database: " . $db_conn->error);
6970
while (($row = $results->fetch_row())) {
7071
if ($row[0] == 805371935)
7172
$data_array["real_name"] = $row[1];
@@ -80,7 +81,7 @@ function get_user_info_by_id($user_id)
8081
$sql_string = "SELECT maildir, '', username, id, domain_id, timezone FROM users WHERE id=" . $user_id;
8182
$results = $db_conn->query($sql_string);
8283
if (!$results) {
83-
die("fail to query database: " . $db_conn->error);
84+
_log_db("fail to query database: " . $db_conn->error);
8485
}
8586
if (1 != mysqli_num_rows($results)) {
8687
return NULL;
@@ -105,7 +106,7 @@ function get_user_info_by_id($user_id)
105106
$sql_string = "SELECT proptag, propval_str FROM users INNER JOIN user_properties AS up ON users.id=up.user_id WHERE users.id=" . $user_id;
106107
$results = $db_conn->query($sql_string);
107108
if (!$results)
108-
die("failed to query database: " . $db_conn->error);
109+
_log_db("failed to query database: " . $db_conn->error);
109110
while (($row = $results->fetch_row())) {
110111
if ($row[0] == 805371935)
111112
$data_array["real_name"] = $row[1];
@@ -120,7 +121,7 @@ function get_domain_info_by_name($domain)
120121
$sql_string = "SELECT homedir, id, domainname FROM domains WHERE domainname='" . $db_conn->real_escape_string($domain) . "'";
121122
$results = $db_conn->query($sql_string);
122123
if (!$results) {
123-
die("fail to query database: " . $db_conn->error);
124+
_log_db("fail to query database: " . $db_conn->error);
124125
}
125126
if (1 != mysqli_num_rows($results)) {
126127
return NULL;
@@ -136,7 +137,7 @@ function get_domain_info_by_id($domain_id)
136137
$sql_string = "SELECT homedir, id, domainname FROM domains WHERE id=" . $domain_id;
137138
$results = $db_conn->query($sql_string);
138139
if (!$results) {
139-
die("fail to query database: " . $db_conn->error);
140+
_log_db("fail to query database: " . $db_conn->error);
140141
}
141142
if (1 != mysqli_num_rows($results)) {
142143
return NULL;
@@ -152,7 +153,7 @@ function get_domains()
152153
$sql_string = "SELECT domainname FROM domains";
153154
$results = $db_conn->query($sql_string);
154155
if (!$results) {
155-
die("fail to query database: " . $db_conn->error);
156+
_log_db("fail to query database: " . $db_conn->error);
156157
}
157158
$domains = array();
158159
for ($i = 0; $i < mysqli_num_rows($results); ++$i) {

exch/php/lib/util.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,17 @@ function get_mapihttp_supported()
208208
return false;
209209
}
210210

211-
?>
211+
function _log($from, $message, $exit=-1)
212+
{
213+
error_log($from . ": " . $message);
214+
if ($exit >= 0) {
215+
exit($exit);
216+
}
217+
}
218+
219+
function _log_db($message)
220+
{
221+
_log("gromox-php-db", $message, 0);
222+
}
223+
224+
?>

0 commit comments

Comments
 (0)