Skip to content

Commit 3edd69f

Browse files
committed
Dashboard: Avoid backtick quoting breaking PostgreSQL
Credits to @greenbea for reporting the issue and helping with the fix! Related to #293
1 parent 9fad823 commit 3edd69f

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

Diff for: config/db.inc.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
if (!empty($config->db_port) ) $config->db_host = $config->db_host . ";port=" . $config->db_port;
4545

4646
//connection attributes, optional
47-
//$config->db_attr = array(PDO::MYSQL_ATTR_LOCAL_INFILE => true);
47+
//$config->db_attr = array(
48+
// PDO::MYSQL_ATTR_LOCAL_INFILE => true,
49+
// PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="ANSI_QUOTES"');
4850

4951
?>

Diff for: web/common/cfg_comm.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function load_panels() {
246246
require("".__DIR__."/../../config/tools/system/dashboard/settings.inc.php");
247247
unset($_SESSION['config']['panels']);
248248
$max_order = -1;
249-
$sql = 'select `name`, id, content, positions, `order` from ocp_dashboard';
249+
$sql = 'select "name", id, content, positions, "order" from ocp_dashboard';
250250
$stm = $link->prepare($sql);
251251
if ($stm === false) {
252252
die('Failed to issue query ['.$sql.'], error message : ' . print_r($link->errorInfo(), true));

Diff for: web/tools/system/dashboard/dashboard.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@
303303
if ($action == "add_verify") {
304304
if(!$_SESSION['read_only']){
305305
extract($_POST);
306-
$sql = 'INSERT INTO '.$table.' (`name`, `order`) VALUES (?, ?) ';
306+
$sql = 'INSERT INTO '.$table.' ("name", "order") VALUES (?, ?) ';
307307
$stm = $link->prepare($sql);
308308
if ($stm === false) {
309309
die('Failed to issue query ['.$sql.'], error message : ' . print_r($link->errorInfo(), true));
@@ -375,7 +375,7 @@
375375
}
376376
$widget_contents_json = json_encode($widget_contents);
377377

378-
$sql = 'UPDATE '.$table.' SET `name`=?, content=?, `order`=?, positions=? where id = ?';
378+
$sql = 'UPDATE '.$table.' SET "name"=?, content=?, "order"=?, positions=? where id = ?';
379379
$stm = $link->prepare($sql);
380380
if ($stm === false) {
381381
die('Failed to issue query ['.$sql.'], error message : ' . print_r($link->errorInfo(), true));
@@ -427,7 +427,7 @@
427427
if ($action == "change_name_verify") {
428428
if(!$_SESSION['read_only']){
429429
extract($_POST);
430-
$sql = 'UPDATE '.$table.' SET name = ? WHERE id = ? ';
430+
$sql = 'UPDATE '.$table.' SET "name" = ? WHERE id = ? ';
431431
$stm = $link->prepare($sql);
432432
if ($stm === false) {
433433
die('Failed to issue query ['.$sql.'], error message : ' . print_r($link->errorInfo(), true));

Diff for: web/tools/system/dashboard/db_connect.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@
3232
$config->db_name = $config->db_name_smonitor;
3333
}
3434

35+
$db_attr = $config->db_attr;
36+
if (!isset($db_attr))
37+
$db_attr = array();
38+
39+
// dashboard uses "identifier" quoting in queries, so try to stay Postgres-compatible too
40+
$db_attr[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET sql_mode='ANSI_QUOTES'";
41+
3542
$dsn = $config->db_driver . ':host=' . $config->db_host . ';dbname='. $config->db_name;
3643
try {
37-
$link = new PDO($dsn, $config->db_user, $config->db_pass);
44+
$link = new PDO($dsn, $config->db_user, $config->db_pass, options: $db_attr);
3845
} catch (PDOException $e) {
3946
error_log(print_r("Failed to connect to: ".$dsn, true));
4047
print "Error!: " . $e->getMessage() . "<br/>";

Diff for: web/tools/system/dashboard/template/dashboard.edit_panel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
}
6666
$start_limit=($page-1)*$res_no;
6767
//$sql_command.=" limit ".$start_limit.", ".$res_no;
68-
$sql_command="select `name`, id, `order` from ".$table." order by `order` asc limit ".$res_no;
68+
$sql_command='select "name", id, "order" from '.$table.' order by "order" asc limit '.$res_no;
6969
if ($start_limit!=0)
7070
$sql_command.=" OFFSET " . $start_limit;
7171
$stm = $link->prepare( $sql_command );

Diff for: web/tools/system/dashboard/template/functions.inc.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ function swap_panels($first, $second, $table) {
3030
require("../../../../config/db.inc.php");
3131
require("../../../../config/tools/system/dashboard/settings.inc.php");
3232

33-
$sql = 'UPDATE '.$table.' SET `order` =
33+
$sql = 'UPDATE '.$table.' SET "order" =
3434
CASE
35-
WHEN `order` = ? THEN ?
36-
WHEN `order` = ? THEN ?
35+
WHEN "order" = ? THEN ?
36+
WHEN "order" = ? THEN ?
3737
END
38-
WHERE `order` = ? or `order` = ?
38+
WHERE "order" = ? or "order" = ?
3939
';
4040
$stm = $link->prepare($sql);
4141
if ($stm === false) {
@@ -80,4 +80,4 @@ function print_description_widget($desc) {
8080
8181
");
8282
}
83-
?>
83+
?>

0 commit comments

Comments
 (0)