Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sanitizer.php
1<?
9
11
12Loc::loadMessages(__FILE__);
13
19{
26 public static function clean($html)
27 {
28 $tags = self::getTags() + array(
29 'html' => array('xmlns'),
30 'head' => array(),
31 'body' => array(),
32 'meta' => array('content', 'name', 'http-equiv'),
33 'title' => array(),
34 'style' => array(Editor::STYLIST_TAG_ATTR, 'type'),
35 'link' => array('type', 'rel', 'href'),
36 );
37
38 $commonAttributes = self::getCommonAttributes();
39 foreach ($tags as $tagName => $attributes)
40 {
41 $tags[$tagName] = array_merge($attributes, $commonAttributes);
42 }
43
44 $sanitizer = new \CBXSanitizer();
45 $sanitizer->setLevel(\CBXSanitizer::SECURE_LEVEL_LOW);
46 $sanitizer->addTags($tags);
47 $sanitizer->allowAttributes([
49 'tag' => function ()
50 {
51 return true;
52 },
53 'content' => function ()
54 {
55 return true;
56 },
57 ]
58 ]);
59 $sanitizer->applyDoubleEncode(false);
60
61 $storedMap = self::replacePhpToTags($html);
62 $html = $sanitizer->sanitizeHtml($html);
63 self::replaceTagsToPhp($html, $storedMap);
64
65 return $html;
66 }
67
68 protected static function getCommonAttributes()
69 {
70 return array(
72 'style', 'id', 'class', 'color', 'align', 'valign',
73 'height', 'width', 'title', 'style', 'class',
74 'dir', 'role',
76 'data-bx-block-editor-block-type'
77 );
78 }
79
80 protected static function getTags()
81 {
82 $tags = array(
83 'a' => array('href', 'title','name','style','id','class','shape','coords','alt','target'),
84 'b' => array('style','id','class'),
85 'br' => array('style','id','class'),
86 'big' => array('style','id','class'),
87 'blockquote' => array('title','style','id','class'),
88 'caption' => array('style','id','class'),
89 'code' => array('style','id','class'),
90 'del' => array('title','style','id','class'),
91 'div' => array('title','style','id','class','align'),
92 'dt' => array('style','id','class'),
93 'dd' => array('style','id','class'),
94 'font' => array('color','size','face','style','id','class'),
95 'h1' => array('style','id','class','align'),
96 'h2' => array('style','id','class','align'),
97 'h3' => array('style','id','class','align'),
98 'h4' => array('style','id','class','align'),
99 'h5' => array('style','id','class','align'),
100 'h6' => array('style','id','class','align'),
101 'hr' => array('style','id','class'),
102 'i' => array('style','id','class'),
103 'img' => array('src','alt','height','width','title'),
104 'ins' => array('title','style','id','class'),
105 'li' => array('style','id','class'),
106 'map' => array('shape','coords','href','alt','title','style','id','class','name'),
107 'ol' => array('style','id','class'),
108 'p' => array('style','id','class','align'),
109 'pre' => array('style','id','class'),
110 's' => array('style','id','class'),
111 'small' => array('style','id','class'),
112 'strong' => array('style','id','class'),
113 'span' => array('title','style','id','class','align'),
114 'sub' => array('style','id','class'),
115 'sup' => array('style','id','class'),
116 'table' => array('border','width','style','id','class','cellspacing','cellpadding'),
117 'tbody' => array('align','valign','style','id','class'),
118 'td' => array('width','height','style','id','class','align','valign','colspan','rowspan','bgcolor'),
119 'tfoot' => array('align','valign','style','id','class','align','valign'),
120 'th' => array('width','height','style','id','class','colspan','rowspan'),
121 'thead' => array('align','valign','style','id','class'),
122 'tr' => array('align','valign','style','id','class'),
123 'u' => array('style','id','class'),
124 'ul' => array('style','id','class'),
125 'php' => array('id'),
126 );
127
128 return $tags;
129 }
130
131 private static function replacePhpToTags(&$html)
132 {
133 if(!preg_match_all('/(<\?[\W\w\n]*?\?>)/i', $html, $matches, PREG_SET_ORDER))
134 {
135 return [];
136 }
137
138 if (!is_array($matches))
139 {
140 return [];
141 }
142
143 $stored = [];
144 $counter = 0;
145 foreach($matches as $key => $value)
146 {
147 $counter++;
148 $stored["<php id=\"{$counter}\"></php>"] = $value[0];
149 }
150
151 $html = str_replace(
152 array_values($stored),
153 array_keys($stored),
154 $html
155 );
156
157 return $stored;
158 }
159
160 private static function replaceTagsToPhp(&$html, array $stored = [])
161 {
162 $html = str_replace(
163 array_keys($stored),
164 array_values($stored),
165 $html
166 );
167
168 return $html;
169 }
170}
static loadMessages($file)
Definition loc.php:64