Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 344 lines (299 sloc) 10.664 kb
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
1 <?php
2 /**
3 * DokuWiki StyleSheet creator
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
8
d0a27cb Andreas Gohr don't use fullpath() before initialized
authored
9 if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
10 if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
98bda4f Andreas Gohr fixed multiple gzip/sendfile problems in css and js dispatchers FS#1571
authored
11 if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
12 require_once(DOKU_INC.'inc/init.php');
13
14 // Main (don't run when UNIT test)
15 if(!defined('SIMPLE_TEST')){
16 header('Content-Type: text/css; charset=utf-8');
17 css_out();
18 }
19
20
21 // ---------------------- functions ------------------------------
22
23 /**
24 * Output all needed Styles
25 *
26 * @author Andreas Gohr <andi@splitbrain.org>
27 */
28 function css_out(){
29 global $conf;
30 global $lang;
09edb71 Andreas Gohr use config cascade for userscripts and styles FS#1678
authored
31 global $config_cascade;
32
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
33 $mediatype = 'screen';
c66972f Adrian Heine né Lang Emit less E_NOTICEs and E_STRICTs
adrianheine authored
34 if (isset($_REQUEST['s']) &&
35 in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
36 $mediatype = $_REQUEST['s'];
615960f Tom N Harris use generated CSS for RSS feeds
whoopdedo authored
37 }
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
38
f7589b0 Update character class for template names to include dash
Chris Smith authored
39 $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t']));
124af65 Andreas Gohr allow dynamic passing of template to use for css.php
authored
40 if($tpl){
41 $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/';
42 $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/';
43 }else{
81aca18 Andreas Gohr removed some more occurances of DOKU_TPL*
authored
44 $tplinc = tpl_incdir();
45 $tpldir = tpl_basedir();
124af65 Andreas Gohr allow dynamic passing of template to use for css.php
authored
46 }
47
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
48 // The generated script depends on some dynamic options
6619f42 Adrian Heine né Lang Refactor CSS and JS caching
adrianheine authored
49 $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css');
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
50
519b317 Andreas Gohr Template stylesheets are now defined in sytle.ini
authored
51 // load template styles
52 $tplstyles = array();
124af65 Andreas Gohr allow dynamic passing of template to use for css.php
authored
53 if(@file_exists($tplinc.'style.ini')){
54 $ini = parse_ini_file($tplinc.'style.ini',true);
519b317 Andreas Gohr Template stylesheets are now defined in sytle.ini
authored
55 foreach($ini['stylesheets'] as $file => $mode){
124af65 Andreas Gohr allow dynamic passing of template to use for css.php
authored
56 $tplstyles[$mode][$tplinc.$file] = $tpldir;
519b317 Andreas Gohr Template stylesheets are now defined in sytle.ini
authored
57 }
58 }
59
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
60 // Array of needed files and their web locations, the latter ones
61 // are needed to fix relative paths in the stylesheets
62 $files = array();
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
63 // load core styles
64 $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/';
4357675 Andreas Gohr Updated jQuery-UI, added jQuery updater, load theme in CSS dispatcher
authored
65 // load jQuery-UI theme
bf1ec65 Andreas Gohr another fix for the jQuery-UI theme
authored
66 $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/';
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
67 // load plugin styles
68 $files = array_merge($files, css_pluginstyles($mediatype));
69 // load template styles
70 if (isset($tplstyles[$mediatype])) {
71 $files = array_merge($files, $tplstyles[$mediatype]);
72 }
73 // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
74 if (isset($config_cascade['userstyle']['default'])) {
75 $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
76 }
77 // load user styles
78 if(isset($config_cascade['userstyle'][$mediatype])){
79 $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
80 }
81 // load rtl styles
82 // @todo: this currently adds the rtl styles only to the 'screen' media type
83 // but 'print' and 'all' should also be supported
84 if ($mediatype=='screen') {
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
85 if($lang['direction'] == 'rtl'){
1f5663f Christopher Smith css.php bugfix for unitialised arrays in php5
Chris--S authored
86 if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']);
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
87 }
88 }
89
6619f42 Adrian Heine né Lang Refactor CSS and JS caching
adrianheine authored
90 $cache_files = array_merge(array_keys($files), getConfigFiles('main'));
91 $cache_files[] = $tplinc.'style.ini';
92 $cache_files[] = __FILE__;
ca2b464 Add capability to send pre-compressed js & css files if the browser can ...
Chris Smith authored
93
6619f42 Adrian Heine né Lang Refactor CSS and JS caching
adrianheine authored
94 // check cache age & handle conditional request
95 // This may exit if a cache can be used
96 http_cached($cache->cache,
97 $cache->useCache(array('files' => $cache_files)));
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
98
99 // start output buffering and build the stylesheet
100 ob_start();
101
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
102 // print the default classes for interwiki links and file downloads
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
103 css_interwiki();
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
104 css_filetypes();
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
105
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
106 // load files
107 foreach($files as $file => $location){
108 print css_loadfile($file, $location);
109 }
110
111 // end output buffering and get contents
112 $css = ob_get_contents();
113 ob_end_clean();
114
6e69c1b Andreas Gohr CSS replacement support
authored
115 // apply style replacements
124af65 Andreas Gohr allow dynamic passing of template to use for css.php
authored
116 $css = css_applystyle($css,$tplinc);
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
117
f7d780b Gabriel Birke Preserve @import statements in CSS
gbirke authored
118 // place all @import statements at the top of the file
119 $css = css_moveimports($css);
6e69c1b Andreas Gohr CSS replacement support
authored
120
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
121 // compress whitespace and comments
122 if($conf['compress']){
123 $css = css_compress($css);
124 }
125
809d3ba Andreas Gohr Use data uris for small image files in CSS
authored
126 // embed small images right into the stylesheet
127 if($conf['cssdatauri']){
128 $base = preg_quote(DOKU_BASE,'#');
129 $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css);
130 }
131
6619f42 Adrian Heine né Lang Refactor CSS and JS caching
adrianheine authored
132 http_cached_finish($cache->cache, $css);
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
133 }
134
135 /**
208c021 Andreas Gohr support rtl.css for plugins FS#1114
authored
136 * Does placeholder replacements in the style according to
6e69c1b Andreas Gohr CSS replacement support
authored
137 * the ones defined in a templates style.ini file
138 *
139 * @author Andreas Gohr <andi@splitbrain.org>
140 */
124af65 Andreas Gohr allow dynamic passing of template to use for css.php
authored
141 function css_applystyle($css,$tplinc){
142 if(@file_exists($tplinc.'style.ini')){
143 $ini = parse_ini_file($tplinc.'style.ini',true);
519b317 Andreas Gohr Template stylesheets are now defined in sytle.ini
authored
144 $css = strtr($css,$ini['replacements']);
6e69c1b Andreas Gohr CSS replacement support
authored
145 }
146 return $css;
147 }
148
149 /**
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
150 * Prints classes for interwikilinks
151 *
152 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
153 * $name is the identifier given in the config. All Interwiki links get
154 * an default style with a default icon. If a special icon is available
155 * for an interwiki URL it is set in it's own class. Both classes can be
156 * overwritten in the template or userstyles.
157 *
158 * @author Andreas Gohr <andi@splitbrain.org>
159 */
160 function css_interwiki(){
161
162 // default style
163 echo 'a.interwiki {';
164 echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;';
7b4ea08 marklundeberg Make interwiki links match with every other icon-link: need 1px top and ...
marklundeberg authored
165 echo ' padding: 1px 0px 1px 16px;';
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
166 echo '}';
167
168 // additional styles when icon available
169 $iwlinks = getInterwiki();
170 foreach(array_keys($iwlinks) as $iw){
9d2ddea Andreas Gohr fix for bad characters in interwiki classes
authored
171 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw);
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
172 if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){
9d2ddea Andreas Gohr fix for bad characters in interwiki classes
authored
173 echo "a.iw_$class {";
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
174 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)';
175 echo '}';
176 }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){
9d2ddea Andreas Gohr fix for bad characters in interwiki classes
authored
177 echo "a.iw_$class {";
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
178 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)';
179 echo '}';
180 }
181 }
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
182 }
183
184 /**
185 * Prints classes for file download links
186 *
187 * @author Andreas Gohr <andi@splitbrain.org>
188 */
189 function css_filetypes(){
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
190
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
191 // default style
035e07f Kate Arzamastseva issue #57, function names, params, html fixes
akate authored
192 echo '.mediafile {';
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
193 echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;';
5b77caf Andreas Gohr changes order of CSS loading #746
authored
194 echo ' padding-left: 18px;';
195 echo ' padding-bottom: 1px;';
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
196 echo '}';
197
198 // additional styles when icon available
27bf792 Tom N Harris Use all available icons for file links (FS#1759)
whoopdedo authored
199 // scan directory for all icons
200 $exts = array();
201 if($dh = opendir(DOKU_INC.'lib/images/fileicons')){
202 while(false !== ($file = readdir($dh))){
203 if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){
204 $ext = strtolower($match[1]);
205 $type = '.'.strtolower($match[2]);
206 if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){
207 $exts[$ext] = $type;
208 }
209 }
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
210 }
27bf792 Tom N Harris Use all available icons for file links (FS#1759)
whoopdedo authored
211 closedir($dh);
212 }
213 foreach($exts as $ext=>$type){
214 $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext);
035e07f Kate Arzamastseva issue #57, function names, params, html fixes
akate authored
215 echo ".mf_$class {";
27bf792 Tom N Harris Use all available icons for file links (FS#1759)
whoopdedo authored
216 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')';
217 echo '}';
d15166e Andreas Gohr use classes for file icons like in interwiki links
authored
218 }
1c2d101 Andreas Gohr Interwiki icons are set through CSS classes now
authored
219 }
220
221 /**
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
222 * Loads a given file and fixes relative URLs with the
223 * given location prefix
224 */
225 function css_loadfile($file,$location=''){
226 if(!@file_exists($file)) return '';
227 $css = io_readFile($file);
228 if(!$location) return $css;
229
809d3ba Andreas Gohr Use data uris for small image files in CSS
authored
230 $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css);
231 $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css);
232
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
233 return $css;
234 }
235
809d3ba Andreas Gohr Use data uris for small image files in CSS
authored
236 /**
237 * Converte local image URLs to data URLs if the filesize is small
238 *
239 * Callback for preg_replace_callback
240 */
241 function css_datauri($match){
28f4004 Andreas Gohr Made the maximum embed size for datauris configurable
authored
242 global $conf;
243
809d3ba Andreas Gohr Use data uris for small image files in CSS
authored
244 $pre = unslash($match[1]);
245 $base = unslash($match[2]);
246 $url = unslash($match[3]);
247 $ext = unslash($match[4]);
248
249 $local = DOKU_INC.$url;
250 $size = @filesize($local);
28f4004 Andreas Gohr Made the maximum embed size for datauris configurable
authored
251 if($size && $size < $conf['cssdatauri']){
809d3ba Andreas Gohr Use data uris for small image files in CSS
authored
252 $data = base64_encode(file_get_contents($local));
253 }
254 if($data){
255 $url = 'data:image/'.$ext.';base64,'.$data;
256 }else{
257 $url = $base.$url;
258 }
259 return $pre.$url;
260 }
261
15c394a Andreas Gohr url fixing in CSS compressor fixed #793
authored
262
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
263 /**
264 * Returns a list of possible Plugin Styles (no existance check here)
265 *
266 * @author Andreas Gohr <andi@splitbrain.org>
267 */
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
268 function css_pluginstyles($mediatype='screen'){
208c021 Andreas Gohr support rtl.css for plugins FS#1114
authored
269 global $lang;
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
270 $list = array();
271 $plugins = plugin_list();
272 foreach ($plugins as $p){
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
273 $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/";
274 // alternative for screen.css
275 if ($mediatype=='screen') {
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
276 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
277 }
208c021 Andreas Gohr support rtl.css for plugins FS#1114
authored
278 if($lang['direction'] == 'rtl'){
279 $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/";
280 }
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
281 }
282 return $list;
283 }
284
285 /**
f7d780b Gabriel Birke Preserve @import statements in CSS
gbirke authored
286 * Move all @import statements in a combined stylesheet to the top so they
318cd03 Anika Henke improved css.php and core styles
selfthinker authored
287 * aren't ignored by the browser.
f7d780b Gabriel Birke Preserve @import statements in CSS
gbirke authored
288 *
289 * @author Gabriel Birke <birke@d-scribe.de>
290 */
291 function css_moveimports($css)
292 {
293 if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) {
294 return $css;
295 }
296 $newCss = "";
297 $imports = "";
298 $offset = 0;
299 foreach($matches[0] as $match) {
300 $newCss .= substr($css, $offset, $match[1] - $offset);
301 $imports .= $match[0];
302 $offset = $match[1] + strlen($match[0]);
303 }
304 $newCss .= substr($css, $offset);
305 return $imports.$newCss;
306 }
307
308 /**
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
309 * Very simple CSS optimizer
310 *
311 * @author Andreas Gohr <andi@splitbrain.org>
312 */
313 function css_compress($css){
fd7c2db Andreas Gohr CSS compressor fix
authored
314 //strip comments through a callback
315 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css);
316
247c1c5 Andreas Gohr strip (incorrect but common) one line comments in CSS compressor
authored
317 //strip (incorrect but common) one line comments
fd7c2db Andreas Gohr CSS compressor fix
authored
318 $css = preg_replace('/(?<!:)\/\/.*$/m','',$css);
247c1c5 Andreas Gohr strip (incorrect but common) one line comments in CSS compressor
authored
319
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
320 // strip whitespaces
321 $css = preg_replace('![\r\n\t ]+!',' ',$css);
f537958 Christopher Smith FS#2317 fix CSS compress for generic pseudo classes/pseudo elements
Chris--S authored
322 $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css);
323 $css = preg_replace('/ ?: /',':',$css);
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
324
325 // shorten colors
326 $css = preg_replace("/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3/", "#\\1\\2\\3",$css);
327
328 return $css;
329 }
330
c00aef7 Andreas Gohr fix for comment stripping in CSS compression
authored
331 /**
332 * Callback for css_compress()
333 *
334 * Keeps short comments (< 5 chars) to maintain typical browser hacks
335 *
336 * @author Andreas Gohr <andi@splitbrain.org>
337 */
338 function css_comment_cb($matches){
339 if(strlen($matches[2]) > 4) return '';
340 return $matches[0];
341 }
78a6aeb Andreas Gohr More work on Javascript and CSS dispatchers
authored
342
e3776c0 Michael Hamann Remove enc=utf-8 in VIM modeline as it is not allowed in VIM 7.3
michitux authored
343 //Setup VIM: ex: et ts=4 :
Something went wrong with that request. Please try again.