PageRenderTime 40ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/BAM/lib/class/Db.inc.php

https://github.com/Kodomo/Dazzler
PHP | 204 lines | 164 code | 21 blank | 19 comment | 39 complexity | c3646a9fa49a2446d1252c1b06a75a3f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /* !
  3. * This file is part of Dazzler
  4. * Copyright(c) 2011 USI - Universidad de Concepcion
  5. *
  6. * Dazzler is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Dazzler is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Dazzler. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //version 1.2.2
  21. $NumeroDeConsultas = 0;
  22. $NumeroDeConexiones = 0;
  23. class DB {
  24. protected $TipoBaseDatos;
  25. protected $Identificador;
  26. public $ResultadoConsulta;
  27. public $UltimoId;
  28. public $NumeroResultados;
  29. public $Estado;
  30. public $MostrarConsultas;
  31. public $Error;
  32. public $Error_debug;
  33. function __construct($BaseDatos="MySQL",$Servidor="localhost",$Usuario="root",$Contrasena="",$Tipo = "MySQL"){
  34. global $NumeroDeConexiones;
  35. $this->Error = "";
  36. $this->Error_debug = "";
  37. $this->Estado = 1;
  38. $this->TipoBaseDatos = "";
  39. $this->ResultadoConsulta = 0;
  40. $this->NumeroResultados = 0;
  41. $this->UltimoId = 0;
  42. $this->MostrarConsultas = 0;
  43. if($Tipo == "MySQL" || (strcasecmp($Tipo, "my") == 0)){
  44. $this->TipoBaseDatos = "MySQL";
  45. $this->Identificador = @mysql_connect($Servidor,$Usuario,$Contrasena,1);
  46. if ($this->Identificador == 0){
  47. $this->Error = "Error al Conectar $Tipo ($Usuario@$Servidor => $BaseDatos)<br>";
  48. $this->Error_debug = "Error al Conectar $Tipo ($Usuario:$Contrasena@$Servidor => $BaseDatos)<br>";
  49. $this->Estado = 0;
  50. }
  51. else{
  52. $bit = @mysql_select_db($BaseDatos,$this->Identificador);
  53. $NumeroDeConexiones++;
  54. if ($bit == 0){
  55. $this->Error = "Error al Seleccionar Base de Datos $Tipo ($Usuario@$Servidor => $BaseDatos)<br>";
  56. $this->Error_debug = "Error al Seleccionar Base de Datos $Tipo ($Usuario:$Contrasena@$Servidor => $BaseDatos)<br>";
  57. $this->Estado = 0;
  58. }
  59. else{
  60. mysql_query("SET NAMES 'utf8';",$this->Identificador);
  61. }
  62. }
  63. }
  64. elseif($Tipo == "PosgreSQL" || strcasecmp($Tipo, "pg") == 0){
  65. $this->TipoBaseDatos = "PosgreSQL";
  66. $conectar = "dbname=$BaseDatos host=$Servidor user=$Usuario password=$Contrasena";
  67. $this->Identificador = @pg_connect($conectar,PGSQL_CONNECT_FORCE_NEW);
  68. if ($this->Identificador == FALSE){
  69. $this->Error = "Error al Conectar $Tipo ($Usuario@$Servidor => $BaseDatos)<br>";
  70. $this->Error_debug = "Error al Conectar $Tipo ($Usuario:$Contrasena@$Servidor => $BaseDatos)<br>";
  71. $this->Estado = 0;
  72. }
  73. else{
  74. $NumeroDeConexiones++;
  75. $this->Consulta("SET CLIENT_ENCODING TO 'LATIN1'");
  76. }
  77. }
  78. else{
  79. $this->Error = "Error, Tipo de Base de Datos Incorrecta: $Tipo ($Usuario@$Servidor => $BaseDatos)<br>";
  80. $this->Error_debug = "Error, Tipo de Base de Datos Incorrecta: $Tipo ($Usuario:$Contrasena@$Servidor => $BaseDatos)<br>";
  81. $this->Estado = 0;
  82. }
  83. }
  84. function __destruct() {
  85. if($this->TipoBaseDatos == "MySQL"){
  86. return @mysql_close($this->Identificador);
  87. }
  88. else{
  89. return @pg_close($this->Identificador);
  90. }
  91. }
  92. function EscapeString($InputData){
  93. if($this->TipoBaseDatos == "MySQL"){
  94. return @mysql_escape_string($InputData);
  95. }
  96. else{
  97. return @pg_escape_string($InputData);
  98. }
  99. }
  100. function EscapeArray($InputData, $EscapeKeys = NULL){
  101. if(is_string($EscapeKeys)){
  102. $EscapeKeys = array( "$EscapeKeys" );
  103. }
  104. else if(!is_array($EscapeKeys)){
  105. $EscapeKeys = array_keys($InputData);
  106. }
  107. if($this->TipoBaseDatos == "MySQL"){
  108. foreach($EscapeKeys as $k){
  109. $InputData[$k] = mysql_escape_string($InputData[$k]);
  110. }
  111. }
  112. else{
  113. foreach($EscapeKeys as $k){
  114. $InputData[$k] = pg_escape_string($InputData[$k]);
  115. }
  116. }
  117. return $InputData;
  118. }
  119. function EscapeObject($InputData, $EscapeKeys = NULL){
  120. return $this->EscapeArray(get_object_vars($InputData),$EscapeKeys);
  121. }
  122. function Consulta($Query){
  123. global $NumeroDeConsultas;
  124. $this->Estado = 1;
  125. if (!is_string($Query)){
  126. $this->Error_debug = $this->Error = "Consulta \"$Query\" no vรกlida<br>";
  127. $this->Estado = $this->NumeroResultados = 0;
  128. return $this->NumeroResultados;
  129. }
  130. if($this->MostrarConsultas)
  131. echo nl2br($Query)."<br>";
  132. if($this->TipoBaseDatos == "MySQL"){
  133. if ($this->ResultadoConsulta != 0)
  134. @mysql_free_result($this->ResultadoConsulta);
  135. $NumeroDeConsultas++;
  136. $this->ResultadoConsulta = @mysql_query($Query,$this->Identificador);
  137. $this->UltimoId = @mysql_insert_id($this->Identificador);
  138. if($this->ResultadoConsulta == 0){
  139. $this->Error = "Error Realizar la Consulta \"$Query\"<br>";
  140. $this->Error_debug = "Error Realizar la Consulta \"$Query\" (". @mysql_error($this->Identificador) .")<br>";
  141. $this->Estado = $this->NumeroResultados = 0;
  142. }
  143. else
  144. $this->NumeroResultados = @mysql_affected_rows($this->Identificador);
  145. }
  146. else{
  147. if ($this->ResultadoConsulta != 0)
  148. @pg_free_result($this->ResultadoConsulta);
  149. $NumeroDeConsultas++;
  150. $this->ResultadoConsulta = @pg_query($this->Identificador,$Query);
  151. if($this->ResultadoConsulta == FALSE){
  152. $this->Error = "Error Realizar la Consulta \"$Query\"<br>";
  153. $this->Error_debug = "Error Realizar la Consulta \"$Query\" (". @pg_last_error($this->Identificador) .")<br>";
  154. $this->Estado = $this->NumeroResultados = 0;
  155. }
  156. else
  157. $this->NumeroResultados = @pg_num_rows($this->ResultadoConsulta);
  158. }
  159. return $this->NumeroResultados;
  160. }
  161. function SacaTupla($full = true){
  162. if($this->TipoBaseDatos == "MySQL"){
  163. if($full)
  164. return @mysql_fetch_array($this->ResultadoConsulta);
  165. else
  166. return @mysql_fetch_assoc($this->ResultadoConsulta);
  167. }
  168. else{
  169. if($full)
  170. return @pg_fetch_array($this->ResultadoConsulta);
  171. else
  172. return @pg_fetch_assoc($this->ResultadoConsulta);
  173. }
  174. }
  175. function MuestraError($Debug = 0){
  176. if($Debug)
  177. echo $this->Error_debug;
  178. else
  179. echo $this->Error;
  180. $this->Estado = 1;
  181. }
  182. }
  183. ?>