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