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;
26 use Friendica\Model\Photo;
27 use Friendica\Model\Post;
30 * Tumblr Neue Post Format
31 * @see https://www.tumblr.com/docs/npf
35 static $heading_subtype = [];
37 static public function fromBBCode(string $bbcode, int $uri_id): array
39 $bbcode = self::prepareBody($bbcode);
41 $html = BBCode::convert($bbcode, false, BBCode::CONNECTORS);
46 $doc = new DOMDocument();
47 $doc->formatOutput = true;
48 if (!@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) {
52 self::setHeadingSubStyles($doc);
54 $element = $doc->getElementsByTagName('body')->item(0);
56 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, true, []);
58 return self::addLinkBlockForUriId($uri_id, 0, $npf);
61 static function setHeadingSubStyles($doc)
63 self::$heading_subtype = [];
64 foreach (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as $element) {
65 if ($doc->getElementsByTagName($element)->count() > 0) {
66 if (empty(self::$heading_subtype)) {
67 self::$heading_subtype[$element] = 'heading1';
69 self::$heading_subtype[$element] = 'heading2';
75 static private function prepareBody(string $body): string
77 $shared = BBCode::fetchShareAttributes($body);
78 if (!empty($shared)) {
79 $body = $shared['shared'];
82 $body = BBCode::removeAttachment($body);
84 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
86 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
87 foreach ($pictures as $picture) {
88 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
89 $body = str_replace($picture[0], "\n\n[img=" . str_replace('-1.', '-0.', $picture[2]) . "]" . $picture[3] . "[/img]\n\n", $body);
94 $body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", "\n\n[img=$1]$2[/img]\n\n", $body);
96 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
97 foreach ($pictures as $picture) {
98 if (preg_match('#/photo/.*-[01]\.#ism', $picture[2]) && (preg_match('#/photo/.*-0\.#ism', $picture[1]) || preg_match('#/photos/.*/image/#ism', $picture[1]))) {
99 $body = str_replace($picture[0], "\n\n[img]" . str_replace('-1.', '-0.', $picture[2]) . "[/img]\n\n", $body);
104 $body = preg_replace("/\[img\](.*?)\[\/img\]/ism", "\n\n[img]$1[/img]\n\n", $body);
105 $body = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", "\n\n[audio]$1[/audio]\n\n", $body);
106 $body = preg_replace("/\[video\](.*?)\[\/video\]/ism", "\n\n[video]$1[/video]\n\n", $body);
110 $body = str_replace(["\n\n\n"], ["\n\n"], $body);
111 } while ($oldbody != $body);
116 static private function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array $npf = [], string $text = '', array $formatting = []): array
118 if ($parse_structure && $text) {
119 list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
122 $callstack[] = $element->nodeName;
123 $level = self::getLevelByCallstack($callstack);
125 foreach ($element->childNodes as $child) {
126 switch ($child->nodeName) {
129 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'bold', $callstack, $npf, $text, $formatting);
134 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'italic', $callstack, $npf, $text, $formatting);
138 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'strikethrough', $callstack, $npf, $text, $formatting);
143 list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, '', $callstack, $npf, $text, $formatting);
154 $text .= $child->textContent;
159 // Ignore tables and spoilers
164 list($npf, $text, $formatting) = self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting);
166 $npf = self::addLinkBlock($child, $uri_id, $level, $npf);
171 $npf = self::addImageBlock($child, $uri_id, $level, $npf);
175 list($npf, $text, $formatting) = self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting);
180 if ($parse_structure && $text) {
181 list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
183 return [$npf, $text, $formatting];
186 static private function getLevelByCallstack($callstack): int
189 foreach ($callstack as $entry) {
190 if (in_array($entry, ['ol', 'ul', 'blockquote'])) {
194 return max(0, $level - 1);
197 static private function getSubTypeByCallstack($callstack, string $text): string
200 foreach ($callstack as $entry) {
203 $subtype = 'ordered-list-item';
207 $subtype = 'unordered-list-item';
211 $subtype = self::$heading_subtype[$entry];
215 $subtype = self::$heading_subtype[$entry];
219 $subtype = self::$heading_subtype[$entry];
223 $subtype = self::$heading_subtype[$entry];
227 $subtype = self::$heading_subtype[$entry];
231 $subtype = self::$heading_subtype[$entry];
235 $subtype = mb_strlen($text) < 100 ? 'quote' : 'indented';
239 $subtype = 'indented';
250 static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array $npf, string $text, array $formatting): array
252 $start = mb_strlen($text);
253 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
258 'end' => mb_strlen($text),
262 return [$npf, $text, $formatting];
265 static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array $npf, string $text, array $formatting): array
267 $start = mb_strlen($text);
268 list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
271 foreach ($element->attributes as $key => $attribute) {
272 $attributes[$key] = trim($attribute->value);
274 if (!empty($attributes['href'])) {
277 'end' => mb_strlen($text),
279 'url' => $attributes['href']
282 return [$npf, $text, $formatting];
285 static private function addBlock(string $text, array $formatting, array $npf, array $callstack): array
293 if (!empty($formatting)) {
294 $block['formatting'] = $formatting;
297 $level = self::getLevelByCallstack($callstack);
299 $block['indent_level'] = $level;
302 $subtype = self::getSubTypeByCallstack($callstack, $text);
304 $block['subtype'] = $subtype;
306 unset($block['subtype']);
312 return [$npf, $text, $formatting];
315 static private function addPoster(array $media, array $block): array
318 if (!empty($media['preview'])) {
319 $poster['url'] = $media['preview'];
321 if (!empty($media['preview-width'])) {
322 $poster['width'] = $media['preview-width'];
324 if (!empty($media['preview-height'])) {
325 $poster['height'] = $media['preview-height'];
327 if (!empty($poster)) {
328 $block['poster'] = [$poster];
333 static private function addLinkBlockForUriId(int $uri_id, int $level, array $npf): array
335 foreach (Post\Media::getByURIId($uri_id, [Post\Media::HTML]) as $link) {
336 $host = parse_url($link['url'], PHP_URL_HOST);
337 if (in_array($host, ['www.youtube.com', 'youtu.be'])) {
340 'provider' => 'youtube',
341 'url' => $link['url'],
343 } elseif (in_array($host, ['vimeo.com'])) {
346 'provider' => 'vimeo',
347 'url' => $link['url'],
349 } elseif (in_array($host, ['open.spotify.com'])) {
352 'provider' => 'spotify',
353 'url' => $link['url'],
358 'url' => $link['url'],
360 if (!empty($link['name'])) {
361 $block['title'] = $link['name'];
363 if (!empty($link['description'])) {
364 $block['description'] = $link['description'];
366 if (!empty($link['author-name'])) {
367 $block['author'] = $link['author-name'];
369 if (!empty($link['publisher-name'])) {
370 $block['site_name'] = $link['publisher-name'];
375 $block['indent_level'] = $level;
378 $npf[] = self::addPoster($link, $block);
383 static private function addImageBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
386 foreach ($element->attributes as $key => $attribute) {
387 $attributes[$key] = trim($attribute->value);
389 if (empty($attributes['src'])) {
398 if (!empty($attributes['alt'])) {
399 $block['alt_text'] = $attributes['alt'];
402 if (!empty($attributes['title']) && (($attributes['alt'] ?? '') != $attributes['title'])) {
403 $block['caption'] = $attributes['title'];
406 $rid = Photo::ridFromURI($attributes['src']);
408 $photos = Photo::selectToArray([], ['resource-id' => $rid]);
409 foreach ($photos as $photo) {
410 $block['media'][] = [
411 'type' => $photo['type'],
412 'url' => str_replace('-0.', '-' . $photo['scale'] . '.', $attributes['src']),
413 'width' => $photo['width'],
414 'height' => $photo['height'],
417 if (empty($attributes['alt']) && !empty($photos[0]['desc'])) {
418 $block['alt_text'] = $photos[0]['desc'];
420 } elseif ($media = Post\Media::getByURL($uri_id, $attributes['src'], [Post\Media::IMAGE])) {
421 $block['media'][] = [
422 'type' => $media['mimetype'],
423 'url' => $media['url'],
424 'width' => $media['width'],
425 'height' => $media['height'],
427 if (empty($attributes['alt']) && !empty($media['description'])) {
428 $block['alt_text'] = $media['description'];
431 $block['media'][] = ['url' => $attributes['src']];
435 $block['indent_level'] = $level;
443 static private function addLinkBlock(DOMElement $element, int $uri_id, int $level, array $npf): array
446 foreach ($element->attributes as $key => $attribute) {
447 $attributes[$key] = trim($attribute->value);
449 if (empty($attributes['href'])) {
453 $media = Post\Media::getByURL($uri_id, $attributes['href'], [Post\Media::AUDIO, Post\Media::VIDEO]);
454 if (!empty($media)) {
455 switch ($media['type']) {
456 case Post\Media::AUDIO:
460 'type' => $media['mimetype'],
461 'url' => $media['url'],
465 if (!empty($media['name'])) {
466 $block['title'] = $media['name'];
467 } elseif (!empty($media['description'])) {
468 $block['title'] = $media['description'];
471 $block = self::addPoster($media, $block);
474 case Post\Media::VIDEO:
478 'type' => $media['mimetype'],
479 'url' => $media['url'],
483 $block = self::addPoster($media, $block);
489 'text' => $element->textContent,
493 'end' => mb_strlen($element->textContent),
495 'url' => $attributes['href']
502 $block['indent_level'] = $level;