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