Class methods to handle global config, requirements updated

This commit is contained in:
mattpass
2020-07-09 14:35:38 +01:00
parent 5683c5d000
commit 2e877b9ad8
2 changed files with 96 additions and 26 deletions

View File

@@ -4,6 +4,83 @@ namespace ICEcoder;
class Settings
{
public function __construct()
{
// Set version number and document root as core settings
$this->versionNo = "7.0";
$this->docRoot = $_SERVER['DOCUMENT_ROOT'];
}
public function getConfigGlobalFileDetails()
{
// Return details about the global config file
$fileName = 'config-global.php';
$fullPath = dirname(__FILE__) . "/../data/" . $fileName;
$exists = file_exists($fullPath);
$readable = is_readable($fullPath);
$writable = is_writable($fullPath);
return [
"fileName" => $fileName,
"fullPath" => $fullPath,
"exists" => $exists,
"readable" => $readable,
"writable" => $writable,
];
}
public function getConfigGlobalTemplate()
{
// Return the serialized global config template
$fileName = 'template-config-global.php';
$fullPath = dirname(__FILE__) . "/../lib/" . $fileName;
if (function_exists('opcache_invalidate')) {
opcache_invalidate($fullPath, true);
}
$settings = file_get_contents($fullPath);
return $settings;
}
public function getConfigGlobalSettings()
{
// Start an array with version number and document root
$settings = [];
$settings['versionNo'] = $this->versionNo;
$settings['docRoot'] = $this->docRoot;
// Get global config file details
$fullPath = $this->getConfigGlobalFileDetails()['fullPath'];
// Load serialized data from the global config and convert to an array
if (function_exists('opcache_invalidate')) {
opcache_invalidate($fullPath, true);
}
$settingsFromFile = file_get_contents($fullPath);
$settingsFromFile = str_replace("<?php\n/*\n\n", "", $settingsFromFile);
$settingsFromFile = str_replace("\n\n*/\n?>", "", $settingsFromFile);
$settingsFromFile = unserialize($settingsFromFile);
// Merge that with the array we started with and return
$settings = array_merge($settings, $settingsFromFile);
return $settings;
}
public function setConfigGlobalSettings($settings)
{
// Get the global config file details
$fullPath = $this->getConfigGlobalFileDetails()['fullPath'];
if ($fConfigSettings = fopen($fullPath, 'w')) {
// If the settings we've received aren't in serialized format yet, do that now
// As $settings could be a serialized string or array
if (is_array($settings)) {
unset($settings['versionNo']);
unset($settings['docRoot']);
$settings = "<?php\n/*\n\n" . serialize($settings) . "\n\n*/\n?" . ">";
}
// Now we have a serialized string, save it in the global config file
fwrite($fConfigSettings, $settings);
fclose($fConfigSettings);
return true;
} else {
return false;
}
}
public function updateConfigCreateDate(): void
{
@@ -74,20 +151,12 @@ class Settings
public function disableFurtherRegistration(): void
{
global $configSettings;
// Disable the enableRegistration config setting if the user had that option chosen
if (true === isset($_POST['disableFurtherRegistration'])) {
$updatedConfigSettingsFile = getData(dirname(__FILE__) . "/../data/" . $configSettings);
if ($fUConfigSettings = fopen(dirname(__FILE__) . "/../data/" . $configSettings, 'w')) {
$updatedConfigSettingsFile = str_replace('"enableRegistration" => true','"enableRegistration" => false', $updatedConfigSettingsFile);
fwrite($fUConfigSettings, $updatedConfigSettingsFile);
fclose($fUConfigSettings);
} else {
$reqsPassed = false;
$reqsFailures = ["phpUpdateConfig"];
include dirname(__FILE__)."/../lib/requirements.php";
}
// Update global config settings file
$ICEcoderSettingsFromFile = $this->getConfigGlobalSettings();
$ICEcoderSettingsFromFile['enableRegistration'] = false;
$this->setConfigGlobalSettings($ICEcoderSettingsFromFile);
}
}
}

View File

@@ -1,4 +1,8 @@
<?php
require_once dirname(__FILE__) . "/../classes/Settings.php";
$settingsClass = new \ICEcoder\Settings();
if (false === isset($reqsPassed)) {
// Start off assuming all is fine with requirements
$reqsPassed = true;
@@ -17,15 +21,6 @@ if (false === isset($_SESSION) || "" === session_id()) {
array_push($reqsFailures, "phpSession");
}
// Check we can read config settings
if (file_exists(dirname(__FILE__) . "/../data/config-settings.php")) {
include(dirname(__FILE__) . "/../data/config-settings.php");
if (false === isset($ICEcoderSettings['versionNo'])) {
$reqsPassed = false;
array_push($reqsFailures, "phpReadFile");
}
}
// Check we have allow_url_fopen enabled
if (false === ini_get('allow_url_fopen')) {
$reqsPassed = false;
@@ -38,7 +33,7 @@ if (false === $reqsPassed) {
?>
<html>
<head>
<title>ICEcoder <?php echo $ICEcoderSettings['versionNo'];?> : Requirements problem!</title>
<title>ICEcoder <?php echo $settingsClass->versionNo;?> : Requirements problem!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
@@ -53,7 +48,7 @@ if (false === $reqsPassed) {
<div class="screenVCenter">
<div class="screenCenter">
<img src="assets/images/icecoder.png" alt="ICEcoder">
<div class="version" style="margin-bottom: 22px">v <?php echo $ICEcoderSettings['versionNo'];?></div>
<div class="version" style="margin-bottom: 22px">v <?php echo $settingsClass->versionNo;?></div>
<span style="display: inline-block; color: #fff">
<b style="padding: 5px; background: #b00; color: #fff">Requirements problem!</b><br><br><br><br>
@@ -65,15 +60,21 @@ if (false === $reqsPassed) {
if (true === in_array("phpSession", $reqsFailures)) {
echo "You don't appear to have a<br>working PHP session<br><br>";
}
if (true === in_array("phpCreateConfig", $reqsFailures)) {
echo "Cannot create config file:<br>data/" . $settingsClass->getConfigGlobalFileDetails()['fileName'] . "<br>Please check write permissions<br>on data dir and reload page<br><br>";
}
if (true === in_array("phpFileExists", $reqsFailures)) {
echo "You don't seem to have<br>the config file<br><br>";
}
if (true === in_array("phpReadFile", $reqsFailures)) {
echo "You don't seem to be able<br>to read the config file<br><br>";
}
if (true === in_array("phpWriteFile", $reqsFailures)) {
echo "You don't seem to be able<br>to write to the config file<br><br>";
}
if (true === in_array("phpAllowURLFOpen", $reqsFailures)) {
echo "You don't seem to have<br>allow_url_fopen enabled<br><br>";
}
if (true === in_array("phpCreateConfig", $reqsFailures)) {
echo "Cannot update config file:<br>data/".$configSettings."<br>Please check write permissions<br>on data dir and reload page<br><br>";
}
if (true === in_array("phpCreateSettings", $reqsFailures)) {
echo "Couldn't create:<br>data/$settingsFile<br>Maybe you need write<br>permissions on the data dir?<br><br>";
}