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);
}
if ($download) {
$disp = 'attachment';
$mime = 'application/octet-stream';
} else {
$disp = preg_match('/^(image|text)/i', $file['mime']) || $file['mime'] == 'application/x-shockwave-flash' ? 'inline' : 'attachment';
$mime = $file['mime'];
}
$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) {
// Safari
$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-Location: ' . $file['name'], 'Content-Transfer-Encoding: binary', 'Content-Length: ' . $file['size'], 'Connection: close'));
return $result;
}