]> git.donarmstrong.com Git - roundcube.git/blob - program/include/html.php
Imported Upstream version 0.5
[roundcube.git] / program / include / html.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/html.php                                              |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2010, Roundcube Dev, - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Helper class to create valid XHTML code                             |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: html.php 4216 2010-11-12 10:47:04Z alec $
19
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();
32     protected $allowed = array();
33     protected $content;
34
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');
38
39     /**
40      * Constructor
41      *
42      * @param array $attrib 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     {
58         return self::tag($this->tagname, $this->attrib, $this->content, array_merge(self::$common_attrib, $this->allowed));
59     }
60
61     /****** STATIC METHODS *******/
62
63     /**
64      * Generic method to create a HTML tag
65      *
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
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 (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);
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  $attr Hash array with tag attributes or string with class name
92      * @param string $cont 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         }
101         return self::tag('div', $attr, $cont, array_merge(self::$common_attrib, array('onclick')));
102     }
103
104     /**
105      * Derrived method for <p> blocks
106      *
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
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 $attr 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         }
132         return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib,
133             array('src','alt','width','height','border','usemap')));
134     }
135
136     /**
137      * Derrived method for link tags
138      *
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
142      * @see html::tag()
143      */
144     public static function a($attr, $cont)
145     {
146         if (is_string($attr)) {
147             $attr = array('href' => $attr);
148         }
149         return self::tag('a', $attr, $cont, array_merge(self::$common_attrib,
150             array('href','target','name','onclick','onmouseover','onmouseout','onmousedown','onmouseup')));
151     }
152
153     /**
154      * Derrived method for inline span tags
155      *
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
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  $attr Hash array with tag attributes or string with 'for' attrib
173      * @param string $cont 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     /**
186      * Derrived method to create <iframe></iframe>
187      *
188      * @param mixed $attr 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         }
197         return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib,
198             array('src','name','width','height','border','frameborder')));
199     }
200
201     /**
202      * Derrived method for line breaks
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 $attrib Associative arry with tag attributes
216      * @param array $allowed 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';
269     protected $allowed = array('type','name','value','size','tabindex',
270         'autocomplete','checked','onchange','onclick','disabled','readonly',
271         'spellcheck','results','maxlength','src');
272
273     /**
274      * Object constructor
275      *
276      * @param array $attrib Associative array with tag attributes
277      */
278     public function __construct($attrib = array())
279     {
280         if (is_array($attrib)) {
281             $this->attrib = $attrib;
282         }
283
284         if ($attrib['type']) {
285             $this->type = $attrib['type'];
286         }
287
288         if ($attrib['newline']) {
289             $this->newline = true;
290         }
291     }
292
293     /**
294      * Compose input tag
295      *
296      * @param string $value Field value
297      * @param array  $attrib Additional attributes to override
298      * @return string HTML output
299      */
300     public function show($value = null, $attrib = null)
301     {
302         // overwrite object attributes
303         if (is_array($attrib)) {
304             $this->attrib = array_merge($this->attrib, $attrib);
305         }
306
307         // set value attribute
308         if ($value !== null) {
309             $this->attrib['value'] = $value;
310         }
311         // set type
312         $this->attrib['type'] = $this->type;
313         return parent::show();
314     }
315 }
316
317 /**
318  * Class to create an HTML password field
319  *
320  * @package HTML
321  */
322 class html_passwordfield extends html_inputfield
323 {
324     protected $type = 'password';
325 }
326
327 /**
328  * Class to create an hidden HTML input field
329  *
330  * @package HTML
331  */
332
333 class html_hiddenfield extends html_inputfield
334 {
335     protected $type = 'hidden';
336     protected $fields_arr = array();
337     protected $newline = true;
338
339     /**
340      * Constructor
341      *
342      * @param array $attrib Named tag attributes
343      */
344     public function __construct($attrib = null)
345     {
346         if (is_array($attrib)) {
347             $this->add($attrib);
348         }
349     }
350
351     /**
352      * Add a hidden field to this instance
353      *
354      * @param array $attrib Named tag attributes
355      */
356     public function add($attrib)
357     {
358         $this->fields_arr[] = $attrib;
359     }
360
361     /**
362      * Create HTML code for the hidden fields
363      *
364      * @return string Final HTML code
365      */
366     public function show()
367     {
368         $out = '';
369         foreach ($this->fields_arr as $attrib) {
370             $out .= self::tag($this->tagname, array('type' => $this->type) + $attrib);
371         }
372         return $out;
373     }
374 }
375
376 /**
377  * Class to create HTML radio buttons
378  *
379  * @package HTML
380  */
381 class html_radiobutton extends html_inputfield
382 {
383     protected $type = 'radio';
384
385     /**
386      * Get HTML code for this object
387      *
388      * @param string $value  Value of the checked field
389      * @param array  $attrib Additional attributes to override
390      * @return string HTML output
391      */
392     public function show($value = '', $attrib = null)
393     {
394         // overwrite object attributes
395         if (is_array($attrib)) {
396             $this->attrib = array_merge($this->attrib, $attrib);
397         }
398
399         // set value attribute
400         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
401
402         return parent::show();
403     }
404 }
405
406 /**
407  * Class to create HTML checkboxes
408  *
409  * @package HTML
410  */
411 class html_checkbox extends html_inputfield
412 {
413     protected $type = 'checkbox';
414
415     /**
416      * Get HTML code for this object
417      *
418      * @param string $value  Value of the checked field
419      * @param array  $attrib Additional attributes to override
420      * @return string HTML output
421      */
422     public function show($value = '', $attrib = null)
423     {
424         // overwrite object attributes
425         if (is_array($attrib)) {
426             $this->attrib = array_merge($this->attrib, $attrib);
427         }
428
429         // set value attribute
430         $this->attrib['checked'] = ((string)$value == (string)$this->attrib['value']);
431
432         return parent::show();
433     }
434 }
435
436 /**
437  * Class to create an HTML textarea
438  *
439  * @package HTML
440  */
441 class html_textarea extends html
442 {
443     protected $tagname = 'textarea';
444     protected $allowed = array('name','rows','cols','wrap','tabindex',
445         'onchange','disabled','readonly','spellcheck');
446
447     /**
448      * Get HTML code for this object
449      *
450      * @param string $value  Textbox value
451      * @param array  $attrib Additional attributes to override
452      * @return string HTML output
453      */
454     public function show($value = '', $attrib = null)
455     {
456         // overwrite object attributes
457         if (is_array($attrib)) {
458             $this->attrib = array_merge($this->attrib, $attrib);
459         }
460
461         // take value attribute as content
462         if (empty($value) && !empty($this->attrib['value'])) {
463             $value = $this->attrib['value'];
464         }
465
466         // make shure we don't print the value attribute
467         if (isset($this->attrib['value'])) {
468             unset($this->attrib['value']);
469         }
470
471         if (!empty($value) && !preg_match('/mce_editor/', $this->attrib['class'])) {
472             $value = Q($value, 'strict', false);
473         }
474
475         return self::tag($this->tagname, $this->attrib, $value,
476             array_merge(self::$common_attrib, $this->allowed));
477     }
478 }
479
480 /**
481  * Builder for HTML drop-down menus
482  * Syntax:<pre>
483  * // create instance. arguments are used to set attributes of select-tag
484  * $select = new html_select(array('name' => 'fieldname'));
485  *
486  * // add one option
487  * $select->add('Switzerland', 'CH');
488  *
489  * // add multiple options
490  * $select->add(array('Switzerland','Germany'), array('CH','DE'));
491  *
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');
495  * </pre>
496  *
497  * @package HTML
498  */
499 class html_select extends html
500 {
501     protected $tagname = 'select';
502     protected $options = array();
503     protected $allowed = array('name','size','tabindex','autocomplete',
504         'multiple','onchange','disabled');
505     
506     /**
507      * Add a new option to this drop-down
508      *
509      * @param mixed $names  Option name or array with option names
510      * @param mixed $values Option value or array with option values
511      */
512     public function add($names, $values = null)
513     {
514         if (is_array($names)) {
515             foreach ($names as $i => $text) {
516                 $this->options[] = array('text' => $text, 'value' => $values[$i]);
517             }
518         }
519         else {
520             $this->options[] = array('text' => $names, 'value' => $values);
521         }
522     }
523
524
525     /**
526      * Get HTML code for this object
527      *
528      * @param string $select Value of the selection option
529      * @param array  $attrib Additional attributes to override
530      * @return string HTML output
531      */
532     public function show($select = array(), $attrib = null)
533     {
534         // overwrite object attributes
535         if (is_array($attrib)) {
536             $this->attrib = array_merge($this->attrib, $attrib);
537         }
538
539         $this->content = "\n";
540         $select = (array)$select;
541         foreach ($this->options as $option) {
542             $attr = array(
543                 'value' => $option['value'],
544                 'selected' => (in_array($option['value'], $select, true) ||
545                   in_array($option['text'], $select, true)) ? 1 : null);
546
547             $this->content .= self::tag('option', $attr, Q($option['text']));
548         }
549         return parent::show();
550     }
551 }
552
553
554 /**
555  * Class to build an HTML table
556  *
557  * @package HTML
558  */
559 class html_table extends html
560 {
561     protected $tagname = 'table';
562     protected $allowed = array('id','class','style','width','summary',
563         'cellpadding','cellspacing','border');
564
565     private $header = array();
566     private $rows = array();
567     private $rowindex = 0;
568     private $colindex = 0;
569
570     /**
571      * Constructor
572      *
573      * @param array $attrib Named tag attributes
574      */
575     public function __construct($attrib = array())
576     {
577         $this->attrib = array_merge($attrib, array('summary' => '', 'border' => 0));
578     }
579
580     /**
581      * Add a table cell
582      *
583      * @param array  $attr Cell attributes
584      * @param string $cont Cell content
585      */
586     public function add($attr, $cont)
587     {
588         if (is_string($attr)) {
589             $attr = array('class' => $attr);
590         }
591
592         $cell = new stdClass;
593         $cell->attrib = $attr;
594         $cell->content = $cont;
595
596         $this->rows[$this->rowindex]->cells[$this->colindex] = $cell;
597         $this->colindex++;
598
599         if ($this->attrib['cols'] && $this->colindex == $this->attrib['cols']) {
600             $this->add_row();
601         }
602     }
603
604     /**
605      * Add a table header cell
606      *
607      * @param array  $attr Cell attributes
608      * @param string $cont Cell content
609      */
610     public function add_header($attr, $cont)
611     {
612         if (is_string($attr))
613             $attr = array('class' => $attr);
614
615         $cell = new stdClass;
616         $cell->attrib = $attr;
617         $cell->content = $cont;
618         $this->header[] = $cell;
619     }
620
621      /**
622      * Remove a column from a table
623      * Useful for plugins making alterations
624      * 
625      * @param string $class 
626      */
627     public function remove_column($class)
628     {
629         // Remove the header
630         foreach($this->header as $index=>$header){
631             if($header->attrib['class'] == $class){
632                 unset($this->header[$index]);
633                 break;
634             }
635         }
636
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]);
642                     break;
643                 }
644             }
645         }
646     }
647
648
649     /**
650      * Jump to next row
651      *
652      * @param array $attr Row attributes
653      */
654     public function add_row($attr = array())
655     {
656         $this->rowindex++;
657         $this->colindex = 0;
658         $this->rows[$this->rowindex] = new stdClass;
659         $this->rows[$this->rowindex]->attrib = $attr;
660         $this->rows[$this->rowindex]->cells = array();
661     }
662
663     /**
664      * Set current row attrib
665      *
666      * @param array $attr Row attributes
667      */
668     public function set_row_attribs($attr = array())
669     {
670         if (is_string($attr))
671             $attr = array('class' => $attr);
672
673         $this->rows[$this->rowindex]->attrib = $attr;
674     }
675
676     /**
677      * Build HTML output of the table data
678      *
679      * @param array $attrib Table attributes
680      * @return string The final table HTML code
681      */
682     public function show($attrib = null)
683     {
684         if (is_array($attrib))
685             $this->attrib = array_merge($this->attrib, $attrib);
686         
687         $thead = $tbody = "";
688
689         // include <thead>
690         if (!empty($this->header)) {
691             $rowcontent = '';
692             foreach ($this->header as $c => $col) {
693                 $rowcontent .= self::tag('td', $col->attrib, $col->content);
694             }
695             $thead = self::tag('thead', null, self::tag('tr', null, $rowcontent));
696         }
697
698         foreach ($this->rows as $r => $row) {
699             $rowcontent = '';
700             foreach ($row->cells as $c => $col) {
701                 $rowcontent .= self::tag('td', $col->attrib, $col->content);
702             }
703
704             if ($r < $this->rowindex || count($row->cells)) {
705                 $tbody .= self::tag('tr', $row->attrib, $rowcontent);
706             }
707         }
708
709         if ($this->attrib['rowsonly']) {
710             return $tbody;
711         }
712
713         // add <tbody>
714         $this->content = $thead . self::tag('tbody', null, $tbody);
715
716         unset($this->attrib['cols'], $this->attrib['rowsonly']);
717         return parent::show();
718     }
719     
720     /**
721      * Count number of rows
722      *
723      * @return The number of rows
724      */
725     public function size()
726     {
727       return count($this->rows);
728     }
729 }
730