From bb40c8c9a45602fb5d7a801efc949cd1b29021a1 Mon Sep 17 00:00:00 2001 From: Anonymous Contributor Date: Sun, 10 Sep 2023 04:53:13 +0200 Subject: [PATCH] Port working cronjobs to new CronJob API --- app/cron/AssetChecker.php | 51 ++++++++++++++++++ app/cron/CheckInventory.php | 26 +++++++++ app/cron/OfflineIm.php | 89 +++++++++++++++++++++++++++++++ app/cron/RegionChecker.php | 55 +++++++++++++++++++ cron/assetChecker.php | 36 ------------- cron/checkInventar.php | 19 ------- cron/offlineIM.php | 102 ------------------------------------ cron/regionChecker.php | 50 ------------------ 8 files changed, 221 insertions(+), 207 deletions(-) create mode 100644 app/cron/AssetChecker.php create mode 100644 app/cron/CheckInventory.php create mode 100644 app/cron/OfflineIm.php create mode 100644 app/cron/RegionChecker.php delete mode 100644 cron/assetChecker.php delete mode 100644 cron/checkInventar.php delete mode 100644 cron/offlineIM.php delete mode 100644 cron/regionChecker.php diff --git a/app/cron/AssetChecker.php b/app/cron/AssetChecker.php new file mode 100644 index 0000000..6214cc1 --- /dev/null +++ b/app/cron/AssetChecker.php @@ -0,0 +1,51 @@ +app->db()->prepare("SELECT id,hash FROM fsassets ORDER BY create_time DESC"); + $statement->execute(); + + $count = 0; + + while ($row = $statement->fetch()) { + $fileNameParts = array(); + $fileNameParts[0] = substr($row['hash'], 0, 2); + $fileNameParts[1] = substr($row['hash'], 2, 2); + $fileNameParts[2] = substr($row['hash'], 4, 2); + $fileNameParts[3] = substr($row['hash'], 6, 4); + $fileNameParts[4] = $row['hash'] . ".gz"; + + $fileNameParts['UUID'] = $row['id']; + $fileNameParts['FilePath'] = "/data/assets/base/" . $fileNameParts[0] . "/" . $fileNameParts[1] . "/" . $fileNameParts[2] . "/" . $fileNameParts[3] . "/" . $fileNameParts[4]; + + if (file_exists($fileNameParts['FilePath'])) { + $filesize = filesize($fileNameParts['FilePath']); + if ($filesize === false) { + continue; + } + } else { + $filesize = 0; + } + + $fileNameParts['FileSize'] = $filesize; + $fileNameParts['Count'] = $count++; + + if ($fileNameParts['FileSize'] == 0) { + $add = $this->app->db()->prepare('DELETE FROM fsassets WHERE hash = :fileHash'); + $add->execute(['fileHash' => $row['hash']]); + } + } + return true; + } +} diff --git a/app/cron/CheckInventory.php b/app/cron/CheckInventory.php new file mode 100644 index 0000000..2f67fda --- /dev/null +++ b/app/cron/CheckInventory.php @@ -0,0 +1,26 @@ +app->db()->prepare("UPDATE inventoryitems i SET i.inventoryName = concat('[DEFEKT] ', i.inventoryName) + WHERE i.assetID IN ( + SELECT i.assetID FROM inventoryitems i WHERE + NOT EXISTS( + SELECT * FROM fsassets fs WHERE fs.id = i.assetID) + AND NOT i.inventoryName LIKE '[DEFEKT] %' AND i.assetType <> 24 + )"); + + $invCheckStatement->execute(); + return true; + } +} diff --git a/app/cron/OfflineIm.php b/app/cron/OfflineIm.php new file mode 100644 index 0000000..cbbebd7 --- /dev/null +++ b/app/cron/OfflineIm.php @@ -0,0 +1,89 @@ + "eine Nachricht", + "3" => "eine Gruppeneinladung", + "4" => "ein Inventaritem", + "5" => "eine Bestätigung zur Annahme von Inventar", + "6" => "eine Information zur Ablehnung von Inventar", + "7" => "eine Aufforderung zur Gruppenwahl", + "9" => "ein Inventaritem von einem Script", + "19" => "eine Nachricht von einem Script", + "32" => "eine Gruppennachricht", + "38" => "eine Freundschaftsanfrage", + "39" => "eine Bestätigung über die Annahme der Freundschaft", + "40" => "eine Information über das Ablehnen der Freundschaft" + ); + + public function __construct(\Mcp\Mcp $app) + { + parent::__construct($app, Frequency::EACH_MINUTE); + } + + public function run(): bool + { + $statement = $this->app->db()->prepare("SELECT ID,PrincipalID,Message FROM im_offline"); + $statement->execute(); + + while ($row = $statement->fetch()) { + $opensim = new OpenSim($this->app->db()); + + $email = $opensim->getUserMail($row['PrincipalID']); + $allowOfflineIM = $opensim->allowOfflineIM($row['PrincipalID']); + + if ($email != "" && $allowOfflineIM == "TRUE" && !$this->isMailAlreadySent($row['ID'])) { + $statementSend = $this->app->db()->prepare('INSERT INTO mcp_offlineim_send (id) VALUES (:idnummer)'); + $statementSend->execute(['idnummer' => $row['ID']]); + + $mailcfg = $this->app->config('smtp'); + $smtpClient = new SmtpClient($mailcfg['host'], intval($mailcfg['port']), $mailcfg['address'], $mailcfg['password']); + + $xmlMessage = new SimpleXMLElement($row['Message']); + + $imType = $this::IM_TYPE["" . $xmlMessage->dialog . ""]; + $htmlMessage = "Du hast " . $imType . " in " . $this->app->config('grid')['name'] . " bekommen.

Gesendet von: "; + + if (isset($xmlMessage->fromAgentName)) { + $htmlMessage .= $xmlMessage->fromAgentName; + } + + if (isset($xmlMessage->RegionID) && isset($xmlMessage->Position)) { + if ($xmlMessage->Position->X != 0 || $xmlMessage->Position->Y != 0 || $xmlMessage->Position->Z != 0) { //TODO + $htmlMessage .= " @ " . $opensim->getRegionName($xmlMessage->RegionID) . "/" . $xmlMessage->Position->X . "/" . $xmlMessage->Position->Y . "/" . $xmlMessage->Position->Z; + } else { + $htmlMessage .= " @ " . $opensim->getRegionName($xmlMessage->RegionID); + } + } + + $tpl = $this->app->template('mail.php')->vars([ + 'title' => substr($imType, strpos($imType, ' ')) + ])->unsafeVar('message', $htmlMessage); + $smtpClient->sendHtml($mailcfg['address'], $mailcfg['name'], $email, "Du hast " . $imType . " in " . $this->app->config('grid')['name'] . ".", $tpl); + } + } + return true; + } + + private function isMailAlreadySent($id): bool + { + $statement = $this->app->db()->prepare("SELECT 1 FROM mcp_offlineim_send WHERE id = ? LIMIT 1"); + $statement->execute(array($id)); + + if ($statement->rowCount() != 0) { + return true; + } + + return false; + } + +} diff --git a/app/cron/RegionChecker.php b/app/cron/RegionChecker.php new file mode 100644 index 0000000..b3fefb5 --- /dev/null +++ b/app/cron/RegionChecker.php @@ -0,0 +1,55 @@ +app->db()->prepare("SELECT uuid,regionName,owner_uuid,serverURI,OfflineTimer FROM regions JOIN mcp_regions_info ON regions.uuid = mcp_regions_info.regionID COLLATE utf8mb3_unicode_ci"); + $statement->execute(); + + while ($row = $statement->fetch()) { + $curl = curl_init($row['serverURI'] . 'jsonSimStats'); + curl_setopt($curl, CURLOPT_TIMEOUT, 15); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($curl); + + if ($result === false || strlen($result) == 0) { + $opensim = new OpenSim($this->app->db()); + + $longOffline = ($row['OfflineTimer'] + 3600) <= time(); + echo "Die Region " . $row['regionName'] . " von " . $opensim->getUserName($row['owner_uuid']) . " ist " . ($longOffline ? "seit über einer Stunde" : "") . " nicht erreichbar.\n"; //TODO: Increase to 1-3 months + + if ($longOffline) { + if ($this->app->config('grid')['delete-inactive-regions']) { + Util::sendInworldIM("00000000-0000-0000-0000-000000000000", $row['owner_uuid'], "Region", $this->app->config('grid')['homeurl'], "WARNUNG: Deine Region '" . $row['regionName'] . "' ist nicht erreichbar und wurde deshalb aus dem Grid entfernt."); + + $statementUpdate = $this->app->db()->prepare('DELETE FROM regions WHERE uuid = :uuid'); + $statementUpdate->execute(['uuid' => $row['uuid']]); + } + $statementRemoveStats = $this->app->db()->prepare('DELETE FROM mcp_regions_info WHERE regionID = :uuid'); + $statementRemoveStats->execute(['uuid' => $row['uuid']]); + } elseif ($this->app->config('grid')['warn-inactive-regions']) { + Util::sendInworldIM("00000000-0000-0000-0000-000000000000", $row['owner_uuid'], "Region", $this->app->config('grid')['homeurl'], "WARNUNG: Deine Region '" . $row['regionName'] . "' ist nicht erreichbar!"); + } + } else { + $regionData = json_decode($result); + + $statementAccounts = $this->app->db()->prepare('REPLACE INTO `mcp_regions_info` (`regionID`, `RegionVersion`, `ProcMem`, `Prims`, `SimFPS`, `PhyFPS`, `OfflineTimer`) VALUES (:regionID, :RegionVersion, :ProcMem, :Prims, :SimFPS, :PhyFPS, :OfflineTimer)'); + $statementAccounts->execute(['regionID' => $row['uuid'], 'RegionVersion' => $regionData->Version, 'ProcMem' => $regionData->ProcMem, 'Prims' => $regionData->Prims, 'SimFPS' => $regionData->SimFPS, 'PhyFPS' => $regionData->PhyFPS, 'OfflineTimer' => time()]); + } + } + + return true; + } +} diff --git a/cron/assetChecker.php b/cron/assetChecker.php deleted file mode 100644 index 5513211..0000000 --- a/cron/assetChecker.php +++ /dev/null @@ -1,36 +0,0 @@ -prepare("SELECT id,hash FROM fsassets ORDER BY create_time DESC"); -$statement->execute(); - -$count = 0; - -while ($row = $statement->fetch()) { - $fileNameParts = array(); - $fileNameParts[0] = substr($row['hash'], 0, 2); - $fileNameParts[1] = substr($row['hash'], 2, 2); - $fileNameParts[2] = substr($row['hash'], 4, 2); - $fileNameParts[3] = substr($row['hash'], 6, 4); - $fileNameParts[4] = $row['hash'].".gz"; - - //$fileNameParts['Time'] = time(); - $fileNameParts['UUID'] = $row['id']; - $fileNameParts['FilePath'] = "/data/assets/base/".$fileNameParts[0]."/".$fileNameParts[1]."/".$fileNameParts[2]."/".$fileNameParts[3]."/".$fileNameParts[4]; - - if (file_exists($fileNameParts['FilePath'])) { - $filesize = filesize($fileNameParts['FilePath']); - if ($filesize === false) { - continue; - } - } - else { - $filesize = 0; - } - - $fileNameParts['FileSize'] = $filesize; - $fileNameParts['Count'] = $count++; - - if ($fileNameParts['FileSize'] == 0) { - $add = $RUNTIME['PDO']->prepare('DELETE FROM fsassets WHERE hash = :fileHash'); - $add->execute(['fileHash' => $row['hash']]); - } -} diff --git a/cron/checkInventar.php b/cron/checkInventar.php deleted file mode 100644 index 05c59d6..0000000 --- a/cron/checkInventar.php +++ /dev/null @@ -1,19 +0,0 @@ -prepare("UPDATE inventoryitems i SET -i.inventoryName = concat('[DEFEKT] ', i.inventoryName) -WHERE -i.assetID IN ( - SELECT - i.assetID - FROM inventoryitems i - WHERE - NOT EXISTS( SELECT * - FROM fsassets fs - WHERE - fs.id = i.assetID - ) - AND NOT i.inventoryName LIKE '[DEFEKT] %' - AND i.assetType <> 24 - )"); - -$InventarCheckStatement->execute(); diff --git a/cron/offlineIM.php b/cron/offlineIM.php deleted file mode 100644 index 01498df..0000000 --- a/cron/offlineIM.php +++ /dev/null @@ -1,102 +0,0 @@ -prepare("CREATE TABLE IF NOT EXISTS im_offline_send (`id` int(6) NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"); - $statement->execute(); - - function isMailAlreadySent($id) - { - global $RUNTIME; - - $statement = $RUNTIME['PDO']->prepare("SELECT 1 FROM im_offline_send WHERE id = ? LIMIT 1"); - $statement->execute(array($id)); - - if ($statement->rowCount() != 0) { - return true; - } - - return false; - } - - $IMTYP = array( - "0" => "eine Nachricht", - "3" => "eine Gruppeneinladung", - "4" => "ein Inventaritem", - "5" => "eine Bestätigung zur Annahme von Inventar", - "6" => "eine Information zur Ablehnung von Inventar", - "7" => "eine Aufforderung zur Gruppenwahl", - "9" => "ein Inventaritem von einem Script", - "19" => "eine Nachricht von einem Script", - "32" => "eine Gruppennachricht", - "38" => "eine Freundschaftsanfrage", - "39" => "eine Bestätigung über die Annahme der Freundschaft", - "40" => "eine Information über das Ablehnen der Freundschaft" - ); - - //$statement = $RUNTIME['PDO']->prepare("SELECT * FROM im_offline WHERE PrincipalID = '1148b04d-7a93-49e9-b3c9-ea0cdeec38f7'"); - $statement = $RUNTIME['PDO']->prepare("SELECT ID,PrincipalID,Message FROM im_offline"); - $statement->execute(); - - while ($row = $statement->fetch()) { - include_once 'app/OpenSim.php'; - $opensim = new OpenSim(); - - $email = $opensim->getUserMail($row['PrincipalID']); - $allowOfflineIM = $opensim->allowOfflineIM($row['PrincipalID']); - - if ($email != "" && $allowOfflineIM == "TRUE") { - if (!isMailAlreadySent($row['ID'])) { - $statementSend = $RUNTIME['PDO']->prepare('INSERT INTO im_offline_send (id) VALUES (:idnummer)'); - $statementSend->execute(['idnummer' => $row['ID']]); - - $mail = new PHPMailer(true); - - $mail->SMTPDebug = SMTP::DEBUG_SERVER; - $mail->isSMTP(); - $mail->Host = $RUNTIME['SMTP']['SERVER']; - $mail->Port = $RUNTIME['SMTP']['PORT']; - $mail->SMTPAuth = false; - - $mail->setFrom($RUNTIME['SMTP']['ADRESS'], $RUNTIME['GRID']['NAME']); - $mail->addAddress($email, $opensim->getUserName($row['PrincipalID'])); - - $XMLMESSAGE = new SimpleXMLElement($row['Message']); - - $HTMLMESSAGE = "Du hast ".$IMTYP["".$XMLMESSAGE->dialog.""]." in ".$RUNTIME['GRID']['NAME']." bekommen.

Gesendet von: "; - - if (isset($XMLMESSAGE->fromAgentName)) { - $HTMLMESSAGE .= $XMLMESSAGE->fromAgentName; - } - - if (isset($XMLMESSAGE->RegionID) && isset($XMLMESSAGE->Position)) { - if ($XMLMESSAGE->Position->X != 0 || $XMLMESSAGE->Position->X != 0 || $XMLMESSAGE->Position->X != 0) { //TODO - $HTMLMESSAGE .= " @ ".$opensim->getRegionName($XMLMESSAGE->RegionID)."/".$XMLMESSAGE->Position->X."/".$XMLMESSAGE->Position->Y."/".$XMLMESSAGE->Position->Z; - } else { - $HTMLMESSAGE .= " @ ".$opensim->getRegionName($XMLMESSAGE->RegionID); - } - } - - $HTML = new HTML(); - $HTML->importHTML("mail.html"); - $HTML->setSeitenInhalt($HTMLMESSAGE); - $HTML->build(); - - $mail->isHTML(true); - $mail->Subject = "Du hast ".$IMTYP["".$XMLMESSAGE->dialog.""]." in ".$RUNTIME['GRID']['NAME']."."; - $mail->Body = $HTML->ausgabe(); - $mail->AltBody = strip_tags($HTMLMESSAGE); - - //print_r($mail); - $mail->send(); - }else{ - //echo $row['ID']." wurde bereits gesendet."; - } - }else{ - //echo $row['PrincipalID']." möchte keine offline IM oder hat keine E-MAIL Adresse hinterlegt."; - } - } diff --git a/cron/regionChecker.php b/cron/regionChecker.php deleted file mode 100644 index df2b284..0000000 --- a/cron/regionChecker.php +++ /dev/null @@ -1,50 +0,0 @@ -prepare("CREATE TABLE IF NOT EXISTS `regions_info` (`regionID` VARCHAR(36) NOT NULL COLLATE 'utf8_unicode_ci', `RegionVersion` VARCHAR(128) NOT NULL DEFAULT '' COLLATE 'utf8_unicode_ci', `ProcMem` INT(11) NOT NULL, `Prims` INT(11) NOT NULL, `SimFPS` INT(11) NOT NULL, `PhyFPS` INT(11) NOT NULL, `OfflineTimer` INT(11) NOT NULL DEFAULT '0', PRIMARY KEY (`regionID`) USING BTREE) COLLATE='utf8_unicode_ci' ENGINE=InnoDB;"); - $createStatement->execute(); - - $statement = $RUNTIME['PDO']->prepare("SELECT uuid,regionName,owner_uuid,serverURI FROM regions"); - $statement->execute(); - - ini_set('default_socket_timeout', 3); - - $ctx = stream_context_create(array('http'=> - array( - 'timeout' => 3, - ) - )); - - while($row = $statement->fetch()) - { - $result = file_get_contents($row['serverURI']."jsonSimStats", false, $ctx); - - if($result == FALSE || $result == "") - { - include 'app/OpenSim.php'; - - echo "Die Region ".$row['regionName']." von ".$opensim->getUserName($row['owner_uuid'])." ist nicht erreichbar.\n"; - - $infoStatement = $RUNTIME['PDO']->prepare("SELECT OfflineTimer FROM regions_info WHERE regionID = :regionID"); - $infoStatement->execute(['regionID' => $row['uuid']]); - - if($infoRow = $infoStatement->fetch()) - { - if(($infoRow['OfflineTimer'] + 3600) <= time()) - { - echo "Die Region ".$row['regionName']." von ".$opensim->getUserName($row['owner_uuid'])." ist seit über eine Stunde nicht erreichbar!\n"; - - //sendInworldIM("00000000-0000-0000-0000-000000000000", $row['owner_uuid'], "Region", $RUNTIME['GRID']['HOMEURL'], "WARNUNG: Deine Region '".$row['regionName']."' ist nicht erreichbar und wurde deshalb aus dem Grid entfernt."); - - //$statementUpdate = $RUNTIME['PDO']->prepare('DELETE FROM regions WHERE uuid = :uuid'); - //$statementUpdate->execute(['uuid' => $row['uuid']]); - }else{ - //sendInworldIM("00000000-0000-0000-0000-000000000000", $row['owner_uuid'], "Region", $RUNTIME['GRID']['HOMEURL'], "WARNUNG: Deine Region '".$row['regionName']."' ist nicht erreichbar!"); - } - } - }else{ - $regionData = json_decode($result); - - $statementAccounts = $RUNTIME['PDO']->prepare('REPLACE INTO `regions_info` (`regionID`, `RegionVersion`, `ProcMem`, `Prims`, `SimFPS`, `PhyFPS`, `OfflineTimer`) VALUES (:regionID, :RegionVersion, :ProcMem, :Prims, :SimFPS, :PhyFPS, :OfflineTimer)'); - $statementAccounts->execute(['regionID' => $row['uuid'], 'RegionVersion' => $regionData->Version, 'ProcMem' => $regionData->ProcMem, 'Prims' => $regionData->Prims, 'SimFPS' => $regionData->SimFPS, 'PhyFPS' => $regionData->PhyFPS, 'OfflineTimer' => time()]); - } - } -?> \ No newline at end of file