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