1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class MediaView extends View {
25: 26: 27: 28: 29: 30:
31: var $mimeType = array('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', 'dxr' => 'application/x-director',
37: 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
38: 'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
39: 'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
40: 'hqx' => 'application/mac-binhex40', 'ips' => 'application/x-ipscript', 'ipx' => 'application/x-ipix',
41: 'js' => 'application/x-javascript', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream',
42: 'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream', 'man' => 'application/x-troff-man',
43: 'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif', 'ms' => 'application/x-troff-ms',
44: 'nc' => 'application/x-netcdf', 'oda' => 'application/oda', 'pdf' => 'application/pdf',
45: 'pgn' => 'application/x-chess-pgn', 'pot' => 'application/mspowerpoint', 'pps' => 'application/mspowerpoint',
46: 'ppt' => 'application/mspowerpoint', 'ppz' => 'application/mspowerpoint', 'pre' => 'application/x-freelance',
47: 'prt' => 'application/pro_eng', 'ps' => 'application/postscript', 'roff' => 'application/x-troff',
48: 'scm' => 'application/x-lotusscreencam', 'set' => 'application/set', 'sh' => 'application/x-sh',
49: 'shar' => 'application/x-shar', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan',
50: 'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan',
51: 'smi' => 'application/smil', 'smil' => 'application/smil', 'sol' => 'application/solids',
52: 'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'step' => 'application/STEP',
53: 'stl' => 'application/SLA', 'stp' => 'application/STEP', 'sv4cpio' => 'application/x-sv4cpio',
54: 'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
55: 'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff',
56: 'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
57: 'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tr' => 'application/x-troff',
58: 'tsp' => 'application/dsptype', 'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
59: 'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
60: 'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
61: 'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
62: 'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
63: 'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
64: 'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
65: 'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
66: 'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
67: 'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
68: 'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
69: 'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
70: 'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
71: 'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
72: 'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
73: 'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
74: 'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
75: 'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
76: 'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
77: 'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
78: 'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
79: 'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
80: 'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb');
81: 82: 83: 84: 85: 86:
87: var $_headers = array();
88: 89: 90: 91: 92:
93: function __construct(&$controller) {
94: parent::__construct($controller);
95: }
96: 97: 98: 99: 100:
101: function render() {
102: $name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
103: extract($this->viewVars, EXTR_OVERWRITE);
104:
105: if ($size) {
106: $id = $id . '_' . $size;
107: }
108:
109: if (is_dir($path)) {
110: $path = $path . $id;
111: } else {
112: $path = APP . $path . $id;
113: }
114:
115: if (!file_exists($path)) {
116: header('Content-Type: text/html');
117: $this->cakeError('error404');
118: }
119:
120: if (is_null($name)) {
121: $name = $id;
122: }
123:
124: if (is_array($mimeType)) {
125: $this->mimeType = array_merge($this->mimeType, $mimeType);
126: }
127:
128: if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
129: $chunkSize = 8192;
130: $buffer = '';
131: $fileSize = @filesize($path);
132: $handle = fopen($path, 'rb');
133:
134: if ($handle === false) {
135: return false;
136: }
137: if (!empty($modified)) {
138: $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
139: } else {
140: $modified = gmdate('D, d M Y H:i:s') . ' GMT';
141: }
142:
143: if ($download) {
144: $contentTypes = array('application/octet-stream');
145: $agent = env('HTTP_USER_AGENT');
146:
147: if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
148: $contentTypes[0] = 'application/octetstream';
149: } else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
150: $contentTypes[0] = 'application/force-download';
151: array_merge($contentTypes, array(
152: 'application/octet-stream',
153: 'application/download'
154: ));
155: }
156: foreach($contentTypes as $contentType) {
157: $this->_header('Content-Type: ' . $contentType);
158: }
159: $this->_header(array(
160: 'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
161: 'Expires: 0',
162: 'Accept-Ranges: bytes',
163: 'Cache-Control: private' => false,
164: 'Pragma: private'));
165:
166: $httpRange = env('HTTP_RANGE');
167: if (isset($httpRange)) {
168: list($toss, $range) = explode('=', $httpRange);
169:
170: $size = $fileSize - 1;
171: $length = $fileSize - $range;
172:
173: $this->_header(array(
174: 'HTTP/1.1 206 Partial Content',
175: 'Content-Length: ' . $length,
176: 'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
177:
178: fseek($handle, $range);
179: } else {
180: $this->_header('Content-Length: ' . $fileSize);
181: }
182: } else {
183: $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
184: if ($cache) {
185: if (!is_numeric($cache)) {
186: $cache = strtotime($cache) - time();
187: }
188: $this->_header(array(
189: 'Cache-Control: max-age=' . $cache,
190: 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
191: 'Pragma: cache'));
192: } else {
193: $this->_header(array(
194: 'Cache-Control: must-revalidate, post-check=0, pre-check=0',
195: 'Pragma: no-cache'));
196: }
197: $this->_header(array(
198: 'Last-Modified: ' . $modified,
199: 'Content-Type: ' . $this->mimeType[$extension],
200: 'Content-Length: ' . $fileSize));
201: }
202: $this->_output();
203: @ob_end_clean();
204:
205: while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
206: set_time_limit(0);
207: $buffer = fread($handle, $chunkSize);
208: echo $buffer;
209: @flush();
210: @ob_flush();
211: }
212: fclose($handle);
213: exit(0);
214: }
215: return false;
216: }
217: 218: 219: 220: 221: 222:
223: function _header($header, $boolean = true) {
224: if (is_array($header)) {
225: foreach ($header as $string => $boolean) {
226: if (is_numeric($string)) {
227: $this->_headers[] = array($boolean => true);
228: } else {
229: $this->_headers[] = array($string => $boolean);
230: }
231: }
232: return;
233: }
234: $this->_headers[] = array($header => $boolean);
235: return;
236: }
237: 238: 239: 240:
241: function _output() {
242: foreach ($this->_headers as $key => $value) {
243: $header = key($value);
244: header($header, $value[$header]);
245: }
246: }
247: }
248: ?>