inc/database.inc.php
author rejo
Fri, 11 Apr 2008 13:33:30 +0000
changeset 138 3e36ebbfe048
parent 136 c795dd75a77e
child 142 edd0c3ee3e1d
permissions -rw-r--r--
[feladat @ 244] Improved SOA record validation.
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
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    26
        // General function for printing critical errors.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    27
        include_once("header.inc.php");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
        ?>
71
e1b918eaf69a [feladat @ 118]
peter
parents: 67
diff changeset
    29
	<h2><?php echo _('Oops! An error occured!'); ?></h2>
e1b918eaf69a [feladat @ 118]
peter
parents: 67
diff changeset
    30
	<p class="error"><?php echo $msg->getDebugInfo(); ?></p>
e1b918eaf69a [feladat @ 118]
peter
parents: 67
diff changeset
    31
	<?php        
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 8
diff changeset
    32
	include_once("footer.inc.php");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    33
        die();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    34
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    36
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'dbError');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    37
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    38
function dbConnect() {
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    39
	global $db_type;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    40
	global $db_user;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    41
	global $db_pass;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    42
	global $db_host;
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    43
	global $db_name;
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    44
	global $sql_regexp;
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    45
	
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    46
	if (!(isset($db_user) && $db_user != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    47
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    48
		error(ERR_DB_NO_DB_USER);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    49
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    50
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    51
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    52
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    53
	if (!(isset($db_pass) && $db_pass != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    54
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    55
		error(ERR_DB_NO_DB_PASS);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    56
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    57
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    58
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    59
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    60
	if (!(isset($db_host) && $db_host != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    61
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    62
		error(ERR_DB_NO_DB_HOST);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    63
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    64
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    65
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    66
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    67
	if (!(isset($db_name) && $db_name != "")) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    68
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    69
		error(ERR_DB_NO_DB_NAME);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    70
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    71
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    72
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    73
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    74
	if ((!isset($db_type)) || (!($db_type == "mysql" || $db_type == "pgsql"))) {
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    75
		include_once("header.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    76
		error(ERR_DB_NO_DB_TYPE);
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    77
		include_once("footer.inc.php");
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    78
		exit;
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    79
	}
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    80
		
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    81
	$dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    82
	$db = MDB2::connect($dsn);
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    83
	$db->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
    85
	if (MDB2::isError($db)) {
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    86
		// Error handling should be put.
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    87
		error(MYSQL_ERROR_FATAL, $db->getMessage());
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    88
	}
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    90
	// Do an ASSOC fetch. Gives us the ability to use ["id"] fields.
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    91
	$db->setFetchMode(MDB2_FETCHMODE_ASSOC);
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    92
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    93
	/* erase info */
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    94
	$mysql_pass = $dsn = '';
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    95
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    96
	// Add support for regular expressions in both MySQL and PostgreSQL
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    97
	if ( $db_type == "mysql" ) {
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
    98
		$sql_regexp = "REGEXP";
136
c795dd75a77e [feladat @ 242]
rejo
parents: 123
diff changeset
    99
	} elseif ( $db_type == "pgsql" ) {
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
   100
		$sql_regexp = "~";
123
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
   101
	} else {
0aa6b2cf8006 [feladat @ 226]
rejo
parents: 119
diff changeset
   102
		error(ERR_DB_NO_DB_TYPE);
82
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
   103
	};
c255196bc447 [feladat @ 182]
rejo
parents: 71
diff changeset
   104
	return $db;
55
a885f557678f [feladat @ 102]
peter
parents: 47
diff changeset
   105
}
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   106
?>