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