]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
Only import new OStatus posts if they are from our followers
[friendica.git] / include / plaintext.php
1 <?php
2
3 /**
4  * @file include/plaintext.php
5  */
6
7 use \Friendica\ParseUrl;
8
9 require_once("include/Photo.php");
10 require_once("include/bbcode.php");
11 require_once("include/html2plain.php");
12 require_once("include/network.php");
13
14 /**
15  * @brief Fetches attachment data that were generated the old way
16  *
17  * @param string $body Message body
18  * @return array
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
26  */
27 function get_old_attachment_data($body) {
28
29         $post = array();
30
31         // Simplify image codes
32         $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
33
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")))
37                                 continue;
38
39                         $post["type"] = substr($data[1], 5);
40
41                         $pos = strpos($body, $data[0]);
42                         if ($pos > 0) {
43                                 $post["text"] = trim(substr($body, 0, $pos));
44                                 $post["after"] = trim(substr($body, $pos + strlen($data[0])));
45                         } else
46                                 $post["text"] = trim(str_replace($data[0], "", $body));
47
48                         $attacheddata = $data[2];
49
50                         $URLSearchString = "^\[\]";
51
52                         if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
53
54                                 $picturedata = get_photo_info($matches[1]);
55
56                                 if (($picturedata[0] >= 500) AND ($picturedata[0] >= $picturedata[1]))
57                                         $post["image"] = $matches[1];
58                                 else
59                                         $post["preview"] = $matches[1];
60                         }
61
62                         if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
63                                 $post["url"] = $matches[1];
64                                 $post["title"] = $matches[2];
65                         }
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];
69                         }
70
71                         // Search for description
72                         if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
73                                 $post["description"] = $matches[1];
74
75                 }
76         }
77
78         return $post;
79 }
80
81 /**
82  * @brief Fetches attachment data that were generated with the "attachment" element
83  *
84  * @param string $body Message body
85  * @return array
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
93  */
94 function get_attachment_data($body) {
95
96         $data = array();
97
98         if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match))
99                 return get_old_attachment_data($body);
100
101         $attributes = $match[2];
102
103         $data["text"] = trim($match[1]);
104
105         $type = "";
106         preg_match("/type='(.*?)'/ism", $attributes, $matches);
107         if ($matches[1] != "")
108                 $type = strtolower($matches[1]);
109
110         preg_match('/type="(.*?)"/ism', $attributes, $matches);
111         if ($matches[1] != "")
112                 $type = strtolower($matches[1]);
113
114         if ($type == "")
115                 return(array());
116
117         if (!in_array($type, array("link", "audio", "photo", "video")))
118                 return(array());
119
120         if ($type != "")
121                 $data["type"] = $type;
122
123         $url = "";
124         preg_match("/url='(.*?)'/ism", $attributes, $matches);
125         if ($matches[1] != "")
126                 $url = $matches[1];
127
128         preg_match('/url="(.*?)"/ism', $attributes, $matches);
129         if ($matches[1] != "")
130                 $url = $matches[1];
131
132         if ($url != "")
133                 $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
134
135         $title = "";
136         preg_match("/title='(.*?)'/ism", $attributes, $matches);
137         if ($matches[1] != "")
138                 $title = $matches[1];
139
140         preg_match('/title="(.*?)"/ism', $attributes, $matches);
141         if ($matches[1] != "")
142                 $title = $matches[1];
143
144         if ($title != "") {
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("&#91;", "&#93;"), $title);
148                 $data["title"] = $title;
149         }
150
151         $image = "";
152         preg_match("/image='(.*?)'/ism", $attributes, $matches);
153         if ($matches[1] != "")
154                 $image = $matches[1];
155
156         preg_match('/image="(.*?)"/ism', $attributes, $matches);
157         if ($matches[1] != "")
158                 $image = $matches[1];
159
160         if ($image != "")
161                 $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
162
163         $preview = "";
164         preg_match("/preview='(.*?)'/ism", $attributes, $matches);
165         if ($matches[1] != "")
166                 $preview = $matches[1];
167
168         preg_match('/preview="(.*?)"/ism', $attributes, $matches);
169         if ($matches[1] != "")
170                 $preview = $matches[1];
171
172         if ($preview != "")
173                 $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
174
175         $data["description"] = trim($match[3]);
176
177         $data["after"] = trim($match[4]);
178
179         return($data);
180 }
181
182 function get_attached_data($body) {
183 /*
184  - text:
185  - type: link, video, photo
186  - title:
187  - url:
188  - image:
189  - description:
190  - (thumbnail)
191 */
192
193         $post = get_attachment_data($body);
194
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);
202
203                                 // Workaround:
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);
208
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"];
214                                         } else
215                                                 $post["image"] = $data["url"];
216
217                                         $post["preview"] = $pictures[0][2];
218                                         $post["text"] = str_replace($pictures[0][0], "", $body);
219                                 } else {
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);
226                                         }
227                                 }
228                         } elseif (count($pictures) > 1) {
229                                 $post["type"] = "link";
230                                 $post["url"] = $b["plink"];
231                                 $post["image"] = $pictures[0][2];
232                                 $post["text"] = $body;
233                         }
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;
244                         }
245                 }
246
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;
252                         }
253                 }
254                 if (!isset($post["type"])) {
255                         $post["type"] = "text";
256                         $post["text"] = trim($body);
257                 }
258         } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
259                 $data = ParseUrl::getSiteinfoCached($post["url"], true);
260
261                 if (isset($data["images"][0]))
262                         $post["image"] = $data["images"][0]["src"];
263         }
264
265         return($post);
266 }
267
268 function shortenmsg($msg, $limit, $twitter = false) {
269         /// @TODO
270         /// For Twitter URLs aren't shortened, but they have to be calculated as if.
271
272         $lines = explode("\n", $msg);
273         $msg = "";
274         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
275         $ellipsis = html_entity_decode("&#x2026;", 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;
282                 else
283                         break;
284         }
285         return($msg);
286 }
287
288 /**
289  * @brief Convert a message into plaintext for connectors to other networks
290  *
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.
297  *
298  * @return string The converted message
299  */
300 function plaintext(App $a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
301
302         // Remove the hash tags
303         $URLSearchString = "^\[\]";
304         $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
305
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);
308
309         // Remove the abstract
310         $body = remove_abstract($body);
311
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);
316
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"]);
321
322         $abstract = "";
323
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);
328
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))
332                         $abstract = "";
333
334         } else // Try to guess the correct target network
335                 switch ($htmlmode) {
336                         case 8:
337                                 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
338                                 break;
339                         case 7:
340                                 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
341                                 break;
342                         case 6:
343                                 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
344                                 break;
345                         default: // We don't know the exact target.
346                                  // We fetch an abstract since there is a posting limit.
347                                 if ($limit > 0)
348                                         $abstract = fetch_abstract($b["body"]);
349                 }
350
351         if ($abstract != "") {
352                 $post["text"] = $abstract;
353
354                 if ($post["type"] == "text") {
355                         $post["type"] = "link";
356                         $post["url"] = $b["plink"];
357                 }
358         }
359
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'));
363
364         $link = "";
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"];
374
375                 if (($msg == "") AND isset($post["title"]))
376                         $msg = trim($post["title"]);
377
378                 if (($msg == "") AND isset($post["description"]))
379                         $msg = trim($post["description"]);
380
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);
385
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);
395
396                                 $link = "";
397
398                                 if ($post["type"] == "text")
399                                         unset($post["url"]);
400                         }
401                 }
402         }
403
404         if ($limit > 0) {
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);
409
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;
413
414                 if (iconv_strlen($msg, "UTF-8") > $limit) {
415
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"];
425
426                         $msg = shortenmsg($msg, $limit);
427                 }
428         }
429
430         $post["text"] = trim($msg);
431
432         return($post);
433 }
434 ?>