3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content\Text;
27 use Friendica\Model\Photo;
28 use Friendica\Model\Post;
31 * Tumblr Neue Post Format
32 * @see https://www.tumblr.com/docs/npf
36 static public function fromBBCode(string $bbcode, int $uri_id): array
40 $bbcode = self::prepareBody($bbcode);
42 $html = BBCode::convert($bbcode, false, BBCode::CONNECTORS);
47 $doc = new DOMDocument();
48 if (!@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
52 $node = $doc->getElementsByTagName('body')->item(0);
53 foreach ($node->childNodes as $child) {
54 $npf = self::routeElements($child, $uri_id, $npf);
57 return self::addLinkBlock($uri_id, $npf);
60 public static function prepareBody(string $body): string
62 $shared = BBCode::fetchShareAttributes($body);
63 if (!empty($shared)) {
64 $body = $shared['shared'];
67 $body = BBCode::removeAttachment($body);
69 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
71 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
72 foreach ($pictures as $picture) {
73 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
74 $body = str_replace($picture[0], "\n\n[img=" . str_replace('-1.', '-0.', $picture[2]) . "]" . $picture[3] . "[/img]\n\n", $body);
79 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", "\n\n[img=$1]$2[/img]\n\n", $body);
81 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
82 foreach ($pictures as $picture) {
83 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
84 $body = str_replace($picture[0], "\n\n[img]" . str_replace('-1.', '-0.', $picture[2]) . "[/img]\n\n", $body);
89 $body = preg_replace("/\[img\](.*?)\[\/img\]/ism", "\n\n[img]$1[/img]\n\n", $body);
90 $body = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", "\n\n[audio]$1[/audio]\n\n", $body);
91 $body = preg_replace("/\[video\](.*?)\[\/video\]/ism", "\n\n[video]$1[/video]\n\n", $body);
95 $body = str_replace(["\n\n\n"], ["\n\n"], $body);
96 } while ($oldbody != $body);
101 static private function routeElements(DOMElement|DOMNode $child, int $uri_id, array $npf): array
103 switch ($child->tagName ?? '') {
105 $npf = self::addTextBlock($child, $uri_id, $npf, 'indented');
109 $npf = self::addTextBlock($child, $uri_id, $npf, 'heading1');
113 $npf = self::addTextBlock($child, $uri_id, $npf, 'heading1');
117 $npf = self::addTextBlock($child, $uri_id, $npf, 'heading1');
121 $npf = self::addTextBlock($child, $uri_id, $npf, 'heading2');
125 $npf = self::addTextBlock($child, $uri_id, $npf, 'heading2');
129 $npf = self::addTextBlock($child, $uri_id, $npf, 'heading2');
133 $npf = self::addListBlock($child, $uri_id, $npf, false, 0);
137 $npf = self::addListBlock($child, $uri_id, $npf, true, 0);
146 $npf = self::addTextBlock($child, $uri_id, $npf, 'indented');
150 $npf = self::addMediaBlock($child, $uri_id, $npf);
154 // $child->ownerDocument->saveHTML($child)
158 $npf = self::addImageBlock($child, $uri_id, $npf);
162 $npf = self::addTextBlock($child, $uri_id, $npf);
168 static private function addImageBlock(DOMElement|DOMNode $child, int $uri_id, array $npf): array
171 foreach ($child->attributes as $key => $attribute) {
172 $attributes[$key] = $attribute->value;
174 if (empty($attributes['src'])) {
183 if (!empty($attributes['alt'])) {
184 $entry['alt_text'] = $attributes['alt'];
187 if (!empty($attributes['title']) && ($attributes['alt'] ?? '' != $attributes['title'])) {
188 $entry['caption'] = $attributes['title'];
191 $rid = Photo::ridFromURI($attributes['src']);
193 $photos = Photo::selectToArray([], ['resource-id' => $rid]);
194 foreach ($photos as $photo) {
195 $entry['media'][] = [
196 'type' => $photo['type'],
197 'url' => str_replace('-0.', '-' . $photo['scale'] . '.', $attributes['src']),
198 'width' => $photo['width'],
199 'height' => $photo['height'],
202 if (empty($attributes['alt']) && !empty($photos[0]['desc'])) {
203 $entry['alt_text'] = $photos[0]['desc'];
205 } elseif ($media = Post\Media::getByURL($uri_id, $attributes['src'], [Post\Media::IMAGE])) {
206 $entry['media'][] = [
207 'type' => $media['mimetype'],
208 'url' => $media['url'],
209 'width' => $media['width'],
210 'height' => $media['height'],
212 if (empty($attributes['alt']) && !empty($media['description'])) {
213 $entry['alt_text'] = $media['description'];
216 $entry['media'][] = ['url' => $attributes['src']];
224 static private function addMediaBlock(DOMElement|DOMNode $child, int $uri_id, array $npf): array
227 foreach ($child->attributes as $key => $attribute) {
228 $attributes[$key] = $attribute->value;
230 if (empty($attributes['href'])) {
234 $media = Post\Media::getByURL($uri_id, $attributes['href'], [Post\Media::AUDIO, Post\Media::VIDEO]);
235 if (!empty($media)) {
236 switch ($media['type']) {
237 case Post\Media::AUDIO:
241 'type' => $media['mimetype'],
242 'url' => $media['url'],
246 if (!empty($media['name'])) {
247 $entry['title'] = $media['name'];
248 } elseif (!empty($media['description'])) {
249 $entry['title'] = $media['description'];
252 $npf[] = self::addPoster($media, $entry);
255 case Post\Media::VIDEO:
259 'type' => $media['mimetype'],
260 'url' => $media['url'],
264 $npf[] = self::addPoster($media, $entry);
270 'text' => $child->textContent,
273 'end' => strlen($child->textContent),
275 'url' => $attributes['href']
282 static private function addPoster(array $media, array $entry): array
285 if (!empty($media['preview'])) {
286 $poster['url'] = $media['preview'];
288 if (!empty($media['preview-width'])) {
289 $poster['width'] = $media['preview-width'];
291 if (!empty($media['preview-height'])) {
292 $poster['height'] = $media['preview-height'];
294 if (!empty($poster)) {
295 $entry['poster'] = $poster;
300 static private function fetchText(DOMElement|DOMNode $child, array $text = ['text' => '', 'formatting' => []]): array
302 foreach ($child->childNodes as $node) {
303 $start = strlen($text['text']);
305 switch ($node->nodeName) {
317 $type = 'strikethrough';
324 if ($node->nodeName == 'br') {
325 $text['text'] .= "\n";
327 $text['text'] .= $node->textContent;
330 $text['formatting'][] = ['start' => $start, 'end' => strlen($text['text']), 'type' => $type];
336 static private function addTextBlock(DOMElement|DOMNode $child, int $uri_id, array $npf, string $subtype = ''): array
338 if (empty($subtype) && (($child->childElementCount) ?? 0 == 1) && ($child->textContent == $child->firstChild->textContent)) {
339 return self::routeElements($child->firstChild, $uri_id, $npf);
342 $element = ['type' => 'text'];
344 if (!empty($subtype)) {
345 $element['subtype'] = $subtype;
348 $text = self::fetchText($child);
350 $element['text'] = $text['text'];
351 $element['formatting'] = $text['formatting'];
353 if (empty($subtype)) {
354 switch ($child->tagName ?? '') {
357 $element['formatting'][] = ['start' => 0, 'end' => strlen($element['text']), 'type' => 'bold'];
362 $element['formatting'][] = ['start' => 0, 'end' => strlen($element['text']), 'type' => 'italic'];
366 $element['formatting'][] = ['start' => 0, 'end' => strlen($element['text']), 'type' => 'strikethrough'];
377 die($child->tagName . "\n");
382 if (empty($element['formatting'])) {
383 unset($element['formatting']);
391 static private function addListBlock(DOMElement|DOMNode $child, int $uri_id, array $npf, bool $ordered, int $level): array
393 foreach ($child->childNodes as $node) {
394 switch ($node->nodeName) {
396 $npf = self::addListBlock($node, $uri_id, $npf, false, $level++);
398 $npf = self::addListBlock($node, $uri_id, $npf, true, $level++);
400 $text = self::fetchText($node);
404 'subtype' => $ordered ? 'ordered-list-item' : 'unordered-list-item',
405 'text' => $text['text']
408 $entry['indent_level'] = $level;
410 if (!empty($text['formatting'])) {
411 $entry['formatting'] = $text['formatting'];
420 static private function addLinkBlock(int $uri_id, array $npf): array
422 foreach (Post\Media::getByURIId($uri_id, [Post\Media::HTML]) as $link) {
423 $host = parse_url($link['url'], PHP_URL_HOST);
424 if (in_array($host, ['www.youtube.com', 'youtu.be'])) {
427 'provider' => 'youtube',
428 'url' => $link['url'],
430 } elseif (in_array($host, ['vimeo.com'])) {
433 'provider' => 'vimeo',
434 'url' => $link['url'],
436 } elseif (in_array($host, ['open.spotify.com'])) {
439 'provider' => 'spotify',
440 'url' => $link['url'],
445 'url' => $link['url'],
447 if (!empty($link['name'])) {
448 $entry['title'] = $link['name'];
450 if (!empty($link['description'])) {
451 $entry['description'] = $link['description'];
453 if (!empty($link['author-name'])) {
454 $entry['author'] = $link['author-name'];
456 if (!empty($link['publisher-name'])) {
457 $entry['site_name'] = $link['publisher-name'];
461 $npf[] = self::addPoster($link, $entry);