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) 11if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here 12require_once(DOKU_INC.'inc/init.php'); 13 14// Main (don't run when UNIT test) 15if(!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 */ 28function css_out(){ 29 global $conf; 30 global $lang; 31 global $config_cascade; 32 33 $mediatype = 'screen'; 34 if (isset($_REQUEST['s']) && 35 in_array($_REQUEST['s'], array('all', 'print', 'feed'))) { 36 $mediatype = $_REQUEST['s']; 37 } 38 39 $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t'])); 40 if($tpl){ 41 $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; 42 $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; 43 }else{ 44 $tplinc = DOKU_TPLINC; 45 $tpldir = DOKU_TPL; 46 } 47 48 // The generated script depends on some dynamic options 49 $cache = getCacheName('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css'); 50 51 // load template styles 52 $tplstyles = array(); 53 if(@file_exists($tplinc.'style.ini')){ 54 $ini = parse_ini_file($tplinc.'style.ini',true); 55 foreach($ini['stylesheets'] as $file => $mode){ 56 $tplstyles[$mode][$tplinc.$file] = $tpldir; 57 } 58 } 59 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(); 63 // load core styles 64 $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 65 // load jQuery-UI theme 66 $files[DOKU_INC.'lib/js/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/js/jquery/jquery-ui-theme/'; 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') { 85 if($lang['direction'] == 'rtl'){ 86 if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']); 87 } 88 } 89 90 // check cache age & handle conditional request 91 header('Cache-Control: public, max-age=3600'); 92 header('Pragma: public'); 93 if(css_cacheok($cache,array_keys($files),$tplinc)){ 94 http_conditionalRequest(filemtime($cache)); 95 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 96 97 // finally send output 98 if ($conf['gzip_output'] && http_gzip_valid($cache)) { 99 header('Vary: Accept-Encoding'); 100 header('Content-Encoding: gzip'); 101 readfile($cache.".gz"); 102 } else { 103 if (!http_sendfile($cache)) readfile($cache); 104 } 105 106 return; 107 } else { 108 http_conditionalRequest(time()); 109 } 110 111 // start output buffering and build the stylesheet 112 ob_start(); 113 114 // print the default classes for interwiki links and file downloads 115 css_interwiki(); 116 css_filetypes(); 117 118 // load files 119 foreach($files as $file => $location){ 120 print css_loadfile($file, $location); 121 } 122 123 // end output buffering and get contents 124 $css = ob_get_contents(); 125 ob_end_clean(); 126 127 // apply style replacements 128 $css = css_applystyle($css,$tplinc); 129 130 // place all @import statements at the top of the file 131 $css = css_moveimports($css); 132 133 // compress whitespace and comments 134 if($conf['compress']){ 135 $css = css_compress($css); 136 } 137 138 // embed small images right into the stylesheet 139 if($conf['cssdatauri']){ 140 $base = preg_quote(DOKU_BASE,'#'); 141 $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css); 142 } 143 144 // save cache file 145 io_saveFile($cache,$css); 146 if(function_exists('gzopen')) io_saveFile("$cache.gz",$css); 147 148 // finally send output 149 if ($conf['gzip_output']) { 150 header('Vary: Accept-Encoding'); 151 header('Content-Encoding: gzip'); 152 print gzencode($css,9,FORCE_GZIP); 153 } else { 154 print $css; 155 } 156} 157 158/** 159 * Checks if a CSS Cache file still is valid 160 * 161 * @author Andreas Gohr <andi@splitbrain.org> 162 */ 163function css_cacheok($cache,$files,$tplinc){ 164 global $config_cascade; 165 166 if(isset($_REQUEST['purge'])) return false; //support purge request 167 168 $ctime = @filemtime($cache); 169 if(!$ctime) return false; //There is no cache 170 171 // some additional files to check 172 $files = array_merge($files, getConfigFiles('main')); 173 $files[] = $tplinc.'style.ini'; 174 $files[] = __FILE__; 175 176 // now walk the files 177 foreach($files as $file){ 178 if(@filemtime($file) > $ctime){ 179 return false; 180 } 181 } 182 return true; 183} 184 185/** 186 * Does placeholder replacements in the style according to 187 * the ones defined in a templates style.ini file 188 * 189 * @author Andreas Gohr <andi@splitbrain.org> 190 */ 191function css_applystyle($css,$tplinc){ 192 if(@file_exists($tplinc.'style.ini')){ 193 $ini = parse_ini_file($tplinc.'style.ini',true); 194 $css = strtr($css,$ini['replacements']); 195 } 196 return $css; 197} 198 199/** 200 * Prints classes for interwikilinks 201 * 202 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 203 * $name is the identifier given in the config. All Interwiki links get 204 * an default style with a default icon. If a special icon is available 205 * for an interwiki URL it is set in it's own class. Both classes can be 206 * overwritten in the template or userstyles. 207 * 208 * @author Andreas Gohr <andi@splitbrain.org> 209 */ 210function css_interwiki(){ 211 212 // default style 213 echo 'a.interwiki {'; 214 echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 215 echo ' padding: 1px 0px 1px 16px;'; 216 echo '}'; 217 218 // additional styles when icon available 219 $iwlinks = getInterwiki(); 220 foreach(array_keys($iwlinks) as $iw){ 221 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 222 if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 223 echo "a.iw_$class {"; 224 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 225 echo '}'; 226 }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 227 echo "a.iw_$class {"; 228 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 229 echo '}'; 230 } 231 } 232} 233 234/** 235 * Prints classes for file download links 236 * 237 * @author Andreas Gohr <andi@splitbrain.org> 238 */ 239function css_filetypes(){ 240 241 // default style 242 echo 'a.mediafile {'; 243 echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 244 echo ' padding-left: 18px;'; 245 echo ' padding-bottom: 1px;'; 246 echo '}'; 247 248 // additional styles when icon available 249 // scan directory for all icons 250 $exts = array(); 251 if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 252 while(false !== ($file = readdir($dh))){ 253 if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 254 $ext = strtolower($match[1]); 255 $type = '.'.strtolower($match[2]); 256 if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 257 $exts[$ext] = $type; 258 } 259 } 260 } 261 closedir($dh); 262 } 263 foreach($exts as $ext=>$type){ 264 $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 265 echo "a.mf_$class {"; 266 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 267 echo '}'; 268 } 269} 270 271/** 272 * Loads a given file and fixes relative URLs with the 273 * given location prefix 274 */ 275function css_loadfile($file,$location=''){ 276 if(!@file_exists($file)) return ''; 277 $css = io_readFile($file); 278 if(!$location) return $css; 279 280 $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); 281 $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); 282 283 return $css; 284} 285 286/** 287 * Converte local image URLs to data URLs if the filesize is small 288 * 289 * Callback for preg_replace_callback 290 */ 291function css_datauri($match){ 292 global $conf; 293 294 $pre = unslash($match[1]); 295 $base = unslash($match[2]); 296 $url = unslash($match[3]); 297 $ext = unslash($match[4]); 298 299 $local = DOKU_INC.$url; 300 $size = @filesize($local); 301 if($size && $size < $conf['cssdatauri']){ 302 $data = base64_encode(file_get_contents($local)); 303 } 304 if($data){ 305 $url = 'data:image/'.$ext.';base64,'.$data; 306 }else{ 307 $url = $base.$url; 308 } 309 return $pre.$url; 310} 311 312 313/** 314 * Returns a list of possible Plugin Styles (no existance check here) 315 * 316 * @author Andreas Gohr <andi@splitbrain.org> 317 */ 318function css_pluginstyles($mediatype='screen'){ 319 global $lang; 320 $list = array(); 321 $plugins = plugin_list(); 322 foreach ($plugins as $p){ 323 $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 324 // alternative for screen.css 325 if ($mediatype=='screen') { 326 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 327 } 328 if($lang['direction'] == 'rtl'){ 329 $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/"; 330 } 331 } 332 return $list; 333} 334 335/** 336 * Move all @import statements in a combined stylesheet to the top so they 337 * aren't ignored by the browser. 338 * 339 * @author Gabriel Birke <birke@d-scribe.de> 340 */ 341function css_moveimports($css) 342{ 343 if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) { 344 return $css; 345 } 346 $newCss = ""; 347 $imports = ""; 348 $offset = 0; 349 foreach($matches[0] as $match) { 350 $newCss .= substr($css, $offset, $match[1] - $offset); 351 $imports .= $match[0]; 352 $offset = $match[1] + strlen($match[0]); 353 } 354 $newCss .= substr($css, $offset); 355 return $imports.$newCss; 356} 357 358/** 359 * Very simple CSS optimizer 360 * 361 * @author Andreas Gohr <andi@splitbrain.org> 362 */ 363function css_compress($css){ 364 //strip comments through a callback 365 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 366 367 //strip (incorrect but common) one line comments 368 $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 369 370 // strip whitespaces 371 $css = preg_replace('![\r\n\t ]+!',' ',$css); 372 $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css); 373 374 // shorten colors 375 $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); 376 377 return $css; 378} 379 380/** 381 * Callback for css_compress() 382 * 383 * Keeps short comments (< 5 chars) to maintain typical browser hacks 384 * 385 * @author Andreas Gohr <andi@splitbrain.org> 386 */ 387function css_comment_cb($matches){ 388 if(strlen($matches[2]) > 4) return ''; 389 return $matches[0]; 390} 391 392//Setup VIM: ex: et ts=4 : 393