1 | <?php |
---|
2 | /** |
---|
3 | * CSS helping functions |
---|
4 | * |
---|
5 | * PHP versions 4 and 5 |
---|
6 | * |
---|
7 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
8 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
9 | * |
---|
10 | * Licensed under The MIT License |
---|
11 | * Redistributions of files must retain the above copyright notice. |
---|
12 | * |
---|
13 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
14 | * @link http://cakephp.org CakePHP(tm) Project |
---|
15 | * @package cake |
---|
16 | * @subpackage cake.app.webroot |
---|
17 | * @since CakePHP(tm) v 0.2.9 |
---|
18 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
19 | */ |
---|
20 | if (!defined('CAKE_CORE_INCLUDE_PATH')) { |
---|
21 | header('HTTP/1.1 404 Not Found'); |
---|
22 | exit('File Not Found'); |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Ensure required classes are available. |
---|
27 | */ |
---|
28 | if (!class_exists('File')) { |
---|
29 | uses('file'); |
---|
30 | } |
---|
31 | |
---|
32 | /** |
---|
33 | * Make clean CSS |
---|
34 | * |
---|
35 | * @param unknown_type $path |
---|
36 | * @param unknown_type $name |
---|
37 | * @return unknown |
---|
38 | */ |
---|
39 | function make_clean_css($path, $name) { |
---|
40 | App::import('Vendor', 'csspp' . DS . 'csspp'); |
---|
41 | $data = file_get_contents($path); |
---|
42 | $csspp = new csspp(); |
---|
43 | $output = $csspp->compress($data); |
---|
44 | $ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100); |
---|
45 | $output = " /* file: $name, ratio: $ratio% */ " . $output; |
---|
46 | return $output; |
---|
47 | } |
---|
48 | /** |
---|
49 | * Write CSS cache |
---|
50 | * |
---|
51 | * @param unknown_type $path |
---|
52 | * @param unknown_type $content |
---|
53 | * @return unknown |
---|
54 | */ |
---|
55 | function write_css_cache($path, $content) { |
---|
56 | if (!is_dir(dirname($path))) { |
---|
57 | mkdir(dirname($path)); |
---|
58 | } |
---|
59 | $cache = new File($path); |
---|
60 | return $cache->write($content); |
---|
61 | } |
---|
62 | |
---|
63 | if (preg_match('|\.\.|', $url) || !preg_match('|^ccss/(.+)$|i', $url, $regs)) { |
---|
64 | die('Wrong file name.'); |
---|
65 | } |
---|
66 | |
---|
67 | $filename = 'css/' . $regs[1]; |
---|
68 | $filepath = CSS . $regs[1]; |
---|
69 | $cachepath = CACHE . 'css' . DS . str_replace(array('/','\\'), '-', $regs[1]); |
---|
70 | |
---|
71 | if (!file_exists($filepath)) { |
---|
72 | die('Wrong file name.'); |
---|
73 | } |
---|
74 | |
---|
75 | if (file_exists($cachepath)) { |
---|
76 | $templateModified = filemtime($filepath); |
---|
77 | $cacheModified = filemtime($cachepath); |
---|
78 | |
---|
79 | if ($templateModified > $cacheModified) { |
---|
80 | $output = make_clean_css($filepath, $filename); |
---|
81 | write_css_cache($cachepath, $output); |
---|
82 | } else { |
---|
83 | $output = file_get_contents($cachepath); |
---|
84 | } |
---|
85 | } else { |
---|
86 | $output = make_clean_css($filepath, $filename); |
---|
87 | write_css_cache($cachepath, $output); |
---|
88 | $templateModified = time(); |
---|
89 | } |
---|
90 | |
---|
91 | header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT'); |
---|
92 | header("Content-Type: text/css"); |
---|
93 | header("Expires: " . gmdate("D, d M Y H:i:s", time() + DAY) . " GMT"); |
---|
94 | header("Cache-Control: max-age=86400, must-revalidate"); // HTTP/1.1 |
---|
95 | header("Pragma: cache"); // HTTP/1.0 |
---|
96 | print $output; |
---|