Commit 75b15c35f879ac842a9cd6876f054dac0ed79f1a
1 parent
b4b5131c
Exists in
master
Adicionado suporte a queries preparadas (usado evitar sql injection), adicionado…
… try para pegar erros redirecionamento para /modules/Error
Showing
1 changed file
with
54 additions
and
72 deletions
Show diff stats
ieducar/intranet/include/clsBancoPgSql.inc.php
| @@ -32,6 +32,8 @@ require_once 'clsConfigItajai.inc.php'; | @@ -32,6 +32,8 @@ require_once 'clsConfigItajai.inc.php'; | ||
| 32 | require_once 'include/clsCronometro.inc.php'; | 32 | require_once 'include/clsCronometro.inc.php'; |
| 33 | require_once 'include/clsEmail.inc.php'; | 33 | require_once 'include/clsEmail.inc.php'; |
| 34 | 34 | ||
| 35 | +require_once 'modules/Error/Mailers/NotificationMailer.php'; | ||
| 36 | + | ||
| 35 | /** | 37 | /** |
| 36 | * clsBancoSQL_ abstract class. | 38 | * clsBancoSQL_ abstract class. |
| 37 | * | 39 | * |
| @@ -148,11 +150,6 @@ abstract class clsBancoSQL_ | @@ -148,11 +150,6 @@ abstract class clsBancoSQL_ | ||
| 148 | /** | 150 | /** |
| 149 | * @var bool | 151 | * @var bool |
| 150 | */ | 152 | */ |
| 151 | - var $showReportarErro = TRUE; | ||
| 152 | - | ||
| 153 | - /** | ||
| 154 | - * @var bool | ||
| 155 | - */ | ||
| 156 | var $executandoEcho = FALSE; | 153 | var $executandoEcho = FALSE; |
| 157 | 154 | ||
| 158 | /** | 155 | /** |
| @@ -362,16 +359,10 @@ abstract class clsBancoSQL_ | @@ -362,16 +359,10 @@ abstract class clsBancoSQL_ | ||
| 362 | // Verifica se o link de conexão está inativo e conecta | 359 | // Verifica se o link de conexão está inativo e conecta |
| 363 | if (0 == $this->bLink_ID) { | 360 | if (0 == $this->bLink_ID) { |
| 364 | $this->FraseConexao(); | 361 | $this->FraseConexao(); |
| 365 | - | ||
| 366 | - if ($this->bDepurar) { | ||
| 367 | - printf("<br>Depurar: Frase de Conexão : %s<br>\n", $this->strFraseConexao); | ||
| 368 | - } | ||
| 369 | - | ||
| 370 | $this->bLink_ID = pg_connect($this->strFraseConexao); | 362 | $this->bLink_ID = pg_connect($this->strFraseConexao); |
| 371 | 363 | ||
| 372 | - if (!$this->bLink_ID) { | ||
| 373 | - $this->Interrompe("Link inválido, conexão falhou!"); | ||
| 374 | - } | 364 | + if (! $this->bLink_ID) |
| 365 | + $this->Interrompe("Não foi possivel conectar ao banco de dados"); | ||
| 375 | } | 366 | } |
| 376 | } | 367 | } |
| 377 | 368 | ||
| @@ -447,23 +438,17 @@ abstract class clsBancoSQL_ | @@ -447,23 +438,17 @@ abstract class clsBancoSQL_ | ||
| 447 | 438 | ||
| 448 | if (!$this->bConsulta_ID) { | 439 | if (!$this->bConsulta_ID) { |
| 449 | if ($this->getThrowException()) { | 440 | if ($this->getThrowException()) { |
| 450 | - $message = $this->bErro_no ? "($this->bErro_no) " . $this->strErro : | ||
| 451 | - pg_last_error($this->bLink_ID); | 441 | + $message = $this->bErro_no ? "($this->bErro_no) " . $this->strErro : |
| 442 | + pg_last_error($this->bLink_ID); | ||
| 452 | 443 | ||
| 453 | $message .= PHP_EOL . $this->strStringSQL; | 444 | $message .= PHP_EOL . $this->strStringSQL; |
| 454 | 445 | ||
| 455 | throw new Exception($message); | 446 | throw new Exception($message); |
| 456 | } | 447 | } |
| 457 | - else { | 448 | + else |
| 449 | + { | ||
| 458 | $erroMsg = "SQL invalido: {$this->strStringSQL}<br>\n"; | 450 | $erroMsg = "SQL invalido: {$this->strStringSQL}<br>\n"; |
| 459 | - die($erroMsg); | ||
| 460 | - | ||
| 461 | - if ($this->transactionBlock) { | ||
| 462 | - // Nada. | ||
| 463 | - } | ||
| 464 | - | ||
| 465 | - $this->Interrompe($erroMsg); | ||
| 466 | - return FALSE; | 451 | + throw new Exception("Erro ao executar uma ação no banco de dados: $erroMsg"); |
| 467 | } | 452 | } |
| 468 | } | 453 | } |
| 469 | 454 | ||
| @@ -810,58 +795,55 @@ abstract class clsBancoSQL_ | @@ -810,58 +795,55 @@ abstract class clsBancoSQL_ | ||
| 810 | * @param string $msg | 795 | * @param string $msg |
| 811 | * @param bool $getError | 796 | * @param bool $getError |
| 812 | */ | 797 | */ |
| 813 | - function Interrompe($msg, $getError = FALSE) | 798 | + function Interrompe($appErrorMsg, $getError = FALSE) |
| 814 | { | 799 | { |
| 815 | - if ($getError) { | ||
| 816 | - $this->strErro = pg_result_error($this->bConsulta_ID); | ||
| 817 | - $this->bErro_no = ($this->strErro == '') ? FALSE : TRUE; | ||
| 818 | - } | 800 | + $lastError = error_get_last(); |
| 819 | 801 | ||
| 820 | - $erro1 = substr(md5('1erro'), 0, 10); | ||
| 821 | - $erro2 = substr(md5('2erro'), 0, 10); | 802 | + @session_start(); |
| 803 | + $_SESSION['last_php_error_message'] = $lastError['message']; | ||
| 804 | + $_SESSION['last_php_error_line'] = $lastError['line']; | ||
| 805 | + $_SESSION['last_php_error_file'] = $lastError['file']; | ||
| 806 | + @session_write_close(); | ||
| 822 | 807 | ||
| 823 | - function show($data, $func = 'var_dump') { | ||
| 824 | - ob_start(); | ||
| 825 | - $func($data); | ||
| 826 | - $output = ob_get_contents(); | ||
| 827 | - ob_end_clean(); | ||
| 828 | - return $output; | ||
| 829 | - } | 808 | + $pgErrorMsg = $getError ? pg_result_error($this->bConsulta_ID) : ''; |
| 809 | + NotificationMailer::unexpectedDataBaseError($appErrorMsg, $pgErrorMsg, $this->strStringSQL); | ||
| 810 | + | ||
| 811 | + die("<script>document.location.href = '/module/Error/unexpected';</script>"); | ||
| 812 | + } | ||
| 813 | + | ||
| 814 | + | ||
| 815 | + /** | ||
| 816 | + * Executa uma consulta SQL preparada ver: http://php.net/manual/en/function.pg-prepare.php | ||
| 817 | + * ex: $db->execPreparedQuery("select * from portal.funcionario where matricula = $1 and senha = $2", array('admin', '123')) | ||
| 818 | + * | ||
| 819 | + * @param string $name nome da consulta | ||
| 820 | + * @param string $query sql para ser preparado | ||
| 821 | + * @param array $params parametros para consulta | ||
| 822 | + * | ||
| 823 | + * @return bool|resource FALSE em caso de erro ou o identificador da consulta | ||
| 824 | + * em caso de sucesso. | ||
| 825 | + */ | ||
| 826 | + public function execPreparedQuery($query, $params = array()) { | ||
| 827 | + try { | ||
| 828 | + $errorMsgs = ''; | ||
| 829 | + $this->Conecta(); | ||
| 830 | + $dbConn = $this->bLink_ID; | ||
| 831 | + | ||
| 832 | + if (! is_array($params)) | ||
| 833 | + $params = array($params); | ||
| 834 | + | ||
| 835 | + $this->bConsulta_ID = @pg_query_params($dbConn, $query, $params); | ||
| 836 | + $resultError = @pg_result_error($this->bConsulta_ID); | ||
| 837 | + $errorMsgs .= trim($resultError) != '' ? $resultError : @pg_last_error($this->bConsulta_ID); | ||
| 830 | 838 | ||
| 831 | - @session_start(); | ||
| 832 | - $_SESSION['vars_session'] = show($_SESSION); | ||
| 833 | - $_SESSION['vars_post'] = show($_POST); | ||
| 834 | - $_SESSION['vars_get'] = show($_GET); | ||
| 835 | - $_SESSION['vars_cookie'] = show($_COOKIE); | ||
| 836 | - $_SESSION['vars_erro1'] = $msg; | ||
| 837 | - $_SESSION['vars_erro2'] = $this->strErro; | ||
| 838 | - $_SESSION['vars_server'] = show($_SERVER); | ||
| 839 | - $id_pessoa = $_SESSION['id_pessoa']; | ||
| 840 | - | ||
| 841 | - session_write_close(); | ||
| 842 | - | ||
| 843 | - if ($this->showReportarErro) { | ||
| 844 | - $array_idpes_erro_total = array(); | ||
| 845 | - $array_idpes_erro_total[4910] = TRUE; | ||
| 846 | - $array_idpes_erro_total[2151] = TRUE; | ||
| 847 | - $array_idpes_erro_total[8194] = TRUE; | ||
| 848 | - $array_idpes_erro_total[7470] = TRUE; | ||
| 849 | - $array_idpes_erro_total[4637] = TRUE; | ||
| 850 | - $array_idpes_erro_total[4702] = TRUE; | ||
| 851 | - $array_idpes_erro_total[1801] = TRUE; | ||
| 852 | - | ||
| 853 | - if (! $array_idpes_erro_total[$id_pessoa]) { | ||
| 854 | - die( "<script>document.location.href = 'erro_banco.php';</script>" ); | ||
| 855 | } | 839 | } |
| 856 | - else { | ||
| 857 | - printf("</td></tr></table><b>Erro de Banco de Dados:</b> %s<br><br>\n", $msg); | ||
| 858 | - printf("<b>SQL:</b> %s<br><br>\n", $this->strStringSQL ); | ||
| 859 | - printf("<b>Erro do PgSQL </b>: %s (%s)<br><br>\n", $this->bErro_no, $this->strErro); | ||
| 860 | - die("Sessão Interrompida."); | 840 | + catch(Exception $e) { |
| 841 | + $errorMsgs .= "Exception: " . $e->getMessage(); | ||
| 861 | } | 842 | } |
| 862 | - } | ||
| 863 | - else { | ||
| 864 | - die($this->strErro . "\n"); | ||
| 865 | - } | 843 | + |
| 844 | + if ($this->bConsulta_ID == false || trim($errorMsgs) != '') | ||
| 845 | + throw new Exception("Erro ao preparar consulta ($query) no banco de dados: $errorMsgs"); | ||
| 846 | + | ||
| 847 | + return $this->bConsulta_ID; | ||
| 866 | } | 848 | } |
| 867 | -} | ||
| 868 | \ No newline at end of file | 849 | \ No newline at end of file |
| 850 | +} |