source: trunk/lib/CSS.inc.php @ 441

Last change on this file since 441 was 396, checked in by anonymous, 12 years ago

Updated copyright date; comments elaboration; spelling fixes.

File size: 5.7 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 */
31class CSS {
32
33    // Include these style sheets.
[33]34    var $_css_files = array('default' => array());
[42]35
[92]36    // CSS object parameters.
[1]37    var $_params = array(
38        'character_set' => 'utf-8',
[393]39        'cache_css' => false,
40        'strip_whitespace' => false,
41        'output_compression' => false,
[1]42    );
[42]43
[1]44    /**
45     * Set (or overwrite existing) parameters by passing an array of new parameters.
46     *
47     * @access public
48     * @param  array    $params     Array of parameters (key => val pairs).
49     */
50    function setParam($params)
51    {
[136]52        $app =& App::getInstance();
[393]53
[1]54        if (isset($params) && is_array($params)) {
55            // Merge new parameters with old overriding only those passed.
56            $this->_params = array_merge($this->_params, $params);
57        } else {
[136]58            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
[1]59        }
60    }
61
62    /**
63     * Return the value of a parameter, if it exists.
64     *
65     * @access public
66     * @param string $param        Which parameter to return.
67     * @return mixed               Configured parameter value.
68     */
69    function getParam($param)
70    {
[136]71        $app =& App::getInstance();
[393]72
[1]73        if (isset($this->_params[$param])) {
74            return $this->_params[$param];
75        } else {
[146]76            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
[1]77            return null;
78        }
79    }
[42]80
[1]81    /**
82     * Add a file-path to the array of files to include as CSS.
83     *
84     * @access  public
[33]85     * @param   string  $file   Include path to css files.
[136]86     * @param   mixed   $realms   Realm name string or array of realm names.
[1]87     * @return  bool    True on success, false on failure.
88     */
[136]89    function setFile($file, $realms='')
[1]90    {
[136]91        $app =& App::getInstance();
[393]92
[136]93        if (!is_array($realms)) {
94            $realms = array($realms);
[33]95        }
96
[26]97        if ($fp = fopen($file, 'r', true)) {
[136]98            foreach ($realms as $realm) {
99                $realm = '' == $realm ? 'default' : $realm;
100                $this->_css_files[$realm][] = $file;
[33]101            }
[26]102            fclose($fp);
[1]103            return true;
104        } else {
[136]105            $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__);
[1]106            return false;
107        }
108    }
109
110    /**
111     * Output headers for CSS.
112     *
113     * @access  public
114     *
115     * @return  bool    False if no files have been set.
116     */
[136]117    function headers($realm='')
[1]118    {
[136]119        $app =& App::getInstance();
[393]120
[136]121        $realm = '' == $realm ? 'default' : $realm;
[33]122
[136]123        if (empty($this->_css_files[$realm])) {
124            $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
[1]125            return false;
126        }
[26]127
[1]128        // Get time of latest modified file, including this class file.
[26]129        $files_mtime = array();
[136]130        foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) {
[26]131            $files_mtime[] = statIncludePath($file, 'mtime');
132        }
[1]133        sort($files_mtime, SORT_NUMERIC);
[26]134        $latest_mtime = array_pop($files_mtime);
[42]135
[393]136        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
137            ob_start('ob_gzhandler');
138        }
139
140        if ($this->getParam('cache_css')) {
[26]141            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT');
[1]142            header('Cache-Control: public, max-age=86400');
[395]143            header('Pragma: public');
[1]144        } else {
145            header('Expires: -1');
146            header('Pragma: no-cache');
147            header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
148        }
[393]149        header('Content-Type: text/css; charset=' . $this->getParam('character_set'));
[1]150    }
[42]151
[1]152    /**
153     * Include CSS files specified by setFile().
154     *
155     * @access  public
156     *
157     * @return  bool    False if no files have been set.
158     */
[136]159    function output($realm='')
[1]160    {
[136]161        $realm = '' == $realm ? 'default' : $realm;
[33]162
[136]163        if (empty($this->_css_files[$realm])) {
164            $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
[1]165            return false;
166        }
[42]167
[136]168        foreach ($this->_css_files[$realm] as $file) {
[393]169            if ($this->getParam('strip_whitespace')) {
170                // Strip whitespace and print file.
171                echo preg_replace('/[ \t\n\r]+/', ' ', file_get_contents($file, true));
172            } else {
173                // Include file as is.
174                include $file;
175            }
[27]176        }
[393]177
178        if ($this->getParam('output_compression') && extension_loaded('zlib')) {
179            ob_end_flush();
180        }
[1]181    }
182}
183?>
Note: See TracBrowser for help on using the repository browser.