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