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.

126 lines
2.4 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. require_once('library/HTML5/Parser.php');
  3. require_once('library/HTMLPurifier.auto.php');
  4. function parse_url_content(&$a) {
  5. logger('parse_url: ' . $_GET['url']);
  6. $url = trim(hex2bin($_GET['url']));
  7. logger('parse_url: ' . $url);
  8. $text = null;
  9. $template = "<br /><a href=\"%s\" >%s</a>%s<br />";
  10. $arr = array('url' => $url, 'text' => '');
  11. call_hooks('parse_link', $arr);
  12. if(strlen($arr['text'])) {
  13. echo $arr['text'];
  14. killme();
  15. }
  16. if($url) {
  17. $s = fetch_url($url);
  18. } else {
  19. echo '';
  20. killme();
  21. }
  22. logger('parse_url: data: ' . $s, LOGGER_DATA);
  23. if(! $s) {
  24. echo sprintf($template,$url,$url,'');
  25. killme();
  26. }
  27. if(strpos($s,'<title>')) {
  28. $title = substr($s,strpos($s,'<title>')+7,64);
  29. if(strpos($title,'<') !== false)
  30. $title = strip_tags(substr($title,0,strpos($title,'<')));
  31. }
  32. $config = HTMLPurifier_Config::createDefault();
  33. $config->set('Cache.DefinitionImpl', null);
  34. $purifier = new HTMLPurifier($config);
  35. $s = $purifier->purify($s);
  36. // logger('parse_url: purified: ' . $s, LOGGER_DATA);
  37. $dom = @HTML5_Parser::parse($s);
  38. if(! $dom) {
  39. echo sprintf($template,$url,$url,'');
  40. killme();
  41. }
  42. $items = $dom->getElementsByTagName('title');
  43. if($items) {
  44. foreach($items as $item) {
  45. $title = trim($item->textContent);
  46. break;
  47. }
  48. }
  49. $divs = $dom->getElementsByTagName('div');
  50. if($divs) {
  51. foreach($divs as $div) {
  52. $class = $div->getAttribute('class');
  53. if($class && (stristr($class,'article') || stristr($class,'content'))) {
  54. $items = $div->getElementsByTagName('p');
  55. if($items) {
  56. foreach($items as $item) {
  57. $text = $item->textContent;
  58. if(stristr($text,'<script')) {
  59. $text = '';
  60. continue;
  61. }
  62. $text = strip_tags($text);
  63. if(strlen($text) < 100) {
  64. $text = '';
  65. continue;
  66. }
  67. $text = substr($text,0,250) . '...' ;
  68. break;
  69. }
  70. }
  71. }
  72. if($text)
  73. break;
  74. }
  75. }
  76. if(! $text) {
  77. $items = $dom->getElementsByTagName('p');
  78. if($items) {
  79. foreach($items as $item) {
  80. $text = $item->textContent;
  81. if(stristr($text,'<script'))
  82. continue;
  83. $text = strip_tags($text);
  84. if(strlen($text) < 100) {
  85. $text = '';
  86. continue;
  87. }
  88. $text = substr($text,0,250) . '...' ;
  89. break;
  90. }
  91. }
  92. }
  93. if(strlen($text)) {
  94. $text = '<br /><br /><blockquote>' . $text . '</blockquote><br />';
  95. }
  96. $title = str_replace("\n",'',$title);
  97. echo sprintf($template,$url,($title) ? $title : $url,$text);
  98. killme();
  99. }