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) && ($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"] == "") && (in_array($post["type"], array("link", "video")))
68 && 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];
82 * @brief Fetches attachment data that were generated with the "attachment" element
84 * @param string $body Message body
86 * 'type' -> Message type ("link", "video", "photo")
87 * 'text' -> Text before the shared message
88 * 'after' -> Text after the shared message
89 * 'image' -> Preview image of the message
90 * 'url' -> Url to the attached message
91 * 'title' -> Title of the attachment
92 * 'description' -> Description of the attachment
94 function get_attachment_data($body) {
98 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
99 return get_old_attachment_data($body);
101 $attributes = $match[2];
103 $data["text"] = trim($match[1]);
106 preg_match("/type='(.*?)'/ism", $attributes, $matches);
107 if ($matches[1] != "")
108 $type = strtolower($matches[1]);
110 preg_match('/type="(.*?)"/ism', $attributes, $matches);
111 if ($matches[1] != "")
112 $type = strtolower($matches[1]);
117 if (!in_array($type, array("link", "audio", "photo", "video")))
121 $data["type"] = $type;
124 preg_match("/url='(.*?)'/ism", $attributes, $matches);
125 if ($matches[1] != "")
128 preg_match('/url="(.*?)"/ism', $attributes, $matches);
129 if ($matches[1] != "")
133 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
136 preg_match("/title='(.*?)'/ism", $attributes, $matches);
137 if ($matches[1] != "")
138 $title = $matches[1];
140 preg_match('/title="(.*?)"/ism', $attributes, $matches);
141 if ($matches[1] != "")
142 $title = $matches[1];
145 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
146 $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
147 $title = str_replace(array("[", "]"), array("[", "]"), $title);
148 $data["title"] = $title;
152 preg_match("/image='(.*?)'/ism", $attributes, $matches);
153 if ($matches[1] != "")
154 $image = $matches[1];
156 preg_match('/image="(.*?)"/ism', $attributes, $matches);
157 if ($matches[1] != "")
158 $image = $matches[1];
161 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
164 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
165 if ($matches[1] != "")
166 $preview = $matches[1];
168 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
169 if ($matches[1] != "")
170 $preview = $matches[1];
173 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
175 $data["description"] = trim($match[3]);
177 $data["after"] = trim($match[4]);
182 function get_attached_data($body, $item = array()) {
185 - type: link, video, photo
193 $has_title = !empty($item['title']);
194 $plink = (!empty($item['plink']) ? $item['plink'] : '');
195 $post = get_attachment_data($body);
197 // if nothing is found, it maybe having an image.
198 if (!isset($post["type"])) {
199 // Simplify image codes
200 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
202 $URLSearchString = "^\[\]";
203 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
204 if ((count($pictures) == 1) && !$has_title) {
205 // Checking, if the link goes to a picture
206 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
209 // Sometimes photo posts to the own album are not detected at the start.
210 // So we seem to cannot use the cache for these cases. That's strange.
211 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/"))
212 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
214 if ($data["type"] == "photo") {
215 $post["type"] = "photo";
216 if (isset($data["images"][0])) {
217 $post["image"] = $data["images"][0]["src"];
218 $post["url"] = $data["url"];
220 $post["image"] = $data["url"];
222 $post["preview"] = $pictures[0][2];
223 $post["text"] = str_replace($pictures[0][0], "", $body);
225 $imgdata = get_photo_info($pictures[0][1]);
226 if (substr($imgdata["mime"], 0, 6) == "image/") {
227 $post["type"] = "photo";
228 $post["image"] = $pictures[0][1];
229 $post["preview"] = $pictures[0][2];
230 $post["text"] = str_replace($pictures[0][0], "", $body);
233 } elseif (count($pictures) > 0) {
234 $post["type"] = "link";
235 $post["url"] = $plink;
236 $post["image"] = $pictures[0][2];
237 $post["text"] = $body;
239 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
240 if ((count($pictures) == 1) && !$has_title) {
241 $post["type"] = "photo";
242 $post["image"] = $pictures[0][1];
243 $post["text"] = str_replace($pictures[0][0], "", $body);
244 } elseif (count($pictures) > 0) {
245 $post["type"] = "link";
246 $post["url"] = $plink;
247 $post["image"] = $pictures[0][1];
248 $post["text"] = $body;
252 // Test for the external links
253 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
254 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
256 $links = array_merge($links1, $links2);
258 // If there is only a single one, then use it.
259 // This should cover link posts via API.
260 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
261 $post["type"] = "link";
262 $post["text"] = trim($body);
263 $post["url"] = $links[0][1];
266 // Now count the number of external media links
267 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
268 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
269 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
270 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
272 // Add them to the other external links
273 $links = array_merge($links, $links1, $links2, $links3, $links4);
275 // Are there more than one?
276 if (count($links) > 1) {
277 // The post will be the type "text", which means a blog post
278 unset($post["type"]);
279 $post["url"] = $plink;
282 if (!isset($post["type"])) {
283 $post["type"] = "text";
284 $post["text"] = trim($body);
286 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
287 $data = ParseUrl::getSiteinfoCached($post["url"], true);
289 if (isset($data["images"][0]))
290 $post["image"] = $data["images"][0]["src"];
296 function shortenmsg($msg, $limit, $twitter = false) {
298 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
300 $lines = explode("\n", $msg);
302 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
303 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
304 foreach ($lines AS $row=>$line) {
305 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
306 $msg = trim($msg."\n".$line);
307 // Is the new message empty by now or is it a reshared message?
308 elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle)))
309 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
317 * @brief Convert a message into plaintext for connectors to other networks
319 * @param App $a The application class
320 * @param array $b The message array that is about to be posted
321 * @param int $limit The maximum number of characters when posting to that network
322 * @param bool $includedlinks Has an attached link to be included into the message?
323 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
324 * @param string $target_network Name of the network where the post should go to.
326 * @return string The converted message
328 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
330 // Remove the hash tags
331 $URLSearchString = "^\[\]";
332 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
334 // Add an URL element if the text contains a raw link
335 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
337 // Remove the abstract
338 $body = remove_abstract($body);
340 // At first look at data that is attached via "type-..." stuff
341 // This will hopefully replaced with a dedicated bbcode later
342 //$post = get_attached_data($b["body"]);
343 $post = get_attached_data($body, $b);
345 if (($b["title"] != "") && ($post["text"] != ""))
346 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
347 elseif ($b["title"] != "")
348 $post["text"] = trim($b["title"]);
352 // Fetch the abstract from the given target network
353 if ($target_network != "") {
354 $default_abstract = fetch_abstract($b["body"]);
355 $abstract = fetch_abstract($b["body"], $target_network);
357 // If we post to a network with no limit we only fetch
358 // an abstract exactly for this network
359 if (($limit == 0) && ($abstract == $default_abstract))
362 } else // Try to guess the correct target network
365 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
368 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
371 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
373 default: // We don't know the exact target.
374 // We fetch an abstract since there is a posting limit.
376 $abstract = fetch_abstract($b["body"]);
379 if ($abstract != "") {
380 $post["text"] = $abstract;
382 if ($post["type"] == "text") {
383 $post["type"] = "link";
384 $post["url"] = $b["plink"];
388 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
389 $msg = html2plain($html, 0, true);
390 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
393 if ($includedlinks) {
394 if ($post["type"] == "link")
395 $link = $post["url"];
396 elseif ($post["type"] == "text")
397 $link = $post["url"];
398 elseif ($post["type"] == "video")
399 $link = $post["url"];
400 elseif ($post["type"] == "photo")
401 $link = $post["image"];
403 if (($msg == "") && isset($post["title"]))
404 $msg = trim($post["title"]);
406 if (($msg == "") && isset($post["description"]))
407 $msg = trim($post["description"]);
409 // If the link is already contained in the post, then it neeedn't to be added again
410 // But: if the link is beyond the limit, then it has to be added.
411 if (($link != "") && strstr($msg, $link)) {
412 $pos = strpos($msg, $link);
414 // Will the text be shortened in the link?
415 // Or is the link the last item in the post?
416 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg))))
417 $msg = trim(str_replace($link, "", $msg));
418 elseif (($limit == 0) || ($pos < $limit)) {
419 // The limit has to be increased since it will be shortened - but not now
420 // Only do it with Twitter (htmlmode = 8)
421 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8))
422 $limit = $limit - 23 + strlen($link);
426 if ($post["type"] == "text")
433 // Reduce multiple spaces
434 // When posted to a network with limited space, we try to gain space where possible
435 while (strpos($msg, " ") !== false)
436 $msg = str_replace(" ", " ", $msg);
438 // Twitter is using its own limiter, so we always assume that shortened links will have this length
439 if (iconv_strlen($link, "UTF-8") > 0)
440 $limit = $limit - 23;
442 if (iconv_strlen($msg, "UTF-8") > $limit) {
444 if (($post["type"] == "text") && isset($post["url"])) {
445 $post["url"] = $b["plink"];
446 } elseif (!isset($post["url"])) {
447 $limit = $limit - 23;
448 $post["url"] = $b["plink"];
449 // Which purpose has this line? It is now uncommented, but left as a reminder
450 //} elseif (strpos($b["body"], "[share") !== false) {
451 // $post["url"] = $b["plink"];
452 } elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening")) {
453 $post["url"] = $b["plink"];
455 $msg = shortenmsg($msg, $limit);
459 $post["text"] = trim($msg);