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

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