|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Html; |
|
26
|
|
|
|
|
27
|
|
|
defined('APP_ROOT') || die(); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Esta clase es la encargada de mostrar el HTML |
|
31
|
|
|
*/ |
|
32
|
|
|
final class Html |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Limpia los datos recibidos de un formulario. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $data con los datos a limpiar |
|
38
|
|
|
* |
|
39
|
|
|
* @return false|string con los datos limpiados |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function sanitize(&$data) |
|
42
|
|
|
{ |
|
43
|
|
|
if (empty($data)) { |
|
44
|
|
|
return $data; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (is_array($data)) { |
|
|
|
|
|
|
48
|
|
|
array_walk_recursive($data, '\SP\Html\Html::sanitize'); |
|
49
|
|
|
} else { |
|
50
|
|
|
$data = strip_tags($data); |
|
51
|
|
|
|
|
52
|
|
|
// Fix &entity\n; |
|
53
|
|
|
$data = str_replace(['&', '<', '>'], ['&amp;', '&lt;', '&gt;'], $data); |
|
54
|
|
|
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data); |
|
55
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
56
|
|
|
'/(&#x*[0-9A-F]+);*/iu', '$1;', $data); |
|
57
|
|
|
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8'); |
|
58
|
|
|
|
|
59
|
|
|
// Remove any attribute starting with "on" or xmlns |
|
60
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
61
|
|
|
'#(<[^>]+?[\x00-\x20\x2f"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data); |
|
62
|
|
|
|
|
63
|
|
|
// Remove javascript: and vbscript: protocols |
|
64
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
65
|
|
|
'#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data); |
|
66
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
67
|
|
|
'#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data); |
|
68
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
69
|
|
|
'#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data); |
|
70
|
|
|
|
|
71
|
|
|
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span> |
|
72
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
73
|
|
|
'#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data); |
|
74
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
75
|
|
|
'#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data); |
|
76
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
77
|
|
|
'#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data); |
|
78
|
|
|
|
|
79
|
|
|
// Remove namespaced elements (we do not need them) |
|
80
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
81
|
|
|
'#</*\w+:\w[^>]*+>#i', '', $data); |
|
82
|
|
|
|
|
83
|
|
|
do { |
|
84
|
|
|
// Remove really unwanted tags |
|
85
|
|
|
$old_data = $data; |
|
86
|
|
|
$data = preg_replace(/** @lang RegExp */ |
|
87
|
|
|
'#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data); |
|
88
|
|
|
} while ($old_data !== $data); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $data; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Truncar un texto a una determinada longitud. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $text la cadena a truncar |
|
98
|
|
|
* @param int $limit la longitud máxima de la cadena |
|
99
|
|
|
* @param string $ellipsis |
|
100
|
|
|
* |
|
101
|
|
|
* @return string con el texto truncado |
|
102
|
|
|
* |
|
103
|
|
|
* @link http://www.pjgalbraith.com/truncating-text-html-with-php/ |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function truncate($text, $limit, $ellipsis = '...') |
|
106
|
|
|
{ |
|
107
|
|
|
if (mb_strlen($text) > $limit) { |
|
108
|
|
|
return trim(mb_substr($text, 0, $limit)) . $ellipsis; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $text; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Convertir un color RGB a HEX |
|
116
|
|
|
* From: http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/ |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $rgb con color en RGB |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function rgb2hex(array $rgb) |
|
123
|
|
|
{ |
|
124
|
|
|
$hex = "#"; |
|
125
|
|
|
|
|
126
|
|
|
foreach ($rgb as $val) { |
|
127
|
|
|
$hex .= str_pad(dechex($val), 2, "0", STR_PAD_LEFT); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $hex; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Devolver una cadena con el tag HTML strong. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $text con la cadena de texto |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function strongText($text) |
|
141
|
|
|
{ |
|
142
|
|
|
return '<strong>' . $text . '</strong>'; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Devolver un link HTML. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $text con la cadena de texto |
|
149
|
|
|
* @param string $link con el destino del enlace |
|
150
|
|
|
* @param string $title con el título del enlace |
|
151
|
|
|
* @param string $attribs con atributos del enlace |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function anchorText($text, $link = null, $title = null, $attribs = '') |
|
156
|
|
|
{ |
|
157
|
|
|
$alink = $link !== null ? $link : $text; |
|
158
|
|
|
$atitle = $title !== null ? $title : $text; |
|
159
|
|
|
|
|
160
|
|
|
return sprintf('<a href="%s" title="%s" %s>%s</a>', $alink, $atitle, $attribs, $text); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|