oembed: don't try to fetch media files and add Accept request header to fetch_url

This commit is contained in:
Fabio Comuni 2011-11-17 13:40:11 +01:00
parent d77c87f42a
commit 02281be0c3
2 changed files with 29 additions and 15 deletions

View File

@ -5,7 +5,7 @@
// results. // results.
if(! function_exists('fetch_url')) { if(! function_exists('fetch_url')) {
function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) { function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null) {
$a = get_app(); $a = get_app();
@ -14,9 +14,17 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) {
return false; return false;
@curl_setopt($ch, CURLOPT_HEADER, true); @curl_setopt($ch, CURLOPT_HEADER, true);
if (!is_null($accept_content)){
curl_setopt($ch,CURLOPT_HTTPHEADER, array (
"Accept: "+$accept_content
));
}
@curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
@curl_setopt($ch, CURLOPT_USERAGENT, "Friendika"); @curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
if(intval($timeout)) { if(intval($timeout)) {
@curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
} }

View File

@ -13,24 +13,30 @@ function oembed_replacecb($matches){
function oembed_fetch_url($embedurl){ function oembed_fetch_url($embedurl){
$txt = Cache::get($embedurl); $txt = Cache::get($embedurl);
$noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm");
$ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION);
if(is_null($txt)){ if(is_null($txt)){
$txt = ""; $txt = "";
// try oembed autodiscovery if (!in_array($ext, $noexts)){
$redirects = 0; // try oembed autodiscovery
$html_text = fetch_url($embedurl, false, $redirects, 15); $redirects = 0;
if($html_text){ $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*");
$dom = @DOMDocument::loadHTML($html_text); if($html_text){
if ($dom){ $dom = @DOMDocument::loadHTML($html_text);
$xpath = new DOMXPath($dom); if ($dom){
$attr = "oembed"; $xpath = new DOMXPath($dom);
$attr = "oembed";
$xattr = oe_build_xpath("class","oembed");
$entries = $xpath->query("//link[@type='application/json+oembed']"); $xattr = oe_build_xpath("class","oembed");
foreach($entries as $e){ $entries = $xpath->query("//link[@type='application/json+oembed']");
$href = $e->getAttributeNode("href")->nodeValue; foreach($entries as $e){
$txt = fetch_url($href); $href = $e->getAttributeNode("href")->nodeValue;
$txt = fetch_url($href);
}
} }
} }
} }