~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/view/media.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Methods to display or download any type of file
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
8
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP(tm) Project
 
15
 * @package       cake
 
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)
 
19
 */
 
20
App::import('View', 'View', false);
 
21
 
 
22
class MediaView extends View {
 
23
 
 
24
/**
 
25
 * Holds known mime type mappings
 
26
 *
 
27
 * @var array
 
28
 * @access public
 
29
 */
 
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');
 
84
 
 
85
/**
 
86
 * Holds headers sent to browser before rendering media
 
87
 *
 
88
 * @var array
 
89
 * @access protected
 
90
 */
 
91
        var $_headers = array();
 
92
 
 
93
/**
 
94
 * Constructor
 
95
 *
 
96
 * @param object $controller
 
97
 */
 
98
        function __construct(&$controller) {
 
99
                parent::__construct($controller);
 
100
        }
 
101
 
 
102
/**
 
103
 * Display or download the given file
 
104
 *
 
105
 * @return unknown
 
106
 */
 
107
        function render() {
 
108
                $name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
 
109
                extract($this->viewVars, EXTR_OVERWRITE);
 
110
 
 
111
                if ($size) {
 
112
                        $id = $id . '_' . $size;
 
113
                }
 
114
 
 
115
                if (is_dir($path)) {
 
116
                        $path = $path . $id;
 
117
                } else {
 
118
                        $path = APP . $path . $id;
 
119
                }
 
120
 
 
121
                if (!file_exists($path)) {
 
122
                        header('Content-Type: text/html');
 
123
                        $this->cakeError('error404');
 
124
                }
 
125
 
 
126
                if (is_null($name)) {
 
127
                        $name = $id;
 
128
                }
 
129
 
 
130
                if (is_array($mimeType)) {
 
131
                        $this->mimeType = array_merge($this->mimeType, $mimeType);
 
132
                }
 
133
 
 
134
                if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
 
135
                        $chunkSize = 8192;
 
136
                        $buffer = '';
 
137
                        $fileSize = @filesize($path);
 
138
                        $handle = fopen($path, 'rb');
 
139
 
 
140
                        if ($handle === false) {
 
141
                                return false;
 
142
                        }
 
143
                        if (!empty($modified)) {
 
144
                                $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
 
145
                        } else {
 
146
                                $modified = gmdate('D, d M Y H:i:s') . ' GMT';
 
147
                        }
 
148
 
 
149
                        if ($download) {
 
150
                                $contentTypes = array('application/octet-stream');
 
151
                                $agent = env('HTTP_USER_AGENT');
 
152
 
 
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'
 
160
                                        ));
 
161
                                }
 
162
                                foreach($contentTypes as $contentType) {
 
163
                                        $this->_header('Content-Type: ' . $contentType);
 
164
                                }
 
165
                                $this->_header(array(
 
166
                                        'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
 
167
                                        'Expires: 0',
 
168
                                        'Accept-Ranges: bytes',
 
169
                                        'Cache-Control: private' => false,
 
170
                                        'Pragma: private'));
 
171
 
 
172
                                $httpRange = env('HTTP_RANGE');
 
173
                                if (isset($httpRange)) {
 
174
                                        list($toss, $range) = explode('=', $httpRange);
 
175
 
 
176
                                        $size = $fileSize - 1;
 
177
                                        $length = $fileSize - $range;
 
178
 
 
179
                                        $this->_header(array(
 
180
                                                'HTTP/1.1 206 Partial Content',
 
181
                                                'Content-Length: ' . $length,
 
182
                                                'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
 
183
 
 
184
                                        fseek($handle, $range);
 
185
                                } else {
 
186
                                        $this->_header('Content-Length: ' . $fileSize);
 
187
                                }
 
188
                        } else {
 
189
                                $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
 
190
                                if ($cache) {
 
191
                                        if (!is_numeric($cache)) {
 
192
                                                $cache = strtotime($cache) - time();
 
193
                                        }
 
194
                                        $this->_header(array(
 
195
                                                'Cache-Control: max-age=' . $cache,
 
196
                                                'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
 
197
                                                'Pragma: cache'));
 
198
                                } else {
 
199
                                        $this->_header(array(
 
200
                                                'Cache-Control: must-revalidate, post-check=0, pre-check=0',
 
201
                                                'Pragma: no-cache'));
 
202
                                }
 
203
                                $this->_header(array(
 
204
                                        'Last-Modified: ' . $modified,
 
205
                                        'Content-Type: ' . $this->mimeType[$extension],
 
206
                                        'Content-Length: ' . $fileSize));
 
207
                        }
 
208
                        $this->_output();
 
209
                        $this->_clearBuffer();
 
210
 
 
211
                        while (!feof($handle)) {
 
212
                                if (!$this->_isActive()) {
 
213
                                        fclose($handle);
 
214
                                        return false;
 
215
                                }
 
216
                                set_time_limit(0);
 
217
                                $buffer = fread($handle, $chunkSize);
 
218
                                echo $buffer;
 
219
                                $this->_flushBuffer();
 
220
                        }
 
221
                        fclose($handle);
 
222
                        return;
 
223
                }
 
224
                return false;
 
225
        }
 
226
 
 
227
/**
 
228
 * Method to set headers
 
229
 * @param mixed $header
 
230
 * @param boolean $boolean
 
231
 * @access protected
 
232
 */
 
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);
 
238
                                } else {
 
239
                                        $this->_headers[] = array($string => $boolean);
 
240
                                }
 
241
                        }
 
242
                        return;
 
243
                }
 
244
                $this->_headers[] = array($header => $boolean);
 
245
                return;
 
246
        }
 
247
 
 
248
/**
 
249
 * Method to output headers
 
250
 * @access protected
 
251
 */
 
252
        function _output() {
 
253
                foreach ($this->_headers as $key => $value) {
 
254
                        $header = key($value);
 
255
                        header($header, $value[$header]);
 
256
                }
 
257
        }
 
258
 
 
259
/**
 
260
 * Returns true if connection is still active
 
261
 * @return boolean
 
262
 * @access protected
 
263
 */
 
264
        function _isActive() {
 
265
                return connection_status() == 0 && !connection_aborted();
 
266
        }
 
267
 
 
268
/**
 
269
 * Clears the contents of the topmost output buffer and discards them
 
270
 * @return boolean
 
271
 * @access protected
 
272
 */
 
273
        function _clearBuffer() {
 
274
                return @ob_end_clean();
 
275
        }
 
276
 
 
277
/**
 
278
 * Flushes the contents of the output buffer
 
279
 * @access protected
 
280
 */
 
281
        function _flushBuffer() {
 
282
                @flush();
 
283
                @ob_flush();
 
284
        }
 
285
}