#!/usr/bin/php -q strtotime(CONF_DATA_HORA_FIM)){ continue; } unset($values[15],$values[18],$values[19],$values[20],$values[21],$values[22],$values[23]); foreach ($values as $value) { $data .= !$data ? __NullDb($value) : ("," . __NullDb($value)); } $query = str_replace("{values}", $data, $queryModel); if (!pg_query($query)) { GeraExcept("Erro: {$query}", true); } } catch (Exception $ex) { $erroImport++; WriteLog($ex->getMessage(), $pathLog); echo $ex->getMessage() . "\n"; } } $query = "insert into ast_bilhetes(accountcode, src, dst, dcontext, clid, channel, dstchannel, lastapp, lastdata, calldate, duration, billsec, disposition, amaflags, uniqueid, userfield) select coalesce(accountcode, ''), coalesce(src, ''), coalesce(dst, ''), coalesce(dcontext, ''), clid, coalesce(channel, ''), coalesce(dstchannel, ''), coalesce(lastapp, ''), coalesce(lastdata, ''), calldate, duration, billsec, coalesce(disposition, ''), strtoint(amaflags) , uniqueid, coalesce(userfield, '') from cdr a where not exists(select '' from pbx_bilhetes where uniqueid = a.uniqueid);"; if (!pg_query($query)) { $inclusao = 'Erro'; GeraExcept("Erro ao carregar cdr", true); } } catch (Exception $ex) { echo $ex->getMessage() . "\n"; WriteLog($ex->getMessage(), $pathLog); } WriteLog(sprintf("Execucao encerrada: %ss Registros: %s Erros Processamento: %s Inclusao: %s ", round(time() - $time), $erroImport, $inclusao), $pathLog); function PreparaLinhaCdr($buffer) { $tamBuffer = strlen($buffer); $contaAspa = 0; $start = 0; $value = ''; $fields = array(); $ch = ''; $chOld = ''; $VerificaQuebra = function($numOcorencia) { return ($numOcorencia % 2) == 0; }; for ($i = 0; $i < $tamBuffer; $i++) { $ch = substr($buffer, $i, 1); if (($ch == '"') && ($chOld != '\\')) { $contaAspa++; } if (((($ch == ',')) && $VerificaQuebra($contaAspa)) || (($i + 1) == $tamBuffer)) { $contaAspa = 0; $fields[] = __RemoveAcentos(trim($value)); $value = ''; } if (((($ch != '"') && ($ch != ',')) || (($ch == ',') && !$VerificaQuebra($contaAspa))) || ($chOld == '\\')) { $value .= $ch; } $chOld = $ch; } return $fields; } function PreparaLinhaCdrOld($buffer) { $tamBuffer = strlen($buffer); $IniciaCampo = array(); $start = 0; $value = ''; $fields = array(); $VerificaQuebra = function($ar) { return (count($ar) % 2) == 0; }; for ($i = 0; $i < $tamBuffer; $i++) { $ch = substr($buffer, $i, 1); if ($ch == '"') { $IniciaCampo[] = $ch; } if (((($ch == ',')) && $VerificaQuebra($IniciaCampo)) || (($i + 1) == $tamBuffer)) { $IniciaCampo = array(); $fields[] = __RemoveAcentos($value); $value = ''; } if (($ch != '"') && ($ch != ',')) { $value .= $ch; } } return $fields; } function __GetConectString() { $dbPort = 5432; $dbHost = '127.0.0.1'; $dbName = 'pbx'; $dbUser = 'contacte'; $dbPassword = 'ctepgSQL'; return "host='$dbHost' port='$dbPort' dbname='$dbName' user='$dbUser' password='$dbPassword'"; } function __NullDb($value) { /* * Var nao esta setado eh empty * Zero eh empty * String vazia '' eh empty * String vazia "" eh empty * Espaco " " nao eh empty * False eh empty * Array vazio eh empty */ /* Valores empty que nao serao considerados nulos para o banco de dados */ if (is_numeric($value) && (((int) $value) === 0)) return __QuotedStr($value); return empty($value) ? 'null' : __QuotedStr($value); } function __QuotedStr($str) { return sprintf("'%s'", pg_escape_string(trim($str))); } function __RemoveAcentos($str, $upper = False) { $text = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý'); $subs = array('A', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'U', 'P', 'B', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y'); for ($i = 0; $i < strlen($str); $i++) { $j = array_search($str[$i], $text); if ($j) $str[$i] = $subs[$j]; } if ($upper) $str = strtoupper($str); return($str); }