Files

139 lines
5.4 KiB
PHP
Executable File

<?php
/**
* tononixPC System Update Server
* Version 1.0.1 (Slim Rewrite)
*/
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use PharIO\GnuPG as GnuPG;
use Noodlehaus\Config;
use Noodlehaus\Parser\Properties;
use Slim\Factory\ServerRequestCreatorFactory;
define("ROOT_DIR", __DIR__);
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/libUpdateService.php';
require __DIR__ . '/libSignedComms.php';
$AppConfiguration = new Config("config.prop", new Properties);
/** Check for GnuPG support
*
* First we check for the native PHP gnupg library, then we check if we're running under PeachPie and have Gpgme-sharp.
*
**/
if (function_exists("gnupg_verify") OR (class_exists('\Libgpgme\Gpgme') && defined("PEACHPIE_VERSION"))) {
// Lets's fine tune this detection a little more
// If we're in peachpie and the gpgme-sharp namespace is available, we obviously have it.
if(defined("PEACHPIE_VERSION") && class_exists('\Libgpgme\Gpgme')) {
$gnupg_support['state'] = true;
$gnupg_support['provider'] = "gpgme-sharp";
} elseif(function_exists("gnupg_verify")) {
// Likewise, if the gnupg_verify function exists, we have access to that as well.
$gnupg_support['state'] = true;
$gnupg_support['provider'] = "ext-gnupg";
}
//Check to see if we're running under PeachPie, that would explain not having it.
} elseif(defined("PEACHPIE_VERSION") && !class_exists('Libgpgme\Gpgme')) {
$gnupg_support['state'] = false;
$gnupg_support['reason'] = "was not compiled with GnuPG support.";
} elseif(!defined("PEACHPIE_VERSION") && function_exists("gnupg_verify")) {
$gnupg_support['state'] = false;
$gnupg_support['reason'] = "GnuPG extension is either not activated or not installed.";
} else {
$gnupg_support['state'] = false;
$gnupg_support['reason'] = "unable to determine reason.";
}
$AppConfiguration = $AppConfiguration->all();
if($AppConfiguration['update-server.debug'] == true)
{
define("DEBUG", 1);
}
$GPGKeyFile = $AppConfiguration['security.gpg-key-file'];
$GPGFingerprint = $AppConfiguration['security.gpg-fingerprint'];
$GPGHome = $AppConfiguration['security.gpg-key-tmp'];
$GPGEnabled = $AppConfiguration['security.gpg.enabled'];
$GPGPassphrase = $AppConfiguration['security.gpg-passphrase'];
$gnupg_support['enabled'] = $GPGEnabled;
if($gnupg_support['state'] == true && $GPGEnabled == true)
{
/** Unlike the reference version, we're just going to go ahead and pre-emptively set up the keyring. **/
if(!defined("DEBUG"))
{
$realPassphrase = file_get_contents($GPGPassphrase);
} else {
$realPassphrase = $GPGPassphrase;
}
//TODO: Fix $secomm = new SignedCommunicationProvider($GPGKeyFile, $GPGFingerprint, $realPassphrase, $GPGHome);
}
$recoveryParser = new RecoveryUpdateService();
$app = AppFactory::create();
$twig = Twig::create(__DIR__.'/templates');
$app->add(TwigMiddleware::create($app, $twig));
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
include("routes/recovery-routes.php");
include("routes/osinfo-routes.php");
$app->any("/", function ($request, $response, array $args) {
global $AppConfiguration;
global $recoveryParser;
global $app;
global $gnupg_support;
$response = $response->withHeader("Content-Type","text/html")->withStatus(200);
if(defined("PEACHPIE_VERSION")) {
// If we're running a compiled copy, we should get the version number of the resulting assembly.
$Assembly = \System\Reflection\Assembly::GetExecutingAssembly();
$version = $Assembly->GetName()->Version->ToString();
} else {
$version = $AppConfiguration['update-server.version'];
}
$GPGKeyFile = $AppConfiguration['security.gpg-key-file'];
$GPGFingerprint = $AppConfiguration['security.gpg-fingerprint'];
$GPGHome = $AppConfiguration['security.gpg-key-tmp'];
$OSInfo = new OSUpdateService();
if(defined("PEACHPIE_VERSION"))
{
$runtime = "PeachPie/.NET ".PEACHPIE_VERSION;
} else {
$runtime = "PHP/Native ".phpversion();
}
$view = Twig::fromRequest($request);
$OSVersions = $OSInfo->GetAvailableOSList();
if($gnupg_support['state'] == true) {
$gnupg_support_string = "True (Engine: ".$gnupg_support['provider'].", Secure responses enabled: ".$gnupg_support['enabled'].")";
} else {
$gnupg_support_string = "False, ".$gnupg_support['reason'];
}
$bootloaderVersion = $recoveryParser->getBootloaderUpdateVersion();
$template = $view->render($response, 'serverstatus.tpl', [
'version' => $version,
'runtime' => $runtime,
'gnupg_support' => $gnupg_support_string,
'gnupg_fingerprint' => $GPGFingerprint,
'gnupg_home' => $GPGHome,
'gnupg_certificate' => $GPGKeyFile,
'tononixOSBootloaderVersion' => $bootloaderVersion,
'OSList' => $OSVersions
]);
$test = $template;
return $template;
});
$app->run();