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 9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 101c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 1198bda4fdSAndreas Gohrif(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here 1278a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php'); 1378a6aeb1SAndreas Gohr 1478a6aeb1SAndreas Gohr// Main (don't run when UNIT test) 1578a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){ 1678a6aeb1SAndreas Gohr header('Content-Type: text/css; charset=utf-8'); 1778a6aeb1SAndreas Gohr css_out(); 1878a6aeb1SAndreas Gohr} 1978a6aeb1SAndreas Gohr 2078a6aeb1SAndreas Gohr 2178a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------ 2278a6aeb1SAndreas Gohr 2378a6aeb1SAndreas Gohr/** 2478a6aeb1SAndreas Gohr * Output all needed Styles 2578a6aeb1SAndreas Gohr * 2678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2778a6aeb1SAndreas Gohr */ 2878a6aeb1SAndreas Gohrfunction css_out(){ 2978a6aeb1SAndreas Gohr global $conf; 3078a6aeb1SAndreas Gohr global $lang; 3109edb711SAndreas Gohr global $config_cascade; 3209edb711SAndreas Gohr 33318cd03eSAnika Henke $mediatype = 'screen'; 34c66972f2SAdrian Lang if (isset($_REQUEST['s']) && 35c66972f2SAdrian Lang in_array($_REQUEST['s'], array('all', 'print', 'feed'))) { 36318cd03eSAnika Henke $mediatype = $_REQUEST['s']; 37615960feSTom N Harris } 3878a6aeb1SAndreas Gohr 39f7589b08SChris Smith $tpl = trim(preg_replace('/[^\w-]+/','',$_REQUEST['t'])); 40124af657SAndreas Gohr if($tpl){ 41124af657SAndreas Gohr $tplinc = DOKU_INC.'lib/tpl/'.$tpl.'/'; 42124af657SAndreas Gohr $tpldir = DOKU_BASE.'lib/tpl/'.$tpl.'/'; 43124af657SAndreas Gohr }else{ 44*81aca18eSAndreas Gohr $tplinc = tpl_incdir(); 45*81aca18eSAndreas Gohr $tpldir = tpl_basedir(); 46124af657SAndreas Gohr } 47124af657SAndreas Gohr 4878a6aeb1SAndreas Gohr // The generated script depends on some dynamic options 496619f42eSAdrian Lang $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tplinc.$mediatype,'.css'); 5078a6aeb1SAndreas Gohr 51519b3173SAndreas Gohr // load template styles 52519b3173SAndreas Gohr $tplstyles = array(); 53124af657SAndreas Gohr if(@file_exists($tplinc.'style.ini')){ 54124af657SAndreas Gohr $ini = parse_ini_file($tplinc.'style.ini',true); 55519b3173SAndreas Gohr foreach($ini['stylesheets'] as $file => $mode){ 56124af657SAndreas Gohr $tplstyles[$mode][$tplinc.$file] = $tpldir; 57519b3173SAndreas Gohr } 58519b3173SAndreas Gohr } 59519b3173SAndreas Gohr 6078a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 6178a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 6278a6aeb1SAndreas Gohr $files = array(); 63318cd03eSAnika Henke // load core styles 64318cd03eSAnika Henke $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 6543576758SAndreas Gohr // load jQuery-UI theme 66bf1ec652SAndreas Gohr $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/'; 67318cd03eSAnika Henke // load plugin styles 68318cd03eSAnika Henke $files = array_merge($files, css_pluginstyles($mediatype)); 69318cd03eSAnika Henke // load template styles 70318cd03eSAnika Henke if (isset($tplstyles[$mediatype])) { 71318cd03eSAnika Henke $files = array_merge($files, $tplstyles[$mediatype]); 7209edb711SAndreas Gohr } 73318cd03eSAnika Henke // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility 74318cd03eSAnika Henke if (isset($config_cascade['userstyle']['default'])) { 75318cd03eSAnika Henke $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default']; 76318cd03eSAnika Henke } 77318cd03eSAnika Henke // load user styles 78318cd03eSAnika Henke if(isset($config_cascade['userstyle'][$mediatype])){ 79318cd03eSAnika Henke $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE; 80318cd03eSAnika Henke } 81318cd03eSAnika Henke // load rtl styles 82318cd03eSAnika Henke // @todo: this currently adds the rtl styles only to the 'screen' media type 83318cd03eSAnika Henke // but 'print' and 'all' should also be supported 84318cd03eSAnika Henke if ($mediatype=='screen') { 8578a6aeb1SAndreas Gohr if($lang['direction'] == 'rtl'){ 861f5663fdSchris if (isset($tplstyles['rtl'])) $files = array_merge($files, $tplstyles['rtl']); 8778a6aeb1SAndreas Gohr } 8878a6aeb1SAndreas Gohr } 8978a6aeb1SAndreas Gohr 906619f42eSAdrian Lang $cache_files = array_merge(array_keys($files), getConfigFiles('main')); 916619f42eSAdrian Lang $cache_files[] = $tplinc.'style.ini'; 926619f42eSAdrian Lang $cache_files[] = __FILE__; 936619f42eSAdrian Lang 9438f56bffSBen Coburn // check cache age & handle conditional request 956619f42eSAdrian Lang // This may exit if a cache can be used 966619f42eSAdrian Lang http_cached($cache->cache, 976619f42eSAdrian Lang $cache->useCache(array('files' => $cache_files))); 9878a6aeb1SAndreas Gohr 9978a6aeb1SAndreas Gohr // start output buffering and build the stylesheet 10078a6aeb1SAndreas Gohr ob_start(); 10178a6aeb1SAndreas Gohr 102d15166e5SAndreas Gohr // print the default classes for interwiki links and file downloads 1031c2d1019SAndreas Gohr css_interwiki(); 104d15166e5SAndreas Gohr css_filetypes(); 1051c2d1019SAndreas Gohr 10678a6aeb1SAndreas Gohr // load files 10778a6aeb1SAndreas Gohr foreach($files as $file => $location){ 10878a6aeb1SAndreas Gohr print css_loadfile($file, $location); 10978a6aeb1SAndreas Gohr } 11078a6aeb1SAndreas Gohr 11178a6aeb1SAndreas Gohr // end output buffering and get contents 11278a6aeb1SAndreas Gohr $css = ob_get_contents(); 11378a6aeb1SAndreas Gohr ob_end_clean(); 11478a6aeb1SAndreas Gohr 1156e69c1baSAndreas Gohr // apply style replacements 116124af657SAndreas Gohr $css = css_applystyle($css,$tplinc); 1176e69c1baSAndreas Gohr 118f7d780b9SGabriel Birke // place all @import statements at the top of the file 119f7d780b9SGabriel Birke $css = css_moveimports($css); 120f7d780b9SGabriel Birke 12178a6aeb1SAndreas Gohr // compress whitespace and comments 12278a6aeb1SAndreas Gohr if($conf['compress']){ 12378a6aeb1SAndreas Gohr $css = css_compress($css); 12478a6aeb1SAndreas Gohr } 12578a6aeb1SAndreas Gohr 126809d3ba5SAndreas Gohr // embed small images right into the stylesheet 127809d3ba5SAndreas Gohr if($conf['cssdatauri']){ 128809d3ba5SAndreas Gohr $base = preg_quote(DOKU_BASE,'#'); 129809d3ba5SAndreas Gohr $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css); 130809d3ba5SAndreas Gohr } 131809d3ba5SAndreas Gohr 1326619f42eSAdrian Lang http_cached_finish($cache->cache, $css); 13378a6aeb1SAndreas Gohr} 13478a6aeb1SAndreas Gohr 13578a6aeb1SAndreas Gohr/** 1366e69c1baSAndreas Gohr * Does placeholder replacements in the style according to 1376e69c1baSAndreas Gohr * the ones defined in a templates style.ini file 1386e69c1baSAndreas Gohr * 1396e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1406e69c1baSAndreas Gohr */ 141124af657SAndreas Gohrfunction css_applystyle($css,$tplinc){ 142124af657SAndreas Gohr if(@file_exists($tplinc.'style.ini')){ 143124af657SAndreas Gohr $ini = parse_ini_file($tplinc.'style.ini',true); 144519b3173SAndreas Gohr $css = strtr($css,$ini['replacements']); 1456e69c1baSAndreas Gohr } 1466e69c1baSAndreas Gohr return $css; 1476e69c1baSAndreas Gohr} 1486e69c1baSAndreas Gohr 1496e69c1baSAndreas Gohr/** 1501c2d1019SAndreas Gohr * Prints classes for interwikilinks 1511c2d1019SAndreas Gohr * 1521c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 1531c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 1541c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 1551c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 1561c2d1019SAndreas Gohr * overwritten in the template or userstyles. 1571c2d1019SAndreas Gohr * 1581c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1591c2d1019SAndreas Gohr */ 1601c2d1019SAndreas Gohrfunction css_interwiki(){ 1611c2d1019SAndreas Gohr 1621c2d1019SAndreas Gohr // default style 1631c2d1019SAndreas Gohr echo 'a.interwiki {'; 1641c2d1019SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 1657b4ea081Smarklundeberg echo ' padding: 1px 0px 1px 16px;'; 1661c2d1019SAndreas Gohr echo '}'; 1671c2d1019SAndreas Gohr 1681c2d1019SAndreas Gohr // additional styles when icon available 1691c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 1701c2d1019SAndreas Gohr foreach(array_keys($iwlinks) as $iw){ 1719d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 1721c2d1019SAndreas Gohr if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 1739d2ddea4SAndreas Gohr echo "a.iw_$class {"; 1741c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 1751c2d1019SAndreas Gohr echo '}'; 1761c2d1019SAndreas Gohr }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 1779d2ddea4SAndreas Gohr echo "a.iw_$class {"; 1781c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 1791c2d1019SAndreas Gohr echo '}'; 1801c2d1019SAndreas Gohr } 1811c2d1019SAndreas Gohr } 182d15166e5SAndreas Gohr} 1831c2d1019SAndreas Gohr 184d15166e5SAndreas Gohr/** 185d15166e5SAndreas Gohr * Prints classes for file download links 186d15166e5SAndreas Gohr * 187d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 188d15166e5SAndreas Gohr */ 189d15166e5SAndreas Gohrfunction css_filetypes(){ 190d15166e5SAndreas Gohr 191d15166e5SAndreas Gohr // default style 192035e07f1SKate Arzamastseva echo '.mediafile {'; 193d15166e5SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 1945b77caf4SAndreas Gohr echo ' padding-left: 18px;'; 1955b77caf4SAndreas Gohr echo ' padding-bottom: 1px;'; 196d15166e5SAndreas Gohr echo '}'; 197d15166e5SAndreas Gohr 198d15166e5SAndreas Gohr // additional styles when icon available 19927bf7924STom N Harris // scan directory for all icons 20027bf7924STom N Harris $exts = array(); 20127bf7924STom N Harris if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 20227bf7924STom N Harris while(false !== ($file = readdir($dh))){ 20327bf7924STom N Harris if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 20427bf7924STom N Harris $ext = strtolower($match[1]); 20527bf7924STom N Harris $type = '.'.strtolower($match[2]); 20627bf7924STom N Harris if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 20727bf7924STom N Harris $exts[$ext] = $type; 208d15166e5SAndreas Gohr } 209d15166e5SAndreas Gohr } 2101c2d1019SAndreas Gohr } 21127bf7924STom N Harris closedir($dh); 21227bf7924STom N Harris } 21327bf7924STom N Harris foreach($exts as $ext=>$type){ 21427bf7924STom N Harris $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 215035e07f1SKate Arzamastseva echo ".mf_$class {"; 21627bf7924STom N Harris echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 21727bf7924STom N Harris echo '}'; 21827bf7924STom N Harris } 21927bf7924STom N Harris} 2201c2d1019SAndreas Gohr 2211c2d1019SAndreas Gohr/** 22278a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 22378a6aeb1SAndreas Gohr * given location prefix 22478a6aeb1SAndreas Gohr */ 22578a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){ 22678a6aeb1SAndreas Gohr if(!@file_exists($file)) return ''; 22778a6aeb1SAndreas Gohr $css = io_readFile($file); 22878a6aeb1SAndreas Gohr if(!$location) return $css; 22978a6aeb1SAndreas Gohr 230809d3ba5SAndreas Gohr $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); 231809d3ba5SAndreas Gohr $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); 232809d3ba5SAndreas Gohr 23378a6aeb1SAndreas Gohr return $css; 23478a6aeb1SAndreas Gohr} 23578a6aeb1SAndreas Gohr 236809d3ba5SAndreas Gohr/** 237809d3ba5SAndreas Gohr * Converte local image URLs to data URLs if the filesize is small 238809d3ba5SAndreas Gohr * 239809d3ba5SAndreas Gohr * Callback for preg_replace_callback 240809d3ba5SAndreas Gohr */ 241809d3ba5SAndreas Gohrfunction css_datauri($match){ 24228f4004cSAndreas Gohr global $conf; 24328f4004cSAndreas Gohr 244809d3ba5SAndreas Gohr $pre = unslash($match[1]); 245809d3ba5SAndreas Gohr $base = unslash($match[2]); 246809d3ba5SAndreas Gohr $url = unslash($match[3]); 247809d3ba5SAndreas Gohr $ext = unslash($match[4]); 248809d3ba5SAndreas Gohr 249809d3ba5SAndreas Gohr $local = DOKU_INC.$url; 250809d3ba5SAndreas Gohr $size = @filesize($local); 25128f4004cSAndreas Gohr if($size && $size < $conf['cssdatauri']){ 252809d3ba5SAndreas Gohr $data = base64_encode(file_get_contents($local)); 253809d3ba5SAndreas Gohr } 254809d3ba5SAndreas Gohr if($data){ 255809d3ba5SAndreas Gohr $url = 'data:image/'.$ext.';base64,'.$data; 256809d3ba5SAndreas Gohr }else{ 257809d3ba5SAndreas Gohr $url = $base.$url; 258809d3ba5SAndreas Gohr } 259809d3ba5SAndreas Gohr return $pre.$url; 260809d3ba5SAndreas Gohr} 261809d3ba5SAndreas Gohr 26215c394afSAndreas Gohr 26378a6aeb1SAndreas Gohr/** 26478a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 26578a6aeb1SAndreas Gohr * 26678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 26778a6aeb1SAndreas Gohr */ 268318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){ 269208c0215SAndreas Gohr global $lang; 27078a6aeb1SAndreas Gohr $list = array(); 27178a6aeb1SAndreas Gohr $plugins = plugin_list(); 27278a6aeb1SAndreas Gohr foreach ($plugins as $p){ 273318cd03eSAnika Henke $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 274318cd03eSAnika Henke // alternative for screen.css 275318cd03eSAnika Henke if ($mediatype=='screen') { 27678a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 27778a6aeb1SAndreas Gohr } 278208c0215SAndreas Gohr if($lang['direction'] == 'rtl'){ 279208c0215SAndreas Gohr $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/"; 280208c0215SAndreas Gohr } 28178a6aeb1SAndreas Gohr } 28278a6aeb1SAndreas Gohr return $list; 28378a6aeb1SAndreas Gohr} 28478a6aeb1SAndreas Gohr 28578a6aeb1SAndreas Gohr/** 286f7d780b9SGabriel Birke * Move all @import statements in a combined stylesheet to the top so they 287f7d780b9SGabriel Birke * aren't ignored by the browser. 288f7d780b9SGabriel Birke * 289f7d780b9SGabriel Birke * @author Gabriel Birke <birke@d-scribe.de> 290f7d780b9SGabriel Birke */ 291f7d780b9SGabriel Birkefunction css_moveimports($css) 292f7d780b9SGabriel Birke{ 293f7d780b9SGabriel Birke if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) { 294f7d780b9SGabriel Birke return $css; 295f7d780b9SGabriel Birke } 296f7d780b9SGabriel Birke $newCss = ""; 297f7d780b9SGabriel Birke $imports = ""; 298f7d780b9SGabriel Birke $offset = 0; 299f7d780b9SGabriel Birke foreach($matches[0] as $match) { 300f7d780b9SGabriel Birke $newCss .= substr($css, $offset, $match[1] - $offset); 301f7d780b9SGabriel Birke $imports .= $match[0]; 302f7d780b9SGabriel Birke $offset = $match[1] + strlen($match[0]); 303f7d780b9SGabriel Birke } 304f7d780b9SGabriel Birke $newCss .= substr($css, $offset); 305f7d780b9SGabriel Birke return $imports.$newCss; 306f7d780b9SGabriel Birke} 307f7d780b9SGabriel Birke 308f7d780b9SGabriel Birke/** 30978a6aeb1SAndreas Gohr * Very simple CSS optimizer 31078a6aeb1SAndreas Gohr * 31178a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 31278a6aeb1SAndreas Gohr */ 31378a6aeb1SAndreas Gohrfunction css_compress($css){ 314fd7c2db0SAndreas Gohr //strip comments through a callback 315fd7c2db0SAndreas Gohr $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 316fd7c2db0SAndreas Gohr 317247c1c5dSAndreas Gohr //strip (incorrect but common) one line comments 318fd7c2db0SAndreas Gohr $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 319247c1c5dSAndreas Gohr 32078a6aeb1SAndreas Gohr // strip whitespaces 32178a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!',' ',$css); 322f5379589SChristopher Smith $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css); 323f5379589SChristopher Smith $css = preg_replace('/ ?: /',':',$css); 32478a6aeb1SAndreas Gohr 32578a6aeb1SAndreas Gohr // shorten colors 32678a6aeb1SAndreas 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); 32778a6aeb1SAndreas Gohr 32878a6aeb1SAndreas Gohr return $css; 32978a6aeb1SAndreas Gohr} 33078a6aeb1SAndreas Gohr 331c00aef76SAndreas Gohr/** 332c00aef76SAndreas Gohr * Callback for css_compress() 333c00aef76SAndreas Gohr * 334c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks 335c00aef76SAndreas Gohr * 336c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 337c00aef76SAndreas Gohr */ 338c00aef76SAndreas Gohrfunction css_comment_cb($matches){ 339c00aef76SAndreas Gohr if(strlen($matches[2]) > 4) return ''; 340c00aef76SAndreas Gohr return $matches[0]; 341c00aef76SAndreas Gohr} 34278a6aeb1SAndreas Gohr 343e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 344