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 default: // We don't know the exact target.
141 // We fetch an abstract since there is a posting limit.
143 $abstract = BBCode::getAbstract($item['body']);
147 if ($abstract != '') {
148 $post['text'] = $abstract;
150 if ($post['type'] == 'text') {
151 $post['type'] = 'link';
152 $post['url'] = $item['plink'];
156 $html = BBCode::convertForUriId($item['uri-id'], $post['text'] . ($post['after'] ?? ''), $htmlmode);
157 $msg = HTML::toPlaintext($html, 0, true);
158 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
160 $complete_msg = $msg;
163 if ($includedlinks) {
164 if ($post['type'] == 'link') {
165 $link = $post['url'];
166 } elseif ($post['type'] == 'text') {
167 $link = $post['url'] ?? '';
168 } elseif ($post['type'] == 'video') {
169 $link = $post['url'];
170 } elseif ($post['type'] == 'photo') {
171 $link = $post['image'];
174 if (($msg == '') && isset($post['title'])) {
175 $msg = trim($post['title']);
178 if (($msg == '') && isset($post['description'])) {
179 $msg = trim($post['description']);
182 // If the link is already contained in the post, then it needn't to be added again
183 // But: if the link is beyond the limit, then it has to be added.
184 if (($link != '') && strstr($msg, $link)) {
185 $pos = strpos($msg, $link);
187 // Will the text be shortened in the link?
188 // Or is the link the last item in the post?
189 if (($limit > 0) && ($pos < $limit) && (($pos + self::URL_LENGTH > $limit) || ($pos + mb_strlen($link) == mb_strlen($msg)))) {
190 $msg = trim(str_replace($link, '', $msg));
191 } elseif (($limit == 0) || ($pos < $limit)) {
192 // The limit has to be increased since it will be shortened - but not now
193 // Only do it with Twitter
194 if (($limit > 0) && (mb_strlen($link) > self::URL_LENGTH) && ($htmlmode == BBCode::TWITTER)) {
195 $limit = $limit - self::URL_LENGTH + mb_strlen($link);
200 if ($post['type'] == 'text') {
208 // Reduce multiple spaces
209 // When posted to a network with limited space, we try to gain space where possible
210 while (strpos($msg, ' ') !== false) {
211 $msg = str_replace(' ', ' ', $msg);
214 if (!in_array($link, ['', $item['plink']]) && ($post['type'] != 'photo') && (strpos($complete_msg, $link) === false)) {
215 $complete_msg .= "\n" . $link;
218 $post['parts'] = self::getParts(trim($complete_msg), $limit);
220 // Twitter is using its own limiter, so we always assume that shortened links will have this length
221 if (mb_strlen($link) > 0) {
222 $limit = $limit - self::URL_LENGTH;
225 if (mb_strlen($msg) > $limit) {
226 if (($post['type'] == 'text') && isset($post['url'])) {
227 $post['url'] = $item['plink'];
228 } elseif (!isset($post['url'])) {
229 $limit = $limit - self::URL_LENGTH;
230 $post['url'] = $item['plink'];
231 } elseif (strpos($item['body'], '[share') !== false) {
232 $post['url'] = $item['plink'];
233 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
234 $post['url'] = $item['plink'];
236 $msg = self::shorten($msg, $limit, $item['uid']);
240 $post['text'] = trim($msg);
246 * Split the message in parts
248 * @param string $message
249 * @param integer $baselimit
252 private static function getParts(string $message, int $baselimit): array
260 $pos1 = strpos($message, ' ');
261 $pos2 = strpos($message, "\n");
263 if (($pos1 !== false) && ($pos2 !== false)) {
264 $pos = min($pos1, $pos2) + 1;
265 } elseif ($pos1 !== false) {
267 } elseif ($pos2 !== false) {
274 if (trim($message)) {
275 $word = substr($message, 0, $pos);
276 $message = trim(substr($message, $pos));
279 if (Network::isValidHttpUrl(trim($word))) {
280 $limit += mb_strlen(trim($word)) - self::URL_LENGTH;
283 if ((mb_strlen($part . $word) > $limit - 8) && ($parts || (mb_strlen($part . $word . $message) > $limit))) {
284 $parts[] = trim($part);
290 $parts[] = trim($part);
292 if (count($parts) > 1) {
293 foreach ($parts as $key => $part) {
294 $parts[$key] .= ' (' . ($key + 1) . '/' . count($parts) . ')';
302 * Fetch attached media to the post and simplify the body.
307 private static function getPostMedia(array $item): array
309 $post = ['type' => 'text', 'images' => [], 'remote_images' => []];
311 // Remove mentions and hashtag links
312 $URLSearchString = '^\[\]';
313 $post['text'] = preg_replace("/([#!@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
316 $post['text'] = BBCode::stripAbstract($post['text']);
317 // Remove attached links
318 $post['text'] = BBCode::removeAttachment($post['text']);
320 $post['text'] = Post\Media::removeFromBody($post['text']);
322 $images = Post\Media::getByURIId($item['uri-id'], [Post\Media::IMAGE]);
323 if (!empty($item['quote-uri-id'])) {
324 $images = array_merge($images, Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::IMAGE]));
326 foreach ($images as $image) {
327 if ($id = Photo::getIdForName($image['url'])) {
328 $post['images'][] = ['url' => $image['url'], 'description' => $image['description'], 'id' => $id];
330 $post['remote_images'][] = ['url' => $image['url'], 'description' => $image['description']];
334 if (empty($post['images'])) {
335 unset($post['images']);
338 if (empty($post['remote_images'])) {
339 unset($post['remote_images']);
342 if (!empty($post['images'])) {
343 $post['type'] = 'photo';
344 $post['image'] = $post['images'][0]['url'];
345 $post['image_description'] = $post['images'][0]['description'];
346 } elseif (!empty($post['remote_images'])) {
347 $post['type'] = 'photo';
348 $post['image'] = $post['remote_images'][0]['url'];
349 $post['image_description'] = $post['remote_images'][0]['description'];
352 // Look for audio or video links
353 $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO]);
354 if (!empty($item['quote-uri-id'])) {
355 $media = array_merge($media, Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::AUDIO, Post\Media::VIDEO]));
358 foreach ($media as $medium) {
359 if (in_array($medium['type'], [Post\Media::AUDIO, Post\Media::VIDEO])) {
360 $post['type'] = 'link';
361 $post['url'] = $medium['url'];
365 // Look for an attached link
366 $page = Post\Media::getByURIId($item['uri-id'], [Post\Media::HTML]);
367 if (!empty($item['quote-uri-id']) && empty($page)) {
368 $page = Post\Media::getByURIId($item['quote-uri-id'], [Post\Media::HTML]);
371 $post['type'] = 'link';
372 $post['url'] = $page[0]['url'];
373 $post['description'] = $page[0]['description'];
374 $post['title'] = $page[0]['name'];
376 if (empty($post['image']) && !empty($page[0]['preview'])) {
377 $post['image'] = $page[0]['preview'];