*
  • PANIC 0
  • *
  • ERROR 1
  • *
  • WARNING 2
  • *
  • NOTICE 3
  • *
  • DEBUG_USER 4
  • *
  • DEBUG_IGEP 5
  • * * * @version $Id: IgepDebug.php,v 1.39 2010-05-26 10:30:54 afelixf Exp $ * @author Toni * @author Vero * @author David * @package gvHIDRA */ require_once "igep_utils/DBAL/gvHidraDBAL.php"; use gvHidraDBAL\gvHidraDBAL; class IgepDebug { public static $nombreTabla = 'tcmn_errlog'; public static function setDebug ($tipo, $mensaje, $dsn=null) { if (!is_numeric($tipo)) { throw new Exception('IgepDebug: el tipo de mensaje no está definido: '.$tipo); } $conf = ConfigFramework::getConfig(); $debug = $conf->getLogStatus(); //Comprobamos si tenemos que insertar if($tipo>=$debug) { return; } //Obtenemos DSN if (empty($dsn)) { $dsn = $conf->getDSNLog(); } //Obtenemos los datos $aplicacion = IgepSession::dameAplicacion(); $modulo = ''; $version = $conf->getAppVersion(); if (strlen($version) > 10) { $version = substr($version,-10); } $usuario = IgepSession::dameUsuario(); if (empty($usuario)) { $usuario = $_SERVER['REMOTE_ADDR']; if (empty($usuario)) { $usuario = 'UNKNOWN'; } } //reemplazamos caracter octal 0 con string \000 (relacionados con serialización de objetos) $mensaje = str_replace("\000",'\\000', $mensaje);//REVIEW: Coste //Realizamos la insercion try { IgepDebug::_setDB($tipo, $mensaje, $aplicacion, $modulo, $version, $usuario, $dsn); } catch (Exception $e) { error_log('Error en debug ('.$e->getMessage().') al intentar registrar: '.$mensaje); } } public static function _setDB($tipo, $mensaje, $aplicacion, $modulo, $version, $usuario, $dsn_log) { // variable static para controlar el acceso exclusivo al metodo, y avitar asi bucles // infinitos provocados por los propios errores dentro del metodo static $conexion = null; static $ins_prepared = null; $nombreTabla = self::$nombreTabla; if(empty($dsn_log)) { throw new Exception (__CLASS__.'::'.__METHOD__.': Error DSN vacío'); } $driver = gvHidraDBAL::MDB2_DRIVER; if (mb_strtoupper(trim($dsn_log['driver']), 'UTF-8') == 'PDO') { $driver = gvHidraDBAL::PDO_DRIVER; } //$tipo, $mensaje, $aplicacion, $modulo, $version, $usuario $vTypesInsert = array ( 'aplicacion' => gvHidraDBAL::T_STRING, 'version' => gvHidraDBAL::T_STRING, 'usuario' => gvHidraDBAL::T_STRING, 'tipo' => gvHidraDBAL::T_INTEGER, 'mensaje' => gvHidraDBAL::T_CLOB ); $vValuesInsert = array ( 'aplicacion' => $aplicacion, 'version' => $version, 'usuario' => $usuario, 'tipo' => $tipo, 'mensaje' => $mensaje ); $dbms = gvHidraDBAL::getNormalizedSGBDType($dsn_log); if ($dbms == gvHidraDBAL::SGBD_MYSQL) { $sqlIdError = 'null'; $sqlFechaActual = 'SYSDATE()'; } elseif ($dbms == gvHidraDBAL::SGBD_ORACLE) { $sqlIdError = '(scmn_id_errlog.nextval)'; $sqlFechaActual = 'CURRENT_TIMESTAMP'; } elseif ($dbms == gvHidraDBAL::SGBD_POSTGRESQL) { $sqlIdError = "(nextval('scmn_id_errlog'))"; $sqlFechaActual = 'CURRENT_TIMESTAMP'; } elseif ($dbms == gvHidraDBAL::SGBD_SQLITE) { $sqlIdError = 'null';//Aplica un RowId automático $sqlFechaActual = "STRFTIME('%Y/%m/%d %H:%M:%f', 'NOW', 'localtime')"; try { $dsn_log['xmlhost'] = self::buildSQLiteResource($dsn_log); } catch (Exception $e) { $msg = 'Error con el uso de SQLite para el DEBUG: '.$e->getMessage(); throw new Exception($msg); } } else { $dsn_log['username'] = ''; $dsn_log['password'] = ''; $msg = 'El tipo de conexión no está soportado por gvHidraDBAL: '.print_r($dsn_log, true); throw new Exception($msg); } try { $codificacion = 'UTF-8'; if (isset($dsn_log['xmlcharset'])) { $codificacion = $dsn_log['xmlcharset']; } $conexion = gvHidraDBAL::makeConnection($dsn_log, $codificacion, $driver); //$conexion = new gvHidraDBAL($dsn_log, $codificacion, $driver); } catch (Exception $e) { $mensaje = __CLASS__.'::'.__METHOD__.': Error de conexión al debug, desactivelo (pase a LOG_NONE) o corrija el problema.'; $mensaje.=' Posiblemente se trata de un error en los parámetros de conexión. Descrición: '.$e->getMessage(); throw new Exception(); } if (empty($ins_prepared)) { $ins_prepared =<<prepare($ins_prepared, $vTypesInsert); } catch (Exception $e) { $mensaje = __CLASS__.'::'.__METHOD__.': Error en la preparación de la SQL para inserción del DEBUG: '.$e->getMessage(); error_log($mensaje);die; throw new Exception($mensaje); } try { $conexion->execute($vValuesInsert); } catch (Exception $e) { $mensaje = __CLASS__.'::'.__METHOD__.': Error en la ejecución de la SQL para inserción del DEBUG: '.$e->getMessage(); error_log($mensaje);die; throw new Exception($mensaje); } }//_setDB /** * Elimina los LOGS de más de $dias en tablas para el $usuario especificado (o todos si es vacío) * @param integer $dias * @param string $usuario * @return */ public static function purgeDBLog($dias=30, $usuario = null, $dsn_log = null) { if (empty($dsn_log)) { $conf = ConfigFramework::getConfig(); $dsn_log = $conf->getDSNLog(); } $dbms = gvHidraDBAL::getNormalizedSGBDType($dsn_log); if ($dbms == gvHidraDBAL::SGBD_SQLITE) { try { $dsn_log['xmlOrig'] = $dsn_log['xmlhost']; $dsn_log['xmlhost'] = IgepDebug::getSqlitePDOResource($dsn_log, $usuario); $usuario = '%'; } catch (Exception $e) { $msg = 'Error al purgar BD SQLite: '.$e->getMessage(); throw new Exception($msg); } } if (empty($usuario)) { $usuario = '%'; } $codificacion = 'UTF-8'; if (isset($dsn_log['xmlcharset'])) { $codificacion = $dsn_log['xmlcharset']; } $driver = gvHidraDBAL::MDB2_DRIVER; if (mb_strtoupper(trim($dsn_log['driver']), 'UTF-8') == 'PDO') { $driver = gvHidraDBAL::PDO_DRIVER; } try { //$conDBAL = new gvHidraDBAL($dsn_log, $codificacion, $driver); $conDBAL = gvHidraDBAL::makeConnection($dsn_log, $codificacion, $driver); } catch (Exception $e) { $msg = __CLASS__.':'.__METHOD__.': Error al conectar al DEBUG para purga: '.$e->getMessage(); throw new Exception($msg); } $nombreTable = self::$nombreTabla; $formatSGBDenPHP = $conDBAL->getMask('timestamp','php'); $fechaBD = date($formatSGBDenPHP, strtotime("-$dias minute"));//days $sql = "DELETE FROM $nombreTable WHERE iderror > 1 AND usuario like '$usuario' AND fecha < '$fechaBD'"; try { $conDBAL->prepare($sql); $conDBAL->execute(); } catch (Exception $e) { $msg = __CLASS__.':'.__METHOD__.': Error al ejecutar la purga: '.$e->getMessage(); throw new Exception($msg); } }//Fin public static function getSqlitePDOResource($dsnLog, $usuario = null) { if (mb_strtolower(trim($dsnLog['driver']), 'UTF-8') == 'mdb2') { $msg = 'No es posible utilizar MDB2 para conectar a SQLite. Utilice PDO.'; throw new Exception($msg); } if (empty($usuario)) { $usuario = IgepSession::dameUsuario(); } $parte = pathinfo($dsnLog['xmlhost']); $dir = isset($parte['dirname'])?$parte['dirname']:'./'; $sufijo = isset($parte['filename'])?$parte['filename']:'_DBIgepDebug'; $extension = isset($parte['extension'])?('.'.$parte['extension']):'.sqlite3'; $ficheroDB = $usuario.$sufijo.$extension; $recurso = realpath($dir).DIRECTORY_SEPARATOR.$ficheroDB; if (!file_exists($recurso)) { $msg = 'No existe la BD de LOG para para: '.$usuario; error_log(__CLASS__.__METHOD__.':: '.$msg); throw new \Exception ($msg); } return $recurso; }//Fin /** * Crea la BD si no existe y devuelve la ruta a la misma */ private static function buildSQLiteResource($dsnLog) { //Obtenemos los datos $aplicacion = IgepSession::dameAplicacion(); $usuario = IgepSession::dameUsuario(); if (empty($usuario)) { $usuario = 'UNKNOWN'; } $tipo = DEBUG_IGEP; $mensaje = "Create SQLite BD LOG para $usuario"; $recurso = null; $parte = pathinfo($dsnLog['xmlhost']); $ruta = isset($parte['dirname'])?$parte['dirname']:'.'; $sufijo = isset($parte['filename'])?$parte['filename']:'DBIgepDebug_'; $extension = isset($parte['extension'])?('.'.$parte['extension']):'.sqlite3'; $ficheroDB = $usuario.$sufijo.$extension; if (file_exists($ruta.DIRECTORY_SEPARATOR.$ficheroDB)) { $recurso = realpath($ruta).DIRECTORY_SEPARATOR.$ficheroDB; return ($recurso); } else { if (!is_dir($ruta)) { if (!mkdir($ruta, 700, true)) { $msg = 'No pudo crearse el directorio: '. $ruta; throw new Exception($msg); } $ruta = realpath($ruta); } try { $recurso = $ruta.DIRECTORY_SEPARATOR.$ficheroDB; //Creamos el fichero físico al conectar $connectionString = 'sqlite:'.$recurso; $conDB = new PDO ($connectionString, null, null, array ( PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //Marcamos el fetchMode asociativo por defecto PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //En caso de error, se lanza excepción ) ); $ddl = <<exec($ddl); } catch (Exception $e) { $msg = 'No pudo crearse la BD de Log con SQLite: '.$e->getMessage(); throw new Exception($msg); } //1a inserción try { $insertSQL = <<exec($insertSQL); } catch (Exception $e) { $conDB = null; $msg = 'No pudo insertarse en el LOG de SQLite: '.$e->getMessage(); @unlink($ficheroDB); throw new Exception($msg); } }//Fin if/else return ($recurso); }//Fin }//Fin IgepDebug ?>