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