From 5aa3fd6f6a4dbe15fd8e3d76220f04550b5ae106 Mon Sep 17 00:00:00 2001 From: Lars Scheibling Date: Thu, 24 Jun 2021 15:31:08 +0200 Subject: [PATCH 1/2] Created export button for server list Created page for for import upload Created function to receive data --- src/lang/en_US.lang.php | 2 + .../Server/Controller/ServerController.php | 86 ++++++++++++++++++- src/templates/default/main/macros.tpl.html | 16 ++++ .../module/server/server/import.tpl.html | 15 ++++ 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/templates/default/module/server/server/import.tpl.html diff --git a/src/lang/en_US.lang.php b/src/lang/en_US.lang.php index 856fd14c..005152e8 100644 --- a/src/lang/en_US.lang.php +++ b/src/lang/en_US.lang.php @@ -50,6 +50,8 @@ $sm_lang = array( 'no' => 'No', 'insert' => 'Insert', 'add_new' => 'Add new', + 'export_config' => 'Export Configuration', + 'import_config' => 'Import Configuration', 'update_available' => 'A new version ({version}) is available. Click here to download the update.', 'back_to_top' => 'Back to top', 'go_back' => 'Go back', diff --git a/src/psm/Module/Server/Controller/ServerController.php b/src/psm/Module/Server/Controller/ServerController.php index cb08e5fc..afa7c047 100644 --- a/src/psm/Module/Server/Controller/ServerController.php +++ b/src/psm/Module/Server/Controller/ServerController.php @@ -29,6 +29,9 @@ namespace psm\Module\Server\Controller; use psm\Service\Database; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; + /** * Server module. Add/edit/delete servers, show a list of all servers etc. @@ -50,16 +53,77 @@ class ServerController extends AbstractServerController $this->setCSRFKey('server'); $this->setActions(array( - 'index', 'edit', 'save', 'delete', 'view', + 'index', 'edit', 'save', 'delete', 'view', 'export', 'import', 'importpost' ), 'index'); // make sure only admins are allowed to edit/delete servers: $this->setMinUserLevelRequiredForAction(PSM_USER_ADMIN, array( - 'delete', 'edit', 'save' + 'delete', 'edit', 'save', 'export' )); $this->twig->addGlobal('subtitle', psm_get_lang('menu', 'server')); } + /** + * Exports a JSON file with the server configuration + */ + protected function executeExport() + { + $request = new Request(); + $response = new Response( + json_encode($this->getServers()), + 200, + ['Content-Type' => 'application/json'] + ); + + $response->prepare($request); + $response->send(); + return exit; + } + + /** + * Import settings page + */ + protected function executeImport() + { + $sidebar = new \psm\Util\Module\Sidebar($this->twig); + $this->setSidebar($sidebar); + if ($this->getUser()->getUserLevel() == PSM_USER_ADMIN) { + $modal = new \psm\Util\Module\Modal($this->twig, 'delete', \psm\Util\Module\Modal::MODAL_TYPE_DANGER); + $this->addModal($modal); + $modal->setTitle(psm_get_lang('servers', 'delete_title')); + $modal->setMessage(psm_get_lang('servers', 'delete_message')); + $modal->setOKButtonLabel(psm_get_lang('system', 'delete')); + + $tpl_data = [ + 'label_upload' => 'Upload', + 'url_save' => psm_build_url(array( + 'mod' => 'server', + 'action' => 'importpost', + 'back_to' => "" + )) + ]; + + return $this->twig->render('module/server/server/import.tpl.html', $tpl_data); + } + else { + return $this->executeIndex(); + } + } + + /** + * Import post page + */ + protected function executeImportpost() + { + /** + * - Load file from $_FILES + * - Check if valid JSON + * - List all allowed fields + * - foreach (check if exists, else import) + * + * Check if exists based on? + */ + } /** * Prepare the template to show a list of all servers */ @@ -86,6 +150,24 @@ class ServerController extends AbstractServerController 'success', psm_get_lang('system', 'add_new') ); + + $sidebar->addButton( + 'export_config', + psm_get_lang('system', 'export_config'), + psm_build_url(array('mod' => 'server', 'action' => 'export')), + 'arrow-down', + 'success', + psm_get_lang('system', 'export_config') + ); + + $sidebar->addButton( + 'import_config', + psm_get_lang('system', 'import_config'), + psm_build_url(array('mod' => 'server', 'action' => 'import')), + 'arrow-up', + 'success', + psm_get_lang('system', 'import_config') + ); } $sidebar->addButton( diff --git a/src/templates/default/main/macros.tpl.html b/src/templates/default/main/macros.tpl.html index bda08f93..29b0184c 100644 --- a/src/templates/default/main/macros.tpl.html +++ b/src/templates/default/main/macros.tpl.html @@ -33,6 +33,22 @@ {% endmacro input_select %} + +{% macro input_upload(id, name, label, help, help_label) %} +
+
+ + + {% if help %} + {{ + help_label|striptags(',,
')|raw }}
+ {% endif %} + +
+{% endmacro input_upload %} + + {% macro input_select_multiple(id, name, label, placeholder, options, select_message) %}
diff --git a/src/templates/default/module/server/server/import.tpl.html b/src/templates/default/module/server/server/import.tpl.html new file mode 100644 index 00000000..01ee3063 --- /dev/null +++ b/src/templates/default/module/server/server/import.tpl.html @@ -0,0 +1,15 @@ +{% import 'main/macros.tpl.html' as macro %} +
+
+ {{ titlemode }} +
+ + {{ macro.input_upload("import_upload", "import_upload", "Upload Configuration", null, null) }} + + + {{ label_upload }} + {{ macro.button_save(null, label_upload) }} + Go Back + {{ macro.input_csrf() }} + From 346295cd92794cfc6175dd86e6cebfc0af6c955d Mon Sep 17 00:00:00 2001 From: Lars Scheibling Date: Fri, 25 Jun 2021 00:21:57 +0200 Subject: [PATCH 2/2] Added beta import and export functionality (eng only) --- .../Server/Controller/ServerController.php | 171 +++++++++++------- .../module/server/server/import.tpl.html | 3 + 2 files changed, 105 insertions(+), 69 deletions(-) diff --git a/src/psm/Module/Server/Controller/ServerController.php b/src/psm/Module/Server/Controller/ServerController.php index afa7c047..a01732f0 100644 --- a/src/psm/Module/Server/Controller/ServerController.php +++ b/src/psm/Module/Server/Controller/ServerController.php @@ -63,67 +63,6 @@ class ServerController extends AbstractServerController $this->twig->addGlobal('subtitle', psm_get_lang('menu', 'server')); } - /** - * Exports a JSON file with the server configuration - */ - protected function executeExport() - { - $request = new Request(); - $response = new Response( - json_encode($this->getServers()), - 200, - ['Content-Type' => 'application/json'] - ); - - $response->prepare($request); - $response->send(); - return exit; - } - - /** - * Import settings page - */ - protected function executeImport() - { - $sidebar = new \psm\Util\Module\Sidebar($this->twig); - $this->setSidebar($sidebar); - if ($this->getUser()->getUserLevel() == PSM_USER_ADMIN) { - $modal = new \psm\Util\Module\Modal($this->twig, 'delete', \psm\Util\Module\Modal::MODAL_TYPE_DANGER); - $this->addModal($modal); - $modal->setTitle(psm_get_lang('servers', 'delete_title')); - $modal->setMessage(psm_get_lang('servers', 'delete_message')); - $modal->setOKButtonLabel(psm_get_lang('system', 'delete')); - - $tpl_data = [ - 'label_upload' => 'Upload', - 'url_save' => psm_build_url(array( - 'mod' => 'server', - 'action' => 'importpost', - 'back_to' => "" - )) - ]; - - return $this->twig->render('module/server/server/import.tpl.html', $tpl_data); - } - else { - return $this->executeIndex(); - } - } - - /** - * Import post page - */ - protected function executeImportpost() - { - /** - * - Load file from $_FILES - * - Check if valid JSON - * - List all allowed fields - * - foreach (check if exists, else import) - * - * Check if exists based on? - */ - } /** * Prepare the template to show a list of all servers */ @@ -350,13 +289,21 @@ class ServerController extends AbstractServerController /** * Executes the saving of one of the servers */ - protected function executeSave() + protected function executeSave($internal = false) { + if (empty($_POST)) { // dont process anything if no data has been posted return $this->executeIndex(); } + if ($internal) { + $this->server_id = (isset($_POST['server_id']) ? $_POST['server_id'] : 0); + if (empty($this->getServers($this->server_id))) { + $this->server_id = 0; + } + } + // We need the server id to encrypt the password. Encryption will be done after the server is added $encrypted_password = ''; @@ -365,6 +312,7 @@ class ServerController extends AbstractServerController if ($this->server_id > 0) { $edit_server = $this->getServers($this->server_id); + $hash = sha1($edit_server['website_password']); if ($new_password == $hash) { @@ -375,7 +323,6 @@ class ServerController extends AbstractServerController } } } - $clean = array( 'label' => trim(strip_tags(psm_POST('label', ''))), 'ip' => trim(strip_tags(psm_POST('ip', ''))), @@ -451,6 +398,7 @@ class ServerController extends AbstractServerController // check for edit or add if ($this->server_id > 0) { + // edit $this->db->save( PSM_DB_PREFIX . 'servers', @@ -497,13 +445,15 @@ class ServerController extends AbstractServerController // add all new users $this->db->insertMultiple(PSM_DB_PREFIX . 'users_servers', $user_idc_save); } - - $back_to = isset($_GET['back_to']) ? $_GET['back_to'] : 'index'; - if ($back_to == 'view') { - return $this->runAction('view'); - } else { - return $this->runAction('index'); + if (!$internal) { + $back_to = isset($_GET['back_to']) ? $_GET['back_to'] : 'index'; + if ($back_to == 'view') { + return $this->runAction('view'); + } else { + return $this->runAction('index'); + } } + return true; } /** @@ -526,6 +476,89 @@ class ServerController extends AbstractServerController } return $this->runAction('index'); } + + /** + * Exports a JSON file with the server configuration + */ + protected function executeExport() + { + $request = new Request(); + $response = new Response( + json_encode($this->getServers()), + 200, + array( + 'Content-disposition' => 'attachment; filename=server_export.json', + 'Content-Type' => 'application/json' + ) + ); + + $response->prepare($request); + $response->send(); + return exit; + } + + /** + * Import settings page + */ + protected function executeImport($error = "") + { + $sidebar = new \psm\Util\Module\Sidebar($this->twig); + $this->setSidebar($sidebar); + if ($this->getUser()->getUserLevel() == PSM_USER_ADMIN) { + $modal = new \psm\Util\Module\Modal($this->twig, 'delete', \psm\Util\Module\Modal::MODAL_TYPE_DANGER); + $this->addModal($modal); + $modal->setTitle(psm_get_lang('servers', 'delete_title')); + $modal->setMessage(psm_get_lang('servers', 'delete_message')); + $modal->setOKButtonLabel(psm_get_lang('system', 'delete')); + + $tpl_data = [ + 'label_upload' => 'Upload', + 'error_message' => $error, + 'url_save' => psm_build_url(array( + 'mod' => 'server', + 'action' => 'importpost', + 'back_to' => "" + )) + ]; + + return $this->twig->render('module/server/server/import.tpl.html', $tpl_data); + } + else { + return $this->executeIndex(); + } + } + + /** + * Import post page + */ + protected function executeImportpost() + { + $temp_name = $_FILES['import_upload']['tmp_name']; + echo "Started decode"; + $file = json_decode( + file_get_contents($temp_name), + true + ); + + if (!$file) { + echo "NoFile"; + return $this->executeImport("Not a valid JSON file"); + } + + foreach ($file as $server) { + $_POST = $server; + $this->executeSave(true); + } + + return $this->executeIndex(); + /** + * - List all allowed fields + * - foreach (check if exists, else import) + * + * Check if exists based on? + */ + } + /** * Prepare the view template diff --git a/src/templates/default/module/server/server/import.tpl.html b/src/templates/default/module/server/server/import.tpl.html index 01ee3063..6ae1ec6a 100644 --- a/src/templates/default/module/server/server/import.tpl.html +++ b/src/templates/default/module/server/server/import.tpl.html @@ -3,6 +3,9 @@ autocomplete="off" enctype="multipart/form-data">
{{ titlemode }} + {% if error_message != "" %} +

Error: {{ error_message }}

+ {% endif %}
{{ macro.input_upload("import_upload", "import_upload", "Upload Configuration", null, null) }}