2 function get_old_attachment_data($body) {
6 // Simplify image codes
7 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
9 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached, PREG_SET_ORDER)) {
10 foreach ($attached AS $data) {
11 if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
14 $post["type"] = substr($data[1], 5);
16 $post["text"] = trim(str_replace($data[0], "", $body));
18 $attacheddata = $data[2];
20 $URLSearchString = "^\[\]";
22 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches))
23 $post["image"] = $matches[1];
25 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
26 $post["url"] = $matches[1];
27 $post["title"] = $matches[2];
30 // Search for description
31 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
32 $post["description"] = $matches[1];
40 function get_attachment_data($body) {
44 if (!preg_match("/(.*)\[attachment(.*)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
45 return get_old_attachment_data($body);
47 $attributes = $match[2];
49 $data["text"] = trim($match[1]);
52 preg_match("/type='(.*?)'/ism", $attributes, $matches);
53 if ($matches[1] != "")
54 $type = strtolower($matches[1]);
56 preg_match('/type="(.*?)"/ism', $attributes, $matches);
57 if ($matches[1] != "")
58 $type = strtolower($matches[1]);
63 if (!in_array($type, array("link", "audio", "video")))
67 $data["type"] = $type;
70 preg_match("/url='(.*?)'/ism", $attributes, $matches);
71 if ($matches[1] != "")
74 preg_match('/url="(.*?)"/ism', $attributes, $matches);
75 if ($matches[1] != "")
82 preg_match("/title='(.*?)'/ism", $attributes, $matches);
83 if ($matches[1] != "")
86 preg_match('/title="(.*?)"/ism', $attributes, $matches);
87 if ($matches[1] != "")
90 //$title = htmlentities($title, ENT_QUOTES, 'UTF-8', false);
91 $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
92 $title = str_replace(array("[", "]"), array("[", "]"), $title);
95 $data["title"] = $title;
98 if ($type != "video") {
99 preg_match("/image='(.*?)'/ism", $attributes, $matches);
100 if ($matches[1] != "")
101 $image = $matches[1];
103 preg_match('/image="(.*?)"/ism', $attributes, $matches);
104 if ($matches[1] != "")
105 $image = $matches[1];
109 $data["image"] = $image;
112 if ($type != "video") {
113 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
114 if ($matches[1] != "")
115 $preview = $matches[1];
117 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
118 if ($matches[1] != "")
119 $preview = $matches[1];
122 if (($image == "") AND ($preview != ""))
123 $data["image"] = $preview;
125 $data["preview"] = $preview;
127 $data["description"] = trim($match[3]);
129 $data["after"] = trim($match[4]);
134 function get_attached_data($body) {
137 - type: link, video, photo
145 $post = get_attachment_data($body);
147 // if nothing is found, it maybe having an image.
148 if (!isset($post["type"])) {
149 require_once("mod/parse_url.php");
150 require_once("include/Photo.php");
152 $URLSearchString = "^\[\]";
153 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
154 if (count($pictures) == 1) {
155 // Checking, if the link goes to a picture
156 $data = parseurl_getsiteinfo_cached($pictures[0][1], true);
157 if ($data["type"] == "photo") {
158 $post["type"] = "photo";
159 if (isset($data["images"][0])) {
160 $post["image"] = $data["images"][0]["src"];
161 $post["url"] = $data["url"];
163 $post["image"] = $data["url"];
165 $post["preview"] = $pictures[0][2];
166 $post["text"] = str_replace($pictures[0][0], "", $body);
168 $imgdata = get_photo_info($pictures[0][1]);
169 if (substr($imgdata["mime"], 0, 6) == "image/") {
170 $post["type"] = "photo";
171 $post["image"] = $pictures[0][1];
172 $post["preview"] = $pictures[0][2];
173 $post["text"] = str_replace($pictures[0][0], "", $body);
176 } elseif (count($pictures) > 1) {
177 $post["type"] = "link";
178 $post["url"] = $b["plink"];
179 $post["image"] = $pictures[0][2];
180 $post["text"] = $body;
182 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
183 if (count($pictures) == 1) {
184 $post["type"] = "photo";
185 $post["image"] = $pictures[0][1];
186 $post["text"] = str_replace($pictures[0][0], "", $body);
187 } elseif (count($pictures) > 1) {
188 $post["type"] = "link";
189 $post["url"] = $b["plink"];
190 $post["image"] = $pictures[0][1];
191 $post["text"] = $body;
195 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links, PREG_SET_ORDER)) {
196 if (count($links) == 1) {
197 $post["type"] = "text";
198 $post["url"] = $links[0][1];
199 $post["text"] = $body;
202 if (!isset($post["type"])) {
203 $post["type"] = "text";
204 $post["text"] = trim($body);
206 } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
207 require_once("mod/parse_url.php");
208 $data = parseurl_getsiteinfo_cached($post["url"], true);
210 if (isset($data["images"][0]))
211 $post["image"] = $data["images"][0]["src"];
217 function shortenmsg($msg, $limit, $twitter = false) {
219 /// For Twitter URLs aren't shortened, but they have to be calculated as if.
221 $lines = explode("\n", $msg);
223 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
224 foreach ($lines AS $row=>$line) {
225 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
226 $msg = trim($msg."\n".$line);
227 // Is the new message empty by now or is it a reshared message?
228 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
229 $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8")."...";
237 * @brief Convert a message into plaintext for connectors to other networks
239 * @param App $a The application class
240 * @param array $b The message array that is about to be posted
241 * @param int $limit The maximum number of characters when posting to that network
242 * @param bool $includedlinks Has an attached link to be included into the message?
243 * @param int $htmlmode This triggers the behaviour of the bbcode conversion
244 * @param string $target_network Name of the network where the post should go to.
246 * @return string The converted message
248 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
249 require_once("include/bbcode.php");
250 require_once("include/html2plain.php");
251 require_once("include/network.php");
253 // Remove the hash tags
254 $URLSearchString = "^\[\]";
255 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
257 // Add an URL element if the text contains a raw link
258 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
260 // Remove the abstract
261 $body = remove_abstract($body);
263 // At first look at data that is attached via "type-..." stuff
264 // This will hopefully replaced with a dedicated bbcode later
265 //$post = get_attached_data($b["body"]);
266 $post = get_attached_data($body);
268 if (($b["title"] != "") AND ($post["text"] != ""))
269 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
270 elseif ($b["title"] != "")
271 $post["text"] = trim($b["title"]);
275 // Fetch the abstract from the given target network
276 if ($target_network != "") {
277 $default_abstract = fetch_abstract($b["body"]);
278 $abstract = fetch_abstract($b["body"], $target_network);
280 // If we post to a network with no limit we only fetch
281 // an abstract exactly for this network
282 if (($limit == 0) AND ($abstract == $default_abstract))
285 } else // Try to guess the correct target network
288 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
291 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
294 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
296 default: // We don't know the exact target.
297 // We fetch an abstract since there is a posting limit.
299 $abstract = fetch_abstract($b["body"]);
302 if ($abstract != "") {
303 $post["text"] = $abstract;
305 if ($post["type"] == "text") {
306 $post["type"] = "link";
307 $post["url"] = $b["plink"];
311 $html = bbcode($post["text"], false, false, $htmlmode);
312 $msg = html2plain($html, 0, true);
313 $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
316 if ($includedlinks) {
317 if ($post["type"] == "link")
318 $link = $post["url"];
319 elseif ($post["type"] == "text")
320 $link = $post["url"];
321 elseif ($post["type"] == "video")
322 $link = $post["url"];
323 elseif ($post["type"] == "photo")
324 $link = $post["image"];
326 if (($msg == "") AND isset($post["title"]))
327 $msg = trim($post["title"]);
329 if (($msg == "") AND isset($post["description"]))
330 $msg = trim($post["description"]);
332 // If the link is already contained in the post, then it neeedn't to be added again
333 // But: if the link is beyond the limit, then it has to be added.
334 if (($link != "") AND strstr($msg, $link)) {
335 $pos = strpos($msg, $link);
337 // Will the text be shortened in the link?
338 // Or is the link the last item in the post?
339 if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
340 $msg = trim(str_replace($link, "", $msg));
341 elseif (($limit == 0) OR ($pos < $limit)) {
342 // The limit has to be increased since it will be shortened - but not now
343 // Only do it with Twitter (htmlmode = 8)
344 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
345 $limit = $limit - 23 + strlen($link);
349 if ($post["type"] == "text")
356 // Reduce multiple spaces
357 // When posted to a network with limited space, we try to gain space where possible
358 while (strpos($msg, " ") !== false)
359 $msg = str_replace(" ", " ", $msg);
361 // Twitter is using its own limiter, so we always assume that shortened links will have this length
362 if (iconv_strlen($link, "UTF-8") > 0)
363 $limit = $limit - 23;
365 if (iconv_strlen($msg, "UTF-8") > $limit) {
367 if (($post["type"] == "text") AND isset($post["url"]))
368 $post["url"] = $b["plink"];
369 elseif (!isset($post["url"])) {
370 $limit = $limit - 23;
371 $post["url"] = $b["plink"];
372 } elseif (strpos($b["body"], "[share") !== false)
373 $post["url"] = $b["plink"];
374 elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
375 $post["url"] = $b["plink"];
377 $msg = shortenmsg($msg, $limit);
381 $post["text"] = trim($msg);