-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtml.php
481 lines (426 loc) · 11.2 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
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
<?php namespace Laravel;
class HTML {
/**
* The registered custom macros.
*
* @var array
*/
public static $macros = array();
/**
* Cache application encoding locally to save expensive calls to config::get().
*
* @var string
*/
public static $encoding = null;
/**
* Registers a custom macro.
*
* @param string $name
* @param Closure $macro
* @return void
*/
public static function macro($name, $macro)
{
static::$macros[$name] = $macro;
}
/**
* Convert HTML characters to entities.
*
* The encoding specified in the application configuration file will be used.
*
* @param string $value
* @return string
*/
public static function entities($value)
{
return htmlentities($value, ENT_QUOTES, static::encoding(), false);
}
/**
* Convert entities to HTML characters.
*
* @param string $value
* @return string
*/
public static function decode($value)
{
return html_entity_decode($value, ENT_QUOTES, static::encoding());
}
/**
* Convert HTML special characters.
*
* The encoding specified in the application configuration file will be used.
*
* @param string $value
* @return string
*/
public static function specialchars($value)
{
return htmlspecialchars($value, ENT_QUOTES, static::encoding(), false);
}
/**
* Generate a link to a JavaScript file.
*
* <code>
* // Generate a link to a JavaScript file
* echo HTML::script('js/jquery.js');
*
* // Generate a link to a JavaScript file and add some attributes
* echo HTML::script('js/jquery.js', array('defer'));
* </code>
*
* @param string $url
* @param array $attributes
* @return string
*/
public static function script($url, $attributes = array())
{
$url = URL::to_asset($url);
return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
}
/**
* Generate a link to a CSS file.
*
* If no media type is selected, "all" will be used.
*
* <code>
* // Generate a link to a CSS file
* echo HTML::style('css/common.css');
*
* // Generate a link to a CSS file and add some attributes
* echo HTML::style('css/common.css', array('media' => 'print'));
* </code>
*
* @param string $url
* @param array $attributes
* @return string
*/
public static function style($url, $attributes = array())
{
$defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
$attributes = $attributes + $defaults;
$url = URL::to_asset($url);
return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
}
/**
* Generate a HTML span.
*
* @param string $value
* @param array $attributes
* @return string
*/
public static function span($value, $attributes = array())
{
return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
}
/**
* Generate a HTML link.
*
* <code>
* // Generate a link to a location within the application
* echo HTML::link('user/profile', 'User Profile');
*
* // Generate a link to a location outside of the application
* echo HTML::link('http://google.com', 'Google');
* </code>
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $https
* @return string
*/
public static function link($url, $title = null, $attributes = array(), $https = null)
{
$url = URL::to($url, $https);
if (is_null($title)) $title = $url;
return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
}
/**
* Generate a HTTPS HTML link.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
public static function link_to_secure($url, $title = null, $attributes = array())
{
return static::link($url, $title, $attributes, true);
}
/**
* Generate an HTML link to an asset.
*
* The application index page will not be added to asset links.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $https
* @return string
*/
public static function link_to_asset($url, $title = null, $attributes = array(), $https = null)
{
$url = URL::to_asset($url, $https);
if (is_null($title)) $title = $url;
return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
}
/**
* Generate an HTTPS HTML link to an asset.
*
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
public static function link_to_secure_asset($url, $title = null, $attributes = array())
{
return static::link_to_asset($url, $title, $attributes, true);
}
/**
* Generate an HTML link to a route.
*
* An array of parameters may be specified to fill in URI segment wildcards.
*
* <code>
* // Generate a link to the "profile" named route
* echo HTML::link_to_route('profile', 'Profile');
*
* // Generate a link to the "profile" route and add some parameters
* echo HTML::link_to_route('profile', 'Profile', array('taylor'));
* </code>
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
public static function link_to_route($name, $title = null, $parameters = array(), $attributes = array())
{
return static::link(URL::to_route($name, $parameters), $title, $attributes);
}
/**
* Generate an HTML link to a controller action.
*
* An array of parameters may be specified to fill in URI segment wildcards.
*
* <code>
* // Generate a link to the "home@index" action
* echo HTML::link_to_action('home@index', 'Home');
*
* // Generate a link to the "user@profile" route and add some parameters
* echo HTML::link_to_action('user@profile', 'Profile', array('taylor'));
* </code>
*
* @param string $action
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
public static function link_to_action($action, $title = null, $parameters = array(), $attributes = array())
{
return static::link(URL::to_action($action, $parameters), $title, $attributes);
}
/**
* Generate an HTML link to a different language
*
* @param string $language
* @param string $title
* @param array $attributes
* @return string
*/
public static function link_to_language($language, $title = null, $attributes = array())
{
return static::link(URL::to_language($language), $title, $attributes);
}
/**
* Generate an HTML mailto link.
*
* The E-Mail address will be obfuscated to protect it from spam bots.
*
* @param string $email
* @param string $title
* @param array $attributes
* @return string
*/
public static function mailto($email, $title = null, $attributes = array())
{
$email = static::email($email);
if (is_null($title)) $title = $email;
$email = 'mailto:'.$email;
return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
}
/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
*
* @param string $email
* @return string
*/
public static function email($email)
{
return str_replace('@', '@', static::obfuscate($email));
}
/**
* Generate an HTML image element.
*
* @param string $url
* @param string $alt
* @param array $attributes
* @return string
*/
public static function image($url, $alt = '', $attributes = array())
{
$attributes['alt'] = $alt;
return '<img src="'.URL::to_asset($url).'"'.static::attributes($attributes).'>';
}
/**
* Generate an ordered list of items.
*
* @param array $list
* @param array $attributes
* @return string
*/
public static function ol($list, $attributes = array())
{
return static::listing('ol', $list, $attributes);
}
/**
* Generate an un-ordered list of items.
*
* @param array $list
* @param array $attributes
* @return string
*/
public static function ul($list, $attributes = array())
{
return static::listing('ul', $list, $attributes);
}
/**
* Generate an ordered or un-ordered list.
*
* @param string $type
* @param array $list
* @param array $attributes
* @return string
*/
private static function listing($type, $list, $attributes = array())
{
$html = '';
if (count($list) == 0) return $html;
foreach ($list as $key => $value)
{
// If the value is an array, we will recurse the function so that we can
// produce a nested list within the list being built. Of course, nested
// lists may exist within nested lists, etc.
if (is_array($value))
{
if (is_int($key))
{
$html .= static::listing($type, $value);
}
else
{
$html .= '<li>'.$key.static::listing($type, $value).'</li>';
}
}
else
{
$html .= '<li>'.static::entities($value).'</li>';
}
}
return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
}
/**
* Generate a definition list.
*
* @param array $list
* @param array $attributes
* @return string
*/
public static function dl($list, $attributes = array())
{
$html = '';
if (count($list) == 0) return $html;
foreach ($list as $term => $description)
{
$html .= '<dt>'.static::entities($term).'</dt>';
$html .= '<dd>'.static::entities($description).'</dd>';
}
return '<dl'.static::attributes($attributes).'>'.$html.'</dl>';
}
/**
* Build a list of HTML attributes from an array.
*
* @param array $attributes
* @return string
*/
public static function attributes($attributes)
{
$html = array();
foreach ((array) $attributes as $key => $value)
{
// For numeric keys, we will assume that the key and the value are the
// same, as this will convert HTML attributes such as "required" that
// may be specified as required="required", etc.
if (is_numeric($key)) $key = $value;
if ( ! is_null($value))
{
$html[] = $key.'="'.static::entities($value).'"';
}
}
return (count($html) > 0) ? ' '.implode(' ', $html) : '';
}
/**
* Obfuscate a string to prevent spam-bots from sniffing it.
*
* @param string $value
* @return string
*/
protected static function obfuscate($value)
{
$safe = '';
foreach (str_split($value) as $letter)
{
// To properly obfuscate the value, we will randomly convert each
// letter to its entity or hexadecimal representation, keeping a
// bot from sniffing the randomly obfuscated letters.
switch (rand(1, 3))
{
case 1:
$safe .= '&#'.ord($letter).';';
break;
case 2:
$safe .= '&#x'.dechex(ord($letter)).';';
break;
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Get the appliction.encoding without needing to request it from Config::get() each time.
*
* @return string
*/
protected static function encoding()
{
return static::$encoding ?: static::$encoding = Config::get('application.encoding');
}
/**
* Dynamically handle calls to custom macros.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
if (isset(static::$macros[$method]))
{
return call_user_func_array(static::$macros[$method], $parameters);
}
throw new \Exception("Method [$method] does not exist.");
}
}