refactor: unused InstallKey include for extensible usage

This commit is contained in:
Zack Spear
2024-02-02 15:05:26 -08:00
committed by Zack Spear
parent f42cb70e4f
commit a3137c4086
+57 -45
View File
@@ -1,4 +1,4 @@
<?PHP
<?php
/* Copyright 2005-2023, Lime Technology
*
* This program is free software; you can redistribute it and/or
@@ -8,56 +8,68 @@
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
// add translations
$_SERVER['REQUEST_URI'] = 'settings';
$docroot = ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/Translations.php";
/**
* @name response_complete
* @param {HTTP Response Status Code} $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.
* @param {String} $cli_success_msg
*/
class KeyInstaller
{
private $isGetRequest;
private $getHasUrlParam;
function response_complete($httpcode, $result, $cli_success_msg='') {
global $cli;
$mutatedResult = is_array($result) ? json_encode($result) : $result;
if ($cli) {
$json = @json_decode($mutatedResult,true);
if (!empty($json['error'])) {
echo 'Error: '.$json['error'].PHP_EOL;
exit(1);
public function __construct()
{
$this->isGetRequest = !empty($_SERVER) && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET';
$this->getHasUrlParam = $_GET !== null && !empty($_GET) && isset($_GET['url']);
if ($this->isGetRequest && $this->getHasUrlParam) {
$this->installKey();
}
}
exit($cli_success_msg.PHP_EOL);
}
header('Content-Type: application/json');
http_response_code($httpcode);
exit((string)$mutatedResult);
}
$cli = php_sapi_name()=='cli';
$url = unscript(_var($_GET,'url'));
$host = parse_url($url)['host']??'';
/**
* @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)
{
$mutatedResult = is_array($result) ? json_encode($result) : $result;
if ($host && in_array($host,['keys.lime-technology.com','lime-technology.com'])) {
$key_file = basename($url);
exec("/usr/bin/wget -q -O ".escapeshellarg("/boot/config/$key_file")." ".escapeshellarg($url), $output, $return_var);
if ($return_var === 0) {
$var = (array)@parse_ini_file('/var/local/emhttp/var.ini');
if (_var($var,'mdState')=="STARTED") {
response_complete(200, array('status' => _('Please Stop array to complete key installation')), _('success').', '._('Please Stop array to complete key installation'));
} else {
response_complete(200, array('status' => ''), _('success'));
if ($this->isGetRequest && $this->getHasUrlParam) { // return JSON to the caller
header('Content-Type: application/json');
http_response_code($httpcode);
exit((string)$mutatedResult);
} else { // return the result to the caller
return $mutatedResult;
}
}
public function installKey($keyUrl = null)
{
$url = unscript($keyUrl ?? _var($_GET, 'url'));
$host = parse_url($url)['host'] ?? '';
if (!function_exists('_')) {
function _($text) {return $text;}
}
if ($host && in_array($host, ['keys.lime-technology.com', 'lime-technology.com'])) {
$keyFile = basename($url);
exec("/usr/bin/wget -q -O " . escapeshellarg("/boot/config/$keyFile") . " " . escapeshellarg($url), $output, $returnVar);
if ($returnVar === 0) {
$var = (array)@parse_ini_file('/var/local/emhttp/var.ini');
if (_var($var, 'mdState') == "STARTED") {
$this->responseComplete(200, ['status' => _('Please Stop array to complete key installation')], _('success') . ', ' . _('Please Stop array to complete key installation'));
} else {
$this->responseComplete(200, ['status' => ''], _('success'));
}
} else {
$this->responseComplete(406, ['error' => _('download error') . " $returnVar"]);
}
} else {
$this->responseComplete(406, ['error' => _('bad or missing key file') . ": $url"]);
}
}
} else {
response_complete(406, array('error' => _('download error') . " $return_var"));
}
} else {
response_complete(406, array('error' => _('bad or missing key file') . ": $url"));
}
?>