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