4 +-----------------------------------------------------------------------+
5 | program/include/html.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2010, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Helper class to create valid XHTML code |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: html.php 4867 2011-06-18 11:28:43Z alec $
24 * Class for HTML code creation
31 protected $attrib = array();
32 protected $allowed = array();
35 public static $lc_tags = true;
36 public static $common_attrib = array('id','class','style','title','align');
37 public static $containers = array('iframe','div','span','p','h1','h2','h3','form','textarea','table','thead','tbody','tr','th','td','style','script');
42 * @param array $attrib Hash array with tag attributes
44 public function __construct($attrib = array())
46 if (is_array($attrib)) {
47 $this->attrib = $attrib;
54 * @return string The finally composed HTML tag
56 public function show()
58 return self::tag($this->tagname, $this->attrib, $this->content, array_merge(self::$common_attrib, $this->allowed));
61 /****** STATIC METHODS *******/
64 * Generic method to create a HTML tag
66 * @param string $tagname Tag name
67 * @param array $attrib Tag attributes as key/value pairs
68 * @param string $content Optinal Tag content (creates a container tag)
69 * @param array $allowed_attrib List with allowed attributes, omit to allow all
70 * @return string The XHTML tag
72 public static function tag($tagname, $attrib = array(), $content = null, $allowed_attrib = null)
74 if (is_string($attrib))
75 $attrib = array('class' => $attrib);
77 $inline_tags = array('a','span','img');
78 $suffix = $attrib['nl'] || ($content && $attrib['nl'] !== false && !in_array($tagname, $inline_tags)) ? "\n" : '';
80 $tagname = self::$lc_tags ? strtolower($tagname) : $tagname;
81 if (isset($content) || in_array($tagname, self::$containers)) {
82 $templ = $attrib['noclose'] ? "<%s%s>%s" : "<%s%s>%s</%s>%s";
83 unset($attrib['noclose']);
84 return sprintf($templ, $tagname, self::attrib_string($attrib, $allowed_attrib), $content, $tagname, $suffix);
87 return sprintf("<%s%s />%s", $tagname, self::attrib_string($attrib, $allowed_attrib), $suffix);
92 * Derrived method for <div> containers
94 * @param mixed $attr Hash array with tag attributes or string with class name
95 * @param string $cont Div content
96 * @return string HTML code
99 public static function div($attr = null, $cont = null)
101 if (is_string($attr)) {
102 $attr = array('class' => $attr);
104 return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, array('onclick')));
108 * Derrived method for <p> blocks
110 * @param mixed $attr Hash array with tag attributes or string with class name
111 * @param string $cont Paragraph content
112 * @return string HTML code
115 public static function p($attr = null, $cont = null)
117 if (is_string($attr)) {
118 $attr = array('class' => $attr);
120 return self::tag('p', $attr, $cont, self::$common_attrib);
124 * Derrived method to create <img />
126 * @param mixed $attr Hash array with tag attributes or string with image source (src)
127 * @return string HTML code
130 public static function img($attr = null)
132 if (is_string($attr)) {
133 $attr = array('src' => $attr);
135 return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib,
136 array('src','alt','width','height','border','usemap')));
140 * Derrived method for link tags
142 * @param mixed $attr Hash array with tag attributes or string with link location (href)
143 * @param string $cont Link content
144 * @return string HTML code
147 public static function a($attr, $cont)
149 if (is_string($attr)) {
150 $attr = array('href' => $attr);
152 return self::tag('a', $attr, $cont, array_merge(self::$common_attrib,
153 array('href','target','name','rel','onclick','onmouseover','onmouseout','onmousedown','onmouseup')));
157 * Derrived method for inline span tags
159 * @param mixed $attr Hash array with tag attributes or string with class name
160 * @param string $cont Tag content
161 * @return string HTML code
164 public static function span($attr, $cont)
166 if (is_string($attr)) {
167 $attr = array('class' => $attr);
169 return self::tag('span', $attr, $cont, self::$common_attrib);
173 * Derrived method for form element labels
175 * @param mixed $attr Hash array with tag attributes or string with 'for' attrib
176 * @param string $cont Tag content
177 * @return string HTML code
180 public static function label($attr, $cont)
182 if (is_string($attr)) {
183 $attr = array('for' => $attr);
185 return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, array('for')));
189 * Derrived method to create <iframe></iframe>
191 * @param mixed $attr Hash array with tag attributes or string with frame source (src)
192 * @return string HTML code
195 public static function iframe($attr = null, $cont = null)
197 if (is_string($attr)) {
198 $attr = array('src' => $attr);
200 return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib,
201 array('src','name','width','height','border','frameborder')));
205 * Derrived method for line breaks
207 * @return string HTML code
210 public static function br()
212 return self::tag('br');
216 * Create string with attributes
218 * @param array $attrib Associative arry with tag attributes
219 * @param array $allowed List of allowed attributes
220 * @return string Valid attribute string
222 public static function attrib_string($attrib = array(), $allowed = null)
224 if (empty($attrib)) {
228 $allowed_f = array_flip((array)$allowed);
229 $attrib_arr = array();
230 foreach ($attrib as $key => $value) {
231 // skip size if not numeric
232 if (($key=='size' && !is_numeric($value))) {
236 // ignore "internal" or not allowed attributes
237 if ($key == 'nl' || ($allowed && !isset($allowed_f[$key])) || $value === null) {
241 // skip empty eventhandlers
242 if (preg_match('/^on[a-z]+/', $key) && !$value) {
246 // attributes with no value
247 if (in_array($key, array('checked', 'multiple', 'disabled', 'selected'))) {
249 $attrib_arr[] = sprintf('%s="%s"', $key, $key);
252 else if ($key=='value') {
253 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value, 'strict', false));
256 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value));
259 return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
264 * Class to create an HTML input field
268 class html_inputfield extends html
270 protected $tagname = 'input';
271 protected $type = 'text';
272 protected $allowed = array('type','name','value','size','tabindex',
273 'autocomplete','checked','onchange','onclick','disabled','readonly',
274 'spellcheck','results','maxlength','src','multiple');
279 * @param array $attrib Associative array with tag attributes
281 public function __construct($attrib = array())
283 if (is_array($attrib)) {
284 $this->attrib = $attrib;
287 if ($attrib['type']) {
288 $this->type = $attrib['type'];
291 if ($attrib['newline']) {
292 $this->newline = true;
299 * @param string $value Field value
300 * @param array $attrib Additional attributes to override
301 * @return string HTML output
303 public function show($value = null, $attrib = null)
305 // overwrite object attributes
306 if (is_array($attrib)) {
307 $this->attrib = array_merge($this->attrib, $attrib);
310 // set value attribute
311 if ($value !== null) {
312 $this->attrib['value'] = $value;
315 $this->attrib['type'] = $this->type;
316 return parent::show();
321 * Class to create an HTML password field
325 class html_passwordfield extends html_inputfield
327 protected $type = 'password';
331 * Class to create an hidden HTML input field
336 class html_hiddenfield extends html_inputfield
338 protected $type = 'hidden';
339 protected $fields_arr = array();
340 protected $newline = true;
345 * @param array $attrib Named tag attributes
347 public function __construct($attrib = null)
349 if (is_array($attrib)) {
355 * Add a hidden field to this instance
357 * @param array $attrib Named tag attributes
359 public function add($attrib)
361 $this->fields_arr[] = $attrib;
365 * Create HTML code for the hidden fields
367 * @return string Final HTML code
369 public function show()
372 foreach ($this->fields_arr as $attrib) {
373 $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
380 * Class to create HTML radio buttons
384 class html_radiobutton extends html_inputfield
386 protected $type = 'radio';
389 * Get HTML code for this object
391 * @param string $value Value of the checked field
392 * @param array $attrib Additional attributes to override
393 * @return string HTML output
395 public function show($value = '', $attrib = null)
397 // overwrite object attributes
398 if (is_array($attrib)) {
399 $this->attrib = array_merge($this->attrib, $attrib);
402 // set value attribute
403 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
405 return parent::show();
410 * Class to create HTML checkboxes
414 class html_checkbox extends html_inputfield
416 protected $type = 'checkbox';
419 * Get HTML code for this object
421 * @param string $value Value of the checked field
422 * @param array $attrib Additional attributes to override
423 * @return string HTML output
425 public function show($value = '', $attrib = null)
427 // overwrite object attributes
428 if (is_array($attrib)) {
429 $this->attrib = array_merge($this->attrib, $attrib);
432 // set value attribute
433 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
435 return parent::show();
440 * Class to create an HTML textarea
444 class html_textarea extends html
446 protected $tagname = 'textarea';
447 protected $allowed = array('name','rows','cols','wrap','tabindex',
448 'onchange','disabled','readonly','spellcheck');
451 * Get HTML code for this object
453 * @param string $value Textbox value
454 * @param array $attrib Additional attributes to override
455 * @return string HTML output
457 public function show($value = '', $attrib = null)
459 // overwrite object attributes
460 if (is_array($attrib)) {
461 $this->attrib = array_merge($this->attrib, $attrib);
464 // take value attribute as content
465 if (empty($value) && !empty($this->attrib['value'])) {
466 $value = $this->attrib['value'];
469 // make shure we don't print the value attribute
470 if (isset($this->attrib['value'])) {
471 unset($this->attrib['value']);
474 if (!empty($value) && !preg_match('/mce_editor/', $this->attrib['class'])) {
475 $value = Q($value, 'strict', false);
478 return self::tag($this->tagname, $this->attrib, $value,
479 array_merge(self::$common_attrib, $this->allowed));
484 * Builder for HTML drop-down menus
486 * // create instance. arguments are used to set attributes of select-tag
487 * $select = new html_select(array('name' => 'fieldname'));
490 * $select->add('Switzerland', 'CH');
492 * // add multiple options
493 * $select->add(array('Switzerland','Germany'), array('CH','DE'));
495 * // generate pulldown with selection 'Switzerland' and return html-code
496 * // as second argument the same attributes available to instanciate can be used
497 * print $select->show('CH');
502 class html_select extends html
504 protected $tagname = 'select';
505 protected $options = array();
506 protected $allowed = array('name','size','tabindex','autocomplete',
507 'multiple','onchange','disabled','rel');
510 * Add a new option to this drop-down
512 * @param mixed $names Option name or array with option names
513 * @param mixed $values Option value or array with option values
515 public function add($names, $values = null)
517 if (is_array($names)) {
518 foreach ($names as $i => $text) {
519 $this->options[] = array('text' => $text, 'value' => $values[$i]);
523 $this->options[] = array('text' => $names, 'value' => $values);
528 * Get HTML code for this object
530 * @param string $select Value of the selection option
531 * @param array $attrib Additional attributes to override
532 * @return string HTML output
534 public function show($select = array(), $attrib = null)
536 // overwrite object attributes
537 if (is_array($attrib)) {
538 $this->attrib = array_merge($this->attrib, $attrib);
541 $this->content = "\n";
542 $select = (array)$select;
543 foreach ($this->options as $option) {
545 'value' => $option['value'],
546 'selected' => (in_array($option['value'], $select, true) ||
547 in_array($option['text'], $select, true)) ? 1 : null);
549 $this->content .= self::tag('option', $attr, Q($option['text']));
551 return parent::show();
557 * Class to build an HTML table
561 class html_table extends html
563 protected $tagname = 'table';
564 protected $allowed = array('id','class','style','width','summary',
565 'cellpadding','cellspacing','border');
567 private $header = array();
568 private $rows = array();
569 private $rowindex = 0;
570 private $colindex = 0;
575 * @param array $attrib Named tag attributes
577 public function __construct($attrib = array())
579 $this->attrib = array_merge($attrib, array('summary' => '', 'border' => 0));
585 * @param array $attr Cell attributes
586 * @param string $cont Cell content
588 public function add($attr, $cont)
590 if (is_string($attr)) {
591 $attr = array('class' => $attr);
594 $cell = new stdClass;
595 $cell->attrib = $attr;
596 $cell->content = $cont;
598 $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
601 if ($this->attrib['cols'] && $this->colindex == $this->attrib['cols']) {
607 * Add a table header cell
609 * @param array $attr Cell attributes
610 * @param string $cont Cell content
612 public function add_header($attr, $cont)
614 if (is_string($attr))
615 $attr = array('class' => $attr);
617 $cell = new stdClass;
618 $cell->attrib = $attr;
619 $cell->content = $cont;
620 $this->header[] = $cell;
624 * Remove a column from a table
625 * Useful for plugins making alterations
627 * @param string $class
629 public function remove_column($class)
632 foreach ($this->header as $index=>$header){
633 if ($header->attrib['class'] == $class){
634 unset($this->header[$index]);
639 // Remove cells from rows
640 foreach ($this->rows as $i=>$row){
641 foreach ($row->cells as $j=>$cell){
642 if ($cell->attrib['class'] == $class){
643 unset($this->rows[$i]->cells[$j]);
653 * @param array $attr Row attributes
655 public function add_row($attr = array())
659 $this->rows[$this->rowindex] = new stdClass;
660 $this->rows[$this->rowindex]->attrib = $attr;
661 $this->rows[$this->rowindex]->cells = array();
667 * @param array $attr Row attributes
668 * @param int $index Optional row index (default current row index)
670 public function set_row_attribs($attr = array(), $index = null)
672 if (is_string($attr))
673 $attr = array('class' => $attr);
676 $index = $this->rowindex;
678 $this->rows[$index]->attrib = $attr;
684 * @param int $index Row index
686 * @return array Row attributes
688 public function get_row_attribs($index = null)
691 $index = $this->rowindex;
693 return $this->rows[$index] ? $this->rows[$index]->attrib : null;
697 * Build HTML output of the table data
699 * @param array $attrib Table attributes
700 * @return string The final table HTML code
702 public function show($attrib = null)
704 if (is_array($attrib))
705 $this->attrib = array_merge($this->attrib, $attrib);
707 $thead = $tbody = "";
710 if (!empty($this->header)) {
712 foreach ($this->header as $c => $col) {
713 $rowcontent .= self::tag('td', $col->attrib, $col->content);
715 $thead = self::tag('thead', null, self::tag('tr', null, $rowcontent, parent::$common_attrib));
718 foreach ($this->rows as $r => $row) {
720 foreach ($row->cells as $c => $col) {
721 $rowcontent .= self::tag('td', $col->attrib, $col->content);
724 if ($r < $this->rowindex || count($row->cells)) {
725 $tbody .= self::tag('tr', $row->attrib, $rowcontent, parent::$common_attrib);
729 if ($this->attrib['rowsonly']) {
734 $this->content = $thead . self::tag('tbody', null, $tbody);
736 unset($this->attrib['cols'], $this->attrib['rowsonly']);
737 return parent::show();
741 * Count number of rows
743 * @return The number of rows
745 public function size()
747 return count($this->rows);
751 * Remove table body (all rows)
753 public function remove_body()
755 $this->rows = array();