Skip to content

Commit

Permalink
feat(form): version check of file on import
Browse files Browse the repository at this point in the history
allows import of files without version data, at own risk of the admin

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Feb 5, 2020
1 parent 76b2aae commit b380b79
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion front/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}

$form = new PluginFormcreatorForm;
$export_array = ['forms' => []];
$export_array = ['schema_version' => PLUGIN_FORMCREATOR_SCHEMA_VERSION, 'forms' => []];
foreach ($_GET['plugin_formcreator_forms_id'] as $id) {
$form->getFromDB($id);
$export_array['forms'][] = $form->export();
Expand Down
8 changes: 8 additions & 0 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ public function export($remove_uuid = false) {
$question = new PluginFormcreatorQuestion();
$question->getFromDB($condition['plugin_formcreator_questions_id']);
$condition['plugin_formcreator_questions_id'] = $question->fields['uuid'];

$containerType = $condition['itemtype'];
if (!class_exists($containerType) || !is_subclass_of($containerType, PluginFormcreatorConditionnableInterface::class)) {
return false;
}
$container = new $containerType();
$container->getFromDB($condition['items_id']);
$condition['items_id'] = $container->fields['uuid'];
}
unset($condition[$idToRemove]);

Expand Down
20 changes: 17 additions & 3 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1810,17 +1810,31 @@ public function importJson($params = []) {
// parse json file(s)
foreach ($params['_json_file'] as $filename) {
if (!$json = file_get_contents(GLPI_TMP_DIR."/".$filename)) {
Session::addMessageAfterRedirect(__("Forms import impossible, the file is empty"));
Session::addMessageAfterRedirect(__("Forms import impossible, the file is empty", 'formcreator'));
continue;
}
if (!$forms_toimport = json_decode($json, true)) {
Session::addMessageAfterRedirect(__("Forms import impossible, the file seems corrupt"));
Session::addMessageAfterRedirect(__("Forms import impossible, the file seems corrupt", 'formcreator'));
continue;
}
if (!isset($forms_toimport['forms'])) {
Session::addMessageAfterRedirect(__("Forms import impossible, the file seems corrupt"));
Session::addMessageAfterRedirect(__("Forms import impossible, the file seems corrupt", 'formcreator'));
continue;
}
if (isset($forms_toimport['schema_version'])) {
if (($forms_toimport['schema_version']) != PLUGIN_FORMCREATOR_SCHEMA_VERSION) {
Session::addMessageAfterRedirect(
__("Forms import impossible, the file was generated with another version", 'formcreator'),
false, ERROR
);
continue;
}
} else {
Session::addMessageAfterRedirect(
__("The file does not specifies the schema version. It was probably generated with a version older than 2.10 and import is expected to create incomplete or buggy forms.", 'formcreator'),
false, WARNING
);
}

$success = true;
foreach ($forms_toimport['forms'] as $form) {
Expand Down
1 change: 1 addition & 0 deletions tests/suite-unit/PluginFormcreatorCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public function testExport() {
// Test the exported data
$fieldsWithoutID = [
'itemtype',
'items_id',
'plugin_formcreator_questions_id',
'show_condition',
'show_value',
Expand Down

0 comments on commit b380b79

Please sign in to comment.