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