/
inc.tpl.php
103 lines (77 loc) · 2.04 KB
/
inc.tpl.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
<?php
function postTitleToHtml($post) {
// HTML-ize
$html = nl2br(h(trim($post->title)));
// Insert inner links
$html = preg_replace('#(https?://[^\s<]+)#', '</a><a href="$1">$1</a><a href="' . h($post->url) . '">', $html);
// Wrap with outer link
$html = '<a href="' . h($post->url) . '">' . $html . '</a>';
// Remove empty links
$html = preg_replace_callback('#<a[\s\S]+?>([\s\S]*?)</a>#', function($match) {
list($match, $label) = $match;
// No contents
if (!trim($label)) {
return $label;
}
// Only newlines
if (preg_match('#^(<br />\s*)+$#', trim($label))) {
return $label;
}
// Valid contents
return $match;
}, $html);
return $html;
}
function formError($error) {
return $error ? 'error' : 'valid';
}
function ul($items) {
return '<ul><li>' . implode('</li><li>', $items) . '</li></ul>';
}
function attr($attr, $except = array()) {
$html = '';
foreach ( $attr AS $name => $value ) {
if ( !in_array($name, $except) ) {
$html .= ' ' . $name . '="' . h($value) . '"';
}
}
return $html;
}
function redirect($uri = null) {
if ( null !== $uri ) {
$uri = u($uri);
}
else {
$uri = $_SERVER['REQUEST_URI'];
/*if ( is_int($p = strpos($uri, '?')) ) {
$uri = substr($uri, 0, $p);
}*/
}
header('Location: ' . $uri);
exit;
}
function l($label, $uri, $options = array()) {
!empty($options['html']) or $label = h($label);
$attr = attr($options, array('attributes', 'html', 'query'));
return '<a' . $attr . ' href="' . u($uri) . '">' . $label . '</a>';
}
function baseUrl() {
$dir = dirname(__FILE__);
$docroot = $_SERVER['DOCUMENT_ROOT'];
$doc = (string)substr($dir, strlen($docroot));
$base = '/' . str_replace('\\', '/', $doc) . '/';
$base = str_replace('//', '/', $base);
return $base;
}
function u($uri, $options = array()) {
$base = baseUrl();
if ( 0 !== strpos($uri, 'http') && 0 !== strpos($uri, '/') ) {
if ( @$options['absolute'] ) {
$uri = 'http://' . $_SERVER['HTTP_HOST'] . $uri;
}
}
return $uri;
}
function h($str) {
return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
}