diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-06-29 08:24:42 -0700 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-06-29 08:24:42 -0700 |
commit | 6ec293dfe70665c528d803b78a2bb295633496ec (patch) | |
tree | be3b7f8d7c2e8ba0e11e3432e2891206272efc1a /modules | |
parent | 10b4eda6f0cbb44fde9fb3158bd174c82467f2ee (diff) |
*Note* work in progress.
Implement the combined css functionality. Local url references and replace with absolute urls instead of relative.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gallery/controllers/combined.php | 46 | ||||
-rw-r--r-- | modules/gallery/libraries/Gallery_View.php | 47 |
2 files changed, 90 insertions, 3 deletions
diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index 510482db..50fe77c4 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -58,5 +58,51 @@ class Combined_Controller extends Controller { Kohana::close_buffers(false); print $content; } + + public function css($key) { + if (preg_match('/[^0-9a-f]/', $key)) { + // The key can't contain non-hex, so just terminate early + Kohana::show_404(); + } + + // We don't need to save the session for this request + Session::abort_save(); + + // Our data is immutable, so if they already have a copy then it needs no updating. + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { + header('HTTP/1.0 304 Not Modified'); + return; + } + + $cache = Cache::instance(); + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { + $content = $cache->get("{$key}_gz"); + } + + if (empty($content)) { + $content = $cache->get($key); + } + + if (empty($content)) { + Kohana::show_404(); + } + + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { + header("Content-Encoding: gzip"); + header("Cache-Control: public"); + } + + header("Content-Type: text/css; charset=UTF-8"); + header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); + + Kohana::close_buffers(false); + print $content; + } + + public function __call($function, $args) { + array_unshift($args, $function); + print "<!-- " . implode("/", $args) . " -->"; + } } diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 7000d3de..1c1dec38 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -113,12 +113,53 @@ class Gallery_View_Core extends View { protected function combine_css() { $links = array(); $key = ""; + static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; + foreach (array_keys($this->css) as $file) { - $links[] = "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" . - url::file($file) . "\" />"; + $path = DOCROOT . $file; + if (file_exists($path)) { + $stats = stat($path); + $links[] = $path; + // 7 == size, 9 == mtime, see http://php.net/stat + $key = "{$key}$file $stats[7] $stats[9],"; + } else { + Kohana::log("alert", "CSS file missing: " . $file); + } + } + $key = md5($key); + $cache = Cache::instance(); + $contents = $cache->get($key); + $docroot_length = strlen(DOCROOT); + + if (empty($contents)) { + $contents = ""; + foreach ($links as $link) { + $css = file_get_contents($link); + if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { + $search = $replace = array(); + foreach ($matches as $match) { + $relative = substr(realpath(dirname($link) . "/$match[1]"), $docroot_length); + if (!empty($relative)) { + $search[] = $match[1]; + $replace[] = url::abs_file($relative); + } else { + Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ", + $match[1], $link)); + } + } + $css = str_replace($search, $replace, $css); + } + $contents .= $css; + } + $cache->set($key, $contents, array("css"), 30 * 84600); + if (function_exists("gzencode")) { + $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array("css", "gzip"), 30 * 84600); + } } - return implode("\n", $links); + return "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" . + url::site("combined/css/$key") . "\" />"; } }
\ No newline at end of file |