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.

199 lines
5.8 KiB

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