forked from smogon/pokemon-showdown-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomcss.php
More file actions
59 lines (52 loc) · 1.95 KB
/
customcss.php
File metadata and controls
59 lines (52 loc) · 1.95 KB
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
<?php
include '../pokemonshowdown.com/config/servers.inc.php';
header('Content-type: text/css');
$server = @$_REQUEST['server'];
$customcssuri = @$PokemonServers[$server]['customcss'];
if (empty($customcssuri)) {
die();
}
// No need to sanitise $server because it should be safe already.
$cssfile = '../pokemonshowdown.com/config/customcss/' . $server;
$lastmodified = @filemtime($cssfile);
$timenow = time();
$expiration = ($lastmodified ? $lastmodified : $timenow) + 3600;
header('Expires: ' . gmdate('D, d M Y H:i:s T', $expiration));
if (!isset($_REQUEST['invalidate']) && $lastmodified && (($timenow - $lastmodified) < 3600)) {
// Don't check for modifications more than once an hour.
readfile($cssfile);
die();
}
$curl = curl_init($customcssuri);
if ($lastmodified && !isset($_REQUEST['invalidate'])) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'If-Modified-Since: ' . gmdate('D, d M Y H:i:s T', $lastmodified)
));
}
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curlret = curl_exec($curl);
if ($curlret) {
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code === 200) {
// Sanitise the CSS.
require '../pokemonshowdown.com/lib/htmlpurifier/HTMLPurifier.auto.php';
require '../pokemonshowdown.com/lib/csstidy/class.csstidy.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('Filter.ExtractStyleBlocks', true);
$config->set('CSS.Proprietary', true);
$purifier = new HTMLPurifier($config);
$level = error_reporting(E_ALL & ~E_STRICT);
$html = $purifier->purify('<style>' . $curlret . '</style>');
error_reporting($level);
list($outputcss) = $purifier->context->get('StyleBlocks');
file_put_contents($cssfile, $outputcss);
echo $outputcss;
} else {
// Either no modifications (status: 304) or an error condition.
readfile($cssfile);
}
touch($cssfile, $timenow); // Don't check again for an hour.
}
curl_close($curl);