]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
Bugfix for pull request #2147 (Fix for issue #2122)
[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                 if (!isset($post["type"])) {
94                         $post["type"] = "text";
95                         $post["text"] = trim($body);
96                 }
97         } elseif (isset($post["url"]) AND ($post["type"] == "video")) {
98                 require_once("mod/parse_url.php");
99                 $data = parseurl_getsiteinfo_cached($post["url"], true);
100
101                 if (isset($data["images"][0]))
102                         $post["image"] = $data["images"][0]["src"];
103         }
104
105         return($post);
106 }
107
108 function shortenmsg($msg, $limit, $twitter = false) {
109         // To-Do:
110         // For Twitter URLs aren't shortened, but they have to be calculated as if.
111
112         $lines = explode("\n", $msg);
113         $msg = "";
114         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
115         foreach ($lines AS $row=>$line) {
116                 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
117                         $msg = trim($msg."\n".$line);
118                 // Is the new message empty by now or is it a reshared message?
119                 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
120                         $msg = iconv_substr(iconv_substr(trim($msg."\n".$line), 0, $limit, "UTF-8"), 0, -3, "UTF-8")."...";
121                 else
122                         break;
123         }
124         return($msg);
125 }
126
127 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2) {
128         require_once("include/bbcode.php");
129         require_once("include/html2plain.php");
130         require_once("include/network.php");
131
132         // At first look at data that is attached via "type-..." stuff
133         // This will hopefully replaced with a dedicated bbcode later
134         $post = get_attached_data($b["body"]);
135
136         if (($b["title"] != "") AND ($post["text"] != ""))
137                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
138         elseif ($b["title"] != "")
139                 $post["text"] = trim($b["title"]);
140
141         $html = bbcode($post["text"], false, false, $htmlmode);
142         $msg = html2plain($html, 0, true);
143         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
144
145         $link = "";
146         if ($includedlinks) {
147                 if ($post["type"] == "link")
148                         $link = $post["url"];
149                 elseif ($post["type"] == "video")
150                         $link = $post["url"];
151                 elseif ($post["type"] == "photo")
152                         $link = $post["image"];
153
154                 if (($msg == "") AND isset($post["title"]))
155                         $msg = trim($post["title"]);
156
157                 if (($msg == "") AND isset($post["description"]))
158                         $msg = trim($post["description"]);
159
160                 // If the link is already contained in the post, then it neeedn't to be added again
161                 // But: if the link is beyond the limit, then it has to be added.
162                 if (($link != "") AND strstr($msg, $link)) {
163                         $pos = strpos($msg, $link);
164                         if (($limit == 0) OR ($pos < $limit))
165                                 $link = "";
166                 }
167         }
168
169         if ($limit > 0) {
170                 // Reduce multiple spaces
171                 // When posted to a network with limited space, we try to gain space where possible
172                 while (strpos($msg, "  ") !== false)
173                         $msg = str_replace("  ", " ", $msg);
174
175                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
176                 if (iconv_strlen($link, "UTF-8") > 0)
177                         $limit = $limit - 23;
178
179                 if (iconv_strlen($msg, "UTF-8") > $limit) {
180
181                         if (!isset($post["url"])) {
182                                 $limit = $limit - 23;
183                                 $post["url"] = $b["plink"];
184                         } elseif (strpos($b["body"], "[share") !== false)
185                                 $post["url"] = $b["plink"];
186                         elseif (get_pconfig($b["uid"], "system", "no_intelligent_shortening"))
187                                 $post["url"] = $b["plink"];
188
189                         $msg = shortenmsg($msg, $limit);
190                 }
191         }
192
193         $post["text"] = trim($msg);
194
195         return($post);
196 }
197 ?>