<?php
class BootstrapController
{
	private $starterBatchPath;
	private $tasksPath;
	private $config;

	function __construct ($path = "", $path2 = "")
	{
		$this->config = parse_ini_file(dirname(__FILE__) . "/../../../../GAPS.ini");
		$this->starterBatchPath = ($path == "") ? $this->config['STARTER'] : $path;
		$this->tasksPath = ($path2 == "") ? realpath(dirname(__FILE__) . "/../../../..") : $path2;
		$this->protPath = (isset($this->config['PROT'])) ? $this->config['PROT'] : $this->config['PORTAL'] . "\\System\\Prot";
	}


	function bootstrap ($get, $post = array())
	{
		if (!isset($get['action']) || $get['action'] == "") {
			if (isset($get['gchr'])) {
				$get['action'] = "gchr";
			}
			else if (isset($get['log'])) {
				$get['action'] = "logs";
				$get['params'] = $get['log'];
			}
			else if (isset($get['batch'])) {
				$get['action'] = "batch";
				$get['params'] = $get['batch'];
			}
			else if (isset($get['valid'])) {
				$get['action'] = "batch";
				$get['params'] = "GCStarter.bat";
			}
			else {
				return "Webservice funktioniert.";
			}
		}

		switch ($get['action']) {
			case 'options':
				require_once(dirname(__FILE__) . '/RemoteController.php');
				$remote = RemoteController::Run();
				return json_encode($remote);

			case 'details':
				if (!isset($get['params'])) {
					return "Fehler: fehlender Parameter";
				}
				require_once(dirname(__FILE__) . '/RemoteController.php');
				$remote = RemoteController::Run($get['params'], $post['files']);
				return json_encode($remote);

			case 'gchr':
				if (!isset($post['dsn']) || !isset($post['query'])) {
					return "Fehler: fehlende Parameter";
				}
				require_once(dirname(__FILE__) . '/OdbcController.php');

				$odbcCtrl = new OdbcController($post['dsn']);
				return $odbcCtrl->executeAndConvertToCsv($post['query']);

			case 'logs':
				if (!isset($get['params'])) {
					return "Fehler: fehlender Parameter";
				}

				if (substr($get['params'], 0, 5) == "Prot\\") {
					$logfile = "{$this->protPath}\\" . substr($get['params'], 5);
				} else {
					$logfile = "{$this->tasksPath}\\logs\\{$get['params']}";
				}

				if (substr($logfile, -4) != ".log") {
					$logfile .= ".log";
				}
				if (!file_exists($logfile)) {
					return "Fehler: Datei `" . addslashes($logfile) . "` nicht gefunden";
				}
				$stop = (filemtime($logfile) < strtotime("now") - 20 * 60) ? "(.)" : "";
				$json = json_encode(str_replace("\"", "`", utf8_encode(file_get_contents($logfile)) . $stop));
				if (function_exists("json_last_error") && json_last_error() != 0) {
					return "Fehler: " . $this->jsonLastErrorMsg();
				}
				return $json;

			case 'batch':
				if (!isset($get['params'])) {
					return "Fehler: fehlender Parameter";
				}

				$batchFile = "{$this->tasksPath}\\{$get['params']}";
				$result = $get['params'];

				if (isset($post['job'])) {
					file_put_contents($batchFile, $post['job']);
					$result = json_encode($result);
				}

				if (!file_exists($batchFile)) {
					$batchFile = "{$this->starterBatchPath}\\{$get['params']}";
				}
				if (!file_exists($batchFile)) {
					return "Fehler: Datei '{$get['params']}' unbekannt";
				}
				$logFile = "{$this->tasksPath}\\logs\\{$get['params']}.log";

				$WshShell = new COM("WScript.Shell");
				$cmd = "cmd /C \"call {$batchFile} 1> {$logFile} 2>&1\"";
				$WshShell->Run($cmd, 0, false);
				return $result;
		}
		return "Fehler: Falscher Aufruf";
	}

	private function jsonLastErrorMsg ()
	{
		switch (json_last_error()) {
			case JSON_ERROR_NONE:
				return ' - No errors';
			case JSON_ERROR_DEPTH:
				return ' - Maximum stack depth exceeded';
			case JSON_ERROR_STATE_MISMATCH:
				return ' - Underflow or the modes mismatch';
			case JSON_ERROR_CTRL_CHAR:
				return ' - Unexpected control character found';
			case JSON_ERROR_SYNTAX:
				return ' - Syntax error, malformed JSON';
			case JSON_ERROR_UTF8:
				return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
			default:
				return ' - Unknown error';
		}
	}
}