[1] | 1 | <?php |
---|
| 2 | /** |
---|
[42] | 3 | * CSS.inc.css |
---|
[1] | 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> |
---|
[26] | 9 | * @version 1.2 |
---|
[1] | 10 | */ |
---|
| 11 | class CSS { |
---|
| 12 | |
---|
| 13 | // Include these style sheets. |
---|
[33] | 14 | var $_css_files = array('default' => array()); |
---|
[42] | 15 | |
---|
[92] | 16 | // CSS object parameters. |
---|
[1] | 17 | var $_params = array( |
---|
| 18 | 'cache_css' => true, |
---|
| 19 | 'character_set' => 'utf-8', |
---|
| 20 | ); |
---|
[42] | 21 | |
---|
[1] | 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 | { |
---|
[127] | 30 | $app =& App::getInstance(); |
---|
| 31 | |
---|
[1] | 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 { |
---|
[127] | 36 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 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 | { |
---|
[127] | 49 | $app =& App::getInstance(); |
---|
| 50 | |
---|
[1] | 51 | if (isset($this->_params[$param])) { |
---|
| 52 | return $this->_params[$param]; |
---|
| 53 | } else { |
---|
[127] | 54 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 55 | return null; |
---|
| 56 | } |
---|
| 57 | } |
---|
[42] | 58 | |
---|
[1] | 59 | /** |
---|
| 60 | * Add a file-path to the array of files to include as CSS. |
---|
| 61 | * |
---|
| 62 | * @access public |
---|
[33] | 63 | * @param string $file Include path to css files. |
---|
[127] | 64 | * @param mixed $realms Realm name string or array of realm names. |
---|
[1] | 65 | * @return bool True on success, false on failure. |
---|
| 66 | */ |
---|
[127] | 67 | function setFile($file, $realms='') |
---|
[1] | 68 | { |
---|
[127] | 69 | $app =& App::getInstance(); |
---|
| 70 | |
---|
| 71 | if (!is_array($realms)) { |
---|
| 72 | $realms = array($realms); |
---|
[33] | 73 | } |
---|
| 74 | |
---|
[26] | 75 | if ($fp = fopen($file, 'r', true)) { |
---|
[127] | 76 | foreach ($realms as $realm) { |
---|
| 77 | $realm = '' == $realm ? 'default' : $realm; |
---|
| 78 | $this->_css_files[$realm][] = $file; |
---|
[33] | 79 | } |
---|
[26] | 80 | fclose($fp); |
---|
[1] | 81 | return true; |
---|
| 82 | } else { |
---|
[127] | 83 | $app->logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 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 | */ |
---|
[127] | 95 | function headers($realm='') |
---|
[1] | 96 | { |
---|
[127] | 97 | $app =& App::getInstance(); |
---|
| 98 | |
---|
| 99 | $realm = '' == $realm ? 'default' : $realm; |
---|
[33] | 100 | |
---|
[127] | 101 | if (empty($this->_css_files[$realm])) { |
---|
| 102 | $app->logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 103 | return false; |
---|
| 104 | } |
---|
[26] | 105 | |
---|
[1] | 106 | // Get time of latest modified file, including this class file. |
---|
[26] | 107 | $files_mtime = array(); |
---|
[127] | 108 | foreach (array_merge($this->_css_files[$realm], array(__FILE__)) as $file) { |
---|
[26] | 109 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
| 110 | } |
---|
[1] | 111 | sort($files_mtime, SORT_NUMERIC); |
---|
[26] | 112 | $latest_mtime = array_pop($files_mtime); |
---|
[42] | 113 | |
---|
[1] | 114 | if ($this->_params['cache_css']) { |
---|
[26] | 115 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT'); |
---|
[1] | 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 | } |
---|
[42] | 124 | |
---|
[1] | 125 | /** |
---|
| 126 | * Include CSS files specified by setFile(). |
---|
| 127 | * |
---|
| 128 | * @access public |
---|
| 129 | * |
---|
| 130 | * @return bool False if no files have been set. |
---|
| 131 | */ |
---|
[127] | 132 | function output($realm='') |
---|
[1] | 133 | { |
---|
[127] | 134 | $realm = '' == $realm ? 'default' : $realm; |
---|
[33] | 135 | |
---|
[127] | 136 | if (empty($this->_css_files[$realm])) { |
---|
| 137 | $app->logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 138 | return false; |
---|
| 139 | } |
---|
[42] | 140 | |
---|
[127] | 141 | foreach ($this->_css_files[$realm] as $file) { |
---|
[27] | 142 | include $file; |
---|
| 143 | } |
---|
[1] | 144 | } |
---|
[42] | 145 | |
---|
[1] | 146 | } |
---|
| 147 | ?> |
---|