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