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