3 require_once("include/Photo.php");
6 * @brief Fetches attachment data that were generated the old way
8 * @param string $body Message body
10 * 'type' -> Message type ("link", "video", "photo")
11 * 'text' -> Text before the shared message
12 * 'after' -> Text after the shared message
13 * 'image' -> Preview image of the message
14 * 'url' -> Url to the attached message
15 * 'title' -> Title of the attachment
16 * 'description' -> Description of the attachment
18 function get_old_attachment_data($body) {
22 // Simplify image codes
23 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
25 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
26 foreach ($attached AS $data) {
27 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
30 $post["type"] = substr($data[1], 5);
32 $pos = strpos($body, $data[0]);
34 $post["text"] = trim(substr($body, 0, $pos));
35 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
37 $post["text"] = trim(str_replace($data[0], "", $body));
39 $attacheddata = $data[2];
41 $URLSearchString = "^\[\]";
43 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
45 $picturedata = get_photo_info($matches[1]);
47 if (($picturedata[0] >= 500) AND ($picturedata[0] >= $picturedata[1]))
48 $post["image"] = $matches[1];
50 $post["preview"] = $matches[1];
53 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
54 $post["url"] = $matches[1];
55 $post["title"] = $matches[2];
58 // Search for description
59 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
60 $post["description"] = $matches[1];
69 * @brief Fetches attachment data that were generated with the "attachment" element
71 * @param string $body Message body
73 * 'type' -> Message type ("link", "video", "photo")
74 * 'text' -> Text before the shared message
75 * 'after' -> Text after the shared message
76 * 'image' -> Preview image of the message
77 * 'url' -> Url to the attached message
78 * 'title' -> Title of the attachment
79 * 'description' -> Description of the attachment
81 function get_attachment_data($body) {
85 if (!preg_match("/(.*)\[attachment(.*)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
86 return get_old_attachment_data($body);
88 $attributes = $match[2];
90 $data["text"] = trim($match[1]);
93 preg_match("/type='(.*?)'/ism", $attributes, $matches);
94 if ($matches[1] != "")
95 $type = strtolower($matches[1]);
97 preg_match('/type="(.*?)"/ism', $attributes, $matches);
98 if ($matches[1] != "")
99 $type = strtolower($matches[1]);
104 if (!in_array($type, array("link", "audio", "photo", "video")))
108 $data["type"] = $type;
111 preg_match("/url='(.*?)'/ism", $attributes, $matches);
112 if ($matches[1] != "")
115 preg_match('/url="(.*?)"/ism', $attributes, $matches);
116 if ($matches[1] != "")
123 preg_match("/title='(.*?)'/ism", $attributes, $matches);
124 if ($matches[1] != "")
125 $title = $matches[1];
127 preg_match('/title="(.*?)"/ism', $attributes, $matches);
128 if ($matches[1] != "")
129 $title = $matches[1];
131 //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
132 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
133 $title = str_replace(array("[", "]"), array("[", "]"), $title);
136 $data["title"] = $title;
139 if ($type != "video") {
140 preg_match("/image='(.*?)'/ism", $attributes, $matches);
141 if ($matches[1] != "")
142 $image = $matches[1];
144 preg_match('/image="(.*?)"/ism', $attributes, $matches);
145 if ($matches[1] != "")
146 $image = $matches[1];
150 $data["image"] = $image;
153 if ($type != "video") {
154 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
155 if ($matches[1] != "")
156 $preview = $matches[1];
158 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
159 if ($matches[1] != "")
160 $preview = $matches[1];
164 $data["preview"] = $preview;
166 $data["description"] = trim($match[3]);
168 $data["after"] = trim($match[4]);
173 function get_attached_data($body) {
176 - type: link, video, photo
184 $post = get_attachment_data($body);
186 // if nothing is found, it maybe having an image.
187 if (!isset($post["type"])) {
188 require_once("mod/parse_url.php");
189 require_once("include/Photo.php");
191 $URLSearchString = "^\[\]";
192 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
193 if (count($pictures) == 1) {
194 // Checking, if the link goes to a picture
195 $data = parseurl_getsiteinfo_cached($pictures[0][1], true);
196 if ($data["type"] == "photo") {
197 $post["type"] = "photo";
198 if (isset($data["images"][0])) {
199 $post["image"] = $data["images"][0]["src"];
200 $post["url"] = $data["url"];
202 $post["image"] = $data["url"];
204 $post["preview"] = $pictures[0][2];
205 $post["text"] = str_replace($pictures[0][0], "", $body);
207 $imgdata = get_photo_info($pictures[0][1]);
208 if (substr($imgdata["mime"], 0, 6) == "image/") {
209 $post["type"] = "photo";
210 $post["image"] = $pictures[0][1];
211 $post["preview"] = $pictures[0][2];
212 $post["text"] = str_replace($pictures[0][0], "", $body);
215 } elseif (count($pictures) > 1) {
216 $post["type"] = "link";
217 $post["url"] = $b["plink"];
218 $post["image"] = $pictures[0][2];
219 $post["text"] = $body;
221 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
222 if (count($pictures) == 1) {
223 $post["type"] = "photo";
224 $post["image"] = $pictures[0][1];
225 $post["text"] = str_replace($pictures[0][0], "", $body);
226 } elseif (count($pictures) > 1) {
227 $post["type"] = "link";
228 $post["url"] = $b["plink"];
229 $post["image"] = $pictures[0][1];
230 $post["text"] = $body;
234 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links, PREG_SET_ORDER)) {
235 if (count($links) == 1) {
236 $post["type"] = "text";
237 $post["url"] = $links[0][1];
238 $post["text"] = $body;
241 if (!isset($post["type"])) {
242 $post["type"] = "text";
243 $post["text"] = trim($body);
245 } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
246 require_once("mod/parse_url.php");
247 $data = parseurl_getsiteinfo_cached($post["url"], true);
249 if (isset($data["images"][0]))
250 $post["image"] = $data["images"][0]["src"];
256 function shortenmsg($msg, $limit, $twitter = false) {
258 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
260 $lines = explode("\n", $msg);
262 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
263 foreach ($lines AS $row=>$line) {
264 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
265 $msg = trim($msg."\n".$line);
266 // Is the new message empty by now or is it a reshared message?
267 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
268 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8")."...";
276 * @brief Convert a message into plaintext for connectors to other networks
278 * @param App $a The application class
279 * @param array $b The message array that is about to be posted
280 * @param int $limit The maximum number of characters when posting to that network
281 * @param bool $includedlinks Has an attached link to be included into the message?
282 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
283 * @param string $target_network Name of the network where the post should go to.
285 * @return string The converted message
287 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
288 require_once("include/bbcode.php");
289 require_once("include/html2plain.php");
290 require_once("include/network.php");
292 // Remove the hash tags
293 $URLSearchString = "^\[\]";
294 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
296 // Add an URL element if the text contains a raw link
297 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
299 // Remove the abstract
300 $body = remove_abstract($body);
302 // At first look at data that is attached via "type-..." stuff
303 // This will hopefully replaced with a dedicated bbcode later
304 //$post = get_attached_data($b["body"]);
305 $post = get_attached_data($body);
307 if (($b["title"] != "") AND ($post["text"] != ""))
308 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
309 elseif ($b["title"] != "")
310 $post["text"] = trim($b["title"]);
314 // Fetch the abstract from the given target network
315 if ($target_network != "") {
316 $default_abstract = fetch_abstract($b["body"]);
317 $abstract = fetch_abstract($b["body"], $target_network);
319 // If we post to a network with no limit we only fetch
320 // an abstract exactly for this network
321 if (($limit == 0) AND ($abstract == $default_abstract))
324 } else // Try to guess the correct target network
327 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
330 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
333 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
335 default: // We don't know the exact target.
336 // We fetch an abstract since there is a posting limit.
338 $abstract = fetch_abstract($b["body"]);
341 if ($abstract != "") {
342 $post["text"] = $abstract;
344 if ($post["type"] == "text") {
345 $post["type"] = "link";
346 $post["url"] = $b["plink"];
350 $html = bbcode($post["text"], false, false, $htmlmode);
351 $msg = html2plain($html, 0, true);
352 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
355 if ($includedlinks) {
356 if ($post["type"] == "link")
357 $link = $post["url"];
358 elseif ($post["type"] == "text")
359 $link = $post["url"];
360 elseif ($post["type"] == "video")
361 $link = $post["url"];
362 elseif ($post["type"] == "photo")
363 $link = $post["image"];
365 if (($msg == "") AND isset($post["title"]))
366 $msg = trim($post["title"]);
368 if (($msg == "") AND isset($post["description"]))
369 $msg = trim($post["description"]);
371 // If the link is already contained in the post, then it neeedn't to be added again
372 // But: if the link is beyond the limit, then it has to be added.
373 if (($link != "") AND strstr($msg, $link)) {
374 $pos = strpos($msg, $link);
376 // Will the text be shortened in the link?
377 // Or is the link the last item in the post?
378 if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
379 $msg = trim(str_replace($link, "", $msg));
380 elseif (($limit == 0) OR ($pos < $limit)) {
381 // The limit has to be increased since it will be shortened - but not now
382 // Only do it with Twitter (htmlmode = 8)
383 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
384 $limit = $limit - 23 + strlen($link);
388 if ($post["type"] == "text")
395 // Reduce multiple spaces
396 // When posted to a network with limited space, we try to gain space where possible
397 while (strpos($msg, " ") !== false)
398 $msg = str_replace(" ", " ", $msg);
400 // Twitter is using its own limiter, so we always assume that shortened links will have this length
401 if (iconv_strlen($link, "UTF-8") > 0)
402 $limit = $limit - 23;
404 if (iconv_strlen($msg, "UTF-8") > $limit) {
406 if (($post["type"] == "text") AND isset($post["url"]))
407 $post["url"] = $b["plink"];
408 elseif (!isset($post["url"])) {
409 $limit = $limit - 23;
410 $post["url"] = $b["plink"];
411 } elseif (strpos($b["body"], "[share") !== false)
412 $post["url"] = $b["plink"];
413 elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
414 $post["url"] = $b["plink"];
416 $msg = shortenmsg($msg, $limit);
420 $post["text"] = trim($msg);