-
Notifications
You must be signed in to change notification settings - Fork 0
/
Html.php
310 lines (274 loc) · 8.95 KB
/
Html.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
<?php
/**
* @package Flextype Components
*
* @author Sergey Romanenko <awilum@yandex.ru>
* @link http://components.flextype.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flextype\Component\Html;
class Html
{
/**
* Preferred order of attributes
*
* @var array
*/
public static $attribute_order = [
'action', 'method', 'type', 'id', 'name', 'value',
'href', 'src', 'width', 'height', 'cols', 'rows',
'size', 'maxlength', 'rel', 'media', 'accept-charset',
'accept', 'tabindex', 'accesskey', 'alt', 'title', 'class',
'style', 'selected', 'checked', 'readonly', 'disabled',
];
/**
* The registered custom macros.
*
* @var array
*/
public static $macros = [];
/**
* Registers a custom macro.
*
* // Registering a Html macro
* Html::macro('my_element', function() {
* return '<element id="flextype">';
* });
*
* // Calling a custom Html macro
* echo Html::my_element();
*
*
* // Registering a Html macro with parameters
* Html::macro('my_element', function($id = '') {
* return '<element id="'.$id.'">';
* });
*
* // Calling a custom Html macro with parameters
* echo Html::my_element('flextype');
*
* @param string $name Name
* @param Closure $macro Macro
*/
public static function macro(string $name, $macro)
{
Html::$macros[$name] = $macro;
}
/**
* Convert special characters to HTML entities. All untrusted content
* should be passed through this method to prevent XSS injections.
*
* echo Html::toText('test');
*
* @param string $value String to convert
* @param bool $double_encode Encode existing entities
* @return string
*/
public static function toText(string $value, bool $double_encode = true) : string
{
return htmlspecialchars($value, ENT_QUOTES, 'utf-8', $double_encode);
}
/**
* Compiles an array of HTML attributes into an attribute string.
* Attributes will be sorted using Html::$attribute_order for consistency.
*
* echo '<div'.Html::attributes($attrs).'>'.$content.'</div>';
*
* @param array $attributes Attribute list
* @return string
*/
public static function attributes(array $attributes = null) : string
{
if (empty($attributes)) return '';
// Init var
$sorted = [];
foreach (Html::$attribute_order as $key) {
if (isset($attributes[$key])) {
// Add the attribute to the sorted list
$sorted[$key] = $attributes[$key];
}
}
// Combine the sorted attributes
$attributes = $sorted + $attributes;
$compiled = '';
foreach ($attributes as $key => $value) {
if ($value === NULL) {
// Skip attributes that have NULL values
continue;
}
if (is_int($key)) {
// Assume non-associative keys are mirrored attributes
$key = $value;
}
// Add the attribute value
$compiled .= ' '.$key.'="'.Html::toText($value).'"';
}
return $compiled;
}
/**
* Create br tags
*
* echo Html::br(2);
*
* @param int $num Count of line break tag
* @return string
*/
public static function br(int $num = 1) : string
{
return str_repeat("<br>", $num);
}
/**
* Create
*
* echo Html::nbsp(2);
*
* @param int $num Count of
* @return string
*/
public static function nbsp(int $num = 1) : string
{
return str_repeat(" ", $num);
}
/**
* Create an arrow
*
* echo Html::arrow('right');
*
* @param string $direction Arrow direction [up,down,left,right]
* @return string
*/
public static function arrow(string $direction) : string
{
switch ($direction) {
case "up": $output = '<span class="arrow">↑</span>'; break;
case "down": $output = '<span class="arrow">↓</span>'; break;
case "left": $output = '<span class="arrow">←</span>'; break;
case "right": $output = '<span class="arrow">→</span>'; break;
}
return $output;
}
/**
* Create HTML link anchor.
*
* echo Html::anchor('About', 'http://sitename.com/about');
*
* @param string $title Anchor title
* @param string $url Anchor url
* @param array $attributes Anchor attributes
* @return string
*/
public static function anchor(string $title, string $url = '', array $attributes = null) : string
{
// Add link
if ($url !== '') $attributes['href'] = $url;
return '<a'.Html::attributes($attributes).'>'.$title.'</a>';
}
/**
* Create HTML <h> tag
*
* echo Html::heading('Title', 1);
*
* @param string $title Text
* @param int $h Number [1-6]
* @param array $attributes Heading attributes
* @return string
*/
public static function heading(string $title, int $h = 1, array $attributes = null) : string
{
$output = '<h'.$h.Html::attributes($attributes).'>'.$title.'</h'.$h.'>';
return $output;
}
/**
* Generate document type declarations
*
* echo Html::doctype('html5');
*
* @param string $type Doctype to generated
* @return mixed
*/
public static function doctype(string $type = 'html5')
{
$doctypes = ['xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
'xhtml1-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
'xhtml1-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
'xhtml1-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
'html5' => '<!DOCTYPE html>',
'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'];
if (isset($doctypes[$type])) return $doctypes[$type]; else return false;
}
/**
* Create image
*
* echo Html::image('data/files/pic1.jpg');
*
* @param string $file File
* @param array $attributes Image attributes
* @return string
*/
public static function image(string $file, array $attributes = null) : string
{
// Add the image link
$attributes['src'] = $file;
$attributes['alt'] = (isset($attributes['alt'])) ? $attributes['alt'] : pathinfo($file, PATHINFO_FILENAME);
return '<img'.Html::attributes($attributes).'>';
}
/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
*
* echo Html::email('hello@flextype.org');
*
* @param string $email
* @return string
*/
public static function email(string $email) : string
{
return str_replace('@', '@', Html::obfuscate($email));
}
/**
* Obfuscate a string to prevent spam-bots from sniffing it.
*
* This method obfuscate the value, randomly convert each
* letter to its entity or hexadecimal representation, keeping a
* bot from sniffing the randomly obfuscated letters.
*
* echo Html::obfuscate('hello@flextype.org');
*
* @param string $value
* @return string
*/
public static function obfuscate(string $value) : string
{
$safe = '';
foreach (str_split($value) as $letter) {
switch (rand(1, 3)) {
case 1:
$safe .= '&#'.ord($letter).';';
break;
case 2:
$safe .= '&#x'.dechex(ord($letter)).';';
break;
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Dynamically handle calls to custom macros.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic(string $method, array $parameters)
{
if (isset(Html::$macros[$method])) {
return call_user_func_array(Html::$macros[$method], $parameters);
}
throw new RuntimeException("Method [$method] does not exist.");
}
}