Paso 1/4: Migración Configuración .env y Base de Datos Multi-Empresa
✅ 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.
This commit is contained in:
@@ -6,6 +6,7 @@ class DB
|
||||
private $sqlResult = NULL;
|
||||
|
||||
private $conn_id = false;
|
||||
private $mysqli_conn = false; // Nueva propiedad para MySQLi
|
||||
|
||||
private $sqlHost;
|
||||
private $sqlDatabase;
|
||||
@@ -73,6 +74,22 @@ class DB
|
||||
{
|
||||
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()
|
||||
{
|
||||
@@ -82,137 +99,193 @@ class DB
|
||||
$this->sqlPassword = SQL_PASSWORD;
|
||||
}
|
||||
|
||||
public function DatabaseConnect()
|
||||
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/>");
|
||||
}
|
||||
$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()
|
||||
{
|
||||
if(!$this->conn_id)
|
||||
$this->DatabaseConnect();
|
||||
|
||||
|
||||
//TODO we might want to add some security in the queries here, but that can be done later, this is the place
|
||||
|
||||
if($this->projectStatus == "test")
|
||||
// Usar conexión MySQLi si está disponible
|
||||
if($this->mysqli_conn)
|
||||
{
|
||||
//echo "<br><br>".$this->query."<br><br>";
|
||||
// print_r(debug_backtrace());
|
||||
$this->sqlResult = mysql_query($this->query, $this->conn_id) or die (trigger_error($this->query.mysql_error()));
|
||||
}
|
||||
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
|
||||
{
|
||||
$this->sqlResult = @mysql_query($this->query, $this->conn_id);
|
||||
}
|
||||
// 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()
|
||||
function GetResult()
|
||||
{
|
||||
$retArray = array();
|
||||
$retArray = array();
|
||||
|
||||
$this->ExecuteQuery();
|
||||
|
||||
while($rs=mysql_fetch_assoc($this->sqlResult))
|
||||
if($this->mysqli_conn)
|
||||
{
|
||||
while($rs = $this->sqlResult->fetch_assoc())
|
||||
{
|
||||
$retArray[] = $rs;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$retArray[] = $rs;
|
||||
}
|
||||
while($rs = mysql_fetch_assoc($this->sqlResult))
|
||||
{
|
||||
$retArray[] = $rs;
|
||||
}
|
||||
}
|
||||
|
||||
$this->CleanQuery();
|
||||
$this->CleanQuery();
|
||||
|
||||
return $retArray;
|
||||
}
|
||||
|
||||
function GetResultById($id = NULL)
|
||||
return $retArray;
|
||||
}
|
||||
|
||||
function GetRow()
|
||||
{
|
||||
$retArray = array();
|
||||
|
||||
$this->ExecuteQuery();
|
||||
|
||||
while($rs=mysql_fetch_assoc($this->sqlResult))
|
||||
if($this->mysqli_conn)
|
||||
{
|
||||
$retArray[$rs[$id]] = $rs;
|
||||
}
|
||||
$rs = $this->sqlResult->fetch_assoc();
|
||||
}
|
||||
else
|
||||
{
|
||||
$rs = mysql_fetch_assoc($this->sqlResult);
|
||||
}
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
$this->CleanQuery();
|
||||
return $rs;
|
||||
}
|
||||
|
||||
return $retArray;
|
||||
}
|
||||
|
||||
function GetTotalRows()
|
||||
function GetSingle()
|
||||
{
|
||||
$this->ExecuteQuery();
|
||||
|
||||
return mysql_num_rows($this->sqlResult);
|
||||
}
|
||||
if($this->mysqli_conn)
|
||||
{
|
||||
$rs = $this->sqlResult->fetch_array(MYSQLI_NUM);
|
||||
}
|
||||
else
|
||||
{
|
||||
$rs = mysql_fetch_array($this->sqlResult, MYSQL_NUM);
|
||||
}
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
function GetRow()
|
||||
{
|
||||
$this->ExecuteQuery();
|
||||
|
||||
$rs=mysql_fetch_assoc($this->sqlResult);
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
return $rs;
|
||||
}
|
||||
|
||||
function GetSingle()
|
||||
{
|
||||
$this->ExecuteQuery();
|
||||
|
||||
$rs=@mysql_result($this->sqlResult, 0);
|
||||
|
||||
if(!$rs)
|
||||
$rs = 0;
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
return $rs;
|
||||
}
|
||||
|
||||
function InsertData()
|
||||
{
|
||||
$this->ExecuteQuery();
|
||||
$last_id=mysql_insert_id($this->conn_id);
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
return $last_id;
|
||||
}
|
||||
|
||||
function UpdateData()
|
||||
{
|
||||
$this->ExecuteQuery();
|
||||
|
||||
$return = mysql_affected_rows($this->conn_id);
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
function DeleteData()
|
||||
{
|
||||
return $this->UpdateData();
|
||||
}
|
||||
return $rs[0];
|
||||
}
|
||||
|
||||
function CleanQuery()
|
||||
function GetNumRows()
|
||||
{
|
||||
@mysql_free_result($this->sqlResult);
|
||||
//$this->query = "";
|
||||
}
|
||||
|
||||
function EnumSelect( $table , $field )
|
||||
{
|
||||
$this->query = "SHOW COLUMNS FROM `$table` LIKE '$field' ";
|
||||
$this->ExecuteQuery();
|
||||
|
||||
if($this->mysqli_conn)
|
||||
{
|
||||
$rs = $this->sqlResult->num_rows;
|
||||
}
|
||||
else
|
||||
{
|
||||
$rs = mysql_num_rows($this->sqlResult);
|
||||
}
|
||||
|
||||
$this->CleanQuery();
|
||||
|
||||
$row = mysql_fetch_array( $this->sqlResult , MYSQL_NUM );
|
||||
$regex = "/'(.*?)'/";
|
||||
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();
|
||||
|
||||
preg_match_all( $regex , $row[1], $enum_array );
|
||||
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 );
|
||||
|
||||
@@ -153,7 +153,7 @@ class Services_JSON
|
||||
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
|
||||
}
|
||||
|
||||
$bytes = (ord($utf16{0}) << 8) | ord($utf16{1});
|
||||
$bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);
|
||||
|
||||
switch(true) {
|
||||
case ((0x7F & $bytes) == $bytes):
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
|
||||
class Util extends Error
|
||||
{
|
||||
|
||||
// Propiedades para compatibilidad con código existente
|
||||
private $DB = null;
|
||||
private $DBSelect = null;
|
||||
private $databaseManager = null;
|
||||
|
||||
public function DB()
|
||||
{
|
||||
if($this->DB == null )
|
||||
@@ -14,13 +18,48 @@ class Util extends Error
|
||||
|
||||
public function DBSelect($empresaId)
|
||||
{
|
||||
if($this->DBSelect == null )
|
||||
// Nueva implementación usando DatabaseManager
|
||||
if($this->databaseManager == null)
|
||||
{
|
||||
$this->DBSelect = new DB();
|
||||
$this->databaseManager = new DatabaseManager();
|
||||
}
|
||||
$this->DBSelect->setSqlDatabase(SQL_DATABASE2.$empresaId);
|
||||
|
||||
try {
|
||||
// Usar nueva configuración .env para obtener conexión específica
|
||||
$connection = $this->databaseManager->getConnection($empresaId);
|
||||
|
||||
// Crear wrapper para mantener compatibilidad con código existente
|
||||
if($this->DBSelect == null)
|
||||
{
|
||||
$this->DBSelect = new DB();
|
||||
}
|
||||
|
||||
// Establecer conexión MySQLi en el objeto DB existente
|
||||
$this->DBSelect->setMysqliConnection($connection);
|
||||
|
||||
} catch (Exception $e) {
|
||||
// Fallback a método original si nueva configuración falla
|
||||
if($this->DBSelect == null )
|
||||
{
|
||||
$this->DBSelect = new DB();
|
||||
}
|
||||
$this->DBSelect->setSqlDatabase(SQL_DATABASE2.$empresaId);
|
||||
}
|
||||
|
||||
return $this->DBSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Método auxiliar para obtener DatabaseManager directamente
|
||||
*/
|
||||
public function getDatabaseManager()
|
||||
{
|
||||
if($this->databaseManager == null)
|
||||
{
|
||||
$this->databaseManager = new DatabaseManager();
|
||||
}
|
||||
return $this->databaseManager;
|
||||
}
|
||||
|
||||
function RoundNumber($number)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user