]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git] / include / plaintext.php
1 <?php
2 function get_attached_data($body) {
3 /*
4  - text:
5  - type: link, video, photo
6  - title:
7  - url:
8  - image:
9  - description:
10  - (thumbnail)
11 */
12
13         // Simplify image codes
14         $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
15
16         $post = array();
17
18         if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached,  PREG_SET_ORDER)) {
19                 foreach ($attached AS $data) {
20                         if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
21                                 continue;
22
23                         $post["type"] = substr($data[1], 5);
24
25                         $post["text"] = trim(str_replace($data[0], "", $body));
26
27                         $attacheddata = $data[2];
28
29                         $URLSearchString = "^\[\]";
30
31                         if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches))
32                                 $post["image"] = $matches[1];
33
34                         if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
35                                 $post["url"] = $matches[1];
36                                 $post["title"] = $matches[2];
37                         }
38
39                         // Search for description
40                         if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
41                                 $post["description"] = $matches[1];
42
43                 }
44         }
45
46         // if nothing is found, it maybe having an image.
47         if (!isset($post["type"])) {
48                 require_once("mod/parse_url.php");
49                 require_once("include/Photo.php");
50
51                 $URLSearchString = "^\[\]";
52                 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures,  PREG_SET_ORDER)) {
53                         if (count($pictures) == 1) {
54                                 // Checking, if the link goes to a picture
55                                 $data = parseurl_getsiteinfo_cached($pictures[0][1], true);
56                                 if ($data["type"] == "photo") {
57                                         $post["type"] = "photo";
58                                         if (isset($data["images"][0])) {
59                                                 $post["image"] = $data["images"][0]["src"];
60                                                 $post["url"] = $data["url"];
61                                         } else
62                                                 $post["image"] = $data["url"];
63
64                                         $post["preview"] = $pictures[0][2];
65                                         $post["text"] = str_replace($pictures[0][0], "", $body);
66                                 } else {
67                                         $imgdata = get_photo_info($pictures[0][1]);
68                                         if (substr($imgdata["mime"], 0, 6) == "image/") {
69                                                 $post["type"] = "photo";
70                                                 $post["image"] = $pictures[0][1];
71                                                 $post["preview"] = $pictures[0][2];
72                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
73                                         }
74                                 }
75                         } elseif (count($pictures) > 1) {
76                                 $post["type"] = "link";
77                                 $post["url"] = $b["plink"];
78                                 $post["image"] = $pictures[0][2];
79                                 $post["text"] = $body;
80                         }
81                 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures,  PREG_SET_ORDER)) {
82                         if (count($pictures) == 1) {
83                                 $post["type"] = "photo";
84                                 $post["image"] = $pictures[0][1];
85                                 $post["text"] = str_replace($pictures[0][0], "", $body);
86                         } elseif (count($pictures) > 1) {
87                                 $post["type"] = "link";
88                                 $post["url"] = $b["plink"];
89                                 $post["image"] = $pictures[0][1];
90                                 $post["text"] = $body;
91                         }
92                 }
93
94                 if (preg_match_all("(\[url\]([$URLSearchString]*)\[\/url\])ism", $body, $links,  PREG_SET_ORDER)) {
95                         if (count($links) == 1) {
96                                 $post["type"] = "text";
97                                 $post["url"] = $links[0][1];
98                                 $post["text"] = $body;
99                         }
100                 }
101                 if (!isset($post["type"])) {
102                         $post["type"] = "text";
103                         $post["text"] = trim($body);
104                 }
105         } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
106                 require_once("mod/parse_url.php");
107                 $data = parseurl_getsiteinfo_cached($post["url"], true);
108
109                 if (isset($data["images"][0]))
110                         $post["image"] = $data["images"][0]["src"];
111         }
112
113         return($post);
114 }
115
116 function shortenmsg($msg, $limit, $twitter = false) {
117         /// @TODO
118         /// For Twitter URLs aren't shortened, but they have to be calculated as if.
119
120         $lines = explode("\n", $msg);
121         $msg = "";
122         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
123         foreach ($lines AS $row=>$line) {
124                 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
125                         $msg = trim($msg."\n".$line);
126                 // Is the new message empty by now or is it a reshared message?
127                 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
128                         $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8")."...";
129                 else
130                         break;
131         }
132         return($msg);
133 }
134
135 /**
136  * @brief Convert a message into plaintext for connectors to other networks
137  *
138  * @param App $a The application class
139  * @param array $b The message array that is about to be posted
140  * @param int $limit The maximum number of characters when posting to that network
141  * @param bool $includedlinks Has an attached link to be included into the message?
142  * @param int $htmlmode This triggers the behaviour of the bbcode conversion
143  * @param string $target_network Name of the network where the post should go to.
144  *
145  * @return string The converted message
146  */
147 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = "") {
148         require_once("include/bbcode.php");
149         require_once("include/html2plain.php");
150         require_once("include/network.php");
151
152         // Remove the hash tags
153         $URLSearchString = "^\[\]";
154         $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $b["body"]);
155
156         // Add an URL element if the text contains a raw link
157         $body = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url]$2[/url]', $body);
158
159         // Remove the abstract
160         $body = remove_abstract($body);
161
162         // At first look at data that is attached via "type-..." stuff
163         // This will hopefully replaced with a dedicated bbcode later
164         //$post = get_attached_data($b["body"]);
165         $post = get_attached_data($body);
166
167         if (($b["title"] != "") AND ($post["text"] != ""))
168                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
169         elseif ($b["title"] != "")
170                 $post["text"] = trim($b["title"]);
171
172         $abstract = "";
173
174         // Fetch the abstract from the given target network
175         if ($target_network != "") {
176                 $default_abstract = fetch_abstract($b["body"]);
177                 $abstract = fetch_abstract($b["body"], $target_network);
178
179                 // If we post to a network with no limit we only fetch
180                 // an abstract exactly for this network
181                 if (($limit == 0) AND ($abstract == $default_abstract))
182                         $abstract = "";
183
184         } else // Try to guess the correct target network
185                 switch ($htmlmode) {
186                         case 8:
187                                 $abstract = fetch_abstract($b["body"], NETWORK_TWITTER);
188                                 break;
189                         case 7:
190                                 $abstract = fetch_abstract($b["body"], NETWORK_STATUSNET);
191                                 break;
192                         case 6:
193                                 $abstract = fetch_abstract($b["body"], NETWORK_APPNET);
194                                 break;
195                         default: // We don't know the exact target.
196                                  // We fetch an abstract since there is a posting limit.
197                                 if ($limit > 0)
198                                         $abstract = fetch_abstract($b["body"]);
199                 }
200
201         if ($abstract != "") {
202                 $post["text"] = $abstract;
203
204                 if ($post["type"] == "text") {
205                         $post["type"] = "link";
206                         $post["url"] = $b["plink"];
207                 }
208         }
209
210         $html = bbcode($post["text"], false, false, $htmlmode);
211         $msg = html2plain($html, 0, true);
212         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
213
214         $link = "";
215         if ($includedlinks) {
216                 if ($post["type"] == "link")
217                         $link = $post["url"];
218                 elseif ($post["type"] == "text")
219                         $link = $post["url"];
220                 elseif ($post["type"] == "video")
221                         $link = $post["url"];
222                 elseif ($post["type"] == "photo")
223                         $link = $post["image"];
224
225                 if (($msg == "") AND isset($post["title"]))
226                         $msg = trim($post["title"]);
227
228                 if (($msg == "") AND isset($post["description"]))
229                         $msg = trim($post["description"]);
230
231                 // If the link is already contained in the post, then it neeedn't to be added again
232                 // But: if the link is beyond the limit, then it has to be added.
233                 if (($link != "") AND strstr($msg, $link)) {
234                         $pos = strpos($msg, $link);
235
236                         // Will the text be shortened in the link?
237                         // Or is the link the last item in the post?
238                         if (($limit > 0) AND ($pos < $limit) AND (($pos + 23 > $limit) OR ($pos + strlen($link) == strlen($msg))))
239                                 $msg = trim(str_replace($link, "", $msg));
240                         elseif (($limit == 0) OR ($pos < $limit)) {
241                                 // The limit has to be increased since it will be shortened - but not now
242                                 // Only do it with Twitter (htmlmode = 8)
243                                 if (($limit > 0) AND (strlen($link) > 23) AND ($htmlmode == 8))
244                                         $limit = $limit - 23 + strlen($link);
245
246                                 $link = "";
247
248                                 if ($post["type"] == "text")
249                                         unset($post["url"]);
250                         }
251                 }
252         }
253
254         if ($limit > 0) {
255                 // Reduce multiple spaces
256                 // When posted to a network with limited space, we try to gain space where possible
257                 while (strpos($msg, "  ") !== false)
258                         $msg = str_replace("  ", " ", $msg);
259
260                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
261                 if (iconv_strlen($link, "UTF-8") > 0)
262                         $limit = $limit - 23;
263
264                 if (iconv_strlen($msg, "UTF-8") > $limit) {
265
266                         if (($post["type"] == "text") AND isset($post["url"]))
267                                 $post["url"] = $b["plink"];
268                         elseif (!isset($post["url"])) {
269                                 $limit = $limit - 23;
270                                 $post["url"] = $b["plink"];
271                         } elseif (strpos($b["body"], "[share") !== false)
272                                 $post["url"] = $b["plink"];
273                         elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
274                                 $post["url"] = $b["plink"];
275
276                         $msg = shortenmsg($msg, $limit);
277                 }
278         }
279
280         $post["text"] = trim($msg);
281
282         return($post);
283 }
284 ?>