issue 171

This commit is contained in:
Mitan Omar
2020-12-25 18:18:48 +01:00
parent 625d14ffcc
commit 730c30042b
2 changed files with 85 additions and 153 deletions

View File

@@ -9,8 +9,7 @@
//no for directly open //no for directly open
if (! defined('IN_COMMON')) if (! defined('IN_COMMON')) {
{
exit(); exit();
} }
@@ -45,8 +44,7 @@ class KleejaDatabase
{ {
$port = 3306; $port = 3306;
if (strpos($host, ':') !== false) if (strpos($host, ':') !== false) {
{
$host = substr($host, 0, strpos($host, ':')); $host = substr($host, 0, strpos($host, ':'));
$port = (int) substr($host, strpos($host, ':')+1); $port = (int) substr($host, strpos($host, ':')+1);
} }
@@ -57,14 +55,12 @@ class KleejaDatabase
$this->connect_id = @mysqli_connect($host, $db_username, $db_password, $db_name, $port); $this->connect_id = @mysqli_connect($host, $db_username, $db_password, $db_name, $port);
//no error //no error
if (defined('SQL_NO_ERRORS') || defined('MYSQL_NO_ERRORS')) if (defined('SQL_NO_ERRORS') || defined('MYSQL_NO_ERRORS')) {
{
$this->show_errors = false; $this->show_errors = false;
} }
if (! $this->connect_id) if (! $this->connect_id) {
{
//loggin -> no database -> close connection //loggin -> no database -> close connection
$this->close(); $this->close();
$this->error_msg('We can not connect to the server ...'); $this->error_msg('We can not connect to the server ...');
@@ -75,10 +71,8 @@ class KleejaDatabase
kleeja_log('[Connected] : ' . kleeja_get_page()); kleeja_log('[Connected] : ' . kleeja_get_page());
if (! defined('DISABLE_MYSQL_UTF8')) if (! defined('DISABLE_MYSQL_UTF8')) {
{ if (mysqli_set_charset($this->connect_id, 'utf8')) {
if (mysqli_set_charset($this->connect_id, 'utf8'))
{
kleeja_log('[Set to UTF8] : --> '); kleeja_log('[Set to UTF8] : --> ');
} }
} }
@@ -99,22 +93,19 @@ class KleejaDatabase
// close the connection // close the connection
public function close() public function close()
{ {
if (! $this->is_connected()) if (! $this->is_connected()) {
{
return true; return true;
} }
// Commit any remaining transactions // Commit any remaining transactions
if ($this->in_transaction) if ($this->in_transaction) {
{
mysqli_commit($this->connect_id); mysqli_commit($this->connect_id);
} }
//loggin -> close connection //loggin -> close connection
kleeja_log('[Closing connection] : ' . kleeja_get_page()); kleeja_log('[Closing connection] : ' . kleeja_get_page());
if (! is_resource($this->connect_id)) if (! is_resource($this->connect_id)) {
{
return true; return true;
} }
@@ -156,8 +147,7 @@ class KleejaDatabase
public function query($query, $transaction = false) public function query($query, $transaction = false)
{ {
//no connection //no connection
if (! $this->is_connected()) if (! $this->is_connected()) {
{
return false; return false;
} }
@@ -166,16 +156,13 @@ class KleejaDatabase
// //
unset($this->result); unset($this->result);
if (! empty($query)) if (! empty($query)) {
{
//debug .. ////////////// //debug .. //////////////
$srartum_sql = get_microtime(); $srartum_sql = get_microtime();
//////////////// ////////////////
if ($transaction && ! $this->in_transaction) if ($transaction && ! $this->in_transaction) {
{ if (! mysqli_autocommit($this->connect_id, false)) {
if (! mysqli_autocommit($this->connect_id, false))
{
return false; return false;
} }
@@ -188,33 +175,24 @@ class KleejaDatabase
$this->debugr[$this->query_num+1] = [$query, sprintf('%.5f', get_microtime() - $srartum_sql)]; $this->debugr[$this->query_num+1] = [$query, sprintf('%.5f', get_microtime() - $srartum_sql)];
//////////////// ////////////////
if (! $this->result) if (! $this->result) {
{
$this->error_msg('Error In query'); $this->error_msg('Error In query');
} } else {
else
{
//let's debug it //let's debug it
kleeja_log('[Query] : --> ' . $query); kleeja_log('[Query] : --> ' . $query);
} }
} } else {
else if ($this->in_transaction) {
{
if ($this->in_transaction)
{
$this->result = mysqli_commit($this->connect_id); $this->result = mysqli_commit($this->connect_id);
} }
} }
//is there any result //is there any result
if ($this->result) if ($this->result) {
{ if ($this->in_transaction) {
if ($this->in_transaction)
{
$this->in_transaction = false; $this->in_transaction = false;
if (! mysqli_commit($this->connect_id)) if (! mysqli_commit($this->connect_id)) {
{
mysqli_rollback($this->connect_id); mysqli_rollback($this->connect_id);
return false; return false;
} }
@@ -222,11 +200,8 @@ class KleejaDatabase
$this->query_num++; $this->query_num++;
return $this->result; return $this->result;
} } else {
else if ($this->in_transaction) {
{
if ($this->in_transaction)
{
mysqli_rollback($this->connect_id); mysqli_rollback($this->connect_id);
$this->in_transaction = false; $this->in_transaction = false;
} }
@@ -244,83 +219,62 @@ class KleejaDatabase
{ {
$sql = ''; $sql = '';
if (isset($query['SELECT']) && isset($query['FROM'])) if (isset($query['SELECT']) && isset($query['FROM'])) {
{
$sql = 'SELECT ' . $query['SELECT'] . ' FROM ' . $query['FROM']; $sql = 'SELECT ' . $query['SELECT'] . ' FROM ' . $query['FROM'];
if (isset($query['JOINS'])) if (isset($query['JOINS'])) {
{ foreach ($query['JOINS'] as $cur_join) {
foreach ($query['JOINS'] as $cur_join)
{
$sql .= ' ' . key($cur_join) . ' ' . @current($cur_join) . ' ON ' . $cur_join['ON']; $sql .= ' ' . key($cur_join) . ' ' . @current($cur_join) . ' ON ' . $cur_join['ON'];
} }
} }
if (! empty($query['WHERE'])) if (! empty($query['WHERE'])) {
{
$sql .= ' WHERE ' . $query['WHERE']; $sql .= ' WHERE ' . $query['WHERE'];
} }
if (! empty($query['GROUP BY'])) if (! empty($query['GROUP BY'])) {
{
$sql .= ' GROUP BY ' . $query['GROUP BY']; $sql .= ' GROUP BY ' . $query['GROUP BY'];
} }
if (! empty($query['HAVING'])) if (! empty($query['HAVING'])) {
{
$sql .= ' HAVING ' . $query['HAVING']; $sql .= ' HAVING ' . $query['HAVING'];
} }
if (! empty($query['ORDER BY'])) if (! empty($query['ORDER BY'])) {
{
$sql .= ' ORDER BY ' . $query['ORDER BY']; $sql .= ' ORDER BY ' . $query['ORDER BY'];
} }
if (! empty($query['LIMIT'])) if (! empty($query['LIMIT'])) {
{
$sql .= ' LIMIT ' . $query['LIMIT']; $sql .= ' LIMIT ' . $query['LIMIT'];
} }
} } elseif (isset($query['INSERT'])) {
elseif (isset($query['INSERT']))
{
$sql = 'INSERT INTO ' . $query['INTO']; $sql = 'INSERT INTO ' . $query['INTO'];
if (! empty($query['INSERT'])) if (! empty($query['INSERT'])) {
{
$sql .= ' (' . $query['INSERT'] . ')'; $sql .= ' (' . $query['INSERT'] . ')';
} }
$sql .= ' VALUES(' . $query['VALUES'] . ')'; $sql .= ' VALUES(' . $query['VALUES'] . ')';
} } elseif (isset($query['UPDATE'])) {
elseif (isset($query['UPDATE'])) if (isset($query['PARAMS']['LOW_PRIORITY'])) {
{
if (isset($query['PARAMS']['LOW_PRIORITY']))
{
$query['UPDATE'] = 'LOW_PRIORITY ' . $query['UPDATE']; $query['UPDATE'] = 'LOW_PRIORITY ' . $query['UPDATE'];
} }
$sql = 'UPDATE ' . $query['UPDATE'] . ' SET ' . $query['SET']; $sql = 'UPDATE ' . $query['UPDATE'] . ' SET ' . $query['SET'];
if (! empty($query['WHERE'])) if (! empty($query['WHERE'])) {
{
$sql .= ' WHERE ' . $query['WHERE']; $sql .= ' WHERE ' . $query['WHERE'];
} }
} } elseif (isset($query['DELETE'])) {
elseif (isset($query['DELETE']))
{
$sql = 'DELETE FROM ' . $query['DELETE']; $sql = 'DELETE FROM ' . $query['DELETE'];
if (! empty($query['WHERE'])) if (! empty($query['WHERE'])) {
{
$sql .= ' WHERE ' . $query['WHERE']; $sql .= ' WHERE ' . $query['WHERE'];
} }
} } elseif (isset($query['REPLACE'])) {
elseif (isset($query['REPLACE']))
{
$sql = 'REPLACE INTO ' . $query['INTO']; $sql = 'REPLACE INTO ' . $query['INTO'];
if (! empty($query['REPLACE'])) if (! empty($query['REPLACE'])) {
{
$sql .= ' (' . $query['REPLACE'] . ')'; $sql .= ' (' . $query['REPLACE'] . ')';
} }
@@ -338,18 +292,14 @@ class KleejaDatabase
*/ */
public function freeresult($query_id = 0) public function freeresult($query_id = 0)
{ {
if (! $query_id) if (! $query_id) {
{
$query_id = $this->result; $query_id = $this->result;
} }
if ($query_id) if ($query_id) {
{
mysqli_free_result($query_id); mysqli_free_result($query_id);
return true; return true;
} } else {
else
{
return false; return false;
} }
} }
@@ -374,8 +324,7 @@ class KleejaDatabase
*/ */
public function fetch_array($query_id = 0) public function fetch_array($query_id = 0)
{ {
if (! $query_id) if (! $query_id) {
{
$query_id = $this->result; $query_id = $this->result;
} }
@@ -390,8 +339,7 @@ class KleejaDatabase
*/ */
public function num_rows($query_id = 0) public function num_rows($query_id = 0)
{ {
if (! $query_id) if (! $query_id) {
{
$query_id = $this->result; $query_id = $this->result;
} }
@@ -430,8 +378,7 @@ class KleejaDatabase
*/ */
public function real_escape($msg) public function real_escape($msg)
{ {
if (! $this->is_connected()) if (! $this->is_connected()) {
{
return false; return false;
} }
@@ -466,8 +413,7 @@ class KleejaDatabase
*/ */
private function error_msg($msg) private function error_msg($msg)
{ {
if (! $this->show_errors || (defined('SQL_NO_ERRORS') || defined('MYSQL_NO_ERRORS'))) if (! $this->show_errors || (defined('SQL_NO_ERRORS') || defined('MYSQL_NO_ERRORS'))) {
{
kleeja_log('MySQL: ' . $msg); kleeja_log('MySQL: ' . $msg);
return false; return false;
} }
@@ -476,21 +422,20 @@ class KleejaDatabase
$error_sql = @current($this->debugr[$this->query_num+1]); $error_sql = @current($this->debugr[$this->query_num+1]);
//some ppl want hide their table names //some ppl want hide their table names
if (! defined('DEV_STAGE')) if (! defined('DEV_STAGE')) {
{ $error_sql = preg_replace_callback("#\s{1,3}`*{$this->dbprefix}([a-z0-9]+)`*\s{1,3}#", function ($m) {
$error_sql = preg_replace_callback("#\s{1,3}`*{$this->dbprefix}([a-z0-9]+)`*\s{1,3}#", function($m) {
return ' <span style="color:blue">' . substr($m[1], 0, 1) . '</span> '; return ' <span style="color:blue">' . substr($m[1], 0, 1) . '</span> ';
}, $error_sql); }, $error_sql);
$error_msg = preg_replace_callback("#{$this->dbname}.{$this->dbprefix}([a-z0-9]+)#", function($m) { $error_msg = preg_replace_callback("#{$this->dbname}.{$this->dbprefix}([a-z0-9]+)#", function ($m) {
return ' <span style="color:blue">' . substr($m[1], 0, 1) . '</span> '; return ' <span style="color:blue">' . substr($m[1], 0, 1) . '</span> ';
}, $error_msg); }, $error_msg);
$error_sql = preg_replace_callback("#\s{1,3}(from|update|into)\s{1,3}([a-z0-9]+)\s{1,3}#i", function($m) { $error_sql = preg_replace_callback("#\s{1,3}(from|update|into)\s{1,3}([a-z0-9]+)\s{1,3}#i", function ($m) {
return $m[1] . ' <span style="color:blue">' . substr($m[2], 0, 1) . '</span> '; return $m[1] . ' <span style="color:blue">' . substr($m[2], 0, 1) . '</span> ';
}, $error_sql); }, $error_sql);
$error_msg = preg_replace_callback("#\s{1,3}(from|update|into)\s{1,3}([a-z0-9]+)\s{1,3}#i", function($m) { $error_msg = preg_replace_callback("#\s{1,3}(from|update|into)\s{1,3}([a-z0-9]+)\s{1,3}#i", function ($m) {
return $m[1] . ' <span style="color:blue">' . substr($m[2], 0, 1) . '</span> '; return $m[1] . ' <span style="color:blue">' . substr($m[2], 0, 1) . '</span> ';
}, $error_msg); }, $error_msg);
$error_msg = preg_replace_callback("#\s'([^']+)'@'([^']+)'#i", function($m) { $error_msg = preg_replace_callback("#\s'([^']+)'@'([^']+)'#i", function ($m) {
return ' <span style="color:blue">hidden</span>@' . $m[2] . ' '; return ' <span style="color:blue">hidden</span>@' . $m[2] . ' ';
}, $error_msg); }, $error_msg);
$error_sql = preg_replace("#password\s*=\s*'[^']+'#i", "password='<span style=\"color:blue\">hidden</span>'", $error_sql); $error_sql = preg_replace("#password\s*=\s*'[^']+'#i", "password='<span style=\"color:blue\">hidden</span>'", $error_sql);
@@ -499,8 +444,7 @@ class KleejaDatabase
//is this error related to updating? //is this error related to updating?
$updating_related = false; $updating_related = false;
if (strpos($error_msg, 'Unknown column') !== false) if (strpos($error_msg, 'Unknown column') !== false) {
{
$updating_related = true; $updating_related = true;
} }
@@ -512,14 +456,12 @@ class KleejaDatabase
$error_message .= " <a href='#' onclick='window.location.reload( false );'>click to Refresh this page ...</a><br />"; $error_message .= " <a href='#' onclick='window.location.reload( false );'>click to Refresh this page ...</a><br />";
$error_message .= '<h2>Sorry , We encountered a MySQL error: ' . ($msg !='' ? $msg : '') . '</h2>'; $error_message .= '<h2>Sorry , We encountered a MySQL error: ' . ($msg !='' ? $msg : '') . '</h2>';
if ($error_sql != '') if ($error_sql != '') {
{
$error_message .= "<br />--[query]-------------------------- <br />$error_sql<br />---------------------------------<br /><br />"; $error_message .= "<br />--[query]-------------------------- <br />$error_sql<br />---------------------------------<br /><br />";
} }
$error_message .= "[$error_no : $error_msg] <br />"; $error_message .= "[$error_no : $error_msg] <br />";
if ($updating_related) if ($updating_related) {
{
global $config; global $config;
$error_message .= '<br /><strong>Your Kleeja database might be old, try to update it now from: ' . rtrim($config['siteurl'], '/') . '/install</strong>'; $error_message .= '<br /><strong>Your Kleeja database might be old, try to update it now from: ' . rtrim($config['siteurl'], '/') . '/install</strong>';
$error_message .= "<br /><br><strong>If this error happened after installing a plugin, add <span style=\"background-color:#ccc; padding:2px\">define('STOP_PLUGINS', true);</span> to end of config.php file.</strong>"; $error_message .= "<br /><br><strong>If this error happened after installing a plugin, add <span style=\"background-color:#ccc; padding:2px\">define('STOP_PLUGINS', true);</span> to end of config.php file.</strong>";
@@ -547,15 +489,22 @@ class KleejaDatabase
*/ */
public function get_error() public function get_error()
{ {
if ($this->is_connected()) if ($this->is_connected()) {
{
return [@mysqli_errno($this->connect_id), @mysqli_error($this->connect_id)]; return [@mysqli_errno($this->connect_id), @mysqli_error($this->connect_id)];
} } else {
else
{
return [@mysqli_connect_errno(), @mysqli_connect_error()]; return [@mysqli_connect_errno(), @mysqli_connect_error()];
} }
} }
public function showErrors()
{
$this->show_errors = true;
}
public function hideErrors()
{
$this->show_errors = false;
}
} }
endif; endif;

View File

@@ -19,8 +19,7 @@ define('IN_COMMON', true);
define('STOP_PLUGINS', true); define('STOP_PLUGINS', true);
define('PATH', '../'); define('PATH', '../');
if (file_exists(PATH . 'config.php')) if (file_exists(PATH . 'config.php')) {
{
include_once PATH . 'config.php'; include_once PATH . 'config.php';
} }
@@ -28,12 +27,9 @@ include_once PATH . 'includes/plugins.php';
include_once PATH . 'includes/functions.php'; include_once PATH . 'includes/functions.php';
include_once PATH . 'includes/functions_alternative.php'; include_once PATH . 'includes/functions_alternative.php';
if (isset($dbtype) && $dbtype == 'sqlite') if (isset($dbtype) && $dbtype == 'sqlite') {
{
include PATH . 'includes/sqlite.php'; include PATH . 'includes/sqlite.php';
} } else {
else
{
include PATH . 'includes/mysqli.php'; include PATH . 'includes/mysqli.php';
} }
@@ -48,8 +44,7 @@ $SQL = new KleejaDatabase($dbserver, $dbuser, $dbpass, $dbname, $dbprefix);
// //
$config['db_version'] = inst_get_config('db_version'); $config['db_version'] = inst_get_config('db_version');
if ($config['db_version'] == false) if ($config['db_version'] == false) {
{
$SQL->query("INSERT INTO `{$dbprefix}config` (`name` ,`value`) VALUES ('db_version', '')"); $SQL->query("INSERT INTO `{$dbprefix}config` (`name` ,`value`) VALUES ('db_version', '')");
} }
@@ -60,8 +55,7 @@ $IN_UPDATE = true;
/** /**
* print header * print header
*/ */
if (! ip('action_file_do')) if (! ip('action_file_do')) {
{
echo gettpl('header.html'); echo gettpl('header.html');
} }
@@ -69,8 +63,7 @@ if (! ip('action_file_do'))
/** /**
* Navigation .. * Navigation ..
*/ */
switch (g('step', 'str', 'action_file')) switch (g('step', 'str', 'action_file')) {
{
default: default:
case 'update_now': case 'update_now':
@@ -86,8 +79,7 @@ case 'update_now':
sort($available_db_updates); sort($available_db_updates);
if (! sizeof($available_db_updates)) if (! sizeof($available_db_updates)) {
{
$update_msgs_arr[] = '<span style="color:green;">' . $lang['INST_UPDATE_CUR_VER_IS_UP'] . '</span>'; $update_msgs_arr[] = '<span style="color:green;">' . $lang['INST_UPDATE_CUR_VER_IS_UP'] . '</span>';
$complete_update = false; $complete_update = false;
} }
@@ -95,50 +87,41 @@ case 'update_now':
// //
//is there any sqls //is there any sqls
// //
if ($complete_update) if ($complete_update) {
{
//loop through available updates //loop through available updates
foreach ($available_db_updates as $db_update_version) foreach ($available_db_updates as $db_update_version) {
{ $SQL->hideErrors();
$SQL->show_errors = false;
//sqls //sqls
if (isset($update_schema[$db_update_version]['sql']) if (isset($update_schema[$db_update_version]['sql'])
&& sizeof($update_schema[$db_update_version]['sql']) > 0) && sizeof($update_schema[$db_update_version]['sql']) > 0) {
{
$err = ''; $err = '';
$complete_update = true; $complete_update = true;
foreach ($update_schema[$db_update_version]['sql'] as $name=>$sql_content) foreach ($update_schema[$db_update_version]['sql'] as $name=>$sql_content) {
{
$err = ''; $err = '';
$SQL->query($sql_content); $SQL->query($sql_content);
$err = $SQL->get_error(); $err = $SQL->get_error();
if (strpos($err[1], 'Duplicate') !== false || $err[0] == '1062' || $err[0] == '1060') if (strpos($err[1], 'Duplicate') !== false || $err[0] == '1062' || $err[0] == '1060') {
{
$complete_update = false; $complete_update = false;
} }
} }
} }
//functions //functions
if ($complete_update) if ($complete_update) {
{ if (isset($update_schema[$db_update_version]['functions']) && sizeof($update_schema[$db_update_version]['functions']) > 0) {
if (isset($update_schema[$db_update_version]['functions']) && sizeof($update_schema[$db_update_version]['functions']) > 0) foreach ($update_schema[$db_update_version]['functions'] as $n) {
{ if (is_callable($n)) {
foreach ($update_schema[$db_update_version]['functions'] as $n)
{
if (is_callable($n))
{
$n(); $n();
} }
} }
} }
} }
$sql = "UPDATE `{$dbprefix}config` SET `value` = '" . UPDATE_DB_VERSION . "' WHERE `name` = 'db_version'"; $sql = "UPDATE `{$dbprefix}config` SET `value` = '" . KLEEJA_DB_VERSION . "' WHERE `name` = 'db_version'";
$SQL->query($sql); $SQL->query($sql);
} }
} }