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