Adds editing of the config file

main
Yasen Pramatarov 2024-11-01 18:23:40 +02:00
parent 7841b13fd8
commit c00c1845f6
3 changed files with 42 additions and 3 deletions

View File

@ -2,6 +2,35 @@
class Config { class Config {
// edit the config file
public function editConfigFile($updatedConfig) {
// first we get a fresh config file contents as text
$config_contents = file_get_contents($config_file);
if (!$config_contents) {
return "Failed to read the config file \"$config_file\".";
}
// loop through the variables and updated them
foreach ($updatedConfig as $key => $newValue) {
// we look for 'option' => value
// option is always in single quotes
// value is without quotes, because it could be true/false
$patterm = "/(['\"]{$key}['\"]\s*=>\s*)([^,]+),/"
// prepare the value and replace it
$replacementValue = var_export($newValue, true);
$config_contents = preg_replace($pattern, "$1{$replacementValue},", $config_contents);
}
// write the new config file
if (!file_put_contents($config_file, $config_contents)) {
return "Failed to write the config file \"$config_file\".";
}
return true;
}
// loading the config.js // loading the config.js
public function getPlatformConfigjs($jitsiUrl, $raw = false) { public function getPlatformConfigjs($jitsiUrl, $raw = false) {
// constructing the URL // constructing the URL

View File

@ -20,8 +20,17 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// $content = file_get_contents($config_file); // $content = file_get_contents($config_file);
// $updatedContent = $content; // $updatedContent = $content;
// editing the config file
if (isset($_POST['item']) && $_POST['item'] === 'config_file') {
$result = $configObject->editConfigFile($_POST);
if ($result === true) {
$_SESSION['notice'] = "The config file is edited.";
} else {
$_SESSION['error'] = "Editing the config file failed. Error: $result";
}
// new host adding // new host adding
if (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'host') { } elseif (isset($_POST['new']) && isset($_POST['item']) && $_POST['new'] === 'true' && $_POST['item'] === 'host') {
$newHost = [ $newHost = [
'address' => $address, 'address' => $address,
'port' => $port, 'port' => $port,

View File

@ -6,7 +6,7 @@
<div class="card-text"> <div class="card-text">
<p class="text-danger"><strong>this may break everything, use with extreme caution</strong></p> <p class="text-danger"><strong>this may break everything, use with extreme caution</strong></p>
</div> </div>
<form method="POST" action="<?= htmlspecialchars($app_root) ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=config&item=config_file"> <form method="POST" action="<?= htmlspecialchars($app_root) ?>?page=config&item=config_file">
<?php <?php
include '../app/helpers/render.php'; include '../app/helpers/render.php';
@ -15,12 +15,13 @@ echo "\n";
?> ?>
<p class="text-danger"><strong>this may break everything, use with extreme caution</strong></p> <p class="text-danger"><strong>this may break everything, use with extreme caution</strong></p>
<br />
<input type="hidden" name="item" value="config_file" />
<a class="btn btn-outline-secondary btn-sm" href="<?= htmlspecialchars($app_root) ?>?page=config&item=config_file" />Cancel</a> <a class="btn btn-outline-secondary btn-sm" href="<?= htmlspecialchars($app_root) ?>?page=config&item=config_file" />Cancel</a>
&nbsp;&nbsp; &nbsp;&nbsp;
<input type="submit" class="btn btn-danger btn-sm" value="Save" /> <input type="submit" class="btn btn-danger btn-sm" value="Save" />
</form> </form>
</div> </div>
</div> </div>
<!-- /widget "config file" --> <!-- /widget "config file" -->