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

Last change on this file since 27 was 27, checked in by scdev, 18 years ago
File size: 3.8 KB
Line 
1<?php
2/**
3 * CSS.inc.css
4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
5 *
6 * Dynamically outputs cached CSS data.
7 *
8 * @author  Quinn Comendant <quinn@strangecode.com>
9 * @version 1.2
10 */
11class CSS {
12
13    // Include these style sheets.
14    var $_css_files = array('_default' => array());
15   
16    // Cache style sheets?
17    var $_params = array(
18        'cache_css' => true,
19        'character_set' => 'utf-8',
20    );
21   
22    /**
23     * Set (or overwrite existing) parameters by passing an array of new parameters.
24     *
25     * @access public
26     * @param  array    $params     Array of parameters (key => val pairs).
27     */
28    function setParam($params)
29    {
30        if (isset($params) && is_array($params)) {
31            // Merge new parameters with old overriding only those passed.
32            $this->_params = array_merge($this->_params, $params);
33        } else {
34            App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
35        }
36    }
37
38    /**
39     * Return the value of a parameter, if it exists.
40     *
41     * @access public
42     * @param string $param        Which parameter to return.
43     * @return mixed               Configured parameter value.
44     */
45    function getParam($param)
46    {
47        if (isset($this->_params[$param])) {
48            return $this->_params[$param];
49        } else {
50            App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
51            return null;
52        }
53    }
54   
55    /**
56     * Add a file-path to the array of files to include as CSS.
57     *
58     * @access  public
59     *
60     * @param   string  Full path to css file.
61     *
62     * @return  bool    True on success, false on failure.
63     */
64    function setFile($file, $app='_default')
65    {
66        if ($fp = fopen($file, 'r', true)) {
67            $this->_css_files[$app][] = $file;
68            fclose($fp);
69            return true;
70        } else {
71            App::logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__);
72            return false;
73        }
74    }
75
76    /**
77     * Output headers for CSS.
78     *
79     * @access  public
80     *
81     * @return  bool    False if no files have been set.
82     */
83    function headers($app='_default')
84    {
85        if (empty($this->_css_files[$app])) {
86            App::logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
87            return false;
88        }
89
90        // Get time of latest modified file, including this class file.
91        $files_mtime = array();
92        foreach (array_merge($this->_css_files[$app], array(__FILE__)) as $file) {
93            $files_mtime[] = statIncludePath($file, 'mtime');
94        }
95        sort($files_mtime, SORT_NUMERIC);
96        $latest_mtime = array_pop($files_mtime);
97       
98        if ($this->_params['cache_css']) {
99            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT');
100            header('Cache-Control: public, max-age=86400');
101        } else {
102            header('Expires: -1');
103            header('Pragma: no-cache');
104            header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
105        }
106        header('Content-Type: text/css; charset=' . $this->_params['character_set']);
107    }
108   
109    /**
110     * Include CSS files specified by setFile().
111     *
112     * @access  public
113     *
114     * @return  bool    False if no files have been set.
115     */
116    function output($app='_default')
117    {
118        if (empty($this->_css_files[$app])) {
119            App::logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__);
120            return false;
121        }
122       
123        foreach ($this->_css_files[$app] as $file) {
124            include $file;
125        }
126    }
127   
128}
129?>
Note: See TracBrowser for help on using the repository browser.