[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[393] | 6 | * |
---|
[362] | 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. |
---|
[393] | 13 | * |
---|
[362] | 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. |
---|
[393] | 18 | * |
---|
[362] | 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 | /** |
---|
[42] | 24 | * CSS.inc.css |
---|
[1] | 25 | * |
---|
| 26 | * Dynamically outputs cached CSS data. |
---|
| 27 | * |
---|
| 28 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[26] | 29 | * @version 1.2 |
---|
[1] | 30 | */ |
---|
[502] | 31 | class CSS |
---|
| 32 | { |
---|
[1] | 33 | |
---|
| 34 | // Include these style sheets. |
---|
[484] | 35 | protected $_css_files = array('default' => array()); |
---|
[42] | 36 | |
---|
[92] | 37 | // CSS object parameters. |
---|
[484] | 38 | protected $_params = array( |
---|
[1] | 39 | 'character_set' => 'utf-8', |
---|
[554] | 40 | 'cache_enable' => false, |
---|
| 41 | 'cache_css' => null, // Same as above; kept for backwards compatibility. |
---|
[553] | 42 | 'cache_seconds' => 86400, |
---|
[393] | 43 | 'strip_whitespace' => false, |
---|
| 44 | 'output_compression' => false, |
---|
[1] | 45 | ); |
---|
[42] | 46 | |
---|
[1] | 47 | /** |
---|
| 48 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 49 | * |
---|
| 50 | * @access public |
---|
| 51 | * @param array $params Array of parameters (key => val pairs). |
---|
| 52 | */ |
---|
[468] | 53 | public function setParam($params) |
---|
[1] | 54 | { |
---|
[479] | 55 | $app =& App::getInstance(); |
---|
[393] | 56 | |
---|
[1] | 57 | if (isset($params) && is_array($params)) { |
---|
| 58 | // Merge new parameters with old overriding only those passed. |
---|
| 59 | $this->_params = array_merge($this->_params, $params); |
---|
[554] | 60 | // Maintaining backwards compatibility. |
---|
| 61 | if (isset($params['cache_css']) && !isset($params['cache_enable'])) { |
---|
| 62 | $this->_params['cache_enable'] = $params['cache_css']; |
---|
| 63 | } |
---|
[1] | 64 | } else { |
---|
[136] | 65 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Return the value of a parameter, if it exists. |
---|
| 71 | * |
---|
| 72 | * @access public |
---|
| 73 | * @param string $param Which parameter to return. |
---|
| 74 | * @return mixed Configured parameter value. |
---|
| 75 | */ |
---|
[468] | 76 | public function getParam($param) |
---|
[1] | 77 | { |
---|
[479] | 78 | $app =& App::getInstance(); |
---|
[393] | 79 | |
---|
[478] | 80 | if (array_key_exists($param, $this->_params)) { |
---|
[1] | 81 | return $this->_params[$param]; |
---|
| 82 | } else { |
---|
[146] | 83 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 84 | return null; |
---|
| 85 | } |
---|
| 86 | } |
---|
[42] | 87 | |
---|
[1] | 88 | /** |
---|
| 89 | * Add a file-path to the array of files to include as CSS. |
---|
| 90 | * |
---|
| 91 | * @access public |
---|
[33] | 92 | * @param string $file Include path to css files. |
---|
[136] | 93 | * @param mixed $realms Realm name string or array of realm names. |
---|
[1] | 94 | * @return bool True on success, false on failure. |
---|
| 95 | */ |
---|
[468] | 96 | public function setFile($file, $realms='') |
---|
[1] | 97 | { |
---|
[479] | 98 | $app =& App::getInstance(); |
---|
[393] | 99 | |
---|
[136] | 100 | if (!is_array($realms)) { |
---|
| 101 | $realms = array($realms); |
---|
[33] | 102 | } |
---|
| 103 | |
---|
[26] | 104 | if ($fp = fopen($file, 'r', true)) { |
---|
[136] | 105 | foreach ($realms as $realm) { |
---|
| 106 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 107 | $this->_css_files[$realm][] = $file; |
---|
[33] | 108 | } |
---|
[26] | 109 | fclose($fp); |
---|
[1] | 110 | return true; |
---|
| 111 | } else { |
---|
[136] | 112 | $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 113 | return false; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Output headers for CSS. |
---|
| 119 | * |
---|
| 120 | * @access public |
---|
| 121 | * |
---|
| 122 | * @return bool False if no files have been set. |
---|
| 123 | */ |
---|
[468] | 124 | public function headers($realm='') |
---|
[1] | 125 | { |
---|
[479] | 126 | $app =& App::getInstance(); |
---|
[393] | 127 | |
---|
[136] | 128 | $realm = '' == $realm ? 'default' : $realm; |
---|
[33] | 129 | |
---|
[136] | 130 | if (empty($this->_css_files[$realm])) { |
---|
| 131 | $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 132 | return false; |
---|
| 133 | } |
---|
[26] | 134 | |
---|
[1] | 135 | // Get time of latest modified file, including this class file. |
---|
[26] | 136 | $files_mtime = array(); |
---|
[136] | 137 | foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) { |
---|
[26] | 138 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
| 139 | } |
---|
[1] | 140 | sort($files_mtime, SORT_NUMERIC); |
---|
[26] | 141 | $latest_mtime = array_pop($files_mtime); |
---|
[42] | 142 | |
---|
[393] | 143 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
| 144 | ob_start('ob_gzhandler'); |
---|
| 145 | } |
---|
| 146 | |
---|
[555] | 147 | header('Content-Type: text/css; charset=' . $this->getParam('character_set')); |
---|
| 148 | header(sprintf('Last-Modified: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime), null)); |
---|
[554] | 149 | if ($this->getParam('cache_enable')) { |
---|
[553] | 150 | header(sprintf('Cache-Control: public, max-age=%s', $this->getParam('cache_seconds'))); |
---|
[555] | 151 | header(sprintf('Expires: %s GMT', gmdate('D, d M Y H:i:s', $latest_mtime + $this->getParam('cache_seconds')))); |
---|
[395] | 152 | header('Pragma: public'); |
---|
[555] | 153 | header('Vary: Accept-Encoding'); |
---|
[1] | 154 | } else { |
---|
[555] | 155 | header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
---|
[1] | 156 | header('Expires: -1'); |
---|
| 157 | header('Pragma: no-cache'); |
---|
| 158 | } |
---|
| 159 | } |
---|
[42] | 160 | |
---|
[1] | 161 | /** |
---|
| 162 | * Include CSS files specified by setFile(). |
---|
| 163 | * |
---|
| 164 | * @access public |
---|
| 165 | * |
---|
| 166 | * @return bool False if no files have been set. |
---|
| 167 | */ |
---|
[468] | 168 | public function output($realm='') |
---|
[1] | 169 | { |
---|
[473] | 170 | $app =& App::getInstance(); |
---|
| 171 | |
---|
[136] | 172 | $realm = '' == $realm ? 'default' : $realm; |
---|
[33] | 173 | |
---|
[136] | 174 | if (empty($this->_css_files[$realm])) { |
---|
| 175 | $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 176 | return false; |
---|
| 177 | } |
---|
[42] | 178 | |
---|
[136] | 179 | foreach ($this->_css_files[$realm] as $file) { |
---|
[393] | 180 | if ($this->getParam('strip_whitespace')) { |
---|
| 181 | // Strip whitespace and print file. |
---|
[471] | 182 | echo preg_replace( |
---|
| 183 | array('!/\*.*?\*/!s', '/[\n\r]+/', '/([;:])\s+/m', '/\s*}[ \t]*/', '/\s*{\s*/', '/[ \t\n\r]*,[ \t\n\r]*/', '/^\s+/'), |
---|
| 184 | array('', "\n", '$1', '}', '{', ',', ''), file_get_contents($file, true) |
---|
| 185 | ); |
---|
[393] | 186 | } else { |
---|
| 187 | // Include file as is. |
---|
| 188 | include $file; |
---|
| 189 | } |
---|
[27] | 190 | } |
---|
[393] | 191 | |
---|
| 192 | if ($this->getParam('output_compression') && extension_loaded('zlib')) { |
---|
| 193 | ob_end_flush(); |
---|
| 194 | } |
---|
[1] | 195 | } |
---|
| 196 | } |
---|