source: tags/1.0.0/lib/CSS.inc.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

File size: 3.6 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.0
10 */
11class CSS {
12
13    // Include these style sheets.
14    var $_css_files = array();
15   
16    // Cache style sheets?
17    var $_params = array(
18        'cache_css' => true,
19        'character_set' => 'ISO-8859-1',
20    );
21   
22    /**
23     * Constructor.
24     */
25    function CSS()
26    {
27        $this->_params['character_set'] = isset($CFG->character_set) ? $CFG->character_set : $this->_params['character_set'];
28    }
29
30    /**
31     * Set (or overwrite existing) parameters by passing an array of new parameters.
32     *
33     * @access public
34     *
35     * @param  array    $params     Array of parameters (key => val pairs).
36     */
37    function setParam($params=null)
38    {
39        if (isset($params) && is_array($params)) {
40            // Merge new parameters with old overriding only those passed.
41            $this->_params = array_merge($this->_params, $params);
42        } else {
43            logMsg(sprintf('Supplied argument is not an array: %s', $params), LOG_WARNING, __FILE__, __LINE__);
44        }
45    }
46
47    /**
48     * Return the value of a parameter.
49     *
50     * @access  public
51     *
52     * @param   string  $param      The key of the parameter to return.
53     *
54     * @return  mixed               Parameter value.
55     */
56    function getParam($param)
57    {
58        return $this->_params[$param];
59    }
60   
61    /**
62     * Add a file-path to the array of files to include as CSS.
63     *
64     * @access  public
65     *
66     * @param   string  Full path to css file.
67     *
68     * @return  bool    True on success, false on failure.
69     */
70    function setFile($file)
71    {
72        if (file_exists($file)) {
73            $this->_css_files[] = $file;
74            return true;
75        } else {
76            logMsg(sprintf('CSS file non-existant: %s', $file), LOG_WARNING, __FILE__, __LINE__);
77            return false;
78        }
79    }
80
81    /**
82     * Output headers for CSS.
83     *
84     * @access  public
85     *
86     * @return  bool    False if no files have been set.
87     */
88    function headers()
89    {
90        if (empty($this->_css_files)) {
91            logMsg(sprintf('CSS::headers called without specifiying any files.', null), LOG_NOTICE, __FILE__, __LINE__);
92            return false;
93        }
94       
95        // Get time of latest modified file.
96        $files_mtime = array_map('filemtime', array_merge($this->_css_files, array(__FILE__)));
97        sort($files_mtime, SORT_NUMERIC);
98        $mtime = array_pop($files_mtime);
99       
100        if ($this->_params['cache_css']) {
101            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT');
102            header('Cache-Control: public, max-age=86400');
103        } else {
104            header('Expires: -1');
105            header('Pragma: no-cache');
106            header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
107        }
108        header('Content-Type: text/css; charset=' . $this->_params['character_set']);
109    }
110   
111    /**
112     * Include CSS files specified by setFile().
113     *
114     * @access  public
115     *
116     * @return  bool    False if no files have been set.
117     */
118    function output()
119    {
120        if (empty($this->_css_files)) {
121            logMsg(sprintf('CSS::output called without specifiying any files.', null), LOG_NOTICE, __FILE__, __LINE__);
122            return false;
123        }
124       
125        foreach ($this->_css_files as $file) {
126            include $file;
127        }
128    }
129}
130?>
Note: See TracBrowser for help on using the repository browser.