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