]> git.mxchange.org Git - friendica.git/blob - src/Model/ItemContent.php
Merge pull request #8939 from MrPetovan/task/8906-frio-viewas-redesign
[friendica.git] / src / Model / ItemContent.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Content\Text;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Protocol;
27 use Friendica\DI;
28
29 class ItemContent
30 {
31         /**
32          * Convert a message into plaintext for connectors to other networks
33          *
34          * @param array  $item           The message array that is about to be posted
35          * @param int    $limit          The maximum number of characters when posting to that network
36          * @param bool   $includedlinks  Has an attached link to be included into the message?
37          * @param int    $htmlmode       This controls the behavior of the BBCode conversion
38          * @param string $target_network Name of the network where the post should go to.
39          *
40          * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
41          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
42          * @see   \Friendica\Content\Text\BBCode::getAttachedData
43          *
44          */
45         public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '')
46         {
47                 // Remove hashtags
48                 $URLSearchString = '^\[\]';
49                 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
50
51                 // Add an URL element if the text contains a raw link
52                 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
53                         '$1[url]$2[/url]', $body);
54
55                 // Remove the abstract
56                 $body = Text\BBCode::stripAbstract($body);
57
58                 // At first look at data that is attached via "type-..." stuff
59                 // This will hopefully replaced with a dedicated bbcode later
60                 //$post = self::getAttachedData($b['body']);
61                 $post = Text\BBCode::getAttachedData($body, $item);
62
63                 if (($item['title'] != '') && ($post['text'] != '')) {
64                         $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
65                 } elseif ($item['title'] != '') {
66                         $post['text'] = trim($item['title']);
67                 }
68
69                 $abstract = '';
70
71                 // Fetch the abstract from the given target network
72                 if ($target_network != '') {
73                         $default_abstract = Text\BBCode::getAbstract($item['body']);
74                         $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
75
76                         // If we post to a network with no limit we only fetch
77                         // an abstract exactly for this network
78                         if (($limit == 0) && ($abstract == $default_abstract)) {
79                                 $abstract = '';
80                         }
81                 } else {// Try to guess the correct target network
82                         switch ($htmlmode) {
83                                 case BBCode::TWITTER:
84                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
85                                         break;
86
87                                 case BBCode::OSTATUS:
88                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
89                                         break;
90
91                                 default: // We don't know the exact target.
92                                         // We fetch an abstract since there is a posting limit.
93                                         if ($limit > 0) {
94                                                 $abstract = Text\BBCode::getAbstract($item['body']);
95                                         }
96                         }
97                 }
98
99                 if ($abstract != '') {
100                         $post['text'] = $abstract;
101
102                         if ($post['type'] == 'text') {
103                                 $post['type'] = 'link';
104                                 $post['url'] = $item['plink'];
105                         }
106                 }
107
108                 $html = Text\BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
109                 $msg = Text\HTML::toPlaintext($html, 0, true);
110                 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
111
112                 $link = '';
113                 if ($includedlinks) {
114                         if ($post['type'] == 'link') {
115                                 $link = $post['url'];
116                         } elseif ($post['type'] == 'text') {
117                                 $link = $post['url'] ?? '';
118                         } elseif ($post['type'] == 'video') {
119                                 $link = $post['url'];
120                         } elseif ($post['type'] == 'photo') {
121                                 $link = $post['image'];
122                         }
123
124                         if (($msg == '') && isset($post['title'])) {
125                                 $msg = trim($post['title']);
126                         }
127
128                         if (($msg == '') && isset($post['description'])) {
129                                 $msg = trim($post['description']);
130                         }
131
132                         // If the link is already contained in the post, then it neeedn't to be added again
133                         // But: if the link is beyond the limit, then it has to be added.
134                         if (($link != '') && strstr($msg, $link)) {
135                                 $pos = strpos($msg, $link);
136
137                                 // Will the text be shortened in the link?
138                                 // Or is the link the last item in the post?
139                                 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
140                                         $msg = trim(str_replace($link, '', $msg));
141                                 } elseif (($limit == 0) || ($pos < $limit)) {
142                                         // The limit has to be increased since it will be shortened - but not now
143                                         // Only do it with Twitter
144                                         if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == BBCode::TWITTER)) {
145                                                 $limit = $limit - 23 + strlen($link);
146                                         }
147
148                                         $link = '';
149
150                                         if ($post['type'] == 'text') {
151                                                 unset($post['url']);
152                                         }
153                                 }
154                         }
155                 }
156
157                 if ($limit > 0) {
158                         // Reduce multiple spaces
159                         // When posted to a network with limited space, we try to gain space where possible
160                         while (strpos($msg, '  ') !== false) {
161                                 $msg = str_replace('  ', ' ', $msg);
162                         }
163
164                         // Twitter is using its own limiter, so we always assume that shortened links will have this length
165                         if (iconv_strlen($link, 'UTF-8') > 0) {
166                                 $limit = $limit - 23;
167                         }
168
169                         if (iconv_strlen($msg, 'UTF-8') > $limit) {
170                                 if (($post['type'] == 'text') && isset($post['url'])) {
171                                         $post['url'] = $item['plink'];
172                                 } elseif (!isset($post['url'])) {
173                                         $limit = $limit - 23;
174                                         $post['url'] = $item['plink'];
175                                 } elseif (strpos($item['body'], '[share') !== false) {
176                                         $post['url'] = $item['plink'];
177                                 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
178                                         $post['url'] = $item['plink'];
179                                 }
180                                 $msg = Text\Plaintext::shorten($msg, $limit);
181                         }
182                 }
183
184                 $post['text'] = trim($msg);
185
186                 return $post;
187         }
188 }