forked from Phorum/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css.php
368 lines (333 loc) · 13.2 KB
/
css.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011 Phorum Development Team //
// http://www.phorum.org //
// //
// This program is free software. You can redistribute it and/or modify //
// it under the terms of either the current Phorum License (viewable at //
// phorum.org) or the Phorum License that was distributed with this file //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY, without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
// //
// You should have received a copy of the Phorum License //
// along with this program. //
// //
////////////////////////////////////////////////////////////////////////////////
define('phorum_page','css');
require_once './common.php';
// Set to FALSE to disable CSS compression.
define('PHORUM_COMPRESS_CSS', TRUE);
// Argument 1 should be the name of the css template to load.
if (!empty($PHORUM["args"]["1"])){
$css = basename((string)$PHORUM["args"]["1"]);
} else {
trigger_error("Missing argument", E_USER_ERROR);
exit(1);
}
// let it only process css templates
if (substr($css,0,3) != 'css') {
trigger_error("Wrong template", E_USER_ERROR);
exit(1);
}
// So we can use {URL->HTTP_PATH} in the templates.
phorum_build_common_urls();
/**
* [hook]
* css_register
*
* [description]
* Modules can provide extra CSS data for CSS code that is retrieved
* through the css.php script. Extra CSS definitions can be added to
* the start and to the end of the base CSS code. Modules that make use
* of this facility should register the additional CSS code using
* this hook.
*
* [category]
* Templating
*
* [when]
* At the start of the css.php script.
*
* [input]
* An array, containing the following fields:
* <ul>
* <li><b>css</b><br>
* The name of the css file that was requested for the css.php
* script. Phorum requests either "css" or "css_print".
* The module can use this parameter to decide whether
* CSS code has to be registered or not.</li>
* <li><b>register</b><br>
* An array of registrations, filled by the modules. Modules
* can register their CSS code for inclusion in the base CSS
* file by adding a registration to this array. A registration
* is an array, containing the following fields:
* <ul>
* <li><b>module</b><br>
* The name of the module that adds the registration.
* </li>
* <li><b>where</b><br>
* This field determines whether the CSS data is added
* before or after the base CSS code. The value for this field
* is either "before" or "after".
* </li>
* <li><b>source</b><br>
* Specifies the source of the CSS data. This can be one of:
* <ul>
* <li><b>file(<path to filename>)</b><br>
* For including a static CSS file. The path should be
* absolute or relative to the Phorum install directory,
* e.g. "<literal>file(mods/foobar/baz.css)</literal>".
* Because this file is loaded using a PHP include call,
* it is possible to include PHP code in this file. Mind that
* this code is stored interpreted in the cache.</li>
* <li><b>template(<template name>)</b><br>
* For including a Phorum template,
* e.g. "<literal>template(foobar::baz)</literal>"</li>
* <li><b>function(<function name>)</b><br>
* For calling a function to retrieve the CSS code,
* e.g. "<literal>function(mod_foobar_get_css)</literal>"</li>
* </ul>
* </li>
* <li><b>cache_key</b><br>
* To make caching of the generated CSS data
* possible, the module should provide the css.php script
* a cache key using this field. This cache key needs to
* change if the module will provide different CSS data.<br>
* <br>
* Note: in case "file" or "template" is used as the source,
* you are allowed to omit the cache_key. In that case, the
* modification time of the involved file(s) will be used as
* the cache key.<br>
* <br>
* It is okay for the module to provide multiple cache keys
* for different situations (e.g. if the CSS code depends on
* a group or so). Keep in mind though that for each different
* cache key, a separate cache file is generated. If you are
* generating different CSS code per user or so, then it might
* be better to add the CSS code differently (e.g. through a
* custom CSS generating script or by adding the CSS code to
* the $PHORUM['DATA']['HEAD_DATA'] variable. Also, do not use
* this to only add CSS code to certain phorum pages. Since
* the resulting CSS data is cached, it is no problem if you
* add the CSS data for your module to the CSS code for
* every page.
* </li>
* </ul>
* </li>
* </ul>
*
* [output]
* The same array as the one that was used for the hook call
* arguments, possibly with the "register" field updated.
* A module can add multiple registrations to the register array.
*/
$module_registrations = array();
if (isset($PHORUM['hooks']['css_register'])) {
$res = phorum_api_hook('css_register', array(
'css' => $css,
'register' => $module_registrations)
);
$module_registrations = $res['register'];
}
// We load the parsed template into memory. This will refresh
// the cached template file if required. This is the easiest
// way to make this work correctly for nested template files.
ob_start();
include phorum_api_template($css);
$base = ob_get_contents();
ob_end_clean();
// Find the modification time for the css file and the settings file.
list ($css, $css_php, $css_tpl) = phorum_api_template_resolve($css);
list ($d, $settings_php, $settings_tpl) = phorum_api_template_resolve('settings');
$css_t = @filemtime($css_php);
$settings_t = @filemtime($settings_php);
// Generate the cache key. While adding cache keys for the module
// registrations, we also check the validity of the registration data.
$cache_key = $PHORUM['template'] .'|'.
$css .'|'.
$css_t .'|'.
$settings_t;
foreach ($module_registrations as $id => $r)
{
if (!isset($r['module'])) {
trigger_error(
"css_register hook: module registration error: " .
"the \"module\" field was not set."
);
exit(1);
}
if (!isset($r['source'])) {
trigger_error(
"css_register hook: module registration error: " .
"the \"source\" field was not set."
);
exit(1);
}
if (!isset($r['where'])) {
trigger_error(
"css_register hook: module registration error: " .
"the \"where\" field was not set."
);
exit(1);
}
if ($r['where'] != 'before' && $r['where'] != 'after') {
trigger_error(
"css_register hook: module registration error: " .
"illegal \"where\" field value\"{$r['where']}\"."
);
exit(1);
}
if (preg_match('/^(file|template|function)\((.+)\)$/', $r['source'], $m))
{
$module_registrations[$id]['type'] = $m[1];
$module_registrations[$id]['source'] = $m[2];
switch ($m[1])
{
case "file":
if (!isset($r['cache_key'])) {
$mtime = @filemtime($m[2]);
$r['cache_key'] = $mtime;
$module_registrations[$id]['cache_key'] = $mtime;
}
break;
case "template":
// We load the parsed template into memory. This will refresh
// the cached template file if required. This is the easiest
// way to make this work correctly for nested template files.
ob_start();
include phorum_api_template($m[2]);
$module_registrations[$id]['content'] = ob_get_contents();
ob_end_clean();
// We use the mtime of the compiled template as the cache
// key if no specific cache key was set.
if (!isset($r['cache_key'])) {
list ($m[2], $php, $tpl) = phorum_api_template_resolve($m[2]);
$mtime = @filemtime($php);
$r['cache_key'] = $mtime;
$module_registrations[$id]['cache_key'] = $mtime;
}
break;
case "function":
if (!isset($r['cache_key'])) {
trigger_error(
"css_register hook: module registration error: " .
"\"cache_key\" field missing for source " .
"\"{$r['source']}\" in module \"{$r['module']}\"."
);
exit(1);
}
break;
}
} else {
trigger_error(
"css_register hook: module registration error: " .
"illegal format for source definition \"{$r['source']}\" " .
"in module \"{$r['module']}\"."
);
exit(1);
}
$cache_key .= '|' . $r['module'] . ':' . $r['cache_key'];
}
$content = NULL;
$cache_time = 0;
$cache_key .= '|' . (PHORUM_COMPRESS_CSS ? 'compress' : 'nocompress');
$cache_key = md5($cache_key);
if (!empty($PHORUM['cache_css'])) {
$cache_data = phorum_cache_get('css', $cache_key);
if ($cache_data !== null) {
list ($cache_time, $content) = $cache_data;
}
}
// Create the cache file if it does not exist or if caching is disabled.
if ($content === null)
{
$before = '';
$after = '';
foreach ($module_registrations as $id => $r)
{
$add = "/* Added by module \"{$r['module']}\", " .
"{$r['type']} \"{$r['source']}\" */\n";
switch ($r['type'])
{
case "file":
ob_start();
include $r['source'];
$add .= ob_get_contents();
ob_end_clean();
break;
case "template":
$add .= $r['content'];
break;
case "function":
$add .= call_user_func($r['source']);
break;
}
if ($r['where'] == 'before') {
$before .= ($before == '') ? $add : "\n\n$add";
} else {
$after .= "\n\n$add";
}
}
$content = "$before\n$base\n$after";
// Compress the CSS code.
if (PHORUM_COMPRESS_CSS)
{
$content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $content);
$content = str_replace(
array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '),
'', $content
);
}
/**
* [hook]
* css_filter
*
* [availability]
* Phorum 5 >= 5.2.11
*
* [description]
* This hook can be used to apply a filter to the Phorum CSS
* code. This can for example be used for compressing or cleaning
* up the CSS.
*
* [category]
* Templating
*
* [when]
* Right after the css.php script has generated a new
* CSS file and right before storing that file in the cache.
* The filter hook will not be run for every request to
* css.php, but only in case the CSS code has
* to be refreshed.
*
* [input]
* The generated CSS code.
*
* [output]
* The filtered CSS code.
*/
if (isset($PHORUM['hooks']['css_filter'])) {
$content = phorum_api_hook('css_filter', $content);
}
if (!empty($PHORUM['cache_css'])) {
$cache_time = time();
phorum_cache_put('css',$cache_key,array($cache_time,$content),86400);
}
}
// Find the modification time for the cache file.
$last_modified = $cache_time;
// Check if a If-Modified-Since header is in the request. If yes, then
// check if the CSS code has changed, based on the filemtime() data from
// above. If nothing changed, then we return a 304 header, to tell the
// browser to use the cached data.
phorum_api_output_last_modify_time($last_modified);
// Send the CSS to the browser.
header("Content-Type: text/css");
echo $content;
// Exit here explicitly for not giving back control to portable and
// embedded Phorum setups.
exit(0);
?>