"item" is replaced by "post-view" / postupdate check added
[friendica.git/.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\Database\DBA;
28 use Friendica\DI;
29
30 class ItemContent
31 {
32         /**
33          * Search posts for given content
34          *
35          * @param string $search
36          * @param integer $uid
37          * @param integer $start
38          * @param integer $limit
39          * @param integer $last_uriid
40          * @return array
41          */
42         public static function getURIIdListBySearch(string $search, int $uid = 0, int $start = 0, int $limit = 100, int $last_uriid = 0)
43         {
44                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
45                         AND (NOT `private` OR (`private` AND `uid` = ?))
46                         AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))",
47                         $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
48
49                 if (!empty($last_uriid)) {
50                         $condition = DBA::mergeConditions($condition, ["`uri-id` < ?", $last_uriid]);
51                 }
52
53                 $params = [
54                         'order' => ['uri-id' => true],
55                         'group_by' => ['uri-id'],
56                         'limit' => [$start, $limit]
57                 ];
58
59                 $tags = Post::select(['uri-id'], $condition, $params);
60
61                 $uriids = [];
62                 while ($tag = DBA::fetch($tags)) {
63                         $uriids[] = $tag['uri-id'];
64                 }
65                 DBA::close($tags);
66
67                 return $uriids;
68         }
69
70         public static function countBySearch(string $search, int $uid = 0)
71         {
72                 $condition = ["`uri-id` IN (SELECT `uri-id` FROM `item-content` WHERE MATCH (`title`, `content-warning`, `body`) AGAINST (? IN BOOLEAN MODE))
73                         AND (NOT `private` OR (`private` AND `uid` = ?))
74                         AND `uri-id` IN (SELECT `uri-id` FROM `post-view` WHERE `network` IN (?, ?, ?, ?))",
75                         $search, $uid, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS];
76                 return Post::count($condition);
77         }
78
79         /**
80          * Convert a message into plaintext for connectors to other networks
81          *
82          * @param array  $item           The message array that is about to be posted
83          * @param int    $limit          The maximum number of characters when posting to that network
84          * @param bool   $includedlinks  Has an attached link to be included into the message?
85          * @param int    $htmlmode       This controls the behavior of the BBCode conversion
86          * @param string $target_network Name of the network where the post should go to.
87          *
88          * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
89          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
90          * @see   \Friendica\Content\Text\BBCode::getAttachedData
91          *
92          */
93         public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = BBCode::API, $target_network = '')
94         {
95                 // Remove hashtags
96                 $URLSearchString = '^\[\]';
97                 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
98
99                 // Add an URL element if the text contains a raw link
100                 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
101                         '$1[url]$2[/url]', $body);
102
103                 // Remove the abstract
104                 $body = Text\BBCode::stripAbstract($body);
105
106                 // At first look at data that is attached via "type-..." stuff
107                 // This will hopefully replaced with a dedicated bbcode later
108                 //$post = self::getAttachedData($b['body']);
109                 $post = Text\BBCode::getAttachedData($body, $item);
110
111                 if (($item['title'] != '') && ($post['text'] != '')) {
112                         $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
113                 } elseif ($item['title'] != '') {
114                         $post['text'] = trim($item['title']);
115                 }
116
117                 $abstract = '';
118
119                 // Fetch the abstract from the given target network
120                 if ($target_network != '') {
121                         $default_abstract = Text\BBCode::getAbstract($item['body']);
122                         $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
123
124                         // If we post to a network with no limit we only fetch
125                         // an abstract exactly for this network
126                         if (($limit == 0) && ($abstract == $default_abstract)) {
127                                 $abstract = '';
128                         }
129                 } else {// Try to guess the correct target network
130                         switch ($htmlmode) {
131                                 case BBCode::TWITTER:
132                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
133                                         break;
134
135                                 case BBCode::OSTATUS:
136                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
137                                         break;
138
139                                 default: // We don't know the exact target.
140                                         // We fetch an abstract since there is a posting limit.
141                                         if ($limit > 0) {
142                                                 $abstract = Text\BBCode::getAbstract($item['body']);
143                                         }
144                         }
145                 }
146
147                 if ($abstract != '') {
148                         $post['text'] = $abstract;
149
150                         if ($post['type'] == 'text') {
151                                 $post['type'] = 'link';
152                                 $post['url'] = $item['plink'];
153                         }
154                 }
155
156                 $html = Text\BBCode::convert($post['text'] . ($post['after'] ?? ''), false, $htmlmode);
157                 $msg = Text\HTML::toPlaintext($html, 0, true);
158                 $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
159
160                 $link = '';
161                 if ($includedlinks) {
162                         if ($post['type'] == 'link') {
163                                 $link = $post['url'];
164                         } elseif ($post['type'] == 'text') {
165                                 $link = $post['url'] ?? '';
166                         } elseif ($post['type'] == 'video') {
167                                 $link = $post['url'];
168                         } elseif ($post['type'] == 'photo') {
169                                 $link = $post['image'];
170                         }
171
172                         if (($msg == '') && isset($post['title'])) {
173                                 $msg = trim($post['title']);
174                         }
175
176                         if (($msg == '') && isset($post['description'])) {
177                                 $msg = trim($post['description']);
178                         }
179
180                         // If the link is already contained in the post, then it neeedn't to be added again
181                         // But: if the link is beyond the limit, then it has to be added.
182                         if (($link != '') && strstr($msg, $link)) {
183                                 $pos = strpos($msg, $link);
184
185                                 // Will the text be shortened in the link?
186                                 // Or is the link the last item in the post?
187                                 if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
188                                         $msg = trim(str_replace($link, '', $msg));
189                                 } elseif (($limit == 0) || ($pos < $limit)) {
190                                         // The limit has to be increased since it will be shortened - but not now
191                                         // Only do it with Twitter
192                                         if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == BBCode::TWITTER)) {
193                                                 $limit = $limit - 23 + strlen($link);
194                                         }
195
196                                         $link = '';
197
198                                         if ($post['type'] == 'text') {
199                                                 unset($post['url']);
200                                         }
201                                 }
202                         }
203                 }
204
205                 if ($limit > 0) {
206                         // Reduce multiple spaces
207                         // When posted to a network with limited space, we try to gain space where possible
208                         while (strpos($msg, '  ') !== false) {
209                                 $msg = str_replace('  ', ' ', $msg);
210                         }
211
212                         // Twitter is using its own limiter, so we always assume that shortened links will have this length
213                         if (iconv_strlen($link, 'UTF-8') > 0) {
214                                 $limit = $limit - 23;
215                         }
216
217                         if (iconv_strlen($msg, 'UTF-8') > $limit) {
218                                 if (($post['type'] == 'text') && isset($post['url'])) {
219                                         $post['url'] = $item['plink'];
220                                 } elseif (!isset($post['url'])) {
221                                         $limit = $limit - 23;
222                                         $post['url'] = $item['plink'];
223                                 } elseif (strpos($item['body'], '[share') !== false) {
224                                         $post['url'] = $item['plink'];
225                                 } elseif (DI::pConfig()->get($item['uid'], 'system', 'no_intelligent_shortening')) {
226                                         $post['url'] = $item['plink'];
227                                 }
228                                 $msg = Text\Plaintext::shorten($msg, $limit);
229                         }
230                 }
231
232                 $post['text'] = trim($msg);
233
234                 return $post;
235         }
236 }