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 126c47a78cSAnika Henkeif(!defined('NL')) define('NL',"\n"); 1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php'); 1478a6aeb1SAndreas Gohr 1578a6aeb1SAndreas Gohr// Main (don't run when UNIT test) 1678a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){ 1778a6aeb1SAndreas Gohr header('Content-Type: text/css; charset=utf-8'); 1878a6aeb1SAndreas Gohr css_out(); 1978a6aeb1SAndreas Gohr} 2078a6aeb1SAndreas Gohr 2178a6aeb1SAndreas Gohr 2278a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------ 2378a6aeb1SAndreas Gohr 2478a6aeb1SAndreas Gohr/** 2578a6aeb1SAndreas Gohr * Output all needed Styles 2678a6aeb1SAndreas Gohr * 2778a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2878a6aeb1SAndreas Gohr */ 2978a6aeb1SAndreas Gohrfunction css_out(){ 3078a6aeb1SAndreas Gohr global $conf; 3178a6aeb1SAndreas Gohr global $lang; 3209edb711SAndreas Gohr global $config_cascade; 33bfd0f597STom N Harris global $INPUT; 3409edb711SAndreas Gohr 3529f2dfdcSAndreas Gohr if ($INPUT->str('s') == 'feed') { 366c47a78cSAnika Henke $mediatypes = array('feed'); 376c47a78cSAnika Henke $type = 'feed'; 386c47a78cSAnika Henke } else { 396c47a78cSAnika Henke $mediatypes = array('screen', 'all', 'print'); 406c47a78cSAnika Henke $type = ''; 41615960feSTom N Harris } 4278a6aeb1SAndreas Gohr 43afb2c082SAndreas Gohr // decide from where to get the template 44bfd0f597STom N Harris $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t'))); 45afb2c082SAndreas Gohr if(!$tpl) $tpl = $conf['template']; 460e6f9f08SAnika Henke 4778a6aeb1SAndreas Gohr // The generated script depends on some dynamic options 48afb2c082SAndreas Gohr $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tpl.$type,'.css'); 4978a6aeb1SAndreas Gohr 50afb2c082SAndreas Gohr // load styl.ini 51afb2c082SAndreas Gohr $styleini = css_styleini($tpl); 52afb2c082SAndreas Gohr 53dbf794bfSMichael Hamann // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility 54dbf794bfSMichael Hamann if (isset($config_cascade['userstyle']['default'])) { 55dbf794bfSMichael Hamann $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default']; 56dbf794bfSMichael Hamann } 57dbf794bfSMichael Hamann 58afb2c082SAndreas Gohr // cache influencers 59afb2c082SAndreas Gohr $tplinc = tpl_basedir($tpl); 60afb2c082SAndreas Gohr $cache_files = getConfigFiles('main'); 61afb2c082SAndreas Gohr $cache_files[] = $tplinc.'style.ini'; 62724f2999SAnika Henke $cache_files[] = $tplinc.'style.local.ini'; // @deprecated 63afb2c082SAndreas Gohr $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini"; 64afb2c082SAndreas Gohr $cache_files[] = __FILE__; 65afb2c082SAndreas Gohr 6678a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 6778a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 6878a6aeb1SAndreas Gohr $files = array(); 6914977bd2SMichael Hamann foreach($mediatypes as $mediatype) { 7014977bd2SMichael Hamann $files[$mediatype] = array(); 71318cd03eSAnika Henke // load core styles 7214977bd2SMichael Hamann $files[$mediatype][DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 7343576758SAndreas Gohr // load jQuery-UI theme 746c47a78cSAnika Henke if ($mediatype == 'screen') { 7514977bd2SMichael Hamann $files[$mediatype][DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/'; 766c47a78cSAnika Henke } 77318cd03eSAnika Henke // load plugin styles 7814977bd2SMichael Hamann $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype)); 79318cd03eSAnika Henke // load template styles 80afb2c082SAndreas Gohr if (isset($styleini['stylesheets'][$mediatype])) { 81afb2c082SAndreas Gohr $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets'][$mediatype]); 8209edb711SAndreas Gohr } 83318cd03eSAnika Henke // load user styles 84318cd03eSAnika Henke if(isset($config_cascade['userstyle'][$mediatype])){ 8514977bd2SMichael Hamann $files[$mediatype][$config_cascade['userstyle'][$mediatype]] = DOKU_BASE; 86318cd03eSAnika Henke } 8778a6aeb1SAndreas Gohr 8814977bd2SMichael Hamann $cache_files = array_merge($cache_files, array_keys($files[$mediatype])); 8914977bd2SMichael Hamann } 906619f42eSAdrian Lang 9138f56bffSBen Coburn // check cache age & handle conditional request 926619f42eSAdrian Lang // This may exit if a cache can be used 936619f42eSAdrian Lang http_cached($cache->cache, 946619f42eSAdrian Lang $cache->useCache(array('files' => $cache_files))); 9578a6aeb1SAndreas Gohr 963899c2ecSMichael Hamann // start output buffering 973899c2ecSMichael Hamann ob_start(); 983899c2ecSMichael Hamann 996c47a78cSAnika Henke // build the stylesheet 10014977bd2SMichael Hamann foreach ($mediatypes as $mediatype) { 10178a6aeb1SAndreas Gohr 102d15166e5SAndreas Gohr // print the default classes for interwiki links and file downloads 1036c47a78cSAnika Henke if ($mediatype == 'screen') { 104cacfb606SAnika Henke print '@media screen {'; 1051c2d1019SAndreas Gohr css_interwiki(); 106d15166e5SAndreas Gohr css_filetypes(); 107cacfb606SAnika Henke print '}'; 10878a6aeb1SAndreas Gohr } 10978a6aeb1SAndreas Gohr 1106c47a78cSAnika Henke // load files 1116c47a78cSAnika Henke $css_content = ''; 11214977bd2SMichael Hamann foreach($files[$mediatype] as $file => $location){ 11372a66eb7SAndreas Gohr $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 11472a66eb7SAndreas Gohr $css_content .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 1156c47a78cSAnika Henke $css_content .= css_loadfile($file, $location); 1166c47a78cSAnika Henke } 1176c47a78cSAnika Henke switch ($mediatype) { 1186c47a78cSAnika Henke case 'screen': 1196c47a78cSAnika Henke print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL; 1206c47a78cSAnika Henke break; 1216c47a78cSAnika Henke case 'print': 1226c47a78cSAnika Henke print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL; 1236c47a78cSAnika Henke break; 1246c47a78cSAnika Henke case 'all': 1256c47a78cSAnika Henke case 'feed': 1266c47a78cSAnika Henke default: 1276c47a78cSAnika Henke print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL; 1286c47a78cSAnika Henke break; 1296c47a78cSAnika Henke } 1306c47a78cSAnika Henke } 13178a6aeb1SAndreas Gohr // end output buffering and get contents 13278a6aeb1SAndreas Gohr $css = ob_get_contents(); 13378a6aeb1SAndreas Gohr ob_end_clean(); 13478a6aeb1SAndreas Gohr 1356e69c1baSAndreas Gohr // apply style replacements 136afb2c082SAndreas Gohr $css = css_applystyle($css, $styleini['replacements']); 1376e69c1baSAndreas Gohr 13872a66eb7SAndreas Gohr // parse less 13972a66eb7SAndreas Gohr $css = css_parseless($css); 140d4a1ece8SAndreas Gohr 14178a6aeb1SAndreas Gohr // compress whitespace and comments 14278a6aeb1SAndreas Gohr if($conf['compress']){ 14378a6aeb1SAndreas Gohr $css = css_compress($css); 14478a6aeb1SAndreas Gohr } 14578a6aeb1SAndreas Gohr 146809d3ba5SAndreas Gohr // embed small images right into the stylesheet 147809d3ba5SAndreas Gohr if($conf['cssdatauri']){ 148809d3ba5SAndreas Gohr $base = preg_quote(DOKU_BASE,'#'); 149809d3ba5SAndreas Gohr $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css); 150809d3ba5SAndreas Gohr } 151809d3ba5SAndreas Gohr 1526619f42eSAdrian Lang http_cached_finish($cache->cache, $css); 15378a6aeb1SAndreas Gohr} 15478a6aeb1SAndreas Gohr 15578a6aeb1SAndreas Gohr/** 15672a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS 15772a66eb7SAndreas Gohr * 15872a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when 15972a66eb7SAndreas Gohr * LESS compilation fails 16072a66eb7SAndreas Gohr * 16172a66eb7SAndreas Gohr * @param $css 16272a66eb7SAndreas Gohr * @return string 16372a66eb7SAndreas Gohr */ 16472a66eb7SAndreas Gohrfunction css_parseless($css) { 16572a66eb7SAndreas Gohr $less = new lessc(); 16630f686ebSChristopher Smith $less->importDir[] = DOKU_INC; 16730f686ebSChristopher Smith 16830f686ebSChristopher Smith if (defined('DOKU_UNITTEST')){ 16930f686ebSChristopher Smith $less->importDir[] = TMP_DIR; 17030f686ebSChristopher Smith } 17130f686ebSChristopher Smith 17272a66eb7SAndreas Gohr try { 17372a66eb7SAndreas Gohr return $less->compile($css); 17472a66eb7SAndreas Gohr } catch(Exception $e) { 17572a66eb7SAndreas Gohr // get exception message 17672a66eb7SAndreas Gohr $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage()); 17772a66eb7SAndreas Gohr 17872a66eb7SAndreas Gohr // try to use line number to find affected file 17972a66eb7SAndreas Gohr if(preg_match('/line: (\d+)$/', $msg, $m)){ 18072a66eb7SAndreas Gohr $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber 18172a66eb7SAndreas Gohr $lno = $m[1]; 18272a66eb7SAndreas Gohr 18372a66eb7SAndreas Gohr // walk upwards to last include 18472a66eb7SAndreas Gohr $lines = explode("\n", $css); 18572a66eb7SAndreas Gohr for($i=$lno-1; $i>=0; $i--){ 18672a66eb7SAndreas Gohr if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){ 18772a66eb7SAndreas Gohr // we found it, add info to message 18872a66eb7SAndreas Gohr $msg .= ' in '.$m[2].' at line '.($lno-$i); 18972a66eb7SAndreas Gohr break; 19072a66eb7SAndreas Gohr } 19172a66eb7SAndreas Gohr } 19272a66eb7SAndreas Gohr } 19372a66eb7SAndreas Gohr 19472a66eb7SAndreas Gohr // something went wrong 19572a66eb7SAndreas Gohr $error = 'A fatal error occured during compilation of the CSS files. '. 19672a66eb7SAndreas Gohr 'If you recently installed a new plugin or template it '. 19772a66eb7SAndreas Gohr 'might be broken and you should try disabling it again. ['.$msg.']'; 19872a66eb7SAndreas Gohr 19972a66eb7SAndreas Gohr echo ".dokuwiki:before { 20072a66eb7SAndreas Gohr content: '$error'; 20172a66eb7SAndreas Gohr background-color: red; 20272a66eb7SAndreas Gohr display: block; 20372a66eb7SAndreas Gohr background-color: #fcc; 20472a66eb7SAndreas Gohr border-color: #ebb; 20572a66eb7SAndreas Gohr color: #000; 20672a66eb7SAndreas Gohr padding: 0.5em; 20772a66eb7SAndreas Gohr }"; 20872a66eb7SAndreas Gohr 20972a66eb7SAndreas Gohr exit; 21072a66eb7SAndreas Gohr } 21172a66eb7SAndreas Gohr} 21272a66eb7SAndreas Gohr 21372a66eb7SAndreas Gohr/** 2146e69c1baSAndreas Gohr * Does placeholder replacements in the style according to 2156e69c1baSAndreas Gohr * the ones defined in a templates style.ini file 2166e69c1baSAndreas Gohr * 217d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables 218d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix) 219d4a1ece8SAndreas Gohr * 2206e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2216e69c1baSAndreas Gohr */ 222afb2c082SAndreas Gohrfunction css_applystyle($css, $replacements) { 223cbe37079SAndreas Gohr // we convert ini replacements to LESS variable names 224cbe37079SAndreas Gohr // and build a list of variable: value; pairs 225d4a1ece8SAndreas Gohr $less = ''; 226afb2c082SAndreas Gohr foreach((array) $replacements as $key => $value) { 227cbe37079SAndreas Gohr $lkey = trim($key, '_'); 228cbe37079SAndreas Gohr $lkey = '@ini_'.$lkey; 229cbe37079SAndreas Gohr $less .= "$lkey: $value;\n"; 230cbe37079SAndreas Gohr 231afb2c082SAndreas Gohr $replacements[$key] = $lkey; 232d4a1ece8SAndreas Gohr } 233c51b334eSAndreas Gohr 234cbe37079SAndreas Gohr // we now replace all old ini replacements with LESS variables 235afb2c082SAndreas Gohr $css = strtr($css, $replacements); 236cbe37079SAndreas Gohr 237cbe37079SAndreas Gohr // now prepend the list of LESS variables as the very first thing 238c51b334eSAndreas Gohr $css = $less.$css; 2396e69c1baSAndreas Gohr return $css; 2406e69c1baSAndreas Gohr} 2416e69c1baSAndreas Gohr 2426e69c1baSAndreas Gohr/** 243afb2c082SAndreas Gohr * Load style ini contents 2440ac69508SAnika Henke * 245afb2c082SAndreas Gohr * Loads and merges style.ini files from template and config and prepares 246afb2c082SAndreas Gohr * the stylesheet modes 247afb2c082SAndreas Gohr * 248afb2c082SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 249afb2c082SAndreas Gohr * @param string $tpl the used template 250afb2c082SAndreas Gohr * @return array with keys 'stylesheets' and 'replacements' 2510e6f9f08SAnika Henke */ 252afb2c082SAndreas Gohrfunction css_styleini($tpl) { 253afb2c082SAndreas Gohr $stylesheets = array(); // mode, file => base 254afb2c082SAndreas Gohr $replacements = array(); // placeholder => value 2550ac69508SAnika Henke 256afb2c082SAndreas Gohr // load template's style.ini 257afb2c082SAndreas Gohr $incbase = tpl_incdir($tpl); 258afb2c082SAndreas Gohr $webbase = tpl_basedir($tpl); 259afb2c082SAndreas Gohr $ini = $incbase.'style.ini'; 260afb2c082SAndreas Gohr if(file_exists($ini)){ 261afb2c082SAndreas Gohr $data = parse_ini_file($ini, true); 2620ac69508SAnika Henke 263afb2c082SAndreas Gohr // stylesheets 264afb2c082SAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 265afb2c082SAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 266afb2c082SAndreas Gohr } 267afb2c082SAndreas Gohr 268afb2c082SAndreas Gohr // replacements 269afb2c082SAndreas Gohr if(is_array($data['replacements'])){ 27047f862d1SChristopher Smith $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); 2710ac69508SAnika Henke } 2720ac69508SAnika Henke } 273afb2c082SAndreas Gohr 274568ffd7eSAndreas Gohr // load template's style.local.ini 275568ffd7eSAndreas Gohr // @deprecated 2013-08-03 276568ffd7eSAndreas Gohr $ini = $incbase.'style.local.ini'; 277568ffd7eSAndreas Gohr if(file_exists($ini)){ 278568ffd7eSAndreas Gohr $data = parse_ini_file($ini, true); 279568ffd7eSAndreas Gohr 280568ffd7eSAndreas Gohr // stylesheets 281568ffd7eSAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 282568ffd7eSAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 283568ffd7eSAndreas Gohr } 284568ffd7eSAndreas Gohr 285568ffd7eSAndreas Gohr // replacements 286568ffd7eSAndreas Gohr if(is_array($data['replacements'])){ 28747f862d1SChristopher Smith $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); 288568ffd7eSAndreas Gohr } 289568ffd7eSAndreas Gohr } 290568ffd7eSAndreas Gohr 291afb2c082SAndreas Gohr // load configs's style.ini 292afb2c082SAndreas Gohr $webbase = DOKU_BASE; 2938c5aad7bSAnika Henke $ini = DOKU_CONF."tpl/$tpl/style.ini"; 2948c5aad7bSAnika Henke $incbase = dirname($ini).'/'; 295afb2c082SAndreas Gohr if(file_exists($ini)){ 296afb2c082SAndreas Gohr $data = parse_ini_file($ini, true); 297afb2c082SAndreas Gohr 298afb2c082SAndreas Gohr // stylesheets 299afb2c082SAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 300afb2c082SAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 3010ac69508SAnika Henke } 302afb2c082SAndreas Gohr 303afb2c082SAndreas Gohr // replacements 304afb2c082SAndreas Gohr if(is_array($data['replacements'])){ 30547f862d1SChristopher Smith $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); 306afb2c082SAndreas Gohr } 307afb2c082SAndreas Gohr } 308afb2c082SAndreas Gohr 309afb2c082SAndreas Gohr return array( 310afb2c082SAndreas Gohr 'stylesheets' => $stylesheets, 311afb2c082SAndreas Gohr 'replacements' => $replacements 312afb2c082SAndreas Gohr ); 3130e6f9f08SAnika Henke} 3140e6f9f08SAnika Henke 3151e2c5948SChristopher Smith/** 3161e2c5948SChristopher Smith * Amend paths used in replacement relative urls, refer FS#2879 3171e2c5948SChristopher Smith * 3181e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 3191e2c5948SChristopher Smith */ 32047f862d1SChristopher Smithfunction css_fixreplacementurls($replacements, $location) { 32147f862d1SChristopher Smith foreach($replacements as $key => $value) { 32247f862d1SChristopher Smith $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value); 32347f862d1SChristopher Smith } 32447f862d1SChristopher Smith return $replacements; 32547f862d1SChristopher Smith} 32647f862d1SChristopher Smith 3270e6f9f08SAnika Henke/** 3281c2d1019SAndreas Gohr * Prints classes for interwikilinks 3291c2d1019SAndreas Gohr * 3301c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 3311c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 3321c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 3331c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 3341c2d1019SAndreas Gohr * overwritten in the template or userstyles. 3351c2d1019SAndreas Gohr * 3361c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3371c2d1019SAndreas Gohr */ 3381c2d1019SAndreas Gohrfunction css_interwiki(){ 3391c2d1019SAndreas Gohr 3401c2d1019SAndreas Gohr // default style 3411c2d1019SAndreas Gohr echo 'a.interwiki {'; 3421c2d1019SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 3437b4ea081Smarklundeberg echo ' padding: 1px 0px 1px 16px;'; 3441c2d1019SAndreas Gohr echo '}'; 3451c2d1019SAndreas Gohr 3461c2d1019SAndreas Gohr // additional styles when icon available 3471c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 3481c2d1019SAndreas Gohr foreach(array_keys($iwlinks) as $iw){ 3499d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 3501c2d1019SAndreas Gohr if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 3519d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3521c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 3531c2d1019SAndreas Gohr echo '}'; 3541c2d1019SAndreas Gohr }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 3559d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3561c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 3571c2d1019SAndreas Gohr echo '}'; 3581c2d1019SAndreas Gohr } 3591c2d1019SAndreas Gohr } 360d15166e5SAndreas Gohr} 3611c2d1019SAndreas Gohr 362d15166e5SAndreas Gohr/** 363d15166e5SAndreas Gohr * Prints classes for file download links 364d15166e5SAndreas Gohr * 365d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 366d15166e5SAndreas Gohr */ 367d15166e5SAndreas Gohrfunction css_filetypes(){ 368d15166e5SAndreas Gohr 369d15166e5SAndreas Gohr // default style 370035e07f1SKate Arzamastseva echo '.mediafile {'; 371d15166e5SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 3725b77caf4SAndreas Gohr echo ' padding-left: 18px;'; 3735b77caf4SAndreas Gohr echo ' padding-bottom: 1px;'; 374d15166e5SAndreas Gohr echo '}'; 375d15166e5SAndreas Gohr 376d15166e5SAndreas Gohr // additional styles when icon available 37727bf7924STom N Harris // scan directory for all icons 37827bf7924STom N Harris $exts = array(); 37927bf7924STom N Harris if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 38027bf7924STom N Harris while(false !== ($file = readdir($dh))){ 38127bf7924STom N Harris if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 38227bf7924STom N Harris $ext = strtolower($match[1]); 38327bf7924STom N Harris $type = '.'.strtolower($match[2]); 38427bf7924STom N Harris if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 38527bf7924STom N Harris $exts[$ext] = $type; 386d15166e5SAndreas Gohr } 387d15166e5SAndreas Gohr } 3881c2d1019SAndreas Gohr } 38927bf7924STom N Harris closedir($dh); 39027bf7924STom N Harris } 39127bf7924STom N Harris foreach($exts as $ext=>$type){ 39227bf7924STom N Harris $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 393035e07f1SKate Arzamastseva echo ".mf_$class {"; 39427bf7924STom N Harris echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 39527bf7924STom N Harris echo '}'; 39627bf7924STom N Harris } 39727bf7924STom N Harris} 3981c2d1019SAndreas Gohr 3991c2d1019SAndreas Gohr/** 40078a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 40178a6aeb1SAndreas Gohr * given location prefix 40278a6aeb1SAndreas Gohr */ 40378a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){ 40412ffbbc3SChristopher Smith $css_file = new DokuCssFile($file); 40512ffbbc3SChristopher Smith return $css_file->load($location); 40612ffbbc3SChristopher Smith} 40712ffbbc3SChristopher Smith 4081e2c5948SChristopher Smith/** 4091e2c5948SChristopher Smith * Helper class to abstract loading of css/less files 4101e2c5948SChristopher Smith * 4111e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 4121e2c5948SChristopher Smith */ 41312ffbbc3SChristopher Smithclass DokuCssFile { 41412ffbbc3SChristopher Smith 4151e2c5948SChristopher Smith protected $filepath; // file system path to the CSS/Less file 4161e2c5948SChristopher Smith protected $location; // base url location of the CSS/Less file 41712ffbbc3SChristopher Smith private $relative_path = null; 41812ffbbc3SChristopher Smith 41912ffbbc3SChristopher Smith public function __construct($file) { 42012ffbbc3SChristopher Smith $this->filepath = $file; 42112ffbbc3SChristopher Smith } 42212ffbbc3SChristopher Smith 4231e2c5948SChristopher Smith /** 4241e2c5948SChristopher Smith * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be 4251e2c5948SChristopher Smith * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC) 4261e2c5948SChristopher Smith * for less files. 4271e2c5948SChristopher Smith * 4281e2c5948SChristopher Smith * @param string $location base url for this file 4291e2c5948SChristopher Smith * @return string the CSS/Less contents of the file 4301e2c5948SChristopher Smith */ 43112ffbbc3SChristopher Smith public function load($location='') { 43212ffbbc3SChristopher Smith if (!@file_exists($this->filepath)) return ''; 43312ffbbc3SChristopher Smith 43412ffbbc3SChristopher Smith $css = io_readFile($this->filepath); 43578a6aeb1SAndreas Gohr if (!$location) return $css; 43678a6aeb1SAndreas Gohr 43712ffbbc3SChristopher Smith $this->location = $location; 438de737055SChristopher Smith 43912ffbbc3SChristopher Smith $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css); 44012ffbbc3SChristopher Smith $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css); 441809d3ba5SAndreas Gohr 44278a6aeb1SAndreas Gohr return $css; 44378a6aeb1SAndreas Gohr } 44478a6aeb1SAndreas Gohr 4451e2c5948SChristopher Smith /** 4461e2c5948SChristopher Smith * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC 4471e2c5948SChristopher Smith * 4481e2c5948SChristopher Smith * @return string relative file system path 4491e2c5948SChristopher Smith */ 45012ffbbc3SChristopher Smith private function getRelativePath(){ 45112ffbbc3SChristopher Smith 45212ffbbc3SChristopher Smith if (is_null($this->relative_path)) { 45312ffbbc3SChristopher Smith $basedir = array(DOKU_INC); 4541e2c5948SChristopher Smith 4551e2c5948SChristopher Smith // during testing, files may be found relative to a second base dir, TMP_DIR 45612ffbbc3SChristopher Smith if (defined('DOKU_UNITTEST')) { 45712ffbbc3SChristopher Smith $basedir[] = realpath(TMP_DIR); 45812ffbbc3SChristopher Smith } 45912ffbbc3SChristopher Smith 460*73f25ac0SAndreas Gohr $basedir = array_map('preg_quote_cb', $basedir); 461*73f25ac0SAndreas Gohr $regex = '/^('.join('|',$basedir).')/'; 46212ffbbc3SChristopher Smith $this->relative_path = preg_replace($regex, '', dirname($this->filepath)); 46312ffbbc3SChristopher Smith } 46412ffbbc3SChristopher Smith 46512ffbbc3SChristopher Smith return $this->relative_path; 46612ffbbc3SChristopher Smith } 46712ffbbc3SChristopher Smith 4681e2c5948SChristopher Smith /** 4691e2c5948SChristopher Smith * preg_replace callback to adjust relative urls from relative to this file to relative 4701e2c5948SChristopher Smith * to the appropriate dokuwiki root location as described in the code 4711e2c5948SChristopher Smith * 4721e2c5948SChristopher Smith * @param array see http://php.net/preg_replace_callback 4731e2c5948SChristopher Smith * @return string see http://php.net/preg_replace_callback 4741e2c5948SChristopher Smith */ 47512ffbbc3SChristopher Smith public function replacements($match) { 476de737055SChristopher Smith 4771e2c5948SChristopher Smith // not a relative url? - no adjustment required 478de737055SChristopher Smith if (preg_match('#^(/|data:|https?://)#',$match[3])) { 479de737055SChristopher Smith return $match[0]; 480de737055SChristopher Smith } 4811e2c5948SChristopher Smith // a less file import? - requires a file system location 482de737055SChristopher Smith else if (substr($match[3],-5) == '.less') { 483de737055SChristopher Smith if ($match[3]{0} != '/') { 48412ffbbc3SChristopher Smith $match[3] = $this->getRelativePath() . '/' . $match[3]; 485de737055SChristopher Smith } 486de737055SChristopher Smith } 4871e2c5948SChristopher Smith // everything else requires a url adjustment 488de737055SChristopher Smith else { 48912ffbbc3SChristopher Smith $match[3] = $this->location . $match[3]; 490de737055SChristopher Smith } 491de737055SChristopher Smith 492de737055SChristopher Smith return join('',array_slice($match,1)); 493de737055SChristopher Smith } 49412ffbbc3SChristopher Smith} 495de737055SChristopher Smith 496809d3ba5SAndreas Gohr/** 4974eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small 498809d3ba5SAndreas Gohr * 499809d3ba5SAndreas Gohr * Callback for preg_replace_callback 500809d3ba5SAndreas Gohr */ 501809d3ba5SAndreas Gohrfunction css_datauri($match){ 50228f4004cSAndreas Gohr global $conf; 50328f4004cSAndreas Gohr 504809d3ba5SAndreas Gohr $pre = unslash($match[1]); 505809d3ba5SAndreas Gohr $base = unslash($match[2]); 506809d3ba5SAndreas Gohr $url = unslash($match[3]); 507809d3ba5SAndreas Gohr $ext = unslash($match[4]); 508809d3ba5SAndreas Gohr 509809d3ba5SAndreas Gohr $local = DOKU_INC.$url; 510809d3ba5SAndreas Gohr $size = @filesize($local); 51128f4004cSAndreas Gohr if($size && $size < $conf['cssdatauri']){ 512809d3ba5SAndreas Gohr $data = base64_encode(file_get_contents($local)); 513809d3ba5SAndreas Gohr } 514809d3ba5SAndreas Gohr if($data){ 5156a5d6817SAnika Henke $url = 'data:image/'.$ext.';base64,'.$data; 516809d3ba5SAndreas Gohr }else{ 517809d3ba5SAndreas Gohr $url = $base.$url; 518809d3ba5SAndreas Gohr } 519809d3ba5SAndreas Gohr return $pre.$url; 520809d3ba5SAndreas Gohr} 521809d3ba5SAndreas Gohr 52215c394afSAndreas Gohr 52378a6aeb1SAndreas Gohr/** 52478a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 52578a6aeb1SAndreas Gohr * 52678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 52778a6aeb1SAndreas Gohr */ 528318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){ 529208c0215SAndreas Gohr global $lang; 53078a6aeb1SAndreas Gohr $list = array(); 53178a6aeb1SAndreas Gohr $plugins = plugin_list(); 53278a6aeb1SAndreas Gohr foreach ($plugins as $p){ 533318cd03eSAnika Henke $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 534d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/$mediatype.less"] = DOKU_BASE."lib/plugins/$p/"; 535318cd03eSAnika Henke // alternative for screen.css 536318cd03eSAnika Henke if ($mediatype=='screen') { 53778a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 538d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/style.less"] = DOKU_BASE."lib/plugins/$p/"; 53978a6aeb1SAndreas Gohr } 54078a6aeb1SAndreas Gohr } 54178a6aeb1SAndreas Gohr return $list; 54278a6aeb1SAndreas Gohr} 54378a6aeb1SAndreas Gohr 54478a6aeb1SAndreas Gohr/** 54578a6aeb1SAndreas Gohr * Very simple CSS optimizer 54678a6aeb1SAndreas Gohr * 54778a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 54878a6aeb1SAndreas Gohr */ 54978a6aeb1SAndreas Gohrfunction css_compress($css){ 550fd7c2db0SAndreas Gohr //strip comments through a callback 551fd7c2db0SAndreas Gohr $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 552fd7c2db0SAndreas Gohr 553247c1c5dSAndreas Gohr //strip (incorrect but common) one line comments 554fd7c2db0SAndreas Gohr $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 555247c1c5dSAndreas Gohr 55678a6aeb1SAndreas Gohr // strip whitespaces 55778a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!',' ',$css); 558f5379589SChristopher Smith $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css); 559f5379589SChristopher Smith $css = preg_replace('/ ?: /',':',$css); 56078a6aeb1SAndreas Gohr 561205907a7Sfurun // number compression 562205907a7Sfurun $css = preg_replace('/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2$3', $css); // "0.1em" to ".1em", "1.10em" to "1.1em" 563205907a7Sfurun $css = preg_replace('/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2', $css); // ".0em" to "0" 564205907a7Sfurun $css = preg_replace('/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1', $css); // "0.0em" to "0" 565205907a7Sfurun $css = preg_replace('/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$3', $css); // "1.0em" to "1em" 566205907a7Sfurun $css = preg_replace('/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$2$3', $css); // "001em" to "1em" 567205907a7Sfurun 568205907a7Sfurun // shorten attributes (1em 1em 1em 1em -> 1em) 569205907a7Sfurun $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/', '$1$2', $css); // "1em 1em 1em 1em" to "1em" 570205907a7Sfurun $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/', '$1$2 $3', $css); // "1em 2em 1em 2em" to "1em 2em" 571205907a7Sfurun 57278a6aeb1SAndreas Gohr // shorten colors 573205907a7Sfurun $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); 57478a6aeb1SAndreas Gohr 57578a6aeb1SAndreas Gohr return $css; 57678a6aeb1SAndreas Gohr} 57778a6aeb1SAndreas Gohr 578c00aef76SAndreas Gohr/** 579c00aef76SAndreas Gohr * Callback for css_compress() 580c00aef76SAndreas Gohr * 581c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks 582c00aef76SAndreas Gohr * 583c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 584c00aef76SAndreas Gohr */ 585c00aef76SAndreas Gohrfunction css_comment_cb($matches){ 586c00aef76SAndreas Gohr if(strlen($matches[2]) > 4) return ''; 587c00aef76SAndreas Gohr return $matches[0]; 588c00aef76SAndreas Gohr} 58978a6aeb1SAndreas Gohr 590e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 591