]> git.mxchange.org Git - friendica.git/blob - src/Model/ItemContent.php
Fix mods/README.md format
[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 require_once 'boot.php';
15 require_once 'include/items.php';
16 require_once 'include/text.php';
17
18 class ItemContent extends BaseObject
19 {
20         /**
21          * @brief Convert a message into plaintext for connectors to other networks
22          *
23          * @param array  $item           The message array that is about to be posted
24          * @param int    $limit          The maximum number of characters when posting to that network
25          * @param bool   $includedlinks  Has an attached link to be included into the message?
26          * @param int    $htmlmode       This controls the behavior of the BBCode conversion
27          * @param string $target_network Name of the network where the post should go to.
28          *
29          * @see \Friendica\Content\Text\BBCode::getAttachedData
30          *
31          * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
32          */
33         public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = '')
34         {
35                 // Remove hashtags
36                 $URLSearchString = '^\[\]';
37                 $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
38
39                 // Add an URL element if the text contains a raw link
40                 $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
41                         '$1[url]$2[/url]', $body);
42
43                 // Remove the abstract
44                 $body = Text\BBCode::stripAbstract($body);
45
46                 // At first look at data that is attached via "type-..." stuff
47                 // This will hopefully replaced with a dedicated bbcode later
48                 //$post = self::getAttachedData($b['body']);
49                 $post = Text\BBCode::getAttachedData($body, $item);
50
51                 if (($item['title'] != '') && ($post['text'] != '')) {
52                         $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
53                 } elseif ($item['title'] != '') {
54                         $post['text'] = trim($item['title']);
55                 }
56
57                 $abstract = '';
58
59                 // Fetch the abstract from the given target network
60                 if ($target_network != '') {
61                         $default_abstract = Text\BBCode::getAbstract($item['body']);
62                         $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
63
64                         // If we post to a network with no limit we only fetch
65                         // an abstract exactly for this network
66                         if (($limit == 0) && ($abstract == $default_abstract)) {
67                                 $abstract = '';
68                         }
69                 } else {// Try to guess the correct target network
70                         switch ($htmlmode) {
71                                 case 8:
72                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
73                                         break;
74
75                                 case 7:
76                                         $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
77                                         break;
78
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 }