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