Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
parser.php
1<?php
3
4use \Bitrix\Main\Web\DOM;
5use \Bitrix\Landing\Manager;
6use \Bitrix\Landing\File;
7
8class Parser
9{
13 const ACTION_DOWNLOAD_FILE = '/bitrix/tools/disk/uf.php?attachedId=#attachedId#&amp;action=download&amp;ncc=1';
14
20 protected static function convertText(string $text): string
21 {
22 static $parser = null;
23
24 if ($parser === null)
25 {
26 $parser = new \CTextParser;
27 $parser->allow['SMILES'] = 'N';
28 }
29
30 // prepare before parsing
31 $text = preg_replace('#\[P\]\s*\[VIDEO#s', '[VIDEO', $text);
32 $text = preg_replace('#\[/VIDEO\]\s*\[/P\]#s', '[/VIDEO]', $text);
33
34 $text = $parser->convertText($text);
35
36 // prepare after parsing
37 $text = preg_replace('#<p>\s*</p>#s', '<br/>', $text);
38
39 return $text;
40 }
41
47 protected static function getVideoContent(string $videoSrc): ?array
48 {
49 if (preg_match('#^//www.youtube.com/embed/([^?]+)#i', $videoSrc, $matches))
50 {
51 $ytCode = $matches[1];
52
53 return [
54 'src' => $videoSrc,
55 'source' => 'https://www.youtube.com/watch?v=' . $ytCode,
56 'preview' => '//img.youtube.com/vi/' .$ytCode . '/sddefault.jpg'
57 ];
58 }
59
60 return null;
61 }
62
69 protected static function getImgContent($attacheId, array $files = []): ?array
70 {
71 if (isset($files[$attacheId]))
72 {
73 $fileItem = $files[$attacheId];
74 $file = \CFile::getFileArray($fileItem['file_id']);
75 if (strpos($file['CONTENT_TYPE'], 'image/') === 0)
76 {
77 $fileIO = new \Bitrix\Main\IO\File(
78 Manager::getDocRoot() . $file['SRC']
79 );
80 if ($fileIO->isExists())
81 {
82 $newFile = Manager::savePicture([
83 $fileItem['file_name'],
84 base64_encode($fileIO->getContents())
85 ]);
86 if ($newFile)
87 {
88 return [
89 'id' => $newFile['ID'],
90 'src' => File::getFilePath($newFile['ID'])
91 ];
92 }
93 }
94 }
95 }
96
97 return null;
98 }
99
106 protected static function replaceFilesInContent(string $text, array $files = []): string
107 {
108 $replace = [];
109
110 foreach ($files as $fId => $item)
111 {
112 if ($item['prefix'])
113 {
114 $actionUrl = str_replace(
115 '#attachedId#',
116 $item['id'],
117 self::ACTION_DOWNLOAD_FILE
118 );
119 $replace['[DISK FILE ID=' . $fId . ']'] =
120 '<a href="' . $actionUrl . '" target="_blank">' .
121 $item['file_name'] .
122 '</a>';
123 }
124 }
125
126 if ($replace)
127 {
128 $text = str_replace(
129 array_keys($replace),
130 array_values($replace),
131 $text
132 );
133 }
134
135 return $text;
136 }
137
144 protected static function getBlockFromNode(\Bitrix\Main\Web\DOM\Node $node, array $params = []): ?array
145 {
146 $type = 'text';
147 $content = $node->getOuterHTML();
148 $attrs = $node->getAttributes();
149
150 // quote / code
151 if (isset($attrs['class']) && in_array($attrs['class']->getValue(), ['quote', 'code']))
152 {
153 $type = $attrs['class']->getValue();
154 $regExp = '#^<div class="' . $type . '">\s*<table class="' . $type . '"><tr><td>' .
155 '(.*?)</td></tr></table></div>$#is';
156 if (preg_match($regExp, $content, $matches))
157 {
158 $content = $matches[1];
159 }
160 if ($type == 'code' && preg_match('#^<pre>(.*?)</pre>$#is', $content, $matches))
161 {
162 $content = $matches[1];
163 }
164 }
165 // table
166 else if (isset($attrs['class']) && $attrs['class']->getValue() == 'data-table')
167 {
168 $type = 'table';
169 }
170 // video
171 else if (($node->getNodeName() == 'IFRAME') && isset($attrs['src']))
172 {
173 $type = 'video';
174 $content = self::getVideoContent(
175 $attrs['src']->getValue()
176 );
177 }
178 // picture / file on a single line
179 else if (preg_match('/^\[DISK FILE ID=([^\]]+)\]/is', trim($content), $matches))
180 {
181 $type = 'img';
182 $content = self::getImgContent($matches[1], $params['files']);;
183 }
184
185 // replace files in content
186 if (
187 $type == 'text' &&
188 isset($params['files']) &&
189 is_array($params['files'])
190 )
191 {
192 $content = self::replaceFilesInContent($content, $params['files']);
193 }
194
195 if ($content)
196 {
197 return [
198 'type' => $type,
199 'content' => $content
200 ];
201 }
202
203 return null;
204 }
205
212 public static function textToBlocks(string $text, array $params = []): array
213 {
214 $count = -1;
215 $blocks = [];
216
217 $dom = new DOM\Document;
218 $text = self::convertText($text);
219 $dom->loadHTML($text);
220
221 foreach ($dom->getChildNodesArray() as $child)
222 {
223 $bewBlock = self::getBlockFromNode($child, $params);
224 if ($bewBlock)
225 {
226 if (
227 $blocks &&
228 $bewBlock['type'] == 'text' &&
229 $blocks[$count]['type'] == 'text'
230 )
231 {
232 $blocks[$count]['content'] .= $bewBlock['content'];
233 }
234 else
235 {
236 $count++;
237 $blocks[] = $bewBlock;
238 }
239 }
240 }
241
242 // trim block content
243 foreach ($blocks as $key => $item)
244 {
245 if ($item['type'] == 'text')
246 {
247 if (!trim(str_replace(['<br />', '<br/>'], '', $item['content'])))
248 {
249 unset($blocks[$key]);
250 }
251 }
252 }
253
254 return array_values($blocks);
255 }
256}
static getFilePath($fileId)
Definition file.php:600
static savePicture($file, $ext=false, $params=array())
Definition manager.php:585
static replaceFilesInContent(string $text, array $files=[])
Definition parser.php:106
static getVideoContent(string $videoSrc)
Definition parser.php:47
static convertText(string $text)
Definition parser.php:20
static getImgContent($attacheId, array $files=[])
Definition parser.php:69
static textToBlocks(string $text, array $params=[])
Definition parser.php:212
static getBlockFromNode(\Bitrix\Main\Web\DOM\Node $node, array $params=[])
Definition parser.php:144