<?php
 class Minify_ImportProcessor { public static $filesIncluded = array(); public static function process($file) { self::$filesIncluded = array(); self::$_isCss = (strtolower(substr($file, -4)) === '.css'); $obj = new Minify_ImportProcessor(dirname($file)); return $obj->_getContent($file); } private $_currentDir = null; private $_importedContent = ''; private static $_isCss = null; private function __construct($currentDir) { $this->_currentDir = $currentDir; } private function _getContent($file) { $file = realpath($file); if (! $file || in_array($file, self::$filesIncluded) || false === ($content = @file_get_contents($file)) ) { return ''; } self::$filesIncluded[] = realpath($file); $this->_currentDir = dirname($file); if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) { $content = substr($content, 3); } $content = str_replace("\r\n", "\n", $content); $content = preg_replace_callback( '/
                @import\\s+
                (?:url\\(\\s*)?      # maybe url(
                [\'"]?               # maybe quote
                (.*?)                # 1 = URI
                [\'"]?               # maybe end quote
                (?:\\s*\\))?         # maybe )
                ([a-zA-Z,\\s]*)?     # 2 = media list
                ;                    # end token
            /x' ,array($this, '_importCB') ,$content ); if (self::$_isCss) { $content = preg_replace_callback( '/url\\(\\s*([^\\)\\s]+)\\s*\\)/' ,array($this, '_urlCB') ,$content ); } return $this->_importedContent . $content; } private function _importCB($m) { $url = $m[1]; $mediaList = preg_replace('/\\s+/', '', $m[2]); if (strpos($url, '://') > 0) { return self::$_isCss ? $m[0] : "/* Minify_ImportProcessor will not include remote content */"; } if ('/' === $url[0]) { $url = ltrim($url, '/'); $file = realpath($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); } else { $file = $this->_currentDir . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); } $obj = new Minify_ImportProcessor(dirname($file)); $content = $obj->_getContent($file); if ('' === $content) { return self::$_isCss ? $m[0] : "/* Minify_ImportProcessor could not fetch '{$file}' */";; } return (!self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList)) ? $content : "@media {$mediaList} {\n{$content}\n}\n"; } private function _urlCB($m) { $quote = ($m[1][0] === "'" || $m[1][0] === '"') ? $m[1][0] : ''; $url = ($quote === '') ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2); if ('/' !== $url[0]) { if (strpos($url, '//') > 0) { } else { $path = $this->_currentDir . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); $path = substr($path, strlen(realpath($_SERVER['DOCUMENT_ROOT']))); $url = strtr($path, '/\\', '//'); $url = str_replace('/./', '/', $url); do { $url = preg_replace('@/[^/]+/\\.\\./@', '/', $url, 1, $changed); } while ($changed); } } return "url({$quote}{$url}{$quote})"; } } 