CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  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://cakephp.org)
  9:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  *
 11:  * Licensed under The MIT License
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       cake
 17:  * @subpackage    cake.cake.libs.view
 18:  * @since         CakePHP(tm) v 1.2.0.5714
 19:  * @version       $Revision$
 20:  * @modifiedby    $LastChangedBy$
 21:  * @lastmodified  $Date$
 22:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 23:  */
 24: class MediaView extends View {
 25: /**
 26:  * Holds known mime type mappings
 27:  *
 28:  * @var array
 29:  * @access public
 30:  */
 31:     var $mimeType = array('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', 'dxr' => 'application/x-director',
 37:                                 'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
 38:                                 'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
 39:                                 'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
 40:                                 'hqx' => 'application/mac-binhex40', 'ips' => 'application/x-ipscript', 'ipx' => 'application/x-ipix',
 41:                                 'js' => 'application/x-javascript', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream',
 42:                                 'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream', 'man' => 'application/x-troff-man',
 43:                                 'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif', 'ms' => 'application/x-troff-ms',
 44:                                 'nc' => 'application/x-netcdf', 'oda' => 'application/oda', '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', 'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
 59:                                 'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
 60:                                 'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
 61:                                 'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
 62:                                 'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
 63:                                 'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
 64:                                 'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
 65:                                 'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
 66:                                 'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
 67:                                 'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
 68:                                 'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
 69:                                 'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
 70:                                 'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
 71:                                 'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
 72:                                 'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
 73:                                 'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
 74:                                 'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
 75:                                 'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
 76:                                 'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
 77:                                 'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
 78:                                 'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
 79:                                 'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
 80:                                 'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb');
 81: /**
 82:  * Holds headers sent to browser before rendering media
 83:  *
 84:  * @var array
 85:  * @access protected
 86:  */
 87:     var $_headers = array();
 88: /**
 89:  * Constructor
 90:  *
 91:  * @param object $controller
 92:  */
 93:     function __construct(&$controller) {
 94:         parent::__construct($controller);
 95:     }
 96: /**
 97:  * Display or download the given file
 98:  *
 99:  * @return unknown
100:  */
101:     function render() {
102:         $name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
103:         extract($this->viewVars, EXTR_OVERWRITE);
104: 
105:         if ($size) {
106:             $id = $id . '_' . $size;
107:         }
108: 
109:         if (is_dir($path)) {
110:             $path = $path . $id;
111:         } else {
112:             $path = APP . $path . $id;
113:         }
114: 
115:         if (!file_exists($path)) {
116:             header('Content-Type: text/html');
117:             $this->cakeError('error404');
118:         }
119: 
120:         if (is_null($name)) {
121:             $name = $id;
122:         }
123: 
124:         if (is_array($mimeType)) {
125:             $this->mimeType = array_merge($this->mimeType, $mimeType);
126:         }
127: 
128:         if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
129:             $chunkSize = 8192;
130:             $buffer = '';
131:             $fileSize = @filesize($path);
132:             $handle = fopen($path, 'rb');
133: 
134:             if ($handle === false) {
135:                 return false;
136:             }
137:             if (!empty($modified)) {
138:                 $modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
139:             } else {
140:                 $modified = gmdate('D, d M Y H:i:s') . ' GMT';
141:             }
142: 
143:             if ($download) {
144:                 $contentTypes = array('application/octet-stream');
145:                 $agent = env('HTTP_USER_AGENT');
146: 
147:                 if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
148:                     $contentTypes[0] = 'application/octetstream';
149:                 } else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
150:                     $contentTypes[0] = 'application/force-download';
151:                     array_merge($contentTypes, array(
152:                         'application/octet-stream',
153:                         'application/download'
154:                     ));
155:                 }
156:                 foreach($contentTypes as $contentType) {
157:                     $this->_header('Content-Type: ' . $contentType);
158:                 }
159:                 $this->_header(array(
160:                     'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
161:                     'Expires: 0',
162:                     'Accept-Ranges: bytes',
163:                     'Cache-Control: private' => false,
164:                     'Pragma: private'));
165: 
166:                 $httpRange = env('HTTP_RANGE');
167:                 if (isset($httpRange)) {
168:                     list($toss, $range) = explode('=', $httpRange);
169: 
170:                     $size = $fileSize - 1;
171:                     $length = $fileSize - $range;
172: 
173:                     $this->_header(array(
174:                         'HTTP/1.1 206 Partial Content',
175:                         'Content-Length: ' . $length,
176:                         'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
177: 
178:                     fseek($handle, $range);
179:                 } else {
180:                     $this->_header('Content-Length: ' . $fileSize);
181:                 }
182:             } else {
183:                 $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
184:                 if ($cache) {
185:                     if (!is_numeric($cache)) {
186:                         $cache = strtotime($cache) - time();
187:                     }
188:                     $this->_header(array(
189:                         'Cache-Control: max-age=' . $cache,
190:                         'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
191:                         'Pragma: cache'));
192:                 } else {
193:                     $this->_header(array(
194:                         'Cache-Control: must-revalidate, post-check=0, pre-check=0',
195:                         'Pragma: no-cache'));
196:                 }
197:                 $this->_header(array(
198:                     'Last-Modified: ' . $modified,
199:                     'Content-Type: ' . $this->mimeType[$extension],
200:                     'Content-Length: ' . $fileSize));
201:             }
202:             $this->_output();
203:             @ob_end_clean();
204: 
205:             while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
206:                 set_time_limit(0);
207:                 $buffer = fread($handle, $chunkSize);
208:                 echo $buffer;
209:                 @flush();
210:                 @ob_flush();
211:             }
212:             fclose($handle);
213:             exit(0);
214:         }
215:         return false;
216:     }
217: /**
218:  * Method to set headers
219:  * @param mixed $header
220:  * @param boolean $boolean
221:  * @access protected
222:  */
223:     function _header($header, $boolean = true) {
224:         if (is_array($header)) {
225:             foreach ($header as $string => $boolean) {
226:                 if (is_numeric($string)) {
227:                     $this->_headers[] = array($boolean => true);
228:                 } else {
229:                     $this->_headers[] = array($string => $boolean);
230:                 }
231:             }
232:             return;
233:         }
234:         $this->_headers[] = array($header => $boolean);
235:         return;
236:     }
237: /**
238:  * Method to output headers
239:  * @access protected
240:  */
241:     function _output() {
242:         foreach ($this->_headers as $key => $value) {
243:             $header = key($value);
244:             header($header, $value[$header]);
245:         }
246:     }
247: }
248: ?>
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs