From 21b94f5b762633c4e3288667fdcbe10ab3453e1b Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Mon, 9 Aug 2021 09:11:00 +0200 Subject: [PATCH] fix(question): save images in description as inline base64 Signed-off-by: Thierry Bugier --- inc/abstractfield.class.php | 10 ++++++- inc/question.class.php | 58 ++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/inc/abstractfield.class.php b/inc/abstractfield.class.php index 9882a87c6..c05ab45f6 100644 --- a/inc/abstractfield.class.php +++ b/inc/abstractfield.class.php @@ -88,7 +88,15 @@ public function show($domain, $canEdit = true) { $html .= ''; } if ($this->isEditableField() && !empty($this->question->fields['description'])) { - $html .= '
' . html_entity_decode(__($this->question->fields['description'], $domain)) . '
'; + $description = $this->question->fields['description']; + foreach (PluginFormcreatorCommon::getDocumentsFromTag($description) as $document) { + $prefix = uniqid('', true); + $filename = $prefix . 'image_paste.' . pathinfo($document['filename'], PATHINFO_EXTENSION); + if (!copy(GLPI_DOC_DIR . '/' . $document['filepath'], GLPI_TMP_DIR . '/' . $filename)) { + continue; + } + } + $html .= '
' . html_entity_decode(__($description, $domain)) . '
'; } $html .= '
'; $html .= $this->getRenderedHtml($domain, $canEdit); diff --git a/inc/question.class.php b/inc/question.class.php index 203a5877f..bc9e061e5 100644 --- a/inc/question.class.php +++ b/inc/question.class.php @@ -440,6 +440,34 @@ public function prepareInputForAdd($input) { } } + // handle description field and its inline pictures + if (isset($input['_description'])) { + foreach ($input['_description'] as $id => $filename) { + // TODO :replace PluginFormcreatorCommon::getDuplicateOf by Document::getDuplicateOf + // when is merged https://github.com/glpi-project/glpi/pull/9335 + if ($document = PluginFormcreatorCommon::getDuplicateOf(Session::getActiveEntity(), GLPI_TMP_DIR . '/' . $filename)) { + $this->value = str_replace('id="' . $input['_tag_description'] . '"', $document->fields['tag'], $this->value); + $input['_tag_description'][$id] = $document->fields['tag']; + } + } + + $input = $this->addFiles( + $input, + [ + 'force_update' => true, + 'content_field' => null, + 'name' => 'description', + ] + ); + + $input['description'] = Html::entity_decode_deep($input['description']); + foreach ($input['_tag_description'] as $tag) { + $regex = '/]+' . preg_quote($tag, '/') . '[^<]+>/im'; + $input['description'] = preg_replace($regex, "#$tag#", $input['description']); + } + $input['description'] = Html::entities_deep($input['description']); + } + // generate a unique id if (!isset($input['uuid']) || empty($input['uuid'])) { @@ -469,6 +497,34 @@ public function prepareInputForUpdate($input) { return false; } + // handle description field and its inline pictures + if (isset($input['_description'])) { + foreach ($input['_description'] as $id => $filename) { + // TODO :replace PluginFormcreatorCommon::getDuplicateOf by Document::getDuplicateOf + // when is merged https://github.com/glpi-project/glpi/pull/9335 + if ($document = PluginFormcreatorCommon::getDuplicateOf(Session::getActiveEntity(), GLPI_TMP_DIR . '/' . $filename)) { + $this->value = str_replace('id="' . $input['_tag_description'] . '"', $document->fields['tag'], $this->value); + $input['_tag_description'][$id] = $document->fields['tag']; + } + } + + $input = $this->addFiles( + $input, + [ + 'force_update' => true, + 'content_field' => null, + 'name' => 'description', + ] + ); + + $input['description'] = Html::entity_decode_deep($input['description']); + foreach ($input['_tag_description'] as $tag) { + $regex = '/]+' . preg_quote($tag, '/') . '[^<]+>/im'; + $input['description'] = preg_replace($regex, "#$tag#", $input['description']); + } + $input['description'] = Html::entities_deep($input['description']); + } + // generate a unique id if (!isset($input['uuid']) || empty($input['uuid'])) { @@ -831,7 +887,7 @@ public function showForm($ID, $options = []) { echo Html::textarea([ 'name' => 'description', 'id' => 'description', - 'value' => $this->fields['description'], + 'value' => Toolbox::convertTagToImage($this->fields['description'], $this), 'enable_richtext' => true, 'filecontainer' => 'description_info', 'display' => false,