]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
shortening message: Solved issue with non latin characters
[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
50                 $URLSearchString = "^\[\]";
51                 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures,  PREG_SET_ORDER)) {
52                         if (count($pictures) == 1) {
53                                 // Checking, if the link goes to a picture
54                                 $data = parseurl_getsiteinfo($pictures[0][1], true);
55
56                                 if ($data["type"] == "photo") {
57                                         $post["type"] = "photo";
58                                         if (isset($data["images"][0]))
59                                                 $post["image"] = $data["images"][0]["src"];
60                                         else
61                                                 $post["image"] = $data["url"];
62
63                                         $post["preview"] = $pictures[0][2];
64                                         $post["text"] = str_replace($pictures[0][0], "", $body);
65                                 } else {
66                                         $img_str = fetch_url($pictures[0][1]);
67
68                                         $tempfile = tempnam(get_config("system","temppath"), "cache");
69                                         file_put_contents($tempfile, $img_str);
70                                         $mime = image_type_to_mime_type(exif_imagetype($tempfile));
71                                         unlink($tempfile);
72                                         if (substr($mime, 0, 6) == "image/") {
73                                                 $post["type"] = "photo";
74                                                 $post["image"] = $pictures[0][1];
75                                                 $post["preview"] = $pictures[0][2];
76                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
77                                         }
78                                 }
79                         } elseif (count($pictures) > 1) {
80                                 $post["type"] = "link";
81                                 $post["url"] = $b["plink"];
82                                 $post["image"] = $pictures[0][2];
83                                 $post["text"] = $body;
84                         }
85                 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures,  PREG_SET_ORDER)) {
86                         if (count($pictures) == 1) {
87                                 $post["type"] = "photo";
88                                 $post["image"] = $pictures[0][1];
89                                 $post["text"] = str_replace($pictures[0][0], "", $body);
90                         } elseif (count($pictures) > 1) {
91                                 $post["type"] = "link";
92                                 $post["url"] = $b["plink"];
93                                 $post["image"] = $pictures[0][1];
94                                 $post["text"] = $body;
95                         }
96                 }
97                 if (!isset($post["type"])) {
98                         $post["type"] = "text";
99                         $post["text"] = trim($body);
100                 }
101         }
102
103         return($post);
104 }
105
106 function shortenmsg($msg, $limit, $twitter = false) {
107         // To-Do:
108         // For Twitter URLs aren't shortened, but they have to be calculated as if.
109
110         $lines = explode("\n", $msg);
111         $msg = "";
112         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
113         foreach ($lines AS $row=>$line) {
114                 if (iconv_strlen(trim($msg."\n".$line), "UTF-8") <= $limit)
115                         $msg = trim($msg."\n".$line);
116                 // Is the new message empty by now or is it a reshared message?
117                 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
118                         $msg = substr(substr(trim($msg."\n".$line), 0, $limit), 0, -3)."...";
119                 else
120                         break;
121         }
122         return($msg);
123 }
124
125 function plaintext($a, $b, $limit = 0, $includedlinks = false, $htmlmode = 2) {
126         require_once("include/bbcode.php");
127         require_once("include/html2plain.php");
128         require_once("include/network.php");
129
130         // At first look at data that is attached via "type-..." stuff
131         // This will hopefully replaced with a dedicated bbcode later
132         $post = get_attached_data($b["body"]);
133
134         if (($b["title"] != "") AND ($post["text"] != ""))
135                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
136         elseif ($b["title"] != "")
137                 $post["text"] = trim($b["title"]);
138
139         $html = bbcode($post["text"], false, false, $htmlmode);
140         $msg = html2plain($html, 0, true);
141         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
142
143         $link = "";
144         if ($includedlinks) {
145                 if ($post["type"] == "link")
146                         $link = $post["url"];
147                 elseif ($post["type"] == "video")
148                         $link = $post["url"];
149                 elseif ($post["type"] == "photo")
150                         $link = $post["image"];
151
152                 if (($msg == "") AND isset($post["title"]))
153                         $msg = trim($post["title"]);
154
155                 if (($msg == "") AND isset($post["description"]))
156                         $msg = trim($post["description"]);
157
158                 // If the link is already contained in the post, then it neeedn't to be added again
159                 // But: if the link is beyond the limit, then it has to be added.
160                 if (($link != "") AND strstr($msg, $link)) {
161                         $pos = strpos($msg, $link);
162                         if (($limit == 0) OR ($pos < $limit))
163                                 $link = "";
164                 }
165         }
166
167         if ($limit > 0) {
168                 // Reduce multiple spaces
169                 // When posted to a network with limited space, we try to gain space where possible
170                 while (strpos($msg, "  ") !== false)
171                         $msg = str_replace("  ", " ", $msg);
172
173                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
174                 if (strlen($link) > 0)
175                         $limit = $limit - 23;
176
177                 if (strlen($msg) > $limit) {
178
179                         if (!isset($post["url"])) {
180                                 $limit = $limit - 23;
181                                 $post["url"] = $b["plink"];
182                         }
183
184                         $msg = shortenmsg($msg, $limit);
185                 }
186         }
187
188         $post["text"] = trim($msg);
189
190         return($post);
191 }
192 ?>