PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/media.php

https://github.com/t73biz/BaseApp
PHP | 256 lines | 185 code | 21 blank | 50 comment | 30 complexity | cfd954537972b1c561b2cafd95278a5a MD5 | raw file
  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. class MediaView extends View {
  22. /**
  23. * Holds known mime type mappings
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. var $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. var $_headers = array();
  89. /**
  90. * Constructor
  91. *
  92. * @param object $controller
  93. */
  94. function __construct(&$controller = null) {
  95. parent::__construct($controller);
  96. }
  97. /**
  98. * Display or download the given file
  99. *
  100. * @return unknown
  101. */
  102. function render() {
  103. $name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
  104. extract($this->viewVars, EXTR_OVERWRITE);
  105. if ($size) {
  106. $id = $id . '_' . $size;
  107. }
  108. if (is_dir($path)) {
  109. $path = $path . $id;
  110. } else {
  111. $path = APP . $path . $id;
  112. }
  113. if (!file_exists($path)) {
  114. header('Content-Type: text/html');
  115. $this->cakeError('error404');
  116. }
  117. if (is_null($name)) {
  118. $name = $id;
  119. }
  120. if (is_array($mimeType)) {
  121. $this->mimeType = array_merge($this->mimeType, $mimeType);
  122. }
  123. if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
  124. $chunkSize = 8192;
  125. $buffer = '';
  126. $fileSize = @filesize($path);
  127. $handle = fopen($path, 'rb');
  128. if ($handle === false) {
  129. return false;
  130. }
  131. if (!empty($modified)) {
  132. $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
  133. } else {
  134. $modified = gmdate('D, d M Y H:i:s') . ' GMT';
  135. }
  136. if ($download) {
  137. $contentTypes = array('application/octet-stream');
  138. $agent = env('HTTP_USER_AGENT');
  139. if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
  140. $contentTypes[0] = 'application/octetstream';
  141. } else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
  142. $contentTypes[0] = 'application/force-download';
  143. array_merge($contentTypes, array(
  144. 'application/octet-stream',
  145. 'application/download'
  146. ));
  147. }
  148. foreach($contentTypes as $contentType) {
  149. $this->_header('Content-Type: ' . $contentType);
  150. }
  151. $this->_header(array(
  152. 'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
  153. 'Expires: 0',
  154. 'Accept-Ranges: bytes',
  155. 'Cache-Control: private' => false,
  156. 'Pragma: private'));
  157. $httpRange = env('HTTP_RANGE');
  158. if (isset($httpRange)) {
  159. list($toss, $range) = explode('=', $httpRange);
  160. $size = $fileSize - 1;
  161. $length = $fileSize - $range;
  162. $this->_header(array(
  163. 'HTTP/1.1 206 Partial Content',
  164. 'Content-Length: ' . $length,
  165. 'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
  166. fseek($handle, $range);
  167. } else {
  168. $this->_header('Content-Length: ' . $fileSize);
  169. }
  170. } else {
  171. $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
  172. if ($cache) {
  173. if (!is_numeric($cache)) {
  174. $cache = strtotime($cache) - time();
  175. }
  176. $this->_header(array(
  177. 'Cache-Control: max-age=' . $cache,
  178. 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
  179. 'Pragma: cache'));
  180. } else {
  181. $this->_header(array(
  182. 'Cache-Control: must-revalidate, post-check=0, pre-check=0',
  183. 'Pragma: no-cache'));
  184. }
  185. $this->_header(array(
  186. 'Last-Modified: ' . $modified,
  187. 'Content-Type: ' . $this->mimeType[$extension],
  188. 'Content-Length: ' . $fileSize));
  189. }
  190. $this->_output();
  191. @ob_end_clean();
  192. while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
  193. set_time_limit(0);
  194. $buffer = fread($handle, $chunkSize);
  195. echo $buffer;
  196. @flush();
  197. @ob_flush();
  198. }
  199. fclose($handle);
  200. exit(0);
  201. }
  202. return false;
  203. }
  204. /**
  205. * Method to set headers
  206. * @param mixed $header
  207. * @param boolean $boolean
  208. * @access protected
  209. */
  210. function _header($header, $boolean = true) {
  211. if (is_array($header)) {
  212. foreach ($header as $string => $boolean) {
  213. if (is_numeric($string)) {
  214. $this->_headers[] = array($boolean => true);
  215. } else {
  216. $this->_headers[] = array($string => $boolean);
  217. }
  218. }
  219. return;
  220. }
  221. $this->_headers[] = array($header => $boolean);
  222. return;
  223. }
  224. /**
  225. * Method to output headers
  226. * @access protected
  227. */
  228. function _output() {
  229. foreach ($this->_headers as $key => $value) {
  230. $header = key($value);
  231. header($header, $value[$header]);
  232. }
  233. }
  234. }
  235. ?>