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