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