Fix several problems with image download
Fix fetch_file_contents, so that it returns the contents even if the data isn't an image. This is needed because the get_favicon_url function tries to download the webpage using this function, to see if there is a favicon in the page. The function now takes an optional $type parameter. This parameter control if the calling function cares about the content-type, or if the function should just return everything. If the $type parameter is set, the content-type should contain the string contained in $type, otherwise the function returns false. The second problem solved with this patch, is that the temporary file that should contain the image was empty in some cases. I never found out why this happended, but as curl_exec is capable of returning the fetched data, thus eliminating the need for the temporary file all together, the function have been changed to use this way of obtaining the data. The last problem fixed by this patch is that curl will now follow redirects. Author: Klaus S. Madsen <ksm@42.dk>
This commit is contained in:
parent
83573d3118
commit
a1af157410
|
@ -353,33 +353,31 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetch_file_contents($url) {
|
function fetch_file_contents($url, $type) {
|
||||||
if (USE_CURL_FOR_ICONS) {
|
if (USE_CURL_FOR_ICONS) {
|
||||||
$tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
|
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
$fp = fopen($tmpfile, "w");
|
|
||||||
|
|
||||||
if ($fp) {
|
|
||||||
curl_setopt($ch, CURLOPT_FILE, $fp);
|
|
||||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
|
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
|
||||||
curl_exec($ch);
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
|
||||||
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
|
||||||
if (strpos(curl_getinfo($ch, CURLINFO_CONTENT_TYPE), "image/") !== false) {
|
$contents = curl_exec($ch);
|
||||||
|
if ($contents === false) {
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
fclose($fp);
|
return false;
|
||||||
$contents = file_get_contents($tmpfile);
|
|
||||||
} else {
|
|
||||||
curl_close($ch);
|
|
||||||
fclose($fp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unlink($tmpfile);
|
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($type && strpos($content_type, "$type") === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return $contents;
|
return $contents;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return file_get_contents($url);
|
return file_get_contents($url);
|
||||||
}
|
}
|
||||||
|
@ -491,8 +489,9 @@
|
||||||
$icon_file = ICONS_DIR . "/$feed.ico";
|
$icon_file = ICONS_DIR . "/$feed.ico";
|
||||||
|
|
||||||
if ($favicon_url && !file_exists($icon_file)) {
|
if ($favicon_url && !file_exists($icon_file)) {
|
||||||
$contents = fetch_file_contents($favicon_url);
|
$contents = fetch_file_contents($favicon_url, "image");
|
||||||
|
|
||||||
|
if ($contents) {
|
||||||
$fp = fopen($icon_file, "w");
|
$fp = fopen($icon_file, "w");
|
||||||
|
|
||||||
if ($fp) {
|
if ($fp) {
|
||||||
|
@ -501,6 +500,7 @@
|
||||||
chmod($icon_file, 0644);
|
chmod($icon_file, 0644);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
error_reporting(DEFAULT_ERROR_LEVEL);
|
error_reporting(DEFAULT_ERROR_LEVEL);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue