2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2008-10-24 01:12:09 +00:00
|
|
|
* Methods to display or download any type of file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5714
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2010-12-03 23:07:21 +00:00
|
|
|
App::uses('View', 'View');
|
2010-12-15 05:50:02 +00:00
|
|
|
App::uses('CakeRequest', 'Network');
|
2009-12-01 14:50:27 +00:00
|
|
|
|
2010-12-23 02:49:53 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Media View provides a custom view implementation for sending files to visitors. Its great
|
2010-12-23 02:49:53 +00:00
|
|
|
* for making the response of a controller action be a file that is saved somewhere on the filesystem.
|
|
|
|
*
|
|
|
|
* An example use comes from the CakePHP internals. MediaView is used to serve plugin and theme assets,
|
2012-12-22 22:48:15 +00:00
|
|
|
* as they are not normally accessible from an application's webroot. Unlike other views, MediaView
|
2010-12-23 02:49:53 +00:00
|
|
|
* uses several viewVars that have special meaning:
|
|
|
|
*
|
|
|
|
* - `id` The filename on the server's filesystem, including extension.
|
|
|
|
* - `name` The filename that will be sent to the user, specified without the extension.
|
2012-12-22 22:48:15 +00:00
|
|
|
* - `download` Set to true to set a `Content-Disposition` header. This is ideal for file downloads.
|
2010-12-23 02:49:53 +00:00
|
|
|
* - `path` The absolute path, including the trailing / on the server's filesystem to `id`.
|
|
|
|
* - `mimeType` The mime type of the file if CakeResponse doesn't know about it.
|
2012-02-07 19:38:04 +00:00
|
|
|
* Must be an associative array with extension as key and mime type as value eg. array('ini' => 'text/plain')
|
2010-12-23 02:49:53 +00:00
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2010-12-23 02:49:53 +00:00
|
|
|
* class ExampleController extends AppController {
|
2012-02-23 13:38:02 +00:00
|
|
|
* public function download() {
|
2011-03-09 22:24:20 +00:00
|
|
|
* $this->viewClass = 'Media';
|
2010-12-23 02:49:53 +00:00
|
|
|
* $params = array(
|
|
|
|
* 'id' => 'example.zip',
|
|
|
|
* 'name' => 'example',
|
|
|
|
* 'download' => true,
|
|
|
|
* 'extension' => 'zip',
|
|
|
|
* 'path' => APP . 'files' . DS
|
|
|
|
* );
|
|
|
|
* $this->set($params);
|
|
|
|
* }
|
|
|
|
* }
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2010-12-23 02:49:53 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Deprecated since version 2.3, use CakeResponse::file() instead
|
2010-12-23 02:49:53 +00:00
|
|
|
*/
|
2009-12-01 14:50:27 +00:00
|
|
|
class MediaView extends View {
|
2012-03-03 22:10:12 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-24 01:12:09 +00:00
|
|
|
* Display or download the given file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-30 23:17:20 +00:00
|
|
|
* @param string $view Not used
|
|
|
|
* @param string $layout Not used
|
2014-02-05 21:11:36 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-07-30 23:17:20 +00:00
|
|
|
public function render($view = null, $layout = null) {
|
2015-08-21 10:35:49 +00:00
|
|
|
$name = $extension = $download = $id = $modified = $path = $cache = $mimeType = $compress = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($this->viewVars, EXTR_OVERWRITE);
|
|
|
|
|
2012-09-03 11:16:40 +00:00
|
|
|
$path = $path . $id;
|
2009-03-19 21:10:13 +00:00
|
|
|
|
2008-10-24 01:12:09 +00:00
|
|
|
if (is_array($mimeType)) {
|
2010-10-03 21:10:54 +00:00
|
|
|
$this->response->type($mimeType);
|
2008-10-24 01:12:09 +00:00
|
|
|
}
|
2009-03-19 21:10:13 +00:00
|
|
|
|
2012-07-10 20:03:40 +00:00
|
|
|
if ($cache) {
|
2010-10-04 00:04:37 +00:00
|
|
|
if (!empty($modified) && !is_numeric($modified)) {
|
|
|
|
$modified = strtotime($modified, time());
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-10-04 00:04:37 +00:00
|
|
|
$modified = time();
|
|
|
|
}
|
2012-07-10 20:03:40 +00:00
|
|
|
$this->response->cache($modified, $cache);
|
|
|
|
} else {
|
|
|
|
$this->response->disableCache();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-04 00:04:37 +00:00
|
|
|
|
2012-07-10 20:03:40 +00:00
|
|
|
if ($name !== null) {
|
2015-08-21 10:35:49 +00:00
|
|
|
if (empty($extension)) {
|
|
|
|
$extension = pathinfo($id, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
if (!empty($extension)) {
|
|
|
|
$name .= '.' . $extension;
|
|
|
|
}
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2012-07-10 20:03:40 +00:00
|
|
|
$this->response->file($path, compact('name', 'download'));
|
2010-04-13 20:04:13 +00:00
|
|
|
|
2012-07-10 20:03:40 +00:00
|
|
|
if ($compress) {
|
|
|
|
$this->response->compress();
|
|
|
|
}
|
2010-04-13 20:04:13 +00:00
|
|
|
}
|
2012-03-03 22:10:12 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|