BlueGit

timfa 66wiki

Files

66wiki / web / templateCssLinker.php

templateCssLinker.php

Initial commit
timfa Tim · pushed by timfa · 11 months ago
dd360904 History
templateCssLinker.php 1.2 KiB Download Raw
1<?php
2
3/**
4 * Echo <link rel="stylesheet"> tags for every *.css file in the templates folder.
5 *
6 * @param string $templatesDirFs Filesystem path to templates dir, e.g. __DIR__ . '/templates'
7 * @param string $templatesDirUrl URL path to templates dir, e.g. '/templates'
8 */
9function echoTemplateCssLinks(string $templatesDirFs, string $templatesDirUrl = '/templates'): void
10{
11 $templatesDirFs = rtrim($templatesDirFs, DIRECTORY_SEPARATOR);
12 $templatesDirUrl = rtrim($templatesDirUrl, '/');
13
14 if (!is_dir($templatesDirFs)) {
15 return;
16 }
17
18 $files = glob($templatesDirFs . DIRECTORY_SEPARATOR . '*.css');
19 if ($files === false) {
20 return;
21 }
22
23 // Deterministic order
24 sort($files, SORT_NATURAL | SORT_FLAG_CASE);
25
26 foreach ($files as $filePath) {
27 $fileName = basename($filePath);
28
29 // Basic hardening: only allow .css files with sane filenames
30 if (!preg_match('/^[a-zA-Z0-9_.-]+\.css$/', $fileName)) {
31 continue;
32 }
33
34 $href = $templatesDirUrl . '/' . rawurlencode($fileName);
35 echo '<link rel="stylesheet" href="' . htmlspecialchars($href, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '">' . "\n";
36 }
37}