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