]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
Merge pull request #4170 from MrPetovan/bug/4155-remove-proxy-oembed
[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 = array();
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], array("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"], array("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 = array();
96
97         if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
98                 return get_old_attachment_data($body);
99
100         $attributes = $match[2];
101
102         $data["text"] = trim($match[1]);
103
104         $type = "";
105         preg_match("/type='(.*?)'/ism", $attributes, $matches);
106         if ($matches[1] != "")
107                 $type = strtolower($matches[1]);
108
109         preg_match('/type="(.*?)"/ism', $attributes, $matches);
110         if ($matches[1] != "")
111                 $type = strtolower($matches[1]);
112
113         if ($type == "")
114                 return(array());
115
116         if (!in_array($type, array("link", "audio", "photo", "video")))
117                 return(array());
118
119         if ($type != "")
120                 $data["type"] = $type;
121
122         $url = "";
123         preg_match("/url='(.*?)'/ism", $attributes, $matches);
124         if ($matches[1] != "")
125                 $url = $matches[1];
126
127         preg_match('/url="(.*?)"/ism', $attributes, $matches);
128         if ($matches[1] != "")
129                 $url = $matches[1];
130
131         if ($url != "")
132                 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
133
134         $title = "";
135         preg_match("/title='(.*?)'/ism", $attributes, $matches);
136         if ($matches[1] != "")
137                 $title = $matches[1];
138
139         preg_match('/title="(.*?)"/ism', $attributes, $matches);
140         if ($matches[1] != "")
141                 $title = $matches[1];
142
143         if ($title != "") {
144                 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
145                 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
146                 $title = str_replace(array("[", "]"), array("&#91;", "&#93;"), $title);
147                 $data["title"] = $title;
148         }
149
150         $image = "";
151         preg_match("/image='(.*?)'/ism", $attributes, $matches);
152         if ($matches[1] != "")
153                 $image = $matches[1];
154
155         preg_match('/image="(.*?)"/ism', $attributes, $matches);
156         if ($matches[1] != "")
157                 $image = $matches[1];
158
159         if ($image != "")
160                 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
161
162         $preview = "";
163         preg_match("/preview='(.*?)'/ism", $attributes, $matches);
164         if ($matches[1] != "")
165                 $preview = $matches[1];
166
167         preg_match('/preview="(.*?)"/ism', $attributes, $matches);
168         if ($matches[1] != "")
169                 $preview = $matches[1];
170
171         if ($preview != "")
172                 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
173
174         $data["description"] = trim($match[3]);
175
176         $data["after"] = trim($match[4]);
177
178         return($data);
179 }
180
181 function get_attached_data($body, $item = array()) {
182 /*
183  - text:
184  - type: link, video, photo
185  - title:
186  - url:
187  - image:
188  - description:
189  - (thumbnail)
190 */
191
192         $has_title = !empty($item['title']);
193         $plink = (!empty($item['plink']) ? $item['plink'] : '');
194         $post = get_attachment_data($body);
195
196         // if nothing is found, it maybe having an image.
197         if (!isset($post["type"])) {
198                 // Simplify image codes
199                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
200
201                 $URLSearchString = "^\[\]";
202                 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures,  PREG_SET_ORDER)) {
203                         if ((count($pictures) == 1) && !$has_title) {
204                                 // Checking, if the link goes to a picture
205                                 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
206
207                                 // Workaround:
208                                 // Sometimes photo posts to the own album are not detected at the start.
209                                 // So we seem to cannot use the cache for these cases. That's strange.
210                                 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/"))
211                                         $data = ParseUrl::getSiteinfo($pictures[0][1], true);
212
213                                 if ($data["type"] == "photo") {
214                                         $post["type"] = "photo";
215                                         if (isset($data["images"][0])) {
216                                                 $post["image"] = $data["images"][0]["src"];
217                                                 $post["url"] = $data["url"];
218                                         } else
219                                                 $post["image"] = $data["url"];
220
221                                         $post["preview"] = $pictures[0][2];
222                                         $post["text"] = str_replace($pictures[0][0], "", $body);
223                                 } else {
224                                         $imgdata = Image::getInfoFromURL($pictures[0][1]);
225                                         if (substr($imgdata["mime"], 0, 6) == "image/") {
226                                                 $post["type"] = "photo";
227                                                 $post["image"] = $pictures[0][1];
228                                                 $post["preview"] = $pictures[0][2];
229                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
230                                         }
231                                 }
232                         } elseif (count($pictures) > 0) {
233                                 $post["type"] = "link";
234                                 $post["url"] = $plink;
235                                 $post["image"] = $pictures[0][2];
236                                 $post["text"] = $body;
237                         }
238                 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures,  PREG_SET_ORDER)) {
239                         if ((count($pictures) == 1) && !$has_title) {
240                                 $post["type"] = "photo";
241                                 $post["image"] = $pictures[0][1];
242                                 $post["text"] = str_replace($pictures[0][0], "", $body);
243                         } elseif (count($pictures) > 0) {
244                                 $post["type"] = "link";
245                                 $post["url"] = $plink;
246                                 $post["image"] = $pictures[0][1];
247                                 $post["text"] = $body;
248                         }
249                 }
250
251                 // Test for the external links
252                 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1,  PREG_SET_ORDER);
253                 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2,  PREG_SET_ORDER);
254
255                 $links = array_merge($links1, $links2);
256
257                 // If there is only a single one, then use it.
258                 // This should cover link posts via API.
259                 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
260                         $post["type"] = "link";
261                         $post["text"] = trim($body);
262                         $post["url"] = $links[0][1];
263                 }
264
265                 // Now count the number of external media links
266                 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1,  PREG_SET_ORDER);
267                 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2,  PREG_SET_ORDER);
268                 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3,  PREG_SET_ORDER);
269                 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4,  PREG_SET_ORDER);
270
271                 // Add them to the other external links
272                 $links = array_merge($links, $links1, $links2, $links3, $links4);
273
274                 // Are there more than one?
275                 if (count($links) > 1) {
276                         // The post will be the type "text", which means a blog post
277                         unset($post["type"]);
278                         $post["url"] = $plink;
279                 }
280
281                 if (!isset($post["type"])) {
282                         $post["type"] = "text";
283                         $post["text"] = trim($body);
284                 }
285         } elseif (isset($post["url"]) && ($post["type"] == "video")) {
286                 $data = ParseUrl::getSiteinfoCached($post["url"], true);
287
288                 if (isset($data["images"][0]))
289                         $post["image"] = $data["images"][0]["src"];
290         }
291
292         return $post;
293 }
294
295 /**
296  * Shortens message
297  *
298  * @param type $msg
299  * @param type $limit
300  * @return type
301  *
302  * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
303  */
304 function shortenmsg($msg, $limit)
305 {
306         $lines = explode("\n", $msg);
307         $msg = "";
308         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
309         $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
310         foreach ($lines AS $row => $line) {
311                 if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
312                         $msg = trim($msg . "\n" . $line);
313                 } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
314                         // Is the new message empty by now or is it a reshared message?
315                         $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
316                 } else {
317                         break;
318                 }
319         }
320
321         return $msg;
322 }
323
324 /**
325  * @brief Convert a message into plaintext for connectors to other networks
326  *
327  * @param array $b The message array that is about to be posted
328  * @param int $limit The maximum number of characters when posting to that network
329  * @param bool $includedlinks Has an attached link to be included into the message?
330  * @param int $htmlmode This triggers the behaviour of the bbcode conversion
331  * @param string $target_network Name of the network where the post should go to.
332  *
333  * @return string The converted message
334  */
335 function plaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
336
337         // Remove the hash tags
338         $URLSearchString = "^\[\]";
339         $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
340
341         // Add an URL element if the text contains a raw link
342         $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
343
344         // Remove the abstract
345         $body = remove_abstract($body);
346
347         // At first look at data that is attached via "type-..." stuff
348         // This will hopefully replaced with a dedicated bbcode later
349         //$post = get_attached_data($b["body"]);
350         $post = get_attached_data($body, $b);
351
352         if (($b["title"] != "") && ($post["text"] != ""))
353                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
354         elseif ($b["title"] != "")
355                 $post["text"] = trim($b["title"]);
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         if ($abstract != "") {
387                 $post["text"] = $abstract;
388
389                 if ($post["type"] == "text") {
390                         $post["type"] = "link";
391                         $post["url"] = $b["plink"];
392                 }
393         }
394
395         $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
396         $msg = html2plain($html, 0, true);
397         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
398
399         $link = "";
400         if ($includedlinks) {
401                 if ($post["type"] == "link")
402                         $link = $post["url"];
403                 elseif ($post["type"] == "text")
404                         $link = $post["url"];
405                 elseif ($post["type"] == "video")
406                         $link = $post["url"];
407                 elseif ($post["type"] == "photo")
408                         $link = $post["image"];
409
410                 if (($msg == "") && isset($post["title"]))
411                         $msg = trim($post["title"]);
412
413                 if (($msg == "") && isset($post["description"]))
414                         $msg = trim($post["description"]);
415
416                 // If the link is already contained in the post, then it neeedn't to be added again
417                 // But: if the link is beyond the limit, then it has to be added.
418                 if (($link != "") && strstr($msg, $link)) {
419                         $pos = strpos($msg, $link);
420
421                         // Will the text be shortened in the link?
422                         // Or is the link the last item in the post?
423                         if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg))))
424                                 $msg = trim(str_replace($link, "", $msg));
425                         elseif (($limit == 0) || ($pos < $limit)) {
426                                 // The limit has to be increased since it will be shortened - but not now
427                                 // Only do it with Twitter (htmlmode = 8)
428                                 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8))
429                                         $limit = $limit - 23 + strlen($link);
430
431                                 $link = "";
432
433                                 if ($post["type"] == "text")
434                                         unset($post["url"]);
435                         }
436                 }
437         }
438
439         if ($limit > 0) {
440                 // Reduce multiple spaces
441                 // When posted to a network with limited space, we try to gain space where possible
442                 while (strpos($msg, "  ") !== false)
443                         $msg = str_replace("  ", " ", $msg);
444
445                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
446                 if (iconv_strlen($link, "UTF-8") > 0)
447                         $limit = $limit - 23;
448
449                 if (iconv_strlen($msg, "UTF-8") > $limit) {
450
451                         if (($post["type"] == "text") && isset($post["url"])) {
452                                 $post["url"] = $b["plink"];
453                         } elseif (!isset($post["url"])) {
454                                 $limit = $limit - 23;
455                                 $post["url"] = $b["plink"];
456                         // Which purpose has this line? It is now uncommented, but left as a reminder
457                         //} elseif (strpos($b["body"], "[share") !== false) {
458                         //      $post["url"] = $b["plink"];
459                         } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
460                                 $post["url"] = $b["plink"];
461                         }
462                         $msg = shortenmsg($msg, $limit);
463                 }
464         }
465
466         $post["text"] = trim($msg);
467
468         return($post);
469 }