From 4478c903155a5cc50b518b8bc8399c02a91e27c1 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Fri, 2 Sep 2016 12:50:22 +0200 Subject: [PATCH 1/2] Avoid calling realpath on URLs Calling realpath on URLs like //fonts.googleapis.com/css takes ages on Windows: let's avoid that... --- lessc.inc.php | 4 ++-- lib/Less/Parser.php | 14 +++++++++++++- lib/Less/Tree/Import.php | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lessc.inc.php b/lessc.inc.php index bd4bc37a..e6cdf20a 100644 --- a/lessc.inc.php +++ b/lessc.inc.php @@ -150,7 +150,7 @@ public function compileFile( $fname, $outFname = null ) { $oldImport = $this->importDir; $this->importDir = (array)$this->importDir; - $this->importDir[] = realpath( $pi['dirname'] ).'/'; + $this->importDir[] = Less_Parser::AbsPath( $pi['dirname'] ).'/'; $this->allParsedFiles = array(); $this->addParsedFile( $fname ); @@ -271,6 +271,6 @@ public function allParsedFiles() { } protected function addParsedFile( $file ) { - $this->allParsedFiles[realpath( $file )] = filemtime( $file ); + $this->allParsedFiles[Less_Parser::AbsPath( $file )] = filemtime( $file ); } } diff --git a/lib/Less/Parser.php b/lib/Less/Parser.php index 1c65f982..67b84fc4 100644 --- a/lib/Less/Parser.php +++ b/lib/Less/Parser.php @@ -352,7 +352,7 @@ public function parseFile( $filename, $uri_root = '', $returnRoot = false){ if( $filename ){ - $filename = self::WinPath(realpath($filename)); + $filename = self::AbsPath($filename, true); } $uri_root = self::WinPath($uri_root); @@ -2660,6 +2660,18 @@ public static function WinPath($path){ return str_replace('\\', '/', $path); } + public static function AbsPath($path, $winPath = false){ + if (preg_match('_^(https?:)?//\\w+(\\.\\w+)+/\\w+_', $path)) { + return $winPath ? '' : false; + } else { + $path = realpath($path); + if ($winPath) { + $path = self::WinPath($path); + } + return $path; + } + } + public function CacheEnabled(){ return (Less_Parser::$options['cache_method'] && (Less_Cache::$cache_dir || (Less_Parser::$options['cache_method'] == 'callback'))); } diff --git a/lib/Less/Tree/Import.php b/lib/Less/Tree/Import.php index 4d28abca..4e14afe7 100644 --- a/lib/Less/Tree/Import.php +++ b/lib/Less/Tree/Import.php @@ -289,7 +289,7 @@ public function ParseImport( $full_path, $uri, $env ){ */ private function Skip($path, $env){ - $path = Less_Parser::winPath(realpath($path)); + $path = Less_Parser::AbsPath($path, true); if( $path && Less_Parser::FileParsed($path) ){ From 3f4db8fa880c673c02c3b7174a4b8378b1b6c030 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Fri, 2 Sep 2016 12:54:28 +0200 Subject: [PATCH 2/2] Call preg_match only if necessary --- lib/Less/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Less/Parser.php b/lib/Less/Parser.php index 67b84fc4..eba75742 100644 --- a/lib/Less/Parser.php +++ b/lib/Less/Parser.php @@ -2661,7 +2661,7 @@ public static function WinPath($path){ } public static function AbsPath($path, $winPath = false){ - if (preg_match('_^(https?:)?//\\w+(\\.\\w+)+/\\w+_', $path)) { + if (strpos($path, '//') !== false && preg_match('_^(https?:)?//\\w+(\\.\\w+)+/\\w+_', $path)) { return $winPath ? '' : false; } else { $path = realpath($path);