3 * Class Minify_CSS_UriRewriter
8 * Rewrite file-relative URIs as root-relative in CSS files
11 * @author Stephen Clay <steve@mrclay.org>
13 class Minify_CSS_UriRewriter {
16 * rewrite() and rewriteRelative() append debugging information here
20 public static $debugText = '';
23 * In CSS content, rewrite file relative URIs as root relative
27 * @param string $currentDir The directory of the current CSS file.
29 * @param string $docRoot The document root of the web site in which
30 * the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
32 * @param array $symlinks (default = array()) If the CSS file is stored in
33 * a symlink-ed directory, provide an array of link paths to
34 * target paths, where the link paths are within the document root. Because
35 * paths need to be normalized for this to work, use "//" to substitute
36 * the doc root in the link paths (the array keys). E.g.:
38 * array('//symlink' => '/real/target/path') // unix
39 * array('//static' => 'D:\\staticStorage') // Windows
44 public static function rewrite($css, $currentDir, $docRoot = null, $symlinks = array())
46 self::$_docRoot = self::_realpath(
47 $docRoot ? $docRoot : $_SERVER['DOCUMENT_ROOT']
49 self::$_currentDir = self::_realpath($currentDir);
50 self::$_symlinks = array();
53 foreach ($symlinks as $link => $target) {
54 $link = ($link === '//')
56 : str_replace('//', self::$_docRoot . '/', $link);
57 $link = strtr($link, '/', DIRECTORY_SEPARATOR);
58 self::$_symlinks[$link] = self::_realpath($target);
61 self::$debugText .= "docRoot : " . self::$_docRoot . "\n"
62 . "currentDir : " . self::$_currentDir . "\n";
63 if (self::$_symlinks) {
64 self::$debugText .= "symlinks : " . var_export(self::$_symlinks, 1) . "\n";
66 self::$debugText .= "\n";
68 $css = self::_trimUrls($css);
71 $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
72 ,array(self::$className, '_processUriCB'), $css);
73 $css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
74 ,array(self::$className, '_processUriCB'), $css);
80 * In CSS content, prepend a path to relative URIs
84 * @param string $path The path to prepend.
88 public static function prepend($css, $path)
90 self::$_prependPath = $path;
92 $css = self::_trimUrls($css);
95 $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
96 ,array(self::$className, '_processUriCB'), $css);
97 $css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
98 ,array(self::$className, '_processUriCB'), $css);
100 self::$_prependPath = null;
105 * Get a root relative URI from a file relative URI
108 * Minify_CSS_UriRewriter::rewriteRelative(
110 * , '/home/user/www/css' // path of CSS file
111 * , '/home/user/www' // doc root
113 * // returns '/img/hello.gif'
115 * // example where static files are stored in a symlinked directory
116 * Minify_CSS_UriRewriter::rewriteRelative(
118 * , '/var/staticFiles/theme'
120 * , array('/home/user/www/static' => '/var/staticFiles')
122 * // returns '/static/theme/hello.gif'
125 * @param string $uri file relative URI
127 * @param string $realCurrentDir realpath of the current file's directory.
129 * @param string $realDocRoot realpath of the site document root.
131 * @param array $symlinks (default = array()) If the file is stored in
132 * a symlink-ed directory, provide an array of link paths to
133 * real target paths, where the link paths "appear" to be within the document
136 * array('/home/foo/www/not/real/path' => '/real/target/path') // unix
137 * array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows
142 public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
144 // prepend path with current dir separator (OS-independent)
145 $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR)
146 . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
148 self::$debugText .= "file-relative URI : {$uri}\n"
149 . "path prepended : {$path}\n";
151 // "unresolve" a symlink back to doc root
152 foreach ($symlinks as $link => $target) {
153 if (0 === strpos($path, $target)) {
154 // replace $target with $link
155 $path = $link . substr($path, strlen($target));
157 self::$debugText .= "symlink unresolved : {$path}\n";
163 $path = substr($path, strlen($realDocRoot));
165 self::$debugText .= "docroot stripped : {$path}\n";
167 // fix to root-relative URI
168 $uri = strtr($path, '/\\', '//');
169 $uri = self::removeDots($uri);
171 self::$debugText .= "traversals removed : {$uri}\n\n";
177 * Remove instances of "./" and "../" where possible from a root-relative URI
183 public static function removeDots($uri)
185 $uri = str_replace('/./', '/', $uri);
186 // inspired by patch from Oleg Cherniy
188 $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
194 * Defines which class to call as part of callbacks, change this
195 * if you extend Minify_CSS_UriRewriter
199 protected static $className = 'Minify_CSS_UriRewriter';
202 * Get realpath with any trailing slash removed. If realpath() fails,
203 * just remove the trailing slash.
205 * @param string $path
207 * @return mixed path with no trailing slash
209 protected static function _realpath($path)
211 $realPath = realpath($path);
212 if ($realPath !== false) {
215 return rtrim($path, '/\\');
219 * Directory of this stylesheet
223 private static $_currentDir = '';
230 private static $_docRoot = '';
233 * directory replacements to map symlink targets back to their
234 * source (within the document root) E.g. '/var/www/symlink' => '/var/realpath'
238 private static $_symlinks = array();
245 private static $_prependPath = null;
252 private static function _trimUrls($css)
254 return preg_replace('/
257 ([^\\)]+?) # 1 = URI (assuming does not contain ")")
260 /x', 'url($1)', $css);
268 private static function _processUriCB($m)
270 // $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
271 $isImport = ($m[0][0] === '@');
272 // determine URI and the quote character (if any)
277 // $m[1] is either quoted or not
278 $quoteChar = ($m[1][0] === "'" || $m[1][0] === '"')
281 $uri = ($quoteChar === '')
283 : substr($m[1], 1, strlen($m[1]) - 2);
286 if ('/' !== $uri[0] // root-relative
287 && false === strpos($uri, '//') // protocol (non-data)
288 && 0 !== strpos($uri, 'data:') // data protocol
290 // URI is file-relative: rewrite depending on options
291 if (self::$_prependPath === null) {
292 $uri = self::rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks);
294 $uri = self::$_prependPath . $uri;
295 if ($uri[0] === '/') {
297 $rootRelative = $uri;
298 $uri = $root . self::removeDots($rootRelative);
299 } elseif (preg_match('@^((https?\:)?//([^/]+))/@', $uri, $m) && (false !== strpos($m[3], '.'))) {
301 $rootRelative = substr($uri, strlen($root));
302 $uri = $root . self::removeDots($rootRelative);
307 ? "@import {$quoteChar}{$uri}{$quoteChar}"
308 : "url({$quoteChar}{$uri}{$quoteChar})";