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