replace LESS library. fixes #4088
[dokuwiki.git] / lib / exe / css.php
blobf8bb9749fc4c1eba302f688274b720e98e775ed7
1 <?php
3 /**
4 * DokuWiki StyleSheet creator
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Andreas Gohr <andi@splitbrain.org>
8 */
10 use dokuwiki\StyleUtils;
11 use dokuwiki\Cache\Cache;
12 use dokuwiki\Extension\Event;
14 if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../');
15 if (!defined('NOSESSION')) define('NOSESSION', true); // we do not use a session or authentication here (better caching)
16 if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); // we gzip ourself here
17 if (!defined('NL')) define('NL', "\n");
18 require_once(DOKU_INC . 'inc/init.php');
20 // Main (don't run when UNIT test)
21 if (!defined('SIMPLE_TEST')) {
22 header('Content-Type: text/css; charset=utf-8');
23 css_out();
27 // ---------------------- functions ------------------------------
29 /**
30 * Output all needed Styles
32 * @author Andreas Gohr <andi@splitbrain.org>
34 function css_out()
36 global $conf;
37 global $lang;
38 global $config_cascade;
39 global $INPUT;
41 if ($INPUT->str('s') == 'feed') {
42 $mediatypes = ['feed'];
43 $type = 'feed';
44 } else {
45 $mediatypes = ['screen', 'all', 'print', 'speech'];
46 $type = '';
49 // decide from where to get the template
50 $tpl = trim(preg_replace('/[^\w-]+/', '', $INPUT->str('t')));
51 if (!$tpl) {
52 $tpl = $conf['template'];
55 // load style.ini
56 $styleUtil = new StyleUtils($tpl, $INPUT->bool('preview'));
57 $styleini = $styleUtil->cssStyleini();
59 // cache influencers
60 $tplinc = tpl_incdir($tpl);
61 $cache_files = getConfigFiles('main');
62 $cache_files[] = $tplinc . 'style.ini';
63 $cache_files[] = DOKU_CONF . "tpl/$tpl/style.ini";
64 $cache_files[] = __FILE__;
65 if ($INPUT->bool('preview')) {
66 $cache_files[] = $conf['cachedir'] . '/preview.ini';
69 // Array of needed files and their web locations, the latter ones
70 // are needed to fix relative paths in the stylesheets
71 $media_files = [];
72 foreach ($mediatypes as $mediatype) {
73 $files = [];
75 // load core styles
76 $files[DOKU_INC . 'lib/styles/' . $mediatype . '.css'] = DOKU_BASE . 'lib/styles/';
78 // load jQuery-UI theme
79 if ($mediatype == 'screen') {
80 $files[DOKU_INC . 'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] =
81 DOKU_BASE . 'lib/scripts/jquery/jquery-ui-theme/';
83 // load plugin styles
84 $files = array_merge($files, css_pluginstyles($mediatype));
85 // load template styles
86 if (isset($styleini['stylesheets'][$mediatype])) {
87 $files = array_merge($files, $styleini['stylesheets'][$mediatype]);
89 // load user styles
90 if (isset($config_cascade['userstyle'][$mediatype]) && is_array($config_cascade['userstyle'][$mediatype])) {
91 foreach ($config_cascade['userstyle'][$mediatype] as $userstyle) {
92 $files[$userstyle] = DOKU_BASE;
96 // Let plugins decide to either put more styles here or to remove some
97 $media_files[$mediatype] = css_filewrapper($mediatype, $files);
98 $CSSEvt = new Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]);
100 // Make it preventable.
101 if ($CSSEvt->advise_before()) {
102 $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files']));
103 } else {
104 // unset if prevented. Nothing will be printed for this mediatype.
105 unset($media_files[$mediatype]);
108 // finish event.
109 $CSSEvt->advise_after();
112 // The generated script depends on some dynamic options
113 $cache = new Cache(
114 'styles' .
115 $_SERVER['HTTP_HOST'] .
116 $_SERVER['SERVER_PORT'] .
117 $INPUT->bool('preview') .
118 DOKU_BASE .
119 $tpl .
120 $type,
121 '.css'
123 $cache->setEvent('CSS_CACHE_USE');
125 // check cache age & handle conditional request
126 // This may exit if a cache can be used
127 $cache_ok = $cache->useCache(['files' => $cache_files]);
128 http_cached($cache->cache, $cache_ok);
130 // start output buffering
131 ob_start();
133 // Fire CSS_STYLES_INCLUDED for one last time to let the
134 // plugins decide whether to include the DW default styles.
135 // This can be done by preventing the Default.
136 $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT');
137 Event::createAndTrigger('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles');
139 // build the stylesheet
140 foreach ($mediatypes as $mediatype) {
141 // Check if there is a wrapper set for this type.
142 if (!isset($media_files[$mediatype])) {
143 continue;
146 $cssData = $media_files[$mediatype];
148 // Print the styles.
149 echo NL;
150 if ($cssData['encapsulate'] === true) {
151 echo $cssData['encapsulationPrefix'] . ' {';
153 echo '/* START ' . $cssData['mediatype'] . ' styles */' . NL;
155 // load files
156 foreach ($cssData['files'] as $file => $location) {
157 $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
158 echo "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
159 echo css_loadfile($file, $location);
162 echo NL;
163 if ($cssData['encapsulate'] === true) {
164 echo '} /* /@media ';
165 } else {
166 echo '/*';
168 echo ' END ' . $cssData['mediatype'] . ' styles */' . NL;
171 // end output buffering and get contents
172 $css = ob_get_contents();
173 ob_end_clean();
175 // strip any source maps
176 stripsourcemaps($css);
178 // apply style replacements
179 $css = css_applystyle($css, $styleini['replacements']);
181 // parse less
182 $css = css_parseless($css);
184 // compress whitespace and comments
185 if ($conf['compress']) {
186 $css = css_compress($css);
189 // embed small images right into the stylesheet
190 if ($conf['cssdatauri']) {
191 $base = preg_quote(DOKU_BASE, '#');
192 $css = preg_replace_callback('#(url\([ \'"]*)(' . $base . ')(.*?(?:\.(png|gif)))#i', 'css_datauri', $css);
195 http_cached_finish($cache->cache, $css);
199 * Uses phpless to parse LESS in our CSS
201 * most of this function is error handling to show a nice useful error when
202 * LESS compilation fails
204 * @param string $css
205 * @return string
207 function css_parseless($css)
209 global $conf;
211 $less = new LesserPHP\Lessc();
212 $less->setImportDir([DOKU_INC]);
213 $less->setPreserveComments(!$conf['compress']);
215 if (defined('DOKU_UNITTEST')) {
216 $less->addImportDir(TMP_DIR);
219 try {
220 return $less->compile($css);
221 } catch (Exception $e) {
222 // get exception message
223 $msg = str_replace(["\n", "\r", "'"], [], $e->getMessage());
225 // try to use line number to find affected file
226 if (preg_match('/line: (\d+)$/', $msg, $m)) {
227 $msg = substr($msg, 0, -1 * strlen($m[0])); //remove useless linenumber
228 $lno = $m[1];
230 // walk upwards to last include
231 $lines = explode("\n", $css);
232 for ($i = $lno - 1; $i >= 0; $i--) {
233 if (preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)) {
234 // we found it, add info to message
235 $msg .= ' in ' . $m[2] . ' at line ' . ($lno - $i);
236 break;
241 // something went wrong
242 $error = 'A fatal error occured during compilation of the CSS files. ' .
243 'If you recently installed a new plugin or template it ' .
244 'might be broken and you should try disabling it again. [' . $msg . ']';
246 echo ".dokuwiki:before {
247 content: '$error';
248 background-color: red;
249 display: block;
250 background-color: #fcc;
251 border-color: #ebb;
252 color: #000;
253 padding: 0.5em;
256 exit;
261 * Does placeholder replacements in the style according to
262 * the ones defined in a templates style.ini file
264 * This also adds the ini defined placeholders as less variables
265 * (sans the surrounding __ and with a ini_ prefix)
267 * @param string $css
268 * @param array $replacements array(placeholder => value)
269 * @return string
271 * @author Andreas Gohr <andi@splitbrain.org>
273 function css_applystyle($css, $replacements)
275 // we convert ini replacements to LESS variable names
276 // and build a list of variable: value; pairs
277 $less = '';
278 foreach ((array)$replacements as $key => $value) {
279 $lkey = trim($key, '_');
280 $lkey = '@ini_' . $lkey;
281 $less .= "$lkey: $value;\n";
283 $replacements[$key] = $lkey;
286 // we now replace all old ini replacements with LESS variables
287 $css = strtr($css, $replacements);
289 // now prepend the list of LESS variables as the very first thing
290 $css = $less . $css;
291 return $css;
295 * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED
297 * @param string $mediatype type ofthe current media files/content set
298 * @param array $files set of files that define the current mediatype
299 * @return array
301 * @author Gerry Weißbach <gerry.w@gammaproduction.de>
303 function css_filewrapper($mediatype, $files = [])
305 return [
306 'files' => $files,
307 'mediatype' => $mediatype,
308 'encapsulate' => $mediatype != 'all',
309 'encapsulationPrefix' => '@media ' . $mediatype
314 * Prints the @media encapsulated default styles of DokuWiki
316 * This function is being called by a CSS_STYLES_INCLUDED event
317 * The event can be distinguished by the mediatype which is:
318 * DW_DEFAULT
320 * @author Gerry Weißbach <gerry.w@gammaproduction.de>
322 function css_defaultstyles()
324 // print the default classes for interwiki links and file downloads
325 echo '@media screen {';
326 css_interwiki();
327 css_filetypes();
328 echo '}';
332 * Prints classes for interwikilinks
334 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where
335 * $name is the identifier given in the config. All Interwiki links get
336 * an default style with a default icon. If a special icon is available
337 * for an interwiki URL it is set in it's own class. Both classes can be
338 * overwritten in the template or userstyles.
340 * @author Andreas Gohr <andi@splitbrain.org>
342 function css_interwiki()
345 // default style
346 echo 'a.interwiki {';
347 echo ' background: transparent url(' . DOKU_BASE . 'lib/images/interwiki.svg) 0 0 no-repeat;';
348 echo ' background-size: 1.2em;';
349 echo ' padding: 0 0 0 1.4em;';
350 echo '}';
352 // additional styles when icon available
353 $iwlinks = getInterwiki();
354 foreach (array_keys($iwlinks) as $iw) {
355 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $iw);
356 foreach (['svg', 'png', 'gif'] as $ext) {
357 $file = 'lib/images/interwiki/' . $iw . '.' . $ext;
359 if (file_exists(DOKU_INC . $file)) {
360 echo "a.iw_$class {";
361 echo ' background-image: url(' . DOKU_BASE . $file . ')';
362 echo '}';
363 break;
370 * Prints classes for file download links
372 * @author Andreas Gohr <andi@splitbrain.org>
374 function css_filetypes()
377 // default style
378 echo '.mediafile {';
379 echo ' background: transparent url(' . DOKU_BASE . 'lib/images/fileicons/svg/file.svg) 0px 1px no-repeat;';
380 echo ' background-size: 1.2em;';
381 echo ' padding-left: 1.5em;';
382 echo '}';
384 // additional styles when icon available
385 // scan directory for all icons
386 $exts = [];
387 if ($dh = opendir(DOKU_INC . 'lib/images/fileicons/svg')) {
388 while (false !== ($file = readdir($dh))) {
389 if (preg_match('/(.*?)\.svg$/i', $file, $match)) {
390 $exts[] = strtolower($match[1]);
393 closedir($dh);
395 foreach ($exts as $ext) {
396 $class = preg_replace('/[^_\-a-z0-9]+/', '_', $ext);
397 echo ".mf_$class {";
398 echo ' background-image: url(' . DOKU_BASE . 'lib/images/fileicons/svg/' . $ext . '.svg)';
399 echo '}';
404 * Loads a given file and fixes relative URLs with the
405 * given location prefix
407 * @param string $file file system path
408 * @param string $location
409 * @return string
411 function css_loadfile($file, $location = '')
413 $css_file = new DokuCssFile($file);
414 return $css_file->load($location);
418 * Helper class to abstract loading of css/less files
420 * @author Chris Smith <chris@jalakai.co.uk>
422 class DokuCssFile
424 protected $filepath; // file system path to the CSS/Less file
425 protected $location; // base url location of the CSS/Less file
426 protected $relative_path;
428 public function __construct($file)
430 $this->filepath = $file;
434 * Load the contents of the css/less file and adjust any relative paths/urls (relative to this file) to be
435 * relative to the dokuwiki root: the web root (DOKU_BASE) for most files; the file system root (DOKU_INC)
436 * for less files.
438 * @param string $location base url for this file
439 * @return string the CSS/Less contents of the file
441 public function load($location = '')
443 if (!file_exists($this->filepath)) return '';
445 $css = io_readFile($this->filepath);
446 if (!$location) return $css;
448 $this->location = $location;
450 $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#', [$this, 'replacements'], $css);
451 $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#', [$this, 'replacements'], $css);
453 return $css;
457 * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC
459 * @return string relative file system path
461 protected function getRelativePath()
464 if (is_null($this->relative_path)) {
465 $basedir = [DOKU_INC];
467 // during testing, files may be found relative to a second base dir, TMP_DIR
468 if (defined('DOKU_UNITTEST')) {
469 $basedir[] = realpath(TMP_DIR);
472 $basedir = array_map('preg_quote_cb', $basedir);
473 $regex = '/^(' . implode('|', $basedir) . ')/';
474 $this->relative_path = preg_replace($regex, '', dirname($this->filepath));
477 return $this->relative_path;
481 * preg_replace callback to adjust relative urls from relative to this file to relative
482 * to the appropriate dokuwiki root location as described in the code
484 * @param array $match see http://php.net/preg_replace_callback
485 * @return string see http://php.net/preg_replace_callback
487 public function replacements($match)
490 if (preg_match('#^(/|data:|https?://)#', $match[3])) { // not a relative url? - no adjustment required
491 return $match[0];
492 } elseif (str_ends_with($match[3], '.less')) { // a less file import? - requires a file system location
493 if ($match[3][0] != '/') {
494 $match[3] = $this->getRelativePath() . '/' . $match[3];
496 } else { // everything else requires a url adjustment
497 $match[3] = $this->location . $match[3];
500 return implode('', array_slice($match, 1));
505 * Convert local image URLs to data URLs if the filesize is small
507 * Callback for preg_replace_callback
509 * @param array $match
510 * @return string
512 function css_datauri($match)
514 global $conf;
516 $pre = unslash($match[1]);
517 $base = unslash($match[2]);
518 $url = unslash($match[3]);
519 $ext = unslash($match[4]);
521 $local = DOKU_INC . $url;
522 $size = @filesize($local);
523 if ($size && $size < $conf['cssdatauri']) {
524 $data = base64_encode(file_get_contents($local));
526 if (!empty($data)) {
527 $url = 'data:image/' . $ext . ';base64,' . $data;
528 } else {
529 $url = $base . $url;
531 return $pre . $url;
536 * Returns a list of possible Plugin Styles (no existance check here)
538 * @param string $mediatype
539 * @return array
540 * @author Andreas Gohr <andi@splitbrain.org>
543 function css_pluginstyles($mediatype = 'screen')
545 $list = [];
546 $plugins = plugin_list();
547 foreach ($plugins as $p) {
548 $list[DOKU_PLUGIN . "$p/$mediatype.css"] = DOKU_BASE . "lib/plugins/$p/";
549 $list[DOKU_PLUGIN . "$p/$mediatype.less"] = DOKU_BASE . "lib/plugins/$p/";
550 // alternative for screen.css
551 if ($mediatype == 'screen') {
552 $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/";
553 $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/";
556 return $list;
560 * Very simple CSS optimizer
562 * @param string $css
563 * @return string
564 * @author Andreas Gohr <andi@splitbrain.org>
567 function css_compress($css)
569 // replace quoted strings with placeholder
570 $quote_storage = [];
572 $quote_cb = function ($match) use (&$quote_storage) {
573 $quote_storage[] = $match[0];
574 return '"STR' . (count($quote_storage) - 1) . '"';
577 $css = preg_replace_callback('/(([\'"]).*?(?<!\\\\)\2)/', $quote_cb, $css);
579 // strip comments through a callback
580 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s', 'css_comment_cb', $css);
582 // strip (incorrect but common) one line comments
583 $css = preg_replace_callback('/^.*\/\/.*$/m', 'css_onelinecomment_cb', $css);
585 // strip whitespaces
586 $css = preg_replace('![\r\n\t ]+!', ' ', $css);
587 $css = preg_replace('/ ?([;,{}\/]) ?/', '\\1', $css);
588 $css = preg_replace('/ ?: /', ':', $css);
590 // number compression
591 $css = preg_replace(
592 '/([: ])0+(\.\d+?)0*((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/',
593 '$1$2$3',
594 $css
595 ); // "0.1em" to ".1em", "1.10em" to "1.1em"
596 $css = preg_replace(
597 '/([: ])\.(0)+((?:pt|pc|in|mm|cm|em|ex|px)\b|%)(?=[^\{]*[;\}])/',
598 '$1$2',
599 $css
600 ); // ".0em" to "0"
601 $css = preg_replace(
602 '/([: ]0)0*(\.0*)?((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
603 '$1',
604 $css
605 ); // "0.0em" to "0"
606 $css = preg_replace(
607 '/([: ]\d+)(\.0*)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
608 '$1$3',
609 $css
610 ); // "1.0em" to "1em"
611 $css = preg_replace(
612 '/([: ])0+(\d+|\d*\.\d+)((?:pt|pc|in|mm|cm|em|ex|px)(?=[^\{]*[;\}])\b|%)/',
613 '$1$2$3',
614 $css
615 ); // "001em" to "1em"
617 // shorten attributes (1em 1em 1em 1em -> 1em)
618 $css = preg_replace(
619 '/(?<![\w\-])((?:margin|padding|border|border-(?:width|radius)):)([\w\.]+)( \2)+(?=[;\}]| !)/',
620 '$1$2',
621 $css
622 ); // "1em 1em 1em 1em" to "1em"
623 $css = preg_replace(
624 '/(?<![\w\-])((?:margin|padding|border|border-(?:width)):)([\w\.]+) ([\w\.]+) \2 \3(?=[;\}]| !)/',
625 '$1$2 $3',
626 $css
627 ); // "1em 2em 1em 2em" to "1em 2em"
629 // shorten colors
630 $css = preg_replace(
631 "/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3(?=[^\{]*[;\}])/",
632 "#\\1\\2\\3",
633 $css
636 // replace back protected strings
637 $quote_back_cb = function ($match) use (&$quote_storage) {
638 return $quote_storage[$match[1]];
641 $css = preg_replace_callback('/"STR(\d+)"/', $quote_back_cb, $css);
642 $css = trim($css);
644 return $css;
648 * Callback for css_compress()
650 * Keeps short comments (< 5 chars) to maintain typical browser hacks
652 * @param array $matches
653 * @return string
655 * @author Andreas Gohr <andi@splitbrain.org>
658 function css_comment_cb($matches)
660 if (strlen($matches[2]) > 4) return '';
661 return $matches[0];
665 * Callback for css_compress()
667 * Strips one line comments but makes sure it will not destroy url() constructs with slashes
669 * @param array $matches
670 * @return string
672 function css_onelinecomment_cb($matches)
674 $line = $matches[0];
676 $i = 0;
677 $len = strlen($line);
679 while ($i < $len) {
680 $nextcom = strpos($line, '//', $i);
681 $nexturl = stripos($line, 'url(', $i);
683 if ($nextcom === false) {
684 // no more comments, we're done
685 $i = $len;
686 break;
689 if ($nexturl === false || $nextcom < $nexturl) {
690 // no url anymore, strip comment and be done
691 $i = $nextcom;
692 break;
695 // we have an upcoming url
696 $i = strpos($line, ')', $nexturl);
699 return substr($line, 0, $i);
702 //Setup VIM: ex: et ts=4 :