11 function GetDocType() {
28 function GetFilePermissions() {
38 function GetExecPermission() {
46 function GetGlobalDirectory() {
47 return "/home/homecu/skeleton/en_US";
56 function GetCuDirectory($cu) {
57 return "/home/" . strtolower($cu) .
"/bin";
64 function GetEstatementText() {
73 function GetBackupSuffix() {
82 function GetExecutableFileExtensions() {
91 function GetPdfDefsFile() {
101 function GetTmpDir($cu) {
102 return "/home/" . strtolower($cu) .
"/tmp";
111 function GetDefaultTmpInput($cu) {
112 return GetTmpDir($cu) .
"/testpdfinput.txt";
121 function GetDefaultTmpOutput($cu) {
122 return GetTmpDir($cu) .
"/testpdfoutput.pdf";
132 function GetCheckMORunningSecs() {
142 function GetMOLastXPeriods() {
158 function ExtractPdfDefsForms($cu, $giveContents =
false) {
160 $file = GetCuDirectory($cu) .
"/" . GetPdfDefsFile();
162 if (!file_exists($file)) {
163 throw new exception (
"File does not exist.", 3);
166 $contents = file_get_contents($file);
167 if ($contents ===
false) {
168 throw new exception (
"File contents could not be found.", 1);
171 $results = preg_match_all (
'/^\s*(.*\s*NEWFORM=([a-z0-9_]+).*>>)$/im', $contents, $matches, PREG_SET_ORDER);
173 if ($results ===
false) {
174 throw new exception (
"Regex failed.", 2);
177 $returnResults = array();
180 foreach($matches as $match) {
181 $returnResults[$match[2]] = str_ireplace(
"NEWFORM",
"USEFORM", $match[1]);
184 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"formArray" => $returnResults));
187 $returnArray[
"data"][
"contents"] = $contents;
190 }
catch (exception $e) {
191 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
210 function SubstituteKeywords($textIn, $cu) {
212 if (!isset($textIn)) {
213 throw new exception (
"Text in is required.", 1);
215 if (!isset($cu) || trim($cu) ==
"") {
216 throw new exception (
"Cu is required.", 2);
218 if (trim($textIn) !=
"") {
220 $cuUpper = strtoupper($cu);
221 $cuLower = strtolower($cu);
222 $cuHome =
"/home/$cuLower";
225 $textIn = str_replace(
"#~CUUPPER~#", $cuUpper, $textIn);
226 $textIn = str_replace(
"#~CULOWER~#", $cuLower, $textIn);
227 $textIn = str_replace(
"#~CUHOME~#", $cuHome, $textIn);
230 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $textIn);
231 }
catch (exception $e) {
232 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
256 function ReadEstmntDefinitions($dbh, $cu, $section) {
259 $isGlobal = !isset($cu) || trim($cu) ==
"";
262 $sql =
"select d.docsid as docid, d.docsname as docname, d.docsdesc as descr, d.docsmaintsort as sort, d.docsmaintsection as section, 263 d.docstitle as vendor from cucmsdocs d 264 where d.docstype = " . GetDocType();
266 $upperCu = strtoupper(trim($cu));
267 if (!isset($section) || trim($section) ==
"") {
268 throw new exception (
"Section is required.", 3);
271 if ($section ==
"Utility") {
272 $sql =
"select d.docsid as docid, d.docsname as docname, d.docsdesc as descr, d.docsmaintsort as sort, trim(d.docstitle) as vendor 274 where d.docstype = " . GetDocType() .
" and d.docsmaintsection = '" . prep_save($section) .
"' 275 order by d.docsmaintsort";
277 $sql =
"select d.docsid as docid, d.docsname as docname, d.docsdesc as descr, d.docsmaintsort as sort, trim(d.docstitle) as vendor, 278 trim(e.docstitle) as vendorl from cucmsdocs d 279 inner join cucmsdocs e on d.docsname = e.docsname and d.docstype = e.docstype and d.docsmaintsection = e.docsmaintsection 280 and d.docstype = " . GetDocType() .
" and d.docsmaintsection = '" . prep_save($section) .
"' 281 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10) .
"' 282 order by d.docsmaintsort, e.docstitle";
286 $sth = db_query($sql, $dbh);
288 throw new exception (
"Select query failed.", 1);
291 $results = db_fetch_all($sth);
292 $gridData = $results ===
false ? array() : $results;
294 $currentVendor =
null;
297 if (!$isGlobal && $section !==
"Utility") {
299 foreach($gridData as $row) {
302 $currentVendor = $row[
"vendor"];
304 if (!HCU_array_key_exists($row[
"docid"], $gridMap)) {
305 $row[
"allvendors"] = array($row[
"vendorl"]);
306 unset($row[
"vendorl"]);
307 $gridMap[$row[
"docid"]] = $row;
309 $gridMap[$row[
"docid"]][
"allvendors"][] = $row[
"vendorl"];
312 $gridData = array_values($gridMap);
315 $estatementTextPlural = GetEstatementText() .
"s";
316 $data = array(
"gridData" => $gridData);
318 $sql =
"select distinct trim(docstitle) as vendor from cucmsdocs where docstype = " . GetDocType() .
" order by 1";
319 $sth = db_query($sql, $dbh);
321 throw new exception (
"Select query failed.", 2);
323 $results = db_fetch_all($sth);
324 $results = $results ===
false ? array() : $results;
325 $vendorData = array();
326 foreach($results as $row) {
327 $vendorData[] = $row[
"vendor"];
329 $data[
"vendorData"] = $vendorData;
331 $sql =
"select distinct trim(docsname) as name from cucmsdocs where docstype = " . GetDocType() .
" order by 1";
332 $sth = db_query($sql, $dbh);
334 throw new exception (
"Select query failed.", 8);
336 $results = db_fetch_all($sth);
337 $results = $results ===
false ? array() : $results;
339 foreach($results as $row) {
340 $docData[] = $row[
"name"];
342 $data[
"docnameData"] = $docData;
343 }
else if ($section ==
"Credit Card") {
344 $data[
"currentVendor"] = $currentVendor;
346 $filesAffected = array();
347 $sql =
"select d.docsid as docid, d.docsname as docname from cucmsdocs d 348 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10) .
"' 349 where d.docsmaintsection = '" . prep_save($section) .
"' and d.docstype = " . GetDocType() .
" 350 order by d.docsmaintsort";
351 $sth = db_query($sql, $dbh);
353 throw new exception (
"Select query failed.", 3);
355 $results = db_fetch_all($sth);
356 $toBeDeleted = $results ===
false ? array() : $results;
359 foreach ($toBeDeleted as $row) {
360 $file =
"$dir/" . $row[
"docname"];
361 if (file_exists($file)) {
362 $filesAffected[] = trim($row[
"docname"]);
365 $data[
"filesAffected"] = $filesAffected;
366 }
else if ($section == $estatementTextPlural) {
369 $results = ExtractPdfDefsForms($cu);
370 $data[
"formList"] = $results[
"status"] ===
"000" ? array_keys($results[
"data"][
"formArray"]) : array();
372 $data[
"currentVendor"] = $currentVendor;
373 $sql =
"select distinct trim(d.docstitle) as value, trim(d.docsmaintsection) as section, a.cu is not null as default 375 left join cuadmin a on trim(docstitle) = a.vendor and a.cu = '" . prep_save($upperCu, 10) .
"' 376 where d.docstype = " . GetDocType() .
" 377 order by 2, 3 desc, 1";
378 $sth = db_query($sql, $dbh);
380 throw new exception (
"Select query failed.", 7);
382 $results = db_fetch_all($sth);
383 $configList = $results ===
false ? array() : $results;
385 foreach($configList as $i => $row) {
386 if ($row[
"default"] ==
"t") {
387 $row[
"default"] =
true;
388 $row[
"text"] = $row[
"value"] .
" (Default)";
390 $row[
"default"] =
false;
391 $row[
"text"] = $row[
"value"];
393 $configList[$i] = $row;
395 $data[
"configList"] = $configList;
398 $dir = GetCuDirectory($cu);
399 if (!is_readable($dir)) {
400 throw new exception (
"CU directory is not readable.", 9);
403 $filesAffected = array();
404 $sql =
"select d.docsid as docid, d.docsname as docname from cucmsdocs d 405 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10) .
"' 406 where d.docsmaintsection = '" . prep_save($section) .
"' and d.docstype = " . GetDocType() .
" 407 order by d.docsmaintsort";
408 $sth = db_query($sql, $dbh);
410 throw new exception (
"Select query failed.", 3);
412 $results = db_fetch_all($sth);
413 $toBeDeleted = $results ===
false ? array() : $results;
416 foreach ($toBeDeleted as $row) {
417 $file =
"$dir/" . $row[
"docname"];
418 if (file_exists($file)) {
419 $filesAffected[] = trim($row[
"docname"]);
422 $data[
"filesAffected"] = $filesAffected;
424 $sql =
"select trim(d.docsname) as docname from cucmsdocs d where d.docsmaintsection = 'Disclosure' and d.docstype = " . GetDocType() .
" 426 select trim(d.docsname) as docname from cucmsdocs d 427 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10)
428 .
"' and d.docsmaintsection = 'Disclosure' and d.docstype = " . GetDocType() .
" 431 $sth = db_query($sql, $dbh);
433 throw new exception (
"Select query failed.", 4);
435 $extraDocumentData = db_fetch_all($sth);
436 if ($extraDocumentData ===
false) {
437 $extraDocumentData = array();
439 $data[
"extraDocumentData"] = $extraDocumentData;
441 $sql =
"select d.docsid as docid, trim(d.docsname) as docname, trim(d.docstitle) as vendor from cucmsdocs d 442 where d.docstype = " . GetDocType() .
" and d.docsmaintsection = 'Disclosure' 443 order by docsname, docstitle";
444 $sth = db_query($sql, $dbh);
446 throw new exception (
"Select query failed.", 5);
448 $extraVendorData = db_fetch_all($sth);
449 if ($extraVendorData ===
false) {
450 $extraVendorData = array();
452 $data[
"extraVendorData"] = $extraVendorData;
454 $results = ReadManualOperations($dbh, $cu);
455 if ($results[
"status"] !==
"000") {
456 throw new exception ($results[
"error"], 10);
459 $data[
"moData"] = $results[
"data"];
462 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $data,
"operation" =>
"readInit",
"section" => $section);
463 }
catch (exception $e) {
464 $data = array(
"gridData" => array());
465 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => $data,
"operation" =>
"readInit");
483 function ReadManualOperations($dbh, $cu) {
486 $cuLower = strtolower($cu);
487 $cuDir =
"/home/$cuLower";
488 $tmpDir = $cuDir .
"/tmp";
490 $returnData = array();
491 $returnData[
"moActions"] = array(
492 array(
"text" =>
"Reload",
"value" =>
"reload"),
493 array(
"text" =>
"Undo",
"value" =>
"undo")
496 $lastPeriods = array();
497 $summaryFile =
"$cuDir/stmnt/summary.log";
498 $numPeriods = GetMOLastXPeriods();
499 if (file_exists($summaryFile)) {
500 $lines = file($summaryFile);
501 $lineCount = count($lines);
502 for($i = 0; $i != $numPeriods; $i++) {
503 $lineNum = $lineCount - 1 - $i;
507 $line = $lines[$lineNum];
508 $f = explode(
"\t", $line);
509 $lastPeriods[] = array(
510 "date" => trim($f[0]),
511 "period" => trim($f[8]),
512 "action" => trim($f[4])
517 $returnData[
"moPeriods"] = $lastPeriods;
521 foreach(glob(
"$tmpDir/${cuLower}stmnt.daz*") as $filename) {
522 $fileRec = array(
"type" =>
"statement",
"filename" => basename($filename));
523 $fileRec[
"date"] = date(
"F d Y H:i:s", filemtime($filename));
524 $allFiles[] = $fileRec;
526 foreach(glob(
"$tmpDir/${cuLower}ccard.daz*") as $filename) {
527 $fileRec = array(
"type" =>
"credit card",
"filename" => basename($filename));
528 $fileRec[
"date"] = date(
"F d Y H:i:s", filemtime($filename));
529 $allFiles[] = $fileRec;
532 $returnData[
"moAllFiles"] = $allFiles;
534 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $returnData);
535 }
catch (exception $e) {
536 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
555 function ExecuteManualOperations($dbh, $cu, $file, $period, $suboperation) {
557 if (!in_array($suboperation, array(
"reload",
"undo"))) {
558 throw new exception (
"Operation is not valid.", 2);
561 if ($suboperation ==
"reload") {
563 throw new exception (
"File is required.", 3);
568 throw new exception (
"Period is required.", 4);
571 $ccname = GetCCName();
572 $scriptDir = GetScriptDir();
573 $cuLower = strtolower($cu);
574 $cuDir =
"/home/$cuLower";
576 if (!IsPeriodValid($period, is_readable(GetCCConfig($cuDir)))) {
577 throw new exception (
"Period is invalid.", 5);
580 $tmpDir =
"$cuDir/tmp";
581 $hasCC = strpos($period, $ccname) !==
false;
582 $savedFile = $hasCC ?
"$tmpDir/${cuLower}ccard.daz" :
"$tmpDir/${cuLower}stmnt.daz";
585 if ($suboperation ==
"reload") {
586 $reloadFile =
"$tmpDir/" . basename($file);
587 if (!file_exists($reloadFile)) {
588 throw new exception (
"File is invalid.", 8);
590 if ($reloadFile !== $savedFile && !copy($reloadFile, $savedFile)) {
591 throw new exception (
"File couldn't be copied.", 9);
595 putenv(
"ADMUSER=hcuSupport");
597 $scriptName = $hasCC ?
"ccardload.pl" :
"stmntload.pl";
598 $undoParam = $suboperation ===
"undo" ?
"--undo" :
"";
599 $command =
"/usr/bin/nice -19 /usr/bin/env perl $scriptDir/$scriptName $undoParam --noemail " . escapeshellcmd($cuDir) .
" \"{$period}\" >/dev/null &";
601 $rc = exec ($command);
603 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"operation" =>
"exec");
604 }
catch (exception $e) {
605 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" =>
"exec");
622 function RetrieveStatusManualOperations($dbh, $cu) {
624 $scriptDir = GetScriptDir();
625 $cuLower = strtolower($cu);
626 $command =
"/usr/bin/nice -19 /usr/bin/env perl $scriptDir/stmntload_running_status.pl $cuLower";
627 $commandRs = exec($command);
628 $commandStatusAry = explode(
"\t", $commandRs);
629 $commandStatus = trim($commandStatusAry[0]);
631 $lastUploadedStatus = 0;
632 switch ($commandStatus) {
635 $lastUploadedStatus = 1;
638 $lastUploadedStatus = 2;
643 $lastUploadedStatus = 0;
651 $tmpDir =
"/home/$cuLower/tmp";
652 if (is_dir($tmpDir) && file_exists(
"$tmpDir/{$cuLower}stmnt.daz")) {
653 $lastUploadedStatus = 2;
656 switch($lastUploadedStatus) {
658 $infoMessage =
"eStatement process failed. Please try again.";
661 $infoMessage = GetRunningMOInfo();
669 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"infoCode" => $lastUploadedStatus,
"infoMessage" => $infoMessage);
672 }
catch (exception $e) {
673 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
682 function GetRunningMOInfo() {
683 return "There is a running eStatement process. This will check every " . GetCheckMORunningSecs() .
" seconds or you can close window.";
696 function IsPeriodValid($periodText, $isCcConfigReadable) {
699 $ccname = GetCCName();
700 $hasCC = strpos($periodText, $ccname) !==
false;
701 if ($hasCC && !$isCcConfigReadable) {
702 throw new exception (
"CC Config is not readable", 1);
705 $pattern = GetPeriodPattern();
706 $doesMatch = preg_match($pattern, $periodText) === 1;
709 throw new exception (
"Period does not match", 2);
713 }
catch (exception $e) {
725 function GetMonthAbbrevs() {
726 $date = DateTime::createFromFormat(
'd-m-Y|',
'01-01-2019');
727 $monthAbbrevs = array();
728 for($i = 0; $i != 12; $i++) {
729 $monthAbbrevs[] = $date->format(
"M");
730 $date->setDate(2019, $date->format(
"m") + 1, 1);
733 return $monthAbbrevs;
742 function GetPeriodPattern() {
743 $ccname = GetCCName();
744 $monthAbbrevs = GetMonthAbbrevs();
746 $quarterPattern =
'Quarter [1-4]';
747 $halfPattern =
'(?:1st Half)|(?:2nd Half)';
748 $yearPattern =
'[1-9]\d{3}';
749 $monthPattern =
'(?:' . implode(
')|(?:', $monthAbbrevs) .
')';
751 $pattern =
'/(?:' . $ccname .
' )?(?:' . $quarterPattern
752 .
')|(?:' . $monthPattern .
')|(?:' . $halfPattern .
') ' . $yearPattern .
'/';
768 function UpdateEstmntDefinitionsMntc($dbh, $dataItem) {
770 $operation =
"update";
771 $isGlobal = !isset($cu) || trim($cu) ==
"";
773 $results = _ValidateDataItemUpdateGlobal($dataItem);
775 if ($results [
"status"] !==
"000") {
776 throw new exception ($results[
"error"], 1);
778 extract($results[
"data"]);
781 $sql =
"select 'FOUND' from cucmsdocs where docstitle = '" . prep_save($vendor, 100) .
"' and docsname = '" . prep_save($docname, 50) .
"' and docstype = " 782 . GetDocType() .
" and docsid <> $docid";
783 $sth = db_query($sql, $dbh);
785 throw new exception (
"Duplicate check failed.", 4);
787 if (db_num_rows($sth) > 0) {
788 throw new exception (
"Cannot save duplicate record.", 5);
791 $sql =
"insert into cucmsdocs (docsname, docsdesc, docstype, docstitle, docsmaintsection, docsmaintsort) 792 values ('" . prep_save($docname, 50) .
"', '" . prep_save($descr, 250) .
"', " . GetDocType() .
", '" . prep_save($vendor, 100)
793 .
"', '" . prep_save($section) .
"', $sort) 794 returning docsid as docid, docsname as docname, docstitle as vendor, docsmaintsection as section, docsmaintsort as sort, docsdesc as descr";
796 $sql =
"update cucmsdocs set docsname = '" . prep_save($docname, 50) .
"', docsdesc = '" . prep_save($descr, 250) .
"', docstitle = '" 797 . prep_save($vendor, 100) .
"', 798 docsmaintsection = '" . prep_save($section) .
"', docsmaintsort = $sort 799 where docsid = $docid and docstype = " . GetDocType() .
" 800 returning docsid as docid, docsname as docname, docstitle as vendor, docsmaintsection as section, docsmaintsort as sort, docsdesc as descr";
803 $sth = db_query($sql, $dbh);
805 throw new exception (
"Update query failed.", 2);
807 if (db_affected_rows($sth) == 0) {
808 throw new exception (
"Update query failed.", 3);
810 $updatedRecord = db_fetch_assoc($sth, 0);
812 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $updatedRecord,
"operation" => $operation,
"info" => GetEstatementText()
813 .
" was saved successfully.");
814 }
catch (exception $e) {
816 $updatedRecord =
false;
818 $sql =
"select docsid as docid, docsname as docname, docstitle as vendor, docsmaintsection as section, docsmaintsort as sort, docsdesc as descr 819 from cucmsdocs where docsid = $docid";
820 $sth = db_query ($sql, $dbh);
822 $updatedRecord = db_fetch_all($sth);
823 $updatedRecord = $updatedRecord ===
false ? false : $updatedRecord[0];
827 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" => $operation,
"data" => $updatedRecord);
848 function UpdateEstmntCu ($dbh, $dataItem, $cu, $section) {
850 $operation =
"update";
852 if (!isset($cu) || trim($cu) ==
"") {
853 throw new exception (
"Cu is required.", 8);
856 $results = _ValidateDataItemUpdateCu($dataItem);
858 if ($results [
"status"] !==
"000") {
859 throw new exception ($results[
"error"], 1);
861 extract($results[
"data"]);
862 $upperCu = strtoupper(trim($cu));
864 $sql =
"select d.docsid as docid, d.docsname as docname, d.docsdesc as descr, d.docsmaintsort as sort, trim(d.docstitle) as vendor, 865 trim(e.docstitle) as vendorl from cucmsdocs d 866 inner join cucmsdocs e on d.docsname = e.docsname and d.docstype = e.docstype and d.docsmaintsection = e.docsmaintsection 867 and d.docstype = " . GetDocType() .
" and d.docsmaintsection = '" . prep_save($section) .
"' and d.docsid = $docid 868 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10) .
"' 869 order by d.docsmaintsort, e.docstitle";
870 $sth = db_query($sql, $dbh);
872 throw new exception (
"Select query failed.", 9);
874 if (db_num_rows($sth) == 0) {
875 $upperCu = strtoupper(trim($cu));
876 $sql =
"insert into cucmsfrags (docsid, cu) values ($docid, '" . prep_save($upperCu, 10) .
"')";
878 $sth = db_query($sql, $dbh);
880 throw new exception (
"Update query failed.", 4);
882 if (db_affected_rows($sth) == 0) {
883 throw new exception (
"Update query failed.", 5);
886 $sql =
"select d.docsid as docid, d.docsname as docname, d.docsdesc as descr, d.docsmaintsort as sort, trim(d.docstitle) as vendor, 887 trim(e.docstitle) as vendorl from cucmsdocs d 888 inner join cucmsdocs e on d.docsname = e.docsname and d.docstype = e.docstype and d.docsmaintsection = e.docsmaintsection 889 and d.docstype = " . GetDocType() .
" and d.docsmaintsection = '" . prep_save($section) .
"' and d.docsid = $docid 890 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10) .
"' 891 order by d.docsmaintsort, e.docstitle";
892 $sth = db_query($sql, $dbh);
894 throw new exception (
"Select query failed.", 6);
896 if (db_num_rows($sth) == 0) {
897 throw new exception (
"Select query failed.", 7);
899 $gridData = db_fetch_all($sth);
900 $gridData = $gridData ===
false ? array() : $gridData;
903 $gridData = db_fetch_all($sth);
904 $gridData = $gridData ===
false ? array() : $gridData;
910 foreach($gridData as $row) {
911 if (!HCU_array_key_exists($row[
"docid"], $gridMap)) {
912 $row[
"allvendors"] = array($row[
"vendorl"]);
913 unset($row[
"vendorl"]);
914 $gridMap[$row[
"docid"]] = $row;
916 $gridMap[$row[
"docid"]][
"allvendors"][] = $row[
"vendorl"];
919 $data[
"record"] = $gridMap[$docid];
922 $results = UpdateEstmntFile($dbh, $docid, $contents, $cu, $section);
923 if ($results [
"status"] !==
"000") {
924 throw new exception (
"File wasn't saved.", 10);
927 switch(trim($data[
"record"][
"docname"])) {
928 case GetPdfDefsFile():
930 $results = ExtractPdfDefsForms($cu);
931 $data[
"formList"] = $results[
"status"] ===
"000" ? array_keys($results[
"data"][
"formArray"]) : array();
935 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $data,
"operation" => $operation,
"info" => GetEstatementText() .
" file saved successfully.",
936 "section" => $section);
938 }
catch (exception $e) {
939 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" => $operation,
"data" => array(
"record" => array()),
940 "section" => $section);
955 function _ValidateDataItemUpdateGlobal($dataItem) {
957 if (!is_string($dataItem) || $dataItem ==
"") {
958 throw new exception (
"DataItem is not valid.", 1);
960 $dataItem = HCU_JsonDecode($dataItem);
961 if (!is_array($dataItem) || count($dataItem) == 0) {
962 throw new exception (
"DataItem is not valid.", 2);
965 if (!HCU_array_key_exists(
"docid", $dataItem) || intval($dataItem[
"docid"]) == 0) {
966 $dataItem [
"isAdd"] =
true;
967 $dataItem[
"docid"] = 0;
968 $dataItem [
"operation"] =
"create";
970 $dataItem [
"isAdd"] =
false;
971 $dataItem [
"operation"] =
"update";
974 if (!HCU_array_key_exists(
"docname", $dataItem) || trim($dataItem[
"docname"]) ==
"") {
975 throw new exception (
"Doc name is required.", 4);
978 if (!HCU_array_key_exists(
"sort", $dataItem)) {
979 $dataItem [
"sort"] = 0;
981 $dataItem [
"sort"] = intval($dataItem [
"sort"]);
984 if (!HCU_array_key_exists(
"vendor", $dataItem) || trim($dataItem[
"vendor"]) ==
"") {
985 throw new exception (
"Vendor is required.", 7);
988 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $dataItem);
989 }
catch (exception $e) {
990 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1005 function _ValidateDataItemUpdateCu($dataItem) {
1007 if (!is_string($dataItem) || $dataItem ==
"") {
1008 throw new exception (
"DataItem is not valid.", 1);
1010 $dataItem = HCU_JsonDecode($dataItem);
1011 if (!is_array($dataItem) || count($dataItem) == 0) {
1012 throw new exception (
"DataItem is not valid.", 2);
1015 if (!HCU_array_key_exists(
"docid", $dataItem) || intval($dataItem[
"docid"]) == 0) {
1016 throw new exception (
"DataItem is not valid.", 3);
1019 if (!HCU_array_key_exists(
"contents", $dataItem) || trim($dataItem[
"contents"]) ==
"") {
1020 throw new exception (
"Contents are not valid.", 4);
1023 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $dataItem);
1024 }
catch (exception $e) {
1025 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1027 return $returnArray;
1046 function RemoveEstmntDefinitionsMntc($dbh, $docid, $cu, $section) {
1048 $upperCu = isset($cu) ? trim(strtoupper($cu)) :
"";
1049 $lowerCu = strtolower($upperCu);
1050 if ($upperCu !=
"") {
1051 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor, docsdesc as descr from cucmsdocs d 1052 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCu, 10) .
"' 1053 where d.docstype = " . GetDocType() .
" and d.docsid = " . intval($docid);
1055 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor, d.docsdesc as descr from cucmsdocs d 1056 where d.docstype = " . GetDocType() .
" and d.docsid = " . intval($docid);
1059 $sth = db_query($sql, $dbh);
1061 throw new exception (
"SQL is not valid.", 1);
1063 if (db_num_rows($sth) == 0) {
1064 throw new exception (
"Document record is not found.", 2);
1067 list($docid, $docname, $vendor) = db_fetch_array($sth, 0);
1070 $dir = GetGlobalDirectory();
1071 $filename =
"$docname.$vendor";
1073 $dir = GetCuDirectory($lowerCu);
1074 $filename =
"$docname";
1077 if (!is_writable($dir)) {
1078 throw new exception (
"Directory is not writable.", 4);
1080 $fullname =
"$dir/$filename";
1081 if (file_exists($fullname)) {
1082 $results = unlink($fullname);
1083 if ($results ===
false) {
1084 throw new exception (
"File could not be removed.", 5);
1088 if (isset($cu) && trim($cu) !=
"") {
1089 $sql =
"delete from cucmsfrags where docsid = $docid and cu = '" . prep_save($upperCu, 10) .
"'";
1091 $sql =
"delete from cucmsdocs where docsid = $docid and docstype = " . GetDocType() .
" and docsresponsetype is null";
1095 $sth = db_query($sql, $dbh);
1098 throw new exception (
"Delete failed.", 1);
1100 if (db_affected_rows($sth) == 0) {
1101 throw new exception (
"Delete failed.", 2);
1104 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"info" => GetEstatementText() .
" was deleted successfully.",
"section" => $section,
1105 "operation" =>
"delete",
"data" => array(
"record" => array(
"docid" => $docid,
"docname" => $docname,
"vendor" => $vendor)));
1108 switch(trim($docname)) {
1109 case GetPdfDefsFile():
1110 $returnArray[
"data"][
"formList"] = array();
1113 }
catch (exception $e) {
1114 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"section" => $section,
"operation" =>
"delete");
1116 return $returnArray;
1132 function ReadEstmntFileGlobal($dbh, $docid, $fromCuScript, $cu, $vendor) {
1136 if (isset($vendor) && trim($vendor) !=
"") {
1137 $sql =
"select e.docsid as docid, e.docsname as docname, e.docstitle as vendor, e.docsdesc as descr from cucmsdocs d 1138 inner join cucmsdocs e on d.docstype = e.docstype and d.docsname = e.docsname and e.docstitle = '" . prep_save($vendor, 100) .
"' 1139 where d.docstype = " . GetDocType() .
" and d.docsid = " . intval($docid);
1141 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor, d.docsdesc as descr from cucmsdocs d 1142 where d.docstype = " . GetDocType() .
" and d.docsid = " . intval($docid);
1145 $sth = db_query($sql, $dbh);
1147 throw new exception (
"SQL is not valid.", 1);
1149 if (db_num_rows($sth) == 0) {
1150 throw new exception (
"Document record is not found.", 2);
1153 list($docid, $docname, $vendor) = db_fetch_array($sth, 0);
1155 $dirList = array(
"Directory" => GetGlobalDirectory());
1156 $filename =
"$docname.$vendor";
1158 foreach ($dirList as $title => $dir) {
1159 if (!is_readable($dir)) {
1160 throw new exception (
"$title is not readable.", 3);
1162 $fullname =
"$dir/$filename";
1163 if (file_exists($fullname)) {
1164 $contents = file_get_contents($fullname);
1165 if ($contents ===
false) {
1166 throw new exception (
"File is not readable.", 6);
1172 if ($fromCuScript) {
1173 $results = SubstituteKeywords($contents, $cu);
1174 if ($results [
"status"] !==
"000") {
1175 throw new exception (
"$retrieveFile could not translate correctly.", 7);
1177 $contents = $results[
"data"];
1180 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(array(
"docid" => $docid,
"contents" => $contents,
"filename" => $filename)),
1181 "operation" =>
"read");
1182 }
catch (exception $e) {
1183 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" =>
"read",
"data" => array());
1185 return $returnArray;
1201 function ReadEstmntFileCu($dbh, $docid, $cu, $section) {
1205 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor, d.docsdesc as descr from cucmsdocs d 1206 where d.docstype = " . GetDocType() .
" and d.docsid = " . intval($docid);
1208 $sth = db_query($sql, $dbh);
1210 throw new exception (
"SQL is not valid.", 1);
1212 if (db_num_rows($sth) == 0) {
1213 throw new exception (
"Document record is not found.", 2);
1216 list($docid, $docname, $vendor) = db_fetch_array($sth, 0);
1218 $dirList = array(
"Directory" => GetCuDirectory($cu));
1219 $filename =
"$docname";
1221 foreach ($dirList as $title => $dir) {
1222 if (!is_readable($dir)) {
1223 throw new exception (
"$title is not readable.", 3);
1225 $fullname =
"$dir/$filename";
1226 if (file_exists($fullname)) {
1227 $contents = file_get_contents($fullname);
1228 if ($contents ===
false) {
1229 throw new exception (
"File is not readable.", 6);
1235 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(array(
"docid" => $docid,
"contents" => $contents,
"filename" => $filename)),
1236 "operation" =>
"read");
1237 }
catch (exception $e) {
1238 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" =>
"read",
"data" => array());
1240 return $returnArray;
1258 function UpdateEstmntFile($dbh, $docid, $contents, $cu, $section) {
1260 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor from cucmsdocs d 1261 where docstype = " . GetDocType() .
" and docsid = " . intval($docid);
1262 $sth = db_query($sql, $dbh);
1264 throw new exception (
"SQL is not valid.", 1);
1266 if (db_num_rows($sth) == 0) {
1267 throw new exception (
"Document record is not found.", 2);
1270 list($docid, $docname, $vendor) = db_fetch_array($sth, 0);
1273 $dir = GetGlobalDirectory();
1274 $filename =
"$docname.$vendor";
1276 $dir = GetCuDirectory($cu);
1277 $filename =
"$docname";
1280 if (!is_writable($dir)) {
1281 throw new exception (
"Directory is not writable.", 3);
1283 $fullname =
"$dir/$filename";
1284 $results = file_put_contents($fullname, $contents);
1285 if ($results ===
false) {
1286 throw new exception (
"File was not written.", 6);
1290 $dontSetExecutableBit = $cu ==
"";
1291 $results = SetFileMetadata($fullname, $dontSetExecutableBit);
1292 if ($results[
"status"] !=
"000") {
1293 throw new exception (
"File metadata was not updated.", 10);
1296 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"operation" =>
"update",
"info" => GetEstatementText() .
" file was saved successfully.");
1297 }
catch (exception $e) {
1298 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" =>
"update");
1300 return $returnArray;
1317 function RevertEstmntFile($dbh, $docid, $cu, $section) {
1320 throw new exception (
"Cu needs to be defined.", 9);
1322 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor from cucmsdocs d 1323 where docstype = " . GetDocType() .
" and docsid = " . intval($docid);
1324 $sth = db_query($sql, $dbh);
1326 throw new exception (
"SQL is not valid.", 1);
1328 if (db_num_rows($sth) == 0) {
1329 throw new exception (
"Document record is not found.", 2);
1332 list($docid, $docname, $vendor) = db_fetch_array($sth, 0);
1335 $defaultFilename =
"$docname.$vendor";
1336 $realDir = GetCuDirectory($cu);
1337 $defaultDir = GetGlobalDirectory();
1338 $filename =
"$docname";
1340 if (!is_writable($realDir)) {
1341 throw new exception (
"Directory is not writable.", 3);
1343 $fullname =
"$realDir/$filename";
1346 if (!is_readable($defaultDir)) {
1347 throw new exception (
"Default directory is not readable.", 4);
1350 $defaultFullname =
"$defaultDir/$defaultFilename";
1351 if (file_exists($defaultFullname)) {
1352 $contents = file_get_contents($defaultFullname);
1353 if ($contents ===
false) {
1354 throw new exception (
"Default contents are not readable.", 5);
1356 if (!isset($contents) || trim($contents) ==
"") {
1357 throw new exception (
"No default found.", 7);
1361 $results = SubstituteKeywords($contents, $cu);
1362 if ($results [
"status"] !==
"000") {
1363 throw new exception ($results [
"error"], 8);
1365 $contents = $results [
"data"];
1367 $results = file_put_contents($fullname, $contents);
1368 if ($results ===
false) {
1369 throw new exception (
"File was not written.", 6);
1372 $results = SetFileMetadata($fullname);
1373 if ($results[
"status"] !=
"000") {
1374 throw new exception (
"File metadata was not updated.", 10);
1377 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"info" => GetEstatementText() .
" file was reverted successfully.",
1378 "data" => array(
"record" => array()));
1380 if (isset($docname)) {
1381 switch(trim($docname)) {
1382 case GetPdfDefsFile():
1384 $results = ExtractPdfDefsForms($cu);
1385 $returnArray[
"data"][
"formList"] = $results[
"status"] ===
"000" ? array_keys($results[
"data"][
"formArray"]) : array();
1389 }
catch (exception $e) {
1390 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"record" => array()));
1392 return $returnArray;
1403 function SetFileMetadata($fullname, $isBackup =
false) {
1405 $docname = basename($fullname);
1408 $pos = strrpos($docname,
".");
1409 $ext = $pos ===
false ?
"" : substr($docname, $pos + 1);
1410 $execBit = GetExecPermission();
1412 $currentPerm = intval(substr(sprintf(
'%o', fileperms($fullname)), -4), 8);
1413 $neededFilePerms = GetFilePermissions();
1415 if (in_array($ext, GetExecutableFileExtensions())) {
1420 if (!chmod($fullname, $neededFilePerms)) {
1421 throw new exception (
"Change mode failed.", 9);
1424 if (!chmod($fullname, $neededFilePerms | $execBit)) {
1425 throw new exception (
"Change mode failed.", 10);
1428 }
else if ($currentPerm != $neededFilePerms) {
1429 if (!chmod($fullname, $neededFilePerms)) {
1430 throw new exception (
"Change mode failed.", 11);
1434 $returnArray = array(
"status" =>
"000",
"error" =>
"");
1435 }
catch (exception $e) {
1436 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1438 return $returnArray;
1457 function StartNewConfig($dbh, $cu, $vendor, $section) {
1459 $isGlobal = !isset($cu) || trim($cu) ==
"";
1461 throw new exception (
"Cu is required.", 1);
1463 $upperCaseCu = strtoupper(trim($cu));
1465 $dir = GetCuDirectory($cu);
1467 if (!is_writable($dir)) {
1468 throw new exception (
"Directory is not writable.", 2);
1472 $sql =
"select d.docsid as docid, d.docsname as docname, d.docsdesc as descr, d.docsmaintsort as sort, trim(d.docstitle) as vendor, 1473 trim(e.docstitle) as vendorl from cucmsdocs d 1474 inner join cucmsdocs e on d.docsname = e.docsname and d.docstype = e.docstype and d.docsmaintsection = e.docsmaintsection 1475 and d.docstype = " . GetDocType() .
" and d.docsmaintsection = '" . prep_save($section) .
"' and d.docstitle = '" . prep_save($vendor, 100) .
"' 1476 order by d.docsmaintsort, e.docstitle";
1477 $sth = db_query($sql, $dbh);
1479 throw new exception (
"Select query failed.", 1);
1481 $results = db_fetch_all($sth);
1482 $toBeAdded = $results ===
false ? array() : $results;
1485 $filesOverwritten = array();
1486 foreach($toBeAdded as $row) {
1487 if (!HCU_array_key_exists($row[
"docid"], $gridMap)) {
1488 $row[
"allvendors"] = array($row[
"vendorl"]);
1489 unset($row[
"vendorl"]);
1490 $gridMap[$row[
"docid"]] = $row;
1492 $filesOverwritten[] = trim($row[
"docname"]);
1494 $gridMap[$row[
"docid"]][
"allvendors"][] = $row[
"vendorl"];
1497 $toBeAdded = array_values($gridMap);
1499 if (count($toBeAdded) == 0) {
1500 throw new exception (
"There is no configuration for $vendor.", 12);
1503 $sql =
"select d.docsid as docid, d.docsname as docname from cucmsdocs d 1504 inner join cucmsfrags f on d.docsid = f.docsid and f.cu = '" . prep_save($upperCaseCu, 10) .
"' 1505 where d.docsmaintsection = '" . prep_save($section) .
"' and d.docstype = " . GetDocType() .
" 1506 order by d.docsmaintsort";
1507 $sth = db_query($sql, $dbh);
1509 throw new exception (
"Select query failed.", 3);
1511 $results = db_fetch_all($sth);
1512 $toBeDeleted = $results ===
false ? array() : $results;
1515 foreach ($toBeDeleted as $row) {
1516 $file =
"$dir/" . $row[
"docname"];
1517 if (file_exists($file)) {
1518 if (!unlink($file)) {
1519 throw new exception (
"$file was not deleted.", 4);
1525 $globalDir = GetGlobalDirectory();
1527 if (!is_readable($globalDir)) {
1528 throw new exception (
"Global Directory is not readable.", 6);
1531 foreach ($toBeAdded as $row) {
1532 $saveFile =
"$dir/" . $row[
"docname"];
1533 $retrieveFile =
"$globalDir/" . $row[
"docname"] .
".$vendor";
1534 if (!file_exists($retrieveFile)) {
1535 throw new exception (
"$retrieveFile does not exist.", 5);
1538 $fileContents = file_get_contents($retrieveFile);
1539 if ($fileContents ===
false) {
1540 throw new exception (
"$retrieveFile is not readable.", 8);
1543 $results = SubstituteKeywords($fileContents, $cu);
1544 if ($results [
"status"] !==
"000") {
1545 throw new exception (
"$retrieveFile could not translate correctly.", 14);
1547 $fileContents = $results[
"data"];
1549 $results = file_put_contents($saveFile, $fileContents);
1550 if ($results ===
false) {
1551 throw new exception (
"$saveFile is not writable.", 9);
1554 $results = SetFileMetadata($saveFile);
1555 if ($results[
"status"] !=
"000") {
1556 throw new exception (
"File metadata change failed.", 15);
1561 $results = ExtractPdfDefsForms($cu);
1562 $formList = $results[
"status"] ===
"000" ? array_keys($results[
"data"][
"formArray"]) : array();
1564 if (!db_work ($dbh, HOMECU_WORK_BEGIN)) {
1565 throw new exception(
"Begin query failed.", 13);
1569 $docidsToDelete = array();
1570 foreach ($toBeDeleted as $row) {
1571 $docidsToDelete [] = intval($row[
"docid"]);
1574 if (count($docidsToDelete) > 0) {
1575 $sql =
"delete from cucmsfrags where cu = '" . prep_save($upperCaseCu, 10) .
"' and docsid in (" . implode(
",", $docidsToDelete) .
")";
1576 $sth = db_query($sql, $dbh);
1578 throw new exception (
"Delete failed.", 110);
1584 foreach ($toBeAdded as $row) {
1585 $toAdd [] =
"('" . prep_save($upperCaseCu, 10) .
"', " . intval($row[
"docid"]) .
")";
1588 if (count($toAdd) > 0) {
1589 $sql =
"insert into cucmsfrags (cu, docsid) values " . implode(
", ", $toAdd);
1590 $sth = db_query($sql, $dbh);
1592 throw new exception (
"Add failed.", 111);
1596 if (!db_work ($dbh, HOMECU_WORK_COMMIT)) {
1597 throw new exception (
"End query failed.", 113);
1600 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"gridData" => $toBeAdded,
"currentVendor" => $vendor,
1601 "filesAffected" => $filesOverwritten,
"formList" => $formList),
1602 "operation" =>
"read",
"section" => $section,
"info" =>
"Configuration changed to $vendor successfully.");
1603 }
catch (exception $e) {
1604 if ($e->getCode() >= 100) {
1605 db_work($dbh, HOMECU_WORK_ROLLBACK);
1608 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array(
"gridData" => array()),
"operation" =>
"read",
1609 "section" => $section);
1611 return $returnArray;
1625 function ReadBackupFiles($dbh, $cu) {
1627 $gridData = array();
1628 $backupSuffix = GetBackupSuffix();
1630 if (!isset($cu) || trim($cu) ==
"") {
1631 throw new exception (
"Cu is required.", 1);
1634 $dir = GetCuDirectory($cu);
1636 if (!is_writable($dir)) {
1637 throw new exception (
"Cu directory is not writable.", 2);
1640 if ($dh = opendir($dir)) {
1641 while (($file = readdir($dh)) !==
false) {
1642 $split = explode(
".", $file);
1643 $count = count($split);
1645 $keyword = $split [$count - 1];
1646 $maybe = $split [$count - 2];
1648 if ($maybe == $backupSuffix && filetype(
"$dir/$file") ==
"file") {
1649 $gridData[] = array(
"file" => $file,
"keyword" => $keyword);
1656 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $gridData);
1657 }
catch (exception $e) {
1658 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array());
1660 return $returnArray;
1675 function MakeBackup($dbh, $cu, $keyword) {
1677 $gridData = array();
1678 $backupSuffix = GetBackupSuffix();
1680 if (!isset($cu) || trim($cu) ==
"") {
1681 throw new exception (
"Cu is required.", 1);
1684 if (!isset($keyword) || trim($keyword) ==
"") {
1685 throw new exception (
"Keyword is required.", 3);
1688 $sql =
"select d.docsid as docid, d.docsname as docname, d.docstitle as vendor, docsdesc as descr, docsmaintsection as section from cucmsdocs d 1689 where docstype = " . GetDocType();
1690 $sth = db_query($sql, $dbh);
1693 throw new exception (
"Select has errors.", 4);
1696 $validFiles = array();
1697 $results = db_fetch_all($sth, $dbh);
1698 $results = $results ===
false ? array() : $results;
1699 foreach($results as $row) {
1700 $validFiles[] = trim($row[
"docname"]);
1703 $dir = GetCuDirectory($cu);
1705 if (!is_writable($dir)) {
1706 throw new exception (
"Cu directory is not writable.", 2);
1709 if ($dh = opendir($dir)) {
1710 while (($file = readdir($dh)) !==
false) {
1711 if (in_array($file, $validFiles)) {
1712 $oldFile =
"$dir/$file";
1713 $newFile =
"$dir/$file.$backupSuffix.$keyword";
1714 if (!copy($oldFile, $newFile)) {
1715 throw new exception (
"File wasn't copied.", 5);
1718 $results = SetFileMetadata($newFile,
true);
1719 if ($results[
"status"] !=
"000") {
1720 throw new exception (
"Metadata was not updated.", 6);
1723 $gridData[] = array(
"file" =>
"$file.$backupSuffix.$keyword",
"keyword" => $keyword);
1725 $split = explode(
".", $file);
1726 $count = count($split);
1728 $testKeyword = $split [$count - 1];
1729 $maybe = $split [$count - 2];
1731 if ($maybe == $backupSuffix && filetype(
"$dir/$file") ==
"file") {
1732 $gridData[] = array(
"file" => $file,
"keyword" => $testKeyword);
1740 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $gridData,
"info" =>
"Backup was made successfully.");
1741 }
catch (exception $e) {
1742 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array());
1744 return $returnArray;
1760 function RemoveBackup($dbh, $cu, $keyword) {
1762 $gridData = array();
1763 $backupSuffix = GetBackupSuffix();
1765 if (!isset($cu) || trim($cu) ==
"") {
1766 throw new exception (
"Cu is required.", 1);
1769 if (!isset($keyword) || trim($keyword) ==
"") {
1770 throw new exception (
"Keyword is required.", 3);
1773 $dir = GetCuDirectory($cu);
1775 if (!is_writable($dir)) {
1776 throw new exception (
"Cu directory is not writable.", 2);
1779 if ($dh = opendir($dir)) {
1780 while (($file = readdir($dh)) !==
false) {
1781 $split = explode(
".", $file);
1782 $count = count($split);
1785 $testKeyword = $split [$count - 1];
1786 $maybe = $split [$count - 2];
1788 if ($maybe == $backupSuffix && filetype(
"$dir/$file") ==
"file") {
1789 if ($testKeyword == $keyword) {
1790 if (!unlink(
"$dir/$file")) {
1791 throw new exception (
"File could not be deleted.", 3);
1794 $gridData[] = array(
"file" => $file,
"keyword" => $testKeyword);
1803 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => $gridData,
"info" =>
"Backup was removed successfully.");
1804 }
catch (exception $e) {
1805 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"data" => array());
1807 return $returnArray;
1824 function ViewBackup($dbh, $cu, $file) {
1826 if (!isset($cu) || trim($cu) ==
"") {
1827 throw new exception (
"Cu is required.", 1);
1830 if (!isset($file) || trim($file) ==
"") {
1831 throw new exception (
"File is required.", 3);
1835 if (preg_match(
'/^(.*)[.](' . GetBackupSuffix() .
'[.][^.]+)$/', $file, $matches) !== 1){
1836 throw new exception (
"File is not valid.", 9);
1838 $regularFile = $matches[1];
1841 $sql =
"select 'FOUND' from cucmsdocs d 1842 where d.docsname = '" . prep_save($regularFile, 50) .
"' and docsmaintsection is not null 1843 and docstype = " . GetDocType();
1845 $sth = db_query($sql, $dbh);
1847 throw new exception (
"Found query failed.", 5);
1849 if (db_num_rows($sth) == 0) {
1850 throw new exception (
"File is not valid.", 6);
1853 $dir = GetCuDirectory($cu);
1855 if (!is_readable($dir)) {
1856 throw new exception (
"Cu directory is not writable.", 2);
1859 if (!is_readable(
"$dir/$file")) {
1860 throw new exception (
"File is not readable.", 7);
1863 $contents = file_get_contents(
"$dir/$file");
1864 if ($contents ===
false) {
1865 throw new exception (
"File is not readable.", 8);
1868 $returnArray = array(
"status" =>
"000",
"error" =>
"",
"data" => array(
"file" => $file,
"contents" => $contents),
"operation" =>
"backup");
1869 }
catch (exception $e) {
1870 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage(),
"operation" =>
"backup",
"data" => array());
1872 return $returnArray;
1886 function TestPdfDefs ($cu, $selectedForm) {
1888 $results = ExtractPdfDefsForms($cu,
true);
1889 if ($results[
"status"] !==
"000") {
1890 throw new exception (
"Forms are not valid.", 1);
1892 extract($results[
"data"]);
1894 $useformLine = HCU_array_key_value($selectedForm, $formArray);
1896 if ($useformLine ===
false) {
1897 throw new exception (
"Form is not found.", 2);
1900 $contents .=
"\n$useformLine";
1902 $inputFile = GetDefaultTmpInput($cu);
1904 $results = file_put_contents($inputFile, $contents);
1905 if ($results ===
false) {
1906 throw new exception (
"Temporary file was not created.", 3);
1909 $returnArray = TestPdfFile($inputFile, $cu);
1911 }
catch (exception $e) {
1912 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1914 return $returnArray;
1929 function TestPdfDisclosure ($dbh, $cu, $docid, $section) {
1931 $sql =
"select docsname, docstitle 1933 where docsid = " . intval($docid) .
" and docsmaintsection = '" . prep_save($section) .
"' 1934 and docstype = " . GetDocType();
1935 $sth = db_query($sql, $dbh);
1937 throw new exception (
"Query failed.", 1);
1939 if (db_num_rows($sth) <= 0) {
1940 throw new exception (
"File not found.", 2);
1942 list($file, $vendor) = db_fetch_row($sth, 0);
1944 if ($section ==
"Utility") {
1945 $inputFile = GetGlobalDirectory() .
"/" . basename($file) .
"." . $vendor;
1947 $inputFile = GetCuDirectory($cu) .
"/" . basename($file);
1950 $returnArray = TestPdfFile($inputFile, $cu);
1951 }
catch (exception $e) {
1952 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1954 return $returnArray;
1967 function TestPdfFile ($inputFile, $cu) {
1969 $outputFile = GetDefaultTmpOutput($cu);
1971 if (!file_exists($inputFile)) {
1972 throw new exception (
"input file doesn't exist.", 1);
1975 $cmd =
"/opt/odyssey/tools/bin/pdffile.cgi $inputFile $outputFile 2>&1";
1978 $results = exec($cmd, $output, $returnVar);
1980 if ($results ===
false) {
1981 throw new exception (
"Command failed.", 2);
1984 if ($results !==
"") {
1985 throw new exception ($results, 3);
1988 if (!file_exists($outputFile)) {
1989 throw new exception (
"output file wasn't created successfully.", 4);
1992 $returnArray = array(
"status" =>
"000",
"error" =>
"");
1993 }
catch (exception $e) {
1994 $returnArray = array(
"status" => $e->getCode(),
"error" => $e->getMessage());
1996 return $returnArray;
2005 function ViewPdfTestOutput($cu) {
2007 $pdffile = fopen(GetDefaultTmpOutput($cu),
"r");
2008 $sArray = fstat($pdffile);
2009 $size = $sArray[
"size"];
2010 header (
"Content-Type: application/pdf");
2011 header (
"Content-length: $size");
2012 fpassthru ($pdffile);