Facebook and App.net is removed from nearly all places (#5581)
[friendica.git/.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                                 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 }