summaryrefslogtreecommitdiffstats
path: root/lib/kolab_html.php
blob: a2088a1fc7351570e8177a99c3da561d24e14288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2012, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

/**
 * HTML output generation
 */
class kolab_html
{
    public static $common_attribs = array('id', 'class', 'style', 'title', 'align', 'dir');
    public static $event_attribs  = array('onclick', 'ondblclick', 'onmousedown', 'onmouseup',
        'onmouseover', 'onmousemove', 'onmouseout');
    public static $input_event_attribs = array('onfocus', 'onblur', 'onkeypress', 'onkeydown', 'onkeyup',
        'onsubmit', 'onreset', 'onselect', 'onchange');
    public static $table_attribs  = array('summary');
    public static $tr_attribs     = array();
    public static $td_attribs     = array('colspan', 'rowspan');
    public static $textarea_attribs = array('cols', 'rows', 'disabled', 'name', 'readonly', 'tabindex');
    public static $input_attribs  = array('checked', 'disabled', 'name', 'readonly', 'tabindex',
        'type', 'size', 'maxlength', 'value', 'autofocus', 'placeholder');
    public static $select_attribs = array('multiple', 'name', 'size', 'disabled', 'readonly', 'autofocus');
    public static $option_attribs = array('selected', 'value', 'disabled', 'readonly');
    public static $a_attribs      = array('href', 'name', 'rel', 'tabindex', 'target');
    public static $form_attribs   = array('action', 'enctype', 'method', 'name', 'target');
    public static $label_attribs  = array('for');


    /**
     * Table element (TABLE).
     *
     * @param array  $attribs  Table attributes
     * @param string $content  Optional table content. If empty
     *                         head, body, foot attributes will be used.
     *
     * @return string HTML output of the table
     */
    public static function table($attribs = array(), $content = null)
    {
        $table_attribs = array_merge(self::$table_attribs, self::$common_attribs, self::$event_attribs);
        $table = '<table' . self::attrib_string($attribs, $table_attribs) . '>';

        if ($content) {
            $table .= $content;
        }
        else {
            if (!empty($attribs['head']) && is_array($attribs['head'])) {
                $table .= '<thead>';
                foreach ($attribs['head'] as $row) {
                    $table .= "\n" . self::tr($row, true);
                }
                $table .= '</thead>';
            }
            if (!empty($attribs['body']) && is_array($attribs['body'])) {
                $table .= '<tbody>';
                foreach ($attribs['body'] as $row) {
                    $table .= "\n" . self::tr($row);
                }
                $table .= '</tbody>';
            }
            if (!empty($attribs['foot']) && is_array($attribs['foot'])) {
                $table .= '<tfoot>';
                foreach ($attribs['foot'] as $row) {
                    $table .= "\n" . self::tr($row);
                }
                $table .= '</tfoot>';
            }
        }

        $table .= "\n</table>";

        return $table;
    }

    /**
     * Table row (TR).
     *
     * @param array  $attribs  Row attributes
     * @param string $is_head  Set to true if it is a part of table head.
     *
     * @return string HTML output of the row
     */
    public static function tr($attribs = array(), $is_head = false)
    {
        $row_attribs = array_merge(self::$tr_attribs, self::$common_attribs, self::$event_attribs);
        $row = '<tr' . self::attrib_string($attribs, $row_attribs) . '>';

        if (!empty($attribs['cells']) && is_array($attribs['cells'])) {
            foreach ($attribs['cells'] as $cell) {
                $row .= "\n" . self::td($cell, $is_head);
            }
        }

        $row .= "\n</tr>";

        return $row;
    }

    /**
     * Table cell (TD or TH).
     *
     * @param array  $attribs  Cell attributes
     * @param string $is_head  Set to true if it is a part of table head.
     *
     * @return string HTML output of the cell
     */
    public static function td($attribs = array(), $is_head = false)
    {
        $cell_attribs = array_merge(self::$td_attribs, self::$common_attribs, self::$event_attribs);
        $tag = $is_head ? 'th' : 'td';
        $cell .= '<' . $tag . self::attrib_string($attribs, $cell_attribs) . '>';

        if (isset($attribs['body'])) {
            $cell .= $attribs['body'];
        }

        $cell .= "</$tag>";

        return $cell;
    }

    /**
     * Input element.
     *
     * @param array  $attribs  Element attributes
     *
     * @return string HTML output of the input
     */
    public static function input($attribs = array())
    {
        $elem_attribs = array_merge(self::$input_attribs, self::$input_event_attribs,
            self::$common_attribs, self::$event_attribs);

        if ($attribs['type'] == 'checkbox' && $attribs['readonly']) {
            // readonly checkbox should be disabled, otherwise the user could still check or uncheck the box
            $attribs['disabled'] = 'disabled';
        }

        if ($attribs['type'] == 'button' || $attribs['type'] == 'submit') {
            $attribs['class'] = trim($attribs['class'] . ' btn');

            if (!preg_match('/btn-/', $attribs['class'])) {
                $attribs['class'] .= ' btn-' . (strpos($attribs['class'], 'submit') !== false ? 'primary' : 'secondary');
            }
        }
        else if ($attribs['type'] == 'checkbox') {
            // TODO: Bootstrap style
        }
        else {
            $attribs['class'] = trim($attribs['class'] . ' form-control');
        }

        return sprintf('<input%s />', self::attrib_string($attribs, $elem_attribs));
    }

    /**
     * Input element for mail quota. user can select the unit (GB, MB, KB)
     *
     * @param array  $attribs  Element attributes
     *
     * @return string HTML output of the input
     */
    public static function inputquota($attribs = array())
    {
        if ($attribs['value'] % 1024 == 0) {
            if ($attribs['value'] >= 1024) {
                $attribs['value'] /= 1024;
                $unit = 'mb';
            }
            if ($attribs['value'] % 1024 == 0 && $attribs['value'] >= 1024) {
                $attribs['value'] /= 1024;
                $unit = 'gb';
            }
        }

        if (empty($attribs['size'])) {
            $attribs['size'] = 10;
        }

        // show no select dropdown box if value is readonly
        if (!empty($attribs['readonly'])) {
            if ($unit) {
                $attribs['value'] .= ' ' . strtoupper($unit);
            }

            return self::input($attribs);
        }

        $select = array(
            'name'     => $attribs['name'] . '-unit',
            'options'  => array(),
        );

        foreach (array('kb', 'mb', 'gb') as $u) {
            $select['options'][] = array(
                'value'    => $u,
                'content'  => strtoupper($u),
                'selected' => $unit == $u,
            );
        }

        $attribs['data-type'] = 'quota';
        $select['class'] = 'input-group-append';

        return self::div(array(
            'class'   => 'input-group',
            'content' => self::input($attribs) . self::select($select)
        ));
    }

    /**
     * Textarea element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the textarea
     */
    public static function textarea($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$textarea_attribs, self::$input_event_attribs,
            self::$common_attribs, self::$event_attribs);

        $content = isset($attribs['value']) ? $attribs['value'] : '';

        if ($escape) {
            $content = self::escape($content);
        }

        $attribs['class'] = trim($attribs['class'] . ' form-control');

        return sprintf('<textarea%s>%s</textarea>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Select element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the select tag
     */
    public static function select($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$select_attribs, self::$input_event_attribs,
            self::$common_attribs, self::$event_attribs);

        $content = array();
        if (!empty($attribs['options']) && is_array($attribs['options'])) {
            foreach ($attribs['options'] as $idx => $option) {
                if (!is_array($option)) {
                    $option = array('content' => $option);
                }
                if (empty($option['value'])) {
                    $option['value'] = $idx;
                }
                if (!empty($attribs['value'])) {
                    if (is_array($attribs['value'])) {
                        $option['selected'] = in_array($option['value'], $attribs['value']);
                    }
                    else if ($attribs['value'] == $option['value']) {
                        $option['selected'] = true;
                    }
                }
                // make a select really readonly by disabling options
                else if (!empty($attribs['disabled']) || !empty($attribs['readonly'])) {
                    $option['disabled'] = true;
                }

                $content[] = self::option($option, $escape);
            }
        }

        $attribs['class'] = trim($attribs['class'] . ' custom-select');

        return sprintf('<select%s>%s</select>',
            self::attrib_string($attribs, $elem_attribs), implode("\n", $content));
    }

    /**
     * Option element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the option tag
     */
    public static function option($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$option_attribs, self::$common_attribs);

        $content = isset($attribs['content']) ? $attribs['content'] : '';

        if ($escape) {
            $content = self::escape($content);
        }

        return sprintf('<option%s>%s</option>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Fieldset element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the fieldset tag
     */
    public static function fieldset($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$common_attribs);

        $legend  = isset($attribs['legend']) ? $attribs['legend'] : $attribs['label'];
        $content = isset($attribs['content']) ? $attribs['content'] : '';

        if ($escape) {
            $legend = self::escape($legend);
        }

        return sprintf('<fieldset%s><legend>%s</legend>%s</fieldset>',
            self::attrib_string($attribs, $elem_attribs), $legend, $content);
    }

    /**
     * Link element (A).
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the link
     */
    public static function a($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$a_attribs, self::$common_attribs, self::$event_attribs);

        $content = isset($attribs['content']) ? $attribs['content'] : '';

        if ($escape) {
            $content = self::escape($content);
        }

        return sprintf('<a%s>%s</a>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Label element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the label tag
     */
    public static function label($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$label_attribs, self::$common_attribs);

        $content = isset($attribs['content']) ? $attribs['content'] : '';

        if ($escape) {
            $content = self::escape($content);
        }

        return sprintf('<label%s>%s</label>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Division element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the div tag
     */
    public static function div($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$common_attribs, self::$event_attribs);

        $content = isset($attribs['content']) ? $attribs['content'] : '';

        if ($escape) {
            $content = self::escape($content);
        }

        return sprintf('<div%s>%s</div>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Span element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the span tag
     */
    public static function span($attribs = array(), $escape = false)
    {
        $elem_attribs = array_merge(self::$common_attribs, self::$event_attribs);

        $content = isset($attribs['content']) ? $attribs['content'] : '';

        if ($escape) {
            $content = self::escape($content);
        }

        return sprintf('<span%s>%s</span>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Form element.
     *
     * @param array  $attribs  Element attributes
     * @param string $escape   Content of the form
     *
     * @return string HTML output of the form tag
     */
    public static function form($attribs = array(), $content = null)
    {
        $elem_attribs = array_merge(self::$form_attribs, self::$common_attribs, self::$event_attribs);

        return sprintf('<form%s>%s</form>',
            self::attrib_string($attribs, $elem_attribs), $content);
    }

    /**
     * Script element.
     *
     * @param array $attribs  Element attributes
     * @param bool  $escape   Enables escaping of the content
     *
     * @return string HTML output of the script tag
     */
    public static function script($content = null, $escape = false)
    {
        if ($escape) {
            $content = self::escape($content);
        }

        return sprintf('<script type="text/javascript">%s</script>', $content);
    }

    /**
     * Create string with attributes
     *
     * @param array $attrib   Associative array with tag attributes
     * @param array $allowed  List of allowed attributes
     *
     * @return string Valid attribute string
     */
    public static function attrib_string($attrib = array(), $allowed = array())
    {
        if (empty($attrib)) {
            return '';
        }

        $allowed    = array_flip((array)$allowed);
        $attrib_arr = array();

        foreach ($attrib as $key => $value) {
            // skip size if not numeric
            if (($key == 'size' && !is_numeric($value))) {
                continue;
            }

            // ignore empty values
            if ($value === null || $value === '') {
                continue;
            }

            // ignore unpermitted attributes, allow "data-"
            if (!empty($allowed) && strpos($key, 'data-') !== 0 && !isset($allowed[$key])) {
                continue;
            }

            // skip empty eventhandlers
            if (preg_match('/^on[a-z]+/', $key) && !$value) {
                continue;
            }

            // boolean attributes
            if (preg_match('/^(checked|multiple|disabled|selected|readonly|autofocus)$/', $key)) {
                if ($value) {
                    $attrib_arr[] = sprintf('%s="%s"', $key, $key);
                }
            }
            // the rest
            else {
                $attrib_arr[] = sprintf('%s="%s"', $key, self::escape($value));
            }
        }

        return count($attrib_arr) ? ' '.implode(' ', $attrib_arr) : '';
    }

    /**
     * Escape special characters into HTML entities.
     *
     * @param string|array $value  Value to escape
     *
     * @return string|array Escaped value
     */
    public static function escape($value)
    {
        if (is_array($value)) {
            foreach ($value as $idx => $val) {
                $value[$idx] = self::escape($val);
            }
            return $value;
        }

        return htmlspecialchars($value, ENT_COMPAT, KADM_CHARSET);
    }
}