-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstylesheet.php
161 lines (134 loc) · 4.28 KB
/
stylesheet.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
<?php
global $CMS_INSTALL_PAGE;
global $CMS_STYLESHEET;
$CMS_STYLESHEET = 1;
// Parse pretty URLS
if (!isset($_SERVER['REQUEST_URI']) && isset($_SERVER['QUERY_STRING']))
{
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
}
$url = substr($_SERVER['REQUEST_URI'],strlen($_SERVER['PHP_SELF']));
$url = rtrim($url,'/');
$matches = array();
if( preg_match('+^/[0-9]*/.*?$+',$url,$matches) )
{
$tmp = substr($url,1);
list($_GET['cssid'],$_GET['mediatype']) = explode('/',$tmp);
}
else if( preg_match('+^/[0-9]*$+',$url,$matches) )
{
$_GET['cssid'] = (int)substr($url,1);
}
require('fileloc.php');
require(CONFIG_FILE_LOCATION);
require(dirname(__FILE__).DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'misc.functions.php');
$mediatype = '';
if (isset($_GET["mediatype"])) $mediatype = $_GET["mediatype"];
$cssid = '';
if (isset($_GET['cssid'])) $cssid = $_GET['cssid'];
$name = '';
if (isset($_GET['name'])) $name = $_GET['name'];
$stripbackground = false;
if (isset($_GET["stripbackground"])) $stripbackground = true;
if ($name == '' && $cssid == '') return '';
// Get the hash filename
$hashfile = TMP_CACHE_LOCATION . DIRECTORY_SEPARATOR . 'csshash.dat';
// Get the cache
$hashmtime = @filemtime($hashfile);
$hash = csscache_csvfile_to_hash($hashfile);
// Get the etag header if set
//print_r( $_SERVER ); echo "\n";
$etag = '';
if( function_exists('getallheaders') )
{
$headers = getallheaders();
if (isset($headers['If-None-Match']) )
{
$etag = trim($headers['If-None-Match'],'"');
}
}
else if( isset($_SERVER['HTTP_IF_NONE_MATCH']) )
{
$etag = trim($_SERVER['HTTP_IF_NONE_MATCH']);
$etag = trim($etag,'"');
}
//echo "DEBUG: cssid = $cssid, hashval = \"{$hash[$cssid]}\" etag = \"$etag\" \n";
//echo "DEBUG: ".strcmp($hash[$cssid],$etag)."\n";
//if( $hash[$cssid] != $etag ) die('uhoh');
if( isset($hash[$cssid]) && strcmp($hash[$cssid],$etag) == 0 &&
$config['debug'] != true )
{
// we have a value
// and it's fine
// just have to output a 304
header('Etag: "'.$etag.'"');
header('HTTP/1.1 304 Not Modified');
exit;
}
//
// Either we don't have a value for this cache
// or the hash is out of date
// so get the styesheets,
//
// connect to the database
require(cms_join_path(dirname(__FILE__),'include.php'));
$db = cmsms()->GetDb();
// extract the stylesheet(s)
$sql="SELECT css_text, css_name FROM ".$config['db_prefix']."css WHERE css_id = ".$db->qstr($cssid);
$row = $db->GetRow($sql);
// calculate the new etag
$etag = md5($row['css_text']);
// update the hash cache
$hash[$cssid] = $etag;
csscache_hash_to_csvfile($hashfile,$hash);
// add a comment at the start
$css = "/* Start of CMSMS style sheet '{$row['css_name']}' */\n{$row['css_text']}\n/* End of '{$row['css_name']}' */\n\n";
// set encoding
$encoding = '';
if ($config['admin_encoding'] != '')
$encoding = $config['admin_encoding'];
elseif ($config['default_encoding'] != '')
$encoding = $config['default_encoding'];
else
$encoding = 'UTF-8';
//
// Begin output
//
// postprocess
if ($stripbackground)
{
#$css = preg_replace('/(\w*?background-color.*?\:\w*?).*?(;.*?)/', '', $css);
$css = preg_replace('/(\w*?background-color.*?\:\w*?).*?(;.*?)/', '\\1transparent\\2', $css);
$css = preg_replace('/(\w*?background-image.*?\:\w*?).*?(;.*?)/', '', $css);
}
if( !isset($CMS_INSTALL_PAGE) && !isset($CMS_ADMIN_PAGE) )
{
if( !class_exists('Events') )
{
$fn = cms_join_path($config['root_path'],'lib','classes','class.events.inc.php');
include($fn);
}
$parms = array();
$parms['content'] =& $css;
Events::SendEvent('Core','ContentStylesheet',$parms);
}
if( isset($config['output_compression']) && ($config['output_compression']) && ($config['debug'] != true) )
{
@ob_start('ob_gzhandler');
}
$max_age = (int)get_site_preference('css_max_age',0);
header("Content-Type: text/css; charset=$encoding");
$datestr = gmdate('D, d M Y H:i:s',$hashmtime).' GMT';
header("Last-Modified: ".$datestr);
if( $max_age > 0 )
{
$datestr = gmdate('D, d M Y H:i:s',$hashmtime+$max_age).' GMT';
header("Expires: ".$datestr);
header("Cache-Control: must-revalidate");
// no caching?
//header("Cache-Control: max-age=$max_age, s-max-age=$max_age, must-revalidate");
}
header('Etag: "'.$etag.'"');
echo $css;
// EOF
?>