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 35afb2c082SAndreas Gohr // decide from where to get the template 36bfd0f597STom N Harris $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t'))); 37afb2c082SAndreas Gohr if(!$tpl) $tpl = $conf['template']; 380e6f9f08SAnika Henke 397f253bcdSRainbow Spike // load style.ini 40*4d6524b8SAndreas Gohr $styleini = css_styleini($tpl, $INPUT->bool('preview')); 41afb2c082SAndreas Gohr 42de4634ecSGerry Weißbach // find mediatypes 43de4634ecSGerry Weißbach if ($INPUT->str('s') == 'feed') { 44de4634ecSGerry Weißbach $mediatypes = array('feed'); 45de4634ecSGerry Weißbach $type = 'feed'; 46de4634ecSGerry Weißbach } else { 47de4634ecSGerry Weißbach $mediatypes = array_unique(array_merge(array('screen', 'all', 'print'), array_keys($styleini['stylesheets']))); 48de4634ecSGerry Weißbach $type = ''; 49de4634ecSGerry Weißbach } 50de4634ecSGerry Weißbach 51de4634ecSGerry Weißbach // The generated script depends on some dynamic options 52*4d6524b8SAndreas Gohr $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].$INPUT->int('preview').DOKU_BASE.$tpl.$type,'.css'); 53de4634ecSGerry Weißbach 54dbf794bfSMichael Hamann // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility 55dbf794bfSMichael Hamann if (isset($config_cascade['userstyle']['default'])) { 567b909d5eSGerrit Uitslag $config_cascade['userstyle']['screen'] = array($config_cascade['userstyle']['default']); 57dbf794bfSMichael Hamann } 58dbf794bfSMichael Hamann 59afb2c082SAndreas Gohr // cache influencers 60259571aaSMichal Koutný $tplinc = tpl_incdir($tpl); 61afb2c082SAndreas Gohr $cache_files = getConfigFiles('main'); 62afb2c082SAndreas Gohr $cache_files[] = $tplinc.'style.ini'; 63724f2999SAnika Henke $cache_files[] = $tplinc.'style.local.ini'; // @deprecated 64afb2c082SAndreas Gohr $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini"; 65afb2c082SAndreas Gohr $cache_files[] = __FILE__; 66*4d6524b8SAndreas Gohr if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini'; 67afb2c082SAndreas Gohr 6878a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 6978a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 7078a6aeb1SAndreas Gohr $files = array(); 7114977bd2SMichael Hamann foreach($mediatypes as $mediatype) { 7214977bd2SMichael Hamann $files[$mediatype] = array(); 73318cd03eSAnika Henke // load core styles 7414977bd2SMichael Hamann $files[$mediatype][DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 75af4684acSAndreas Gohr 7643576758SAndreas Gohr // load jQuery-UI theme 776c47a78cSAnika Henke if ($mediatype == 'screen') { 7814977bd2SMichael Hamann $files[$mediatype][DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/'; 796c47a78cSAnika Henke } 80318cd03eSAnika Henke // load plugin styles 8114977bd2SMichael Hamann $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype)); 82318cd03eSAnika Henke // load template styles 83afb2c082SAndreas Gohr if (isset($styleini['stylesheets'][$mediatype])) { 84afb2c082SAndreas Gohr $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets'][$mediatype]); 8509edb711SAndreas Gohr } 86318cd03eSAnika Henke // load user styles 877b909d5eSGerrit Uitslag if(!empty($config_cascade['userstyle'][$mediatype])) { 887b909d5eSGerrit Uitslag foreach($config_cascade['userstyle'][$mediatype] as $userstyle) { 897b909d5eSGerrit Uitslag $files[$mediatype][$userstyle] = DOKU_BASE; 907b909d5eSGerrit Uitslag } 91318cd03eSAnika Henke } 9278a6aeb1SAndreas Gohr 9314977bd2SMichael Hamann $cache_files = array_merge($cache_files, array_keys($files[$mediatype])); 9414977bd2SMichael Hamann } 956619f42eSAdrian Lang 9638f56bffSBen Coburn // check cache age & handle conditional request 976619f42eSAdrian Lang // This may exit if a cache can be used 986619f42eSAdrian Lang http_cached($cache->cache, 996619f42eSAdrian Lang $cache->useCache(array('files' => $cache_files))); 10078a6aeb1SAndreas Gohr 1013899c2ecSMichael Hamann // start output buffering 1023899c2ecSMichael Hamann ob_start(); 1033899c2ecSMichael Hamann 1046c47a78cSAnika Henke // build the stylesheet 10514977bd2SMichael Hamann foreach ($mediatypes as $mediatype) { 10678a6aeb1SAndreas Gohr 107d15166e5SAndreas Gohr // print the default classes for interwiki links and file downloads 1086c47a78cSAnika Henke if ($mediatype == 'screen') { 109cacfb606SAnika Henke print '@media screen {'; 1101c2d1019SAndreas Gohr css_interwiki(); 111d15166e5SAndreas Gohr css_filetypes(); 112cacfb606SAnika Henke print '}'; 11378a6aeb1SAndreas Gohr } 11478a6aeb1SAndreas Gohr 1156c47a78cSAnika Henke // load files 1166c47a78cSAnika Henke $css_content = ''; 11714977bd2SMichael Hamann foreach($files[$mediatype] as $file => $location){ 11872a66eb7SAndreas Gohr $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 11972a66eb7SAndreas Gohr $css_content .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 1206c47a78cSAnika Henke $css_content .= css_loadfile($file, $location); 1216c47a78cSAnika Henke } 1226c47a78cSAnika Henke switch ($mediatype) { 1236c47a78cSAnika Henke case 'screen': 1246c47a78cSAnika Henke print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL; 1256c47a78cSAnika Henke break; 1266c47a78cSAnika Henke case 'print': 1276c47a78cSAnika Henke print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL; 1286c47a78cSAnika Henke break; 1296c47a78cSAnika Henke case 'all': 1306c47a78cSAnika Henke case 'feed': 1316c47a78cSAnika Henke default: 1326c47a78cSAnika Henke print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL; 1336c47a78cSAnika Henke break; 1346c47a78cSAnika Henke } 1356c47a78cSAnika Henke } 13678a6aeb1SAndreas Gohr // end output buffering and get contents 13778a6aeb1SAndreas Gohr $css = ob_get_contents(); 13878a6aeb1SAndreas Gohr ob_end_clean(); 13978a6aeb1SAndreas Gohr 140f8fb2d18SAndreas Gohr // strip any source maps 141f8fb2d18SAndreas Gohr stripsourcemaps($css); 142f8fb2d18SAndreas Gohr 1436e69c1baSAndreas Gohr // apply style replacements 144afb2c082SAndreas Gohr $css = css_applystyle($css, $styleini['replacements']); 1456e69c1baSAndreas Gohr 14672a66eb7SAndreas Gohr // parse less 14772a66eb7SAndreas Gohr $css = css_parseless($css); 148d4a1ece8SAndreas Gohr 14978a6aeb1SAndreas Gohr // compress whitespace and comments 15078a6aeb1SAndreas Gohr if($conf['compress']){ 15178a6aeb1SAndreas Gohr $css = css_compress($css); 15278a6aeb1SAndreas Gohr } 15378a6aeb1SAndreas Gohr 154809d3ba5SAndreas Gohr // embed small images right into the stylesheet 155809d3ba5SAndreas Gohr if($conf['cssdatauri']){ 156809d3ba5SAndreas Gohr $base = preg_quote(DOKU_BASE,'#'); 157809d3ba5SAndreas Gohr $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css); 158809d3ba5SAndreas Gohr } 159809d3ba5SAndreas Gohr 1606619f42eSAdrian Lang http_cached_finish($cache->cache, $css); 16178a6aeb1SAndreas Gohr} 16278a6aeb1SAndreas Gohr 16378a6aeb1SAndreas Gohr/** 16472a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS 16572a66eb7SAndreas Gohr * 16672a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when 16772a66eb7SAndreas Gohr * LESS compilation fails 16872a66eb7SAndreas Gohr * 169253d4b48SGerrit Uitslag * @param string $css 17072a66eb7SAndreas Gohr * @return string 17172a66eb7SAndreas Gohr */ 17272a66eb7SAndreas Gohrfunction css_parseless($css) { 1737d247a3cSGerrit Uitslag global $conf; 1747d247a3cSGerrit Uitslag 17572a66eb7SAndreas Gohr $less = new lessc(); 17630f686ebSChristopher Smith $less->importDir[] = DOKU_INC; 1777d247a3cSGerrit Uitslag $less->setPreserveComments(!$conf['compress']); 17830f686ebSChristopher Smith 17930f686ebSChristopher Smith if (defined('DOKU_UNITTEST')){ 18030f686ebSChristopher Smith $less->importDir[] = TMP_DIR; 18130f686ebSChristopher Smith } 18230f686ebSChristopher Smith 18372a66eb7SAndreas Gohr try { 18472a66eb7SAndreas Gohr return $less->compile($css); 18572a66eb7SAndreas Gohr } catch(Exception $e) { 18672a66eb7SAndreas Gohr // get exception message 18772a66eb7SAndreas Gohr $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage()); 18872a66eb7SAndreas Gohr 18972a66eb7SAndreas Gohr // try to use line number to find affected file 19072a66eb7SAndreas Gohr if(preg_match('/line: (\d+)$/', $msg, $m)){ 19172a66eb7SAndreas Gohr $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber 19272a66eb7SAndreas Gohr $lno = $m[1]; 19372a66eb7SAndreas Gohr 19472a66eb7SAndreas Gohr // walk upwards to last include 19572a66eb7SAndreas Gohr $lines = explode("\n", $css); 19672a66eb7SAndreas Gohr for($i=$lno-1; $i>=0; $i--){ 19772a66eb7SAndreas Gohr if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){ 19872a66eb7SAndreas Gohr // we found it, add info to message 19972a66eb7SAndreas Gohr $msg .= ' in '.$m[2].' at line '.($lno-$i); 20072a66eb7SAndreas Gohr break; 20172a66eb7SAndreas Gohr } 20272a66eb7SAndreas Gohr } 20372a66eb7SAndreas Gohr } 20472a66eb7SAndreas Gohr 20572a66eb7SAndreas Gohr // something went wrong 20672a66eb7SAndreas Gohr $error = 'A fatal error occured during compilation of the CSS files. '. 20772a66eb7SAndreas Gohr 'If you recently installed a new plugin or template it '. 20872a66eb7SAndreas Gohr 'might be broken and you should try disabling it again. ['.$msg.']'; 20972a66eb7SAndreas Gohr 21072a66eb7SAndreas Gohr echo ".dokuwiki:before { 21172a66eb7SAndreas Gohr content: '$error'; 21272a66eb7SAndreas Gohr background-color: red; 21372a66eb7SAndreas Gohr display: block; 21472a66eb7SAndreas Gohr background-color: #fcc; 21572a66eb7SAndreas Gohr border-color: #ebb; 21672a66eb7SAndreas Gohr color: #000; 21772a66eb7SAndreas Gohr padding: 0.5em; 21872a66eb7SAndreas Gohr }"; 21972a66eb7SAndreas Gohr 22072a66eb7SAndreas Gohr exit; 22172a66eb7SAndreas Gohr } 22272a66eb7SAndreas Gohr} 22372a66eb7SAndreas Gohr 22472a66eb7SAndreas Gohr/** 2256e69c1baSAndreas Gohr * Does placeholder replacements in the style according to 2266e69c1baSAndreas Gohr * the ones defined in a templates style.ini file 2276e69c1baSAndreas Gohr * 228d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables 229d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix) 230d4a1ece8SAndreas Gohr * 2316e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 232253d4b48SGerrit Uitslag * 233253d4b48SGerrit Uitslag * @param string $css 234253d4b48SGerrit Uitslag * @param array $replacements array(placeholder => value) 235253d4b48SGerrit Uitslag * @return string 2366e69c1baSAndreas Gohr */ 237afb2c082SAndreas Gohrfunction css_applystyle($css, $replacements) { 238cbe37079SAndreas Gohr // we convert ini replacements to LESS variable names 239cbe37079SAndreas Gohr // and build a list of variable: value; pairs 240d4a1ece8SAndreas Gohr $less = ''; 241afb2c082SAndreas Gohr foreach((array) $replacements as $key => $value) { 242cbe37079SAndreas Gohr $lkey = trim($key, '_'); 243cbe37079SAndreas Gohr $lkey = '@ini_'.$lkey; 244cbe37079SAndreas Gohr $less .= "$lkey: $value;\n"; 245cbe37079SAndreas Gohr 246afb2c082SAndreas Gohr $replacements[$key] = $lkey; 247d4a1ece8SAndreas Gohr } 248c51b334eSAndreas Gohr 249cbe37079SAndreas Gohr // we now replace all old ini replacements with LESS variables 250afb2c082SAndreas Gohr $css = strtr($css, $replacements); 251cbe37079SAndreas Gohr 252cbe37079SAndreas Gohr // now prepend the list of LESS variables as the very first thing 253c51b334eSAndreas Gohr $css = $less.$css; 2546e69c1baSAndreas Gohr return $css; 2556e69c1baSAndreas Gohr} 2566e69c1baSAndreas Gohr 2576e69c1baSAndreas Gohr/** 258afb2c082SAndreas Gohr * Load style ini contents 2590ac69508SAnika Henke * 260afb2c082SAndreas Gohr * Loads and merges style.ini files from template and config and prepares 261afb2c082SAndreas Gohr * the stylesheet modes 262afb2c082SAndreas Gohr * 263afb2c082SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 264253d4b48SGerrit Uitslag * 265afb2c082SAndreas Gohr * @param string $tpl the used template 266*4d6524b8SAndreas Gohr * @param bool $preview load preview replacements 267afb2c082SAndreas Gohr * @return array with keys 'stylesheets' and 'replacements' 2680e6f9f08SAnika Henke */ 269*4d6524b8SAndreas Gohrfunction css_styleini($tpl, $preview=false) { 270*4d6524b8SAndreas Gohr global $conf; 271*4d6524b8SAndreas Gohr 272afb2c082SAndreas Gohr $stylesheets = array(); // mode, file => base 273afb2c082SAndreas Gohr $replacements = array(); // placeholder => value 2740ac69508SAnika Henke 275afb2c082SAndreas Gohr // load template's style.ini 276afb2c082SAndreas Gohr $incbase = tpl_incdir($tpl); 277afb2c082SAndreas Gohr $webbase = tpl_basedir($tpl); 278afb2c082SAndreas Gohr $ini = $incbase.'style.ini'; 279afb2c082SAndreas Gohr if(file_exists($ini)){ 280afb2c082SAndreas Gohr $data = parse_ini_file($ini, true); 2810ac69508SAnika Henke 282afb2c082SAndreas Gohr // stylesheets 283afb2c082SAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 284afb2c082SAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 285afb2c082SAndreas Gohr } 286afb2c082SAndreas Gohr 287afb2c082SAndreas Gohr // replacements 288afb2c082SAndreas Gohr if(is_array($data['replacements'])){ 28947f862d1SChristopher Smith $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); 2900ac69508SAnika Henke } 2910ac69508SAnika Henke } 292afb2c082SAndreas Gohr 293568ffd7eSAndreas Gohr // load template's style.local.ini 294568ffd7eSAndreas Gohr // @deprecated 2013-08-03 295568ffd7eSAndreas Gohr $ini = $incbase.'style.local.ini'; 296568ffd7eSAndreas Gohr if(file_exists($ini)){ 297568ffd7eSAndreas Gohr $data = parse_ini_file($ini, true); 298568ffd7eSAndreas Gohr 299568ffd7eSAndreas Gohr // stylesheets 300568ffd7eSAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 301568ffd7eSAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 302568ffd7eSAndreas Gohr } 303568ffd7eSAndreas Gohr 304568ffd7eSAndreas Gohr // replacements 305568ffd7eSAndreas Gohr if(is_array($data['replacements'])){ 30647f862d1SChristopher Smith $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); 307568ffd7eSAndreas Gohr } 308568ffd7eSAndreas Gohr } 309568ffd7eSAndreas Gohr 310afb2c082SAndreas Gohr // load configs's style.ini 311afb2c082SAndreas Gohr $webbase = DOKU_BASE; 3128c5aad7bSAnika Henke $ini = DOKU_CONF."tpl/$tpl/style.ini"; 3138c5aad7bSAnika Henke $incbase = dirname($ini).'/'; 314afb2c082SAndreas Gohr if(file_exists($ini)){ 315afb2c082SAndreas Gohr $data = parse_ini_file($ini, true); 316afb2c082SAndreas Gohr 317afb2c082SAndreas Gohr // stylesheets 318afb2c082SAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 319afb2c082SAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 3200ac69508SAnika Henke } 321afb2c082SAndreas Gohr 322afb2c082SAndreas Gohr // replacements 323afb2c082SAndreas Gohr if(is_array($data['replacements'])){ 32447f862d1SChristopher Smith $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'],$webbase)); 325afb2c082SAndreas Gohr } 326afb2c082SAndreas Gohr } 327afb2c082SAndreas Gohr 328*4d6524b8SAndreas Gohr // allow replacement overwrites in preview mode 329*4d6524b8SAndreas Gohr if($preview) { 330*4d6524b8SAndreas Gohr $webbase = DOKU_BASE; 331*4d6524b8SAndreas Gohr $ini = $conf['cachedir'].'/preview.ini'; 332*4d6524b8SAndreas Gohr if(file_exists($ini)) { 333*4d6524b8SAndreas Gohr $data = parse_ini_file($ini, true); 334*4d6524b8SAndreas Gohr // replacements 335*4d6524b8SAndreas Gohr if(is_array($data['replacements'])) { 336*4d6524b8SAndreas Gohr $replacements = array_merge($replacements, css_fixreplacementurls($data['replacements'], $webbase)); 337*4d6524b8SAndreas Gohr } 338*4d6524b8SAndreas Gohr } 339*4d6524b8SAndreas Gohr } 340*4d6524b8SAndreas Gohr 341afb2c082SAndreas Gohr return array( 342afb2c082SAndreas Gohr 'stylesheets' => $stylesheets, 343afb2c082SAndreas Gohr 'replacements' => $replacements 344afb2c082SAndreas Gohr ); 3450e6f9f08SAnika Henke} 3460e6f9f08SAnika Henke 3471e2c5948SChristopher Smith/** 3481e2c5948SChristopher Smith * Amend paths used in replacement relative urls, refer FS#2879 3491e2c5948SChristopher Smith * 3501e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 351253d4b48SGerrit Uitslag * 352253d4b48SGerrit Uitslag * @param array $replacements with key-value pairs 353253d4b48SGerrit Uitslag * @param string $location 354253d4b48SGerrit Uitslag * @return array 3551e2c5948SChristopher Smith */ 35647f862d1SChristopher Smithfunction css_fixreplacementurls($replacements, $location) { 35747f862d1SChristopher Smith foreach($replacements as $key => $value) { 35847f862d1SChristopher Smith $replacements[$key] = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$value); 35947f862d1SChristopher Smith } 36047f862d1SChristopher Smith return $replacements; 36147f862d1SChristopher Smith} 36247f862d1SChristopher Smith 3630e6f9f08SAnika Henke/** 3641c2d1019SAndreas Gohr * Prints classes for interwikilinks 3651c2d1019SAndreas Gohr * 3661c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 3671c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 3681c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 3691c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 3701c2d1019SAndreas Gohr * overwritten in the template or userstyles. 3711c2d1019SAndreas Gohr * 3721c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3731c2d1019SAndreas Gohr */ 3741c2d1019SAndreas Gohrfunction css_interwiki(){ 3751c2d1019SAndreas Gohr 3761c2d1019SAndreas Gohr // default style 3771c2d1019SAndreas Gohr echo 'a.interwiki {'; 3781c2d1019SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 3797b4ea081Smarklundeberg echo ' padding: 1px 0px 1px 16px;'; 3801c2d1019SAndreas Gohr echo '}'; 3811c2d1019SAndreas Gohr 3821c2d1019SAndreas Gohr // additional styles when icon available 3831c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 3841c2d1019SAndreas Gohr foreach(array_keys($iwlinks) as $iw){ 3859d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 38679e79377SAndreas Gohr if(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 3879d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3881c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 3891c2d1019SAndreas Gohr echo '}'; 39079e79377SAndreas Gohr }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 3919d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3921c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 3931c2d1019SAndreas Gohr echo '}'; 3941c2d1019SAndreas Gohr } 3951c2d1019SAndreas Gohr } 396d15166e5SAndreas Gohr} 3971c2d1019SAndreas Gohr 398d15166e5SAndreas Gohr/** 399d15166e5SAndreas Gohr * Prints classes for file download links 400d15166e5SAndreas Gohr * 401d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 402d15166e5SAndreas Gohr */ 403d15166e5SAndreas Gohrfunction css_filetypes(){ 404d15166e5SAndreas Gohr 405d15166e5SAndreas Gohr // default style 406035e07f1SKate Arzamastseva echo '.mediafile {'; 407d15166e5SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 4085b77caf4SAndreas Gohr echo ' padding-left: 18px;'; 4095b77caf4SAndreas Gohr echo ' padding-bottom: 1px;'; 410d15166e5SAndreas Gohr echo '}'; 411d15166e5SAndreas Gohr 412d15166e5SAndreas Gohr // additional styles when icon available 41327bf7924STom N Harris // scan directory for all icons 41427bf7924STom N Harris $exts = array(); 41527bf7924STom N Harris if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 41627bf7924STom N Harris while(false !== ($file = readdir($dh))){ 41727bf7924STom N Harris if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 41827bf7924STom N Harris $ext = strtolower($match[1]); 41927bf7924STom N Harris $type = '.'.strtolower($match[2]); 42027bf7924STom N Harris if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 42127bf7924STom N Harris $exts[$ext] = $type; 422d15166e5SAndreas Gohr } 423d15166e5SAndreas Gohr } 4241c2d1019SAndreas Gohr } 42527bf7924STom N Harris closedir($dh); 42627bf7924STom N Harris } 42727bf7924STom N Harris foreach($exts as $ext=>$type){ 42827bf7924STom N Harris $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 429035e07f1SKate Arzamastseva echo ".mf_$class {"; 43027bf7924STom N Harris echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 43127bf7924STom N Harris echo '}'; 43227bf7924STom N Harris } 43327bf7924STom N Harris} 4341c2d1019SAndreas Gohr 4351c2d1019SAndreas Gohr/** 43678a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 43778a6aeb1SAndreas Gohr * given location prefix 438253d4b48SGerrit Uitslag * 439253d4b48SGerrit Uitslag * @param string $file file system path 440253d4b48SGerrit Uitslag * @param string $location 441253d4b48SGerrit Uitslag * @return string 44278a6aeb1SAndreas Gohr */ 44378a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){ 44412ffbbc3SChristopher Smith $css_file = new DokuCssFile($file); 44512ffbbc3SChristopher Smith return $css_file->load($location); 44612ffbbc3SChristopher Smith} 44712ffbbc3SChristopher Smith 4481e2c5948SChristopher Smith/** 4491e2c5948SChristopher Smith * Helper class to abstract loading of css/less files 4501e2c5948SChristopher Smith * 4511e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 4521e2c5948SChristopher Smith */ 45312ffbbc3SChristopher Smithclass DokuCssFile { 45412ffbbc3SChristopher Smith 4551e2c5948SChristopher Smith protected $filepath; // file system path to the CSS/Less file 4561e2c5948SChristopher Smith protected $location; // base url location of the CSS/Less file 457fd18b5f4SGerrit Uitslag protected $relative_path = null; 45812ffbbc3SChristopher Smith 45912ffbbc3SChristopher Smith public function __construct($file) { 46012ffbbc3SChristopher Smith $this->filepath = $file; 46112ffbbc3SChristopher Smith } 46212ffbbc3SChristopher Smith 4631e2c5948SChristopher Smith /** 4641e2c5948SChristopher Smith * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be 4651e2c5948SChristopher Smith * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC) 4661e2c5948SChristopher Smith * for less files. 4671e2c5948SChristopher Smith * 4681e2c5948SChristopher Smith * @param string $location base url for this file 4691e2c5948SChristopher Smith * @return string the CSS/Less contents of the file 4701e2c5948SChristopher Smith */ 47112ffbbc3SChristopher Smith public function load($location='') { 47279e79377SAndreas Gohr if (!file_exists($this->filepath)) return ''; 47312ffbbc3SChristopher Smith 47412ffbbc3SChristopher Smith $css = io_readFile($this->filepath); 47578a6aeb1SAndreas Gohr if (!$location) return $css; 47678a6aeb1SAndreas Gohr 47712ffbbc3SChristopher Smith $this->location = $location; 478de737055SChristopher Smith 47912ffbbc3SChristopher Smith $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#',array($this,'replacements'),$css); 48012ffbbc3SChristopher Smith $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#',array($this,'replacements'),$css); 481809d3ba5SAndreas Gohr 48278a6aeb1SAndreas Gohr return $css; 48378a6aeb1SAndreas Gohr } 48478a6aeb1SAndreas Gohr 4851e2c5948SChristopher Smith /** 4861e2c5948SChristopher Smith * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC 4871e2c5948SChristopher Smith * 4881e2c5948SChristopher Smith * @return string relative file system path 4891e2c5948SChristopher Smith */ 490fd18b5f4SGerrit Uitslag protected function getRelativePath(){ 49112ffbbc3SChristopher Smith 49212ffbbc3SChristopher Smith if (is_null($this->relative_path)) { 49312ffbbc3SChristopher Smith $basedir = array(DOKU_INC); 4941e2c5948SChristopher Smith 4951e2c5948SChristopher Smith // during testing, files may be found relative to a second base dir, TMP_DIR 49612ffbbc3SChristopher Smith if (defined('DOKU_UNITTEST')) { 49712ffbbc3SChristopher Smith $basedir[] = realpath(TMP_DIR); 49812ffbbc3SChristopher Smith } 49912ffbbc3SChristopher Smith 50073f25ac0SAndreas Gohr $basedir = array_map('preg_quote_cb', $basedir); 50173f25ac0SAndreas Gohr $regex = '/^('.join('|',$basedir).')/'; 50212ffbbc3SChristopher Smith $this->relative_path = preg_replace($regex, '', dirname($this->filepath)); 50312ffbbc3SChristopher Smith } 50412ffbbc3SChristopher Smith 50512ffbbc3SChristopher Smith return $this->relative_path; 50612ffbbc3SChristopher Smith } 50712ffbbc3SChristopher Smith 5081e2c5948SChristopher Smith /** 5091e2c5948SChristopher Smith * preg_replace callback to adjust relative urls from relative to this file to relative 5101e2c5948SChristopher Smith * to the appropriate dokuwiki root location as described in the code 5111e2c5948SChristopher Smith * 5121e2c5948SChristopher Smith * @param array see http://php.net/preg_replace_callback 5131e2c5948SChristopher Smith * @return string see http://php.net/preg_replace_callback 5141e2c5948SChristopher Smith */ 51512ffbbc3SChristopher Smith public function replacements($match) { 516de737055SChristopher Smith 5171e2c5948SChristopher Smith // not a relative url? - no adjustment required 518de737055SChristopher Smith if (preg_match('#^(/|data:|https?://)#',$match[3])) { 519de737055SChristopher Smith return $match[0]; 520de737055SChristopher Smith } 5211e2c5948SChristopher Smith // a less file import? - requires a file system location 522de737055SChristopher Smith else if (substr($match[3],-5) == '.less') { 523de737055SChristopher Smith if ($match[3]{0} != '/') { 52412ffbbc3SChristopher Smith $match[3] = $this->getRelativePath() . '/' . $match[3]; 525de737055SChristopher Smith } 526de737055SChristopher Smith } 5271e2c5948SChristopher Smith // everything else requires a url adjustment 528de737055SChristopher Smith else { 52912ffbbc3SChristopher Smith $match[3] = $this->location . $match[3]; 530de737055SChristopher Smith } 531de737055SChristopher Smith 532de737055SChristopher Smith return join('',array_slice($match,1)); 533de737055SChristopher Smith } 53412ffbbc3SChristopher Smith} 535de737055SChristopher Smith 536809d3ba5SAndreas Gohr/** 5374eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small 538809d3ba5SAndreas Gohr * 539809d3ba5SAndreas Gohr * Callback for preg_replace_callback 540253d4b48SGerrit Uitslag * 541253d4b48SGerrit Uitslag * @param array $match 542253d4b48SGerrit Uitslag * @return string 543809d3ba5SAndreas Gohr */ 544809d3ba5SAndreas Gohrfunction css_datauri($match){ 54528f4004cSAndreas Gohr global $conf; 54628f4004cSAndreas Gohr 547809d3ba5SAndreas Gohr $pre = unslash($match[1]); 548809d3ba5SAndreas Gohr $base = unslash($match[2]); 549809d3ba5SAndreas Gohr $url = unslash($match[3]); 550809d3ba5SAndreas Gohr $ext = unslash($match[4]); 551809d3ba5SAndreas Gohr 552809d3ba5SAndreas Gohr $local = DOKU_INC.$url; 553809d3ba5SAndreas Gohr $size = @filesize($local); 55428f4004cSAndreas Gohr if($size && $size < $conf['cssdatauri']){ 555809d3ba5SAndreas Gohr $data = base64_encode(file_get_contents($local)); 556809d3ba5SAndreas Gohr } 557809d3ba5SAndreas Gohr if($data){ 5586a5d6817SAnika Henke $url = 'data:image/'.$ext.';base64,'.$data; 559809d3ba5SAndreas Gohr }else{ 560809d3ba5SAndreas Gohr $url = $base.$url; 561809d3ba5SAndreas Gohr } 562809d3ba5SAndreas Gohr return $pre.$url; 563809d3ba5SAndreas Gohr} 564809d3ba5SAndreas Gohr 56515c394afSAndreas Gohr 56678a6aeb1SAndreas Gohr/** 56778a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 56878a6aeb1SAndreas Gohr * 56978a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 570253d4b48SGerrit Uitslag * 571253d4b48SGerrit Uitslag * @param string $mediatype 572253d4b48SGerrit Uitslag * @return array 57378a6aeb1SAndreas Gohr */ 574318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){ 57578a6aeb1SAndreas Gohr $list = array(); 57678a6aeb1SAndreas Gohr $plugins = plugin_list(); 57778a6aeb1SAndreas Gohr foreach ($plugins as $p){ 578318cd03eSAnika Henke $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 579d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/$mediatype.less"] = DOKU_BASE."lib/plugins/$p/"; 580318cd03eSAnika Henke // alternative for screen.css 581318cd03eSAnika Henke if ($mediatype=='screen') { 58278a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 583d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/style.less"] = DOKU_BASE."lib/plugins/$p/"; 58478a6aeb1SAndreas Gohr } 58578a6aeb1SAndreas Gohr } 58678a6aeb1SAndreas Gohr return $list; 58778a6aeb1SAndreas Gohr} 58878a6aeb1SAndreas Gohr 58978a6aeb1SAndreas Gohr/** 59078a6aeb1SAndreas Gohr * Very simple CSS optimizer 59178a6aeb1SAndreas Gohr * 59278a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 593253d4b48SGerrit Uitslag * 594253d4b48SGerrit Uitslag * @param string $css 595253d4b48SGerrit Uitslag * @return string 59678a6aeb1SAndreas Gohr */ 59778a6aeb1SAndreas Gohrfunction css_compress($css){ 598fd7c2db0SAndreas Gohr //strip comments through a callback 599fd7c2db0SAndreas Gohr $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 600fd7c2db0SAndreas Gohr 601247c1c5dSAndreas Gohr //strip (incorrect but common) one line comments 602fe5a50c3SAndreas Gohr $css = preg_replace_callback('/^.*\/\/.*$/m','css_onelinecomment_cb',$css); 603247c1c5dSAndreas Gohr 60478a6aeb1SAndreas Gohr // strip whitespaces 60578a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!',' ',$css); 606f5379589SChristopher Smith $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css); 607f5379589SChristopher Smith $css = preg_replace('/ ?: /',':',$css); 60878a6aeb1SAndreas Gohr 609205907a7Sfurun // number compression 610205907a7Sfurun $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" 611205907a7Sfurun $css = preg_replace('/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', '$1$2', $css); // ".0em" to "0" 612205907a7Sfurun $css = preg_replace('/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1', $css); // "0.0em" to "0" 613205907a7Sfurun $css = preg_replace('/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$3', $css); // "1.0em" to "1em" 614205907a7Sfurun $css = preg_replace('/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', '$1$2$3', $css); // "001em" to "1em" 615205907a7Sfurun 616205907a7Sfurun // shorten attributes (1em 1em 1em 1em -> 1em) 617205907a7Sfurun $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/', '$1$2', $css); // "1em 1em 1em 1em" to "1em" 618205907a7Sfurun $css = preg_replace('/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/', '$1$2 $3', $css); // "1em 2em 1em 2em" to "1em 2em" 619205907a7Sfurun 62078a6aeb1SAndreas Gohr // shorten colors 621205907a7Sfurun $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); 62278a6aeb1SAndreas Gohr 62378a6aeb1SAndreas Gohr return $css; 62478a6aeb1SAndreas Gohr} 62578a6aeb1SAndreas Gohr 626c00aef76SAndreas Gohr/** 627c00aef76SAndreas Gohr * Callback for css_compress() 628c00aef76SAndreas Gohr * 629c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks 630c00aef76SAndreas Gohr * 631c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 632253d4b48SGerrit Uitslag * 633253d4b48SGerrit Uitslag * @param array $matches 634253d4b48SGerrit Uitslag * @return string 635c00aef76SAndreas Gohr */ 636c00aef76SAndreas Gohrfunction css_comment_cb($matches){ 637c00aef76SAndreas Gohr if(strlen($matches[2]) > 4) return ''; 638c00aef76SAndreas Gohr return $matches[0]; 639c00aef76SAndreas Gohr} 64078a6aeb1SAndreas Gohr 641fe5a50c3SAndreas Gohr/** 642fe5a50c3SAndreas Gohr * Callback for css_compress() 643fe5a50c3SAndreas Gohr * 644fe5a50c3SAndreas Gohr * Strips one line comments but makes sure it will not destroy url() constructs with slashes 645fe5a50c3SAndreas Gohr * 646253d4b48SGerrit Uitslag * @param array $matches 647fe5a50c3SAndreas Gohr * @return string 648fe5a50c3SAndreas Gohr */ 649fe5a50c3SAndreas Gohrfunction css_onelinecomment_cb($matches) { 650fe5a50c3SAndreas Gohr $line = $matches[0]; 651fe5a50c3SAndreas Gohr 652fe5a50c3SAndreas Gohr $i = 0; 653fe5a50c3SAndreas Gohr $len = strlen($line); 654918a4468SAndreas Gohr 655fe5a50c3SAndreas Gohr while ($i< $len){ 656fe5a50c3SAndreas Gohr $nextcom = strpos($line, '//', $i); 657fe5a50c3SAndreas Gohr $nexturl = stripos($line, 'url(', $i); 658fe5a50c3SAndreas Gohr 659fe5a50c3SAndreas Gohr if($nextcom === false) { 660fe5a50c3SAndreas Gohr // no more comments, we're done 661918a4468SAndreas Gohr $i = $len; 662fe5a50c3SAndreas Gohr break; 663fe5a50c3SAndreas Gohr } 664fe5a50c3SAndreas Gohr 665918a4468SAndreas Gohr // keep any quoted string that starts before a comment 666918a4468SAndreas Gohr $nextsqt = strpos($line, "'", $i); 667918a4468SAndreas Gohr $nextdqt = strpos($line, '"', $i); 668918a4468SAndreas Gohr if(min($nextsqt, $nextdqt) < $nextcom) { 669918a4468SAndreas Gohr $skipto = false; 670918a4468SAndreas Gohr if($nextsqt !== false && ($nextdqt === false || $nextsqt < $nextdqt)) { 671918a4468SAndreas Gohr $skipto = strpos($line, "'", $nextsqt+1) +1; 672918a4468SAndreas Gohr } else if ($nextdqt !== false) { 673918a4468SAndreas Gohr $skipto = strpos($line, '"', $nextdqt+1) +1; 674918a4468SAndreas Gohr } 675918a4468SAndreas Gohr 676918a4468SAndreas Gohr if($skipto !== false) { 677918a4468SAndreas Gohr $i = $skipto; 678918a4468SAndreas Gohr continue; 679918a4468SAndreas Gohr } 680918a4468SAndreas Gohr } 681918a4468SAndreas Gohr 682918a4468SAndreas Gohr if($nexturl === false || $nextcom < $nexturl) { 683918a4468SAndreas Gohr // no url anymore, strip comment and be done 684918a4468SAndreas Gohr $i = $nextcom; 685918a4468SAndreas Gohr break; 686918a4468SAndreas Gohr } 687918a4468SAndreas Gohr 688918a4468SAndreas Gohr // we have an upcoming url 689918a4468SAndreas Gohr $i = strpos($line, ')', $nexturl); 690918a4468SAndreas Gohr } 691918a4468SAndreas Gohr 692918a4468SAndreas Gohr return substr($line, 0, $i); 693fe5a50c3SAndreas Gohr} 694fe5a50c3SAndreas Gohr 695e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 696