Skip to content

Commit f48f010

Browse files
committed
fix(form): error message when anonymous form submitted
refactor of the whole file, was too messy Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent d7152e6 commit f48f010

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

front/formdisplay.php

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,71 +33,70 @@
3333
include ('../../../inc/includes.php');
3434

3535
// Check if plugin is activated...
36-
$plugin = new Plugin();
37-
38-
if (!$plugin->isActivated('formcreator')) {
36+
if (!(new Plugin())->isActivated('formcreator')) {
3937
Html::displayNotFoundError();
4038
}
4139

4240
PluginFormcreatorForm::header();
4341

44-
if (!isset($_REQUEST['id'])
45-
|| !is_numeric($_REQUEST['id'])) {
46-
$message = __("The form has been successfully saved!");
47-
Html::displayTitle($CFG_GLPI['root_doc']."/pics/ok.png", $message, $message);
48-
}
42+
if (isset($_REQUEST['id'])
43+
&& is_numeric($_REQUEST['id'])) {
4944

50-
$criteria = [
51-
'id' => (int) $_REQUEST['id'],
52-
'is_active' => '1',
53-
'is_deleted'=> '0',
54-
];
55-
$form = new PluginFormcreatorForm();
56-
if (!$form->getFromDBByCrit($criteria)) {
57-
Html::displayNotFoundError();
58-
}
45+
$criteria = [
46+
'id' => (int) $_REQUEST['id'],
47+
'is_active' => '1',
48+
'is_deleted'=> '0',
49+
];
50+
$form = new PluginFormcreatorForm();
51+
if (!$form->getFromDBByCrit($criteria)) {
52+
Html::displayNotFoundError();
53+
}
5954

60-
if ($form->fields['access_rights'] != PluginFormcreatorForm::ACCESS_PUBLIC) {
61-
Session::checkLoginUser();
62-
if (!$form->checkEntity(true)) {
63-
Html::displayRightError();
64-
exit();
55+
if ($form->fields['access_rights'] != PluginFormcreatorForm::ACCESS_PUBLIC) {
56+
Session::checkLoginUser();
57+
if (!$form->checkEntity(true)) {
58+
Html::displayRightError();
59+
exit();
60+
}
6561
}
66-
}
6762

68-
if ($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
69-
$iterator = $DB->request(PluginFormcreatorForm_Profile::getTable(), [
70-
'WHERE' => [
71-
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
72-
'plugin_formcreator_forms_id' => $form->getID()
73-
],
74-
'LIMIT' => 1
75-
]);
76-
if (count($iterator) == 0) {
77-
Html::displayRightError();
78-
exit();
63+
if ($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
64+
$iterator = $DB->request(PluginFormcreatorForm_Profile::getTable(), [
65+
'WHERE' => [
66+
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
67+
'plugin_formcreator_forms_id' => $form->getID()
68+
],
69+
'LIMIT' => 1
70+
]);
71+
if (count($iterator) == 0) {
72+
Html::displayRightError();
73+
exit();
74+
}
7975
}
80-
}
81-
if (($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC) && (!isset($_SESSION['glpiID']))) {
82-
// If user is not authenticated, create temporary user
83-
if (!isset($_SESSION['glpiname'])) {
84-
$_SESSION['formcreator_forms_id'] = $form->getID();
85-
$_SESSION['glpiname'] = 'formcreator_temp_user';
86-
$_SESSION['valid_id'] = session_id();
87-
$_SESSION['glpiactiveentities'] = [$form->fields['entities_id']];
88-
$subentities = getSonsOf('glpi_entities', $form->fields['entities_id']);
89-
$_SESSION['glpiactiveentities_string'] = (!empty($subentities))
90-
? "'" . implode("', '", $subentities) . "'"
91-
: "'" . $form->fields['entities_id'] . "'";
76+
if (($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC) && (!isset($_SESSION['glpiID']))) {
77+
// If user is not authenticated, create temporary user
78+
if (!isset($_SESSION['glpiname'])) {
79+
$_SESSION['formcreator_forms_id'] = $form->getID();
80+
$_SESSION['glpiname'] = 'formcreator_temp_user';
81+
$_SESSION['valid_id'] = session_id();
82+
$_SESSION['glpiactiveentities'] = [$form->fields['entities_id']];
83+
$subentities = getSonsOf('glpi_entities', $form->fields['entities_id']);
84+
$_SESSION['glpiactiveentities_string'] = (!empty($subentities))
85+
? "'" . implode("', '", $subentities) . "'"
86+
: "'" . $form->fields['entities_id'] . "'";
87+
}
9288
}
93-
}
9489

95-
$form->displayUserForm();
90+
$form->displayUserForm();
9691

97-
// If user was not authenticated, remove temporary user
98-
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
99-
session_write_close();
100-
unset($_SESSION['glpiname']);
92+
// If user was not authenticated, remove temporary user
93+
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
94+
session_write_close();
95+
unset($_SESSION['glpiname']);
96+
}
97+
} else if (isset($_GET['answer_saved'])) {
98+
$message = __("The form has been successfully saved!");
99+
Html::displayTitle($CFG_GLPI['root_doc']."/pics/ok.png", $message, $message);
101100
}
102101

103102
PluginFormcreatorForm::footer();

0 commit comments

Comments
 (0)