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__).'/../../').'/'); 10*1c2d1019SAndreas 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'); 14*1c2d1019SAndreas 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 3878a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 3978a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 4078a6aeb1SAndreas Gohr $files = array(); 4178a6aeb1SAndreas Gohr if($print){ 4278a6aeb1SAndreas Gohr $files[DOKU_TPLINC.'print.css'] = DOKU_TPL; 4378a6aeb1SAndreas Gohr // load plugin styles 4478a6aeb1SAndreas Gohr $files = array_merge($files, css_pluginstyles('print')); 4578a6aeb1SAndreas Gohr $files[DOKU_CONF.'userprint.css'] = ''; 4678a6aeb1SAndreas Gohr }else{ 4778a6aeb1SAndreas Gohr $files[DOKU_INC.'lib/styles/style.css'] = DOKU_BASE.'lib/styles/'; 4878a6aeb1SAndreas Gohr //fixme spellchecker style 4978a6aeb1SAndreas Gohr $files[DOKU_TPLINC.'layout.css'] = DOKU_TPL; 5078a6aeb1SAndreas Gohr $files[DOKU_TPLINC.'design.css'] = DOKU_TPL; 5178a6aeb1SAndreas Gohr if($lang['direction'] == 'rtl'){ 5278a6aeb1SAndreas Gohr $files[DOKU_TPLINC.'rtl.css'] = DOKU_TPL; 5378a6aeb1SAndreas Gohr } 5478a6aeb1SAndreas Gohr // load plugin styles 5578a6aeb1SAndreas Gohr $files = array_merge($files, css_pluginstyles('screen')); 5678a6aeb1SAndreas Gohr $files[DOKU_CONF.'userstyle.css'] = ''; 5778a6aeb1SAndreas Gohr } 5878a6aeb1SAndreas Gohr 5978a6aeb1SAndreas Gohr // check cache age 6078a6aeb1SAndreas Gohr if(css_cacheok($cache,array_keys($files))){ 6178a6aeb1SAndreas Gohr readfile($cache); 6278a6aeb1SAndreas Gohr return; 6378a6aeb1SAndreas Gohr } 6478a6aeb1SAndreas Gohr 6578a6aeb1SAndreas Gohr // start output buffering and build the stylesheet 6678a6aeb1SAndreas Gohr ob_start(); 6778a6aeb1SAndreas Gohr 68*1c2d1019SAndreas Gohr // print the default classes for Interwikilinks 69*1c2d1019SAndreas Gohr css_interwiki(); 70*1c2d1019SAndreas Gohr 7178a6aeb1SAndreas Gohr // load files 7278a6aeb1SAndreas Gohr foreach($files as $file => $location){ 7378a6aeb1SAndreas Gohr print css_loadfile($file, $location); 7478a6aeb1SAndreas Gohr } 7578a6aeb1SAndreas Gohr 7678a6aeb1SAndreas Gohr // end output buffering and get contents 7778a6aeb1SAndreas Gohr $css = ob_get_contents(); 7878a6aeb1SAndreas Gohr ob_end_clean(); 7978a6aeb1SAndreas Gohr 8078a6aeb1SAndreas Gohr // compress whitespace and comments 8178a6aeb1SAndreas Gohr if($conf['compress']){ 8278a6aeb1SAndreas Gohr $css = css_compress($css); 8378a6aeb1SAndreas Gohr } 8478a6aeb1SAndreas Gohr 8578a6aeb1SAndreas Gohr // save cache file 8678a6aeb1SAndreas Gohr io_saveFile($cache,$css); 8778a6aeb1SAndreas Gohr 8878a6aeb1SAndreas Gohr // finally send output 8978a6aeb1SAndreas Gohr print $css; 9078a6aeb1SAndreas Gohr} 9178a6aeb1SAndreas Gohr 9278a6aeb1SAndreas Gohr/** 9378a6aeb1SAndreas Gohr * Checks if a CSS Cache file still is valid 9478a6aeb1SAndreas Gohr * 9578a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9678a6aeb1SAndreas Gohr */ 9778a6aeb1SAndreas Gohrfunction css_cacheok($cache,$files){ 9878a6aeb1SAndreas Gohr $ctime = @filemtime($cache); 9978a6aeb1SAndreas Gohr if(!$ctime) return false; //There is no cache 10078a6aeb1SAndreas Gohr 10178a6aeb1SAndreas Gohr // some additional files to check 10278a6aeb1SAndreas Gohr $files[] = DOKU_CONF.'dokuwiki.conf'; 10378a6aeb1SAndreas Gohr $files[] = DOKU_CONF.'local.conf'; 10478a6aeb1SAndreas Gohr $files[] = __FILE__; 10578a6aeb1SAndreas Gohr 10678a6aeb1SAndreas Gohr // now walk the files 10778a6aeb1SAndreas Gohr foreach($files as $file){ 10878a6aeb1SAndreas Gohr if(@filemtime($file) > $ctime){ 10978a6aeb1SAndreas Gohr return false; 11078a6aeb1SAndreas Gohr } 11178a6aeb1SAndreas Gohr } 11278a6aeb1SAndreas Gohr return true; 11378a6aeb1SAndreas Gohr} 11478a6aeb1SAndreas Gohr 11578a6aeb1SAndreas Gohr/** 116*1c2d1019SAndreas Gohr * Prints classes for interwikilinks 117*1c2d1019SAndreas Gohr * 118*1c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 119*1c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 120*1c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 121*1c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 122*1c2d1019SAndreas Gohr * overwritten in the template or userstyles. 123*1c2d1019SAndreas Gohr * 124*1c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 125*1c2d1019SAndreas Gohr */ 126*1c2d1019SAndreas Gohrfunction css_interwiki(){ 127*1c2d1019SAndreas Gohr 128*1c2d1019SAndreas Gohr // default style 129*1c2d1019SAndreas Gohr echo 'a.interwiki {'; 130*1c2d1019SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 131*1c2d1019SAndreas Gohr echo ' padding-left: 16px;'; 132*1c2d1019SAndreas Gohr echo '}'; 133*1c2d1019SAndreas Gohr 134*1c2d1019SAndreas Gohr // additional styles when icon available 135*1c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 136*1c2d1019SAndreas Gohr foreach(array_keys($iwlinks) as $iw){ 137*1c2d1019SAndreas Gohr if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 138*1c2d1019SAndreas Gohr echo "a.iw_$iw {"; 139*1c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 140*1c2d1019SAndreas Gohr echo '}'; 141*1c2d1019SAndreas Gohr }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 142*1c2d1019SAndreas Gohr echo "a.iw_$iw {"; 143*1c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 144*1c2d1019SAndreas Gohr echo '}'; 145*1c2d1019SAndreas Gohr } 146*1c2d1019SAndreas Gohr } 147*1c2d1019SAndreas Gohr 148*1c2d1019SAndreas Gohr} 149*1c2d1019SAndreas Gohr 150*1c2d1019SAndreas Gohr/** 15178a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 15278a6aeb1SAndreas Gohr * given location prefix 15378a6aeb1SAndreas Gohr */ 15478a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){ 15578a6aeb1SAndreas Gohr if(!@file_exists($file)) return ''; 15678a6aeb1SAndreas Gohr $css = io_readFile($file); 15778a6aeb1SAndreas Gohr if(!$location) return $css; 15878a6aeb1SAndreas Gohr 15978a6aeb1SAndreas Gohr $css = preg_replace('!(url\( *)([^/])!','\\1'.$location.'\\2',$css); 16078a6aeb1SAndreas Gohr return $css; 16178a6aeb1SAndreas Gohr} 16278a6aeb1SAndreas Gohr 16378a6aeb1SAndreas Gohr/** 16478a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 16578a6aeb1SAndreas Gohr * 16678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 16778a6aeb1SAndreas Gohr */ 16878a6aeb1SAndreas Gohrfunction css_pluginstyles($mode='screen'){ 16978a6aeb1SAndreas Gohr $list = array(); 17078a6aeb1SAndreas Gohr $plugins = plugin_list(); 17178a6aeb1SAndreas Gohr foreach ($plugins as $p){ 17278a6aeb1SAndreas Gohr if($mode == 'print'){ 17378a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/"; 17478a6aeb1SAndreas Gohr }else{ 17578a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 17678a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/"; 17778a6aeb1SAndreas Gohr } 17878a6aeb1SAndreas Gohr } 17978a6aeb1SAndreas Gohr return $list; 18078a6aeb1SAndreas Gohr} 18178a6aeb1SAndreas Gohr 18278a6aeb1SAndreas Gohr/** 18378a6aeb1SAndreas Gohr * Very simple CSS optimizer 18478a6aeb1SAndreas Gohr * 18578a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 18678a6aeb1SAndreas Gohr */ 18778a6aeb1SAndreas Gohrfunction css_compress($css){ 18878a6aeb1SAndreas Gohr // strip whitespaces 18978a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!',' ',$css); 19078a6aeb1SAndreas Gohr $css = preg_replace('/ ?([:;,{}]) ?/','\\1',$css); 19178a6aeb1SAndreas Gohr 19278a6aeb1SAndreas Gohr // strip comments (ungreedy) 19378a6aeb1SAndreas Gohr // We keep very small comments to maintain typical browser hacks 19478a6aeb1SAndreas Gohr $css = preg_replace('!(/\*)(.{4,})(\*/)!U','',$css); 19578a6aeb1SAndreas Gohr 19678a6aeb1SAndreas Gohr // shorten colors 19778a6aeb1SAndreas 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); 19878a6aeb1SAndreas Gohr 19978a6aeb1SAndreas Gohr return $css; 20078a6aeb1SAndreas Gohr} 20178a6aeb1SAndreas Gohr 20278a6aeb1SAndreas Gohr 20378a6aeb1SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 20478a6aeb1SAndreas Gohr?> 205