Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: media.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Methods to display or download any type of file
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
9
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
10
 *
11
 * Licensed under The MIT License
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @filesource
15
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
16
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
17
 * @package       cake
18
 * @subpackage    cake.cake.libs.view
19
 * @since         CakePHP(tm) v 1.2.0.5714
20
 * @version       $Revision: 7945 $
21
 * @modifiedby    $LastChangedBy: gwoo $
22
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
23
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
24
 */
25
class MediaView extends View {
26
/**
27
 * Holds known mime type mappings
28
 *
29
 * @var array
30
 * @access public
31
 */
32
	var $mimeType = array('ai' => 'application/postscript', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream',
33
								'ccad' => 'application/clariscad', 'cdf' => 'application/x-netcdf', 'class' => 'application/octet-stream',
34
								'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh',
35
								'csv' => 'application/csv', 'dcr' => 'application/x-director', 'dir' => 'application/x-director',
36
								'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'drw' => 'application/drafting',
37
								'dvi' => 'application/x-dvi', 'dwg' => 'application/acad', 'dxf' => 'application/dxf', 'dxr' => 'application/x-director',
38
								'eps' => 'application/postscript', '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', 'ips' => 'application/x-ipscript', 'ipx' => 'application/x-ipix',
42
								'js' => 'application/x-javascript', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream',
43
								'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream', 'man' => 'application/x-troff-man',
44
								'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif', 'ms' => 'application/x-troff-ms',
45
								'nc' => 'application/x-netcdf', 'oda' => 'application/oda', 'pdf' => 'application/pdf',
46
								'pgn' => 'application/x-chess-pgn', 'pot' => 'application/mspowerpoint', 'pps' => 'application/mspowerpoint',
47
								'ppt' => 'application/mspowerpoint', 'ppz' => 'application/mspowerpoint', 'pre' => 'application/x-freelance',
48
								'prt' => 'application/pro_eng', 'ps' => 'application/postscript', 'roff' => 'application/x-troff',
49
								'scm' => 'application/x-lotusscreencam', 'set' => 'application/set', 'sh' => 'application/x-sh',
50
								'shar' => 'application/x-shar', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan',
51
								'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan',
52
								'smi' => 'application/smil', 'smil' => 'application/smil', 'sol' => 'application/solids',
53
								'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'step' => 'application/STEP',
54
								'stl' => 'application/SLA', 'stp' => 'application/STEP', 'sv4cpio' => 'application/x-sv4cpio',
55
								'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
56
								'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff',
57
								'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
58
								'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tr' => 'application/x-troff',
59
								'tsp' => 'application/dsptype', 'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
60
								'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
61
								'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
62
								'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
63
								'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
64
								'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
65
								'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
66
								'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
67
								'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
68
								'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
69
								'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
70
								'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
71
								'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
72
								'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
73
								'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
74
								'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
75
								'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
76
								'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
77
								'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
78
								'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
79
								'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
80
								'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
81
								'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb');
82
/**
83
 * Constructor
84
 *
85
 * @param object $controller
86
 */
87
	function __construct(&$controller) {
88
		parent::__construct($controller);
89
	}
90
/**
91
 * Display or download the given file
92
 *
93
 * @return unknown
94
 */
95
	function render() {
96
		$name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
97
		extract($this->viewVars, EXTR_OVERWRITE);
98
 
99
		if ($size) {
100
			$id = $id . '_' . $size;
101
		}
102
 
103
		if (is_dir($path)) {
104
			$path = $path . $id;
105
		} else {
106
			$path = APP . $path . $id;
107
		}
108
 
109
		if (is_null($name)) {
110
			$name = $id;
111
		}
112
 
113
		if (is_array($mimeType)) {
114
			$this->mimeType = array_merge($this->mimeType, $mimeType);
115
		}
116
 
117
		if (file_exists($path) && isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
118
			$chunkSize = 8192;
119
			$buffer = '';
120
			$fileSize = @filesize($path);
121
			$handle = fopen($path, 'rb');
122
 
123
			if ($handle === false) {
124
				return false;
125
			}
126
			if (!empty($modified)) {
127
				$modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
128
			} else {
129
				$modified = gmdate('D, d M Y H:i:s') . ' GMT';
130
			}
131
 
132
			if ($download) {
133
				$contentType = 'application/octet-stream';
134
				$agent = env('HTTP_USER_AGENT');
135
 
136
				if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
137
					$contentType = 'application/octetstream';
138
				}
139
				header('Content-Type: ' . $contentType);
140
				header('Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";');
141
				header('Expires: 0');
142
				header('Accept-Ranges: bytes');
143
				header('Cache-Control: private', false);
144
				header('Pragma: private');
145
 
146
				$httpRange = env('HTTP_RANGE');
147
				if (isset($httpRange)) {
148
					list($toss, $range) = explode('=', $httpRange);
149
 
150
					$size = $fileSize - 1;
151
					$length = $fileSize - $range;
152
 
153
					header('HTTP/1.1 206 Partial Content');
154
					header('Content-Length: ' . $length);
155
					header('Content-Range: bytes ' . $range . $size . '/' . $fileSize);
156
					fseek($handle, $range);
157
				} else {
158
					header('Content-Length: ' . $fileSize);
159
				}
160
			} else {
161
				header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
162
				if ($cache) {
163
					if (!is_numeric($cache)) {
164
						$cache = strtotime($cache) - time();
165
					}
166
					header('Cache-Control: max-age=' . $cache);
167
					header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT');
168
					header('Pragma: cache');
169
				} else {
170
					header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
171
					header('Pragma: no-cache');
172
				}
173
				header('Last-Modified: ' . $modified);
174
				header('Content-Type: ' . $this->mimeType[$extension]);
175
				header('Content-Length: ' . $fileSize);
176
			}
177
			@ob_end_clean();
178
 
179
			while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
180
				set_time_limit(0);
181
				$buffer = fread($handle, $chunkSize);
182
				echo $buffer;
183
				@flush();
184
				@ob_flush();
185
			}
186
			fclose($handle);
187
			exit(0);
188
		}
189
		return false;
190
	}
191
}
192
?>