3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Content\Text;
24 use Friendica\Core\Protocol;
26 use Friendica\Model\Photo;
27 use Friendica\Model\Post;
28 use Friendica\Util\Network;
32 // Assumed length of an URL when shortened via the network's own url shortener (e.g. Twitter)
33 const URL_LENGTH = 23;
43 * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
45 public static function shorten(string $msg, int $limit, int $uid = 0): string
47 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
49 if (!empty($uid) && DI::pConfig()->get($uid, 'system', 'simple_shortening')) {
50 return mb_substr(mb_substr(trim($msg), 0, $limit), 0, -3) . $ellipsis;
53 $lines = explode("\n", $msg);
55 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
56 foreach ($lines as $row => $line) {
57 if (mb_strlen(trim($msg . "\n" . $line)) <= $limit) {
58 $msg = trim($msg . "\n" . $line);
59 } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
60 // Is the new message empty by now or is it a reshared message?
61 $msg = mb_substr(mb_substr(trim($msg . "\n" . $line), 0, $limit), 0, -3) . $ellipsis;
71 * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
73 * @param string $text Text to search
74 * @param string $open Left boundary
75 * @param string $close Right boundary
76 * @param int $occurrences Number of first occurrences to skip
77 * @return boolean|array
79 public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
81 if ($occurrences < 0) {
86 for ($i = 0; $i <= $occurrences; $i++) {
87 if ($start_pos !== false) {
88 $start_pos = strpos($text, $open, $start_pos + 1);
92 if ($start_pos === false) {
96 $end_pos = strpos($text, $close, $start_pos);
98 if ($end_pos === false) {
102 $res = ['start' => $start_pos, 'end' => $end_pos];
108 * Convert a message into plaintext for connectors to other networks
110 * @param array $item The message array that is about to be posted
111 * @param int $limit The maximum number of characters when posting to that network
112 * @param bool $includedlinks Has an attached link to be included into the message?
113 * @param int $htmlmode This controls the behavior of the BBCode conversion
115 * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
116 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
117 * @see \Friendica\Content\Text\BBCode::getAttachedData
119 public static function getPost(array $item, int $limit = 0, bool $includedlinks = false, int $htmlmode = BBCode::MASTODON_API)
121 // Fetch attached media information
122 $post = self::getPostMedia($item);
124 if (($item['title'] != '') && ($post['text'] != '')) {
125 $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
126 } elseif ($item['title'] != '') {
127 $post['text'] = trim($item['title']);
130 // Fetch the abstract from the given target network
132 case BBCode::TWITTER:
133 $abstract = BBCode::getAbstract($item['body'], Protocol::TWITTER);
136 case BBCode::OSTATUS:
137 $abstract = BBCode::getAbstract($item['body'], Protocol::STATUSNET);
140 case BBCode::BLUESKY:
141 $abstract = BBCode::getAbstract($item['body'], Protocol::BLUESKY);
144 default: // We don't know the exact target.
145 // We fetch an abstract since there is a posting limit.
147 $abstract = BBCode::getAbstract($item['body']);
151 if ($abstract != '') {
152 $post['text'] = $abstract;
154 if ($post['type'] == 'text') {
155 $post['type'] = 'link';
156 $post['url'] = $item['plink'];
160 $html = BBCode::convertForUriId($item['uri-id'], $post['text'] . ($post['after'] ?? ''), $htmlmode);
161 $msg = HTML::toPlaintext($html, 0, true);
162 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
164 $complete_msg = $msg;
167 if ($includedlinks) {
168 if ($post['type'] == 'link') {
169 $link = $post['url'];
170 } elseif ($post['type'] == 'text') {
171 $link = $post['url'] ?? '';
172 } elseif ($post['type'] == 'video') {
173 $link = $post['url'];
174 } elseif ($post['type'] == 'photo') {
175 $link = $post['image'];
178 if (($msg == '') && isset($post['title'])) {
179 $msg = trim($post['title']);
182 if (($msg == '') && isset($post['description'])) {
183 $msg = trim($post['description']);
186 // If the link is already contained in the post, then it needn't to be added again
187 // But: if the link is beyond the limit, then it has to be added.
188 if (($link != '') && strstr($msg, $link)) {
189 $pos = strpos($msg, $link);
191 // Will the text be shortened in the link?
192 // Or is the link the last item in the post?
193 if (($limit > 0) && ($pos < $limit) && (($pos + self::URL_LENGTH > $limit) || ($pos + mb_strlen($link) == mb_strlen($msg)))) {
194 $msg = trim(str_replace($link, '', $msg));
195 } elseif (($limit == 0) || ($pos < $limit)) {
196 // The limit has to be increased since it will be shortened - but not now
197 // Only do it with Twitter
198 if (($limit > 0) && (mb_strlen($link) > self::URL_LENGTH) && ($htmlmode == BBCode::TWITTER)) {
199 $limit = $limit - self::URL_LENGTH + mb_strlen($link);
204 if ($post['type'] == 'text') {
212 // Reduce multiple spaces
213 // When posted to a network with limited space, we try to gain space where possible
214 while (strpos($msg, ' ') !== false) {
215 $msg = str_replace(' ', ' ', $msg);
218 if (!in_array($link, ['', $item['plink']]) && ($post['type'] != 'photo') && (strpos($complete_msg, $link) === false)) {
219 $complete_msg .= "\n" . $link;
222 $post['parts'] = self::getParts(trim($complete_msg), $limit);
224 // Twitter is using its own limiter, so we always assume that shortened links will have this length
225 if (mb_strlen($link) > 0) {
226 $limit = $limit - self::URL_LENGTH;
229 if (mb_strlen($msg) > $limit) {
230 if (($post['type'] == 'text') && isset($post['url'])) {
231 $post['url'] = $item['plink'];
232 } elseif (!isset($post['url'])) {
233 $limit = $limit - self::URL_LENGTH;
234 $post['url'] = $item['plink'];
235 } elseif (strpos($item['body'], '[share') !== false) {
236 $post['url'] = $item['plink'];
237 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
238 $post['url'] = $item['plink'];
240 $msg = self::shorten($msg, $limit, $item['uid']);
244 $post['text'] = trim($msg);
250 * Split the message in parts
252 * @param string $message
253 * @param integer $baselimit
256 private static function getParts(string $message, int $baselimit): array
264 $pos1 = strpos($message, ' ');
265 $pos2 = strpos($message, "\n");
267 if (($pos1 !== false) && ($pos2 !== false)) {
268 $pos = min($pos1, $pos2) + 1;
269 } elseif ($pos1 !== false) {
271 } elseif ($pos2 !== false) {
278 if (trim($message)) {
279 $word = substr($message, 0, $pos);
280 $message = trim(substr($message, $pos));
283 if (Network::isValidHttpUrl(trim($word))) {
284 $limit += mb_strlen(trim($word)) - self::URL_LENGTH;
287 if ((mb_strlen($part . $word) > $limit - 8) && ($parts || (mb_strlen($part . $word . $message) > $limit))) {
288 $parts[] = trim($part);
294 $parts[] = trim($part);
296 if (count($parts) > 1) {
297 foreach ($parts as $key => $part) {
298 $parts[$key] .= ' (' . ($key + 1) . '/' . count($parts) . ')';
306 * Fetch attached media to the post and simplify the body.
311 private static function getPostMedia(array $item): array
313 $post = ['type' => 'text', 'images' => [], 'remote_images' => []];
315 // Remove mentions and hashtag links
316 $URLSearchString = '^\[\]';
317 $post['text'] = preg_replace("/([#!@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
320 $post['text'] = BBCode::stripAbstract($post['text']);
321 // Remove attached links
322 $post['text'] = BBCode::removeAttachment($post['text']);
324 $post['text'] = Post\Media::removeFromBody($post['text']);
326 $images = Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE]);
327 if (!empty($item['quote-uri-id'])) {
328 $images = array_merge($images, Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::IMAGE]));
330 foreach ($images as $image) {
331 if ($id = Photo::getIdForName($image['url'])) {
332 $post['images'][] = ['url' => $image['url'], 'description' => $image['description'], 'id' => $id];
334 $post['remote_images'][] = ['url' => $image['url'], 'description' => $image['description']];
338 if (empty($post['images'])) {
339 unset($post['images']);
342 if (empty($post['remote_images'])) {
343 unset($post['remote_images']);
346 if (!empty($post['images'])) {
347 $post['type'] = 'photo';
348 $post['image'] = $post['images'][0]['url'];
349 $post['image_description'] = $post['images'][0]['description'];
350 } elseif (!empty($post['remote_images'])) {
351 $post['type'] = 'photo';
352 $post['image'] = $post['remote_images'][0]['url'];
353 $post['image_description'] = $post['remote_images'][0]['description'];
356 // Look for audio or video links
357 $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO]);
358 if (!empty($item['quote-uri-id'])) {
359 $media = array_merge($media, Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO]));
362 foreach ($media as $medium) {
363 if (in_array($medium['type'], [Post\Media::AUDIO, Post\Media::VIDEO])) {
364 $post['type'] = 'link';
365 $post['url'] = $medium['url'];
369 // Look for an attached link
370 $page = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
371 if (!empty($item['quote-uri-id']) && empty($page)) {
372 $page = Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::HTML]);
375 $post['type'] = 'link';
376 $post['url'] = $page[0]['url'];
377 $post['description'] = $page[0]['description'];
378 $post['title'] = $page[0]['name'];
380 if (empty($post['image']) && !empty($page[0]['preview'])) {
381 $post['image'] = $page[0]['preview'];