3
* Methods to display or download any type of file
7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
16
* @subpackage cake.cake.libs.view
17
* @since CakePHP(tm) v 1.2.0.5714
18
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20
App::import('View', 'View', false);
22
class MediaView extends View {
25
* Holds known mime type mappings
30
var $mimeType = array(
31
'ai' => 'application/postscript', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream',
32
'ccad' => 'application/clariscad', 'cdf' => 'application/x-netcdf', 'class' => 'application/octet-stream',
33
'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh',
34
'csv' => 'application/csv', 'dcr' => 'application/x-director', 'dir' => 'application/x-director',
35
'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'drw' => 'application/drafting',
36
'dvi' => 'application/x-dvi', 'dwg' => 'application/acad', 'dxf' => 'application/dxf',
37
'dxr' => 'application/x-director', 'eot' => 'application/vnd.ms-fontobject', 'eps' => 'application/postscript',
38
'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
39
'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
40
'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
41
'hqx' => 'application/mac-binhex40', 'ico' => 'image/vnd.microsoft.icon', 'ips' => 'application/x-ipscript',
42
'ipx' => 'application/x-ipix', 'js' => 'application/x-javascript', 'latex' => 'application/x-latex',
43
'lha' => 'application/octet-stream', 'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream',
44
'man' => 'application/x-troff-man', 'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif',
45
'ms' => 'application/x-troff-ms', 'nc' => 'application/x-netcdf', 'oda' => 'application/oda',
46
'otf' => 'font/otf', 'pdf' => 'application/pdf',
47
'pgn' => 'application/x-chess-pgn', 'pot' => 'application/mspowerpoint', 'pps' => 'application/mspowerpoint',
48
'ppt' => 'application/mspowerpoint', 'ppz' => 'application/mspowerpoint', 'pre' => 'application/x-freelance',
49
'prt' => 'application/pro_eng', 'ps' => 'application/postscript', 'roff' => 'application/x-troff',
50
'scm' => 'application/x-lotusscreencam', 'set' => 'application/set', 'sh' => 'application/x-sh',
51
'shar' => 'application/x-shar', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan',
52
'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan',
53
'smi' => 'application/smil', 'smil' => 'application/smil', 'sol' => 'application/solids',
54
'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'step' => 'application/STEP',
55
'stl' => 'application/SLA', 'stp' => 'application/STEP', 'sv4cpio' => 'application/x-sv4cpio',
56
'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
57
'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff',
58
'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
59
'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tr' => 'application/x-troff',
60
'tsp' => 'application/dsptype', 'ttf' => 'font/ttf',
61
'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
62
'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
63
'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
64
'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
65
'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
66
'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
67
'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
68
'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
69
'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
70
'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
71
'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
72
'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
73
'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
74
'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
75
'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
76
'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
77
'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
78
'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
79
'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
80
'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
81
'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
82
'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
83
'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb');
86
* Holds headers sent to browser before rendering media
91
var $_headers = array();
96
* @param object $controller
98
function __construct(&$controller) {
99
parent::__construct($controller);
103
* Display or download the given file
108
$name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
109
extract($this->viewVars, EXTR_OVERWRITE);
112
$id = $id . '_' . $size;
118
$path = APP . $path . $id;
121
if (!file_exists($path)) {
122
header('Content-Type: text/html');
123
$this->cakeError('error404');
126
if (is_null($name)) {
130
if (is_array($mimeType)) {
131
$this->mimeType = array_merge($this->mimeType, $mimeType);
134
if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
137
$fileSize = @filesize($path);
138
$handle = fopen($path, 'rb');
140
if ($handle === false) {
143
if (!empty($modified)) {
144
$modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
146
$modified = gmdate('D, d M Y H:i:s') . ' GMT';
150
$contentTypes = array('application/octet-stream');
151
$agent = env('HTTP_USER_AGENT');
153
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
154
$contentTypes[0] = 'application/octetstream';
155
} else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
156
$contentTypes[0] = 'application/force-download';
157
array_merge($contentTypes, array(
158
'application/octet-stream',
159
'application/download'
162
foreach($contentTypes as $contentType) {
163
$this->_header('Content-Type: ' . $contentType);
165
$this->_header(array(
166
'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
168
'Accept-Ranges: bytes',
169
'Cache-Control: private' => false,
172
$httpRange = env('HTTP_RANGE');
173
if (isset($httpRange)) {
174
list($toss, $range) = explode('=', $httpRange);
176
$size = $fileSize - 1;
177
$length = $fileSize - $range;
179
$this->_header(array(
180
'HTTP/1.1 206 Partial Content',
181
'Content-Length: ' . $length,
182
'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
184
fseek($handle, $range);
186
$this->_header('Content-Length: ' . $fileSize);
189
$this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
191
if (!is_numeric($cache)) {
192
$cache = strtotime($cache) - time();
194
$this->_header(array(
195
'Cache-Control: max-age=' . $cache,
196
'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
199
$this->_header(array(
200
'Cache-Control: must-revalidate, post-check=0, pre-check=0',
201
'Pragma: no-cache'));
203
$this->_header(array(
204
'Last-Modified: ' . $modified,
205
'Content-Type: ' . $this->mimeType[$extension],
206
'Content-Length: ' . $fileSize));
209
$this->_clearBuffer();
211
while (!feof($handle)) {
212
if (!$this->_isActive()) {
217
$buffer = fread($handle, $chunkSize);
219
$this->_flushBuffer();
228
* Method to set headers
229
* @param mixed $header
230
* @param boolean $boolean
233
function _header($header, $boolean = true) {
234
if (is_array($header)) {
235
foreach ($header as $string => $boolean) {
236
if (is_numeric($string)) {
237
$this->_headers[] = array($boolean => true);
239
$this->_headers[] = array($string => $boolean);
244
$this->_headers[] = array($header => $boolean);
249
* Method to output headers
253
foreach ($this->_headers as $key => $value) {
254
$header = key($value);
255
header($header, $value[$header]);
260
* Returns true if connection is still active
264
function _isActive() {
265
return connection_status() == 0 && !connection_aborted();
269
* Clears the contents of the topmost output buffer and discards them
273
function _clearBuffer() {
274
return @ob_end_clean();
278
* Flushes the contents of the output buffer
281
function _flushBuffer() {