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