<?php
if (!defined('__KIMS__')) exit;

/* PHP 8+ / MySQL 8 compatibility layer.  The legacy application calls the
 * db_* helpers below, so the rest of the code can continue to use its
 * existing query builder while the removed mysql_* extension is retired. */
function isConnectDb($db)
{
	if (!extension_loaded('mysqli')) return false;
	mysqli_report(MYSQLI_REPORT_OFF);
	$port = isset($db['port']) && (int)$db['port'] > 0 ? (int)$db['port'] : 3306;
	$conn = @mysqli_connect((string)$db['host'], (string)$db['user'], (string)$db['pass'], (string)$db['name'], $port);
	if (!$conn) return false;
	if (!mysqli_set_charset($conn, 'utf8mb4'))
	{
		mysqli_close($conn);
		return false;
	}
	/* Preserve the old application's permissive date behaviour without
	 * disabling every MySQL safety check for the server. */
	mysqli_query($conn, "SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'");
	return $conn;
}

function db_query($sql, $con)
{
	if (!$con) return false;
	return mysqli_query($con, $sql);
}

function db_fetch_array($que)
{
	return $que ? @mysqli_fetch_array($que, MYSQLI_BOTH) : false;
}

function db_fetch_assoc($que)
{
	return $que ? mysqli_fetch_assoc($que) : false;
}

function db_num_rows($que)
{
	return $que instanceof mysqli_result ? mysqli_num_rows($que) : 0;
}

function db_info()
{
	return !empty($GLOBALS['DB_CONNECT']) ? mysqli_get_server_info($GLOBALS['DB_CONNECT']) : '';
}

function db_error()
{
	return !empty($GLOBALS['DB_CONNECT']) ? mysqli_error($GLOBALS['DB_CONNECT']) : '';
}

function db_close($conn)
{
	return $conn ? mysqli_close($conn) : true;
}

function db_insert_id($conn)
{
	return $conn ? mysqli_insert_id($conn) : 0;
}

function getUidData($table,$uid)
{
	return getDbData($table,'uid='.(int)$uid,'*');
}

function getDbData($table,$where,$data)
{
	$row = db_fetch_array(getDbSelect($table,getSqlFilter($where),$data));
	return $row;
}

function getDbArray($table,$where,$data,$sort,$orderby,$recnum,$p)
{
	global $DB_CONNECT;
	$rcd = db_query('select '.$data.' from '.$table.($where?' where '.getSqlFilter($where):'').' order by '.$sort.' '.$orderby.($recnum?' limit '.(($p-1)*$recnum).', '.$recnum:''),$DB_CONNECT);
	return $rcd;
}

function getDbRows($table,$where)
{
	global $DB_CONNECT;
	$rows = db_fetch_array(db_query('select count(*) from '.$table.($where?' where '.getSqlFilter($where):''),$DB_CONNECT));
	return $rows ? $rows[0] : 0;
}

function getDbCnt($table,$type,$where)
{
	global $DB_CONNECT;
	$cnts = db_fetch_array(db_query('select '.$type.' from '.$table.($where?' where '.getSqlFilter($where):''),$DB_CONNECT));
	return $cnts ? $cnts[0] : 0;
}

function getDbSelect($table,$where,$data)
{
	global $DB_CONNECT;
	return db_query('select '.$data.' from '.$table.($where?' where '.getSqlFilter($where):''),$DB_CONNECT);
}

function getDbInsert($table,$key,$val)
{
	global $DB_CONNECT;
	db_query('insert into '.$table.' ('.$key.')values('.$val.')',$DB_CONNECT);
}

function getDbUpdate($table,$set,$where)
{
	global $DB_CONNECT;
	db_query('update '.$table.' set '.$set.($where?' where '.getSqlFilter($where):''),$DB_CONNECT);
}

function getDbDelete($table,$where)
{
	global $DB_CONNECT;
	db_query('delete from '.$table.($where?' where '.getSqlFilter($where):''),$DB_CONNECT);
}

function getSqlFilter($sql)
{
	return $sql;
}
?>
