Example #1
0
/**
 * Function to deal with zip archives of uploaded files
 *
 * This function examines the zip archive, checks for unwanted files, unpacks
 * the files to the IBR_UPLOAD_DIR, and creates an array of file paths by the
 * type of file
 *
 * @uses ibr_upload_match_file()
 * @param string $zipfilename -- the $_FILES['file']['tmp_name'];
 * @param array $param_ar -- the parameters array, so we don't have to create it here
 * @param string &$html_str -- passed by reference for appending
 * @return array $f_ar -- paths to unpacked files accepted by this function
 */
function edih_ziptoarray($zipfilename, $param_ar, $single = false)
{
    // note that this function moves files and set permissions, so platform issues may occur
    //
    $html_str = '';
    $edih_upldir = csv_edih_tmpdir();
    //
    $zip_obj = new ZipArchive();
    // open archive (ZIPARCHIVE::CHECKCONS the ZIPARCHIVE::CREATE is supposedly necessary for microsoft)
    if ($zip_obj->open($zipfilename, ZIPARCHIVE::CHECKCONS) !== true) {
        //$html_str .= "Error: Could not open archive $zipfilename <br />" . PHP_EOL;
        csv_edihist_log('edih_ziptoarray: Error: Could not open archive ' . $zipfilename);
        $f_zr['reject'][] = array('name' => $zipfilename, 'comment' => 'Error: Could not open archive ' . $zipfilename);
        return $f_zr;
    }
    if ($zip_obj->status != 0) {
        $err .= "Error code: " . $zip_obj->status . " " . $zip_obj->getStatusString() . "<br />" . PHP_EOL;
        csv_edihist_log('edih_ziptoarray: ' . $zipfilename . ' ' . $err);
        $f_zr['reject'][] = array('name' => $zipfilename, 'comment' => $err);
        return $f_zr;
    }
    // initialize output array and counter
    $f_zr = array();
    $p_ct = 0;
    // get number of files
    $f_ct = $zip_obj->numFiles;
    if ($single && $f_ct > 1) {
        csv_edihist_log('edih_ziptoarray: Usage: only single zipped file accepted through this input');
        $f_zr['reject'][] = array('name' => $zipfilename, 'comment' => 'Usage: only single zipped file accepted through this input');
        return $f_zr;
    }
    // get the file names
    for ($i = 0; $i < $f_ct; $i++) {
        //
        $isOK = true;
        $fstr = "";
        $file = $zip_obj->statIndex($i);
        $name = $file['name'];
        $oldCrc = $file['crc'];
        // get file contents
        $fstr = stream_get_contents($zip_obj->getStream($name));
        if ($fstr) {
            // use only the file name
            $bnm = basename($name);
            $newname = tempnam($edih_upldir, 'edi');
            //
            // extract the file to unzip tmp dir with read/write access
            $chrs = file_put_contents($newname, $fstr);
            // test crc
            $newCrc = hexdec(hash_file("crc32b", $newname));
            //
            if ($newCrc !== $oldCrc && $oldCrc + 4294967296.0 !== $newCrc) {
                // failure case, mismatched crc file integrity values
                $html_str .= "CRC error: The files don't match! Removing file {$bnm} <br />" . PHP_EOL;
                $isGone = unlink($newname);
                if ($isGone) {
                    $is_tmpzip = false;
                    $html_str .= "File Removed {$bnm}<br />" . PHP_EOL;
                } else {
                    $html_str .= "Failed to removed file {$bnm}<br />" . PHP_EOL;
                }
            } else {
                // passed the CRC test, now type and verify file
                $fzp['name'] = $bnm;
                $fzp['tmp_name'] = $newname;
                // verification checks special to our application
                $f_uplz = edih_upload_match_file($param_ar, $fzp, $html_str);
                //
                if (is_array($f_uplz) && count($f_uplz)) {
                    if (isset($f_uplz['reject'])) {
                        $f_zr['reject'][] = $f_uplz['reject'];
                    } elseif (isset($f_uplz['name'])) {
                        $f_zr[$f_uplz['type']][] = $f_uplz['name'];
                        $p_ct++;
                    }
                } else {
                    // verification failed
                    $f_zr['reject'][] = array('name' => $fzp['name'], 'comment' => 'verification failed');
                }
            }
            //
        } else {
            csv_edihist_log("Did not get file contents {$name}");
            $isOK = false;
        }
    }
    // end for ($i=0; $i<$numFiles; $i++)
    //
    csv_edihist_log("Accepted {$p_ct} of {$f_ct} files from {$zipfilename}");
    //
    return $f_zr;
}
require_once "{$srcdir}/edihistory/ibr_status_code_arrays.php";
//dirname(__FILE__) . "/edihist/ibr_status_code_arrays.php");
require_once "{$srcdir}/edihistory/ibr_batch_read.php";
//dirname(__FILE__) . "/edihist/ibr_batch_read.php");
require_once "{$srcdir}/edihistory/ibr_ack_read.php";
//dirname(__FILE__) . "/edihist/ibr_ack_read.php");
require_once "{$srcdir}/edihistory/ibr_uploads.php";
//dirname(__FILE__) . "/edihist/ibr_uploads.php");
require_once "{$srcdir}/edihistory/ibr_io.php";
//dirname(__FILE__) . "/edihist/ibr_io.php");
//
// php may output line endings if include files are utf-8
ob_clean();
if (isset($GLOBALS['OE_SITE_DIR'])) {
    $ibr_upldir = csv_edih_basedir();
    $ibr_tmp = csv_edih_tmpdir();
} else {
    die("EDI History: Did not get directory path information!");
}
// if we are not set up, create directories and csv files
//if (!is_dir(dirname(__FILE__) . '/edihist' . IBR_HISTORY_DIR) ) {
if (!is_dir($ibr_upldir)) {
    //
    echo "setup with base directory: {$ibr_upldir} <br />" . PHP_EOL;
    $is_set = csv_setup($html_str);
    if (!$is_set) {
        print $html_str;
        csv_clear_tmpdir();
        exit;
    }
}
Example #3
0
/**
 * Empty all contents of tmp dir IBR_UPLOAD_DIR
 * 
 * @param  none
 * @return bool
 */
function csv_clear_tmpdir()
{
    //
    $edih_tmpdir = csv_edih_tmpdir();
    $tmp_files = scandir($edih_tmpdir);
    if (count($tmp_files)) {
        foreach ($tmp_files as $idx => $tmpf) {
            if ($tmpf == "." || $tmpf == "..") {
                // can't delete . and ..
                continue;
            }
            if (is_file($edih_tmpdir . DIRECTORY_SEPARATOR . $tmpf)) {
                unlink($edih_tmpdir . DIRECTORY_SEPARATOR . $tmpf);
                unset($tmp_files[$idx]);
            }
        }
    }
    if (count($tmp_files) > 2) {
        //
        csv_edihist_log("tmp dir contents remain in {$edih_tmpdir}");
        return FALSE;
    } else {
        return TRUE;
    }
}
Example #4
0
//dirname(__FILE__) . "/edihist/ibr_ack_read.php");
require_once "{$srcdir}/edihistory/edih_277_html.php";
require_once "{$srcdir}/edihistory/edih_278_html.php";
require_once "{$srcdir}/edihistory/edih_835_html.php";
//dirname(__FILE__) . "/edihist/ibr_era_read.php");
require_once "{$srcdir}/edihistory/codes/edih_271_code_class.php";
//dirname(__FILE__) . "/edihist/ibr_code_arrays.php");
require_once "{$srcdir}/edihistory/codes/edih_835_code_class.php";
//dirname(__FILE__) . "/edihist/ibr_status_code_arrays.php");
require_once "{$srcdir}/edihistory/codes/edih_997_codes.php";
//
// php may output line endings with included files
ob_clean();
if (isset($GLOBALS['OE_SITE_DIR'])) {
    $edih_base_dir = csv_edih_basedir();
    $edih_tmp_dir = csv_edih_tmpdir();
} else {
    die("EDI History: Did not get directory path information!");
}
// if we are not set up, create directories and csv files
//if (!is_dir(dirname(__FILE__) . '/edihist' . IBR_HISTORY_DIR) ) {
if (!is_dir($edih_tmp_dir)) {
    //
    //echo "setup with base directory: $edih_base_dir <br />" .PHP_EOL;
    if (csv_setup() == true) {
        $html_str = '';
        if (is_dir($edih_tmp_dir)) {
            csv_clear_tmpdir();
        }
    } else {
        print $html_str;
Example #5
0
/**
 * The main function in this ibr_archive.php script.  This function gets the parameters array
 * from csv_parameters() and calls the archiving functions on each type of file
 * in the parameters array.
 * 
 * @param string $archive_date    yyyy/mm/dd date prior to which is archived 
 * @return string                 descriptive message in html format
 */
function csv_archive_old($archive_date)
{
    //
    // paths
    $edih_dir = csv_edih_basedir();
    $archive_dir = $edih_dir . DIRECTORY_SEPARATOR . 'archive';
    $csv_dir = $edih_dir . DIRECTORY_SEPARATOR . 'csv';
    $tmp_dir = csv_edih_tmpdir();
    $tmp_dir .= $tmp_dir . DIRECTORY_SEPARATOR;
    //
    if (!is_dir($edih_dir . DIRECTORY_SEPARATOR . 'archive')) {
        // should have been created at setup
        mkdir($edih_dir . DIRECTORY_SEPARATOR . 'archive', 0755);
    }
    //
    $days = csv_days_prior($archive_date);
    if (!$days || $days < 90) {
        $out_html = "Archive date {$archive_date} invalid or less than 90 days prior <br />" . PHP_EOL;
        return $out_html;
    }
    //
    $out_html = "Archiving records prior to {$archive_date} <br />" . PHP_EOL;
    //
    $dt = str_replace('/', '', $archive_date);
    //
    $isarchived = FALSE;
    $haserr = FALSE;
    $params = csv_parameters();
    //
    $f_max = 200;
    //
    foreach ($params as $k => $p) {
        $type = $p['type'];
        $fdir = $p['directory'] . DIRECTORY_SEPARATOR;
        //
        $fn_ar = array();
        $arch_csv = array();
        $curr_csvd = array();
        //
        $archive_ar = array();
        //
        // type dpr has only a claim csv type
        $head_ar = $type == 'dpr' ? csv_files_header($type, 'claim') : csv_files_header($type, 'file');
        //
        $fncol = $p['fncolumn'];
        $datecol = $p['datecolumn'];
        //
        // files csv temporary names
        $file_csv = $p['files_csv'];
        $file_csv_copy = $tmp_dir . basename($file_csv);
        $tmp_fold_csv = $tmp_dir . $type . '_old_' . basename($file_csv);
        $tmp_fnew_csv = $tmp_dir . $type . '_new_' . basename($file_csv);
        $iscpf = copy($file_csv, $file_csv_copy);
        //
        // claims csv temporary names
        $claim_csv = $p['claims_csv'];
        $claim_csv_copy = $tmp_dir . basename($claim_csv);
        $tmp_cold_csv = $tmp_dir . $type . '_old_' . basename($claim_csv);
        $tmp_cnew_csv = $tmp_dir . $type . '_new_' . basename($claim_csv);
        $iscpc = copy($claim_csv, $claim_csv_copy);
        //
        if (!$iscpf || !$iscpc) {
            csv_edihist_log("csv_archive_old: copy to tmp dir failed for csv file {$type}");
            $out_html = "Archive temporary files operation failed ... aborting <br />" . PHP_EOL;
            return $out_html;
        }
        //
        // lock the original files
        $fh1 = fopen($file_csv, 'r');
        $islk1 = flock($fh1, LOCK_EX);
        if (!$islk1) {
            fclose($fh1);
        }
        // assume we are on a system that does not support locks
        $fh2 = fopen($claim_csv, 'r');
        $islk2 = flock($fh2, LOCK_EX);
        if (!$islk2) {
            fclose($fh2);
        }
        // assume we are on a system that does not support locks
        //
        // do the archive for the files_type.csv
        $archive_ar = csv_archive_array('file', $file_csv_copy, $datecol, $fncol, $dt);
        if (!$archive_ar) {
            csv_edihist_log("csv_archive_old: creating archive information failed for " . basename($file_csv_copy));
            continue;
        }
        $och = csv_rewrite_record($tmp_old_csv, $head_ar, $archive_ar['arch_csv']);
        $nch = csv_rewrite_record($tmp_new_csv, $head_ar, $archive_ar['curr_csv']);
        $zarch = csv_zip_dir($params, $archive_ar['files'], $archive_date);
        // now move the reconfigured files
        // unlink the present csv file, since it is possible for a rename error if it exists
        $islk1 = $islk1 ? flock($fh1, LOCK_UN) : $islk1;
        if ($islk1) {
            fclose($fh1);
        }
        $isunl = unlink($file_csv);
        if ($zarch) {
            // we got back the zip archive name from csv_zip_dir()
            $ismvz = rename($zarch, $archive_dir . DIRECTORY_SEPARATOR . basename($zarch));
            $ismvo = rename($tmp_fold_csv, $archive_dir . DIRECTORY_SEPARATOR . $dt . basename($tmp_fold_csv));
            $ismvn = rename($tmp_fnew_csv, $file_csv);
            //
            if ($ismvz && $ismvo && $ismvn) {
                // everything is working - clear out the files we put in tmp_dir
                // the tmp dir should be empty, but there might have been something else created there
                $isclr = csv_clear_tmpdir();
                $out_html .= "Archived: type {$type} <br />" . PHP_EOL;
                $out_html .= "&nbsp; archived " . count($archive_ar['files']) . " files\t <br />" . PHP_EOL;
                $out_html .= "&nbsp; archived " . count($archive_ar['arch_csv']) . " rows from " . basename($file_csv) . " <br />" . PHP_EOL;
                $out_html .= "&nbsp; there are now " . count($archive_ar['curr_csv']) . " rows in " . basename($file_csv) . " <br />" . PHP_EOL;
            } else {
                // in case or error, try to restore everything
                $fl_ar = csv_restore_files('file', $p, $archive_ar['files']);
                if (is_array($fl_ar) && count($fl_ar) > 0) {
                    foreach ($fl_ar as $f) {
                        csv_edihist_log("csv_archive_old: lost file {$f}");
                    }
                } elseif (is_array($fl_ar) && count($fl_ar) == 0) {
                    csv_edihist_log("csv_archive_old archiving failed, and files restored");
                } else {
                    csv_edihist_log("csv_archive_old archive failed and files were lost");
                }
                // give a message and quit
                $out_html .= "Archiving error: type {$type} archive errors ... aborting <br />" . PHP_EOL;
                return $out_html;
            }
        } else {
            // zip create error
            csv_edihist_log("csv_archive_old: creating zip archive failed for " . basename($file_csv));
            $fl_ar = csv_restore_files('file', $p, $archive_ar['files']);
            if (is_array($fl_ar) && count($fl_ar) > 0) {
                foreach ($fl_ar as $f) {
                    csv_edihist_log("csv_archive_old: lost file {$f}");
                }
            }
            $out_html .= "Archiving error: type {$type} archive errors ... aborting <br />" . PHP_EOL;
            return $out_html;
        }
        //
        // now we do the claims table
        //$cldate = date_create($archive_date);
        //date_sub($cldate, date_interval_create_from_date_string('1 month'));
        //$cldt = date_format($cldate, 'Ymd');
        //
        // dpr type has only claim table, treated as a file table above
        if ($type == 'dpr') {
            continue;
        }
        //
        $head_ar = csv_files_header($type, 'claim');
        //
        $archive_ar = csv_archive_array('claim', $claim_csv_copy, $datecol, $fncol, $dt);
        if (!$archive_ar) {
            csv_edihist_log("csv_archive_old: creating archive information failed for " . basename($file_csv_copy));
            continue;
        }
        //
        $och = csv_rewrite_record($tmp_cold_csv, $head_ar, $archive_ar['arch_csv']);
        $nch = csv_rewrite_record($tmp_cnew_csv, $head_ar, $archive_ar['curr_csv']);
        //
        $islk2 = $islk2 ? flock($fh2, LOCK_UN) : $islk2;
        if ($islk2) {
            fclose($fh2);
        }
        $isunl = unlink($claim_csv);
        $ismvo = rename($tmp_cold_csv, $archive_dir . DIRECTORY_SEPARATOR . $dt . basename($tmp_cold_csv));
        $ismvn = rename($tmp_cnew_csv, $claim_csv);
        //
        if ($ismvo && $ismvn) {
            // everything is working - clear out the files we put in tmp_dir
            // the tmp dir should be empty, but there might have been something else created there
            $isclr = csv_clear_tmpdir();
            $out_html .= "&nbsp; archived " . count($archive_ar['arch_csv']) . " rows from " . basename($claim_csv) . " <br />" . PHP_EOL;
            $out_html .= "&nbsp; there are now " . count($archive_ar['curr_csv']) . " rows in " . basename($claim_csv) . " <br />" . PHP_EOL;
        } else {
            $fl_ar = csv_restore_files('claim', $p, $archive_ar['files']);
            if (is_array($fl_ar) && count($fl_ar) > 0) {
                foreach ($fl_ar as $f) {
                    csv_edihist_log("csv_archive_old: lost file {$f}");
                }
            } elseif (is_array($fl_ar) && count($fl_ar) == 0) {
                csv_edihist_log("csv_archive_old: archiving failed, and files restored");
            } else {
                csv_edihist_log("csv_archive_old: archive failed and " . count($fl_ar) . " files were lost");
            }
            $out_html .= "Archiving error: type {$type} archive errors ... aborting <br />" . PHP_EOL;
            return $out_html;
        }
    }
    // end foreach($params as $k=>$p)
    //
    return $out_html;
}
Example #6
0
/**
 * Empty all contents of tmp dir /edi/history/tmp
 *
 * @uses csv_edih_tmpdir()
 * @param  none
 * @return bool
 */
function csv_clear_tmpdir()
{
    //
    $tmpdir = csv_edih_tmpdir();
    if (basename($tmpdir) != 'tmp') {
        csv_edihist_log('tmp dir not /edi/history/tmp');
        return false;
    }
    $tmp_files = scandir($tmpdir);
    if (count($tmp_files) > 2) {
        foreach ($tmp_files as $idx => $tmpf) {
            if ($tmpf == "." || $tmpf == "..") {
                // can't delete . and ..
                continue;
            } elseif (is_file($tmpdir . DS . $tmpf)) {
                unlink($tmpdir . DS . $tmpf);
            } elseif (is_dir($tmpdir . DS . $tmpf)) {
                $tdir_ar = scandir($tmpdir . DS . $tmpf);
                foreach ($tdir_ar as $tfn) {
                    if ($tfn == "." || $tfn == "..") {
                        continue;
                    } elseif (is_file($tmpdir . DS . $tmpf . DS . $tfn)) {
                        unlink($tmpdir . DS . $tmpf . DS . $tfn);
                    }
                }
                rmdir($tmpdir . DS . $tmpf);
            }
        }
    }
    $tmp_files = scandir($tmpdir);
    if (count($tmp_files) > 2) {
        csv_edihist_log('tmp dir contents remain in ... /edi/history/tmp');
        return false;
    } else {
        return true;
    }
}
Example #7
0
/**
 * The main function in this edih_archive.php script.  This function gets the parameters array
 * from csv_parameters() and calls the archiving functions on each type of file
 * in the parameters array.
 *
 * @uses edih_archive_date()
 * @uses csv_edih_basedir()
 * @uses csv_parameters()
 * @uses csv_edih_tmpdir()
 * @uses csv_table_header()
 * @uses edih_archive_filenames()
 * @uses edih_archive_rewrite_csv()
 * @uses edih_archive_csv_split()
 * @uses edih_archive_create_zip()
 * 
 * @param string 		from select drop-down 6m, 12m, 18m, etc
 * 
 * @return string       descriptive message in html format
 */
function edih_archive_main($period)
{
    //
    $out_html = '';
    if ($period) {
        $archive_date = edih_archive_date($period);
        if ($archive_date) {
            $archive_dir = csv_edih_basedir() . DS . 'archive';
            $tmp_dir = csv_edih_tmpdir();
            $arch_fn = $archive_date . '_archive.zip';
            $params = csv_parameters();
        } else {
            csv_edihist_log("edih_archive_main: error creating archive date from {$period}");
            $out_html = "Error creating archive date from {$period}<br />" . PHP_EOL;
        }
    } else {
        $out_html = "Archive period invalid.<br />" . PHP_EOL;
        return $out_html;
    }
    //
    if (is_dir($archive_dir)) {
        if (is_file($archive_dir . DS . $arch_fn)) {
            csv_edihist_log("edih_archive_main: archive file {$arch_fn} already exists");
            $out_html = "Archive: archive file {$arch_fn} already exists<br>" . PHP_EOL;
            return $out_html;
        }
    } else {
        // should have been created at setup
        if (!mkdir($archive_dir, 0755)) {
            csv_edihist_log("edih_archive_main: archive directory does not exist");
            $out_html = "Archive: archive directory does not exist<br>" . PHP_EOL;
            return $out_html;
        }
    }
    //
    foreach ($params as $k => $p) {
        //
        $ft = $p['type'];
        // could be $k
        //
        if ($ft == 'f837') {
            csv_edihist_log("edih_archive_main: 837 Claims files are not archived");
            continue;
        }
        $fdir = $p['directory'];
        $scan = scandir($fdir);
        if (!$scan || count($scan) < 3) {
            continue;
        }
        //
        $files_csv = $p['files_csv'];
        $claims_csv = $p['claims_csv'];
        $date_col = $p['filedate'];
        $fncol = 'FileName';
        //
        // create three csv file paths 'old_', 'arch_', and 'keep_'
        // files csv temporary names
        $fn_files_old = $tmp_dir . DS . 'old_' . basename($files_csv);
        $fn_files_arch = $tmp_dir . DS . 'arch_' . basename($files_csv);
        $fn_files_keep = $tmp_dir . DS . 'keep_' . basename($files_csv);
        // claims csv temporary names
        $fn_claims_old = $tmp_dir . DS . 'old_' . basename($claims_csv);
        $fn_claims_arch = $tmp_dir . DS . 'arch_' . basename($claims_csv);
        $fn_claims_keep = $tmp_dir . DS . 'keep_' . basename($claims_csv);
        // table headings
        $fh_ar = csv_table_header($ft, 'file');
        $ch_ar = csv_table_header($ft, 'claim');
        // copy existing csv files -- continue to next type if no files_csv
        $iscpc = $iscpf = false;
        if (is_file($files_csv)) {
            $iscpf = copy($files_csv, $fn_files_old);
            csv_edihist_log("edih_archive_main: copy {$ft} files csv to tmp dir");
        } else {
            csv_edihist_log("edih_archive_main: {$ft} files csv does not exist");
            continue;
        }
        if (is_file($claims_csv)) {
            $iscpc = copy($claims_csv, $fn_claims_old);
            csv_edihist_log("edih_archive_main: copy {$ft} claims csv to tmp dir");
        } else {
            if ($ft == 'f997') {
                // there may be no 997 type claims records, so create a dummy file
                $fh = fopen($fn_claims_old, 'wb');
                if ($fh) {
                    fputcsv($fh, $ch_ar);
                    fclose($fh);
                }
            } else {
                csv_edihist_log("edih_archive_main: {$ft} claims csv does not exist");
                continue;
            }
        }
        //
        if (!$iscpf || !$iscpc) {
            csv_edihist_log("edih_archive_csv_old: copy to tmp dir failed for csv file {$ft}");
            $out_html = "Archive temporary files operation failed ... aborting <br />" . PHP_EOL;
            // need to call archive_undo()
            $out_html .= edih_archive_undo();
            return $out_html;
        }
        // get the csv data
        $csv_files_ar = csv_assoc_array($ft, 'file');
        $csv_claims_ar = csv_assoc_array($ft, 'claim');
        // get filenames to be archived
        $fn_ar = array();
        $tp_ar = array();
        $fn_ar = edih_archive_filenames($csv_files_ar, $archive_date);
        if (count($fn_ar)) {
            // add type to list
            $tp_ar[] = $ft;
            // get the old and new csv row arrays for files_csv
            $arch_new = edih_archive_csv_split($csv_files_ar, $fn_ar);
            if ($arch_new) {
                if (isset($arch_new['keep'])) {
                    // write the new
                    $frws = edih_archive_rewrite_csv($fn_files_keep, $fh_ar, $arch_new['keep']);
                    $out_html .= "type {$ft} keep files_csv file with {$frws} rows<br>";
                }
                if (isset($arch_new['arch'])) {
                    // write the old
                    $frws2 = edih_archive_rewrite_csv($fn_files_arch, $fh_ar, $arch_new['arch']);
                    $out_html .= "type {$ft} archive files_csv file with {$frws2} rows<br>";
                }
            } else {
                $out_html .= "type {$ft} error creating new files_csv tables";
            }
            // repeat for claims_csv
            $arch_new = edih_archive_csv_split($csv_claims_ar, $fn_ar);
            if ($arch_new) {
                if (isset($arch_new['keep'])) {
                    // write the new
                    $crws = edih_archive_rewrite_csv($fn_claims_keep, $ch_ar, $arch_new['keep']);
                    $out_html .= "type {$ft} keep claims_csv file with {$crws} rows<br>";
                }
                if (isset($arch_new['arch'])) {
                    // write the old
                    $crws = edih_archive_rewrite_csv($fn_claims_arch, $ch_ar, $arch_new['arch']);
                    $out_html .= "type {$ft} archive claims_csv file with {$crws} rows<br>";
                }
            } else {
                $out_html .= "type {$ft} error creating new claims csv tables<br>";
            }
            // now the csv_records are in files
            // file records in $fn_files_arch  $fn_files_keep
            // claim records in $fn_claims_arch  $fn_claims_new
            //
            // create a zip archive
            // zf is result of zipArchive functions true or false
            $zf = edih_archive_create_zip($p, $fn_ar, $archive_date, $arch_fn);
            //
            // delete archived files
            if ($zf) {
                // replace the csv files
                $rn = rename($fn_files_keep, $files_csv);
                if ($rn) {
                    csv_edihist_log("edih_archive_main: replaced {$files_csv}");
                } else {
                    csv_edihist_log("edih_archive_main: error trying to replace {$files_csv}");
                }
                $rn = rename($fn_claims_keep, $claims_csv);
                if ($rn) {
                    csv_edihist_log("edih_archive_main: replaced {$claims_csv}");
                } else {
                    csv_edihist_log("edih_archive_main: error trying to replace {$claims_csv}");
                }
                // move archive files to tmpdir/ftype
                // $rndir = mkdir($tmpdir.DS.$fdir);
                csv_edihist_log("edih_archive_main: {$ft} now moving old files ");
                $del = edih_archive_move_old($p, $fn_ar);
                $out_html .= "Archive moved {$del} {$ft} type files<br>" . PHP_EOL;
                //
            } else {
                csv_edihist_log("edih_archive_main: type {$ft} error in creating archive");
                $out_html .= "type {$ft} error in creating archive<br>" . PHP_EOL;
            }
        } else {
            csv_edihist_log("edih_archive_main: search found no type {$ft} files older than {$period}");
            $out_html .= "Archive: type {$ft} archive found no files older than {$period}<br>" . PHP_EOL;
        }
    }
    // end foreach($params as $k=>$p)
    //
    if (is_file($tmp_dir . DS . $arch_fn)) {
        $rn = rename($tmp_dir . DS . $arch_fn, $archive_dir . DS . $arch_fn);
        $cm = chmod($archive_dir . DS . $arch_fn, 0400);
        if ($rn) {
            csv_edihist_log("edih_archive_main: moved {$arch_fn} to archive directory");
        } else {
            csv_edihist_log("edih_archive_main: error moving archive file {$arch_fn}");
            $out_html .= "<p>edih_archive_main: error moving archive file {$arch_fn}</p>";
        }
    } else {
        csv_edihist_log("edih_archive_main: is_file false {$tmp_dir}.DS.{$arch_fn}");
    }
    //edih_archive_cleanup($arch_fn, $tp_ar);
    $is_clear = csv_clear_tmpdir();
    if ($is_clear) {
        $out_html .= "Archive: temporary files removed. Process complete.<br>" . PHP_EOL;
    } else {
        $out_html .= "Archive: still some files in /history/tmp/. Process complete.<br>" . PHP_EOL;
    }
    //
    return $out_html;
}
Example #8
0
/**
 * display fule uploaded from x12 File tab
 * wrap individual transactions in accordian jquery ui widget
 *
 * @uses csv_check_x12_obj()
 * @uses edih_html_heading()
 * @uses edih_271_transaction_html()
 * @uses edih_278_transaction_html()
 * @uses edih_277_transaction_html()
 * @uses edih_835_html_page()
 * @uses edih_display_text()
 *
 * @param string  path to x12 file
 * @return string
 */
function edih_disp_x12file()
{
    //
    $str_htm = '';
    $fn = $ft = $icn = $trace = $rsptype = $format = '';
    //
    if (isset($_POST['x12_html'])) {
        $htmval = filter_input(INPUT_POST, 'x12_html', FILTER_SANITIZE_STRING);
        $format = $htmval == 'html' ? 'htm' : 'seg';
        $upldir = csv_edih_tmpdir();
    } else {
        $format = 'seg';
    }
    // post request from x12 file tab
    if (count($_FILES) && isset($_FILES['fileUplx12'])) {
        $fnupl = htmlentities($_FILES['fileUplx12']['name']);
        // the upload files validator
        $f_array = edih_upload_files();
        //
        if (is_array($f_array) && count($f_array)) {
            // was file rejected?
            if (isset($f_array['reject'])) {
                $fn = count($f_array['reject'][0]) ? $f_array['reject'][0]['name'] : '';
                $cmt = count($f_array['reject'][0]) ? $f_array['reject'][0]['comment'] : '';
                //$str_html = edih_html_heading('error');
                $str_htm .= "<p>Rejected file:</p>" . PHP_EOL;
                $str_htm .= "<p>{$fn}<br>" . PHP_EOL;
                $str_htm .= " -- {$cmt}</p>" . PHP_EOL;
                //
                csv_edihist_log("edih_disp_x12file: rejected file {$fn} comment: {$cmt}");
                //
                return $str_htm;
            } else {
                $fnar = reset($f_array);
                // type filename array
                $ft = key($f_array);
                // type
                $fn1 = $f_array[$ft][0];
                //$upldir.DS.
                $fn = csv_check_filepath($fn1);
                csv_edihist_log("edih_disp_x12file: submitted POST {$format} {$ft} {$fn1} {$fnupl}");
                //
                if (!$fn) {
                    //$str_htm = edih_html_heading('error');
                    $str_htm .= "<p>Path error for {$fn1}</p>" . PHP_EOL;
                    csv_edihist_log("edih_disp_x12file: Path error for {$fn1}");
                    return $str_htm;
                }
            }
        } else {
            //$str_htm = edih_html_heading('error');
            $str_htm .= "<p>File not accepted {$fnupl}</p>" . PHP_EOL;
            csv_edihist_log("edih_disp_x12file: File not accepted {$fnupl}");
            return $str_htm;
        }
    } elseif (isset($_GET['gtbl']) && $_GET['gtbl'] == 'file') {
        // this is a GET request from csv files table
        // assemble variables
        $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
        $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
        $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_SANITIZE_STRING) : '';
        $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
        $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_SANITIZE_STRING) : '';
        $format = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_SANITIZE_STRING) : '';
        //
    } else {
        //$str_htm = edih_html_heading('error');
        $str_htm .= "<p>Error: No request received by server</p>" . PHP_EOL;
        csv_edihist_log("edih_disp_x12file: No request received by server");
        return $str_htm;
    }
    //
    if (!$fn) {
        if ($ft && $icn) {
            $fnr = csv_file_by_controlnum($ft, $icn);
            $fn = csv_check_filepath($fnr);
        } elseif ($ft && $trace && $rsptype) {
            $fnr = csv_file_by_trace($trace, $ft, $rsptype);
            $fn = csv_check_filepath($fnr);
            $ft = $rsptype;
            $trace = '';
        } elseif ($ft == 'f835' && $trace) {
            $fnr = csv_file_by_trace($trace, $ft, $rsptype);
            $fn = csv_check_filepath($fnr);
        } elseif ($ft == 'f997' && $trace && $rsptype) {
            $fnr = csv_file_by_controlnum($rsptype, $trace);
            $fn = csv_check_filepath($fnr);
            $ft = $rsptype;
            $trace = '';
            if (!$fn) {
                $str_htm .= "<p>997/999 Trace value {$trace} not found for type {$rsptype}</p>" . PHP_EOL;
                csv_edihist_log("edih_disp_x12file: 997/999 Trace value {$trace} not found for type {$rsptype}");
                return $str_htm;
            }
        }
    }
    if (!$fn) {
        //$str_htm = edih_html_heading('error');
        $str_htm .= "<p>Name error for file: type {$ft} icn {$icn} trace {$trace} rsp {$rsptype}</p>" . PHP_EOL;
        csv_edihist_log("edih_disp_x12file: Name error for file: type {$ft} icn {$icn} trace {$trace} rsp {$rsptype}");
        return $str_htm;
    }
    //
    if ($format == 'seg') {
        if ($ft == 'f835' && $trace) {
            $str_htm .= edih_display_text($fn, $ft, $trace, true);
        } elseif ($icn) {
            $str_htm .= edih_display_text($fn, $ft, $icn, true);
        } else {
            $str_htm .= edih_display_text($fn, $ft);
        }
        csv_edihist_log("edih_disp_x12file: segments display {$fn}");
        //
        return $str_htm;
    }
    // request is for html display
    // now go through each file type
    // 'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277',
    // 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
    if ($ft == 'f271' || $ft == 'f270') {
        //$str_htm .= edih_html_heading('eligibility', $fn);
        $str_htm .= edih_271_html($fn);
        //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
    } elseif ($ft == 'f276' || $ft == 'f277') {
        //$str_htm .= edih_html_heading('claimstatus', $fn);
        $str_htm .= edih_277_html($fn);
        //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
    } elseif ($ft == 'f278') {
        //$str_htm .= edih_html_heading('claimstatus', $fn);
        $str_htm .= edih_278_html($fn);
        //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
    } elseif ($ft == 'f835') {
        //$str_htm .= edih_html_heading('eradisplay', $fn);
        $str_htm = edih_835_html($fn, $trace);
        //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
    } else {
        // no html format for this type
        // object is created in edih_display_text function
        // edih_display_text($filepath, $filetype='', $claimid='', $trace=false, $err_info='')
        //$str_htm .= edih_html_heading('x12display', $fn);
        $str_htm .= edih_display_text($fn, $ft);
        //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
    }
    //
    return $str_htm;
}
Example #9
0
/**
 * Function to deal with zip archives of uploaded files
 * 
 * This function examines the zip archive, checks for unwanted files, unpacks
 * the files to the IBR_UPLOAD_DIR, and creates an array of file paths by the
 * type of file
 * 
 * @uses ibr_upload_match_file()
 * @param string $zipfilename -- the $_FILES['file']['tmp_name'];
 * @param array $param_ar -- the parameters array, so we don't have to create it here
 * @param string &$html_str -- passed by reference for appending
 * @return array $f_ar -- paths to unpacked files accepted by this function
 */
function ibr_ziptoarray($zipfilename, $param_ar, &$html_str)
{
    //
    // note that this function moves files and set permissions, so platform issues may occur
    //
    // zerr array is probably not needed, use $zip_obj->getStatusString
    $zerr = array('0' => 'ER_OK N No error', '1' => 'ER_MULTIDISK N Multi-disk zip archives not supported', '2' => 'ER_RENAME S Renaming temporary file failed', '3' => 'ER_CLOSE S Closing zip archive failed', '4' => 'ER_SEEK S Seek error', '5' => 'ER_READ S Read error', '6' => 'ER_WRITE S Write error', '7' => 'ER_CRC N CRC error', '8' => 'ER_ZIPCLOSED N Containing zip archive was closed', '9' => 'ER_NOENT N No such file', '10' => 'ER_EXISTS N File already exists', '11' => 'ER_OPEN S Can not open file', '12' => 'ER_TMPOPEN S Failure to create temporary file', '13' => 'ER_ZLIB Z Zlib error', '14' => 'ER_MEMORY N Malloc failure', '15' => 'ER_CHANGED N Entry has been changed', '16' => 'ER_COMPNOTSUPP N Compression method not supported', '17' => 'ER_EOF N Premature EOF', '18' => 'ER_INVAL N Invalid argument', '19' => 'ER_NOZIP N Not a zip archive', '20' => 'ER_INTERNAL N Internal error', '21' => 'ER_INCONS N Zip archive inconsistent', '22' => 'ER_REMOVE S Can not remove file', '23' => 'ER_DELETED N Entry has been deleted');
    //
    $ibr_upldir = csv_edih_tmpdir();
    //
    $zip_obj = new ZipArchive();
    // open archive (the ZIPARCHIVE::CREATE is supposedly necessary for microsoft)
    if ($zip_obj->open($zipfilename, ZIPARCHIVE::CREATE) !== TRUE) {
        //
        $html_str .= "Error: Could not open archive {$zipfilename} <br />" . PHP_EOL;
        return FALSE;
    }
    if ($zip_obj->status != 0) {
        //
        $html_str .= "Error code: " . $zip_obj->status . " " . $zip_obj->getStatusString() . "<br />" . PHP_EOL;
        return FALSE;
    }
    // initialize output array and counter
    $f_zr = array();
    $p_ct = 0;
    // get number of files
    $f_ct = $zip_obj->numFiles;
    // get the file names
    for ($i = 0; $i < $f_ct; $i++) {
        //
        $isOK = TRUE;
        $fstr = "";
        $file = $zip_obj->statIndex($i);
        $name = $file['name'];
        $oldCrc = $file['crc'];
        // get file contents
        $fstr = stream_get_contents($zip_obj->getStream($name));
        // file -bi  277-201203140830-001.277ibr --> 'text/plain'; charset=us-ascii
        // for linux servers: echo system("file -b '<file path>'");
        if ($fstr) {
            // use only the file name
            $bnm = basename($name);
            //
            // $newname = tempnam (IBR_UPLOAD_DIR , "edi"); --won't work since we need the file name to classify
            // the scheme of inserting "ediz" into the name allows us to do the CRC test and then
            // rename the file, all in the temporary directory
            // Note that BCBS files have no extension, just a name scheme (recently changed)
            if (strpos($bnm, "835") === 0) {
                $newname = $ibr_upldir . DIRECTORY_SEPARATOR . $bnm . "ediz";
            } else {
                $newname = $ibr_upldir . DIRECTORY_SEPARATOR . "ediz" . $bnm;
            }
            //
            // extract the file to unzip tmp dir with read/write access
            $chrs = file_put_contents($newname, $fstr);
            // test crc
            $newCrc = hexdec(hash_file("crc32b", $newname));
            // is this the best way to do this test?
            if ($newCrc !== $oldCrc && $oldCrc + 4294967296 !== $newCrc) {
                // failure case, mismatched crc file integrity values
                $html_str .= "CRC error: The files don't match! Removing file {$bnm} <br />" . PHP_EOL;
                $isGone = unlink($newname);
                if ($isGone) {
                    $is_tmpzip = FALSE;
                    $html_str .= "File Removed {$bnm}<br />" . PHP_EOL;
                } else {
                    $html_str .= "Failed to removed file {$bnm}<br />" . PHP_EOL;
                }
            } else {
                // passed the CRC test, now type and verify file
                $fzp['name'] = $bnm;
                $fzp['tmp_name'] = $newname;
                // tmp/edihist/ediz.$bnm or 83511111---ediz
                // verification checks special to our application
                $f_uplz = ibr_upload_match_file($param_ar, $fzp, $html_str);
                //
                if (is_array($f_uplz) && count($f_uplz) > 0) {
                    $t = $f_uplz['type'];
                    $n = $f_uplz['name'];
                    $f_zr[$t][] = $n;
                    $p_ct++;
                } else {
                    // verification failed
                    $f_zr['reject'][] = $fzp['name'];
                }
            }
            //
        } else {
            $html_str .= "Did not get file contents {$name}" . PHP_EOL;
            $isOK = FALSE;
        }
    }
    // end for ($i=0; $i<$numFiles; $i++)
    //
    $html_str .= "Accepted {$p_ct} of {$f_ct} files from {$zipfilename} <br />" . PHP_EOL;
    //
    return $f_zr;
}