4 * @file include/plaintext.php
8 use Friendica\ParseUrl;
9 use Friendica\Core\PConfig;
11 require_once("include/Photo.php");
12 require_once("include/bbcode.php");
13 require_once("include/html2plain.php");
14 require_once("include/network.php");
17 * @brief Fetches attachment data that were generated the old way
19 * @param string $body Message body
21 * 'type' -> Message type ("link", "video", "photo")
22 * 'text' -> Text before the shared message
23 * 'after' -> Text after the shared message
24 * 'image' -> Preview image of the message
25 * 'url' -> Url to the attached message
26 * 'title' -> Title of the attachment
27 * 'description' -> Description of the attachment
29 function get_old_attachment_data($body) {
33 // Simplify image codes
34 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
36 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
37 foreach ($attached AS $data) {
38 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
41 $post["type"] = substr($data[1], 5);
43 $pos = strpos($body, $data[0]);
45 $post["text"] = trim(substr($body, 0, $pos));
46 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
48 $post["text"] = trim(str_replace($data[0], "", $body));
50 $attacheddata = $data[2];
52 $URLSearchString = "^\[\]";
54 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
56 $picturedata = get_photo_info($matches[1]);
58 if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1]))
59 $post["image"] = $matches[1];
61 $post["preview"] = $matches[1];
64 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
65 $post["url"] = $matches[1];
66 $post["title"] = $matches[2];
68 if (($post["url"] == "") && (in_array($post["type"], array("link", "video")))
69 && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
70 $post["url"] = $matches[1];
73 // Search for description
74 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
75 $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, $item = array()) {
186 - type: link, video, photo
194 $has_title = !empty($item['title']);
195 $plink = (!empty($item['plink']) ? $item['plink'] : '');
196 $post = get_attachment_data($body);
198 // if nothing is found, it maybe having an image.
199 if (!isset($post["type"])) {
200 // Simplify image codes
201 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
203 $URLSearchString = "^\[\]";
204 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
205 if ((count($pictures) == 1) && !$has_title) {
206 // Checking, if the link goes to a picture
207 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
210 // Sometimes photo posts to the own album are not detected at the start.
211 // So we seem to cannot use the cache for these cases. That's strange.
212 if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/"))
213 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
215 if ($data["type"] == "photo") {
216 $post["type"] = "photo";
217 if (isset($data["images"][0])) {
218 $post["image"] = $data["images"][0]["src"];
219 $post["url"] = $data["url"];
221 $post["image"] = $data["url"];
223 $post["preview"] = $pictures[0][2];
224 $post["text"] = str_replace($pictures[0][0], "", $body);
226 $imgdata = get_photo_info($pictures[0][1]);
227 if (substr($imgdata["mime"], 0, 6) == "image/") {
228 $post["type"] = "photo";
229 $post["image"] = $pictures[0][1];
230 $post["preview"] = $pictures[0][2];
231 $post["text"] = str_replace($pictures[0][0], "", $body);
234 } elseif (count($pictures) > 0) {
235 $post["type"] = "link";
236 $post["url"] = $plink;
237 $post["image"] = $pictures[0][2];
238 $post["text"] = $body;
240 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
241 if ((count($pictures) == 1) && !$has_title) {
242 $post["type"] = "photo";
243 $post["image"] = $pictures[0][1];
244 $post["text"] = str_replace($pictures[0][0], "", $body);
245 } elseif (count($pictures) > 0) {
246 $post["type"] = "link";
247 $post["url"] = $plink;
248 $post["image"] = $pictures[0][1];
249 $post["text"] = $body;
253 // Test for the external links
254 preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
255 preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
257 $links = array_merge($links1, $links2);
259 // If there is only a single one, then use it.
260 // This should cover link posts via API.
261 if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
262 $post["type"] = "link";
263 $post["text"] = trim($body);
264 $post["url"] = $links[0][1];
267 // Now count the number of external media links
268 preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
269 preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
270 preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
271 preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
273 // Add them to the other external links
274 $links = array_merge($links, $links1, $links2, $links3, $links4);
276 // Are there more than one?
277 if (count($links) > 1) {
278 // The post will be the type "text", which means a blog post
279 unset($post["type"]);
280 $post["url"] = $plink;
283 if (!isset($post["type"])) {
284 $post["type"] = "text";
285 $post["text"] = trim($body);
287 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
288 $data = ParseUrl::getSiteinfoCached($post["url"], true);
290 if (isset($data["images"][0]))
291 $post["image"] = $data["images"][0]["src"];
297 function shortenmsg($msg, $limit, $twitter = false) {
299 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
301 $lines = explode("\n", $msg);
303 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
304 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
305 foreach ($lines AS $row=>$line) {
306 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
307 $msg = trim($msg."\n".$line);
308 // Is the new message empty by now or is it a reshared message?
309 elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle)))
310 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
318 * @brief Convert a message into plaintext for connectors to other networks
320 * @param App $a The application class
321 * @param array $b The message array that is about to be posted
322 * @param int $limit The maximum number of characters when posting to that network
323 * @param bool $includedlinks Has an attached link to be included into the message?
324 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
325 * @param string $target_network Name of the network where the post should go to.
327 * @return string The converted message
329 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
331 // Remove the hash tags
332 $URLSearchString = "^\[\]";
333 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
335 // Add an URL element if the text contains a raw link
336 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
338 // Remove the abstract
339 $body = remove_abstract($body);
341 // At first look at data that is attached via "type-..." stuff
342 // This will hopefully replaced with a dedicated bbcode later
343 //$post = get_attached_data($b["body"]);
344 $post = get_attached_data($body, $b);
346 if (($b["title"] != "") && ($post["text"] != ""))
347 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
348 elseif ($b["title"] != "")
349 $post["text"] = trim($b["title"]);
353 // Fetch the abstract from the given target network
354 if ($target_network != "") {
355 $default_abstract = fetch_abstract($b["body"]);
356 $abstract = fetch_abstract($b["body"], $target_network);
358 // If we post to a network with no limit we only fetch
359 // an abstract exactly for this network
360 if (($limit == 0) && ($abstract == $default_abstract))
363 } else // Try to guess the correct target network
366 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
369 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
372 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
374 default: // We don't know the exact target.
375 // We fetch an abstract since there is a posting limit.
377 $abstract = fetch_abstract($b["body"]);
380 if ($abstract != "") {
381 $post["text"] = $abstract;
383 if ($post["type"] == "text") {
384 $post["type"] = "link";
385 $post["url"] = $b["plink"];
389 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
390 $msg = html2plain($html, 0, true);
391 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
394 if ($includedlinks) {
395 if ($post["type"] == "link")
396 $link = $post["url"];
397 elseif ($post["type"] == "text")
398 $link = $post["url"];
399 elseif ($post["type"] == "video")
400 $link = $post["url"];
401 elseif ($post["type"] == "photo")
402 $link = $post["image"];
404 if (($msg == "") && isset($post["title"]))
405 $msg = trim($post["title"]);
407 if (($msg == "") && isset($post["description"]))
408 $msg = trim($post["description"]);
410 // If the link is already contained in the post, then it neeedn't to be added again
411 // But: if the link is beyond the limit, then it has to be added.
412 if (($link != "") && strstr($msg, $link)) {
413 $pos = strpos($msg, $link);
415 // Will the text be shortened in the link?
416 // Or is the link the last item in the post?
417 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg))))
418 $msg = trim(str_replace($link, "", $msg));
419 elseif (($limit == 0) || ($pos < $limit)) {
420 // The limit has to be increased since it will be shortened - but not now
421 // Only do it with Twitter (htmlmode = 8)
422 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8))
423 $limit = $limit - 23 + strlen($link);
427 if ($post["type"] == "text")
434 // Reduce multiple spaces
435 // When posted to a network with limited space, we try to gain space where possible
436 while (strpos($msg, " ") !== false)
437 $msg = str_replace(" ", " ", $msg);
439 // Twitter is using its own limiter, so we always assume that shortened links will have this length
440 if (iconv_strlen($link, "UTF-8") > 0)
441 $limit = $limit - 23;
443 if (iconv_strlen($msg, "UTF-8") > $limit) {
445 if (($post["type"] == "text") && isset($post["url"])) {
446 $post["url"] = $b["plink"];
447 } elseif (!isset($post["url"])) {
448 $limit = $limit - 23;
449 $post["url"] = $b["plink"];
450 // Which purpose has this line? It is now uncommented, but left as a reminder
451 //} elseif (strpos($b["body"], "[share") !== false) {
452 // $post["url"] = $b["plink"];
453 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
454 $post["url"] = $b["plink"];
456 $msg = shortenmsg($msg, $limit);
460 $post["text"] = trim($msg);