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