protected function file($args)
{
$target = $args['target'];
$download = !empty($args['download']);
$h403 = 'HTTP/1.x 403 Access Denied';
$h404 = 'HTTP/1.x 404 Not Found';
if (($volume = $this->volume($target)) == false) {
return array('error' => 'File not found', 'header' => $h404, 'raw' => true);
}
if (($file = $volume->file($target)) == false) {
return array('error' => 'File not found', 'header' => $h404, 'raw' => true);
}
if (!$file['read']) {
return array('error' => 'Access denied', 'header' => $h403, 'raw' => true);
}
if (($fp = $volume->open($target)) == false) {
return array('error' => 'File not found', 'header' => $h404, 'raw' => true);
}
// allow change MIME type by 'file.pre' callback functions
$mime = isset($args['mime']) ? $args['mime'] : $file['mime'];
if ($download) {
$disp = 'attachment';
} else {
$dispInlineRegex = $volume->getOption('dispInlineRegex');
$inlineRegex = false;
if ($dispInlineRegex) {
$inlineRegex = '#' . str_replace('#', '\\#', $dispInlineRegex) . '#';
try {
preg_match($inlineRegex, '');
} catch (Exception $e) {
$inlineRegex = false;
}
}
if (!$inlineRegex) {
$inlineRegex = '#^(?:(?:image|text)|application/x-shockwave-flash$)#';
}
$disp = preg_match($inlineRegex, $mime) ? 'inline' : 'attachment';
}
$filenameEncoded = rawurlencode($file['name']);
if (strpos($filenameEncoded, '%') === false) {
// ASCII only
$filename = 'filename="' . $file['name'] . '"';
} else {
$ua = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/MSIE [4-8]/', $ua)) {
// IE < 9 do not support RFC 6266 (RFC 2231/RFC 5987)
$filename = 'filename="' . $filenameEncoded . '"';
} elseif (strpos($ua, 'Chrome') === false && strpos($ua, 'Safari') !== false && preg_match('#Version/[3-5]#', $ua)) {
// Safari < 6
$filename = 'filename="' . str_replace('"', '', $file['name']) . '"';
} else {
// RFC 6266 (RFC 2231/RFC 5987)
$filename = 'filename*=UTF-8\'\'' . $filenameEncoded;
}
}
$result = array('volume' => $volume, 'pointer' => $fp, 'info' => $file, 'header' => array('Content-Type: ' . $mime, 'Content-Disposition: ' . $disp . '; ' . $filename, 'Content-Transfer-Encoding: binary', 'Content-Length: ' . $file['size'], 'Connection: close'));
if (isset($file['url']) && $file['url'] && $file['url'] != 1) {
$result['header'][] = 'Content-Location: ' . $file['url'];
}
return $result;
}