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.2 |
---|
10 | */ |
---|
11 | class CSS { |
---|
12 | |
---|
13 | // Include these style sheets. |
---|
14 | var $_css_files = array('default' => 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 | * @param string $file Include path to css files. |
---|
60 | * @param mixed $apps App name string or array of app names. |
---|
61 | * @return bool True on success, false on failure. |
---|
62 | */ |
---|
63 | function setFile($file, $apps='') |
---|
64 | { |
---|
65 | if (!is_array($apps)) { |
---|
66 | $apps = array($apps); |
---|
67 | } |
---|
68 | |
---|
69 | if ($fp = fopen($file, 'r', true)) { |
---|
70 | foreach ($apps as $app) { |
---|
71 | $app = '' == $app ? 'default' : $app; |
---|
72 | $this->_css_files[$app][] = $file; |
---|
73 | } |
---|
74 | fclose($fp); |
---|
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 | */ |
---|
89 | function headers($app='') |
---|
90 | { |
---|
91 | $app = '' == $app ? 'default' : $app; |
---|
92 | |
---|
93 | if (empty($this->_css_files[$app])) { |
---|
94 | App::logMsg(sprintf('CSS::headers called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
95 | return false; |
---|
96 | } |
---|
97 | |
---|
98 | // Get time of latest modified file, including this class file. |
---|
99 | $files_mtime = array(); |
---|
100 | foreach (array_merge($this->_css_files[$app], array(__FILE__)) as $file) { |
---|
101 | $files_mtime[] = statIncludePath($file, 'mtime'); |
---|
102 | } |
---|
103 | sort($files_mtime, SORT_NUMERIC); |
---|
104 | $latest_mtime = array_pop($files_mtime); |
---|
105 | |
---|
106 | if ($this->_params['cache_css']) { |
---|
107 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $latest_mtime) . ' GMT'); |
---|
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 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Include CSS files specified by setFile(). |
---|
119 | * |
---|
120 | * @access public |
---|
121 | * |
---|
122 | * @return bool False if no files have been set. |
---|
123 | */ |
---|
124 | function output($app='') |
---|
125 | { |
---|
126 | $app = '' == $app ? 'default' : $app; |
---|
127 | |
---|
128 | if (empty($this->_css_files[$app])) { |
---|
129 | App::logMsg(sprintf('CSS::output called without specifying any files.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
130 | return false; |
---|
131 | } |
---|
132 | |
---|
133 | foreach ($this->_css_files[$app] as $file) { |
---|
134 | include $file; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | } |
---|
139 | ?> |
---|