PageRenderTime 73ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lavado/clases/aplicacion/ClaseBase.php

https://gitlab.com/maccevedor/zend
PHP | 361 lines | 184 code | 65 blank | 112 comment | 22 complexity | d3f5a9469012cfd4baab54ea86817ba2 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * @package co.org.lavado.app
  4. * @subpackage sistema
  5. */
  6. require_once 'Auditoria.php';
  7. require_once 'Variable.php';
  8. /**
  9. * Clase base para todas las clases de instancia de la aplicacion
  10. *
  11. * @author Universidad Cooperativa de Colombia - 2012
  12. * @since 2012/09/01
  13. * @version 1.0
  14. * @package co.org.lavado.app
  15. * @subpackage sistema
  16. */
  17. abstract class ClaseBase {
  18. //put your code here
  19. /**
  20. *
  21. * @var boolean
  22. */
  23. protected $modificado;
  24. /**
  25. *
  26. * @var mixed
  27. */
  28. protected $objetoJson;
  29. /**
  30. *
  31. * @var mixed
  32. */
  33. protected $prefijoPropiedadJson;
  34. /**
  35. *
  36. * @var ConexionMySQL
  37. */
  38. protected $conexion;
  39. /**
  40. *
  41. * @var string
  42. */
  43. protected $hash='';
  44. /**
  45. *
  46. * @var string
  47. */
  48. protected $firma='';
  49. /**
  50. *
  51. * @var string
  52. */
  53. protected $nombreDeClase='';
  54. /**
  55. *
  56. * @var string
  57. */
  58. protected $nombreDeTabla='';
  59. /**
  60. *
  61. * @var string
  62. */
  63. protected $campoId='';
  64. /**
  65. *
  66. * @var string
  67. */
  68. protected $rawData='';
  69. /**
  70. *
  71. */
  72. public function __construct($prefijoPropiedadJson=null) {
  73. $this->conexion= FrameWork::getConexion();
  74. $this->modificado=false;
  75. $this->prefijoPropiedadJson=$prefijoPropiedadJson;
  76. $this->objetoJson=new stdClass();
  77. $this->nombreDeClase=get_class($this);
  78. $this->nombreDeTabla=mb_strtolower($this->nombreDeClase);
  79. $this->campoId='id'.$this->nombreDeTabla;
  80. }
  81. /**
  82. *
  83. * @param string $propiedad
  84. * @param boolean $lanzarExcepcion
  85. * @return boolean
  86. * @throws AppException
  87. */
  88. public function existePropiedad($propiedad, $lanzarExcepcion=true){
  89. if(!isset($this->modificado))
  90. throw new AppException('La propiedad directa no existe en '.$this->nombreDeClase);
  91. //echo "$propiedad: ".$this->$propiedad." - ";
  92. if(!isset($this->$propiedad)){
  93. if($lanzarExcepcion)
  94. throw new AppException('La propiedad '.$propiedad.' no existe en '.$this->nombreDeClase);
  95. else
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. *
  102. * @param string $propiedad
  103. * @return mixed
  104. */
  105. public function getValorOriginal($propiedad){
  106. $this->existePropiedad($propiedad);
  107. $nuevaPropiedad="original_".$propiedad;
  108. if(isset($this->$nuevaPropiedad))
  109. return $this->$nuevaPropiedad;
  110. return null;
  111. }
  112. /**
  113. *
  114. * @param type $propiedad
  115. * @return boolean
  116. */
  117. public function estaModificada($propiedad=NULL){
  118. if(empty($propiedad))
  119. return $this->modificado;
  120. $this->existePropiedad($propiedad);
  121. $propiedadOriginal="original_".$propiedad;
  122. if(!$this->existePropiedad($propiedadOriginal, false))
  123. return false;
  124. if($this->$propiedadOriginal===$this->$propiedad)
  125. return false;
  126. return true;
  127. }
  128. /*
  129. public function getTextoParaAuditoria($propiedad){
  130. $this->existePropiedad($propiedad);
  131. $propiedadOriginal="original_".$propiedad;
  132. if(!$this->existePropiedad($propiedadOriginal, false))
  133. return $propiedad.':'.$this->$propiedad;
  134. return $propiedad.':'.$this->$propiedadOriginal.'->'.$this->$propiedad;
  135. }
  136. * */
  137. /**
  138. *
  139. * @param type $propiedad
  140. * @return boolean
  141. */
  142. public function marcarNoModificada($propiedad){
  143. $this->existePropiedad($propiedad);
  144. $nuevaPropiedad="original_".$propiedad;
  145. $this->$nuevaPropiedad=$this->$propiedad;
  146. $propiedadJson=$this->prefijoPropiedadJson.strtolower($propiedad);
  147. $this->objetoJson->$propiedadJson=$this->$propiedad;
  148. return $this->$propiedad;
  149. }
  150. /**
  151. *
  152. * @param string $propiedad
  153. * @param mixed $valor
  154. */
  155. public function setPropiedad($propiedad, $valor, $forzarOriginal=false){
  156. $this->existePropiedad($propiedad);
  157. $nuevaPropiedad="original_".$propiedad;
  158. if($forzarOriginal){
  159. $this->$nuevaPropiedad=$valor;
  160. }else{
  161. if($this->existePropiedad($nuevaPropiedad, false)){
  162. if($this->$propiedad===$valor)
  163. return $valor;
  164. else{
  165. $this->$nuevaPropiedad=$this->$propiedad;
  166. }
  167. }else{
  168. $this->$nuevaPropiedad=$valor;
  169. }
  170. }
  171. $this->$propiedad=$valor;
  172. //echo "El prefino de joson es para $propiedad es ".$this->prefijoPropiedadJson."-";
  173. $propiedadJson=$this->prefijoPropiedadJson.strtolower($propiedad);
  174. $this->objetoJson->$propiedadJson=$valor;
  175. $this->modificado=true;
  176. return $this->$nuevaPropiedad;
  177. }
  178. /**
  179. *
  180. * @param string $propiedad
  181. * @return string
  182. */
  183. public function getNombreJson($propiedad){
  184. return $this->prefijoPropiedadJson.strtolower($propiedad);
  185. }
  186. /**
  187. *
  188. * @param type $propiedad
  189. * @return string
  190. */
  191. public function getTextoParaAuditoria($propiedad){
  192. $this->existePropiedad($propiedad);
  193. $textoAuditoria=$propiedad.": ";
  194. $propiedadOriginal="original_".$propiedad;
  195. if($this->$propiedadOriginal!==$this->$propiedad){
  196. if(is_string($this->$propiedadOriginal))
  197. $textoAuditoria.="'".$this->$propiedadOriginal."'>>";
  198. else
  199. $textoAuditoria.=$this->$propiedadOriginal.">>";
  200. }
  201. if(is_string($this->$propiedad))
  202. $textoAuditoria.="'".$this->$propiedad."'";
  203. else
  204. $textoAuditoria.=$this->$propiedad;
  205. return $textoAuditoria;
  206. }
  207. /**
  208. *
  209. * @param type $comoObjeto
  210. * @return stdClass
  211. */
  212. public function getJson($comoObjeto=false, $prefijoJson=null){
  213. $objetoJson=null;
  214. if(empty($prefijoJson)){
  215. $objetoJson=$this->objetoJson;
  216. }else{
  217. $objetoJson=new stdClass();
  218. foreach($this->objetoJson as $key => $value) {
  219. $nuevaPropiedad=$prefijoJson.$key;
  220. $objetoJson->$nuevaPropiedad=$value;
  221. }
  222. }
  223. if($comoObjeto)
  224. return $objetoJson;
  225. else
  226. return json_encode($objetoJson);
  227. }
  228. public function getHash(){
  229. return $this->hash;
  230. }
  231. public function getFirma(){
  232. return $this->firma;
  233. }
  234. public function calcularHash(){
  235. $this->calcularRawData();
  236. return hash ("sha256", $this->rawData);
  237. }
  238. public function verificarHash(){
  239. if(empty($this->hash))
  240. throw new Exception('Imposible verificar hash de '.$this->nombreDeClase.', no existe hash guardado');
  241. if($this->hash!=$this->calcularHash())
  242. return false;
  243. return true;
  244. }
  245. public function calcularFirma(){
  246. $this->firma=FrameWork::firmarDatos($this->rawData);
  247. if(!empty($this->firma))
  248. $this->firma=base64_encode($this->firma);
  249. return $this->firma;
  250. }
  251. public function verificarFirma(){
  252. return FrameWork::verificarFirma($this->rawData, empty($this->firma)?'':base64_decode($this->firma));
  253. }
  254. protected function calcularRawData(){
  255. if(empty($this->rawData)){
  256. $this->rawData='';
  257. $sql = 'SELECT lower(COLUMN_NAME) as columna
  258. FROM information_schema.COLUMNS
  259. WHERE
  260. TABLE_SCHEMA like \'lavado\'
  261. AND
  262. TABLE_NAME = lower(\''.$this->nombreDeClase.'\')
  263. AND
  264. COLUMN_NAME<>\'hash\'
  265. AND
  266. COLUMN_NAME<>\'firma\'
  267. ORDER BY
  268. COLUMN_NAME';
  269. $resultados=$this->conexion->consultar($sql);
  270. $numeroCampos=$resultados->getCantidad();
  271. if($numeroCampos==0)
  272. throw new Exception('Imposible obtener RawData de '.$this->nombreDeClase.', cero columnas devueltas');
  273. while($resultados->irASiguiente()){
  274. foreach($this as $columna => $valor) {
  275. if(strtolower($columna)===$resultados->get()->columna){
  276. $numeroCampos--;
  277. $this->rawData.=(string)$valor."-";
  278. break;
  279. }
  280. }
  281. }
  282. if($numeroCampos>0)
  283. throw new Exception('Imposible obtener RawData de '.$this->nombreDeClase.', no todos los campos fueron incluidos');
  284. }
  285. }
  286. public function getRawData(){
  287. $this->calcularRawData();
  288. return $this->rawData;
  289. }
  290. abstract protected function cargarObjeto($string);
  291. abstract public function guardarObjeto(Auditoria $auditoria=null);
  292. abstract public function haSidoUtilizado();
  293. }
  294. ?>