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.
 
 
 
 
 
 

283 lines
8.3 KiB

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