PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/view/media.php

https://github.com/bb-dev/cakephp2x
PHP | 258 lines | 185 code | 21 blank | 52 comment | 30 complexity | 2f4610e7045a1ef32f4ea612cab3f849 MD5 | raw file
  1. <?php
  2. /**
  3. * Methods to display or download any type of file
  4. *
  5. * PHP Version 5.x
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2009, 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-2009, 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. class MediaView extends View {
  22. /**
  23. * Holds known mime type mappings
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $mimeType = array(
  29. 'ai' => 'application/postscript', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream',
  30. 'ccad' => 'application/clariscad', 'cdf' => 'application/x-netcdf', 'class' => 'application/octet-stream',
  31. 'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh',
  32. 'csv' => 'application/csv', 'dcr' => 'application/x-director', 'dir' => 'application/x-director',
  33. 'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'drw' => 'application/drafting',
  34. 'dvi' => 'application/x-dvi', 'dwg' => 'application/acad', 'dxf' => 'application/dxf',
  35. 'dxr' => 'application/x-director', 'eot' => 'application/vnd.ms-fontobject', 'eps' => 'application/postscript',
  36. 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
  37. 'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
  38. 'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
  39. 'hqx' => 'application/mac-binhex40', 'ico' => 'image/vnd.microsoft.icon', 'ips' => 'application/x-ipscript',
  40. 'ipx' => 'application/x-ipix', 'js' => 'application/x-javascript', 'latex' => 'application/x-latex',
  41. 'lha' => 'application/octet-stream', 'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream',
  42. 'man' => 'application/x-troff-man', 'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif',
  43. 'ms' => 'application/x-troff-ms', 'nc' => 'application/x-netcdf', 'oda' => 'application/oda',
  44. 'otf' => 'font/otf', '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', 'ttf' => 'font/ttf',
  59. '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. * Holds headers sent to browser before rendering media
  84. *
  85. * @var array
  86. * @access protected
  87. */
  88. protected $_headers = array();
  89. /**
  90. * Constructor
  91. *
  92. * @param object $controller
  93. * @access public
  94. */
  95. public function __construct($controller = null) {
  96. parent::__construct($controller);
  97. }
  98. /**
  99. * Display or download the given file
  100. *
  101. * @return unknown
  102. * @access public
  103. */
  104. public function render() {
  105. $name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
  106. extract($this->viewVars, EXTR_OVERWRITE);
  107. if ($size) {
  108. $id = $id . '_' . $size;
  109. }
  110. if (is_dir($path)) {
  111. $path = $path . $id;
  112. } else {
  113. $path = APP . $path . $id;
  114. }
  115. if (!file_exists($path)) {
  116. header('Content-Type: text/html');
  117. $this->cakeError('error404');
  118. }
  119. if (is_null($name)) {
  120. $name = $id;
  121. }
  122. if (is_array($mimeType)) {
  123. $this->mimeType = array_merge($this->mimeType, $mimeType);
  124. }
  125. if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
  126. $chunkSize = 8192;
  127. $buffer = '';
  128. $fileSize = @filesize($path);
  129. $handle = fopen($path, 'rb');
  130. if ($handle === false) {
  131. return false;
  132. }
  133. if (!empty($modified)) {
  134. $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
  135. } else {
  136. $modified = gmdate('D, d M Y H:i:s') . ' GMT';
  137. }
  138. if ($download) {
  139. $contentTypes = array('application/octet-stream');
  140. $agent = env('HTTP_USER_AGENT');
  141. if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
  142. $contentTypes[0] = 'application/octetstream';
  143. } else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  144. $contentTypes[0] = 'application/force-download';
  145. array_push($contentTypes, array(
  146. 'application/octet-stream',
  147. 'application/download'
  148. ));
  149. }
  150. foreach($contentTypes as $contentType) {
  151. $this->_header('Content-Type: ' . $contentType);
  152. }
  153. $this->_header(array(
  154. 'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
  155. 'Expires: 0',
  156. 'Accept-Ranges: bytes',
  157. 'Cache-Control: private' => false,
  158. 'Pragma: private'));
  159. $httpRange = env('HTTP_RANGE');
  160. if (isset($httpRange)) {
  161. list($toss, $range) = explode('=', $httpRange);
  162. $size = $fileSize - 1;
  163. $length = $fileSize - $range;
  164. $this->_header(array(
  165. 'HTTP/1.1 206 Partial Content',
  166. 'Content-Length: ' . $length,
  167. 'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
  168. fseek($handle, $range);
  169. } else {
  170. $this->_header('Content-Length: ' . $fileSize);
  171. }
  172. } else {
  173. $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
  174. if ($cache) {
  175. if (!is_numeric($cache)) {
  176. $cache = strtotime($cache) - time();
  177. }
  178. $this->_header(array(
  179. 'Cache-Control: max-age=' . $cache,
  180. 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
  181. 'Pragma: cache'));
  182. } else {
  183. $this->_header(array(
  184. 'Cache-Control: must-revalidate, post-check=0, pre-check=0',
  185. 'Pragma: no-cache'));
  186. }
  187. $this->_header(array(
  188. 'Last-Modified: ' . $modified,
  189. 'Content-Type: ' . $this->mimeType[$extension],
  190. 'Content-Length: ' . $fileSize));
  191. }
  192. $this->_output();
  193. @ob_end_clean();
  194. while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
  195. set_time_limit(0);
  196. $buffer = fread($handle, $chunkSize);
  197. echo $buffer;
  198. @flush();
  199. @ob_flush();
  200. }
  201. fclose($handle);
  202. exit(0);
  203. }
  204. return false;
  205. }
  206. /**
  207. * Method to set headers
  208. * @param mixed $header
  209. * @param boolean $boolean
  210. * @access protected
  211. */
  212. protected function _header($header, $boolean = true) {
  213. if (is_array($header)) {
  214. foreach ($header as $string => $boolean) {
  215. if (is_numeric($string)) {
  216. $this->_headers[] = array($boolean => true);
  217. } else {
  218. $this->_headers[] = array($string => $boolean);
  219. }
  220. }
  221. return;
  222. }
  223. $this->_headers[] = array($header => $boolean);
  224. return;
  225. }
  226. /**
  227. * Method to output headers
  228. * @access protected
  229. */
  230. protected function _output() {
  231. foreach ($this->_headers as $key => $value) {
  232. $header = key($value);
  233. header($header, $value[$header]);
  234. }
  235. }
  236. }
  237. ?>