BootstrapController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. class BootstrapController
  3. {
  4. private $starterBatchPath;
  5. private $tasksPath;
  6. private $config;
  7. function __construct ($path = "", $path2 = "")
  8. {
  9. $this->config = parse_ini_file(dirname(__FILE__) . "/../../../../GAPS.ini");
  10. $this->starterBatchPath = ($path == "") ? $this->config['STARTER'] : $path;
  11. $this->tasksPath = ($path2 == "") ? realpath(dirname(__FILE__) . "/../../../..") : $path2;
  12. $this->protPath = (isset($this->config['PROT'])) ? $this->config['PROT'] : $this->config['PORTAL'] . "\\System\\Prot";
  13. }
  14. function bootstrap ($get, $post = array())
  15. {
  16. if (!isset($get['action']) || $get['action'] == "") {
  17. if (isset($get['gchr'])) {
  18. $get['action'] = "gchr";
  19. }
  20. else if (isset($get['log'])) {
  21. $get['action'] = "logs";
  22. $get['params'] = $get['log'];
  23. }
  24. else if (isset($get['batch'])) {
  25. $get['action'] = "batch";
  26. $get['params'] = $get['batch'];
  27. }
  28. else if (isset($get['valid'])) {
  29. $get['action'] = "batch";
  30. $get['params'] = "GCStarter.bat";
  31. }
  32. else {
  33. return "Webservice funktioniert.";
  34. }
  35. }
  36. switch ($get['action']) {
  37. case 'options':
  38. require_once(dirname(__FILE__) . '/RemoteController.php');
  39. $remote = RemoteController::Run();
  40. return json_encode($remote);
  41. case 'details':
  42. if (!isset($get['params'])) {
  43. return "Fehler: fehlender Parameter";
  44. }
  45. require_once(dirname(__FILE__) . '/RemoteController.php');
  46. $remote = RemoteController::Run($get['params'], $post['files']);
  47. return json_encode($remote);
  48. case 'gchr':
  49. if (!isset($post['dsn']) || !isset($post['query'])) {
  50. return "Fehler: fehlende Parameter";
  51. }
  52. require_once(dirname(__FILE__) . '/OdbcController.php');
  53. $odbcCtrl = new OdbcController($post['dsn']);
  54. return $odbcCtrl->executeAndConvertToCsv($post['query']);
  55. case 'logs':
  56. if (!isset($get['params'])) {
  57. return "Fehler: fehlender Parameter";
  58. }
  59. if (substr($get['params'], 0, 5) == "Prot\\") {
  60. $logfile = "{$this->protPath}\\" . substr($get['params'], 5);
  61. } else {
  62. $logfile = "{$this->tasksPath}\\logs\\{$get['params']}";
  63. }
  64. if (substr($logfile, -4) != ".log") {
  65. $logfile .= ".log";
  66. }
  67. if (!file_exists($logfile)) {
  68. return "Fehler: Datei `" . addslashes($logfile) . "` nicht gefunden";
  69. }
  70. $stop = (filemtime($logfile) < strtotime("now") - 20 * 60) ? "(.)" : "";
  71. $json = json_encode(str_replace("\"", "`", utf8_encode(file_get_contents($logfile)) . $stop));
  72. if (function_exists("json_last_error") && json_last_error() != 0) {
  73. return "Fehler: " . $this->jsonLastErrorMsg();
  74. }
  75. return $json;
  76. case 'batch':
  77. if (!isset($get['params'])) {
  78. return "Fehler: fehlender Parameter";
  79. }
  80. $batchFile = "{$this->tasksPath}\\{$get['params']}";
  81. $result = $get['params'];
  82. if (isset($post['job'])) {
  83. file_put_contents($batchFile, $post['job']);
  84. $result = json_encode($result);
  85. }
  86. if (!file_exists($batchFile)) {
  87. $batchFile = "{$this->starterBatchPath}\\{$get['params']}";
  88. }
  89. if (!file_exists($batchFile)) {
  90. return "Fehler: Datei '{$get['params']}' unbekannt";
  91. }
  92. $logFile = "{$this->tasksPath}\\logs\\{$get['params']}.log";
  93. $WshShell = new COM("WScript.Shell");
  94. $cmd = "cmd /C \"call {$batchFile} 1> {$logFile} 2>&1\"";
  95. $WshShell->Run($cmd, 0, false);
  96. return $result;
  97. }
  98. return "Fehler: Falscher Aufruf";
  99. }
  100. private function jsonLastErrorMsg ()
  101. {
  102. switch (json_last_error()) {
  103. case JSON_ERROR_NONE:
  104. return ' - No errors';
  105. case JSON_ERROR_DEPTH:
  106. return ' - Maximum stack depth exceeded';
  107. case JSON_ERROR_STATE_MISMATCH:
  108. return ' - Underflow or the modes mismatch';
  109. case JSON_ERROR_CTRL_CHAR:
  110. return ' - Unexpected control character found';
  111. case JSON_ERROR_SYNTAX:
  112. return ' - Syntax error, malformed JSON';
  113. case JSON_ERROR_UTF8:
  114. return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  115. default:
  116. return ' - Unknown error';
  117. }
  118. }
  119. }