Fix attachment ids in metacode elements during import (#4213)
authorMatthias Schmidt <gravatronics@live.com>
Fri, 14 May 2021 11:14:59 +0000 (13:14 +0200)
committerGitHub <noreply@github.com>
Fri, 14 May 2021 11:14:59 +0000 (13:14 +0200)
* Fix attachment ids in metacode elements during import

Close #4212

* Update wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php

Co-authored-by: Tim Düsterhus <duesterhus@woltlab.com>
Co-authored-by: Tim Düsterhus <duesterhus@woltlab.com>
wcfsetup/install/files/lib/system/importer/AbstractAttachmentImporter.class.php

index 36b86b6b8b15f67624ad3c7169a8a2d890c78529..6cdc414b4c5bce4135ed0a547a0e83a97e0cdca4 100644 (file)
@@ -4,6 +4,7 @@ use wcf\data\attachment\Attachment;
 use wcf\data\attachment\AttachmentEditor;
 use wcf\system\exception\SystemException;
 use wcf\util\FileUtil;
+use wcf\util\JSON;
 
 /**
  * Imports attachments.
@@ -108,6 +109,32 @@ class AbstractAttachmentImporter extends AbstractImporter {
                        
                        return $message;
                }
+               else {
+                       return \preg_replace_callback(
+                               '~<woltlab-metacode data-name="attach" data-attributes="(?<attributes>[^"]+)">~',
+                               static function (array $matches) use ($oldID, $newID): string {
+                                       $encodedAttributes = $matches['attributes'];
+                                       
+                                       $base64Decoded = \base64_decode($matches['attributes']);
+                                       if ($base64Decoded) {
+                                               try {
+                                                       $attributes = JSON::decode($base64Decoded);
+                                                       if ($attributes[0] == $oldID) {
+                                                               $attributes[0] = $newID;
+                                                       }
+                                                       
+                                                       $encodedAttributes = \base64_encode(JSON::encode($attributes));
+                                               }
+                                               catch (\Exception $e) {
+                                                       $encodedAttributes = $matches['attributes'];
+                                               }
+                                       }
+                                       
+                                       return '<woltlab-metacode data-name="attach" data-attributes="' . $encodedAttributes . '">';
+                               },
+                               $message
+                       );
+               }
                
                return false;
        }