forked from poweradmin/poweradmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.inc.php
149 lines (127 loc) · 3.99 KB
/
database.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/* Poweradmin, a friendly web-based admin tool for PowerDNS.
* See <https://www.poweradmin.org> for more details.
*
* Copyright 2007-2009 Rejo Zenger <[email protected]>
* Copyright 2010-2011 Poweradmin Development Team <http://www.poweradmin.org/credits>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(@include_once "MDB2.php") or die (error('You have to install MDB2 library!'));
function dbError($msg)
{
$debug = $msg->getDebugInfo();
if (preg_match("/Unknown column 'zone_templ_id'/", $debug)) {
$debug = ERR_DB_NO_DB_UPDATE;
}
echo " <div class=\"error\">Error: " . $debug . "</div>\n";
include_once("footer.inc.php");
die();
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'dbError');
function dbConnect() {
// XXX: one day all globals will die, I promise
global $db_type;
global $db_user;
global $db_pass;
global $db_host;
global $db_port;
global $db_name;
global $db_file;
global $db_debug;
global $sql_regexp;
if (!(isset($db_type) && $db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'pgsql' || $db_type == 'sqlite' || $db_type == 'oci8'))
{
include_once("header.inc.php");
error(ERR_DB_NO_DB_TYPE);
include_once("footer.inc.php");
exit;
}
if ($db_type != 'sqlite' && !(isset($db_user) && $db_user != "")) {
include_once("header.inc.php");
error(ERR_DB_NO_DB_USER);
include_once("footer.inc.php");
exit;
}
if ($db_type != 'sqlite' && !(isset($db_pass) && $db_pass != '')) {
include_once("header.inc.php");
error(ERR_DB_NO_DB_PASS);
include_once("footer.inc.php");
exit;
}
if ($db_type != 'sqlite' && !(isset($db_host) && $db_host != '')) {
include_once("header.inc.php");
error(ERR_DB_NO_DB_HOST);
include_once("footer.inc.php");
exit;
}
if ($db_type != 'sqlite' && !(isset($db_name) && $db_name != '')) {
include_once("header.inc.php");
error(ERR_DB_NO_DB_NAME);
include_once("footer.inc.php");
exit;
}
if ($db_type != 'sqlite' && !(isset($db_port))) {
if ($db_type == "mysql" || $db_type == "mysqli") {
$db_port = 3306;
} else if ($db_type == 'oci8') {
$db_port = 1521;
} else {
$db_port = 5432;
}
}
if ($db_type == 'sqlite' && !(isset($db_file) && $db_file != '')) {
include_once("header.inc.php");
error(ERR_DB_NO_DB_FILE);
include_once("footer.inc.php");
exit;
}
if ($db_type == 'sqlite') {
$dsn = "$db_type:///$db_file";
} else {
if ($db_type == 'oci8') {
$db_name = '?service='.$db_name;
}
$dsn = "$db_type://$db_user:$db_pass@$db_host:$db_port/$db_name";
}
$options = array(
'portability' => MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL,
);
$db = MDB2::connect($dsn, $options);
if (isset($db_debug) && $db_debug) {
$db->setOption('debug', 1);
}
// FIXME - it's strange, but this doesn't work, perhaps bug in MDB2 library
if (PEAR::isError($db)) {
// Error handling should be put.
error(MYSQL_ERROR_FATAL, $db->getMessage());
}
// Do an ASSOC fetch. Gives us the ability to use ["id"] fields.
$db->setFetchMode(MDB2_FETCHMODE_ASSOC);
/* erase info */
$dsn = '';
// Add support for regular expressions in both MySQL and PostgreSQL
if ( $db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'sqlite') {
$sql_regexp = "REGEXP";
} elseif ( $db_type == "oci8" ) {
# TODO: what is regexp syntax in Oracle?
$sql_regexp = "";
} elseif ( $db_type == "pgsql" ) {
$sql_regexp = "~";
} else {
error(ERR_DB_NO_DB_TYPE);
};
return $db;
}
?>