PEEL Shopping
Open source ecommerce : PEEL Shopping
Minify_CSS_UriRewriter.php
Go to the documentation of this file.
1 <?php
14 
20  public static $debugText = '';
21 
44  public static function rewrite($css, $currentDir, $docRoot = null, $symlinks = array())
45  {
46  self::$_docRoot = self::_realpath(
47  $docRoot ? $docRoot : $_SERVER['DOCUMENT_ROOT']
48  );
49  self::$_currentDir = self::_realpath($currentDir);
50  self::$_symlinks = array();
51 
52  // normalize symlinks
53  foreach ($symlinks as $link => $target) {
54  $link = ($link === '//')
55  ? self::$_docRoot
56  : str_replace('//', self::$_docRoot . '/', $link);
57  $link = strtr($link, '/', DIRECTORY_SEPARATOR);
58  self::$_symlinks[$link] = self::_realpath($target);
59  }
60 
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";
65  }
66  self::$debugText .= "\n";
67 
68  $css = self::_trimUrls($css);
69 
70  // rewrite
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);
75  return $css;
76  }
77 
87  public static function prepend($css, $path)
88  {
89  self::$_prependPath = $path;
90 
91  $css = self::_trimUrls($css);
92 
93  // append
94  $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
95  ,array(self::$className, '_processUriCB'), $css);
96  $css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
97  ,array(self::$className, '_processUriCB'), $css);
98 
99  self::$_prependPath = null;
100  return $css;
101  }
102 
141  public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
142  {
143  // prepend path with current dir separator (OS-independent)
144  $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR)
145  . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
146 
147  self::$debugText .= "file-relative URI : {$uri}\n"
148  . "path prepended : {$path}\n";
149 
150  // "unresolve" a symlink back to doc root
151  foreach ($symlinks as $link => $target) {
152  if (0 === strpos($path, $target)) {
153  // replace $target with $link
154  $path = $link . substr($path, strlen($target));
155 
156  self::$debugText .= "symlink unresolved : {$path}\n";
157 
158  break;
159  }
160  }
161  // strip doc root
162  $path = substr($path, strlen($realDocRoot));
163 
164  self::$debugText .= "docroot stripped : {$path}\n";
165 
166  // fix to root-relative URI
167  $uri = strtr($path, '/\\', '//');
168  $uri = self::removeDots($uri);
169 
170  self::$debugText .= "traversals removed : {$uri}\n\n";
171 
172  return $uri;
173  }
174 
182  public static function removeDots($uri)
183  {
184  $uri = str_replace('/./', '/', $uri);
185  // inspired by patch from Oleg Cherniy
186  $i=0;
187  do {
188  $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
189  } while ($changed && $i++<100);
190  return $uri;
191  }
192 
199  protected static $className = 'Minify_CSS_UriRewriter';
200 
209  protected static function _realpath($path)
210  {
211  $realPath = realpath($path);
212  if ($realPath !== false) {
213  $path = $realPath;
214  }
215  return rtrim($path, '/\\');
216  }
217 
223  private static $_currentDir = '';
224 
230  private static $_docRoot = '';
231 
238  private static $_symlinks = array();
239 
245  private static $_prependPath = null;
246 
252  private static function _trimUrls($css)
253  {
254  return preg_replace('/
255  url\\( # url(
256  \\s*
257  ([^\\)]+?) # 1 = URI (assuming does not contain ")")
258  \\s*
259  \\) # )
260  /x', 'url($1)', $css);
261  }
262 
268  private static function _processUriCB($m)
269  {
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)
273  if ($isImport) {
274  $quoteChar = $m[1];
275  $uri = $m[2];
276  } else {
277  // $m[1] is either quoted or not
278  $quoteChar = ($m[1][0] === "'" || $m[1][0] === '"')
279  ? $m[1][0]
280  : '';
281  $uri = ($quoteChar === '')
282  ? $m[1]
283  : substr($m[1], 1, strlen($m[1]) - 2);
284  }
285  // analyze URI
286  if ('/' !== $uri[0] // root-relative
287  && false === strpos($uri, '//') // protocol (non-data)
288  && 0 !== strpos($uri, 'data:') // data protocol
289  ) {
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);
293  } else {
294  $uri = self::$_prependPath . $uri;
295  if ($uri[0] === '/') {
296  $root = '';
297  $rootRelative = $uri;
298  $uri = $root . self::removeDots($rootRelative);
299  } elseif (preg_match('@^((https?\:)?//([^/]+))/@', $uri, $m) && (false !== strpos($m[3], '.'))) {
300  $root = $m[1];
301  $rootRelative = substr($uri, strlen($root));
302  $uri = $root . self::removeDots($rootRelative);
303  }
304  }
305  }
306  return $isImport
307  ? "@import {$quoteChar}{$uri}{$quoteChar}"
308  : "url({$quoteChar}{$uri}{$quoteChar})";
309  }
310 }
static removeDots($uri)
Remove instances of "./" and "../" where possible from a root-relative URI.
static _realpath($path)
Get realpath with any trailing slash removed.
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
static rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks=array())
Get a root relative URI from a file relative URI.
static prepend($css, $path)
In CSS content, prepend a path to relative URIs.
static rewrite($css, $currentDir, $docRoot=null, $symlinks=array())
In CSS content, rewrite file relative URIs as root relative.

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:30:03 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.