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;
29 use Friendica\Util\Strings;
33 // Assumed length of an URL when shortened via the network's own url shortener (e.g. Twitter)
34 const URL_LENGTH = 23;
44 * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
46 public static function shorten(string $msg, int $limit, int $uid = 0): string
48 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
50 if (!empty($uid) && DI::pConfig()->get($uid, 'system', 'simple_shortening')) {
51 return mb_substr(mb_substr(trim($msg), 0, $limit), 0, -3) . $ellipsis;
54 $lines = explode("\n", $msg);
56 $recycle = html_entity_decode("♲ ", 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;
72 * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
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
80 public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
82 if ($occurrences < 0) {
87 for ($i = 0; $i <= $occurrences; $i++) {
88 if ($start_pos !== false) {
89 $start_pos = strpos($text, $open, $start_pos + 1);
93 if ($start_pos === false) {
97 $end_pos = strpos($text, $close, $start_pos);
99 if ($end_pos === false) {
103 $res = ['start' => $start_pos, 'end' => $end_pos];
109 * Convert a message into plaintext for connectors to other networks
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
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
120 public static function getPost(array $item, int $limit = 0, bool $includedlinks = false, int $htmlmode = BBCode::MASTODON_API)
122 // Fetch attached media information
123 $post = self::getPostMedia($item);
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']);
131 // Fetch the abstract from the given target network
133 case BBCode::TWITTER:
134 $abstract = BBCode::getAbstract($item['body'], Protocol::TWITTER);
137 case BBCode::OSTATUS:
138 $abstract = BBCode::getAbstract($item['body'], Protocol::STATUSNET);
141 default: // We don't know the exact target.
142 // We fetch an abstract since there is a posting limit.
144 $abstract = BBCode::getAbstract($item['body']);
148 if ($abstract != '') {
149 $post['text'] = $abstract;
151 if ($post['type'] == 'text') {
152 $post['type'] = 'link';
153 $post['url'] = $item['plink'];
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'));
161 $complete_msg = $msg;
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'];
175 if (($msg == '') && isset($post['title'])) {
176 $msg = trim($post['title']);
179 if (($msg == '') && isset($post['description'])) {
180 $msg = trim($post['description']);
183 // If the link is already contained in the post, then it needn'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);
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);
201 if ($post['type'] == 'text') {
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);
215 if (!in_array($link, ['', $item['plink']]) && ($post['type'] != 'photo') && (strpos($complete_msg, $link) === false)) {
216 $complete_msg .= "\n" . $link;
219 $post['parts'] = self::getParts(trim($complete_msg), $limit);
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;
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'];
237 $msg = self::shorten($msg, $limit, $item['uid']);
241 $post['text'] = trim($msg);
247 * Split the message in parts
249 * @param string $message
250 * @param integer $baselimit
253 private static function getParts(string $message, int $baselimit): array
261 $pos1 = strpos($message, ' ');
262 $pos2 = strpos($message, "\n");
264 if (($pos1 !== false) && ($pos2 !== false)) {
265 $pos = min($pos1, $pos2) + 1;
266 } elseif ($pos1 !== false) {
268 } elseif ($pos2 !== false) {
275 if (trim($message)) {
276 $word = substr($message, 0, $pos);
277 $message = trim(substr($message, $pos));
280 if (Network::isValidHttpUrl(trim($word))) {
281 $limit += mb_strlen(trim($word)) - self::URL_LENGTH;
284 if ((mb_strlen($part . $word) > $limit - 8) && ($parts || (mb_strlen($part . $word . $message) > $limit))) {
285 $parts[] = trim($part);
291 $parts[] = trim($part);
293 if (count($parts) > 1) {
294 foreach ($parts as $key => $part) {
295 $parts[$key] .= ' (' . ($key + 1) . '/' . count($parts) . ')';
303 * Fetch attached media to the post and simplify the body.
308 private static function getPostMedia(array $item): array
310 $post = ['type' => 'text', 'images' => [], 'remote_images' => []];
312 // Remove mentions and hashtag links
313 $URLSearchString = '^\[\]';
314 $post['text'] = preg_replace("/([#!@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
317 $post['text'] = BBCode::stripAbstract($post['text']);
318 // Remove attached links
319 $post['text'] = BBCode::removeAttachment($post['text']);
321 $post['text'] = Post\Media::removeFromBody($post['text']);
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]));
327 foreach ($images as $image) {
328 if ($id = Photo::getIdForName($image['url'])) {
329 $post['images'][] = ['url' => $image['url'], 'description' => $image['description'], 'id' => $id];
331 $post['remote_images'][] = ['url' => $image['url'], 'description' => $image['description']];
335 if (empty($post['images'])) {
336 unset($post['images']);
339 if (empty($post['remote_images'])) {
340 unset($post['remote_images']);
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'];
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]));
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'];
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]);
372 $post['type'] = 'link';
373 $post['url'] = $page[0]['url'];
374 $post['description'] = $page[0]['description'];
375 $post['title'] = $page[0]['name'];
377 if (empty($post['image']) && !empty($page[0]['preview'])) {
378 $post['image'] = $page[0]['preview'];