178a6aeb1SAndreas Gohr<?php 278a6aeb1SAndreas Gohr/** 378a6aeb1SAndreas Gohr * DokuWiki StyleSheet creator 478a6aeb1SAndreas Gohr * 578a6aeb1SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 778a6aeb1SAndreas Gohr */ 878a6aeb1SAndreas Gohr 978a6aeb1SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 101c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 1178a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php'); 1278a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/pageutils.php'); 1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/io.php'); 141c2d1019SAndreas Gohrrequire_once(DOKU_INC.'inc/confutils.php'); 1578a6aeb1SAndreas Gohr 1678a6aeb1SAndreas Gohr// Main (don't run when UNIT test) 1778a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){ 1878a6aeb1SAndreas Gohr header('Content-Type: text/css; charset=utf-8'); 1978a6aeb1SAndreas Gohr css_out(); 2078a6aeb1SAndreas Gohr} 2178a6aeb1SAndreas Gohr 2278a6aeb1SAndreas Gohr 2378a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------ 2478a6aeb1SAndreas Gohr 2578a6aeb1SAndreas Gohr/** 2678a6aeb1SAndreas Gohr * Output all needed Styles 2778a6aeb1SAndreas Gohr * 2878a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2978a6aeb1SAndreas Gohr */ 3078a6aeb1SAndreas Gohrfunction css_out(){ 3178a6aeb1SAndreas Gohr global $conf; 3278a6aeb1SAndreas Gohr global $lang; 3378a6aeb1SAndreas Gohr $print = (bool) $_REQUEST['print']; //print mode? 3478a6aeb1SAndreas Gohr 3578a6aeb1SAndreas Gohr // The generated script depends on some dynamic options 3678a6aeb1SAndreas Gohr $cache = getCacheName('styles'.$print,'.css'); 3778a6aeb1SAndreas Gohr 38*519b3173SAndreas Gohr // load template styles 39*519b3173SAndreas Gohr $tplstyles = array(); 40*519b3173SAndreas Gohr if(@file_exists(DOKU_TPLINC.'style.ini')){ 41*519b3173SAndreas Gohr $ini = parse_ini_file(DOKU_TPLINC.'style.ini',true); 42*519b3173SAndreas Gohr foreach($ini['stylesheets'] as $file => $mode){ 43*519b3173SAndreas Gohr $tplstyles[$mode][DOKU_TPLINC.$file] = DOKU_TPL; 44*519b3173SAndreas Gohr } 45*519b3173SAndreas Gohr } 46*519b3173SAndreas Gohr 4778a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 4878a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 4978a6aeb1SAndreas Gohr $files = array(); 5078a6aeb1SAndreas Gohr if($print){ 5178a6aeb1SAndreas Gohr $files[DOKU_TPLINC.'print.css'] = DOKU_TPL; 52*519b3173SAndreas Gohr $files = array_merge($files, $tplstyles['print']); 5378a6aeb1SAndreas Gohr // load plugin styles 5478a6aeb1SAndreas Gohr $files = array_merge($files, css_pluginstyles('print')); 5578a6aeb1SAndreas Gohr $files[DOKU_CONF.'userprint.css'] = ''; 5678a6aeb1SAndreas Gohr }else{ 5778a6aeb1SAndreas Gohr $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/'; 58*519b3173SAndreas Gohr $files = array_merge($files, $tplstyles['screen']); 5978a6aeb1SAndreas Gohr if($lang['direction'] == 'rtl'){ 60*519b3173SAndreas Gohr $files = array_merge($files, $tplstyles['rtl']); 6178a6aeb1SAndreas Gohr } 6278a6aeb1SAndreas Gohr // load plugin styles 6378a6aeb1SAndreas Gohr $files = array_merge($files, css_pluginstyles('screen')); 6478a6aeb1SAndreas Gohr $files[DOKU_CONF.'userstyle.css'] = ''; 6578a6aeb1SAndreas Gohr } 6678a6aeb1SAndreas Gohr 6778a6aeb1SAndreas Gohr // check cache age 6878a6aeb1SAndreas Gohr if(css_cacheok($cache,array_keys($files))){ 6978a6aeb1SAndreas Gohr readfile($cache); 7078a6aeb1SAndreas Gohr return; 7178a6aeb1SAndreas Gohr } 7278a6aeb1SAndreas Gohr 7378a6aeb1SAndreas Gohr // start output buffering and build the stylesheet 7478a6aeb1SAndreas Gohr ob_start(); 7578a6aeb1SAndreas Gohr 76d15166e5SAndreas Gohr // print the default classes for interwiki links and file downloads 771c2d1019SAndreas Gohr css_interwiki(); 78d15166e5SAndreas Gohr css_filetypes(); 791c2d1019SAndreas Gohr 8078a6aeb1SAndreas Gohr // load files 8178a6aeb1SAndreas Gohr foreach($files as $file => $location){ 8278a6aeb1SAndreas Gohr print css_loadfile($file, $location); 8378a6aeb1SAndreas Gohr } 8478a6aeb1SAndreas Gohr 8578a6aeb1SAndreas Gohr // end output buffering and get contents 8678a6aeb1SAndreas Gohr $css = ob_get_contents(); 8778a6aeb1SAndreas Gohr ob_end_clean(); 8878a6aeb1SAndreas Gohr 896e69c1baSAndreas Gohr // apply style replacements 906e69c1baSAndreas Gohr $css = css_applystyle($css); 916e69c1baSAndreas Gohr 9278a6aeb1SAndreas Gohr // compress whitespace and comments 9378a6aeb1SAndreas Gohr if($conf['compress']){ 9478a6aeb1SAndreas Gohr $css = css_compress($css); 9578a6aeb1SAndreas Gohr } 9678a6aeb1SAndreas Gohr 9778a6aeb1SAndreas Gohr // save cache file 9878a6aeb1SAndreas Gohr io_saveFile($cache,$css); 9978a6aeb1SAndreas Gohr 10078a6aeb1SAndreas Gohr // finally send output 10178a6aeb1SAndreas Gohr print $css; 10278a6aeb1SAndreas Gohr} 10378a6aeb1SAndreas Gohr 10478a6aeb1SAndreas Gohr/** 10578a6aeb1SAndreas Gohr * Checks if a CSS Cache file still is valid 10678a6aeb1SAndreas Gohr * 10778a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10878a6aeb1SAndreas Gohr */ 10978a6aeb1SAndreas Gohrfunction css_cacheok($cache,$files){ 11078a6aeb1SAndreas Gohr $ctime = @filemtime($cache); 11178a6aeb1SAndreas Gohr if(!$ctime) return false; //There is no cache 11278a6aeb1SAndreas Gohr 11378a6aeb1SAndreas Gohr // some additional files to check 114c591aabeSAndreas Gohr $files[] = DOKU_CONF.'dokuwiki.php'; 115c591aabeSAndreas Gohr $files[] = DOKU_CONF.'local.php'; 1166e69c1baSAndreas Gohr $files[] = DOKU_TPLINC.'style.ini'; 11778a6aeb1SAndreas Gohr $files[] = __FILE__; 11878a6aeb1SAndreas Gohr 11978a6aeb1SAndreas Gohr // now walk the files 12078a6aeb1SAndreas Gohr foreach($files as $file){ 12178a6aeb1SAndreas Gohr if(@filemtime($file) > $ctime){ 12278a6aeb1SAndreas Gohr return false; 12378a6aeb1SAndreas Gohr } 12478a6aeb1SAndreas Gohr } 12578a6aeb1SAndreas Gohr return true; 12678a6aeb1SAndreas Gohr} 12778a6aeb1SAndreas Gohr 12878a6aeb1SAndreas Gohr/** 1296e69c1baSAndreas Gohr * Does placeholder replacements in the style according to 1306e69c1baSAndreas Gohr * the ones defined in a templates style.ini file 1316e69c1baSAndreas Gohr * 1326e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1336e69c1baSAndreas Gohr */ 1346e69c1baSAndreas Gohrfunction css_applystyle($css){ 1356e69c1baSAndreas Gohr if(@file_exists(DOKU_TPLINC.'style.ini')){ 136*519b3173SAndreas Gohr $ini = parse_ini_file(DOKU_TPLINC.'style.ini',true); 137*519b3173SAndreas Gohr $css = strtr($css,$ini['replacements']); 1386e69c1baSAndreas Gohr } 1396e69c1baSAndreas Gohr return $css; 1406e69c1baSAndreas Gohr} 1416e69c1baSAndreas Gohr 1426e69c1baSAndreas Gohr/** 1431c2d1019SAndreas Gohr * Prints classes for interwikilinks 1441c2d1019SAndreas Gohr * 1451c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 1461c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 1471c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 1481c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 1491c2d1019SAndreas Gohr * overwritten in the template or userstyles. 1501c2d1019SAndreas Gohr * 1511c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1521c2d1019SAndreas Gohr */ 1531c2d1019SAndreas Gohrfunction css_interwiki(){ 1541c2d1019SAndreas Gohr 1551c2d1019SAndreas Gohr // default style 1561c2d1019SAndreas Gohr echo 'a.interwiki {'; 1571c2d1019SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 1581c2d1019SAndreas Gohr echo ' padding-left: 16px;'; 1591c2d1019SAndreas Gohr echo '}'; 1601c2d1019SAndreas Gohr 1611c2d1019SAndreas Gohr // additional styles when icon available 1621c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 1631c2d1019SAndreas Gohr foreach(array_keys($iwlinks) as $iw){ 1649d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 1651c2d1019SAndreas Gohr if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 1669d2ddea4SAndreas Gohr echo "a.iw_$class {"; 1671c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 1681c2d1019SAndreas Gohr echo '}'; 1691c2d1019SAndreas Gohr }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 1709d2ddea4SAndreas Gohr echo "a.iw_$class {"; 1711c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 1721c2d1019SAndreas Gohr echo '}'; 1731c2d1019SAndreas Gohr } 1741c2d1019SAndreas Gohr } 175d15166e5SAndreas Gohr} 1761c2d1019SAndreas Gohr 177d15166e5SAndreas Gohr/** 178d15166e5SAndreas Gohr * Prints classes for file download links 179d15166e5SAndreas Gohr * 180d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 181d15166e5SAndreas Gohr */ 182d15166e5SAndreas Gohrfunction css_filetypes(){ 183d15166e5SAndreas Gohr 184d15166e5SAndreas Gohr // default style 185d15166e5SAndreas Gohr echo 'a.mediafile {'; 186d15166e5SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 187d15166e5SAndreas Gohr echo ' padding-left: 16px;'; 188d15166e5SAndreas Gohr echo '}'; 189d15166e5SAndreas Gohr 190d15166e5SAndreas Gohr // additional styles when icon available 191d15166e5SAndreas Gohr $mimes = getMimeTypes(); 192d15166e5SAndreas Gohr foreach(array_keys($mimes) as $mime){ 1939d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$mime); 194d15166e5SAndreas Gohr if(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.png')){ 1959d2ddea4SAndreas Gohr echo "a.mf_$class {"; 196d15166e5SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.png)'; 197d15166e5SAndreas Gohr echo '}'; 198d15166e5SAndreas Gohr }elseif(@file_exists(DOKU_INC.'lib/images/fileicons/'.$mime.'.gif')){ 1999d2ddea4SAndreas Gohr echo "a.mf_$class {"; 200d15166e5SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$mime.'.gif)'; 201d15166e5SAndreas Gohr echo '}'; 202d15166e5SAndreas Gohr } 203d15166e5SAndreas Gohr } 2041c2d1019SAndreas Gohr} 2051c2d1019SAndreas Gohr 2061c2d1019SAndreas Gohr/** 20778a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 20878a6aeb1SAndreas Gohr * given location prefix 20978a6aeb1SAndreas Gohr */ 21078a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){ 21178a6aeb1SAndreas Gohr if(!@file_exists($file)) return ''; 21278a6aeb1SAndreas Gohr $css = io_readFile($file); 21378a6aeb1SAndreas Gohr if(!$location) return $css; 21478a6aeb1SAndreas Gohr 21578a6aeb1SAndreas Gohr $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css); 21678a6aeb1SAndreas Gohr return $css; 21778a6aeb1SAndreas Gohr} 21878a6aeb1SAndreas Gohr 21978a6aeb1SAndreas Gohr/** 22078a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 22178a6aeb1SAndreas Gohr * 22278a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 22378a6aeb1SAndreas Gohr */ 22478a6aeb1SAndreas Gohrfunction css_pluginstyles($mode='screen'){ 22578a6aeb1SAndreas Gohr $list = array(); 22678a6aeb1SAndreas Gohr $plugins = plugin_list(); 22778a6aeb1SAndreas Gohr foreach ($plugins as $p){ 22878a6aeb1SAndreas Gohr if($mode == 'print'){ 22978a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/"; 23078a6aeb1SAndreas Gohr }else{ 23178a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 23278a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/"; 23378a6aeb1SAndreas Gohr } 23478a6aeb1SAndreas Gohr } 23578a6aeb1SAndreas Gohr return $list; 23678a6aeb1SAndreas Gohr} 23778a6aeb1SAndreas Gohr 23878a6aeb1SAndreas Gohr/** 23978a6aeb1SAndreas Gohr * Very simple CSS optimizer 24078a6aeb1SAndreas Gohr * 24178a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 24278a6aeb1SAndreas Gohr */ 24378a6aeb1SAndreas Gohrfunction css_compress($css){ 24478a6aeb1SAndreas Gohr // strip whitespaces 24578a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!',' ',$css); 2465646f690SAndreas Gohr $css = preg_replace('/ ?([:;,{}\/]) ?/','\\1',$css); 24778a6aeb1SAndreas Gohr 248c00aef76SAndreas Gohr //strip comments through a callback 249c00aef76SAndreas Gohr $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 25078a6aeb1SAndreas Gohr 25178a6aeb1SAndreas Gohr // shorten colors 25278a6aeb1SAndreas Gohr $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); 25378a6aeb1SAndreas Gohr 25478a6aeb1SAndreas Gohr return $css; 25578a6aeb1SAndreas Gohr} 25678a6aeb1SAndreas Gohr 257c00aef76SAndreas Gohr/** 258c00aef76SAndreas Gohr * Callback for css_compress() 259c00aef76SAndreas Gohr * 260c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks 261c00aef76SAndreas Gohr * 262c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 263c00aef76SAndreas Gohr */ 264c00aef76SAndreas Gohrfunction css_comment_cb($matches){ 265c00aef76SAndreas Gohr if(strlen($matches[2]) > 4) return ''; 266c00aef76SAndreas Gohr return $matches[0]; 267c00aef76SAndreas Gohr} 26878a6aeb1SAndreas Gohr 26978a6aeb1SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 27078a6aeb1SAndreas Gohr?> 271