diff --git a/app/Mcp.php b/app/Mcp.php index f0e5f78..94d16cb 100644 --- a/app/Mcp.php +++ b/app/Mcp.php @@ -11,6 +11,7 @@ class Mcp implements ConnectionProvider private ?PDO $db = null; private array $config; + private string $dataDir; private string $templateDir; const ROUTES = [ @@ -42,6 +43,7 @@ class Mcp implements ConnectionProvider public function __construct($basedir) { $this->templateDir = $basedir.DIRECTORY_SEPARATOR.'templates'; + $this->dataDir = $basedir.DIRECTORY_SEPARATOR.'data'; $this->config = array(); try { $config = parse_ini_file($basedir.DIRECTORY_SEPARATOR.'config.ini', true); @@ -95,6 +97,11 @@ class Mcp implements ConnectionProvider ])->unsafeVar('csrf', $this->csrfField()); } + public function getDataDir(): string + { + return $this->dataDir; + } + public function handleRequest() { $reqClass = 'Mcp\\Page\\Error'; diff --git a/app/cron/IarMonitor.php b/app/cron/IarMonitor.php new file mode 100644 index 0000000..d6d6344 --- /dev/null +++ b/app/cron/IarMonitor.php @@ -0,0 +1,66 @@ +app->db()); + + $statement = $this->app->db()->prepare("SELECT userID,iarfilename,filesize,state,created FROM mcp_iar_state WHERE state < ?"); + $statement->execute([2]); + + while ($row = $statement->fetch()) { + if ($row['state'] == 0) { // 0 - Request to OS pending + $name = explode(' ', $opensim->getUserName($row['userID'])); + + $restCfg = $this->app->config('restconsole'); + $restConsole = new RestConsole($restCfg['host'], intval($restCfg['port'])); + if ($restConsole->startSession($restCfg['user'], $restCfg['password']) && $restConsole->sendCommand('save iar '.$name[0].' '.$name[1].' /* password '.$restCfg['os-iar-path'].$row['iarfilename'])) { + $statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ? WHERE userID = ?'); + $statementUpdate->execute([1, $row['userID']]); + } + } elseif ($row['state'] == 1) { // 1 - IAR Creation in progress + $fullFilePath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars'.DIRECTORY_SEPARATOR.$row['iarfilename']; + if (file_exists($fullFilePath)) { + $filesize = filesize($fullFilePath); + + if ($filesize != $row['filesize']) { + $statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET filesize = ? WHERE userID = ?'); + $statementUpdate->execute([$filesize, $row['userID']]); + } else { + $statementUpdate = $this->app->db()->prepare('UPDATE mcp_iar_state SET state = ?, created = ? WHERE userID = ?'); + $statementUpdate->execute([2, time(), $row['userID']]); + + Util::sendInworldIM("00000000-0000-0000-0000-000000000000", $row['userID'], "Inventory", $this->app->config('grid')['homeurl'], "Deine IAR ist fertig zum Download: https://".$this->app->config('domain').'/iars/'.$row['iarfilename']); + } + } + } + } + + // 2 - IAR creation finished; delete if expired + $weekOld = time() - 604800; + $statementExpired = $this->app->db()->prepare('SELECT userID,iarfilename FROM mcp_iar_state WHERE state = ? AND created < ?'); + $statementExpired->execute([2, $weekOld]); + $statementDeleteExpired = $this->app->db()->prepare('DELETE FROM mcp_iar_state WHERE state = ? AND userID = ?'); + while ($row = $statementExpired->fetch()) { + $fullFilePath = $this->app->getDataDir().DIRECTORY_SEPARATOR.'iars'.DIRECTORY_SEPARATOR.$row['iarfilename']; + if (file_exists($fullFilePath) && unlink($fullFilePath)) { + $statementDeleteExpired->execute([2, $row['userID']]); + } + } + + return true; + } +} diff --git a/config.example.ini b/config.example.ini index 12fdf28..e1778f1 100644 --- a/config.example.ini +++ b/config.example.ini @@ -35,4 +35,14 @@ password = secret [grid] name = OpenSim main-news = Yet another OpenSim Grid. -homeurl = http://...:8002 \ No newline at end of file +homeurl = http://...:8002 + +; Benötigt für die Anforderung von IARs +[iarfetcher] +; Zugangsdaten der REST-Konsole von OpenSimulator +host = example.com +port = 9001 +user = mcp +password = secret +; IAR-Verzeichnis aus Sicht der ROBUST-Instanz +os-iar-path = /opt/opensim/iars/ \ No newline at end of file diff --git a/cron/IARMonitor.php b/cron/IARMonitor.php deleted file mode 100644 index 63935fe..0000000 --- a/cron/IARMonitor.php +++ /dev/null @@ -1,58 +0,0 @@ -prepare("CREATE TABLE IF NOT EXISTS `iarstates` (`userID` VARCHAR(36) NOT NULL COLLATE 'utf8_unicode_ci', `filesize` BIGINT(20) NOT NULL DEFAULT '0', `iarfilename` VARCHAR(64) NOT NULL COLLATE 'utf8_unicode_ci', `running` INT(1) NOT NULL DEFAULT '0', PRIMARY KEY (`userID`) USING BTREE) COLLATE='utf8_unicode_ci' ENGINE=InnoDB;"); - $statement->execute(); - - $statement = $RUNTIME['PDO']->prepare("SELECT userID,iarfilename,filesize FROM iarstates WHERE running = 1 LIMIT 1"); - $statement->execute(); - - if ($row = $statement->fetch()) { - $email = $opensim->getUserMail($row['userID']); - - $fullFilePath = "/var/www/html/data/".$row['iarfilename']; - - echo "Aktive IAR für ".$opensim->getUserName($row['userID'])." gefunden. File: ".$fullFilePath."\n"; - - if (file_exists($fullFilePath)) { - $filesize = filesize($fullFilePath); - - if ($filesize != $row['filesize']) { - $statementUpdate = $RUNTIME['PDO']->prepare('UPDATE iarstates SET filesize = :filesize WHERE userID = :userID'); - $statementUpdate->execute(['filesize' => $filesize, 'userID' => $row['userID']]); - - echo "Status der IAR für ".$opensim->getUserName($row['userID']).": Speichert...\n"; - } else { - $APIURL = $RUNTIME['SIDOMAN']['URL']."api.php?CONTAINER=".$RUNTIME['SIDOMAN']['CONTAINER']."&KEY=".$RUNTIME['SIDOMAN']['PASSWORD']."&METODE=RESTART"; - $APIResult = file_get_contents($APIURL); - echo "Status der IAR für ".$opensim->getUserName($row['userID']).": Sende Mail...\n"; - $statementUpdate = $RUNTIME['PDO']->prepare('DELETE FROM iarstates WHERE userID = :userID'); - $statementUpdate->execute(['userID' => $row['userID']]); - - sendInworldIM("00000000-0000-0000-0000-000000000000", $row['userID'], "Inventory", $RUNTIME['GRID']['HOMEURL'], "Deine IAR ist fertig zum Download: ".$RUNTIME['IAR']['BASEURL'].$row['iarfilename']); - } - } else { - $name = explode(" ", $opensim->getUserName($row['userID'])); - - $APIURL = $RUNTIME['SIDOMAN']['URL']."api.php?CONTAINER=".$RUNTIME['SIDOMAN']['CONTAINER']."&KEY=".$RUNTIME['SIDOMAN']['PASSWORD']."&METODE=COMMAND&COMMAND=".urlencode("save iar ".$name[0]." ".$name[1]." /* PASSWORD /downloads/".$row['iarfilename']); - $APIResult = file_get_contents($APIURL); - - echo "IAR für ".$name[0]." ".$name[1]." wurde gestartet: Status: ".$APIResult."\n"; - } - } else { - $statement = $RUNTIME['PDO']->prepare("SELECT userID,iarfilename FROM iarstates WHERE running = 0 LIMIT 1"); - $statement->execute(); - - while ($row = $statement->fetch()) { - $statementUpdate = $RUNTIME['PDO']->prepare('UPDATE iarstates SET running = :running WHERE userID = :userID'); - $statementUpdate->execute(['running' => 1, 'userID' => $row['userID']]); - - $name = explode(" ", $opensim->getUserName($row['userID'])); - - $APIURL = $RUNTIME['SIDOMAN']['URL']."api.php?CONTAINER=".$RUNTIME['SIDOMAN']['CONTAINER']."&KEY=".$RUNTIME['SIDOMAN']['PASSWORD']."&METODE=COMMAND&COMMAND=".urlencode("save iar ".$name[0]." ".$name[1]." /* PASSWORD /downloads/".$row['iarfilename']); - $APIResult = file_get_contents($APIURL); - - echo "IAR für ".$name[0]." ".$name[1]." wurde gestartet: Status: ".$APIResult."\n"; - } - }