CopyController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. class CopyController
  3. {
  4. public static function Run ($argv)
  5. {
  6. if (count($argv) < 3) die();
  7. $copyCtrl = new CopyController();
  8. $tol = (count($argv) > 3) ? $argv[3] : 20;
  9. $copyCtrl->Copy($argv[1], $argv[2], $tol);
  10. }
  11. public function Copy ($source, $target, $tolerance = 20, $timeout = 300, $retry = 20)
  12. {
  13. if (!is_dir($source)) {
  14. $source = dirname($source);
  15. }
  16. $dir = dir($source);
  17. $start = strtotime("now");
  18. $oldest = $start - ($tolerance * 60 * 60);
  19. for ($i = 0; $i < $retry; $i++) {
  20. if ($this->anyFileYoungerThan($source, $oldest) && $this->AllFilesOlderThan($source, strtotime("-10 min"))) {
  21. $mins = floor((strtotime("now") - $start) / 60);
  22. if ($mins > 1) {
  23. echo "* Wartezeit: {$mins} Min.\r\n";
  24. }
  25. break;
  26. }
  27. if ($i == 0) {
  28. if (!$this->anyFileYoungerThan($source, $oldest)) {
  29. echo " Es liegen noch keine aktuellen Daten vor\r\n";
  30. } else {
  31. echo " Der Export ist noch nicht vollstaendig\r\n";
  32. }
  33. }
  34. echo " Warte 5 Minuten...\r\n";
  35. sleep($timeout);
  36. }
  37. if (!$this->anyFileYoungerThan($source, $oldest)) {
  38. echo "(CR0004) Alle Dateien sind veraltet. Prozess wurde abgebrochen.\r\n";
  39. exit(5);
  40. }
  41. while ($txtFile = $dir->read()) {
  42. if (!preg_match('/\.txt$/i', $txtFile)) {
  43. continue;
  44. }
  45. $sourceFile = $source . "\\" . $txtFile;
  46. $targetFile = $target . "\\" . $txtFile;
  47. $modified = date("d.m.Y H:i", filemtime($sourceFile));
  48. if (filemtime($sourceFile) >= $oldest) {
  49. $unlocker = exec("cmd.exe /c unlocker.bat {$targetFile}");
  50. if (copy($sourceFile, $targetFile)) {
  51. echo " {$txtFile} ({$modified})\r\n";
  52. $this->Replace($targetFile);
  53. } else {
  54. echo $unlocker . "\r\n";
  55. echo "(CR0002) Kopieren von '{$sourceFile}' fehlgeschlagen.\r\n";
  56. }
  57. } else {
  58. echo "(CR0005) Datei '{$sourceFile}' ist nicht aktuell. Stand: {$modified}\r\n";
  59. }
  60. }
  61. }
  62. public function AllFilesOlderThan ($dirname, $timestamp)
  63. {
  64. $dir = dir($dirname);
  65. while ($txtFile = $dir->read()) {
  66. if (!preg_match('/\.txt$/i', $txtFile)) {
  67. continue;
  68. }
  69. if (filemtime($dirname . "\\" . $txtFile) > $timestamp) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. public function AnyFileYoungerThan ($dirname, $timestamp)
  76. {
  77. $dir = dir($dirname);
  78. while ($txtFile = $dir->read()) {
  79. if (!preg_match('/\.txt$/i', $txtFile)) {
  80. continue;
  81. }
  82. if (filemtime($dirname . "\\" . $txtFile) > $timestamp) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. public function Replace ($filename)
  89. {
  90. if (!isset($filename) || !file_exists($filename)) {
  91. return "(CR0003) Die Datei '{$filename}' ist nicht vorhanden.\r\n";
  92. }
  93. $tempfile = $filename . ".temp";
  94. @unlink($tempfile);
  95. if (!rename($filename, $tempfile)) return "(CR0007) Die Datei '{$filename}' ist schreibgeschuetzt.\r\n";
  96. $readHandle = fopen($tempfile, "r");
  97. $writeHandle = fopen($filename, "w+");
  98. if (!$readHandle) return "(CR0008) Die temporaere Datei '{$tempfile}' ist nicht vorhanden.\r\n";
  99. if (!$writeHandle) return "(CR0009) Die Datei '{$filename}' ist schreibgeschuetzt.\r\n";
  100. while (!feof($readHandle)) {
  101. fwrite($writeHandle, str_replace('"', "'", fgets($readHandle)));
  102. }
  103. fclose($readHandle);
  104. fclose($writeHandle);
  105. unlink($tempfile);
  106. return true;
  107. }
  108. }