4 * @file include/plaintext.php
8 use Friendica\ParseUrl;
10 require_once("include/Photo.php");
11 require_once("include/bbcode.php");
12 require_once("include/html2plain.php");
13 require_once("include/network.php");
16 * @brief Fetches attachment data that were generated the old way
18 * @param string $body Message body
20 * 'type' -> Message type ("link", "video", "photo")
21 * 'text' -> Text before the shared message
22 * 'after' -> Text after the shared message
23 * 'image' -> Preview image of the message
24 * 'url' -> Url to the attached message
25 * 'title' -> Title of the attachment
26 * 'description' -> Description of the attachment
28 function get_old_attachment_data($body) {
32 // Simplify image codes
33 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
35 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
36 foreach ($attached AS $data) {
37 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
40 $post["type"] = substr($data[1], 5);
42 $pos = strpos($body, $data[0]);
44 $post["text"] = trim(substr($body, 0, $pos));
45 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
47 $post["text"] = trim(str_replace($data[0], "", $body));
49 $attacheddata = $data[2];
51 $URLSearchString = "^\[\]";
53 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
55 $picturedata = get_photo_info($matches[1]);
57 if (($picturedata[0] >= 500) AND ($picturedata[0] >= $picturedata[1]))
58 $post["image"] = $matches[1];
60 $post["preview"] = $matches[1];
63 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
64 $post["url"] = $matches[1];
65 $post["title"] = $matches[2];
67 if (($post["url"] == "") AND (in_array($post["type"], array("link", "video")))
68 AND preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
69 $post["url"] = $matches[1];
72 // Search for description
73 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
74 $post["description"] = $matches[1];
83 * @brief Fetches attachment data that were generated with the "attachment" element
85 * @param string $body Message body
87 * 'type' -> Message type ("link", "video", "photo")
88 * 'text' -> Text before the shared message
89 * 'after' -> Text after the shared message
90 * 'image' -> Preview image of the message
91 * 'url' -> Url to the attached message
92 * 'title' -> Title of the attachment
93 * 'description' -> Description of the attachment
95 function get_attachment_data($body) {
99 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
100 return get_old_attachment_data($body);
102 $attributes = $match[2];
104 $data["text"] = trim($match[1]);
107 preg_match("/type='(.*?)'/ism", $attributes, $matches);
108 if ($matches[1] != "")
109 $type = strtolower($matches[1]);
111 preg_match('/type="(.*?)"/ism', $attributes, $matches);
112 if ($matches[1] != "")
113 $type = strtolower($matches[1]);
118 if (!in_array($type, array("link", "audio", "photo", "video")))
122 $data["type"] = $type;
125 preg_match("/url='(.*?)'/ism", $attributes, $matches);
126 if ($matches[1] != "")
129 preg_match('/url="(.*?)"/ism', $attributes, $matches);
130 if ($matches[1] != "")
134 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
137 preg_match("/title='(.*?)'/ism", $attributes, $matches);
138 if ($matches[1] != "")
139 $title = $matches[1];
141 preg_match('/title="(.*?)"/ism', $attributes, $matches);
142 if ($matches[1] != "")
143 $title = $matches[1];
146 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
147 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
148 $title = str_replace(array("[", "]"), array("[", "]"), $title);
149 $data["title"] = $title;
153 preg_match("/image='(.*?)'/ism", $attributes, $matches);
154 if ($matches[1] != "")
155 $image = $matches[1];
157 preg_match('/image="(.*?)"/ism', $attributes, $matches);
158 if ($matches[1] != "")
159 $image = $matches[1];
162 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
165 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
166 if ($matches[1] != "")
167 $preview = $matches[1];
169 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
170 if ($matches[1] != "")
171 $preview = $matches[1];
174 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
176 $data["description"] = trim($match[3]);
178 $data["after"] = trim($match[4]);
183 function get_attached_data($body) {
186 - type: link, video, photo
194 $post = get_attachment_data($body);
196 // if nothing is found, it maybe having an image.
197 if (!isset($post["type"])) {
198 $URLSearchString = "^\[\]";
199 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
200 if (count($pictures) == 1) {
201 // Checking, if the link goes to a picture
202 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
205 // Sometimes photo posts to the own album are not detected at the start.
206 // So we seem to cannot use the cache for these cases. That's strange.
207 if (($data["type"] != "photo") AND strstr($pictures[0][1], "/photos/"))
208 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
210 if ($data["type"] == "photo") {
211 $post["type"] = "photo";
212 if (isset($data["images"][0])) {
213 $post["image"] = $data["images"][0]["src"];
214 $post["url"] = $data["url"];
216 $post["image"] = $data["url"];
218 $post["preview"] = $pictures[0][2];
219 $post["text"] = str_replace($pictures[0][0], "", $body);
221 $imgdata = get_photo_info($pictures[0][1]);
222 if (substr($imgdata["mime"], 0, 6) == "image/") {
223 $post["type"] = "photo";
224 $post["image"] = $pictures[0][1];
225 $post["preview"] = $pictures[0][2];
226 $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][2];
233 $post["text"] = $body;
235 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
236 if (count($pictures) == 1) {
237 $post["type"] = "photo";
238 $post["image"] = $pictures[0][1];
239 $post["text"] = str_replace($pictures[0][0], "", $body);
240 } elseif (count($pictures) > 1) {
241 $post["type"] = "link";
242 $post["url"] = $b["plink"];
243 $post["image"] = $pictures[0][1];
244 $post["text"] = $body;
248 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links, PREG_SET_ORDER)) {
249 if (count($links) == 1) {
250 $post["type"] = "text";
251 $post["url"] = $links[0][1];
252 $post["text"] = $body;
255 if (!isset($post["type"])) {
256 $post["type"] = "text";
257 $post["text"] = trim($body);
259 } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
260 $data = ParseUrl::getSiteinfoCached($post["url"], true);
262 if (isset($data["images"][0]))
263 $post["image"] = $data["images"][0]["src"];
269 function shortenmsg($msg, $limit, $twitter = false) {
271 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
273 $lines = explode("\n", $msg);
275 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
276 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
277 foreach ($lines AS $row=>$line) {
278 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
279 $msg = trim($msg."\n".$line);
280 // Is the new message empty by now or is it a reshared message?
281 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
282 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
290 * @brief Convert a message into plaintext for connectors to other networks
292 * @param App $a The application class
293 * @param array $b The message array that is about to be posted
294 * @param int $limit The maximum number of characters when posting to that network
295 * @param bool $includedlinks Has an attached link to be included into the message?
296 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
297 * @param string $target_network Name of the network where the post should go to.
299 * @return string The converted message
301 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
303 // Remove the hash tags
304 $URLSearchString = "^\[\]";
305 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
307 // Add an URL element if the text contains a raw link
308 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
310 // Remove the abstract
311 $body = remove_abstract($body);
313 // At first look at data that is attached via "type-..." stuff
314 // This will hopefully replaced with a dedicated bbcode later
315 //$post = get_attached_data($b["body"]);
316 $post = get_attached_data($body);
318 if (($b["title"] != "") AND ($post["text"] != ""))
319 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
320 elseif ($b["title"] != "")
321 $post["text"] = trim($b["title"]);
325 // Fetch the abstract from the given target network
326 if ($target_network != "") {
327 $default_abstract = fetch_abstract($b["body"]);
328 $abstract = fetch_abstract($b["body"], $target_network);
330 // If we post to a network with no limit we only fetch
331 // an abstract exactly for this network
332 if (($limit == 0) AND ($abstract == $default_abstract))
335 } else // Try to guess the correct target network
338 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
341 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
344 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
346 default: // We don't know the exact target.
347 // We fetch an abstract since there is a posting limit.
349 $abstract = fetch_abstract($b["body"]);
352 if ($abstract != "") {
353 $post["text"] = $abstract;
355 if ($post["type"] == "text") {
356 $post["type"] = "link";
357 $post["url"] = $b["plink"];
361 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
362 $msg = html2plain($html, 0, true);
363 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
366 if ($includedlinks) {
367 if ($post["type"] == "link")
368 $link = $post["url"];
369 elseif ($post["type"] == "text")
370 $link = $post["url"];
371 elseif ($post["type"] == "video")
372 $link = $post["url"];
373 elseif ($post["type"] == "photo")
374 $link = $post["image"];
376 if (($msg == "") AND isset($post["title"]))
377 $msg = trim($post["title"]);
379 if (($msg == "") AND isset($post["description"]))
380 $msg = trim($post["description"]);
382 // If the link is already contained in the post, then it neeedn't to be added again
383 // But: if the link is beyond the limit, then it has to be added.
384 if (($link != "") AND strstr($msg, $link)) {
385 $pos = strpos($msg, $link);
387 // Will the text be shortened in the link?
388 // Or is the link the last item in the post?
389 if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
390 $msg = trim(str_replace($link, "", $msg));
391 elseif (($limit == 0) OR ($pos < $limit)) {
392 // The limit has to be increased since it will be shortened - but not now
393 // Only do it with Twitter (htmlmode = 8)
394 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
395 $limit = $limit - 23 + strlen($link);
399 if ($post["type"] == "text")
406 // Reduce multiple spaces
407 // When posted to a network with limited space, we try to gain space where possible
408 while (strpos($msg, " ") !== false)
409 $msg = str_replace(" ", " ", $msg);
411 // Twitter is using its own limiter, so we always assume that shortened links will have this length
412 if (iconv_strlen($link, "UTF-8") > 0)
413 $limit = $limit - 23;
415 if (iconv_strlen($msg, "UTF-8") > $limit) {
417 if (($post["type"] == "text") AND isset($post["url"]))
418 $post["url"] = $b["plink"];
419 elseif (!isset($post["url"])) {
420 $limit = $limit - 23;
421 $post["url"] = $b["plink"];
422 } elseif (strpos($b["body"], "[share") !== false)
423 $post["url"] = $b["plink"];
424 elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
425 $post["url"] = $b["plink"];
427 $msg = shortenmsg($msg, $limit);
431 $post["text"] = trim($msg);