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