4 * @file include/plaintext.php
7 use \Friendica\ParseUrl;
9 require_once("include/Photo.php");
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 = get_photo_info($matches[1]);
56 if (($picturedata[0] >= 500) AND ($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"] == "") AND (in_array($post["type"], array("link", "video")))
67 AND 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];
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) {
185 - type: link, video, photo
193 $post = get_attachment_data($body);
195 // if nothing is found, it maybe having an image.
196 if (!isset($post["type"])) {
197 $URLSearchString = "^\[\]";
198 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
199 if (count($pictures) == 1) {
200 // Checking, if the link goes to a picture
201 $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
204 // Sometimes photo posts to the own album are not detected at the start.
205 // So we seem to cannot use the cache for these cases. That's strange.
206 if (($data["type"] != "photo") AND strstr($pictures[0][1], "/photos/"))
207 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
209 if ($data["type"] == "photo") {
210 $post["type"] = "photo";
211 if (isset($data["images"][0])) {
212 $post["image"] = $data["images"][0]["src"];
213 $post["url"] = $data["url"];
215 $post["image"] = $data["url"];
217 $post["preview"] = $pictures[0][2];
218 $post["text"] = str_replace($pictures[0][0], "", $body);
220 $imgdata = get_photo_info($pictures[0][1]);
221 if (substr($imgdata["mime"], 0, 6) == "image/") {
222 $post["type"] = "photo";
223 $post["image"] = $pictures[0][1];
224 $post["preview"] = $pictures[0][2];
225 $post["text"] = str_replace($pictures[0][0], "", $body);
228 } elseif (count($pictures) > 1) {
229 $post["type"] = "link";
230 $post["url"] = $b["plink"];
231 $post["image"] = $pictures[0][2];
232 $post["text"] = $body;
234 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
235 if (count($pictures) == 1) {
236 $post["type"] = "photo";
237 $post["image"] = $pictures[0][1];
238 $post["text"] = str_replace($pictures[0][0], "", $body);
239 } elseif (count($pictures) > 1) {
240 $post["type"] = "link";
241 $post["url"] = $b["plink"];
242 $post["image"] = $pictures[0][1];
243 $post["text"] = $body;
247 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links, PREG_SET_ORDER)) {
248 if (count($links) == 1) {
249 $post["type"] = "text";
250 $post["url"] = $links[0][1];
251 $post["text"] = $body;
254 if (!isset($post["type"])) {
255 $post["type"] = "text";
256 $post["text"] = trim($body);
258 } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
259 $data = ParseUrl::getSiteinfoCached($post["url"], true);
261 if (isset($data["images"][0]))
262 $post["image"] = $data["images"][0]["src"];
268 function shortenmsg($msg, $limit, $twitter = false) {
270 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
272 $lines = explode("\n", $msg);
274 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
275 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
276 foreach ($lines AS $row=>$line) {
277 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
278 $msg = trim($msg."\n".$line);
279 // Is the new message empty by now or is it a reshared message?
280 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
281 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8").$ellipsis;
289 * @brief Convert a message into plaintext for connectors to other networks
291 * @param App $a The application class
292 * @param array $b The message array that is about to be posted
293 * @param int $limit The maximum number of characters when posting to that network
294 * @param bool $includedlinks Has an attached link to be included into the message?
295 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
296 * @param string $target_network Name of the network where the post should go to.
298 * @return string The converted message
300 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
302 // Remove the hash tags
303 $URLSearchString = "^\[\]";
304 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
306 // Add an URL element if the text contains a raw link
307 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
309 // Remove the abstract
310 $body = remove_abstract($body);
312 // At first look at data that is attached via "type-..." stuff
313 // This will hopefully replaced with a dedicated bbcode later
314 //$post = get_attached_data($b["body"]);
315 $post = get_attached_data($body);
317 if (($b["title"] != "") AND ($post["text"] != ""))
318 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
319 elseif ($b["title"] != "")
320 $post["text"] = trim($b["title"]);
324 // Fetch the abstract from the given target network
325 if ($target_network != "") {
326 $default_abstract = fetch_abstract($b["body"]);
327 $abstract = fetch_abstract($b["body"], $target_network);
329 // If we post to a network with no limit we only fetch
330 // an abstract exactly for this network
331 if (($limit == 0) AND ($abstract == $default_abstract))
334 } else // Try to guess the correct target network
337 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
340 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
343 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
345 default: // We don't know the exact target.
346 // We fetch an abstract since there is a posting limit.
348 $abstract = fetch_abstract($b["body"]);
351 if ($abstract != "") {
352 $post["text"] = $abstract;
354 if ($post["type"] == "text") {
355 $post["type"] = "link";
356 $post["url"] = $b["plink"];
360 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
361 $msg = html2plain($html, 0, true);
362 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
365 if ($includedlinks) {
366 if ($post["type"] == "link")
367 $link = $post["url"];
368 elseif ($post["type"] == "text")
369 $link = $post["url"];
370 elseif ($post["type"] == "video")
371 $link = $post["url"];
372 elseif ($post["type"] == "photo")
373 $link = $post["image"];
375 if (($msg == "") AND isset($post["title"]))
376 $msg = trim($post["title"]);
378 if (($msg == "") AND isset($post["description"]))
379 $msg = trim($post["description"]);
381 // If the link is already contained in the post, then it neeedn't to be added again
382 // But: if the link is beyond the limit, then it has to be added.
383 if (($link != "") AND strstr($msg, $link)) {
384 $pos = strpos($msg, $link);
386 // Will the text be shortened in the link?
387 // Or is the link the last item in the post?
388 if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
389 $msg = trim(str_replace($link, "", $msg));
390 elseif (($limit == 0) OR ($pos < $limit)) {
391 // The limit has to be increased since it will be shortened - but not now
392 // Only do it with Twitter (htmlmode = 8)
393 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
394 $limit = $limit - 23 + strlen($link);
398 if ($post["type"] == "text")
405 // Reduce multiple spaces
406 // When posted to a network with limited space, we try to gain space where possible
407 while (strpos($msg, " ") !== false)
408 $msg = str_replace(" ", " ", $msg);
410 // Twitter is using its own limiter, so we always assume that shortened links will have this length
411 if (iconv_strlen($link, "UTF-8") > 0)
412 $limit = $limit - 23;
414 if (iconv_strlen($msg, "UTF-8") > $limit) {
416 if (($post["type"] == "text") AND isset($post["url"]))
417 $post["url"] = $b["plink"];
418 elseif (!isset($post["url"])) {
419 $limit = $limit - 23;
420 $post["url"] = $b["plink"];
421 } elseif (strpos($b["body"], "[share") !== false)
422 $post["url"] = $b["plink"];
423 elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
424 $post["url"] = $b["plink"];
426 $msg = shortenmsg($msg, $limit);
430 $post["text"] = trim($msg);