-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathformanswer.php
158 lines (147 loc) · 4.81 KB
/
formanswer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
include ('../../../inc/includes.php');
// Check if plugin is activated...
if (!Plugin::isPluginActive('formcreator')) {
http_response_code(404);
die();
}
if (!isset($_POST['add']) || !isset($_POST['plugin_formcreator_forms_id'])) {
http_response_code(500);
die();
}
$form = new PluginFormcreatorForm();
if (!$form->getFromDB($_POST['plugin_formcreator_forms_id'])) {
http_response_code(500);
die();
}
// If user is not authenticated, create temporary user
if (!isset($_SESSION['glpiname'])) {
$_SESSION['glpiname'] = 'formcreator_temp_user';
}
// Save form
$backup_debug = $_SESSION['glpi_use_mode'];
$_SESSION['glpi_use_mode'] = Session::NORMAL_MODE;
$formAnswer = new PluginFormcreatorFormAnswer();
if ($formAnswer->add($_POST) === false) {
http_response_code(400);
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
// Messages are for authenticated users. This is a workaround
ob_start();
Html::displayMessageAfterRedirect(filter_var(($_GET['display_container'] ?? true), FILTER_VALIDATE_BOOLEAN));
$messages = ob_get_clean();
echo json_encode([
'message' => $messages
]);
}
$_SESSION['glpi_use_mode'] = $backup_debug;
die();
}
$form->increaseUsageCount();
$_SESSION['glpi_use_mode'] = $backup_debug;
if (!PluginFormcreatorForm::isNewID($form->fields['plugin_formcreator_forms_id'])) {
$nextForm = new PluginFormcreatorForm();
$nextForm->getFromDBByCrit([
'id' => $form->fields['plugin_formcreator_forms_id'],
'is_active' => 1,
'is_deleted' => 0,
]);
if (!$nextForm->isNewItem()) {
echo json_encode(
[
'redirect' => 'formdisplay.php?id=' . $nextForm->getID(),
], JSON_FORCE_OBJECT
);
die();
}
}
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
// Form was saved by an annymous user
unset($_SESSION['glpiname']);
// don't show notifications
unset($_SESSION['MESSAGE_AFTER_REDIRECT']);
echo json_encode(
[
'redirect' => 'formdisplay.php?answer_saved',
], JSON_FORCE_OBJECT
);
die();
}
// redirect to created item
if ($_SESSION['glpibackcreated'] && Ticket::canView()) {
if (strpos($_SERVER['HTTP_REFERER'], 'form.form.php') === false) {
// User was not testing the form from preview
if (count($formAnswer->targetList) == 1) {
$target = current($formAnswer->targetList);
echo json_encode(
[
'redirect' => $target->getFormURLWithID($target->getID()),
], JSON_FORCE_OBJECT
);
die();
}
echo json_encode(
[
'redirect' => $formAnswer->getFormURLWithID($formAnswer->getID()),
], JSON_FORCE_OBJECT
);
die();
}
echo json_encode(
[
'redirect' => (new PluginFormcreatorForm())->getFormURLWithID($formAnswer->fields['plugin_formcreator_forms_id']),
], JSON_FORCE_OBJECT
);
die();
}
if (plugin_formcreator_replaceHelpdesk()) {
if (Ticket::canView()) {
$redirect = PluginFormcreatorIssue::getSearchURL();
} else {
$redirect = 'wizard.php';
}
// Form was saved from the service catalog
echo json_encode(
[
'redirect' => $redirect,
], JSON_FORCE_OBJECT
);
die();
}
if (strpos($_SERVER['HTTP_REFERER'], 'formdisplay.php') !== false) {
// Form was saved from helpdesk (assistance > forms)
echo json_encode(
[
'redirect' => 'formlist.php',
], JSON_FORCE_OBJECT
);
die();
}