cakephp2-php8/lib/Cake/View/MediaView.php

102 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
* Methods to display or download any type of file
*
2010-10-03 16:38:58 +00:00
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @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
* @package Cake.View
* @since CakePHP(tm) v 1.2.0.5714
2009-11-06 06:51:51 +00:00
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('View', 'View');
App::uses('CakeRequest', 'Network');
/**
2012-12-22 22:48:15 +00:00
* Media View provides a custom view implementation for sending files to visitors. Its great
* 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
* 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.
* - `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.
* Must be an associative array with extension as key and mime type as value eg. array('ini' => 'text/plain')
*
* ### Usage
*
* {{{
* class ExampleController extends AppController {
2012-02-23 13:38:02 +00:00
* public function download() {
* $this->viewClass = 'Media';
* $params = array(
* 'id' => 'example.zip',
* 'name' => 'example',
* 'download' => true,
* 'extension' => 'zip',
* 'path' => APP . 'files' . DS
* );
* $this->set($params);
* }
* }
* }}}
*
* @package Cake.View
2012-12-10 20:13:10 +00:00
* @deprecated Deprecated since version 2.3, use CakeResponse::file() instead
*/
class MediaView extends View {
2012-03-03 22:10:12 +00:00
/**
* Display or download the given file
*
* @param string $view Not used
* @param string $layout Not used
* @return boolean
*/
public function render($view = null, $layout = null) {
$name = $download = $id = $modified = $path = $cache = $mimeType = $compress = null;
extract($this->viewVars, EXTR_OVERWRITE);
2012-09-03 11:16:40 +00:00
$path = $path . $id;
2009-03-19 21:10:13 +00:00
if (is_array($mimeType)) {
$this->response->type($mimeType);
}
2009-03-19 21:10:13 +00:00
if ($cache) {
if (!empty($modified) && !is_numeric($modified)) {
$modified = strtotime($modified, time());
} else {
$modified = time();
}
$this->response->cache($modified, $cache);
} else {
$this->response->disableCache();
}
if ($name !== null) {
$name .= '.' . pathinfo($id, PATHINFO_EXTENSION);
2009-03-19 21:10:13 +00:00
}
$this->response->file($path, compact('name', 'download'));
if ($compress) {
$this->response->compress();
}
$this->response->send();
return true;
}
2012-03-03 22:10:12 +00:00
}