source: trunk/lib/CSS.inc.php

Last change on this file was 795, checked in by anonymous, 12 months ago

Update cache durations

File size: 6.9 KB
RevLine 
[1]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[393]6 *
[362]7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
[393]13 *
[362]14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
[393]18 *
[362]19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
[42]24 * CSS.inc.css
[1]25 *
26 * Dynamically outputs cached CSS data.
27 *
28 * @author  Quinn Comendant <quinn@strangecode.com>
[26]29 * @version 1.2
[1]30 */
[502]31class CSS
32{
[1]33
34    // Include these style sheets.
[484]35    protected $_css_files = array('default' => array());
[42]36
[92]37    // CSS object parameters.
[484]38    protected $_params = array(
[1]39        'character_set' => 'utf-8',
[554]40        'cache_enable' => false,
41        'cache_css' => null, // Same as above; kept for backwards compatibility.
[795]42        'cache_seconds' => 7776000, // 90 days.
[393]43        'strip_whitespace' => false,
44        'output_compression' => false,
[1]45    );
[42]46
[1]47    /**
48     * Set (or overwrite existing) parameters by passing an array of new parameters.
49     *
50     * @access public
51     * @param  array    $params     Array of parameters (key => val pairs).
52     */
[468]53    public function setParam($params)
[1]54    {
[479]55        $app =& App::getInstance();
[393]56
[1]57        if (isset($params) && is_array($params)) {
58            // Merge new parameters with old overriding only those passed.
59            $this->_params = array_merge($this->_params, $params);
[554]60            // Maintaining backwards compatibility.
61            if (isset($params['cache_css']) && !isset($params['cache_enable'])) {
62                $this->_params['cache_enable'] = $params['cache_css'];
63            }
[1]64        } else {
[136]65            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
[1]66        }
67    }
68
69    /**
70     * Return the value of a parameter, if it exists.
71     *
72     * @access public
73     * @param string $param        Which parameter to return.
74     * @return mixed               Configured parameter value.
75     */
[468]76    public function getParam($param)
[1]77    {
[479]78        $app =& App::getInstance();
[393]79
[478]80        if (array_key_exists($param, $this->_params)) {
[1]81            return $this->_params[$param];
82        } else {
[146]83            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
[1]84            return null;
85        }
86    }
[42]87
[1]88    /**
89     * Add a file-path to the array of files to include as CSS.
90     *
91     * @access  public
[33]92     * @param   string  $file   Include path to css files.
[136]93     * @param   mixed   $realms   Realm name string or array of realm names.
[1]94     * @return  bool    True on success, false on failure.
95     */
[468]96    public function setFile($file, $realms='')
[1]97    {
[479]98        $app =& App::getInstance();
[393]99
[136]100        if (!is_array($realms)) {
101            $realms = array($realms);
[33]102        }
103
[26]104        if ($fp = fopen($file, 'r', true)) {
[136]105            foreach ($realms as $realm) {
106                $realm = '' == $realm ? 'default' : $realm;
107                $this->_css_files[$realm][] = $file;
[33]108            }
[26]109            fclose($fp);
[1]110            return true;
111        } else {
[136]112            $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__);
[1]113            return false;
114        }
115    }
116
117    /**
118     * Output headers for CSS.
119     *
120     * @access  public
121     *
122     * @return  bool    False if no files have been set.
123     */
[468]124    public function headers($realm='')
[1]125    {
[479]126        $app =& App::getInstance();
[393]127
[136]128        $realm = '' == $realm ? 'default' : $realm;
[33]129
[136]130        if (empty($this->_css_files[$realm])) {
131            $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
[1]132            return false;
133        }
[26]134
[1]135        // Get time of latest modified file, including this class file.
[26]136        $files_mtime = array();
[136]137        foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) {
[26]138            $files_mtime[] = statIncludePath($file, 'mtime');
139        }
[1]140        sort($files_mtime, SORT_NUMERIC);
[26]141        $latest_mtime = array_pop($files_mtime);
[42]142
[393]143        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
144            ob_start('ob_gzhandler');
145        }
146
[574]147        header(sprintf('Content-Type: text/css; charset=%s', $this->getParam('character_set')));
[555]148        header(sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime), null));
[554]149        if ($this->getParam('cache_enable')) {
[553]150            header(sprintf('Cache-Control: public, max-age=%s', $this->getParam('cache_seconds')));
[555]151            header(sprintf('Expires: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime + $this->getParam('cache_seconds'))));
[395]152            header('Pragma: public');
[555]153            header('Vary: Accept-Encoding');
[1]154        } else {
[603]155            // Disallow HTTP caching entirely. http://stackoverflow.com/a/2068407
156            header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
157            header('Pragma: no-cache'); // HTTP 1.0.
158            header('Expires: 0'); // Proxies.
[1]159        }
160    }
[42]161
[1]162    /**
163     * Include CSS files specified by setFile().
164     *
165     * @access  public
166     *
167     * @return  bool    False if no files have been set.
168     */
[468]169    public function output($realm='')
[1]170    {
[473]171        $app =& App::getInstance();
172
[136]173        $realm = '' == $realm ? 'default' : $realm;
[33]174
[136]175        if (empty($this->_css_files[$realm])) {
176            $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
[1]177            return false;
178        }
[42]179
[136]180        foreach ($this->_css_files[$realm] as $file) {
[393]181            if ($this->getParam('strip_whitespace')) {
182                // Strip whitespace and print file.
[471]183                echo preg_replace(
[724]184                    array('!/\*.*?\*/!s' . $app->getParam('preg_u'), '/[\n\r]+/' . $app->getParam('preg_u'), '/([;:])\s+/m' . $app->getParam('preg_u'), '/\s*}[ \t]*/' . $app->getParam('preg_u'), '/\s*{\s*/' . $app->getParam('preg_u'), '/[ \t\n\r]*,[ \t\n\r]*/' . $app->getParam('preg_u'), '/^\s+/' . $app->getParam('preg_u')),
[471]185                    array('', "\n", '$1', '}', '{', ',', ''), file_get_contents($file, true)
186                );
[393]187            } else {
188                // Include file as is.
189                include $file;
190            }
[27]191        }
[393]192
193        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
194            ob_end_flush();
195        }
[1]196    }
197}
Note: See TracBrowser for help on using the repository browser.