]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
Merge branch 'develop' into improvement/move-app-to-src-2
[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) AND ($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"] == "") AND (in_array($post["type"], array("link", "video")))
68                                 AND 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) {
184 /*
185  - text:
186  - type: link, video, photo
187  - title:
188  - url:
189  - image:
190  - description:
191  - (thumbnail)
192 */
193
194         $post = get_attachment_data($body);
195
196         // if nothing is found, it maybe having an image.
197         if (!isset($post["type"])) {
198                 $URLSearchString = "^\[\]";
199                 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures,  PREG_SET_ORDER)) {
200                         if (count($pictures) == 1) {
201                                 // Checking, if the link goes to a picture
202                                 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
203
204                                 // Workaround:
205                                 // Sometimes photo posts to the own album are not detected at the start.
206                                 // So we seem to cannot use the cache for these cases. That's strange.
207                                 if (($data["type"] != "photo") AND strstr($pictures[0][1], "/photos/"))
208                                         $data = ParseUrl::getSiteinfo($pictures[0][1], true);
209
210                                 if ($data["type"] == "photo") {
211                                         $post["type"] = "photo";
212                                         if (isset($data["images"][0])) {
213                                                 $post["image"] = $data["images"][0]["src"];
214                                                 $post["url"] = $data["url"];
215                                         } else
216                                                 $post["image"] = $data["url"];
217
218                                         $post["preview"] = $pictures[0][2];
219                                         $post["text"] = str_replace($pictures[0][0], "", $body);
220                                 } else {
221                                         $imgdata = get_photo_info($pictures[0][1]);
222                                         if (substr($imgdata["mime"], 0, 6) == "image/") {
223                                                 $post["type"] = "photo";
224                                                 $post["image"] = $pictures[0][1];
225                                                 $post["preview"] = $pictures[0][2];
226                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
227                                         }
228                                 }
229                         } elseif (count($pictures) > 1) {
230                                 $post["type"] = "link";
231                                 $post["url"] = $b["plink"];
232                                 $post["image"] = $pictures[0][2];
233                                 $post["text"] = $body;
234                         }
235                 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures,  PREG_SET_ORDER)) {
236                         if (count($pictures) == 1) {
237                                 $post["type"] = "photo";
238                                 $post["image"] = $pictures[0][1];
239                                 $post["text"] = str_replace($pictures[0][0], "", $body);
240                         } elseif (count($pictures) > 1) {
241                                 $post["type"] = "link";
242                                 $post["url"] = $b["plink"];
243                                 $post["image"] = $pictures[0][1];
244                                 $post["text"] = $body;
245                         }
246                 }
247
248                 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links,  PREG_SET_ORDER)) {
249                         if (count($links) == 1) {
250                                 $post["type"] = "text";
251                                 $post["url"] = $links[0][1];
252                                 $post["text"] = $body;
253                         }
254                 }
255                 if (!isset($post["type"])) {
256                         $post["type"] = "text";
257                         $post["text"] = trim($body);
258                 }
259         } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
260                 $data = ParseUrl::getSiteinfoCached($post["url"], true);
261
262                 if (isset($data["images"][0]))
263                         $post["image"] = $data["images"][0]["src"];
264         }
265
266         return($post);
267 }
268
269 function shortenmsg($msg, $limit, $twitter = false) {
270         /// @TODO
271         /// For Twitter URLs aren't shortened, but they have to be calculated as if.
272
273         $lines = explode("\n", $msg);
274         $msg = "";
275         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
276         $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
277         foreach ($lines AS $row=>$line) {
278                 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
279                         $msg = trim($msg."\n".$line);
280                 // Is the new message empty by now or is it a reshared message?
281                 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
282                         $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
283                 else
284                         break;
285         }
286         return($msg);
287 }
288
289 /**
290  * @brief Convert a message into plaintext for connectors to other networks
291  *
292  * @param App $a The application class
293  * @param array $b The message array that is about to be posted
294  * @param int $limit The maximum number of characters when posting to that network
295  * @param bool $includedlinks Has an attached link to be included into the message?
296  * @param int $htmlmode This triggers the behaviour of the bbcode conversion
297  * @param string $target_network Name of the network where the post should go to.
298  *
299  * @return string The converted message
300  */
301 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
302
303         // Remove the hash tags
304         $URLSearchString = "^\[\]";
305         $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
306
307         // Add an URL element if the text contains a raw link
308         $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
309
310         // Remove the abstract
311         $body = remove_abstract($body);
312
313         // At first look at data that is attached via "type-..." stuff
314         // This will hopefully replaced with a dedicated bbcode later
315         //$post = get_attached_data($b["body"]);
316         $post = get_attached_data($body);
317
318         if (($b["title"] != "") AND ($post["text"] != ""))
319                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
320         elseif ($b["title"] != "")
321                 $post["text"] = trim($b["title"]);
322
323         $abstract = "";
324
325         // Fetch the abstract from the given target network
326         if ($target_network != "") {
327                 $default_abstract = fetch_abstract($b["body"]);
328                 $abstract = fetch_abstract($b["body"], $target_network);
329
330                 // If we post to a network with no limit we only fetch
331                 // an abstract exactly for this network
332                 if (($limit == 0) AND ($abstract == $default_abstract))
333                         $abstract = "";
334
335         } else // Try to guess the correct target network
336                 switch ($htmlmode) {
337                         case 8:
338                                 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
339                                 break;
340                         case 7:
341                                 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
342                                 break;
343                         case 6:
344                                 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
345                                 break;
346                         default: // We don't know the exact target.
347                                  // We fetch an abstract since there is a posting limit.
348                                 if ($limit > 0)
349                                         $abstract = fetch_abstract($b["body"]);
350                 }
351
352         if ($abstract != "") {
353                 $post["text"] = $abstract;
354
355                 if ($post["type"] == "text") {
356                         $post["type"] = "link";
357                         $post["url"] = $b["plink"];
358                 }
359         }
360
361         $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
362         $msg = html2plain($html, 0, true);
363         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
364
365         $link = "";
366         if ($includedlinks) {
367                 if ($post["type"] == "link")
368                         $link = $post["url"];
369                 elseif ($post["type"] == "text")
370                         $link = $post["url"];
371                 elseif ($post["type"] == "video")
372                         $link = $post["url"];
373                 elseif ($post["type"] == "photo")
374                         $link = $post["image"];
375
376                 if (($msg == "") AND isset($post["title"]))
377                         $msg = trim($post["title"]);
378
379                 if (($msg == "") AND isset($post["description"]))
380                         $msg = trim($post["description"]);
381
382                 // If the link is already contained in the post, then it neeedn't to be added again
383                 // But: if the link is beyond the limit, then it has to be added.
384                 if (($link != "") AND strstr($msg, $link)) {
385                         $pos = strpos($msg, $link);
386
387                         // Will the text be shortened in the link?
388                         // Or is the link the last item in the post?
389                         if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
390                                 $msg = trim(str_replace($link, "", $msg));
391                         elseif (($limit == 0) OR ($pos < $limit)) {
392                                 // The limit has to be increased since it will be shortened - but not now
393                                 // Only do it with Twitter (htmlmode = 8)
394                                 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
395                                         $limit = $limit - 23 + strlen($link);
396
397                                 $link = "";
398
399                                 if ($post["type"] == "text")
400                                         unset($post["url"]);
401                         }
402                 }
403         }
404
405         if ($limit > 0) {
406                 // Reduce multiple spaces
407                 // When posted to a network with limited space, we try to gain space where possible
408                 while (strpos($msg, "  ") !== false)
409                         $msg = str_replace("  ", " ", $msg);
410
411                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
412                 if (iconv_strlen($link, "UTF-8") > 0)
413                         $limit = $limit - 23;
414
415                 if (iconv_strlen($msg, "UTF-8") > $limit) {
416
417                         if (($post["type"] == "text") AND isset($post["url"]))
418                                 $post["url"] = $b["plink"];
419                         elseif (!isset($post["url"])) {
420                                 $limit = $limit - 23;
421                                 $post["url"] = $b["plink"];
422                         } elseif (strpos($b["body"], "[share") !== false)
423                                 $post["url"] = $b["plink"];
424                         elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
425                                 $post["url"] = $b["plink"];
426
427                         $msg = shortenmsg($msg, $limit);
428                 }
429         }
430
431         $post["text"] = trim($msg);
432
433         return($post);
434 }