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.

264 lines
7.7 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. $j->embedurl = $embedurl;
  61. // If fetching information doesn't work, then improve via internal functions
  62. if (($j->type == "error") OR ($no_rich_type AND ($j->type == "rich"))) {
  63. require_once("mod/parse_url.php");
  64. $data = parseurl_getsiteinfo($embedurl, true, false);
  65. $j->type = $data["type"];
  66. if ($j->type == "photo") {
  67. $j->url = $data["url"];
  68. //$j->width = $data["images"][0]["width"];
  69. //$j->height = $data["images"][0]["height"];
  70. }
  71. if (isset($data["title"]))
  72. $j->title = $data["title"];
  73. if (isset($data["text"]))
  74. $j->description = $data["text"];
  75. if (is_array($data["images"])) {
  76. $j->thumbnail_url = $data["images"][0]["src"];
  77. $j->thumbnail_width = $data["images"][0]["width"];
  78. $j->thumbnail_height = $data["images"][0]["height"];
  79. }
  80. }
  81. return $j;
  82. }
  83. function oembed_format_object($j){
  84. require_once("mod/proxy.php");
  85. $a = get_app();
  86. $embedurl = $j->embedurl;
  87. $jhtml = oembed_iframe($j->embedurl,(isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null) );
  88. $ret="<span class='oembed ".$j->type."'>";
  89. switch ($j->type) {
  90. case "video": {
  91. if (isset($j->thumbnail_url)) {
  92. $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width:200;
  93. $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height:180;
  94. // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
  95. $tr = (($th) ? $tw/$th : 1);
  96. $th=120; $tw = $th*$tr;
  97. $tpl=get_markup_template('oembed_video.tpl');
  98. $ret.=replace_macros($tpl, array(
  99. '$baseurl' => $a->get_baseurl(),
  100. '$embedurl'=>$embedurl,
  101. '$escapedhtml'=>base64_encode($jhtml),
  102. '$tw'=>$tw,
  103. '$th'=>$th,
  104. '$turl'=>$j->thumbnail_url,
  105. ));
  106. } else {
  107. $ret=$jhtml;
  108. }
  109. $ret.="<br>";
  110. }; break;
  111. case "photo": {
  112. $ret.= "<img width='".$j->width."' src='".proxy_url($j->url)."'>";
  113. //$ret.= "<img width='".$j->width."' height='".$j->height."' src='".proxy_url($j->url)."'>";
  114. $ret.="<br>";
  115. }; break;
  116. case "link": {
  117. //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
  118. }; break;
  119. case "rich": {
  120. // not so safe..
  121. if (!get_config("system","no_oembed_rich_content"))
  122. $ret.= $jhtml;
  123. }; break;
  124. }
  125. // add link to source if not present in "rich" type
  126. if ($j->type!='rich' || !strpos($j->html,$embedurl) ){
  127. if (isset($j->title)) {
  128. if (isset($j->provider_name))
  129. $ret .= $j->provider_name.": ";
  130. $embedlink = (isset($j->title))?$j->title:$embedurl;
  131. $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
  132. if (isset($j->author_name))
  133. $ret.=" (".$j->author_name.")";
  134. } elseif (isset($j->provider_name) OR isset($j->author_name)) {
  135. $embedlink = "";
  136. if (isset($j->provider_name))
  137. $embedlink .= $j->provider_name;
  138. if (isset($j->author_name)) {
  139. if ($embedlink != "")
  140. $embedlink .= ": ";
  141. $embedlink .= $j->author_name;
  142. }
  143. if (trim($embedlink) == "")
  144. $embedlink = $embedurl;
  145. $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
  146. }
  147. //if (isset($j->author_name)) $ret.=" by ".$j->author_name;
  148. //if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
  149. } else {
  150. // add <a> for html2bbcode conversion
  151. $ret .= "<a href='$embedurl' rel='oembed'>$embedurl</a>";
  152. }
  153. $ret.="<br style='clear:left'></span>";
  154. return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
  155. }
  156. function oembed_iframe($src,$width,$height) {
  157. if(! $width || strstr($width,'%'))
  158. $width = '640';
  159. if(! $height || strstr($height,'%'))
  160. $height = '300';
  161. // try and leave some room for the description line.
  162. $height = intval($height) + 80;
  163. $width = intval($width) + 40;
  164. $a = get_app();
  165. $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
  166. return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
  167. }
  168. function oembed_bbcode2html($text){
  169. $stopoembed = get_config("system","no_oembed");
  170. if ($stopoembed == true){
  171. return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
  172. }
  173. return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
  174. }
  175. function oe_build_xpath($attr, $value){
  176. // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
  177. 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'";
  178. }
  179. function oe_get_inner_html( $node ) {
  180. $innerHTML= '';
  181. $children = $node->childNodes;
  182. foreach ($children as $child) {
  183. $innerHTML .= $child->ownerDocument->saveXML( $child );
  184. }
  185. return $innerHTML;
  186. }
  187. /**
  188. * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
  189. * and replace it with [embed]url[/embed]
  190. */
  191. function oembed_html2bbcode($text) {
  192. // start parser only if 'oembed' is in text
  193. if (strpos($text, "oembed")){
  194. // convert non ascii chars to html entities
  195. $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
  196. // If it doesn't parse at all, just return the text.
  197. $dom = @DOMDocument::loadHTML($html_text);
  198. if(! $dom)
  199. return $text;
  200. $xpath = new DOMXPath($dom);
  201. $attr = "oembed";
  202. $xattr = oe_build_xpath("class","oembed");
  203. $entries = $xpath->query("//span[$xattr]");
  204. $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
  205. foreach($entries as $e) {
  206. $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
  207. if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
  208. }
  209. return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
  210. } else {
  211. return $text;
  212. }
  213. }