3 namespace BookStack\Theming;
5 use BookStack\Util\CspService;
6 use BookStack\Util\HtmlContentFilter;
7 use BookStack\Util\HtmlNonceApplicator;
8 use Illuminate\Contracts\Cache\Repository as Cache;
10 class CustomHtmlHeadContentProvider
15 protected $cspService;
22 public function __construct(CspService $cspService, Cache $cache)
24 $this->cspService = $cspService;
25 $this->cache = $cache;
29 * Fetch our custom HTML head content prepared for use on web pages.
30 * Content has a nonce applied for CSP.
32 public function forWeb(): string
34 $content = $this->getSourceContent();
35 $hash = md5($content);
36 $html = $this->cache->remember('custom-head-web:' . $hash, 86400, function () use ($content) {
37 return HtmlNonceApplicator::prepare($content);
40 return HtmlNonceApplicator::apply($html, $this->cspService->getNonce());
44 * Fetch our custom HTML head content prepared for use in export formats.
45 * Scripts are stripped to avoid potential issues.
47 public function forExport(): string
49 $content = $this->getSourceContent();
50 $hash = md5($content);
52 return $this->cache->remember('custom-head-export:' . $hash, 86400, function () use ($content) {
53 return HtmlContentFilter::removeScriptsFromHtmlString($content);
58 * Get the original custom head content to use.
60 protected function getSourceContent(): string
62 return setting('app-custom-head', '');