1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2012 Strangecode, LLC |
---|
6 | * |
---|
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. |
---|
13 | * |
---|
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. |
---|
18 | * |
---|
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 | /** |
---|
24 | * CSS.inc.css |
---|
25 | * |
---|
26 | * Dynamically outputs cached CSS data. |
---|
27 | * |
---|
28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
29 | * @version 1.2 |
---|
30 | */ |
---|
31 | class CSS { |
---|
32 | |
---|
33 | // Include these style sheets. |
---|
34 | protected $_css_files = array('default' => array()); |
---|
35 | |
---|
36 | // CSS object parameters. |
---|
37 | protected $_params = array( |
---|
38 | 'character_set' => 'utf-8', |
---|
39 | 'cache_css' => false, |
---|
40 | 'strip_whitespace' => false, |
---|
41 | 'output_compression' => false, |
---|
42 | ); |
---|
43 | |
---|
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 | public function setParam($params) |
---|
51 | { |
---|
52 | $app =& App::getInstance(); |
---|
53 | |
---|
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 { |
---|
58 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
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 | public function getParam($param) |
---|
70 | { |
---|
71 | $app =& App::getInstance(); |
---|
72 | |
---|
73 | if (array_key_exists($param, $this->_params)) { |
---|
74 | return $this->_params[$param]; |
---|
75 | } else { |
---|
76 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
77 | return null; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Add a file-path to the array of files to include as CSS. |
---|
83 | * |
---|
84 | * @access public |
---|
85 | * @param string $file Include path to css files. |
---|
86 | * @param mixed $realms Realm name string or array of realm names. |
---|
87 | * @return bool True on success, false on failure. |
---|
88 | */ |
---|
89 | public function setFile($file, $realms='') |
---|
90 | { |
---|
91 | $app =& App::getInstance(); |
---|
92 | |
---|
93 | if (!is_array($realms)) { |
---|
94 | $realms = array($realms); |
---|
95 | } |
---|
96 | |
---|
97 | if ($fp = fopen($file, 'r', true)) { |
---|
98 | foreach ($realms as $realm) { |
---|
99 | $realm = '' == $realm ? 'default' : $realm; |
---|
100 | $this->_css_files[$realm][] = $file; |
---|
101 | } |
---|
102 | fclose($fp); |
---|
103 | return true; |
---|
104 | } else { |
---|
105 | $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
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 | */ |
---|
117 | public function headers($realm='') |
---|
118 | { |
---|
119 | $app =& App::getInstance(); |
---|
120 | |
---|
121 | $realm = '' == $realm ? 'default' : $realm; |
---|
122 | |
---|
123 | if (empty($this->_css_files[$realm])) { |
---|
124 | $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | // Get time of latest modified file, including this class file. |
---|
129 | $files_mtime = array(); |
---|
130 | foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) { |
---|
131 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
132 | } |
---|
133 | sort($files_mtime, SORT_NUMERIC); |
---|
134 | $latest_mtime = array_pop($files_mtime); |
---|
135 | |
---|
136 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
137 | ob_start('ob_gzhandler'); |
---|
138 | } |
---|
139 | |
---|
140 | if ($this->getParam('cache_css')) { |
---|
141 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT'); |
---|
142 | header('Cache-Control: public, max-age=86400'); |
---|
143 | header('Pragma: public'); |
---|
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 | } |
---|
149 | header('Content-Type: text/css; charset=' . $this->getParam('character_set')); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Include CSS files specified by setFile(). |
---|
154 | * |
---|
155 | * @access public |
---|
156 | * |
---|
157 | * @return bool False if no files have been set. |
---|
158 | */ |
---|
159 | public function output($realm='') |
---|
160 | { |
---|
161 | $app =& App::getInstance(); |
---|
162 | |
---|
163 | $realm = '' == $realm ? 'default' : $realm; |
---|
164 | |
---|
165 | if (empty($this->_css_files[$realm])) { |
---|
166 | $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
167 | return false; |
---|
168 | } |
---|
169 | |
---|
170 | foreach ($this->_css_files[$realm] as $file) { |
---|
171 | if ($this->getParam('strip_whitespace')) { |
---|
172 | // Strip whitespace and print file. |
---|
173 | echo preg_replace( |
---|
174 | array('!/\*.*?\*/!s', '/[\n\r]+/', '/([;:])\s+/m', '/\s*}[ \t]*/', '/\s*{\s*/', '/[ \t\n\r]*,[ \t\n\r]*/', '/^\s+/'), |
---|
175 | array('', "\n", '$1', '}', '{', ',', ''), file_get_contents($file, true) |
---|
176 | ); |
---|
177 | } else { |
---|
178 | // Include file as is. |
---|
179 | include $file; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
184 | ob_end_flush(); |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|