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.

182 lines
5.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. function oembed_replacecb($matches){
  3. logger('oembedcb');
  4. $embedurl=$matches[1];
  5. $j = oembed_fetch_url($embedurl);
  6. $s = oembed_format_object($j);
  7. return $s;//oembed_iframe($s,$j->width,$j->height);
  8. }
  9. function oembed_fetch_url($embedurl){
  10. $txt = Cache::get($embedurl);
  11. if(is_null($txt)){
  12. $txt = "";
  13. // try oembed autodiscovery
  14. $redirects = 0;
  15. $html_text = fetch_url($embedurl, false, $redirects, 15);
  16. if($html_text){
  17. $dom = @DOMDocument::loadHTML($html_text);
  18. if ($dom){
  19. $xpath = new DOMXPath($dom);
  20. $attr = "oembed";
  21. $xattr = oe_build_xpath("class","oembed");
  22. $entries = $xpath->query("//link[@type='application/json+oembed']");
  23. foreach($entries as $e){
  24. $href = $e->getAttributeNode("href")->nodeValue;
  25. $txt = fetch_url($href);
  26. }
  27. }
  28. }
  29. if ($txt==false || $txt==""){
  30. // try oohembed service
  31. $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
  32. $txt = fetch_url($ourl);
  33. }
  34. $txt=trim($txt);
  35. if ($txt[0]!="{") $txt='{"type":"error"}';
  36. //save in cache
  37. Cache::set($embedurl,$txt);
  38. }
  39. $j = json_decode($txt);
  40. $j->embedurl = $embedurl;
  41. return $j;
  42. }
  43. function oembed_format_object($j){
  44. $embedurl = $j->embedurl;
  45. $jhtml = oembed_iframe($j->html,$j->width,$j->height );
  46. $ret="<span class='oembed ".$j->type."'>";
  47. switch ($j->type) {
  48. case "video": {
  49. if (isset($j->thumbnail_url)) {
  50. $tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
  51. $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;
  52. $tr = $tw/$th;
  53. $th=120; $tw = $th*$tr;
  54. $tpl=get_markup_template('oembed_video.tpl');
  55. $ret.=replace_macros($tpl, array(
  56. '$embedurl'=>$embedurl,
  57. '$escapedhtml'=>base64_encode($jhtml),
  58. '$tw'=>$tw,
  59. '$th'=>$th,
  60. '$turl'=>$j->thumbnail_url,
  61. ));
  62. } else {
  63. $ret=$jhtml;
  64. }
  65. $ret.="<br>";
  66. }; break;
  67. case "photo": {
  68. $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
  69. $ret.="<br>";
  70. }; break;
  71. case "link": {
  72. //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
  73. }; break;
  74. case "rich": {
  75. // not so safe..
  76. $ret.= $jhtml;
  77. }; break;
  78. }
  79. // add link to source if not present in "rich" type
  80. if ( $j->type!='rich' || !strpos($j->html,$embedurl) ){
  81. $embedlink = (isset($j->title))?$j->title:$embedurl;
  82. $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
  83. if (isset($j->author_name)) $ret.=" by ".$j->author_name;
  84. if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
  85. } else {
  86. // add <a> for html2bbcode conversion
  87. $ret .= "<a href='$embedurl' rel='oembed'/>";
  88. }
  89. $ret.="<br style='clear:left'></span>";
  90. return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
  91. }
  92. function oembed_iframe($src,$width,$height) {
  93. if(! $width || strstr($width,'%'))
  94. $width = '640';
  95. if(! $height || strstr($height,'%'))
  96. $height = '300';
  97. // try and leave some room for the description line.
  98. $height = intval($height) + 80;
  99. $width = intval($width) + 40;
  100. $s = 'data:text/html;base64,' . base64_encode('<html><body>' . $src . '</body></html>');
  101. return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
  102. }
  103. function oembed_bbcode2html($text){
  104. $stopoembed = get_config("system","no_oembed");
  105. if ($stopoembed == true){
  106. return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
  107. }
  108. return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
  109. }
  110. function oe_build_xpath($attr, $value){
  111. // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
  112. return "contains( normalize-space( @$attr ), ' $value ' ) or substring( normalize-space( @$attr ), 1, string-length( '$value' ) + 1 ) = '$value ' or substring( normalize-space( @$attr ), string-length( @$attr ) - string-length( '$value' ) ) = ' $value' or @$attr = '$value'";
  113. }
  114. function oe_get_inner_html( $node ) {
  115. $innerHTML= '';
  116. $children = $node->childNodes;
  117. foreach ($children as $child) {
  118. $innerHTML .= $child->ownerDocument->saveXML( $child );
  119. }
  120. return $innerHTML;
  121. }
  122. /**
  123. * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
  124. * and replace it with [embed]url[/embed]
  125. */
  126. function oembed_html2bbcode($text) {
  127. // start parser only if 'oembed' is in text
  128. if (strpos($text, "oembed")){
  129. // convert non ascii chars to html entities
  130. $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
  131. // If it doesn't parse at all, just return the text.
  132. $dom = @DOMDocument::loadHTML($html_text);
  133. if(! $dom)
  134. return $text;
  135. $xpath = new DOMXPath($dom);
  136. $attr = "oembed";
  137. $xattr = oe_build_xpath("class","oembed");
  138. $entries = $xpath->query("//span[$xattr]");
  139. $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
  140. foreach($entries as $e) {
  141. $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
  142. if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
  143. }
  144. return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
  145. } else {
  146. return $text;
  147. }
  148. }