|
28 | 28 | // phpcs:disable moodle.Files.RequireLogin.Missing |
29 | 29 | require_once('../../../config.php'); |
30 | 30 |
|
31 | | -@header('Content-Disposition: inline; filename="styles.php"'); |
32 | | -@header('Content-Type: text/css; charset=utf-8'); |
| 31 | +$revision = optional_param('revision', 0, PARAM_ALPHANUM); |
33 | 32 |
|
34 | 33 | $withunits = ['font-size', 'line-height', 'margin', 'padding', 'border-width', 'border-radius']; |
35 | 34 | $csscontent = ''; |
|
133 | 132 | } |
134 | 133 | } |
135 | 134 |
|
| 135 | +$csstabstyles = trim($csstabstyles); |
| 136 | +$etag = md5($csstabstyles . $revision); |
| 137 | + |
| 138 | +// ETag validation: Return 304 if client has the current version. This will |
| 139 | +// preserve the existing cache for $cache_lifetime. |
| 140 | +if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { |
| 141 | + $clientetag = trim($_SERVER['HTTP_IF_NONE_MATCH'], '"'); |
| 142 | + if ($clientetag === $etag) { |
| 143 | + header('HTTP/1.1 304 Not Modified'); |
| 144 | + header('ETag: "' . $etag . '"'); |
| 145 | + exit; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// Return empty response for edge cases (no styles configured & direct URL access). |
| 150 | +if (empty($csstabstyles)) { |
| 151 | + header('HTTP/1.1 304 Not Modified'); |
| 152 | + header('Content-Length: 0'); |
| 153 | + exit; |
| 154 | +} |
| 155 | + |
| 156 | +// Cache for 1 year, this is safe due to cache busting via revision param. |
| 157 | +$cache_lifetime = 31536000; |
| 158 | +header('Cache-Control: public, max-age=' . $cache_lifetime . ', immutable'); |
| 159 | +header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_lifetime) . ' GMT'); |
| 160 | +header('ETag: "' . $etag . '"'); |
| 161 | + |
| 162 | +// Content headers. |
| 163 | +header('Content-Length: ' . strlen($csstabstyles)); |
| 164 | +header('Content-Disposition: inline; filename="styles.php"'); |
| 165 | +header('Content-Type: text/css; charset=utf-8'); |
| 166 | +header('X-Content-Type-Options: nosniff'); |
| 167 | + |
136 | 168 | echo $csstabstyles; |
0 commit comments