Project

General

Profile

1
<?php
2

    
3
// $Id: class.cssparser.php 2070 2014-01-03 01:21:42Z darkviper $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27
 * Class to parse css information.
28
 *
29
 * See the readme file : http://www.phpclasses.org/browse/file/4685.html
30
 *
31
 * $Id: class.cssparser.php 2070 2014-01-03 01:21:42Z darkviper $
32
 *
33
 * @author http://www.phpclasses.org/browse/package/1289.html
34
 * @package PhpGedView
35
 * @subpackage Charts
36
 *
37
 * added function GetXML to the cssparser class (Christian Sommer, 2007)
38
 *
39
 */
40
// Must include code to stop this file being access directly
41
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
42

    
43
class cssparser {
44
	var $css;
45
	var $html;
46
  
47
	function cssparser($html = true) {
48
		// Register "destructor"
49
		register_shutdown_function(array(&$this, "finalize"));
50
		$this->html = ($html != false);
51
		$this->Clear();
52
	}
53
  
54
	function finalize() {
55
		unset($this->css);
56
	}
57
  
58
	function Clear() {
59
		unset($this->css);
60
		$this->css = array();
61
		if($this->html) {
62
			$this->Add("ADDRESS", "");
63
			$this->Add("APPLET", "");
64
			$this->Add("AREA", "");
65
			$this->Add("A", "");
66
			$this->Add("A:visited", "");
67
			$this->Add("BASE", "");
68
			$this->Add("BASEFONT", "");
69
			$this->Add("BIG", "");
70
			$this->Add("BLOCKQUOTE", "");
71
			$this->Add("BODY", "");
72
			$this->Add("BR", "");
73
			$this->Add("B", "");
74
			$this->Add("CAPTION", "");
75
			$this->Add("CENTER", "");
76
			$this->Add("CITE", "");
77
			$this->Add("CODE", "");
78
			$this->Add("DD", "");
79
			$this->Add("DFN", "");
80
			$this->Add("DIR", "");
81
			$this->Add("DIV", "");
82
			$this->Add("DL", "");
83
			$this->Add("DT", "");
84
			$this->Add("EM", "");
85
			$this->Add("FONT", "");
86
			$this->Add("FORM", "");
87
			$this->Add("H1", "");
88
			$this->Add("H2", "");
89
			$this->Add("H3", "");
90
			$this->Add("H4", "");
91
			$this->Add("H5", "");
92
			$this->Add("H6", "");
93
			$this->Add("HEAD", "");
94
			$this->Add("HR", "");
95
			$this->Add("HTML", "");
96
			$this->Add("IMG", "");
97
			$this->Add("INPUT", "");
98
			$this->Add("ISINDEX", "");
99
			$this->Add("I", "");
100
			$this->Add("KBD", "");
101
			$this->Add("LINK", "");
102
			$this->Add("LI", "");
103
			$this->Add("MAP", "");
104
			$this->Add("MENU", "");
105
			$this->Add("META", "");
106
			$this->Add("OL", "");
107
			$this->Add("OPTION", "");
108
			$this->Add("PARAM", "");
109
			$this->Add("PRE", "");
110
			$this->Add("P", "");
111
			$this->Add("SAMP", "");
112
			$this->Add("SCRIPT", "");
113
			$this->Add("SELECT", "");
114
			$this->Add("SMALL", "");
115
			$this->Add("STRIKE", "");
116
			$this->Add("STRONG", "");
117
			$this->Add("STYLE", "");
118
			$this->Add("SUB", "");
119
			$this->Add("SUP", "");
120
			$this->Add("TABLE", "");
121
			$this->Add("TD", "");
122
			$this->Add("TEXTAREA", "");
123
			$this->Add("TH", "");
124
			$this->Add("TITLE", "");
125
			$this->Add("TR", "");
126
			$this->Add("TT", "");
127
			$this->Add("UL", "");
128
			$this->Add("U", "");
129
			$this->Add("VAR", "");
130
		}
131
	}
132
  
133
	function SetHTML($html) {
134
		$this->html = ($html != false);
135
	}
136
  
137
	function Add($key, $codestr) {
138
		$key = strtolower($key);
139
		//    $codestr = strtolower($codestr);
140
		if(!isset($this->css[$key])) {
141
			$this->css[$key] = array();
142
		}
143
		$codes = explode(";",$codestr);
144
		if(count($codes) > 0) {
145
			foreach($codes as $code) {
146
				$code = trim($code);
147
				@list($codekey, $codevalue) = explode(":",$code);
148
				if(strlen($codekey) > 0) {
149
					$this->css[$key][trim($codekey)] = trim($codevalue);
150
				}
151
			}
152
		}
153
	}
154
  
155
	function Get($key, $property) {
156
		$key = strtolower($key);
157
		//    $property = strtolower($property);
158
		@list($tag, $subtag) = explode(":",$key);
159
		@list($tag, $class) = explode(".",$tag);
160
		@list($tag, $id) = explode("#",$tag);
161
		$result = "";
162
		foreach($this->css as $_tag => $value) {
163
			@list($_tag, $_subtag) = explode(":",$_tag);
164
			@list($_tag, $_class) = explode(".",$_tag);
165
			@list($_tag, $_id) = explode("#",$_tag);
166
            $tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
167
			$subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
168
			$classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
169
			$idmatch = (strcmp($id, $_id) == 0);
170
            if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
171
				$temp = $_tag;
172
				if((strlen($temp) > 0) & (strlen($_class) > 0)) {
173
					$temp .= ".".$_class;
174
				}
175
				elseif(strlen($temp) == 0) {
176
					$temp = ".".$_class;
177
				}
178
				if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
179
					$temp .= ":".$_subtag;
180
				}
181
				elseif(strlen($temp) == 0) {
182
					$temp = ":".$_subtag;
183
				}
184
				if(isset($this->css[$temp][$property])) {
185
					$result = $this->css[$temp][$property];
186
				}
187
			}
188
		}
189
		return $result;
190
	}
191
  
192
	function GetSection($key) {
193
		$key = strtolower($key);
194
        @list($tag, $subtag) = explode(":",$key);
195
		@list($tag, $class) = explode(".",$tag);
196
		@list($tag, $id) = explode("#",$tag);
197
		$result = array();
198
		foreach($this->css as $_tag => $value) {
199
			@list($_tag, $_subtag) = explode(":",$_tag);
200
			@list($_tag, $_class) = explode(".",$_tag);
201
			@list($_tag, $_id) = explode("#",$_tag);
202
			$tagmatch = (strcmp($tag, $_tag) == 0) | (strlen($_tag) == 0);
203
			$subtagmatch = (strcmp($subtag, $_subtag) == 0) | (strlen($_subtag) == 0);
204
			$classmatch = (strcmp($class, $_class) == 0) | (strlen($_class) == 0);
205
			$idmatch = (strcmp($id, $_id) == 0);
206
			if($tagmatch & $subtagmatch & $classmatch & $idmatch) {
207
				$temp = $_tag;
208
				if((strlen($temp) > 0) & (strlen($_class) > 0)) {
209
					$temp .= ".".$_class;
210
				}
211
				elseif(strlen($temp) == 0) {
212
					$temp = ".".$_class;
213
				}
214
				if((strlen($temp) > 0) & (strlen($_subtag) > 0)) {
215
					$temp .= ":".$_subtag;
216
				}
217
				elseif(strlen($temp) == 0) {
218
					$temp = ":".$_subtag;
219
				}	
220
				foreach($this->css[$temp] as $property => $value) {
221
					$result[$property] = $value;
222
				}
223
			}
224
		}
225
		return $result;
226
	}
227
  
228
	function ParseStr($str) {
229
		$this->Clear();
230
		// Remove comments
231
		$str = preg_replace("/\/\*(.*)?\*\//Usi", "", $str);
232
		// Parse this damn csscode
233
		$parts = explode("}",$str);
234
		if(count($parts) > 0) {
235
			foreach($parts as $part) {
236
				@list($keystr,$codestr) = explode("{",$part);
237
				$keys = explode(",",trim($keystr));
238
				if(count($keys) > 0) {
239
					foreach($keys as $key) {
240
						if(strlen($key) > 0) {
241
							$key = str_replace("\n", "", $key);
242
							$key = str_replace("\\", "", $key);
243
							$this->Add($key, trim($codestr));
244
						}
245
					}
246
				}
247
			}
248
		}
249
		//
250
		return (count($this->css) > 0);
251
	}
252
  
253
	function Parse($filename) {
254
		$this->Clear();
255
		if(file_exists($filename)) {
256
			return $this->ParseStr(file_get_contents($filename));
257
		}
258
		else {
259
			return false;
260
		}
261
	}
262
	
263
	function GetCSS() {
264
		$result = "";
265
		foreach($this->css as $key => $values) {
266
			$result .= $key." {\n";
267
			foreach($values as $key => $value) {
268
				$result .= "  $key: $value;\n";
269
			}
270
			$result .= "}\n\n";
271
		}
272
		return $result;
273
	}
274

    
275
	function GetXML() {
276
		// Construction of "fckstyles.xml" for FCKeditor
277
		$styles = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"."\n";
278
		$styles .= '<Styles>'."\n";
279
		foreach ($this->css as $key => $value) {
280
			// skip all CSS that are a combination of CSS
281
			// skip CSS that are binded on an EVENT
282
			if (strpos($key, " ") === false && strpos($key, ":") === false) {
283
				$pieces = explode(".", $key, 2);
284
				if (strcmp($pieces[0], "") != 0) {
285
					continue;
286
				} else {
287
					$style_elem = "span";
288
				}
289
				if (strcmp($pieces[1], "") != 0) {
290
					$style_class_name = $pieces[1];
291
				} else {
292
					$style_class_name = $pieces[0];
293
				}
294
				$styles .= '<Style name="'.$style_class_name.'" element="'.$style_elem.'"';
295
				if (strcmp($style_class_name, $style_elem) != 0) {
296
					$styles .= '>'."\n".'<Attribute name="class" value="'.$style_class_name.'" />'."\n".'</Style>'."\n";
297
				} else {
298
					$styles .= '/>'."\n";
299
				}
300
			}
301
		}
302
		$styles .= '</Styles>'."\n";
303
		return trim($styles);
304
	}
305
}
306
?>
(1-1/8)