|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require('funcoesFormulario.php');
|
|
|
|
|
$templateName = 'discador/naoPerturbeFile.tpl';
|
|
|
|
|
|
|
|
|
|
define("MAX_BUFFER_INSERT", 100000);
|
|
|
|
|
$tpLayout = 1;
|
|
|
|
|
$upload = 1;
|
|
|
|
|
|
|
|
|
|
$fieldsForm = array();
|
|
|
|
|
$fieldsForm = $_REQUEST;
|
|
|
|
|
$fieldsForm['erro'] = "";
|
|
|
|
|
$fieldsForm['msg'] = "Selecione um arquivo e clique em incluir!";
|
|
|
|
|
|
|
|
|
|
if (IsPostBack()) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// [np_file] => Array ( [name] => naoperturbe.txt [type] => text/plain [tmp_name] => /tmp/phpTnGMAd [error] => 0 [size] => 71486468
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$file = (object) $_FILES['np_file'];
|
|
|
|
|
|
|
|
|
|
if (empty($file->name) || !$file->size) {
|
|
|
|
|
GeraExcept("Para realizar esta opera<EFBFBD><EFBFBD>o <EFBFBD> necessario enviar um arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!file_exists($file->tmp_name)) {
|
|
|
|
|
GeraExcept("N<EFBFBD>o foi poss<EFBFBD>vel ler o arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Numero de linhas do aquivo original.
|
|
|
|
|
*/
|
|
|
|
|
$origLines = FileNumberLines($file->tmp_name);
|
|
|
|
|
|
|
|
|
|
$pathFile = "/var/www/html/aplicativo/integracao/arquivoCampanha/";
|
|
|
|
|
$fileName = sprintf("npb_%s_%s.npb", date('YmdHis'), GetLogin());
|
|
|
|
|
$fileProc = "/tmp/npb_proc.npb";
|
|
|
|
|
$fileData = "/tmp/npb_proc.tmp";
|
|
|
|
|
$fileTar = str_replace(".npb", ".tar.gz", $fileName);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Limpa e copia o arquivo.
|
|
|
|
|
*/
|
|
|
|
|
exec(sprintf("sed -e 's/[[:alpha:],[:space:],[:punct:]]//g' %s > %s", $file->tmp_name, $fileProc));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Numero de linhas do aquivo processado.
|
|
|
|
|
*/
|
|
|
|
|
$newLines = FileNumberLines($fileProc);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Elimina o arquivo temporario caso exista, e inclui linha para executar no psql.
|
|
|
|
|
|
|
|
|
|
exec(sprintf("rm -rf %s", $fileData));
|
|
|
|
|
exec(sprintf("sed -e '1i\' -e 'COPY pbx_nao_perturbe from stdin;' %s > %s", $fileProc, $fileData));
|
|
|
|
|
|
|
|
|
|
if (!file_exists($fileData)) {
|
|
|
|
|
GeraExcept("N<EFBFBD>o foi poss<EFBFBD>vel copiar o arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
* exec("mv {$fileData} /tmp/{$fileName}|tar -C /tmp -czf {$pathFile}{$fileTar} {$fileName}");
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
exec("mv {$fileProc} /tmp/{$fileName}|tar -C /tmp -czf {$pathFile}{$fileTar} {$fileName}");
|
|
|
|
|
|
|
|
|
|
if (!file_exists($pathFile . $fileTar)) {
|
|
|
|
|
GeraExcept("N<EFBFBD>o foi poss<EFBFBD>vel compactar o arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$buffer = '';
|
|
|
|
|
$insert = '';
|
|
|
|
|
|
|
|
|
|
$inTRan = 0;
|
|
|
|
|
if (!pg_query($dbcon, 'begin')) {
|
|
|
|
|
GeraExcept("N<EFBFBD>o foi poss<EFBFBD>vel iniciar uma transa<EFBFBD><EFBFBD>o!");
|
|
|
|
|
}
|
|
|
|
|
$inTRan = 1;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Md5 para verificar a integridade durante o download.
|
|
|
|
|
*/
|
|
|
|
|
$md5 = md5_file("{$pathFile}{$fileTar}");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Verifica se houve linhas eliminadas do aquivo original.
|
|
|
|
|
*/
|
|
|
|
|
$diffLines = $origLines - $newLines;
|
|
|
|
|
$npLog = "Arquivo processado, Linhas Excluidas: [{$diffLines}] Orig. Lines: {$origLines} New Lines: {$newLines}";
|
|
|
|
|
|
|
|
|
|
$query = "insert into pbx_nao_perturbe_controle(np_data, np_original, np_nome, np_path, np_num_linhas, np_md5, np_log)values(now()::date, %s, %s, %s, %s, %s, %s);";
|
|
|
|
|
$query = sprintf($query, QuotedStr($file->name), QuotedStr($fileName), QuotedStr("{$pathFile}{$fileTar}"), QuotedStr($newLines), QuotedStr($md5), QuotedStr($npLog));
|
|
|
|
|
|
|
|
|
|
if (!pg_query($dbcon, $query)) {
|
|
|
|
|
GeraExcept("N<EFBFBD>o foi poss<EFBFBD>vel gravar informa<EFBFBD><EFBFBD>es do arquivo!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pg_query($dbcon, 'commit')) {
|
|
|
|
|
GeraExcept("Arquivo importado com sucesso!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Apaga arquivos temporarios.
|
|
|
|
|
*/
|
|
|
|
|
exec("rm -rf /tmp/npb_*");
|
|
|
|
|
|
|
|
|
|
$fieldsForm['msg'] = "Arquivo carregado com sucesso!";
|
|
|
|
|
$jsStartup[] = "window.opener.ResetForm();";
|
|
|
|
|
|
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
|
$fieldsForm['msg'] = $ex->getMessage();
|
|
|
|
|
if ($inTran) {
|
|
|
|
|
!pg_query($dbcon, 'rollback');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Atualiza templates templates
|
|
|
|
|
*/
|
|
|
|
|
foreach ($fieldsForm as $key => $value) {
|
|
|
|
|
$smarty->assign($key, $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GetTemplate($smarty, $templateName);
|
|
|
|
|
?>
|