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