]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Plaintext.php
Update function calls
[friendica.git] / src / Content / Text / Plaintext.php
1 <?php
2 /**
3  * @file src/Content/Text/Plaintext.php
4  */
5 namespace Friendica\Content\Text;
6
7 use Friendica\App;
8 use Friendica\Core\PConfig;
9 use Friendica\Object\Image;
10 use Friendica\Util\ParseUrl;
11
12 require_once "include/bbcode.php";
13 require_once "include/html2plain.php";
14 require_once "include/network.php";
15
16 class Plaintext
17 {
18         /**
19          * @brief Fetches attachment data that were generated the old way
20          *
21          * @param string $body Message body
22          * @return array
23          * 'type' -> Message type ("link", "video", "photo")
24          * 'text' -> Text before the shared message
25          * 'after' -> Text after the shared message
26          * 'image' -> Preview image of the message
27          * 'url' -> Url to the attached message
28          * 'title' -> Title of the attachment
29          * 'description' -> Description of the attachment
30          */
31         private static function getOldAttachmentData($body)
32         {
33                 $post = [];
34
35                 // Simplify image codes
36                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
37
38                 if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism", $body, $attached, PREG_SET_ORDER)) {
39                         foreach ($attached as $data) {
40                                 if (!in_array($data[1], ["type-link", "type-video", "type-photo"])) {
41                                         continue;
42                                 }
43
44                                 $post["type"] = substr($data[1], 5);
45
46                                 $pos = strpos($body, $data[0]);
47                                 if ($pos > 0) {
48                                         $post["text"] = trim(substr($body, 0, $pos));
49                                         $post["after"] = trim(substr($body, $pos + strlen($data[0])));
50                                 } else {
51                                         $post["text"] = trim(str_replace($data[0], "", $body));
52                                 }
53
54                                 $attacheddata = $data[2];
55
56                                 $URLSearchString = "^\[\]";
57
58                                 if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches)) {
59
60                                         $picturedata = Image::getInfoFromURL($matches[1]);
61
62                                         if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
63                                                 $post["image"] = $matches[1];
64                                         } else {
65                                                 $post["preview"] = $matches[1];
66                                         }
67                                 }
68
69                                 if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
70                                         $post["url"] = $matches[1];
71                                         $post["title"] = $matches[2];
72                                 }
73                                 if (($post["url"] == "") && (in_array($post["type"], ["link", "video"]))
74                                         && preg_match("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
75                                         $post["url"] = $matches[1];
76                                 }
77
78                                 // Search for description
79                                 if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches)) {
80                                         $post["description"] = $matches[1];
81                                 }
82                         }
83                 }
84                 return $post;
85         }
86
87         /**
88          * @brief Fetches attachment data that were generated with the "attachment" element
89          *
90          * @param string $body Message body
91          * @return array
92          * 'type' -> Message type ("link", "video", "photo")
93          * 'text' -> Text before the shared message
94          * 'after' -> Text after the shared message
95          * 'image' -> Preview image of the message
96          * 'url' -> Url to the attached message
97          * 'title' -> Title of the attachment
98          * 'description' -> Description of the attachment
99          */
100         public static function getAttachmentData($body)
101         {
102                 $data = [];
103
104                 if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
105                         return self::getOldAttachmentData($body);
106                 }
107
108                 $attributes = $match[2];
109
110                 $data["text"] = trim($match[1]);
111
112                 $type = "";
113                 preg_match("/type='(.*?)'/ism", $attributes, $matches);
114                 if (x($matches, 1)) {
115                         $type = strtolower($matches[1]);
116                 }
117
118                 preg_match('/type="(.*?)"/ism', $attributes, $matches);
119                 if (x($matches, 1)) {
120                         $type = strtolower($matches[1]);
121                 }
122
123                 if ($type == "") {
124                         return [];
125                 }
126
127                 if (!in_array($type, ["link", "audio", "photo", "video"])) {
128                         return [];
129                 }
130
131                 if ($type != "") {
132                         $data["type"] = $type;
133                 }
134
135                 $url = "";
136                 preg_match("/url='(.*?)'/ism", $attributes, $matches);
137                 if (x($matches, 1)) {
138                         $url = $matches[1];
139                 }
140
141                 preg_match('/url="(.*?)"/ism', $attributes, $matches);
142                 if (x($matches, 1)) {
143                         $url = $matches[1];
144                 }
145
146                 if ($url != "") {
147                         $data["url"] = html_entity_decode($url, ENT_QUOTES, 'UTF-8');
148                 }
149
150                 $title = "";
151                 preg_match("/title='(.*?)'/ism", $attributes, $matches);
152                 if (x($matches, 1)) {
153                         $title = $matches[1];
154                 }
155
156                 preg_match('/title="(.*?)"/ism', $attributes, $matches);
157                 if (x($matches, 1)) {
158                         $title = $matches[1];
159                 }
160
161                 if ($title != "") {
162                         $title = bbcode(html_entity_decode($title, ENT_QUOTES, 'UTF-8'), false, false, true);
163                         $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
164                         $title = str_replace(["[", "]"], ["&#91;", "&#93;"], $title);
165                         $data["title"] = $title;
166                 }
167
168                 $image = "";
169                 preg_match("/image='(.*?)'/ism", $attributes, $matches);
170                 if (x($matches, 1)) {
171                         $image = $matches[1];
172                 }
173
174                 preg_match('/image="(.*?)"/ism', $attributes, $matches);
175                 if (x($matches, 1)) {
176                         $image = $matches[1];
177                 }
178
179                 if ($image != "") {
180                         $data["image"] = html_entity_decode($image, ENT_QUOTES, 'UTF-8');
181                 }
182
183                 $preview = "";
184                 preg_match("/preview='(.*?)'/ism", $attributes, $matches);
185                 if (x($matches, 1)) {
186                         $preview = $matches[1];
187                 }
188
189                 preg_match('/preview="(.*?)"/ism', $attributes, $matches);
190                 if (x($matches, 1)) {
191                         $preview = $matches[1];
192                 }
193
194                 if ($preview != "") {
195                         $data["preview"] = html_entity_decode($preview, ENT_QUOTES, 'UTF-8');
196                 }
197
198                 $data["description"] = trim($match[3]);
199
200                 $data["after"] = trim($match[4]);
201
202                 return $data;
203         }
204
205         public static function getAttachedData($body, $item = [])
206         {
207                 /*
208                 - text:
209                 - type: link, video, photo
210                 - title:
211                 - url:
212                 - image:
213                 - description:
214                 - (thumbnail)
215                 */
216
217                 $has_title = !empty($item['title']);
218                 $plink = (!empty($item['plink']) ? $item['plink'] : '');
219                 $post = self::getAttachmentData($body);
220
221                 // if nothing is found, it maybe having an image.
222                 if (!isset($post["type"])) {
223                         // Simplify image codes
224                         $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
225
226                         $URLSearchString = "^\[\]";
227                         if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures, PREG_SET_ORDER)) {
228                                 if ((count($pictures) == 1) && !$has_title) {
229                                         // Checking, if the link goes to a picture
230                                         $data = ParseUrl::getSiteinfoCached($pictures[0][1], true);
231
232                                         // Workaround:
233                                         // Sometimes photo posts to the own album are not detected at the start.
234                                         // So we seem to cannot use the cache for these cases. That's strange.
235                                         if (($data["type"] != "photo") && strstr($pictures[0][1], "/photos/")) {
236                                                 $data = ParseUrl::getSiteinfo($pictures[0][1], true);
237                                         }
238
239                                         if ($data["type"] == "photo") {
240                                                 $post["type"] = "photo";
241                                                 if (isset($data["images"][0])) {
242                                                         $post["image"] = $data["images"][0]["src"];
243                                                         $post["url"] = $data["url"];
244                                                 } else {
245                                                         $post["image"] = $data["url"];
246                                                 }
247
248                                                 $post["preview"] = $pictures[0][2];
249                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
250                                         } else {
251                                                 $imgdata = Image::getInfoFromURL($pictures[0][1]);
252                                                 if (substr($imgdata["mime"], 0, 6) == "image/") {
253                                                         $post["type"] = "photo";
254                                                         $post["image"] = $pictures[0][1];
255                                                         $post["preview"] = $pictures[0][2];
256                                                         $post["text"] = str_replace($pictures[0][0], "", $body);
257                                                 }
258                                         }
259                                 } elseif (count($pictures) > 0) {
260                                         $post["type"] = "link";
261                                         $post["url"] = $plink;
262                                         $post["image"] = $pictures[0][2];
263                                         $post["text"] = $body;
264                                 }
265                         } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures, PREG_SET_ORDER)) {
266                                 if ((count($pictures) == 1) && !$has_title) {
267                                         $post["type"] = "photo";
268                                         $post["image"] = $pictures[0][1];
269                                         $post["text"] = str_replace($pictures[0][0], "", $body);
270                                 } elseif (count($pictures) > 0) {
271                                         $post["type"] = "link";
272                                         $post["url"] = $plink;
273                                         $post["image"] = $pictures[0][1];
274                                         $post["text"] = $body;
275                                 }
276                         }
277
278                         // Test for the external links
279                         preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links1, PREG_SET_ORDER);
280                         preg_match_all("(\[url\=([$URLSearchString]*)\].*?\[\/url\])ism", $body, $links2, PREG_SET_ORDER);
281
282                         $links = array_merge($links1, $links2);
283
284                         // If there is only a single one, then use it.
285                         // This should cover link posts via API.
286                         if ((count($links) == 1) && !isset($post["preview"]) && !$has_title) {
287                                 $post["type"] = "link";
288                                 $post["text"] = trim($body);
289                                 $post["url"] = $links[0][1];
290                         }
291
292                         // Now count the number of external media links
293                         preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $body, $links1, PREG_SET_ORDER);
294                         preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $body, $links2, PREG_SET_ORDER);
295                         preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $body, $links3, PREG_SET_ORDER);
296                         preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $body, $links4, PREG_SET_ORDER);
297
298                         // Add them to the other external links
299                         $links = array_merge($links, $links1, $links2, $links3, $links4);
300
301                         // Are there more than one?
302                         if (count($links) > 1) {
303                                 // The post will be the type "text", which means a blog post
304                                 unset($post["type"]);
305                                 $post["url"] = $plink;
306                         }
307
308                         if (!isset($post["type"])) {
309                                 $post["type"] = "text";
310                                 $post["text"] = trim($body);
311                         }
312                 } elseif (isset($post["url"]) && ($post["type"] == "video")) {
313                         $data = ParseUrl::getSiteinfoCached($post["url"], true);
314
315                         if (isset($data["images"][0])) {
316                                 $post["image"] = $data["images"][0]["src"];
317                         }
318                 }
319
320                 return $post;
321         }
322
323         /**
324          * Shortens message
325          *
326          * @param type $msg
327          * @param type $limit
328          * @return type
329          *
330          * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
331          */
332         public static function shortenMsg($msg, $limit)
333         {
334                 $lines = explode("\n", $msg);
335                 $msg = "";
336                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
337                 $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
338                 foreach ($lines as $row => $line) {
339                         if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
340                                 $msg = trim($msg . "\n" . $line);
341                         } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
342                                 // Is the new message empty by now or is it a reshared message?
343                                 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
344                         } else {
345                                 break;
346                         }
347                 }
348
349                 return $msg;
350         }
351
352         /**
353          * @brief Convert a message into plaintext for connectors to other networks
354          *
355          * @param array $b The message array that is about to be posted
356          * @param int $limit The maximum number of characters when posting to that network
357          * @param bool $includedlinks Has an attached link to be included into the message?
358          * @param int $htmlmode This triggers the behaviour of the bbcode conversion
359          * @param string $target_network Name of the network where the post should go to.
360          *
361          * @return string The converted message
362          */
363         public static function toPlaintext($b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "")
364         {
365                 // Remove the hash tags
366                 $URLSearchString = "^\[\]";
367                 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
368
369                 // Add an URL element if the text contains a raw link
370                 $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
371
372                 // Remove the abstract
373                 $body = remove_abstract($body);
374
375                 // At first look at data that is attached via "type-..." stuff
376                 // This will hopefully replaced with a dedicated bbcode later
377                 //$post = self::getAttachedData($b["body"]);
378                 $post = self::getAttachedData($body, $b);
379
380                 if (($b["title"] != "") && ($post["text"] != "")) {
381                         $post["text"] = trim($b["title"]."\n\n".$post["text"]);
382                 } elseif ($b["title"] != "") {
383                         $post["text"] = trim($b["title"]);
384                 }
385
386                 $abstract = "";
387
388                 // Fetch the abstract from the given target network
389                 if ($target_network != "") {
390                         $default_abstract = fetch_abstract($b["body"]);
391                         $abstract = fetch_abstract($b["body"], $target_network);
392
393                         // If we post to a network with no limit we only fetch
394                         // an abstract exactly for this network
395                         if (($limit == 0) && ($abstract == $default_abstract)) {
396                                 $abstract = "";
397                         }
398                 } else {// Try to guess the correct target network
399                         switch ($htmlmode) {
400                                 case 8:
401                                         $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
402                                         break;
403                                 case 7:
404                                         $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
405                                         break;
406                                 case 6:
407                                         $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
408                                         break;
409                                 default: // We don't know the exact target.
410                                         // We fetch an abstract since there is a posting limit.
411                                         if ($limit > 0) {
412                                                 $abstract = fetch_abstract($b["body"]);
413                                         }
414                         }
415                 }
416
417                 if ($abstract != "") {
418                         $post["text"] = $abstract;
419
420                         if ($post["type"] == "text") {
421                                 $post["type"] = "link";
422                                 $post["url"] = $b["plink"];
423                         }
424                 }
425
426                 $html = bbcode($post["text"].$post["after"], false, false, $htmlmode);
427                 $msg = html2plain($html, 0, true);
428                 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
429
430                 $link = "";
431                 if ($includedlinks) {
432                         if ($post["type"] == "link") {
433                                 $link = $post["url"];
434                         } elseif ($post["type"] == "text") {
435                                 $link = $post["url"];
436                         } elseif ($post["type"] == "video") {
437                                 $link = $post["url"];
438                         } elseif ($post["type"] == "photo") {
439                                 $link = $post["image"];
440                         }
441
442                         if (($msg == "") && isset($post["title"])) {
443                                 $msg = trim($post["title"]);
444                         }
445
446                         if (($msg == "") && isset($post["description"])) {
447                                 $msg = trim($post["description"]);
448                         }
449
450                         // If the link is already contained in the post, then it neeedn't to be added again
451                         // But: if the link is beyond the limit, then it has to be added.
452                         if (($link != "") && strstr($msg, $link)) {
453                                 $pos = strpos($msg, $link);
454
455                                 // Will the text be shortened in the link?
456                                 // Or is the link the last item in the post?
457                                 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
458                                         $msg = trim(str_replace($link, "", $msg));
459                                 } elseif (($limit == 0) || ($pos < $limit)) {
460                                         // The limit has to be increased since it will be shortened - but not now
461                                         // Only do it with Twitter (htmlmode = 8)
462                                         if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
463                                                 $limit = $limit - 23 + strlen($link);
464                                         }
465
466                                         $link = "";
467
468                                         if ($post["type"] == "text") {
469                                                 unset($post["url"]);
470                                         }
471                                 }
472                         }
473                 }
474
475                 if ($limit > 0) {
476                         // Reduce multiple spaces
477                         // When posted to a network with limited space, we try to gain space where possible
478                         while (strpos($msg, "  ") !== false) {
479                                 $msg = str_replace("  ", " ", $msg);
480                         }
481
482                         // Twitter is using its own limiter, so we always assume that shortened links will have this length
483                         if (iconv_strlen($link, "UTF-8") > 0) {
484                                 $limit = $limit - 23;
485                         }
486
487                         if (iconv_strlen($msg, "UTF-8") > $limit) {
488                                 if (($post["type"] == "text") && isset($post["url"])) {
489                                         $post["url"] = $b["plink"];
490                                 } elseif (!isset($post["url"])) {
491                                         $limit = $limit - 23;
492                                         $post["url"] = $b["plink"];
493                                 // Which purpose has this line? It is now uncommented, but left as a reminder
494                                 //} elseif (strpos($b["body"], "[share") !== false) {
495                                 //      $post["url"] = $b["plink"];
496                                 } elseif (PConfig::get($b["uid"], "system", "no_intelligent_shortening")) {
497                                         $post["url"] = $b["plink"];
498                                 }
499                                 $msg = self::shortenMsg($msg, $limit);
500                         }
501                 }
502
503                 $post["text"] = trim($msg);
504
505                 return($post);
506         }
507 }