[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * CSS.inc.css |
---|
| 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> |
---|
| 9 | * @version 1.1 |
---|
| 10 | */ |
---|
| 11 | class CSS { |
---|
| 12 | |
---|
| 13 | // Include these style sheets. |
---|
| 14 | var $_css_files = array(); |
---|
| 15 | |
---|
| 16 | // Cache style sheets? |
---|
| 17 | var $_params = array( |
---|
| 18 | 'cache_css' => true, |
---|
| 19 | 'character_set' => 'utf-8', |
---|
| 20 | ); |
---|
| 21 | |
---|
| 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 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Add a file-path to the array of files to include as CSS. |
---|
| 57 | * |
---|
| 58 | * @access public |
---|
| 59 | * |
---|
| 60 | * @param string Full path to css file. |
---|
| 61 | * |
---|
| 62 | * @return bool True on success, false on failure. |
---|
| 63 | */ |
---|
| 64 | function setFile($file) |
---|
| 65 | { |
---|
| 66 | if (file_exists($file)) { |
---|
| 67 | $this->_css_files[] = $file; |
---|
| 68 | return true; |
---|
| 69 | } else { |
---|
| 70 | App::logMsg(sprintf('CSS file non-existent: %s', $file), LOG_ERR, __FILE__, __LINE__); |
---|
| 71 | return false; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Output headers for CSS. |
---|
| 77 | * |
---|
| 78 | * @access public |
---|
| 79 | * |
---|
| 80 | * @return bool False if no files have been set. |
---|
| 81 | */ |
---|
| 82 | function headers() |
---|
| 83 | { |
---|
| 84 | if (empty($this->_css_files)) { |
---|
| 85 | App::logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
| 86 | return false; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | // Get time of latest modified file, including this class file. |
---|
| 90 | $files_mtime = array_map('filemtime', array_merge($this->_css_files, array(__FILE__))); |
---|
| 91 | sort($files_mtime, SORT_NUMERIC); |
---|
| 92 | $mtime = array_pop($files_mtime); |
---|
| 93 | |
---|
| 94 | if ($this->_params['cache_css']) { |
---|
| 95 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT'); |
---|
| 96 | header('Cache-Control: public, max-age=86400'); |
---|
| 97 | } else { |
---|
| 98 | header('Expires: -1'); |
---|
| 99 | header('Pragma: no-cache'); |
---|
| 100 | header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); |
---|
| 101 | } |
---|
| 102 | header('Content-Type: text/css; charset=' . $this->_params['character_set']); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Include CSS files specified by setFile(). |
---|
| 107 | * |
---|
| 108 | * @access public |
---|
| 109 | * |
---|
| 110 | * @return bool False if no files have been set. |
---|
| 111 | */ |
---|
| 112 | function output() |
---|
| 113 | { |
---|
| 114 | if (empty($this->_css_files)) { |
---|
| 115 | App::logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
| 116 | return false; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | foreach ($this->_css_files as $file) { |
---|
| 120 | include $file; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | ?> |
---|