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