]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
221d1471b1a7576fd8199e42108027037abf43d1
[friendica.git] / include / plaintext.php
1 <?php
2 /**
3  * @file include/plaintext.php
4  */
5 use Friendica\App;
6 use Friendica\ParseUrl;
7 use Friendica\Core\PConfig;
8 use Friendica\Object\Photo;
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 = Photo::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 = Photo::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 function shortenmsg($msg, $limit, $twitter = false) {
296         /// @TODO
297         /// For Twitter URLs aren't shortened, but they have to be calculated as if.
298
299         $lines = explode("\n", $msg);
300         $msg = "";
301         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
302         $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
303         foreach ($lines AS $row=>$line) {
304                 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
305                         $msg = trim($msg."\n".$line);
306                 // Is the new message empty by now or is it a reshared message?
307                 elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle)))
308                         $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
309                 else
310                         break;
311         }
312         return($msg);
313 }
314
315 /**
316  * @brief Convert a message into plaintext for connectors to other networks
317  *
318  * @param App $a The application class
319  * @param array $b The message array that is about to be posted
320  * @param int $limit The maximum number of characters when posting to that network
321  * @param bool $includedlinks Has an attached link to be included into the message?
322  * @param int $htmlmode This triggers the behaviour of the bbcode conversion
323  * @param string $target_network Name of the network where the post should go to.
324  *
325  * @return string The converted message
326  */
327 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
328
329         // Remove the hash tags
330         $URLSearchString = "^\[\]";
331         $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
332
333         // Add an URL element if the text contains a raw link
334         $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
335
336         // Remove the abstract
337         $body = remove_abstract($body);
338
339         // At first look at data that is attached via "type-..." stuff
340         // This will hopefully replaced with a dedicated bbcode later
341         //$post = get_attached_data($b["body"]);
342         $post = get_attached_data($body, $b);
343
344         if (($b["title"] != "") && ($post["text"] != ""))
345                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
346         elseif ($b["title"] != "")
347                 $post["text"] = trim($b["title"]);
348
349         $abstract = "";
350
351         // Fetch the abstract from the given target network
352         if ($target_network != "") {
353                 $default_abstract = fetch_abstract($b["body"]);
354                 $abstract = fetch_abstract($b["body"], $target_network);
355
356                 // If we post to a network with no limit we only fetch
357                 // an abstract exactly for this network
358                 if (($limit == 0) && ($abstract == $default_abstract))
359                         $abstract = "";
360
361         } else // Try to guess the correct target network
362                 switch ($htmlmode) {
363                         case 8:
364                                 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
365                                 break;
366                         case 7:
367                                 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
368                                 break;
369                         case 6:
370                                 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
371                                 break;
372                         default: // We don't know the exact target.
373                                  // We fetch an abstract since there is a posting limit.
374                                 if ($limit > 0)
375                                         $abstract = fetch_abstract($b["body"]);
376                 }
377
378         if ($abstract != "") {
379                 $post["text"] = $abstract;
380
381                 if ($post["type"] == "text") {
382                         $post["type"] = "link";
383                         $post["url"] = $b["plink"];
384                 }
385         }
386
387         $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
388         $msg = html2plain($html, 0, true);
389         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
390
391         $link = "";
392         if ($includedlinks) {
393                 if ($post["type"] == "link")
394                         $link = $post["url"];
395                 elseif ($post["type"] == "text")
396                         $link = $post["url"];
397                 elseif ($post["type"] == "video")
398                         $link = $post["url"];
399                 elseif ($post["type"] == "photo")
400                         $link = $post["image"];
401
402                 if (($msg == "") && isset($post["title"]))
403                         $msg = trim($post["title"]);
404
405                 if (($msg == "") && isset($post["description"]))
406                         $msg = trim($post["description"]);
407
408                 // If the link is already contained in the post, then it neeedn't to be added again
409                 // But: if the link is beyond the limit, then it has to be added.
410                 if (($link != "") && strstr($msg, $link)) {
411                         $pos = strpos($msg, $link);
412
413                         // Will the text be shortened in the link?
414                         // Or is the link the last item in the post?
415                         if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg))))
416                                 $msg = trim(str_replace($link, "", $msg));
417                         elseif (($limit == 0) || ($pos < $limit)) {
418                                 // The limit has to be increased since it will be shortened - but not now
419                                 // Only do it with Twitter (htmlmode = 8)
420                                 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8))
421                                         $limit = $limit - 23 + strlen($link);
422
423                                 $link = "";
424
425                                 if ($post["type"] == "text")
426                                         unset($post["url"]);
427                         }
428                 }
429         }
430
431         if ($limit > 0) {
432                 // Reduce multiple spaces
433                 // When posted to a network with limited space, we try to gain space where possible
434                 while (strpos($msg, "  ") !== false)
435                         $msg = str_replace("  ", " ", $msg);
436
437                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
438                 if (iconv_strlen($link, "UTF-8") > 0)
439                         $limit = $limit - 23;
440
441                 if (iconv_strlen($msg, "UTF-8") > $limit) {
442
443                         if (($post["type"] == "text") && isset($post["url"])) {
444                                 $post["url"] = $b["plink"];
445                         } elseif (!isset($post["url"])) {
446                                 $limit = $limit - 23;
447                                 $post["url"] = $b["plink"];
448                         // Which purpose has this line? It is now uncommented, but left as a reminder
449                         //} elseif (strpos($b["body"], "[share") !== false) {
450                         //      $post["url"] = $b["plink"];
451                         } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
452                                 $post["url"] = $b["plink"];
453                         }
454                         $msg = shortenmsg($msg, $limit);
455                 }
456         }
457
458         $post["text"] = trim($msg);
459
460         return($post);
461 }