]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
parse_url: Fetch multiple pictures so that the user can decide what to take.
[friendica.git] / mod / parse_url.php
1 <?php
2 require_once('include/Photo.php');
3
4 if(!function_exists('deletenode')) {
5         function deletenode(&$doc, $node)
6         {
7                 $xpath = new DomXPath($doc);
8                 $list = $xpath->query("//".$node);
9                 foreach ($list as $child)
10                         $child->parentNode->removeChild($child);
11         }
12 }
13
14 function parseurl_getsiteinfo($url) {
15         $siteinfo = array();
16
17         $ch = curl_init();
18         curl_setopt($ch, CURLOPT_URL, $url);
19         curl_setopt($ch, CURLOPT_HEADER, 1);
20         curl_setopt($ch, CURLOPT_NOBODY, 0);
21         curl_setopt($ch, CURLOPT_TIMEOUT, 3);
22         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23         curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
24
25         $header = curl_exec($ch);
26         curl_close($ch);
27
28         if (preg_match('/charset=(.*?)\n/', $header, $matches))
29                 $charset = trim(array_pop($matches));
30         else
31                 $charset = "utf-8";
32
33         $pos = strpos($header, "\r\n\r\n");
34
35         if ($pos)
36                 $body = trim(substr($header, $pos));
37         else
38                 $body = $header;
39
40         $body = mb_convert_encoding($body, "UTF-8", $charset);
41         $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
42
43         $doc = new DOMDocument();
44         @$doc->loadHTML($body);
45
46         deletenode($doc, 'style');
47         deletenode($doc, 'script');
48         deletenode($doc, 'option');
49         deletenode($doc, 'h1');
50         deletenode($doc, 'h2');
51         deletenode($doc, 'h3');
52         deletenode($doc, 'h4');
53         deletenode($doc, 'h5');
54         deletenode($doc, 'h6');
55         deletenode($doc, 'ol');
56         deletenode($doc, 'ul');
57
58         $xpath = new DomXPath($doc);
59
60         $list = $xpath->query("head/title");
61         foreach ($list as $node)
62                 $siteinfo["title"] =  html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
63
64         $list = $xpath->query("head/meta[@name]");
65         foreach ($list as $node) {
66                 $attr = array();
67                 if ($node->attributes->length)
68                         foreach ($node->attributes as $attribute)
69                                 $attr[$attribute->name] = $attribute->value;
70
71                 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
72
73                 switch (strtolower($attr["name"])) {
74                         case "fulltitle":
75                                 $siteinfo["title"] = $attr["content"];
76                                 break;
77                         case "description":
78                                 $siteinfo["text"] = $attr["content"];
79                                 break;
80                         case "dc.title":
81                                 $siteinfo["title"] = $attr["content"];
82                                 break;
83                         case "dc.description":
84                                 $siteinfo["text"] = $attr["content"];
85                                 break;
86                 }
87         }
88
89         $list = $xpath->query("head/meta[@property]");
90         foreach ($list as $node) {
91                 $attr = array();
92                 if ($node->attributes->length)
93                         foreach ($node->attributes as $attribute)
94                                 $attr[$attribute->name] = $attribute->value;
95
96                 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
97
98                 switch (strtolower($attr["property"])) {
99                         case "og:image":
100                                 $siteinfo["image"] = $attr["content"];
101                                 break;
102                         case "og:title":
103                                 $siteinfo["title"] = $attr["content"];
104                                 break;
105                         case "og:description":
106                                 $siteinfo["text"] = $attr["content"];
107                                 break;
108                 }
109         }
110
111         if ($siteinfo["image"] == "") {
112                 $list = $xpath->query("//img[@src]");
113                 foreach ($list as $node) {
114                         $attr = array();
115                         if ($node->attributes->length)
116                                 foreach ($node->attributes as $attribute)
117                                         $attr[$attribute->name] = $attribute->value;
118
119                         // guess mimetype from headers or filename
120                         $type = guess_image_type($attr["src"],true);
121
122                         $i = fetch_url($attr["src"]);
123                         $ph = new Photo($i, $type);
124
125                         if($ph->is_valid() and ($ph->getWidth() > 200) and ($ph->getHeight() > 200)) {
126                                 if ($siteinfo["image"] == "")
127                                         $siteinfo["image"] = $attr["src"];
128
129                                 if($ph->getWidth() > 300 || $ph->getHeight() > 300) {
130                                         $ph->scaleImage(300);
131                                         $siteinfo["images"][] = array("src"=>$attr["src"],
132                                                                         "width"=>$ph->getWidth(),
133                                                                         "height"=>$ph->getHeight());
134                                 } else
135                                         $siteinfo["images"][] = array("src"=>$attr["src"],
136                                                                         "width"=>$ph->getWidth(),
137                                                                         "height"=>$ph->getHeight());
138                         }
139                 }
140         } else {
141                 // guess mimetype from headers or filename
142                 $type = guess_image_type($siteinfo["image"],true);
143
144                 $i = fetch_url($siteinfo["image"]);
145                 $ph = new Photo($i, $type);
146
147                 if($ph->is_valid())
148                         $siteinfo["images"][] = array("src"=>$siteinfo["image"],
149                                                         "width"=>$ph->getWidth(),
150                                                         "height"=>$ph->getHeight());
151         }
152
153         if ($siteinfo["text"] == "") {
154                 $text = "";
155
156                 $list = $xpath->query("//div[@class='article']");
157                 foreach ($list as $node)
158                         $text .= " ".trim($node->nodeValue);
159
160                 if ($text == "") {
161                         $list = $xpath->query("//div[@class='content']");
162                         foreach ($list as $node)
163                                 $text .= " ".trim($node->nodeValue);
164                 }
165
166                 // If none text was found then take the paragraph content
167                 if ($text == "") {
168                         $list = $xpath->query("//p");
169                         foreach ($list as $node)
170                                 $text .= " ".trim($node->nodeValue);
171                 }
172
173                 if ($text != "") {
174                         $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
175
176                         while (strpos($text, "  "))
177                                 $text = trim(str_replace("  ", " ", $text));
178
179                         $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
180                 }
181         }
182
183         return($siteinfo);
184 }
185
186 function arr_add_hashes(&$item,$k) {
187         $item = '#' . $item;
188 }
189
190 function parse_url_content(&$a) {
191
192         $text = null;
193         $str_tags = '';
194
195         $textmode = false;
196         if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
197                 $textmode = true;
198
199         //if($textmode)
200         $br = (($textmode) ? "\n" : '<br />');
201
202         if(x($_GET,'binurl'))
203                 $url = trim(hex2bin($_GET['binurl']));
204         else
205                 $url = trim($_GET['url']);
206
207         if($_GET['title'])
208                 $title = strip_tags(trim($_GET['title']));
209
210         if($_GET['description'])
211                 $text = strip_tags(trim($_GET['description']));
212
213         if($_GET['tags']) {
214                 $arr_tags = str_getcsv($_GET['tags']);
215                 if(count($arr_tags)) {
216                         array_walk($arr_tags,'arr_add_hashes');
217                         $str_tags = $br . implode(' ',$arr_tags) . $br;
218                 }
219         }
220
221         logger('parse_url: ' . $url);
222
223         if($textmode)
224                 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
225         else
226                 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
227
228         $arr = array('url' => $url, 'text' => '');
229
230         call_hooks('parse_link', $arr);
231
232         if(strlen($arr['text'])) {
233                 echo $arr['text'];
234                 killme();
235         }
236
237
238         if($url && $title && $text) {
239
240                 if($textmode)
241                         $text = $br . $br . '[quote]' . trim($text) . '[/quote]' . $br;
242                 else
243                         $text = '<br /><br /><blockquote>' . trim($text) . '</blockquote><br />';
244
245                 $title = str_replace(array("\r","\n"),array('',''),$title);
246
247                 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
248
249                 logger('parse_url (unparsed): returns: ' . $result);
250
251                 echo $result;
252                 killme();
253         }
254
255         $siteinfo = parseurl_getsiteinfo($url);
256
257         if($siteinfo["title"] == "") {
258                 echo sprintf($template,$url,$url,'') . $str_tags;
259                 killme();
260         } else {
261                 $text = $siteinfo["text"];
262                 $title = $siteinfo["title"];
263         }
264
265         $image = "";
266
267         foreach ($siteinfo["images"] as $imagedata)
268                 if($textmode)
269                         $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]';
270                 else
271                         $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" />';
272
273 /*      if ($image != "") {
274                 $i = fetch_url($image);
275                 if($i) {
276                         require_once('include/Photo.php');
277                         // guess mimetype from headers or filename
278                         $type = guess_image_type($image,true);
279
280                         $ph = new Photo($i, $type);
281                         if($ph->is_valid()) {
282                                 if($ph->getWidth() > 300 || $ph->getHeight() > 300) {
283                                         $ph->scaleImage(300);
284                                         $new_width = $ph->getWidth();
285                                         $new_height = $ph->getHeight();
286                                         if($textmode)
287                                                 $image = $br . $br . '[img=' . $new_width . 'x' . $new_height . ']' . $image . '[/img]';
288                                         else
289                                                 $image = '<br /><br /><img height="' . $new_height . '" width="' . $new_width . '" src="' .$image . '" alt="photo" />';
290                                 } else {
291                                         if($textmode)
292                                                 $image = $br.$br.'[img]'.$image.'[/img]';
293                                         else
294                                                 $image = '<br /><br /><img src="'.$image.'" alt="photo" />';
295                                 }
296                         }
297                 }
298         }*/
299
300         if(strlen($text)) {
301                 if($textmode)
302                         $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
303                 else
304                         $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
305         }
306
307         if($image) {
308                 $text = $br.$br.$image.$br.$text;
309         }
310         $title = str_replace(array("\r","\n"),array('',''),$title);
311
312         $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
313
314         logger('parse_url: returns: ' . $result);
315
316         echo $result;
317         killme();
318 }