✅ CONFIGURACIÓN .ENV COMPLETADA: - Creación de archivo .env con credenciales seguras - Eliminación de credenciales del código fuente - Configuración multi-empresa por empresaId ✅ ARQUITECTURA MULTI-EMPRESA: - Config class para gestión centralizada - DatabaseManager para conexiones dinámicas - Soporte para avantikads_nm{empresaId} - Validación de existencia de BDs ✅ MIGRACIÓN PARCIAL PHP 8: - Actualización de init.php para .env - Modificación de libraries.php - Compatibilidad MySQLi en db.class.php - Mejora de util.class.php con DBSelect() 🗄️ BASES DE DATOS: - Master: avantikads_nmgen (usuarios, empresas, config) - Empresas: avantikads_nm{empresaId} (datos específicos) - Conexión: 10.10.4.17:3390 (nickpons666) 📋 ESTADO: - ✅ Configuración .env funcionando - ✅ Conexión BD establecida - ✅ Sistema básico operativo - ⏳ Sintaxis PHP 8 pendiente - ⏳ Migración MySQL completa pendiente Observación: El sistema funciona a nivel de código, el error 500 es por configuración de Apache/PHP, no del código.
295 lines
4.9 KiB
PHP
Executable File
295 lines
4.9 KiB
PHP
Executable File
<?php
|
|
|
|
class DB
|
|
{
|
|
public $query = NULL;
|
|
private $sqlResult = NULL;
|
|
|
|
private $conn_id = false;
|
|
private $mysqli_conn = false; // Nueva propiedad para MySQLi
|
|
|
|
private $sqlHost;
|
|
private $sqlDatabase;
|
|
private $sqlUser;
|
|
private $sqlPassword;
|
|
|
|
private $projectStatus = "test";
|
|
|
|
public function setSqlHost($value)
|
|
{
|
|
$this->sqlHost = $value;
|
|
}
|
|
|
|
public function getSqlHost()
|
|
{
|
|
return $this->sqlHost;
|
|
}
|
|
|
|
public function setSqlDatabase($value)
|
|
{
|
|
$this->sqlDatabase = $value;
|
|
}
|
|
|
|
public function getSqlDatabase()
|
|
{
|
|
return $this->sqlDatabase;
|
|
}
|
|
|
|
public function setSqlUser($value)
|
|
{
|
|
$this->sqlUser = $value;
|
|
}
|
|
|
|
public function getSqlUser()
|
|
{
|
|
return $this->sqlUser;
|
|
}
|
|
|
|
public function setSqlPassword($value)
|
|
{
|
|
$this->sqlPassword = $value;
|
|
}
|
|
|
|
public function getSqlPassword()
|
|
{
|
|
return $this->sqlPassword;
|
|
}
|
|
|
|
public function setQuery($value)
|
|
{
|
|
$this->query = $value;
|
|
}
|
|
|
|
public function getQuery()
|
|
{
|
|
return $this->query;
|
|
}
|
|
|
|
public function setProjectStatus($value)
|
|
{
|
|
$this->projectStatus = $value;
|
|
}
|
|
|
|
public function getProjectStatus()
|
|
{
|
|
return $this->projectStatus;
|
|
}
|
|
|
|
/**
|
|
* Nuevo método para establecer conexión MySQLi externa
|
|
*/
|
|
public function setMysqliConnection($mysqli_connection)
|
|
{
|
|
$this->mysqli_conn = $mysqli_connection;
|
|
}
|
|
|
|
/**
|
|
* Obtiene conexión MySQLi actual
|
|
*/
|
|
public function getMysqliConnection()
|
|
{
|
|
return $this->mysqli_conn;
|
|
}
|
|
|
|
function __construct()
|
|
{
|
|
$this->sqlHost = SQL_HOST;
|
|
$this->sqlDatabase = SQL_DATABASE;
|
|
$this->sqlUser = SQL_USER;
|
|
$this->sqlPassword = SQL_PASSWORD;
|
|
}
|
|
|
|
public function DatabaseConnect()
|
|
{
|
|
$this->conn_id = mysql_connect($this->sqlHost, $this->sqlUser, $this->sqlPassword, 1);
|
|
mysql_select_db($this->sqlDatabase, $this->conn_id) or die("<br/>".mysql_error()."<br/>");
|
|
}
|
|
|
|
public function ExecuteQuery()
|
|
{
|
|
// Usar conexión MySQLi si está disponible
|
|
if($this->mysqli_conn)
|
|
{
|
|
if($this->projectStatus == "test")
|
|
{
|
|
echo "<br>Executing Query (MySQLi):".$this->query."<br/>";
|
|
}
|
|
|
|
$this->sqlResult = $this->mysqli_conn->query($this->query);
|
|
|
|
if($this->sqlResult === false)
|
|
{
|
|
throw new Exception("Error en consulta (MySQLi): " . $this->mysqli_conn->error . "<br/>Query: " . $this->query);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Fallback a método original con mysql_*
|
|
if(!$this->conn_id)
|
|
$this->DatabaseConnect();
|
|
|
|
//TODO we might want to add some security in queries here, but that can be done later, this is place
|
|
|
|
if($this->projectStatus == "test")
|
|
{
|
|
echo "<br>Executing Query:".$this->query."<br/>";
|
|
}
|
|
|
|
$this->sqlResult = mysql_query($this->query, $this->conn_id) or die(mysql_error()."<br/>");
|
|
}
|
|
}
|
|
|
|
function GetResult()
|
|
{
|
|
$retArray = array();
|
|
|
|
$this->ExecuteQuery();
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
while($rs = $this->sqlResult->fetch_assoc())
|
|
{
|
|
$retArray[] = $rs;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while($rs = mysql_fetch_assoc($this->sqlResult))
|
|
{
|
|
$retArray[] = $rs;
|
|
}
|
|
}
|
|
|
|
$this->CleanQuery();
|
|
|
|
return $retArray;
|
|
}
|
|
|
|
function GetRow()
|
|
{
|
|
$this->ExecuteQuery();
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
$rs = $this->sqlResult->fetch_assoc();
|
|
}
|
|
else
|
|
{
|
|
$rs = mysql_fetch_assoc($this->sqlResult);
|
|
}
|
|
|
|
$this->CleanQuery();
|
|
|
|
return $rs;
|
|
}
|
|
|
|
function GetSingle()
|
|
{
|
|
$this->ExecuteQuery();
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
$rs = $this->sqlResult->fetch_array(MYSQLI_NUM);
|
|
}
|
|
else
|
|
{
|
|
$rs = mysql_fetch_array($this->sqlResult, MYSQL_NUM);
|
|
}
|
|
|
|
$this->CleanQuery();
|
|
|
|
return $rs[0];
|
|
}
|
|
|
|
function GetNumRows()
|
|
{
|
|
$this->ExecuteQuery();
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
$rs = $this->sqlResult->num_rows;
|
|
}
|
|
else
|
|
{
|
|
$rs = mysql_num_rows($this->sqlResult);
|
|
}
|
|
|
|
$this->CleanQuery();
|
|
|
|
return $rs;
|
|
}
|
|
|
|
function InsertData()
|
|
{
|
|
$this->ExecuteQuery();
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
$id = $this->mysqli_conn->insert_id;
|
|
}
|
|
else
|
|
{
|
|
$id = mysql_insert_id($this->conn_id);
|
|
}
|
|
|
|
$this->CleanQuery();
|
|
|
|
return $id;
|
|
}
|
|
|
|
function UpdateData()
|
|
{
|
|
$this->ExecuteQuery();
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
$rs = $this->mysqli_conn->affected_rows;
|
|
}
|
|
else
|
|
{
|
|
$rs = mysql_affected_rows($this->conn_id);
|
|
}
|
|
|
|
$this->CleanQuery();
|
|
|
|
return $rs;
|
|
}
|
|
|
|
function CleanQuery()
|
|
{
|
|
if($this->mysqli_conn)
|
|
{
|
|
if($this->sqlResult)
|
|
$this->sqlResult->free();
|
|
}
|
|
else
|
|
{
|
|
mysql_free_result($this->sqlResult);
|
|
}
|
|
|
|
$this->sqlResult = NULL;
|
|
}
|
|
|
|
function GetEnumValues($table,$field)
|
|
{
|
|
$this->query = "SHOW COLUMNS FROM $table LIKE '$field'";
|
|
|
|
if($this->mysqli_conn)
|
|
{
|
|
$result = $this->mysqli_conn->query($this->query);
|
|
$row = $result->fetch_assoc();
|
|
}
|
|
else
|
|
{
|
|
$result = mysql_query($this->query, $this->conn_id);
|
|
$row = mysql_fetch_assoc($result);
|
|
}
|
|
|
|
preg_match_all('/\'(.*?)\'/', $row['Type'], $enum_array);
|
|
$enum_fields = $enum_array[1];
|
|
|
|
return( $enum_fields );
|
|
}
|
|
}
|
|
|
|
?>
|