remove image type double guess by checking HTTP headers

This commit is contained in:
Max Kostikov 2018-11-08 22:43:17 +01:00
parent 17cf824545
commit 098ec1abb4

View File

@ -485,11 +485,11 @@ function guess_image_type($filename, $headers = '') {
$h = explode("\n",$headers); $h = explode("\n",$headers);
foreach ($h as $l) { foreach ($h as $l) {
list($k,$v) = array_map("trim", explode(":", trim($l), 2)); list($k,$v) = array_map("trim", explode(":", trim($l), 2));
$hdrs[$k] = $v; $hdrs[strtolower($k)] = $v;
} }
logger('Curl headers: '.var_export($hdrs, true), LOGGER_DEBUG); logger('Curl headers: '.var_export($hdrs, true), LOGGER_DEBUG);
if (array_key_exists('Content-Type', $hdrs)) if (array_key_exists('content-type', $hdrs))
$type = $hdrs['Content-Type']; $type = $hdrs['content-type'];
} }
if (is_null($type)){ if (is_null($type)){
@ -637,17 +637,9 @@ function import_xchan_photo($photo,$xchan,$thing = false,$force = false) {
$type = guess_image_type($photo, $result['header']); $type = guess_image_type($photo, $result['header']);
$modified = gmdate('Y-m-d H:i:s', (preg_match('/last-modified: (.+) \S+/i', $result['header'], $o) ? strtotime($o[1] . 'Z') : time())); $modified = gmdate('Y-m-d H:i:s', (preg_match('/last-modified: (.+) \S+/i', $result['header'], $o) ? strtotime($o[1] . 'Z') : time()));
$h = explode("\n",$result['header']); if(is_null($type))
if($h) {
foreach($h as $hl) {
if(stristr($hl,'content-type:')) {
if(! stristr($hl,'image/')) {
$photo_failure = true; $photo_failure = true;
} }
}
}
}
}
elseif($result['return_code'] = 304) { elseif($result['return_code'] = 304) {
$photo = z_root() . '/photo/' . $hash . '-4'; $photo = z_root() . '/photo/' . $hash . '-4';
$thumb = z_root() . '/photo/' . $hash . '-5'; $thumb = z_root() . '/photo/' . $hash . '-5';
@ -744,18 +736,10 @@ function import_channel_photo_from_url($photo,$aid,$uid) {
$img_str = $result['body']; $img_str = $result['body'];
$type = guess_image_type($photo, $result['header']); $type = guess_image_type($photo, $result['header']);
$h = explode("\n",$result['header']); if(is_null($type))
if($h) {
foreach($h as $hl) {
if(stristr($hl,'content-type:')) {
if(! stristr($hl,'image/')) {
$photo_failure = true; $photo_failure = true;
} }
} }
}
}
}
}
else { else {
$photo_failure = true; $photo_failure = true;
} }