3 * @copyright Copyright (C) 2010-2021, 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;
36 * @todo For Twitter URLs aren't shortened, but they have to be calculated as if.
38 public static function shorten($msg, $limit)
40 $lines = explode("\n", $msg);
42 $recycle = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8');
43 $ellipsis = html_entity_decode("…", ENT_QUOTES, 'UTF-8');
44 foreach ($lines as $row => $line) {
45 if (iconv_strlen(trim($msg . "\n" . $line), "UTF-8") <= $limit) {
46 $msg = trim($msg . "\n" . $line);
47 } elseif (($msg == "") || (($row == 1) && (substr($msg, 0, 4) == $recycle))) {
48 // Is the new message empty by now or is it a reshared message?
49 $msg = iconv_substr(iconv_substr(trim($msg . "\n" . $line), 0, $limit, "UTF-8"), 0, -3, "UTF-8") . $ellipsis;
59 * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences
61 * @param string $text Text to search
62 * @param string $open Left boundary
63 * @param string $close Right boundary
64 * @param int $occurrences Number of first occurrences to skip
65 * @return boolean|array
67 public static function getBoundariesPosition($text, $open, $close, $occurrences = 0)
69 if ($occurrences < 0) {
74 for ($i = 0; $i <= $occurrences; $i++) {
75 if ($start_pos !== false) {
76 $start_pos = strpos($text, $open, $start_pos + 1);
80 if ($start_pos === false) {
84 $end_pos = strpos($text, $close, $start_pos);
86 if ($end_pos === false) {
90 $res = ['start' => $start_pos, 'end' => $end_pos];
96 * Convert a message into plaintext for connectors to other networks
98 * @param array $item The message array that is about to be posted
99 * @param int $limit The maximum number of characters when posting to that network
100 * @param bool $includedlinks Has an attached link to be included into the message?
101 * @param int $htmlmode This controls the behavior of the BBCode conversion
102 * @param string $target_network Name of the network where the post should go to.
104 * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
105 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
106 * @see \Friendica\Content\Text\BBCode::getAttachedData
109 public static function getPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '')
112 $URLSearchString = '^\[\]';
113 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
115 // Add an URL element if the text contains a raw link
116 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
117 '$1[url]$2[/url]', $body);
119 // Remove the abstract
120 $body = BBCode::stripAbstract($body);
122 // At first look at data that is attached via "type-..." stuff
123 // This will hopefully replaced with a dedicated bbcode later
124 //$post = self::getAttachedData($b['body']);
125 $post = BBCode::getAttachedData($body, $item);
127 if (($item['title'] != '') && ($post['text'] != '')) {
128 $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
129 } elseif ($item['title'] != '') {
130 $post['text'] = trim($item['title']);
135 // Fetch the abstract from the given target network
136 if ($target_network != '') {
137 $default_abstract = BBCode::getAbstract($item['body']);
138 $abstract = BBCode::getAbstract($item['body'], $target_network);
140 // If we post to a network with no limit we only fetch
141 // an abstract exactly for this network
142 if (($limit == 0) && ($abstract == $default_abstract)) {
145 } else {// Try to guess the correct target network
147 case BBCode::TWITTER:
148 $abstract = BBCode::getAbstract($item['body'], Protocol::TWITTER);
151 case BBCode::OSTATUS:
152 $abstract = BBCode::getAbstract($item['body'], Protocol::STATUSNET);
155 default: // We don't know the exact target.
156 // We fetch an abstract since there is a posting limit.
158 $abstract = BBCode::getAbstract($item['body']);
163 if ($abstract != '') {
164 $post['text'] = $abstract;
166 if ($post['type'] == 'text') {
167 $post['type'] = 'link';
168 $post['url'] = $item['plink'];
172 $html = BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
173 $msg = HTML::toPlaintext($html, 0, true);
174 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
177 if ($includedlinks) {
178 if ($post['type'] == 'link') {
179 $link = $post['url'];
180 } elseif ($post['type'] == 'text') {
181 $link = $post['url'] ?? '';
182 } elseif ($post['type'] == 'video') {
183 $link = $post['url'];
184 } elseif ($post['type'] == 'photo') {
185 $link = $post['image'];
188 if (($msg == '') && isset($post['title'])) {
189 $msg = trim($post['title']);
192 if (($msg == '') && isset($post['description'])) {
193 $msg = trim($post['description']);
196 // If the link is already contained in the post, then it neeedn't to be added again
197 // But: if the link is beyond the limit, then it has to be added.
198 if (($link != '') && strstr($msg, $link)) {
199 $pos = strpos($msg, $link);
201 // Will the text be shortened in the link?
202 // Or is the link the last item in the post?
203 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
204 $msg = trim(str_replace($link, '', $msg));
205 } elseif (($limit == 0) || ($pos < $limit)) {
206 // The limit has to be increased since it will be shortened - but not now
207 // Only do it with Twitter
208 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == BBCode::TWITTER)) {
209 $limit = $limit - 23 + strlen($link);
214 if ($post['type'] == 'text') {
222 // Reduce multiple spaces
223 // When posted to a network with limited space, we try to gain space where possible
224 while (strpos($msg, ' ') !== false) {
225 $msg = str_replace(' ', ' ', $msg);
228 // Twitter is using its own limiter, so we always assume that shortened links will have this length
229 if (iconv_strlen($link, 'UTF-8') > 0) {
230 $limit = $limit - 23;
233 if (iconv_strlen($msg, 'UTF-8') > $limit) {
234 if (($post['type'] == 'text') && isset($post['url'])) {
235 $post['url'] = $item['plink'];
236 } elseif (!isset($post['url'])) {
237 $limit = $limit - 23;
238 $post['url'] = $item['plink'];
239 } elseif (strpos($item['body'], '[share') !== false) {
240 $post['url'] = $item['plink'];
241 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
242 $post['url'] = $item['plink'];
244 $msg = self::shorten($msg, $limit);
248 $post['text'] = trim($msg);