alecpl
2010-09-25 e019f2d0f2dc2fbfa345ab5d7ae85e67bfdd76b8
commit | author | age
47124c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/html.php                                              |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
A 8  | Copyright (C) 2005-2010, Roundcube Dev, - Switzerland                 |
47124c 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Helper class to create valid XHTML code                             |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
1d786c 18  $Id$
47124c 19
T 20  */
21
22
23 /**
24  * Class for HTML code creation
25  *
26  * @package HTML
27  */
28 class html
29 {
30     protected $tagname;
31     protected $attrib = array();
e3e597 32     protected $allowed = array();
47124c 33     protected $content;
T 34
35     public static $lc_tags = true;
878030 36     public static $common_attrib = array('id','class','style','title','align');
f52c93 37     public static $containers = array('iframe','div','span','p','h1','h2','h3','form','textarea','table','thead','tbody','tr','th','td','style','script');
47124c 38
T 39     /**
40      * Constructor
41      *
42      * @param array Hash array with tag attributes
43      */
44     public function __construct($attrib = array())
45     {
46         if (is_array($attrib)) {
47             $this->attrib = $attrib;
48         }
49     }
50
51     /**
52      * Return the tag code
53      *
54      * @return string The finally composed HTML tag
55      */
56     public function show()
57     {
e3e597 58         return self::tag($this->tagname, $this->attrib, $this->content, array_merge(self::$common_attrib, $this->allowed));
47124c 59     }
T 60
61     /****** STATIC METHODS *******/
62
63     /**
64      * Generic method to create a HTML tag
65      *
66      * @param string Tag name
67      * @param array  Tag attributes as key/value pairs
68      * @param string Optinal Tag content (creates a container tag)
69      * @param array  List with allowed attributes, omit to allow all
70      * @return string The XHTML tag
71      */
72     public static function tag($tagname, $attrib = array(), $content = null, $allowed_attrib = null)
73     {
74         $inline_tags = array('a','span','img');
75         $suffix = $attrib['nl'] || ($content && $attrib['nl'] !== false && !in_array($tagname, $inline_tags)) ? "\n" : '';
76
77         $tagname = self::$lc_tags ? strtolower($tagname) : $tagname;
78         if ($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);
82         }
83         else {
84             return sprintf("<%s%s />%s", $tagname, self::attrib_string($attrib, $allowed_attrib), $suffix);
85         }
86     }
87
88     /**
89      * Derrived method for <div> containers
90      *
91      * @param mixed  Hash array with tag attributes or string with class name
92      * @param string Div content
93      * @return string HTML code
94      * @see html::tag()
95      */
96     public static function div($attr = null, $cont = null)
97     {
98         if (is_string($attr)) {
99             $attr = array('class' => $attr);
100         }
f5aa16 101         return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, array('onclick')));
47124c 102     }
T 103
104     /**
105      * Derrived method for <p> blocks
106      *
107      * @param mixed  Hash array with tag attributes or string with class name
108      * @param string Paragraph content
109      * @return string HTML code
110      * @see html::tag()
111      */
112     public static function p($attr = null, $cont = null)
113     {
114         if (is_string($attr)) {
115             $attr = array('class' => $attr);
116         }
117         return self::tag('p', $attr, $cont, self::$common_attrib);
118     }
119
120     /**
121      * Derrived method to create <img />
122      *
123      * @param mixed Hash array with tag attributes or string with image source (src)
124      * @return string HTML code
125      * @see html::tag()
126      */
127     public static function img($attr = null)
128     {
129         if (is_string($attr)) {
130             $attr = array('src' => $attr);
131         }
878030 132         return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib,
A 133         array('src','alt','width','height','border','usemap')));
47124c 134     }
T 135
136     /**
137      * Derrived method for link tags
138      *
139      * @param mixed  Hash array with tag attributes or string with link location (href)
140      * @param string Link content
141      * @return string HTML code
142      * @see html::tag()
143      */
144     public static function a($attr, $cont)
145     {
146         if (is_string($attr)) {
147             $attr = array('href' => $attr);
148         }
878030 149         return self::tag('a', $attr, $cont, array_merge(self::$common_attrib,
A 150         array('href','target','name','onclick','onmouseover','onmouseout','onmousedown','onmouseup')));
47124c 151     }
T 152
153     /**
154      * Derrived method for inline span tags
155      *
156      * @param mixed  Hash array with tag attributes or string with class name
157      * @param string Tag content
158      * @return string HTML code
159      * @see html::tag()
160      */
161     public static function span($attr, $cont)
162     {
163         if (is_string($attr)) {
164             $attr = array('class' => $attr);
165         }
166         return self::tag('span', $attr, $cont, self::$common_attrib);
167     }
168
169     /**
170      * Derrived method for form element labels
171      *
172      * @param mixed  Hash array with tag attributes or string with 'for' attrib
173      * @param string Tag content
174      * @return string HTML code
175      * @see html::tag()
176      */
177     public static function label($attr, $cont)
178     {
179         if (is_string($attr)) {
180             $attr = array('for' => $attr);
181         }
182         return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, array('for')));
183     }
184
185     /**
95fcc3 186      * Derrived method to create <iframe></iframe>
T 187      *
188      * @param mixed Hash array with tag attributes or string with frame source (src)
189      * @return string HTML code
190      * @see html::tag()
191      */
192     public static function iframe($attr = null, $cont = null)
193     {
194         if (is_string($attr)) {
195             $attr = array('src' => $attr);
196         }
878030 197         return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib,
A 198         array('src','name','width','height','border','frameborder')));
95fcc3 199     }
T 200
201     /**
47124c 202      * Derrived method for line breaks
T 203      *
204      * @return string HTML code
205      * @see html::tag()
206      */
207     public static function br()
208     {
209         return self::tag('br');
210     }
211
212     /**
213      * Create string with attributes
214      *
215      * @param array Associative arry with tag attributes
216      * @param array List of allowed attributes
217      * @return string Valid attribute string
218      */
219     public static function attrib_string($attrib = array(), $allowed = null)
220     {
221         if (empty($attrib)) {
222             return '';
223         }
224
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))) {
230                 continue;
231             }
232
233             // ignore "internal" or not allowed attributes
234             if ($key == 'nl' || ($allowed && !isset($allowed_f[$key])) || $value === null) {
235                 continue;
236             }
237
238             // skip empty eventhandlers
239             if (preg_match('/^on[a-z]+/', $key) && !$value) {
240                 continue;
241             }
242
243             // attributes with no value
244             if (in_array($key, array('checked', 'multiple', 'disabled', 'selected'))) {
245                 if ($value) {
246                     $attrib_arr[] = sprintf('%s="%s"', $key, $key);
247                 }
248             }
249             else if ($key=='value') {
250                 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value, 'strict', false));
251             }
252             else {
253                 $attrib_arr[] = sprintf('%s="%s"', $key, Q($value));
254             }
255         }
256         return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
257     }
258 }
259
260 /**
261  * Class to create an HTML input field
262  *
263  * @package HTML
264  */
265 class html_inputfield extends html
266 {
267     protected $tagname = 'input';
268     protected $type = 'text';
878030 269     protected $allowed = array('type','name','value','size','tabindex',
A 270     'autocomplete','checked','onchange','onclick','disabled','readonly',
271     'spellcheck','results','maxlength','src');
47124c 272
T 273     public function __construct($attrib = array())
274     {
275         if (is_array($attrib)) {
276             $this->attrib = $attrib;
277         }
278
279         if ($attrib['type']) {
280             $this->type = $attrib['type'];
281         }
282
283         if ($attrib['newline']) {
284             $this->newline = true;
285         }
286     }
287
288     /**
289      * Compose input tag
290      *
291      * @param string Field value
292      * @param array Additional attributes to override
293      * @return string HTML output
294      */
295     public function show($value = null, $attrib = null)
296     {
297         // overwrite object attributes
298         if (is_array($attrib)) {
299             $this->attrib = array_merge($this->attrib, $attrib);
300         }
301
302         // set value attribute
303         if ($value !== null) {
304             $this->attrib['value'] = $value;
305         }
306         // set type
307         $this->attrib['type'] = $this->type;
308         return parent::show();
309     }
310 }
311
312 /**
313  * Class to create an HTML password field
314  *
315  * @package HTML
316  */
317 class html_passwordfield extends html_inputfield
318 {
319     protected $type = 'password';
320 }
321
322 /**
323  * Class to create an hidden HTML input field
324  *
325  * @package HTML
326  */
327
328 class html_hiddenfield extends html_inputfield
329 {
330     protected $type = 'hidden';
331     protected $fields_arr = array();
332     protected $newline = true;
333
334     /**
335      * Constructor
336      *
337      * @param array Named tag attributes
338      */
339     public function __construct($attrib = null)
340     {
341         if (is_array($attrib)) {
342             $this->add($attrib);
343         }
344     }
345
346     /**
347      * Add a hidden field to this instance
348      *
349      * @param array Named tag attributes
350      */
351     public function add($attrib)
352     {
353         $this->fields_arr[] = $attrib;
354     }
355
356     /**
357      * Create HTML code for the hidden fields
358      *
359      * @return string Final HTML code
360      */
361     public function show()
362     {
363         $out = '';
364         foreach ($this->fields_arr as $attrib) {
365             $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
366         }
367         return $out;
368     }
369 }
370
371 /**
372  * Class to create HTML radio buttons
373  *
374  * @package HTML
375  */
376 class html_radiobutton extends html_inputfield
377 {
378     protected $type = 'radio';
379
380     /**
381      * Get HTML code for this object
382      *
383      * @param string Value of the checked field
384      * @param array Additional attributes to override
385      * @return string HTML output
386      */
387     public function show($value = '', $attrib = null)
388     {
389         // overwrite object attributes
390         if (is_array($attrib)) {
391             $this->attrib = array_merge($this->attrib, $attrib);
392         }
393
394         // set value attribute
ff6def 395         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
47124c 396
T 397         return parent::show();
398     }
399 }
400
401 /**
402  * Class to create HTML checkboxes
403  *
404  * @package HTML
405  */
406 class html_checkbox extends html_inputfield
407 {
408     protected $type = 'checkbox';
409
410     /**
411      * Get HTML code for this object
412      *
413      * @param string Value of the checked field
414      * @param array Additional attributes to override
415      * @return string HTML output
416      */
417     public function show($value = '', $attrib = null)
418     {
419         // overwrite object attributes
420         if (is_array($attrib)) {
421             $this->attrib = array_merge($this->attrib, $attrib);
422         }
423
424         // set value attribute
ff6def 425         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
47124c 426
T 427         return parent::show();
428     }
429 }
430
431 /**
432  * Class to create an HTML textarea
433  *
434  * @package HTML
435  */
436 class html_textarea extends html
437 {
438     protected $tagname = 'textarea';
878030 439     protected $allowed = array('name','rows','cols','wrap','tabindex',
A 440     'onchange','disabled','readonly','spellcheck');
47124c 441
T 442     /**
443      * Get HTML code for this object
444      *
445      * @param string Textbox value
446      * @param array Additional attributes to override
447      * @return string HTML output
448      */
449     public function show($value = '', $attrib = null)
450     {
451         // overwrite object attributes
452         if (is_array($attrib)) {
453             $this->attrib = array_merge($this->attrib, $attrib);
454         }
455
456         // take value attribute as content
54d830 457         if (empty($value) && !empty($this->attrib['value'])) {
47124c 458             $value = $this->attrib['value'];
T 459         }
460
461         // make shure we don't print the value attribute
462         if (isset($this->attrib['value'])) {
463             unset($this->attrib['value']);
464         }
465
23a2ee 466         if (!empty($value) && !preg_match('/mce_editor/', $this->attrib['class'])) {
54d830 467             $value = Q($value, 'strict', false);
47124c 468         }
6025c8 469
878030 470         return self::tag($this->tagname, $this->attrib, $value,
A 471         array_merge(self::$common_attrib, $this->allowed));
47124c 472     }
T 473 }
474
475 /**
476  * Builder for HTML drop-down menus
477  * Syntax:<pre>
478  * // create instance. arguments are used to set attributes of select-tag
479  * $select = new html_select(array('name' => 'fieldname'));
480  *
481  * // add one option
482  * $select->add('Switzerland', 'CH');
483  *
484  * // add multiple options
485  * $select->add(array('Switzerland','Germany'), array('CH','DE'));
486  *
487  * // generate pulldown with selection 'Switzerland'  and return html-code
488  * // as second argument the same attributes available to instanciate can be used
489  * print $select->show('CH');
490  * </pre>
491  *
492  * @package HTML
493  */
494 class html_select extends html
495 {
496     protected $tagname = 'select';
497     protected $options = array();
878030 498     protected $allowed = array('name','size','tabindex','autocomplete',
A 499     'multiple','onchange','disabled');
47124c 500     
T 501     /**
502      * Add a new option to this drop-down
503      *
504      * @param mixed Option name or array with option names
505      * @param mixed Option value or array with option values
506      */
507     public function add($names, $values = null)
508     {
509         if (is_array($names)) {
510             foreach ($names as $i => $text) {
511                 $this->options[] = array('text' => $text, 'value' => $values[$i]);
512             }
513         }
514         else {
515             $this->options[] = array('text' => $names, 'value' => $values);
516         }
517     }
518
519
520     /**
521      * Get HTML code for this object
522      *
523      * @param string Value of the selection option
524      * @param array Additional attributes to override
525      * @return string HTML output
526      */
527     public function show($select = array(), $attrib = null)
528     {
529         // overwrite object attributes
530         if (is_array($attrib)) {
531             $this->attrib = array_merge($this->attrib, $attrib);
532         }
533
534         $this->content = "\n";
535         $select = (array)$select;
536         foreach ($this->options as $option) {
537             $attr = array(
538                 'value' => $option['value'],
ff6def 539                 'selected' => (in_array($option['value'], $select, true) ||
5b3dd4 540                   in_array($option['text'], $select, true)) ? 1 : null);
47124c 541
T 542             $this->content .= self::tag('option', $attr, Q($option['text']));
543         }
544         return parent::show();
545     }
546 }
547
548
549 /**
550  * Class to build an HTML table
551  *
552  * @package HTML
553  */
554 class html_table extends html
555 {
556     protected $tagname = 'table';
878030 557     protected $allowed = array('id','class','style','width','summary',
A 558     'cellpadding','cellspacing','border');
559
47124c 560     private $header = array();
T 561     private $rows = array();
562     private $rowindex = 0;
563     private $colindex = 0;
564
565
566     public function __construct($attrib = array())
567     {
568         $this->attrib = array_merge($attrib, array('summary' => '', 'border' => 0));
569     }
570
571     /**
572      * Add a table cell
573      *
574      * @param array Cell attributes
575      * @param string Cell content
576      */
577     public function add($attr, $cont)
578     {
579         if (is_string($attr)) {
580             $attr = array('class' => $attr);
581         }
582
583         $cell = new stdClass;
584         $cell->attrib = $attr;
585         $cell->content = $cont;
586
587         $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
588         $this->colindex++;
589
590         if ($this->attrib['cols'] && $this->colindex == $this->attrib['cols']) {
591             $this->add_row();
592         }
593     }
594
595     /**
596      * Add a table header cell
597      *
598      * @param array Cell attributes
599      * @param string Cell content
600      */
83a763 601     public function add_header($attr, $cont)
47124c 602     {
T 603         if (is_string($attr))
ffae15 604             $attr = array('class' => $attr);
47124c 605
T 606         $cell = new stdClass;
607         $cell->attrib = $attr;
608         $cell->content = $cont;
609         $this->header[] = $cell;
610     }
611
cc97ea 612      /**
T 613      * Remove a column from a table
614      * Useful for plugins making alterations
615      * 
616      * @param string $class 
617      */
618     public function remove_column($class)
619     {
620         // Remove the header
621         foreach($this->header as $index=>$header){
622             if($header->attrib['class'] == $class){
623                 unset($this->header[$index]);
624                 break;
625             }
626         }
627
628         // Remove cells from rows
629         foreach($this->rows as $i=>$row){
630             foreach($row->cells as $j=>$cell){
631                 if($cell->attrib['class'] == $class){
632                     unset($this->rows[$i]->cells[$j]);
633                     break;
634                 }
635             }
636         }
637     }
638
639
47124c 640     /**
T 641      * Jump to next row
642      *
643      * @param array Row attributes
644      */
83a763 645     public function add_row($attr = array())
47124c 646     {
T 647         $this->rowindex++;
648         $this->colindex = 0;
649         $this->rows[$this->rowindex] = new stdClass;
650         $this->rows[$this->rowindex]->attrib = $attr;
651         $this->rows[$this->rowindex]->cells = array();
652     }
653
ffae15 654     /**
A 655      * Set current row attrib
656      *
657      * @param array Row attributes
658      */
659     public function set_row_attribs($attr = array())
660     {
661         if (is_string($attr))
662             $attr = array('class' => $attr);
663
664         $this->rows[$this->rowindex]->attrib = $attr;
665     }
47124c 666
T 667     /**
668      * Build HTML output of the table data
669      *
670      * @param array Table attributes
671      * @return string The final table HTML code
672      */
46290a 673     public function show($attrib = null)
47124c 674     {
f92aba 675         if (is_array($attrib))
T 676             $this->attrib = array_merge($this->attrib, $attrib);
46290a 677         
f92aba 678         $thead = $tbody = "";
47124c 679
T 680         // include <thead>
681         if (!empty($this->header)) {
682             $rowcontent = '';
683             foreach ($this->header as $c => $col) {
83a763 684                 $rowcontent .= self::tag('td', $col->attrib, $col->content);
47124c 685             }
T 686             $thead = self::tag('thead', null, self::tag('tr', null, $rowcontent));
687         }
688
689         foreach ($this->rows as $r => $row) {
690             $rowcontent = '';
691             foreach ($row->cells as $c => $col) {
692                 $rowcontent .= self::tag('td', $col->attrib, $col->content);
693             }
694
695             if ($r < $this->rowindex || count($row->cells)) {
83a763 696                 $tbody .= self::tag('tr', $row->attrib, $rowcontent);
47124c 697             }
T 698         }
699
700         if ($this->attrib['rowsonly']) {
701             return $tbody;
702         }
703
704         // add <tbody>
705         $this->content = $thead . self::tag('tbody', null, $tbody);
706
707         unset($this->attrib['cols'], $this->attrib['rowsonly']);
708         return parent::show();
709     }
35c31e 710     
T 711     /**
712      * Count number of rows
713      *
714      * @return The number of rows
715      */
716     public function size()
717     {
718       return count($this->rows);
719     }
47124c 720 }
T 721