+<?php
+error_reporting(E_ALL);
+$root = $_SERVER['DOCUMENT_ROOT'];
+$kind = array_key_exists('kind', $_POST) ? $_POST['kind'] : 'youtube';
+$template = array_key_exists('template', $_POST) ? htmlentities($_POST['template']) : loadFile("$kind.html");
+$csv = array_key_exists('csv', $_POST) ? htmlentities($_POST['csv']) : loadFile("$kind.csv");
+$result = array_key_exists('result', $_POST) ? htmlentities($_POST['result']) : '';
+
+$mode = array_key_exists('mode', $_POST) ? htmlentities($_POST['mode']) : '';
+if (array_key_exists('mode', $_GET)){
+ $mode = $_GET['mode'];
+}
+if ($mode === ''){
+ $mode = 'main';
+}
+$yt_select = '';
+switch($kind){
+case 'youtube':
+ $yt_select = ' select="select"';
+ break;
+default:
+ break;
+}
+$body = "<html><body><pre>Mode: '$mode'</pre></body></html>";
+$result = '';
+if ($mode === 'main'){
+ if (array_key_exists('convert', $_POST)) {
+ $result = convert($template, $csv);
+ } elseif (array_key_exists('save_template', $_POST)) {
+ saveFile("$kind.html", $_POST['template']);
+ } elseif (array_key_exists('save_csv', $_POST)) {
+ saveFile("$kind.csv", $_POST['csv']);
+ } elseif (array_key_exists('upload', $_POST)){
+ $mode = 'upload';
+ }
+ $body = "
+<html>
+<body>
+<form action=\"emastool.php\" method=\"post\">
+<select name=\"kind\">
+<option value=\"youtube\"$yt_select>Youtube</option>
+</select>
+<input type=\"hidden\" name=\"mode\" value=\"$mode\" />
+<p>Vorlage: <button name=\"save_template\">Speichern</button></p>
+<textarea name=\"template\" rows=\"5\" cols=\"80\">$template
+</textArea>
+<p>CSV-Daten: <button name=\"save_csv\">Speichern</button></p>
+<textarea name=\"csv\" rows=\"5\" cols=\"80\">$csv
+</textArea>
+<p><button name=\"convert\">Konvertieren</button>
+</p>
+<p>Ergebnis:</p>
+<textarea name=\"result\" rows=\"15\" cols=\"80\">$result
+</textArea>
+</form>
+</body>
+</html>
+";
+} elseif ($mode === 'upload') {
+ $info = '';
+ if (array_key_exists('toShell', $_POST)) {
+ $mode = "main";
+ } elseif (array_key_exists('select', $_FILES)) {
+ $tempName = $_FILES['select']['tmp_name'];
+ $nodeName = $_FILES['select']['name'];
+ $error = $_FILES['select']['error'];
+ $size = $_FILES['select']['size'];
+ rename($tempName, $tempDir . '/' . $nodeName);
+ if (! file_exists($tempDir)){
+ mkdir($tempDir);
+ }
+ $list = scandir($tempDir);
+ $files = implode("\n", $list);
+ $info = "<h1>Hochladeinfo</h1>
+<p>TempName: $tempName<br/>
+Name: $nodeName<br/>
+Größe: $size<br/>
+Fehler: $error<br/>
+</p>
+<h1>Temp-Verzeichnis $tempDir</h1>
+<pre>$files</pre>
+";
+ }
+ $body = "
+<html>
+<body>
+<form enctype=\"multipart/form-data\" action=\"emastool.php\" method=\"post\">
+<p><a href=\"emastool.php?mode=shell\">Gehe zur Shell</a>
+</p>
+<input type=\"hidden\" name=\"mode\" value=\"$mode\" />
+<p>Eingabe:</p>
+<input type=\"file\" name=\"select\" />
+<p><button name=\"doit\">Hochladen</button>
+</p>
+$info
+</form>
+</body>
+</html>
+";
+}
+print $body;
+
+function saveFile(string $name, string $content){
+ global $root;
+ $fn = "$root/data/$name";
+ file_put_contents($fn, $content);
+}
+function loadFile(string $name): string{
+ global $root;
+ $fn = "$root/data/$name";
+ $rc = file_get_contents($fn);
+ return $rc;
+}
+function convert(string $template, string $csv): string {
+ $rc = '';
+ $data = explode("\n", $csv);
+ if (count($data) > 1){
+ $headers = explode(';', trim($data[0]));
+ unset($data[0]);
+ foreach ($data as $line){
+ if ( ($line = trim($line)) === ''){
+ continue;
+ }
+ $cols = explode(';', $line);
+ $item = $template;
+ for ($ix = 0; $ix < count($cols); $ix++){
+ if ($ix >= count($headers)){
+ continue;
+ }
+ $name = $headers[$ix];
+ $item = str_replace("#$name#", $cols[$ix], $item);
+ }
+ $rc .= $item . "\n";
+ }
+ }
+ return $rc;
+}
\ No newline at end of file