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