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 9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 11require_once(DOKU_INC.'inc/init.php'); 12require_once(DOKU_INC.'inc/pageutils.php'); 13require_once(DOKU_INC.'inc/io.php'); 14require_once(DOKU_INC.'inc/confutils.php'); 15 16// Main (don't run when UNIT test) 17if(!defined('SIMPLE_TEST')){ 18 header('Content-Type: text/css; charset=utf-8'); 19 css_out(); 20} 21 22 23// ---------------------- functions ------------------------------ 24 25/** 26 * Output all needed Styles 27 * 28 * @author Andreas Gohr <andi@splitbrain.org> 29 */ 30function css_out(){ 31 global $conf; 32 global $lang; 33 switch ($_REQUEST['s']) { 34 case 'all': 35 case 'print': 36 case 'feed': 37 $style = $_REQUEST['s']; 38 break; 39 default: 40 $style = ''; 41 break; 42 } 43 44 $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t'])); 45 if($tpl){ 46 $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; 47 $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; 48 }else{ 49 $tplinc = DOKU_TPLINC; 50 $tpldir = DOKU_TPL; 51 } 52 53 // The generated script depends on some dynamic options 54 $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$style,'.css'); 55 56 // load template styles 57 $tplstyles = array(); 58 if(@file_exists($tplinc.'style.ini')){ 59 $ini = parse_ini_file($tplinc.'style.ini',true); 60 foreach($ini['stylesheets'] as $file => $mode){ 61 $tplstyles[$mode][$tplinc.$file] = $tpldir; 62 } 63 } 64 65 // Array of needed files and their web locations, the latter ones 66 // are needed to fix relative paths in the stylesheets 67 $files = array(); 68 //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']); 69 if(!empty($style)){ 70 $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/'; 71 // load plugin, template, user styles 72 $files = array_merge($files, css_pluginstyles($style)); 73 if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]); 74 $files[DOKU_CONF.'user'.$style.'.css'] = DOKU_BASE; 75 }else{ 76 $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/'; 77 if($conf['spellchecker']){ 78 $files[DOKU_INC.'lib/styles/spellcheck.css'] = DOKU_BASE.'lib/styles/'; 79 } 80 // load plugin, template, user styles 81 $files = array_merge($files, css_pluginstyles('screen')); 82 if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']); 83 if($lang['direction'] == 'rtl'){ 84 if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']); 85 } 86 $files[DOKU_CONF.'userstyle.css'] = DOKU_BASE; 87 } 88 89 // check cache age & handle conditional request 90 header('Cache-Control: public, max-age=3600'); 91 header('Pragma: public'); 92 if(css_cacheok($cache,array_keys($files),$tplinc)){ 93 http_conditionalRequest(filemtime($cache)); 94 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 95 96 // finally send output 97 if (http_accepts_gzip() && http_gzip_valid($cache)) { 98 header('Vary: Accept-Encoding'); 99 header('Content-Encoding: gzip'); 100 if (!http_sendfile($cache.'.gz')) readfile($cache.".gz"); 101# } else if (http_accepts_deflate()) { 102# header('Vary: Accept-Encoding'); 103# header('Content-Encoding: deflate'); 104# readfile($cache.".zip"); 105 } else { 106 if (!http_sendfile($cache)) readfile($cache); 107 } 108 109 return; 110 } else { 111 http_conditionalRequest(time()); 112 } 113 114 // start output buffering and build the stylesheet 115 ob_start(); 116 117 // print the default classes for interwiki links and file downloads 118 css_interwiki(); 119 css_filetypes(); 120 121 // load files 122 foreach($files as $file => $location){ 123 print css_loadfile($file, $location); 124 } 125 126 // end output buffering and get contents 127 $css = ob_get_contents(); 128 ob_end_clean(); 129 130 // apply style replacements 131 $css = css_applystyle($css,$tplinc); 132 133 // compress whitespace and comments 134 if($conf['compress']){ 135 $css = css_compress($css); 136 } 137 138 // save cache file 139 io_saveFile($cache,$css); 140 copy($cache,"compress.zlib://$cache.gz"); 141 142 // finally send output 143 if (http_accepts_gzip()) { 144 header('Vary: Accept-Encoding'); 145 header('Content-Encoding: gzip'); 146 print gzencode($css,9,FORCE_GZIP); 147# } else if (http_accepts_deflate()) { 148# header('Vary: Accept-Encoding'); 149# header('Content-Encoding: deflate'); 150# print gzencode($css,9,FORCE_DEFLATE); 151 } else { 152 print $css; 153 } 154} 155 156/** 157 * Checks if a CSS Cache file still is valid 158 * 159 * @author Andreas Gohr <andi@splitbrain.org> 160 */ 161function css_cacheok($cache,$files,$tplinc){ 162 global $config_cascade; 163 164 if($_REQUEST['purge']) return false; //support purge request 165 166 $ctime = @filemtime($cache); 167 if(!$ctime) return false; //There is no cache 168 169 // some additional files to check 170 $files = array_merge($files, getConfigFiles('main')); 171 $files[] = $tplinc.'style.ini'; 172 $files[] = __FILE__; 173 174 // now walk the files 175 foreach($files as $file){ 176 if(@filemtime($file) > $ctime){ 177 return false; 178 } 179 } 180 return true; 181} 182 183/** 184 * Does placeholder replacements in the style according to 185 * the ones defined in a templates style.ini file 186 * 187 * @author Andreas Gohr <andi@splitbrain.org> 188 */ 189function css_applystyle($css,$tplinc){ 190 if(@file_exists($tplinc.'style.ini')){ 191 $ini = parse_ini_file($tplinc.'style.ini',true); 192 $css = strtr($css,$ini['replacements']); 193 } 194 return $css; 195} 196 197/** 198 * Prints classes for interwikilinks 199 * 200 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 201 * $name is the identifier given in the config. All Interwiki links get 202 * an default style with a default icon. If a special icon is available 203 * for an interwiki URL it is set in it's own class. Both classes can be 204 * overwritten in the template or userstyles. 205 * 206 * @author Andreas Gohr <andi@splitbrain.org> 207 */ 208function css_interwiki(){ 209 210 // default style 211 echo 'a.interwiki {'; 212 echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 213 echo ' padding-left: 16px;'; 214 echo '}'; 215 216 // additional styles when icon available 217 $iwlinks = getInterwiki(); 218 foreach(array_keys($iwlinks) as $iw){ 219 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 220 if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 221 echo "a.iw_$class {"; 222 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 223 echo '}'; 224 }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 225 echo "a.iw_$class {"; 226 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 227 echo '}'; 228 } 229 } 230} 231 232/** 233 * Prints classes for file download links 234 * 235 * @author Andreas Gohr <andi@splitbrain.org> 236 */ 237function css_filetypes(){ 238 239 // default style 240 echo 'a.mediafile {'; 241 echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 242 echo ' padding-left: 18px;'; 243 echo ' padding-bottom: 1px;'; 244 echo '}'; 245 246 // additional styles when icon available 247 $mimes = getMimeTypes(); 248 foreach(array_keys($mimes) as $mime){ 249 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$mime); 250 if(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.png')){ 251 echo "a.mf_$class {"; 252 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.png)'; 253 echo '}'; 254 }elseif(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.gif')){ 255 echo "a.mf_$class {"; 256 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.gif)'; 257 echo '}'; 258 } 259 } 260} 261 262/** 263 * Loads a given file and fixes relative URLs with the 264 * given location prefix 265 */ 266function css_loadfile($file,$location=''){ 267 if(!@file_exists($file)) return ''; 268 $css = io_readFile($file); 269 if(!$location) return $css; 270 271 $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css); 272 return $css; 273} 274 275 276/** 277 * Returns a list of possible Plugin Styles (no existance check here) 278 * 279 * @author Andreas Gohr <andi@splitbrain.org> 280 */ 281function css_pluginstyles($mode='screen'){ 282 global $lang; 283 $list = array(); 284 $plugins = plugin_list(); 285 foreach ($plugins as $p){ 286 if($mode == 'all'){ 287 $list[DOKU_PLUGIN."$p/all.css"] = DOKU_BASE."lib/plugins/$p/"; 288 }elseif($mode == 'print'){ 289 $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/"; 290 }elseif($mode == 'feed'){ 291 $list[DOKU_PLUGIN."$p/feed.css"] = DOKU_BASE."lib/plugins/$p/"; 292 }else{ 293 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 294 $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/"; 295 } 296 if($lang['direction'] == 'rtl'){ 297 $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/"; 298 } 299 } 300 return $list; 301} 302 303/** 304 * Very simple CSS optimizer 305 * 306 * @author Andreas Gohr <andi@splitbrain.org> 307 */ 308function css_compress($css){ 309 //strip comments through a callback 310 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 311 312 //strip (incorrect but common) one line comments 313 $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 314 315 // strip whitespaces 316 $css = preg_replace('![\r\n\t ]+!',' ',$css); 317 $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css); 318 319 // shorten colors 320 $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); 321 322 return $css; 323} 324 325/** 326 * Callback for css_compress() 327 * 328 * Keeps short comments (< 5 chars) to maintain typical browser hacks 329 * 330 * @author Andreas Gohr <andi@splitbrain.org> 331 */ 332function css_comment_cb($matches){ 333 if(strlen($matches[2]) > 4) return ''; 334 return $matches[0]; 335} 336 337//Setup VIM: ex: et ts=4 enc=utf-8 : 338?> 339