root/trunk/app/webroot/css.php

Revision 235, 2.8 kB (checked in by aarkerio, 10 months ago)

New 1.2 CakePHP version

Line 
1<?php
2/* SVN FILE: $Id: css.php 6311 2008-01-02 06:33:52Z phpnut $ */
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.
12 *                              1785 E. Sahara Avenue, Suite 490-204
13 *                              Las Vegas, Nevada 89104
14 *
15 * Licensed under The MIT License
16 * Redistributions of files must retain the above copyright notice.
17 *
18 * @filesource
19 * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
20 * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
21 * @package         cake
22 * @subpackage      cake.app.webroot
23 * @since           CakePHP(tm) v 0.2.9
24 * @version         $Revision: 6311 $
25 * @modifiedby      $LastChangedBy: phpnut $
26 * @lastmodified    $Date: 2008-01-02 00:33:52 -0600 (Wed, 02 Jan 2008) $
27 * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
28 */
29if (!defined('CAKE_CORE_INCLUDE_PATH')) {
30    header('HTTP/1.1 404 Not Found');
31    exit('File Not Found');
32}
33/**
34 * Enter description here...
35 */
36    uses('file');
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        require(VENDORS . 'csspp' . DS . 'csspp.php');
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: cache"); // HTTP/1.1
100    header("Pragma: cache");        // HTTP/1.0
101    print $output;
102?>
Note: See TracBrowser for help on using the browser.