Gracefully handle broken preload JSON
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 19 Sep 2023 07:58:44 +0000 (09:58 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 19 Sep 2023 07:58:44 +0000 (09:58 +0200)
see https://www.woltlab.com/community/thread/301669-was-will-mir-diese-fehlermeldung-sagen/

wcfsetup/install/files/lib/system/style/StyleHandler.class.php

index 4ba38527ae26919c46c5601730965d72c2e668e3..b087706e32004215cb4cf1cdb14e0e61390b3e37 100644 (file)
@@ -147,10 +147,21 @@ class StyleHandler extends SingletonFactory
                 StyleCompiler::getInstance()->compile($this->getStyle()->getDecoratedObject());
             }
 
-            if (\is_readable(WCF_DIR . 'style/style-' . $this->getStyle()->styleID . '-preload.json')) {
-                $decoded = JSON::decode(\file_get_contents(WCF_DIR . 'style/style-' . $this->getStyle()->styleID . '-preload.json'));
-                if (isset($decoded['html']) && \is_array($decoded['html'])) {
-                    $preload = \implode('', $decoded['html']);
+            $preloadFilename = WCF_DIR . 'style/style-' . $this->getStyle()->styleID . '-preload.json';
+            if (\is_readable($preloadFilename)) {
+                try {
+                    $decoded = \json_decode(
+                        \file_get_contents($preloadFilename),
+                        associative: true,
+                        flags: \JSON_THROW_ON_ERROR
+                    );
+                    if (isset($decoded['html']) && \is_array($decoded['html'])) {
+                        $preload = \implode('', $decoded['html']);
+                    } else {
+                        @\unlink($preloadFilename);
+                    }
+                } catch (\JsonException) {
+                    @\unlink($preloadFilename);
                 }
             }
         }