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