Project

General

Profile

1 528 doc
<?php
2
3
/*
4
5
 Website Baker Project <http://www.websitebaker.org/>
6
 Copyright (C) 2004-2008, Ryan Djurovich
7
8
 Website Baker is free software; you can redistribute it and/or modify
9
 it under the terms of the GNU General Public License as published by
10
 the Free Software Foundation; either version 2 of the License, or
11
 (at your option) any later version.
12
13
 Website Baker is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 GNU General Public License for more details.
17
18
 You should have received a copy of the GNU General Public License
19
 along with Website Baker; if not, write to the Free Software
20
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22
*/
23
24
/*
25
 * Class to parse css information.
26
 *
27
 * See the readme file : http://www.phpclasses.org/browse/file/4685.html
28
 *
29
 * $Id: cssparser.inc.php,v 1.2 2005/03/30 23:42:15 yalnifj dead $
30
 *
31
 * @author http://www.phpclasses.org/browse/package/1289.html
32
 * @package PhpGedView
33
 * @subpackage Charts
34
 *
35
 * added function GetXML to the cssparser class (Christian Sommer, 2007)
36
 *
37
 */
38
39
class cssparser {
40
	var $css;
41
	var $html;
42
43
	function cssparser($html = true) {
44
		// Register "destructor"
45
		register_shutdown_function(array(&$this, "finalize"));
46
		$this->html = ($html != false);
47
		$this->Clear();
48
	}
49
50
	function finalize() {
51
		unset($this->css);
52
	}
53
54
	function Clear() {
55
		unset($this->css);
56
		$this->css = array();
57
		if($this->html) {
58
			$this->Add("ADDRESS", "");
59
			$this->Add("APPLET", "");
60
			$this->Add("AREA", "");
61
			$this->Add("A", "");
62
			$this->Add("A:visited", "");
63
			$this->Add("BASE", "");
64
			$this->Add("BASEFONT", "");
65
			$this->Add("BIG", "");
66
			$this->Add("BLOCKQUOTE", "");
67
			$this->Add("BODY", "");
68
			$this->Add("BR", "");
69
			$this->Add("B", "");
70
			$this->Add("CAPTION", "");
71
			$this->Add("CENTER", "");
72
			$this->Add("CITE", "");
73
			$this->Add("CODE", "");
74
			$this->Add("DD", "");
75
			$this->Add("DFN", "");
76
			$this->Add("DIR", "");
77
			$this->Add("DIV", "");
78
			$this->Add("DL", "");
79
			$this->Add("DT", "");
80
			$this->Add("EM", "");
81
			$this->Add("FONT", "");
82
			$this->Add("FORM", "");
83
			$this->Add("H1", "");
84
			$this->Add("H2", "");
85
			$this->Add("H3", "");
86
			$this->Add("H4", "");
87
			$this->Add("H5", "");
88
			$this->Add("H6", "");
89
			$this->Add("HEAD", "");
90
			$this->Add("HR", "");
91
			$this->Add("HTML", "");
92
			$this->Add("IMG", "");
93
			$this->Add("INPUT", "");
94
			$this->Add("ISINDEX", "");
95
			$this->Add("I", "");
96
			$this->Add("KBD", "");
97
			$this->Add("LINK", "");
98
			$this->Add("LI", "");
99
			$this->Add("MAP", "");
100
			$this->Add("MENU", "");
101
			$this->Add("META", "");
102
			$this->Add("OL", "");
103
			$this->Add("OPTION", "");
104
			$this->Add("PARAM", "");
105
			$this->Add("PRE", "");
106
			$this->Add("P", "");
107
			$this->Add("SAMP", "");
108
			$this->Add("SCRIPT", "");
109
			$this->Add("SELECT", "");
110
			$this->Add("SMALL", "");
111
			$this->Add("STRIKE", "");
112
			$this->Add("STRONG", "");
113
			$this->Add("STYLE", "");
114
			$this->Add("SUB", "");
115
			$this->Add("SUP", "");
116
			$this->Add("TABLE", "");
117
			$this->Add("TD", "");
118
			$this->Add("TEXTAREA", "");
119
			$this->Add("TH", "");
120
			$this->Add("TITLE", "");
121
			$this->Add("TR", "");
122
			$this->Add("TT", "");
123
			$this->Add("UL", "");
124
			$this->Add("U", "");
125
			$this->Add("VAR", "");
126
		}
127
	}
128
129
	function SetHTML($html) {
130
		$this->html = ($html != false);
131
	}
132
133
	function Add($key, $codestr) {
134
		$key = strtolower($key);
135
		//    $codestr = strtolower($codestr);
136
		if(!isset($this->css[$key])) {
137
			$this->css[$key] = array();
138
		}
139
		$codes = explode(";",$codestr);
140
		if(count($codes) > 0) {
141
			foreach($codes as $code) {
142
				$code = trim($code);
143
				@list($codekey, $codevalue) = explode(":",$code);
144
				if(strlen($codekey) > 0) {
145
					$this->css[$key][trim($codekey)] = trim($codevalue);
146
				}
147
			}
148
		}
149
	}
150
151
	function Get($key, $property) {
152
		$key = strtolower($key);
153
		//    $property = strtolower($property);
154
		@list($tag, $subtag) = explode(":",$key);
155
		@list($tag, $class) = explode(".",$tag);
156
		@list($tag, $id) = explode("#",$tag);
157
		$result = "";
158
		foreach($this->css as $_tag => $value) {
159
			@list($_tag, $_subtag) = explode(":",$_tag);
160
			@list($_tag, $_class) = explode(".",$_tag);
161
			@list($_tag, $_id) = explode("#",$_tag);
162
            $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
163
			$subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
164
			$classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
165
			$idmatch = (strcmp($id, $_id) == 0);
166
            if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
167
				$temp = $_tag;
168
				if((strlen($temp) > 0) & (strlen($_class) > 0)) {
169
					$temp .= ".".$_class;
170
				}
171
				elseif(strlen($temp) == 0) {
172
					$temp = ".".$_class;
173
				}
174
				if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
175
					$temp .= ":".$_subtag;
176
				}
177
				elseif(strlen($temp) == 0) {
178
					$temp = ":".$_subtag;
179
				}
180
				if(isset($this->css[$temp][$property])) {
181
					$result = $this->css[$temp][$property];
182
				}
183
			}
184
		}
185
		return $result;
186
	}
187
188
	function GetSection($key) {
189
		$key = strtolower($key);
190
        @list($tag, $subtag) = explode(":",$key);
191
		@list($tag, $class) = explode(".",$tag);
192
		@list($tag, $id) = explode("#",$tag);
193
		$result = array();
194
		foreach($this->css as $_tag => $value) {
195
			@list($_tag, $_subtag) = explode(":",$_tag);
196
			@list($_tag, $_class) = explode(".",$_tag);
197
			@list($_tag, $_id) = explode("#",$_tag);
198
			$tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
199
			$subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
200
			$classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
201
			$idmatch = (strcmp($id, $_id) == 0);
202
			if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
203
				$temp = $_tag;
204
				if((strlen($temp) > 0) & (strlen($_class) > 0)) {
205
					$temp .= ".".$_class;
206
				}
207
				elseif(strlen($temp) == 0) {
208
					$temp = ".".$_class;
209
				}
210
				if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
211
					$temp .= ":".$_subtag;
212
				}
213
				elseif(strlen($temp) == 0) {
214
					$temp = ":".$_subtag;
215
				}
216
				foreach($this->css[$temp] as $property => $value) {
217
					$result[$property] = $value;
218
				}
219
			}
220
		}
221
		return $result;
222
	}
223
224
	function ParseStr($str) {
225
		$this->Clear();
226
		// Remove comments
227
		$str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str);
228
		// Parse this damn csscode
229
		$parts = explode("}",$str);
230
		if(count($parts) > 0) {
231
			foreach($parts as $part) {
232
				@list($keystr,$codestr) = explode("{",$part);
233
				$keys = explode(",",trim($keystr));
234
				if(count($keys) > 0) {
235
					foreach($keys as $key) {
236
						if(strlen($key) > 0) {
237
							$key = str_replace("\n", "", $key);
238
							$key = str_replace("\\", "", $key);
239
							$this->Add($key, trim($codestr));
240
						}
241
					}
242
				}
243
			}
244
		}
245
		//
246
		return (count($this->css) > 0);
247
	}
248
249
	function Parse($filename) {
250
		$this->Clear();
251
		if(file_exists($filename)) {
252
			return $this->ParseStr(file_get_contents($filename));
253
		}
254
		else {
255
			return false;
256
		}
257
	}
258
259
	function GetCSS() {
260
		$result = "";
261
		foreach($this->css as $key => $values) {
262
			$result .= $key." {\n";
263
			foreach($values as $key => $value) {
264
				$result .= "  $key: $value;\n";
265
			}
266
			$result .= "}\n\n";
267
		}
268
		return $result;
269
	}
270
271
	function GetXML() {
272
		// Construction of "fckstyles.xml" for FCKeditor
273
		$styles = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"."\n";
274
		$styles .= '<Styles>'."\n";
275
		foreach ($this->css as $key => $value) {
276
			// skip all CSS that are a combination of CSS
277
			// skip CSS that are binded on an EVENT
278
			if (strpos($key, " ") === false && strpos($key, ":") === false) {
279
				$pieces = explode(".", $key, 2);
280
				if (strcmp($pieces[0], "") != 0) {
281
					continue;
282
				} else {
283
					$style_elem = "span";
284
				}
285
				if (strcmp($pieces[1], "") != 0) {
286
					$style_class_name = $pieces[1];
287
				} else {
288
					$style_class_name = $pieces[0];
289
				}
290
				$styles .= '<Style name="'.$style_class_name.'" element="'.$style_elem.'"';
291
				if (strcmp($style_class_name, $style_elem) != 0) {
292
					$styles .= '>'."\n".'<Attribute name="class" value="'.$style_class_name.'" />'."\n".'</Style>'."\n";
293
				} else {
294
					$styles .= '/>'."\n";
295
				}
296
			}
297
		}
298
		$styles .= '</Styles>'."\n";
299
		return trim($styles);
300
	}
301
}
302
?>