/** * Save the uploaded files array to the correct directory * * If a matching filename file is already in the directory it is not overwritten. * The uploaded file will just be discarded. * * @uses csv_parameters() * @see edih_upload_files() * @param array $files_array files array created by edih_upload_files() * @param bool -- whether to return html output * @param bool -- whether to only report errors (ignored) * @return string html formatted messages */ function edih_sort_upload($files_array, $html_out = true, $err_only = true) { // $prc_htm = ''; $rmk_htm = ''; $dirpath = csv_edih_basedir(); // if (is_array($files_array) && count($files_array)) { // we have some files $p_ar = csv_parameters($type = "ALL"); // $prc_htm .= "<p><em>Received Files</em></p>" . PHP_EOL; foreach ($files_array as $key => $val) { // $prc_htm .= "<ul class='fupl'>" . PHP_EOL; if (isset($p_ar[$key])) { $tp_dir = $p_ar[$key]['directory']; $tp_base = basename($tp_dir); $idx = 0; $prc_htm .= "<li>type {$key}</li>" . PHP_EOL; if (!is_array($val) || !count($val)) { $prc_htm .= "<li>no new files</li>" . PHP_EOL; continue; } foreach ($val as $idx => $nf) { // check if the file has already been stored // a matching file name will not be replaced $nfb = basename($nf); $testname = $tp_dir . DS . $nfb; $prc_htm .= "<li>{$nfb}</li>" . PHP_EOL; if (is_file($testname)) { $prc_htm .= "<li> -- file exists</li>" . PHP_EOL; } elseif (rename($nf, $testname)) { $iscm = chmod($testname, 0400); if (!$iscm) { // if we could write, we should be able to set permissions $prc_htm .= "<li> -- file save error</li>" . PHP_EOL; unlink($testname); } } else { $prc_htm .= "<li> -- file save error</li>" . PHP_EOL; } } } elseif ($key == 'reject') { $prc_htm .= "<li><bd>Reject:</bd></li>" . PHP_EOL; foreach ($val as $idx => $nf) { $prc_htm .= "<li>" . $nf['name'] . "</li>" . PHP_EOL; $prc_htm .= "<li> --" . $nf['comment'] . "</li>" . PHP_EOL; } } elseif ($key == 'remark') { $rmk_htm .= "<p><bd>Remarks:</bd><br>" . PHP_EOL; foreach ($val as $idx => $r) { $rmk_htm .= $r . "<br>" . PHP_EOL; } $rmk_htm .= "</p>" . PHP_EOL; } else { $prc_htm .= "<li>{$key} type not stored</li>" . PHP_EOL; foreach ($val as $idx => $nf) { $prc_htm .= "<li>" . basename($nf) . "</li>" . PHP_EOL; } } $prc_htm .= "</ul>" . PHP_EOL; $prc_htm .= $rmk_htm . PHP_EOL; } } else { // should not happen since this function should not be called unless there are new files $prc_htm .= "<ul><li>No files submitted</li></ul>" . PHP_EOL; } // return $prc_htm; }
//dirname(__FILE__) . "/edihist/ibr_277_read.php"); 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; }
require_once "{$srcdir}/edihistory/edih_271_html.php"; //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 {
/** * determine if a csv table has data for select dropdown * * @param string default 'json' * @return array json if argument is 'json' */ function csv_table_select_list($outtp = 'json') { $optlist = array(); // $edihist_dir = csv_edih_basedir(); // $GLOBALS['OE_SITE_DIR'].'/edi/history' $thisdir = $edihist_dir . '/csv'; $tbllist = scandir($thisdir); $idx = 0; foreach ($tbllist as $csvf) { if ($csvf == "." || $csvf == "..") { continue; } if (filesize($thisdir . DIRECTORY_SEPARATOR . $csvf) < 90) { continue; } if (substr($csvf, -1) == '~') { continue; } $finfo = pathinfo($thisdir . DIRECTORY_SEPARATOR . $csvf); $fn = $finfo['filename']; $tp = explode('_', $fn); $optlist[$idx]['fname'] = $fn; $optlist[$idx]['desc'] = $tp[1] . ' ' . $tp[0]; $idx++; } if ($outtp == 'json') { return json_encode($optlist); } else { return $optlist; } }
/** * 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 .= " archived " . count($archive_ar['files']) . " files\t <br />" . PHP_EOL; $out_html .= " archived " . count($archive_ar['arch_csv']) . " rows from " . basename($file_csv) . " <br />" . PHP_EOL; $out_html .= " 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 .= " archived " . count($archive_ar['arch_csv']) . " rows from " . basename($claim_csv) . " <br />" . PHP_EOL; $out_html .= " 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; }
/** * list existing archive files * * @param string default 'json' * @return array json if argument is 'json' */ function csv_archive_select_list($outtp = 'json') { // $flist = array(); $archdir = csv_edih_basedir() . DS . 'archive'; // // debug csv_edihist_log("csv_archive_select_list: using {$archdir}"); // $scan = scandir($archdir); if (is_array($scan) && count($scan)) { foreach ($scan as $s) { if ($s == '.' || $s == '..') { continue; } elseif (strpos($s, 'note')) { continue; } else { $flist[] = $s; } } } if ($outtp == 'json') { return json_encode($flist); } else { return $flist; } }
/** * 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; }