2014-04-28 23:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
2017-05-12 05:05:49 +02:00
|
|
|
use DDDBL\DataObjectPool;
|
|
|
|
use DDDBL\Queue;
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2014-04-28 23:55:47 +02:00
|
|
|
require_once('include/datetime.php');
|
|
|
|
|
2017-05-12 05:05:49 +02:00
|
|
|
$objDDDBLResultHandler = new DataObjectPool('Result-Handler');
|
2014-04-28 23:55:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* create handler, which returns just the PDOStatement object
|
|
|
|
* this allows usage of the cursor to scroll through
|
|
|
|
* big result-sets
|
|
|
|
*
|
|
|
|
**/
|
2017-05-12 05:05:49 +02:00
|
|
|
$cloPDOStatementResultHandler = function(Queue $objQueue) {
|
2017-04-04 19:47:32 +02:00
|
|
|
$objPDO = $objQueue->getState()->get('PDOStatement');
|
|
|
|
$objQueue->getState()->update(array('result' => $objPDO));
|
2014-04-28 23:55:47 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
/*
|
|
|
|
* delete handler which closes the PDOStatement-cursor
|
|
|
|
* this will be done manual if using this handler
|
|
|
|
*/
|
|
|
|
$objQueue->deleteHandler(QUEUE_CLOSE_CURSOR_POSITION);
|
2014-04-28 23:55:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$objDDDBLResultHandler->add('PDOStatement', array('HANDLER' => $cloPDOStatementResultHandler));
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! class_exists('dba')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* MySQL database class
|
|
|
|
*
|
|
|
|
* For debugging, insert 'dbg(1);' anywhere in the program flow.
|
|
|
|
* dbg(0); will turn it off. Logging is performed at LOGGER_DATA level.
|
2017-05-11 17:53:04 +02:00
|
|
|
* When logging, all binary info is converted to text and html entities are escaped so that
|
2014-04-28 23:55:47 +02:00
|
|
|
* the debugging stream is safe to view within both terminals and web pages.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class dba {
|
|
|
|
|
|
|
|
private $debug = 0;
|
|
|
|
private $db;
|
|
|
|
private $result;
|
|
|
|
public $connected = false;
|
|
|
|
public $error = false;
|
|
|
|
|
|
|
|
function __construct($server,$user,$pass,$db,$install = false) {
|
2016-12-13 10:16:36 +01:00
|
|
|
$a = get_app();
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
// work around, to store the database - configuration in DDDBL
|
|
|
|
$objDataObjectPool = new DataObjectPool('Database-Definition');
|
|
|
|
$objDataObjectPool->add('DEFAULT', array(
|
|
|
|
'CONNECTION' => "mysql:host=$server;dbname=$db",
|
|
|
|
'USER' => $user,
|
|
|
|
'PASS' => $pass,
|
|
|
|
'DEFAULT' => true
|
|
|
|
));
|
2014-04-28 23:55:47 +02:00
|
|
|
|
|
|
|
$stamp1 = microtime(true);
|
|
|
|
|
|
|
|
$server = trim($server);
|
|
|
|
$user = trim($user);
|
|
|
|
$pass = trim($pass);
|
|
|
|
$db = trim($db);
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (!(strlen($server) && strlen($user))) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$this->connected = false;
|
|
|
|
$this->db = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($install && strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
|
|
|
|
if (! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) {
|
|
|
|
$this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server);
|
|
|
|
$this->connected = false;
|
|
|
|
$this->db = null;
|
|
|
|
return;
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
// Establish connection to database and store PDO object
|
|
|
|
DDDBL\connect();
|
|
|
|
$this->db = DDDBL\getDB();
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (DDDBL\isConnected()) {
|
|
|
|
$this->connected = true;
|
|
|
|
}
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! $this->connected) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$this->db = null;
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! $install) {
|
2014-04-28 23:55:47 +02:00
|
|
|
system_unavailable();
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a->save_timestamp($stamp1, "network");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getdb() {
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function q($sql, $onlyquery = false) {
|
2016-12-13 10:16:36 +01:00
|
|
|
$a = get_app();
|
2014-06-10 20:22:53 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
$strHandler = (true === $onlyquery) ? 'PDOStatement' : 'MULTI';
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
$strQueryAlias = md5($sql);
|
|
|
|
$strSQLType = strtoupper(strstr($sql, ' ', true));
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
$objPreparedQueryPool = new DataObjectPool('Query-Definition');
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
// check if query do not exists till now, if so create its definition
|
|
|
|
if (!$objPreparedQueryPool->exists($strQueryAlias)) {
|
|
|
|
$objPreparedQueryPool->add($strQueryAlias, array(
|
|
|
|
'QUERY' => $sql,
|
|
|
|
'HANDLER' => $strHandler
|
|
|
|
));
|
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ((! $this->db) || (! $this->connected)) {
|
2014-04-28 23:55:47 +02:00
|
|
|
return false;
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
|
|
|
|
$this->error = '';
|
|
|
|
|
|
|
|
$stamp1 = microtime(true);
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
try {
|
|
|
|
$r = DDDBL\get($strQueryAlias);
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
// bad workaround to emulate the bizzare behavior of mysql_query
|
|
|
|
if (in_array($strSQLType, array('INSERT', 'UPDATE', 'DELETE', 'CREATE', 'DROP', 'SET'))) {
|
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
$intErrorCode = false;
|
|
|
|
} catch (Exception $objException) {
|
|
|
|
$result = false;
|
|
|
|
$intErrorCode = $objPreparedQueryPool->get($strQueryAlias)->get('PDOStatement')->errorCode();
|
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
|
|
|
|
$stamp2 = microtime(true);
|
|
|
|
$duration = (float)($stamp2-$stamp1);
|
|
|
|
|
|
|
|
$a->save_timestamp($stamp1, "database");
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
/// @TODO really check $a->config for 'system'? it is very generic and should be there
|
2017-04-04 19:47:32 +02:00
|
|
|
if (x($a->config, 'system') && x($a->config['system'], 'db_log') && ($duration > $a->config["system"]["db_loglimit"])) {
|
|
|
|
$duration = round($duration, 3);
|
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
|
|
|
@file_put_contents($a->config["system"]["db_log"], datetime_convert()."\t".$duration."\t".
|
|
|
|
basename($backtrace[1]["file"])."\t".
|
|
|
|
$backtrace[1]["line"]."\t".$backtrace[2]["function"]."\t".
|
|
|
|
substr($sql, 0, 2000)."\n", FILE_APPEND);
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($intErrorCode) {
|
|
|
|
$this->error = $intErrorCode;
|
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (strlen($this->error)) {
|
2014-04-28 23:55:47 +02:00
|
|
|
logger('dba: ' . $this->error);
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($this->debug) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$mesg = '';
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($result === false) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$mesg = 'false';
|
2017-04-04 19:47:32 +02:00
|
|
|
} elseif ($result === true) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$mesg = 'true';
|
2017-04-04 19:47:32 +02:00
|
|
|
} else {
|
|
|
|
/// @TODO this needs fixing, but is a bug itself
|
|
|
|
// $mesg = mysql_num_rows($result) . ' results' . EOL;
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg
|
|
|
|
. (($this->error) ? ' error: ' . $this->error : '')
|
|
|
|
. EOL;
|
|
|
|
|
|
|
|
logger('dba: ' . $str );
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
/*
|
2014-04-28 23:55:47 +02:00
|
|
|
* If dbfail.out exists, we will write any failed calls directly to it,
|
|
|
|
* regardless of any logging that may or may nor be in effect.
|
|
|
|
* These usually indicate SQL syntax errors that need to be resolved.
|
|
|
|
*/
|
2017-04-04 19:47:32 +02:00
|
|
|
if (isset($result) AND ($result === false)) {
|
2014-04-28 23:55:47 +02:00
|
|
|
logger('dba: ' . printable($sql) . ' returned false.' . "\n" . $this->error);
|
2017-04-04 19:47:32 +02:00
|
|
|
if (file_exists('dbfail.out')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND);
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (isset($result) AND (($result === true) || ($result === false))) {
|
2014-04-28 23:55:47 +02:00
|
|
|
return $result;
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2014-04-28 23:55:47 +02:00
|
|
|
if ($onlyquery) {
|
2017-04-04 19:47:32 +02:00
|
|
|
// this will store an PDOStatement Object in result
|
|
|
|
$this->result = $r;
|
|
|
|
|
|
|
|
// execute the Statement, to get its result
|
|
|
|
$this->result->execute();
|
2014-04-28 23:55:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2014-04-28 23:55:47 +02:00
|
|
|
//$a->save_timestamp($stamp1, "database");
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($this->debug) {
|
2014-04-28 23:55:47 +02:00
|
|
|
logger('dba: ' . printable(print_r($r, true)));
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $r;
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function qfetch() {
|
2017-04-04 19:47:32 +02:00
|
|
|
if (false === $this->result) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-09 20:43:06 +02:00
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
return $this->result->fetch();
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
2017-05-11 17:53:04 +02:00
|
|
|
|
2014-04-28 23:55:47 +02:00
|
|
|
public function qclose() {
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($this->result) {
|
|
|
|
return $this->result->closeCursor();
|
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dbg($dbg) {
|
|
|
|
$this->debug = $dbg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function escape($str) {
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($this->db && $this->connected) {
|
2017-04-04 19:47:32 +02:00
|
|
|
$strQuoted = $this->db->quote($str);
|
|
|
|
/*
|
|
|
|
* this workaround is needed, because quote creates "'" and the beginning and the end
|
|
|
|
* of the string, which is correct. but until now the queries set this delimiter manually,
|
|
|
|
* so we must remove them from here and wait until everything uses prepared statements
|
|
|
|
*/
|
|
|
|
return mb_substr($strQuoted, 1, mb_strlen($strQuoted) - 2);
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
public function __destruct() {
|
|
|
|
if ($this->db) {
|
|
|
|
DDDBL\disconnect();
|
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! function_exists('printable')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
function printable($s) {
|
|
|
|
$s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~",".", $s);
|
|
|
|
$s = str_replace("\x00",'.',$s);
|
2017-04-04 19:47:32 +02:00
|
|
|
if (x($_SERVER,'SERVER_NAME'))
|
2014-04-28 23:55:47 +02:00
|
|
|
$s = escape_tags($s);
|
|
|
|
return $s;
|
|
|
|
}}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
// --- Procedural functions ---
|
|
|
|
|
2017-05-11 17:53:04 +02:00
|
|
|
if (! function_exists('dbg')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
function dbg($state) {
|
|
|
|
global $db;
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($db)
|
2014-04-28 23:55:47 +02:00
|
|
|
$db->dbg($state);
|
|
|
|
}}
|
|
|
|
|
2017-05-11 17:53:04 +02:00
|
|
|
if (! function_exists('dbesc')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
function dbesc($str) {
|
|
|
|
global $db;
|
2017-04-04 19:47:32 +02:00
|
|
|
|
|
|
|
if ($db && $db->connected) {
|
|
|
|
return $db->escape($str);
|
|
|
|
} else {
|
|
|
|
return str_replace("'","\\'",$str);
|
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
}}
|
|
|
|
|
2017-05-11 17:53:04 +02:00
|
|
|
if (! function_exists('q')) {
|
2017-04-04 19:47:32 +02:00
|
|
|
/*
|
2017-05-16 17:54:45 +02:00
|
|
|
* Function: q($sql,$args);
|
|
|
|
* Description: execute SQL query with printf style args.
|
|
|
|
* Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
|
2017-04-04 19:47:32 +02:00
|
|
|
* 'user', 1);
|
2017-05-16 17:54:45 +02:00
|
|
|
*/
|
2014-04-28 23:55:47 +02:00
|
|
|
function q($sql) {
|
|
|
|
|
|
|
|
global $db;
|
|
|
|
$args = func_get_args();
|
|
|
|
unset($args[0]);
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($db && $db->connected) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$stmt = @vsprintf($sql,$args); // Disabled warnings
|
|
|
|
//logger("dba: q: $stmt", LOGGER_ALL);
|
2017-05-16 17:54:45 +02:00
|
|
|
if ($stmt === false) {
|
2014-04-28 23:55:47 +02:00
|
|
|
logger('dba: vsprintf error: ' . print_r(debug_backtrace(),true), LOGGER_DEBUG);
|
2017-05-16 17:54:45 +02:00
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
return $db->q($stmt);
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
/*
|
2017-05-11 17:53:04 +02:00
|
|
|
* This will happen occasionally trying to store the
|
|
|
|
* session data after abnormal program termination
|
2014-04-28 23:55:47 +02:00
|
|
|
*/
|
|
|
|
logger('dba: no database: ' . print_r($args,true));
|
2017-05-11 17:53:04 +02:00
|
|
|
return false;
|
2014-04-28 23:55:47 +02:00
|
|
|
}}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! function_exists('dbq')) {
|
2017-04-04 19:47:32 +02:00
|
|
|
/*
|
2014-04-28 23:55:47 +02:00
|
|
|
* Raw db query, no arguments
|
|
|
|
*/
|
|
|
|
function dbq($sql) {
|
|
|
|
global $db;
|
2017-04-04 19:47:32 +02:00
|
|
|
if ($db && $db->connected) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$ret = $db->q($sql);
|
2017-04-04 19:47:32 +02:00
|
|
|
} else {
|
2014-04-28 23:55:47 +02:00
|
|
|
$ret = false;
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
return $ret;
|
|
|
|
}}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! function_exists('dbesc_array_cb')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
function dbesc_array_cb(&$item, $key) {
|
2017-04-04 19:47:32 +02:00
|
|
|
/*
|
|
|
|
* Caller is responsible for ensuring that any integer arguments to
|
|
|
|
* dbesc_array are actually integers and not malformed strings containing
|
|
|
|
* SQL injection vectors. All integer array elements should be specifically
|
|
|
|
* cast to int to avoid trouble.
|
|
|
|
*/
|
2017-04-04 19:47:32 +02:00
|
|
|
if (is_string($item)) {
|
2014-04-28 23:55:47 +02:00
|
|
|
$item = dbesc($item);
|
2017-04-04 19:47:32 +02:00
|
|
|
}
|
2014-04-28 23:55:47 +02:00
|
|
|
}}
|
|
|
|
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! function_exists('dbesc_array')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
function dbesc_array(&$arr) {
|
2017-04-04 19:47:32 +02:00
|
|
|
if (is_array($arr) && count($arr)) {
|
2014-04-28 23:55:47 +02:00
|
|
|
array_walk($arr,'dbesc_array_cb');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2017-04-04 19:47:32 +02:00
|
|
|
if (! function_exists('dba_timer')) {
|
2014-04-28 23:55:47 +02:00
|
|
|
function dba_timer() {
|
2017-04-04 19:47:32 +02:00
|
|
|
return microtime(true);
|
2014-04-28 23:55:47 +02:00
|
|
|
}}
|