3 * @brief Fetches attachment data that were generated the old way
5 * @param string $body Message body
7 * 'type' -> Message type ("link", "video", "photo")
8 * 'text' -> Text outside of the shared message
9 * 'image' -> Preview image of the message
10 * 'url' -> Url to the attached message
11 * 'title' -> Title of the attachment
12 * 'description' -> Description of the attachment
14 function get_old_attachment_data($body) {
18 // Simplify image codes
19 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
21 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
22 foreach ($attached AS $data) {
23 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
26 $post["type"] = substr($data[1], 5);
28 $post["text"] = trim(str_replace($data[0], "", $body));
30 $attacheddata = $data[2];
32 $URLSearchString = "^\[\]";
34 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches))
35 $post["image"] = $matches[1];
37 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
38 $post["url"] = $matches[1];
39 $post["title"] = $matches[2];
42 // Search for description
43 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
44 $post["description"] = $matches[1];
53 * @brief Fetches attachment data that were generated with the "attachment" element
55 * @param string $body Message body
57 * 'type' -> Message type ("link", "video", "photo")
58 * 'text' -> Text before the shared message
59 * 'after' -> Text after the shared message
60 * 'image' -> Preview image of the message
61 * 'url' -> Url to the attached message
62 * 'title' -> Title of the attachment
63 * 'description' -> Description of the attachment
65 function get_attachment_data($body) {
69 if (!preg_match("/(.*)\[attachment(.*)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
70 return get_old_attachment_data($body);
72 $attributes = $match[2];
74 $data["text"] = trim($match[1]);
77 preg_match("/type='(.*?)'/ism", $attributes, $matches);
78 if ($matches[1] != "")
79 $type = strtolower($matches[1]);
81 preg_match('/type="(.*?)"/ism', $attributes, $matches);
82 if ($matches[1] != "")
83 $type = strtolower($matches[1]);
88 if (!in_array($type, array("link", "audio", "photo", "video")))
92 $data["type"] = $type;
95 preg_match("/url='(.*?)'/ism", $attributes, $matches);
96 if ($matches[1] != "")
99 preg_match('/url="(.*?)"/ism', $attributes, $matches);
100 if ($matches[1] != "")
107 preg_match("/title='(.*?)'/ism", $attributes, $matches);
108 if ($matches[1] != "")
109 $title = $matches[1];
111 preg_match('/title="(.*?)"/ism', $attributes, $matches);
112 if ($matches[1] != "")
113 $title = $matches[1];
115 //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
116 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
117 $title = str_replace(array("[", "]"), array("[", "]"), $title);
120 $data["title"] = $title;
123 if ($type != "video") {
124 preg_match("/image='(.*?)'/ism", $attributes, $matches);
125 if ($matches[1] != "")
126 $image = $matches[1];
128 preg_match('/image="(.*?)"/ism', $attributes, $matches);
129 if ($matches[1] != "")
130 $image = $matches[1];
134 $data["image"] = $image;
137 if ($type != "video") {
138 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
139 if ($matches[1] != "")
140 $preview = $matches[1];
142 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
143 if ($matches[1] != "")
144 $preview = $matches[1];
147 if (($image == "") AND ($preview != ""))
148 $data["image"] = $preview;
150 $data["preview"] = $preview;
152 $data["description"] = trim($match[3]);
154 $data["after"] = trim($match[4]);
159 function get_attached_data($body) {
162 - type: link, video, photo
170 $post = get_attachment_data($body);
172 // if nothing is found, it maybe having an image.
173 if (!isset($post["type"])) {
174 require_once("mod/parse_url.php");
175 require_once("include/Photo.php");
177 $URLSearchString = "^\[\]";
178 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
179 if (count($pictures) == 1) {
180 // Checking, if the link goes to a picture
181 $data = parseurl_getsiteinfo_cached($pictures[0][1], true);
182 if ($data["type"] == "photo") {
183 $post["type"] = "photo";
184 if (isset($data["images"][0])) {
185 $post["image"] = $data["images"][0]["src"];
186 $post["url"] = $data["url"];
188 $post["image"] = $data["url"];
190 $post["preview"] = $pictures[0][2];
191 $post["text"] = str_replace($pictures[0][0], "", $body);
193 $imgdata = get_photo_info($pictures[0][1]);
194 if (substr($imgdata["mime"], 0, 6) == "image/") {
195 $post["type"] = "photo";
196 $post["image"] = $pictures[0][1];
197 $post["preview"] = $pictures[0][2];
198 $post["text"] = str_replace($pictures[0][0], "", $body);
201 } elseif (count($pictures) > 1) {
202 $post["type"] = "link";
203 $post["url"] = $b["plink"];
204 $post["image"] = $pictures[0][2];
205 $post["text"] = $body;
207 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
208 if (count($pictures) == 1) {
209 $post["type"] = "photo";
210 $post["image"] = $pictures[0][1];
211 $post["text"] = str_replace($pictures[0][0], "", $body);
212 } elseif (count($pictures) > 1) {
213 $post["type"] = "link";
214 $post["url"] = $b["plink"];
215 $post["image"] = $pictures[0][1];
216 $post["text"] = $body;
220 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links, PREG_SET_ORDER)) {
221 if (count($links) == 1) {
222 $post["type"] = "text";
223 $post["url"] = $links[0][1];
224 $post["text"] = $body;
227 if (!isset($post["type"])) {
228 $post["type"] = "text";
229 $post["text"] = trim($body);
231 } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
232 require_once("mod/parse_url.php");
233 $data = parseurl_getsiteinfo_cached($post["url"], true);
235 if (isset($data["images"][0]))
236 $post["image"] = $data["images"][0]["src"];
242 function shortenmsg($msg, $limit, $twitter = false) {
244 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
246 $lines = explode("\n", $msg);
248 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
249 foreach ($lines AS $row=>$line) {
250 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
251 $msg = trim($msg."\n".$line);
252 // Is the new message empty by now or is it a reshared message?
253 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
254 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8")."...";
262 * @brief Convert a message into plaintext for connectors to other networks
264 * @param App $a The application class
265 * @param array $b The message array that is about to be posted
266 * @param int $limit The maximum number of characters when posting to that network
267 * @param bool $includedlinks Has an attached link to be included into the message?
268 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
269 * @param string $target_network Name of the network where the post should go to.
271 * @return string The converted message
273 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
274 require_once("include/bbcode.php");
275 require_once("include/html2plain.php");
276 require_once("include/network.php");
278 // Remove the hash tags
279 $URLSearchString = "^\[\]";
280 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
282 // Add an URL element if the text contains a raw link
283 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
285 // Remove the abstract
286 $body = remove_abstract($body);
288 // At first look at data that is attached via "type-..." stuff
289 // This will hopefully replaced with a dedicated bbcode later
290 //$post = get_attached_data($b["body"]);
291 $post = get_attached_data($body);
293 if (($b["title"] != "") AND ($post["text"] != ""))
294 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
295 elseif ($b["title"] != "")
296 $post["text"] = trim($b["title"]);
300 // Fetch the abstract from the given target network
301 if ($target_network != "") {
302 $default_abstract = fetch_abstract($b["body"]);
303 $abstract = fetch_abstract($b["body"], $target_network);
305 // If we post to a network with no limit we only fetch
306 // an abstract exactly for this network
307 if (($limit == 0) AND ($abstract == $default_abstract))
310 } else // Try to guess the correct target network
313 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
316 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
319 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
321 default: // We don't know the exact target.
322 // We fetch an abstract since there is a posting limit.
324 $abstract = fetch_abstract($b["body"]);
327 if ($abstract != "") {
328 $post["text"] = $abstract;
330 if ($post["type"] == "text") {
331 $post["type"] = "link";
332 $post["url"] = $b["plink"];
336 $html = bbcode($post["text"], false, false, $htmlmode);
337 $msg = html2plain($html, 0, true);
338 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
341 if ($includedlinks) {
342 if ($post["type"] == "link")
343 $link = $post["url"];
344 elseif ($post["type"] == "text")
345 $link = $post["url"];
346 elseif ($post["type"] == "video")
347 $link = $post["url"];
348 elseif ($post["type"] == "photo")
349 $link = $post["image"];
351 if (($msg == "") AND isset($post["title"]))
352 $msg = trim($post["title"]);
354 if (($msg == "") AND isset($post["description"]))
355 $msg = trim($post["description"]);
357 // If the link is already contained in the post, then it neeedn't to be added again
358 // But: if the link is beyond the limit, then it has to be added.
359 if (($link != "") AND strstr($msg, $link)) {
360 $pos = strpos($msg, $link);
362 // Will the text be shortened in the link?
363 // Or is the link the last item in the post?
364 if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
365 $msg = trim(str_replace($link, "", $msg));
366 elseif (($limit == 0) OR ($pos < $limit)) {
367 // The limit has to be increased since it will be shortened - but not now
368 // Only do it with Twitter (htmlmode = 8)
369 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
370 $limit = $limit - 23 + strlen($link);
374 if ($post["type"] == "text")
381 // Reduce multiple spaces
382 // When posted to a network with limited space, we try to gain space where possible
383 while (strpos($msg, " ") !== false)
384 $msg = str_replace(" ", " ", $msg);
386 // Twitter is using its own limiter, so we always assume that shortened links will have this length
387 if (iconv_strlen($link, "UTF-8") > 0)
388 $limit = $limit - 23;
390 if (iconv_strlen($msg, "UTF-8") > $limit) {
392 if (($post["type"] == "text") AND isset($post["url"]))
393 $post["url"] = $b["plink"];
394 elseif (!isset($post["url"])) {
395 $limit = $limit - 23;
396 $post["url"] = $b["plink"];
397 } elseif (strpos($b["body"], "[share") !== false)
398 $post["url"] = $b["plink"];
399 elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
400 $post["url"] = $b["plink"];
402 $msg = shortenmsg($msg, $limit);
406 $post["text"] = trim($msg);