4 +-----------------------------------------------------------------------+
5 | program/include/html.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2010, Roundcube Dev, - Switzerland |
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 4216 2010-11-12 10:47:04Z 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 $inline_tags = array('a','span','img');
75 $suffix = $attrib['nl'] || ($content && $attrib['nl'] !== false && !in_array($tagname, $inline_tags)) ? "\n" : '';
77 $tagname = self::$lc_tags ? strtolower($tagname) : $tagname;
78 if (isset($content) || in_array($tagname, self::$containers)) {
79 $templ = $attrib['noclose'] ? "<%s%s>%s" : "<%s%s>%s</%s>%s";
80 unset($attrib['noclose']);
81 return sprintf($templ, $tagname, self::attrib_string($attrib, $allowed_attrib), $content, $tagname, $suffix);
84 return sprintf("<%s%s />%s", $tagname, self::attrib_string($attrib, $allowed_attrib), $suffix);
89 * Derrived method for <div> containers
91 * @param mixed $attr Hash array with tag attributes or string with class name
92 * @param string $cont Div content
93 * @return string HTML code
96 public static function div($attr = null, $cont = null)
98 if (is_string($attr)) {
99 $attr = array('class' => $attr);
101 return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, array('onclick')));
105 * Derrived method for <p> blocks
107 * @param mixed $attr Hash array with tag attributes or string with class name
108 * @param string $cont Paragraph content
109 * @return string HTML code
112 public static function p($attr = null, $cont = null)
114 if (is_string($attr)) {
115 $attr = array('class' => $attr);
117 return self::tag('p', $attr, $cont, self::$common_attrib);
121 * Derrived method to create <img />
123 * @param mixed $attr Hash array with tag attributes or string with image source (src)
124 * @return string HTML code
127 public static function img($attr = null)
129 if (is_string($attr)) {
130 $attr = array('src' => $attr);
132 return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib,
133 array('src','alt','width','height','border','usemap')));
137 * Derrived method for link tags
139 * @param mixed $attr Hash array with tag attributes or string with link location (href)
140 * @param string $cont Link content
141 * @return string HTML code
144 public static function a($attr, $cont)
146 if (is_string($attr)) {
147 $attr = array('href' => $attr);
149 return self::tag('a', $attr, $cont, array_merge(self::$common_attrib,
150 array('href','target','name','onclick','onmouseover','onmouseout','onmousedown','onmouseup')));
154 * Derrived method for inline span tags
156 * @param mixed $attr Hash array with tag attributes or string with class name
157 * @param string $cont Tag content
158 * @return string HTML code
161 public static function span($attr, $cont)
163 if (is_string($attr)) {
164 $attr = array('class' => $attr);
166 return self::tag('span', $attr, $cont, self::$common_attrib);
170 * Derrived method for form element labels
172 * @param mixed $attr Hash array with tag attributes or string with 'for' attrib
173 * @param string $cont Tag content
174 * @return string HTML code
177 public static function label($attr, $cont)
179 if (is_string($attr)) {
180 $attr = array('for' => $attr);
182 return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, array('for')));
186 * Derrived method to create <iframe></iframe>
188 * @param mixed $attr Hash array with tag attributes or string with frame source (src)
189 * @return string HTML code
192 public static function iframe($attr = null, $cont = null)
194 if (is_string($attr)) {
195 $attr = array('src' => $attr);
197 return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib,
198 array('src','name','width','height','border','frameborder')));
202 * Derrived method for line breaks
204 * @return string HTML code
207 public static function br()
209 return self::tag('br');
213 * Create string with attributes
215 * @param array $attrib Associative arry with tag attributes
216 * @param array $allowed List of allowed attributes
217 * @return string Valid attribute string
219 public static function attrib_string($attrib = array(), $allowed = null)
221 if (empty($attrib)) {
225 $allowed_f = array_flip((array)$allowed);
226 $attrib_arr = array();
227 foreach ($attrib as $key => $value) {
228 // skip size if not numeric
229 if (($key=='size' && !is_numeric($value))) {
233 // ignore "internal" or not allowed attributes
234 if ($key == 'nl' || ($allowed && !isset($allowed_f[$key])) || $value === null) {
238 // skip empty eventhandlers
239 if (preg_match('/^on[a-z]+/', $key) && !$value) {
243 // attributes with no value
244 if (in_array($key, array('checked', 'multiple', 'disabled', 'selected'))) {
246 $attrib_arr[] = sprintf('%s="%s"', $key, $key);
249 else if ($key=='value') {
250 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value, 'strict', false));
253 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value));
256 return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
261 * Class to create an HTML input field
265 class html_inputfield extends html
267 protected $tagname = 'input';
268 protected $type = 'text';
269 protected $allowed = array('type','name','value','size','tabindex',
270 'autocomplete','checked','onchange','onclick','disabled','readonly',
271 'spellcheck','results','maxlength','src');
276 * @param array $attrib Associative array with tag attributes
278 public function __construct($attrib = array())
280 if (is_array($attrib)) {
281 $this->attrib = $attrib;
284 if ($attrib['type']) {
285 $this->type = $attrib['type'];
288 if ($attrib['newline']) {
289 $this->newline = true;
296 * @param string $value Field value
297 * @param array $attrib Additional attributes to override
298 * @return string HTML output
300 public function show($value = null, $attrib = null)
302 // overwrite object attributes
303 if (is_array($attrib)) {
304 $this->attrib = array_merge($this->attrib, $attrib);
307 // set value attribute
308 if ($value !== null) {
309 $this->attrib['value'] = $value;
312 $this->attrib['type'] = $this->type;
313 return parent::show();
318 * Class to create an HTML password field
322 class html_passwordfield extends html_inputfield
324 protected $type = 'password';
328 * Class to create an hidden HTML input field
333 class html_hiddenfield extends html_inputfield
335 protected $type = 'hidden';
336 protected $fields_arr = array();
337 protected $newline = true;
342 * @param array $attrib Named tag attributes
344 public function __construct($attrib = null)
346 if (is_array($attrib)) {
352 * Add a hidden field to this instance
354 * @param array $attrib Named tag attributes
356 public function add($attrib)
358 $this->fields_arr[] = $attrib;
362 * Create HTML code for the hidden fields
364 * @return string Final HTML code
366 public function show()
369 foreach ($this->fields_arr as $attrib) {
370 $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
377 * Class to create HTML radio buttons
381 class html_radiobutton extends html_inputfield
383 protected $type = 'radio';
386 * Get HTML code for this object
388 * @param string $value Value of the checked field
389 * @param array $attrib Additional attributes to override
390 * @return string HTML output
392 public function show($value = '', $attrib = null)
394 // overwrite object attributes
395 if (is_array($attrib)) {
396 $this->attrib = array_merge($this->attrib, $attrib);
399 // set value attribute
400 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
402 return parent::show();
407 * Class to create HTML checkboxes
411 class html_checkbox extends html_inputfield
413 protected $type = 'checkbox';
416 * Get HTML code for this object
418 * @param string $value Value of the checked field
419 * @param array $attrib Additional attributes to override
420 * @return string HTML output
422 public function show($value = '', $attrib = null)
424 // overwrite object attributes
425 if (is_array($attrib)) {
426 $this->attrib = array_merge($this->attrib, $attrib);
429 // set value attribute
430 $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
432 return parent::show();
437 * Class to create an HTML textarea
441 class html_textarea extends html
443 protected $tagname = 'textarea';
444 protected $allowed = array('name','rows','cols','wrap','tabindex',
445 'onchange','disabled','readonly','spellcheck');
448 * Get HTML code for this object
450 * @param string $value Textbox value
451 * @param array $attrib Additional attributes to override
452 * @return string HTML output
454 public function show($value = '', $attrib = null)
456 // overwrite object attributes
457 if (is_array($attrib)) {
458 $this->attrib = array_merge($this->attrib, $attrib);
461 // take value attribute as content
462 if (empty($value) && !empty($this->attrib['value'])) {
463 $value = $this->attrib['value'];
466 // make shure we don't print the value attribute
467 if (isset($this->attrib['value'])) {
468 unset($this->attrib['value']);
471 if (!empty($value) && !preg_match('/mce_editor/', $this->attrib['class'])) {
472 $value = Q($value, 'strict', false);
475 return self::tag($this->tagname, $this->attrib, $value,
476 array_merge(self::$common_attrib, $this->allowed));
481 * Builder for HTML drop-down menus
483 * // create instance. arguments are used to set attributes of select-tag
484 * $select = new html_select(array('name' => 'fieldname'));
487 * $select->add('Switzerland', 'CH');
489 * // add multiple options
490 * $select->add(array('Switzerland','Germany'), array('CH','DE'));
492 * // generate pulldown with selection 'Switzerland' and return html-code
493 * // as second argument the same attributes available to instanciate can be used
494 * print $select->show('CH');
499 class html_select extends html
501 protected $tagname = 'select';
502 protected $options = array();
503 protected $allowed = array('name','size','tabindex','autocomplete',
504 'multiple','onchange','disabled');
507 * Add a new option to this drop-down
509 * @param mixed $names Option name or array with option names
510 * @param mixed $values Option value or array with option values
512 public function add($names, $values = null)
514 if (is_array($names)) {
515 foreach ($names as $i => $text) {
516 $this->options[] = array('text' => $text, 'value' => $values[$i]);
520 $this->options[] = array('text' => $names, 'value' => $values);
526 * Get HTML code for this object
528 * @param string $select Value of the selection option
529 * @param array $attrib Additional attributes to override
530 * @return string HTML output
532 public function show($select = array(), $attrib = null)
534 // overwrite object attributes
535 if (is_array($attrib)) {
536 $this->attrib = array_merge($this->attrib, $attrib);
539 $this->content = "\n";
540 $select = (array)$select;
541 foreach ($this->options as $option) {
543 'value' => $option['value'],
544 'selected' => (in_array($option['value'], $select, true) ||
545 in_array($option['text'], $select, true)) ? 1 : null);
547 $this->content .= self::tag('option', $attr, Q($option['text']));
549 return parent::show();
555 * Class to build an HTML table
559 class html_table extends html
561 protected $tagname = 'table';
562 protected $allowed = array('id','class','style','width','summary',
563 'cellpadding','cellspacing','border');
565 private $header = array();
566 private $rows = array();
567 private $rowindex = 0;
568 private $colindex = 0;
573 * @param array $attrib Named tag attributes
575 public function __construct($attrib = array())
577 $this->attrib = array_merge($attrib, array('summary' => '', 'border' => 0));
583 * @param array $attr Cell attributes
584 * @param string $cont Cell content
586 public function add($attr, $cont)
588 if (is_string($attr)) {
589 $attr = array('class' => $attr);
592 $cell = new stdClass;
593 $cell->attrib = $attr;
594 $cell->content = $cont;
596 $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
599 if ($this->attrib['cols'] && $this->colindex == $this->attrib['cols']) {
605 * Add a table header cell
607 * @param array $attr Cell attributes
608 * @param string $cont Cell content
610 public function add_header($attr, $cont)
612 if (is_string($attr))
613 $attr = array('class' => $attr);
615 $cell = new stdClass;
616 $cell->attrib = $attr;
617 $cell->content = $cont;
618 $this->header[] = $cell;
622 * Remove a column from a table
623 * Useful for plugins making alterations
625 * @param string $class
627 public function remove_column($class)
630 foreach($this->header as $index=>$header){
631 if($header->attrib['class'] == $class){
632 unset($this->header[$index]);
637 // Remove cells from rows
638 foreach($this->rows as $i=>$row){
639 foreach($row->cells as $j=>$cell){
640 if($cell->attrib['class'] == $class){
641 unset($this->rows[$i]->cells[$j]);
652 * @param array $attr Row attributes
654 public function add_row($attr = array())
658 $this->rows[$this->rowindex] = new stdClass;
659 $this->rows[$this->rowindex]->attrib = $attr;
660 $this->rows[$this->rowindex]->cells = array();
664 * Set current row attrib
666 * @param array $attr Row attributes
668 public function set_row_attribs($attr = array())
670 if (is_string($attr))
671 $attr = array('class' => $attr);
673 $this->rows[$this->rowindex]->attrib = $attr;
677 * Build HTML output of the table data
679 * @param array $attrib Table attributes
680 * @return string The final table HTML code
682 public function show($attrib = null)
684 if (is_array($attrib))
685 $this->attrib = array_merge($this->attrib, $attrib);
687 $thead = $tbody = "";
690 if (!empty($this->header)) {
692 foreach ($this->header as $c => $col) {
693 $rowcontent .= self::tag('td', $col->attrib, $col->content);
695 $thead = self::tag('thead', null, self::tag('tr', null, $rowcontent));
698 foreach ($this->rows as $r => $row) {
700 foreach ($row->cells as $c => $col) {
701 $rowcontent .= self::tag('td', $col->attrib, $col->content);
704 if ($r < $this->rowindex || count($row->cells)) {
705 $tbody .= self::tag('tr', $row->attrib, $rowcontent);
709 if ($this->attrib['rowsonly']) {
714 $this->content = $thead . self::tag('tbody', null, $tbody);
716 unset($this->attrib['cols'], $this->attrib['rowsonly']);
717 return parent::show();
721 * Count number of rows
723 * @return The number of rows
725 public function size()
727 return count($this->rows);