inc/database.inc.php
author peter
Fri, 26 Sep 2008 10:02:33 +0000
changeset 194 df674d004508
parent 142 edd0c3ee3e1d
permissions -rw-r--r--
[feladat @ 307] Minor bug fix: bg.shiny@gmail.com reported that cyrillic characters where not supported. Applied the patch which was supplied. This closes #93.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
e1b918eaf69a [feladat @ 118]
peter
parents: 67
diff changeset
     1
<?php
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     2
119
f74e4f88b680 [feladat @ 222]
rejo
parents: 82
diff changeset
     3
/*  Poweradmin, a friendly web-based admin tool for PowerDNS.
47
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
     4
 *  See <https://rejo.zenger.nl/poweradmin> for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
     5
 *
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
     6
 *  Copyright 2007, 2008  Rejo Zenger <rejo@zenger.nl>
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
     7
 *
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
     8
 *  This program is free software: you can redistribute it and/or modify
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
     9
 *  it under the terms of the GNU General Public License as published by
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    10
 *  the Free Software Foundation, either version 3 of the License, or
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    11
 *  (at your option) any later version.
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    12
 *
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    13
 *  This program is distributed in the hope that it will be useful,
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    16
 *  GNU General Public License for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    17
 *
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    18
 *  You should have received a copy of the GNU General Public License
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    20
 */
ae140472d97c [feladat @ 94]
rejo
parents: 13
diff changeset
    21
8
47dd15d8bb8c [feladat @ 20]
rejo
parents: 4
diff changeset
    22
require_once("MDB2.php");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    23
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    24
function dbError($msg)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    25
{
142
edd0c3ee3e1d [feladat @ 248]
rejo
parents: 136
diff changeset
    26
	echo "     <div class=\"error\">Error: " . $msg->getDebugInfo() . "</div>\n";
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 8
diff changeset
    27
	include_once("footer.inc.php");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
        die();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    29
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    30
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    31
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'dbError');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    32
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    33
function dbConnect() {
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    34
	global $db_type;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    35
	global $db_user;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    36
	global $db_pass;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    37
	global $db_host;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    38
	global $db_name;
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    39
	global $sql_regexp;
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    40
	
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    41
	if (!(isset($db_user) && $db_user != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    42
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    43
		error(ERR_DB_NO_DB_USER);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    44
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    45
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    46
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    47
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    48
	if (!(isset($db_pass) && $db_pass != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    49
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    50
		error(ERR_DB_NO_DB_PASS);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    51
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    52
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    53
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    54
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    55
	if (!(isset($db_host) && $db_host != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    56
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    57
		error(ERR_DB_NO_DB_HOST);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    58
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    59
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    60
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    61
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    62
	if (!(isset($db_name) && $db_name != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    63
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    64
		error(ERR_DB_NO_DB_NAME);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    65
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    66
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    67
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    68
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    69
	if ((!isset($db_type)) || (!($db_type == "mysql" || $db_type == "pgsql"))) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    70
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    71
		error(ERR_DB_NO_DB_TYPE);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    72
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    73
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    74
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    75
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    76
	$dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    77
	$db = MDB2::connect($dsn);
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    78
	$db->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    79
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    80
	if (MDB2::isError($db)) {
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    81
		// Error handling should be put.
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    82
		error(MYSQL_ERROR_FATAL, $db->getMessage());
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    83
	}
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    85
	// Do an ASSOC fetch. Gives us the ability to use ["id"] fields.
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    86
	$db->setFetchMode(MDB2_FETCHMODE_ASSOC);
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    87
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    88
	/* erase info */
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    89
	$mysql_pass = $dsn = '';
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    90
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    91
	// Add support for regular expressions in both MySQL and PostgreSQL
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    92
	if ( $db_type == "mysql" ) {
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    93
		$sql_regexp = "REGEXP";
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    94
	} elseif ( $db_type == "pgsql" ) {
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    95
		$sql_regexp = "~";
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    96
	} else {
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    97
		error(ERR_DB_NO_DB_TYPE);
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    98
	};
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    99
	return $db;
55
a885f557678f [feladat @ 102]
peter
parents: 47
diff changeset
   100
}
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   101
?>