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 outside of the shared message
12 * 'image' -> Preview image of the message
13 * 'url' -> Url to the attached message
14 * 'title' -> Title of the attachment
15 * 'description' -> Description of the attachment
17 function get_old_attachment_data($body) {
21 // Simplify image codes
22 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
24 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
25 foreach ($attached AS $data) {
26 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
29 $post["type"] = substr($data[1], 5);
31 $post["text"] = trim(str_replace($data[0], "", $body));
33 $attacheddata = $data[2];
35 $URLSearchString = "^\[\]";
37 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
39 $picturedata = get_photo_info($matches[1]);
41 if (($picturedata[0] >= 500) AND ($picturedata[0] >= $picturedata[1]))
42 $post["image"] = $matches[1];
44 $post["preview"] = $matches[1];
47 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
48 $post["url"] = $matches[1];
49 $post["title"] = $matches[2];
52 // Search for description
53 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
54 $post["description"] = $matches[1];
63 * @brief Fetches attachment data that were generated with the "attachment" element
65 * @param string $body Message body
67 * 'type' -> Message type ("link", "video", "photo")
68 * 'text' -> Text before the shared message
69 * 'after' -> Text after the shared message
70 * 'image' -> Preview image of the message
71 * 'url' -> Url to the attached message
72 * 'title' -> Title of the attachment
73 * 'description' -> Description of the attachment
75 function get_attachment_data($body) {
79 if (!preg_match("/(.*)\[attachment(.*)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
80 return get_old_attachment_data($body);
82 $attributes = $match[2];
84 $data["text"] = trim($match[1]);
87 preg_match("/type='(.*?)'/ism", $attributes, $matches);
88 if ($matches[1] != "")
89 $type = strtolower($matches[1]);
91 preg_match('/type="(.*?)"/ism', $attributes, $matches);
92 if ($matches[1] != "")
93 $type = strtolower($matches[1]);
98 if (!in_array($type, array("link", "audio", "photo", "video")))
102 $data["type"] = $type;
105 preg_match("/url='(.*?)'/ism", $attributes, $matches);
106 if ($matches[1] != "")
109 preg_match('/url="(.*?)"/ism', $attributes, $matches);
110 if ($matches[1] != "")
117 preg_match("/title='(.*?)'/ism", $attributes, $matches);
118 if ($matches[1] != "")
119 $title = $matches[1];
121 preg_match('/title="(.*?)"/ism', $attributes, $matches);
122 if ($matches[1] != "")
123 $title = $matches[1];
125 //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
126 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
127 $title = str_replace(array("[", "]"), array("[", "]"), $title);
130 $data["title"] = $title;
133 if ($type != "video") {
134 preg_match("/image='(.*?)'/ism", $attributes, $matches);
135 if ($matches[1] != "")
136 $image = $matches[1];
138 preg_match('/image="(.*?)"/ism', $attributes, $matches);
139 if ($matches[1] != "")
140 $image = $matches[1];
144 $data["image"] = $image;
147 if ($type != "video") {
148 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
149 if ($matches[1] != "")
150 $preview = $matches[1];
152 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
153 if ($matches[1] != "")
154 $preview = $matches[1];
158 $data["preview"] = $preview;
160 $data["description"] = trim($match[3]);
162 $data["after"] = trim($match[4]);
167 function get_attached_data($body) {
170 - type: link, video, photo
178 $post = get_attachment_data($body);
180 // if nothing is found, it maybe having an image.
181 if (!isset($post["type"])) {
182 require_once("mod/parse_url.php");
183 require_once("include/Photo.php");
185 $URLSearchString = "^\[\]";
186 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
187 if (count($pictures) == 1) {
188 // Checking, if the link goes to a picture
189 $data = parseurl_getsiteinfo_cached($pictures[0][1], true);
190 if ($data["type"] == "photo") {
191 $post["type"] = "photo";
192 if (isset($data["images"][0])) {
193 $post["image"] = $data["images"][0]["src"];
194 $post["url"] = $data["url"];
196 $post["image"] = $data["url"];
198 $post["preview"] = $pictures[0][2];
199 $post["text"] = str_replace($pictures[0][0], "", $body);
201 $imgdata = get_photo_info($pictures[0][1]);
202 if (substr($imgdata["mime"], 0, 6) == "image/") {
203 $post["type"] = "photo";
204 $post["image"] = $pictures[0][1];
205 $post["preview"] = $pictures[0][2];
206 $post["text"] = str_replace($pictures[0][0], "", $body);
209 } elseif (count($pictures) > 1) {
210 $post["type"] = "link";
211 $post["url"] = $b["plink"];
212 $post["image"] = $pictures[0][2];
213 $post["text"] = $body;
215 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
216 if (count($pictures) == 1) {
217 $post["type"] = "photo";
218 $post["image"] = $pictures[0][1];
219 $post["text"] = str_replace($pictures[0][0], "", $body);
220 } elseif (count($pictures) > 1) {
221 $post["type"] = "link";
222 $post["url"] = $b["plink"];
223 $post["image"] = $pictures[0][1];
224 $post["text"] = $body;
228 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links, PREG_SET_ORDER)) {
229 if (count($links) == 1) {
230 $post["type"] = "text";
231 $post["url"] = $links[0][1];
232 $post["text"] = $body;
235 if (!isset($post["type"])) {
236 $post["type"] = "text";
237 $post["text"] = trim($body);
239 } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
240 require_once("mod/parse_url.php");
241 $data = parseurl_getsiteinfo_cached($post["url"], true);
243 if (isset($data["images"][0]))
244 $post["image"] = $data["images"][0]["src"];
250 function shortenmsg($msg, $limit, $twitter = false) {
252 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
254 $lines = explode("\n", $msg);
256 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
257 foreach ($lines AS $row=>$line) {
258 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
259 $msg = trim($msg."\n".$line);
260 // Is the new message empty by now or is it a reshared message?
261 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
262 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8")."...";
270 * @brief Convert a message into plaintext for connectors to other networks
272 * @param App $a The application class
273 * @param array $b The message array that is about to be posted
274 * @param int $limit The maximum number of characters when posting to that network
275 * @param bool $includedlinks Has an attached link to be included into the message?
276 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
277 * @param string $target_network Name of the network where the post should go to.
279 * @return string The converted message
281 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
282 require_once("include/bbcode.php");
283 require_once("include/html2plain.php");
284 require_once("include/network.php");
286 // Remove the hash tags
287 $URLSearchString = "^\[\]";
288 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
290 // Add an URL element if the text contains a raw link
291 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
293 // Remove the abstract
294 $body = remove_abstract($body);
296 // At first look at data that is attached via "type-..." stuff
297 // This will hopefully replaced with a dedicated bbcode later
298 //$post = get_attached_data($b["body"]);
299 $post = get_attached_data($body);
301 if (($b["title"] != "") AND ($post["text"] != ""))
302 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
303 elseif ($b["title"] != "")
304 $post["text"] = trim($b["title"]);
308 // Fetch the abstract from the given target network
309 if ($target_network != "") {
310 $default_abstract = fetch_abstract($b["body"]);
311 $abstract = fetch_abstract($b["body"], $target_network);
313 // If we post to a network with no limit we only fetch
314 // an abstract exactly for this network
315 if (($limit == 0) AND ($abstract == $default_abstract))
318 } else // Try to guess the correct target network
321 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
324 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
327 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
329 default: // We don't know the exact target.
330 // We fetch an abstract since there is a posting limit.
332 $abstract = fetch_abstract($b["body"]);
335 if ($abstract != "") {
336 $post["text"] = $abstract;
338 if ($post["type"] == "text") {
339 $post["type"] = "link";
340 $post["url"] = $b["plink"];
344 $html = bbcode($post["text"], false, false, $htmlmode);
345 $msg = html2plain($html, 0, true);
346 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
349 if ($includedlinks) {
350 if ($post["type"] == "link")
351 $link = $post["url"];
352 elseif ($post["type"] == "text")
353 $link = $post["url"];
354 elseif ($post["type"] == "video")
355 $link = $post["url"];
356 elseif ($post["type"] == "photo")
357 $link = $post["image"];
359 if (($msg == "") AND isset($post["title"]))
360 $msg = trim($post["title"]);
362 if (($msg == "") AND isset($post["description"]))
363 $msg = trim($post["description"]);
365 // If the link is already contained in the post, then it neeedn't to be added again
366 // But: if the link is beyond the limit, then it has to be added.
367 if (($link != "") AND strstr($msg, $link)) {
368 $pos = strpos($msg, $link);
370 // Will the text be shortened in the link?
371 // Or is the link the last item in the post?
372 if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
373 $msg = trim(str_replace($link, "", $msg));
374 elseif (($limit == 0) OR ($pos < $limit)) {
375 // The limit has to be increased since it will be shortened - but not now
376 // Only do it with Twitter (htmlmode = 8)
377 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
378 $limit = $limit - 23 + strlen($link);
382 if ($post["type"] == "text")
389 // Reduce multiple spaces
390 // When posted to a network with limited space, we try to gain space where possible
391 while (strpos($msg, " ") !== false)
392 $msg = str_replace(" ", " ", $msg);
394 // Twitter is using its own limiter, so we always assume that shortened links will have this length
395 if (iconv_strlen($link, "UTF-8") > 0)
396 $limit = $limit - 23;
398 if (iconv_strlen($msg, "UTF-8") > $limit) {
400 if (($post["type"] == "text") AND isset($post["url"]))
401 $post["url"] = $b["plink"];
402 elseif (!isset($post["url"])) {
403 $limit = $limit - 23;
404 $post["url"] = $b["plink"];
405 } elseif (strpos($b["body"], "[share") !== false)
406 $post["url"] = $b["plink"];
407 elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
408 $post["url"] = $b["plink"];
410 $msg = shortenmsg($msg, $limit);
414 $post["text"] = trim($msg);