1<?php 2/** 3 * DokuWiki StyleSheet creator 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9use dokuwiki\StyleUtils; 10use dokuwiki\Cache\Cache; 11use dokuwiki\Extension\Event; 12 13if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../'); 14if (!defined('NOSESSION')) define('NOSESSION', true); // we do not use a session or authentication here (better caching) 15if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); // we gzip ourself here 16if (!defined('NL')) define('NL', "\n"); 17require_once(DOKU_INC . 'inc/init.php'); 18 19// Main (don't run when UNIT test) 20if (!defined('SIMPLE_TEST')) { 21 header('Content-Type: text/css; charset=utf-8'); 22 css_out(); 23} 24 25 26// ---------------------- functions ------------------------------ 27 28/** 29 * Output all needed Styles 30 * 31 * @author Andreas Gohr <andi@splitbrain.org> 32 */ 33function css_out() 34{ 35 global $conf; 36 global $lang; 37 global $config_cascade; 38 global $INPUT; 39 40 if ($INPUT->str('s') == 'feed') { 41 $mediatypes = ['feed']; 42 $type = 'feed'; 43 } else { 44 $mediatypes = ['screen', 'all', 'print', 'speech']; 45 $type = ''; 46 } 47 48 // decide from where to get the template 49 $tpl = trim(preg_replace('/[^\w-]+/', '', $INPUT->str('t'))); 50 if (!$tpl) { 51 $tpl = $conf['template']; 52 } 53 54 // load style.ini 55 $styleUtil = new StyleUtils($tpl, $INPUT->bool('preview')); 56 $styleini = $styleUtil->cssStyleini(); 57 58 // cache influencers 59 $tplinc = tpl_incdir($tpl); 60 $cache_files = getConfigFiles('main'); 61 $cache_files[] = $tplinc . 'style.ini'; 62 $cache_files[] = DOKU_CONF . "tpl/$tpl/style.ini"; 63 $cache_files[] = __FILE__; 64 if ($INPUT->bool('preview')) { 65 $cache_files[] = $conf['cachedir'] . '/preview.ini'; 66 } 67 68 // Array of needed files and their web locations, the latter ones 69 // are needed to fix relative paths in the stylesheets 70 $media_files = []; 71 foreach ($mediatypes as $mediatype) { 72 $files = []; 73 74 // load core styles 75 $files[DOKU_INC . 'lib/styles/' . $mediatype . '.css'] = DOKU_BASE . 'lib/styles/'; 76 77 // load jQuery-UI theme 78 if ($mediatype == 'screen') { 79 $files[DOKU_INC . 'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = 80 DOKU_BASE . 'lib/scripts/jquery/jquery-ui-theme/'; 81 } 82 // load plugin styles 83 $files = array_merge($files, css_pluginstyles($mediatype)); 84 // load template styles 85 if (isset($styleini['stylesheets'][$mediatype])) { 86 $files = array_merge($files, $styleini['stylesheets'][$mediatype]); 87 } 88 // load user styles 89 if (isset($config_cascade['userstyle'][$mediatype]) && is_array($config_cascade['userstyle'][$mediatype])) { 90 foreach ($config_cascade['userstyle'][$mediatype] as $userstyle) { 91 $files[$userstyle] = DOKU_BASE; 92 } 93 } 94 95 // Let plugins decide to either put more styles here or to remove some 96 $media_files[$mediatype] = css_filewrapper($mediatype, $files); 97 $CSSEvt = new Event('CSS_STYLES_INCLUDED', $media_files[$mediatype]); 98 99 // Make it preventable. 100 if ($CSSEvt->advise_before()) { 101 $cache_files = array_merge($cache_files, array_keys($media_files[$mediatype]['files'])); 102 } else { 103 // unset if prevented. Nothing will be printed for this mediatype. 104 unset($media_files[$mediatype]); 105 } 106 107 // finish event. 108 $CSSEvt->advise_after(); 109 } 110 111 // The generated script depends on some dynamic options 112 $cache = new Cache( 113 'styles' . 114 $_SERVER['HTTP_HOST'] . 115 $_SERVER['SERVER_PORT'] . 116 $INPUT->bool('preview') . 117 DOKU_BASE . 118 $tpl . 119 $type, 120 '.css' 121 ); 122 $cache->setEvent('CSS_CACHE_USE'); 123 124 // check cache age & handle conditional request 125 // This may exit if a cache can be used 126 $cache_ok = $cache->useCache(['files' => $cache_files]); 127 http_cached($cache->cache, $cache_ok); 128 129 // start output buffering 130 ob_start(); 131 132 // Fire CSS_STYLES_INCLUDED for one last time to let the 133 // plugins decide whether to include the DW default styles. 134 // This can be done by preventing the Default. 135 $media_files['DW_DEFAULT'] = css_filewrapper('DW_DEFAULT'); 136 Event::createAndTrigger('CSS_STYLES_INCLUDED', $media_files['DW_DEFAULT'], 'css_defaultstyles'); 137 138 // build the stylesheet 139 foreach ($mediatypes as $mediatype) { 140 // Check if there is a wrapper set for this type. 141 if (!isset($media_files[$mediatype])) { 142 continue; 143 } 144 145 $cssData = $media_files[$mediatype]; 146 147 // Print the styles. 148 echo NL; 149 if ($cssData['encapsulate'] === true) { 150 echo $cssData['encapsulationPrefix'] . ' {'; 151 } 152 echo '/* START ' . $cssData['mediatype'] . ' styles */' . NL; 153 154 // load files 155 foreach ($cssData['files'] as $file => $location) { 156 $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 157 echo "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 158 echo css_loadfile($file, $location); 159 } 160 161 echo NL; 162 if ($cssData['encapsulate'] === true) { 163 echo '} /* /@media '; 164 } else { 165 echo '/*'; 166 } 167 echo ' END ' . $cssData['mediatype'] . ' styles */' . NL; 168 } 169 170 // end output buffering and get contents 171 $css = ob_get_contents(); 172 ob_end_clean(); 173 174 // strip any source maps 175 stripsourcemaps($css); 176 177 // apply style replacements 178 $css = css_applystyle($css, $styleini['replacements']); 179 180 // parse less 181 $css = css_parseless($css); 182 183 // compress whitespace and comments 184 if ($conf['compress']) { 185 $css = css_compress($css); 186 } 187 188 // embed small images right into the stylesheet 189 if ($conf['cssdatauri']) { 190 $base = preg_quote(DOKU_BASE, '#'); 191 $css = preg_replace_callback('#(url\([ \'"]*)(' . $base . ')(.*?(?:\.(png|gif)))#i', 'css_datauri', $css); 192 } 193 194 http_cached_finish($cache->cache, $css); 195} 196 197/** 198 * Uses phpless to parse LESS in our CSS 199 * 200 * most of this function is error handling to show a nice useful error when 201 * LESS compilation fails 202 * 203 * @param string $css 204 * @return string 205 */ 206function css_parseless($css) 207{ 208 global $conf; 209 210 $less = new lessc(); 211 $less->importDir = [DOKU_INC]; 212 $less->setPreserveComments(!$conf['compress']); 213 214 if (defined('DOKU_UNITTEST')) { 215 $less->importDir[] = TMP_DIR; 216 } 217 218 try { 219 return $less->compile($css); 220 } catch (Exception $e) { 221 // get exception message 222 $msg = str_replace(["\n", "\r", "'"], [], $e->getMessage()); 223 224 // try to use line number to find affected file 225 if (preg_match('/line: (\d+)$/', $msg, $m)) { 226 $msg = substr($msg, 0, -1 * strlen($m[0])); //remove useless linenumber 227 $lno = $m[1]; 228 229 // walk upwards to last include 230 $lines = explode("\n", $css); 231 for ($i = $lno - 1; $i >= 0; $i--) { 232 if (preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)) { 233 // we found it, add info to message 234 $msg .= ' in ' . $m[2] . ' at line ' . ($lno - $i); 235 break; 236 } 237 } 238 } 239 240 // something went wrong 241 $error = 'A fatal error occured during compilation of the CSS files. ' . 242 'If you recently installed a new plugin or template it ' . 243 'might be broken and you should try disabling it again. [' . $msg . ']'; 244 245 echo ".dokuwiki:before { 246 content: '$error'; 247 background-color: red; 248 display: block; 249 background-color: #fcc; 250 border-color: #ebb; 251 color: #000; 252 padding: 0.5em; 253 }"; 254 255 exit; 256 } 257} 258 259/** 260 * Does placeholder replacements in the style according to 261 * the ones defined in a templates style.ini file 262 * 263 * This also adds the ini defined placeholders as less variables 264 * (sans the surrounding __ and with a ini_ prefix) 265 * 266 * @param string $css 267 * @param array $replacements array(placeholder => value) 268 * @return string 269 * 270 * @author Andreas Gohr <andi@splitbrain.org> 271 */ 272function css_applystyle($css, $replacements) 273{ 274 // we convert ini replacements to LESS variable names 275 // and build a list of variable: value; pairs 276 $less = ''; 277 foreach ((array)$replacements as $key => $value) { 278 $lkey = trim($key, '_'); 279 $lkey = '@ini_' . $lkey; 280 $less .= "$lkey: $value;\n"; 281 282 $replacements[$key] = $lkey; 283 } 284 285 // we now replace all old ini replacements with LESS variables 286 $css = strtr($css, $replacements); 287 288 // now prepend the list of LESS variables as the very first thing 289 $css = $less . $css; 290 return $css; 291} 292 293/** 294 * Wrapper for the files, content and mediatype for the event CSS_STYLES_INCLUDED 295 * 296 * @param string $mediatype type ofthe current media files/content set 297 * @param array $files set of files that define the current mediatype 298 * @return array 299 * 300 * @author Gerry Weißbach <gerry.w@gammaproduction.de> 301 */ 302function css_filewrapper($mediatype, $files = []) 303{ 304 return [ 305 'files' => $files, 306 'mediatype' => $mediatype, 307 'encapsulate' => $mediatype != 'all', 308 'encapsulationPrefix' => '@media ' . $mediatype 309 ]; 310} 311 312/** 313 * Prints the @media encapsulated default styles of DokuWiki 314 * 315 * This function is being called by a CSS_STYLES_INCLUDED event 316 * The event can be distinguished by the mediatype which is: 317 * DW_DEFAULT 318 * 319 * @author Gerry Weißbach <gerry.w@gammaproduction.de> 320 */ 321function css_defaultstyles() 322{ 323 // print the default classes for interwiki links and file downloads 324 echo '@media screen {'; 325 css_interwiki(); 326 css_filetypes(); 327 echo '}'; 328} 329 330/** 331 * Prints classes for interwikilinks 332 * 333 * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 334 * $name is the identifier given in the config. All Interwiki links get 335 * an default style with a default icon. If a special icon is available 336 * for an interwiki URL it is set in it's own class. Both classes can be 337 * overwritten in the template or userstyles. 338 * 339 * @author Andreas Gohr <andi@splitbrain.org> 340 */ 341function css_interwiki() 342{ 343 344 // default style 345 echo 'a.interwiki {'; 346 echo ' background: transparent url(' . DOKU_BASE . 'lib/images/interwiki.svg) 0 0 no-repeat;'; 347 echo ' background-size: 1.2em;'; 348 echo ' padding: 0 0 0 1.4em;'; 349 echo '}'; 350 351 // additional styles when icon available 352 $iwlinks = getInterwiki(); 353 foreach (array_keys($iwlinks) as $iw) { 354 $class = preg_replace('/[^_\-a-z0-9]+/i', '_', $iw); 355 foreach (['svg', 'png', 'gif'] as $ext) { 356 $file = 'lib/images/interwiki/' . $iw . '.' . $ext; 357 358 if (file_exists(DOKU_INC . $file)) { 359 echo "a.iw_$class {"; 360 echo ' background-image: url(' . DOKU_BASE . $file . ')'; 361 echo '}'; 362 break; 363 } 364 } 365 } 366} 367 368/** 369 * Prints classes for file download links 370 * 371 * @author Andreas Gohr <andi@splitbrain.org> 372 */ 373function css_filetypes() 374{ 375 376 // default style 377 echo '.mediafile {'; 378 echo ' background: transparent url(' . DOKU_BASE . 'lib/images/fileicons/svg/file.svg) 0px 1px no-repeat;'; 379 echo ' background-size: 1.2em;'; 380 echo ' padding-left: 1.5em;'; 381 echo '}'; 382 383 // additional styles when icon available 384 // scan directory for all icons 385 $exts = []; 386 if ($dh = opendir(DOKU_INC . 'lib/images/fileicons/svg')) { 387 while (false !== ($file = readdir($dh))) { 388 if (preg_match('/(.*?)\.svg$/i', $file, $match)) { 389 $exts[] = strtolower($match[1]); 390 } 391 } 392 closedir($dh); 393 } 394 foreach ($exts as $ext) { 395 $class = preg_replace('/[^_\-a-z0-9]+/', '_', $ext); 396 echo ".mf_$class {"; 397 echo ' background-image: url(' . DOKU_BASE . 'lib/images/fileicons/svg/' . $ext . '.svg)'; 398 echo '}'; 399 } 400} 401 402/** 403 * Loads a given file and fixes relative URLs with the 404 * given location prefix 405 * 406 * @param string $file file system path 407 * @param string $location 408 * @return string 409 */ 410function css_loadfile($file, $location = '') 411{ 412 $css_file = new DokuCssFile($file); 413 return $css_file->load($location); 414} 415 416/** 417 * Helper class to abstract loading of css/less files 418 * 419 * @author Chris Smith <chris@jalakai.co.uk> 420 */ 421class DokuCssFile 422{ 423 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; 427 428 public function __construct($file) 429 { 430 $this->filepath = $file; 431 } 432 433 /** 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. 437 * 438 * @param string $location base url for this file 439 * @return string the CSS/Less contents of the file 440 */ 441 public function load($location = '') 442 { 443 if (!file_exists($this->filepath)) return ''; 444 445 $css = io_readFile($this->filepath); 446 if (!$location) return $css; 447 448 $this->location = $location; 449 450 $css = preg_replace_callback('#(url\( *)([\'"]?)(.*?)(\2)( *\))#', [$this, 'replacements'], $css); 451 $css = preg_replace_callback('#(@import\s+)([\'"])(.*?)(\2)#', [$this, 'replacements'], $css); 452 453 return $css; 454 } 455 456 /** 457 * Get the relative file system path of this file, relative to dokuwiki's root folder, DOKU_INC 458 * 459 * @return string relative file system path 460 */ 461 protected function getRelativePath() 462 { 463 464 if (is_null($this->relative_path)) { 465 $basedir = [DOKU_INC]; 466 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); 470 } 471 472 $basedir = array_map('preg_quote_cb', $basedir); 473 $regex = '/^(' . implode('|', $basedir) . ')/'; 474 $this->relative_path = preg_replace($regex, '', dirname($this->filepath)); 475 } 476 477 return $this->relative_path; 478 } 479 480 /** 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 483 * 484 * @param array $match see http://php.net/preg_replace_callback 485 * @return string see http://php.net/preg_replace_callback 486 */ 487 public function replacements($match) 488 { 489 490 if (preg_match('#^(/|data:|https?://)#', $match[3])) { // not a relative url? - no adjustment required 491 return $match[0]; 492 } elseif (substr($match[3], -5) == '.less') { // a less file import? - requires a file system location 493 if ($match[3][0] != '/') { 494 $match[3] = $this->getRelativePath() . '/' . $match[3]; 495 } 496 } else { // everything else requires a url adjustment 497 $match[3] = $this->location . $match[3]; 498 } 499 500 return implode('', array_slice($match, 1)); 501 } 502} 503 504/** 505 * Convert local image URLs to data URLs if the filesize is small 506 * 507 * Callback for preg_replace_callback 508 * 509 * @param array $match 510 * @return string 511 */ 512function css_datauri($match) 513{ 514 global $conf; 515 516 $pre = unslash($match[1]); 517 $base = unslash($match[2]); 518 $url = unslash($match[3]); 519 $ext = unslash($match[4]); 520 521 $local = DOKU_INC . $url; 522 $size = @filesize($local); 523 if ($size && $size < $conf['cssdatauri']) { 524 $data = base64_encode(file_get_contents($local)); 525 } 526 if (!empty($data)) { 527 $url = 'data:image/' . $ext . ';base64,' . $data; 528 } else { 529 $url = $base . $url; 530 } 531 return $pre . $url; 532} 533 534 535/** 536 * Returns a list of possible Plugin Styles (no existance check here) 537 * 538 * @param string $mediatype 539 * @return array 540 * @author Andreas Gohr <andi@splitbrain.org> 541 * 542 */ 543function css_pluginstyles($mediatype = 'screen') 544{ 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/"; 554 } 555 } 556 return $list; 557} 558 559/** 560 * Very simple CSS optimizer 561 * 562 * @param string $css 563 * @return string 564 * @author Andreas Gohr <andi@splitbrain.org> 565 * 566 */ 567function css_compress($css) 568{ 569 // replace quoted strings with placeholder 570 $quote_storage = []; 571 572 $quote_cb = function ($match) use (&$quote_storage) { 573 $quote_storage[] = $match[0]; 574 return '"STR' . (count($quote_storage) - 1) . '"'; 575 }; 576 577 $css = preg_replace_callback('/(([\'"]).*?(?<!\\\\)\2)/', $quote_cb, $css); 578 579 // strip comments through a callback 580 $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s', 'css_comment_cb', $css); 581 582 // strip (incorrect but common) one line comments 583 $css = preg_replace_callback('/^.*\/\/.*$/m', 'css_onelinecomment_cb', $css); 584 585 // strip whitespaces 586 $css = preg_replace('![\r\n\t ]+!', ' ', $css); 587 $css = preg_replace('/ ?([;,{}\/]) ?/', '\\1', $css); 588 $css = preg_replace('/ ?: /', ':', $css); 589 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" 616 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" 628 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 634 ); 635 636 // replace back protected strings 637 $quote_back_cb = function ($match) use (&$quote_storage) { 638 return $quote_storage[$match[1]]; 639 }; 640 641 $css = preg_replace_callback('/"STR(\d+)"/', $quote_back_cb, $css); 642 $css = trim($css); 643 644 return $css; 645} 646 647/** 648 * Callback for css_compress() 649 * 650 * Keeps short comments (< 5 chars) to maintain typical browser hacks 651 * 652 * @param array $matches 653 * @return string 654 * 655 * @author Andreas Gohr <andi@splitbrain.org> 656 * 657 */ 658function css_comment_cb($matches) 659{ 660 if (strlen($matches[2]) > 4) return ''; 661 return $matches[0]; 662} 663 664/** 665 * Callback for css_compress() 666 * 667 * Strips one line comments but makes sure it will not destroy url() constructs with slashes 668 * 669 * @param array $matches 670 * @return string 671 */ 672function css_onelinecomment_cb($matches) 673{ 674 $line = $matches[0]; 675 676 $i = 0; 677 $len = strlen($line); 678 679 while ($i < $len) { 680 $nextcom = strpos($line, '//', $i); 681 $nexturl = stripos($line, 'url(', $i); 682 683 if ($nextcom === false) { 684 // no more comments, we're done 685 $i = $len; 686 break; 687 } 688 689 if ($nexturl === false || $nextcom < $nexturl) { 690 // no url anymore, strip comment and be done 691 $i = $nextcom; 692 break; 693 } 694 695 // we have an upcoming url 696 $i = strpos($line, ')', $nexturl); 697 } 698 699 return substr($line, 0, $i); 700} 701 702//Setup VIM: ex: et ts=4 : 703