]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/BBCode.php
Merge pull request #4345 from annando/bugfix-oembed
[friendica.git] / src / Content / Text / BBCode.php
1 <?php
2 /**
3  * @file src/Content/Text/BBCode.php
4  */
5 namespace Friendica\Content\Text;
6
7 use Friendica\App;
8 use Friendica\Content\Text\Plaintext;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Object\Image;
14 use Friendica\Util\ParseUrl;
15 use Friendica\Content\OEmbed;
16
17 require_once "include/bbcode.php";
18 require_once "include/html2plain.php";
19
20 class BBCode
21 {
22         /**
23          * @brief Fetches attachment data that were generated the old way
24          *
25          * @param string $body Message body
26          * @return array
27          * 'type' -> Message type ("link", "video", "photo")
28          * 'text' -> Text before the shared message
29          * 'after' -> Text after the shared message
30          * 'image' -> Preview image of the message
31          * 'url' -> Url to the attached message
32          * 'title' -> Title of the attachment
33          * 'description' -> Description of the attachment
34          */
35         private static function getOldAttachmentData($body)
36         {
37                 $post = [];
38
39                 // Simplify image codes
40                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
41
42                 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism", $body, $attached, PREG_SET_ORDER)) {
43                         foreach ($attached as $data) {
44                                 if (!in_array($data[1], ["type-link", "type-video", "type-photo"])) {
45                                         continue;
46                                 }
47
48                                 $post["type"] = substr($data[1], 5);
49
50                                 $pos = strpos($body, $data[0]);
51                                 if ($pos > 0) {
52                                         $post["text"] = trim(substr($body, 0, $pos));
53                                         $post["after"] = trim(substr($body, $pos + strlen($data[0])));
54                                 } else {
55                                         $post["text"] = trim(str_replace($data[0], "", $body));
56                                 }
57
58                                 $attacheddata = $data[2];
59
60                                 $URLSearchString = "^\[\]";
61
62                                 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
63
64                                         $picturedata = Image::getInfoFromURL($matches[1]);
65
66                                         if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
67                                                 $post["image"] = $matches[1];
68                                         } else {
69                                                 $post["preview"] = $matches[1];
70                                         }
71                                 }
72
73                                 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
74                                         $post["url"] = $matches[1];
75                                         $post["title"] = $matches[2];
76                                 }
77                                 if (($post["url"] == "") && (in_array($post["type"], ["link", "video"]))
78                                         && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
79                                         $post["url"] = $matches[1];
80                                 }
81
82                                 // Search for description
83                                 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches)) {
84                                         $post["description"] = $matches[1];
85                                 }
86                         }
87                 }
88                 return $post;
89         }
90
91         /**
92          * @brief Fetches attachment data that were generated with the "attachment" element
93          *
94          * @param string $body Message body
95          * @return array
96          * 'type' -> Message type ("link", "video", "photo")
97          * 'text' -> Text before the shared message
98          * 'after' -> Text after the shared message
99          * 'image' -> Preview image of the message
100          * 'url' -> Url to the attached message
101          * 'title' -> Title of the attachment
102          * 'description' -> Description of the attachment
103          */
104         public static function getAttachmentData($body)
105         {
106                 $data = [];
107
108                 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
109                         return self::getOldAttachmentData($body);
110                 }
111
112                 $attributes = $match[2];
113
114                 $data["text"] = trim($match[1]);
115
116                 $type = "";
117                 preg_match("/type='(.*?)'/ism", $attributes, $matches);
118                 if (x($matches, 1)) {
119                         $type = strtolower($matches[1]);
120                 }
121
122                 preg_match('/type="(.*?)"/ism', $attributes, $matches);
123                 if (x($matches, 1)) {
124                         $type = strtolower($matches[1]);
125                 }
126
127                 if ($type == "") {
128                         return [];
129                 }
130
131                 if (!in_array($type, ["link", "audio", "photo", "video"])) {
132                         return [];
133                 }
134
135                 if ($type != "") {
136                         $data["type"] = $type;
137                 }
138
139                 $url = "";
140                 preg_match("/url='(.*?)'/ism", $attributes, $matches);
141                 if (x($matches, 1)) {
142                         $url = $matches[1];
143                 }
144
145                 preg_match('/url="(.*?)"/ism', $attributes, $matches);
146                 if (x($matches, 1)) {
147                         $url = $matches[1];
148                 }
149
150                 if ($url != "") {
151                         $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
152                 }
153
154                 $title = "";
155                 preg_match("/title='(.*?)'/ism", $attributes, $matches);
156                 if (x($matches, 1)) {
157                         $title = $matches[1];
158                 }
159
160                 preg_match('/title="(.*?)"/ism', $attributes, $matches);
161                 if (x($matches, 1)) {
162                         $title = $matches[1];
163                 }
164
165                 if ($title != "") {
166                         $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
167                         $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
168                         $title = str_replace(["[", "]"], ["&#91;", "&#93;"], $title);
169                         $data["title"] = $title;
170                 }
171
172                 $image = "";
173                 preg_match("/image='(.*?)'/ism", $attributes, $matches);
174                 if (x($matches, 1)) {
175                         $image = $matches[1];
176                 }
177
178                 preg_match('/image="(.*?)"/ism', $attributes, $matches);
179                 if (x($matches, 1)) {
180                         $image = $matches[1];
181                 }
182
183                 if ($image != "") {
184                         $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
185                 }
186
187                 $preview = "";
188                 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
189                 if (x($matches, 1)) {
190                         $preview = $matches[1];
191                 }
192
193                 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
194                 if (x($matches, 1)) {
195                         $preview = $matches[1];
196                 }
197
198                 if ($preview != "") {
199                         $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
200                 }
201
202                 $data["description"] = trim($match[3]);
203
204                 $data["after"] = trim($match[4]);
205
206                 return $data;
207         }
208
209         public static function getAttachedData($body, $item = [])
210         {
211                 /*
212                 - text:
213                 - type: link, video, photo
214                 - title:
215                 - url:
216                 - image:
217                 - description:
218                 - (thumbnail)
219                 */
220
221                 $has_title = !empty($item['title']);
222                 $plink = (!empty($item['plink']) ? $item['plink'] : '');
223                 $post = self::getAttachmentData($body);
224
225                 // if nothing is found, it maybe having an image.
226                 if (!isset($post["type"])) {
227                         // Simplify image codes
228                         $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
229
230                         $URLSearchString = "^\[\]";
231                         if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
232                                 if ((count($pictures) == 1) && !$has_title) {
233                                         // Checking, if the link goes to a picture
234                                         $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
235
236                                         // Workaround:
237                                         // Sometimes photo posts to the own album are not detected at the start.
238                                         // So we seem to cannot use the cache for these cases. That's strange.
239                                         if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/")) {
240                                                 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
241                                         }
242
243                                         if ($data["type"] == "photo") {
244                                                 $post["type"] = "photo";
245                                                 if (isset($data["images"][0])) {
246                                                         $post["image"] = $data["images"][0]["src"];
247                                                         $post["url"] = $data["url"];
248                                                 } else {
249                                                         $post["image"] = $data["url"];
250                                                 }
251
252                                                 $post["preview"] = $pictures[0][2];
253                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
254                                         } else {
255                                                 $imgdata = Image::getInfoFromURL($pictures[0][1]);
256                                                 if (substr($imgdata["mime"], 0, 6) == "image/") {
257                                                         $post["type"] = "photo";
258                                                         $post["image"] = $pictures[0][1];
259                                                         $post["preview"] = $pictures[0][2];
260                                                         $post["text"] = str_replace($pictures[0][0], "", $body);
261                                                 }
262                                         }
263                                 } elseif (count($pictures) > 0) {
264                                         $post["type"] = "link";
265                                         $post["url"] = $plink;
266                                         $post["image"] = $pictures[0][2];
267                                         $post["text"] = $body;
268                                 }
269                         } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
270                                 if ((count($pictures) == 1) && !$has_title) {
271                                         $post["type"] = "photo";
272                                         $post["image"] = $pictures[0][1];
273                                         $post["text"] = str_replace($pictures[0][0], "", $body);
274                                 } elseif (count($pictures) > 0) {
275                                         $post["type"] = "link";
276                                         $post["url"] = $plink;
277                                         $post["image"] = $pictures[0][1];
278                                         $post["text"] = $body;
279                                 }
280                         }
281
282                         // Test for the external links
283                         preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
284                         preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
285
286                         $links = array_merge($links1, $links2);
287
288                         // If there is only a single one, then use it.
289                         // This should cover link posts via API.
290                         if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
291                                 $post["type"] = "link";
292                                 $post["text"] = trim($body);
293                                 $post["url"] = $links[0][1];
294                         }
295
296                         // Now count the number of external media links
297                         preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
298                         preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
299                         preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
300                         preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
301
302                         // Add them to the other external links
303                         $links = array_merge($links, $links1, $links2, $links3, $links4);
304
305                         // Are there more than one?
306                         if (count($links) > 1) {
307                                 // The post will be the type "text", which means a blog post
308                                 unset($post["type"]);
309                                 $post["url"] = $plink;
310                         }
311
312                         if (!isset($post["type"])) {
313                                 $post["type"] = "text";
314                                 $post["text"] = trim($body);
315                         }
316                 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
317                         $data = ParseUrl::getSiteinfoCached($post["url"], true);
318
319                         if (isset($data["images"][0])) {
320                                 $post["image"] = $data["images"][0]["src"];
321                         }
322                 }
323
324                 return $post;
325         }
326
327         /**
328          * @brief Convert a message into plaintext for connectors to other networks
329          *
330          * @param array $b The message array that is about to be posted
331          * @param int $limit The maximum number of characters when posting to that network
332          * @param bool $includedlinks Has an attached link to be included into the message?
333          * @param int $htmlmode This triggers the behaviour of the bbcode conversion
334          * @param string $target_network Name of the network where the post should go to.
335          *
336          * @return string The converted message
337          */
338         public static function toPlaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "")
339         {
340                 // Remove the hash tags
341                 $URLSearchString = "^\[\]";
342                 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
343
344                 // Add an URL element if the text contains a raw link
345                 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
346
347                 // Remove the abstract
348                 $body = remove_abstract($body);
349
350                 // At first look at data that is attached via "type-..." stuff
351                 // This will hopefully replaced with a dedicated bbcode later
352                 //$post = self::getAttachedData($b["body"]);
353                 $post = self::getAttachedData($body, $b);
354
355                 if (($b["title"] != "") && ($post["text"] != "")) {
356                         $post["text"] = trim($b["title"]."\n\n".$post["text"]);
357                 } elseif ($b["title"] != "") {
358                         $post["text"] = trim($b["title"]);
359                 }
360
361                 $abstract = "";
362
363                 // Fetch the abstract from the given target network
364                 if ($target_network != "") {
365                         $default_abstract = fetch_abstract($b["body"]);
366                         $abstract = fetch_abstract($b["body"], $target_network);
367
368                         // If we post to a network with no limit we only fetch
369                         // an abstract exactly for this network
370                         if (($limit == 0) && ($abstract == $default_abstract)) {
371                                 $abstract = "";
372                         }
373                 } else {// Try to guess the correct target network
374                         switch ($htmlmode) {
375                                 case 8:
376                                         $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
377                                         break;
378                                 case 7:
379                                         $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
380                                         break;
381                                 case 6:
382                                         $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
383                                         break;
384                                 default: // We don't know the exact target.
385                                         // We fetch an abstract since there is a posting limit.
386                                         if ($limit > 0) {
387                                                 $abstract = fetch_abstract($b["body"]);
388                                         }
389                         }
390                 }
391
392                 if ($abstract != "") {
393                         $post["text"] = $abstract;
394
395                         if ($post["type"] == "text") {
396                                 $post["type"] = "link";
397                                 $post["url"] = $b["plink"];
398                         }
399                 }
400
401                 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
402                 $msg = html2plain($html, 0, true);
403                 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
404
405                 $link = "";
406                 if ($includedlinks) {
407                         if ($post["type"] == "link") {
408                                 $link = $post["url"];
409                         } elseif ($post["type"] == "text") {
410                                 $link = $post["url"];
411                         } elseif ($post["type"] == "video") {
412                                 $link = $post["url"];
413                         } elseif ($post["type"] == "photo") {
414                                 $link = $post["image"];
415                         }
416
417                         if (($msg == "") && isset($post["title"])) {
418                                 $msg = trim($post["title"]);
419                         }
420
421                         if (($msg == "") && isset($post["description"])) {
422                                 $msg = trim($post["description"]);
423                         }
424
425                         // If the link is already contained in the post, then it neeedn't to be added again
426                         // But: if the link is beyond the limit, then it has to be added.
427                         if (($link != "") && strstr($msg, $link)) {
428                                 $pos = strpos($msg, $link);
429
430                                 // Will the text be shortened in the link?
431                                 // Or is the link the last item in the post?
432                                 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
433                                         $msg = trim(str_replace($link, "", $msg));
434                                 } elseif (($limit == 0) || ($pos < $limit)) {
435                                         // The limit has to be increased since it will be shortened - but not now
436                                         // Only do it with Twitter (htmlmode = 8)
437                                         if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
438                                                 $limit = $limit - 23 + strlen($link);
439                                         }
440
441                                         $link = "";
442
443                                         if ($post["type"] == "text") {
444                                                 unset($post["url"]);
445                                         }
446                                 }
447                         }
448                 }
449
450                 if ($limit > 0) {
451                         // Reduce multiple spaces
452                         // When posted to a network with limited space, we try to gain space where possible
453                         while (strpos($msg, "  ") !== false) {
454                                 $msg = str_replace("  ", " ", $msg);
455                         }
456
457                         // Twitter is using its own limiter, so we always assume that shortened links will have this length
458                         if (iconv_strlen($link, "UTF-8") > 0) {
459                                 $limit = $limit - 23;
460                         }
461
462                         if (iconv_strlen($msg, "UTF-8") > $limit) {
463                                 if (($post["type"] == "text") && isset($post["url"])) {
464                                         $post["url"] = $b["plink"];
465                                 } elseif (!isset($post["url"])) {
466                                         $limit = $limit - 23;
467                                         $post["url"] = $b["plink"];
468                                 // Which purpose has this line? It is now uncommented, but left as a reminder
469                                 //} elseif (strpos($b["body"], "[share") !== false) {
470                                 //      $post["url"] = $b["plink"];
471                                 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
472                                         $post["url"] = $b["plink"];
473                                 }
474                                 $msg = Plaintext::shorten($msg, $limit);
475                         }
476                 }
477
478                 $post["text"] = trim($msg);
479
480                 return($post);
481         }
482
483         public static function scaleExternalImages($srctext, $include_link = true, $scale_replace = false)
484         {
485                 // Suppress "view full size"
486                 if (intval(Config::get('system', 'no_view_full_size'))) {
487                         $include_link = false;
488                 }
489
490                 // Picture addresses can contain special characters
491                 $s = htmlspecialchars_decode($srctext);
492
493                 $matches = null;
494                 $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
495                 if ($c) {
496                         foreach ($matches as $mtch) {
497                                 logger('scale_external_image: ' . $mtch[1]);
498
499                                 $hostname = str_replace('www.', '', substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3));
500                                 if (stristr($mtch[1], $hostname)) {
501                                         continue;
502                                 }
503
504                                 // $scale_replace, if passed, is an array of two elements. The
505                                 // first is the name of the full-size image. The second is the
506                                 // name of a remote, scaled-down version of the full size image.
507                                 // This allows Friendica to display the smaller remote image if
508                                 // one exists, while still linking to the full-size image
509                                 if ($scale_replace) {
510                                         $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
511                                 } else {
512                                         $scaled = $mtch[1];
513                                 }
514                                 $i = OEmbed::fetchURL($scaled);
515                                 if (! $i) {
516                                         return $srctext;
517                                 }
518
519                                 // guess mimetype from headers or filename
520                                 $type = Image::guessType($mtch[1], true);
521
522                                 if ($i) {
523                                         $Image = new Image($i, $type);
524                                         if ($Image->isValid()) {
525                                                 $orig_width = $Image->getWidth();
526                                                 $orig_height = $Image->getHeight();
527
528                                                 if ($orig_width > 640 || $orig_height > 640) {
529                                                         $Image->scaleDown(640);
530                                                         $new_width = $Image->getWidth();
531                                                         $new_height = $Image->getHeight();
532                                                         logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
533                                                         $s = str_replace(
534                                                                 $mtch[0],
535                                                                 '[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
536                                                                 . "\n" . (($include_link)
537                                                                         ? '[url=' . $mtch[1] . ']' . L10n::t('view full size') . '[/url]' . "\n"
538                                                                         : ''),
539                                                                 $s
540                                                         );
541                                                         logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
542                                                 }
543                                         }
544                                 }
545                         }
546                 }
547
548                 // replace the special char encoding
549                 $s = htmlspecialchars($s, ENT_NOQUOTES, 'UTF-8');
550                 return $s;
551         }
552 }