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);
39 return HtmlNonceApplicator::apply($html, $this->cspService->getNonce());
43 * Fetch our custom HTML head content prepared for use in export formats.
44 * Scripts are stripped to avoid potential issues.
46 public function forExport(): string
48 $content = $this->getSourceContent();
49 $hash = md5($content);
50 return $this->cache->remember('custom-head-export:' . $hash, 86400, function() use ($content) {
51 return HtmlContentFilter::removeScripts($content);
56 * Get the original custom head content to use.
58 protected function getSourceContent(): string
60 return setting('app-custom-head', '');