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