Web-UI: Load .env via phpdotenv

This commit is contained in:
Malte Bublitz 2023-11-09 12:43:54 +01:00
parent 363e242654
commit 953534cce8
Signed by: malte70
GPG key ID: 605DA5C729F9C184
2 changed files with 43 additions and 6 deletions

View file

@ -1,16 +1,48 @@
<?php
require_once("vendor/autoload.php");
define("FIFO", "/tmp/mapid");
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . "/..");
$dotenv->load();
//define("FIFO", "/tmp/mapid");
define("FIFO", $_ENV['FIFO']);
if (!file_exists(FIFO))
die("<!DOCTYPE html>\n<meta charset=\"UTF-8\">\n<h3 style=\"color:red;\">Error: FIFO file “" . FIFO . "” does not exist!</h3><p>Is <tt>fifo_bridge.py</tt> running?</p>");
if (!file_exists(FIFO)) {
$ErrorDoc = <<<EOT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h3 style="color:red;">Error: FIFO file %FIFO% does not exist!</h3>
<p>
Is <tt>fifo_bridge.py</tt> running?
</p>
<p>
<a href="./">
<button>
Return
</button>
</a>
</p>
</body>
</html>
EOT;
$ErrorDoc = str_replace("%FIFO%", FIFO, $ErrorDoc);
die($ErrorDoc);
}
$f = fopen(FIFO, "w+");
if (count($_POST) == 0)
if (count($_POST) == 0) {
/**
* Support httpie like this:
* http --print=HBhb POST mcp.local:8080/~malte70/mapid-fifo-webui/command.php cmd=cls
*/
$_POST = json_decode(file_get_contents('php://input'), true);
}
switch (@$_POST["cmd"]) {
case "getty":
@ -43,11 +75,11 @@ switch (@$_POST["cmd"]) {
fwrite($f, "CLS\n");
fwrite($f, "LINE 1\n");
fwrite($f, "ECHO ");
fwrite($f, $_POST["line1"]);
fwrite($f, substr($_POST["line1"], 0, 16));
fwrite($f, "\n");
fwrite($f, "LINE 2\n");
fwrite($f, "ECHO ");
fwrite($f, $_POST["line2"]);
fwrite($f, substr($_POST["line2"], 0, 16));
break;
}

5
fifo-webui/composer.json Normal file
View file

@ -0,0 +1,5 @@
{
"require": {
"vlucas/phpdotenv": "^5.5"
}
}