diff --git a/cloudfiles-cdn.php b/cloudfiles-cdn.php index 6f7e2b96fa832146a706012407461999bccd1fab..9cbf3de41c2b175c58ea717c5b8e04af1e4130b1 100644 --- a/cloudfiles-cdn.php +++ b/cloudfiles-cdn.php @@ -362,4 +362,137 @@ class CloudfilesCdn { } require_once('voce-settings.php'); -$cdn = new CloudfilesCdn(); \ No newline at end of file +$cdn = new CloudfilesCdn(); + +class CDN_Rewrite { + + private $file_extensions; + private $blog_details; + private $cdn_root_url; + + public function __construct() { + $this->file_extensions = 'bmp|bz2|css|gif|ico|gz|jpg|jpeg|js|mp3|pdf|png|rar|rtf|swf|tar|tgz|txt|wav|zip'; + $this->cdn_root_url = trailingslashit(CloudfilesCdn::get_setting('root_url')); + } + + /** + * Initializes class and registers hooks + * + */ + public function initialize() { + if(!empty($this->cdn_root_url)) { + add_action('template_redirect', array($this, 'start_buffer'), 1); + } + } + + /** + * start output buffering. + * + */ + public function start_buffer() { + ob_start(array($this, 'filter_urls')); + } + + /** + * Callback for output buffering. Search content for urls to replace + * + * @param string $content + * @return string + */ + public function filter_urls($content) { + $root_url = $this->get_site_root_url(); + $regex = '#(?<=[(\"\'])'.quotemeta($root_url).'(?:(/[^\"\')]+\.('.$this->file_extensions.')))#'; + + $content = preg_replace_callback($regex, array($this, 'url_rewrite'), $content); + + return $content; + } + + /** + * Returns the root url of the current site + * + * @return string + */ + public function get_site_root_url() { + if(is_multisite() && !is_subdomain_install()) { + $root_blog = get_blog_details(1); + $root_url = $root_blog->siteurl; + } else { + $root_url = site_url(); + } + return $root_url; + } + + /** + * Returns the details for the current blog + * + * @return object + */ + public function get_this_blog_details() { + if(!isset($this->blog_details)) { + global $blog_id; + $this->blog_details = get_blog_details($blog_id); + } + return $this->blog_details; + } + + /** + * Callback for url preg_replace_callback. Returns corrected URL + * + * @param array $match + * @return string + */ + public function url_rewrite($match) { + global $blog_id; + $path = $match[1]; + //if is subfolder install and isn't root blog and path starts with site_url and isnt uploads dir + if(is_multisite() && !is_subdomain_install() && $blog_id !== 1) { + $bloginfo = $this->get_this_blog_details(); + if((0 === strpos($path, $bloginfo->path)) && (0 !== strpos($path, $bloginfo->path.'files/'))) { + $path = '/'.substr($path, strlen($bloginfo->path)); + } + } + return $this->cdn_root_url . $path; + } +} +add_action('init', array(new CDN_Rewrite(), 'initialize')); + +class CDN_VersionAssets { + + private $default_version = ''; + private $root_url; + + public function __construct() { + $this->root_url = site_url(); + } + + public function initialize() { + add_filter('style_loader_src', array($this, 'replace_version'), 10); + add_filter('script_loader_src', array($this, 'replace_version'), 10); + add_filter('style_loader_src', array($this, 'replace_version'), 10); + } + + public function on_template_redirect() { + $this->default_version = @filemtime(get_stylesheet_directory().'/style.css'); + } + + private function get_version($url) { + if(0 === strpos($url, $this->root_url)) { + $parts = parse_url($url); + $file_path = str_replace(site_url('/'), ABSPATH, $parts['scheme'].'://'.$parts['host'].$parts['path']); + if( !($version = @filemtime($file_path)) ) { + $version = $this->default_version; + } + return $version; + } + return false; + } + + public function replace_version($src) { + if( $new_version = $this->get_version($src) ) { + return add_query_arg('ver', $new_version, $src); + } + return $src; + } +} +add_action('init', array(new CDN_VersionAssets(), 'initialize')); \ No newline at end of file