2010-12-17 09:18:03 -05:00
|
|
|
<?php
|
2012-04-11 12:49:22 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2012 Tinyboard Development Group
|
|
|
|
*/
|
|
|
|
|
2012-04-12 10:18:19 -04:00
|
|
|
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
|
2012-04-11 12:49:22 -04:00
|
|
|
// You cannot request this file directly.
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
class PreparedQueryDebug {
|
|
|
|
protected $query;
|
2010-12-17 09:18:03 -05:00
|
|
|
|
2012-04-11 12:49:22 -04:00
|
|
|
public function __construct($query) {
|
|
|
|
global $pdo;
|
|
|
|
$query = preg_replace("/[\n\t]+/", ' ', $query);
|
2011-10-08 04:05:59 -04:00
|
|
|
|
2012-04-11 12:49:22 -04:00
|
|
|
$this->query = $pdo->prepare($query);
|
2011-10-08 04:05:59 -04:00
|
|
|
}
|
2012-04-11 12:49:22 -04:00
|
|
|
public function __call($function, $args) {
|
|
|
|
global $config, $debug;
|
2010-12-17 09:18:03 -05:00
|
|
|
|
2012-04-12 10:18:19 -04:00
|
|
|
if ($config['debug'] && $function == 'execute') {
|
2012-04-11 12:49:22 -04:00
|
|
|
$start = microtime(true);
|
2010-12-17 09:18:03 -05:00
|
|
|
}
|
2011-10-08 04:05:59 -04:00
|
|
|
|
2012-04-11 12:49:22 -04:00
|
|
|
$return = call_user_func_array(array($this->query, $function), $args);
|
2011-04-22 10:24:15 -04:00
|
|
|
|
2012-04-12 10:18:19 -04:00
|
|
|
if ($config['debug'] && $function == 'execute') {
|
2011-10-08 04:05:59 -04:00
|
|
|
$time = round((microtime(true) - $start) * 1000, 2) . 'ms';
|
2012-04-11 12:49:22 -04:00
|
|
|
|
2011-10-08 04:05:59 -04:00
|
|
|
$debug['sql'][] = Array(
|
2012-04-11 12:49:22 -04:00
|
|
|
'query' => $this->query->queryString,
|
|
|
|
'rows' => $this->query->rowCount(),
|
2011-10-08 04:05:59 -04:00
|
|
|
'time' => '~' . $time
|
|
|
|
);
|
|
|
|
}
|
2012-04-11 12:49:22 -04:00
|
|
|
|
|
|
|
return $return;
|
2010-12-17 09:18:03 -05:00
|
|
|
}
|
2012-04-11 12:49:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function sql_open() {
|
|
|
|
global $pdo, $config;
|
2012-04-12 10:18:19 -04:00
|
|
|
if ($pdo) return true;
|
2010-12-17 09:18:03 -05:00
|
|
|
|
2012-04-11 12:49:22 -04:00
|
|
|
$dsn = $config['db']['type'] . ':host=' . $config['db']['server'] . ';dbname=' . $config['db']['database'];
|
2012-04-12 10:18:19 -04:00
|
|
|
if (!empty($config['db']['dsn']))
|
2012-04-11 12:49:22 -04:00
|
|
|
$dsn .= ';' . $config['db']['dsn'];
|
|
|
|
try {
|
|
|
|
$options = Array(PDO::ATTR_TIMEOUT => $config['db']['timeout']);
|
|
|
|
$options = Array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
|
2012-04-12 10:18:19 -04:00
|
|
|
if ($config['db']['persistent'])
|
2012-04-11 12:49:22 -04:00
|
|
|
$options[PDO::ATTR_PERSISTENT] = true;
|
|
|
|
return $pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options);
|
|
|
|
} catch(PDOException $e) {
|
|
|
|
$message = $e->getMessage();
|
|
|
|
|
|
|
|
// Remove any sensitive information
|
|
|
|
$message = str_replace($config['db']['user'], '<em>hidden</em>', $message);
|
|
|
|
$message = str_replace($config['db']['password'], '<em>hidden</em>', $message);
|
|
|
|
|
|
|
|
// Print error
|
|
|
|
error('Database error: ' . $message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepare($query) {
|
|
|
|
global $pdo, $debug, $config;
|
|
|
|
|
|
|
|
sql_open();
|
|
|
|
|
2012-04-12 10:18:19 -04:00
|
|
|
if ($config['debug'])
|
2012-04-11 12:49:22 -04:00
|
|
|
return new PreparedQueryDebug($query);
|
2012-08-30 11:35:27 -04:00
|
|
|
|
2012-04-11 12:49:22 -04:00
|
|
|
return $pdo->prepare($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function query($query) {
|
|
|
|
global $pdo, $debug, $config;
|
|
|
|
|
|
|
|
sql_open();
|
|
|
|
|
2012-04-12 10:18:19 -04:00
|
|
|
if ($config['debug']) {
|
2012-04-11 12:49:22 -04:00
|
|
|
$start = microtime(true);
|
|
|
|
$query = $pdo->query($query);
|
2012-04-12 10:18:19 -04:00
|
|
|
if (!$query)
|
2012-04-11 12:49:22 -04:00
|
|
|
return false;
|
|
|
|
$time = round((microtime(true) - $start) * 1000, 2) . 'ms';
|
|
|
|
$debug['sql'][] = Array(
|
|
|
|
'query' => $query->queryString,
|
|
|
|
'rows' => $query->rowCount(),
|
|
|
|
'time' => '~' . $time
|
|
|
|
);
|
|
|
|
return $query;
|
|
|
|
}
|
2012-08-30 11:35:27 -04:00
|
|
|
|
|
|
|
return $pdo->query($query);
|
2012-04-11 12:49:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function db_error($PDOStatement=null) {
|
|
|
|
global $pdo;
|
2012-08-30 11:35:27 -04:00
|
|
|
|
2012-04-12 10:18:19 -04:00
|
|
|
if (isset($PDOStatement)) {
|
2012-04-11 12:49:22 -04:00
|
|
|
$err = $PDOStatement->errorInfo();
|
|
|
|
return $err[2];
|
2010-12-17 09:18:03 -05:00
|
|
|
}
|
2012-04-11 12:49:22 -04:00
|
|
|
|
2012-08-30 11:35:27 -04:00
|
|
|
$err = $pdo->errorInfo();
|
|
|
|
return $err[2];
|
|
|
|
}
|