vB5: Support import of [img2=json] BBCode
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 24 Aug 2020 12:09:26 +0000 (14:09 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 24 Aug 2020 12:09:26 +0000 (14:09 +0200)
files/lib/system/exporter/VB5xExporter.class.php

index 208029667296f4f02183d7778c09c1b48d4b3d04..571eadda910801ae5ae87ea3e5d99f58a678df74 100644 (file)
@@ -761,6 +761,8 @@ class VB5xExporter extends AbstractExporter {
                static $urlCallback = null;
                static $imgRegex = null;
                static $mediaRegex = null;
+               static $img2Regex = null;
+               static $img2Callback = null;
                static $attachRegex = null;
                static $attachCallback = null;
                
@@ -789,6 +791,25 @@ class VB5xExporter extends AbstractExporter {
                        $imgRegex = new Regex('\[img width=(\d+) height=\d+\](.*?)\[/img\]');
                        $mediaRegex = new Regex('\[video=([a-z]+);([a-z0-9-_]+)\]', Regex::CASE_INSENSITIVE);
                        
+                       $img2Regex = new Regex('\[img2=json\](.*?)\[/img2\]', Regex::CASE_INSENSITIVE);
+                       $img2Callback = function ($matches) {
+                               if (!empty($matches[1])) {
+                                       // json
+                                       try {
+                                               $payload = JSON::decode($matches[1]);
+                                       }
+                                       catch (SystemException $e) {
+                                               return $matches[0];
+                                       }
+                                       
+                                       if (isset($payload['src'])) {
+                                               return "[img]".$payload['src']."[/img]";
+                                       }
+                               }
+                               
+                               return $matches[0];
+                       };
+                       
                        $attachRegex = new Regex('\[attach=(?:json\](\{.*?\})|config\]([0-9]+))\[/attach\]', Regex::CASE_INSENSITIVE);
                        $attachCallback = function ($matches) {
                                if (!empty($matches[1])) {
@@ -841,6 +862,7 @@ class VB5xExporter extends AbstractExporter {
                
                // img
                $message = $imgRegex->replace($message, "[img='\\2',none,\\1][/img]");
+               $message = $img2Regex->replace($message, $img2Callback);
                
                // attach
                $message = $attachRegex->replace($message, $attachCallback);