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