2010-12-17 09:18:03 -05:00
|
|
|
<?php
|
2011-04-13 08:21:07 -04:00
|
|
|
if($_SERVER['SCRIPT_FILENAME'] == str_replace('\\', '/', __FILE__)) {
|
|
|
|
// You cannot request this file directly.
|
|
|
|
header('Location: ../', true, 302);
|
|
|
|
exit;
|
|
|
|
}
|
2010-12-17 09:18:03 -05:00
|
|
|
|
|
|
|
function sql_open() {
|
2011-02-12 01:25:15 -05:00
|
|
|
global $pdo, $config;
|
2010-12-17 09:18:03 -05:00
|
|
|
if($pdo) return true;
|
|
|
|
|
2011-02-12 01:25:15 -05:00
|
|
|
$dsn = $config['db']['type'] . ':host=' . $config['db']['server'] . ';dbname=' . $config['db']['database'];
|
|
|
|
if(!empty($config['db']['dsn']))
|
|
|
|
$dsn .= ';' . $config['db']['dsn'];
|
2010-12-17 09:18:03 -05:00
|
|
|
try {
|
2011-04-22 10:24:15 -04:00
|
|
|
$options = Array(PDO::ATTR_TIMEOUT => $config['db']['timeout']);
|
|
|
|
if($config['db']['persistent'])
|
|
|
|
$options[PDO::ATTR_PERSISTENT] = true;
|
2011-02-12 01:25:15 -05:00
|
|
|
return $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password']);
|
2010-12-17 09:18:03 -05:00
|
|
|
} catch(PDOException $e) {
|
2010-12-17 09:32:12 -05:00
|
|
|
$message = $e->getMessage();
|
|
|
|
|
|
|
|
// Remove any sensitive information
|
2011-02-12 01:25:15 -05:00
|
|
|
$message = str_replace($config['db']['user'], '<em>hidden</em>', $message);
|
|
|
|
$message = str_replace($config['db']['password'], '<em>hidden</em>', $message);
|
2010-12-17 09:32:12 -05:00
|
|
|
|
|
|
|
// Print error
|
|
|
|
error('Database error: ' . $message);
|
2010-12-17 09:18:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepare($query) {
|
2011-05-21 01:22:10 -04:00
|
|
|
global $pdo, $debug, $config;
|
|
|
|
if($config['debug']) {
|
|
|
|
$debug['sql'][] = $query;
|
|
|
|
}
|
2011-04-22 10:24:15 -04:00
|
|
|
|
|
|
|
sql_open();
|
2010-12-17 09:18:03 -05:00
|
|
|
return $pdo->prepare($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function query($query) {
|
2011-05-21 01:22:10 -04:00
|
|
|
global $pdo, $debug, $config;
|
|
|
|
if($config['debug']) {
|
|
|
|
$debug['sql'][] = $query;
|
|
|
|
}
|
2011-04-22 10:24:15 -04:00
|
|
|
|
|
|
|
sql_open();
|
2010-12-17 09:18:03 -05:00
|
|
|
return $pdo->query($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function db_error($PDOStatement=null) {
|
|
|
|
global $pdo;
|
|
|
|
if(isset($PDOStatement)) {
|
|
|
|
$err = $PDOStatement->errorInfo();
|
|
|
|
return $err[2];
|
|
|
|
} else {
|
|
|
|
$err = $pdo->errorInfo();
|
|
|
|
return $err[2];
|
|
|
|
}
|
|
|
|
}
|
2011-08-14 10:49:52 -04:00
|
|
|
?>
|