Added a way to edit a server's properties - right now it only edits the server-port property
This commit is contained in:
@ -117,4 +117,36 @@ class ServerController extends Controller {
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
// GET edit server properties route
|
||||
public function getEdit(Request $request, Response $response, $args): Response {
|
||||
$config = $this->get('config');
|
||||
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
|
||||
|
||||
$server = new Server($serverDir);
|
||||
$data = array(
|
||||
'serverName' => $args['serverName'],
|
||||
'serverPort' => $server->getProperty('server-port'),
|
||||
);
|
||||
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'edit.twig', $data);
|
||||
}
|
||||
|
||||
// POST edit server properties route
|
||||
public function postEdit(Request $request, Response $response, $args): Response {
|
||||
// set up the POST parameters and grab our config
|
||||
$params = (array)$request->getParsedBody();
|
||||
$config = $this->get('config');
|
||||
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
|
||||
|
||||
// create server object and save new value
|
||||
$server = new Server($serverDir);
|
||||
$server->updateProperty('server-port', $params['serverPort']);
|
||||
|
||||
// redirect the user back to the home page
|
||||
return $response
|
||||
->withHeader('Location', '/')
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -84,11 +84,16 @@ class Server {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// get parameter from server properties file
|
||||
// get property from server properties file
|
||||
public function getProperty(string $param) {
|
||||
return $this->properties->get($param);
|
||||
}
|
||||
|
||||
// update property in server properties file
|
||||
public function updateProperty(string $paramName, $paramValue) {
|
||||
$this->properties->update($paramName, $paramValue);
|
||||
}
|
||||
|
||||
// getters & setters
|
||||
public function getDirectory(): string {
|
||||
return $this->rootDir;
|
||||
|
@ -4,9 +4,13 @@ namespace BitGoblin\MCST\Minecraft;
|
||||
|
||||
class ServerConfig {
|
||||
|
||||
// path to the server.properties file
|
||||
protected string $propertiesFile;
|
||||
// loaded properties
|
||||
protected array $config;
|
||||
|
||||
public function __construct($propertiesFile) {
|
||||
$this->propertiesFile = $propertiesFile; // save the properties file
|
||||
$this->config = array(); // initialize array
|
||||
|
||||
// parse config file
|
||||
@ -15,7 +19,7 @@ class ServerConfig {
|
||||
$line = fgets($fh);
|
||||
$eqpos = strpos($line, '=');
|
||||
$parameterName = substr($line, 0, $eqpos);
|
||||
$parameterValue = substr($line, ($eqpos + 1));
|
||||
$parameterValue = trim(substr($line, ($eqpos + 1)));
|
||||
|
||||
// assign parameter values to the config array
|
||||
$this->config[$parameterName] = $parameterValue;
|
||||
@ -26,4 +30,35 @@ class ServerConfig {
|
||||
return $this->config[$param];
|
||||
}
|
||||
|
||||
public function update(string $paramName, $paramValue) {
|
||||
// update the local variable value
|
||||
$this->config[$paramName] = $paramValue;
|
||||
|
||||
// update and save the server.properties file
|
||||
$newFileContent = '';
|
||||
$fh = fopen($this->propertiesFile, 'r+');
|
||||
while (!feof($fh)) {
|
||||
$line = fgets($fh);
|
||||
|
||||
if (!str_starts_with($line, '#')) {
|
||||
$eqpos = strpos($line, '=');
|
||||
$parameterName = substr($line, 0, $eqpos);
|
||||
$parameterValue = substr($line, ($eqpos + 1));
|
||||
|
||||
// use the new value if it's being updated
|
||||
if ($parameterName == $paramName) {
|
||||
$parameterValue = $paramValue . "\n";
|
||||
}
|
||||
|
||||
$newFileContent .= $parameterName . '=' . $parameterValue;
|
||||
} else {
|
||||
$newFileContent .= $line;
|
||||
}
|
||||
}
|
||||
|
||||
// save the new properties file
|
||||
file_put_contents($this->propertiesFile, $newFileContent);
|
||||
fclose($fh);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,3 +21,8 @@ $app->get('/server/{serverName}/status', '\\BitGoblin\\MCST\\Controllers\\Server
|
||||
$app->get('/server/{serverName}/start', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStart')->setName('server.start');
|
||||
// server stop route
|
||||
$app->get('/server/{serverName}/stop', '\\BitGoblin\\MCST\\Controllers\\ServerController:getStop')->setName('server.stop');
|
||||
|
||||
// edit GET route - this page allows a user to edit a server's properties
|
||||
$app->get('/server/{serverName}/edit', '\\BitGoblin\\MCST\\Controllers\\ServerController:getEdit')->setName('edit');
|
||||
// edit POST route - processes the edits a server's properties file
|
||||
$app->post('/server/{serverName}/edit', '\\BitGoblin\\MCST\\Controllers\\ServerController:postEdit');
|
||||
|
Reference in New Issue
Block a user