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