diff --git a/html/Setup.php b/html/Setup.php index f346a38..4d9636b 100644 --- a/html/Setup.php +++ b/html/Setup.php @@ -4,6 +4,26 @@ require_once("Wizard.php"); $wizard = new Wizard(); +$wizard->addCode('selectdatabase',function($storedvariables){ + $selectdatabase['databasetype'] = $_POST['databasetype']; + return $selectdatabase; +}); + +$wizard->addCode('enterlogindata',function($storedvariables){ + $logindata['host'] = $_POST['host']; + $logindata['user'] = $_POST['user']; + $logindata['password'] = $_POST['password']; + $logindata['dbname'] = $_POST['dbname']; + $conf_file = fopen("../etc/konfiguration.php", "w"); + fwrite($conf_file, ""); + return $logindata; +}); + $wizard->renderPHP(); ?> diff --git a/html/Wizard.php b/html/Wizard.php index 26e9c7a..a559b5f 100644 --- a/html/Wizard.php +++ b/html/Wizard.php @@ -3,6 +3,8 @@ class WizardStep { public string $page = ""; public array $redirects = array(); + public string $warning = ""; + public $code = null; public function __construct($step){ if(isset($step['page'])){ $this->page = $step['page']; @@ -16,49 +18,76 @@ class WizardStep { foreach($this->redirects as $redirect){ if(isset($redirect['==']) && isset($redirect['id'])){ foreach($redirect['=='] as $key => $value){ - if(isset($_GET[$key]) && $_GET[$key] == $value){ - $_GET['step'] = $redirect['id']; + if(isset($_POST[$key]) && $_POST[$key] == $value){ + $_POST['step'] = $redirect['id']; + if(isset($redirect['warning'])){ + $this->warning = $redirect['warning']; + } + return; } } } if(isset($redirect['!=']) && isset($redirect['id'])){ foreach($redirect['!='] as $key => $value){ - if(isset($_GET[$key]) && $_GET[$key] != $value){ - $_GET['step'] = $redirect['id']; + if(isset($_POST[$key]) && $_POST[$key] != $value){ + $_POST['step'] = $redirect['id']; + if(isset($redirect['warning'])){ + $this->warning = $redirect['warning']; + } + return; } } } if(isset($redirect['>']) && isset($redirect['id'])){ foreach($redirect['>'] as $key => $value){ - if(isset($_GET[$key]) && $_GET[$key] > $value){ - $_GET['step'] = $redirect['id']; + if(isset($_POST[$key]) && $_POST[$key] > $value){ + $_POST['step'] = $redirect['id']; + if(isset($redirect['warning'])){ + $this->warning = $redirect['warning']; + } + return; } } } if(isset($redirect['<']) && isset($redirect['id'])){ foreach($redirect['<'] as $key => $value){ - if(isset($_GET[$key]) && $_GET[$key] < $value){ - $_GET['step'] = $redirect['id']; + if(isset($_POST[$key]) && $_POST[$key] < $value){ + $_POST['step'] = $redirect['id']; + if(isset($redirect['warning'])){ + $this->warning = $redirect['warning']; + } + return; } } } if(isset($redirect['>=']) && isset($redirect['id'])){ foreach($redirect['>='] as $key => $value){ - if(isset($_GET[$key]) && $_GET[$key] >= $value){ - $_GET['step'] = $redirect['id']; + if(isset($_POST[$key]) && $_POST[$key] >= $value){ + $_POST['step'] = $redirect['id']; + if(isset($redirect['warning'])){ + $this->warning = $redirect['warning']; + } + return; } } } if(isset($redirect['<=']) && isset($redirect['id'])){ foreach($redirect['<='] as $key => $value){ - if(isset($_GET[$key]) && $_GET[$key] <= $value){ - $_GET['step'] = $redirect['id']; + if(isset($_POST[$key]) && $_POST[$key] <= $value){ + $_POST['step'] = $redirect['id']; + if(isset($redirect['warning'])){ + $this->warning = $redirect['warning']; + } + return; } } } } } } + public function setCode(callable $function){ + $this->code = $function; + } } class Wizard { @@ -66,6 +95,7 @@ class Wizard { private string $firststep = ""; private array $steps = array(); private string $footer = ""; + private array $storedvariables = array(); public function __construct($json_file = 'setupWizard.json'){ $stepsArray = json_decode(file_get_contents($json_file), true); $this->header = $stepsArray['header']; @@ -74,19 +104,40 @@ class Wizard { $this->steps[$step['id']] = new WizardStep($step); } $this->footer = $stepsArray['footer']; + if(isset($_POST['storedvariables'])){ + $this->storedvariables = json_decode($_POST['storedvariables'],JSON_FORCE_OBJECT); + } } public function renderPHP(){ - if(isset($_GET['stepfrom'])){ - $this->steps[$_GET['stepfrom']]->setCurrentStep(); - } echo $this->header; - if(isset($_GET['step'])){ - echo $this->steps[$_GET['step']]->page; + if(isset($_POST['stepfrom'])){ + $this->steps[$_POST['stepfrom']]->setCurrentStep(); + if(isset($_POST['step']) && ($_POST['step'] != $_POST['stepfrom'])){ + if(!is_null($this->steps[$_POST['stepfrom']]->code)){ + $this->storedvariables[$_POST['stepfrom']] = call_user_func($this->steps[$_POST['stepfrom']]->code, $this->storedvariables); + } + } } - else{ - echo $this->steps[$this->firststep]->page; + if(!isset($_POST['step'])){ + $_POST['step'] = $this->firststep; } + // If warning not set this does not add anything to the page: + echo $this->steps[$_POST['step']]->warning; + $pagedom = new DOMDocument(); + $pagedom->loadHTML("\xEF\xBB\xBF".$this->steps[$_POST['step']]->page); + $forms = $pagedom->getElementsByTagName('form'); + foreach($forms as $form){ + $input = $pagedom->createElement('input'); + $input->setAttribute('type','hidden'); + $input->setAttribute('name','storedvariables'); + $input->setAttribute('value',json_encode($this->storedvariables,JSON_FORCE_OBJECT)); + $form->appendChild($input); + } + echo $pagedom->saveHTML(); echo $this->footer; } + public function addCode($stepid, $function){ + $this->steps[$stepid]->setCode($function); + } } ?> diff --git a/html/setupWizard.json b/html/setupWizard.json index e47a17d..d98acf2 100644 --- a/html/setupWizard.json +++ b/html/setupWizard.json @@ -4,7 +4,7 @@ "steps": [ { "id": "selectdatabase", - "page": "
Für die meisten Conventions sollte eine SQLite Datenbank ausreichen. In diesem Fall ist die Datenbank in einer einzelnen Datei und es ist nicht nötig eine separate Datenbank aufzusetzen.<\/p>