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 $style = ''; 34 if (isset($_REQUEST['s']) && 35 in_array($_REQUEST['s'], array('all', 'print', 'feed'))) { 36 $style = $_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.$style,'.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 //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']); 64 if(!empty($style)){ 65 $files[DOKU_INC.'lib/styles/'.$style.'.css'] = DOKU_BASE.'lib/styles/'; 66 // load plugin, template, user styles 67 $files = array_merge($files, css_pluginstyles($style)); 68 if (isset($tplstyles[$style])) $files = array_merge($files, $tplstyles[$style]); 69 70 if(isset($config_cascade['userstyle'][$style])){ 71 $files[$config_cascade['userstyle'][$style]] = DOKU_BASE; 72 } 73 }else{ 74 $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/'; 75 // load plugin, template, user styles 76 $files = array_merge($files, css_pluginstyles('screen')); 77 if (isset($tplstyles['screen'])) $files = array_merge($files, $tplstyles['screen']); 78 if($lang['direction'] == 'rtl'){ 79 if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']); 80 } 81 if(isset($config_cascade['userstyle']['default'])){ 82 $files[$config_cascade['userstyle']['default']] = DOKU_BASE; 83 } 84 } 85 86 // check cache age & handle conditional request 87 header('Cache-Control: public, max-age=3600'); 88 header('Pragma: public'); 89 if(css_cacheok($cache,array_keys($files),$tplinc)){ 90 http_conditionalRequest(filemtime($cache)); 91 if($conf['allowdebug']) header("X-CacheUsed: $cache"); 92 93 // finally send output 94 if ($conf['gzip_output'] && http_gzip_valid($cache)) { 95 header('Vary: Accept-Encoding'); 96 header('Content-Encoding: gzip'); 97 readfile($cache.".gz"); 98 } else { 99 if (!http_sendfile($cache)) readfile($cache); 100 } 101 102 return; 103 } else { 104 http_conditionalRequest(time()); 105 } 106 107 // start output buffering and build the stylesheet 108 ob_start(); 109 110 // print the default classes for interwiki links and file downloads 111 css_interwiki(); 112 css_filetypes(); 113 114 // load files 115 foreach($files as $file => $location){ 116 print css_loadfile($file, $location); 117 } 118 119 // end output buffering and get contents 120 $css = ob_get_contents(); 121 ob_end_clean(); 122 123 // apply style replacements 124 $css = css_applystyle($css,$tplinc); 125 126 // compress whitespace and comments 127 if($conf['compress']){ 128 $css = css_compress($css); 129 } 130 131 // save cache file 132 io_saveFile($cache,$css); 133 if(function_exists('gzopen')) io_saveFile("$cache.gz",$css); 134 135 // finally send output 136 if ($conf['gzip_output']) { 137 header('Vary: Accept-Encoding'); 138 header('Content-Encoding: gzip'); 139 print gzencode($css,9,FORCE_GZIP); 140 } else { 141 print $css; 142 } 143} 144 145/** 146 * Checks if a CSS Cache file still is valid 147 * 148 * @author Andreas Gohr <andi@splitbrain.org> 149 */ 150function css_cacheok($cache,$files,$tplinc){ 151 global $config_cascade; 152 153 if(isset($_REQUEST['purge'])) return false; //support purge request 154 155 $ctime = @filemtime($cache); 156 if(!$ctime) return false; //There is no cache 157 158 // some additional files to check 159 $files = array_merge($files, getConfigFiles('main')); 160 $files[] = $tplinc.'style.ini'; 161 $files[] = __FILE__; 162 163 // now walk the files 164 foreach($files as $file){ 165 if(@filemtime($file) > $ctime){ 166 return false; 167 } 168 } 169 return true; 170} 171 172/** 173 * Does placeholder replacements in the style according to 174 * the ones defined in a templates style.ini file 175 * 176 * @author Andreas Gohr <andi@splitbrain.org> 177 */ 178function css_applystyle($css,$tplinc){ 179 if(@file_exists($tplinc.'style.ini')){ 180 $ini = parse_ini_file($tplinc.'style.ini',true); 181 $css = strtr($css,$ini['replacements']); 182 } 183 return $css; 184} 185 186/** 187 * Prints classes for interwikilinks 188 * 189 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 190 * $name is the identifier given in the config. All Interwiki links get 191 * an default style with a default icon. If a special icon is available 192 * for an interwiki URL it is set in it's own class. Both classes can be 193 * overwritten in the template or userstyles. 194 * 195 * @author Andreas Gohr <andi@splitbrain.org> 196 */ 197function css_interwiki(){ 198 199 // default style 200 echo 'a.interwiki {'; 201 echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 202 echo ' padding-left: 16px;'; 203 echo '}'; 204 205 // additional styles when icon available 206 $iwlinks = getInterwiki(); 207 foreach(array_keys($iwlinks) as $iw){ 208 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 209 if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 210 echo "a.iw_$class {"; 211 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 212 echo '}'; 213 }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 214 echo "a.iw_$class {"; 215 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 216 echo '}'; 217 } 218 } 219} 220 221/** 222 * Prints classes for file download links 223 * 224 * @author Andreas Gohr <andi@splitbrain.org> 225 */ 226function css_filetypes(){ 227 228 // default style 229 echo 'a.mediafile {'; 230 echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 231 echo ' padding-left: 18px;'; 232 echo ' padding-bottom: 1px;'; 233 echo '}'; 234 235 // additional styles when icon available 236 // scan directory for all icons 237 $exts = array(); 238 if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 239 while(false !== ($file = readdir($dh))){ 240 if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 241 $ext = strtolower($match[1]); 242 $type = '.'.strtolower($match[2]); 243 if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 244 $exts[$ext] = $type; 245 } 246 } 247 } 248 closedir($dh); 249 } 250 foreach($exts as $ext=>$type){ 251 $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 252 echo "a.mf_$class {"; 253 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 254 echo '}'; 255 } 256} 257 258/** 259 * Loads a given file and fixes relative URLs with the 260 * given location prefix 261 */ 262function css_loadfile($file,$location=''){ 263 if(!@file_exists($file)) return ''; 264 $css = io_readFile($file); 265 if(!$location) return $css; 266 267 $css = preg_replace('#(url\([ \'"]*)((?!/|http://|https://| |\'|"))#','\\1'.$location.'\\3',$css); 268 return $css; 269} 270 271 272/** 273 * Returns a list of possible Plugin Styles (no existance check here) 274 * 275 * @author Andreas Gohr <andi@splitbrain.org> 276 */ 277function css_pluginstyles($mode='screen'){ 278 global $lang; 279 $list = array(); 280 $plugins = plugin_list(); 281 foreach ($plugins as $p){ 282 if($mode == 'all'){ 283 $list[DOKU_PLUGIN."$p/all.css"] = DOKU_BASE."lib/plugins/$p/"; 284 }elseif($mode == 'print'){ 285 $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/"; 286 }elseif($mode == 'feed'){ 287 $list[DOKU_PLUGIN."$p/feed.css"] = DOKU_BASE."lib/plugins/$p/"; 288 }else{ 289 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 290 $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/"; 291 } 292 if($lang['direction'] == 'rtl'){ 293 $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/"; 294 } 295 } 296 return $list; 297} 298 299/** 300 * Very simple CSS optimizer 301 * 302 * @author Andreas Gohr <andi@splitbrain.org> 303 */ 304function css_compress($css){ 305 //strip comments through a callback 306 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 307 308 //strip (incorrect but common) one line comments 309 $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 310 311 // strip whitespaces 312 $css = preg_replace('![\r\n\t ]+!',' ',$css); 313 $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css); 314 315 // shorten colors 316 $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); 317 318 return $css; 319} 320 321/** 322 * Callback for css_compress() 323 * 324 * Keeps short comments (< 5 chars) to maintain typical browser hacks 325 * 326 * @author Andreas Gohr <andi@splitbrain.org> 327 */ 328function css_comment_cb($matches){ 329 if(strlen($matches[2]) > 4) return ''; 330 return $matches[0]; 331} 332 333//Setup VIM: ex: et ts=4 enc=utf-8 : 334