3 * @file include/plaintext.php
6 use Friendica\ParseUrl;
7 use Friendica\Core\PConfig;
8 use Friendica\Object\Photo;
10 require_once "include/bbcode.php";
11 require_once "include/html2plain.php";
12 require_once "include/network.php";
15 * @brief Fetches attachment data that were generated the old way
17 * @param string $body Message body
19 * 'type' -> Message type ("link", "video", "photo")
20 * 'text' -> Text before the shared message
21 * 'after' -> Text after the shared message
22 * 'image' -> Preview image of the message
23 * 'url' -> Url to the attached message
24 * 'title' -> Title of the attachment
25 * 'description' -> Description of the attachment
27 function get_old_attachment_data($body) {
31 // Simplify image codes
32 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
34 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
35 foreach ($attached AS $data) {
36 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
39 $post["type"] = substr($data[1], 5);
41 $pos = strpos($body, $data[0]);
43 $post["text"] = trim(substr($body, 0, $pos));
44 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
46 $post["text"] = trim(str_replace($data[0], "", $body));
48 $attacheddata = $data[2];
50 $URLSearchString = "^\[\]";
52 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
54 $picturedata = Photo::getInfoFromURL($matches[1]);
56 if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1]))
57 $post["image"] = $matches[1];
59 $post["preview"] = $matches[1];
62 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
63 $post["url"] = $matches[1];
64 $post["title"] = $matches[2];
66 if (($post["url"] == "") && (in_array($post["type"], array("link", "video")))
67 && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
68 $post["url"] = $matches[1];
71 // Search for description
72 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
73 $post["description"] = $matches[1];
81 * @brief Fetches attachment data that were generated with the "attachment" element
83 * @param string $body Message body
85 * 'type' -> Message type ("link", "video", "photo")
86 * 'text' -> Text before the shared message
87 * 'after' -> Text after the shared message
88 * 'image' -> Preview image of the message
89 * 'url' -> Url to the attached message
90 * 'title' -> Title of the attachment
91 * 'description' -> Description of the attachment
93 function get_attachment_data($body) {
97 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
98 return get_old_attachment_data($body);
100 $attributes = $match[2];
102 $data["text"] = trim($match[1]);
105 preg_match("/type='(.*?)'/ism", $attributes, $matches);
106 if ($matches[1] != "")
107 $type = strtolower($matches[1]);
109 preg_match('/type="(.*?)"/ism', $attributes, $matches);
110 if ($matches[1] != "")
111 $type = strtolower($matches[1]);
116 if (!in_array($type, array("link", "audio", "photo", "video")))
120 $data["type"] = $type;
123 preg_match("/url='(.*?)'/ism", $attributes, $matches);
124 if ($matches[1] != "")
127 preg_match('/url="(.*?)"/ism', $attributes, $matches);
128 if ($matches[1] != "")
132 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
135 preg_match("/title='(.*?)'/ism", $attributes, $matches);
136 if ($matches[1] != "")
137 $title = $matches[1];
139 preg_match('/title="(.*?)"/ism', $attributes, $matches);
140 if ($matches[1] != "")
141 $title = $matches[1];
144 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
145 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
146 $title = str_replace(array("[", "]"), array("[", "]"), $title);
147 $data["title"] = $title;
151 preg_match("/image='(.*?)'/ism", $attributes, $matches);
152 if ($matches[1] != "")
153 $image = $matches[1];
155 preg_match('/image="(.*?)"/ism', $attributes, $matches);
156 if ($matches[1] != "")
157 $image = $matches[1];
160 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
163 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
164 if ($matches[1] != "")
165 $preview = $matches[1];
167 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
168 if ($matches[1] != "")
169 $preview = $matches[1];
172 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
174 $data["description"] = trim($match[3]);
176 $data["after"] = trim($match[4]);
181 function get_attached_data($body, $item = array()) {
184 - type: link, video, photo
192 $has_title = !empty($item['title']);
193 $plink = (!empty($item['plink']) ? $item['plink'] : '');
194 $post = get_attachment_data($body);
196 // if nothing is found, it maybe having an image.
197 if (!isset($post["type"])) {
198 // Simplify image codes
199 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
201 $URLSearchString = "^\[\]";
202 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
203 if ((count($pictures) == 1) && !$has_title) {
204 // Checking, if the link goes to a picture
205 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
208 // Sometimes photo posts to the own album are not detected at the start.
209 // So we seem to cannot use the cache for these cases. That's strange.
210 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/"))
211 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
213 if ($data["type"] == "photo") {
214 $post["type"] = "photo";
215 if (isset($data["images"][0])) {
216 $post["image"] = $data["images"][0]["src"];
217 $post["url"] = $data["url"];
219 $post["image"] = $data["url"];
221 $post["preview"] = $pictures[0][2];
222 $post["text"] = str_replace($pictures[0][0], "", $body);
224 $imgdata = Photo::getInfoFromURL($pictures[0][1]);
225 if (substr($imgdata["mime"], 0, 6) == "image/") {
226 $post["type"] = "photo";
227 $post["image"] = $pictures[0][1];
228 $post["preview"] = $pictures[0][2];
229 $post["text"] = str_replace($pictures[0][0], "", $body);
232 } elseif (count($pictures) > 0) {
233 $post["type"] = "link";
234 $post["url"] = $plink;
235 $post["image"] = $pictures[0][2];
236 $post["text"] = $body;
238 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
239 if ((count($pictures) == 1) && !$has_title) {
240 $post["type"] = "photo";
241 $post["image"] = $pictures[0][1];
242 $post["text"] = str_replace($pictures[0][0], "", $body);
243 } elseif (count($pictures) > 0) {
244 $post["type"] = "link";
245 $post["url"] = $plink;
246 $post["image"] = $pictures[0][1];
247 $post["text"] = $body;
251 // Test for the external links
252 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
253 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
255 $links = array_merge($links1, $links2);
257 // If there is only a single one, then use it.
258 // This should cover link posts via API.
259 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
260 $post["type"] = "link";
261 $post["text"] = trim($body);
262 $post["url"] = $links[0][1];
265 // Now count the number of external media links
266 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
267 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
268 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
269 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
271 // Add them to the other external links
272 $links = array_merge($links, $links1, $links2, $links3, $links4);
274 // Are there more than one?
275 if (count($links) > 1) {
276 // The post will be the type "text", which means a blog post
277 unset($post["type"]);
278 $post["url"] = $plink;
281 if (!isset($post["type"])) {
282 $post["type"] = "text";
283 $post["text"] = trim($body);
285 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
286 $data = ParseUrl::getSiteinfoCached($post["url"], true);
288 if (isset($data["images"][0]))
289 $post["image"] = $data["images"][0]["src"];
295 function shortenmsg($msg, $limit, $twitter = false) {
297 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
299 $lines = explode("\n", $msg);
301 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
302 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
303 foreach ($lines AS $row=>$line) {
304 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
305 $msg = trim($msg."\n".$line);
306 // Is the new message empty by now or is it a reshared message?
307 elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle)))
308 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
316 * @brief Convert a message into plaintext for connectors to other networks
318 * @param App $a The application class
319 * @param array $b The message array that is about to be posted
320 * @param int $limit The maximum number of characters when posting to that network
321 * @param bool $includedlinks Has an attached link to be included into the message?
322 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
323 * @param string $target_network Name of the network where the post should go to.
325 * @return string The converted message
327 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
329 // Remove the hash tags
330 $URLSearchString = "^\[\]";
331 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
333 // Add an URL element if the text contains a raw link
334 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
336 // Remove the abstract
337 $body = remove_abstract($body);
339 // At first look at data that is attached via "type-..." stuff
340 // This will hopefully replaced with a dedicated bbcode later
341 //$post = get_attached_data($b["body"]);
342 $post = get_attached_data($body, $b);
344 if (($b["title"] != "") && ($post["text"] != ""))
345 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
346 elseif ($b["title"] != "")
347 $post["text"] = trim($b["title"]);
351 // Fetch the abstract from the given target network
352 if ($target_network != "") {
353 $default_abstract = fetch_abstract($b["body"]);
354 $abstract = fetch_abstract($b["body"], $target_network);
356 // If we post to a network with no limit we only fetch
357 // an abstract exactly for this network
358 if (($limit == 0) && ($abstract == $default_abstract))
361 } else // Try to guess the correct target network
364 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
367 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
370 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
372 default: // We don't know the exact target.
373 // We fetch an abstract since there is a posting limit.
375 $abstract = fetch_abstract($b["body"]);
378 if ($abstract != "") {
379 $post["text"] = $abstract;
381 if ($post["type"] == "text") {
382 $post["type"] = "link";
383 $post["url"] = $b["plink"];
387 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
388 $msg = html2plain($html, 0, true);
389 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
392 if ($includedlinks) {
393 if ($post["type"] == "link")
394 $link = $post["url"];
395 elseif ($post["type"] == "text")
396 $link = $post["url"];
397 elseif ($post["type"] == "video")
398 $link = $post["url"];
399 elseif ($post["type"] == "photo")
400 $link = $post["image"];
402 if (($msg == "") && isset($post["title"]))
403 $msg = trim($post["title"]);
405 if (($msg == "") && isset($post["description"]))
406 $msg = trim($post["description"]);
408 // If the link is already contained in the post, then it neeedn't to be added again
409 // But: if the link is beyond the limit, then it has to be added.
410 if (($link != "") && strstr($msg, $link)) {
411 $pos = strpos($msg, $link);
413 // Will the text be shortened in the link?
414 // Or is the link the last item in the post?
415 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg))))
416 $msg = trim(str_replace($link, "", $msg));
417 elseif (($limit == 0) || ($pos < $limit)) {
418 // The limit has to be increased since it will be shortened - but not now
419 // Only do it with Twitter (htmlmode = 8)
420 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8))
421 $limit = $limit - 23 + strlen($link);
425 if ($post["type"] == "text")
432 // Reduce multiple spaces
433 // When posted to a network with limited space, we try to gain space where possible
434 while (strpos($msg, " ") !== false)
435 $msg = str_replace(" ", " ", $msg);
437 // Twitter is using its own limiter, so we always assume that shortened links will have this length
438 if (iconv_strlen($link, "UTF-8") > 0)
439 $limit = $limit - 23;
441 if (iconv_strlen($msg, "UTF-8") > $limit) {
443 if (($post["type"] == "text") && isset($post["url"])) {
444 $post["url"] = $b["plink"];
445 } elseif (!isset($post["url"])) {
446 $limit = $limit - 23;
447 $post["url"] = $b["plink"];
448 // Which purpose has this line? It is now uncommented, but left as a reminder
449 //} elseif (strpos($b["body"], "[share") !== false) {
450 // $post["url"] = $b["plink"];
451 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
452 $post["url"] = $b["plink"];
454 $msg = shortenmsg($msg, $limit);
458 $post["text"] = trim($msg);