Source code of Leftypol imageboard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.2 KiB

<?php
12 years ago
/*
* Copyright (c) 2010-2013 Tinyboard Development Group
12 years ago
*/
12 years ago
if (realpath($_SERVER['SCRIPT_FILENAME']) == str_replace('\\', '/', __FILE__)) {
12 years ago
// You cannot request this file directly.
exit;
}
class PreparedQueryDebug {
protected $query;
12 years ago
public function __construct($query) {
global $pdo;
$query = preg_replace("/[\n\t]+/", ' ', $query);
12 years ago
$this->query = $pdo->prepare($query);
}
12 years ago
public function __call($function, $args) {
global $config, $debug;
12 years ago
if ($config['debug'] && $function == 'execute') {
12 years ago
$start = microtime(true);
}
12 years ago
$return = call_user_func_array(array($this->query, $function), $args);
12 years ago
if ($config['debug'] && $function == 'execute') {
$time = round((microtime(true) - $start) * 1000, 2) . 'ms';
12 years ago
$debug['sql'][] = array(
12 years ago
'query' => $this->query->queryString,
'rows' => $this->query->rowCount(),
'time' => '~' . $time
);
}
12 years ago
return $return;
}
12 years ago
}
function sql_open() {
global $pdo, $config;
12 years ago
if ($pdo) return true;
12 years ago
$dsn = $config['db']['type'] . ':host=' . $config['db']['server'] . ';dbname=' . $config['db']['database'];
12 years ago
if (!empty($config['db']['dsn']))
12 years ago
$dsn .= ';' . $config['db']['dsn'];
try {
$options = array(
PDO::ATTR_TIMEOUT => $config['db']['timeout'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
);
12 years ago
if ($config['db']['persistent'])
12 years ago
$options[PDO::ATTR_PERSISTENT] = true;
$pdo = new PDO($dsn, $config['db']['user'], $config['db']['password'], $options);
if (mysql_version() >= 50503)
query('SET NAMES utf8mb4') or error(db_error());
else
query('SET NAMES utf8') or error(db_error());
return $pdo;
12 years ago
} 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);
12 years ago
}
}
// 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]);
}
12 years ago
function prepare($query) {
global $pdo, $debug, $config;
$query = preg_replace('/``('.$config['board_regex'].')``/u', '`' . $config['db']['prefix'] . '$1`', $query);
12 years ago
sql_open();
12 years ago
if ($config['debug'])
12 years ago
return new PreparedQueryDebug($query);
11 years ago
12 years ago
return $pdo->prepare($query);
}
function query($query) {
global $pdo, $debug, $config;
$query = preg_replace('/``('.$config['board_regex'].')``/u', '`' . $config['db']['prefix'] . '$1`', $query);
12 years ago
sql_open();
12 years ago
if ($config['debug']) {
12 years ago
$start = microtime(true);
$query = $pdo->query($query);
12 years ago
if (!$query)
12 years ago
return false;
$time = round((microtime(true) - $start) * 1000, 2) . 'ms';
$debug['sql'][] = array(
12 years ago
'query' => $query->queryString,
'rows' => $query->rowCount(),
'time' => '~' . $time
);
return $query;
}
11 years ago
return $pdo->query($query);
12 years ago
}
function db_error($PDOStatement=null) {
global $pdo;
11 years ago
12 years ago
if (isset($PDOStatement)) {
12 years ago
$err = $PDOStatement->errorInfo();
return $err[2];
}
12 years ago
11 years ago
$err = $pdo->errorInfo();
return $err[2];
}