Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

548 lines
14 KiB

  1. <?php
  2. /* To-Do
  3. https://developers.google.com/+/plugins/snippet/
  4. <meta itemprop="name" content="Toller Titel">
  5. <meta itemprop="description" content="Eine tolle Beschreibung">
  6. <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
  7. <body itemscope itemtype="http://schema.org/Product">
  8. <h1 itemprop="name">Shiny Trinket</h1>
  9. <img itemprop="image" src="{image-url}" />
  10. <p itemprop="description">Shiny trinkets are shiny.</p>
  11. </body>
  12. */
  13. if(!function_exists('deletenode')) {
  14. function deletenode(&$doc, $node)
  15. {
  16. $xpath = new DomXPath($doc);
  17. $list = $xpath->query("//".$node);
  18. foreach ($list as $child)
  19. $child->parentNode->removeChild($child);
  20. }
  21. }
  22. function completeurl($url, $scheme) {
  23. $urlarr = parse_url($url);
  24. if (isset($urlarr["scheme"]))
  25. return($url);
  26. $schemearr = parse_url($scheme);
  27. $complete = $schemearr["scheme"]."://".$schemearr["host"];
  28. if (@$schemearr["port"] != "")
  29. $complete .= ":".$schemearr["port"];
  30. if(strpos($urlarr['path'],'/') !== 0)
  31. $complete .= '/';
  32. $complete .= $urlarr["path"];
  33. if (@$urlarr["query"] != "")
  34. $complete .= "?".$urlarr["query"];
  35. if (@$urlarr["fragment"] != "")
  36. $complete .= "#".$urlarr["fragment"];
  37. return($complete);
  38. }
  39. function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true) {
  40. $data = Cache::get("parse_url:".$no_guessing.":".$do_oembed.":".$url);
  41. if (!is_null($data)) {
  42. $data = unserialize($data);
  43. return $data;
  44. }
  45. $data = parseurl_getsiteinfo($url, $no_guessing, $do_oembed);
  46. Cache::set("parse_url:".$no_guessing.":".$do_oembed.":".$url,serialize($data), CACHE_DAY);
  47. return $data;
  48. }
  49. function parseurl_getsiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) {
  50. require_once("include/network.php");
  51. $a = get_app();
  52. $siteinfo = array();
  53. if ($count > 10) {
  54. logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
  55. return($siteinfo);
  56. }
  57. $url = trim($url, "'");
  58. $url = trim($url, '"');
  59. $url = original_url($url);
  60. $siteinfo["url"] = $url;
  61. $siteinfo["type"] = "link";
  62. $stamp1 = microtime(true);
  63. $ch = curl_init();
  64. curl_setopt($ch, CURLOPT_URL, $url);
  65. curl_setopt($ch, CURLOPT_HEADER, 1);
  66. curl_setopt($ch, CURLOPT_NOBODY, 1);
  67. curl_setopt($ch, CURLOPT_TIMEOUT, 3);
  68. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  69. //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  70. curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
  71. $header = curl_exec($ch);
  72. $curl_info = @curl_getinfo($ch);
  73. $http_code = $curl_info['http_code'];
  74. curl_close($ch);
  75. $a->save_timestamp($stamp1, "network");
  76. if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
  77. AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
  78. if ($curl_info['redirect_url'] != "")
  79. $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
  80. else
  81. $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
  82. return($siteinfo);
  83. }
  84. // if the file is too large then exit
  85. if ($curl_info["download_content_length"] > 1000000)
  86. return($siteinfo);
  87. // if it isn't a HTML file then exit
  88. if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
  89. return($siteinfo);
  90. if ($do_oembed) {
  91. require_once("include/oembed.php");
  92. $oembed_data = oembed_fetch_url($url);
  93. if ($oembed_data->type != "error")
  94. $siteinfo["type"] = $oembed_data->type;
  95. if (($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
  96. if (isset($oembed_data->title))
  97. $siteinfo["title"] = $oembed_data->title;
  98. if (isset($oembed_data->description))
  99. $siteinfo["text"] = trim($oembed_data->description);
  100. if (isset($oembed_data->thumbnail_url))
  101. $siteinfo["image"] = $oembed_data->thumbnail_url;
  102. }
  103. }
  104. $stamp1 = microtime(true);
  105. // Now fetch the body as well
  106. $ch = curl_init();
  107. curl_setopt($ch, CURLOPT_URL, $url);
  108. curl_setopt($ch, CURLOPT_HEADER, 1);
  109. curl_setopt($ch, CURLOPT_NOBODY, 0);
  110. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  112. curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
  113. $header = curl_exec($ch);
  114. $curl_info = @curl_getinfo($ch);
  115. $http_code = $curl_info['http_code'];
  116. curl_close($ch);
  117. $a->save_timestamp($stamp1, "network");
  118. // Fetch the first mentioned charset. Can be in body or header
  119. $charset = "";
  120. if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
  121. $charset = trim(trim(trim(array_pop($matches)), ';,'));
  122. if ($charset == "")
  123. $charset = "utf-8";
  124. $pos = strpos($header, "\r\n\r\n");
  125. if ($pos)
  126. $body = trim(substr($header, $pos));
  127. else
  128. $body = $header;
  129. if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
  130. logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
  131. //$body = mb_convert_encoding($body, "UTF-8", $charset);
  132. $body = iconv($charset, "UTF-8//TRANSLIT", $body);
  133. }
  134. $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
  135. $doc = new DOMDocument();
  136. @$doc->loadHTML($body);
  137. deletenode($doc, 'style');
  138. deletenode($doc, 'script');
  139. deletenode($doc, 'option');
  140. deletenode($doc, 'h1');
  141. deletenode($doc, 'h2');
  142. deletenode($doc, 'h3');
  143. deletenode($doc, 'h4');
  144. deletenode($doc, 'h5');
  145. deletenode($doc, 'h6');
  146. deletenode($doc, 'ol');
  147. deletenode($doc, 'ul');
  148. $xpath = new DomXPath($doc);
  149. $list = $xpath->query("//meta[@content]");
  150. foreach ($list as $node) {
  151. $attr = array();
  152. if ($node->attributes->length)
  153. foreach ($node->attributes as $attribute)
  154. $attr[$attribute->name] = $attribute->value;
  155. if (@$attr["http-equiv"] == 'refresh') {
  156. $path = $attr["content"];
  157. $pathinfo = explode(";", $path);
  158. $content = "";
  159. foreach ($pathinfo AS $value) {
  160. if (substr(strtolower($value), 0, 4) == "url=")
  161. $content = substr($value, 4);
  162. }
  163. if ($content != "") {
  164. $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
  165. return($siteinfo);
  166. }
  167. }
  168. }
  169. //$list = $xpath->query("head/title");
  170. $list = $xpath->query("//title");
  171. foreach ($list as $node)
  172. $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
  173. //$list = $xpath->query("head/meta[@name]");
  174. $list = $xpath->query("//meta[@name]");
  175. foreach ($list as $node) {
  176. $attr = array();
  177. if ($node->attributes->length)
  178. foreach ($node->attributes as $attribute)
  179. $attr[$attribute->name] = $attribute->value;
  180. $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
  181. if ($attr["content"] != "")
  182. switch (strtolower($attr["name"])) {
  183. case "fulltitle":
  184. $siteinfo["title"] = $attr["content"];
  185. break;
  186. case "description":
  187. $siteinfo["text"] = $attr["content"];
  188. break;
  189. case "thumbnail":
  190. $siteinfo["image"] = $attr["content"];
  191. break;
  192. case "twitter:image":
  193. $siteinfo["image"] = $attr["content"];
  194. break;
  195. case "twitter:image:src":
  196. $siteinfo["image"] = $attr["content"];
  197. break;
  198. case "twitter:card":
  199. if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
  200. $siteinfo["type"] = $attr["content"];
  201. break;
  202. case "twitter:description":
  203. $siteinfo["text"] = $attr["content"];
  204. break;
  205. case "twitter:title":
  206. $siteinfo["title"] = $attr["content"];
  207. break;
  208. case "dc.title":
  209. $siteinfo["title"] = $attr["content"];
  210. break;
  211. case "dc.description":
  212. $siteinfo["text"] = $attr["content"];
  213. break;
  214. case "keywords":
  215. $keywords = explode(",", $attr["content"]);
  216. break;
  217. case "news_keywords":
  218. $keywords = explode(",", $attr["content"]);
  219. break;
  220. }
  221. if ($siteinfo["type"] == "summary")
  222. $siteinfo["type"] = "link";
  223. }
  224. if (isset($keywords)) {
  225. $siteinfo["keywords"] = array();
  226. foreach ($keywords as $keyword)
  227. if (!in_array(trim($keyword), $siteinfo["keywords"]))
  228. $siteinfo["keywords"][] = trim($keyword);
  229. }
  230. //$list = $xpath->query("head/meta[@property]");
  231. $list = $xpath->query("//meta[@property]");
  232. foreach ($list as $node) {
  233. $attr = array();
  234. if ($node->attributes->length)
  235. foreach ($node->attributes as $attribute)
  236. $attr[$attribute->name] = $attribute->value;
  237. $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
  238. if ($attr["content"] != "")
  239. switch (strtolower($attr["property"])) {
  240. case "og:image":
  241. $siteinfo["image"] = $attr["content"];
  242. break;
  243. case "og:title":
  244. $siteinfo["title"] = $attr["content"];
  245. break;
  246. case "og:description":
  247. $siteinfo["text"] = $attr["content"];
  248. break;
  249. }
  250. }
  251. if ((@$siteinfo["image"] == "") AND !$no_guessing) {
  252. $list = $xpath->query("//img[@src]");
  253. foreach ($list as $node) {
  254. $attr = array();
  255. if ($node->attributes->length)
  256. foreach ($node->attributes as $attribute)
  257. $attr[$attribute->name] = $attribute->value;
  258. $src = completeurl($attr["src"], $url);
  259. $photodata = @getimagesize($src);
  260. if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
  261. if ($photodata[0] > 300) {
  262. $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
  263. $photodata[0] = 300;
  264. }
  265. if ($photodata[1] > 300) {
  266. $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
  267. $photodata[1] = 300;
  268. }
  269. $siteinfo["images"][] = array("src"=>$src,
  270. "width"=>$photodata[0],
  271. "height"=>$photodata[1]);
  272. }
  273. }
  274. } else {
  275. $src = completeurl($siteinfo["image"], $url);
  276. unset($siteinfo["image"]);
  277. $photodata = @getimagesize($src);
  278. if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
  279. $siteinfo["images"][] = array("src"=>$src,
  280. "width"=>$photodata[0],
  281. "height"=>$photodata[1]);
  282. }
  283. if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
  284. $text = "";
  285. $list = $xpath->query("//div[@class='article']");
  286. foreach ($list as $node)
  287. if (strlen($node->nodeValue) > 40)
  288. $text .= " ".trim($node->nodeValue);
  289. if ($text == "") {
  290. $list = $xpath->query("//div[@class='content']");
  291. foreach ($list as $node)
  292. if (strlen($node->nodeValue) > 40)
  293. $text .= " ".trim($node->nodeValue);
  294. }
  295. // If none text was found then take the paragraph content
  296. if ($text == "") {
  297. $list = $xpath->query("//p");
  298. foreach ($list as $node)
  299. if (strlen($node->nodeValue) > 40)
  300. $text .= " ".trim($node->nodeValue);
  301. }
  302. if ($text != "") {
  303. $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
  304. while (strpos($text, " "))
  305. $text = trim(str_replace(" ", " ", $text));
  306. $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
  307. }
  308. }
  309. logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
  310. call_hooks('getsiteinfo', $siteinfo);
  311. return($siteinfo);
  312. }
  313. function arr_add_hashes(&$item,$k) {
  314. $item = '#' . $item;
  315. }
  316. function parse_url_content(&$a) {
  317. $text = null;
  318. $str_tags = '';
  319. $textmode = false;
  320. if(local_user() && (! feature_enabled(local_user(),'richtext')))
  321. $textmode = true;
  322. //if($textmode)
  323. $br = (($textmode) ? "\n" : '<br />');
  324. if(x($_GET,'binurl'))
  325. $url = trim(hex2bin($_GET['binurl']));
  326. else
  327. $url = trim($_GET['url']);
  328. if($_GET['title'])
  329. $title = strip_tags(trim($_GET['title']));
  330. if($_GET['description'])
  331. $text = strip_tags(trim($_GET['description']));
  332. if($_GET['tags']) {
  333. $arr_tags = str_getcsv($_GET['tags']);
  334. if(count($arr_tags)) {
  335. array_walk($arr_tags,'arr_add_hashes');
  336. $str_tags = $br . implode(' ',$arr_tags) . $br;
  337. }
  338. }
  339. // add url scheme if missing
  340. $arrurl = parse_url($url);
  341. if (!x($arrurl, 'scheme')) {
  342. if (x($arrurl, 'host'))
  343. $url = "http:".$url;
  344. else
  345. $url = "http://".$url;
  346. }
  347. logger('parse_url: ' . $url);
  348. if($textmode)
  349. $template = '[bookmark=%s]%s[/bookmark]%s';
  350. else
  351. $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
  352. $arr = array('url' => $url, 'text' => '');
  353. call_hooks('parse_link', $arr);
  354. if(strlen($arr['text'])) {
  355. echo $arr['text'];
  356. killme();
  357. }
  358. if($url && $title && $text) {
  359. $title = str_replace(array("\r","\n"),array('',''),$title);
  360. if($textmode)
  361. $text = '[quote]' . trim($text) . '[/quote]' . $br;
  362. else {
  363. $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
  364. $title = htmlspecialchars($title);
  365. }
  366. $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
  367. logger('parse_url (unparsed): returns: ' . $result);
  368. echo $result;
  369. killme();
  370. }
  371. $siteinfo = parseurl_getsiteinfo($url);
  372. // if ($textmode) {
  373. // require_once("include/items.php");
  374. //
  375. // echo add_page_info_data($siteinfo);
  376. // killme();
  377. // }
  378. $url= $siteinfo["url"];
  379. // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
  380. if (strpos($url, '[') OR strpos($url, ']')) {
  381. require_once("include/network.php");
  382. $url = short_link($url);
  383. }
  384. $sitedata = "";
  385. if($siteinfo["title"] != "") {
  386. $text = $siteinfo["text"];
  387. $title = $siteinfo["title"];
  388. }
  389. $image = "";
  390. if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
  391. /* Execute below code only if image is present in siteinfo */
  392. $total_images = 0;
  393. $max_images = get_config('system','max_bookmark_images');
  394. if($max_images === false)
  395. $max_images = 2;
  396. else
  397. $max_images = intval($max_images);
  398. foreach ($siteinfo["images"] as $imagedata) {
  399. if($textmode)
  400. $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
  401. else
  402. $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
  403. $total_images ++;
  404. if($max_images && $max_images >= $total_images)
  405. break;
  406. }
  407. }
  408. if(strlen($text)) {
  409. if($textmode)
  410. $text = '[quote]'.trim($text).'[/quote]';
  411. else
  412. $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
  413. }
  414. if($image)
  415. $text = $br.$br.$image.$text;
  416. else
  417. $text = $br.$text;
  418. $title = str_replace(array("\r","\n"),array('',''),$title);
  419. $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
  420. logger('parse_url: returns: ' . $result);
  421. $sitedata .= trim($result);
  422. if (($siteinfo["type"] == "video") AND ($url != ""))
  423. echo "[class=type-video]".$sitedata."[/class]";
  424. elseif (($siteinfo["type"] != "photo"))
  425. echo "[class=type-link]".$sitedata."[/class]";
  426. else
  427. echo "[class=type-photo]".$title.$br.$image."[/class]";
  428. killme();
  429. }
  430. ?>