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