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.

272 lines
8.1 KiB

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, $no_rich_type = false){
  10. $embedurl = trim($embedurl, "'");
  11. $embedurl = trim($embedurl, '"');
  12. $a = get_app();
  13. $txt = Cache::get($a->videowidth . $embedurl);
  14. // These media files should now be caught in bbcode.php
  15. // left here as a fallback in case this is called from another source
  16. $noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm");
  17. $ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION);
  18. if(is_null($txt)){
  19. $txt = "";
  20. if (!in_array($ext, $noexts)){
  21. // try oembed autodiscovery
  22. $redirects = 0;
  23. $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*"); /**/
  24. if($html_text){
  25. $dom = @DOMDocument::loadHTML($html_text);
  26. if ($dom){
  27. $xpath = new DOMXPath($dom);
  28. $attr = "oembed";
  29. $xattr = oe_build_xpath("class","oembed");
  30. $entries = $xpath->query("//link[@type='application/json+oembed']");
  31. foreach($entries as $e){
  32. $href = $e->getAttributeNode("href")->nodeValue;
  33. $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. if ($txt==false || $txt==""){
  40. $embedly = get_config("system", "embedly");
  41. if ($embedly == "") {
  42. // try oohembed service
  43. $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl).'&maxwidth=' . $a->videowidth;
  44. $txt = fetch_url($ourl);
  45. } else {
  46. // try embedly service
  47. $ourl = "https://api.embed.ly/1/oembed?key=".$embedly."&url=".urlencode($embedurl);
  48. $txt = fetch_url($ourl);
  49. }
  50. logger("oembed_fetch_url: ".$txt, LOGGER_DEBUG);
  51. }
  52. $txt=trim($txt);
  53. if ($txt[0]!="{") $txt='{"type":"error"}';
  54. //save in cache
  55. Cache::set($a->videowidth . $embedurl,$txt);
  56. }
  57. $j = json_decode($txt);
  58. if (!is_object($j))
  59. return false;
  60. // Always embed the SSL version
  61. if (isset($j->html))
  62. $j->html = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
  63. array("https://www.youtube.com/", "https://player.vimeo.com/"), $j->html);
  64. $j->embedurl = $embedurl;
  65. // If fetching information doesn't work, then improve via internal functions
  66. if (($j->type == "error") OR ($no_rich_type AND ($j->type == "rich"))) {
  67. require_once("mod/parse_url.php");
  68. $data = parseurl_getsiteinfo($embedurl, true, false);
  69. $j->type = $data["type"];
  70. if ($j->type == "photo") {
  71. $j->url = $data["url"];
  72. //$j->width = $data["images"][0]["width"];
  73. //$j->height = $data["images"][0]["height"];
  74. }
  75. if (isset($data["title"]))
  76. $j->title = $data["title"];
  77. if (isset($data["text"]))
  78. $j->description = $data["text"];
  79. if (is_array($data["images"])) {
  80. $j->thumbnail_url = $data["images"][0]["src"];
  81. $j->thumbnail_width = $data["images"][0]["width"];
  82. $j->thumbnail_height = $data["images"][0]["height"];
  83. }
  84. }
  85. call_hooks('oembed_fetch_url', $embedurl, $j);
  86. return $j;
  87. }
  88. function oembed_format_object($j){
  89. require_once("mod/proxy.php");
  90. $a = get_app();
  91. $embedurl = $j->embedurl;
  92. $jhtml = oembed_iframe($j->embedurl,(isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null) );
  93. $ret="<span class='oembed ".$j->type."'>";
  94. switch ($j->type) {
  95. case "video": {
  96. if (isset($j->thumbnail_url)) {
  97. $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width:200;
  98. $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height:180;
  99. // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
  100. $tr = (($th) ? $tw/$th : 1);
  101. $th=120; $tw = $th*$tr;
  102. $tpl=get_markup_template('oembed_video.tpl');
  103. $ret.=replace_macros($tpl, array(
  104. '$baseurl' => $a->get_baseurl(),
  105. '$embedurl'=>$embedurl,
  106. '$escapedhtml'=>base64_encode($jhtml),
  107. '$tw'=>$tw,
  108. '$th'=>$th,
  109. '$turl'=>$j->thumbnail_url,
  110. ));
  111. } else {
  112. $ret=$jhtml;
  113. }
  114. $ret.="<br>";
  115. }; break;
  116. case "photo": {
  117. $ret.= "<img width='".$j->width."' src='".proxy_url($j->url)."'>";
  118. //$ret.= "<img width='".$j->width."' height='".$j->height."' src='".proxy_url($j->url)."'>";
  119. $ret.="<br>";
  120. }; break;
  121. case "link": {
  122. //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
  123. }; break;
  124. case "rich": {
  125. // not so safe..
  126. if (!get_config("system","no_oembed_rich_content"))
  127. $ret.= $jhtml;
  128. }; break;
  129. }
  130. // add link to source if not present in "rich" type
  131. if ($j->type!='rich' || !strpos($j->html,$embedurl) ){
  132. $ret .= "<h4>";
  133. if (isset($j->title)) {
  134. if (isset($j->provider_name))
  135. $ret .= $j->provider_name.": ";
  136. $embedlink = (isset($j->title))?$j->title:$embedurl;
  137. $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
  138. if (isset($j->author_name))
  139. $ret.=" (".$j->author_name.")";
  140. } elseif (isset($j->provider_name) OR isset($j->author_name)) {
  141. $embedlink = "";
  142. if (isset($j->provider_name))
  143. $embedlink .= $j->provider_name;
  144. if (isset($j->author_name)) {
  145. if ($embedlink != "")
  146. $embedlink .= ": ";
  147. $embedlink .= $j->author_name;
  148. }
  149. if (trim($embedlink) == "")
  150. $embedlink = $embedurl;
  151. $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
  152. }
  153. //if (isset($j->author_name)) $ret.=" by ".$j->author_name;
  154. //if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
  155. $ret .= "</h4>";
  156. } else {
  157. // add <a> for html2bbcode conversion
  158. $ret .= "<a href='$embedurl' rel='oembed'>$embedurl</a>";
  159. $ret.="<br style='clear:left'></span>";
  160. }
  161. return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
  162. }
  163. function oembed_iframe($src,$width,$height) {
  164. if(! $width || strstr($width,'%'))
  165. $width = '640';
  166. if(! $height || strstr($height,'%'))
  167. $height = '300';
  168. // try and leave some room for the description line.
  169. $height = intval($height) + 80;
  170. $width = intval($width) + 40;
  171. $a = get_app();
  172. $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
  173. return '<iframe class="embed_rich" height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
  174. }
  175. function oembed_bbcode2html($text){
  176. $stopoembed = get_config("system","no_oembed");
  177. if ($stopoembed == true){
  178. return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
  179. }
  180. return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
  181. }
  182. function oe_build_xpath($attr, $value){
  183. // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
  184. 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'";
  185. }
  186. function oe_get_inner_html( $node ) {
  187. $innerHTML= '';
  188. $children = $node->childNodes;
  189. foreach ($children as $child) {
  190. $innerHTML .= $child->ownerDocument->saveXML( $child );
  191. }
  192. return $innerHTML;
  193. }
  194. /**
  195. * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
  196. * and replace it with [embed]url[/embed]
  197. */
  198. function oembed_html2bbcode($text) {
  199. // start parser only if 'oembed' is in text
  200. if (strpos($text, "oembed")){
  201. // convert non ascii chars to html entities
  202. $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
  203. // If it doesn't parse at all, just return the text.
  204. $dom = @DOMDocument::loadHTML($html_text);
  205. if(! $dom)
  206. return $text;
  207. $xpath = new DOMXPath($dom);
  208. $attr = "oembed";
  209. $xattr = oe_build_xpath("class","oembed");
  210. $entries = $xpath->query("//span[$xattr]");
  211. $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
  212. foreach($entries as $e) {
  213. $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
  214. if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
  215. }
  216. return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
  217. } else {
  218. return $text;
  219. }
  220. }