4 * @file src/Model/ItemContent.php
7 namespace Friendica\Model;
9 use Friendica\BaseObject;
10 use Friendica\Content\Text;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\Protocol;
14 class ItemContent extends BaseObject
17 * @brief Convert a message into plaintext for connectors to other networks
19 * @param array $item The message array that is about to be posted
20 * @param int $limit The maximum number of characters when posting to that network
21 * @param bool $includedlinks Has an attached link to be included into the message?
22 * @param int $htmlmode This controls the behavior of the BBCode conversion
23 * @param string $target_network Name of the network where the post should go to.
25 * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
26 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
27 * @see \Friendica\Content\Text\BBCode::getAttachedData
30 public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = '')
33 $URLSearchString = '^\[\]';
34 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
36 // Add an URL element if the text contains a raw link
37 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
38 '$1[url]$2[/url]', $body);
40 // Remove the abstract
41 $body = Text\BBCode::stripAbstract($body);
43 // At first look at data that is attached via "type-..." stuff
44 // This will hopefully replaced with a dedicated bbcode later
45 //$post = self::getAttachedData($b['body']);
46 $post = Text\BBCode::getAttachedData($body, $item);
48 if (($item['title'] != '') && ($post['text'] != '')) {
49 $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
50 } elseif ($item['title'] != '') {
51 $post['text'] = trim($item['title']);
56 // Fetch the abstract from the given target network
57 if ($target_network != '') {
58 $default_abstract = Text\BBCode::getAbstract($item['body']);
59 $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
61 // If we post to a network with no limit we only fetch
62 // an abstract exactly for this network
63 if (($limit == 0) && ($abstract == $default_abstract)) {
66 } else {// Try to guess the correct target network
69 $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
73 $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
76 default: // We don't know the exact target.
77 // We fetch an abstract since there is a posting limit.
79 $abstract = Text\BBCode::getAbstract($item['body']);
84 if ($abstract != '') {
85 $post['text'] = $abstract;
87 if ($post['type'] == 'text') {
88 $post['type'] = 'link';
89 $post['url'] = $item['plink'];
93 $html = Text\BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
94 $msg = Text\HTML::toPlaintext($html, 0, true);
95 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
99 if ($post['type'] == 'link') {
100 $link = $post['url'];
101 } elseif ($post['type'] == 'text') {
102 $link = $post['url'] ?? '';
103 } elseif ($post['type'] == 'video') {
104 $link = $post['url'];
105 } elseif ($post['type'] == 'photo') {
106 $link = $post['image'];
109 if (($msg == '') && isset($post['title'])) {
110 $msg = trim($post['title']);
113 if (($msg == '') && isset($post['description'])) {
114 $msg = trim($post['description']);
117 // If the link is already contained in the post, then it neeedn't to be added again
118 // But: if the link is beyond the limit, then it has to be added.
119 if (($link != '') && strstr($msg, $link)) {
120 $pos = strpos($msg, $link);
122 // Will the text be shortened in the link?
123 // Or is the link the last item in the post?
124 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
125 $msg = trim(str_replace($link, '', $msg));
126 } elseif (($limit == 0) || ($pos < $limit)) {
127 // The limit has to be increased since it will be shortened - but not now
128 // Only do it with Twitter (htmlmode = 8)
129 if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
130 $limit = $limit - 23 + strlen($link);
135 if ($post['type'] == 'text') {
143 // Reduce multiple spaces
144 // When posted to a network with limited space, we try to gain space where possible
145 while (strpos($msg, ' ') !== false) {
146 $msg = str_replace(' ', ' ', $msg);
149 // Twitter is using its own limiter, so we always assume that shortened links will have this length
150 if (iconv_strlen($link, 'UTF-8') > 0) {
151 $limit = $limit - 23;
154 if (iconv_strlen($msg, 'UTF-8') > $limit) {
155 if (($post['type'] == 'text') && isset($post['url'])) {
156 $post['url'] = $item['plink'];
157 } elseif (!isset($post['url'])) {
158 $limit = $limit - 23;
159 $post['url'] = $item['plink'];
160 } elseif (strpos($item['body'], '[share') !== false) {
161 $post['url'] = $item['plink'];
162 } elseif (PConfig::get($item['uid'], 'system', 'no_intelligent_shortening')) {
163 $post['url'] = $item['plink'];
165 $msg = Text\Plaintext::shorten($msg, $limit);
169 $post['text'] = trim($msg);