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-2009 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.15 2009/04/03 15:26:22 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.5
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 15 : if (is_array($attributes)) {
159 15 : $ret = array();
160 15 : foreach ($attributes as $key => $value) {
161 15 : if (is_int($key)) {
162 0 : $key = $value = strtolower($value);
163 0 : } else {
164 15 : $key = strtolower($key);
165 : }
166 15 : $ret[$key] = $value;
167 15 : }
168 15 : 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 : $arrAttr[strtolower(trim($name))] = substr($value, 1, -1);
183 0 : } else {
184 0 : $arrAttr[strtolower(trim($name))] = trim($value);
185 : }
186 : }
187 0 : }
188 0 : return $arrAttr;
189 : }
190 0 : }
191 0 : } // end func _parseAttributes
192 :
193 : /**
194 : * Returns the array key for the given non-name-value pair attribute
195 : *
196 : * @param string $attr Attribute
197 : * @param array $attributes Array of attribute
198 : * @since 1.0
199 : * @access private
200 : * @return bool
201 : */
202 : function _getAttrKey($attr, $attributes)
203 : {
204 0 : if (isset($attributes[strtolower($attr)])) {
205 0 : return true;
206 : } else {
207 0 : return null;
208 : }
209 : } //end func _getAttrKey
210 :
211 : /**
212 : * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
213 : * @param array $attr1 Original attributes array
214 : * @param array $attr2 New attributes array
215 : * @access private
216 : */
217 : function _updateAttrArray(&$attr1, $attr2)
218 : {
219 0 : if (!is_array($attr2)) {
220 0 : return false;
221 : }
222 0 : foreach ($attr2 as $key => $value) {
223 0 : $attr1[$key] = $value;
224 0 : }
225 0 : } // end func _updateAtrrArray
226 :
227 : /**
228 : * Removes the given attribute from the given array
229 : *
230 : * @param string $attr Attribute name
231 : * @param array $attributes Attribute array
232 : * @since 1.4
233 : * @access private
234 : * @return void
235 : */
236 : function _removeAttr($attr, &$attributes)
237 : {
238 0 : $attr = strtolower($attr);
239 0 : if (isset($attributes[$attr])) {
240 0 : unset($attributes[$attr]);
241 0 : }
242 0 : } //end func _removeAttr
243 :
244 : /**
245 : * Returns the value of the given attribute
246 : *
247 : * @param string $attr Attribute name
248 : * @since 1.5
249 : * @access public
250 : * @return string|null returns null if an attribute does not exist
251 : */
252 : function getAttribute($attr)
253 : {
254 0 : $attr = strtolower($attr);
255 0 : if (isset($this->_attributes[$attr])) {
256 0 : return $this->_attributes[$attr];
257 : }
258 0 : return null;
259 : } //end func getAttribute
260 :
261 : /**
262 : * Sets the value of the attribute
263 : *
264 : * @param string Attribute name
265 : * @param string Attribute value (will be set to $name if omitted)
266 : * @access public
267 : */
268 : function setAttribute($name, $value = null)
269 : {
270 0 : $name = strtolower($name);
271 0 : if (is_null($value)) {
272 0 : $value = $name;
273 0 : }
274 0 : $this->_attributes[$name] = $value;
275 0 : } // end func setAttribute
276 :
277 : /**
278 : * Sets the HTML attributes
279 : * @param mixed $attributes Either a typical HTML attribute string or an associative array
280 : * @access public
281 : */
282 : function setAttributes($attributes)
283 : {
284 0 : $this->_attributes = $this->_parseAttributes($attributes);
285 0 : } // end func setAttributes
286 :
287 : /**
288 : * Returns the assoc array (default) or string of attributes
289 : *
290 : * @param bool Whether to return the attributes as string
291 : * @since 1.6
292 : * @access public
293 : * @return mixed attributes
294 : */
295 : function getAttributes($asString = false)
296 : {
297 0 : if ($asString) {
298 0 : return $this->_getAttrString($this->_attributes);
299 : } else {
300 0 : return $this->_attributes;
301 : }
302 : } //end func getAttributes
303 :
304 : /**
305 : * Updates the passed attributes without changing the other existing attributes
306 : * @param mixed $attributes Either a typical HTML attribute string or an associative array
307 : * @access public
308 : */
309 : function updateAttributes($attributes)
310 : {
311 0 : $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
312 0 : } // end func updateAttributes
313 :
314 : /**
315 : * Removes an attribute
316 : *
317 : * @param string $attr Attribute name
318 : * @since 1.4
319 : * @access public
320 : * @return void
321 : */
322 : function removeAttribute($attr)
323 : {
324 0 : $this->_removeAttr($attr, $this->_attributes);
325 0 : } //end func removeAttribute
326 :
327 : /**
328 : * Sets the line end style to Windows, Mac, Unix or a custom string.
329 : *
330 : * @param string $style "win", "mac", "unix" or custom string.
331 : * @since 1.7
332 : * @access public
333 : * @return void
334 : */
335 : function setLineEnd($style)
336 : {
337 : switch ($style) {
338 56 : case 'win':
339 0 : $this->_lineEnd = "\15\12";
340 0 : break;
341 56 : case 'unix':
342 0 : $this->_lineEnd = "\12";
343 0 : break;
344 56 : case 'mac':
345 0 : $this->_lineEnd = "\15";
346 0 : break;
347 56 : default:
348 56 : $this->_lineEnd = $style;
349 56 : }
350 56 : } // end func setLineEnd
351 :
352 : /**
353 : * Sets the tab offset
354 : *
355 : * @param int $offset
356 : * @access public
357 : */
358 : function setTabOffset($offset)
359 : {
360 0 : $this->_tabOffset = $offset;
361 0 : } // end func setTabOffset
362 :
363 : /**
364 : * Returns the tabOffset
365 : *
366 : * @since 1.5
367 : * @access public
368 : * @return int
369 : */
370 : function getTabOffset()
371 : {
372 0 : return $this->_tabOffset;
373 : } //end func getTabOffset
374 :
375 : /**
376 : * Sets the string used to indent HTML
377 : *
378 : * @since 1.7
379 : * @param string $string String used to indent ("\11", "\t", ' ', etc.).
380 : * @access public
381 : * @return void
382 : */
383 : function setTab($string)
384 : {
385 56 : $this->_tab = $string;
386 56 : } // end func setTab
387 :
388 : /**
389 : * Sets the HTML comment to be displayed at the beginning of the HTML string
390 : *
391 : * @param string
392 : * @since 1.4
393 : * @access public
394 : * @return void
395 : */
396 : function setComment($comment)
397 : {
398 0 : $this->_comment = $comment;
399 0 : } // end func setHtmlComment
400 :
401 : /**
402 : * Returns the HTML comment
403 : *
404 : * @since 1.5
405 : * @access public
406 : * @return string
407 : */
408 : function getComment()
409 : {
410 0 : return $this->_comment;
411 : } //end func getComment
412 :
413 : /**
414 : * Abstract method. Must be extended to return the objects HTML
415 : *
416 : * @access public
417 : * @return string
418 : * @abstract
419 : */
420 : function toHtml()
421 : {
422 0 : return '';
423 : } // end func toHtml
424 :
425 : /**
426 : * Displays the HTML to the screen
427 : *
428 : * @access public
429 : */
430 : function display()
431 : {
432 0 : print $this->toHtml();
433 0 : } // end func display
434 :
435 : /**
436 : * Sets the charset to use by htmlspecialchars() function
437 : *
438 : * Since this parameter is expected to be global, the function is designed
439 : * to be called statically:
440 : * <code>
441 : * HTML_Common::charset('utf-8');
442 : * </code>
443 : * or
444 : * <code>
445 : * $charset = HTML_Common::charset();
446 : * </code>
447 : *
448 : * @param string New charset to use. Omit if just getting the
449 : * current value. Consult the htmlspecialchars() docs
450 : * for a list of supported character sets.
451 : * @return string Current charset
452 : * @access public
453 : * @static
454 : */
455 : function charset($newCharset = null)
456 : {
457 0 : static $charset = 'ISO-8859-1';
458 :
459 0 : if (!is_null($newCharset)) {
460 0 : $charset = $newCharset;
461 0 : }
462 0 : return $charset;
463 : } // end func charset
464 : } // end class HTML_Common
465 : ?>
|