css.php

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