]> git.mxchange.org Git - friendica.git/blob - include/plaintext.php
Improvement for shortening of the new plaintext option.
[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         $post = array();
13
14         if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached,  PREG_SET_ORDER)) {
15                 foreach ($attached AS $data) {
16                         if (!in_array($data[1], array("type-link", "type-video", "type-photo")))
17                                 continue;
18
19                         $post["type"] = substr($data[1], 5);
20
21                         $post["text"] = trim(str_replace($data[0], "", $body));
22
23                         $attacheddata = $data[2];
24
25                         $URLSearchString = "^\[\]";
26
27                         if (preg_match("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $attacheddata, $matches))
28                                 $post["image"] = $matches[1];
29
30                         if (preg_match("/\[bookmark\=([$URLSearchString]*)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
31                                 $post["url"] = $matches[1];
32                                 $post["title"] = $matches[2];
33                         }
34
35                         // Search for description
36                         if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches))
37                                 $post["description"] = $matches[1];
38
39                 }
40         }
41         return($post);
42 }
43
44 function plaintext($a, $b, $limit = 0, $includedlinks = false) {
45         require_once("include/bbcode.php");
46         require_once("include/html2plain.php");
47         require_once("mod/parse_url.php");
48         require_once("include/network.php");
49
50         // Simplify image codes
51         $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $b["body"]);
52
53         // At first look at data that is attached via "type-..." stuff
54         // This will hopefully replaced with a dedicated bbcode later
55         $post = get_attached_data($body);
56
57         // if nothing is found, it maybe having an image.
58         if (!isset($post["type"])) {
59                 $URLSearchString = "^\[\]";
60                 if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures,  PREG_SET_ORDER)) {
61                         if (count($pictures) == 1) {
62                                 // Checking, if the link goes to a picture
63                                 $data = parseurl_getsiteinfo($pictures[0][1], true);
64
65                                 if ($data["type"] == "photo") {
66                                         $post["type"] = "photo";
67                                         if (isset($data["images"][0]))
68                                                 $post["image"] = $data["images"][0]["src"];
69                                         else
70                                                 $post["image"] = $data["url"];
71
72                                         $post["preview"] = $pictures[0][2];
73                                         $post["text"] = str_replace($pictures[0][0], "", $body);
74                                 } else {
75                                         $img_str = fetch_url($pictures[0][1]);
76
77                                         $tempfile = tempnam(get_config("system","temppath"), "cache");
78                                         file_put_contents($tempfile, $img_str);
79                                         $mime = image_type_to_mime_type(exif_imagetype($tempfile));
80                                         unlink($tempfile);
81                                         if (substr($mime, 0, 6) == "image/") {
82                                                 $post["type"] = "photo";
83                                                 $post["image"] = $pictures[0][1];
84                                                 $post["preview"] = $pictures[0][2];
85                                                 $post["text"] = str_replace($pictures[0][0], "", $body);
86                                         }
87                                 }
88                         } elseif (count($pictures) > 1) {
89                                 $post["type"] = "link";
90                                 $post["url"] = $b["plink"];
91                                 $post["image"] = $pictures[0][2];
92                                 $post["text"] = $body;
93                         }
94                 } elseif (preg_match_all("(\[img\]([$URLSearchString]*)\[\/img\])ism", $body, $pictures,  PREG_SET_ORDER)) {
95                         if (count($pictures) == 1) {
96                                 $post["type"] = "photo";
97                                 $post["image"] = $pictures[0][1];
98                                 $post["text"] = str_replace($pictures[0][0], "", $body);
99                         } elseif (count($pictures) > 1) {
100                                 $post["type"] = "link";
101                                 $post["url"] = $b["plink"];
102                                 $post["image"] = $pictures[0][1];
103                                 $post["text"] = $body;
104                         }
105                 }
106                 if (!isset($post["type"])) {
107                         $post["type"] = "text";
108                         $post["text"] = trim($body);
109                 }
110         }
111
112         if (($b["title"] != "") AND ($post["text"] != ""))
113                 $post["text"] = trim($b["title"]."\n\n".$post["text"]);
114         elseif ($b["title"] != "")
115                 $post["text"] = trim($b["title"]);
116
117         $html = bbcode($post["text"], false, false, 2);
118         $msg = html2plain($html, 0, true);
119         $msg = trim(html_entity_decode($msg,ENT_QUOTES,'UTF-8'));
120
121         $link = "";
122         if ($includedlinks) {
123                 if ($post["type"] == "link")
124                         $link = $post["url"];
125                 elseif ($post["type"] == "video")
126                         $link = $post["url"];
127                 elseif ($post["type"] == "photo")
128                         $link = $post["image"];
129
130                 if (($msg == "") AND isset($post["title"]))
131                         $msg = trim($post["title"]);
132
133                 if (($msg == "") AND isset($post["description"]))
134                         $msg = trim($post["description"]);
135
136                 // If the link is already contained in the post, then it neeedn't to be added again
137                 // But: if the link is beyond the limit, then it has to be added.
138                 if (($link != "") AND strstr($msg, $link)) {
139                         $pos = strpos($msg, $link);
140                         if (($limit == 0) OR ($pos < $limit))
141                                 $link = "";
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                 // Twitter is using its own limiter, so we always assume that shortened links will have this length
152                 if (strlen($link) > 0)
153                         $limit = $limit - 23;
154
155                 if (strlen($msg) > $limit) {
156
157                         if (!isset($post["url"])) {
158                                 $limit = $limit - 23;
159                                 $post["url"] = $b["plink"];
160                         }
161
162                         $lines = explode("\n", $msg);
163                         $msg = "";
164                         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
165                         foreach ($lines AS $row=>$line) {
166                                 if (strlen(trim($msg."\n".$line)) <= $limit)
167                                         $msg = trim($msg."\n".$line);
168                                 // Is the new message empty by now or is it a reshared message?
169                                 elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
170                                         $msg = substr(substr(trim($msg."\n".$line), 0, $limit), 0, -3)."...";
171                                 else
172                                         break;
173                         }
174                 }
175         }
176
177         $post["text"] = trim($msg);
178
179         return($post);
180 }
181 ?>