media.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: media.php 8101 2009-03-15 00:19:02Z phpnut $ */
00003 /**
00004  * Methods to display or download any type of file
00005  *
00006  * PHP versions 4 and 5
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00009  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00010  *
00011  * Licensed under The MIT License
00012  * Redistributions of files must retain the above copyright notice.
00013  *
00014  * @filesource
00015  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00016  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00017  * @package       cake
00018  * @subpackage    cake.cake.libs.view
00019  * @since         CakePHP(tm) v 1.2.0.5714
00020  * @version       $Revision: 8101 $
00021  * @modifiedby    $LastChangedBy: phpnut $
00022  * @lastmodified  $Date: 2009-03-14 20:19:02 -0400 (Sat, 14 Mar 2009) $
00023  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00024  */
00025 class MediaView extends View {
00026 /**
00027  * Holds known mime type mappings
00028  *
00029  * @var array
00030  * @access public
00031  */
00032     var $mimeType = array('ai' => 'application/postscript', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream',
00033                                 'ccad' => 'application/clariscad', 'cdf' => 'application/x-netcdf', 'class' => 'application/octet-stream',
00034                                 'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh',
00035                                 'csv' => 'application/csv', 'dcr' => 'application/x-director', 'dir' => 'application/x-director',
00036                                 'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'drw' => 'application/drafting',
00037                                 'dvi' => 'application/x-dvi', 'dwg' => 'application/acad', 'dxf' => 'application/dxf', 'dxr' => 'application/x-director',
00038                                 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
00039                                 'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
00040                                 'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
00041                                 'hqx' => 'application/mac-binhex40', 'ips' => 'application/x-ipscript', 'ipx' => 'application/x-ipix',
00042                                 'js' => 'application/x-javascript', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream',
00043                                 'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream', 'man' => 'application/x-troff-man',
00044                                 'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif', 'ms' => 'application/x-troff-ms',
00045                                 'nc' => 'application/x-netcdf', 'oda' => 'application/oda', 'pdf' => 'application/pdf',
00046                                 'pgn' => 'application/x-chess-pgn', 'pot' => 'application/mspowerpoint', 'pps' => 'application/mspowerpoint',
00047                                 'ppt' => 'application/mspowerpoint', 'ppz' => 'application/mspowerpoint', 'pre' => 'application/x-freelance',
00048                                 'prt' => 'application/pro_eng', 'ps' => 'application/postscript', 'roff' => 'application/x-troff',
00049                                 'scm' => 'application/x-lotusscreencam', 'set' => 'application/set', 'sh' => 'application/x-sh',
00050                                 'shar' => 'application/x-shar', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan',
00051                                 'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan',
00052                                 'smi' => 'application/smil', 'smil' => 'application/smil', 'sol' => 'application/solids',
00053                                 'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'step' => 'application/STEP',
00054                                 'stl' => 'application/SLA', 'stp' => 'application/STEP', 'sv4cpio' => 'application/x-sv4cpio',
00055                                 'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
00056                                 'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff',
00057                                 'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
00058                                 'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tr' => 'application/x-troff',
00059                                 'tsp' => 'application/dsptype', 'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
00060                                 'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
00061                                 'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
00062                                 'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
00063                                 'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
00064                                 'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
00065                                 'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
00066                                 'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
00067                                 'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
00068                                 'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
00069                                 'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
00070                                 'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
00071                                 'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
00072                                 'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
00073                                 'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
00074                                 'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
00075                                 'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
00076                                 'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
00077                                 'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
00078                                 'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
00079                                 'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
00080                                 'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
00081                                 'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb');
00082 /**
00083  * Holds headers sent to browser before rendering media
00084  *
00085  * @var array
00086  * @access protected
00087  */
00088     var $_headers = array();
00089 /**
00090  * Constructor
00091  *
00092  * @param object $controller
00093  */
00094     function __construct(&$controller) {
00095         parent::__construct($controller);
00096     }
00097 /**
00098  * Display or download the given file
00099  *
00100  * @return unknown
00101  */
00102     function render() {
00103         $name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
00104         extract($this->viewVars, EXTR_OVERWRITE);
00105 
00106         if ($size) {
00107             $id = $id . '_' . $size;
00108         }
00109 
00110         if (is_dir($path)) {
00111             $path = $path . $id;
00112         } else {
00113             $path = APP . $path . $id;
00114         }
00115 
00116         if (!file_exists($path)) {
00117             header('Content-Type: text/html');
00118             $this->cakeError('error404');
00119         }
00120 
00121         if (is_null($name)) {
00122             $name = $id;
00123         }
00124 
00125         if (is_array($mimeType)) {
00126             $this->mimeType = array_merge($this->mimeType, $mimeType);
00127         }
00128 
00129         if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
00130             $chunkSize = 8192;
00131             $buffer = '';
00132             $fileSize = @filesize($path);
00133             $handle = fopen($path, 'rb');
00134 
00135             if ($handle === false) {
00136                 return false;
00137             }
00138             if (!empty($modified)) {
00139                 $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
00140             } else {
00141                 $modified = gmdate('D, d M Y H:i:s') . ' GMT';
00142             }
00143 
00144             if ($download) {
00145                 $contentTypes = array('application/octet-stream');
00146                 $agent = env('HTTP_USER_AGENT');
00147 
00148                 if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
00149                     $contentTypes[0] = 'application/octetstream';
00150                 } else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
00151                     $contentTypes[0] = 'application/force-download';
00152                     array_push($contentTypes, array(
00153                         'application/octet-stream',
00154                         'application/download'
00155                     ));
00156                 }
00157                 foreach($contentTypes as $contentType) {
00158                     $this->_header('Content-Type: ' . $contentType);
00159                 }
00160                 $this->_header(array(
00161                     'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
00162                     'Expires: 0',
00163                     'Accept-Ranges: bytes',
00164                     'Cache-Control: private' => false,
00165                     'Pragma: private'));
00166 
00167                 $httpRange = env('HTTP_RANGE');
00168                 if (isset($httpRange)) {
00169                     list($toss, $range) = explode('=', $httpRange);
00170 
00171                     $size = $fileSize - 1;
00172                     $length = $fileSize - $range;
00173 
00174                     $this->_header(array(
00175                         'HTTP/1.1 206 Partial Content',
00176                         'Content-Length: ' . $length,
00177                         'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
00178 
00179                     fseek($handle, $range);
00180                 } else {
00181                     $this->_header('Content-Length: ' . $fileSize);
00182                 }
00183             } else {
00184                 $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
00185                 if ($cache) {
00186                     if (!is_numeric($cache)) {
00187                         $cache = strtotime($cache) - time();
00188                     }
00189                     $this->_header(array(
00190                         'Cache-Control: max-age=' . $cache,
00191                         'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
00192                         'Pragma: cache'));
00193                 } else {
00194                     $this->_header(array(
00195                         'Cache-Control: must-revalidate, post-check=0, pre-check=0',
00196                         'Pragma: no-cache'));
00197                 }
00198                 $this->_header(array(
00199                     'Last-Modified: ' . $modified,
00200                     'Content-Type: ' . $this->mimeType[$extension],
00201                     'Content-Length: ' . $fileSize));
00202             }
00203             $this->_output();
00204             @ob_end_clean();
00205 
00206             while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
00207                 set_time_limit(0);
00208                 $buffer = fread($handle, $chunkSize);
00209                 echo $buffer;
00210                 @flush();
00211                 @ob_flush();
00212             }
00213             fclose($handle);
00214             exit(0);
00215         }
00216         return false;
00217     }
00218 /**
00219  * Method to set headers
00220  * @param mixed $header
00221  * @param boolean $boolean
00222  * @access protected
00223  */
00224     function _header($header, $boolean = true) {
00225         if (is_array($header)) {
00226             foreach ($header as $string => $boolean) {
00227                 if (is_numeric($string)) {
00228                     $this->_headers[] = array($boolean => true);
00229                 } else {
00230                     $this->_headers[] = array($string => $boolean);
00231                 }
00232             }
00233             return;
00234         }
00235         $this->_headers[] = array($header => $boolean);
00236         return;
00237     }
00238 /**
00239  * Method to output headers
00240  * @access protected
00241  */
00242     function _output() {
00243         foreach ($this->_headers as $key => $value) {
00244             $header = key($value);
00245             header($header, $value[$header]);
00246         }
00247     }
00248 }
00249 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7