]> git.mxchange.org Git - friendica.git/blob - src/Content/Text/Plaintext.php
spelling: hierarchical
[friendica.git] / src / Content / Text / Plaintext.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content\Text;
23
24 use Friendica\Core\Protocol;
25 use Friendica\DI;
26 use Friendica\Model\Photo;
27 use Friendica\Model\Post;
28 use Friendica\Util\Network;
29 use Friendica\Util\Strings;
30
31 class Plaintext
32 {
33         // Assumed length of an URL when shortened via the network's own url shortener (e.g. Twitter)
34         const URL_LENGTH = 23;
35
36         /**
37          * Shortens message
38          *
39          * @param  string $msg
40          * @param  int    $limit
41          * @param  int    $uid
42          * @return string
43          *
44          * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
45          */
46         public static function shorten(string $msg, int $limit, int $uid = 0): string
47         {
48                 $ellipsis = html_entity_decode("&#x2026;", ENT_QUOTES, 'UTF-8');
49
50                 if (!empty($uid) && DI::pConfig()->get($uid, 'system', 'simple_shortening')) {
51                         return mb_substr(mb_substr(trim($msg), 0, $limit), 0, -3) . $ellipsis;
52                 }
53
54                 $lines = explode("\n", $msg);
55                 $msg = "";
56                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
57                 foreach ($lines as $row => $line) {
58                         if (mb_strlen(trim($msg . "\n" . $line)) <= $limit) {
59                                 $msg = trim($msg . "\n" . $line);
60                         } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
61                                 // Is the new message empty by now or is it a reshared message?
62                                 $msg = mb_substr(mb_substr(trim($msg . "\n" . $line), 0, $limit), 0, -3) . $ellipsis;
63                         } else {
64                                 break;
65                         }
66                 }
67
68                 return $msg;
69         }
70
71         /**
72          * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
73          *
74          * @param string $text        Text to search
75          * @param string $open        Left boundary
76          * @param string $close       Right boundary
77          * @param int    $occurrences Number of first occurrences to skip
78          * @return boolean|array
79          */
80         public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
81         {
82                 if ($occurrences < 0) {
83                         $occurrences = 0;
84                 }
85
86                 $start_pos = -1;
87                 for ($i = 0; $i <= $occurrences; $i++) {
88                         if ($start_pos !== false) {
89                                 $start_pos = strpos($text, $open, $start_pos + 1);
90                         }
91                 }
92
93                 if ($start_pos === false) {
94                         return false;
95                 }
96
97                 $end_pos = strpos($text, $close, $start_pos);
98
99                 if ($end_pos === false) {
100                         return false;
101                 }
102
103                 $res = ['start' => $start_pos, 'end' => $end_pos];
104
105                 return $res;
106         }
107
108         /**
109          * Convert a message into plaintext for connectors to other networks
110          *
111          * @param array  $item           The message array that is about to be posted
112          * @param int    $limit          The maximum number of characters when posting to that network
113          * @param bool   $includedlinks  Has an attached link to be included into the message?
114          * @param int    $htmlmode       This controls the behavior of the BBCode conversion
115          *
116          * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
117          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
118          * @see   \Friendica\Content\Text\BBCode::getAttachedData
119          */
120         public static function getPost(array $item, int $limit = 0, bool $includedlinks = false, int $htmlmode = BBCode::MASTODON_API)
121         {
122                 // Fetch attached media information
123                 $post = self::getPostMedia($item);
124
125                 if (($item['title'] != '') && ($post['text'] != '')) {
126                         $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
127                 } elseif ($item['title'] != '') {
128                         $post['text'] = trim($item['title']);
129                 }
130
131                 // Fetch the abstract from the given target network
132                 switch ($htmlmode) {
133                         case BBCode::TWITTER:
134                                 $abstract = BBCode::getAbstract($item['body'], Protocol::TWITTER);
135                                 break;
136
137                         case BBCode::OSTATUS:
138                                 $abstract = BBCode::getAbstract($item['body'], Protocol::STATUSNET);
139                                 break;
140
141                         default: // We don't know the exact target.
142                                 // We fetch an abstract since there is a posting limit.
143                                 if ($limit > 0) {
144                                         $abstract = BBCode::getAbstract($item['body']);
145                                 }
146                 }
147
148                 if ($abstract != '') {
149                         $post['text'] = $abstract;
150
151                         if ($post['type'] == 'text') {
152                                 $post['type'] = 'link';
153                                 $post['url'] = $item['plink'];
154                         }
155                 }
156
157                 $html = BBCode::convertForUriId($item['uri-id'], $post['text'] . ($post['after'] ?? ''), $htmlmode);
158                 $msg = HTML::toPlaintext($html, 0, true);
159                 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
160
161                 $complete_msg = $msg;
162
163                 $link = '';
164                 if ($includedlinks) {
165                         if ($post['type'] == 'link') {
166                                 $link = $post['url'];
167                         } elseif ($post['type'] == 'text') {
168                                 $link = $post['url'] ?? '';
169                         } elseif ($post['type'] == 'video') {
170                                 $link = $post['url'];
171                         } elseif ($post['type'] == 'photo') {
172                                 $link = $post['image'];
173                         }
174
175                         if (($msg == '') && isset($post['title'])) {
176                                 $msg = trim($post['title']);
177                         }
178
179                         if (($msg == '') && isset($post['description'])) {
180                                 $msg = trim($post['description']);
181                         }
182
183                         // If the link is already contained in the post, then it neeedn't to be added again
184                         // But: if the link is beyond the limit, then it has to be added.
185                         if (($link != '') && strstr($msg, $link)) {
186                                 $pos = strpos($msg, $link);
187
188                                 // Will the text be shortened in the link?
189                                 // Or is the link the last item in the post?
190                                 if (($limit > 0) && ($pos < $limit) && (($pos + self::URL_LENGTH > $limit) || ($pos + mb_strlen($link) == mb_strlen($msg)))) {
191                                         $msg = trim(str_replace($link, '', $msg));
192                                 } elseif (($limit == 0) || ($pos < $limit)) {
193                                         // The limit has to be increased since it will be shortened - but not now
194                                         // Only do it with Twitter
195                                         if (($limit > 0) && (mb_strlen($link) > self::URL_LENGTH) && ($htmlmode == BBCode::TWITTER)) {
196                                                 $limit = $limit - self::URL_LENGTH + mb_strlen($link);
197                                         }
198
199                                         $link = '';
200
201                                         if ($post['type'] == 'text') {
202                                                 unset($post['url']);
203                                         }
204                                 }
205                         }
206                 }
207
208                 if ($limit > 0) {
209                         // Reduce multiple spaces
210                         // When posted to a network with limited space, we try to gain space where possible
211                         while (strpos($msg, '  ') !== false) {
212                                 $msg = str_replace('  ', ' ', $msg);
213                         }
214
215                         if (!in_array($link, ['', $item['plink']]) && ($post['type'] != 'photo') && (strpos($complete_msg, $link) === false)) {
216                                 $complete_msg .= "\n" . $link;
217                         }
218
219                         $post['parts'] = self::getParts(trim($complete_msg), $limit);
220
221                         // Twitter is using its own limiter, so we always assume that shortened links will have this length
222                         if (mb_strlen($link) > 0) {
223                                 $limit = $limit - self::URL_LENGTH;
224                         }
225
226                         if (mb_strlen($msg) > $limit) {
227                                 if (($post['type'] == 'text') && isset($post['url'])) {
228                                         $post['url'] = $item['plink'];
229                                 } elseif (!isset($post['url'])) {
230                                         $limit = $limit - self::URL_LENGTH;
231                                         $post['url'] = $item['plink'];
232                                 } elseif (strpos($item['body'], '[share') !== false) {
233                                         $post['url'] = $item['plink'];
234                                 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
235                                         $post['url'] = $item['plink'];
236                                 }
237                                 $msg = self::shorten($msg, $limit, $item['uid']);
238                         }
239                 }
240
241                 $post['text'] = trim($msg);
242
243                 return $post;
244         }
245
246         /**
247          * Split the message in parts
248          *
249          * @param string  $message
250          * @param integer $baselimit
251          * @return array
252          */
253         private static function getParts(string $message, int $baselimit): array
254         {
255                 $parts = [];
256                 $part = '';
257
258                 $limit = $baselimit;
259
260                 while ($message) {
261                         $pos1 = strpos($message, ' ');
262                         $pos2 = strpos($message, "\n");
263
264                         if (($pos1 !== false) && ($pos2 !== false)) {
265                                 $pos = min($pos1, $pos2) + 1;
266                         } elseif ($pos1 !== false) {
267                                 $pos = $pos1 + 1;
268                         } elseif ($pos2 !== false) {
269                                 $pos = $pos2 + 1;
270                         } else {
271                                 $word = $message;
272                                 $message = '';
273                         }
274
275                         if (trim($message)) {
276                                 $word    = substr($message, 0, $pos);
277                                 $message = trim(substr($message, $pos));
278                         }
279
280                         if (Network::isValidHttpUrl(trim($word))) {
281                                 $limit += mb_strlen(trim($word)) - self::URL_LENGTH;
282                         }
283
284                         if ((mb_strlen($part . $word) > $limit - 8) && ($parts || (mb_strlen($part . $word . $message) > $limit))) {
285                                 $parts[] = trim($part);
286                                 $part    = '';
287                                 $limit   = $baselimit;
288                         }
289                         $part .= $word;
290                 }
291                 $parts[] = trim($part);
292
293                 if (count($parts) > 1) {
294                         foreach ($parts as $key => $part) {
295                                 $parts[$key] .= ' (' . ($key + 1) . '/' . count($parts) . ')';
296                         }
297                 }
298
299                 return $parts;
300         }
301
302         /**
303          * Fetch attached media to the post and simplify the body.
304          *
305          * @param array $item
306          * @return array
307          */
308         private static function getPostMedia(array $item): array
309         {
310                 $post = ['type' => 'text', 'images' => [], 'remote_images' => []];
311
312                 // Remove mentions and hashtag links
313                 $URLSearchString = '^\[\]';
314                 $post['text'] = preg_replace("/([#!@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
315
316                 // Remove abstract
317                 $post['text'] = BBCode::stripAbstract($post['text']);
318                 // Remove attached links
319                 $post['text'] = BBCode::removeAttachment($post['text']);
320                 // Remove any links
321                 $post['text'] = Post\Media::removeFromBody($post['text']);
322
323                 $images = Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE]);
324                 if (!empty($item['quote-uri-id'])) {
325                         $images = array_merge($images, Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::IMAGE]));
326                 }
327                 foreach ($images as $image) {
328                         if ($id = Photo::getIdForName($image['url'])) {
329                                 $post['images'][] = ['url' => $image['url'], 'description' => $image['description'], 'id' => $id];
330                         } else {
331                                 $post['remote_images'][] = ['url' => $image['url'], 'description' => $image['description']];
332                         }
333                 }
334
335                 if (empty($post['images'])) {
336                         unset($post['images']);
337                 }
338
339                 if (empty($post['remote_images'])) {
340                         unset($post['remote_images']);
341                 }
342
343                 if (!empty($post['images'])) {
344                         $post['type']              = 'photo';
345                         $post['image']             = $post['images'][0]['url'];
346                         $post['image_description'] = $post['images'][0]['description'];
347                 } elseif (!empty($post['remote_images'])) {
348                         $post['type']              = 'photo';
349                         $post['image']             = $post['remote_images'][0]['url'];
350                         $post['image_description'] = $post['remote_images'][0]['description'];
351                 }
352
353                 // Look for audio or video links
354                 $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO]);
355                 if (!empty($item['quote-uri-id'])) {
356                         $media = array_merge($media, Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO]));
357                 }
358
359                 foreach ($media as $medium) {
360                         if (in_array($medium['type'], [Post\Media::AUDIO, Post\Media::VIDEO])) {
361                                 $post['type'] = 'link';
362                                 $post['url']  = $medium['url'];
363                         }
364                 }
365
366                 // Look for an attached link
367                 $page = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
368                 if (!empty($item['quote-uri-id']) && empty($page)) {
369                         $page = Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::HTML]);
370                 }
371                 if (!empty($page)) {
372                         $post['type']          = 'link';
373                         $post['url']           = $page[0]['url'];
374                         $post['description']   = $page[0]['description'];
375                         $post['title']         = $page[0]['name'];
376
377                         if (empty($post['image']) && !empty($page[0]['preview'])) {
378                                 $post['image'] = $page[0]['preview'];
379                         }
380                 }
381
382                 return $post;
383         }
384 }