1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: if (!defined('CAKE_CORE_INCLUDE_PATH')) {
27: header('HTTP/1.1 404 Not Found');
28: exit('File Not Found');
29: }
30: 31: 32:
33: if (!class_exists('File')) {
34: uses('file');
35: }
36: 37: 38: 39: 40: 41: 42:
43: function make_clean_css($path, $name) {
44: App::import('Vendor', 'csspp' . DS . 'csspp');
45: $data = file_get_contents($path);
46: $csspp = new csspp();
47: $output = $csspp->compress($data);
48: $ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100);
49: $output = " /* file: $name, ratio: $ratio% */ " . $output;
50: return $output;
51: }
52: 53: 54: 55: 56: 57: 58:
59: function write_css_cache($path, $content) {
60: if (!is_dir(dirname($path))) {
61: mkdir(dirname($path));
62: }
63: $cache = new File($path);
64: return $cache->write($content);
65: }
66:
67: if (preg_match('|\.\.|', $url) || !preg_match('|^ccss/(.+)$|i', $url, $regs)) {
68: exit('Wrong file name.');
69: }
70:
71: $filename = 'css/' . $regs[1];
72: $filepath = CSS . $regs[1];
73: $cachepath = CACHE . 'css' . DS . str_replace(array('/','\\'), '-', $regs[1]);
74:
75: if (!file_exists($filepath)) {
76: exit('Wrong file name.');
77: }
78:
79: if (file_exists($cachepath)) {
80: $templateModified = filemtime($filepath);
81: $cacheModified = filemtime($cachepath);
82:
83: if ($templateModified > $cacheModified) {
84: $output = make_clean_css($filepath, $filename);
85: write_css_cache($cachepath, $output);
86: } else {
87: $output = file_get_contents($cachepath);
88: }
89: } else {
90: $output = make_clean_css($filepath, $filename);
91: write_css_cache($cachepath, $output);
92: $templateModified = time();
93: }
94:
95: header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT');
96: header("Content-Type: text/css");
97: header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
98: header("Cache-Control: max-age=86400, must-revalidate");
99: header("Pragma: cache");
100: print $output;
101: ?>