Skip to content

Commit d58075c

Browse files
committed
fix(textareafield): embedded image upload broken
1 parent 9d72846 commit d58075c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+601
-214
lines changed

ajax/formanswer.php

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

front/form.form.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -124,52 +124,6 @@
124124
$form->importJson($_REQUEST);
125125
Html::back();
126126

127-
} else if (isset($_POST['submit_formcreator'])) {
128-
// Save form to target
129-
if (!$form->getFromDB($_POST['plugin_formcreator_forms_id'])) {
130-
Html::back();
131-
}
132-
133-
// If user is not authenticated, create temporary user
134-
if (!isset($_SESSION['glpiname'])) {
135-
$_SESSION['glpiname'] = 'formcreator_temp_user';
136-
}
137-
138-
// Save form
139-
$formAnswer = PluginFormcreatorCommon::getFormAnswer();
140-
if ($formAnswer->add($_POST) === false) {
141-
Html::back();
142-
}
143-
$form->increaseUsageCount();
144-
145-
if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
146-
// Form was saved by an annymous user
147-
unset($_SESSION['glpiname']);
148-
// don't show notifications
149-
unset($_SESSION['MESSAGE_AFTER_REDIRECT']);
150-
Html::redirect('formdisplay.php?answer_saved');
151-
}
152-
153-
// redirect to created item
154-
if ($_SESSION['glpibackcreated']) {
155-
if (count($formAnswer->targetList) == 1) {
156-
$target = current($formAnswer->targetList);
157-
Html::redirect($target->getFormURLWithID($target->getID()));
158-
}
159-
Html::redirect(PluginFormcreatorFormAnswer::getFormURLWithID($formAnswer->getID()));
160-
}
161-
162-
if (plugin_formcreator_replaceHelpdesk()) {
163-
// Form was saved from the service catalog
164-
Html::redirect('issue.php');
165-
}
166-
if (strpos($_SERVER['HTTP_REFERER'], 'formdisplay.php') !== false) {
167-
// Form was saved from helpdesk (assistance > forms)
168-
Html::redirect('formlist.php');
169-
}
170-
// Form was saved from preview tab, go back to the preview
171-
Html::back();
172-
173127
} else {
174128
// Show forms form
175129
Session::checkRight('entity', UPDATE);

inc/abstractitiltarget.class.php

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131

32-
use Glpi\Application\View\TemplateRenderer;
3332
use Glpi\Toolbox\Sanitizer;
34-
use GlpiPlugin\Formcreator\Field\FileField;
33+
use GlpiPlugin\Formcreator\Field\TextareaField;
3534

3635
if (!defined('GLPI_ROOT')) {
3736
die("Sorry. You can't access this file directly");
@@ -2264,28 +2263,21 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
22642263
return $data;
22652264
}
22662265

2267-
/**
2268-
* Emulate file uploads for documents provided to file questions
2269-
*
2270-
* @param array $data
2271-
* @param PluginFormcreatorFormAnswer $formanswer a form answer
2272-
* @return array input $data updated with (fake) file uploads
2273-
*/
2274-
protected function prepareUploadedFiles(array $data, $formanswer): array {
2266+
protected function prepareUploadsFromTextarea(array $data, PluginFormcreatorFormAnswer $formanswer): array {
22752267
$saved_documents = $formanswer->getFileProperties();
22762268

22772269
if ($saved_documents) {
22782270
foreach ($formanswer->getForm()->getFields() as $questionId => $field) {
2279-
if (!($field instanceOf FileField)) {
2271+
if (!($field instanceOf TextareaField)) {
22802272
continue;
22812273
}
2282-
if (!isset($saved_documents["_filename"][$questionId])) {
2274+
if (!isset($saved_documents["_content"][$questionId])) {
22832275
continue;
22842276
}
2285-
$data["_filename"] = array_merge($data["_filename"], $saved_documents["_filename"][$questionId] ?? []);
2286-
$data["_tag_filename"] = array_merge($data["_tag_filename"], $saved_documents["_tag_filename"][$questionId] ?? []);
2277+
$data["_content"] = array_merge($data["_content"], $saved_documents["_content"][$questionId] ?? []);
2278+
$data["_tag_content"] = array_merge($data["_tag_content"], $saved_documents["_tag_content"][$questionId] ?? []);
22872279

2288-
foreach ($saved_documents["_filename"][$questionId] as $key => $filename) {
2280+
foreach ($saved_documents["_content"][$questionId] as $key => $filename) {
22892281
$uploaded_filename = $formanswer->getFileName($questionId, $key);
22902282
if ($uploaded_filename != '') {
22912283
copy(GLPI_DOC_DIR . '/' . $uploaded_filename, GLPI_TMP_DIR . '/' . $filename);
@@ -2294,12 +2286,12 @@ protected function prepareUploadedFiles(array $data, $formanswer): array {
22942286
}
22952287
} else {
22962288
foreach ($formanswer->getForm()->getFields() as $questionId => $field) {
2297-
if (!($field instanceOf FileField)) {
2289+
if (!($field instanceOf TextareaField)) {
22982290
continue;
22992291
}
2300-
$data["_filename"] = array_merge($data["_filename"], $formanswer->input["_formcreator_field_" . $questionId]);
2301-
$data["_prefix_filename"] = array_merge($data["_prefix_filename"], $formanswer->input["_prefix_formcreator_field_" . $questionId]);
2302-
$data["_tag_filename"] = array_merge($data["_tag_filename"], $formanswer->input["_tag_formcreator_field_" . $questionId]);
2292+
$data["_content"] = array_merge($data["_content"], $formanswer->input["_formcreator_field_" . $questionId]);
2293+
$data["_prefix_content"] = array_merge($data["_prefix_content"], $formanswer->input["_prefix_formcreator_field_" . $questionId]);
2294+
$data["_tag_content"] = array_merge($data["_tag_content"], $formanswer->input["_tag_formcreator_field_" . $questionId]);
23032295
foreach ($formanswer->input["_formcreator_field_" . $questionId] as $key => $filename) {
23042296
$uploaded_filename = $formanswer->getFileName($questionId, $key);
23052297
if ($uploaded_filename != '') {
@@ -2312,6 +2304,65 @@ protected function prepareUploadedFiles(array $data, $formanswer): array {
23122304
return $data;
23132305
}
23142306

2307+
/**
2308+
* Emulate file uploads for documents provided to file questions
2309+
*
2310+
* @param array $data
2311+
* @return array input $data updated with (fake) file uploads
2312+
*/
2313+
protected function prepareUploadedFiles(array $data): array {
2314+
$data['_filename'] = [];
2315+
$data['_prefix_filename'] = [];
2316+
$data['_tag_filename'] = [];
2317+
2318+
// emulate file uploads of inline images
2319+
// TODO: replace PluginFormcreatorCommon::getDocumentsFromTag by Toolbox::getDocumentsFromTag
2320+
// when is merged https://github.com/glpi-project/glpi/pull/9335
2321+
foreach (PluginFormcreatorCommon::getDocumentsFromTag($data['content']) as $document) {
2322+
$prefix = uniqid('', true);
2323+
$filename = $prefix . 'image_paste.' . pathinfo($document['filename'], PATHINFO_EXTENSION);
2324+
if (!copy(GLPI_DOC_DIR . '/' . $document['filepath'], GLPI_TMP_DIR . '/' . $filename)) {
2325+
continue;
2326+
}
2327+
2328+
// Formanswers answers contains document tags to allow
2329+
// Replace them with a IMG tag similar to those found after pasting an
2330+
// image in a textarea
2331+
// <img id="..." src="blob:http://..." data-upload_id=".." />
2332+
// the attribute id is requires to let GLPI process the upload properly
2333+
$img = "<img id='" . $document['tag'] . "' src='' />";
2334+
$data['content'] = preg_replace(
2335+
'/' . Document::getImageTag($document['tag']) . '/',
2336+
Sanitizer::sanitize($img),
2337+
$data['content']
2338+
);
2339+
2340+
$data['_filename'][] = $filename;
2341+
$data['_prefix_filename'][] = $prefix;
2342+
$data['_tag_filename'][] = $document['tag'];
2343+
}
2344+
2345+
// emulate file upload
2346+
foreach (array_keys($this->attachedDocuments) as $documentId) {
2347+
$document = new Document();
2348+
if (!$document->getFromDB($documentId)) {
2349+
continue;
2350+
}
2351+
2352+
$prefix = uniqid('', true);
2353+
$filename = $prefix . $document->fields['filename'];
2354+
if (!copy(GLPI_DOC_DIR . '/' . $document->fields['filepath'], GLPI_TMP_DIR . '/' . $filename)) {
2355+
continue;
2356+
}
2357+
2358+
$data['_filename'][] = $filename;
2359+
$data['_prefix_filename'][] = $prefix;
2360+
$data['_tag_filename'][] = $document->fields['tag'];
2361+
}
2362+
2363+
return $data;
2364+
}
2365+
23152366
public static function getTargetType(): int {
23162367
return self::TARGET_TYPE_OBJECT;
23172368
}

inc/common.class.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,11 @@ public static function checkRegex($regex) {
593593
* @return array data from documents having tags found
594594
*/
595595
public static function getDocumentsFromTag(string $content_text): array {
596-
preg_match_all('/'.Document::getImageTag('(([a-z0-9]+|[\.\-]?)+)').'/', $content_text,
597-
$matches, PREG_PATTERN_ORDER);
596+
preg_match_all(
597+
'/'.Document::getImageTag('(([a-z0-9]+|[\.\-]?)+)').'/',
598+
$content_text,
599+
$matches, PREG_PATTERN_ORDER
600+
);
598601
if (!isset($matches[1]) || count($matches[1]) == 0) {
599602
return [];
600603
}

inc/field/actorfield.class.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Html;
3737
use User;
3838
use Session;
39+
use PluginFormcreatorFormAnswer;
3940
use GlpiPlugin\Formcreator\Exception\ComparisonException;
4041
use Glpi\Application\View\TemplateRenderer;
4142

@@ -125,7 +126,7 @@ public function getRenderedHtml($domain, $canEdit = true): string {
125126
return $html;
126127
}
127128

128-
public function serializeValue(): string {
129+
public function serializeValue(PluginFormcreatorFormAnswer $formanswer): string {
129130
if ($this->value === null || $this->value === '') {
130131
return '';
131132
}
@@ -306,7 +307,10 @@ public function prepareQuestionInputForSave($input) {
306307
}
307308

308309
$this->value = $parsed;
309-
$input['default_values'] = $this->serializeValue();
310+
$input['default_values'] = '';
311+
if ($this->value !== null && $this->value != '') {
312+
$input['default_value'] = json_encode($this->value);
313+
}
310314

311315
return $input;
312316
}

inc/field/checkboxesfield.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Toolbox;
3838
use Session;
3939
use PluginFormcreatorQuestionRange;
40+
use PluginFormcreatorFormAnswer;
4041
use Glpi\Application\View\TemplateRenderer;
4142

4243
class CheckboxesField extends PluginFormcreatorAbstractField
@@ -116,7 +117,7 @@ public static function getName(): string {
116117
return __('Checkboxes', 'formcreator');
117118
}
118119

119-
public function serializeValue(): string {
120+
public function serializeValue(PluginFormcreatorFormAnswer $formanswer): string {
120121
if ($this->value === null || $this->value === '') {
121122
return '';
122123
}

inc/field/datefield.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Html;
3737
use DateTime;
3838
use Session;
39+
use PluginFormcreatorFormAnswer;
3940
use GlpiPlugin\Formcreator\Exception\ComparisonException;
4041
use Glpi\Application\View\TemplateRenderer;
4142

@@ -80,7 +81,7 @@ public function getRenderedHtml($domain, $canEdit = true): string {
8081
return $html;
8182
}
8283

83-
public function serializeValue(): string {
84+
public function serializeValue(PluginFormcreatorFormAnswer $formanswer): string {
8485
return $this->value;
8586
}
8687

0 commit comments

Comments
 (0)