Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
24.62% |
16 / 65 |
CRAP | |
32.57% |
185 / 568 |
| TPDOConnection | |
0.00% |
0 / 1 |
|
24.62% |
16 / 65 |
26124.64 | |
32.21% |
182 / 565 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getDBMS | |
0.00% |
0 / 1 |
3.04 | |
83.33% |
5 / 6 |
|||
| setDBMS | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| setBanco | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getHost | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setHost | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getPort | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setPort | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getDataBaseName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setDataBaseName | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getUsername | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setUsername | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getPassword | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setPassword | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| setUtfDecode | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getUtfDecode | |
0.00% |
0 / 1 |
4.05 | |
85.71% |
6 / 7 |
|||
| connect | |
0.00% |
0 / 1 |
4.00 | |
93.33% |
14 / 15 |
|||
| isMySqlDbUtf8 | |
0.00% |
0 / 1 |
3.04 | |
83.33% |
5 / 6 |
|||
| getExtraConfigPDO | |
0.00% |
0 / 1 |
2.15 | |
66.67% |
2 / 3 |
|||
| validateConnect | |
0.00% |
0 / 1 |
12.53 | |
58.62% |
17 / 29 |
|||
| getConfigFileAndRoot | |
0.00% |
0 / 1 |
240 | |
0.00% |
0 / 30 |
|||
| setConfigDBMS | |
0.00% |
0 / 1 |
7.33 | |
66.67% |
10 / 15 |
|||
| validateDBMS | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| setConfigDbmsPort | |
0.00% |
0 / 1 |
3.58 | |
60.00% |
6 / 10 |
|||
| getDefaultPortDBMS | |
0.00% |
0 / 1 |
10.01 | |
94.74% |
18 / 19 |
|||
| setConfigDatabase | |
0.00% |
0 / 1 |
10.83 | |
38.46% |
5 / 13 |
|||
| setConfigUtf8Decode | |
0.00% |
0 / 1 |
4.59 | |
66.67% |
4 / 6 |
|||
| setConfigHost | |
0.00% |
0 / 1 |
3.33 | |
66.67% |
4 / 6 |
|||
| setConfigUserAndPassword | |
0.00% |
0 / 1 |
15.66 | |
56.52% |
13 / 23 |
|||
| useSimpleDBMS | |
0.00% |
0 / 1 |
15.55 | |
25.00% |
2 / 8 |
|||
| simpleDBMS | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
|||
| defineDsnPDO | |
0.00% |
0 / 1 |
59.23 | |
31.03% |
9 / 29 |
|||
| validateFileDataBaseExists | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 8 |
|||
| showError | |
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 8 |
|||
| setError | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getError | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 5 |
|||
| getInstance | |
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 17 |
|||
| __clone | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| executeSql | |
0.00% |
0 / 1 |
812 | |
0.00% |
0 / 60 |
|||
| prepare | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
| encodeArray | |
0.00% |
0 / 1 |
420 | |
0.00% |
0 / 36 |
|||
| prepareArrayNamed | |
0.00% |
0 / 1 |
132 | |
0.00% |
0 / 20 |
|||
| verifyformtDateYMD | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
| prepareArray | |
0.00% |
0 / 1 |
8.02 | |
93.33% |
14 / 15 |
|||
| getRoot | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 13 |
|||
| showExemplo | |
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 23 |
|||
| test | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 8 |
|||
| beginTransaction | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
| commit | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
| rollBack | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 3 |
|||
| getLastId | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| formatDate | |
0.00% |
0 / 1 |
15.48 | |
66.67% |
12 / 18 |
|||
| dmy | |
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 10 |
|||
| getStrUtf8OrAnsi | |
0.00% |
0 / 1 |
12.33 | |
71.43% |
10 / 14 |
|||
| processResult | |
0.00% |
0 / 1 |
240 | |
0.00% |
0 / 26 |
|||
| pgsqlLOBOpen | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| pgsqlLOBCreate | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| setDieOnError | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getDieOnError | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
|||
| setShowFormErrors | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getShowFormErrors | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
|||
| setMessage | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getMessage | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| setSchema | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| getSchema | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| 1 | <?php |
| 2 | /* |
| 3 | * Formdin Framework |
| 4 | * Copyright (C) 2012 Ministério do Planejamento |
| 5 | * Criado por Luís Eugênio Barbosa |
| 6 | * Essa versão é um Fork https://github.com/bjverde/formDin |
| 7 | * |
| 8 | * ---------------------------------------------------------------------------- |
| 9 | * This file is part of Formdin Framework. |
| 10 | * |
| 11 | * Formdin Framework is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public License version 3 |
| 13 | * as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public License version 3 |
| 21 | * along with this program; if not, see <http://www.gnu.org/licenses/> |
| 22 | * or write to the Free Software Foundation, Inc., 51 Franklin Street, |
| 23 | * Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | * ---------------------------------------------------------------------------- |
| 25 | * Este arquivo é parte do Framework Formdin. |
| 26 | * |
| 27 | * O Framework Formdin é um software livre; você pode redistribuí-lo e/ou |
| 28 | * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação |
| 29 | * do Software Livre (FSF). |
| 30 | * |
| 31 | * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA |
| 32 | * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou |
| 33 | * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português |
| 34 | * para maiores detalhes. |
| 35 | * |
| 36 | * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título |
| 37 | * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> |
| 38 | * ou escreva para a Fundação do Software Livre (FSF) Inc., |
| 39 | * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 40 | */ |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Sobre o PDO |
| 45 | * http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html |
| 46 | **/ |
| 47 | |
| 48 | |
| 49 | if ( !defined ( 'DS' ) ) { define( 'DS', DIRECTORY_SEPARATOR ); } |
| 50 | $currentl_dir = dirname ( __FILE__ ); |
| 51 | |
| 52 | require_once( $currentl_dir . DS . '..' . DS . 'constants.php' ); |
| 53 | |
| 54 | class TPDOConnection { |
| 55 | |
| 56 | const WAY_APP2BANK = 'WAY_APP2BANK'; |
| 57 | const WAY_BANK2APP = 'WAY_BANK2APP'; |
| 58 | |
| 59 | private static $error = null; |
| 60 | private static $instance = null; |
| 61 | private static $lastSql; |
| 62 | private static $lastParams; |
| 63 | private static $useSessionLogin; |
| 64 | private static $dieOnError; |
| 65 | private static $showFormErrors; |
| 66 | private static $message; |
| 67 | private static $configFile; |
| 68 | private static $dsn; |
| 69 | private static $banco; |
| 70 | private static $host; |
| 71 | private static $port; |
| 72 | private static $username; |
| 73 | private static $password; |
| 74 | private static $databaseName; |
| 75 | private static $schema; |
| 76 | private static $utfDecode; |
| 77 | |
| 78 | |
| 79 | // construtor |
| 80 | public function __construct(){ |
| 81 | } |
| 82 | |
| 83 | public static function getDBMS() { |
| 84 | $retorno = null; |
| 85 | if ( isset( self::$banco ) ){ |
| 86 | $retorno = self::$banco; |
| 87 | } else { |
| 88 | if ( defined( 'BANCO' ) ){ |
| 89 | $retorno = BANCO; |
| 90 | } |
| 91 | } |
| 92 | return $retorno; |
| 93 | } |
| 94 | public static function setDBMS( $banco = null ) { |
| 95 | self::$banco = $banco; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @deprecated change to setDBMS |
| 100 | * @param string $banco |
| 101 | */ |
| 102 | public static function setBanco( $banco = null ) { |
| 103 | self::setDBMS($banco); |
| 104 | } |
| 105 | //-------------------------------------------------------------------------------------- |
| 106 | public static function getHost() { |
| 107 | return self::$host; |
| 108 | } |
| 109 | public static function setHost($host) { |
| 110 | self::$host = $host; |
| 111 | } |
| 112 | //-------------------------------------------------------------------------------------- |
| 113 | public static function getPort() { |
| 114 | return self::$port; |
| 115 | } |
| 116 | public static function setPort($port) { |
| 117 | self::$port = $port; |
| 118 | } |
| 119 | //-------------------------------------------------------------------------------------- |
| 120 | public static function getDataBaseName() |
| 121 | { |
| 122 | return self::$databaseName; |
| 123 | } |
| 124 | public static function setDataBaseName( $strNewValue = null ) |
| 125 | { |
| 126 | self::$databaseName = $strNewValue; |
| 127 | } |
| 128 | //-------------------------------------------------------------------------------------- |
| 129 | public static function getUsername() |
| 130 | { |
| 131 | return self::$username; |
| 132 | } |
| 133 | public static function setUsername($username) |
| 134 | { |
| 135 | self::$username = $username; |
| 136 | } |
| 137 | //-------------------------------------------------------------------------------------- |
| 138 | public static function getPassword() |
| 139 | { |
| 140 | return self::$password; |
| 141 | } |
| 142 | public static function setPassword($password) |
| 143 | { |
| 144 | self::$password = $password; |
| 145 | } |
| 146 | //-------------------------------------------------------------------------------------- |
| 147 | public static function setUtfDecode( $boolNewValue = null ) { |
| 148 | self::$utfDecode = $boolNewValue; |
| 149 | } |
| 150 | public static function getUtfDecode() { |
| 151 | $utfDecodeReturn = self::$utfDecode; |
| 152 | if ( is_null($utfDecodeReturn) ){ |
| 153 | if( !defined( 'UTF8_DECODE' ) ) { define( 'UTF8_DECODE', 1 ); } |
| 154 | if( UTF8_DECODE ){ |
| 155 | $utfDecodeReturn = true; |
| 156 | } else { |
| 157 | $utfDecodeReturn = false; |
| 158 | } |
| 159 | } |
| 160 | return $utfDecodeReturn; |
| 161 | } |
| 162 | //------------------------------------------------------------------------------------------ |
| 163 | /*** |
| 164 | * Establishes the connection to the database. the main connection is informed in $configfile |
| 165 | * the secondary connections are informed in $configArray. The $configArray prevails over $configfile. |
| 166 | * In this case $configfile will be ignored. |
| 167 | * |
| 168 | * @param string $configFile - path of file with main connection, |
| 169 | * @param boolean $boolRequired - connection is mandatory |
| 170 | * @param boolean $boolUtfDecode- |
| 171 | * @param array $configArray - config array for others connections |
| 172 | * @return boolean |
| 173 | */ |
| 174 | public static function connect( $configFile = null, $boolRequired = true, $boolUtfDecode = null, $configArray = null ) { |
| 175 | |
| 176 | //POG for run with PHP-CLI |
| 177 | if ( empty($configFile) && defined( 'CONFIG_FILE' ) ) { |
| 178 | $configFile = CONFIG_FILE; |
| 179 | } |
| 180 | |
| 181 | self::setUtfDecode( $boolUtfDecode ); |
| 182 | self::validateConnect( $configFile ,$boolRequired ,$configArray); |
| 183 | try { |
| 184 | self::$instance[ self::getDatabaseName()] = new PDO( self::$dsn, self::$username, self::$password ); |
| 185 | self::$instance[ self::getDatabaseName()]->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); |
| 186 | self::getExtraConfigPDO(); |
| 187 | } catch( PDOException $e ){ |
| 188 | $msg = 'Erro de conexão.<br><b>DNS:</b><br>' |
| 189 | .self::$dsn |
| 190 | .'<br><BR><b>Erro retornado:</b><br>' |
| 191 | .$e->getMessage(); |
| 192 | self::$error = utf8_encode( $msg ); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Verifica se o SGBD é MySQL |
| 201 | * Encoding do banco é UTF 8 |
| 202 | * @return boolean |
| 203 | */ |
| 204 | public static function isMySqlDbUtf8(){ |
| 205 | $result = false; |
| 206 | $DBMS = self::getDBMS(); |
| 207 | $boolUtf8_Decode = self::getUtfDecode(); |
| 208 | if( ($DBMS == DBMS_MYSQL) && $boolUtf8_Decode==false ){ |
| 209 | $result = true; |
| 210 | } |
| 211 | return $result; |
| 212 | } |
| 213 | |
| 214 | public static function getExtraConfigPDO(){ |
| 215 | if( self::isMySqlDbUtf8() ){ |
| 216 | self::$instance[ self::getDatabaseName()]->exec('SET CHARACTER SET utf8'); // acerta a acentuação vinda do banco de dados |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Valida as configurações |
| 222 | * |
| 223 | * @param string $configFile |
| 224 | * @param bool $boolRequired |
| 225 | * @param array $configArray |
| 226 | * @return void |
| 227 | */ |
| 228 | private static function validateConnect($configFile ,$boolRequired ,$configArray) { |
| 229 | $configErrors = array(); |
| 230 | |
| 231 | $useConfigFile = true; |
| 232 | $root = null; |
| 233 | if( !empty($configArray) && is_array($configArray) ){ |
| 234 | $useConfigFile = false; |
| 235 | } else { |
| 236 | $configFileAndRoot = self::getConfigFileAndRoot($configFile); |
| 237 | $root = $configFileAndRoot['root']; |
| 238 | $configFile = $configFileAndRoot['configfile']; |
| 239 | self::$configFile = $configFile; |
| 240 | |
| 241 | if ( !file_exists( $configFile ) ) { |
| 242 | if ( $boolRequired ) { |
| 243 | $configErrors[] = "Classe TPDOConnectio.class.php - Arquivo {$configFile} não encontrado!"; |
| 244 | self::showExemplo( DBMS_MYSQL, $configErrors ); |
| 245 | } |
| 246 | return false; |
| 247 | } else { |
| 248 | require_once( $configFile ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if( count( $configErrors ) == 0 ){ |
| 253 | $configErrors = self::setConfigDBMS($useConfigFile, $configArray ,$configErrors ,$root); |
| 254 | |
| 255 | self::setConfigDbmsPort($useConfigFile, $configArray); |
| 256 | |
| 257 | $configErrors = self::setConfigDatabase($useConfigFile, $configArray, $configErrors); |
| 258 | |
| 259 | self::setConfigUtf8Decode($useConfigFile, $configArray); |
| 260 | self::setConfigHost($useConfigFile, $configArray); |
| 261 | self::setConfigUserAndPassword($useConfigFile, $configArray); |
| 262 | |
| 263 | $configErrors = self::useSimpleDBMS($configErrors); |
| 264 | |
| 265 | $configErrorsDsn = self::defineDsnPDO($configErrors,$useConfigFile); |
| 266 | if ( count( $configErrorsDsn ) > 0 ) { |
| 267 | $configErrors = $configErrors + $configErrorsDsn; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if ( count( $configErrors ) > 0 ) { |
| 272 | self::showExemplo( self::$banco, $configErrors ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Recupera o caminho correto do arquivo de configuração |
| 278 | * |
| 279 | * @param string $configFile |
| 280 | * @return void |
| 281 | */ |
| 282 | private static function getConfigFileAndRoot($configFile){ |
| 283 | $root = null; |
| 284 | |
| 285 | if ( self::getDataBaseName() ) { |
| 286 | $configFileDb = $configFile; |
| 287 | |
| 288 | if ( is_null( $configFileDb ) || !file_exists( $configFileDb . '_' . self::getDataBaseName() ) ) { |
| 289 | $configFileDb = is_null( $configFileDb ) ? 'config_conexao_' . self::getDataBaseName() . '.php' : $configFileDb; |
| 290 | |
| 291 | if ( !file_exists( $configFileDb ) ) { |
| 292 | $configFileDb = 'includes/' . $configFileDb; |
| 293 | |
| 294 | if ( !file_exists( $configFileDb ) ) { |
| 295 | $root = self::getRoot(); |
| 296 | $configFileDb = $root . $configFileDb; |
| 297 | if ( file_exists( $configFileDb ) ) { |
| 298 | $configFile = $configFileDb; |
| 299 | } |
| 300 | } else { |
| 301 | $configFile = $configFileDb; |
| 302 | } |
| 303 | } else { |
| 304 | $configFile = $configFileDb; |
| 305 | } |
| 306 | } else { |
| 307 | $configFile = $configFileDb; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if ( is_null( $configFile ) || !file_exists( $configFile ) ) { |
| 312 | $configFile = is_null( $configFile ) ? 'config_conexao.php' : $configFile; |
| 313 | |
| 314 | if ( !file_exists( $configFile ) ) { |
| 315 | $configFile = 'includes/config_conexao.php'; |
| 316 | |
| 317 | // procurar o arquivo padrão de conexão em até 5 niveis acima |
| 318 | for( $i = 1; $i < 6; $i++ ) { |
| 319 | if( file_exists( str_repeat( '../', $i ) . $configFile ) ) { |
| 320 | $configFile = str_repeat( '../', $i ) . $configFile; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if ( !file_exists( $configFile ) ) { |
| 325 | $root = self::getRoot(); |
| 326 | $configFile = $root . 'includes/config_conexao.php'; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | $return = array(); |
| 331 | $return['root'] = $root; |
| 332 | $return['configfile'] = $configFile; |
| 333 | return $return; |
| 334 | } |
| 335 | |
| 336 | public static function setConfigDBMS($useConfigFile, $configArray ,$configErrors ,$root) |
| 337 | { |
| 338 | if( $useConfigFile ){ |
| 339 | if ( !defined( 'BANCO' ) ) { |
| 340 | $configErrors[] = 'O arquivo ' . $root . 'includes/config_conexao.php não está configurado corretamente! Definal o tipo de banco de dados'; |
| 341 | }else{ |
| 342 | $DBMS = strtoupper(BANCO); |
| 343 | self::validateDBMS($DBMS); |
| 344 | self::$banco = $DBMS; |
| 345 | } |
| 346 | } else { |
| 347 | $dbms = ArrayHelper::get($configArray, 'DBMS'); |
| 348 | $banco = ArrayHelper::get($configArray, 'BANCO'); |
| 349 | $dbms = empty($dbms)?$banco:$dbms; |
| 350 | $dbms = is_null($dbms)?null:strtoupper($dbms); |
| 351 | if( empty($dbms) ){ |
| 352 | $configErrors[] = 'Array Config is not configured! Define DBMS'; |
| 353 | }else{ |
| 354 | self::validateDBMS($dbms); |
| 355 | self::$banco = $dbms; |
| 356 | } |
| 357 | } |
| 358 | return $configErrors; |
| 359 | } |
| 360 | |
| 361 | public static function validateDBMS($DBMS){ |
| 362 | self::getDefaultPortDBMS($DBMS); |
| 363 | } |
| 364 | |
| 365 | public static function setConfigDbmsPort($useConfigFile,$configArray){ |
| 366 | $DBMS = self::getDBMS(); |
| 367 | $portDefault = self::getDefaultPortDBMS($DBMS); |
| 368 | if($useConfigFile){ |
| 369 | if ( !defined( 'PORT' ) ) { |
| 370 | define( 'PORT', $portDefault ); |
| 371 | self::$port = $portDefault; |
| 372 | }else{ |
| 373 | self::$port = PORT; |
| 374 | } |
| 375 | }else{ |
| 376 | $DBMS = self::getDBMS(); |
| 377 | self::$port = ArrayHelper::getDefaultValeu($configArray,'PORT',$portDefault); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | public static function getDefaultPortDBMS($DBMS){ |
| 382 | if ( empty($DBMS) ){ |
| 383 | throw new InvalidArgumentException('Data Base Management System is not defined.'); |
| 384 | } |
| 385 | |
| 386 | $port = null; |
| 387 | switch( strtoupper( $DBMS ) ) { |
| 388 | case DBMS_ACCESS: |
| 389 | case DBMS_FIREBIRD: |
| 390 | case DBMS_SQLITE: |
| 391 | $port=null; |
| 392 | break; |
| 393 | case DBMS_MYSQL: |
| 394 | $port='3306'; |
| 395 | break; |
| 396 | case DBMS_POSTGRES: |
| 397 | $port='5432'; |
| 398 | break; |
| 399 | case DBMS_ORACLE: |
| 400 | $port='1521'; |
| 401 | break; |
| 402 | case DBMS_SQLSERVER: |
| 403 | $port='1433'; |
| 404 | break; |
| 405 | default: |
| 406 | $msg = 'Name of DBMS (Data Base Management System) is wrong or not defined.'; |
| 407 | $msg = $msg.' Please see the list of DBMS in BASE/classes/constants.php'; |
| 408 | throw new InvalidArgumentException($msg); |
| 409 | break; |
| 410 | } |
| 411 | return $port; |
| 412 | } |
| 413 | |
| 414 | private static function setConfigDatabase($useConfigFile ,$configArray ,$configErrors){ |
| 415 | if( $useConfigFile ){ |
| 416 | if ( !defined( 'DATABASE' ) ) { |
| 417 | $dataBaseName = self::getDataBaseName(); |
| 418 | if( empty($dataBaseName) ){ |
| 419 | $configErrors[] = 'Falta informar o DATABASE'; |
| 420 | self::showExemplo( self::$banco, $configErrors ); |
| 421 | } |
| 422 | }else{ |
| 423 | self::setDataBaseName( DATABASE ); |
| 424 | } |
| 425 | } else { |
| 426 | $database = ArrayHelper::get($configArray, 'DATABASE'); |
| 427 | if( empty($database) ){ |
| 428 | $configErrors[] = 'Falta informar o DATABASE'; |
| 429 | self::showExemplo( self::$banco, $configErrors ); |
| 430 | }else{ |
| 431 | self::setDataBaseName( $database ); |
| 432 | } |
| 433 | } |
| 434 | return $configErrors; |
| 435 | } |
| 436 | |
| 437 | public static function setConfigUtf8Decode($useConfigFile,$configArray){ |
| 438 | if($useConfigFile){ |
| 439 | if ( is_null( self::$utfDecode ) && defined( 'UTF8_DECODE' ) ) { |
| 440 | self::setUtfDecode( UTF8_DECODE ); |
| 441 | } |
| 442 | }else{ |
| 443 | $utf8 = ArrayHelper::get($configArray, 'UTF8_DECODE'); |
| 444 | self::setUtfDecode($utf8); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | public static function setConfigHost($useConfigFile,$configArray){ |
| 449 | if($useConfigFile){ |
| 450 | if ( defined( 'HOST' ) ) { |
| 451 | self::setHost(HOST); |
| 452 | } |
| 453 | }else{ |
| 454 | $host = ArrayHelper::get($configArray, 'HOST'); |
| 455 | self::setHost($host); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | private static function setConfigUserAndPassword($useConfigFile, $configArray) |
| 460 | { |
| 461 | if ( !defined( 'USE_SESSION_LOGIN' ) ) { |
| 462 | define( 'USE_SESSION_LOGIN', 0 ); |
| 463 | } |
| 464 | |
| 465 | if ( USE_SESSION_LOGIN ) { |
| 466 | if ( !isset( $_SESSION[ APLICATIVO ][ 'login' ][ 'password' ] ) ) { |
| 467 | die ( 'Para utilizar usuário e senha do usuário logado,<br>defina as varíaveis de sessão:<b>$_SESSION[APLICATIVO]["login"]["username"]</b> e <b>$_SESSION[APLICATIVO]["login"]["password"]</b>.' ); |
| 468 | } |
| 469 | self::$password = $_SESSION[ APLICATIVO ][ 'login' ][ 'password' ]; |
| 470 | self::$username = $_SESSION[ APLICATIVO ][ 'login' ][ 'username' ]; |
| 471 | } else { |
| 472 | if( $useConfigFile ){ |
| 473 | |
| 474 | if ( !defined( 'SENHA' ) ) { |
| 475 | define( 'SENHA', NULL ); |
| 476 | } |
| 477 | |
| 478 | if ( !defined( 'USUARIO' ) ) { |
| 479 | define( 'USUARIO', NULL ); |
| 480 | } |
| 481 | |
| 482 | self::$password = SENHA; |
| 483 | self::$username = USUARIO; |
| 484 | } else { |
| 485 | $usuario = ArrayHelper::get($configArray, 'USUARIO'); |
| 486 | $user = ArrayHelper::get($configArray, 'USERNAME'); |
| 487 | $username = empty($usuario)?$user:$usuario; |
| 488 | self::$username = $username; |
| 489 | |
| 490 | $senha = ArrayHelper::get($configArray, 'SENHA'); |
| 491 | $pass = ArrayHelper::get($configArray, 'PASSWORD'); |
| 492 | $password = empty($senha)?$pass:$senha; |
| 493 | self::$password = $password; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | private static function useSimpleDBMS($configErrors) |
| 499 | { |
| 500 | if ( !self::simpleDBMS() ) { |
| 501 | if ( empty(self::getHost()) ) { |
| 502 | $configErrors[] = 'Falta informar o HOST'; |
| 503 | } |
| 504 | if( empty(self::$username) ){ |
| 505 | $configErrors[] = 'Falta informar um usuario para logar no banco'; |
| 506 | } |
| 507 | if( empty(self::$password) ){ |
| 508 | $configErrors[] = 'Falta informar uma senha para logar no banco'; |
| 509 | } |
| 510 | } |
| 511 | return $configErrors; |
| 512 | } |
| 513 | |
| 514 | /*** |
| 515 | * Data Base Management System is simple or not. |
| 516 | * Simple does not have user and password or host |
| 517 | * @return boolean |
| 518 | */ |
| 519 | private static function simpleDBMS() { |
| 520 | return (self::$banco != DBMS_FIREBIRD) || (self::$banco != DBMS_SQLITE); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Define DSN (Data source name) for connection. |
| 525 | * return implicit, in attributes of class: Host, Database Name and port. |
| 526 | * return explicit: array with errors of config. |
| 527 | * |
| 528 | * @param array $configErrors |
| 529 | * @param boolean $useConfigfile |
| 530 | * @return string |
| 531 | */ |
| 532 | private static function defineDsnPDO($configErrors,$useConfigfile = true) { |
| 533 | $host = self::getHost(); |
| 534 | $database = self::getDataBaseName(); |
| 535 | $port = self::getPort(); |
| 536 | |
| 537 | switch( self::getDBMS() ) { |
| 538 | case DBMS_MYSQL : |
| 539 | self::$dsn = 'mysql:host='.$host.';dbname='.$database.';port='.$port; |
| 540 | break; |
| 541 | //----------------------------------------------------------------------- |
| 542 | case DBMS_POSTGRES : |
| 543 | self::$dsn = 'pgsql:host='.$host.';dbname='.$database.';port='.$port; |
| 544 | break; |
| 545 | //----------------------------------------------------------------------- |
| 546 | case DBMS_SQLITE: |
| 547 | //self::validateFileDataBaseExists ( $configErrors ); |
| 548 | self::$dsn = 'sqlite:'.$database; |
| 549 | break; |
| 550 | //----------------------------------------------------------------------- |
| 551 | case DBMS_ORACLE: |
| 552 | self::$dsn = "oci:dbname=(DESCRIPTION =(ADDRESS_LIST=(ADDRESS = (PROTOCOL = TCP)(HOST = ".$host. ")(PORT = ".$port.")))(CONNECT_DATA =(SERVICE_NAME = " . SERVICE_NAME . ")))"; |
| 553 | //self::$dsn = "oci:dbname=".SERVICE_NAME; |
| 554 | break; |
| 555 | //---------------------------------------------------------- |
| 556 | case DBMS_SQLSERVER: |
| 557 | /** |
| 558 | * Dica de Reinaldo A. Barrêto Junior para utilizar o sql server no linux |
| 559 | * |
| 560 | * No PHP 5.4 ou superior o drive mudou de MSSQL para SQLSRV |
| 561 | * */ |
| 562 | if (PHP_OS == "Linux") { |
| 563 | if (version_compare(PHP_VERSION, '7.0.0') >= 0) { |
| 564 | $driver = 'sqlsrv'; |
| 565 | self::$dsn = $driver.':Server='.$host.','.$port.';Database='.$database; |
| 566 | } else { |
| 567 | $driver = 'dblib'; |
| 568 | //self::$dsn = $driver.':version=7.2;charset=UTF-8;host=' . HOST . ';dbname=' . DATABASE . ';port=' . PORT; |
| 569 | self::$dsn = $driver.':version=7.2;host='.$host.';dbname='.$database.';port='.$port; |
| 570 | } |
| 571 | } else { |
| 572 | $driver = 'sqlsrv'; |
| 573 | //self::$dsn = $driver.':Server='.$host.','.$port.';Database='.$database; |
| 574 | self::$dsn = $driver.':Server='.$host.';Database='.$database; |
| 575 | } |
| 576 | break; |
| 577 | //---------------------------------------------------------- |
| 578 | case DBMS_FIREBIRD: |
| 579 | //self::validateFileDataBaseExists ( $configErrors ); |
| 580 | self::$dsn = 'firebird:dbname='.$database; |
| 581 | break; |
| 582 | //---------------------------------------------------------- |
| 583 | case 'MSACCESS': |
| 584 | case DBMS_ACCESS: |
| 585 | //self::fileDataBaseExists ( $configErrors ); |
| 586 | self::$dsn = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq='.$database.';Uid='.self::$username.';Pwd='.self::$password; |
| 587 | break; |
| 588 | //---------------------------------------------------------- |
| 589 | default: |
| 590 | $configErrors[] = 'Falta informar o sistema gerenciador de Banco de Dados: Access, Firebird, MySQL, Oracle, PostGresSQL, SQL Lite ou SQL Server'; |
| 591 | break; |
| 592 | //---------------------------------------------------------- |
| 593 | } |
| 594 | return $configErrors; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * @param configErrors |
| 599 | */ |
| 600 | private static function validateFileDataBaseExists($configErrors) { |
| 601 | $dataBaseName = self::getDataBaseName(); |
| 602 | $homeUrl = UrlHelper::homeUrl(); |
| 603 | $path = $homeUrl.$dataBaseName; |
| 604 | $file_exists = file_exists( $path ); |
| 605 | if ( !$file_exists ) { |
| 606 | $configErrors[] = 'Arquivo ' . DATABASE . ' não encontrado!'; |
| 607 | self::showExemplo( self::$banco, $configErrors ); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | //------------------------------------------------------------------------------------------- |
| 612 | public static function showError( $error = null ) |
| 613 | { |
| 614 | if ( !is_null( $error ) ) |
| 615 | { |
| 616 | self::$error = $error; |
| 617 | } |
| 618 | |
| 619 | if ( self::$error ) |
| 620 | { |
| 621 | if ( self::getDieOnError() && ( !isset( $_REQUEST[ 'ajax' ] ) || !$_REQUEST[ 'ajax' ] ) ) |
| 622 | { |
| 623 | die ( self::getError()); |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | if ( self::getShowFormErrors() ) |
| 628 | { |
| 629 | echo self::getError(); |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | return null; |
| 634 | } |
| 635 | |
| 636 | //------------------------------------------------------------------------------------------- |
| 637 | public static function setError( $strNewValue = null ) |
| 638 | { |
| 639 | self::$error = $strNewValue; |
| 640 | } |
| 641 | |
| 642 | //------------------------------------------------------------------------------------------- |
| 643 | public static function getError() |
| 644 | { |
| 645 | if ( self::$error ) |
| 646 | { |
| 647 | return utf8_decode( '<br><b>Erro PDO:</b> ' . self::$error . '<br/><br/><b>Sql: </b>' . self::$lastSql . '<div><br/></div><b>Parametros: </b> ' . print_r( self::$lastParams, true ) ); |
| 648 | } |
| 649 | |
| 650 | if ( self::getMessage() ) |
| 651 | { |
| 652 | return self::getMessage(); |
| 653 | } |
| 654 | return null; |
| 655 | } |
| 656 | |
| 657 | //------------------------------------------------------------------------------------------- |
| 658 | public static function getInstance() |
| 659 | { |
| 660 | |
| 661 | if ( self::getDataBaseName() ) |
| 662 | { |
| 663 | if ( !self::$instance[ self::getDatabaseName()] ) |
| 664 | { |
| 665 | self::connect( null, false ); |
| 666 | if ( !self::$instance[ self::getDatabaseName()] ) |
| 667 | { |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | if( self::getSchema() ) |
| 672 | { |
| 673 | if( self::getDataBaseName() == 'POSTGRES') |
| 674 | { |
| 675 | self::executeSQL('set search_path='.self::getSchema()); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | return self::$instance[ self::getDatabaseName()]; |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | if ( !self::$instance ) |
| 684 | { |
| 685 | self::connect( null, false ); |
| 686 | |
| 687 | if ( !self::$instance ) |
| 688 | { |
| 689 | return false; |
| 690 | } |
| 691 | if( self::getSchema() ) |
| 692 | { |
| 693 | if( self::$banco == 'POSTGRES') |
| 694 | { |
| 695 | self::executeSQL('set search_path='.self::getSchema()); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | return self::$instance; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | //------------------------------------------------------------------------------------------ |
| 704 | private function __clone() { |
| 705 | } |
| 706 | |
| 707 | //------------------------------------------------------------------------------------------ |
| 708 | public static function executeSql( $sql, $arrParams = null, $fetchMode = PDO::FETCH_ASSOC, $boolUtfDecode = null ) { |
| 709 | if ( !self::getInstance() ) { |
| 710 | self::getError(); |
| 711 | throw new PDOException(self::$error); |
| 712 | return; |
| 713 | } |
| 714 | self::$error = null; |
| 715 | |
| 716 | // ajuste do teste para banco oracle |
| 717 | if ( $sql == 'select 1 as teste' && strtolower(BANCO)== 'oracle' ) { |
| 718 | $sql .= ' from dual'; |
| 719 | } |
| 720 | |
| 721 | // converter o parametro passado para array |
| 722 | if ( is_string( $arrParams ) || is_numeric( $arrParams ) ) { |
| 723 | $arrParams = array( $arrParams ); |
| 724 | } |
| 725 | $result = null; |
| 726 | |
| 727 | // nás chamadas ajax, não precisa aplicar utf8 |
| 728 | if ( !isset( $_REQUEST[ 'ajax' ] ) || !isset( $_REQUEST[ 'ajax' ] ) ) { |
| 729 | $boolUtf8_Decode = self::getUtfDecode(); |
| 730 | $sql = self::getStrUtf8OrAnsi( $boolUtf8_Decode ,$sql ,self::WAY_APP2BANK ); |
| 731 | $arrParams = self::encodeArray( $arrParams ); |
| 732 | } |
| 733 | $arrParams = self::prepareArray( $arrParams ); |
| 734 | // guardar os parametros recebidos |
| 735 | self::$lastParams = $arrParams; |
| 736 | self::$lastSql = $sql; |
| 737 | |
| 738 | // verificar se a quantidade de parametros é igual a quantidade de variaveis |
| 739 | if ( strpos( $sql, '?' ) > 0 && is_array( $arrParams ) && count( $arrParams ) > 0 ) { |
| 740 | $qtd1 = substr_count( $sql, '?' ); |
| 741 | $qtd2 = count( $arrParams ); |
| 742 | |
| 743 | if ( $qtd1 != $qtd2 ) { |
| 744 | self::$error = 'Quantidade de parametros diferente da quantidade utilizada na instrução sql.'; |
| 745 | self::showError(); |
| 746 | return false; |
| 747 | } |
| 748 | } else { |
| 749 | $arrParams = array(); |
| 750 | } |
| 751 | |
| 752 | try { |
| 753 | $stmt = self::getInstance()->prepare( $sql ); |
| 754 | |
| 755 | if ( !$stmt ) { |
| 756 | self::$error = 'Erro no comando sql'; |
| 757 | self::showError(); |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | $result = $stmt->execute( $arrParams ); |
| 762 | |
| 763 | if ( $result ) { |
| 764 | if( !is_int($fetchMode) ){ |
| 765 | $fetchMode = PDO::FETCH_ASSOC; |
| 766 | } |
| 767 | |
| 768 | // em caso select ou insert com returning, processa o resultado |
| 769 | if ( preg_match( '/^select/i', $sql ) > 0 || preg_match( '/returning/i', $sql ) > 0 || preg_match( '/^with/i', $sql ) > 0 ) { |
| 770 | $res = $stmt->fetchAll( $fetchMode ); |
| 771 | $res = self::processResult( $res, $fetchMode, $boolUtfDecode ); |
| 772 | |
| 773 | if ( is_array( $res ) || is_object( $res ) ){ |
| 774 | return $res; |
| 775 | }else { |
| 776 | return null; |
| 777 | } |
| 778 | |
| 779 | // Para stored procedure do MS SQL Server |
| 780 | }else if( preg_match( '/^exec/i', $sql ) > 0 ){ |
| 781 | $res = array(); |
| 782 | /* |
| 783 | do { |
| 784 | $results = $stmt->fetchAll( $fetchMode ); |
| 785 | $res[] = self::processResult( $results, $fetchMode, $boolUtfDecode ); |
| 786 | } while ($stmt->nextRowset()); |
| 787 | */ |
| 788 | |
| 789 | //https://github.com/bjverde/formDin/issues/164 |
| 790 | while($stmt->columnCount()) { |
| 791 | $results = $stmt->fetchAll( $fetchMode ); |
| 792 | $res[] = self::processResult( $results, $fetchMode, $boolUtfDecode ); |
| 793 | $stmt->nextRowset(); |
| 794 | } |
| 795 | |
| 796 | if ( is_array( $res ) || is_object( $res ) ){ |
| 797 | return $res; |
| 798 | }else { |
| 799 | return null; |
| 800 | } |
| 801 | // Para stored procedure do MySQL |
| 802 | }else if( preg_match( '/^call/i', $sql ) > 0 ){ |
| 803 | $res = $stmt->fetchAll( $fetchMode ); |
| 804 | $res = self::processResult( $res, $fetchMode, $boolUtfDecode ); |
| 805 | |
| 806 | if ( is_array( $res ) || is_object( $res ) ){ |
| 807 | return $res; |
| 808 | }else { |
| 809 | return null; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | return $result; |
| 814 | } |
| 815 | catch( PDOException $e ) { |
| 816 | self::$error = $e->getMessage(); |
| 817 | MessageHelper::logRecordSimple(self::$error); |
| 818 | self::showError(); |
| 819 | } |
| 820 | return false; |
| 821 | } |
| 822 | |
| 823 | //-------------------------------------------------------------------------- |
| 824 | public static function prepare( $strSql ) { |
| 825 | if ( !self::getInstance() ) { |
| 826 | return false; |
| 827 | } |
| 828 | return self::getInstance()->prepare( $strSql ); |
| 829 | } |
| 830 | |
| 831 | //--------------------------------------------------------------------------- |
| 832 | public static function encodeArray( $arrDados = null ) { |
| 833 | $result = array(); |
| 834 | |
| 835 | if ( is_array( $arrDados ) ) { |
| 836 | if ( is_string( key( $arrDados ) ) ) { |
| 837 | foreach( $arrDados as $k => $v ) { |
| 838 | if ( ! is_null( $v ) ) { |
| 839 | |
| 840 | if( !self::isMySqlDbUtf8() ){ |
| 841 | $boolUtf8_DecodeDataBase = self::getUtfDecode(); |
| 842 | $arrDados[ $k ] = self::getStrUtf8OrAnsi($boolUtf8_DecodeDataBase, $v ,self::WAY_APP2BANK); |
| 843 | } |
| 844 | |
| 845 | // inverter campo data |
| 846 | if ( preg_match( '/^DAT[_,A]/i', $k ) > 0 || ( strpos( $v, '/' ) == 2 && strpos( $v, '/', 4 ) == 5 ) ) { |
| 847 | $v = self::verifyformtDateYMD( $v ); |
| 848 | $arrDados[ $k ] = $v; |
| 849 | } else if( preg_match( '/VAL_|NUM_/i', $k ) > 0 ) { |
| 850 | // alterar a virgula por ponto nos campos decimais |
| 851 | $posPonto = ( int ) strpos( $v, '.' ); |
| 852 | $posVirgula = ( int ) strpos( $v, ',' ); |
| 853 | |
| 854 | if ( $posVirgula > $posPonto ) { |
| 855 | if ( $posPonto && $posVirgula && $posPonto > $posVirgula ) { |
| 856 | $v = preg_replace( '/\,/', '', $v ); |
| 857 | } else { |
| 858 | $v = preg_replace( '/,/', ' ', $v ); |
| 859 | $v = preg_replace( '/\./', '', $v ); |
| 860 | $v = preg_replace( '/ /', '.', $v ); |
| 861 | } |
| 862 | } |
| 863 | $arrDados[ $k ] = trim( $v ); |
| 864 | } |
| 865 | } else { |
| 866 | $arrDados[ $k ] = null; |
| 867 | } |
| 868 | $result[] = $arrDados[ $k ]; |
| 869 | } |
| 870 | } else { |
| 871 | foreach( $arrDados as $k => $v ) { |
| 872 | if ( !is_null($v) && !empty($v) ){ |
| 873 | $v = self::verifyformtDateYMD( $v ); |
| 874 | if( !self::isMySqlDbUtf8() ){ |
| 875 | $boolUtf8_DecodeDataBase = self::getUtfDecode(); |
| 876 | $arrDados[ $k ] = self::getStrUtf8OrAnsi($boolUtf8_DecodeDataBase, $v,self::WAY_APP2BANK); |
| 877 | } |
| 878 | }else if( is_int($v) ){ |
| 879 | $arrDados[ $k ] = $v; |
| 880 | }else if( $v === '0' ){ |
| 881 | $arrDados[ $k ] = $v; |
| 882 | }else { |
| 883 | $arrDados[ $k ] = null; |
| 884 | } |
| 885 | } |
| 886 | $result = $arrDados; |
| 887 | } |
| 888 | } |
| 889 | return $result; |
| 890 | } |
| 891 | |
| 892 | public static function prepareArrayNamed( $arrDados = null ) { |
| 893 | $result = array(); |
| 894 | |
| 895 | foreach( $arrDados as $k => $v ) { |
| 896 | if ( !is_null($v) ) { |
| 897 | $arrDados[ $k ] = $v; |
| 898 | // inverter campo data |
| 899 | if ( preg_match( '/^DAT[_,A]/i', $k ) > 0 || ( strpos( $v, '/' ) == 2 && strpos( $v, '/', 4 ) == 5 ) ) { |
| 900 | $v = self::verifyformtDateYMD( $v ); |
| 901 | $arrDados[ $k ] = $v; |
| 902 | } else if( preg_match( '/NR_|VAL_|NUM_/i', $k ) > 0 ) { |
| 903 | // alterar a virgula por ponto nos campos decimais |
| 904 | $posPonto = ( int ) strpos( $v, '.' ); |
| 905 | $posVirgula = ( int ) strpos( $v, ',' ); |
| 906 | |
| 907 | if ( $posVirgula > $posPonto ) { |
| 908 | if ( $posPonto && $posVirgula && $posPonto > $posVirgula ) { |
| 909 | $v = preg_replace( '/\,/', '', $v ); |
| 910 | } else { |
| 911 | $v = preg_replace( '/,/', ' ', $v ); |
| 912 | $v = preg_replace( '/\./', '', $v ); |
| 913 | $v = preg_replace( '/ /', '.', $v ); |
| 914 | } |
| 915 | } |
| 916 | $arrDados[ $k ] = trim( $v ); |
| 917 | } |
| 918 | } else { |
| 919 | $arrDados[ $k ] = null; |
| 920 | } |
| 921 | $result[] = $arrDados[ $k ]; |
| 922 | } |
| 923 | return $result; |
| 924 | } |
| 925 | |
| 926 | /*** |
| 927 | * Campo data deve ser invertido para gravar no banco de dados. |
| 928 | * @param string $v |
| 929 | * @return string |
| 930 | */ |
| 931 | public static function verifyformtDateYMD( $v ) { |
| 932 | if ( strpos( $v, '/' ) == 2 && strpos( $v, '/', 4 ) == 5 ) { |
| 933 | $v = self::formatDate( $v, 'ymd' ); |
| 934 | } |
| 935 | return $v; |
| 936 | } |
| 937 | |
| 938 | public static function prepareArray( $arrDados = null ) { |
| 939 | $result = array(); |
| 940 | |
| 941 | if ( is_array( $arrDados ) ) { |
| 942 | |
| 943 | if ( is_string( key( $arrDados ) ) ) { |
| 944 | $result = self::prepareArrayNamed($arrDados); |
| 945 | } else { |
| 946 | foreach( $arrDados as $k => $v ) { |
| 947 | if ( !is_null($v) && !empty($v) ){ |
| 948 | $v = self::verifyformtDateYMD( $v ); |
| 949 | $arrDados[ $k ] = $v; |
| 950 | } else if( is_int($v) ) { |
| 951 | $arrDados[ $k ] = $v; |
| 952 | } else if( $v === '0' ) { |
| 953 | $arrDados[ $k ] = $v; |
| 954 | } else { |
| 955 | $arrDados[ $k ] = null; |
| 956 | } |
| 957 | } |
| 958 | $result = $arrDados; |
| 959 | } |
| 960 | } |
| 961 | return $result; |
| 962 | } |
| 963 | |
| 964 | //------------------------------------------------------------------------------ |
| 965 | /** |
| 966 | * Localiza a pasta base da framework |
| 967 | * |
| 968 | */ |
| 969 | private static function getRoot() |
| 970 | { |
| 971 | $base = ''; |
| 972 | |
| 973 | for( $i = 0; $i < 10; $i++ ) { |
| 974 | $base = str_repeat( '../', $i ) . 'base/'; |
| 975 | if ( file_exists( $base ) ){ |
| 976 | $i = 20; |
| 977 | break; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | if ( !file_exists( $base ) ) { |
| 982 | $msg = 'pasta base/ não encontrada! ' |
| 983 | .__FILE__.','.__LINE__.','.__METHOD__; |
| 984 | throw new Exception($msg); |
| 985 | } |
| 986 | |
| 987 | $base = str_replace( 'base', '', $base ); |
| 988 | $base = str_replace( '//', '/', $base ); |
| 989 | $root = ( $base == '/' ) ? './' : $base; |
| 990 | return $root; |
| 991 | } |
| 992 | |
| 993 | //------------------------------------------------------------------------------------------ |
| 994 | public static function showExemplo( $banco, $arrErros = null ) { |
| 995 | $msgErro = ''; |
| 996 | |
| 997 | if ( is_array( $arrErros ) ) { |
| 998 | $msgErro = implode( '<br>', $arrErros ); |
| 999 | } |
| 1000 | $html = '<div style="padding:5px;border:1px solid red;background-color:lightyellow;width:400px;color:blue;">'; |
| 1001 | $html .= '<div style="border-bottom:1px solid blue;color:red;text-align:center;"><blink>' . $msgErro . '</blink></div>'; |
| 1002 | |
| 1003 | switch( $banco ) { |
| 1004 | case DBMS_ORACLE: |
| 1005 | $html .= "<center>Exemplo de configuração para conexão com banco ORACLE</center><br> |
| 1006 | define('BANCO','ORACLE');<br> |
| 1007 | define('HOST','192.168.0.132');<br> |
| 1008 | define('PORT','1521');<br> |
| 1009 | define('SERVICE_NAME','xe');<br> |
| 1010 | define('USUARIO','root');<br> |
| 1011 | define('SENHA','root');<br><br>"; |
| 1012 | break; |
| 1013 | |
| 1014 | case DBMS_MYSQL: |
| 1015 | $html .= "<center>Exemplo de configuração para conexão com banco MYSQL</center><br> |
| 1016 | define('BANCO','MYSQL');<br> |
| 1017 | define('HOST','192.168.0.132');<br> |
| 1018 | define('PORT','3306');<br> |
| 1019 | define('DATABASE','exemplo');<br> |
| 1020 | define('UTF8_DECODE',0);<br> |
| 1021 | define('USUARIO','root');<br> |
| 1022 | define('SENHA','root');<br><br>"; |
| 1023 | break; |
| 1024 | |
| 1025 | case DBMS_POSTGRES: |
| 1026 | $html .= "<center>Exemplo de configuração para conexão com banco POSTGRES</center><br> |
| 1027 | define('BANCO','POSTGRES');<br> |
| 1028 | define('HOST','192.168.0.132');<br> |
| 1029 | define('PORT','5432');<br> |
| 1030 | define('DATABASE','exemplo');<br> |
| 1031 | define('SCHEMA','public');<br> |
| 1032 | define('USUARIO','postgres');<br> |
| 1033 | define('SENHA','123456');<br><br>"; |
| 1034 | break; |
| 1035 | |
| 1036 | case DBMS_SQLITE: |
| 1037 | $html .= "<center>Exemplo de configuração para conexão com banco SQLITE</center><br> |
| 1038 | define('BANCO','SQLITE');<br> |
| 1039 | define('DATABASE','includes/exemplo.s3db');<br> |
| 1040 | define('UTF8_DECODE',0);<br>"; |
| 1041 | break; |
| 1042 | |
| 1043 | case DBMS_FIREBIRD: |
| 1044 | $html .= "<center>Exemplo de configuração para conexão com banco FIREBIRD</center><br> |
| 1045 | define('BANCO','FIREBIRD');<br> |
| 1046 | define('DATABASE','C://bd//DBTESTE.FDB');<br>; |
| 1047 | define('UTF8_DECODE',0);<br> |
| 1048 | define('USUARIO','SYSDBA');<br> |
| 1049 | define('SENHA','masterkey');<br>"; |
| 1050 | break; |
| 1051 | |
| 1052 | case DBMS_SQLSERVER: |
| 1053 | $html .= "<center>Exemplo de configuração para conexão com banco SQLSERVER</center><br> |
| 1054 | define('BANCO','SQLSERVER');<br> |
| 1055 | define('HOST','192.168.0.132');<br> |
| 1056 | define('PORT','1433');<br> |
| 1057 | define('DATABASE','exemplo');<br> |
| 1058 | define('UTF8_DECODE',0);<br> |
| 1059 | define('USUARIO','sa');<br> |
| 1060 | define('SENHA','123456');<br><br>"; |
| 1061 | break; |
| 1062 | |
| 1063 | case 'ACCESS': |
| 1064 | $html .= "<center>Exemplo de configuração para conexão com banco ACCES</center><br> |
| 1065 | define('DATABASE','C://bd//DBTESTE.MDB');<br> |
| 1066 | define('UTF8_DECODE',0);<br> |
| 1067 | define('USUARIO','admin');<br> |
| 1068 | define('SENHA','123456');<br><br>"; |
| 1069 | break; |
| 1070 | |
| 1071 | } |
| 1072 | //--------------------- |
| 1073 | $html . '</div>'; |
| 1074 | die ( $html ); |
| 1075 | } |
| 1076 | |
| 1077 | //--------------------------------------------------- |
| 1078 | public static function test( $boolDie = null ) |
| 1079 | { |
| 1080 | $res = self::executeSql( 'select 1 as teste' ); |
| 1081 | if ( $res ) |
| 1082 | { |
| 1083 | echo '<H2>FORMDIN - Teste de conexão com banco de dados.<br>Arquivo de Configuração utilizado: '.self::$configFile.'<br></h2><h3>DNS:'.self::$dsn.'</h3><h1>Conexão com ' . BANCO . ' está Ok!!!!</h1>'; |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | echo '<H2>FORMDIN - Teste de conexão com banco de dados.<br>Arquivo de Configuração utilizado: '.self::$configFile.'<br></h2></h3>'; |
| 1088 | echo '<br><h3>Falha na conexão.<br/><br/>' . self::getError().'</h3>'; |
| 1089 | } |
| 1090 | |
| 1091 | if ( is_null( $boolDie ) || $boolDie ) |
| 1092 | { |
| 1093 | die ( '<hr>' ); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | //---------------------------------------------------- |
| 1098 | public static function beginTransaction() |
| 1099 | { |
| 1100 | if ( self::getInstance() ) |
| 1101 | { |
| 1102 | return self::getInstance()->beginTransaction(); |
| 1103 | } |
| 1104 | return false; |
| 1105 | } |
| 1106 | |
| 1107 | //--------------------------------------------------- |
| 1108 | public static function commit() |
| 1109 | { |
| 1110 | if ( self::getInstance() ) |
| 1111 | { |
| 1112 | return self::getInstance()->commit(); |
| 1113 | } |
| 1114 | return false; |
| 1115 | } |
| 1116 | |
| 1117 | //--------------------------------------------------- |
| 1118 | public static function rollBack() |
| 1119 | { |
| 1120 | if ( self::getInstance() ) |
| 1121 | { |
| 1122 | return self::getInstance()->rollBack(); |
| 1123 | } |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | //-------------------------------------------------------------------------------------- |
| 1128 | public static function getLastId( $strTable, $strKeyField ) |
| 1129 | { |
| 1130 | $res = self::executeSql( "select max( {$strKeyField} ) as last_id from {$strTable}" ); |
| 1131 | return $res[ 'LAST_ID' ][ 0 ]; |
| 1132 | } |
| 1133 | //-------------------------------------------------------------------------------------- |
| 1134 | public static function formatDate( $date = null, $format = 'dmy' ) |
| 1135 | { |
| 1136 | if ( $date ) |
| 1137 | { |
| 1138 | $format = is_null( $format ) ? 'dmy' : strtolower( $format ); |
| 1139 | |
| 1140 | if ( $format != 'dmy' && $format != 'ymd' ) |
| 1141 | { |
| 1142 | $format = 'dmy'; |
| 1143 | } |
| 1144 | if ( preg_match( '/\//', $date ) > 0 ) |
| 1145 | { |
| 1146 | $delim = '/'; |
| 1147 | } |
| 1148 | else if( preg_match( '/-/', $date ) > 0 ) |
| 1149 | { |
| 1150 | $delim = '-'; |
| 1151 | } |
| 1152 | $aDataHora = explode( ' ', $date ); |
| 1153 | $aDMY = explode( $delim, $aDataHora[ 0 ] ); |
| 1154 | |
| 1155 | // esta no formaty ymd |
| 1156 | if ( preg_match( '/^[0-9]{4}/', $date ) ) |
| 1157 | { |
| 1158 | if ( $format == 'ymd' ) |
| 1159 | { |
| 1160 | return $date; |
| 1161 | } |
| 1162 | } |
| 1163 | else |
| 1164 | { |
| 1165 | if ( $format == 'dmy' ) |
| 1166 | { |
| 1167 | return $date; |
| 1168 | } |
| 1169 | } |
| 1170 | $delim = '-'; |
| 1171 | $date = $aDMY[ 2 ] . $delim . $aDMY[ 1 ] . $delim . $aDMY[ 0 ] . ( isset( $aDataHora[ 1 ] ) ? ' ' . $aDataHora[ 1 ] : '' ); |
| 1172 | } |
| 1173 | return $date; |
| 1174 | } |
| 1175 | //-------------------------------------------------------------------------------------- |
| 1176 | public static function dmy( $date = null ) |
| 1177 | { |
| 1178 | if ( $date ) |
| 1179 | { |
| 1180 | // verificar se não está invertida |
| 1181 | if ( !preg_match( '/^[0-9]{4}/', $date ) ) |
| 1182 | { |
| 1183 | // inverter campo data |
| 1184 | if ( preg_match( '/\//', $date ) > 0 ) |
| 1185 | { |
| 1186 | $delim = '/'; |
| 1187 | } |
| 1188 | else if( preg_match( '/-/', $date ) > 0 ) |
| 1189 | { |
| 1190 | $delim = '-'; |
| 1191 | } |
| 1192 | $aDataHora = explode( ' ', $date ); |
| 1193 | $aDMY = explode( $delim, $aDataHora[ 0 ] ); |
| 1194 | $date = $aDMY[ 2 ] . $delim . $aDMY[ 1 ] . $delim . $aDMY[ 0 ] . ( isset( $aDataHora[ 1 ] ) ? ' ' . $aDataHora[ 1 ] : '' ); |
| 1195 | } |
| 1196 | } |
| 1197 | return $date; |
| 1198 | } |
| 1199 | //-------------------------------------------------------------------------------------- |
| 1200 | /** |
| 1201 | * Returns the string in the appropriate format UTF8 or ANSI |
| 1202 | * @param boolean $boolUtf8_Decode |
| 1203 | * @param string $string |
| 1204 | * @param string $way use the const self::WAY_APP2DB or self::WAY_DB2APP |
| 1205 | * @return NULL|string |
| 1206 | */ |
| 1207 | public static function getStrUtf8OrAnsi( $boolUtf8_Decode ,$string ,$way) |
| 1208 | { |
| 1209 | $retorno = $string; |
| 1210 | if( (self::$banco == DBMS_SQLSERVER) && (PHP_OS != "Linux" ) ){ |
| 1211 | $retorno = $string; |
| 1212 | }elseif ( (self::$banco == DBMS_SQLSERVER) && (PHP_OS == "Linux" ) && (version_compare(PHP_VERSION, '7.0.0') >= 0) ) { |
| 1213 | $retorno = $string; |
| 1214 | }elseif (self::$banco == DBMS_SQLITE) { |
| 1215 | $retorno = $string; |
| 1216 | } |
| 1217 | elseif( self::isMySqlDbUtf8() ){ |
| 1218 | $retorno = $string; |
| 1219 | } else{ |
| 1220 | if ( $boolUtf8_Decode ) { |
| 1221 | if($way == self::WAY_APP2BANK){ |
| 1222 | $retorno = utf8_decode($string); |
| 1223 | }else{ |
| 1224 | $retorno = StringHelper::str2utf8($string); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | return $retorno; |
| 1229 | } |
| 1230 | //-------------------------------------------------------------------------------------- |
| 1231 | public static function processResult( $result, $fetchMode, $boolUtf8_Decode = null ) { |
| 1232 | $boolUtf8_Decode = ( $boolUtf8_Decode === null ? self::getUtfDecode() : $boolUtf8_Decode ); |
| 1233 | |
| 1234 | // formato vo |
| 1235 | if ($result && $fetchMode == PDO::FETCH_OBJ) { |
| 1236 | if (count ( $result ) == 1) { |
| 1237 | return $result [0]; |
| 1238 | } |
| 1239 | return $result; |
| 1240 | } |
| 1241 | $res = null; |
| 1242 | |
| 1243 | if ( is_array( $result ) ) { |
| 1244 | foreach( $result as $key => $val ) { |
| 1245 | foreach( $val as $k => $v ) { |
| 1246 | $k = strtoupper( self::getStrUtf8OrAnsi( $boolUtf8_Decode ,$k ,self::WAY_BANK2APP) ); |
| 1247 | |
| 1248 | // transformar tags"< >" em codigo html para não serem interpretadas |
| 1249 | if ( is_string( $v ) ) { |
| 1250 | $res[ $k ][ $key ] = self::getStrUtf8OrAnsi( $boolUtf8_Decode ,$v ,self::WAY_BANK2APP ); |
| 1251 | |
| 1252 | //$res[ $k ][ $key ] = utf8_decode($v); |
| 1253 | // consertar ordem do campo data |
| 1254 | if ( preg_match( '/DAT/i', $k ) > 0 ) { |
| 1255 | $delim = null; |
| 1256 | |
| 1257 | if ( preg_match( '/\//', $v ) > 0 ) { |
| 1258 | $delim = '/'; |
| 1259 | } else if( preg_match( '/-/', $v ) > 0 ) { |
| 1260 | $delim = '-'; |
| 1261 | } |
| 1262 | |
| 1263 | if ( $delim ) { |
| 1264 | $aDataHora = explode( ' ', $v ); |
| 1265 | $aDMY = explode( $delim, $aDataHora[ 0 ] ); |
| 1266 | // verificar se está invertida |
| 1267 | $delim = '/'; |
| 1268 | |
| 1269 | if ( preg_match( '/^[0-9]{4}/', $v ) ) { |
| 1270 | $res[ $k ][ $key ] = $aDMY[ 2 ] . $delim . $aDMY[ 1 ] . $delim . $aDMY[ 0 ] . ( isset( $aDataHora[ 1 ] ) ? ' ' . $aDataHora[ 1 ] : '' ); |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | } else { |
| 1275 | $res[ $k ][ $key ] = $v; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | return $res; |
| 1281 | } |
| 1282 | //-------------------------------------------------------------------------------------- |
| 1283 | public static function pgsqlLOBOpen($oid = null, $mode = null) { |
| 1284 | return self::getInstance()->pgsqlLOBOpen ( $oid, $mode ); |
| 1285 | } |
| 1286 | //-------------------------------------------------------------------------------------- |
| 1287 | public static function pgsqlLOBCreate() { |
| 1288 | return self::getInstance()->pgsqlLOBCreate (); |
| 1289 | } |
| 1290 | //-------------------------------------------------------------------------------------- |
| 1291 | public static function setDieOnError( $boolNewValue = null ) { |
| 1292 | self::$dieOnError = $boolNewValue; |
| 1293 | } |
| 1294 | public static function getDieOnError() { |
| 1295 | return is_null( self::$dieOnError ) ? true : self::$dieOnError; |
| 1296 | } |
| 1297 | public static function setShowFormErrors( $boolNewValue = null ) { |
| 1298 | self::$showFormErrors = $boolNewValue; |
| 1299 | } |
| 1300 | public static function getShowFormErrors() { |
| 1301 | return is_null( self::$showFormErrors ) ? true : self::$showFormErrors; |
| 1302 | } |
| 1303 | public static function setMessage( $strNewValue = null) { |
| 1304 | self::$message = $strNewValue; |
| 1305 | } |
| 1306 | public static function getMessage() { |
| 1307 | return self::$message; |
| 1308 | } |
| 1309 | public static function setSchema( $newSchema = null) { |
| 1310 | self::$schema = $newSchema; |
| 1311 | } |
| 1312 | public static function getSchema() { |
| 1313 | return self::$schema; |
| 1314 | } |
| 1315 | } |
| 1316 | ?> |