1 : <?php
2 : /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3 :
4 : /**
5 : * Base class for all HTML classes
6 : *
7 : * PHP versions 4 and 5
8 : *
9 : * LICENSE: This source file is subject to version 3.01 of the PHP license
10 : * that is available through the world-wide-web at the following URI:
11 : * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 : * the PHP License and are unable to obtain it through the web, please
13 : * send a note to license@php.net so we can mail you a copy immediately.
14 : *
15 : * @category HTML
16 : * @package HTML_Common
17 : * @author Adam Daniel <adaniel1@eesus.jnj.com>
18 : * @copyright 2001-2007 The PHP Group
19 : * @license http://www.php.net/license/3_01.txt PHP License 3.01
20 : * @version CVS: $Id: Common.php,v 1.14 2007/05/16 20:06:44 avb Exp $
21 : * @link http://pear.php.net/package/HTML_Common/
22 : */
23 :
24 : /**
25 : * Base class for all HTML classes
26 : *
27 : * @category HTML
28 : * @package HTML_Common
29 : * @author Adam Daniel <adaniel1@eesus.jnj.com>
30 : * @version Release: 1.2.4
31 : * @abstract
32 : */
33 : class HTML_Common
34 : {
35 : /**
36 : * Associative array of attributes
37 : * @var array
38 : * @access private
39 : */
40 : var $_attributes = array();
41 :
42 : /**
43 : * Tab offset of the tag
44 : * @var int
45 : * @access private
46 : */
47 : var $_tabOffset = 0;
48 :
49 : /**
50 : * Tab string
51 : * @var string
52 : * @since 1.7
53 : * @access private
54 : */
55 : var $_tab = "\11";
56 :
57 : /**
58 : * Contains the line end string
59 : * @var string
60 : * @since 1.7
61 : * @access private
62 : */
63 : var $_lineEnd = "\12";
64 :
65 : /**
66 : * HTML comment on the object
67 : * @var string
68 : * @since 1.5
69 : * @access private
70 : */
71 : var $_comment = '';
72 :
73 : /**
74 : * Class constructor
75 : * @param mixed $attributes Associative array of table tag attributes
76 : * or HTML attributes name="value" pairs
77 : * @param int $tabOffset Indent offset in tabs
78 : * @access public
79 : */
80 : function HTML_Common($attributes = null, $tabOffset = 0)
81 : {
82 0 : $this->setAttributes($attributes);
83 0 : $this->setTabOffset($tabOffset);
84 0 : } // end constructor
85 :
86 : /**
87 : * Returns the current API version
88 : * @access public
89 : * @returns double
90 : */
91 : function apiVersion()
92 : {
93 0 : return 1.7;
94 : } // end func apiVersion
95 :
96 : /**
97 : * Returns the lineEnd
98 : *
99 : * @since 1.7
100 : * @access private
101 : * @return string
102 : */
103 : function _getLineEnd()
104 : {
105 2 : return $this->_lineEnd;
106 : } // end func getLineEnd
107 :
108 : /**
109 : * Returns a string containing the unit for indenting HTML
110 : *
111 : * @since 1.7
112 : * @access private
113 : * @return string
114 : */
115 : function _getTab()
116 : {
117 2 : return $this->_tab;
118 : } // end func _getTab
119 :
120 : /**
121 : * Returns a string containing the offset for the whole HTML code
122 : *
123 : * @return string
124 : * @access private
125 : */
126 : function _getTabs()
127 : {
128 2 : return str_repeat($this->_getTab(), $this->_tabOffset);
129 : } // end func _getTabs
130 :
131 : /**
132 : * Returns an HTML formatted attribute string
133 : * @param array $attributes
134 : * @return string
135 : * @access private
136 : */
137 : function _getAttrString($attributes)
138 : {
139 0 : $strAttr = '';
140 :
141 0 : if (is_array($attributes)) {
142 0 : $charset = HTML_Common::charset();
143 0 : foreach ($attributes as $key => $value) {
144 0 : $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
145 0 : }
146 0 : }
147 0 : return $strAttr;
148 : } // end func _getAttrString
149 :
150 : /**
151 : * Returns a valid atrributes array from either a string or array
152 : * @param mixed $attributes Either a typical HTML attribute string or an associative array
153 : * @access private
154 : * @return array
155 : */
156 : function _parseAttributes($attributes)
157 : {
158 9 : if (is_array($attributes)) {
159 9 : $ret = array();
160 9 : foreach ($attributes as $key => $value) {
161 9 : if (is_int($key)) {
162 0 : $key = $value = strtolower($value);
163 0 : } else {
164 9 : $key = strtolower($key);
165 : }
166 9 : $ret[$key] = $value;
167 9 : }
168 9 : return $ret;
169 :
170 0 : } elseif (is_string($attributes)) {
171 : $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
172 0 : "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
173 0 : if (preg_match_all($preg, $attributes, $regs)) {
174 0 : for ($counter=0; $counter<count($regs[1]); $counter++) {
175 0 : $name = $regs[1][$counter];
176 0 : $check = $regs[0][$counter];
177 0 : $value = $regs[7][$counter];
178 0 : if (trim($name) == trim($check)) {
179 0 : $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
180 0 : } else {
181 0 : if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
182 0 : $value = substr($value, 1, -1);
183 0 : }
184 0 : $arrAttr[strtolower(trim($name))] = trim($value);
185 : }
186 0 : }
187 0 : return $arrAttr;
188 : }
189 0 : }
190 0 : } // end func _parseAttributes
191 :
192 : /**
193 : * Returns the array key for the given non-name-value pair attribute
194 : *
195 : * @param string $attr Attribute
196 : * @param array $attributes Array of attribute
197 : * @since 1.0
198 : * @access private
199 : * @return bool
200 : */
201 : function _getAttrKey($attr, $attributes)
202 : {
203 0 : if (isset($attributes[strtolower($attr)])) {
204 0 : return true;
205 : } else {
206 0 : return null;
207 : }
208 : } //end func _getAttrKey
209 :
210 : /**
211 : * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
212 : * @param array $attr1 Original attributes array
213 : * @param array $attr2 New attributes array
214 : * @access private
215 : */
216 : function _updateAttrArray(&$attr1, $attr2)
217 : {
218 0 : if (!is_array($attr2)) {
219 0 : return false;
220 : }
221 0 : foreach ($attr2 as $key => $value) {
222 0 : $attr1[$key] = $value;
223 0 : }
224 0 : } // end func _updateAtrrArray
225 :
226 : /**
227 : * Removes the given attribute from the given array
228 : *
229 : * @param string $attr Attribute name
230 : * @param array $attributes Attribute array
231 : * @since 1.4
232 : * @access private
233 : * @return void
234 : */
235 : function _removeAttr($attr, &$attributes)
236 : {
237 0 : $attr = strtolower($attr);
238 0 : if (isset($attributes[$attr])) {
239 0 : unset($attributes[$attr]);
240 0 : }
241 0 : } //end func _removeAttr
242 :
243 : /**
244 : * Returns the value of the given attribute
245 : *
246 : * @param string $attr Attribute name
247 : * @since 1.5
248 : * @access public
249 : * @return string|null returns null if an attribute does not exist
250 : */
251 : function getAttribute($attr)
252 : {
253 0 : $attr = strtolower($attr);
254 0 : if (isset($this->_attributes[$attr])) {
255 0 : return $this->_attributes[$attr];
256 : }
257 0 : return null;
258 : } //end func getAttribute
259 :
260 : /**
261 : * Sets the value of the attribute
262 : *
263 : * @param string Attribute name
264 : * @param string Attribute value (will be set to $name if omitted)
265 : * @access public
266 : */
267 : function setAttribute($name, $value = null)
268 : {
269 0 : $name = strtolower($name);
270 0 : if (is_null($value)) {
271 0 : $value = $name;
272 0 : }
273 0 : $this->_attributes[$name] = $value;
274 0 : } // end func setAttribute
275 :
276 : /**
277 : * Sets the HTML attributes
278 : * @param mixed $attributes Either a typical HTML attribute string or an associative array
279 : * @access public
280 : */
281 : function setAttributes($attributes)
282 : {
283 0 : $this->_attributes = $this->_parseAttributes($attributes);
284 0 : } // end func setAttributes
285 :
286 : /**
287 : * Returns the assoc array (default) or string of attributes
288 : *
289 : * @param bool Whether to return the attributes as string
290 : * @since 1.6
291 : * @access public
292 : * @return mixed attributes
293 : */
294 : function getAttributes($asString = false)
295 : {
296 0 : if ($asString) {
297 0 : return $this->_getAttrString($this->_attributes);
298 : } else {
299 0 : return $this->_attributes;
300 : }
301 : } //end func getAttributes
302 :
303 : /**
304 : * Updates the passed attributes without changing the other existing attributes
305 : * @param mixed $attributes Either a typical HTML attribute string or an associative array
306 : * @access public
307 : */
308 : function updateAttributes($attributes)
309 : {
310 0 : $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
311 0 : } // end func updateAttributes
312 :
313 : /**
314 : * Removes an attribute
315 : *
316 : * @param string $attr Attribute name
317 : * @since 1.4
318 : * @access public
319 : * @return void
320 : */
321 : function removeAttribute($attr)
322 : {
323 0 : $this->_removeAttr($attr, $this->_attributes);
324 0 : } //end func removeAttribute
325 :
326 : /**
327 : * Sets the line end style to Windows, Mac, Unix or a custom string.
328 : *
329 : * @param string $style "win", "mac", "unix" or custom string.
330 : * @since 1.7
331 : * @access public
332 : * @return void
333 : */
334 : function setLineEnd($style)
335 : {
336 : switch ($style) {
337 48 : case 'win':
338 0 : $this->_lineEnd = "\15\12";
339 0 : break;
340 48 : case 'unix':
341 0 : $this->_lineEnd = "\12";
342 0 : break;
343 48 : case 'mac':
344 0 : $this->_lineEnd = "\15";
345 0 : break;
346 48 : default:
347 48 : $this->_lineEnd = $style;
348 48 : }
349 48 : } // end func setLineEnd
350 :
351 : /**
352 : * Sets the tab offset
353 : *
354 : * @param int $offset
355 : * @access public
356 : */
357 : function setTabOffset($offset)
358 : {
359 0 : $this->_tabOffset = $offset;
360 0 : } // end func setTabOffset
361 :
362 : /**
363 : * Returns the tabOffset
364 : *
365 : * @since 1.5
366 : * @access public
367 : * @return int
368 : */
369 : function getTabOffset()
370 : {
371 0 : return $this->_tabOffset;
372 : } //end func getTabOffset
373 :
374 : /**
375 : * Sets the string used to indent HTML
376 : *
377 : * @since 1.7
378 : * @param string $string String used to indent ("\11", "\t", ' ', etc.).
379 : * @access public
380 : * @return void
381 : */
382 : function setTab($string)
383 : {
384 48 : $this->_tab = $string;
385 48 : } // end func setTab
386 :
387 : /**
388 : * Sets the HTML comment to be displayed at the beginning of the HTML string
389 : *
390 : * @param string
391 : * @since 1.4
392 : * @access public
393 : * @return void
394 : */
395 : function setComment($comment)
396 : {
397 0 : $this->_comment = $comment;
398 0 : } // end func setHtmlComment
399 :
400 : /**
401 : * Returns the HTML comment
402 : *
403 : * @since 1.5
404 : * @access public
405 : * @return string
406 : */
407 : function getComment()
408 : {
409 0 : return $this->_comment;
410 : } //end func getComment
411 :
412 : /**
413 : * Abstract method. Must be extended to return the objects HTML
414 : *
415 : * @access public
416 : * @return string
417 : * @abstract
418 : */
419 : function toHtml()
420 : {
421 0 : return '';
422 : } // end func toHtml
423 :
424 : /**
425 : * Displays the HTML to the screen
426 : *
427 : * @access public
428 : */
429 : function display()
430 : {
431 0 : print $this->toHtml();
432 0 : } // end func display
433 :
434 : /**
435 : * Sets the charset to use by htmlspecialchars() function
436 : *
437 : * Since this parameter is expected to be global, the function is designed
438 : * to be called statically:
439 : * <code>
440 : * HTML_Common::charset('utf-8');
441 : * </code>
442 : * or
443 : * <code>
444 : * $charset = HTML_Common::charset();
445 : * </code>
446 : *
447 : * @param string New charset to use. Omit if just getting the
448 : * current value. Consult the htmlspecialchars() docs
449 : * for a list of supported character sets.
450 : * @return string Current charset
451 : * @access public
452 : * @static
453 : */
454 : function charset($newCharset = null)
455 : {
456 0 : static $charset = 'ISO-8859-1';
457 :
458 0 : if (!is_null($newCharset)) {
459 0 : $charset = $newCharset;
460 0 : }
461 0 : return $charset;
462 : } // end func charset
463 : } // end class HTML_Common
464 : ?>
|