css.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: app_2webroot_2css_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00011  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00018  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package       cake
00020  * @subpackage    cake.app.webroot
00021  * @since         CakePHP(tm) v 0.2.9
00022  * @version       $Revision: 675 $
00023  * @modifiedby    $LastChangedBy: gwoo $
00024  * @lastmodified  $Date: 2008-12-25 18:27:14 -0600 (Thu, 25 Dec 2008) $
00025  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 if (!defined('CAKE_CORE_INCLUDE_PATH')) {
00028     header('HTTP/1.1 404 Not Found');
00029     exit('File Not Found');
00030 }
00031 /**
00032  * Enter description here...
00033  */
00034 if (!class_exists('File')) {
00035     uses('file');
00036 }
00037 /**
00038  * Enter description here...
00039  *
00040  * @param unknown_type $path
00041  * @param unknown_type $name
00042  * @return unknown
00043  */
00044     function make_clean_css($path, $name) {
00045         App::import('Vendor', 'csspp' . DS . 'csspp');
00046         $data = file_get_contents($path);
00047         $csspp = new csspp();
00048         $output = $csspp->compress($data);
00049         $ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100);
00050         $output = " /* file: $name, ratio: $ratio% */ " . $output;
00051         return $output;
00052     }
00053 /**
00054  * Enter description here...
00055  *
00056  * @param unknown_type $path
00057  * @param unknown_type $content
00058  * @return unknown
00059  */
00060     function write_css_cache($path, $content) {
00061         if (!is_dir(dirname($path))) {
00062             mkdir(dirname($path));
00063         }
00064         $cache = new File($path);
00065         return $cache->write($content);
00066     }
00067 
00068     if (preg_match('|\.\.|', $url) || !preg_match('|^ccss/(.+)$|i', $url, $regs)) {
00069         die('Wrong file name.');
00070     }
00071 
00072     $filename = 'css/' . $regs[1];
00073     $filepath = CSS . $regs[1];
00074     $cachepath = CACHE . 'css' . DS . str_replace(array('/','\\'), '-', $regs[1]);
00075 
00076     if (!file_exists($filepath)) {
00077         die('Wrong file name.');
00078     }
00079 
00080     if (file_exists($cachepath)) {
00081         $templateModified = filemtime($filepath);
00082         $cacheModified = filemtime($cachepath);
00083 
00084         if ($templateModified > $cacheModified) {
00085             $output = make_clean_css($filepath, $filename);
00086             write_css_cache($cachepath, $output);
00087         } else {
00088             $output = file_get_contents($cachepath);
00089         }
00090     } else {
00091         $output = make_clean_css($filepath, $filename);
00092         write_css_cache($cachepath, $output);
00093         $templateModified = time();
00094     }
00095 
00096     header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT');
00097     header("Content-Type: text/css");
00098     header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
00099     header("Cache-Control: max-age=86400, must-revalidate"); // HTTP/1.1
00100     header("Pragma: cache");        // HTTP/1.0
00101     print $output;
00102 ?>