-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
51 lines (46 loc) · 1.8 KB
/
functions.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
<?php
if (!function_exists('h')) {
/**
* Convenience method for htmlspecialchars.
*
* @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
* Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
* implement a `__toString` method. Otherwise the class name will be used.
* @param bool $double Encode existing html entities.
* @param string|null $charset Character set to use when escaping. Defaults to config value in `mb_internal_encoding()`
* or 'UTF-8'.
* @return string Wrapped text.
* @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#h
*/
function h($text, $double = true, $charset = null)
{
if (is_string($text)) {
//optimize for strings
} elseif (is_array($text)) {
$texts = [];
foreach ($text as $k => $t) {
$texts[$k] = h($t, $double, $charset);
}
return $texts;
} elseif (is_object($text)) {
if (method_exists($text, '__toString')) {
$text = (string)$text;
} else {
$text = '(object)' . get_class($text);
}
} elseif (is_bool($text) || is_null($text) || is_int($text)) {
return $text;
}
static $defaultCharset = false;
if ($defaultCharset === false) {
$defaultCharset = mb_internal_encoding();
if ($defaultCharset === null) {
$defaultCharset = 'UTF-8';
}
}
if (is_string($double)) {
$charset = $double;
}
return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, $charset ?: $defaultCharset, $double);
}
}