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',dirname(__FILE__).'/../../'); 10if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 11if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here 12require_once(DOKU_INC.'inc/init.php'); 13 14// Main (don't run when UNIT test) 15if(!defined('SIMPLE_TEST')){ 16 header('Content-Type: text/css; charset=utf-8'); 17 css_out(); 18} 19 20 21// ---------------------- functions ------------------------------ 22 23/** 24 * Output all needed Styles 25 * 26 * @author Andreas Gohr <andi@splitbrain.org> 27 */ 28function css_out(){ 29 global $conf; 30 global $lang; 31 global $config_cascade; 32 33 $mediatype = 'screen'; 34 if (isset($_REQUEST['s']) && 35 in_array($_REQUEST['s'], array('all', 'print', 'feed'))) { 36 $mediatype = $_REQUEST['s']; 37 } 38 39 $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t'])); 40 if($tpl){ 41 $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; 42 $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; 43 }else{ 44 $tplinc = DOKU_TPLINC; 45 $tpldir = DOKU_TPL; 46 } 47 48 // The generated script depends on some dynamic options 49 $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css'); 50 51 // load template styles 52 $tplstyles = array(); 53 if(@file_exists($tplinc.'style.ini')){ 54 $ini = parse_ini_file($tplinc.'style.ini',true); 55 foreach($ini['stylesheets'] as $file => $mode){ 56 $tplstyles[$mode][$tplinc.$file] = $tpldir; 57 } 58 } 59 60 // Array of needed files and their web locations, the latter ones 61 // are needed to fix relative paths in the stylesheets 62 $files = array(); 63 // load core styles 64 $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 65 // load jQuery-UI theme 66 $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/'; 67 // load plugin styles 68 $files = array_merge($files, css_pluginstyles($mediatype)); 69 // load template styles 70 if (isset($tplstyles[$mediatype])) { 71 $files = array_merge($files, $tplstyles[$mediatype]); 72 } 73 // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility 74 if (isset($config_cascade['userstyle']['default'])) { 75 $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default']; 76 } 77 // load user styles 78 if(isset($config_cascade['userstyle'][$mediatype])){ 79 $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE; 80 } 81 // load rtl styles 82 // @todo: this currently adds the rtl styles only to the 'screen' media type 83 // but 'print' and 'all' should also be supported 84 if ($mediatype=='screen') { 85 if($lang['direction'] == 'rtl'){ 86 if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']); 87 } 88 } 89 90 $cache_files = array_merge(array_keys($files), getConfigFiles('main')); 91 $cache_files[] = $tplinc.'style.ini'; 92 $cache_files[] = __FILE__; 93 94 // check cache age & handle conditional request 95 // This may exit if a cache can be used 96 http_cached($cache->cache, 97 $cache->useCache(array('files' => $cache_files))); 98 99 // start output buffering and build the stylesheet 100 ob_start(); 101 102 // print the default classes for interwiki links and file downloads 103 css_interwiki(); 104 css_filetypes(); 105 106 // load files 107 foreach($files as $file => $location){ 108 print css_loadfile($file, $location); 109 } 110 111 // end output buffering and get contents 112 $css = ob_get_contents(); 113 ob_end_clean(); 114 115 // apply style replacements 116 $css = css_applystyle($css,$tplinc); 117 118 // place all @import statements at the top of the file 119 $css = css_moveimports($css); 120 121 // compress whitespace and comments 122 if($conf['compress']){ 123 $css = css_compress($css); 124 } 125 126 http_cached_finish($cache->cache, $css); 127} 128 129/** 130 * Does placeholder replacements in the style according to 131 * the ones defined in a templates style.ini file 132 * 133 * @author Andreas Gohr <andi@splitbrain.org> 134 */ 135function css_applystyle($css,$tplinc){ 136 if(@file_exists($tplinc.'style.ini')){ 137 $ini = parse_ini_file($tplinc.'style.ini',true); 138 $css = strtr($css,$ini['replacements']); 139 } 140 return $css; 141} 142 143/** 144 * Prints classes for interwikilinks 145 * 146 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 147 * $name is the identifier given in the config. All Interwiki links get 148 * an default style with a default icon. If a special icon is available 149 * for an interwiki URL it is set in it's own class. Both classes can be 150 * overwritten in the template or userstyles. 151 * 152 * @author Andreas Gohr <andi@splitbrain.org> 153 */ 154function css_interwiki(){ 155 156 // default style 157 echo 'a.interwiki {'; 158 echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 159 echo ' padding: 1px 0px 1px 16px;'; 160 echo '}'; 161 162 // additional styles when icon available 163 $iwlinks = getInterwiki(); 164 foreach(array_keys($iwlinks) as $iw){ 165 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 166 if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 167 echo "a.iw_$class {"; 168 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 169 echo '}'; 170 }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 171 echo "a.iw_$class {"; 172 echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 173 echo '}'; 174 } 175 } 176} 177 178/** 179 * Prints classes for file download links 180 * 181 * @author Andreas Gohr <andi@splitbrain.org> 182 */ 183function css_filetypes(){ 184 185 // default style 186 echo 'a.mediafile {'; 187 echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 188 echo ' padding-left: 18px;'; 189 echo ' padding-bottom: 1px;'; 190 echo '}'; 191 192 // additional styles when icon available 193 // scan directory for all icons 194 $exts = array(); 195 if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 196 while(false !== ($file = readdir($dh))){ 197 if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 198 $ext = strtolower($match[1]); 199 $type = '.'.strtolower($match[2]); 200 if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 201 $exts[$ext] = $type; 202 } 203 } 204 } 205 closedir($dh); 206 } 207 foreach($exts as $ext=>$type){ 208 $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 209 echo "a.mf_$class {"; 210 echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 211 echo '}'; 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\([ \'"]*)(?!/|http://|https://| |\'|")#','\\1'.$location,$css); 225 $css = preg_replace('#(@import\s+[\'"])(?!/|http://|https://)#', '\\1'.$location, $css); 226 return $css; 227} 228 229 230/** 231 * Returns a list of possible Plugin Styles (no existance check here) 232 * 233 * @author Andreas Gohr <andi@splitbrain.org> 234 */ 235function css_pluginstyles($mediatype='screen'){ 236 global $lang; 237 $list = array(); 238 $plugins = plugin_list(); 239 foreach ($plugins as $p){ 240 $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 241 // alternative for screen.css 242 if ($mediatype=='screen') { 243 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 244 } 245 if($lang['direction'] == 'rtl'){ 246 $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/"; 247 } 248 } 249 return $list; 250} 251 252/** 253 * Move all @import statements in a combined stylesheet to the top so they 254 * aren't ignored by the browser. 255 * 256 * @author Gabriel Birke <birke@d-scribe.de> 257 */ 258function css_moveimports($css) 259{ 260 if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) { 261 return $css; 262 } 263 $newCss = ""; 264 $imports = ""; 265 $offset = 0; 266 foreach($matches[0] as $match) { 267 $newCss .= substr($css, $offset, $match[1] - $offset); 268 $imports .= $match[0]; 269 $offset = $match[1] + strlen($match[0]); 270 } 271 $newCss .= substr($css, $offset); 272 return $imports.$newCss; 273} 274 275/** 276 * Very simple CSS optimizer 277 * 278 * @author Andreas Gohr <andi@splitbrain.org> 279 */ 280function css_compress($css){ 281 //strip comments through a callback 282 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 283 284 //strip (incorrect but common) one line comments 285 $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 286 287 // strip whitespaces 288 $css = preg_replace('![\r\n\t ]+!',' ',$css); 289 $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css); 290 291 // shorten colors 292 $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); 293 294 return $css; 295} 296 297/** 298 * Callback for css_compress() 299 * 300 * Keeps short comments (< 5 chars) to maintain typical browser hacks 301 * 302 * @author Andreas Gohr <andi@splitbrain.org> 303 */ 304function css_comment_cb($matches){ 305 if(strlen($matches[2]) > 4) return ''; 306 return $matches[0]; 307} 308 309//Setup VIM: ex: et ts=4 : 310