diff --git a/emhttp/plugins/dynamix.my.servers/Registration.page b/emhttp/plugins/dynamix.my.servers/Registration.page index 8aa41be25..812ed720b 100644 --- a/emhttp/plugins/dynamix.my.servers/Registration.page +++ b/emhttp/plugins/dynamix.my.servers/Registration.page @@ -14,7 +14,10 @@ Tag="pencil" * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */ +require_once "$docroot/plugins/dynamix/include/ReplaceKey.php"; +$replaceKey = new ReplaceKey(); +$replaceKey->check(true); ?> - + \ No newline at end of file diff --git a/emhttp/plugins/dynamix.plugin.manager/Downgrade.page b/emhttp/plugins/dynamix.plugin.manager/Downgrade.page index 0019b5167..037740ac6 100644 --- a/emhttp/plugins/dynamix.plugin.manager/Downgrade.page +++ b/emhttp/plugins/dynamix.plugin.manager/Downgrade.page @@ -16,11 +16,12 @@ Tag="upload" /** * @note icon-update is rotated via CSS in myservers1.php */ +require_once "$docroot/plugins/dynamix/include/ReplaceKey.php"; +$replaceKey = new ReplaceKey(); +$replaceKey->check(); require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php"; -// Create an instance of the RebootDetails class $rebootDetails = new RebootDetails(); -// Get the current reboot details if there are any $rebootDetails->setPrevious(); $serverNameEscaped = htmlspecialchars(str_replace(' ', '_', strtolower($var['NAME']))); diff --git a/emhttp/plugins/dynamix.plugin.manager/Update.page b/emhttp/plugins/dynamix.plugin.manager/Update.page index 82d0d337c..c3a5e52e6 100644 --- a/emhttp/plugins/dynamix.plugin.manager/Update.page +++ b/emhttp/plugins/dynamix.plugin.manager/Update.page @@ -13,6 +13,10 @@ Tag="upload" * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. */ +require_once "$docroot/plugins/dynamix/include/ReplaceKey.php"; +$replaceKey = new ReplaceKey(); +$replaceKey->check(); + require_once "$docroot/plugins/dynamix.my.servers/include/reboot-details.php"; $rebootDetails = new RebootDetails(); ?> diff --git a/emhttp/plugins/dynamix.plugin.manager/include/UnraidCheckExec.php b/emhttp/plugins/dynamix.plugin.manager/include/UnraidCheckExec.php new file mode 100644 index 000000000..15523e19a --- /dev/null +++ b/emhttp/plugins/dynamix.plugin.manager/include/UnraidCheckExec.php @@ -0,0 +1,42 @@ + 'true', + ]; + // allows the web components to determine the OS_RELEASES url + if (isset($_GET['altUrl']) && filter_var($_GET['altUrl'], FILTER_VALIDATE_URL)) { + $params['altUrl'] = $_GET['altUrl']; + } + // pass the params to the unraidcheck script for usage in UnraidCheck.php + putenv('QUERY_STRING=' . http_build_query($params)); + } + + public function execute(): string + { + $this->setupEnvironment(); + $output = []; + exec(self::SCRIPT_PATH, $output); + return implode("\n", $output); + } +} + +// Usage +$checker = new UnraidCheckExec(); +echo $checker->execute(); \ No newline at end of file diff --git a/emhttp/plugins/dynamix.plugin.manager/scripts/unraidcheck b/emhttp/plugins/dynamix.plugin.manager/scripts/unraidcheck index bb87f6343..71766db8f 100755 --- a/emhttp/plugins/dynamix.plugin.manager/scripts/unraidcheck +++ b/emhttp/plugins/dynamix.plugin.manager/scripts/unraidcheck @@ -13,7 +13,13 @@ ?> check(); +// utilized by UnraidCheckExec.php to have UnraidCheck.php return a json response when this script is called directly +parse_str(getenv('QUERY_STRING') ?? '', $_GET); + +require_once "$docroot/plugins/dynamix.plugin.manager/include/UnraidCheck.php"; $unraidOsCheck = new UnraidOsCheck(); -$unraidOsCheck->checkForUpdate(); +$unraidOsCheck->checkForUpdate(); \ No newline at end of file diff --git a/emhttp/plugins/dynamix/include/InstallKey.php b/emhttp/plugins/dynamix/include/InstallKey.php index 62b80a912..467a90322 100644 --- a/emhttp/plugins/dynamix/include/InstallKey.php +++ b/emhttp/plugins/dynamix/include/InstallKey.php @@ -32,20 +32,20 @@ class KeyInstaller * @param int $httpcode https://developer.mozilla.org/en-US/docs/Web/HTTP/Status * @param string|array $result - strings are assumed to be encoded JSON. Arrays will be encoded to JSON. */ - private function responseComplete($httpcode, $result) + private function responseComplete($httpcode, $result): string { $mutatedResult = is_array($result) ? json_encode($result) : $result; if ($this->isGetRequest && $this->getHasUrlParam) { // return JSON to the caller - header('Content-Type: application/json'); - http_response_code($httpcode); - exit((string)$mutatedResult); + header('Content-Type: application/json'); + http_response_code($httpcode); + exit((string)$mutatedResult); } else { // return the result to the caller - return $mutatedResult; + return $mutatedResult; } } - public function installKey($keyUrl = null) + public function installKey($keyUrl = null): string { $url = unscript($keyUrl ?? _var($_GET, 'url')); $host = parse_url($url)['host'] ?? ''; @@ -61,9 +61,12 @@ class KeyInstaller if ($returnVar === 0) { $var = (array)@parse_ini_file('/var/local/emhttp/var.ini'); if (_var($var, 'mdState') == "STARTED") { - return $this->responseComplete(200, ['status' => _('Please Stop array to complete key installation')], _('success') . ', ' . _('Please Stop array to complete key installation')); + return $this->responseComplete(200, [ + 'status' => 'success', + 'message' => _('Please Stop array to complete key installation'), + ]); } else { - return $this->responseComplete(200, ['status' => ''], _('success')); + return $this->responseComplete(200, ['status' => 'success']); } } else { @unlink(escapeshellarg("/boot/config/$keyFile")); @@ -81,4 +84,4 @@ $getHasUrlParam = $_GET !== null && !empty($_GET) && isset($_GET['url']); if ($isGetRequest && $getHasUrlParam) { $keyInstaller = new KeyInstaller(); $keyInstaller->installKey(); -} +} \ No newline at end of file diff --git a/emhttp/plugins/dynamix/include/ReplaceKey.php b/emhttp/plugins/dynamix/include/ReplaceKey.php index b260d4ed9..502064efb 100644 --- a/emhttp/plugins/dynamix/include/ReplaceKey.php +++ b/emhttp/plugins/dynamix/include/ReplaceKey.php @@ -1,5 +1,4 @@ docroot/webGui/include/InstallKey.php"; $KeyInstaller = new KeyInstaller(); - $KeyInstaller->installKey($key); + $installResponse = $KeyInstaller->installKey($key); + + $installSuccess = false; + + if (!empty($installResponse)) { + $decodedResponse = json_decode($installResponse, true); + if (isset($decodedResponse['error'])) { + $this->writeJsonFile( + '/tmp/ReplaceKey/error.json', + [ + 'error' => $decodedResponse['error'], + 'ts' => time(), + ] + ); + $installSuccess = false; + } else { + $installSuccess = true; + } + } + + $keyType = basename($key, '.key'); + $output = isset($GLOBALS['notify']) ? _var($GLOBALS['notify'],'plugin') : ''; + $script = '/usr/local/emhttp/webGui/scripts/notify'; + + if ($installSuccess) { + $event = "Installed New $keyType License"; + $subject = "Your new $keyType license key has been automatically installed"; + $description = ""; + $importance = "normal $output"; + } else { + $event = "Failed to Install New $keyType License"; + $subject = "Failed to automatically install your new $keyType license key"; + $description = isset($decodedResponse['error']) ? $decodedResponse['error'] : "Unknown error occurred"; + $importance = "alert $output"; + } + + exec("$script -e ".escapeshellarg($event)." -s ".escapeshellarg($subject)." -d ".escapeshellarg($description)." -i ".escapeshellarg($importance)." -l '/Tools/Registration' -x"); + + return $installSuccess; } private function writeJsonFile($file, $data) { - if (!is_dir(dirname($file))) { // prevents errors when directory doesn't exist + if (!is_dir(dirname($file))) { mkdir(dirname($file)); } + file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); } - public function check() + public function check(bool $forceCheck = false) { // we don't need to check if (empty($this->guid) || empty($this->keyfile) || empty($this->regExp)) { return; } - // set $isAlreadyExpired to true if regExp is in the past - $isAlreadyExpired = $this->regExp <= time(); - // if regExp is seven days or less from now, we need to check - $isWithinSevenDays = $this->regExp <= strtotime('+7 days'); + // Check if we're within the 7-day window before and after regExp + $now = time(); + $sevenDaysBefore = strtotime('-7 days', $this->regExp); + $sevenDaysAfter = strtotime('+7 days', $this->regExp); - $shouldCheck = $isAlreadyExpired || $isWithinSevenDays; - if (!$shouldCheck) { + $isWithinWindow = ($now >= $sevenDaysBefore && $now <= $sevenDaysAfter); + + if (!$forceCheck && !$isWithinWindow) { return; } @@ -187,10 +226,12 @@ class ReplaceKey '/tmp/ReplaceKey/error.json', [ 'error' => 'Failed to retrieve latest key after getting a `hasNewerKeyfile` in the validation response.', + 'ts' => time(), ] ); return; } + $this->installNewKey($latestKey); } -} +} \ No newline at end of file