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 */ 8e3c3abf1SAndreas Gohruse dokuwiki\StyleUtils; 90db5771eSMichael Großeuse dokuwiki\Cache\Cache; 10e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 110db5771eSMichael Große 12e1d9dcc8SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC', __DIR__ .'/../../'); 131c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION', true); // we do not use a session or authentication here (better caching) 1498bda4fdSAndreas Gohrif(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); // we gzip ourself here 156c47a78cSAnika Henkeif(!defined('NL')) define('NL', "\n"); 1678a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php'); 1778a6aeb1SAndreas Gohr 1878a6aeb1SAndreas Gohr// Main (don't run when UNIT test) 1978a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){ 2078a6aeb1SAndreas Gohr header('Content-Type: text/css; charset=utf-8'); 2178a6aeb1SAndreas Gohr css_out(); 2278a6aeb1SAndreas Gohr} 2378a6aeb1SAndreas Gohr 2478a6aeb1SAndreas Gohr 2578a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------ 2678a6aeb1SAndreas Gohr 2778a6aeb1SAndreas Gohr/** 2878a6aeb1SAndreas Gohr * Output all needed Styles 2978a6aeb1SAndreas Gohr * 3078a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3178a6aeb1SAndreas Gohr */ 32*d868eb89SAndreas Gohrfunction css_out() 33*d868eb89SAndreas Gohr{ 3478a6aeb1SAndreas Gohr global $conf; 3578a6aeb1SAndreas Gohr global $lang; 3609edb711SAndreas Gohr global $config_cascade; 37bfd0f597STom N Harris global $INPUT; 3809edb711SAndreas Gohr 39de4634ecSGerry Weißbach if ($INPUT->str('s') == 'feed') { 40e3c3abf1SAndreas Gohr $mediatypes = ['feed']; 41de4634ecSGerry Weißbach $type = 'feed'; 42de4634ecSGerry Weißbach } else { 43e3c3abf1SAndreas Gohr $mediatypes = ['screen', 'all', 'print', 'speech']; 44de4634ecSGerry Weißbach $type = ''; 45de4634ecSGerry Weißbach } 46de4634ecSGerry Weißbach 4720a2375aSAndreas Gohr // decide from where to get the template 4820a2375aSAndreas Gohr $tpl = trim(preg_replace('/[^\w-]+/', '', $INPUT->str('t'))); 4920a2375aSAndreas Gohr if(!$tpl) $tpl = $conf['template']; 5020a2375aSAndreas Gohr 5114cd40ecSChang Zhao // load style.ini 52e3c3abf1SAndreas Gohr $styleUtil = new StyleUtils($tpl, $INPUT->bool('preview')); 534593dbd2SAnna Dabrowska $styleini = $styleUtil->cssStyleini(); 5420a2375aSAndreas Gohr 55afb2c082SAndreas Gohr // cache influencers 56259571aaSMichal Koutný $tplinc = tpl_incdir($tpl); 57afb2c082SAndreas Gohr $cache_files = getConfigFiles('main'); 58afb2c082SAndreas Gohr $cache_files[] = $tplinc.'style.ini'; 59afb2c082SAndreas Gohr $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini"; 60afb2c082SAndreas Gohr $cache_files[] = __FILE__; 614d6524b8SAndreas Gohr if($INPUT->bool('preview')) $cache_files[] = $conf['cachedir'].'/preview.ini'; 62afb2c082SAndreas Gohr 6378a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 6478a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 65e3c3abf1SAndreas Gohr $media_files = []; 6614977bd2SMichael Hamann foreach($mediatypes as $mediatype) { 67e3c3abf1SAndreas Gohr $files = []; 683e32b194SGerry Weißbach 69318cd03eSAnika Henke // load core styles 70ef36714bSGerry Weißbach $files[DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 71af4684acSAndreas Gohr 7243576758SAndreas Gohr // load jQuery-UI theme 736c47a78cSAnika Henke if ($mediatype == 'screen') { 7464159a61SAndreas Gohr $files[DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = 7564159a61SAndreas Gohr DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/'; 766c47a78cSAnika Henke } 77318cd03eSAnika Henke // load plugin styles 78ef36714bSGerry Weißbach $files = array_merge($files, css_pluginstyles($mediatype)); 79318cd03eSAnika Henke // load template styles 80afb2c082SAndreas Gohr if (isset($styleini['stylesheets'][$mediatype])) { 81ef36714bSGerry Weißbach $files = array_merge($files, $styleini['stylesheets'][$mediatype]); 8209edb711SAndreas Gohr } 83318cd03eSAnika Henke // load user styles 84e3c3abf1SAndreas Gohr if(isset($config_cascade['userstyle'][$mediatype]) && is_array($config_cascade['userstyle'][$mediatype])) { 857b909d5eSGerrit Uitslag foreach($config_cascade['userstyle'][$mediatype] as $userstyle) { 86ef36714bSGerry Weißbach $files[$userstyle] = DOKU_BASE; 877b909d5eSGerrit Uitslag } 88318cd03eSAnika Henke } 8978a6aeb1SAndreas Gohr 90ef36714bSGerry Weißbach // Let plugins decide to either put more styles here or to remove some 913e32b194SGerry Weißbach $media_files[$mediatype] = css_filewrapper($mediatype, $files); 92e1d9dcc8SAndreas Gohr $CSSEvt = new Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]); 93ef36714bSGerry Weißbach 94ef36714bSGerry Weißbach // Make it preventable. 95ef36714bSGerry Weißbach if ( $CSSEvt->advise_before() ) { 96ef36714bSGerry Weißbach $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files'])); 97ef36714bSGerry Weißbach } else { 98ef36714bSGerry Weißbach // unset if prevented. Nothing will be printed for this mediatype. 99ef36714bSGerry Weißbach unset($media_files[$mediatype]); 10014977bd2SMichael Hamann } 1016619f42eSAdrian Lang 102ef36714bSGerry Weißbach // finish event. 103ef36714bSGerry Weißbach $CSSEvt->advise_after(); 104ef36714bSGerry Weißbach } 105ef36714bSGerry Weißbach 106ef36714bSGerry Weißbach // The generated script depends on some dynamic options 1070db5771eSMichael Große $cache = new Cache( 10864159a61SAndreas Gohr 'styles' . 10964159a61SAndreas Gohr $_SERVER['HTTP_HOST'] . 11064159a61SAndreas Gohr $_SERVER['SERVER_PORT'] . 11164159a61SAndreas Gohr $INPUT->bool('preview') . 11264159a61SAndreas Gohr DOKU_BASE . 11364159a61SAndreas Gohr $tpl . 11464159a61SAndreas Gohr $type, 11564159a61SAndreas Gohr '.css' 11664159a61SAndreas Gohr ); 117dc7da772SMichael Große $cache->setEvent('CSS_CACHE_USE'); 118ef36714bSGerry Weißbach 11938f56bffSBen Coburn // check cache age & handle conditional request 1206619f42eSAdrian Lang // This may exit if a cache can be used 121e3c3abf1SAndreas Gohr $cache_ok = $cache->useCache(['files' => $cache_files]); 122e7c68c4dSGerry Weißbach http_cached($cache->cache, $cache_ok); 12378a6aeb1SAndreas Gohr 1243899c2ecSMichael Hamann // start output buffering 1253899c2ecSMichael Hamann ob_start(); 1263899c2ecSMichael Hamann 127ef36714bSGerry Weißbach // Fire CSS_STYLES_INCLUDED for one last time to let the 128ef36714bSGerry Weißbach // plugins decide whether to include the DW default styles. 129ef36714bSGerry Weißbach // This can be done by preventing the Default. 130ef36714bSGerry Weißbach $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT'); 131cbb44eabSAndreas Gohr Event::createAndTrigger('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles'); 132ef36714bSGerry Weißbach 1336c47a78cSAnika Henke // build the stylesheet 13414977bd2SMichael Hamann foreach ($mediatypes as $mediatype) { 13578a6aeb1SAndreas Gohr 136ef36714bSGerry Weißbach // Check if there is a wrapper set for this type. 137ef36714bSGerry Weißbach if ( !isset($media_files[$mediatype]) ) { 138ef36714bSGerry Weißbach continue; 13978a6aeb1SAndreas Gohr } 14078a6aeb1SAndreas Gohr 141ef36714bSGerry Weißbach $cssData = $media_files[$mediatype]; 142ef36714bSGerry Weißbach 143ef36714bSGerry Weißbach // Print the styles. 144ef36714bSGerry Weißbach print NL; 145ef36714bSGerry Weißbach if ( $cssData['encapsulate'] === true ) print $cssData['encapsulationPrefix'] . ' {'; 146ef36714bSGerry Weißbach print '/* START '.$cssData['mediatype'].' styles */'.NL; 147ef36714bSGerry Weißbach 1486c47a78cSAnika Henke // load files 149ef36714bSGerry Weißbach foreach($cssData['files'] as $file => $location){ 15072a66eb7SAndreas Gohr $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 151ef36714bSGerry Weißbach print "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 152ef36714bSGerry Weißbach print css_loadfile($file, $location); 1536c47a78cSAnika Henke } 154ef36714bSGerry Weißbach 155ef36714bSGerry Weißbach print NL; 156ef36714bSGerry Weißbach if ( $cssData['encapsulate'] === true ) print '} /* /@media '; 157ef36714bSGerry Weißbach else print '/*'; 158ef36714bSGerry Weißbach print ' END '.$cssData['mediatype'].' styles */'.NL; 1596c47a78cSAnika Henke } 160ef36714bSGerry Weißbach 16178a6aeb1SAndreas Gohr // end output buffering and get contents 16278a6aeb1SAndreas Gohr $css = ob_get_contents(); 16378a6aeb1SAndreas Gohr ob_end_clean(); 16478a6aeb1SAndreas Gohr 165f8fb2d18SAndreas Gohr // strip any source maps 166f8fb2d18SAndreas Gohr stripsourcemaps($css); 167f8fb2d18SAndreas Gohr 1686e69c1baSAndreas Gohr // apply style replacements 169afb2c082SAndreas Gohr $css = css_applystyle($css, $styleini['replacements']); 1706e69c1baSAndreas Gohr 17172a66eb7SAndreas Gohr // parse less 17272a66eb7SAndreas Gohr $css = css_parseless($css); 173d4a1ece8SAndreas Gohr 17478a6aeb1SAndreas Gohr // compress whitespace and comments 17578a6aeb1SAndreas Gohr if($conf['compress']){ 17678a6aeb1SAndreas Gohr $css = css_compress($css); 17778a6aeb1SAndreas Gohr } 17878a6aeb1SAndreas Gohr 179809d3ba5SAndreas Gohr // embed small images right into the stylesheet 180809d3ba5SAndreas Gohr if($conf['cssdatauri']){ 181809d3ba5SAndreas Gohr $base = preg_quote(DOKU_BASE, '#'); 182809d3ba5SAndreas Gohr $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i', 'css_datauri', $css); 183809d3ba5SAndreas Gohr } 184809d3ba5SAndreas Gohr 1856619f42eSAdrian Lang http_cached_finish($cache->cache, $css); 18678a6aeb1SAndreas Gohr} 18778a6aeb1SAndreas Gohr 18878a6aeb1SAndreas Gohr/** 18972a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS 19072a66eb7SAndreas Gohr * 19172a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when 19272a66eb7SAndreas Gohr * LESS compilation fails 19372a66eb7SAndreas Gohr * 194253d4b48SGerrit Uitslag * @param string $css 19572a66eb7SAndreas Gohr * @return string 19672a66eb7SAndreas Gohr */ 197*d868eb89SAndreas Gohrfunction css_parseless($css) 198*d868eb89SAndreas Gohr{ 1997d247a3cSGerrit Uitslag global $conf; 2007d247a3cSGerrit Uitslag 20172a66eb7SAndreas Gohr $less = new lessc(); 202e3c3abf1SAndreas Gohr $less->importDir = [DOKU_INC]; 2037d247a3cSGerrit Uitslag $less->setPreserveComments(!$conf['compress']); 20430f686ebSChristopher Smith 20530f686ebSChristopher Smith if (defined('DOKU_UNITTEST')){ 20630f686ebSChristopher Smith $less->importDir[] = TMP_DIR; 20730f686ebSChristopher Smith } 20830f686ebSChristopher Smith 20972a66eb7SAndreas Gohr try { 21072a66eb7SAndreas Gohr return $less->compile($css); 21172a66eb7SAndreas Gohr } catch(Exception $e) { 21272a66eb7SAndreas Gohr // get exception message 213e3c3abf1SAndreas Gohr $msg = str_replace(["\n", "\r", "'"], [], $e->getMessage()); 21472a66eb7SAndreas Gohr 21572a66eb7SAndreas Gohr // try to use line number to find affected file 21672a66eb7SAndreas Gohr if(preg_match('/line: (\d+)$/', $msg, $m)){ 21772a66eb7SAndreas Gohr $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber 21872a66eb7SAndreas Gohr $lno = $m[1]; 21972a66eb7SAndreas Gohr 22072a66eb7SAndreas Gohr // walk upwards to last include 22172a66eb7SAndreas Gohr $lines = explode("\n", $css); 22272a66eb7SAndreas Gohr for($i=$lno-1; $i>=0; $i--){ 22372a66eb7SAndreas Gohr if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){ 22472a66eb7SAndreas Gohr // we found it, add info to message 22572a66eb7SAndreas Gohr $msg .= ' in '.$m[2].' at line '.($lno-$i); 22672a66eb7SAndreas Gohr break; 22772a66eb7SAndreas Gohr } 22872a66eb7SAndreas Gohr } 22972a66eb7SAndreas Gohr } 23072a66eb7SAndreas Gohr 23172a66eb7SAndreas Gohr // something went wrong 23272a66eb7SAndreas Gohr $error = 'A fatal error occured during compilation of the CSS files. '. 23372a66eb7SAndreas Gohr 'If you recently installed a new plugin or template it '. 23472a66eb7SAndreas Gohr 'might be broken and you should try disabling it again. ['.$msg.']'; 23572a66eb7SAndreas Gohr 23672a66eb7SAndreas Gohr echo ".dokuwiki:before { 23772a66eb7SAndreas Gohr content: '$error'; 23872a66eb7SAndreas Gohr background-color: red; 23972a66eb7SAndreas Gohr display: block; 24072a66eb7SAndreas Gohr background-color: #fcc; 24172a66eb7SAndreas Gohr border-color: #ebb; 24272a66eb7SAndreas Gohr color: #000; 24372a66eb7SAndreas Gohr padding: 0.5em; 24472a66eb7SAndreas Gohr }"; 24572a66eb7SAndreas Gohr 24672a66eb7SAndreas Gohr exit; 24772a66eb7SAndreas Gohr } 24872a66eb7SAndreas Gohr} 24972a66eb7SAndreas Gohr 25072a66eb7SAndreas Gohr/** 2516e69c1baSAndreas Gohr * Does placeholder replacements in the style according to 2526e69c1baSAndreas Gohr * the ones defined in a templates style.ini file 2536e69c1baSAndreas Gohr * 254d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables 255d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix) 256d4a1ece8SAndreas Gohr * 2576e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 258253d4b48SGerrit Uitslag * 259253d4b48SGerrit Uitslag * @param string $css 260253d4b48SGerrit Uitslag * @param array $replacements array(placeholder => value) 261253d4b48SGerrit Uitslag * @return string 2626e69c1baSAndreas Gohr */ 263*d868eb89SAndreas Gohrfunction css_applystyle($css, $replacements) 264*d868eb89SAndreas Gohr{ 265cbe37079SAndreas Gohr // we convert ini replacements to LESS variable names 266cbe37079SAndreas Gohr // and build a list of variable: value; pairs 267d4a1ece8SAndreas Gohr $less = ''; 268afb2c082SAndreas Gohr foreach((array) $replacements as $key => $value) { 269cbe37079SAndreas Gohr $lkey = trim($key, '_'); 270cbe37079SAndreas Gohr $lkey = '@ini_'.$lkey; 271cbe37079SAndreas Gohr $less .= "$lkey: $value;\n"; 272cbe37079SAndreas Gohr 273afb2c082SAndreas Gohr $replacements[$key] = $lkey; 274d4a1ece8SAndreas Gohr } 275c51b334eSAndreas Gohr 276cbe37079SAndreas Gohr // we now replace all old ini replacements with LESS variables 277afb2c082SAndreas Gohr $css = strtr($css, $replacements); 278cbe37079SAndreas Gohr 279cbe37079SAndreas Gohr // now prepend the list of LESS variables as the very first thing 280c51b334eSAndreas Gohr $css = $less.$css; 2816e69c1baSAndreas Gohr return $css; 2826e69c1baSAndreas Gohr} 2836e69c1baSAndreas Gohr 2846e69c1baSAndreas Gohr/** 285ef36714bSGerry Weißbach * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED 286ef36714bSGerry Weißbach * 287ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de> 288ef36714bSGerry Weißbach * 289ef36714bSGerry Weißbach * @param string $mediatype type ofthe current media files/content set 290ef36714bSGerry Weißbach * @param array $files set of files that define the current mediatype 291ef36714bSGerry Weißbach * @return array 292ef36714bSGerry Weißbach */ 293*d868eb89SAndreas Gohrfunction css_filewrapper($mediatype, $files = []) 294*d868eb89SAndreas Gohr{ 295e3c3abf1SAndreas Gohr return [ 296ef36714bSGerry Weißbach 'files' => $files, 297ef36714bSGerry Weißbach 'mediatype' => $mediatype, 298cf35519cSGerry Weißbach 'encapsulate' => $mediatype != 'all', 299ef36714bSGerry Weißbach 'encapsulationPrefix' => '@media '.$mediatype 300e3c3abf1SAndreas Gohr ]; 301ef36714bSGerry Weißbach} 302ef36714bSGerry Weißbach 303ef36714bSGerry Weißbach/** 304ef36714bSGerry Weißbach * Prints the @media encapsulated default styles of DokuWiki 305ef36714bSGerry Weißbach * 306ef36714bSGerry Weißbach * @author Gerry Weißbach <gerry.w@gammaproduction.de> 307ef36714bSGerry Weißbach * 308ef36714bSGerry Weißbach * This function is being called by a CSS_STYLES_INCLUDED event 309ef36714bSGerry Weißbach * The event can be distinguished by the mediatype which is: 310ef36714bSGerry Weißbach * DW_DEFAULT 311ef36714bSGerry Weißbach */ 312*d868eb89SAndreas Gohrfunction css_defaultstyles() 313*d868eb89SAndreas Gohr{ 314ef36714bSGerry Weißbach // print the default classes for interwiki links and file downloads 315ef36714bSGerry Weißbach print '@media screen {'; 316ef36714bSGerry Weißbach css_interwiki(); 317ef36714bSGerry Weißbach css_filetypes(); 318ef36714bSGerry Weißbach print '}'; 319ef36714bSGerry Weißbach} 320ef36714bSGerry Weißbach 321ef36714bSGerry Weißbach/** 3221c2d1019SAndreas Gohr * Prints classes for interwikilinks 3231c2d1019SAndreas Gohr * 3241c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 3251c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 3261c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 3271c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 3281c2d1019SAndreas Gohr * overwritten in the template or userstyles. 3291c2d1019SAndreas Gohr * 3301c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3311c2d1019SAndreas Gohr */ 332*d868eb89SAndreas Gohrfunction css_interwiki() 333*d868eb89SAndreas Gohr{ 3341c2d1019SAndreas Gohr 3351c2d1019SAndreas Gohr // default style 3361c2d1019SAndreas Gohr echo 'a.interwiki {'; 3374ef5d38dSAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.svg) 0 0 no-repeat;'; 3381e519eb5SAndreas Gohr echo ' background-size: 1.2em;'; 3391e519eb5SAndreas Gohr echo ' padding: 0 0 0 1.4em;'; 3401c2d1019SAndreas Gohr echo '}'; 3411c2d1019SAndreas Gohr 3421c2d1019SAndreas Gohr // additional styles when icon available 3431c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 3441c2d1019SAndreas Gohr foreach (array_keys($iwlinks) as $iw) { 3459d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $iw); 3461e519eb5SAndreas Gohr foreach (['svg', 'png', 'gif'] as $ext) { 3471e519eb5SAndreas Gohr $file = 'lib/images/interwiki/' . $iw . '.' . $ext; 3481e519eb5SAndreas Gohr 3491e519eb5SAndreas Gohr if (file_exists(DOKU_INC . $file)) { 3509d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3511e519eb5SAndreas Gohr echo ' background-image: url(' . DOKU_BASE . $file . ')'; 3521c2d1019SAndreas Gohr echo '}'; 3531e519eb5SAndreas Gohr break; 3541e519eb5SAndreas Gohr } 3551c2d1019SAndreas Gohr } 3561c2d1019SAndreas Gohr } 357d15166e5SAndreas Gohr} 3581c2d1019SAndreas Gohr 359d15166e5SAndreas Gohr/** 360d15166e5SAndreas Gohr * Prints classes for file download links 361d15166e5SAndreas Gohr * 362d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 363d15166e5SAndreas Gohr */ 364*d868eb89SAndreas Gohrfunction css_filetypes() 365*d868eb89SAndreas Gohr{ 366d15166e5SAndreas Gohr 367d15166e5SAndreas Gohr // default style 368035e07f1SKate Arzamastseva echo '.mediafile {'; 36924115d42SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/svg/file.svg) 0px 1px no-repeat;'; 37024115d42SAndreas Gohr echo ' background-size: 1.2em;'; 37124115d42SAndreas Gohr echo ' padding-left: 1.5em;'; 372d15166e5SAndreas Gohr echo '}'; 373d15166e5SAndreas Gohr 374d15166e5SAndreas Gohr // additional styles when icon available 37527bf7924STom N Harris // scan directory for all icons 376e3c3abf1SAndreas Gohr $exts = []; 37724115d42SAndreas Gohr if($dh = opendir(DOKU_INC.'lib/images/fileicons/svg')){ 37827bf7924STom N Harris while(false !== ($file = readdir($dh))){ 37924115d42SAndreas Gohr if(preg_match('/(.*?)\.svg$/i', $file, $match)){ 38024115d42SAndreas Gohr $exts[] = strtolower($match[1]); 381d15166e5SAndreas Gohr } 3821c2d1019SAndreas Gohr } 38327bf7924STom N Harris closedir($dh); 38427bf7924STom N Harris } 38524115d42SAndreas Gohr foreach($exts as $ext){ 38627bf7924STom N Harris $class = preg_replace('/[^_\-a-z0-9]+/', '_', $ext); 387035e07f1SKate Arzamastseva echo ".mf_$class {"; 38824115d42SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/svg/'.$ext.'.svg)'; 38927bf7924STom N Harris echo '}'; 39027bf7924STom N Harris } 39127bf7924STom N Harris} 3921c2d1019SAndreas Gohr 3931c2d1019SAndreas Gohr/** 39478a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 39578a6aeb1SAndreas Gohr * given location prefix 396253d4b48SGerrit Uitslag * 397253d4b48SGerrit Uitslag * @param string $file file system path 398253d4b48SGerrit Uitslag * @param string $location 399253d4b48SGerrit Uitslag * @return string 40078a6aeb1SAndreas Gohr */ 401*d868eb89SAndreas Gohrfunction css_loadfile($file, $location = '') 402*d868eb89SAndreas Gohr{ 40312ffbbc3SChristopher Smith $css_file = new DokuCssFile($file); 40412ffbbc3SChristopher Smith return $css_file->load($location); 40512ffbbc3SChristopher Smith} 40612ffbbc3SChristopher Smith 4071e2c5948SChristopher Smith/** 4081e2c5948SChristopher Smith * Helper class to abstract loading of css/less files 4091e2c5948SChristopher Smith * 4101e2c5948SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 4111e2c5948SChristopher Smith */ 4128c7c53b0SAndreas Gohrclass DokuCssFile 4138c7c53b0SAndreas Gohr{ 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 417e3c3abf1SAndreas Gohr protected $relative_path; 41812ffbbc3SChristopher Smith 419*d868eb89SAndreas Gohr public function __construct($file) 420*d868eb89SAndreas Gohr { 42112ffbbc3SChristopher Smith $this->filepath = $file; 42212ffbbc3SChristopher Smith } 42312ffbbc3SChristopher Smith 4241e2c5948SChristopher Smith /** 4251e2c5948SChristopher Smith * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be 4261e2c5948SChristopher Smith * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC) 4271e2c5948SChristopher Smith * for less files. 4281e2c5948SChristopher Smith * 4291e2c5948SChristopher Smith * @param string $location base url for this file 4301e2c5948SChristopher Smith * @return string the CSS/Less contents of the file 4311e2c5948SChristopher Smith */ 432*d868eb89SAndreas Gohr public function load($location = '') 433*d868eb89SAndreas Gohr { 43479e79377SAndreas Gohr if (!file_exists($this->filepath)) return ''; 43512ffbbc3SChristopher Smith 43612ffbbc3SChristopher Smith $css = io_readFile($this->filepath); 43778a6aeb1SAndreas Gohr if (!$location) return $css; 43878a6aeb1SAndreas Gohr 43912ffbbc3SChristopher Smith $this->location = $location; 440de737055SChristopher Smith 441e3c3abf1SAndreas Gohr $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#', [$this, 'replacements'], $css); 442e3c3abf1SAndreas Gohr $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#', [$this, 'replacements'], $css); 443809d3ba5SAndreas Gohr 44478a6aeb1SAndreas Gohr return $css; 44578a6aeb1SAndreas Gohr } 44678a6aeb1SAndreas Gohr 4471e2c5948SChristopher Smith /** 4481e2c5948SChristopher Smith * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC 4491e2c5948SChristopher Smith * 4501e2c5948SChristopher Smith * @return string relative file system path 4511e2c5948SChristopher Smith */ 452*d868eb89SAndreas Gohr protected function getRelativePath() 453*d868eb89SAndreas Gohr { 45412ffbbc3SChristopher Smith 45512ffbbc3SChristopher Smith if (is_null($this->relative_path)) { 456e3c3abf1SAndreas Gohr $basedir = [DOKU_INC]; 4571e2c5948SChristopher Smith 4581e2c5948SChristopher Smith // during testing, files may be found relative to a second base dir, TMP_DIR 45912ffbbc3SChristopher Smith if (defined('DOKU_UNITTEST')) { 46012ffbbc3SChristopher Smith $basedir[] = realpath(TMP_DIR); 46112ffbbc3SChristopher Smith } 46212ffbbc3SChristopher Smith 46373f25ac0SAndreas Gohr $basedir = array_map('preg_quote_cb', $basedir); 464e3c3abf1SAndreas Gohr $regex = '/^('.implode('|', $basedir).')/'; 46512ffbbc3SChristopher Smith $this->relative_path = preg_replace($regex, '', dirname($this->filepath)); 46612ffbbc3SChristopher Smith } 46712ffbbc3SChristopher Smith 46812ffbbc3SChristopher Smith return $this->relative_path; 46912ffbbc3SChristopher Smith } 47012ffbbc3SChristopher Smith 4711e2c5948SChristopher Smith /** 4721e2c5948SChristopher Smith * preg_replace callback to adjust relative urls from relative to this file to relative 4731e2c5948SChristopher Smith * to the appropriate dokuwiki root location as described in the code 4741e2c5948SChristopher Smith * 4751e2c5948SChristopher Smith * @param array see http://php.net/preg_replace_callback 4761e2c5948SChristopher Smith * @return string see http://php.net/preg_replace_callback 4771e2c5948SChristopher Smith */ 478*d868eb89SAndreas Gohr public function replacements($match) 479*d868eb89SAndreas Gohr { 480de737055SChristopher Smith 481e24a74c0SAndreas Gohr if (preg_match('#^(/|data:|https?://)#', $match[3])) { // not a relative url? - no adjustment required 482de737055SChristopher Smith return $match[0]; 483e24a74c0SAndreas Gohr } elseif (substr($match[3], -5) == '.less') { // a less file import? - requires a file system location 4845312cb0bSSyntaxseed if ($match[3][0] != '/') { 48512ffbbc3SChristopher Smith $match[3] = $this->getRelativePath() . '/' . $match[3]; 486de737055SChristopher Smith } 487e24a74c0SAndreas Gohr } else { // everything else requires a url adjustment 48812ffbbc3SChristopher Smith $match[3] = $this->location . $match[3]; 489de737055SChristopher Smith } 490de737055SChristopher Smith 491e3c3abf1SAndreas Gohr return implode('', array_slice($match, 1)); 492de737055SChristopher Smith } 49312ffbbc3SChristopher Smith} 494de737055SChristopher Smith 495809d3ba5SAndreas Gohr/** 4964eb5f931SChristopher Smith * Convert local image URLs to data URLs if the filesize is small 497809d3ba5SAndreas Gohr * 498809d3ba5SAndreas Gohr * Callback for preg_replace_callback 499253d4b48SGerrit Uitslag * 500253d4b48SGerrit Uitslag * @param array $match 501253d4b48SGerrit Uitslag * @return string 502809d3ba5SAndreas Gohr */ 503*d868eb89SAndreas Gohrfunction css_datauri($match) 504*d868eb89SAndreas Gohr{ 50528f4004cSAndreas Gohr global $conf; 50628f4004cSAndreas Gohr 507809d3ba5SAndreas Gohr $pre = unslash($match[1]); 508809d3ba5SAndreas Gohr $base = unslash($match[2]); 509809d3ba5SAndreas Gohr $url = unslash($match[3]); 510809d3ba5SAndreas Gohr $ext = unslash($match[4]); 511809d3ba5SAndreas Gohr 512809d3ba5SAndreas Gohr $local = DOKU_INC.$url; 513809d3ba5SAndreas Gohr $size = @filesize($local); 51428f4004cSAndreas Gohr if($size && $size < $conf['cssdatauri']){ 515809d3ba5SAndreas Gohr $data = base64_encode(file_get_contents($local)); 516809d3ba5SAndreas Gohr } 5178f34cf3dSMichael Große if (!empty($data)){ 5186a5d6817SAnika Henke $url = 'data:image/'.$ext.';base64,'.$data; 519809d3ba5SAndreas Gohr }else{ 520809d3ba5SAndreas Gohr $url = $base.$url; 521809d3ba5SAndreas Gohr } 522809d3ba5SAndreas Gohr return $pre.$url; 523809d3ba5SAndreas Gohr} 524809d3ba5SAndreas Gohr 52515c394afSAndreas Gohr 52678a6aeb1SAndreas Gohr/** 52778a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 52878a6aeb1SAndreas Gohr * 52978a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 530253d4b48SGerrit Uitslag * 531253d4b48SGerrit Uitslag * @param string $mediatype 532253d4b48SGerrit Uitslag * @return array 53378a6aeb1SAndreas Gohr */ 534*d868eb89SAndreas Gohrfunction css_pluginstyles($mediatype = 'screen') 535*d868eb89SAndreas Gohr{ 536e3c3abf1SAndreas Gohr $list = []; 53778a6aeb1SAndreas Gohr $plugins = plugin_list(); 53878a6aeb1SAndreas Gohr foreach ($plugins as $p){ 539318cd03eSAnika Henke $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 540d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/$mediatype.less"] = DOKU_BASE."lib/plugins/$p/"; 541318cd03eSAnika Henke // alternative for screen.css 542318cd03eSAnika Henke if ($mediatype=='screen') { 54378a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 544d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/style.less"] = DOKU_BASE."lib/plugins/$p/"; 54578a6aeb1SAndreas Gohr } 54678a6aeb1SAndreas Gohr } 54778a6aeb1SAndreas Gohr return $list; 54878a6aeb1SAndreas Gohr} 54978a6aeb1SAndreas Gohr 55078a6aeb1SAndreas Gohr/** 55178a6aeb1SAndreas Gohr * Very simple CSS optimizer 55278a6aeb1SAndreas Gohr * 55378a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 554253d4b48SGerrit Uitslag * 555253d4b48SGerrit Uitslag * @param string $css 556253d4b48SGerrit Uitslag * @return string 55778a6aeb1SAndreas Gohr */ 558*d868eb89SAndreas Gohrfunction css_compress($css) 559*d868eb89SAndreas Gohr{ 560ba234b18SPhy // replace quoted strings with placeholder 561ba234b18SPhy $quote_storage = []; 562ba234b18SPhy 563ba234b18SPhy $quote_cb = function ($match) use (&$quote_storage) { 564ba234b18SPhy $quote_storage[] = $match[0]; 5655fdc2ff2SPhy return '"STR'.(count($quote_storage)-1).'"'; 566ba234b18SPhy }; 567ba234b18SPhy 5685fdc2ff2SPhy $css = preg_replace_callback('/(([\'"]).*?(?<!\\\\)\2)/', $quote_cb, $css); 569ba234b18SPhy 570fd7c2db0SAndreas Gohr // strip comments through a callback 571fd7c2db0SAndreas Gohr $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s', 'css_comment_cb', $css); 572fd7c2db0SAndreas Gohr 573247c1c5dSAndreas Gohr // strip (incorrect but common) one line comments 574fe5a50c3SAndreas Gohr $css = preg_replace_callback('/^.*\/\/.*$/m', 'css_onelinecomment_cb', $css); 575247c1c5dSAndreas Gohr 57678a6aeb1SAndreas Gohr // strip whitespaces 57778a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!', ' ', $css); 578f5379589SChristopher Smith $css = preg_replace('/ ?([;,{}\/]) ?/', '\\1', $css); 579f5379589SChristopher Smith $css = preg_replace('/ ?: /', ':', $css); 58078a6aeb1SAndreas Gohr 581205907a7Sfurun // number compression 58264159a61SAndreas Gohr $css = preg_replace( 58364159a61SAndreas Gohr '/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', 58464159a61SAndreas Gohr '$1$2$3', 58564159a61SAndreas Gohr $css 58664159a61SAndreas Gohr ); // "0.1em" to ".1em", "1.10em" to "1.1em" 58764159a61SAndreas Gohr $css = preg_replace( 58864159a61SAndreas Gohr '/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/', 58964159a61SAndreas Gohr '$1$2', 59064159a61SAndreas Gohr $css 59164159a61SAndreas Gohr ); // ".0em" to "0" 59264159a61SAndreas Gohr $css = preg_replace( 59364159a61SAndreas Gohr '/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', 59464159a61SAndreas Gohr '$1', 59564159a61SAndreas Gohr $css 59664159a61SAndreas Gohr ); // "0.0em" to "0" 59764159a61SAndreas Gohr $css = preg_replace( 59864159a61SAndreas Gohr '/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', 59964159a61SAndreas Gohr '$1$3', 60064159a61SAndreas Gohr $css 60164159a61SAndreas Gohr ); // "1.0em" to "1em" 60264159a61SAndreas Gohr $css = preg_replace( 60364159a61SAndreas Gohr '/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/', 60464159a61SAndreas Gohr '$1$2$3', 60564159a61SAndreas Gohr $css 60664159a61SAndreas Gohr ); // "001em" to "1em" 607205907a7Sfurun 608205907a7Sfurun // shorten attributes (1em 1em 1em 1em -> 1em) 60964159a61SAndreas Gohr $css = preg_replace( 61064159a61SAndreas Gohr '/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/', 61164159a61SAndreas Gohr '$1$2', 61264159a61SAndreas Gohr $css 61364159a61SAndreas Gohr ); // "1em 1em 1em 1em" to "1em" 61464159a61SAndreas Gohr $css = preg_replace( 61564159a61SAndreas Gohr '/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/', 61664159a61SAndreas Gohr '$1$2 $3', 61764159a61SAndreas Gohr $css 61864159a61SAndreas Gohr ); // "1em 2em 1em 2em" to "1em 2em" 619205907a7Sfurun 62078a6aeb1SAndreas Gohr // shorten colors 62164159a61SAndreas Gohr $css = preg_replace( 62264159a61SAndreas Gohr "/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3(?=[^\{]*[;\}])/", 62364159a61SAndreas Gohr "#\\1\\2\\3", 62464159a61SAndreas Gohr $css 62564159a61SAndreas Gohr ); 62678a6aeb1SAndreas Gohr 627ba234b18SPhy // replace back protected strings 628ba234b18SPhy $quote_back_cb = function ($match) use (&$quote_storage) { 629ba234b18SPhy return $quote_storage[$match[1]]; 630ba234b18SPhy }; 631ba234b18SPhy 6325fdc2ff2SPhy $css = preg_replace_callback('/"STR(\d+)"/', $quote_back_cb, $css); 6332f5645efSPhy $css = trim($css); 634ba234b18SPhy 63578a6aeb1SAndreas Gohr return $css; 63678a6aeb1SAndreas Gohr} 63778a6aeb1SAndreas Gohr 638c00aef76SAndreas Gohr/** 639c00aef76SAndreas Gohr * Callback for css_compress() 640c00aef76SAndreas Gohr * 641c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks 642c00aef76SAndreas Gohr * 643c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 644253d4b48SGerrit Uitslag * 645253d4b48SGerrit Uitslag * @param array $matches 646253d4b48SGerrit Uitslag * @return string 647c00aef76SAndreas Gohr */ 648*d868eb89SAndreas Gohrfunction css_comment_cb($matches) 649*d868eb89SAndreas Gohr{ 650c00aef76SAndreas Gohr if(strlen($matches[2]) > 4) return ''; 651c00aef76SAndreas Gohr return $matches[0]; 652c00aef76SAndreas Gohr} 65378a6aeb1SAndreas Gohr 654fe5a50c3SAndreas Gohr/** 655fe5a50c3SAndreas Gohr * Callback for css_compress() 656fe5a50c3SAndreas Gohr * 657fe5a50c3SAndreas Gohr * Strips one line comments but makes sure it will not destroy url() constructs with slashes 658fe5a50c3SAndreas Gohr * 659253d4b48SGerrit Uitslag * @param array $matches 660fe5a50c3SAndreas Gohr * @return string 661fe5a50c3SAndreas Gohr */ 662*d868eb89SAndreas Gohrfunction css_onelinecomment_cb($matches) 663*d868eb89SAndreas Gohr{ 664fe5a50c3SAndreas Gohr $line = $matches[0]; 665fe5a50c3SAndreas Gohr 666fe5a50c3SAndreas Gohr $i = 0; 667fe5a50c3SAndreas Gohr $len = strlen($line); 668918a4468SAndreas Gohr 669fe5a50c3SAndreas Gohr while ($i< $len){ 670fe5a50c3SAndreas Gohr $nextcom = strpos($line, '//', $i); 671fe5a50c3SAndreas Gohr $nexturl = stripos($line, 'url(', $i); 672fe5a50c3SAndreas Gohr 673fe5a50c3SAndreas Gohr if($nextcom === false) { 674fe5a50c3SAndreas Gohr // no more comments, we're done 675918a4468SAndreas Gohr $i = $len; 676fe5a50c3SAndreas Gohr break; 677fe5a50c3SAndreas Gohr } 678fe5a50c3SAndreas Gohr 679918a4468SAndreas Gohr if($nexturl === false || $nextcom < $nexturl) { 680918a4468SAndreas Gohr // no url anymore, strip comment and be done 681918a4468SAndreas Gohr $i = $nextcom; 682918a4468SAndreas Gohr break; 683918a4468SAndreas Gohr } 684918a4468SAndreas Gohr 685918a4468SAndreas Gohr // we have an upcoming url 686918a4468SAndreas Gohr $i = strpos($line, ')', $nexturl); 687918a4468SAndreas Gohr } 688918a4468SAndreas Gohr 689918a4468SAndreas Gohr return substr($line, 0, $i); 690fe5a50c3SAndreas Gohr} 691fe5a50c3SAndreas Gohr 692e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 693