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 | */ |
---|
11 | class CSS { |
---|
12 | |
---|
13 | // Include these style sheets. |
---|
14 | var $_css_files = array('default' => array()); |
---|
15 | |
---|
16 | // CSS object parameters. |
---|
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 | $app =& App::getInstance(); |
---|
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 | $app =& App::getInstance(); |
---|
50 | |
---|
51 | if (isset($this->_params[$param])) { |
---|
52 | return $this->_params[$param]; |
---|
53 | } else { |
---|
54 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__); |
---|
55 | return null; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Add a file-path to the array of files to include as CSS. |
---|
61 | * |
---|
62 | * @access public |
---|
63 | * @param string $file Include path to css files. |
---|
64 | * @param mixed $realms Realm name string or array of realm names. |
---|
65 | * @return bool True on success, false on failure. |
---|
66 | */ |
---|
67 | function setFile($file, $realms='') |
---|
68 | { |
---|
69 | $app =& App::getInstance(); |
---|
70 | |
---|
71 | if (!is_array($realms)) { |
---|
72 | $realms = array($realms); |
---|
73 | } |
---|
74 | |
---|
75 | if ($fp = fopen($file, 'r', true)) { |
---|
76 | foreach ($realms as $realm) { |
---|
77 | $realm = '' == $realm ? 'default' : $realm; |
---|
78 | $this->_css_files[$realm][] = $file; |
---|
79 | } |
---|
80 | fclose($fp); |
---|
81 | return true; |
---|
82 | } else { |
---|
83 | $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
84 | return false; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Output headers for CSS. |
---|
90 | * |
---|
91 | * @access public |
---|
92 | * |
---|
93 | * @return bool False if no files have been set. |
---|
94 | */ |
---|
95 | function headers($realm='') |
---|
96 | { |
---|
97 | $app =& App::getInstance(); |
---|
98 | |
---|
99 | $realm = '' == $realm ? 'default' : $realm; |
---|
100 | |
---|
101 | if (empty($this->_css_files[$realm])) { |
---|
102 | $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
103 | return false; |
---|
104 | } |
---|
105 | |
---|
106 | // Get time of latest modified file, including this class file. |
---|
107 | $files_mtime = array(); |
---|
108 | foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) { |
---|
109 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
110 | } |
---|
111 | sort($files_mtime, SORT_NUMERIC); |
---|
112 | $latest_mtime = array_pop($files_mtime); |
---|
113 | |
---|
114 | if ($this->_params['cache_css']) { |
---|
115 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT'); |
---|
116 | header('Cache-Control: public, max-age=86400'); |
---|
117 | } else { |
---|
118 | header('Expires: -1'); |
---|
119 | header('Pragma: no-cache'); |
---|
120 | header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
---|
121 | } |
---|
122 | header('Content-Type: text/css; charset=' . $this->_params['character_set']); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Include CSS files specified by setFile(). |
---|
127 | * |
---|
128 | * @access public |
---|
129 | * |
---|
130 | * @return bool False if no files have been set. |
---|
131 | */ |
---|
132 | function output($realm='') |
---|
133 | { |
---|
134 | $realm = '' == $realm ? 'default' : $realm; |
---|
135 | |
---|
136 | if (empty($this->_css_files[$realm])) { |
---|
137 | $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
138 | return false; |
---|
139 | } |
---|
140 | |
---|
141 | foreach ($this->_css_files[$realm] as $file) { |
---|
142 | include $file; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|
147 | ?> |
---|