forked from k0a1a/hotglue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.inc.php
732 lines (664 loc) · 17.5 KB
/
html.inc.php
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/*
* html.inc.php
* Generic html element functions
*
* Copyright Gottfried Haider, Danja Vasiliev 2010.
* This source code is licensed under the GNU General Public License.
* See the file COPYING for more details.
*/
@require_once('config.inc.php');
require_once('util.inc.php');
$single_tags = array('br', 'hr', 'img', 'input', 'link', 'meta', 'param');
if (!isset($html)) {
html_flush();
}
/**
* helper function for sorting an array of arrays by key 'prio'
*
* @param array &$a reference to an array
*/
function _array_sort_by_prio(&$a)
{
// usort makes no guarantee what happens when two entries have the same
// prio
usort($a, '_cmp_prio');
}
/**
* helper function for _array_sort_prio()
*
* @param array $a array to compare
* @param array $b array to compare
* @return int comparison result
*/
function _cmp_prio($a, $b)
{
if ($a['prio'] == $b['prio']) {
return 0;
}
return ($a['prio'] < $b['prio']) ? -1 : 1;
}
/**
* get a reference to the body element
*
* @return &array reference to the body element
*/
function &body()
{
global $html;
return $html['body'];
}
/**
* helper function for appending content to the body element
*
* @param mixed $c content (can be either a string or another element)
*/
function body_append($c)
{
global $html;
elem_append($html['body'], $c);
}
/**
* create a element
*
* @param string $tag element tag
* @return array element
*/
function elem($tag)
{
return array('tag'=>$tag);
}
/**
* add a class to an element
*
* @param array &$elem reference to an element
* @param string $c class
*/
function elem_add_class(&$elem, $c)
{
if (!@is_array($elem['class'])) {
$elem['class'] = array();
}
$elem['class'][] = $c;
$elem['class'] = array_unique($elem['class']);
}
/**
* append content to an element
*
* this function is similar to elem_val().
* @param array &$elem reference to an element
* @param mixed $c content (can be either a string or another element)
*/
function elem_append(&$elem, $c)
{
if (!isset($elem['val'])) {
if (is_array($c)) {
$elem['val'] = array($c);
} else {
$elem['val'] = $c;
}
} elseif (is_array($c) && is_array($elem['val'])) {
$elem['val'][] = $c;
} elseif (is_array($c) && is_string($elem['val'])) {
$elem['val'] = array($elem['val'], $c);
} elseif (is_string($c) && is_array($elem['val'])) {
$elem['val'][] = $c;
} elseif (is_string($c) && is_string($elem['val'])) {
$elem['val'] .= $c;
}
}
/**
* get or set an attribute in an element
*
* @param array &$elem reference to an element
* @param string attribute name
* @param mixed attribute value (to set it)
*/
function elem_attr(&$elem)
{
if (func_num_args() == 2) {
if (isset($elem[func_get_arg(1)])) {
return $elem[func_get_arg(1)];
} else {
return NULL;
}
} elseif (2 < func_num_args()) {
$elem[func_get_arg(1)] = func_get_arg(2);
}
}
/**
* get the element's classes in an array
*
* @param array $elem element
* @return array
*/
function elem_classes($elem)
{
if (@is_array($elem['class'])) {
return $elem['class'];
} else {
return array();
}
}
/**
* get or set a css property in an element
*
* @param array &$elem reference to an element
* @param string css property name
* @param mixed css property value (to set it; empty string to clear it)
*/
function elem_css(&$elem)
{
if (func_num_args() == 2) {
if (@is_array($elem['style']) && isset($elem['style'][func_get_arg(1)])) {
return $elem['style'][func_get_arg(1)];
} else {
return NULL;
}
} elseif (2 < func_num_args()) {
if (!@is_array($elem['style'])) {
$elem['style'] = array();
}
if (func_get_arg(2) === '') {
// clear css property
unset($elem['style'][func_get_arg(1)]);
} else {
$elem['style'][func_get_arg(1)] = func_get_arg(2);
}
}
}
/**
* turn an element into a html string
*
* @param array $elem element
* @return string html
*/
function elem_finalize($elem)
{
global $single_tags;
$ret = '<'.$elem['tag'];
if (isset($elem['id'])) {
$ret .= ' id="'.htmlspecialchars($elem['id'], ENT_COMPAT, 'UTF-8').'"';
unset($elem['id']);
}
if (@is_array($elem['class'])) {
$ret .= ' class="'.htmlspecialchars(implode(' ', $elem['class']), ENT_COMPAT, 'UTF-8').'"';
unset($elem['class']);
}
foreach ($elem as $key=>$val) {
if ($key == 'tag' || $key == 'id' || $key == 'class' || $key == 'val' || $key == 'body_inline') {
continue;
} elseif ($key == 'style') {
$ret .= ' style="';
ksort($val);
foreach ($val as $k=>$v) {
$ret .= htmlspecialchars($k, ENT_COMPAT, 'UTF-8').': '.htmlspecialchars($v, ENT_COMPAT, 'UTF-8').'; ';
}
// strip the last space
$ret = substr($ret, 0, -1);
$ret .= '"';
} else {
$ret .= ' '.htmlspecialchars($key, ENT_NOQUOTES, 'UTF-8').'="'.htmlspecialchars($val, ENT_COMPAT, 'UTF-8').'"';
}
}
$ret .= '>';
// make block elements have a newline after the opening tag
$block_tags = array('blockquote', 'body', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'li', 'ol', 'p', 'pre', 'script', 'ul');
if (in_array($elem['tag'], $block_tags)) {
$block_tag = true;
$ret .= nl();
} else {
$block_tag = false;
}
// handle single-type elements
if (in_array(strtolower($elem['tag']), $single_tags)) {
return $ret;
}
// handle text, an element array or an array of both
$content = '';
if (@is_string($elem['val'])) {
$content = $elem['val'];
} elseif (@is_array($elem['val']) && isset($elem['val']['tag'])) {
// this is recursive
$content = elem_finalize($elem['val']);
} elseif (@is_array($elem['val'])) {
foreach ($elem['val'] as $v) {
if (is_string($v)) {
$content .= $v;
} elseif (is_array($v) && isset($v['tag'])) {
// add a newline when appropriate
if (in_array($v['tag'], $block_tags) && 0 < strlen($content) && substr($content, -1) != "\n") {
$content .= nl();
}
// this is recursive
$content .= elem_finalize($v);
}
}
}
// move block element content in by one tab
if ($block_tag && 0 < strlen($content)) {
// if the content ends with a newline character, remove it
if (substr($content, -1) == "\n") {
$content = substr($content, 0, -1);
}
$content = str_replace("\n", "\n\t", $content);
$ret .= tab().$content.nl();
} elseif (0 < strlen($content)) {
$ret .= $content;
}
$ret .= '</'.$elem['tag'].'>';
// make block elements have a newline after the closing tag
if ($block_tag) {
$ret .= nl();
}
return $ret;
}
/**
* check if an element is of a certain class
*
* @param array $elem element
* @param string $c class to check
* @return bool
*/
function elem_has_class($elem, $c)
{
if (@in_array($c, $elem['class'])) {
return true;
} else {
return false;
}
}
/**
* remove an attribute from an element
*
* @param array &$elem reference to an element
* @param string $a attribute name
*/
function elem_remove_attr(&$elem, $a)
{
unset($elem[$a]);
}
/**
* remove a class from an element
*
* @param array &$elem reference to an element
* @param string $c class
*/
function elem_remove_class(&$elem, $c)
{
if (@is_array($elem['class'])) {
if (($k = array_search($c, $elem['class'])) !== false) {
array_splice($elem['class'], $k, 1);
}
}
}
/**
* get the element's tag
*
* the tag is always returned in lowercase characters.
* @param array $elem element
* @return string
*/
function elem_tag($elem)
{
if (isset($elem['tag'])) {
return strtolower($elem['tag']);
} else {
return '';
}
}
/**
* get or set an element's content
*
* this function is similar to elem_append().
* @param array &$elem reference to an element
* @param mixed $c content (to set it, can be either a string or another element)
*/
function elem_val(&$elem)
{
if (func_num_args() == 1) {
if (@is_string($elem['val'])) {
return $elem['val'];
} else {
return '';
}
} elseif (1 < func_num_args()) {
$elem['val'] = func_get_arg(1);
}
}
/**
* add a link-alternate element to the html header
*
* @param string $type type attribute
* @param string $url url attribute (url-encoded if necessary)
* @param string $title title attribute
*/
function html_add_alternate($type, $url, $title)
{
global $html;
if (!@is_array($html['header']['alternate'])) {
$html['header']['alternate'] = array();
}
$html['header']['alternate'][] = array('type'=>$type, 'url'=>$url, 'title'=>$title);
}
/**
* add a reference to a css file to the html header
*
* @param string $url url attribute (url-encoded if necessary)
* @param int $prio when to insert reference (0 - very early to 9 - late)
* @param string $media media attribute (optional)
*/
function html_add_css($url, $prio = 5, $media = '')
{
global $html;
if (!@is_array($html['header']['css'])) {
$html['header']['css'] = array();
}
$html['header']['css'][] = array('url'=>$url, 'prio'=>$prio, 'media'=>$media);
}
/**
* add a css rule to the html header
*
* @param string $rule css rule
* @param int $prio when to insert code (0 - very early to 9 - late)
*/
function html_add_css_inline($rule, $prio = 5)
{
global $html;
if (!@is_array($html['header']['css_inline'])) {
$html['header']['css_inline'] = array();
}
$html['header']['css_inline'][] = array('rule'=>$rule, 'prio'=>$prio);
}
/**
* add head definitions to the html header
*
* @param string $def heade definition
* @param int $prio when to insert code (0 - very early to 9 - late)
*/
function html_add_head_inline($def, $prio = 5)
{
global $html;
if (!@is_array($html['header']['head_inline'])) {
$html['header']['head_inline'] = array();
}
$html['header']['head_inline'][] = array('def'=>$def, 'prio'=>$prio);
}
/**
* add body definitions to the html body
*
* @param string $def heade definition
* @param int $prio when to insert code (0 - very early to 9 - late)
*/
function html_add_body_inline($def, $prio = 5)
{
global $html;
if (!@is_array($html['body']['body_inline'])) {
$html['body']['body_inline'] = array();
}
$html['body']['body_inline'][] = array('def'=>$def, 'prio'=>$prio);
}
/**
* add a reference to a javascript file to the html header
*
* duplicate references will be removed from the output.
* @param string $url url attribute (url-encoded if necessary)
* @param int $prio when to insert reference (0 - very early to 9 - late)
*/
function html_add_js($url, $prio = 5)
{
global $html;
if (!@is_array($html['header']['js'])) {
$html['header']['js'] = array();
}
$html['header']['js'][] = array('url'=>$url, 'prio'=>$prio);
}
/**
* add javascript code to the html header
*
* @param string $code javscript code
* @param int $prio when to insert code (0 - very early to 9 - late)
* @param string $reason (e.g. your module) (optional)
*/
function html_add_js_inline($code, $prio = 5, $reason = '')
{
global $html;
if (!@is_array($html['header']['js_inline'])) {
$html['header']['js_inline'] = array();
}
$html['header']['js_inline'][] = array('code'=>$code, 'prio'=>$prio, 'reason'=>$reason);
}
/**
* set a variable in the javascript output
*
* @param string $key variable or object the value will be stored)
* @param mixed $val value
*/
function html_add_js_var($key, $val)
{
global $html;
if (!@is_array($html['header']['js_var'])) {
$html['header']['js_var'] = array();
}
$html['header']['js_var'][$key] = $val;
}
/**
* get or set a css property in the html element
*
* @param string $prop css property name
* @param mixed css property value (to set it; empty string to clear it)
*/
function html_css($prop)
{
global $html;
if (func_num_args() == 1) {
if (@is_array($html['header']['style']) && isset($html['header']['style'][$prop])) {
return $html['header']['style'][$prop];
} else {
return NULL;
}
} elseif (1 < func_num_args()) {
if (!@is_array($html['header']['style'])) {
$html['header']['style'] = array();
}
if (func_get_arg(1) === '') {
// clear css property
unset($html['header']['style'][$prop]);
} else {
$html['header']['style'][$prop] = func_get_arg(1);
}
}
}
/**
* disable caching of output
*
* can be used for modules that need the php to be executed every time.
* @param string $reason (e.g. your module)
*/
function html_disable_caching($reason = '')
{
global $html;
if ($html['cache']) {
log_msg('info', 'html: disabled caching for this request because of '.quot($reason));
$html['cache'] = false;
}
}
/**
* get or set favicon
*
* @param string url (to set it, url-encoded if necessary)
*/
function html_favicon()
{
global $html;
if (func_num_args() == 0) {
if (@is_string($html['header']['favicon'])) {
return $html['header']['favicon'];
} else {
return '';
}
} elseif (0 < func_num_args()) {
$html['header']['favicon'] = func_get_arg(0);
}
}
/**
* turn the page into a html string
*
* @param bool &$cache is output cachable (will only modified if $cache is
* true before)
* @return string html
*/
function html_finalize(&$cache = false)
{
global $html;
// return html5
$ret = '<!DOCTYPE html>'.nl();
$ret .= '<html';
if (@is_array($html['header']['style'])) {
$ret .= ' style="';
ksort($html['header']['style']);
foreach ($html['header']['style'] as $key=>$val) {
$ret .= htmlspecialchars($key, ENT_COMPAT, 'UTF-8').': '.htmlspecialchars($val, ENT_COMPAT, 'UTF-8').'; ';
}
// strip the last space
$ret = substr($ret, 0, -1);
$ret .= '"';
}
$ret .= '>'.nl();
$ret .= '<head>'.nl();
$ret .= '<title>'.htmlspecialchars($html['header']['title'], ENT_NOQUOTES, 'UTF-8').'</title>'.nl();
$ret .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'.nl();
if (@is_array($html['header']['alternate'])) {
foreach ($html['header']['alternate'] as $e) {
$ret .= '<link rel="alternate" type="'.htmlspecialchars($e['type'], ENT_COMPAT, 'UTF-8').'" href="'.htmlspecialchars($e['url'], ENT_COMPAT, 'UTF-8').'" title="'.htmlspecialchars($e['title'], ENT_COMPAT, 'UTF-8').'">'.nl();
}
}
if (!empty($html['header']['favicon'])) {
$ret .= '<link rel="shortcut icon" href="'.htmlspecialchars($html['header']['favicon'], ENT_COMPAT, 'UTF-8').'">'.nl();
}
if (@is_array($html['header']['css'])) {
_array_sort_by_prio($html['header']['css']);
// removed the removal of duplicates here as two different media might point to the same url
//array_unique_element($html['header']['css'], 'url');
foreach ($html['header']['css'] as $e) {
$ret .= '<link rel="stylesheet" type="text/css" href="'.htmlspecialchars($e['url'], ENT_COMPAT, 'UTF-8').'"';
if (!empty($e['media'])) {
$ret .= ' media="'.htmlspecialchars($e['media'], ENT_COMPAT, 'UTF-8').'"';
}
$ret .= '>'.nl();
}
}
if (@is_array($html['header']['css_inline'])) {
_array_sort_by_prio($html['header']['css_inline']);
if (0 < count($html['header']['css_inline'])) {
$ret .= '<style type="text/css">'.nl();
}
foreach ($html['header']['css_inline'] as $c) {
$rule = $c['rule'];
// if the rule ends with a newline character, remove it
if (substr($rule, -1) == "\n") {
$rule = substr($rule, 0, -1);
}
// move rule in by one tab
$rule = str_replace("\n", "\n\t", $rule);
$ret .= tab().$rule.nl();
}
if (0 < count($html['header']['css_inline'])) {
$ret .= '</style>'.nl();
}
}
if (@is_array($html['header']['js'])) {
_array_sort_by_prio($html['header']['js']);
array_unique_element($html['header']['js'], 'url');
foreach ($html['header']['js'] as $e) {
$ret .= '<script type="text/javascript" src="'.htmlspecialchars($e['url'], ENT_COMPAT, 'UTF-8').'"></script>'.nl();
}
}
if (@is_array($html['header']['js_var'])) {
$ret .= array_to_js($html['header']['js_var']);
}
if (@is_array($html['header']['js_inline'])) {
_array_sort_by_prio($html['header']['js_inline']);
foreach ($html['header']['js_inline'] as $c) {
if (!empty($c['reason'])) {
$ret .= '<!-- '.$c['reason'].' -->'.nl();
$ret .= '<script type="text/javascript">'.nl();
// if the code ends with a newline character, remove it
if (substr($c['code'], -1) == "\n") {
$c['code'] = substr($c['code'], 0, -1);
}
// move code in by one tab
$c = str_replace("\n", "\n\t", $c);
$ret .= tab().$c['code'].nl();
$ret .= '</script>'.nl();
}
}
}
if (@is_array($html['header']['head_inline'])) {
_array_sort_by_prio($html['header']['head_inline']);
if (0 < count($html['header']['head_inline'])) {
$ret .= '<!-- user HEAD definitions -->'.nl();
}
foreach ($html['header']['head_inline'] as $c) {
$def = $c['def'];
// if the definition ends with a newline character, remove it
if (substr($def, -1) == "\n") {
$def = substr($def, 0, -1);
}
// $rule = str_replace("\n", "\n\t", $def);
$ret .= $def.nl();
}
}
$ret .= '</head>'.nl();
// load user body definitions
if (@is_array($html['body']['body_inline'])) {
_array_sort_by_prio($html['body']['body_inline']);
if (0 < count($html['body']['body_inline'])) {
$user_body = '<!-- user BODY definitions -->'.nl();
}
foreach ($html['body']['body_inline'] as $c) {
$def = $c['def'];
// if the definition ends with a newline character, remove it
if (substr($def, -1) == "\n") {
$def = substr($def, 0, -1);
}
$rule = str_replace("\n", "\n\t", $def);
$user_body .= $def.nl();
}
body_append($user_body);
}
$ret .= elem_finalize($html['body']);
$ret .= '</html>';
// pass caching information up if requested
if ($cache) {
if (!$html['cache']) {
$cache = false;
}
}
return $ret;
}
/**
* reset the html output
*/
function html_flush()
{
global $html;
$html = array();
$html['header'] = array('title'=>'');
$html['body'] = array('tag'=>'body');
$html['cache'] = true;
}
/**
* get or set title
*
* @param string title (to set it)
*/
function html_title()
{
global $html;
if (func_num_args() == 0) {
return $html['header']['title'];
} elseif (0 < func_num_args()) {
$html['header']['title'] = func_get_arg(0);
}
}