Example #1
0
 /**
  * Rewrite a file relative URI as root relative
  *
  * <code>
  * Minify_CSS_UriRewriter::rewriteRelative(
  *       '../img/hello.gif'
  *     , '/home/user/www/css'  // path of CSS file
  *     , '/home/user/www'      // doc root
  * );
  * // returns '/img/hello.gif'
  * 
  * // example where static files are stored in a symlinked directory
  * Minify_CSS_UriRewriter::rewriteRelative(
  *       'hello.gif'
  *     , '/var/staticFiles/theme'
  *     , '/home/user/www'
  *     , array('/home/user/www/static' => '/var/staticFiles')
  * );
  * // returns '/static/theme/hello.gif'
  * </code>
  * 
  * @param string $uri file relative URI
  * 
  * @param string $realCurrentDir realpath of the current file's directory.
  * 
  * @param string $realDocRoot realpath of the site document root.
  * 
  * @param array $symlinks (default = array()) If the file is stored in 
  * a symlink-ed directory, provide an array of link paths to
  * real target paths, where the link paths "appear" to be within the document 
  * root. E.g.:
  * <code>
  * array('/home/foo/www/not/real/path' => '/real/target/path') // unix
  * array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path')  // Windows
  * </code>
  * 
  * @return string
  */
 public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
 {
     if ('/' === $uri[0]) {
         // root-relative
         return $uri;
     }
     // prepend path with current dir separator (OS-independent)
     $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
     self::$debugText .= "file-relative URI  : {$uri}\n" . "path prepended     : {$path}\n";
     // "unresolve" a symlink back to doc root
     foreach ($symlinks as $link => $target) {
         if (0 === strpos($path, $target)) {
             // replace $target with $link
             $path = $link . substr($path, strlen($target));
             self::$debugText .= "symlink unresolved : {$path}\n";
             break;
         }
     }
     // strip doc root
     $path = substr($path, strlen($realDocRoot));
     self::$debugText .= "docroot stripped   : {$path}\n";
     // fix to root-relative URI
     $uri = strtr($path, '/\\', '//');
     // remove /./ and /../ where possible
     $uri = str_replace('/./', '/', $uri);
     // inspired by patch from Oleg Cherniy
     do {
         $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
     } while ($changed);
     self::$debugText .= "traversals removed : {$uri}\n\n";
     $uri = str_replace(w3_get_site_path(), w3_get_blog_path(), $uri);
     return $uri;
 }
Example #2
0
 /**
  * Link replace callback
  *
  * @param array $matches
  * @return string
  */
 function link_replace_callback($matches)
 {
     global $wpdb;
     static $queue = null, $reject_files = null;
     list($match, $quote, $url, $site_url, $path) = $matches;
     $path = ltrim($path, '/');
     /**
      * Check if URL was already replaced
      */
     if (isset($this->replaced_urls[$url])) {
         return $quote . $this->replaced_urls[$url];
     }
     /**
      * Check URL for rejected files
      */
     if ($reject_files === null) {
         $reject_files = $this->_config->get_array('cdn.reject.files');
     }
     foreach ($reject_files as $reject_file) {
         if ($reject_file != '') {
             $reject_file = w3_normalize_file($reject_file);
             $reject_file_regexp = '~^' . $this->get_regexp_by_mask($reject_file) . '$~i';
             if (preg_match($reject_file_regexp, $path)) {
                 return $quote . $url;
             }
         }
     }
     /**
      * Don't replace URL for files that are in the CDN queue
      */
     if ($queue === null) {
         $sql = sprintf('SELECT remote_path FROM %s', $wpdb->prefix . W3TC_CDN_TABLE_QUEUE);
         $queue = $wpdb->get_col($sql);
     }
     if (in_array($path, $queue)) {
         return $quote . $url;
     }
     /**
      * Do replacement
      */
     $cdn =& $this->get_cdn();
     $blog_path = w3_get_blog_path();
     $new_path = $blog_path . $path;
     $new_url = $cdn->format_url($new_path);
     if ($new_url) {
         $this->replaced_urls[$url] = $new_url;
         return $quote . $new_url;
     }
     return $quote . $url;
 }