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