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