2010-12-17 15:18:03 +01:00
|
|
|
<?php
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
/*
|
2013-01-20 11:23:46 +01:00
|
|
|
* Copyright (c) 2010-2013 Tinyboard Development Group
|
2012-04-11 18:49:22 +02:00
|
|
|
*/
|
|
|
|
|
2013-09-06 12:12:04 +02:00
|
|
|
defined('TINYBOARD') or exit;
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
class PreparedQueryDebug {
|
|
|
|
protected $query;
|
2010-12-17 15:18:03 +01:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
public function __construct($query) {
|
|
|
|
global $pdo;
|
|
|
|
$query = preg_replace("/[\n\t]+/", ' ', $query);
|
2011-10-08 10:05:59 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$this->query = $pdo->prepare($query);
|
2011-10-08 10:05:59 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
public function __call($function, $args) {
|
|
|
|
global $config, $debug;
|
2010-12-17 15:18:03 +01:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['debug'] && $function == 'execute') {
|
2012-04-11 18:49:22 +02:00
|
|
|
$start = microtime(true);
|
2010-12-17 15:18:03 +01:00
|
|
|
}
|
2011-10-08 10:05:59 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
$return = call_user_func_array(array($this->query, $function), $args);
|
2011-04-22 16:24:15 +02:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['debug'] && $function == 'execute') {
|
2013-08-30 00:39:11 +02:00
|
|
|
$time = microtime(true) - $start;
|
2012-08-27 13:50:15 +02:00
|
|
|
$debug['sql'][] = array(
|
2012-04-11 18:49:22 +02:00
|
|
|
'query' => $this->query->queryString,
|
|
|
|
'rows' => $this->query->rowCount(),
|
2013-08-30 00:39:11 +02:00
|
|
|
'time' => '~' . round($time * 1000, 2) . 'ms'
|
2011-10-08 10:05:59 +02:00
|
|
|
);
|
2013-08-30 00:39:11 +02:00
|
|
|
$debug['time']['db_queries'] += $time;
|
2011-10-08 10:05:59 +02:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
|
|
|
|
return $return;
|
2010-12-17 15:18:03 +01:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function sql_open() {
|
2013-08-30 00:39:11 +02:00
|
|
|
global $pdo, $config, $debug;
|
2013-08-28 12:09:30 +02:00
|
|
|
if ($pdo)
|
|
|
|
return true;
|
2010-12-17 15:18:03 +01:00
|
|
|
|
2013-08-30 00:39:11 +02:00
|
|
|
|
|
|
|
if ($config['debug'])
|
|
|
|
$start = microtime(true);
|
|
|
|
|
2013-08-28 12:09:30 +02:00
|
|
|
if (isset($config['db']['server'][0]) && $config['db']['server'][0] == ':')
|
|
|
|
$unix_socket = substr($config['db']['server'], 1);
|
|
|
|
else
|
|
|
|
$unix_socket = false;
|
|
|
|
|
|
|
|
$dsn = $config['db']['type'] . ':' .
|
|
|
|
($unix_socket ? 'unix_socket=' . $unix_socket : 'host=' . $config['db']['server']) .
|
|
|
|
';dbname=' . $config['db']['database'];
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!empty($config['db']['dsn']))
|
2012-04-11 18:49:22 +02:00
|
|
|
$dsn .= ';' . $config['db']['dsn'];
|
|
|
|
try {
|
2012-08-27 13:37:21 +02:00
|
|
|
$options = array(
|
|
|
|
PDO::ATTR_TIMEOUT => $config['db']['timeout'],
|
|
|
|
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
|
|
|
|
);
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['db']['persistent'])
|
2012-04-11 18:49:22 +02:00
|
|
|
$options[PDO::ATTR_PERSISTENT] = true;
|
2013-07-31 04:08:56 +02:00
|
|
|
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options);
|
2013-08-30 00:39:11 +02:00
|
|
|
|
|
|
|
if ($config['debug'])
|
|
|
|
$debug['time']['db_connect'] = '~' . round((microtime(true) - $start) * 1000, 2) . 'ms';
|
|
|
|
|
2013-07-31 04:08:56 +02:00
|
|
|
if (mysql_version() >= 50503)
|
|
|
|
query('SET NAMES utf8mb4') or error(db_error());
|
|
|
|
else
|
|
|
|
query('SET NAMES utf8') or error(db_error());
|
|
|
|
return $pdo;
|
2012-04-11 18:49:22 +02:00
|
|
|
} 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
|
2013-07-31 04:08:56 +02:00
|
|
|
error(_('Database error: ') . $message);
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 04:08:56 +02:00
|
|
|
// 5.6.10 becomes 50610
|
|
|
|
function mysql_version() {
|
|
|
|
global $pdo;
|
|
|
|
|
|
|
|
$version = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
$v = explode('.', $version);
|
|
|
|
if (count($v) != 3)
|
|
|
|
return false;
|
|
|
|
return (int) sprintf("%02d%02d%02d", $v[0], $v[1], $v[2]);
|
|
|
|
}
|
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
function prepare($query) {
|
|
|
|
global $pdo, $debug, $config;
|
|
|
|
|
2013-08-01 07:43:33 +02:00
|
|
|
$query = preg_replace('/``('.$config['board_regex'].')``/u', '`' . $config['db']['prefix'] . '$1`', $query);
|
2013-08-01 04:14:26 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
sql_open();
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['debug'])
|
2012-04-11 18:49:22 +02:00
|
|
|
return new PreparedQueryDebug($query);
|
2012-08-30 17:35:27 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
return $pdo->prepare($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function query($query) {
|
|
|
|
global $pdo, $debug, $config;
|
|
|
|
|
2013-08-01 07:43:33 +02:00
|
|
|
$query = preg_replace('/``('.$config['board_regex'].')``/u', '`' . $config['db']['prefix'] . '$1`', $query);
|
2013-08-01 04:14:26 +02:00
|
|
|
|
2012-04-11 18:49:22 +02:00
|
|
|
sql_open();
|
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if ($config['debug']) {
|
2012-04-11 18:49:22 +02:00
|
|
|
$start = microtime(true);
|
|
|
|
$query = $pdo->query($query);
|
2012-04-12 16:18:19 +02:00
|
|
|
if (!$query)
|
2012-04-11 18:49:22 +02:00
|
|
|
return false;
|
2013-08-30 00:39:11 +02:00
|
|
|
$time = microtime(true) - $start;
|
2012-08-27 13:50:15 +02:00
|
|
|
$debug['sql'][] = array(
|
2012-04-11 18:49:22 +02:00
|
|
|
'query' => $query->queryString,
|
|
|
|
'rows' => $query->rowCount(),
|
2013-08-30 00:39:11 +02:00
|
|
|
'time' => '~' . round($time * 1000, 2) . 'ms'
|
2012-04-11 18:49:22 +02:00
|
|
|
);
|
2013-08-30 00:39:11 +02:00
|
|
|
$debug['time']['db_queries'] += $time;
|
2012-04-11 18:49:22 +02:00
|
|
|
return $query;
|
|
|
|
}
|
2012-08-30 17:35:27 +02:00
|
|
|
|
|
|
|
return $pdo->query($query);
|
2012-04-11 18:49:22 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 08:01:52 +02:00
|
|
|
function db_error($PDOStatement = null) {
|
|
|
|
global $pdo, $db_error;
|
2012-08-30 17:35:27 +02:00
|
|
|
|
2012-04-12 16:18:19 +02:00
|
|
|
if (isset($PDOStatement)) {
|
2013-08-03 08:01:52 +02:00
|
|
|
$db_error = $PDOStatement->errorInfo();
|
|
|
|
return $db_error[2];
|
2010-12-17 15:18:03 +01:00
|
|
|
}
|
2012-04-11 18:49:22 +02:00
|
|
|
|
2013-08-03 08:01:52 +02:00
|
|
|
$db_error = $pdo->errorInfo();
|
|
|
|
return $db_error[2];
|
2012-08-30 17:35:27 +02:00
|
|
|
}
|