PABX da Simples IP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.3 KiB

<?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!";
$org_id = GetOrganizacao();
if (IsPostBack()) {
try {
$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);
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, org_id) VALUES(now()::date, %s, %s, %s, %s, %s, %s, %s);";
$query = sprintf($query, QuotedStr($file->name), QuotedStr($fileName), QuotedStr("{$pathFile}{$fileTar}"), QuotedStr($newLines), QuotedStr($md5), QuotedStr($npLog), $org_id);
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);