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